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