1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		A saner implementation of the skbuff stuff scattered everywhere
 *		in the old NET2D code.
 *
 *	Authors:	Alan Cox <iiitac@pyr.swan.ac.uk>
 *
 *	Fixes:
 *		Alan Cox	:	Tracks memory and number of buffers for kernel memory report
 *					and memory leak hunting.
 *		Alan Cox	:	More generic kfree handler
 */

#include <linux/config.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/in.h>
#include "inet.h"
#include "dev.h"
#include "ip.h"
#include "protocol.h"
#include "arp.h"
#include "route.h"
#include "tcp.h"
#include "udp.h"
#include "skbuff.h"
#include "sock.h"


/* Socket buffer operations. Ideally much of this list swap stuff ought to be using
   exch instructions on the 386, and CAS/CAS2 on a 68K. This is the boring generic
   slow C version. No doubt when Linus sees this comment he'll do horrible things
   to this code 8-)
*/

/*
 *	Resource tracking variables
 */

volatile unsigned long net_memory=0;
volatile unsigned long net_skbcount=0;

/*
 *	Debugging paranoia. Can go later when this crud stack works
 */



void skb_check(struct sk_buff *skb, int line, char *file)
{
	if(skb->magic_debug_cookie==SK_FREED_SKB)
	{
		printk("File: %s Line %d, found a freed skb lurking in the undergrowth!\n",
			file,line);
		printk("skb=%p, real size=%ld, claimed size=%ld, magic=%d, list=%p, free=%d\n",
			skb,skb->truesize,skb->mem_len,skb->magic,skb->list,skb->free);
	}
	if(skb->magic_debug_cookie!=SK_GOOD_SKB)
	{
		printk("File: %s Line %d, passed a non skb!\n", file,line);
		printk("skb=%p, real size=%ld, claimed size=%ld, magic=%d, list=%p, free=%d\n",
			skb,skb->truesize,skb->mem_len,skb->magic,skb->list,skb->free);
	}
	if(skb->mem_len!=skb->truesize)
	{
		printk("File: %s Line %d, Dubious size setting!\n",file,line);
		printk("skb=%p, real size=%ld, claimed size=%ld, magic=%d, list=%p\n",
			skb,skb->truesize,skb->mem_len,skb->magic,skb->list);
	}
	/* Guess it might be acceptable then */
}

/*
 *	Insert an sk_buff at the start of a list.
 */

void skb_queue_head(struct sk_buff *volatile* list,struct sk_buff *newsk)
{
	unsigned long flags;

	IS_SKB(newsk);
	if(newsk->list)
		printk("Suspicious queue head: sk_buff on list!\n");
	save_flags(flags);
	cli();
	newsk->list=list;

	newsk->next=*list;

	if(*list)
		newsk->prev=(*list)->prev;
	else
		newsk->prev=newsk;
	newsk->prev->next=newsk;
	newsk->next->prev=newsk;
	IS_SKB(newsk->prev);
	IS_SKB(newsk->next);
	*list=newsk;
	restore_flags(flags);
}

/*
 *	Insert an sk_buff at the end of a list.
 */

void skb_queue_tail(struct sk_buff *volatile* list, struct sk_buff *newsk)
{
	unsigned long flags;

	if(newsk->list)
		printk("Suspicious queue tail: sk_buff on list!\n");

	IS_SKB(newsk);
	save_flags(flags);
	cli();

	newsk->list=list;
	if(*list)
	{
		(*list)->prev->next=newsk;
		newsk->prev=(*list)->prev;
		newsk->next=*list;
		(*list)->prev=newsk;
	}
	else
	{
		newsk->next=newsk;
		newsk->prev=newsk;
		*list=newsk;
	}
	IS_SKB(newsk->prev);
	IS_SKB(newsk->next);
	restore_flags(flags);

}

/*
 *	Remove an sk_buff from a list. This routine is also interrupt safe
 *	so you can grab read and free buffers as another process adds them.
 */

struct sk_buff *skb_dequeue(struct sk_buff *volatile* list)
{
	long flags;
	struct sk_buff *result;

	save_flags(flags);
	cli();

	if(*list==NULL)
	{
		restore_flags(flags);
		return(NULL);
	}

	result=*list;
	if(result->next==result)
		*list=NULL;
	else
	{
		result->next->prev=result->prev;
		result->prev->next=result->next;
		*list=result->next;
	}

	IS_SKB(result);
	restore_flags(flags);

	if(result->list!=list)
		printk("Dequeued packet has invalid list pointer\n");

	result->list=0;
	result->next=0;
	result->prev=0;
	return(result);
}

/*
 *	Insert a packet before another one in a list.
 */

void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
{
	unsigned long flags;

	IS_SKB(old);
	IS_SKB(newsk);

	if(!old->list)
		printk("insert before unlisted item!\n");
	if(newsk->list)
		printk("inserted item is already on a list.\n");

	save_flags(flags);
	cli();
	newsk->list=old->list;
	newsk->next=old;
	newsk->prev=old->prev;
	newsk->next->prev=newsk;
	newsk->prev->next=newsk;

	restore_flags(flags);
}

/*
 *	Place a packet after a given packet in a list.
 */

void skb_append(struct sk_buff *old, struct sk_buff *newsk)
{
	unsigned long flags;

	IS_SKB(old);
	IS_SKB(newsk);

	if(!old->list)
		printk("append before unlisted item!\n");
	if(newsk->list)
		printk("append item is already on a list.\n");

	save_flags(flags);
	cli();
	newsk->list=old->list;
	newsk->prev=old;
	newsk->next=old->next;
	newsk->next->prev=newsk;
	newsk->prev->next=newsk;

	restore_flags(flags);
}

/*
 *	Remove an sk_buff from its list. Works even without knowing the list it
 *	is sitting on, which can be handy at times. It also means that THE LIST
 *	MUST EXIST when you unlink. Thus a list must have its contents unlinked
 *	_FIRST_.
 */

void skb_unlink(struct sk_buff *skb)
{
	unsigned long flags;
	save_flags(flags);
	cli();

	IS_SKB(skb);

	if(skb->list)
	{
		skb->next->prev=skb->prev;
		skb->prev->next=skb->next;
		if(*skb->list==skb)
		{
			if(skb->next==skb)
				*skb->list=NULL;
			else
				*skb->list=skb->next;
		}
		skb->next=0;
		skb->prev=0;
		skb->list=0;
	}
	restore_flags(flags);
}

/*
 *	An skbuff list has had its head reassigned. Move all the list
 *	pointers. Must be called with ints off during the whole head
 *	shifting
 */

void skb_new_list_head(struct sk_buff *volatile* list)
{
	struct sk_buff *skb=skb_peek(list);
	if(skb!=NULL)
	{
		do
		{
			IS_SKB(skb);
			skb->list=list;
			skb=skb->next;
		}
		while(skb!=*list);
	}
}

/*
 *	Peek an sk_buff. Unlike most other operations you _MUST_
 *	be careful with this one. A peek leaves the buffer on the
 *	list and someone else may run off with it. For an interrupt
 *	type system cli() peek the buffer copy the data and sti();
 */

struct sk_buff *skb_peek(struct sk_buff *volatile* list)
{
	return *list;
}

/*
 *	Get a clone of an sk_buff. This is the safe way to peek at
 *	a socket queue without accidents. Its a bit long but most
 *	of it acutally ends up as tiny bits of inline assembler
 *	anyway. Only the memcpy of upto 4K with ints off is not
 *	as nice as I'd like.
 */

struct sk_buff *skb_peek_copy(struct sk_buff *volatile* list)
{
	struct sk_buff *orig,*newsk;
	unsigned long flags;
	unsigned int len;
	/* Now for some games to avoid races */

	do
	{
		save_flags(flags);
		cli();
		orig=skb_peek(list);
		if(orig==NULL)
		{
			restore_flags(flags);
			return NULL;
		}
		IS_SKB(orig);
		len=orig->truesize;
		restore_flags(flags);

		newsk=alloc_skb(len,GFP_KERNEL);	/* May sleep */

		if(newsk==NULL)		/* Oh dear... not to worry */
			return NULL;

		save_flags(flags);
		cli();
		if(skb_peek(list)!=orig)	/* List changed go around another time */
		{
			restore_flags(flags);
			newsk->sk=NULL;
			newsk->free=1;
			newsk->mem_addr=newsk;
			newsk->mem_len=len;
			kfree_skb(newsk, FREE_WRITE);
			continue;
		}

		IS_SKB(orig);
		IS_SKB(newsk);
		memcpy(newsk,orig,len);
		newsk->list=NULL;
		newsk->magic=0;
		newsk->next=NULL;
		newsk->prev=NULL;
		newsk->mem_addr=newsk;
		newsk->h.raw+=((char *)newsk-(char *)orig);
		newsk->link3=NULL;
		newsk->sk=NULL;
		newsk->free=1;
	}
	while(0);

	restore_flags(flags);
	return(newsk);
}

/*
 *	Free an sk_buff. This still knows about things it should
 *	not need to like protocols and sockets.
 */

void kfree_skb(struct sk_buff *skb, int rw)
{
	if (skb == NULL) {
		printk("kfree_skb: skb = NULL\n");
		return;
	}
	IS_SKB(skb);
	if(skb->lock)
	{
		skb->free=1;	/* Free when unlocked */
		return;
	}
	
	if(skb->free == 2)
		printk("Warning: kfree_skb passed an skb that nobody set the free flag on!\n");
	if(skb->list)
		printk("Warning: kfree_skb passed an skb still on a list.\n");
	skb->magic = 0;
	if (skb->sk)
	{
		if(skb->sk->prot!=NULL)
		{
			if (rw)
				skb->sk->prot->rfree(skb->sk, skb->mem_addr, skb->mem_len);
			else
				skb->sk->prot->wfree(skb->sk, skb->mem_addr, skb->mem_len);

		}
		else
		{
			/* Non INET - default wmalloc/rmalloc handler */
			if (rw)
				skb->sk->rmem_alloc-=skb->mem_len;
			else
				skb->sk->wmem_alloc-=skb->mem_len;
			if(!skb->sk->dead)
			wake_up_interruptible(skb->sk->sleep);
			kfree_skbmem(skb->mem_addr,skb->mem_len);
		}
	}
	else
		kfree_skbmem(skb->mem_addr, skb->mem_len);
}

/*
 *	Allocate a new skbuff. We do this ourselves so we can fill in a few 'private'
 *	fields and also do memory statistics to find all the [BEEP] leaks.
 */

struct sk_buff *alloc_skb(unsigned int size,int priority)
{
	struct sk_buff *skb;
	extern unsigned long intr_count;

	if (intr_count && priority != GFP_ATOMIC) {
		printk("alloc_skb called nonatomically from interrupt %08lx\n",
			((unsigned long *)&size)[-1]);
		priority = GFP_ATOMIC;
	}
	skb=(struct sk_buff *)kmalloc(size,priority);
	if(skb==NULL)
		return NULL;
	skb->free= 2;	/* Invalid so we pick up forgetful users */
	skb->list= 0;	/* Not on a list */
	skb->lock= 0;
	skb->truesize=size;
	skb->mem_len=size;
	skb->mem_addr=skb;
	skb->fraglist=NULL;
	net_memory+=size;
	net_skbcount++;
	skb->magic_debug_cookie=SK_GOOD_SKB;
	skb->users=0;
	return skb;
}

/*
 *	Free an skbuff by memory
 */

void kfree_skbmem(void *mem,unsigned size)
{
	struct sk_buff *x=mem;
	IS_SKB(x);
	if(x->magic_debug_cookie==SK_GOOD_SKB)
	{
		x->magic_debug_cookie=SK_FREED_SKB;
		kfree_s(mem,size);
		net_skbcount--;
		net_memory-=size;
	}
}

/*
 *	Skbuff device locking
 */
 
void skb_kept_by_device(struct sk_buff *skb)
{
	skb->lock++;
}

void skb_device_release(struct sk_buff *skb, int mode)
{
	unsigned long flags;

	save_flags(flags);
	cli();
	if (!--skb->lock) {
		if (skb->free==1)
			kfree_skb(skb,mode);
	}
	restore_flags(flags);
}

int skb_device_locked(struct sk_buff *skb)
{
	if(skb->lock)
		return 1;
	return 0;
}
那是我日夜思念 深深愛著的人啊