File: br.c

package info (click to toggle)
kernel-source-2.0.32 2.0.32-5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 29,648 kB
  • ctags: 86,850
  • sloc: ansic: 542,141; asm: 26,201; makefile: 3,423; sh: 1,195; perl: 727; tcl: 408; cpp: 277; lisp: 211; awk: 134
file content (1541 lines) | stat: -rw-r--r-- 41,020 bytes parent folder | download | duplicates (4)
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
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
/*
 *	Linux NET3 Bridge Support
 *
 *	Originally by John Hayes (Network Plumbing).
 *	Minor hacks to get it to run with 1.3.x by Alan Cox <Alan.Cox@linux.org>
 *
 *	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.
 *
 *	Fixes:
 *
 *	Todo:
 *		Don't bring up devices automatically. Start ports disabled
 *	and use a netlink notifier so a daemon can maintain the bridge
 *	port group (could we also do multiple groups ????).
 *		A nice /proc file interface.
 *		Put the path costs in the port info and devices.
 *		Put the bridge port number in the device structure for speed.
 *		Bridge SNMP stats.
 *	
 */
 
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/net.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/string.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <net/br.h>

static int br_device_event(struct notifier_block *dnot, unsigned long event, void *ptr);
static void br_tick(unsigned long arg);
int br_forward(struct sk_buff *skb, int port);	/* 3.7 */
int br_port_cost(struct device *dev);	/* 4.10.2 */
void br_bpdu(struct sk_buff *skb); /* consumes skb */
int br_tx_frame(struct sk_buff *skb);
int br_cmp(unsigned int *a, unsigned int *b);

unsigned char bridge_ula[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };

Bridge_data     bridge_info;			  /* (4.5.3)	 */
Port_data       port_info[All_ports];		  /* (4.5.5)	 */
Config_bpdu     config_bpdu[All_ports];
Tcn_bpdu        tcn_bpdu[All_ports];
Timer           hello_timer;			  /* (4.5.4.1)	 */
Timer           tcn_timer;			  /* (4.5.4.2)	 */
Timer           topology_change_timer;		  /* (4.5.4.3)	 */
Timer           message_age_timer[All_ports];	  /* (4.5.6.1)	 */
Timer           forward_delay_timer[All_ports];	  /* (4.5.6.2)	 */
Timer           hold_timer[All_ports];		  /* (4.5.6.3)	 */

/* entries timeout after this many seconds */
unsigned int fdb_aging_time = FDB_TIMEOUT; 

struct br_stat br_stats;

static struct timer_list tl; /* for 1 second timer... */

/*
 * the following structure is required so that we receive
 * event notifications when network devices are enabled and
 * disabled (ifconfig up and down).
 */
static struct notifier_block br_dev_notifier={
	br_device_event,
	NULL,
	0
};

/** Elements of Procedure (4.6) **/

/*
 * this section of code was graciously borrowed from the IEEE 802.1d
 * specification section 4.9.1 starting on pg 69.  It has been
 * modified somewhat to fit within out framework and structure.  It
 * implements the spanning tree algorithm that is the heart of the
 * 802.1d bridging protocol.
 */

void transmit_config(int port_no)			  /* (4.6.1)	 */
{
	if (hold_timer[port_no].active) {	  /* (4.6.1.3.1)	 */
		port_info[port_no].config_pending = TRUE;	/* (4.6.1.3.1)	 */
	} else {				  /* (4.6.1.3.2)	 */
		config_bpdu[port_no].type = BPDU_TYPE_CONFIG;
		config_bpdu[port_no].root_id = bridge_info.designated_root;
		/* (4.6.1.3.2(1)) */
		config_bpdu[port_no].root_path_cost = bridge_info.root_path_cost;
		/* (4.6.1.3.2(2)) */
		config_bpdu[port_no].bridge_id = bridge_info.bridge_id;
		/* (4.6.1.3.2(3)) */
		config_bpdu[port_no].port_id = port_info[port_no].port_id;
		/*
		 * (4.6.1.3.2(4))
		 */
		if (root_bridge()) {
			config_bpdu[port_no].message_age = Zero;	/* (4.6.1.3.2(5)) */
		} else {
			config_bpdu[port_no].message_age
				= message_age_timer[bridge_info.root_port].value
				+ Message_age_increment;	/* (4.6.1.3.2(6)) */
		}

		config_bpdu[port_no].max_age = bridge_info.max_age;	/* (4.6.1.3.2(7)) */
		config_bpdu[port_no].hello_time = bridge_info.hello_time;
		config_bpdu[port_no].forward_delay = bridge_info.forward_delay;
		config_bpdu[port_no].flags = 0;
		config_bpdu[port_no].flags |=
			port_info[port_no].top_change_ack ? TOPOLOGY_CHANGE_ACK : 0;
		/* (4.6.1.3.2(8)) */
		port_info[port_no].top_change_ack = 0;
		/* (4.6.1.3.2(8)) */
		config_bpdu[port_no].flags |=
			bridge_info.top_change ? TOPOLOGY_CHANGE : 0;
		/* (4.6.1.3.2(9)) */

		send_config_bpdu(port_no, &config_bpdu[port_no]);
		port_info[port_no].config_pending = FALSE;	/* (4.6.1.3.2(10)) */
		start_hold_timer(port_no);	  /* (4.6.1.3.2(11)) */
	}
}

int root_bridge(void)
{
	return (br_cmp(bridge_info.designated_root.BRIDGE_ID,
		 bridge_info.bridge_id.BRIDGE_ID)?FALSE:TRUE);
}

int supersedes_port_info(int port_no, Config_bpdu *config)	  /* (4.6.2.2)	 */
{
	return (
		(br_cmp(config->root_id.BRIDGE_ID,
		 port_info[port_no].designated_root.BRIDGE_ID) < 0)	/* (4.6.2.2.1)	 */ 
		||
		((br_cmp(config->root_id.BRIDGE_ID,
		  port_info[port_no].designated_root.BRIDGE_ID) == 0
		  )
		 &&
		 ((config->root_path_cost
		   < port_info[port_no].designated_cost	/* (4.6.2.2.2)	 */
		   )
		  ||
		  ((config->root_path_cost
		    == port_info[port_no].designated_cost
		    )
		   &&
		   ((br_cmp(config->bridge_id.BRIDGE_ID,
		     port_info[port_no].designated_bridge.BRIDGE_ID) < 0	/* (4.6.2.2.3)    */
		     )
		    ||
		    ((br_cmp(config->bridge_id.BRIDGE_ID,
		      port_info[port_no].designated_bridge.BRIDGE_ID) == 0
		      )				  /* (4.6.2.2.4)	 */
		     &&
		     ((br_cmp(config->bridge_id.BRIDGE_ID,
			bridge_info.bridge_id.BRIDGE_ID) != 0
		       )			  /* (4.6.2.2.4(1)) */
		      ||
		      (config->port_id <=
		       port_info[port_no].designated_port
		       )			  /* (4.6.2.2.4(2)) */
		      ))))))
		);
}

void record_config_information(int port_no, Config_bpdu *config)	  /* (4.6.2)	 */
{
	port_info[port_no].designated_root = config->root_id;	/* (4.6.2.3.1)   */
	port_info[port_no].designated_cost = config->root_path_cost;
	port_info[port_no].designated_bridge = config->bridge_id;
	port_info[port_no].designated_port = config->port_id;
	start_message_age_timer(port_no, config->message_age);	/* (4.6.2.3.2)   */
}

void record_config_timeout_values(Config_bpdu *config)		  /* (4.6.3)	 */
{
	bridge_info.max_age = config->max_age;	  /* (4.6.3.3)	 */
	bridge_info.hello_time = config->hello_time;
	bridge_info.forward_delay = config->forward_delay;
	if (config->flags & TOPOLOGY_CHANGE)
		bridge_info.top_change = 1;
}

void config_bpdu_generation(void)
{						  /* (4.6.4)	 */
	int             port_no;
	for (port_no = One; port_no <= No_of_ports; port_no++) {	/* (4.6.4.3) */
		if (designated_port(port_no)	  /* (4.6.4.3)	 */
				&&
				(port_info[port_no].state != Disabled)
			) {
			transmit_config(port_no); /* (4.6.4.3)	 */
		}				  /* (4.6.1.2)	 */
	}
}

int designated_port(int port_no)
{
	return ((br_cmp(port_info[port_no].designated_bridge.BRIDGE_ID,
		 bridge_info.bridge_id.BRIDGE_ID) == 0
		 )
		&&
		(port_info[port_no].designated_port
		 == port_info[port_no].port_id
		 )
		);
}

void reply(int port_no)					  /* (4.6.5)	 */
{
	transmit_config(port_no);		  /* (4.6.5.3)	 */
}

void transmit_tcn(void)
{						  /* (4.6.6)	 */
	int             port_no;

	port_no = bridge_info.root_port;
	tcn_bpdu[port_no].type = BPDU_TYPE_TOPO_CHANGE;
	send_tcn_bpdu(port_no, &tcn_bpdu[bridge_info.root_port]);	/* (4.6.6.3)     */
}

void configuration_update(void)	/* (4.6.7) */
{
	root_selection();			  /* (4.6.7.3.1)	 */
	/* (4.6.8.2)	 */
	designated_port_selection();		  /* (4.6.7.3.2)	 */
	/* (4.6.9.2)	 */
}

void root_selection(void)
{						  /* (4.6.8) */
	int             root_port;
	int             port_no;
	root_port = No_port;
	for (port_no = One; port_no <= No_of_ports; port_no++) {	/* (4.6.8.3.1) */
		if (((!designated_port(port_no))
		     &&
		     (port_info[port_no].state != Disabled)
		     &&
		(br_cmp(port_info[port_no].designated_root.BRIDGE_ID,
			bridge_info.bridge_id.BRIDGE_ID) < 0)
			)
				&&
				((root_port == No_port)
				 ||
				 (br_cmp(port_info[port_no].designated_root.BRIDGE_ID,
				  port_info[root_port].designated_root.BRIDGE_ID) < 0
				  )
				 ||
				 ((br_cmp(port_info[port_no].designated_root.BRIDGE_ID,
				   port_info[root_port].designated_root.BRIDGE_ID) == 0
				   )
				  &&
				  (((port_info[port_no].designated_cost
				     + port_info[port_no].path_cost
				     )
				    <
				    (port_info[root_port].designated_cost
				     + port_info[root_port].path_cost
				     )		  /* (4.6.8.3.1(2)) */
				    )
				   ||
				   (((port_info[port_no].designated_cost
				      + port_info[port_no].path_cost
				      )
				     ==
				     (port_info[root_port].designated_cost
				      + port_info[root_port].path_cost
				      )
				     )
				    &&
				    ((br_cmp(port_info[port_no].designated_bridge.BRIDGE_ID,
				    port_info[root_port].designated_bridge.BRIDGE_ID) < 0
				      )		  /* (4.6.8.3.1(3)) */
				     ||
				     ((br_cmp(port_info[port_no].designated_bridge.BRIDGE_ID,
				   port_info[root_port].designated_bridge.BRIDGE_ID) == 0
				       )
				      &&
				      ((port_info[port_no].designated_port
				      < port_info[root_port].designated_port
					)	  /* (4.6.8.3.1(4)) */
				       ||
				       ((port_info[port_no].designated_port
				      = port_info[root_port].designated_port
					 )
					&&
					(port_info[port_no].port_id
					 < port_info[root_port].port_id
					 )	  /* (4.6.8.3.1(5)) */
					))))))))) {
			root_port = port_no;
		}
	}
	bridge_info.root_port = root_port;	  /* (4.6.8.3.1)	 */

	if (root_port == No_port) {		  /* (4.6.8.3.2)	 */
		bridge_info.designated_root = bridge_info.bridge_id;
		/* (4.6.8.3.2(1)) */
		bridge_info.root_path_cost = Zero;/* (4.6.8.3.2(2)) */
	} else {				  /* (4.6.8.3.3)	 */
		bridge_info.designated_root = port_info[root_port].designated_root;
		/* (4.6.8.3.3(1)) */
		bridge_info.root_path_cost = (port_info[root_port].designated_cost
					    + port_info[root_port].path_cost
			);			  /* (4.6.8.3.3(2)) */
	}
}

void designated_port_selection(void)
{						  /* (4.6.9)	 */
	int             port_no;

	for (port_no = One; port_no <= No_of_ports; port_no++) {	/* (4.6.9.3)	 */
		if (designated_port(port_no)	  /* (4.6.9.3.1)	 */
				||
				(
				 br_cmp(port_info[port_no].designated_root.BRIDGE_ID,
				 bridge_info.designated_root.BRIDGE_ID) != 0
				 )
				||
				(bridge_info.root_path_cost
				 < port_info[port_no].designated_cost
				 )		  /* (4.6.9.3.3)	 */
				||
				((bridge_info.root_path_cost
				  == port_info[port_no].designated_cost
				  )
				 &&
				 ((br_cmp(bridge_info.bridge_id.BRIDGE_ID,
				   port_info[port_no].designated_bridge.BRIDGE_ID) < 0
				   )		  /* (4.6.9.3.4)	 */
				  ||
				  ((br_cmp(bridge_info.bridge_id.BRIDGE_ID,
				    port_info[port_no].designated_bridge.BRIDGE_ID) == 0
				    )
				   &&
				   (port_info[port_no].port_id
				    <= port_info[port_no].designated_port
				    )		  /* (4.6.9.3.5)	 */
				   )))) {
			become_designated_port(port_no);	/* (4.6.10.3.2.2) */
		}
	}
}

void become_designated_port(int port_no)
{						  /* (4.6.10)	 */

	/* (4.6.10.3.1) */
	port_info[port_no].designated_root = bridge_info.designated_root;
	/* (4.6.10.3.2) */
	port_info[port_no].designated_cost = bridge_info.root_path_cost;
	/* (4.6.10.3.3) */
	port_info[port_no].designated_bridge = bridge_info.bridge_id;
	/* (4.6.10.3.4) */
	port_info[port_no].designated_port = port_info[port_no].port_id;
}

void port_state_selection(void)
{						  /* (4.6.11) */
	int             port_no;
	for (port_no = One; port_no <= No_of_ports; port_no++) {
		if (port_no == bridge_info.root_port) {	/* (4.6.11.3.1) */
			port_info[port_no].config_pending = FALSE;	/* (4.6.11.3~1(1)) */
			port_info[port_no].top_change_ack = 0;
			make_forwarding(port_no); /* (4.6.11.3.1(2)) */
		} else if (designated_port(port_no)) {	/* (4.6.11.3.2) */
			stop_message_age_timer(port_no);	/* (4.6.11.3.2(1)) */
			make_forwarding(port_no); /* (4.6.11.3.2(2)) */
		} else {			  /* (4.6.11.3.3) */
			port_info[port_no].config_pending = FALSE;	/* (4.6.11.3.3(1)) */
			port_info[port_no].top_change_ack = 0;
			make_blocking(port_no);	  /* (4.6.11.3.3(2)) */
		}
	}

}

void make_forwarding(int port_no)
{						  /* (4.6.12) */
	if (port_info[port_no].state == Blocking) {	/* (4.6.12.3) */
		set_port_state(port_no, Listening);	/* (4.6.12.3.1) */
		start_forward_delay_timer(port_no);	/* (4.6.12.3.2) */
	}
}

void topology_change_detection(void)
{						  /* (4.6.14)       */
	if (root_bridge()) {			  /* (4.6.14.3.1)   */
		bridge_info.top_change = 1;
		start_topology_change_timer();	  /* (4.6.14.3.1(2)) */
	} else if (!(bridge_info.top_change_detected)) {
		transmit_tcn();			  /* (4.6.14.3.2(1)) */
		start_tcn_timer();		  /* (4.6.14.3.2(2)) */
	}
	bridge_info.top_change = 1;
}

void topology_change_acknowledged(void)
{						  /* (4.6.15) */
	bridge_info.top_change_detected = 0;
	stop_tcn_timer();			  /* (4.6.15.3.2) */
}

void acknowledge_topology_change(int port_no)
{						  /* (4.6.16) */
	port_info[port_no].top_change_ack = 1;
	transmit_config(port_no);		  /* (4.6.16.3.2) */
}

void make_blocking(int port_no)				  /* (4.6.13)	 */
{

	if ((port_info[port_no].state != Disabled)
			&&
			(port_info[port_no].state != Blocking)
	/* (4.6.13.3)	 */
		) {
		if ((port_info[port_no].state == Forwarding)
				||
				(port_info[port_no].state == Learning)
			) {
			topology_change_detection();	/* (4.6.13.3.1) */
			/* (4.6.14.2.3)	 */
		}
		set_port_state(port_no, Blocking);/* (4.6.13.3.2) */
		stop_forward_delay_timer(port_no);/* (4.6.13.3.3) */
	}
}

void set_port_state(int port_no, int state)
{
	port_info[port_no].state = state;
}

void received_config_bpdu(int port_no, Config_bpdu *config)		  /* (4.7.1)	 */
{
	int         root;

	root = root_bridge();
	if (port_info[port_no].state != Disabled) {
		if (supersedes_port_info(port_no, config)) {	/* (4.7.1.1)	 *//* (4.
								 * 6.2.2)	 */
			record_config_information(port_no, config);	/* (4.7.1.1.1)	 */
			/* (4.6.2.2)	 */
			configuration_update();	  /* (4.7.1.1.2)	 */
			/* (4.6.7.2.1)	 */
			port_state_selection();	  /* (4.7.1.1.3)	 */
			/* (4.6.11.2.1)	 */
			if ((!root_bridge()) && root) {	/* (4.7.1.1.4)	 */
				stop_hello_timer();
				if (bridge_info.top_change_detected) {	/* (4.7.1.1.5~ */
					stop_topology_change_timer();
					transmit_tcn();	/* (4.6.6.1)	 */
					start_tcn_timer();
				}
			}
			if (port_no == bridge_info.root_port) {
				record_config_timeout_values(config);	/* (4.7.1.1.6)	 */
				/* (4.6.3.2)	 */
				config_bpdu_generation();	/* (4.6.4.2.1)	 */
				if (config->flags & TOPOLOGY_CHANGE_ACK) {	/* (4.7.1.1.7)    */
					topology_change_acknowledged();	/* (4.6.15.2)	 */
				}
			}
		} else if (designated_port(port_no)) {	/* (4.7.1.2)	 */
			reply(port_no);		  /* (4.7.1.2.1)	 */
			/* (4.6.5.2)	 */
		}
	}
}

void received_tcn_bpdu(int port_no, Tcn_bpdu *tcn)			  /* (4.7.2)	 */
{
	if (port_info[port_no].state != Disabled) {
		if (designated_port(port_no)) {
			topology_change_detection();	/* (4.7.2.1)	 */
			/* (4.6.14.2.1)	 */
			acknowledge_topology_change(port_no);	/* (4.7.2.2)	 */
		}				  /* (4.6.16.2)	 */
	}
}

void hello_timer_expiry(void)
{						  /* (4.7.3)	 */
	config_bpdu_generation();		  /* (4.6.4.2.2)	 */
	start_hello_timer();
}

void message_age_timer_expiry(int port_no)		  /* (4.7.4)	 */
{
	int         root;
	root = root_bridge();

	become_designated_port(port_no);	  /* (4.7.4.1)	 */
	/* (4.6.10.2.1)	 */
	configuration_update();			  /* (4.7.4.2)	 */
	/* (4.6.7.2.2)	 */
	port_state_selection();			  /* (4.7.4.3)	 */
	/* (4.6.11.2.2)	 */
	if ((root_bridge()) && (!root)) {	  /* (4.7.4.4)	 */

		bridge_info.max_age = bridge_info.bridge_max_age;	/* (4.7.4.4.1)    */
		bridge_info.hello_time = bridge_info.bridge_hello_time;
		bridge_info.forward_delay = bridge_info.bridge_forward_delay;
		topology_change_detection();	  /* (4.7.4.4.2)	 */
		/* (4.6.14.2.4)	 */
		stop_tcn_timer();		  /* (4.7.4.4.3)	 */
		config_bpdu_generation();	  /* (4.7.4.4.4)	 */
		/* (4.6.4.4.3)	 */
		start_hello_timer();
	}
}

void forward_delay_timer_expiry(int port_no)		  /* (4.7.5)	 */
{
	if (port_info[port_no].state == Listening) {	/* (4.7.5.1)	 */
		set_port_state(port_no, Learning);	/* (4.7.5.1.1)	 */
		start_forward_delay_timer(port_no);	/* (4.7.5.1.2)	 */
	} else if (port_info[port_no].state == Learning) {	/* (4.7.5.2) */
		set_port_state(port_no, Forwarding);	/* (4.7.5.2.1) */
		if (designated_for_some_port()) { /* (4.7.5.2.2) */
			topology_change_detection();	/* (4.6.14.2.2) */

		}
	}
}

int designated_for_some_port(void)
{
	int             port_no;


	for (port_no = One; port_no <= No_of_ports; port_no++) {
		if ((br_cmp(port_info[port_no].designated_bridge.BRIDGE_ID,
				bridge_info.bridge_id.BRIDGE_ID) == 0)
			) {
			return (TRUE);
		}
	}
	return (FALSE);
}

void tcn_timer_expiry(void)
{						  /* (4.7.6)	 */
	transmit_tcn();				  /* (4.7.6.1)	 */
	start_tcn_timer();			  /* (4.7.6.2)	 */
}

void topology_change_timer_expiry(void)
{						  /* (4.7.7)	 */
	bridge_info.top_change_detected = 0;
	bridge_info.top_change = 0;
	  /* (4.7.7.2)	 */
}

void hold_timer_expiry(int port_no)			  /* (4.7.8)	 */
{
	if (port_info[port_no].config_pending) {
		transmit_config(port_no);	  /* (4.7.8.1)	 */
	}					  /* (4.6.1.2.3)	 */
}

void br_init(void)
{						  /* (4.8.1)	 */
	int             port_no;

	printk(KERN_INFO "Ethernet Bridge 002 for NET3.035 (Linux 2.0)\n");
	bridge_info.designated_root = bridge_info.bridge_id;	/* (4.8.1.1)	 */
	bridge_info.root_path_cost = Zero;
	bridge_info.root_port = No_port;

	bridge_info.bridge_max_age = BRIDGE_MAX_AGE;
	bridge_info.bridge_hello_time = BRIDGE_HELLO_TIME;
	bridge_info.bridge_forward_delay = BRIDGE_FORWARD_DELAY;
	bridge_info.hold_time = HOLD_TIME;

	bridge_info.max_age = bridge_info.bridge_max_age;	/* (4.8.1.2)	 */
	bridge_info.hello_time = bridge_info.bridge_hello_time;
	bridge_info.forward_delay = bridge_info.bridge_forward_delay;

	bridge_info.top_change_detected = 0;
	bridge_info.top_change = 0;
	stop_tcn_timer();
	stop_topology_change_timer();
	for (port_no = One; port_no <= No_of_ports; port_no++) {	/* (4.8.1.4) */
		br_init_port(port_no);
		disable_port(port_no);
	}
	port_state_selection();			  /* (4.8.1.5)	 */
	config_bpdu_generation();		  /* (4.8.1.6)	 */
	
	/* initialize system timer */
	tl.expires = jiffies+HZ;	/* 1 second */
	tl.function = br_tick;
	add_timer(&tl);

	register_netdevice_notifier(&br_dev_notifier);
	br_stats.flags = 0; /*BR_UP | BR_DEBUG*/;	/* enable bridge */
	/*start_hello_timer();*/
}

void br_init_port(int port_no)
{
	become_designated_port(port_no);	  /* (4.8.1.4.1) */
	set_port_state(port_no, Blocking);	  /* (4.8.1.4.2)    */
	port_info[port_no].top_change_ack = 0;
	port_info[port_no].config_pending = FALSE;/* (4.8.1.4.4)	 */
	stop_message_age_timer(port_no);	  /* (4.8.1.4.5)	 */
	stop_forward_delay_timer(port_no);	  /* (4.8.1.4.6)	 */
	stop_hold_timer(port_no);		  /* (4.8.1.4.7)	 */
}

void enable_port(int port_no)				  /* (4.8.2)	 */
{
	br_init_port(port_no);
	port_state_selection();			  /* (4.8.2.7)	 */
}						  /* */

void disable_port(int port_no)				  /* (4.8.3)	 */
{
	int         root;

	root = root_bridge();
	become_designated_port(port_no);	  /* (4.8.3.1)	 */
	set_port_state(port_no, Disabled);	  /* (4.8.3.2)	 */
	port_info[port_no].top_change_ack = 0;
	port_info[port_no].config_pending = FALSE;/* (4.8.3.4)	 */
	stop_message_age_timer(port_no);	  /* (4.8.3.5)	 */
	stop_forward_delay_timer(port_no);	  /* (4.8.3.6)	 */
	configuration_update();
	port_state_selection();			  /* (4.8.3.7)	 */
	if ((root_bridge()) && (!root)) {	  /* (4.8.3.8)	 */
		bridge_info.max_age = bridge_info.bridge_max_age;	/* (4.8.3.8.1)    */
		bridge_info.hello_time = bridge_info.bridge_hello_time;
		bridge_info.forward_delay = bridge_info.bridge_forward_delay;
		topology_change_detection();	  /* (4.8.3.8.2)    */
		stop_tcn_timer();		  /* (4.8.3.8.3)    */
		config_bpdu_generation();	  /* (4.8.3.8.4)    */
		start_hello_timer();
	}
}


void set_bridge_priority(bridge_id_t *new_bridge_id)		  /* (4.8.4)	 */
{

	int         root;
	int             port_no;
	root = root_bridge();
	for (port_no = One; port_no <= No_of_ports; port_no++) {	/* (4.8.4.2) */
		if (designated_port(port_no)) {
			port_info[port_no].designated_bridge = *new_bridge_id;
		}
	}

	bridge_info.bridge_id = *new_bridge_id;	  /* (4.8.4.3)	 */
	configuration_update();			  /* (4.8.4.4)	 */
	port_state_selection();			  /* (4.8.4.5)	 */
	if ((root_bridge()) && (!root)) {	  /* (4.8.4.6)	 */
		bridge_info.max_age = bridge_info.bridge_max_age;	/* (4.8.4.6.1)    */
		bridge_info.hello_time = bridge_info.bridge_hello_time;
		bridge_info.forward_delay = bridge_info.bridge_forward_delay;
		topology_change_detection();	  /* (4.8.4.6.2)    */
		stop_tcn_timer();		  /* (4.8.4.6.3)    */
		config_bpdu_generation(),	  /* (4.8.4.6.4)    */
		start_hello_timer();
	}
}

void set_port_priority(int port_no, unsigned short new_port_id)		  /* (4.8.5)	 */
{
	if (designated_port(port_no)) {		  /* (4.8.5.2)	 */
		port_info[port_no].designated_port = new_port_id;
	}
	port_info[port_no].port_id = new_port_id; /* (4.8.5.3)	 */
	if ((br_cmp(bridge_info.bridge_id.BRIDGE_ID,
	     port_info[port_no].designated_bridge.BRIDGE_ID) == 0
	     )
			&&
			(port_info[port_no].port_id
			 < port_info[port_no].designated_port

			 )
		) {
		become_designated_port(port_no);  /* (4.8.5.4.1) */
		port_state_selection();		  /* (4.8.5.4.2) */
	}
}

void set_path_cost(int port_no, unsigned short path_cost)		  /* (4.8.6)	 */
{
	port_info[port_no].path_cost = path_cost; /* (4.8.6.1)	 */
	configuration_update();			  /* (4.8.6.2)	 */
	port_state_selection();			  /* (4.8.6.3)	 */
}

static void br_tick(unsigned long arg)
{
	int             port_no;

	if (hello_timer_expired()) {
		hello_timer_expiry();
	}
	if (tcn_timer_expired()) {
		tcn_timer_expiry();
	}
	if (topology_change_timer_expired()) {
		topology_change_timer_expiry();
	}
	for (port_no = One; port_no <= No_of_ports; port_no++) {
		if (forward_delay_timer_expired(port_no)) {
			forward_delay_timer_expiry(port_no);
		}
		if (message_age_timer_expired(port_no)) {
			message_age_timer_expiry(port_no);
		}
		if (hold_timer_expired(port_no)) {
			hold_timer_expiry(port_no);
		}
	}
	/* call me again sometime... */
	tl.expires = jiffies+HZ;	/* 1 second */
	tl.function = br_tick;
	add_timer(&tl);
}

void start_hello_timer(void)
{
	hello_timer.value = 0;
	hello_timer.active = TRUE;
}

void stop_hello_timer(void)
{
	hello_timer.active = FALSE;
}

int hello_timer_expired(void)
{
	if (hello_timer.active && (++hello_timer.value >= bridge_info.hello_time)) {
		hello_timer.active = FALSE;
		return (TRUE);
	}
	return (FALSE);
}

void start_tcn_timer(void)
{
	tcn_timer.value = 0;
	tcn_timer.active = TRUE;
}

void stop_tcn_timer(void)
{
	tcn_timer.active = FALSE;
}

int tcn_timer_expired(void)
{
	if (tcn_timer.active && (++tcn_timer.value >=
				 bridge_info.bridge_hello_time)) {
		tcn_timer.active = FALSE;
		return (TRUE);
	}
	return (FALSE);

}

void start_topology_change_timer(void)
{
	topology_change_timer.value = 0;
	topology_change_timer.active = TRUE;
}

void stop_topology_change_timer(void)
{
	topology_change_timer.active = FALSE;
}

int topology_change_timer_expired(void)
{
	if (topology_change_timer.active
			&& (++topology_change_timer.value
			    >= bridge_info.topology_change_time
			    )) {
		topology_change_timer.active = FALSE;
		return (TRUE);
	}
	return (FALSE);
}

void start_message_age_timer(int port_no, unsigned short message_age)
{
	message_age_timer[port_no].value = message_age;
	message_age_timer[port_no].active = TRUE;
}

void stop_message_age_timer(int port_no)
{
	message_age_timer[port_no].active = FALSE;
}

int message_age_timer_expired(int port_no)
{
	if (message_age_timer[port_no].active &&
	      (++message_age_timer[port_no].value >= bridge_info.max_age)) {
		message_age_timer[port_no].active = FALSE;
		return (TRUE);
	}
	return (FALSE);
}

void start_forward_delay_timer(int port_no)
{
	forward_delay_timer[port_no].value = 0;
	forward_delay_timer[port_no].active = TRUE;
}

void stop_forward_delay_timer(int port_no)
{
	forward_delay_timer[port_no].active = FALSE;
}

int forward_delay_timer_expired(int port_no)
{
		if (forward_delay_timer[port_no].active &&
				(++forward_delay_timer[port_no].value >= bridge_info.forward_delay)) {
			forward_delay_timer[port_no].active = FALSE;
			return (TRUE);
		}
		return (FALSE);
}

void start_hold_timer(int port_no)
{
	hold_timer[port_no].value = 0;
	hold_timer[port_no].active = TRUE;
}

void stop_hold_timer(int port_no)
{
	hold_timer[port_no].active = FALSE;
}


int hold_timer_expired(int port_no)
{
	if (hold_timer[port_no].active &&
		   (++hold_timer[port_no].value >= bridge_info.hold_time)) {
		hold_timer[port_no].active = FALSE;
		return (TRUE);
	}
	return (FALSE);

}

int send_config_bpdu(int port_no, Config_bpdu *config_bpdu)
{
struct sk_buff *skb;
struct device *dev = port_info[port_no].dev;
int size;
unsigned long flags;
	
	if (port_info[port_no].state == Disabled) {
		printk(KERN_DEBUG "send_config_bpdu: port %i not valid\n",port_no);
		return(-1);
		}
	if (br_stats.flags & BR_DEBUG)
		printk("send_config_bpdu: ");
	/*
	 * create and send the message
	 */
	size = sizeof(Config_bpdu) + dev->hard_header_len;
	skb = alloc_skb(size, GFP_ATOMIC);
	if (skb == NULL) {
		printk(KERN_DEBUG "send_config_bpdu: no skb available\n");
		return(-1);
		}
	skb->dev = dev;
	skb->free = 1;
	skb->h.eth = (struct ethhdr *)skb_put(skb, size);
	memcpy(skb->h.eth->h_dest, bridge_ula, ETH_ALEN);
	memcpy(skb->h.eth->h_source, dev->dev_addr, ETH_ALEN);
	if (br_stats.flags & BR_DEBUG)
		printk("port %i src %02x:%02x:%02x:%02x:%02x:%02x\
			dest %02x:%02x:%02x:%02x:%02x:%02x\n", 
			port_no,
			skb->h.eth->h_source[0],
			skb->h.eth->h_source[1],
			skb->h.eth->h_source[2],
			skb->h.eth->h_source[3],
			skb->h.eth->h_source[4],
			skb->h.eth->h_source[5],
			skb->h.eth->h_dest[0],
			skb->h.eth->h_dest[1],
			skb->h.eth->h_dest[2],
			skb->h.eth->h_dest[3],
			skb->h.eth->h_dest[4],
			skb->h.eth->h_dest[5]);
	skb->h.eth->h_proto = htonl(0x8038);	/* XXX verify */

	skb->h.raw += skb->dev->hard_header_len;
	memcpy(skb->h.raw, config_bpdu, sizeof(Config_bpdu));

	/* won't get bridged again... */
	skb->pkt_bridged = IS_BRIDGED;
	skb->arp = 1;	/* do not resolve... */
	skb->h.raw = skb->data + ETH_HLEN;
	save_flags(flags);
	cli();
	skb_queue_tail(dev->buffs, skb);
	restore_flags(flags);
	return(0);
}

int send_tcn_bpdu(int port_no, Tcn_bpdu *bpdu)
{
struct sk_buff *skb;
struct device *dev = port_info[port_no].dev;
int size;
unsigned long flags;
	
	if (port_info[port_no].state == Disabled) {
		printk(KERN_DEBUG "send_tcn_bpdu: port %i not valid\n",port_no);
		return(-1);
		}
	if (br_stats.flags & BR_DEBUG)
		printk("send_tcn_bpdu: ");
	size = sizeof(Tcn_bpdu) + dev->hard_header_len;
	skb = alloc_skb(size, GFP_ATOMIC);
	if (skb == NULL) {
		printk(KERN_DEBUG "send_tcn_bpdu: no skb available\n");
		return(-1);
		}
	skb->dev = dev;
	skb->free = 1;
	skb->h.eth = (struct ethhdr *)skb_put(skb,size);
	memcpy(skb->h.eth->h_dest, bridge_ula, ETH_ALEN);
	memcpy(skb->h.eth->h_source, dev->dev_addr, ETH_ALEN);
	if (br_stats.flags & BR_DEBUG)
		printk("port %i src %02x:%02x:%02x:%02x:%02x:%02x\
			dest %02x:%02x:%02x:%02x:%02x:%02x\n", 
			port_no,
			skb->h.eth->h_source[0],
			skb->h.eth->h_source[1],
			skb->h.eth->h_source[2],
			skb->h.eth->h_source[3],
			skb->h.eth->h_source[4],
			skb->h.eth->h_source[5],
			skb->h.eth->h_dest[0],
			skb->h.eth->h_dest[1],
			skb->h.eth->h_dest[2],
			skb->h.eth->h_dest[3],
			skb->h.eth->h_dest[4],
			skb->h.eth->h_dest[5]);
	skb->h.eth->h_proto = 0x8038;	/* XXX verify */

	skb->h.raw += skb->dev->hard_header_len;
	memcpy(skb->h.raw, bpdu, sizeof(Tcn_bpdu));

	/* mark that's we've been here... */
	skb->pkt_bridged = IS_BRIDGED;
	skb->arp = 1;	/* do not resolve... */
	skb->h.raw = skb->data + ETH_HLEN;
	save_flags(flags);
	cli();
	skb_queue_tail(dev->buffs, skb);
	restore_flags(flags);
	return(0);
}

static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
{
	struct device *dev = ptr;
	int i;

	/* check for loopback devices */
	if (dev->flags & IFF_LOOPBACK)
		return(NOTIFY_DONE);

	switch (event) {
	case NETDEV_DOWN:
		if (br_stats.flags & BR_DEBUG)
			printk("br_device_event: NETDEV_DOWN...\n");
		/* find our device and mark it down */
		for (i = One; i <= No_of_ports; i++) {
			if (port_info[i].dev == dev) {
				disable_port(i);
				return NOTIFY_DONE;
				break;
			}
		}
		break;
	case NETDEV_UP:
		if (br_stats.flags & BR_DEBUG)
			printk("br_device_event: NETDEV_UP...\n");
		/* Only handle ethernet ports */
		if(dev->type!=ARPHRD_ETHER && dev->type!=ARPHRD_LOOPBACK)
			return NOTIFY_DONE;
		/* look up an unused device and enable it */
		for (i = One; i <= No_of_ports; i++) {
			if ((port_info[i].dev == (struct device *)0) ||
				(port_info[i].dev == dev)) {
				port_info[i].dev = dev;
				enable_port(i);
				set_path_cost(i, br_port_cost(dev));
				set_port_priority(i, 128); 
				port_info[i].port_id = i;
				/* set bridge addr from 1st device addr */
				if ((bridge_info.bridge_id.BRIDGE_ID[0] == 0) &&
						(bridge_info.bridge_id.BRIDGE_ID[1] == 0)) {
					memcpy(bridge_info.bridge_id.BRIDGE_ID_ULA, dev->dev_addr, 6);
					bridge_info.bridge_id.BRIDGE_PRIORITY = port_info[i].port_id;
					set_bridge_priority(&bridge_info.bridge_id);
				}	
				make_forwarding(i);
				return NOTIFY_DONE;
				break;
			}
		}
		break;
#if 0
	default:
		printk("br_device_event: unknown event [%x]\n",
			(unsigned int)event);
#endif			
	}
	return NOTIFY_DONE;
}

/*
 * following routine is called when a frame is received
 * from an interface, it returns 1 when it consumes the
 * frame, 0 when it does not
 */

int br_receive_frame(struct sk_buff *skb)	/* 3.5 */
{
	int port;
	int i;
	
	if (br_stats.flags & BR_DEBUG)
		printk("br_receive_frame: ");
	/* sanity */
	if (!skb) {
		printk(KERN_CRIT "br_receive_frame: no skb!\n");
		return(1);
	}

	skb->pkt_bridged = IS_BRIDGED;

	/* check for loopback */
	if (skb->dev->flags & IFF_LOOPBACK)
		return(0);

	port = find_port(skb->dev);
	
	skb->arp = 1;		/* Received frame so it is resolved */
	skb->h.raw = skb->mac.raw;
	if (br_stats.flags & BR_DEBUG)
		printk("port %i src %02x:%02x:%02x:%02x:%02x:%02x\
			dest %02x:%02x:%02x:%02x:%02x:%02x\n", 
			port,
			skb->h.eth->h_source[0],
			skb->h.eth->h_source[1],
			skb->h.eth->h_source[2],
			skb->h.eth->h_source[3],
			skb->h.eth->h_source[4],
			skb->h.eth->h_source[5],
			skb->h.eth->h_dest[0],
			skb->h.eth->h_dest[1],
			skb->h.eth->h_dest[2],
			skb->h.eth->h_dest[3],
			skb->h.eth->h_dest[4],
			skb->h.eth->h_dest[5]);

	if (!port) {
		if(br_stats.flags&BR_DEBUG)
			printk("\nbr_receive_frame: no port!\n");
		return(0);
	}

	switch (port_info[port].state) 
	{
		case Learning:
			(void) br_learn(skb, port);	/* 3.8 */
			/* fall through */
		case Listening:
			/* process BPDUs */
			if (memcmp(skb->h.eth->h_dest, bridge_ula, 6) == 0) {
				br_bpdu(skb);
				return(1); /* br_bpdu consumes skb */
			}
			/* fall through */
		case Blocking:
			/* fall through */
		case Disabled:
			/* should drop frames, but for now, we let
			 * them get passed up to the next higher layer
			return(br_drop(skb));	
			 */
			return(0);	/* pass frame up stack */
			break;
		case Forwarding:
			(void) br_learn(skb, port);	/* 3.8 */
			/* process BPDUs */
			if (memcmp(skb->h.eth->h_dest, bridge_ula, 
					ETH_ALEN) == 0) 
			{
				/*printk("frame bpdu processor for me!!!\n");*/
				br_bpdu(skb);
				return(1); /* br_bpdu consumes skb */
			}
			/* is frame for me? */	
			if (memcmp(skb->h.eth->h_dest, 
					port_info[port].dev->dev_addr, 
					ETH_ALEN) == 0) 
			{
				/* Packet is for us */
				skb->pkt_type = PACKET_HOST;
				return(0);	/* pass frame up our stack (this will */
						/* happen in net_bh() in dev.c) */
			}
			/* ok, forward this frame... */
			return(br_forward(skb, port));
		default:
			printk(KERN_DEBUG "br_receive_frame: port [%i] unknown state [%i]\n",
				port, port_info[port].state);
			return(0);	/* pass frame up stack? */
	}
}

/*
 * the following routine is called to transmit frames from the host
 * stack.  it returns 1 when it consumes the frame and
 * 0 when it does not.
 */

int br_tx_frame(struct sk_buff *skb)	/* 3.5 */
{
	int port;
	
	/* sanity */
	if (!skb) 
	{
		printk(KERN_CRIT "br_tx_frame: no skb!\n");
		return(0);
	}
	
	if (!skb->dev)
	{
		printk(KERN_CRIT "br_tx_frame: no dev!\n");
		return(0);
	}
	
	/* check for loopback */
	if (skb->dev->flags & IFF_LOOPBACK)
		return(0);

	skb->h.raw = skb->data;
	port = 0;	/* an impossible port */	
	if (br_stats.flags & BR_DEBUG)
		printk("br_tx_fr : port %i src %02x:%02x:%02x:%02x:%02x:%02x\
	  		dest %02x:%02x:%02x:%02x:%02x:%02x\n", 
			port,
			skb->h.eth->h_source[0],
			skb->h.eth->h_source[1],
			skb->h.eth->h_source[2],
			skb->h.eth->h_source[3],
			skb->h.eth->h_source[4],
			skb->h.eth->h_source[5],
			skb->h.eth->h_dest[0],
			skb->h.eth->h_dest[1],
			skb->h.eth->h_dest[2],
			skb->h.eth->h_dest[3],
			skb->h.eth->h_dest[4],
			skb->h.eth->h_dest[5]);
	return(br_forward(skb, port));
}

/*
 * this routine returns 0 when it learns (or updates) from the
 * frame, and -1 if the frame is simply discarded due to port
 * state or lack of resources...
 */

int br_learn(struct sk_buff *skb, int port)	/* 3.8 */
{
	struct fdb *f;

	switch (port_info[port].state) {
		case Listening:
		case Blocking:
		case Disabled:
		default:
			return(-1);
			/* break; */
		case Learning:
		case Forwarding:
			/* don't keep group addresses in the tree */
			if (skb->h.eth->h_source[0] & 0x01)
				return(-1);
			
			f = (struct fdb *)kmalloc(sizeof(struct fdb), 
				GFP_ATOMIC);

			if (!f) {
				printk(KERN_DEBUG "br_learn: unable to malloc fdb\n");
				return(-1);
			}
			f->port = port;	/* source port */
			memcpy(f->ula, skb->h.eth->h_source, 6);
			f->timer = CURRENT_TIME;
			f->flags = FDB_ENT_VALID;
			/*
			 * add entity to AVL tree.  If entity already
			 * exists in the tree, update the fields with
			 * what we have here.
		  	 */
			if (br_avl_insert(f) == 0) { /* update */
				kfree(f);
				return(0);
			}
			/* add to head of port chain */
			f->fdb_next = port_info[port].fdb;
			port_info[port].fdb = f;
			return(0);
			/* break */
	}
}

/*
 * this routine always consumes the frame
 */

int br_drop(struct sk_buff *skb)
{
	kfree_skb(skb, 0);
	return(1);
}

/*
 * this routine always consumes the frame
 */

int br_dev_drop(struct sk_buff *skb)
{
	dev_kfree_skb(skb, 0);
	return(1);
}

/*
 * this routine returns 1 if it consumes the frame, 0
 * if not...
 */

int br_forward(struct sk_buff *skb, int port)	/* 3.7 */
{
	struct fdb *f;
	
	/*
   	 * flood all ports with frames destined for a group
	 * address.  If frame came from above, drop it,
	 * otherwise it will be handled in br_receive_frame()
	 * Multicast frames will also need to be seen
	 * by our upper layers.
	 */	
	if (skb->h.eth->h_dest[0] & 0x01) 
	{
		/* group address */
		br_flood(skb, port);
		/*
		 *	External groups are fed out via the normal source
		 *	This probably should be dropped since the flood will
		 *	have sent it anyway.
		 */
		if (port == 0) 			/* locally generated */
			return(br_dev_drop(skb));
		return(0);
	} else {
		/* locate port to forward to */
		f = br_avl_find_addr(skb->h.eth->h_dest);
		/*
		 *	Send flood and drop.
		 */
		if (!f || !(f->flags & FDB_ENT_VALID)) {
		 	/* not found; flood all ports */
			br_flood(skb, port);
			return(br_dev_drop(skb));
		}
		/*
		 *	Sending
		 */
		if (f->port!=port && port_info[f->port].state == Forwarding) {
			/* has entry expired? */
			if (f->timer + fdb_aging_time < CURRENT_TIME) {
				/* timer expired, invalidate entry */
				f->flags &= ~FDB_ENT_VALID;
				if (br_stats.flags & BR_DEBUG)
					printk("fdb entry expired...\n");
				/*
				 *	Send flood and drop original
				 */
				br_flood(skb, port);
				return(br_dev_drop(skb));
			}
			/* mark that's we've been here... */
			skb->pkt_bridged = IS_BRIDGED;
			
			/* reset the skb->ip pointer */	
			skb->h.raw = skb->data + ETH_HLEN;

			/*
			 *	Send the buffer out.
			 */
			 
			skb->dev=port_info[f->port].dev;
			 
			/*
			 *	We send this still locked
			 */
			dev_queue_xmit(skb, skb->dev,1);
			return(1);	/* skb has been consumed */
		} else {
			/*
			 *	Arrived on the right port, we discard
			 */
			return(br_dev_drop(skb));
		}
	}
}

/*
 * this routine sends a copy of the frame to all forwarding ports
 * with the exception of the port given.  This routine never
 * consumes the original frame.
 */
	
int br_flood(struct sk_buff *skb, int port)
{
	int i;
	struct sk_buff *nskb;

	for (i = One; i <= No_of_ports; i++) 
	{
		if (i == port)
			continue;
		if (port_info[i].state == Forwarding) 
		{
			nskb = skb_clone(skb, GFP_ATOMIC);
			if(nskb==NULL)
				continue;
			/* mark that's we've been here... */
			nskb->pkt_bridged = IS_BRIDGED;
			/* Send to each port in turn */
			nskb->dev= port_info[i].dev;
			/* To get here we must have done ARP already,
			   or have a received valid MAC header */
			nskb->arp = 1;
			
/*			printk("Flood to port %d\n",i);*/
			nskb->h.raw = nskb->data + ETH_HLEN;
			dev_queue_xmit(nskb,nskb->dev,1);
		}
	}
	return(0);
}

int find_port(struct device *dev)
{
	int i;

	for (i = One; i <= No_of_ports; i++)
		if ((port_info[i].dev == dev) && 
			(port_info[i].state != Disabled))
			return(i);
	return(0);
}

int br_port_cost(struct device *dev)	/* 4.10.2 */
{
	if (strncmp(dev->name, "eth", 3) == 0)	/* ethernet */
		return(100);
	if (strncmp(dev->name, "wic", 3) == 0)	/* wic */
		return(1600);
	if (strncmp(dev->name, "plip",4) == 0) /* plip */
		return (1600);
	return(100);	/* default */
}

/*
 * this routine always consumes the skb 
 */

void br_bpdu(struct sk_buff *skb) /* consumes skb */
{
	Tcn_bpdu *bpdu;
	int port;

	port = find_port(skb->dev);
	if (port == 0) {	/* unknown port */
		br_drop(skb);
		return;
	}
		
	bpdu = (Tcn_bpdu *) (skb->data + ETH_HLEN);
	switch (bpdu->type) {
		case BPDU_TYPE_CONFIG:
			received_config_bpdu(port, (Config_bpdu *)bpdu);
			break;
		case BPDU_TYPE_TOPO_CHANGE:
			received_tcn_bpdu(port, bpdu);
			break;
		default:
			printk(KERN_DEBUG "br_bpdu: received unknown bpdu, type = %i\n",
				bpdu->type);
			/* break; */
	}
	br_drop(skb);
}

int br_ioctl(unsigned int cmd, void *arg)
{
	int err;
	struct br_cf bcf;

	switch(cmd)
	{
		case SIOCGIFBR:	/* get bridging control blocks */
			err = verify_area(VERIFY_WRITE, arg, 
				sizeof(struct br_stat));
			if(err)
				return err;
			memcpy(&br_stats.bridge_data, &bridge_info, sizeof(Bridge_data));
			memcpy(&br_stats.port_data, &port_info, sizeof(Port_data)*No_of_ports);
			memcpy_tofs(arg, &br_stats, sizeof(struct br_stat));
			return(0);
		case SIOCSIFBR:
			if (!suser())
				return -EPERM;
			err = verify_area(VERIFY_READ, arg, 
				sizeof(struct br_cf));
			if(err)
				return err;
			memcpy_fromfs(&bcf, arg, sizeof(struct br_cf));
			switch (bcf.cmd) {
				case BRCMD_BRIDGE_ENABLE:
					if (br_stats.flags & BR_UP)
						return(-EALREADY);	
					printk(KERN_DEBUG "br: enabling bridging function\n");
					br_stats.flags |= BR_UP;	/* enable bridge */
					start_hello_timer();
					break;
				case BRCMD_BRIDGE_DISABLE:
					if (!(br_stats.flags & BR_UP))
						return(-EALREADY);	
					printk(KERN_DEBUG "br: disabling bridging function\n");
					br_stats.flags &= ~BR_UP;	/* disable bridge */
					stop_hello_timer();
#if 0					
					for (i = One; i <= No_of_ports; i++)
						if (port_info[i].state != Disabled)
							disable_port(i);
#endif							
					break;
				case BRCMD_PORT_ENABLE:
					if (port_info[bcf.arg1].dev == 0)
						return(-EINVAL);
					if (port_info[bcf.arg1].state != Disabled)
						return(-EALREADY);
					printk(KERN_DEBUG "br: enabling port %i\n",bcf.arg1);
					enable_port(bcf.arg1);
					break;
				case BRCMD_PORT_DISABLE:
					if (port_info[bcf.arg1].dev == 0)
						return(-EINVAL);
					if (port_info[bcf.arg1].state == Disabled)
						return(-EALREADY);
					printk(KERN_DEBUG "br: disabling port %i\n",bcf.arg1);
					disable_port(bcf.arg1);
					break;
				case BRCMD_SET_BRIDGE_PRIORITY:
					set_bridge_priority((bridge_id_t *)&bcf.arg1);
					break;
				case BRCMD_SET_PORT_PRIORITY:
					if (port_info[bcf.arg1].dev == 0)
						return(-EINVAL);
					set_port_priority(bcf.arg1, bcf.arg2);
					break;
				case BRCMD_SET_PATH_COST:
					if (port_info[bcf.arg1].dev == 0)
						return(-EINVAL);
					set_path_cost(bcf.arg1, bcf.arg2);
					break;
				case BRCMD_ENABLE_DEBUG:
					br_stats.flags |= BR_DEBUG;
					break;
				case BRCMD_DISABLE_DEBUG:
					br_stats.flags &= ~BR_DEBUG;
					break;
				default:
					return -EINVAL;
			}
			return(0);
		default:
			return -EINVAL;
	}
	/*NOTREACHED*/
	return 0;
}

int br_cmp(unsigned int *a, unsigned int *b)
{
	int i;	
	for (i=0; i<2; i++) 
	{
		if (a[i] == b[i])
			continue;
		if (a[i] < b[i])
			return(1);
		if (a[i] > b[i])
			return(-1);
	}
	return(0);
}