Merge branch 'drm-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / core / skbuff.c
1 /*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
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
22 *
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).
28 *
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.
33 */
34
35 /*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
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>
58
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>
64
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
67
68 #include "kmap_skb.h"
69
70 static struct kmem_cache *skbuff_head_cache __read_mostly;
71 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
72
73 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
74 struct pipe_buffer *buf)
75 {
76 struct sk_buff *skb = (struct sk_buff *) buf->private;
77
78 kfree_skb(skb);
79 }
80
81 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
82 struct pipe_buffer *buf)
83 {
84 struct sk_buff *skb = (struct sk_buff *) buf->private;
85
86 skb_get(skb);
87 }
88
89 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
91 {
92 return 1;
93 }
94
95
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,
105 };
106
107 /*
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
110 * reliable.
111 */
112
113 /**
114 * skb_over_panic - private function
115 * @skb: buffer
116 * @sz: size
117 * @here: address
118 *
119 * Out of line support code for skb_put(). Not user callable.
120 */
121 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
122 {
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();
129 }
130
131 /**
132 * skb_under_panic - private function
133 * @skb: buffer
134 * @sz: size
135 * @here: address
136 *
137 * Out of line support code for skb_push(). Not user callable.
138 */
139
140 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
141 {
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();
148 }
149
150 void skb_truesize_bug(struct sk_buff *skb)
151 {
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));
155 }
156 EXPORT_SYMBOL(skb_truesize_bug);
157
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.
161 *
162 */
163
164 /**
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
171 *
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.
175 *
176 * Buffers may only be allocated from interrupts using a @gfp_mask of
177 * %GFP_ATOMIC.
178 */
179 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
180 int fclone, int node)
181 {
182 struct kmem_cache *cache;
183 struct skb_shared_info *shinfo;
184 struct sk_buff *skb;
185 u8 *data;
186
187 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
188
189 /* Get the HEAD */
190 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
191 if (!skb)
192 goto out;
193
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;
199
200 /*
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!
204 */
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;
221
222 if (fclone) {
223 struct sk_buff *child = skb + 1;
224 atomic_t *fclone_ref = (atomic_t *) (child + 1);
225
226 skb->fclone = SKB_FCLONE_ORIG;
227 atomic_set(fclone_ref, 1);
228
229 child->fclone = SKB_FCLONE_UNAVAILABLE;
230 }
231 out:
232 return skb;
233 nodata:
234 kmem_cache_free(cache, skb);
235 skb = NULL;
236 goto out;
237 }
238
239 /**
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
244 *
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.
249 *
250 * %NULL is returned if there is no free memory.
251 */
252 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
253 unsigned int length, gfp_t gfp_mask)
254 {
255 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
256 struct sk_buff *skb;
257
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;
262 }
263 return skb;
264 }
265
266 /**
267 * dev_alloc_skb - allocate an skbuff for receiving
268 * @length: length to allocate
269 *
270 * Allocate a new &sk_buff and assign it a usage count of one. The
271 * buffer has unspecified headroom built in. Users should allocate
272 * the headroom they think they need without accounting for the
273 * built in space. The built in space is used for optimisations.
274 *
275 * %NULL is returned if there is no free memory. Although this function
276 * allocates memory it can be called from an interrupt.
277 */
278 struct sk_buff *dev_alloc_skb(unsigned int length)
279 {
280 /*
281 * There is more code here than it seems:
282 * __dev_alloc_skb is an inline
283 */
284 return __dev_alloc_skb(length, GFP_ATOMIC);
285 }
286 EXPORT_SYMBOL(dev_alloc_skb);
287
288 static void skb_drop_list(struct sk_buff **listp)
289 {
290 struct sk_buff *list = *listp;
291
292 *listp = NULL;
293
294 do {
295 struct sk_buff *this = list;
296 list = list->next;
297 kfree_skb(this);
298 } while (list);
299 }
300
301 static inline void skb_drop_fraglist(struct sk_buff *skb)
302 {
303 skb_drop_list(&skb_shinfo(skb)->frag_list);
304 }
305
306 static void skb_clone_fraglist(struct sk_buff *skb)
307 {
308 struct sk_buff *list;
309
310 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
311 skb_get(list);
312 }
313
314 static void skb_release_data(struct sk_buff *skb)
315 {
316 if (!skb->cloned ||
317 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
318 &skb_shinfo(skb)->dataref)) {
319 if (skb_shinfo(skb)->nr_frags) {
320 int i;
321 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
322 put_page(skb_shinfo(skb)->frags[i].page);
323 }
324
325 if (skb_shinfo(skb)->frag_list)
326 skb_drop_fraglist(skb);
327
328 kfree(skb->head);
329 }
330 }
331
332 /*
333 * Free an skbuff by memory without cleaning the state.
334 */
335 static void kfree_skbmem(struct sk_buff *skb)
336 {
337 struct sk_buff *other;
338 atomic_t *fclone_ref;
339
340 switch (skb->fclone) {
341 case SKB_FCLONE_UNAVAILABLE:
342 kmem_cache_free(skbuff_head_cache, skb);
343 break;
344
345 case SKB_FCLONE_ORIG:
346 fclone_ref = (atomic_t *) (skb + 2);
347 if (atomic_dec_and_test(fclone_ref))
348 kmem_cache_free(skbuff_fclone_cache, skb);
349 break;
350
351 case SKB_FCLONE_CLONE:
352 fclone_ref = (atomic_t *) (skb + 1);
353 other = skb - 1;
354
355 /* The clone portion is available for
356 * fast-cloning again.
357 */
358 skb->fclone = SKB_FCLONE_UNAVAILABLE;
359
360 if (atomic_dec_and_test(fclone_ref))
361 kmem_cache_free(skbuff_fclone_cache, other);
362 break;
363 }
364 }
365
366 /* Free everything but the sk_buff shell. */
367 static void skb_release_all(struct sk_buff *skb)
368 {
369 dst_release(skb->dst);
370 #ifdef CONFIG_XFRM
371 secpath_put(skb->sp);
372 #endif
373 if (skb->destructor) {
374 WARN_ON(in_irq());
375 skb->destructor(skb);
376 }
377 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
378 nf_conntrack_put(skb->nfct);
379 nf_conntrack_put_reasm(skb->nfct_reasm);
380 #endif
381 #ifdef CONFIG_BRIDGE_NETFILTER
382 nf_bridge_put(skb->nf_bridge);
383 #endif
384 /* XXX: IS this still necessary? - JHS */
385 #ifdef CONFIG_NET_SCHED
386 skb->tc_index = 0;
387 #ifdef CONFIG_NET_CLS_ACT
388 skb->tc_verd = 0;
389 #endif
390 #endif
391 skb_release_data(skb);
392 }
393
394 /**
395 * __kfree_skb - private function
396 * @skb: buffer
397 *
398 * Free an sk_buff. Release anything attached to the buffer.
399 * Clean the state. This is an internal helper function. Users should
400 * always call kfree_skb
401 */
402
403 void __kfree_skb(struct sk_buff *skb)
404 {
405 skb_release_all(skb);
406 kfree_skbmem(skb);
407 }
408
409 /**
410 * kfree_skb - free an sk_buff
411 * @skb: buffer to free
412 *
413 * Drop a reference to the buffer and free it if the usage count has
414 * hit zero.
415 */
416 void kfree_skb(struct sk_buff *skb)
417 {
418 if (unlikely(!skb))
419 return;
420 if (likely(atomic_read(&skb->users) == 1))
421 smp_rmb();
422 else if (likely(!atomic_dec_and_test(&skb->users)))
423 return;
424 __kfree_skb(skb);
425 }
426
427 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
428 {
429 new->tstamp = old->tstamp;
430 new->dev = old->dev;
431 new->transport_header = old->transport_header;
432 new->network_header = old->network_header;
433 new->mac_header = old->mac_header;
434 new->dst = dst_clone(old->dst);
435 #ifdef CONFIG_INET
436 new->sp = secpath_get(old->sp);
437 #endif
438 memcpy(new->cb, old->cb, sizeof(old->cb));
439 new->csum_start = old->csum_start;
440 new->csum_offset = old->csum_offset;
441 new->local_df = old->local_df;
442 new->pkt_type = old->pkt_type;
443 new->ip_summed = old->ip_summed;
444 skb_copy_queue_mapping(new, old);
445 new->priority = old->priority;
446 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
447 new->ipvs_property = old->ipvs_property;
448 #endif
449 new->protocol = old->protocol;
450 new->mark = old->mark;
451 __nf_copy(new, old);
452 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
453 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
454 new->nf_trace = old->nf_trace;
455 #endif
456 #ifdef CONFIG_NET_SCHED
457 new->tc_index = old->tc_index;
458 #ifdef CONFIG_NET_CLS_ACT
459 new->tc_verd = old->tc_verd;
460 #endif
461 #endif
462 new->vlan_tci = old->vlan_tci;
463
464 skb_copy_secmark(new, old);
465 }
466
467 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
468 {
469 #define C(x) n->x = skb->x
470
471 n->next = n->prev = NULL;
472 n->sk = NULL;
473 __copy_skb_header(n, skb);
474
475 C(len);
476 C(data_len);
477 C(mac_len);
478 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
479 n->cloned = 1;
480 n->nohdr = 0;
481 n->destructor = NULL;
482 C(iif);
483 C(tail);
484 C(end);
485 C(head);
486 C(data);
487 C(truesize);
488 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
489 C(do_not_encrypt);
490 #endif
491 atomic_set(&n->users, 1);
492
493 atomic_inc(&(skb_shinfo(skb)->dataref));
494 skb->cloned = 1;
495
496 return n;
497 #undef C
498 }
499
500 /**
501 * skb_morph - morph one skb into another
502 * @dst: the skb to receive the contents
503 * @src: the skb to supply the contents
504 *
505 * This is identical to skb_clone except that the target skb is
506 * supplied by the user.
507 *
508 * The target skb is returned upon exit.
509 */
510 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
511 {
512 skb_release_all(dst);
513 return __skb_clone(dst, src);
514 }
515 EXPORT_SYMBOL_GPL(skb_morph);
516
517 /**
518 * skb_clone - duplicate an sk_buff
519 * @skb: buffer to clone
520 * @gfp_mask: allocation priority
521 *
522 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
523 * copies share the same packet data but not structure. The new
524 * buffer has a reference count of 1. If the allocation fails the
525 * function returns %NULL otherwise the new buffer is returned.
526 *
527 * If this function is called from an interrupt gfp_mask() must be
528 * %GFP_ATOMIC.
529 */
530
531 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
532 {
533 struct sk_buff *n;
534
535 n = skb + 1;
536 if (skb->fclone == SKB_FCLONE_ORIG &&
537 n->fclone == SKB_FCLONE_UNAVAILABLE) {
538 atomic_t *fclone_ref = (atomic_t *) (n + 1);
539 n->fclone = SKB_FCLONE_CLONE;
540 atomic_inc(fclone_ref);
541 } else {
542 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
543 if (!n)
544 return NULL;
545 n->fclone = SKB_FCLONE_UNAVAILABLE;
546 }
547
548 return __skb_clone(n, skb);
549 }
550
551 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
552 {
553 #ifndef NET_SKBUFF_DATA_USES_OFFSET
554 /*
555 * Shift between the two data areas in bytes
556 */
557 unsigned long offset = new->data - old->data;
558 #endif
559
560 __copy_skb_header(new, old);
561
562 #ifndef NET_SKBUFF_DATA_USES_OFFSET
563 /* {transport,network,mac}_header are relative to skb->head */
564 new->transport_header += offset;
565 new->network_header += offset;
566 new->mac_header += offset;
567 #endif
568 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
569 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
570 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
571 }
572
573 /**
574 * skb_copy - create private copy of an sk_buff
575 * @skb: buffer to copy
576 * @gfp_mask: allocation priority
577 *
578 * Make a copy of both an &sk_buff and its data. This is used when the
579 * caller wishes to modify the data and needs a private copy of the
580 * data to alter. Returns %NULL on failure or the pointer to the buffer
581 * on success. The returned buffer has a reference count of 1.
582 *
583 * As by-product this function converts non-linear &sk_buff to linear
584 * one, so that &sk_buff becomes completely private and caller is allowed
585 * to modify all the data of returned buffer. This means that this
586 * function is not recommended for use in circumstances when only
587 * header is going to be modified. Use pskb_copy() instead.
588 */
589
590 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
591 {
592 int headerlen = skb->data - skb->head;
593 /*
594 * Allocate the copy buffer
595 */
596 struct sk_buff *n;
597 #ifdef NET_SKBUFF_DATA_USES_OFFSET
598 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
599 #else
600 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
601 #endif
602 if (!n)
603 return NULL;
604
605 /* Set the data pointer */
606 skb_reserve(n, headerlen);
607 /* Set the tail pointer and length */
608 skb_put(n, skb->len);
609
610 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
611 BUG();
612
613 copy_skb_header(n, skb);
614 return n;
615 }
616
617
618 /**
619 * pskb_copy - create copy of an sk_buff with private head.
620 * @skb: buffer to copy
621 * @gfp_mask: allocation priority
622 *
623 * Make a copy of both an &sk_buff and part of its data, located
624 * in header. Fragmented data remain shared. This is used when
625 * the caller wishes to modify only header of &sk_buff and needs
626 * private copy of the header to alter. Returns %NULL on failure
627 * or the pointer to the buffer on success.
628 * The returned buffer has a reference count of 1.
629 */
630
631 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
632 {
633 /*
634 * Allocate the copy buffer
635 */
636 struct sk_buff *n;
637 #ifdef NET_SKBUFF_DATA_USES_OFFSET
638 n = alloc_skb(skb->end, gfp_mask);
639 #else
640 n = alloc_skb(skb->end - skb->head, gfp_mask);
641 #endif
642 if (!n)
643 goto out;
644
645 /* Set the data pointer */
646 skb_reserve(n, skb->data - skb->head);
647 /* Set the tail pointer and length */
648 skb_put(n, skb_headlen(skb));
649 /* Copy the bytes */
650 skb_copy_from_linear_data(skb, n->data, n->len);
651
652 n->truesize += skb->data_len;
653 n->data_len = skb->data_len;
654 n->len = skb->len;
655
656 if (skb_shinfo(skb)->nr_frags) {
657 int i;
658
659 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
660 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
661 get_page(skb_shinfo(n)->frags[i].page);
662 }
663 skb_shinfo(n)->nr_frags = i;
664 }
665
666 if (skb_shinfo(skb)->frag_list) {
667 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
668 skb_clone_fraglist(n);
669 }
670
671 copy_skb_header(n, skb);
672 out:
673 return n;
674 }
675
676 /**
677 * pskb_expand_head - reallocate header of &sk_buff
678 * @skb: buffer to reallocate
679 * @nhead: room to add at head
680 * @ntail: room to add at tail
681 * @gfp_mask: allocation priority
682 *
683 * Expands (or creates identical copy, if &nhead and &ntail are zero)
684 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
685 * reference count of 1. Returns zero in the case of success or error,
686 * if expansion failed. In the last case, &sk_buff is not changed.
687 *
688 * All the pointers pointing into skb header may change and must be
689 * reloaded after call to this function.
690 */
691
692 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
693 gfp_t gfp_mask)
694 {
695 int i;
696 u8 *data;
697 #ifdef NET_SKBUFF_DATA_USES_OFFSET
698 int size = nhead + skb->end + ntail;
699 #else
700 int size = nhead + (skb->end - skb->head) + ntail;
701 #endif
702 long off;
703
704 if (skb_shared(skb))
705 BUG();
706
707 size = SKB_DATA_ALIGN(size);
708
709 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
710 if (!data)
711 goto nodata;
712
713 /* Copy only real data... and, alas, header. This should be
714 * optimized for the cases when header is void. */
715 #ifdef NET_SKBUFF_DATA_USES_OFFSET
716 memcpy(data + nhead, skb->head, skb->tail);
717 #else
718 memcpy(data + nhead, skb->head, skb->tail - skb->head);
719 #endif
720 memcpy(data + size, skb_end_pointer(skb),
721 sizeof(struct skb_shared_info));
722
723 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
724 get_page(skb_shinfo(skb)->frags[i].page);
725
726 if (skb_shinfo(skb)->frag_list)
727 skb_clone_fraglist(skb);
728
729 skb_release_data(skb);
730
731 off = (data + nhead) - skb->head;
732
733 skb->head = data;
734 skb->data += off;
735 #ifdef NET_SKBUFF_DATA_USES_OFFSET
736 skb->end = size;
737 off = nhead;
738 #else
739 skb->end = skb->head + size;
740 #endif
741 /* {transport,network,mac}_header and tail are relative to skb->head */
742 skb->tail += off;
743 skb->transport_header += off;
744 skb->network_header += off;
745 skb->mac_header += off;
746 skb->csum_start += nhead;
747 skb->cloned = 0;
748 skb->hdr_len = 0;
749 skb->nohdr = 0;
750 atomic_set(&skb_shinfo(skb)->dataref, 1);
751 return 0;
752
753 nodata:
754 return -ENOMEM;
755 }
756
757 /* Make private copy of skb with writable head and some headroom */
758
759 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
760 {
761 struct sk_buff *skb2;
762 int delta = headroom - skb_headroom(skb);
763
764 if (delta <= 0)
765 skb2 = pskb_copy(skb, GFP_ATOMIC);
766 else {
767 skb2 = skb_clone(skb, GFP_ATOMIC);
768 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
769 GFP_ATOMIC)) {
770 kfree_skb(skb2);
771 skb2 = NULL;
772 }
773 }
774 return skb2;
775 }
776
777
778 /**
779 * skb_copy_expand - copy and expand sk_buff
780 * @skb: buffer to copy
781 * @newheadroom: new free bytes at head
782 * @newtailroom: new free bytes at tail
783 * @gfp_mask: allocation priority
784 *
785 * Make a copy of both an &sk_buff and its data and while doing so
786 * allocate additional space.
787 *
788 * This is used when the caller wishes to modify the data and needs a
789 * private copy of the data to alter as well as more space for new fields.
790 * Returns %NULL on failure or the pointer to the buffer
791 * on success. The returned buffer has a reference count of 1.
792 *
793 * You must pass %GFP_ATOMIC as the allocation priority if this function
794 * is called from an interrupt.
795 */
796 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
797 int newheadroom, int newtailroom,
798 gfp_t gfp_mask)
799 {
800 /*
801 * Allocate the copy buffer
802 */
803 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
804 gfp_mask);
805 int oldheadroom = skb_headroom(skb);
806 int head_copy_len, head_copy_off;
807 int off;
808
809 if (!n)
810 return NULL;
811
812 skb_reserve(n, newheadroom);
813
814 /* Set the tail pointer and length */
815 skb_put(n, skb->len);
816
817 head_copy_len = oldheadroom;
818 head_copy_off = 0;
819 if (newheadroom <= head_copy_len)
820 head_copy_len = newheadroom;
821 else
822 head_copy_off = newheadroom - head_copy_len;
823
824 /* Copy the linear header and data. */
825 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
826 skb->len + head_copy_len))
827 BUG();
828
829 copy_skb_header(n, skb);
830
831 off = newheadroom - oldheadroom;
832 n->csum_start += off;
833 #ifdef NET_SKBUFF_DATA_USES_OFFSET
834 n->transport_header += off;
835 n->network_header += off;
836 n->mac_header += off;
837 #endif
838
839 return n;
840 }
841
842 /**
843 * skb_pad - zero pad the tail of an skb
844 * @skb: buffer to pad
845 * @pad: space to pad
846 *
847 * Ensure that a buffer is followed by a padding area that is zero
848 * filled. Used by network drivers which may DMA or transfer data
849 * beyond the buffer end onto the wire.
850 *
851 * May return error in out of memory cases. The skb is freed on error.
852 */
853
854 int skb_pad(struct sk_buff *skb, int pad)
855 {
856 int err;
857 int ntail;
858
859 /* If the skbuff is non linear tailroom is always zero.. */
860 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
861 memset(skb->data+skb->len, 0, pad);
862 return 0;
863 }
864
865 ntail = skb->data_len + pad - (skb->end - skb->tail);
866 if (likely(skb_cloned(skb) || ntail > 0)) {
867 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
868 if (unlikely(err))
869 goto free_skb;
870 }
871
872 /* FIXME: The use of this function with non-linear skb's really needs
873 * to be audited.
874 */
875 err = skb_linearize(skb);
876 if (unlikely(err))
877 goto free_skb;
878
879 memset(skb->data + skb->len, 0, pad);
880 return 0;
881
882 free_skb:
883 kfree_skb(skb);
884 return err;
885 }
886
887 /**
888 * skb_put - add data to a buffer
889 * @skb: buffer to use
890 * @len: amount of data to add
891 *
892 * This function extends the used data area of the buffer. If this would
893 * exceed the total buffer size the kernel will panic. A pointer to the
894 * first byte of the extra data is returned.
895 */
896 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
897 {
898 unsigned char *tmp = skb_tail_pointer(skb);
899 SKB_LINEAR_ASSERT(skb);
900 skb->tail += len;
901 skb->len += len;
902 if (unlikely(skb->tail > skb->end))
903 skb_over_panic(skb, len, __builtin_return_address(0));
904 return tmp;
905 }
906 EXPORT_SYMBOL(skb_put);
907
908 /**
909 * skb_push - add data to the start of a buffer
910 * @skb: buffer to use
911 * @len: amount of data to add
912 *
913 * This function extends the used data area of the buffer at the buffer
914 * start. If this would exceed the total buffer headroom the kernel will
915 * panic. A pointer to the first byte of the extra data is returned.
916 */
917 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
918 {
919 skb->data -= len;
920 skb->len += len;
921 if (unlikely(skb->data<skb->head))
922 skb_under_panic(skb, len, __builtin_return_address(0));
923 return skb->data;
924 }
925 EXPORT_SYMBOL(skb_push);
926
927 /**
928 * skb_pull - remove data from the start of a buffer
929 * @skb: buffer to use
930 * @len: amount of data to remove
931 *
932 * This function removes data from the start of a buffer, returning
933 * the memory to the headroom. A pointer to the next data in the buffer
934 * is returned. Once the data has been pulled future pushes will overwrite
935 * the old data.
936 */
937 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
938 {
939 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
940 }
941 EXPORT_SYMBOL(skb_pull);
942
943 /**
944 * skb_trim - remove end from a buffer
945 * @skb: buffer to alter
946 * @len: new length
947 *
948 * Cut the length of a buffer down by removing data from the tail. If
949 * the buffer is already under the length specified it is not modified.
950 * The skb must be linear.
951 */
952 void skb_trim(struct sk_buff *skb, unsigned int len)
953 {
954 if (skb->len > len)
955 __skb_trim(skb, len);
956 }
957 EXPORT_SYMBOL(skb_trim);
958
959 /* Trims skb to length len. It can change skb pointers.
960 */
961
962 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
963 {
964 struct sk_buff **fragp;
965 struct sk_buff *frag;
966 int offset = skb_headlen(skb);
967 int nfrags = skb_shinfo(skb)->nr_frags;
968 int i;
969 int err;
970
971 if (skb_cloned(skb) &&
972 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
973 return err;
974
975 i = 0;
976 if (offset >= len)
977 goto drop_pages;
978
979 for (; i < nfrags; i++) {
980 int end = offset + skb_shinfo(skb)->frags[i].size;
981
982 if (end < len) {
983 offset = end;
984 continue;
985 }
986
987 skb_shinfo(skb)->frags[i++].size = len - offset;
988
989 drop_pages:
990 skb_shinfo(skb)->nr_frags = i;
991
992 for (; i < nfrags; i++)
993 put_page(skb_shinfo(skb)->frags[i].page);
994
995 if (skb_shinfo(skb)->frag_list)
996 skb_drop_fraglist(skb);
997 goto done;
998 }
999
1000 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1001 fragp = &frag->next) {
1002 int end = offset + frag->len;
1003
1004 if (skb_shared(frag)) {
1005 struct sk_buff *nfrag;
1006
1007 nfrag = skb_clone(frag, GFP_ATOMIC);
1008 if (unlikely(!nfrag))
1009 return -ENOMEM;
1010
1011 nfrag->next = frag->next;
1012 kfree_skb(frag);
1013 frag = nfrag;
1014 *fragp = frag;
1015 }
1016
1017 if (end < len) {
1018 offset = end;
1019 continue;
1020 }
1021
1022 if (end > len &&
1023 unlikely((err = pskb_trim(frag, len - offset))))
1024 return err;
1025
1026 if (frag->next)
1027 skb_drop_list(&frag->next);
1028 break;
1029 }
1030
1031 done:
1032 if (len > skb_headlen(skb)) {
1033 skb->data_len -= skb->len - len;
1034 skb->len = len;
1035 } else {
1036 skb->len = len;
1037 skb->data_len = 0;
1038 skb_set_tail_pointer(skb, len);
1039 }
1040
1041 return 0;
1042 }
1043
1044 /**
1045 * __pskb_pull_tail - advance tail of skb header
1046 * @skb: buffer to reallocate
1047 * @delta: number of bytes to advance tail
1048 *
1049 * The function makes a sense only on a fragmented &sk_buff,
1050 * it expands header moving its tail forward and copying necessary
1051 * data from fragmented part.
1052 *
1053 * &sk_buff MUST have reference count of 1.
1054 *
1055 * Returns %NULL (and &sk_buff does not change) if pull failed
1056 * or value of new tail of skb in the case of success.
1057 *
1058 * All the pointers pointing into skb header may change and must be
1059 * reloaded after call to this function.
1060 */
1061
1062 /* Moves tail of skb head forward, copying data from fragmented part,
1063 * when it is necessary.
1064 * 1. It may fail due to malloc failure.
1065 * 2. It may change skb pointers.
1066 *
1067 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1068 */
1069 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1070 {
1071 /* If skb has not enough free space at tail, get new one
1072 * plus 128 bytes for future expansions. If we have enough
1073 * room at tail, reallocate without expansion only if skb is cloned.
1074 */
1075 int i, k, eat = (skb->tail + delta) - skb->end;
1076
1077 if (eat > 0 || skb_cloned(skb)) {
1078 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1079 GFP_ATOMIC))
1080 return NULL;
1081 }
1082
1083 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1084 BUG();
1085
1086 /* Optimization: no fragments, no reasons to preestimate
1087 * size of pulled pages. Superb.
1088 */
1089 if (!skb_shinfo(skb)->frag_list)
1090 goto pull_pages;
1091
1092 /* Estimate size of pulled pages. */
1093 eat = delta;
1094 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1095 if (skb_shinfo(skb)->frags[i].size >= eat)
1096 goto pull_pages;
1097 eat -= skb_shinfo(skb)->frags[i].size;
1098 }
1099
1100 /* If we need update frag list, we are in troubles.
1101 * Certainly, it possible to add an offset to skb data,
1102 * but taking into account that pulling is expected to
1103 * be very rare operation, it is worth to fight against
1104 * further bloating skb head and crucify ourselves here instead.
1105 * Pure masohism, indeed. 8)8)
1106 */
1107 if (eat) {
1108 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1109 struct sk_buff *clone = NULL;
1110 struct sk_buff *insp = NULL;
1111
1112 do {
1113 BUG_ON(!list);
1114
1115 if (list->len <= eat) {
1116 /* Eaten as whole. */
1117 eat -= list->len;
1118 list = list->next;
1119 insp = list;
1120 } else {
1121 /* Eaten partially. */
1122
1123 if (skb_shared(list)) {
1124 /* Sucks! We need to fork list. :-( */
1125 clone = skb_clone(list, GFP_ATOMIC);
1126 if (!clone)
1127 return NULL;
1128 insp = list->next;
1129 list = clone;
1130 } else {
1131 /* This may be pulled without
1132 * problems. */
1133 insp = list;
1134 }
1135 if (!pskb_pull(list, eat)) {
1136 if (clone)
1137 kfree_skb(clone);
1138 return NULL;
1139 }
1140 break;
1141 }
1142 } while (eat);
1143
1144 /* Free pulled out fragments. */
1145 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1146 skb_shinfo(skb)->frag_list = list->next;
1147 kfree_skb(list);
1148 }
1149 /* And insert new clone at head. */
1150 if (clone) {
1151 clone->next = list;
1152 skb_shinfo(skb)->frag_list = clone;
1153 }
1154 }
1155 /* Success! Now we may commit changes to skb data. */
1156
1157 pull_pages:
1158 eat = delta;
1159 k = 0;
1160 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1161 if (skb_shinfo(skb)->frags[i].size <= eat) {
1162 put_page(skb_shinfo(skb)->frags[i].page);
1163 eat -= skb_shinfo(skb)->frags[i].size;
1164 } else {
1165 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1166 if (eat) {
1167 skb_shinfo(skb)->frags[k].page_offset += eat;
1168 skb_shinfo(skb)->frags[k].size -= eat;
1169 eat = 0;
1170 }
1171 k++;
1172 }
1173 }
1174 skb_shinfo(skb)->nr_frags = k;
1175
1176 skb->tail += delta;
1177 skb->data_len -= delta;
1178
1179 return skb_tail_pointer(skb);
1180 }
1181
1182 /* Copy some data bits from skb to kernel buffer. */
1183
1184 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1185 {
1186 int i, copy;
1187 int start = skb_headlen(skb);
1188
1189 if (offset > (int)skb->len - len)
1190 goto fault;
1191
1192 /* Copy header. */
1193 if ((copy = start - offset) > 0) {
1194 if (copy > len)
1195 copy = len;
1196 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1197 if ((len -= copy) == 0)
1198 return 0;
1199 offset += copy;
1200 to += copy;
1201 }
1202
1203 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1204 int end;
1205
1206 WARN_ON(start > offset + len);
1207
1208 end = start + skb_shinfo(skb)->frags[i].size;
1209 if ((copy = end - offset) > 0) {
1210 u8 *vaddr;
1211
1212 if (copy > len)
1213 copy = len;
1214
1215 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1216 memcpy(to,
1217 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1218 offset - start, copy);
1219 kunmap_skb_frag(vaddr);
1220
1221 if ((len -= copy) == 0)
1222 return 0;
1223 offset += copy;
1224 to += copy;
1225 }
1226 start = end;
1227 }
1228
1229 if (skb_shinfo(skb)->frag_list) {
1230 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1231
1232 for (; list; list = list->next) {
1233 int end;
1234
1235 WARN_ON(start > offset + len);
1236
1237 end = start + list->len;
1238 if ((copy = end - offset) > 0) {
1239 if (copy > len)
1240 copy = len;
1241 if (skb_copy_bits(list, offset - start,
1242 to, copy))
1243 goto fault;
1244 if ((len -= copy) == 0)
1245 return 0;
1246 offset += copy;
1247 to += copy;
1248 }
1249 start = end;
1250 }
1251 }
1252 if (!len)
1253 return 0;
1254
1255 fault:
1256 return -EFAULT;
1257 }
1258
1259 /*
1260 * Callback from splice_to_pipe(), if we need to release some pages
1261 * at the end of the spd in case we error'ed out in filling the pipe.
1262 */
1263 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1264 {
1265 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1266
1267 kfree_skb(skb);
1268 }
1269
1270 /*
1271 * Fill page/offset/length into spd, if it can hold more pages.
1272 */
1273 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1274 unsigned int len, unsigned int offset,
1275 struct sk_buff *skb)
1276 {
1277 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1278 return 1;
1279
1280 spd->pages[spd->nr_pages] = page;
1281 spd->partial[spd->nr_pages].len = len;
1282 spd->partial[spd->nr_pages].offset = offset;
1283 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1284 spd->nr_pages++;
1285 return 0;
1286 }
1287
1288 static inline void __segment_seek(struct page **page, unsigned int *poff,
1289 unsigned int *plen, unsigned int off)
1290 {
1291 *poff += off;
1292 *page += *poff / PAGE_SIZE;
1293 *poff = *poff % PAGE_SIZE;
1294 *plen -= off;
1295 }
1296
1297 static inline int __splice_segment(struct page *page, unsigned int poff,
1298 unsigned int plen, unsigned int *off,
1299 unsigned int *len, struct sk_buff *skb,
1300 struct splice_pipe_desc *spd)
1301 {
1302 if (!*len)
1303 return 1;
1304
1305 /* skip this segment if already processed */
1306 if (*off >= plen) {
1307 *off -= plen;
1308 return 0;
1309 }
1310
1311 /* ignore any bits we already processed */
1312 if (*off) {
1313 __segment_seek(&page, &poff, &plen, *off);
1314 *off = 0;
1315 }
1316
1317 do {
1318 unsigned int flen = min(*len, plen);
1319
1320 /* the linear region may spread across several pages */
1321 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1322
1323 if (spd_fill_page(spd, page, flen, poff, skb))
1324 return 1;
1325
1326 __segment_seek(&page, &poff, &plen, flen);
1327 *len -= flen;
1328
1329 } while (*len && plen);
1330
1331 return 0;
1332 }
1333
1334 /*
1335 * Map linear and fragment data from the skb to spd. It reports failure if the
1336 * pipe is full or if we already spliced the requested length.
1337 */
1338 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1339 unsigned int *len,
1340 struct splice_pipe_desc *spd)
1341 {
1342 int seg;
1343
1344 /*
1345 * map the linear part
1346 */
1347 if (__splice_segment(virt_to_page(skb->data),
1348 (unsigned long) skb->data & (PAGE_SIZE - 1),
1349 skb_headlen(skb),
1350 offset, len, skb, spd))
1351 return 1;
1352
1353 /*
1354 * then map the fragments
1355 */
1356 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1357 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1358
1359 if (__splice_segment(f->page, f->page_offset, f->size,
1360 offset, len, skb, spd))
1361 return 1;
1362 }
1363
1364 return 0;
1365 }
1366
1367 /*
1368 * Map data from the skb to a pipe. Should handle both the linear part,
1369 * the fragments, and the frag list. It does NOT handle frag lists within
1370 * the frag list, if such a thing exists. We'd probably need to recurse to
1371 * handle that cleanly.
1372 */
1373 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1374 struct pipe_inode_info *pipe, unsigned int tlen,
1375 unsigned int flags)
1376 {
1377 struct partial_page partial[PIPE_BUFFERS];
1378 struct page *pages[PIPE_BUFFERS];
1379 struct splice_pipe_desc spd = {
1380 .pages = pages,
1381 .partial = partial,
1382 .flags = flags,
1383 .ops = &sock_pipe_buf_ops,
1384 .spd_release = sock_spd_release,
1385 };
1386 struct sk_buff *skb;
1387
1388 /*
1389 * I'd love to avoid the clone here, but tcp_read_sock()
1390 * ignores reference counts and unconditonally kills the sk_buff
1391 * on return from the actor.
1392 */
1393 skb = skb_clone(__skb, GFP_KERNEL);
1394 if (unlikely(!skb))
1395 return -ENOMEM;
1396
1397 /*
1398 * __skb_splice_bits() only fails if the output has no room left,
1399 * so no point in going over the frag_list for the error case.
1400 */
1401 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1402 goto done;
1403 else if (!tlen)
1404 goto done;
1405
1406 /*
1407 * now see if we have a frag_list to map
1408 */
1409 if (skb_shinfo(skb)->frag_list) {
1410 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1411
1412 for (; list && tlen; list = list->next) {
1413 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1414 break;
1415 }
1416 }
1417
1418 done:
1419 /*
1420 * drop our reference to the clone, the pipe consumption will
1421 * drop the rest.
1422 */
1423 kfree_skb(skb);
1424
1425 if (spd.nr_pages) {
1426 int ret;
1427 struct sock *sk = __skb->sk;
1428
1429 /*
1430 * Drop the socket lock, otherwise we have reverse
1431 * locking dependencies between sk_lock and i_mutex
1432 * here as compared to sendfile(). We enter here
1433 * with the socket lock held, and splice_to_pipe() will
1434 * grab the pipe inode lock. For sendfile() emulation,
1435 * we call into ->sendpage() with the i_mutex lock held
1436 * and networking will grab the socket lock.
1437 */
1438 release_sock(sk);
1439 ret = splice_to_pipe(pipe, &spd);
1440 lock_sock(sk);
1441 return ret;
1442 }
1443
1444 return 0;
1445 }
1446
1447 /**
1448 * skb_store_bits - store bits from kernel buffer to skb
1449 * @skb: destination buffer
1450 * @offset: offset in destination
1451 * @from: source buffer
1452 * @len: number of bytes to copy
1453 *
1454 * Copy the specified number of bytes from the source buffer to the
1455 * destination skb. This function handles all the messy bits of
1456 * traversing fragment lists and such.
1457 */
1458
1459 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1460 {
1461 int i, copy;
1462 int start = skb_headlen(skb);
1463
1464 if (offset > (int)skb->len - len)
1465 goto fault;
1466
1467 if ((copy = start - offset) > 0) {
1468 if (copy > len)
1469 copy = len;
1470 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1471 if ((len -= copy) == 0)
1472 return 0;
1473 offset += copy;
1474 from += copy;
1475 }
1476
1477 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1478 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1479 int end;
1480
1481 WARN_ON(start > offset + len);
1482
1483 end = start + frag->size;
1484 if ((copy = end - offset) > 0) {
1485 u8 *vaddr;
1486
1487 if (copy > len)
1488 copy = len;
1489
1490 vaddr = kmap_skb_frag(frag);
1491 memcpy(vaddr + frag->page_offset + offset - start,
1492 from, copy);
1493 kunmap_skb_frag(vaddr);
1494
1495 if ((len -= copy) == 0)
1496 return 0;
1497 offset += copy;
1498 from += copy;
1499 }
1500 start = end;
1501 }
1502
1503 if (skb_shinfo(skb)->frag_list) {
1504 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1505
1506 for (; list; list = list->next) {
1507 int end;
1508
1509 WARN_ON(start > offset + len);
1510
1511 end = start + list->len;
1512 if ((copy = end - offset) > 0) {
1513 if (copy > len)
1514 copy = len;
1515 if (skb_store_bits(list, offset - start,
1516 from, copy))
1517 goto fault;
1518 if ((len -= copy) == 0)
1519 return 0;
1520 offset += copy;
1521 from += copy;
1522 }
1523 start = end;
1524 }
1525 }
1526 if (!len)
1527 return 0;
1528
1529 fault:
1530 return -EFAULT;
1531 }
1532
1533 EXPORT_SYMBOL(skb_store_bits);
1534
1535 /* Checksum skb data. */
1536
1537 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1538 int len, __wsum csum)
1539 {
1540 int start = skb_headlen(skb);
1541 int i, copy = start - offset;
1542 int pos = 0;
1543
1544 /* Checksum header. */
1545 if (copy > 0) {
1546 if (copy > len)
1547 copy = len;
1548 csum = csum_partial(skb->data + offset, copy, csum);
1549 if ((len -= copy) == 0)
1550 return csum;
1551 offset += copy;
1552 pos = copy;
1553 }
1554
1555 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1556 int end;
1557
1558 WARN_ON(start > offset + len);
1559
1560 end = start + skb_shinfo(skb)->frags[i].size;
1561 if ((copy = end - offset) > 0) {
1562 __wsum csum2;
1563 u8 *vaddr;
1564 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1565
1566 if (copy > len)
1567 copy = len;
1568 vaddr = kmap_skb_frag(frag);
1569 csum2 = csum_partial(vaddr + frag->page_offset +
1570 offset - start, copy, 0);
1571 kunmap_skb_frag(vaddr);
1572 csum = csum_block_add(csum, csum2, pos);
1573 if (!(len -= copy))
1574 return csum;
1575 offset += copy;
1576 pos += copy;
1577 }
1578 start = end;
1579 }
1580
1581 if (skb_shinfo(skb)->frag_list) {
1582 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1583
1584 for (; list; list = list->next) {
1585 int end;
1586
1587 WARN_ON(start > offset + len);
1588
1589 end = start + list->len;
1590 if ((copy = end - offset) > 0) {
1591 __wsum csum2;
1592 if (copy > len)
1593 copy = len;
1594 csum2 = skb_checksum(list, offset - start,
1595 copy, 0);
1596 csum = csum_block_add(csum, csum2, pos);
1597 if ((len -= copy) == 0)
1598 return csum;
1599 offset += copy;
1600 pos += copy;
1601 }
1602 start = end;
1603 }
1604 }
1605 BUG_ON(len);
1606
1607 return csum;
1608 }
1609
1610 /* Both of above in one bottle. */
1611
1612 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1613 u8 *to, int len, __wsum csum)
1614 {
1615 int start = skb_headlen(skb);
1616 int i, copy = start - offset;
1617 int pos = 0;
1618
1619 /* Copy header. */
1620 if (copy > 0) {
1621 if (copy > len)
1622 copy = len;
1623 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1624 copy, csum);
1625 if ((len -= copy) == 0)
1626 return csum;
1627 offset += copy;
1628 to += copy;
1629 pos = copy;
1630 }
1631
1632 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1633 int end;
1634
1635 WARN_ON(start > offset + len);
1636
1637 end = start + skb_shinfo(skb)->frags[i].size;
1638 if ((copy = end - offset) > 0) {
1639 __wsum csum2;
1640 u8 *vaddr;
1641 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1642
1643 if (copy > len)
1644 copy = len;
1645 vaddr = kmap_skb_frag(frag);
1646 csum2 = csum_partial_copy_nocheck(vaddr +
1647 frag->page_offset +
1648 offset - start, to,
1649 copy, 0);
1650 kunmap_skb_frag(vaddr);
1651 csum = csum_block_add(csum, csum2, pos);
1652 if (!(len -= copy))
1653 return csum;
1654 offset += copy;
1655 to += copy;
1656 pos += copy;
1657 }
1658 start = end;
1659 }
1660
1661 if (skb_shinfo(skb)->frag_list) {
1662 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1663
1664 for (; list; list = list->next) {
1665 __wsum csum2;
1666 int end;
1667
1668 WARN_ON(start > offset + len);
1669
1670 end = start + list->len;
1671 if ((copy = end - offset) > 0) {
1672 if (copy > len)
1673 copy = len;
1674 csum2 = skb_copy_and_csum_bits(list,
1675 offset - start,
1676 to, copy, 0);
1677 csum = csum_block_add(csum, csum2, pos);
1678 if ((len -= copy) == 0)
1679 return csum;
1680 offset += copy;
1681 to += copy;
1682 pos += copy;
1683 }
1684 start = end;
1685 }
1686 }
1687 BUG_ON(len);
1688 return csum;
1689 }
1690
1691 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1692 {
1693 __wsum csum;
1694 long csstart;
1695
1696 if (skb->ip_summed == CHECKSUM_PARTIAL)
1697 csstart = skb->csum_start - skb_headroom(skb);
1698 else
1699 csstart = skb_headlen(skb);
1700
1701 BUG_ON(csstart > skb_headlen(skb));
1702
1703 skb_copy_from_linear_data(skb, to, csstart);
1704
1705 csum = 0;
1706 if (csstart != skb->len)
1707 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1708 skb->len - csstart, 0);
1709
1710 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1711 long csstuff = csstart + skb->csum_offset;
1712
1713 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1714 }
1715 }
1716
1717 /**
1718 * skb_dequeue - remove from the head of the queue
1719 * @list: list to dequeue from
1720 *
1721 * Remove the head of the list. The list lock is taken so the function
1722 * may be used safely with other locking list functions. The head item is
1723 * returned or %NULL if the list is empty.
1724 */
1725
1726 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1727 {
1728 unsigned long flags;
1729 struct sk_buff *result;
1730
1731 spin_lock_irqsave(&list->lock, flags);
1732 result = __skb_dequeue(list);
1733 spin_unlock_irqrestore(&list->lock, flags);
1734 return result;
1735 }
1736
1737 /**
1738 * skb_dequeue_tail - remove from the tail of the queue
1739 * @list: list to dequeue from
1740 *
1741 * Remove the tail of the list. The list lock is taken so the function
1742 * may be used safely with other locking list functions. The tail item is
1743 * returned or %NULL if the list is empty.
1744 */
1745 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1746 {
1747 unsigned long flags;
1748 struct sk_buff *result;
1749
1750 spin_lock_irqsave(&list->lock, flags);
1751 result = __skb_dequeue_tail(list);
1752 spin_unlock_irqrestore(&list->lock, flags);
1753 return result;
1754 }
1755
1756 /**
1757 * skb_queue_purge - empty a list
1758 * @list: list to empty
1759 *
1760 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1761 * the list and one reference dropped. This function takes the list
1762 * lock and is atomic with respect to other list locking functions.
1763 */
1764 void skb_queue_purge(struct sk_buff_head *list)
1765 {
1766 struct sk_buff *skb;
1767 while ((skb = skb_dequeue(list)) != NULL)
1768 kfree_skb(skb);
1769 }
1770
1771 /**
1772 * skb_queue_head - queue a buffer at the list head
1773 * @list: list to use
1774 * @newsk: buffer to queue
1775 *
1776 * Queue a buffer at the start of the list. This function takes the
1777 * list lock and can be used safely with other locking &sk_buff functions
1778 * safely.
1779 *
1780 * A buffer cannot be placed on two lists at the same time.
1781 */
1782 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1783 {
1784 unsigned long flags;
1785
1786 spin_lock_irqsave(&list->lock, flags);
1787 __skb_queue_head(list, newsk);
1788 spin_unlock_irqrestore(&list->lock, flags);
1789 }
1790
1791 /**
1792 * skb_queue_tail - queue a buffer at the list tail
1793 * @list: list to use
1794 * @newsk: buffer to queue
1795 *
1796 * Queue a buffer at the tail of the list. This function takes the
1797 * list lock and can be used safely with other locking &sk_buff functions
1798 * safely.
1799 *
1800 * A buffer cannot be placed on two lists at the same time.
1801 */
1802 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1803 {
1804 unsigned long flags;
1805
1806 spin_lock_irqsave(&list->lock, flags);
1807 __skb_queue_tail(list, newsk);
1808 spin_unlock_irqrestore(&list->lock, flags);
1809 }
1810
1811 /**
1812 * skb_unlink - remove a buffer from a list
1813 * @skb: buffer to remove
1814 * @list: list to use
1815 *
1816 * Remove a packet from a list. The list locks are taken and this
1817 * function is atomic with respect to other list locked calls
1818 *
1819 * You must know what list the SKB is on.
1820 */
1821 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1822 {
1823 unsigned long flags;
1824
1825 spin_lock_irqsave(&list->lock, flags);
1826 __skb_unlink(skb, list);
1827 spin_unlock_irqrestore(&list->lock, flags);
1828 }
1829
1830 /**
1831 * skb_append - append a buffer
1832 * @old: buffer to insert after
1833 * @newsk: buffer to insert
1834 * @list: list to use
1835 *
1836 * Place a packet after a given packet in a list. The list locks are taken
1837 * and this function is atomic with respect to other list locked calls.
1838 * A buffer cannot be placed on two lists at the same time.
1839 */
1840 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1841 {
1842 unsigned long flags;
1843
1844 spin_lock_irqsave(&list->lock, flags);
1845 __skb_queue_after(list, old, newsk);
1846 spin_unlock_irqrestore(&list->lock, flags);
1847 }
1848
1849
1850 /**
1851 * skb_insert - insert a buffer
1852 * @old: buffer to insert before
1853 * @newsk: buffer to insert
1854 * @list: list to use
1855 *
1856 * Place a packet before a given packet in a list. The list locks are
1857 * taken and this function is atomic with respect to other list locked
1858 * calls.
1859 *
1860 * A buffer cannot be placed on two lists at the same time.
1861 */
1862 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1863 {
1864 unsigned long flags;
1865
1866 spin_lock_irqsave(&list->lock, flags);
1867 __skb_insert(newsk, old->prev, old, list);
1868 spin_unlock_irqrestore(&list->lock, flags);
1869 }
1870
1871 static inline void skb_split_inside_header(struct sk_buff *skb,
1872 struct sk_buff* skb1,
1873 const u32 len, const int pos)
1874 {
1875 int i;
1876
1877 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1878 pos - len);
1879 /* And move data appendix as is. */
1880 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1881 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1882
1883 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1884 skb_shinfo(skb)->nr_frags = 0;
1885 skb1->data_len = skb->data_len;
1886 skb1->len += skb1->data_len;
1887 skb->data_len = 0;
1888 skb->len = len;
1889 skb_set_tail_pointer(skb, len);
1890 }
1891
1892 static inline void skb_split_no_header(struct sk_buff *skb,
1893 struct sk_buff* skb1,
1894 const u32 len, int pos)
1895 {
1896 int i, k = 0;
1897 const int nfrags = skb_shinfo(skb)->nr_frags;
1898
1899 skb_shinfo(skb)->nr_frags = 0;
1900 skb1->len = skb1->data_len = skb->len - len;
1901 skb->len = len;
1902 skb->data_len = len - pos;
1903
1904 for (i = 0; i < nfrags; i++) {
1905 int size = skb_shinfo(skb)->frags[i].size;
1906
1907 if (pos + size > len) {
1908 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1909
1910 if (pos < len) {
1911 /* Split frag.
1912 * We have two variants in this case:
1913 * 1. Move all the frag to the second
1914 * part, if it is possible. F.e.
1915 * this approach is mandatory for TUX,
1916 * where splitting is expensive.
1917 * 2. Split is accurately. We make this.
1918 */
1919 get_page(skb_shinfo(skb)->frags[i].page);
1920 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1921 skb_shinfo(skb1)->frags[0].size -= len - pos;
1922 skb_shinfo(skb)->frags[i].size = len - pos;
1923 skb_shinfo(skb)->nr_frags++;
1924 }
1925 k++;
1926 } else
1927 skb_shinfo(skb)->nr_frags++;
1928 pos += size;
1929 }
1930 skb_shinfo(skb1)->nr_frags = k;
1931 }
1932
1933 /**
1934 * skb_split - Split fragmented skb to two parts at length len.
1935 * @skb: the buffer to split
1936 * @skb1: the buffer to receive the second part
1937 * @len: new length for skb
1938 */
1939 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1940 {
1941 int pos = skb_headlen(skb);
1942
1943 if (len < pos) /* Split line is inside header. */
1944 skb_split_inside_header(skb, skb1, len, pos);
1945 else /* Second chunk has no header, nothing to copy. */
1946 skb_split_no_header(skb, skb1, len, pos);
1947 }
1948
1949 /**
1950 * skb_prepare_seq_read - Prepare a sequential read of skb data
1951 * @skb: the buffer to read
1952 * @from: lower offset of data to be read
1953 * @to: upper offset of data to be read
1954 * @st: state variable
1955 *
1956 * Initializes the specified state variable. Must be called before
1957 * invoking skb_seq_read() for the first time.
1958 */
1959 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1960 unsigned int to, struct skb_seq_state *st)
1961 {
1962 st->lower_offset = from;
1963 st->upper_offset = to;
1964 st->root_skb = st->cur_skb = skb;
1965 st->frag_idx = st->stepped_offset = 0;
1966 st->frag_data = NULL;
1967 }
1968
1969 /**
1970 * skb_seq_read - Sequentially read skb data
1971 * @consumed: number of bytes consumed by the caller so far
1972 * @data: destination pointer for data to be returned
1973 * @st: state variable
1974 *
1975 * Reads a block of skb data at &consumed relative to the
1976 * lower offset specified to skb_prepare_seq_read(). Assigns
1977 * the head of the data block to &data and returns the length
1978 * of the block or 0 if the end of the skb data or the upper
1979 * offset has been reached.
1980 *
1981 * The caller is not required to consume all of the data
1982 * returned, i.e. &consumed is typically set to the number
1983 * of bytes already consumed and the next call to
1984 * skb_seq_read() will return the remaining part of the block.
1985 *
1986 * Note 1: The size of each block of data returned can be arbitary,
1987 * this limitation is the cost for zerocopy seqeuental
1988 * reads of potentially non linear data.
1989 *
1990 * Note 2: Fragment lists within fragments are not implemented
1991 * at the moment, state->root_skb could be replaced with
1992 * a stack for this purpose.
1993 */
1994 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1995 struct skb_seq_state *st)
1996 {
1997 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1998 skb_frag_t *frag;
1999
2000 if (unlikely(abs_offset >= st->upper_offset))
2001 return 0;
2002
2003 next_skb:
2004 block_limit = skb_headlen(st->cur_skb);
2005
2006 if (abs_offset < block_limit) {
2007 *data = st->cur_skb->data + abs_offset;
2008 return block_limit - abs_offset;
2009 }
2010
2011 if (st->frag_idx == 0 && !st->frag_data)
2012 st->stepped_offset += skb_headlen(st->cur_skb);
2013
2014 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2015 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2016 block_limit = frag->size + st->stepped_offset;
2017
2018 if (abs_offset < block_limit) {
2019 if (!st->frag_data)
2020 st->frag_data = kmap_skb_frag(frag);
2021
2022 *data = (u8 *) st->frag_data + frag->page_offset +
2023 (abs_offset - st->stepped_offset);
2024
2025 return block_limit - abs_offset;
2026 }
2027
2028 if (st->frag_data) {
2029 kunmap_skb_frag(st->frag_data);
2030 st->frag_data = NULL;
2031 }
2032
2033 st->frag_idx++;
2034 st->stepped_offset += frag->size;
2035 }
2036
2037 if (st->frag_data) {
2038 kunmap_skb_frag(st->frag_data);
2039 st->frag_data = NULL;
2040 }
2041
2042 if (st->cur_skb->next) {
2043 st->cur_skb = st->cur_skb->next;
2044 st->frag_idx = 0;
2045 goto next_skb;
2046 } else if (st->root_skb == st->cur_skb &&
2047 skb_shinfo(st->root_skb)->frag_list) {
2048 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2049 goto next_skb;
2050 }
2051
2052 return 0;
2053 }
2054
2055 /**
2056 * skb_abort_seq_read - Abort a sequential read of skb data
2057 * @st: state variable
2058 *
2059 * Must be called if skb_seq_read() was not called until it
2060 * returned 0.
2061 */
2062 void skb_abort_seq_read(struct skb_seq_state *st)
2063 {
2064 if (st->frag_data)
2065 kunmap_skb_frag(st->frag_data);
2066 }
2067
2068 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2069
2070 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2071 struct ts_config *conf,
2072 struct ts_state *state)
2073 {
2074 return skb_seq_read(offset, text, TS_SKB_CB(state));
2075 }
2076
2077 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2078 {
2079 skb_abort_seq_read(TS_SKB_CB(state));
2080 }
2081
2082 /**
2083 * skb_find_text - Find a text pattern in skb data
2084 * @skb: the buffer to look in
2085 * @from: search offset
2086 * @to: search limit
2087 * @config: textsearch configuration
2088 * @state: uninitialized textsearch state variable
2089 *
2090 * Finds a pattern in the skb data according to the specified
2091 * textsearch configuration. Use textsearch_next() to retrieve
2092 * subsequent occurrences of the pattern. Returns the offset
2093 * to the first occurrence or UINT_MAX if no match was found.
2094 */
2095 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2096 unsigned int to, struct ts_config *config,
2097 struct ts_state *state)
2098 {
2099 unsigned int ret;
2100
2101 config->get_next_block = skb_ts_get_next_block;
2102 config->finish = skb_ts_finish;
2103
2104 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2105
2106 ret = textsearch_find(config, state);
2107 return (ret <= to - from ? ret : UINT_MAX);
2108 }
2109
2110 /**
2111 * skb_append_datato_frags: - append the user data to a skb
2112 * @sk: sock structure
2113 * @skb: skb structure to be appened with user data.
2114 * @getfrag: call back function to be used for getting the user data
2115 * @from: pointer to user message iov
2116 * @length: length of the iov message
2117 *
2118 * Description: This procedure append the user data in the fragment part
2119 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2120 */
2121 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2122 int (*getfrag)(void *from, char *to, int offset,
2123 int len, int odd, struct sk_buff *skb),
2124 void *from, int length)
2125 {
2126 int frg_cnt = 0;
2127 skb_frag_t *frag = NULL;
2128 struct page *page = NULL;
2129 int copy, left;
2130 int offset = 0;
2131 int ret;
2132
2133 do {
2134 /* Return error if we don't have space for new frag */
2135 frg_cnt = skb_shinfo(skb)->nr_frags;
2136 if (frg_cnt >= MAX_SKB_FRAGS)
2137 return -EFAULT;
2138
2139 /* allocate a new page for next frag */
2140 page = alloc_pages(sk->sk_allocation, 0);
2141
2142 /* If alloc_page fails just return failure and caller will
2143 * free previous allocated pages by doing kfree_skb()
2144 */
2145 if (page == NULL)
2146 return -ENOMEM;
2147
2148 /* initialize the next frag */
2149 sk->sk_sndmsg_page = page;
2150 sk->sk_sndmsg_off = 0;
2151 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2152 skb->truesize += PAGE_SIZE;
2153 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2154
2155 /* get the new initialized frag */
2156 frg_cnt = skb_shinfo(skb)->nr_frags;
2157 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2158
2159 /* copy the user data to page */
2160 left = PAGE_SIZE - frag->page_offset;
2161 copy = (length > left)? left : length;
2162
2163 ret = getfrag(from, (page_address(frag->page) +
2164 frag->page_offset + frag->size),
2165 offset, copy, 0, skb);
2166 if (ret < 0)
2167 return -EFAULT;
2168
2169 /* copy was successful so update the size parameters */
2170 sk->sk_sndmsg_off += copy;
2171 frag->size += copy;
2172 skb->len += copy;
2173 skb->data_len += copy;
2174 offset += copy;
2175 length -= copy;
2176
2177 } while (length > 0);
2178
2179 return 0;
2180 }
2181
2182 /**
2183 * skb_pull_rcsum - pull skb and update receive checksum
2184 * @skb: buffer to update
2185 * @len: length of data pulled
2186 *
2187 * This function performs an skb_pull on the packet and updates
2188 * the CHECKSUM_COMPLETE checksum. It should be used on
2189 * receive path processing instead of skb_pull unless you know
2190 * that the checksum difference is zero (e.g., a valid IP header)
2191 * or you are setting ip_summed to CHECKSUM_NONE.
2192 */
2193 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2194 {
2195 BUG_ON(len > skb->len);
2196 skb->len -= len;
2197 BUG_ON(skb->len < skb->data_len);
2198 skb_postpull_rcsum(skb, skb->data, len);
2199 return skb->data += len;
2200 }
2201
2202 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2203
2204 /**
2205 * skb_segment - Perform protocol segmentation on skb.
2206 * @skb: buffer to segment
2207 * @features: features for the output path (see dev->features)
2208 *
2209 * This function performs segmentation on the given skb. It returns
2210 * a pointer to the first in a list of new skbs for the segments.
2211 * In case of error it returns ERR_PTR(err).
2212 */
2213 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2214 {
2215 struct sk_buff *segs = NULL;
2216 struct sk_buff *tail = NULL;
2217 unsigned int mss = skb_shinfo(skb)->gso_size;
2218 unsigned int doffset = skb->data - skb_mac_header(skb);
2219 unsigned int offset = doffset;
2220 unsigned int headroom;
2221 unsigned int len;
2222 int sg = features & NETIF_F_SG;
2223 int nfrags = skb_shinfo(skb)->nr_frags;
2224 int err = -ENOMEM;
2225 int i = 0;
2226 int pos;
2227
2228 __skb_push(skb, doffset);
2229 headroom = skb_headroom(skb);
2230 pos = skb_headlen(skb);
2231
2232 do {
2233 struct sk_buff *nskb;
2234 skb_frag_t *frag;
2235 int hsize;
2236 int k;
2237 int size;
2238
2239 len = skb->len - offset;
2240 if (len > mss)
2241 len = mss;
2242
2243 hsize = skb_headlen(skb) - offset;
2244 if (hsize < 0)
2245 hsize = 0;
2246 if (hsize > len || !sg)
2247 hsize = len;
2248
2249 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2250 if (unlikely(!nskb))
2251 goto err;
2252
2253 if (segs)
2254 tail->next = nskb;
2255 else
2256 segs = nskb;
2257 tail = nskb;
2258
2259 __copy_skb_header(nskb, skb);
2260 nskb->mac_len = skb->mac_len;
2261
2262 skb_reserve(nskb, headroom);
2263 skb_reset_mac_header(nskb);
2264 skb_set_network_header(nskb, skb->mac_len);
2265 nskb->transport_header = (nskb->network_header +
2266 skb_network_header_len(skb));
2267 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2268 doffset);
2269 if (!sg) {
2270 nskb->ip_summed = CHECKSUM_NONE;
2271 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2272 skb_put(nskb, len),
2273 len, 0);
2274 continue;
2275 }
2276
2277 frag = skb_shinfo(nskb)->frags;
2278 k = 0;
2279
2280 skb_copy_from_linear_data_offset(skb, offset,
2281 skb_put(nskb, hsize), hsize);
2282
2283 while (pos < offset + len) {
2284 BUG_ON(i >= nfrags);
2285
2286 *frag = skb_shinfo(skb)->frags[i];
2287 get_page(frag->page);
2288 size = frag->size;
2289
2290 if (pos < offset) {
2291 frag->page_offset += offset - pos;
2292 frag->size -= offset - pos;
2293 }
2294
2295 k++;
2296
2297 if (pos + size <= offset + len) {
2298 i++;
2299 pos += size;
2300 } else {
2301 frag->size -= pos + size - (offset + len);
2302 break;
2303 }
2304
2305 frag++;
2306 }
2307
2308 skb_shinfo(nskb)->nr_frags = k;
2309 nskb->data_len = len - hsize;
2310 nskb->len += nskb->data_len;
2311 nskb->truesize += nskb->data_len;
2312 } while ((offset += len) < skb->len);
2313
2314 return segs;
2315
2316 err:
2317 while ((skb = segs)) {
2318 segs = skb->next;
2319 kfree_skb(skb);
2320 }
2321 return ERR_PTR(err);
2322 }
2323
2324 EXPORT_SYMBOL_GPL(skb_segment);
2325
2326 void __init skb_init(void)
2327 {
2328 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2329 sizeof(struct sk_buff),
2330 0,
2331 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2332 NULL);
2333 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2334 (2*sizeof(struct sk_buff)) +
2335 sizeof(atomic_t),
2336 0,
2337 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2338 NULL);
2339 }
2340
2341 /**
2342 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2343 * @skb: Socket buffer containing the buffers to be mapped
2344 * @sg: The scatter-gather list to map into
2345 * @offset: The offset into the buffer's contents to start mapping
2346 * @len: Length of buffer space to be mapped
2347 *
2348 * Fill the specified scatter-gather list with mappings/pointers into a
2349 * region of the buffer space attached to a socket buffer.
2350 */
2351 static int
2352 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2353 {
2354 int start = skb_headlen(skb);
2355 int i, copy = start - offset;
2356 int elt = 0;
2357
2358 if (copy > 0) {
2359 if (copy > len)
2360 copy = len;
2361 sg_set_buf(sg, skb->data + offset, copy);
2362 elt++;
2363 if ((len -= copy) == 0)
2364 return elt;
2365 offset += copy;
2366 }
2367
2368 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2369 int end;
2370
2371 WARN_ON(start > offset + len);
2372
2373 end = start + skb_shinfo(skb)->frags[i].size;
2374 if ((copy = end - offset) > 0) {
2375 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2376
2377 if (copy > len)
2378 copy = len;
2379 sg_set_page(&sg[elt], frag->page, copy,
2380 frag->page_offset+offset-start);
2381 elt++;
2382 if (!(len -= copy))
2383 return elt;
2384 offset += copy;
2385 }
2386 start = end;
2387 }
2388
2389 if (skb_shinfo(skb)->frag_list) {
2390 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2391
2392 for (; list; list = list->next) {
2393 int end;
2394
2395 WARN_ON(start > offset + len);
2396
2397 end = start + list->len;
2398 if ((copy = end - offset) > 0) {
2399 if (copy > len)
2400 copy = len;
2401 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2402 copy);
2403 if ((len -= copy) == 0)
2404 return elt;
2405 offset += copy;
2406 }
2407 start = end;
2408 }
2409 }
2410 BUG_ON(len);
2411 return elt;
2412 }
2413
2414 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2415 {
2416 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2417
2418 sg_mark_end(&sg[nsg - 1]);
2419
2420 return nsg;
2421 }
2422
2423 /**
2424 * skb_cow_data - Check that a socket buffer's data buffers are writable
2425 * @skb: The socket buffer to check.
2426 * @tailbits: Amount of trailing space to be added
2427 * @trailer: Returned pointer to the skb where the @tailbits space begins
2428 *
2429 * Make sure that the data buffers attached to a socket buffer are
2430 * writable. If they are not, private copies are made of the data buffers
2431 * and the socket buffer is set to use these instead.
2432 *
2433 * If @tailbits is given, make sure that there is space to write @tailbits
2434 * bytes of data beyond current end of socket buffer. @trailer will be
2435 * set to point to the skb in which this space begins.
2436 *
2437 * The number of scatterlist elements required to completely map the
2438 * COW'd and extended socket buffer will be returned.
2439 */
2440 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2441 {
2442 int copyflag;
2443 int elt;
2444 struct sk_buff *skb1, **skb_p;
2445
2446 /* If skb is cloned or its head is paged, reallocate
2447 * head pulling out all the pages (pages are considered not writable
2448 * at the moment even if they are anonymous).
2449 */
2450 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2451 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2452 return -ENOMEM;
2453
2454 /* Easy case. Most of packets will go this way. */
2455 if (!skb_shinfo(skb)->frag_list) {
2456 /* A little of trouble, not enough of space for trailer.
2457 * This should not happen, when stack is tuned to generate
2458 * good frames. OK, on miss we reallocate and reserve even more
2459 * space, 128 bytes is fair. */
2460
2461 if (skb_tailroom(skb) < tailbits &&
2462 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2463 return -ENOMEM;
2464
2465 /* Voila! */
2466 *trailer = skb;
2467 return 1;
2468 }
2469
2470 /* Misery. We are in troubles, going to mincer fragments... */
2471
2472 elt = 1;
2473 skb_p = &skb_shinfo(skb)->frag_list;
2474 copyflag = 0;
2475
2476 while ((skb1 = *skb_p) != NULL) {
2477 int ntail = 0;
2478
2479 /* The fragment is partially pulled by someone,
2480 * this can happen on input. Copy it and everything
2481 * after it. */
2482
2483 if (skb_shared(skb1))
2484 copyflag = 1;
2485
2486 /* If the skb is the last, worry about trailer. */
2487
2488 if (skb1->next == NULL && tailbits) {
2489 if (skb_shinfo(skb1)->nr_frags ||
2490 skb_shinfo(skb1)->frag_list ||
2491 skb_tailroom(skb1) < tailbits)
2492 ntail = tailbits + 128;
2493 }
2494
2495 if (copyflag ||
2496 skb_cloned(skb1) ||
2497 ntail ||
2498 skb_shinfo(skb1)->nr_frags ||
2499 skb_shinfo(skb1)->frag_list) {
2500 struct sk_buff *skb2;
2501
2502 /* Fuck, we are miserable poor guys... */
2503 if (ntail == 0)
2504 skb2 = skb_copy(skb1, GFP_ATOMIC);
2505 else
2506 skb2 = skb_copy_expand(skb1,
2507 skb_headroom(skb1),
2508 ntail,
2509 GFP_ATOMIC);
2510 if (unlikely(skb2 == NULL))
2511 return -ENOMEM;
2512
2513 if (skb1->sk)
2514 skb_set_owner_w(skb2, skb1->sk);
2515
2516 /* Looking around. Are we still alive?
2517 * OK, link new skb, drop old one */
2518
2519 skb2->next = skb1->next;
2520 *skb_p = skb2;
2521 kfree_skb(skb1);
2522 skb1 = skb2;
2523 }
2524 elt++;
2525 *trailer = skb1;
2526 skb_p = &skb1->next;
2527 }
2528
2529 return elt;
2530 }
2531
2532 /**
2533 * skb_partial_csum_set - set up and verify partial csum values for packet
2534 * @skb: the skb to set
2535 * @start: the number of bytes after skb->data to start checksumming.
2536 * @off: the offset from start to place the checksum.
2537 *
2538 * For untrusted partially-checksummed packets, we need to make sure the values
2539 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2540 *
2541 * This function checks and sets those values and skb->ip_summed: if this
2542 * returns false you should drop the packet.
2543 */
2544 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2545 {
2546 if (unlikely(start > skb->len - 2) ||
2547 unlikely((int)start + off > skb->len - 2)) {
2548 if (net_ratelimit())
2549 printk(KERN_WARNING
2550 "bad partial csum: csum=%u/%u len=%u\n",
2551 start, off, skb->len);
2552 return false;
2553 }
2554 skb->ip_summed = CHECKSUM_PARTIAL;
2555 skb->csum_start = skb_headroom(skb) + start;
2556 skb->csum_offset = off;
2557 return true;
2558 }
2559
2560 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2561 {
2562 if (net_ratelimit())
2563 pr_warning("%s: received packets cannot be forwarded"
2564 " while LRO is enabled\n", skb->dev->name);
2565 }
2566
2567 EXPORT_SYMBOL(___pskb_trim);
2568 EXPORT_SYMBOL(__kfree_skb);
2569 EXPORT_SYMBOL(kfree_skb);
2570 EXPORT_SYMBOL(__pskb_pull_tail);
2571 EXPORT_SYMBOL(__alloc_skb);
2572 EXPORT_SYMBOL(__netdev_alloc_skb);
2573 EXPORT_SYMBOL(pskb_copy);
2574 EXPORT_SYMBOL(pskb_expand_head);
2575 EXPORT_SYMBOL(skb_checksum);
2576 EXPORT_SYMBOL(skb_clone);
2577 EXPORT_SYMBOL(skb_copy);
2578 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2579 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2580 EXPORT_SYMBOL(skb_copy_bits);
2581 EXPORT_SYMBOL(skb_copy_expand);
2582 EXPORT_SYMBOL(skb_over_panic);
2583 EXPORT_SYMBOL(skb_pad);
2584 EXPORT_SYMBOL(skb_realloc_headroom);
2585 EXPORT_SYMBOL(skb_under_panic);
2586 EXPORT_SYMBOL(skb_dequeue);
2587 EXPORT_SYMBOL(skb_dequeue_tail);
2588 EXPORT_SYMBOL(skb_insert);
2589 EXPORT_SYMBOL(skb_queue_purge);
2590 EXPORT_SYMBOL(skb_queue_head);
2591 EXPORT_SYMBOL(skb_queue_tail);
2592 EXPORT_SYMBOL(skb_unlink);
2593 EXPORT_SYMBOL(skb_append);
2594 EXPORT_SYMBOL(skb_split);
2595 EXPORT_SYMBOL(skb_prepare_seq_read);
2596 EXPORT_SYMBOL(skb_seq_read);
2597 EXPORT_SYMBOL(skb_abort_seq_read);
2598 EXPORT_SYMBOL(skb_find_text);
2599 EXPORT_SYMBOL(skb_append_datato_frags);
2600 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2601
2602 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2603 EXPORT_SYMBOL_GPL(skb_cow_data);
2604 EXPORT_SYMBOL_GPL(skb_partial_csum_set);