]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/core/skbuff.c
[PATCH] libata: PHY reset requires writing 0x4 to SControl
[mirror_ubuntu-artful-kernel.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 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37 /*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/in.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/netdevice.h>
51 #ifdef CONFIG_NET_CLS_ACT
52 #include <net/pkt_sched.h>
53 #endif
54 #include <linux/string.h>
55 #include <linux/skbuff.h>
56 #include <linux/cache.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/init.h>
59 #include <linux/highmem.h>
60
61 #include <net/protocol.h>
62 #include <net/dst.h>
63 #include <net/sock.h>
64 #include <net/checksum.h>
65 #include <net/xfrm.h>
66
67 #include <asm/uaccess.h>
68 #include <asm/system.h>
69
70 static kmem_cache_t *skbuff_head_cache __read_mostly;
71 static kmem_cache_t *skbuff_fclone_cache __read_mostly;
72
73 /*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79 /**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88 {
89 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
90 "data:%p tail:%p end:%p dev:%s\n",
91 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
92 skb->dev ? skb->dev->name : "<NULL>");
93 BUG();
94 }
95
96 /**
97 * skb_under_panic - private function
98 * @skb: buffer
99 * @sz: size
100 * @here: address
101 *
102 * Out of line support code for skb_push(). Not user callable.
103 */
104
105 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
106 {
107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
108 "data:%p tail:%p end:%p dev:%s\n",
109 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
111 BUG();
112 }
113
114 void skb_truesize_bug(struct sk_buff *skb)
115 {
116 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb->truesize, skb->len, sizeof(struct sk_buff));
119 }
120 EXPORT_SYMBOL(skb_truesize_bug);
121
122 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
124 * [BEEP] leaks.
125 *
126 */
127
128 /**
129 * __alloc_skb - allocate a network buffer
130 * @size: size to allocate
131 * @gfp_mask: allocation mask
132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
134 *
135 * Allocate a new &sk_buff. The returned buffer has no headroom and a
136 * tail room of size bytes. The object has a reference count of one.
137 * The return is the buffer. On a failure the return is %NULL.
138 *
139 * Buffers may only be allocated from interrupts using a @gfp_mask of
140 * %GFP_ATOMIC.
141 */
142 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
143 int fclone)
144 {
145 kmem_cache_t *cache;
146 struct skb_shared_info *shinfo;
147 struct sk_buff *skb;
148 u8 *data;
149
150 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
151
152 /* Get the HEAD */
153 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
154 if (!skb)
155 goto out;
156
157 /* Get the DATA. Size must match skb_add_mtu(). */
158 size = SKB_DATA_ALIGN(size);
159 data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
160 if (!data)
161 goto nodata;
162
163 memset(skb, 0, offsetof(struct sk_buff, truesize));
164 skb->truesize = size + sizeof(struct sk_buff);
165 atomic_set(&skb->users, 1);
166 skb->head = data;
167 skb->data = data;
168 skb->tail = data;
169 skb->end = data + size;
170 /* make sure we initialize shinfo sequentially */
171 shinfo = skb_shinfo(skb);
172 atomic_set(&shinfo->dataref, 1);
173 shinfo->nr_frags = 0;
174 shinfo->gso_size = 0;
175 shinfo->gso_segs = 0;
176 shinfo->gso_type = 0;
177 shinfo->ip6_frag_id = 0;
178 shinfo->frag_list = NULL;
179
180 if (fclone) {
181 struct sk_buff *child = skb + 1;
182 atomic_t *fclone_ref = (atomic_t *) (child + 1);
183
184 skb->fclone = SKB_FCLONE_ORIG;
185 atomic_set(fclone_ref, 1);
186
187 child->fclone = SKB_FCLONE_UNAVAILABLE;
188 }
189 out:
190 return skb;
191 nodata:
192 kmem_cache_free(cache, skb);
193 skb = NULL;
194 goto out;
195 }
196
197 /**
198 * alloc_skb_from_cache - allocate a network buffer
199 * @cp: kmem_cache from which to allocate the data area
200 * (object size must be big enough for @size bytes + skb overheads)
201 * @size: size to allocate
202 * @gfp_mask: allocation mask
203 *
204 * Allocate a new &sk_buff. The returned buffer has no headroom and
205 * tail room of size bytes. The object has a reference count of one.
206 * The return is the buffer. On a failure the return is %NULL.
207 *
208 * Buffers may only be allocated from interrupts using a @gfp_mask of
209 * %GFP_ATOMIC.
210 */
211 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
212 unsigned int size,
213 gfp_t gfp_mask)
214 {
215 struct sk_buff *skb;
216 u8 *data;
217
218 /* Get the HEAD */
219 skb = kmem_cache_alloc(skbuff_head_cache,
220 gfp_mask & ~__GFP_DMA);
221 if (!skb)
222 goto out;
223
224 /* Get the DATA. */
225 size = SKB_DATA_ALIGN(size);
226 data = kmem_cache_alloc(cp, gfp_mask);
227 if (!data)
228 goto nodata;
229
230 memset(skb, 0, offsetof(struct sk_buff, truesize));
231 skb->truesize = size + sizeof(struct sk_buff);
232 atomic_set(&skb->users, 1);
233 skb->head = data;
234 skb->data = data;
235 skb->tail = data;
236 skb->end = data + size;
237
238 atomic_set(&(skb_shinfo(skb)->dataref), 1);
239 skb_shinfo(skb)->nr_frags = 0;
240 skb_shinfo(skb)->gso_size = 0;
241 skb_shinfo(skb)->gso_segs = 0;
242 skb_shinfo(skb)->gso_type = 0;
243 skb_shinfo(skb)->frag_list = NULL;
244 out:
245 return skb;
246 nodata:
247 kmem_cache_free(skbuff_head_cache, skb);
248 skb = NULL;
249 goto out;
250 }
251
252 /**
253 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
254 * @dev: network device to receive on
255 * @length: length to allocate
256 * @gfp_mask: get_free_pages mask, passed to alloc_skb
257 *
258 * Allocate a new &sk_buff and assign it a usage count of one. The
259 * buffer has unspecified headroom built in. Users should allocate
260 * the headroom they think they need without accounting for the
261 * built in space. The built in space is used for optimisations.
262 *
263 * %NULL is returned if there is no free memory.
264 */
265 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
266 unsigned int length, gfp_t gfp_mask)
267 {
268 struct sk_buff *skb;
269
270 skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
271 if (likely(skb))
272 skb_reserve(skb, NET_SKB_PAD);
273 return skb;
274 }
275
276 static void skb_drop_list(struct sk_buff **listp)
277 {
278 struct sk_buff *list = *listp;
279
280 *listp = NULL;
281
282 do {
283 struct sk_buff *this = list;
284 list = list->next;
285 kfree_skb(this);
286 } while (list);
287 }
288
289 static inline void skb_drop_fraglist(struct sk_buff *skb)
290 {
291 skb_drop_list(&skb_shinfo(skb)->frag_list);
292 }
293
294 static void skb_clone_fraglist(struct sk_buff *skb)
295 {
296 struct sk_buff *list;
297
298 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
299 skb_get(list);
300 }
301
302 static void skb_release_data(struct sk_buff *skb)
303 {
304 if (!skb->cloned ||
305 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
306 &skb_shinfo(skb)->dataref)) {
307 if (skb_shinfo(skb)->nr_frags) {
308 int i;
309 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
310 put_page(skb_shinfo(skb)->frags[i].page);
311 }
312
313 if (skb_shinfo(skb)->frag_list)
314 skb_drop_fraglist(skb);
315
316 kfree(skb->head);
317 }
318 }
319
320 /*
321 * Free an skbuff by memory without cleaning the state.
322 */
323 void kfree_skbmem(struct sk_buff *skb)
324 {
325 struct sk_buff *other;
326 atomic_t *fclone_ref;
327
328 skb_release_data(skb);
329 switch (skb->fclone) {
330 case SKB_FCLONE_UNAVAILABLE:
331 kmem_cache_free(skbuff_head_cache, skb);
332 break;
333
334 case SKB_FCLONE_ORIG:
335 fclone_ref = (atomic_t *) (skb + 2);
336 if (atomic_dec_and_test(fclone_ref))
337 kmem_cache_free(skbuff_fclone_cache, skb);
338 break;
339
340 case SKB_FCLONE_CLONE:
341 fclone_ref = (atomic_t *) (skb + 1);
342 other = skb - 1;
343
344 /* The clone portion is available for
345 * fast-cloning again.
346 */
347 skb->fclone = SKB_FCLONE_UNAVAILABLE;
348
349 if (atomic_dec_and_test(fclone_ref))
350 kmem_cache_free(skbuff_fclone_cache, other);
351 break;
352 };
353 }
354
355 /**
356 * __kfree_skb - private function
357 * @skb: buffer
358 *
359 * Free an sk_buff. Release anything attached to the buffer.
360 * Clean the state. This is an internal helper function. Users should
361 * always call kfree_skb
362 */
363
364 void __kfree_skb(struct sk_buff *skb)
365 {
366 dst_release(skb->dst);
367 #ifdef CONFIG_XFRM
368 secpath_put(skb->sp);
369 #endif
370 if (skb->destructor) {
371 WARN_ON(in_irq());
372 skb->destructor(skb);
373 }
374 #ifdef CONFIG_NETFILTER
375 nf_conntrack_put(skb->nfct);
376 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
377 nf_conntrack_put_reasm(skb->nfct_reasm);
378 #endif
379 #ifdef CONFIG_BRIDGE_NETFILTER
380 nf_bridge_put(skb->nf_bridge);
381 #endif
382 #endif
383 /* XXX: IS this still necessary? - JHS */
384 #ifdef CONFIG_NET_SCHED
385 skb->tc_index = 0;
386 #ifdef CONFIG_NET_CLS_ACT
387 skb->tc_verd = 0;
388 #endif
389 #endif
390
391 kfree_skbmem(skb);
392 }
393
394 /**
395 * kfree_skb - free an sk_buff
396 * @skb: buffer to free
397 *
398 * Drop a reference to the buffer and free it if the usage count has
399 * hit zero.
400 */
401 void kfree_skb(struct sk_buff *skb)
402 {
403 if (unlikely(!skb))
404 return;
405 if (likely(atomic_read(&skb->users) == 1))
406 smp_rmb();
407 else if (likely(!atomic_dec_and_test(&skb->users)))
408 return;
409 __kfree_skb(skb);
410 }
411
412 /**
413 * skb_clone - duplicate an sk_buff
414 * @skb: buffer to clone
415 * @gfp_mask: allocation priority
416 *
417 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
418 * copies share the same packet data but not structure. The new
419 * buffer has a reference count of 1. If the allocation fails the
420 * function returns %NULL otherwise the new buffer is returned.
421 *
422 * If this function is called from an interrupt gfp_mask() must be
423 * %GFP_ATOMIC.
424 */
425
426 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
427 {
428 struct sk_buff *n;
429
430 n = skb + 1;
431 if (skb->fclone == SKB_FCLONE_ORIG &&
432 n->fclone == SKB_FCLONE_UNAVAILABLE) {
433 atomic_t *fclone_ref = (atomic_t *) (n + 1);
434 n->fclone = SKB_FCLONE_CLONE;
435 atomic_inc(fclone_ref);
436 } else {
437 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
438 if (!n)
439 return NULL;
440 n->fclone = SKB_FCLONE_UNAVAILABLE;
441 }
442
443 #define C(x) n->x = skb->x
444
445 n->next = n->prev = NULL;
446 n->sk = NULL;
447 C(tstamp);
448 C(dev);
449 C(h);
450 C(nh);
451 C(mac);
452 C(dst);
453 dst_clone(skb->dst);
454 C(sp);
455 #ifdef CONFIG_INET
456 secpath_get(skb->sp);
457 #endif
458 memcpy(n->cb, skb->cb, sizeof(skb->cb));
459 C(len);
460 C(data_len);
461 C(csum);
462 C(local_df);
463 n->cloned = 1;
464 n->nohdr = 0;
465 C(pkt_type);
466 C(ip_summed);
467 C(priority);
468 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
469 C(ipvs_property);
470 #endif
471 C(protocol);
472 n->destructor = NULL;
473 #ifdef CONFIG_NETFILTER
474 C(nfmark);
475 C(nfct);
476 nf_conntrack_get(skb->nfct);
477 C(nfctinfo);
478 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
479 C(nfct_reasm);
480 nf_conntrack_get_reasm(skb->nfct_reasm);
481 #endif
482 #ifdef CONFIG_BRIDGE_NETFILTER
483 C(nf_bridge);
484 nf_bridge_get(skb->nf_bridge);
485 #endif
486 #endif /*CONFIG_NETFILTER*/
487 #ifdef CONFIG_NET_SCHED
488 C(tc_index);
489 #ifdef CONFIG_NET_CLS_ACT
490 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
491 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
492 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
493 C(input_dev);
494 #endif
495 skb_copy_secmark(n, skb);
496 #endif
497 C(truesize);
498 atomic_set(&n->users, 1);
499 C(head);
500 C(data);
501 C(tail);
502 C(end);
503
504 atomic_inc(&(skb_shinfo(skb)->dataref));
505 skb->cloned = 1;
506
507 return n;
508 }
509
510 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
511 {
512 /*
513 * Shift between the two data areas in bytes
514 */
515 unsigned long offset = new->data - old->data;
516
517 new->sk = NULL;
518 new->dev = old->dev;
519 new->priority = old->priority;
520 new->protocol = old->protocol;
521 new->dst = dst_clone(old->dst);
522 #ifdef CONFIG_INET
523 new->sp = secpath_get(old->sp);
524 #endif
525 new->h.raw = old->h.raw + offset;
526 new->nh.raw = old->nh.raw + offset;
527 new->mac.raw = old->mac.raw + offset;
528 memcpy(new->cb, old->cb, sizeof(old->cb));
529 new->local_df = old->local_df;
530 new->fclone = SKB_FCLONE_UNAVAILABLE;
531 new->pkt_type = old->pkt_type;
532 new->tstamp = old->tstamp;
533 new->destructor = NULL;
534 #ifdef CONFIG_NETFILTER
535 new->nfmark = old->nfmark;
536 new->nfct = old->nfct;
537 nf_conntrack_get(old->nfct);
538 new->nfctinfo = old->nfctinfo;
539 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
540 new->nfct_reasm = old->nfct_reasm;
541 nf_conntrack_get_reasm(old->nfct_reasm);
542 #endif
543 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
544 new->ipvs_property = old->ipvs_property;
545 #endif
546 #ifdef CONFIG_BRIDGE_NETFILTER
547 new->nf_bridge = old->nf_bridge;
548 nf_bridge_get(old->nf_bridge);
549 #endif
550 #endif
551 #ifdef CONFIG_NET_SCHED
552 #ifdef CONFIG_NET_CLS_ACT
553 new->tc_verd = old->tc_verd;
554 #endif
555 new->tc_index = old->tc_index;
556 #endif
557 skb_copy_secmark(new, old);
558 atomic_set(&new->users, 1);
559 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
560 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
561 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
562 }
563
564 /**
565 * skb_copy - create private copy of an sk_buff
566 * @skb: buffer to copy
567 * @gfp_mask: allocation priority
568 *
569 * Make a copy of both an &sk_buff and its data. This is used when the
570 * caller wishes to modify the data and needs a private copy of the
571 * data to alter. Returns %NULL on failure or the pointer to the buffer
572 * on success. The returned buffer has a reference count of 1.
573 *
574 * As by-product this function converts non-linear &sk_buff to linear
575 * one, so that &sk_buff becomes completely private and caller is allowed
576 * to modify all the data of returned buffer. This means that this
577 * function is not recommended for use in circumstances when only
578 * header is going to be modified. Use pskb_copy() instead.
579 */
580
581 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
582 {
583 int headerlen = skb->data - skb->head;
584 /*
585 * Allocate the copy buffer
586 */
587 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
588 gfp_mask);
589 if (!n)
590 return NULL;
591
592 /* Set the data pointer */
593 skb_reserve(n, headerlen);
594 /* Set the tail pointer and length */
595 skb_put(n, skb->len);
596 n->csum = skb->csum;
597 n->ip_summed = skb->ip_summed;
598
599 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
600 BUG();
601
602 copy_skb_header(n, skb);
603 return n;
604 }
605
606
607 /**
608 * pskb_copy - create copy of an sk_buff with private head.
609 * @skb: buffer to copy
610 * @gfp_mask: allocation priority
611 *
612 * Make a copy of both an &sk_buff and part of its data, located
613 * in header. Fragmented data remain shared. This is used when
614 * the caller wishes to modify only header of &sk_buff and needs
615 * private copy of the header to alter. Returns %NULL on failure
616 * or the pointer to the buffer on success.
617 * The returned buffer has a reference count of 1.
618 */
619
620 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
621 {
622 /*
623 * Allocate the copy buffer
624 */
625 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
626
627 if (!n)
628 goto out;
629
630 /* Set the data pointer */
631 skb_reserve(n, skb->data - skb->head);
632 /* Set the tail pointer and length */
633 skb_put(n, skb_headlen(skb));
634 /* Copy the bytes */
635 memcpy(n->data, skb->data, n->len);
636 n->csum = skb->csum;
637 n->ip_summed = skb->ip_summed;
638
639 n->data_len = skb->data_len;
640 n->len = skb->len;
641
642 if (skb_shinfo(skb)->nr_frags) {
643 int i;
644
645 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
646 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
647 get_page(skb_shinfo(n)->frags[i].page);
648 }
649 skb_shinfo(n)->nr_frags = i;
650 }
651
652 if (skb_shinfo(skb)->frag_list) {
653 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
654 skb_clone_fraglist(n);
655 }
656
657 copy_skb_header(n, skb);
658 out:
659 return n;
660 }
661
662 /**
663 * pskb_expand_head - reallocate header of &sk_buff
664 * @skb: buffer to reallocate
665 * @nhead: room to add at head
666 * @ntail: room to add at tail
667 * @gfp_mask: allocation priority
668 *
669 * Expands (or creates identical copy, if &nhead and &ntail are zero)
670 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
671 * reference count of 1. Returns zero in the case of success or error,
672 * if expansion failed. In the last case, &sk_buff is not changed.
673 *
674 * All the pointers pointing into skb header may change and must be
675 * reloaded after call to this function.
676 */
677
678 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
679 gfp_t gfp_mask)
680 {
681 int i;
682 u8 *data;
683 int size = nhead + (skb->end - skb->head) + ntail;
684 long off;
685
686 if (skb_shared(skb))
687 BUG();
688
689 size = SKB_DATA_ALIGN(size);
690
691 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
692 if (!data)
693 goto nodata;
694
695 /* Copy only real data... and, alas, header. This should be
696 * optimized for the cases when header is void. */
697 memcpy(data + nhead, skb->head, skb->tail - skb->head);
698 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
699
700 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
701 get_page(skb_shinfo(skb)->frags[i].page);
702
703 if (skb_shinfo(skb)->frag_list)
704 skb_clone_fraglist(skb);
705
706 skb_release_data(skb);
707
708 off = (data + nhead) - skb->head;
709
710 skb->head = data;
711 skb->end = data + size;
712 skb->data += off;
713 skb->tail += off;
714 skb->mac.raw += off;
715 skb->h.raw += off;
716 skb->nh.raw += off;
717 skb->cloned = 0;
718 skb->nohdr = 0;
719 atomic_set(&skb_shinfo(skb)->dataref, 1);
720 return 0;
721
722 nodata:
723 return -ENOMEM;
724 }
725
726 /* Make private copy of skb with writable head and some headroom */
727
728 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
729 {
730 struct sk_buff *skb2;
731 int delta = headroom - skb_headroom(skb);
732
733 if (delta <= 0)
734 skb2 = pskb_copy(skb, GFP_ATOMIC);
735 else {
736 skb2 = skb_clone(skb, GFP_ATOMIC);
737 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
738 GFP_ATOMIC)) {
739 kfree_skb(skb2);
740 skb2 = NULL;
741 }
742 }
743 return skb2;
744 }
745
746
747 /**
748 * skb_copy_expand - copy and expand sk_buff
749 * @skb: buffer to copy
750 * @newheadroom: new free bytes at head
751 * @newtailroom: new free bytes at tail
752 * @gfp_mask: allocation priority
753 *
754 * Make a copy of both an &sk_buff and its data and while doing so
755 * allocate additional space.
756 *
757 * This is used when the caller wishes to modify the data and needs a
758 * private copy of the data to alter as well as more space for new fields.
759 * Returns %NULL on failure or the pointer to the buffer
760 * on success. The returned buffer has a reference count of 1.
761 *
762 * You must pass %GFP_ATOMIC as the allocation priority if this function
763 * is called from an interrupt.
764 *
765 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
766 * only by netfilter in the cases when checksum is recalculated? --ANK
767 */
768 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
769 int newheadroom, int newtailroom,
770 gfp_t gfp_mask)
771 {
772 /*
773 * Allocate the copy buffer
774 */
775 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
776 gfp_mask);
777 int head_copy_len, head_copy_off;
778
779 if (!n)
780 return NULL;
781
782 skb_reserve(n, newheadroom);
783
784 /* Set the tail pointer and length */
785 skb_put(n, skb->len);
786
787 head_copy_len = skb_headroom(skb);
788 head_copy_off = 0;
789 if (newheadroom <= head_copy_len)
790 head_copy_len = newheadroom;
791 else
792 head_copy_off = newheadroom - head_copy_len;
793
794 /* Copy the linear header and data. */
795 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
796 skb->len + head_copy_len))
797 BUG();
798
799 copy_skb_header(n, skb);
800
801 return n;
802 }
803
804 /**
805 * skb_pad - zero pad the tail of an skb
806 * @skb: buffer to pad
807 * @pad: space to pad
808 *
809 * Ensure that a buffer is followed by a padding area that is zero
810 * filled. Used by network drivers which may DMA or transfer data
811 * beyond the buffer end onto the wire.
812 *
813 * May return error in out of memory cases. The skb is freed on error.
814 */
815
816 int skb_pad(struct sk_buff *skb, int pad)
817 {
818 int err;
819 int ntail;
820
821 /* If the skbuff is non linear tailroom is always zero.. */
822 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
823 memset(skb->data+skb->len, 0, pad);
824 return 0;
825 }
826
827 ntail = skb->data_len + pad - (skb->end - skb->tail);
828 if (likely(skb_cloned(skb) || ntail > 0)) {
829 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
830 if (unlikely(err))
831 goto free_skb;
832 }
833
834 /* FIXME: The use of this function with non-linear skb's really needs
835 * to be audited.
836 */
837 err = skb_linearize(skb);
838 if (unlikely(err))
839 goto free_skb;
840
841 memset(skb->data + skb->len, 0, pad);
842 return 0;
843
844 free_skb:
845 kfree_skb(skb);
846 return err;
847 }
848
849 /* Trims skb to length len. It can change skb pointers.
850 */
851
852 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
853 {
854 struct sk_buff **fragp;
855 struct sk_buff *frag;
856 int offset = skb_headlen(skb);
857 int nfrags = skb_shinfo(skb)->nr_frags;
858 int i;
859 int err;
860
861 if (skb_cloned(skb) &&
862 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
863 return err;
864
865 i = 0;
866 if (offset >= len)
867 goto drop_pages;
868
869 for (; i < nfrags; i++) {
870 int end = offset + skb_shinfo(skb)->frags[i].size;
871
872 if (end < len) {
873 offset = end;
874 continue;
875 }
876
877 skb_shinfo(skb)->frags[i++].size = len - offset;
878
879 drop_pages:
880 skb_shinfo(skb)->nr_frags = i;
881
882 for (; i < nfrags; i++)
883 put_page(skb_shinfo(skb)->frags[i].page);
884
885 if (skb_shinfo(skb)->frag_list)
886 skb_drop_fraglist(skb);
887 goto done;
888 }
889
890 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
891 fragp = &frag->next) {
892 int end = offset + frag->len;
893
894 if (skb_shared(frag)) {
895 struct sk_buff *nfrag;
896
897 nfrag = skb_clone(frag, GFP_ATOMIC);
898 if (unlikely(!nfrag))
899 return -ENOMEM;
900
901 nfrag->next = frag->next;
902 kfree_skb(frag);
903 frag = nfrag;
904 *fragp = frag;
905 }
906
907 if (end < len) {
908 offset = end;
909 continue;
910 }
911
912 if (end > len &&
913 unlikely((err = pskb_trim(frag, len - offset))))
914 return err;
915
916 if (frag->next)
917 skb_drop_list(&frag->next);
918 break;
919 }
920
921 done:
922 if (len > skb_headlen(skb)) {
923 skb->data_len -= skb->len - len;
924 skb->len = len;
925 } else {
926 skb->len = len;
927 skb->data_len = 0;
928 skb->tail = skb->data + len;
929 }
930
931 return 0;
932 }
933
934 /**
935 * __pskb_pull_tail - advance tail of skb header
936 * @skb: buffer to reallocate
937 * @delta: number of bytes to advance tail
938 *
939 * The function makes a sense only on a fragmented &sk_buff,
940 * it expands header moving its tail forward and copying necessary
941 * data from fragmented part.
942 *
943 * &sk_buff MUST have reference count of 1.
944 *
945 * Returns %NULL (and &sk_buff does not change) if pull failed
946 * or value of new tail of skb in the case of success.
947 *
948 * All the pointers pointing into skb header may change and must be
949 * reloaded after call to this function.
950 */
951
952 /* Moves tail of skb head forward, copying data from fragmented part,
953 * when it is necessary.
954 * 1. It may fail due to malloc failure.
955 * 2. It may change skb pointers.
956 *
957 * It is pretty complicated. Luckily, it is called only in exceptional cases.
958 */
959 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
960 {
961 /* If skb has not enough free space at tail, get new one
962 * plus 128 bytes for future expansions. If we have enough
963 * room at tail, reallocate without expansion only if skb is cloned.
964 */
965 int i, k, eat = (skb->tail + delta) - skb->end;
966
967 if (eat > 0 || skb_cloned(skb)) {
968 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
969 GFP_ATOMIC))
970 return NULL;
971 }
972
973 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
974 BUG();
975
976 /* Optimization: no fragments, no reasons to preestimate
977 * size of pulled pages. Superb.
978 */
979 if (!skb_shinfo(skb)->frag_list)
980 goto pull_pages;
981
982 /* Estimate size of pulled pages. */
983 eat = delta;
984 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
985 if (skb_shinfo(skb)->frags[i].size >= eat)
986 goto pull_pages;
987 eat -= skb_shinfo(skb)->frags[i].size;
988 }
989
990 /* If we need update frag list, we are in troubles.
991 * Certainly, it possible to add an offset to skb data,
992 * but taking into account that pulling is expected to
993 * be very rare operation, it is worth to fight against
994 * further bloating skb head and crucify ourselves here instead.
995 * Pure masohism, indeed. 8)8)
996 */
997 if (eat) {
998 struct sk_buff *list = skb_shinfo(skb)->frag_list;
999 struct sk_buff *clone = NULL;
1000 struct sk_buff *insp = NULL;
1001
1002 do {
1003 BUG_ON(!list);
1004
1005 if (list->len <= eat) {
1006 /* Eaten as whole. */
1007 eat -= list->len;
1008 list = list->next;
1009 insp = list;
1010 } else {
1011 /* Eaten partially. */
1012
1013 if (skb_shared(list)) {
1014 /* Sucks! We need to fork list. :-( */
1015 clone = skb_clone(list, GFP_ATOMIC);
1016 if (!clone)
1017 return NULL;
1018 insp = list->next;
1019 list = clone;
1020 } else {
1021 /* This may be pulled without
1022 * problems. */
1023 insp = list;
1024 }
1025 if (!pskb_pull(list, eat)) {
1026 if (clone)
1027 kfree_skb(clone);
1028 return NULL;
1029 }
1030 break;
1031 }
1032 } while (eat);
1033
1034 /* Free pulled out fragments. */
1035 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1036 skb_shinfo(skb)->frag_list = list->next;
1037 kfree_skb(list);
1038 }
1039 /* And insert new clone at head. */
1040 if (clone) {
1041 clone->next = list;
1042 skb_shinfo(skb)->frag_list = clone;
1043 }
1044 }
1045 /* Success! Now we may commit changes to skb data. */
1046
1047 pull_pages:
1048 eat = delta;
1049 k = 0;
1050 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1051 if (skb_shinfo(skb)->frags[i].size <= eat) {
1052 put_page(skb_shinfo(skb)->frags[i].page);
1053 eat -= skb_shinfo(skb)->frags[i].size;
1054 } else {
1055 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1056 if (eat) {
1057 skb_shinfo(skb)->frags[k].page_offset += eat;
1058 skb_shinfo(skb)->frags[k].size -= eat;
1059 eat = 0;
1060 }
1061 k++;
1062 }
1063 }
1064 skb_shinfo(skb)->nr_frags = k;
1065
1066 skb->tail += delta;
1067 skb->data_len -= delta;
1068
1069 return skb->tail;
1070 }
1071
1072 /* Copy some data bits from skb to kernel buffer. */
1073
1074 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1075 {
1076 int i, copy;
1077 int start = skb_headlen(skb);
1078
1079 if (offset > (int)skb->len - len)
1080 goto fault;
1081
1082 /* Copy header. */
1083 if ((copy = start - offset) > 0) {
1084 if (copy > len)
1085 copy = len;
1086 memcpy(to, skb->data + offset, copy);
1087 if ((len -= copy) == 0)
1088 return 0;
1089 offset += copy;
1090 to += copy;
1091 }
1092
1093 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1094 int end;
1095
1096 BUG_TRAP(start <= offset + len);
1097
1098 end = start + skb_shinfo(skb)->frags[i].size;
1099 if ((copy = end - offset) > 0) {
1100 u8 *vaddr;
1101
1102 if (copy > len)
1103 copy = len;
1104
1105 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1106 memcpy(to,
1107 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1108 offset - start, copy);
1109 kunmap_skb_frag(vaddr);
1110
1111 if ((len -= copy) == 0)
1112 return 0;
1113 offset += copy;
1114 to += copy;
1115 }
1116 start = end;
1117 }
1118
1119 if (skb_shinfo(skb)->frag_list) {
1120 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1121
1122 for (; list; list = list->next) {
1123 int end;
1124
1125 BUG_TRAP(start <= offset + len);
1126
1127 end = start + list->len;
1128 if ((copy = end - offset) > 0) {
1129 if (copy > len)
1130 copy = len;
1131 if (skb_copy_bits(list, offset - start,
1132 to, copy))
1133 goto fault;
1134 if ((len -= copy) == 0)
1135 return 0;
1136 offset += copy;
1137 to += copy;
1138 }
1139 start = end;
1140 }
1141 }
1142 if (!len)
1143 return 0;
1144
1145 fault:
1146 return -EFAULT;
1147 }
1148
1149 /**
1150 * skb_store_bits - store bits from kernel buffer to skb
1151 * @skb: destination buffer
1152 * @offset: offset in destination
1153 * @from: source buffer
1154 * @len: number of bytes to copy
1155 *
1156 * Copy the specified number of bytes from the source buffer to the
1157 * destination skb. This function handles all the messy bits of
1158 * traversing fragment lists and such.
1159 */
1160
1161 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1162 {
1163 int i, copy;
1164 int start = skb_headlen(skb);
1165
1166 if (offset > (int)skb->len - len)
1167 goto fault;
1168
1169 if ((copy = start - offset) > 0) {
1170 if (copy > len)
1171 copy = len;
1172 memcpy(skb->data + offset, from, copy);
1173 if ((len -= copy) == 0)
1174 return 0;
1175 offset += copy;
1176 from += copy;
1177 }
1178
1179 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1180 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1181 int end;
1182
1183 BUG_TRAP(start <= offset + len);
1184
1185 end = start + frag->size;
1186 if ((copy = end - offset) > 0) {
1187 u8 *vaddr;
1188
1189 if (copy > len)
1190 copy = len;
1191
1192 vaddr = kmap_skb_frag(frag);
1193 memcpy(vaddr + frag->page_offset + offset - start,
1194 from, copy);
1195 kunmap_skb_frag(vaddr);
1196
1197 if ((len -= copy) == 0)
1198 return 0;
1199 offset += copy;
1200 from += copy;
1201 }
1202 start = end;
1203 }
1204
1205 if (skb_shinfo(skb)->frag_list) {
1206 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1207
1208 for (; list; list = list->next) {
1209 int end;
1210
1211 BUG_TRAP(start <= offset + len);
1212
1213 end = start + list->len;
1214 if ((copy = end - offset) > 0) {
1215 if (copy > len)
1216 copy = len;
1217 if (skb_store_bits(list, offset - start,
1218 from, copy))
1219 goto fault;
1220 if ((len -= copy) == 0)
1221 return 0;
1222 offset += copy;
1223 from += copy;
1224 }
1225 start = end;
1226 }
1227 }
1228 if (!len)
1229 return 0;
1230
1231 fault:
1232 return -EFAULT;
1233 }
1234
1235 EXPORT_SYMBOL(skb_store_bits);
1236
1237 /* Checksum skb data. */
1238
1239 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1240 int len, unsigned int csum)
1241 {
1242 int start = skb_headlen(skb);
1243 int i, copy = start - offset;
1244 int pos = 0;
1245
1246 /* Checksum header. */
1247 if (copy > 0) {
1248 if (copy > len)
1249 copy = len;
1250 csum = csum_partial(skb->data + offset, copy, csum);
1251 if ((len -= copy) == 0)
1252 return csum;
1253 offset += copy;
1254 pos = copy;
1255 }
1256
1257 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1258 int end;
1259
1260 BUG_TRAP(start <= offset + len);
1261
1262 end = start + skb_shinfo(skb)->frags[i].size;
1263 if ((copy = end - offset) > 0) {
1264 unsigned int csum2;
1265 u8 *vaddr;
1266 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1267
1268 if (copy > len)
1269 copy = len;
1270 vaddr = kmap_skb_frag(frag);
1271 csum2 = csum_partial(vaddr + frag->page_offset +
1272 offset - start, copy, 0);
1273 kunmap_skb_frag(vaddr);
1274 csum = csum_block_add(csum, csum2, pos);
1275 if (!(len -= copy))
1276 return csum;
1277 offset += copy;
1278 pos += copy;
1279 }
1280 start = end;
1281 }
1282
1283 if (skb_shinfo(skb)->frag_list) {
1284 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1285
1286 for (; list; list = list->next) {
1287 int end;
1288
1289 BUG_TRAP(start <= offset + len);
1290
1291 end = start + list->len;
1292 if ((copy = end - offset) > 0) {
1293 unsigned int csum2;
1294 if (copy > len)
1295 copy = len;
1296 csum2 = skb_checksum(list, offset - start,
1297 copy, 0);
1298 csum = csum_block_add(csum, csum2, pos);
1299 if ((len -= copy) == 0)
1300 return csum;
1301 offset += copy;
1302 pos += copy;
1303 }
1304 start = end;
1305 }
1306 }
1307 BUG_ON(len);
1308
1309 return csum;
1310 }
1311
1312 /* Both of above in one bottle. */
1313
1314 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1315 u8 *to, int len, unsigned int csum)
1316 {
1317 int start = skb_headlen(skb);
1318 int i, copy = start - offset;
1319 int pos = 0;
1320
1321 /* Copy header. */
1322 if (copy > 0) {
1323 if (copy > len)
1324 copy = len;
1325 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1326 copy, csum);
1327 if ((len -= copy) == 0)
1328 return csum;
1329 offset += copy;
1330 to += copy;
1331 pos = copy;
1332 }
1333
1334 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1335 int end;
1336
1337 BUG_TRAP(start <= offset + len);
1338
1339 end = start + skb_shinfo(skb)->frags[i].size;
1340 if ((copy = end - offset) > 0) {
1341 unsigned int csum2;
1342 u8 *vaddr;
1343 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1344
1345 if (copy > len)
1346 copy = len;
1347 vaddr = kmap_skb_frag(frag);
1348 csum2 = csum_partial_copy_nocheck(vaddr +
1349 frag->page_offset +
1350 offset - start, to,
1351 copy, 0);
1352 kunmap_skb_frag(vaddr);
1353 csum = csum_block_add(csum, csum2, pos);
1354 if (!(len -= copy))
1355 return csum;
1356 offset += copy;
1357 to += copy;
1358 pos += copy;
1359 }
1360 start = end;
1361 }
1362
1363 if (skb_shinfo(skb)->frag_list) {
1364 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1365
1366 for (; list; list = list->next) {
1367 unsigned int csum2;
1368 int end;
1369
1370 BUG_TRAP(start <= offset + len);
1371
1372 end = start + list->len;
1373 if ((copy = end - offset) > 0) {
1374 if (copy > len)
1375 copy = len;
1376 csum2 = skb_copy_and_csum_bits(list,
1377 offset - start,
1378 to, copy, 0);
1379 csum = csum_block_add(csum, csum2, pos);
1380 if ((len -= copy) == 0)
1381 return csum;
1382 offset += copy;
1383 to += copy;
1384 pos += copy;
1385 }
1386 start = end;
1387 }
1388 }
1389 BUG_ON(len);
1390 return csum;
1391 }
1392
1393 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1394 {
1395 unsigned int csum;
1396 long csstart;
1397
1398 if (skb->ip_summed == CHECKSUM_HW)
1399 csstart = skb->h.raw - skb->data;
1400 else
1401 csstart = skb_headlen(skb);
1402
1403 BUG_ON(csstart > skb_headlen(skb));
1404
1405 memcpy(to, skb->data, csstart);
1406
1407 csum = 0;
1408 if (csstart != skb->len)
1409 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1410 skb->len - csstart, 0);
1411
1412 if (skb->ip_summed == CHECKSUM_HW) {
1413 long csstuff = csstart + skb->csum;
1414
1415 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1416 }
1417 }
1418
1419 /**
1420 * skb_dequeue - remove from the head of the queue
1421 * @list: list to dequeue from
1422 *
1423 * Remove the head of the list. The list lock is taken so the function
1424 * may be used safely with other locking list functions. The head item is
1425 * returned or %NULL if the list is empty.
1426 */
1427
1428 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1429 {
1430 unsigned long flags;
1431 struct sk_buff *result;
1432
1433 spin_lock_irqsave(&list->lock, flags);
1434 result = __skb_dequeue(list);
1435 spin_unlock_irqrestore(&list->lock, flags);
1436 return result;
1437 }
1438
1439 /**
1440 * skb_dequeue_tail - remove from the tail of the queue
1441 * @list: list to dequeue from
1442 *
1443 * Remove the tail of the list. The list lock is taken so the function
1444 * may be used safely with other locking list functions. The tail item is
1445 * returned or %NULL if the list is empty.
1446 */
1447 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1448 {
1449 unsigned long flags;
1450 struct sk_buff *result;
1451
1452 spin_lock_irqsave(&list->lock, flags);
1453 result = __skb_dequeue_tail(list);
1454 spin_unlock_irqrestore(&list->lock, flags);
1455 return result;
1456 }
1457
1458 /**
1459 * skb_queue_purge - empty a list
1460 * @list: list to empty
1461 *
1462 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1463 * the list and one reference dropped. This function takes the list
1464 * lock and is atomic with respect to other list locking functions.
1465 */
1466 void skb_queue_purge(struct sk_buff_head *list)
1467 {
1468 struct sk_buff *skb;
1469 while ((skb = skb_dequeue(list)) != NULL)
1470 kfree_skb(skb);
1471 }
1472
1473 /**
1474 * skb_queue_head - queue a buffer at the list head
1475 * @list: list to use
1476 * @newsk: buffer to queue
1477 *
1478 * Queue a buffer at the start of the list. This function takes the
1479 * list lock and can be used safely with other locking &sk_buff functions
1480 * safely.
1481 *
1482 * A buffer cannot be placed on two lists at the same time.
1483 */
1484 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1485 {
1486 unsigned long flags;
1487
1488 spin_lock_irqsave(&list->lock, flags);
1489 __skb_queue_head(list, newsk);
1490 spin_unlock_irqrestore(&list->lock, flags);
1491 }
1492
1493 /**
1494 * skb_queue_tail - queue a buffer at the list tail
1495 * @list: list to use
1496 * @newsk: buffer to queue
1497 *
1498 * Queue a buffer at the tail of the list. This function takes the
1499 * list lock and can be used safely with other locking &sk_buff functions
1500 * safely.
1501 *
1502 * A buffer cannot be placed on two lists at the same time.
1503 */
1504 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1505 {
1506 unsigned long flags;
1507
1508 spin_lock_irqsave(&list->lock, flags);
1509 __skb_queue_tail(list, newsk);
1510 spin_unlock_irqrestore(&list->lock, flags);
1511 }
1512
1513 /**
1514 * skb_unlink - remove a buffer from a list
1515 * @skb: buffer to remove
1516 * @list: list to use
1517 *
1518 * Remove a packet from a list. The list locks are taken and this
1519 * function is atomic with respect to other list locked calls
1520 *
1521 * You must know what list the SKB is on.
1522 */
1523 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1524 {
1525 unsigned long flags;
1526
1527 spin_lock_irqsave(&list->lock, flags);
1528 __skb_unlink(skb, list);
1529 spin_unlock_irqrestore(&list->lock, flags);
1530 }
1531
1532 /**
1533 * skb_append - append a buffer
1534 * @old: buffer to insert after
1535 * @newsk: buffer to insert
1536 * @list: list to use
1537 *
1538 * Place a packet after a given packet in a list. The list locks are taken
1539 * and this function is atomic with respect to other list locked calls.
1540 * A buffer cannot be placed on two lists at the same time.
1541 */
1542 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1543 {
1544 unsigned long flags;
1545
1546 spin_lock_irqsave(&list->lock, flags);
1547 __skb_append(old, newsk, list);
1548 spin_unlock_irqrestore(&list->lock, flags);
1549 }
1550
1551
1552 /**
1553 * skb_insert - insert a buffer
1554 * @old: buffer to insert before
1555 * @newsk: buffer to insert
1556 * @list: list to use
1557 *
1558 * Place a packet before a given packet in a list. The list locks are
1559 * taken and this function is atomic with respect to other list locked
1560 * calls.
1561 *
1562 * A buffer cannot be placed on two lists at the same time.
1563 */
1564 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1565 {
1566 unsigned long flags;
1567
1568 spin_lock_irqsave(&list->lock, flags);
1569 __skb_insert(newsk, old->prev, old, list);
1570 spin_unlock_irqrestore(&list->lock, flags);
1571 }
1572
1573 #if 0
1574 /*
1575 * Tune the memory allocator for a new MTU size.
1576 */
1577 void skb_add_mtu(int mtu)
1578 {
1579 /* Must match allocation in alloc_skb */
1580 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1581
1582 kmem_add_cache_size(mtu);
1583 }
1584 #endif
1585
1586 static inline void skb_split_inside_header(struct sk_buff *skb,
1587 struct sk_buff* skb1,
1588 const u32 len, const int pos)
1589 {
1590 int i;
1591
1592 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1593
1594 /* And move data appendix as is. */
1595 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1596 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1597
1598 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1599 skb_shinfo(skb)->nr_frags = 0;
1600 skb1->data_len = skb->data_len;
1601 skb1->len += skb1->data_len;
1602 skb->data_len = 0;
1603 skb->len = len;
1604 skb->tail = skb->data + len;
1605 }
1606
1607 static inline void skb_split_no_header(struct sk_buff *skb,
1608 struct sk_buff* skb1,
1609 const u32 len, int pos)
1610 {
1611 int i, k = 0;
1612 const int nfrags = skb_shinfo(skb)->nr_frags;
1613
1614 skb_shinfo(skb)->nr_frags = 0;
1615 skb1->len = skb1->data_len = skb->len - len;
1616 skb->len = len;
1617 skb->data_len = len - pos;
1618
1619 for (i = 0; i < nfrags; i++) {
1620 int size = skb_shinfo(skb)->frags[i].size;
1621
1622 if (pos + size > len) {
1623 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1624
1625 if (pos < len) {
1626 /* Split frag.
1627 * We have two variants in this case:
1628 * 1. Move all the frag to the second
1629 * part, if it is possible. F.e.
1630 * this approach is mandatory for TUX,
1631 * where splitting is expensive.
1632 * 2. Split is accurately. We make this.
1633 */
1634 get_page(skb_shinfo(skb)->frags[i].page);
1635 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1636 skb_shinfo(skb1)->frags[0].size -= len - pos;
1637 skb_shinfo(skb)->frags[i].size = len - pos;
1638 skb_shinfo(skb)->nr_frags++;
1639 }
1640 k++;
1641 } else
1642 skb_shinfo(skb)->nr_frags++;
1643 pos += size;
1644 }
1645 skb_shinfo(skb1)->nr_frags = k;
1646 }
1647
1648 /**
1649 * skb_split - Split fragmented skb to two parts at length len.
1650 * @skb: the buffer to split
1651 * @skb1: the buffer to receive the second part
1652 * @len: new length for skb
1653 */
1654 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1655 {
1656 int pos = skb_headlen(skb);
1657
1658 if (len < pos) /* Split line is inside header. */
1659 skb_split_inside_header(skb, skb1, len, pos);
1660 else /* Second chunk has no header, nothing to copy. */
1661 skb_split_no_header(skb, skb1, len, pos);
1662 }
1663
1664 /**
1665 * skb_prepare_seq_read - Prepare a sequential read of skb data
1666 * @skb: the buffer to read
1667 * @from: lower offset of data to be read
1668 * @to: upper offset of data to be read
1669 * @st: state variable
1670 *
1671 * Initializes the specified state variable. Must be called before
1672 * invoking skb_seq_read() for the first time.
1673 */
1674 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1675 unsigned int to, struct skb_seq_state *st)
1676 {
1677 st->lower_offset = from;
1678 st->upper_offset = to;
1679 st->root_skb = st->cur_skb = skb;
1680 st->frag_idx = st->stepped_offset = 0;
1681 st->frag_data = NULL;
1682 }
1683
1684 /**
1685 * skb_seq_read - Sequentially read skb data
1686 * @consumed: number of bytes consumed by the caller so far
1687 * @data: destination pointer for data to be returned
1688 * @st: state variable
1689 *
1690 * Reads a block of skb data at &consumed relative to the
1691 * lower offset specified to skb_prepare_seq_read(). Assigns
1692 * the head of the data block to &data and returns the length
1693 * of the block or 0 if the end of the skb data or the upper
1694 * offset has been reached.
1695 *
1696 * The caller is not required to consume all of the data
1697 * returned, i.e. &consumed is typically set to the number
1698 * of bytes already consumed and the next call to
1699 * skb_seq_read() will return the remaining part of the block.
1700 *
1701 * Note: The size of each block of data returned can be arbitary,
1702 * this limitation is the cost for zerocopy seqeuental
1703 * reads of potentially non linear data.
1704 *
1705 * Note: Fragment lists within fragments are not implemented
1706 * at the moment, state->root_skb could be replaced with
1707 * a stack for this purpose.
1708 */
1709 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1710 struct skb_seq_state *st)
1711 {
1712 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1713 skb_frag_t *frag;
1714
1715 if (unlikely(abs_offset >= st->upper_offset))
1716 return 0;
1717
1718 next_skb:
1719 block_limit = skb_headlen(st->cur_skb);
1720
1721 if (abs_offset < block_limit) {
1722 *data = st->cur_skb->data + abs_offset;
1723 return block_limit - abs_offset;
1724 }
1725
1726 if (st->frag_idx == 0 && !st->frag_data)
1727 st->stepped_offset += skb_headlen(st->cur_skb);
1728
1729 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1730 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1731 block_limit = frag->size + st->stepped_offset;
1732
1733 if (abs_offset < block_limit) {
1734 if (!st->frag_data)
1735 st->frag_data = kmap_skb_frag(frag);
1736
1737 *data = (u8 *) st->frag_data + frag->page_offset +
1738 (abs_offset - st->stepped_offset);
1739
1740 return block_limit - abs_offset;
1741 }
1742
1743 if (st->frag_data) {
1744 kunmap_skb_frag(st->frag_data);
1745 st->frag_data = NULL;
1746 }
1747
1748 st->frag_idx++;
1749 st->stepped_offset += frag->size;
1750 }
1751
1752 if (st->cur_skb->next) {
1753 st->cur_skb = st->cur_skb->next;
1754 st->frag_idx = 0;
1755 goto next_skb;
1756 } else if (st->root_skb == st->cur_skb &&
1757 skb_shinfo(st->root_skb)->frag_list) {
1758 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1759 goto next_skb;
1760 }
1761
1762 return 0;
1763 }
1764
1765 /**
1766 * skb_abort_seq_read - Abort a sequential read of skb data
1767 * @st: state variable
1768 *
1769 * Must be called if skb_seq_read() was not called until it
1770 * returned 0.
1771 */
1772 void skb_abort_seq_read(struct skb_seq_state *st)
1773 {
1774 if (st->frag_data)
1775 kunmap_skb_frag(st->frag_data);
1776 }
1777
1778 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1779
1780 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1781 struct ts_config *conf,
1782 struct ts_state *state)
1783 {
1784 return skb_seq_read(offset, text, TS_SKB_CB(state));
1785 }
1786
1787 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1788 {
1789 skb_abort_seq_read(TS_SKB_CB(state));
1790 }
1791
1792 /**
1793 * skb_find_text - Find a text pattern in skb data
1794 * @skb: the buffer to look in
1795 * @from: search offset
1796 * @to: search limit
1797 * @config: textsearch configuration
1798 * @state: uninitialized textsearch state variable
1799 *
1800 * Finds a pattern in the skb data according to the specified
1801 * textsearch configuration. Use textsearch_next() to retrieve
1802 * subsequent occurrences of the pattern. Returns the offset
1803 * to the first occurrence or UINT_MAX if no match was found.
1804 */
1805 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1806 unsigned int to, struct ts_config *config,
1807 struct ts_state *state)
1808 {
1809 unsigned int ret;
1810
1811 config->get_next_block = skb_ts_get_next_block;
1812 config->finish = skb_ts_finish;
1813
1814 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1815
1816 ret = textsearch_find(config, state);
1817 return (ret <= to - from ? ret : UINT_MAX);
1818 }
1819
1820 /**
1821 * skb_append_datato_frags: - append the user data to a skb
1822 * @sk: sock structure
1823 * @skb: skb structure to be appened with user data.
1824 * @getfrag: call back function to be used for getting the user data
1825 * @from: pointer to user message iov
1826 * @length: length of the iov message
1827 *
1828 * Description: This procedure append the user data in the fragment part
1829 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1830 */
1831 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1832 int (*getfrag)(void *from, char *to, int offset,
1833 int len, int odd, struct sk_buff *skb),
1834 void *from, int length)
1835 {
1836 int frg_cnt = 0;
1837 skb_frag_t *frag = NULL;
1838 struct page *page = NULL;
1839 int copy, left;
1840 int offset = 0;
1841 int ret;
1842
1843 do {
1844 /* Return error if we don't have space for new frag */
1845 frg_cnt = skb_shinfo(skb)->nr_frags;
1846 if (frg_cnt >= MAX_SKB_FRAGS)
1847 return -EFAULT;
1848
1849 /* allocate a new page for next frag */
1850 page = alloc_pages(sk->sk_allocation, 0);
1851
1852 /* If alloc_page fails just return failure and caller will
1853 * free previous allocated pages by doing kfree_skb()
1854 */
1855 if (page == NULL)
1856 return -ENOMEM;
1857
1858 /* initialize the next frag */
1859 sk->sk_sndmsg_page = page;
1860 sk->sk_sndmsg_off = 0;
1861 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1862 skb->truesize += PAGE_SIZE;
1863 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1864
1865 /* get the new initialized frag */
1866 frg_cnt = skb_shinfo(skb)->nr_frags;
1867 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1868
1869 /* copy the user data to page */
1870 left = PAGE_SIZE - frag->page_offset;
1871 copy = (length > left)? left : length;
1872
1873 ret = getfrag(from, (page_address(frag->page) +
1874 frag->page_offset + frag->size),
1875 offset, copy, 0, skb);
1876 if (ret < 0)
1877 return -EFAULT;
1878
1879 /* copy was successful so update the size parameters */
1880 sk->sk_sndmsg_off += copy;
1881 frag->size += copy;
1882 skb->len += copy;
1883 skb->data_len += copy;
1884 offset += copy;
1885 length -= copy;
1886
1887 } while (length > 0);
1888
1889 return 0;
1890 }
1891
1892 /**
1893 * skb_pull_rcsum - pull skb and update receive checksum
1894 * @skb: buffer to update
1895 * @start: start of data before pull
1896 * @len: length of data pulled
1897 *
1898 * This function performs an skb_pull on the packet and updates
1899 * update the CHECKSUM_HW checksum. It should be used on receive
1900 * path processing instead of skb_pull unless you know that the
1901 * checksum difference is zero (e.g., a valid IP header) or you
1902 * are setting ip_summed to CHECKSUM_NONE.
1903 */
1904 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1905 {
1906 BUG_ON(len > skb->len);
1907 skb->len -= len;
1908 BUG_ON(skb->len < skb->data_len);
1909 skb_postpull_rcsum(skb, skb->data, len);
1910 return skb->data += len;
1911 }
1912
1913 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1914
1915 /**
1916 * skb_segment - Perform protocol segmentation on skb.
1917 * @skb: buffer to segment
1918 * @features: features for the output path (see dev->features)
1919 *
1920 * This function performs segmentation on the given skb. It returns
1921 * the segment at the given position. It returns NULL if there are
1922 * no more segments to generate, or when an error is encountered.
1923 */
1924 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1925 {
1926 struct sk_buff *segs = NULL;
1927 struct sk_buff *tail = NULL;
1928 unsigned int mss = skb_shinfo(skb)->gso_size;
1929 unsigned int doffset = skb->data - skb->mac.raw;
1930 unsigned int offset = doffset;
1931 unsigned int headroom;
1932 unsigned int len;
1933 int sg = features & NETIF_F_SG;
1934 int nfrags = skb_shinfo(skb)->nr_frags;
1935 int err = -ENOMEM;
1936 int i = 0;
1937 int pos;
1938
1939 __skb_push(skb, doffset);
1940 headroom = skb_headroom(skb);
1941 pos = skb_headlen(skb);
1942
1943 do {
1944 struct sk_buff *nskb;
1945 skb_frag_t *frag;
1946 int hsize, nsize;
1947 int k;
1948 int size;
1949
1950 len = skb->len - offset;
1951 if (len > mss)
1952 len = mss;
1953
1954 hsize = skb_headlen(skb) - offset;
1955 if (hsize < 0)
1956 hsize = 0;
1957 nsize = hsize + doffset;
1958 if (nsize > len + doffset || !sg)
1959 nsize = len + doffset;
1960
1961 nskb = alloc_skb(nsize + headroom, GFP_ATOMIC);
1962 if (unlikely(!nskb))
1963 goto err;
1964
1965 if (segs)
1966 tail->next = nskb;
1967 else
1968 segs = nskb;
1969 tail = nskb;
1970
1971 nskb->dev = skb->dev;
1972 nskb->priority = skb->priority;
1973 nskb->protocol = skb->protocol;
1974 nskb->dst = dst_clone(skb->dst);
1975 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1976 nskb->pkt_type = skb->pkt_type;
1977 nskb->mac_len = skb->mac_len;
1978
1979 skb_reserve(nskb, headroom);
1980 nskb->mac.raw = nskb->data;
1981 nskb->nh.raw = nskb->data + skb->mac_len;
1982 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1983 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1984
1985 if (!sg) {
1986 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1987 skb_put(nskb, len),
1988 len, 0);
1989 continue;
1990 }
1991
1992 frag = skb_shinfo(nskb)->frags;
1993 k = 0;
1994
1995 nskb->ip_summed = CHECKSUM_HW;
1996 nskb->csum = skb->csum;
1997 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1998
1999 while (pos < offset + len) {
2000 BUG_ON(i >= nfrags);
2001
2002 *frag = skb_shinfo(skb)->frags[i];
2003 get_page(frag->page);
2004 size = frag->size;
2005
2006 if (pos < offset) {
2007 frag->page_offset += offset - pos;
2008 frag->size -= offset - pos;
2009 }
2010
2011 k++;
2012
2013 if (pos + size <= offset + len) {
2014 i++;
2015 pos += size;
2016 } else {
2017 frag->size -= pos + size - (offset + len);
2018 break;
2019 }
2020
2021 frag++;
2022 }
2023
2024 skb_shinfo(nskb)->nr_frags = k;
2025 nskb->data_len = len - hsize;
2026 nskb->len += nskb->data_len;
2027 nskb->truesize += nskb->data_len;
2028 } while ((offset += len) < skb->len);
2029
2030 return segs;
2031
2032 err:
2033 while ((skb = segs)) {
2034 segs = skb->next;
2035 kfree(skb);
2036 }
2037 return ERR_PTR(err);
2038 }
2039
2040 EXPORT_SYMBOL_GPL(skb_segment);
2041
2042 void __init skb_init(void)
2043 {
2044 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2045 sizeof(struct sk_buff),
2046 0,
2047 SLAB_HWCACHE_ALIGN,
2048 NULL, NULL);
2049 if (!skbuff_head_cache)
2050 panic("cannot create skbuff cache");
2051
2052 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2053 (2*sizeof(struct sk_buff)) +
2054 sizeof(atomic_t),
2055 0,
2056 SLAB_HWCACHE_ALIGN,
2057 NULL, NULL);
2058 if (!skbuff_fclone_cache)
2059 panic("cannot create skbuff cache");
2060 }
2061
2062 EXPORT_SYMBOL(___pskb_trim);
2063 EXPORT_SYMBOL(__kfree_skb);
2064 EXPORT_SYMBOL(kfree_skb);
2065 EXPORT_SYMBOL(__pskb_pull_tail);
2066 EXPORT_SYMBOL(__alloc_skb);
2067 EXPORT_SYMBOL(__netdev_alloc_skb);
2068 EXPORT_SYMBOL(pskb_copy);
2069 EXPORT_SYMBOL(pskb_expand_head);
2070 EXPORT_SYMBOL(skb_checksum);
2071 EXPORT_SYMBOL(skb_clone);
2072 EXPORT_SYMBOL(skb_clone_fraglist);
2073 EXPORT_SYMBOL(skb_copy);
2074 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2075 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2076 EXPORT_SYMBOL(skb_copy_bits);
2077 EXPORT_SYMBOL(skb_copy_expand);
2078 EXPORT_SYMBOL(skb_over_panic);
2079 EXPORT_SYMBOL(skb_pad);
2080 EXPORT_SYMBOL(skb_realloc_headroom);
2081 EXPORT_SYMBOL(skb_under_panic);
2082 EXPORT_SYMBOL(skb_dequeue);
2083 EXPORT_SYMBOL(skb_dequeue_tail);
2084 EXPORT_SYMBOL(skb_insert);
2085 EXPORT_SYMBOL(skb_queue_purge);
2086 EXPORT_SYMBOL(skb_queue_head);
2087 EXPORT_SYMBOL(skb_queue_tail);
2088 EXPORT_SYMBOL(skb_unlink);
2089 EXPORT_SYMBOL(skb_append);
2090 EXPORT_SYMBOL(skb_split);
2091 EXPORT_SYMBOL(skb_prepare_seq_read);
2092 EXPORT_SYMBOL(skb_seq_read);
2093 EXPORT_SYMBOL(skb_abort_seq_read);
2094 EXPORT_SYMBOL(skb_find_text);
2095 EXPORT_SYMBOL(skb_append_datato_frags);