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