xref: /linux/net/core/skbuff.c (revision 60b2737de1b1ddfdb90f3ba622634eb49d6f3603)
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;
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 /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
115  *	'private' fields and also do memory statistics to find all the
116  *	[BEEP] leaks.
117  *
118  */
119 
120 /**
121  *	alloc_skb	-	allocate a network buffer
122  *	@size: size to allocate
123  *	@gfp_mask: allocation mask
124  *
125  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
126  *	tail room of size bytes. The object has a reference count of one.
127  *	The return is the buffer. On a failure the return is %NULL.
128  *
129  *	Buffers may only be allocated from interrupts using a @gfp_mask of
130  *	%GFP_ATOMIC.
131  */
132 struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)
133 {
134 	struct sk_buff *skb;
135 	u8 *data;
136 
137 	/* Get the HEAD */
138 	skb = kmem_cache_alloc(skbuff_head_cache,
139 			       gfp_mask & ~__GFP_DMA);
140 	if (!skb)
141 		goto out;
142 
143 	/* Get the DATA. Size must match skb_add_mtu(). */
144 	size = SKB_DATA_ALIGN(size);
145 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
146 	if (!data)
147 		goto nodata;
148 
149 	memset(skb, 0, offsetof(struct sk_buff, truesize));
150 	skb->truesize = size + sizeof(struct sk_buff);
151 	atomic_set(&skb->users, 1);
152 	skb->head = data;
153 	skb->data = data;
154 	skb->tail = data;
155 	skb->end  = data + size;
156 
157 	atomic_set(&(skb_shinfo(skb)->dataref), 1);
158 	skb_shinfo(skb)->nr_frags  = 0;
159 	skb_shinfo(skb)->tso_size = 0;
160 	skb_shinfo(skb)->tso_segs = 0;
161 	skb_shinfo(skb)->frag_list = NULL;
162 out:
163 	return skb;
164 nodata:
165 	kmem_cache_free(skbuff_head_cache, skb);
166 	skb = NULL;
167 	goto out;
168 }
169 
170 /**
171  *	alloc_skb_from_cache	-	allocate a network buffer
172  *	@cp: kmem_cache from which to allocate the data area
173  *           (object size must be big enough for @size bytes + skb overheads)
174  *	@size: size to allocate
175  *	@gfp_mask: allocation mask
176  *
177  *	Allocate a new &sk_buff. The returned buffer has no headroom and
178  *	tail room of size bytes. The object has a reference count of one.
179  *	The return is the buffer. On a failure the return is %NULL.
180  *
181  *	Buffers may only be allocated from interrupts using a @gfp_mask of
182  *	%GFP_ATOMIC.
183  */
184 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
185 				     unsigned int size, int gfp_mask)
186 {
187 	struct sk_buff *skb;
188 	u8 *data;
189 
190 	/* Get the HEAD */
191 	skb = kmem_cache_alloc(skbuff_head_cache,
192 			       gfp_mask & ~__GFP_DMA);
193 	if (!skb)
194 		goto out;
195 
196 	/* Get the DATA. */
197 	size = SKB_DATA_ALIGN(size);
198 	data = kmem_cache_alloc(cp, gfp_mask);
199 	if (!data)
200 		goto nodata;
201 
202 	memset(skb, 0, offsetof(struct sk_buff, truesize));
203 	skb->truesize = size + sizeof(struct sk_buff);
204 	atomic_set(&skb->users, 1);
205 	skb->head = data;
206 	skb->data = data;
207 	skb->tail = data;
208 	skb->end  = data + size;
209 
210 	atomic_set(&(skb_shinfo(skb)->dataref), 1);
211 	skb_shinfo(skb)->nr_frags  = 0;
212 	skb_shinfo(skb)->tso_size = 0;
213 	skb_shinfo(skb)->tso_segs = 0;
214 	skb_shinfo(skb)->frag_list = NULL;
215 out:
216 	return skb;
217 nodata:
218 	kmem_cache_free(skbuff_head_cache, skb);
219 	skb = NULL;
220 	goto out;
221 }
222 
223 
224 static void skb_drop_fraglist(struct sk_buff *skb)
225 {
226 	struct sk_buff *list = skb_shinfo(skb)->frag_list;
227 
228 	skb_shinfo(skb)->frag_list = NULL;
229 
230 	do {
231 		struct sk_buff *this = list;
232 		list = list->next;
233 		kfree_skb(this);
234 	} while (list);
235 }
236 
237 static void skb_clone_fraglist(struct sk_buff *skb)
238 {
239 	struct sk_buff *list;
240 
241 	for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
242 		skb_get(list);
243 }
244 
245 void skb_release_data(struct sk_buff *skb)
246 {
247 	if (!skb->cloned ||
248 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
249 			       &skb_shinfo(skb)->dataref)) {
250 		if (skb_shinfo(skb)->nr_frags) {
251 			int i;
252 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
253 				put_page(skb_shinfo(skb)->frags[i].page);
254 		}
255 
256 		if (skb_shinfo(skb)->frag_list)
257 			skb_drop_fraglist(skb);
258 
259 		kfree(skb->head);
260 	}
261 }
262 
263 /*
264  *	Free an skbuff by memory without cleaning the state.
265  */
266 void kfree_skbmem(struct sk_buff *skb)
267 {
268 	skb_release_data(skb);
269 	kmem_cache_free(skbuff_head_cache, skb);
270 }
271 
272 /**
273  *	__kfree_skb - private function
274  *	@skb: buffer
275  *
276  *	Free an sk_buff. Release anything attached to the buffer.
277  *	Clean the state. This is an internal helper function. Users should
278  *	always call kfree_skb
279  */
280 
281 void __kfree_skb(struct sk_buff *skb)
282 {
283 	BUG_ON(skb->list != NULL);
284 
285 	dst_release(skb->dst);
286 #ifdef CONFIG_XFRM
287 	secpath_put(skb->sp);
288 #endif
289 	if (skb->destructor) {
290 		WARN_ON(in_irq());
291 		skb->destructor(skb);
292 	}
293 #ifdef CONFIG_NETFILTER
294 	nf_conntrack_put(skb->nfct);
295 #ifdef CONFIG_BRIDGE_NETFILTER
296 	nf_bridge_put(skb->nf_bridge);
297 #endif
298 #endif
299 /* XXX: IS this still necessary? - JHS */
300 #ifdef CONFIG_NET_SCHED
301 	skb->tc_index = 0;
302 #ifdef CONFIG_NET_CLS_ACT
303 	skb->tc_verd = 0;
304 	skb->tc_classid = 0;
305 #endif
306 #endif
307 
308 	kfree_skbmem(skb);
309 }
310 
311 /**
312  *	skb_clone	-	duplicate an sk_buff
313  *	@skb: buffer to clone
314  *	@gfp_mask: allocation priority
315  *
316  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
317  *	copies share the same packet data but not structure. The new
318  *	buffer has a reference count of 1. If the allocation fails the
319  *	function returns %NULL otherwise the new buffer is returned.
320  *
321  *	If this function is called from an interrupt gfp_mask() must be
322  *	%GFP_ATOMIC.
323  */
324 
325 struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
326 {
327 	struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
328 
329 	if (!n)
330 		return NULL;
331 
332 #define C(x) n->x = skb->x
333 
334 	n->next = n->prev = NULL;
335 	n->list = NULL;
336 	n->sk = NULL;
337 	C(stamp);
338 	C(dev);
339 	C(real_dev);
340 	C(h);
341 	C(nh);
342 	C(mac);
343 	C(dst);
344 	dst_clone(skb->dst);
345 	C(sp);
346 #ifdef CONFIG_INET
347 	secpath_get(skb->sp);
348 #endif
349 	memcpy(n->cb, skb->cb, sizeof(skb->cb));
350 	C(len);
351 	C(data_len);
352 	C(csum);
353 	C(local_df);
354 	n->cloned = 1;
355 	n->nohdr = 0;
356 	C(pkt_type);
357 	C(ip_summed);
358 	C(priority);
359 	C(protocol);
360 	C(security);
361 	n->destructor = NULL;
362 #ifdef CONFIG_NETFILTER
363 	C(nfmark);
364 	C(nfcache);
365 	C(nfct);
366 	nf_conntrack_get(skb->nfct);
367 	C(nfctinfo);
368 #ifdef CONFIG_BRIDGE_NETFILTER
369 	C(nf_bridge);
370 	nf_bridge_get(skb->nf_bridge);
371 #endif
372 #endif /*CONFIG_NETFILTER*/
373 #if defined(CONFIG_HIPPI)
374 	C(private);
375 #endif
376 #ifdef CONFIG_NET_SCHED
377 	C(tc_index);
378 #ifdef CONFIG_NET_CLS_ACT
379 	n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
380 	n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
381 	n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
382 	C(input_dev);
383 	C(tc_classid);
384 #endif
385 
386 #endif
387 	C(truesize);
388 	atomic_set(&n->users, 1);
389 	C(head);
390 	C(data);
391 	C(tail);
392 	C(end);
393 
394 	atomic_inc(&(skb_shinfo(skb)->dataref));
395 	skb->cloned = 1;
396 
397 	return n;
398 }
399 
400 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
401 {
402 	/*
403 	 *	Shift between the two data areas in bytes
404 	 */
405 	unsigned long offset = new->data - old->data;
406 
407 	new->list	= NULL;
408 	new->sk		= NULL;
409 	new->dev	= old->dev;
410 	new->real_dev	= old->real_dev;
411 	new->priority	= old->priority;
412 	new->protocol	= old->protocol;
413 	new->dst	= dst_clone(old->dst);
414 #ifdef CONFIG_INET
415 	new->sp		= secpath_get(old->sp);
416 #endif
417 	new->h.raw	= old->h.raw + offset;
418 	new->nh.raw	= old->nh.raw + offset;
419 	new->mac.raw	= old->mac.raw + offset;
420 	memcpy(new->cb, old->cb, sizeof(old->cb));
421 	new->local_df	= old->local_df;
422 	new->pkt_type	= old->pkt_type;
423 	new->stamp	= old->stamp;
424 	new->destructor = NULL;
425 	new->security	= old->security;
426 #ifdef CONFIG_NETFILTER
427 	new->nfmark	= old->nfmark;
428 	new->nfcache	= old->nfcache;
429 	new->nfct	= old->nfct;
430 	nf_conntrack_get(old->nfct);
431 	new->nfctinfo	= old->nfctinfo;
432 #ifdef CONFIG_BRIDGE_NETFILTER
433 	new->nf_bridge	= old->nf_bridge;
434 	nf_bridge_get(old->nf_bridge);
435 #endif
436 #endif
437 #ifdef CONFIG_NET_SCHED
438 #ifdef CONFIG_NET_CLS_ACT
439 	new->tc_verd = old->tc_verd;
440 #endif
441 	new->tc_index	= old->tc_index;
442 #endif
443 	atomic_set(&new->users, 1);
444 	skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
445 	skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
446 }
447 
448 /**
449  *	skb_copy	-	create private copy of an sk_buff
450  *	@skb: buffer to copy
451  *	@gfp_mask: allocation priority
452  *
453  *	Make a copy of both an &sk_buff and its data. This is used when the
454  *	caller wishes to modify the data and needs a private copy of the
455  *	data to alter. Returns %NULL on failure or the pointer to the buffer
456  *	on success. The returned buffer has a reference count of 1.
457  *
458  *	As by-product this function converts non-linear &sk_buff to linear
459  *	one, so that &sk_buff becomes completely private and caller is allowed
460  *	to modify all the data of returned buffer. This means that this
461  *	function is not recommended for use in circumstances when only
462  *	header is going to be modified. Use pskb_copy() instead.
463  */
464 
465 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
466 {
467 	int headerlen = skb->data - skb->head;
468 	/*
469 	 *	Allocate the copy buffer
470 	 */
471 	struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
472 				      gfp_mask);
473 	if (!n)
474 		return NULL;
475 
476 	/* Set the data pointer */
477 	skb_reserve(n, headerlen);
478 	/* Set the tail pointer and length */
479 	skb_put(n, skb->len);
480 	n->csum	     = skb->csum;
481 	n->ip_summed = skb->ip_summed;
482 
483 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
484 		BUG();
485 
486 	copy_skb_header(n, skb);
487 	return n;
488 }
489 
490 
491 /**
492  *	pskb_copy	-	create copy of an sk_buff with private head.
493  *	@skb: buffer to copy
494  *	@gfp_mask: allocation priority
495  *
496  *	Make a copy of both an &sk_buff and part of its data, located
497  *	in header. Fragmented data remain shared. This is used when
498  *	the caller wishes to modify only header of &sk_buff and needs
499  *	private copy of the header to alter. Returns %NULL on failure
500  *	or the pointer to the buffer on success.
501  *	The returned buffer has a reference count of 1.
502  */
503 
504 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
505 {
506 	/*
507 	 *	Allocate the copy buffer
508 	 */
509 	struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
510 
511 	if (!n)
512 		goto out;
513 
514 	/* Set the data pointer */
515 	skb_reserve(n, skb->data - skb->head);
516 	/* Set the tail pointer and length */
517 	skb_put(n, skb_headlen(skb));
518 	/* Copy the bytes */
519 	memcpy(n->data, skb->data, n->len);
520 	n->csum	     = skb->csum;
521 	n->ip_summed = skb->ip_summed;
522 
523 	n->data_len  = skb->data_len;
524 	n->len	     = skb->len;
525 
526 	if (skb_shinfo(skb)->nr_frags) {
527 		int i;
528 
529 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
530 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
531 			get_page(skb_shinfo(n)->frags[i].page);
532 		}
533 		skb_shinfo(n)->nr_frags = i;
534 	}
535 
536 	if (skb_shinfo(skb)->frag_list) {
537 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
538 		skb_clone_fraglist(n);
539 	}
540 
541 	copy_skb_header(n, skb);
542 out:
543 	return n;
544 }
545 
546 /**
547  *	pskb_expand_head - reallocate header of &sk_buff
548  *	@skb: buffer to reallocate
549  *	@nhead: room to add at head
550  *	@ntail: room to add at tail
551  *	@gfp_mask: allocation priority
552  *
553  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
554  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
555  *	reference count of 1. Returns zero in the case of success or error,
556  *	if expansion failed. In the last case, &sk_buff is not changed.
557  *
558  *	All the pointers pointing into skb header may change and must be
559  *	reloaded after call to this function.
560  */
561 
562 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
563 {
564 	int i;
565 	u8 *data;
566 	int size = nhead + (skb->end - skb->head) + ntail;
567 	long off;
568 
569 	if (skb_shared(skb))
570 		BUG();
571 
572 	size = SKB_DATA_ALIGN(size);
573 
574 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
575 	if (!data)
576 		goto nodata;
577 
578 	/* Copy only real data... and, alas, header. This should be
579 	 * optimized for the cases when header is void. */
580 	memcpy(data + nhead, skb->head, skb->tail - skb->head);
581 	memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
582 
583 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
584 		get_page(skb_shinfo(skb)->frags[i].page);
585 
586 	if (skb_shinfo(skb)->frag_list)
587 		skb_clone_fraglist(skb);
588 
589 	skb_release_data(skb);
590 
591 	off = (data + nhead) - skb->head;
592 
593 	skb->head     = data;
594 	skb->end      = data + size;
595 	skb->data    += off;
596 	skb->tail    += off;
597 	skb->mac.raw += off;
598 	skb->h.raw   += off;
599 	skb->nh.raw  += off;
600 	skb->cloned   = 0;
601 	skb->nohdr    = 0;
602 	atomic_set(&skb_shinfo(skb)->dataref, 1);
603 	return 0;
604 
605 nodata:
606 	return -ENOMEM;
607 }
608 
609 /* Make private copy of skb with writable head and some headroom */
610 
611 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
612 {
613 	struct sk_buff *skb2;
614 	int delta = headroom - skb_headroom(skb);
615 
616 	if (delta <= 0)
617 		skb2 = pskb_copy(skb, GFP_ATOMIC);
618 	else {
619 		skb2 = skb_clone(skb, GFP_ATOMIC);
620 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
621 					     GFP_ATOMIC)) {
622 			kfree_skb(skb2);
623 			skb2 = NULL;
624 		}
625 	}
626 	return skb2;
627 }
628 
629 
630 /**
631  *	skb_copy_expand	-	copy and expand sk_buff
632  *	@skb: buffer to copy
633  *	@newheadroom: new free bytes at head
634  *	@newtailroom: new free bytes at tail
635  *	@gfp_mask: allocation priority
636  *
637  *	Make a copy of both an &sk_buff and its data and while doing so
638  *	allocate additional space.
639  *
640  *	This is used when the caller wishes to modify the data and needs a
641  *	private copy of the data to alter as well as more space for new fields.
642  *	Returns %NULL on failure or the pointer to the buffer
643  *	on success. The returned buffer has a reference count of 1.
644  *
645  *	You must pass %GFP_ATOMIC as the allocation priority if this function
646  *	is called from an interrupt.
647  *
648  *	BUG ALERT: ip_summed is not copied. Why does this work? Is it used
649  *	only by netfilter in the cases when checksum is recalculated? --ANK
650  */
651 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
652 				int newheadroom, int newtailroom, int gfp_mask)
653 {
654 	/*
655 	 *	Allocate the copy buffer
656 	 */
657 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
658 				      gfp_mask);
659 	int head_copy_len, head_copy_off;
660 
661 	if (!n)
662 		return NULL;
663 
664 	skb_reserve(n, newheadroom);
665 
666 	/* Set the tail pointer and length */
667 	skb_put(n, skb->len);
668 
669 	head_copy_len = skb_headroom(skb);
670 	head_copy_off = 0;
671 	if (newheadroom <= head_copy_len)
672 		head_copy_len = newheadroom;
673 	else
674 		head_copy_off = newheadroom - head_copy_len;
675 
676 	/* Copy the linear header and data. */
677 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
678 			  skb->len + head_copy_len))
679 		BUG();
680 
681 	copy_skb_header(n, skb);
682 
683 	return n;
684 }
685 
686 /**
687  *	skb_pad			-	zero pad the tail of an skb
688  *	@skb: buffer to pad
689  *	@pad: space to pad
690  *
691  *	Ensure that a buffer is followed by a padding area that is zero
692  *	filled. Used by network drivers which may DMA or transfer data
693  *	beyond the buffer end onto the wire.
694  *
695  *	May return NULL in out of memory cases.
696  */
697 
698 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
699 {
700 	struct sk_buff *nskb;
701 
702 	/* If the skbuff is non linear tailroom is always zero.. */
703 	if (skb_tailroom(skb) >= pad) {
704 		memset(skb->data+skb->len, 0, pad);
705 		return skb;
706 	}
707 
708 	nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
709 	kfree_skb(skb);
710 	if (nskb)
711 		memset(nskb->data+nskb->len, 0, pad);
712 	return nskb;
713 }
714 
715 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
716  * If realloc==0 and trimming is impossible without change of data,
717  * it is BUG().
718  */
719 
720 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
721 {
722 	int offset = skb_headlen(skb);
723 	int nfrags = skb_shinfo(skb)->nr_frags;
724 	int i;
725 
726 	for (i = 0; i < nfrags; i++) {
727 		int end = offset + skb_shinfo(skb)->frags[i].size;
728 		if (end > len) {
729 			if (skb_cloned(skb)) {
730 				if (!realloc)
731 					BUG();
732 				if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
733 					return -ENOMEM;
734 			}
735 			if (len <= offset) {
736 				put_page(skb_shinfo(skb)->frags[i].page);
737 				skb_shinfo(skb)->nr_frags--;
738 			} else {
739 				skb_shinfo(skb)->frags[i].size = len - offset;
740 			}
741 		}
742 		offset = end;
743 	}
744 
745 	if (offset < len) {
746 		skb->data_len -= skb->len - len;
747 		skb->len       = len;
748 	} else {
749 		if (len <= skb_headlen(skb)) {
750 			skb->len      = len;
751 			skb->data_len = 0;
752 			skb->tail     = skb->data + len;
753 			if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
754 				skb_drop_fraglist(skb);
755 		} else {
756 			skb->data_len -= skb->len - len;
757 			skb->len       = len;
758 		}
759 	}
760 
761 	return 0;
762 }
763 
764 /**
765  *	__pskb_pull_tail - advance tail of skb header
766  *	@skb: buffer to reallocate
767  *	@delta: number of bytes to advance tail
768  *
769  *	The function makes a sense only on a fragmented &sk_buff,
770  *	it expands header moving its tail forward and copying necessary
771  *	data from fragmented part.
772  *
773  *	&sk_buff MUST have reference count of 1.
774  *
775  *	Returns %NULL (and &sk_buff does not change) if pull failed
776  *	or value of new tail of skb in the case of success.
777  *
778  *	All the pointers pointing into skb header may change and must be
779  *	reloaded after call to this function.
780  */
781 
782 /* Moves tail of skb head forward, copying data from fragmented part,
783  * when it is necessary.
784  * 1. It may fail due to malloc failure.
785  * 2. It may change skb pointers.
786  *
787  * It is pretty complicated. Luckily, it is called only in exceptional cases.
788  */
789 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
790 {
791 	/* If skb has not enough free space at tail, get new one
792 	 * plus 128 bytes for future expansions. If we have enough
793 	 * room at tail, reallocate without expansion only if skb is cloned.
794 	 */
795 	int i, k, eat = (skb->tail + delta) - skb->end;
796 
797 	if (eat > 0 || skb_cloned(skb)) {
798 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
799 				     GFP_ATOMIC))
800 			return NULL;
801 	}
802 
803 	if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
804 		BUG();
805 
806 	/* Optimization: no fragments, no reasons to preestimate
807 	 * size of pulled pages. Superb.
808 	 */
809 	if (!skb_shinfo(skb)->frag_list)
810 		goto pull_pages;
811 
812 	/* Estimate size of pulled pages. */
813 	eat = delta;
814 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
815 		if (skb_shinfo(skb)->frags[i].size >= eat)
816 			goto pull_pages;
817 		eat -= skb_shinfo(skb)->frags[i].size;
818 	}
819 
820 	/* If we need update frag list, we are in troubles.
821 	 * Certainly, it possible to add an offset to skb data,
822 	 * but taking into account that pulling is expected to
823 	 * be very rare operation, it is worth to fight against
824 	 * further bloating skb head and crucify ourselves here instead.
825 	 * Pure masohism, indeed. 8)8)
826 	 */
827 	if (eat) {
828 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
829 		struct sk_buff *clone = NULL;
830 		struct sk_buff *insp = NULL;
831 
832 		do {
833 			if (!list)
834 				BUG();
835 
836 			if (list->len <= eat) {
837 				/* Eaten as whole. */
838 				eat -= list->len;
839 				list = list->next;
840 				insp = list;
841 			} else {
842 				/* Eaten partially. */
843 
844 				if (skb_shared(list)) {
845 					/* Sucks! We need to fork list. :-( */
846 					clone = skb_clone(list, GFP_ATOMIC);
847 					if (!clone)
848 						return NULL;
849 					insp = list->next;
850 					list = clone;
851 				} else {
852 					/* This may be pulled without
853 					 * problems. */
854 					insp = list;
855 				}
856 				if (!pskb_pull(list, eat)) {
857 					if (clone)
858 						kfree_skb(clone);
859 					return NULL;
860 				}
861 				break;
862 			}
863 		} while (eat);
864 
865 		/* Free pulled out fragments. */
866 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
867 			skb_shinfo(skb)->frag_list = list->next;
868 			kfree_skb(list);
869 		}
870 		/* And insert new clone at head. */
871 		if (clone) {
872 			clone->next = list;
873 			skb_shinfo(skb)->frag_list = clone;
874 		}
875 	}
876 	/* Success! Now we may commit changes to skb data. */
877 
878 pull_pages:
879 	eat = delta;
880 	k = 0;
881 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
882 		if (skb_shinfo(skb)->frags[i].size <= eat) {
883 			put_page(skb_shinfo(skb)->frags[i].page);
884 			eat -= skb_shinfo(skb)->frags[i].size;
885 		} else {
886 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
887 			if (eat) {
888 				skb_shinfo(skb)->frags[k].page_offset += eat;
889 				skb_shinfo(skb)->frags[k].size -= eat;
890 				eat = 0;
891 			}
892 			k++;
893 		}
894 	}
895 	skb_shinfo(skb)->nr_frags = k;
896 
897 	skb->tail     += delta;
898 	skb->data_len -= delta;
899 
900 	return skb->tail;
901 }
902 
903 /* Copy some data bits from skb to kernel buffer. */
904 
905 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
906 {
907 	int i, copy;
908 	int start = skb_headlen(skb);
909 
910 	if (offset > (int)skb->len - len)
911 		goto fault;
912 
913 	/* Copy header. */
914 	if ((copy = start - offset) > 0) {
915 		if (copy > len)
916 			copy = len;
917 		memcpy(to, skb->data + offset, copy);
918 		if ((len -= copy) == 0)
919 			return 0;
920 		offset += copy;
921 		to     += copy;
922 	}
923 
924 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
925 		int end;
926 
927 		BUG_TRAP(start <= offset + len);
928 
929 		end = start + skb_shinfo(skb)->frags[i].size;
930 		if ((copy = end - offset) > 0) {
931 			u8 *vaddr;
932 
933 			if (copy > len)
934 				copy = len;
935 
936 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
937 			memcpy(to,
938 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
939 			       offset - start, copy);
940 			kunmap_skb_frag(vaddr);
941 
942 			if ((len -= copy) == 0)
943 				return 0;
944 			offset += copy;
945 			to     += copy;
946 		}
947 		start = end;
948 	}
949 
950 	if (skb_shinfo(skb)->frag_list) {
951 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
952 
953 		for (; list; list = list->next) {
954 			int end;
955 
956 			BUG_TRAP(start <= offset + len);
957 
958 			end = start + list->len;
959 			if ((copy = end - offset) > 0) {
960 				if (copy > len)
961 					copy = len;
962 				if (skb_copy_bits(list, offset - start,
963 						  to, copy))
964 					goto fault;
965 				if ((len -= copy) == 0)
966 					return 0;
967 				offset += copy;
968 				to     += copy;
969 			}
970 			start = end;
971 		}
972 	}
973 	if (!len)
974 		return 0;
975 
976 fault:
977 	return -EFAULT;
978 }
979 
980 /**
981  *	skb_store_bits - store bits from kernel buffer to skb
982  *	@skb: destination buffer
983  *	@offset: offset in destination
984  *	@from: source buffer
985  *	@len: number of bytes to copy
986  *
987  *	Copy the specified number of bytes from the source buffer to the
988  *	destination skb.  This function handles all the messy bits of
989  *	traversing fragment lists and such.
990  */
991 
992 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
993 {
994 	int i, copy;
995 	int start = skb_headlen(skb);
996 
997 	if (offset > (int)skb->len - len)
998 		goto fault;
999 
1000 	if ((copy = start - offset) > 0) {
1001 		if (copy > len)
1002 			copy = len;
1003 		memcpy(skb->data + offset, from, copy);
1004 		if ((len -= copy) == 0)
1005 			return 0;
1006 		offset += copy;
1007 		from += copy;
1008 	}
1009 
1010 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1011 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1012 		int end;
1013 
1014 		BUG_TRAP(start <= offset + len);
1015 
1016 		end = start + frag->size;
1017 		if ((copy = end - offset) > 0) {
1018 			u8 *vaddr;
1019 
1020 			if (copy > len)
1021 				copy = len;
1022 
1023 			vaddr = kmap_skb_frag(frag);
1024 			memcpy(vaddr + frag->page_offset + offset - start,
1025 			       from, copy);
1026 			kunmap_skb_frag(vaddr);
1027 
1028 			if ((len -= copy) == 0)
1029 				return 0;
1030 			offset += copy;
1031 			from += 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_store_bits(list, offset - start,
1049 						   from, copy))
1050 					goto fault;
1051 				if ((len -= copy) == 0)
1052 					return 0;
1053 				offset += copy;
1054 				from += copy;
1055 			}
1056 			start = end;
1057 		}
1058 	}
1059 	if (!len)
1060 		return 0;
1061 
1062 fault:
1063 	return -EFAULT;
1064 }
1065 
1066 EXPORT_SYMBOL(skb_store_bits);
1067 
1068 /* Checksum skb data. */
1069 
1070 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1071 			  int len, unsigned int csum)
1072 {
1073 	int start = skb_headlen(skb);
1074 	int i, copy = start - offset;
1075 	int pos = 0;
1076 
1077 	/* Checksum header. */
1078 	if (copy > 0) {
1079 		if (copy > len)
1080 			copy = len;
1081 		csum = csum_partial(skb->data + offset, copy, csum);
1082 		if ((len -= copy) == 0)
1083 			return csum;
1084 		offset += copy;
1085 		pos	= copy;
1086 	}
1087 
1088 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1089 		int end;
1090 
1091 		BUG_TRAP(start <= offset + len);
1092 
1093 		end = start + skb_shinfo(skb)->frags[i].size;
1094 		if ((copy = end - offset) > 0) {
1095 			unsigned int csum2;
1096 			u8 *vaddr;
1097 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1098 
1099 			if (copy > len)
1100 				copy = len;
1101 			vaddr = kmap_skb_frag(frag);
1102 			csum2 = csum_partial(vaddr + frag->page_offset +
1103 					     offset - start, copy, 0);
1104 			kunmap_skb_frag(vaddr);
1105 			csum = csum_block_add(csum, csum2, pos);
1106 			if (!(len -= copy))
1107 				return csum;
1108 			offset += copy;
1109 			pos    += copy;
1110 		}
1111 		start = end;
1112 	}
1113 
1114 	if (skb_shinfo(skb)->frag_list) {
1115 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1116 
1117 		for (; list; list = list->next) {
1118 			int end;
1119 
1120 			BUG_TRAP(start <= offset + len);
1121 
1122 			end = start + list->len;
1123 			if ((copy = end - offset) > 0) {
1124 				unsigned int csum2;
1125 				if (copy > len)
1126 					copy = len;
1127 				csum2 = skb_checksum(list, offset - start,
1128 						     copy, 0);
1129 				csum = csum_block_add(csum, csum2, pos);
1130 				if ((len -= copy) == 0)
1131 					return csum;
1132 				offset += copy;
1133 				pos    += copy;
1134 			}
1135 			start = end;
1136 		}
1137 	}
1138 	if (len)
1139 		BUG();
1140 
1141 	return csum;
1142 }
1143 
1144 /* Both of above in one bottle. */
1145 
1146 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1147 				    u8 *to, int len, unsigned int csum)
1148 {
1149 	int start = skb_headlen(skb);
1150 	int i, copy = start - offset;
1151 	int pos = 0;
1152 
1153 	/* Copy header. */
1154 	if (copy > 0) {
1155 		if (copy > len)
1156 			copy = len;
1157 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
1158 						 copy, csum);
1159 		if ((len -= copy) == 0)
1160 			return csum;
1161 		offset += copy;
1162 		to     += copy;
1163 		pos	= copy;
1164 	}
1165 
1166 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1167 		int end;
1168 
1169 		BUG_TRAP(start <= offset + len);
1170 
1171 		end = start + skb_shinfo(skb)->frags[i].size;
1172 		if ((copy = end - offset) > 0) {
1173 			unsigned int csum2;
1174 			u8 *vaddr;
1175 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1176 
1177 			if (copy > len)
1178 				copy = len;
1179 			vaddr = kmap_skb_frag(frag);
1180 			csum2 = csum_partial_copy_nocheck(vaddr +
1181 							  frag->page_offset +
1182 							  offset - start, to,
1183 							  copy, 0);
1184 			kunmap_skb_frag(vaddr);
1185 			csum = csum_block_add(csum, csum2, pos);
1186 			if (!(len -= copy))
1187 				return csum;
1188 			offset += copy;
1189 			to     += copy;
1190 			pos    += copy;
1191 		}
1192 		start = end;
1193 	}
1194 
1195 	if (skb_shinfo(skb)->frag_list) {
1196 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1197 
1198 		for (; list; list = list->next) {
1199 			unsigned int csum2;
1200 			int end;
1201 
1202 			BUG_TRAP(start <= offset + len);
1203 
1204 			end = start + list->len;
1205 			if ((copy = end - offset) > 0) {
1206 				if (copy > len)
1207 					copy = len;
1208 				csum2 = skb_copy_and_csum_bits(list,
1209 							       offset - start,
1210 							       to, copy, 0);
1211 				csum = csum_block_add(csum, csum2, pos);
1212 				if ((len -= copy) == 0)
1213 					return csum;
1214 				offset += copy;
1215 				to     += copy;
1216 				pos    += copy;
1217 			}
1218 			start = end;
1219 		}
1220 	}
1221 	if (len)
1222 		BUG();
1223 	return csum;
1224 }
1225 
1226 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1227 {
1228 	unsigned int csum;
1229 	long csstart;
1230 
1231 	if (skb->ip_summed == CHECKSUM_HW)
1232 		csstart = skb->h.raw - skb->data;
1233 	else
1234 		csstart = skb_headlen(skb);
1235 
1236 	if (csstart > skb_headlen(skb))
1237 		BUG();
1238 
1239 	memcpy(to, skb->data, csstart);
1240 
1241 	csum = 0;
1242 	if (csstart != skb->len)
1243 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1244 					      skb->len - csstart, 0);
1245 
1246 	if (skb->ip_summed == CHECKSUM_HW) {
1247 		long csstuff = csstart + skb->csum;
1248 
1249 		*((unsigned short *)(to + csstuff)) = csum_fold(csum);
1250 	}
1251 }
1252 
1253 /**
1254  *	skb_dequeue - remove from the head of the queue
1255  *	@list: list to dequeue from
1256  *
1257  *	Remove the head of the list. The list lock is taken so the function
1258  *	may be used safely with other locking list functions. The head item is
1259  *	returned or %NULL if the list is empty.
1260  */
1261 
1262 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1263 {
1264 	unsigned long flags;
1265 	struct sk_buff *result;
1266 
1267 	spin_lock_irqsave(&list->lock, flags);
1268 	result = __skb_dequeue(list);
1269 	spin_unlock_irqrestore(&list->lock, flags);
1270 	return result;
1271 }
1272 
1273 /**
1274  *	skb_dequeue_tail - remove from the tail of the queue
1275  *	@list: list to dequeue from
1276  *
1277  *	Remove the tail of the list. The list lock is taken so the function
1278  *	may be used safely with other locking list functions. The tail item is
1279  *	returned or %NULL if the list is empty.
1280  */
1281 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1282 {
1283 	unsigned long flags;
1284 	struct sk_buff *result;
1285 
1286 	spin_lock_irqsave(&list->lock, flags);
1287 	result = __skb_dequeue_tail(list);
1288 	spin_unlock_irqrestore(&list->lock, flags);
1289 	return result;
1290 }
1291 
1292 /**
1293  *	skb_queue_purge - empty a list
1294  *	@list: list to empty
1295  *
1296  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
1297  *	the list and one reference dropped. This function takes the list
1298  *	lock and is atomic with respect to other list locking functions.
1299  */
1300 void skb_queue_purge(struct sk_buff_head *list)
1301 {
1302 	struct sk_buff *skb;
1303 	while ((skb = skb_dequeue(list)) != NULL)
1304 		kfree_skb(skb);
1305 }
1306 
1307 /**
1308  *	skb_queue_head - queue a buffer at the list head
1309  *	@list: list to use
1310  *	@newsk: buffer to queue
1311  *
1312  *	Queue a buffer at the start of the list. This function takes the
1313  *	list lock and can be used safely with other locking &sk_buff functions
1314  *	safely.
1315  *
1316  *	A buffer cannot be placed on two lists at the same time.
1317  */
1318 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1319 {
1320 	unsigned long flags;
1321 
1322 	spin_lock_irqsave(&list->lock, flags);
1323 	__skb_queue_head(list, newsk);
1324 	spin_unlock_irqrestore(&list->lock, flags);
1325 }
1326 
1327 /**
1328  *	skb_queue_tail - queue a buffer at the list tail
1329  *	@list: list to use
1330  *	@newsk: buffer to queue
1331  *
1332  *	Queue a buffer at the tail of the list. This function takes the
1333  *	list lock and can be used safely with other locking &sk_buff functions
1334  *	safely.
1335  *
1336  *	A buffer cannot be placed on two lists at the same time.
1337  */
1338 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1339 {
1340 	unsigned long flags;
1341 
1342 	spin_lock_irqsave(&list->lock, flags);
1343 	__skb_queue_tail(list, newsk);
1344 	spin_unlock_irqrestore(&list->lock, flags);
1345 }
1346 /**
1347  *	skb_unlink	-	remove a buffer from a list
1348  *	@skb: buffer to remove
1349  *
1350  *	Place a packet after a given packet in a list. The list locks are taken
1351  *	and this function is atomic with respect to other list locked calls
1352  *
1353  *	Works even without knowing the list it is sitting on, which can be
1354  *	handy at times. It also means that THE LIST MUST EXIST when you
1355  *	unlink. Thus a list must have its contents unlinked before it is
1356  *	destroyed.
1357  */
1358 void skb_unlink(struct sk_buff *skb)
1359 {
1360 	struct sk_buff_head *list = skb->list;
1361 
1362 	if (list) {
1363 		unsigned long flags;
1364 
1365 		spin_lock_irqsave(&list->lock, flags);
1366 		if (skb->list == list)
1367 			__skb_unlink(skb, skb->list);
1368 		spin_unlock_irqrestore(&list->lock, flags);
1369 	}
1370 }
1371 
1372 
1373 /**
1374  *	skb_append	-	append a buffer
1375  *	@old: buffer to insert after
1376  *	@newsk: buffer to insert
1377  *
1378  *	Place a packet after a given packet in a list. The list locks are taken
1379  *	and this function is atomic with respect to other list locked calls.
1380  *	A buffer cannot be placed on two lists at the same time.
1381  */
1382 
1383 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1384 {
1385 	unsigned long flags;
1386 
1387 	spin_lock_irqsave(&old->list->lock, flags);
1388 	__skb_append(old, newsk);
1389 	spin_unlock_irqrestore(&old->list->lock, flags);
1390 }
1391 
1392 
1393 /**
1394  *	skb_insert	-	insert a buffer
1395  *	@old: buffer to insert before
1396  *	@newsk: buffer to insert
1397  *
1398  *	Place a packet before a given packet in a list. The list locks are taken
1399  *	and this function is atomic with respect to other list locked calls
1400  *	A buffer cannot be placed on two lists at the same time.
1401  */
1402 
1403 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1404 {
1405 	unsigned long flags;
1406 
1407 	spin_lock_irqsave(&old->list->lock, flags);
1408 	__skb_insert(newsk, old->prev, old, old->list);
1409 	spin_unlock_irqrestore(&old->list->lock, flags);
1410 }
1411 
1412 #if 0
1413 /*
1414  * 	Tune the memory allocator for a new MTU size.
1415  */
1416 void skb_add_mtu(int mtu)
1417 {
1418 	/* Must match allocation in alloc_skb */
1419 	mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1420 
1421 	kmem_add_cache_size(mtu);
1422 }
1423 #endif
1424 
1425 static inline void skb_split_inside_header(struct sk_buff *skb,
1426 					   struct sk_buff* skb1,
1427 					   const u32 len, const int pos)
1428 {
1429 	int i;
1430 
1431 	memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1432 
1433 	/* And move data appendix as is. */
1434 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1435 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1436 
1437 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1438 	skb_shinfo(skb)->nr_frags  = 0;
1439 	skb1->data_len		   = skb->data_len;
1440 	skb1->len		   += skb1->data_len;
1441 	skb->data_len		   = 0;
1442 	skb->len		   = len;
1443 	skb->tail		   = skb->data + len;
1444 }
1445 
1446 static inline void skb_split_no_header(struct sk_buff *skb,
1447 				       struct sk_buff* skb1,
1448 				       const u32 len, int pos)
1449 {
1450 	int i, k = 0;
1451 	const int nfrags = skb_shinfo(skb)->nr_frags;
1452 
1453 	skb_shinfo(skb)->nr_frags = 0;
1454 	skb1->len		  = skb1->data_len = skb->len - len;
1455 	skb->len		  = len;
1456 	skb->data_len		  = len - pos;
1457 
1458 	for (i = 0; i < nfrags; i++) {
1459 		int size = skb_shinfo(skb)->frags[i].size;
1460 
1461 		if (pos + size > len) {
1462 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1463 
1464 			if (pos < len) {
1465 				/* Split frag.
1466 				 * We have two variants in this case:
1467 				 * 1. Move all the frag to the second
1468 				 *    part, if it is possible. F.e.
1469 				 *    this approach is mandatory for TUX,
1470 				 *    where splitting is expensive.
1471 				 * 2. Split is accurately. We make this.
1472 				 */
1473 				get_page(skb_shinfo(skb)->frags[i].page);
1474 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1475 				skb_shinfo(skb1)->frags[0].size -= len - pos;
1476 				skb_shinfo(skb)->frags[i].size	= len - pos;
1477 				skb_shinfo(skb)->nr_frags++;
1478 			}
1479 			k++;
1480 		} else
1481 			skb_shinfo(skb)->nr_frags++;
1482 		pos += size;
1483 	}
1484 	skb_shinfo(skb1)->nr_frags = k;
1485 }
1486 
1487 /**
1488  * skb_split - Split fragmented skb to two parts at length len.
1489  * @skb: the buffer to split
1490  * @skb1: the buffer to receive the second part
1491  * @len: new length for skb
1492  */
1493 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1494 {
1495 	int pos = skb_headlen(skb);
1496 
1497 	if (len < pos)	/* Split line is inside header. */
1498 		skb_split_inside_header(skb, skb1, len, pos);
1499 	else		/* Second chunk has no header, nothing to copy. */
1500 		skb_split_no_header(skb, skb1, len, pos);
1501 }
1502 
1503 void __init skb_init(void)
1504 {
1505 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1506 					      sizeof(struct sk_buff),
1507 					      0,
1508 					      SLAB_HWCACHE_ALIGN,
1509 					      NULL, NULL);
1510 	if (!skbuff_head_cache)
1511 		panic("cannot create skbuff cache");
1512 }
1513 
1514 EXPORT_SYMBOL(___pskb_trim);
1515 EXPORT_SYMBOL(__kfree_skb);
1516 EXPORT_SYMBOL(__pskb_pull_tail);
1517 EXPORT_SYMBOL(alloc_skb);
1518 EXPORT_SYMBOL(pskb_copy);
1519 EXPORT_SYMBOL(pskb_expand_head);
1520 EXPORT_SYMBOL(skb_checksum);
1521 EXPORT_SYMBOL(skb_clone);
1522 EXPORT_SYMBOL(skb_clone_fraglist);
1523 EXPORT_SYMBOL(skb_copy);
1524 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1525 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1526 EXPORT_SYMBOL(skb_copy_bits);
1527 EXPORT_SYMBOL(skb_copy_expand);
1528 EXPORT_SYMBOL(skb_over_panic);
1529 EXPORT_SYMBOL(skb_pad);
1530 EXPORT_SYMBOL(skb_realloc_headroom);
1531 EXPORT_SYMBOL(skb_under_panic);
1532 EXPORT_SYMBOL(skb_dequeue);
1533 EXPORT_SYMBOL(skb_dequeue_tail);
1534 EXPORT_SYMBOL(skb_insert);
1535 EXPORT_SYMBOL(skb_queue_purge);
1536 EXPORT_SYMBOL(skb_queue_head);
1537 EXPORT_SYMBOL(skb_queue_tail);
1538 EXPORT_SYMBOL(skb_unlink);
1539 EXPORT_SYMBOL(skb_append);
1540 EXPORT_SYMBOL(skb_split);
1541