summaryrefslogtreecommitdiffstats
path: root/sbin/routed/if.c
blob: 9160e6f2bb130f5e6b39fd9b17d7b1fcbbe314c4 (plain)
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
/*
 * Copyright (c) 1983, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <stdint.h>

#include "defs.h"
#include "pathnames.h"

#ifdef __NetBSD__
__RCSID("$NetBSD$");
#elif defined(__FreeBSD__)
__RCSID("$FreeBSD$");
#else
__RCSID("$Revision: 2.27 $");
#ident "$Revision: 2.27 $"
#endif

struct ifhead ifnet = LIST_HEAD_INITIALIZER(ifnet);	/* all interfaces */
struct ifhead remote_if = LIST_HEAD_INITIALIZER(remote_if);	/* remote interfaces */

/* hash table for all interfaces, big enough to tolerate ridiculous
 * numbers of IP aliases.  Crazy numbers of aliases such as 7000
 * still will not do well, but not just in looking up interfaces
 * by name or address.
 */
#define AHASH_LEN 211			/* must be prime */
#define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
static struct interface *ahash_tbl[AHASH_LEN];

#define BHASH_LEN 211			/* must be prime */
#define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
static struct interface *bhash_tbl[BHASH_LEN];


/* hash for physical interface names.
 * Assume there are never more 100 or 200 real interfaces, and that
 * aliases are put on the end of the hash chains.
 */
#define NHASH_LEN 97
static struct interface *nhash_tbl[NHASH_LEN];

int	tot_interfaces;			/* # of remote and local interfaces */
int	rip_interfaces;			/* # of interfaces doing RIP */
static int foundloopback;			/* valid flag for loopaddr */
naddr	loopaddr;			/* our address on loopback */
static struct rt_spare loop_rts;

struct timeval ifinit_timer;
static struct timeval last_ifinit;
#define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec		\
			   && last_ifinit.tv_usec == now.tv_usec	\
			   && timercmp(&ifinit_timer, &now, >))

int	have_ripv1_out;			/* have a RIPv1 interface */
static int have_ripv1_in;


static void if_bad(struct interface *);
static int addrouteforif(struct interface *);

static struct interface**
nhash(char *p)
{
	u_int i;

	for (i = 0; *p != '\0'; p++) {
		i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
		i ^= *p;
	}
	return &nhash_tbl[i % NHASH_LEN];
}


/* Link a new interface into the lists and hash tables.
 */
void
if_link(struct interface *ifp)
{
	struct interface **hifp;

	LIST_INSERT_HEAD(&ifnet, ifp, int_list);

	hifp = AHASH(ifp->int_addr);
	ifp->int_ahash_prev = hifp;
	if ((ifp->int_ahash = *hifp) != 0)
		(*hifp)->int_ahash_prev = &ifp->int_ahash;
	*hifp = ifp;

	if (ifp->int_if_flags & IFF_BROADCAST) {
		hifp = BHASH(ifp->int_brdaddr);
		ifp->int_bhash_prev = hifp;
		if ((ifp->int_bhash = *hifp) != 0)
			(*hifp)->int_bhash_prev = &ifp->int_bhash;
		*hifp = ifp;
	}

	if (ifp->int_state & IS_REMOTE)
		LIST_INSERT_HEAD(&remote_if, ifp, remote_list);

	hifp = nhash(ifp->int_name);
	if (ifp->int_state & IS_ALIAS) {
		/* put aliases on the end of the hash chain */
		while (*hifp != 0)
			hifp = &(*hifp)->int_nhash;
	}
	ifp->int_nhash_prev = hifp;
	if ((ifp->int_nhash = *hifp) != 0)
		(*hifp)->int_nhash_prev = &ifp->int_nhash;
	*hifp = ifp;
}


/* Find the interface with an address
 */
struct interface *
ifwithaddr(naddr addr,
	   int	bcast,			/* notice IFF_BROADCAST address */
	   int	remote)			/* include IS_REMOTE interfaces */
{
	struct interface *ifp, *possible = 0;

	remote = (remote == 0) ? IS_REMOTE : 0;

	for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
		if (ifp->int_addr != addr)
			continue;
		if ((ifp->int_state & remote) != 0)
			continue;
		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
			return ifp;
		possible = ifp;
	}

	if (possible || !bcast)
		return possible;

	for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
		if (ifp->int_brdaddr != addr)
			continue;
		if ((ifp->int_state & remote) != 0)
			continue;
		if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
			return ifp;
		possible = ifp;
	}

	return possible;
}


/* find the interface with a name
 */
static struct interface *
ifwithname(char *name,			/* "ec0" or whatever */
	   naddr addr)			/* 0 or network address */
{
	struct interface *ifp;

	for (;;) {
		for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
			/* If the network address is not specified,
			 * ignore any alias interfaces.  Otherwise, look
			 * for the interface with the target name and address.
			 */
			if (!strcmp(ifp->int_name, name)
			    && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
				|| (ifp->int_addr == addr)))
				return ifp;
		}

		/* If there is no known interface, maybe there is a
		 * new interface.  So just once look for new interfaces.
		 */
		if (IF_RESCAN_DELAY())
			return 0;
		ifinit();
	}
}


struct interface *
ifwithindex(u_short ifindex,
	    int rescan_ok)
{
	struct interface *ifp;

	for (;;) {
		LIST_FOREACH(ifp, &ifnet, int_list) {
			if (ifp->int_index == ifindex)
				return ifp;
		}

		/* If there is no known interface, maybe there is a
		 * new interface.  So just once look for new interfaces.
		 */
		if (!rescan_ok
		    || IF_RESCAN_DELAY())
			return 0;
		ifinit();
	}
}


/* Find an interface from which the specified address
 * should have come from.  Used for figuring out which
 * interface a packet came in on.
 */
struct interface *
iflookup(naddr addr)
{
	struct interface *ifp, *maybe;
	int once = 0;

	maybe = 0;
	for (;;) {
		LIST_FOREACH(ifp, &ifnet, int_list) {
			if (ifp->int_if_flags & IFF_POINTOPOINT) {
				/* finished with a match */
				if (ifp->int_dstaddr == addr)
					return ifp;

			} else {
				/* finished with an exact match */
				if (ifp->int_addr == addr)
					return ifp;

				/* Look for the longest approximate match.
				 */
				if (on_net(addr, ifp->int_net, ifp->int_mask)
				    && (maybe == 0
					|| ifp->int_mask > maybe->int_mask))
					maybe = ifp;
			}
		}

		if (maybe != 0 || once || IF_RESCAN_DELAY())
			return maybe;
		once = 1;

		/* If there is no known interface, maybe there is a
		 * new interface.  So just once look for new interfaces.
		 */
		ifinit();
	}
}


/* Return the classical netmask for an IP address.
 */
naddr					/* host byte order */
std_mask(naddr addr)			/* network byte order */
{
	addr = ntohl(addr);			/* was a host, not a network */

	if (addr == 0)			/* default route has mask 0 */
		return 0;
	if (IN_CLASSA(addr))
		return IN_CLASSA_NET;
	if (IN_CLASSB(addr))
		return IN_CLASSB_NET;
	return IN_CLASSC_NET;
}


/* Find the netmask that would be inferred by RIPv1 listeners
 *	on the given interface for a given network.
 *	If no interface is specified, look for the best fitting	interface.
 */
naddr
ripv1_mask_net(naddr addr,		/* in network byte order */
	       struct interface *ifp)	/* as seen on this interface */
{
	struct r1net *r1p;
	naddr mask = 0;

	if (addr == 0)			/* default always has 0 mask */
		return mask;

	if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
		/* If the target network is that of the associated interface
		 * on which it arrived, then use the netmask of the interface.
		 */
		if (on_net(addr, ifp->int_net, ifp->int_std_mask))
			mask = ifp->int_ripv1_mask;

	} else {
		/* Examine all interfaces, and if it the target seems
		 * to have the same network number of an interface, use the
		 * netmask of that interface.  If there is more than one
		 * such interface, prefer the interface with the longest
		 * match.
		 */
		LIST_FOREACH(ifp, &ifnet, int_list) {
			if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
			    && ifp->int_ripv1_mask > mask
			    && ifp->int_ripv1_mask != HOST_MASK)
				mask = ifp->int_ripv1_mask;
		}

	}

	/* check special definitions */
	if (mask == 0) {
		for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
			if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
			    && r1p->r1net_mask > mask)
				mask = r1p->r1net_mask;
		}

		/* Otherwise, make the classic A/B/C guess.
		 */
		if (mask == 0)
			mask = std_mask(addr);
	}

	return mask;
}


naddr
ripv1_mask_host(naddr addr,		/* in network byte order */
		struct interface *ifp)	/* as seen on this interface */
{
	naddr mask = ripv1_mask_net(addr, ifp);


	/* If the computed netmask does not mask the address,
	 * then assume it is a host address
	 */
	if ((ntohl(addr) & ~mask) != 0)
		mask = HOST_MASK;
	return mask;
}


/* See if an IP address looks reasonable as a destination.
 */
int					/* 0=bad */
check_dst(naddr addr)
{
	addr = ntohl(addr);

	if (IN_CLASSA(addr)) {
		if (addr == 0)
			return 1;	/* default */

		addr >>= IN_CLASSA_NSHIFT;
		return (addr != 0 && addr != IN_LOOPBACKNET);
	}

	return (IN_CLASSB(addr) || IN_CLASSC(addr));
}


/* See a new interface duplicates an existing interface.
 */
struct interface *
check_dup(naddr addr,			/* IP address, so network byte order */
	  naddr dstaddr,		/* ditto */
	  naddr mask,			/* mask, so host byte order */
	  int if_flags)
{
	struct interface *ifp;

	LIST_FOREACH(ifp, &ifnet, int_list) {
		if (ifp->int_mask != mask)
			continue;

		if (!iff_up(ifp->int_if_flags))
			continue;

		/* The local address can only be shared with a point-to-point
		 * link.
		 */
		if ((!(ifp->int_state & IS_REMOTE) || !(if_flags & IS_REMOTE))
		    && ifp->int_addr == addr
		    && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
			return ifp;

		if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
			return ifp;
	}
	return 0;
}


/* See that a remote gateway is reachable.
 *	Note that the answer can change as real interfaces come and go.
 */
int					/* 0=bad */
check_remote(struct interface *ifp)
{
	struct rt_entry *rt;

	/* do not worry about other kinds */
	if (!(ifp->int_state & IS_REMOTE))
	    return 1;

	rt = rtfind(ifp->int_addr);
	if (rt != 0
	    && rt->rt_ifp != 0
	    &&on_net(ifp->int_addr,
		     rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
		return 1;

	/* the gateway cannot be reached directly from one of our
	 * interfaces
	 */
	if (!(ifp->int_state & IS_BROKE)) {
		msglog("unreachable gateway %s in "_PATH_GATEWAYS,
		       naddr_ntoa(ifp->int_addr));
		if_bad(ifp);
	}
	return 0;
}


/* Delete an interface.
 */
static void
ifdel(struct interface *ifp)
{
	struct interface *ifp1;


	trace_if("Del", ifp);

	ifp->int_state |= IS_BROKE;

	LIST_REMOVE(ifp, int_list);
	*ifp->int_ahash_prev = ifp->int_ahash;
	if (ifp->int_ahash != 0)
		ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
	*ifp->int_nhash_prev = ifp->int_nhash;
	if (ifp->int_nhash != 0)
		ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
	if (ifp->int_if_flags & IFF_BROADCAST) {
		*ifp->int_bhash_prev = ifp->int_bhash;
		if (ifp->int_bhash != 0)
			ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
	}
	if (ifp->int_state & IS_REMOTE)
		LIST_REMOVE(ifp, remote_list);

	if (!(ifp->int_state & IS_ALIAS)) {
		/* delete aliases when the main interface dies
		 */
		LIST_FOREACH(ifp1, &ifnet, int_list) {
			if (ifp1 != ifp
			    && !strcmp(ifp->int_name, ifp1->int_name))
				ifdel(ifp1);
		}

		if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
			struct group_req gr;
			struct sockaddr_in *sin;

			memset(&gr, 0, sizeof(gr));
			gr.gr_interface = ifp->int_index;
			sin = (struct sockaddr_in *)&gr.gr_group;
			sin->sin_family = AF_INET;
#ifdef _HAVE_SIN_LEN
			sin->sin_len = sizeof(struct sockaddr_in);
#endif
			sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
			if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
				       &gr, sizeof(gr)) < 0
			    && errno != EADDRNOTAVAIL
			    && !TRACEACTIONS)
				LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
			if (rip_sock_mcast == ifp)
				rip_sock_mcast = 0;
		}
		if (ifp->int_rip_sock >= 0) {
			(void)close(ifp->int_rip_sock);
			ifp->int_rip_sock = -1;
			fix_select();
		}

		tot_interfaces--;
		if (!IS_RIP_OFF(ifp->int_state))
			rip_interfaces--;

		/* Zap all routes associated with this interface.
		 * Assume routes just using gateways beyond this interface
		 * will timeout naturally, and have probably already died.
		 */
		(void)rn_walktree(rhead, walk_bad, 0);

		set_rdisc_mg(ifp, 0);
		if_bad_rdisc(ifp);
	}

	free(ifp);
}


/* Mark an interface ill.
 */
void
if_sick(struct interface *ifp)
{
	if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
		ifp->int_state |= IS_SICK;
		ifp->int_act_time = NEVER;
		trace_if("Chg", ifp);

		LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
	}
}


/* Mark an interface dead.
 */
static void
if_bad(struct interface *ifp)
{
	struct interface *ifp1;


	if (ifp->int_state & IS_BROKE)
		return;

	LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);

	ifp->int_state |= (IS_BROKE | IS_SICK);
	ifp->int_act_time = NEVER;
	ifp->int_query_time = NEVER;
	ifp->int_data.ts = now.tv_sec;

	trace_if("Chg", ifp);

	if (!(ifp->int_state & IS_ALIAS)) {
		LIST_FOREACH(ifp1, &ifnet, int_list) {
			if (ifp1 != ifp
			    && !strcmp(ifp->int_name, ifp1->int_name))
				if_bad(ifp1);
		}
		(void)rn_walktree(rhead, walk_bad, 0);
		if_bad_rdisc(ifp);
	}
}


/* Mark an interface alive
 */
int					/* 1=it was dead */
if_ok(struct interface *ifp,
      const char *type)
{
	struct interface *ifp1;


	if (!(ifp->int_state & IS_BROKE)) {
		if (ifp->int_state & IS_SICK) {
			trace_act("%sinterface %s to %s working better",
				  type,
				  ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
			ifp->int_state &= ~IS_SICK;
		}
		return 0;
	}

	msglog("%sinterface %s to %s restored",
	       type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
	ifp->int_state &= ~(IS_BROKE | IS_SICK);
	ifp->int_data.ts = 0;

	if (!(ifp->int_state & IS_ALIAS)) {
		LIST_FOREACH(ifp1, &ifnet, int_list) {
			if (ifp1 != ifp
			    && !strcmp(ifp->int_name, ifp1->int_name))
				if_ok(ifp1, type);
		}
		if_ok_rdisc(ifp);
	}

	if (ifp->int_state & IS_REMOTE) {
		if (!addrouteforif(ifp))
			return 0;
	}
	return 1;
}


/* disassemble routing message
 */
void
rt_xaddrs(struct rt_addrinfo *info,
	  struct sockaddr *sa,
	  struct sockaddr *lim,
	  int addrs)
{
	int i;
#ifdef _HAVE_SA_LEN
	static struct sockaddr sa_zero;
#endif

	memset(info, 0, sizeof(*info));
	info->rti_addrs = addrs;
	for (i = 0; i < RTAX_MAX && sa < lim; i++) {
		if ((addrs & (1 << i)) == 0)
			continue;
		info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
		sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
	}
}


/* Find the network interfaces which have configured themselves.
 *	This must be done regularly, if only for extra addresses
 *	that come and go on interfaces.
 */
void
ifinit(void)
{
	static struct ifa_msghdr *sysctl_buf;
	static size_t sysctl_buf_size = 0;
	uint complaints = 0;
	static u_int prev_complaints = 0;
#	define COMP_NOT_INET	0x001
#	define COMP_NOADDR	0x002
#	define COMP_BADADDR	0x004
#	define COMP_NODST	0x008
#	define COMP_NOBADR	0x010
#	define COMP_NOMASK	0x020
#	define COMP_DUP		0x040
#	define COMP_BAD_METRIC	0x080
#	define COMP_NETMASK	0x100

	struct interface ifs, ifs0, *ifp, *ifp1;
	struct rt_entry *rt;
	size_t needed;
	int mib[6];
	struct if_msghdr *ifm;
	void *ifam_lim;
	struct ifa_msghdr *ifam, *ifam2;
	int in, ierr, out, oerr;
	struct intnet *intnetp;
	struct rt_addrinfo info;
#ifdef SIOCGIFMETRIC
	struct ifreq ifr;
#endif


	last_ifinit = now;
	ifinit_timer.tv_sec = now.tv_sec + (supplier
					    ? CHECK_ACT_INTERVAL
					    : CHECK_QUIET_INTERVAL);

	/* mark all interfaces so we can get rid of those that disappear */
	LIST_FOREACH(ifp, &ifnet, int_list)
		ifp->int_state &= ~(IS_CHECKED | IS_DUP);

	/* Fetch the interface list, without too many system calls
	 * since we do it repeatedly.
	 */
	mib[0] = CTL_NET;
	mib[1] = PF_ROUTE;
	mib[2] = 0;
	mib[3] = AF_INET;
	mib[4] = NET_RT_IFLIST;
	mib[5] = 0;
	for (;;) {
		if ((needed = sysctl_buf_size) != 0) {
			if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
				break;
			/* retry if the table grew */
			if (errno != ENOMEM && errno != EFAULT)
				BADERR(1, "ifinit: sysctl(RT_IFLIST)");
			free(sysctl_buf);
			needed = 0;
		}
		if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
			BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
		sysctl_buf = rtmalloc(sysctl_buf_size = needed,
				      "ifinit sysctl");
	}

	/* XXX: thanks to malloc(3), alignment can be presumed OK */
	ifam_lim = (char *)sysctl_buf + needed;
	for (ifam = sysctl_buf; (void *)ifam < ifam_lim; ifam = ifam2) {

		ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);

#ifdef RTM_OIFINFO
		if (ifam->ifam_type == RTM_OIFINFO)
			continue;	/* just ignore compat message */
#endif
		if (ifam->ifam_type == RTM_IFINFO) {
			struct sockaddr_dl *sdl;

			ifm = (struct if_msghdr *)ifam;
			/* make prototype structure for the IP aliases
			 */
			memset(&ifs0, 0, sizeof(ifs0));
			ifs0.int_rip_sock = -1;
			ifs0.int_index = ifm->ifm_index;
			ifs0.int_if_flags = ifm->ifm_flags;
			ifs0.int_state = IS_CHECKED;
			ifs0.int_query_time = NEVER;
			ifs0.int_act_time = now.tv_sec;
			ifs0.int_data.ts = now.tv_sec;
			ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
			ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
			ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
			ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
#ifdef sgi
			ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
#endif
			sdl = (struct sockaddr_dl *)(ifm + 1);
			sdl->sdl_data[sdl->sdl_nlen] = 0;
			strncpy(ifs0.int_name, sdl->sdl_data,
				MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
			continue;
		}
		if (ifam->ifam_type != RTM_NEWADDR) {
			logbad(1,"ifinit: out of sync");
			continue;
		}
		rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
			  (struct sockaddr *)ifam2,
			  ifam->ifam_addrs);

		/* Prepare for the next address of this interface, which
		 * will be an alias.
		 * Do not output RIP or Router-Discovery packets via aliases.
		 */
		memcpy(&ifs, &ifs0, sizeof(ifs));
		ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);

		if (INFO_IFA(&info) == 0) {
			if (iff_up(ifs.int_if_flags)) {
				if (!(prev_complaints & COMP_NOADDR))
					msglog("%s has no address",
					       ifs.int_name);
				complaints |= COMP_NOADDR;
			}
			continue;
		}
		if (INFO_IFA(&info)->sa_family != AF_INET) {
			if (iff_up(ifs.int_if_flags)) {
				if (!(prev_complaints & COMP_NOT_INET))
					trace_act("%s: not AF_INET",
						  ifs.int_name);
				complaints |= COMP_NOT_INET;
			}
			continue;
		}

		ifs.int_addr = S_ADDR(INFO_IFA(&info));

		if (ntohl(ifs.int_addr)>>24 == 0
		    || ntohl(ifs.int_addr)>>24 == 0xff) {
			if (iff_up(ifs.int_if_flags)) {
				if (!(prev_complaints & COMP_BADADDR))
					msglog("%s has a bad address",
					       ifs.int_name);
				complaints |= COMP_BADADDR;
			}
			continue;
		}

		if (ifs.int_if_flags & IFF_LOOPBACK) {
			ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
			if (ifs.int_addr == htonl(INADDR_LOOPBACK))
				ifs.int_state |= IS_PASSIVE;
			ifs.int_dstaddr = ifs.int_addr;
			ifs.int_mask = HOST_MASK;
			ifs.int_ripv1_mask = HOST_MASK;
			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
			ifs.int_net = ntohl(ifs.int_dstaddr);
			if (!foundloopback) {
				foundloopback = 1;
				loopaddr = ifs.int_addr;
				loop_rts.rts_gate = loopaddr;
				loop_rts.rts_router = loopaddr;
			}

		} else if (ifs.int_if_flags & IFF_POINTOPOINT) {
			if (INFO_BRD(&info) == 0
			    || INFO_BRD(&info)->sa_family != AF_INET) {
				if (iff_up(ifs.int_if_flags)) {
					if (!(prev_complaints & COMP_NODST))
						msglog("%s has a bad"
						       " destination address",
						       ifs.int_name);
					complaints |= COMP_NODST;
				}
				continue;
			}
			ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
			if (ntohl(ifs.int_dstaddr)>>24 == 0
			    || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
				if (iff_up(ifs.int_if_flags)) {
					if (!(prev_complaints & COMP_NODST))
						msglog("%s has a bad"
						       " destination address",
						       ifs.int_name);
					complaints |= COMP_NODST;
				}
				continue;
			}
			ifs.int_mask = HOST_MASK;
			ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
			ifs.int_std_mask = std_mask(ifs.int_dstaddr);
			ifs.int_net = ntohl(ifs.int_dstaddr);

		}  else {
			if (INFO_MASK(&info) == 0) {
				if (iff_up(ifs.int_if_flags)) {
					if (!(prev_complaints & COMP_NOMASK))
						msglog("%s has no netmask",
						       ifs.int_name);
					complaints |= COMP_NOMASK;
				}
				continue;
			}
			ifs.int_dstaddr = ifs.int_addr;
			ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
			ifs.int_ripv1_mask = ifs.int_mask;
			ifs.int_std_mask = std_mask(ifs.int_addr);
			ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
			if (ifs.int_mask != ifs.int_std_mask)
				ifs.int_state |= IS_SUBNET;

			if (ifs.int_if_flags & IFF_BROADCAST) {
				if (INFO_BRD(&info) == 0) {
					if (iff_up(ifs.int_if_flags)) {
					    if (!(prev_complaints
						  & COMP_NOBADR))
						msglog("%s has"
						       "no broadcast address",
						       ifs.int_name);
					    complaints |= COMP_NOBADR;
					}
					continue;
				}
				ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
			}
		}
		ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
		ifs.int_std_addr = htonl(ifs.int_std_net);

		/* Use a minimum metric of one.  Treat the interface metric
		 * (default 0) as an increment to the hop count of one.
		 *
		 * The metric obtained from the routing socket dump of
		 * interface addresses is wrong.  It is not set by the
		 * SIOCSIFMETRIC ioctl.
		 */
#ifdef SIOCGIFMETRIC
		strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
		if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
			DBGERR(1, "ioctl(SIOCGIFMETRIC)");
			ifs.int_metric = 0;
		} else {
			ifs.int_metric = ifr.ifr_metric;
		}
#else
		ifs.int_metric = ifam->ifam_metric;
#endif
		if (ifs.int_metric > HOPCNT_INFINITY) {
			ifs.int_metric = 0;
			if (!(prev_complaints & COMP_BAD_METRIC)
			    && iff_up(ifs.int_if_flags)) {
				complaints |= COMP_BAD_METRIC;
				msglog("%s has a metric of %d",
				       ifs.int_name, ifs.int_metric);
			}
		}

		/* See if this is a familiar interface.
		 * If so, stop worrying about it if it is the same.
		 * Start it over if it now is to somewhere else, as happens
		 * frequently with PPP and SLIP.
		 */
		ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
						? ifs.int_addr
						: 0));
		if (ifp != 0) {
			ifp->int_state |= IS_CHECKED;

			if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
				  & (IFF_BROADCAST
				     | IFF_LOOPBACK
				     | IFF_POINTOPOINT
				     | IFF_MULTICAST))
			    || 0 != ((ifp->int_state ^ ifs.int_state)
				     & IS_ALIAS)
			    || ifp->int_addr != ifs.int_addr
			    || ifp->int_brdaddr != ifs.int_brdaddr
			    || ifp->int_dstaddr != ifs.int_dstaddr
			    || ifp->int_mask != ifs.int_mask
			    || ifp->int_metric != ifs.int_metric) {
				/* Forget old information about
				 * a changed interface.
				 */
				trace_act("interface %s has changed",
					  ifp->int_name);
				ifdel(ifp);
				ifp = 0;
			}
		}

		if (ifp != 0) {
			/* The primary representative of an alias worries
			 * about how things are working.
			 */
			if (ifp->int_state & IS_ALIAS)
				continue;

			/* note interfaces that have been turned off
			 */
			if (!iff_up(ifs.int_if_flags)) {
				if (iff_up(ifp->int_if_flags)) {
					msglog("interface %s to %s turned off",
					       ifp->int_name,
					       naddr_ntoa(ifp->int_dstaddr));
					if_bad(ifp);
					ifp->int_if_flags &= ~IFF_UP;
				} else if (now.tv_sec>(ifp->int_data.ts
						       + CHECK_BAD_INTERVAL)) {
					trace_act("interface %s has been off"
						  " %jd seconds; forget it",
						  ifp->int_name,
						  (intmax_t)now.tv_sec -
						      ifp->int_data.ts);
					ifdel(ifp);
				}
				continue;
			}
			/* or that were off and are now ok */
			if (!iff_up(ifp->int_if_flags)) {
				ifp->int_if_flags |= IFF_UP;
				(void)if_ok(ifp, "");
			}

			/* If it has been long enough,
			 * see if the interface is broken.
			 */
			if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
				continue;

			in = ifs.int_data.ipackets - ifp->int_data.ipackets;
			ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
			out = ifs.int_data.opackets - ifp->int_data.opackets;
			oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
#ifdef sgi
			/* Through at least IRIX 6.2, PPP and SLIP
			 * count packets dropped by the filters.
			 * But FDDI rings stuck non-operational count
			 * dropped packets as they wait for improvement.
			 */
			if (!(ifp->int_if_flags & IFF_POINTOPOINT))
				oerr += (ifs.int_data.odrops
					 - ifp->int_data.odrops);
#endif
			/* If the interface just awoke, restart the counters.
			 */
			if (ifp->int_data.ts == 0) {
				ifp->int_data = ifs.int_data;
				continue;
			}
			ifp->int_data = ifs.int_data;

			/* Withhold judgment when the short error
			 * counters wrap or the interface is reset.
			 */
			if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
				LIM_SEC(ifinit_timer,
					now.tv_sec+CHECK_BAD_INTERVAL);
				continue;
			}

			/* Withhold judgement when there is no traffic
			 */
			if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
				continue;

			/* It is bad if input or output is not working.
			 * Require presistent problems before marking it dead.
			 */
			if ((in <= ierr && ierr > 0)
			    || (out <= oerr && oerr > 0)) {
				if (!(ifp->int_state & IS_SICK)) {
					trace_act("interface %s to %s"
						  " sick: in=%d ierr=%d"
						  " out=%d oerr=%d",
						  ifp->int_name,
						  naddr_ntoa(ifp->int_dstaddr),
						  in, ierr, out, oerr);
					if_sick(ifp);
					continue;
				}
				if (!(ifp->int_state & IS_BROKE)) {
					msglog("interface %s to %s broken:"
					       " in=%d ierr=%d out=%d oerr=%d",
					       ifp->int_name,
					       naddr_ntoa(ifp->int_dstaddr),
					       in, ierr, out, oerr);
					if_bad(ifp);
				}
				continue;
			}

			/* otherwise, it is active and healthy
			 */
			ifp->int_act_time = now.tv_sec;
			(void)if_ok(ifp, "");
			continue;
		}

		/* This is a new interface.
		 * If it is dead, forget it.
		 */
		if (!iff_up(ifs.int_if_flags))
			continue;

		/* If it duplicates an existing interface,
		 * complain about it, mark the other one
		 * duplicated, and forget this one.
		 */
		ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
				ifs.int_if_flags);
		if (ifp != 0) {
			/* Ignore duplicates of itself, caused by having
			 * IP aliases on the same network.
			 */
			if (!strcmp(ifp->int_name, ifs.int_name))
				continue;

			if (!(prev_complaints & COMP_DUP)) {
				complaints |= COMP_DUP;
				msglog("%s (%s%s%s) is duplicated by"
				       " %s (%s%s%s)",
				       ifs.int_name,
				       addrname(ifs.int_addr,ifs.int_mask,1),
				       ((ifs.int_if_flags & IFF_POINTOPOINT)
					? "-->" : ""),
				       ((ifs.int_if_flags & IFF_POINTOPOINT)
					? naddr_ntoa(ifs.int_dstaddr) : ""),
				       ifp->int_name,
				       addrname(ifp->int_addr,ifp->int_mask,1),
				       ((ifp->int_if_flags & IFF_POINTOPOINT)
					? "-->" : ""),
				       ((ifp->int_if_flags & IFF_POINTOPOINT)
					? naddr_ntoa(ifp->int_dstaddr) : ""));
			}
			ifp->int_state |= IS_DUP;
			continue;
		}

		if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
			trace_act("%s is neither broadcast, point-to-point,"
				  " nor loopback",
				  ifs.int_name);
			if (!(ifs.int_state & IFF_MULTICAST))
				ifs.int_state |= IS_NO_RDISC;
		}


		/* It is new and ok.   Add it to the list of interfaces
		 */
		ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
		memcpy(ifp, &ifs, sizeof(*ifp));
		get_parms(ifp);
		if_link(ifp);
		trace_if("Add", ifp);

		/* Notice likely bad netmask.
		 */
		if (!(prev_complaints & COMP_NETMASK)
		    && !(ifp->int_if_flags & IFF_POINTOPOINT)
		    && ifp->int_addr != RIP_DEFAULT) {
			LIST_FOREACH(ifp1, &ifnet, int_list) {
				if (ifp1->int_mask == ifp->int_mask)
					continue;
				if (ifp1->int_if_flags & IFF_POINTOPOINT)
					continue;
				if (ifp1->int_dstaddr == RIP_DEFAULT)
					continue;
				/* ignore aliases on the right network */
				if (!strcmp(ifp->int_name, ifp1->int_name))
					continue;
				if (on_net(ifp->int_dstaddr,
					   ifp1->int_net, ifp1->int_mask)
				    || on_net(ifp1->int_dstaddr,
					      ifp->int_net, ifp->int_mask)) {
					msglog("possible netmask problem"
					       " between %s:%s and %s:%s",
					       ifp->int_name,
					       addrname(htonl(ifp->int_net),
							ifp->int_mask, 1),
					       ifp1->int_name,
					       addrname(htonl(ifp1->int_net),
							ifp1->int_mask, 1));
					complaints |= COMP_NETMASK;
				}
			}
		}

		if (!(ifp->int_state & IS_ALIAS)) {
			/* Count the # of directly connected networks.
			 */
			if (!(ifp->int_if_flags & IFF_LOOPBACK))
				tot_interfaces++;
			if (!IS_RIP_OFF(ifp->int_state))
				rip_interfaces++;

			/* turn on router discovery and RIP If needed */
			if_ok_rdisc(ifp);
			rip_on(ifp);
		}
	}

	/* If we are multi-homed and have at least two interfaces
	 * listening to RIP, then output by default.
	 */
	if (!supplier_set && rip_interfaces > 1)
		set_supplier();

	/* If we are multi-homed, optionally advertise a route to
	 * our main address.
	 */
	if (advertise_mhome
	    || (tot_interfaces > 1
		&& mhome
		&& (ifp = ifwithaddr(myaddr, 0, 0)) != 0
		&& foundloopback)) {
		advertise_mhome = 1;
		rt = rtget(myaddr, HOST_MASK);
		if (rt != 0) {
			if (rt->rt_ifp != ifp
			    || rt->rt_router != loopaddr) {
				rtdelete(rt);
				rt = 0;
			} else {
				loop_rts.rts_ifp = ifp;
				loop_rts.rts_metric = 0;
				loop_rts.rts_time = rt->rt_time;
				rtchange(rt, rt->rt_state | RS_MHOME,
					 &loop_rts, 0);
			}
		}
		if (rt == 0) {
			loop_rts.rts_ifp = ifp;
			loop_rts.rts_metric = 0;
			rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
		}
	}

	LIST_FOREACH_SAFE(ifp, &ifnet, int_list, ifp1) {
		/* Forget any interfaces that have disappeared.
		 */
		if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
			trace_act("interface %s has disappeared",
				  ifp->int_name);
			ifdel(ifp);
			continue;
		}

		if ((ifp->int_state & IS_BROKE)
		    && !(ifp->int_state & IS_PASSIVE))
			LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);

		/* If we ever have a RIPv1 interface, assume we always will.
		 * It might come back if it ever goes away.
		 */
		if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
			have_ripv1_out = 1;
		if (!(ifp->int_state & IS_NO_RIPV1_IN))
			have_ripv1_in = 1;
	}

	LIST_FOREACH(ifp, &ifnet, int_list) {
		/* Ensure there is always a network route for interfaces,
		 * after any dead interfaces have been deleted, which
		 * might affect routes for point-to-point links.
		 */
		if (!addrouteforif(ifp))
			continue;

		/* Add routes to the local end of point-to-point interfaces
		 * using loopback.
		 */
		if ((ifp->int_if_flags & IFF_POINTOPOINT)
		    && !(ifp->int_state & IS_REMOTE)
		    && foundloopback) {
			/* Delete any routes to the network address through
			 * foreign routers. Remove even static routes.
			 */
			del_static(ifp->int_addr, HOST_MASK, 0, 0);
			rt = rtget(ifp->int_addr, HOST_MASK);
			if (rt != 0 && rt->rt_router != loopaddr) {
				rtdelete(rt);
				rt = 0;
			}
			if (rt != 0) {
				if (!(rt->rt_state & RS_LOCAL)
				    || rt->rt_metric > ifp->int_metric) {
					ifp1 = ifp;
				} else {
					ifp1 = rt->rt_ifp;
				}
				loop_rts.rts_ifp = ifp1;
				loop_rts.rts_metric = 0;
				loop_rts.rts_time = rt->rt_time;
				rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
					      | (RS_IF|RS_LOCAL)),
					 &loop_rts, 0);
			} else {
				loop_rts.rts_ifp = ifp;
				loop_rts.rts_metric = 0;
				rtadd(ifp->int_addr, HOST_MASK,
				      (RS_IF | RS_LOCAL), &loop_rts);
			}
		}
	}

	/* add the authority routes */
	for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
		rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
		if (rt != 0
		    && !(rt->rt_state & RS_NO_NET_SYN)
		    && !(rt->rt_state & RS_NET_INT)) {
			rtdelete(rt);
			rt = 0;
		}
		if (rt == 0) {
			loop_rts.rts_ifp = 0;
			loop_rts.rts_metric = intnetp->intnet_metric-1;
			rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
			      RS_NET_SYN | RS_NET_INT, &loop_rts);
		}
	}

	prev_complaints = complaints;
}


static void
check_net_syn(struct interface *ifp)
{
	struct rt_entry *rt;
	static struct rt_spare new;


	/* Turn on the need to automatically synthesize a network route
	 * for this interface only if we are running RIPv1 on some other
	 * interface that is on a different class-A,B,or C network.
	 */
	if (have_ripv1_out || have_ripv1_in) {
		ifp->int_state |= IS_NEED_NET_SYN;
		rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
		if (rt != 0
		    && 0 == (rt->rt_state & RS_NO_NET_SYN)
		    && (!(rt->rt_state & RS_NET_SYN)
			|| rt->rt_metric > ifp->int_metric)) {
			rtdelete(rt);
			rt = 0;
		}
		if (rt == 0) {
			new.rts_ifp = ifp;
			new.rts_gate = ifp->int_addr;
			new.rts_router = ifp->int_addr;
			new.rts_metric = ifp->int_metric;
			rtadd(ifp->int_std_addr, ifp->int_std_mask,
			      RS_NET_SYN, &new);
		}

	} else {
		ifp->int_state &= ~IS_NEED_NET_SYN;

		rt = rtget(ifp->int_std_addr,
			   ifp->int_std_mask);
		if (rt != 0
		    && (rt->rt_state & RS_NET_SYN)
		    && rt->rt_ifp == ifp)
			rtbad_sub(rt);
	}
}


/* Add route for interface if not currently installed.
 * Create route to other end if a point-to-point link,
 * otherwise a route to this (sub)network.
 */
static int					/* 0=bad interface */
addrouteforif(struct interface *ifp)
{
	struct rt_entry *rt;
	static struct rt_spare new;
	naddr dst;


	/* skip sick interfaces
	 */
	if (ifp->int_state & IS_BROKE)
		return 0;

	/* If the interface on a subnet, then install a RIPv1 route to
	 * the network as well (unless it is sick).
	 */
	if (ifp->int_state & IS_SUBNET)
		check_net_syn(ifp);

	dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
	       ? ifp->int_dstaddr
	       : htonl(ifp->int_net));

	new.rts_ifp = ifp;
	new.rts_router = ifp->int_addr;
	new.rts_gate = ifp->int_addr;
	new.rts_metric = ifp->int_metric;
	new.rts_time = now.tv_sec;

	/* If we are going to send packets to the gateway,
	 * it must be reachable using our physical interfaces
	 */
	if ((ifp->int_state & IS_REMOTE)
	    && !(ifp->int_state & IS_EXTERNAL)
	    && !check_remote(ifp))
		return 0;

	/* We are finished if the correct main interface route exists.
	 * The right route must be for the right interface, not synthesized
	 * from a subnet, be a "gateway" or not as appropriate, and so forth.
	 */
	del_static(dst, ifp->int_mask, 0, 0);
	rt = rtget(dst, ifp->int_mask);
	if (rt != 0) {
		if ((rt->rt_ifp != ifp
		     || rt->rt_router != ifp->int_addr)
		    && (!(ifp->int_state & IS_DUP)
			|| rt->rt_ifp == 0
			|| (rt->rt_ifp->int_state & IS_BROKE))) {
			rtdelete(rt);
			rt = 0;
		} else {
			rtchange(rt, ((rt->rt_state | RS_IF)
				      & ~(RS_NET_SYN | RS_LOCAL)),
				 &new, 0);
		}
	}
	if (rt == 0) {
		if (ifp->int_transitions++ > 0)
			trace_act("re-install interface %s",
				  ifp->int_name);

		rtadd(dst, ifp->int_mask, RS_IF, &new);
	}

	return 1;
}
OpenPOWER on IntegriCloud