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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
/*
 * 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.
 *
 *		Interface (streams) handling functions.
 *
 * Version:	@(#)dev.c	1.0.19	05/31/93
 *
 * Authors:	Ross Biro, <bir7@leland.Stanford.Edu>
 *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *		Mark Evans, <evansmp@uhura.aston.ac.uk>
 * 
 * Fixes:	
 *		Alan Cox:	check_addr returns a value for a wrong subnet
 *				ie not us but don't forward this!
 *		Alan Cox:	block timer if the inet_bh handler is running
 *		Alan Cox:	generic queue code added. A lot neater now
 *		C.E.Hawkins:	SIOCGIFCONF only reports 'upped' interfaces
 *		C.E.Hawkins:	IFF_PROMISC support
 *		Alan Cox:	Supports Donald Beckers new hardware 
 *				multicast layer, but not yet multicast lists.
 *		Alan Cox:	ip_addr_match problems with class A/B nets.
 *		C.E.Hawkins	IP 0.0.0.0 and also same net route fix. [FIXME: Ought to cause ICMP_REDIRECT]
 *		Alan Cox:	Removed bogus subnet check now the subnet code
 *				a) actually works for all A/B nets
 *				b) doesn't forward off the same interface.
 *		Alan Cox:	Multiple extra protocols
 *		Alan Cox:	Fixed ifconfig up of dud device setting the up flag
 *		Alan Cox:	Fixed verify_area errors
 *		Alan Cox:	Removed IP_SET_DEV as per Fred's comment. I hope this doesn't give
 *				anything away 8)
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 */
#include <asm/segment.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <linux/config.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/if_ether.h>
#include "inet.h"
#include "dev.h"
#include "eth.h"
#include "ip.h"
#include "route.h"
#include "protocol.h"
#include "tcp.h"
#include "skbuff.h"
#include "sock.h"
#include "arp.h"
#ifdef CONFIG_AX25
#include "ax25.h"
#endif


#ifdef CONFIG_IPX

static struct packet_type ipx_8023_type = {
  NET16(ETH_P_802_3),
  0,
  ipx_rcv,
  NULL,
  NULL
};

static struct packet_type ipx_packet_type = {
  NET16(ETH_P_IPX),
  0,
  ipx_rcv,
  NULL,
  &ipx_8023_type
};

#endif

#ifdef CONFIG_AX25

static struct packet_type ax25_packet_type = {
  NET16(ETH_P_AX25),
  0,
  ax25_rcv,
  NULL,
#ifdef CONFIG_IPX
  &ipx_packet_type
#else
  NULL
#endif
};
#endif


static struct packet_type arp_packet_type = {
  NET16(ETH_P_ARP),
  0,		/* copy */
  arp_rcv,
  NULL,
#ifdef CONFIG_IPX
#ifndef CONFIG_AX25
  &ipx_packet_type
#else
  &ax25_packet_type
#endif
#else
#ifdef CONFIG_AX25
  &ax25_packet_type
#else
  NULL		/* next */
#endif
#endif
};


static struct packet_type ip_packet_type = {
  NET16(ETH_P_IP),
  0,		/* copy */
  ip_rcv,
  NULL,
  &arp_packet_type
};
   

struct packet_type *ptype_base = &ip_packet_type;
static struct sk_buff *volatile backlog = NULL;
static unsigned long ip_bcast = 0;


/* Return the lesser of the two values. */
static unsigned long
min(unsigned long a, unsigned long b)
{
  if (a < b) return(a);
  return(b);
}


/* Determine a default network mask, based on the IP address. */
static unsigned long
get_mask(unsigned long addr)
{
  unsigned long dst;

  if (addr == 0L) 
  	return(0L);	/* special case */

  dst = ntohl(addr);
  if (IN_CLASSA(dst)) 
  	return(htonl(IN_CLASSA_NET));
  if (IN_CLASSB(dst)) 
  	return(htonl(IN_CLASSB_NET));
  if (IN_CLASSC(dst)) 
  	return(htonl(IN_CLASSC_NET));
  
  /* Something else, probably a subnet. */
  return(0);
}


int
ip_addr_match(unsigned long me, unsigned long him)
{
  int i;
  unsigned long mask=0xFFFFFFFF;
  DPRINTF((DBG_DEV, "ip_addr_match(%s, ", in_ntoa(me)));
  DPRINTF((DBG_DEV, "%s)\n", in_ntoa(him)));

  if (me == him) 
  	return(1);
  for (i = 0; i < 4; i++, me >>= 8, him >>= 8, mask >>= 8) {
	if ((me & 0xFF) != (him & 0xFF)) {
		/*
		 * The only way this could be a match is for
		 * the rest of addr1 to be 0 or 255.
		 */
		if (me != 0 && me != mask) return(0);
		return(1);
	}
  }
  return(1);
}


/* Check the address for our address, broadcasts, etc. */
int chk_addr(unsigned long addr)
{
	struct device *dev;
	unsigned long mask;

	/* Accept both `all ones' and `all zeros' as BROADCAST. */
	if (addr == INADDR_ANY || addr == INADDR_BROADCAST)
		return IS_BROADCAST;

	mask = get_mask(addr);

	/* Accept all of the `loopback' class A net. */
	if ((addr & mask) == htonl(0x7F000000L))
		return IS_MYADDR;

	/* OK, now check the interface addresses. */
	for (dev = dev_base; dev != NULL; dev = dev->next) {
		if (!(dev->flags & IFF_UP))
			continue;
		if ((dev->pa_addr == 0)/* || (dev->flags&IFF_PROMISC)*/)
			return IS_MYADDR;
		/* Is it the exact IP address? */
		if (addr == dev->pa_addr)
			return IS_MYADDR;
		/* Is it our broadcast address? */
		if ((dev->flags & IFF_BROADCAST) && addr == dev->pa_brdaddr)
			return IS_BROADCAST;
		/* Nope. Check for a subnetwork broadcast. */
		if (((addr ^ dev->pa_addr) & dev->pa_mask) == 0) {
			if ((addr & ~dev->pa_mask) == 0)
				return IS_BROADCAST;
			if ((addr & ~dev->pa_mask) == ~dev->pa_mask)
				return IS_BROADCAST;
		}
		/* Nope. Check for Network broadcast. */
		if (((addr ^ dev->pa_addr) & mask) == 0) {
			if ((addr & ~mask) == 0)
				return IS_BROADCAST;
			if ((addr & ~mask) == ~mask)
				return IS_BROADCAST;
		}
	}
	return 0;		/* no match at all */
}


/*
 * Retrieve our own address.
 * Because the loopback address (127.0.0.1) is already recognized
 * automatically, we can use the loopback interface's address as
 * our "primary" interface.  This is the addressed used by IP et
 * al when it doesn't know which address to use (i.e. it does not
 * yet know from or to which interface to go...).
 */
unsigned long
my_addr(void)
{
  struct device *dev;

  for (dev = dev_base; dev != NULL; dev = dev->next) {
	if (dev->flags & IFF_LOOPBACK) return(dev->pa_addr);
  }
  return(0);
}


static int dev_nit=0; /* Number of network taps running */

/* Add a protocol ID to the list.  This will change soon. */
void
dev_add_pack(struct packet_type *pt)
{
  struct packet_type *p1;
  pt->next = ptype_base;

  /* Don't use copy counts on ETH_P_ALL. Instead keep a global
     count of number of these and use it and pt->copy to decide
     copies */
  pt->copy=0;
  if(pt->type==NET16(ETH_P_ALL))
  	dev_nit++;	/* I'd like a /dev/nit too one day 8) */
  else
  {
  	/* See if we need to copy it. */
  	for (p1 = ptype_base; p1 != NULL; p1 = p1->next) {
		if (p1->type == pt->type) {
			pt->copy = 1;
			break;
		}
	  }
  }
  
  /*
   *	NIT taps must go at the end or inet_bh will leak!
   */
   
  if(pt->type==NET16(ETH_P_ALL))
  {
  	pt->next=NULL;
  	if(ptype_base==NULL)
	  	ptype_base=pt;
	else
	{
		for(p1=ptype_base;p1->next!=NULL;p1=p1->next);
		p1->next=pt;
	}
  }
  else
  	ptype_base = pt;
}


/* Remove a protocol ID from the list.  This will change soon. */
void
dev_remove_pack(struct packet_type *pt)
{
  struct packet_type *lpt, *pt1;

  if (pt->type == NET16(ETH_P_ALL))
  	dev_nit--;
  if (pt == ptype_base) {
	ptype_base = pt->next;
	return;
  }

  lpt = NULL;
  for (pt1 = ptype_base; pt1->next != NULL; pt1 = pt1->next) {
	if (pt1->next == pt ) {
		cli();
		if (!pt->copy && lpt) 
			lpt->copy = 0;
		pt1->next = pt->next;
		sti();
		return;
	}

	if (pt1->next -> type == pt ->type && pt->type != NET16(ETH_P_ALL)) {
		lpt = pt1->next;
	}
  }
}


/* Find an interface in the list. This will change soon. */
struct device *
dev_get(char *name)
{
  struct device *dev;

  for (dev = dev_base; dev != NULL; dev = dev->next) {
	if (strcmp(dev->name, name) == 0) 
		return(dev);
  }
  return(NULL);
}


/* Find an interface that can handle addresses for a certain address. */
struct device * dev_check(unsigned long addr)
{
	struct device *dev;

	for (dev = dev_base; dev; dev = dev->next) {
		if (!(dev->flags & IFF_UP))
			continue;
		if (!(dev->flags & IFF_POINTOPOINT))
			continue;
		if (addr != dev->pa_dstaddr)
			continue;
		return dev;
	}
	for (dev = dev_base; dev; dev = dev->next) {
		if (!(dev->flags & IFF_UP))
			continue;
		if (dev->flags & IFF_POINTOPOINT)
			continue;
		if (dev->pa_mask & (addr ^ dev->pa_addr))
			continue;
		return dev;
	}
	return NULL;
}


/* Prepare an interface for use. */
int
dev_open(struct device *dev)
{
  int ret = 0;

  if (dev->open) 
  	ret = dev->open(dev);
  if (ret == 0) 
  	dev->flags |= (IFF_UP | IFF_RUNNING);

  return(ret);
}


/* Completely shutdown an interface. */
int
dev_close(struct device *dev)
{
  if (dev->flags != 0) {
  	int ct=0;
	dev->flags = 0;
	if (dev->stop) 
		dev->stop(dev);
	rt_flush(dev);
	dev->pa_addr = 0;
	dev->pa_dstaddr = 0;
	dev->pa_brdaddr = 0;
	dev->pa_mask = 0;
	/* Purge any queued packets when we down the link */
	while(ct<DEV_NUMBUFFS)
	{
		struct sk_buff *skb;
		while((skb=skb_dequeue(&dev->buffs[ct]))!=NULL)
			if(skb->free)
				kfree_skb(skb,FREE_WRITE);
		ct++;
	}
  }

  return(0);
}


/* Send (or queue for sending) a packet. */
void
dev_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
{
  int where = 0;		/* used to say if the packet should go	*/
				/* at the front or the back of the	*/
				/* queue.				*/

  DPRINTF((DBG_DEV, "dev_queue_xmit(skb=%X, dev=%X, pri = %d)\n",
							skb, dev, pri));

  if (dev == NULL) {
	printk("dev.c: dev_queue_xmit: dev = NULL\n");
	return;
  }
 
  IS_SKB(skb);
    
  skb->dev = dev;
  if (skb->next != NULL) {
	/* Make sure we haven't missed an interrupt. */
	dev->hard_start_xmit(NULL, dev);
	return;
  }

  if (pri < 0) {
	pri = -pri-1;
	where = 1;
  }

  if (pri >= DEV_NUMBUFFS) {
	printk("bad priority in dev_queue_xmit.\n");
	pri = 1;
  }

  if (dev->hard_start_xmit(skb, dev) == 0) {
	return;
  }

  /* Put skb into a bidirectional circular linked list. */
  DPRINTF((DBG_DEV, "dev_queue_xmit dev->buffs[%d]=%X\n",
					pri, dev->buffs[pri]));

  /* Interrupts should already be cleared by hard_start_xmit. */
  cli();
  skb->magic = DEV_QUEUE_MAGIC;
  if(where)
  	skb_queue_head(&dev->buffs[pri],skb);
  else
  	skb_queue_tail(&dev->buffs[pri],skb);
  skb->magic = DEV_QUEUE_MAGIC;
  sti();
}

/*
 * Receive a packet from a device driver and queue it for the upper
 * (protocol) levels.  It always succeeds.
 */
void
netif_rx(struct sk_buff *skb)
{
  /* Set any necessary flags. */
  skb->sk = NULL;
  skb->free = 1;
  
  /* and add it to the "backlog" queue. */
  IS_SKB(skb);
  skb_queue_tail(&backlog,skb);
   
  /* If any packet arrived, mark it for processing. */
  if (backlog != NULL) mark_bh(INET_BH);

  return;
}


/*
 * The old interface to fetch a packet from a device driver.
 * This function is the base level entry point for all drivers that
 * want to send a packet to the upper (protocol) levels.  It takes
 * care of de-multiplexing the packet to the various modules based
 * on their protocol ID.
 *
 * Return values:	1 <- exit I can't do any more
 *			0 <- feed me more (i.e. "done", "OK"). 
 */
int
dev_rint(unsigned char *buff, long len, int flags, struct device *dev)
{
  static int dropping = 0;
  struct sk_buff *skb = NULL;
  unsigned char *to;
  int amount, left;
  int len2;

  if (dev == NULL || buff == NULL || len <= 0) return(1);
  if (flags & IN_SKBUFF) {
	skb = (struct sk_buff *) buff;
  } else {
	if (dropping) {
	  if (backlog != NULL)
	      return(1);
	  printk("INET: dev_rint: no longer dropping packets.\n");
	  dropping = 0;
	}

	skb = alloc_skb(sizeof(*skb) + len, GFP_ATOMIC);
	if (skb == NULL) {
		printk("dev_rint: packet dropped on %s (no memory) !\n",
		       dev->name);
		dropping = 1;
		return(1);
	}
	skb->mem_len = sizeof(*skb) + len;
	skb->mem_addr = (struct sk_buff *) skb;

	/* First we copy the packet into a buffer, and save it for later. */
	to = skb->data;
	left = len;
	len2 = len;
	while (len2 > 0) {
		amount = min(len2, (unsigned long) dev->rmem_end -
						(unsigned long) buff);
		memcpy(to, buff, amount);
		len2 -= amount;
		left -= amount;
		buff += amount;
		to += amount;
		if ((unsigned long) buff == dev->rmem_end)
			buff = (unsigned char *) dev->rmem_start;
	}
  }
  skb->len = len;
  skb->dev = dev;
  skb->free = 1;

  netif_rx(skb);
  /* OK, all done. */
  return(0);
}


/* This routine causes all interfaces to try to send some data. */
void
dev_transmit(void)
{
  struct device *dev;

  for (dev = dev_base; dev != NULL; dev = dev->next) {
	if (!dev->tbusy) {
		dev_tint(dev);
	}
  }
}

static volatile char in_bh = 0;

int in_inet_bh()	/* Used by timer.c */
{
	return(in_bh==0?0:1);
}

/*
 * This function gets called periodically, to see if we can
 * process any data that came in from some interface.
 *
 */
void
inet_bh(void *tmp)
{
  struct sk_buff *skb;
  struct packet_type *ptype;
  unsigned short type;
  unsigned char flag = 0;
  int nitcount;

  /* Atomically check and mark our BUSY state. */
  if (set_bit(1, (void*)&in_bh))
      return;

  /* Can we send anything now? */
  dev_transmit();
  
  /* Any data left to process? */
  while((skb=skb_dequeue(&backlog))!=NULL)
  {
  	nitcount=dev_nit;
	flag=0;
	sti();
       /*
	* Bump the pointer to the next structure.
	* This assumes that the basic 'skb' pointer points to
	* the MAC header, if any (as indicated by its "length"
	* field).  Take care now!
	*/
       skb->h.raw = skb->data + skb->dev->hard_header_len;
       skb->len -= skb->dev->hard_header_len;

       /*
	* Fetch the packet protocol ID.  This is also quite ugly, as
	* it depends on the protocol driver (the interface itself) to
	* know what the type is, or where to get it from.  The Ethernet
	* interfaces fetch the ID from the two bytes in the Ethernet MAC
	* header (the h_proto field in struct ethhdr), but drivers like
	* SLIP and PLIP have no alternative but to force the type to be
	* IP or something like that.  Sigh- FvK
	*/
       type = skb->dev->type_trans(skb, skb->dev);

	/*
	 * We got a packet ID.  Now loop over the "known protocols"
	 * table (which is actually a linked list, but this will
	 * change soon if I get my way- FvK), and forward the packet
	 * to anyone who wants it.
	 */
	for (ptype = ptype_base; ptype != NULL; ptype = ptype->next) {
		if (ptype->type == type || ptype->type == NET16(ETH_P_ALL)) {
			struct sk_buff *skb2;

			if (ptype->type==NET16(ETH_P_ALL))
				nitcount--;
			if (ptype->copy || nitcount) {	/* copy if we need to	*/
				skb2 = alloc_skb(skb->mem_len, GFP_ATOMIC);
				if (skb2 == NULL) 
					continue;
				memcpy(skb2, (const void *) skb, skb->mem_len);
				skb2->mem_addr = skb2;
				skb2->h.raw = (unsigned char *)(
				    (unsigned long) skb2 +
				    (unsigned long) skb->h.raw -
				    (unsigned long) skb
				);
				skb2->free = 1;
			} else {
				skb2 = skb;
			}

			/* This used to be in the 'else' part, but then
			 * we don't have this flag set when we get a
			 * protocol that *does* require copying... -FvK
			 */
			flag = 1;

			/* Kick the protocol handler. */
			ptype->func(skb2, skb->dev, ptype);
		}
	}

	/*
	 * That's odd.  We got an unknown packet.  Who's using
	 * stuff like Novell or Amoeba on this network??
	 */
	if (!flag) {
		DPRINTF((DBG_DEV,
			"INET: unknown packet type 0x%04X (ignored)\n", type));
		skb->sk = NULL;
		kfree_skb(skb, FREE_WRITE);
	}

	/* Again, see if we can transmit anything now. */
	dev_transmit();
	cli();
  }
  in_bh = 0;
  sti();
  dev_transmit();
}


/*
 * This routine is called when an device driver (i.e. an
 * interface) is * ready to transmit a packet.
 */
 
void dev_tint(struct device *dev)
{
	int i;
	struct sk_buff *skb;
	
	for(i = 0;i < DEV_NUMBUFFS; i++) {
		while((skb=skb_dequeue(&dev->buffs[i]))!=NULL)
		{
			skb->magic = 0;
			skb->next = NULL;
			skb->prev = NULL;
			dev->queue_xmit(skb,dev,-i - 1);
			if (dev->tbusy)
				return;
		}
	}
}


/* Perform a SIOCGIFCONF call. */
static int
dev_ifconf(char *arg)
{
  struct ifconf ifc;
  struct ifreq ifr;
  struct device *dev;
  char *pos;
  int len;
  int err;

  /* Fetch the caller's info block. */
  err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifconf));
  if(err)
  	return err;
  memcpy_fromfs(&ifc, arg, sizeof(struct ifconf));
  len = ifc.ifc_len;
  pos = ifc.ifc_buf;

  /* Loop over the interfaces, and write an info block for each. */
  for (dev = dev_base; dev != NULL; dev = dev->next) {
        if(!(dev->flags & IFF_UP))
        	continue;
	memset(&ifr, 0, sizeof(struct ifreq));
	strcpy(ifr.ifr_name, dev->name);
	(*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = dev->family;
	(*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;

	/* Write this block to the caller's space. */
	memcpy_tofs(pos, &ifr, sizeof(struct ifreq));
	pos += sizeof(struct ifreq);
	len -= sizeof(struct ifreq);
	if (len < sizeof(struct ifreq)) break;
  }

  /* All done.  Write the updated control block back to the caller. */
  ifc.ifc_len = (pos - ifc.ifc_buf);
  ifc.ifc_req = (struct ifreq *) ifc.ifc_buf;
  memcpy_tofs(arg, &ifc, sizeof(struct ifconf));
  return(pos - arg);
}

/* Print device statistics. */
char *sprintf_stats(char *buffer, struct device *dev)
{
  char *pos = buffer;
  struct enet_statistics *stats = (dev->get_stats ? dev->get_stats(dev): NULL);

  if (stats)
    pos += sprintf(pos, "%6s:%7d %4d %4d %4d %4d %8d %4d %4d %4d %5d %4d\n",
		   dev->name,
		   stats->rx_packets, stats->rx_errors,
		   stats->rx_dropped + stats->rx_missed_errors,
		   stats->rx_fifo_errors,
		   stats->rx_length_errors + stats->rx_over_errors
		   + stats->rx_crc_errors + stats->rx_frame_errors,
		   stats->tx_packets, stats->tx_errors, stats->tx_dropped,
		   stats->tx_fifo_errors, stats->collisions,
		   stats->tx_carrier_errors + stats->tx_aborted_errors
		   + stats->tx_window_errors + stats->tx_heartbeat_errors);
  else
      pos += sprintf(pos, "%6s: No statistics available.\n", dev->name);

  return pos;
}

/* Called from the PROCfs module. */
int
dev_get_info(char *buffer)
{
  char *pos = buffer;
  struct device *dev;

  pos +=
      sprintf(pos,
	      "Inter-|   Receive                  |  Transmit\n"
	      " face |packets errs drop fifo frame|packets errs drop fifo colls carrier\n");
  for (dev = dev_base; dev != NULL; dev = dev->next) {
      pos = sprintf_stats(pos, dev);
  }
  return pos - buffer;
}

static inline int bad_mask(unsigned long mask, unsigned long addr)
{
	if (addr & (mask = ~mask))
		return 1;
	mask = ntohl(mask);
	if (mask & (mask+1))
		return 1;
	return 0;
}


/* Perform the SIOCxIFxxx calls. */
static int
dev_ifsioc(void *arg, unsigned int getset)
{
  struct ifreq ifr;
  struct device *dev;
  int ret;

  /* Fetch the caller's info block. */
  int err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifreq));
  if(err)
  	return err;
  memcpy_fromfs(&ifr, arg, sizeof(struct ifreq));

  /* See which interface the caller is talking about. */
  if ((dev = dev_get(ifr.ifr_name)) == NULL) return(-EINVAL);

  switch(getset) {
	case SIOCGIFFLAGS:
		ifr.ifr_flags = dev->flags;
		memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
		ret = 0;
		break;
	case SIOCSIFFLAGS:
		{
		  int old_flags = dev->flags;
		  dev->flags = ifr.ifr_flags & (
			IFF_UP | IFF_BROADCAST | IFF_DEBUG | IFF_LOOPBACK |
			IFF_POINTOPOINT | IFF_NOTRAILERS | IFF_RUNNING |
			IFF_NOARP | IFF_PROMISC | IFF_ALLMULTI);
			
		  if ( (old_flags & IFF_PROMISC) && ((dev->flags & IFF_PROMISC) == 0))
		  	dev->set_multicast_list(dev,0,NULL);
		  if ( (dev->flags & IFF_PROMISC) && ((old_flags & IFF_PROMISC) == 0))
		  	dev->set_multicast_list(dev,-1,NULL);
		  if ((old_flags & IFF_UP) && ((dev->flags & IFF_UP) == 0)) {
			ret = dev_close(dev);
		  } else
		  {
		      ret = (! (old_flags & IFF_UP) && (dev->flags & IFF_UP))
			? dev_open(dev) : 0;
		      if(ret<0)
		      	dev->flags&=~IFF_UP;	/* Didnt open so down the if */
		  }
	        }
		break;
	case SIOCGIFADDR:
		(*(struct sockaddr_in *)
		  &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
		(*(struct sockaddr_in *)
		  &ifr.ifr_addr).sin_family = dev->family;
		(*(struct sockaddr_in *)
		  &ifr.ifr_addr).sin_port = 0;
		memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
		ret = 0;
		break;
	case SIOCSIFADDR:
		dev->pa_addr = (*(struct sockaddr_in *)
				 &ifr.ifr_addr).sin_addr.s_addr;
		dev->family = ifr.ifr_addr.sa_family;
		dev->pa_mask = get_mask(dev->pa_addr);
		dev->pa_brdaddr = dev->pa_addr | ~dev->pa_mask;
		ret = 0;
		break;
	case SIOCGIFBRDADDR:
		(*(struct sockaddr_in *)
		  &ifr.ifr_broadaddr).sin_addr.s_addr = dev->pa_brdaddr;
		(*(struct sockaddr_in *)
		  &ifr.ifr_broadaddr).sin_family = dev->family;
		(*(struct sockaddr_in *)
		  &ifr.ifr_broadaddr).sin_port = 0;
		memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
		ret = 0;
		break;
	case SIOCSIFBRDADDR:
		dev->pa_brdaddr = (*(struct sockaddr_in *)
				    &ifr.ifr_broadaddr).sin_addr.s_addr;
		ret = 0;
		break;
	case SIOCGIFDSTADDR:
		(*(struct sockaddr_in *)
		  &ifr.ifr_dstaddr).sin_addr.s_addr = dev->pa_dstaddr;
		(*(struct sockaddr_in *)
		  &ifr.ifr_broadaddr).sin_family = dev->family;
		(*(struct sockaddr_in *)
		  &ifr.ifr_broadaddr).sin_port = 0;
		memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
		ret = 0;
		break;
	case SIOCSIFDSTADDR:
		dev->pa_dstaddr = (*(struct sockaddr_in *)
				    &ifr.ifr_dstaddr).sin_addr.s_addr;
		ret = 0;
		break;
	case SIOCGIFNETMASK:
		(*(struct sockaddr_in *)
		  &ifr.ifr_netmask).sin_addr.s_addr = dev->pa_mask;
		(*(struct sockaddr_in *)
		  &ifr.ifr_netmask).sin_family = dev->family;
		(*(struct sockaddr_in *)
		  &ifr.ifr_netmask).sin_port = 0;
		memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
		ret = 0;
		break;
	case SIOCSIFNETMASK: {
		unsigned long mask = (*(struct sockaddr_in *)
			&ifr.ifr_netmask).sin_addr.s_addr;
		ret = -EINVAL;
		if (bad_mask(mask,0))
			break;
		dev->pa_mask = mask;
		ret = 0;
		break;
	}
	case SIOCGIFMETRIC:
		ifr.ifr_metric = dev->metric;
		memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
		ret = 0;
		break;
	case SIOCSIFMETRIC:
		dev->metric = ifr.ifr_metric;
		ret = 0;
		break;
	case SIOCGIFMTU:
		ifr.ifr_mtu = dev->mtu;
		memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
		ret = 0;
		break;
	case SIOCSIFMTU:
		dev->mtu = ifr.ifr_mtu;
		ret = 0;
		break;
	case SIOCGIFMEM:
		printk("NET: ioctl(SIOCGIFMEM, 0x%08X)\n", (int)arg);
		ret = -EINVAL;
		break;
	case SIOCSIFMEM:
		printk("NET: ioctl(SIOCSIFMEM, 0x%08X)\n", (int)arg);
		ret = -EINVAL;
		break;
	case SIOCGIFHWADDR:
		memcpy(ifr.ifr_hwaddr,dev->dev_addr, MAX_ADDR_LEN);
		memcpy_tofs(arg,&ifr,sizeof(struct ifreq));
		ret=0;
		break;
	default:
		ret = -EINVAL;
  }
  return(ret);
}


/* This function handles all "interface"-type I/O control requests. */
int
dev_ioctl(unsigned int cmd, void *arg)
{
  struct iflink iflink;
  struct ddi_device *dev;

  switch(cmd) {
	case IP_SET_DEV:
		printk("Your network configuration program needs upgrading.\n");
		return -EINVAL;

	case SIOCGIFCONF:
		(void) dev_ifconf((char *) arg);
		return 0;

	case SIOCGIFFLAGS:
	case SIOCGIFADDR:
	case SIOCGIFDSTADDR:
	case SIOCGIFBRDADDR:
	case SIOCGIFNETMASK:
	case SIOCGIFMETRIC:
	case SIOCGIFMTU:
	case SIOCGIFMEM:
	case SIOCGIFHWADDR:
		return dev_ifsioc(arg, cmd);

	case SIOCSIFFLAGS:
	case SIOCSIFADDR:
	case SIOCSIFDSTADDR:
	case SIOCSIFBRDADDR:
	case SIOCSIFNETMASK:
	case SIOCSIFMETRIC:
	case SIOCSIFMTU:
	case SIOCSIFMEM:
		if (!suser())
			return -EPERM;
		return dev_ifsioc(arg, cmd);

	case SIOCSIFLINK:
		if (!suser())
			return -EPERM;
		memcpy_fromfs(&iflink, arg, sizeof(iflink));
		dev = ddi_map(iflink.id);
		if (dev == NULL)
			return -EINVAL;

		/* Now allocate an interface and connect it. */
		printk("AF_INET: DDI \"%s\" linked to stream \"%s\"\n",
						dev->name, iflink.stream);
		return 0;

	default:
		return -EINVAL;
  }
}


/* Initialize the DEV module. */
void
dev_init(void)
{
  struct device *dev, *dev2;

  /* Add the devices.
   * If the call to dev->init fails, the dev is removed
   * from the chain disconnecting the device until the
   * next reboot.
   */
  dev2 = NULL;
  for (dev = dev_base; dev != NULL; dev=dev->next) {
	if (dev->init && dev->init(dev)) {
		if (dev2 == NULL) dev_base = dev->next;
		  else dev2->next = dev->next;
	} else {
		dev2 = dev;
	}
  }

  /* Set up some IP addresses. */
  ip_bcast = in_aton("255.255.255.255");
}
原諒我這一生不羈放縱愛自由