if.c revision 1.19 1 /* $NetBSD: if.c,v 1.19 2000/03/02 20:57:42 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgment:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
37 static char sccsid[] __attribute__((unused)) = "@(#)if.c 8.1 (Berkeley) 6/5/93";
38 #elif defined(__NetBSD__)
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: if.c,v 1.19 2000/03/02 20:57:42 christos Exp $");
41 #endif
42
43 #include "defs.h"
44 #include "pathnames.h"
45
46 struct interface *ifnet; /* all interfaces */
47
48 /* hash table for all interfaces, big enough to tolerate ridiculous
49 * numbers of IP aliases. Crazy numbers of aliases such as 7000
50 * still will not do well, but not just in looking up interfaces
51 * by name or address.
52 */
53 #define AHASH_LEN 211 /* must be prime */
54 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
55 struct interface *ahash_tbl[AHASH_LEN];
56
57 #define BHASH_LEN 211 /* must be prime */
58 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
59 struct interface *bhash_tbl[BHASH_LEN];
60
61 struct interface *remote_if; /* remote interfaces */
62
63 /* hash for physical interface names.
64 * Assume there are never more 100 or 200 real interfaces, and that
65 * aliases are put on the end of the hash chains.
66 */
67 #define NHASH_LEN 97
68 struct interface *nhash_tbl[NHASH_LEN];
69
70 int tot_interfaces; /* # of remote and local interfaces */
71 int rip_interfaces; /* # of interfaces doing RIP */
72 int foundloopback; /* valid flag for loopaddr */
73 naddr loopaddr; /* our address on loopback */
74 struct rt_spare loop_rts;
75
76 struct timeval ifinit_timer;
77 static struct timeval last_ifinit;
78 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
79 && last_ifinit.tv_usec == now.tv_usec \
80 && timercmp(&ifinit_timer, &now, >))
81
82 int have_ripv1_out; /* have a RIPv1 interface */
83 int have_ripv1_in;
84
85
86 static struct interface**
87 nhash(char *p)
88 {
89 u_int i;
90
91 for (i = 0; *p != '\0'; p++) {
92 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
93 i ^= *p;
94 }
95 return &nhash_tbl[i % NHASH_LEN];
96 }
97
98
99 /* Link a new interface into the lists and hash tables.
100 */
101 void
102 if_link(struct interface *ifp)
103 {
104 struct interface **hifp;
105
106 ifp->int_prev = &ifnet;
107 ifp->int_next = ifnet;
108 if (ifnet != 0)
109 ifnet->int_prev = &ifp->int_next;
110 ifnet = ifp;
111
112 hifp = AHASH(ifp->int_addr);
113 ifp->int_ahash_prev = hifp;
114 if ((ifp->int_ahash = *hifp) != 0)
115 (*hifp)->int_ahash_prev = &ifp->int_ahash;
116 *hifp = ifp;
117
118 if (ifp->int_if_flags & IFF_BROADCAST) {
119 hifp = BHASH(ifp->int_brdaddr);
120 ifp->int_bhash_prev = hifp;
121 if ((ifp->int_bhash = *hifp) != 0)
122 (*hifp)->int_bhash_prev = &ifp->int_bhash;
123 *hifp = ifp;
124 }
125
126 if (ifp->int_state & IS_REMOTE) {
127 ifp->int_rlink_prev = &remote_if;
128 ifp->int_rlink = remote_if;
129 if (remote_if != 0)
130 remote_if->int_rlink_prev = &ifp->int_rlink;
131 remote_if = ifp;
132 }
133
134 hifp = nhash(ifp->int_name);
135 if (ifp->int_state & IS_ALIAS) {
136 /* put aliases on the end of the hash chain */
137 while (*hifp != 0)
138 hifp = &(*hifp)->int_nhash;
139 }
140 ifp->int_nhash_prev = hifp;
141 if ((ifp->int_nhash = *hifp) != 0)
142 (*hifp)->int_nhash_prev = &ifp->int_nhash;
143 *hifp = ifp;
144 }
145
146
147 /* Find the interface with an address
148 */
149 struct interface *
150 ifwithaddr(naddr addr,
151 int bcast, /* notice IFF_BROADCAST address */
152 int remote) /* include IS_REMOTE interfaces */
153 {
154 struct interface *ifp, *possible = 0;
155
156 remote = (remote == 0) ? IS_REMOTE : 0;
157
158 for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
159 if (ifp->int_addr != addr)
160 continue;
161 if ((ifp->int_state & remote) != 0)
162 continue;
163 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
164 return ifp;
165 possible = ifp;
166 }
167
168 if (possible || !bcast)
169 return possible;
170
171 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
172 if (ifp->int_brdaddr != addr)
173 continue;
174 if ((ifp->int_state & remote) != 0)
175 continue;
176 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
177 return ifp;
178 possible = ifp;
179 }
180
181 return possible;
182 }
183
184
185 /* find the interface with a name
186 */
187 struct interface *
188 ifwithname(char *name, /* "ec0" or whatever */
189 naddr addr) /* 0 or network address */
190 {
191 struct interface *ifp;
192
193 for (;;) {
194 for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
195 /* If the network address is not specified,
196 * ignore any alias interfaces. Otherwise, look
197 * for the interface with the target name and address.
198 */
199 if (!strcmp(ifp->int_name, name)
200 && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
201 || (ifp->int_addr == addr)))
202 return ifp;
203 }
204
205 /* If there is no known interface, maybe there is a
206 * new interface. So just once look for new interfaces.
207 */
208 if (IF_RESCAN_DELAY())
209 return 0;
210 ifinit();
211 }
212 }
213
214
215 struct interface *
216 ifwithindex(u_short index,
217 int rescan_ok)
218 {
219 struct interface *ifp;
220
221 for (;;) {
222 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
223 if (ifp->int_index == index)
224 return ifp;
225 }
226
227 /* If there is no known interface, maybe there is a
228 * new interface. So just once look for new interfaces.
229 */
230 if (!rescan_ok
231 || IF_RESCAN_DELAY())
232 return 0;
233 ifinit();
234 }
235 }
236
237
238 /* Find an interface from which the specified address
239 * should have come from. Used for figuring out which
240 * interface a packet came in on.
241 */
242 struct interface *
243 iflookup(naddr addr)
244 {
245 struct interface *ifp, *maybe;
246
247 maybe = 0;
248 for (;;) {
249 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
250 if (ifp->int_if_flags & IFF_POINTOPOINT) {
251 /* finished with a match */
252 if (ifp->int_dstaddr == addr)
253 return ifp;
254
255 } else {
256 /* finished with an exact match */
257 if (ifp->int_addr == addr)
258 return ifp;
259
260 /* Look for the longest approximate match.
261 */
262 if (on_net(addr, ifp->int_net, ifp->int_mask)
263 && (maybe == 0
264 || ifp->int_mask > maybe->int_mask))
265 maybe = ifp;
266 }
267 }
268
269 if (maybe != 0)
270 return maybe;
271
272 /* If there is no known interface, maybe there is a
273 * new interface. So just once look for new interfaces.
274 */
275 ifinit();
276 }
277 }
278
279
280 /* Return the classical netmask for an IP address.
281 */
282 naddr /* host byte order */
283 std_mask(naddr addr) /* network byte order */
284 {
285 NTOHL(addr); /* was a host, not a network */
286
287 if (addr == 0) /* default route has mask 0 */
288 return 0;
289 if (IN_CLASSA(addr))
290 return IN_CLASSA_NET;
291 if (IN_CLASSB(addr))
292 return IN_CLASSB_NET;
293 return IN_CLASSC_NET;
294 }
295
296
297 /* Find the netmask that would be inferred by RIPv1 listeners
298 * on the given interface for a given network.
299 * If no interface is specified, look for the best fitting interface.
300 */
301 naddr
302 ripv1_mask_net(naddr addr, /* in network byte order */
303 struct interface *ifp) /* as seen on this interface */
304 {
305 struct r1net *r1p;
306 naddr mask = 0;
307
308 if (addr == 0) /* default always has 0 mask */
309 return mask;
310
311 if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
312 /* If the target network is that of the associated interface
313 * on which it arrived, then use the netmask of the interface.
314 */
315 if (on_net(addr, ifp->int_net, ifp->int_std_mask))
316 mask = ifp->int_ripv1_mask;
317
318 } else {
319 /* Examine all interfaces, and if it the target seems
320 * to have the same network number of an interface, use the
321 * netmask of that interface. If there is more than one
322 * such interface, prefer the interface with the longest
323 * match.
324 */
325 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
326 if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
327 && ifp->int_ripv1_mask > mask
328 && ifp->int_ripv1_mask != HOST_MASK)
329 mask = ifp->int_ripv1_mask;
330 }
331
332 }
333
334 /* check special definitions */
335 if (mask == 0) {
336 for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
337 if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
338 && r1p->r1net_mask > mask)
339 mask = r1p->r1net_mask;
340 }
341
342 /* Otherwise, make the classic A/B/C guess.
343 */
344 if (mask == 0)
345 mask = std_mask(addr);
346 }
347
348 return mask;
349 }
350
351
352 naddr
353 ripv1_mask_host(naddr addr, /* in network byte order */
354 struct interface *ifp) /* as seen on this interface */
355 {
356 naddr mask = ripv1_mask_net(addr, ifp);
357
358
359 /* If the computed netmask does not mask the address,
360 * then assume it is a host address
361 */
362 if ((ntohl(addr) & ~mask) != 0)
363 mask = HOST_MASK;
364 return mask;
365 }
366
367
368 /* See if a IP address looks reasonable as a destination
369 */
370 int /* 0=bad */
371 check_dst(naddr addr)
372 {
373 NTOHL(addr);
374
375 if (IN_CLASSA(addr)) {
376 if (addr == 0)
377 return 1; /* default */
378
379 addr >>= IN_CLASSA_NSHIFT;
380 return (addr != 0 && addr != IN_LOOPBACKNET);
381 }
382
383 return (IN_CLASSB(addr) || IN_CLASSC(addr));
384 }
385
386
387 /* See a new interface duplicates an existing interface.
388 */
389 struct interface *
390 check_dup(naddr addr, /* IP address, so network byte order */
391 naddr dstaddr, /* ditto */
392 naddr mask, /* mask, so host byte order */
393 int if_flags)
394 {
395 struct interface *ifp;
396
397 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
398 if (ifp->int_mask != mask)
399 continue;
400
401 if (!iff_up(ifp->int_if_flags))
402 continue;
403
404 /* The local address can only be shared with a point-to-point
405 * link.
406 */
407 if (ifp->int_addr == addr
408 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
409 return ifp;
410
411 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
412 return ifp;
413 }
414 return 0;
415 }
416
417
418 /* See that a remote gateway is reachable.
419 * Note that the answer can change as real interfaces come and go.
420 */
421 int /* 0=bad */
422 check_remote(struct interface *ifp)
423 {
424 struct rt_entry *rt;
425
426 /* do not worry about other kinds */
427 if (!(ifp->int_state & IS_REMOTE))
428 return 1;
429
430 rt = rtfind(ifp->int_addr);
431 if (rt != 0
432 && rt->rt_ifp != 0
433 &&on_net(ifp->int_addr,
434 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
435 return 1;
436
437 /* the gateway cannot be reached directly from one of our
438 * interfaces
439 */
440 if (!(ifp->int_state & IS_BROKE)) {
441 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
442 naddr_ntoa(ifp->int_addr));
443 if_bad(ifp);
444 }
445 return 0;
446 }
447
448
449 /* Delete an interface.
450 */
451 static void
452 ifdel(struct interface *ifp)
453 {
454 struct ip_mreq m;
455 struct interface *ifp1;
456
457
458 trace_if("Del", ifp);
459
460 ifp->int_state |= IS_BROKE;
461
462 /* unlink the interface
463 */
464 *ifp->int_prev = ifp->int_next;
465 if (ifp->int_next != 0)
466 ifp->int_next->int_prev = ifp->int_prev;
467 *ifp->int_ahash_prev = ifp->int_ahash;
468 if (ifp->int_ahash != 0)
469 ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
470 *ifp->int_nhash_prev = ifp->int_nhash;
471 if (ifp->int_nhash != 0)
472 ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
473 if (ifp->int_if_flags & IFF_BROADCAST) {
474 *ifp->int_bhash_prev = ifp->int_bhash;
475 if (ifp->int_bhash != 0)
476 ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
477 }
478 if (ifp->int_state & IS_REMOTE) {
479 *ifp->int_rlink_prev = ifp->int_rlink;
480 if (ifp->int_rlink != 0)
481 ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
482 }
483
484 if (!(ifp->int_state & IS_ALIAS)) {
485 /* delete aliases when the main interface dies
486 */
487 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
488 if (ifp1 != ifp
489 && !strcmp(ifp->int_name, ifp1->int_name))
490 ifdel(ifp1);
491 }
492
493 if ((ifp->int_if_flags & IFF_MULTICAST)
494 #ifdef MCAST_PPP_BUG
495 && !(ifp->int_if_flags & IFF_POINTOPOINT)
496 #endif
497 && rip_sock >= 0) {
498 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
499 m.imr_interface.s_addr = ((ifp->int_if_flags
500 & IFF_POINTOPOINT)
501 ? ifp->int_dstaddr
502 : ifp->int_addr);
503 if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP,
504 &m, sizeof(m)) < 0
505 && errno != EADDRNOTAVAIL
506 && !TRACEACTIONS)
507 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
508 if (rip_sock_mcast == ifp)
509 rip_sock_mcast = 0;
510 }
511 if (ifp->int_rip_sock >= 0) {
512 (void)close(ifp->int_rip_sock);
513 ifp->int_rip_sock = -1;
514 fix_select();
515 }
516
517 tot_interfaces--;
518 if (!IS_RIP_OFF(ifp->int_state))
519 rip_interfaces--;
520
521 /* Zap all routes associated with this interface.
522 * Assume routes just using gateways beyond this interface
523 * will timeout naturally, and have probably already died.
524 */
525 (void)rn_walktree(rhead, walk_bad, 0);
526
527 set_rdisc_mg(ifp, 0);
528 if_bad_rdisc(ifp);
529 }
530
531 free(ifp);
532 }
533
534
535 /* Mark an interface ill.
536 */
537 void
538 if_sick(struct interface *ifp)
539 {
540 if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
541 ifp->int_state |= IS_SICK;
542 ifp->int_act_time = NEVER;
543 trace_if("Chg", ifp);
544
545 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
546 }
547 }
548
549
550 /* Mark an interface dead.
551 */
552 void
553 if_bad(struct interface *ifp)
554 {
555 struct interface *ifp1;
556
557
558 if (ifp->int_state & IS_BROKE)
559 return;
560
561 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
562
563 ifp->int_state |= (IS_BROKE | IS_SICK);
564 ifp->int_act_time = NEVER;
565 ifp->int_query_time = NEVER;
566 ifp->int_data.ts = now.tv_sec;
567
568 trace_if("Chg", ifp);
569
570 if (!(ifp->int_state & IS_ALIAS)) {
571 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
572 if (ifp1 != ifp
573 && !strcmp(ifp->int_name, ifp1->int_name))
574 if_bad(ifp1);
575 }
576 (void)rn_walktree(rhead, walk_bad, 0);
577 if_bad_rdisc(ifp);
578 }
579 }
580
581
582 /* Mark an interface alive
583 */
584 int /* 1=it was dead */
585 if_ok(struct interface *ifp,
586 const char *type)
587 {
588 struct interface *ifp1;
589
590
591 if (!(ifp->int_state & IS_BROKE)) {
592 if (ifp->int_state & IS_SICK) {
593 trace_act("%sinterface %s to %s working better",
594 type,
595 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
596 ifp->int_state &= ~IS_SICK;
597 }
598 return 0;
599 }
600
601 msglog("%sinterface %s to %s restored",
602 type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
603 ifp->int_state &= ~(IS_BROKE | IS_SICK);
604 ifp->int_data.ts = 0;
605
606 if (!(ifp->int_state & IS_ALIAS)) {
607 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
608 if (ifp1 != ifp
609 && !strcmp(ifp->int_name, ifp1->int_name))
610 if_ok(ifp1, type);
611 }
612 if_ok_rdisc(ifp);
613 }
614
615 if (ifp->int_state & IS_REMOTE) {
616 if (!addrouteforif(ifp))
617 return 0;
618 }
619 return 1;
620 }
621
622
623 /* disassemble routing message
624 */
625 void
626 rt_xaddrs(struct rt_addrinfo *info,
627 struct sockaddr *sa,
628 struct sockaddr *lim,
629 int addrs)
630 {
631 int i;
632 #ifdef _HAVE_SA_LEN
633 static struct sockaddr sa_zero;
634 #endif
635 #ifdef sgi
636 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
637 : sizeof(__uint64_t))
638 #else
639 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
640 : sizeof(long))
641 #endif
642
643
644 memset(info, 0, sizeof(*info));
645 info->rti_addrs = addrs;
646 for (i = 0; i < RTAX_MAX && sa < lim; i++) {
647 if ((addrs & (1 << i)) == 0)
648 continue;
649 #ifdef _HAVE_SA_LEN
650 info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
651 sa = (struct sockaddr *)((char*)(sa)
652 + ROUNDUP(sa->sa_len));
653 #else
654 info->rti_info[i] = sa;
655 sa = (struct sockaddr *)((char*)(sa)
656 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
657 #endif
658 }
659 }
660
661
662 /* Find the network interfaces which have configured themselves.
663 * This must be done regularly, if only for extra addresses
664 * that come and go on interfaces.
665 */
666 void
667 ifinit(void)
668 {
669 static char *sysctl_buf;
670 static size_t sysctl_buf_size = 0;
671 uint complaints = 0;
672 static u_int prev_complaints = 0;
673 # define COMP_NOT_INET 0x001
674 # define COMP_NOADDR 0x002
675 # define COMP_BADADDR 0x004
676 # define COMP_NODST 0x008
677 # define COMP_NOBADR 0x010
678 # define COMP_NOMASK 0x020
679 # define COMP_DUP 0x040
680 # define COMP_BAD_METRIC 0x080
681 # define COMP_NETMASK 0x100
682
683 struct interface ifs, ifs0, *ifp, *ifp1;
684 struct rt_entry *rt;
685 size_t needed;
686 int mib[6];
687 struct if_msghdr *ifm;
688 struct ifa_msghdr *ifam, *ifam_lim, *ifam2;
689 int in, ierr, out, oerr;
690 struct intnet *intnetp;
691 struct rt_addrinfo info;
692 #ifdef SIOCGIFMETRIC
693 struct ifreq ifr;
694 #endif
695
696
697 last_ifinit = now;
698 ifinit_timer.tv_sec = now.tv_sec + (supplier
699 ? CHECK_ACT_INTERVAL
700 : CHECK_QUIET_INTERVAL);
701
702 /* mark all interfaces so we can get rid of those that disappear */
703 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next)
704 ifp->int_state &= ~(IS_CHECKED | IS_DUP);
705
706 /* Fetch the interface list, without too many system calls
707 * since we do it repeatedly.
708 */
709 mib[0] = CTL_NET;
710 mib[1] = PF_ROUTE;
711 mib[2] = 0;
712 mib[3] = AF_INET;
713 mib[4] = NET_RT_IFLIST;
714 mib[5] = 0;
715 for (;;) {
716 if ((needed = sysctl_buf_size) != 0) {
717 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
718 break;
719 /* retry if the table grew */
720 if (errno != ENOMEM && errno != EFAULT)
721 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
722 free(sysctl_buf);
723 needed = 0;
724 }
725 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
726 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
727 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
728 "ifinit sysctl");
729 }
730
731 ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
732 for (ifam = (struct ifa_msghdr *)sysctl_buf;
733 ifam < ifam_lim;
734 ifam = ifam2) {
735
736 ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
737
738 #ifdef RTM_OIFINFO
739 if (ifam->ifam_type == RTM_OIFINFO) {
740 continue; /* just ignore compat message */
741 }
742 #endif
743 if (ifam->ifam_type == RTM_IFINFO) {
744 struct sockaddr_dl *sdl;
745
746 ifm = (struct if_msghdr *)ifam;
747 /* make prototype structure for the IP aliases
748 */
749 memset(&ifs0, 0, sizeof(ifs0));
750 ifs0.int_rip_sock = -1;
751 ifs0.int_index = ifm->ifm_index;
752 ifs0.int_if_flags = ifm->ifm_flags;
753 ifs0.int_state = IS_CHECKED;
754 ifs0.int_query_time = NEVER;
755 ifs0.int_act_time = now.tv_sec;
756 ifs0.int_data.ts = now.tv_sec;
757 ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
758 ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
759 ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
760 ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
761 #ifdef sgi
762 ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
763 #endif
764 sdl = (struct sockaddr_dl *)(ifm + 1);
765 sdl->sdl_data[sdl->sdl_nlen] = 0;
766 strncpy(ifs0.int_name, sdl->sdl_data,
767 MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
768 continue;
769 }
770 if (ifam->ifam_type != RTM_NEWADDR) {
771 logbad(1,"ifinit: out of sync");
772 continue;
773 }
774 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
775 (struct sockaddr *)ifam2,
776 ifam->ifam_addrs);
777
778 /* Prepare for the next address of this interface, which
779 * will be an alias.
780 * Do not output RIP or Router-Discovery packets via aliases.
781 */
782 memcpy(&ifs, &ifs0, sizeof(ifs));
783 ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
784
785 if (INFO_IFA(&info) == 0) {
786 if (iff_up(ifs.int_if_flags)) {
787 if (!(prev_complaints & COMP_NOADDR))
788 msglog("%s has no address",
789 ifs.int_name);
790 complaints |= COMP_NOADDR;
791 }
792 continue;
793 }
794 if (INFO_IFA(&info)->sa_family != AF_INET) {
795 if (iff_up(ifs.int_if_flags)) {
796 if (!(prev_complaints & COMP_NOT_INET))
797 trace_act("%s: not AF_INET",
798 ifs.int_name);
799 complaints |= COMP_NOT_INET;
800 }
801 continue;
802 }
803
804 ifs.int_addr = S_ADDR(INFO_IFA(&info));
805
806 if (ntohl(ifs.int_addr)>>24 == 0
807 || ntohl(ifs.int_addr)>>24 == 0xff) {
808 if (iff_up(ifs.int_if_flags)) {
809 if (!(prev_complaints & COMP_BADADDR))
810 msglog("%s has a bad address",
811 ifs.int_name);
812 complaints |= COMP_BADADDR;
813 }
814 continue;
815 }
816
817 if (ifs.int_if_flags & IFF_LOOPBACK) {
818 ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
819 ifs.int_dstaddr = ifs.int_addr;
820 ifs.int_mask = HOST_MASK;
821 ifs.int_ripv1_mask = HOST_MASK;
822 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
823 ifs.int_net = ntohl(ifs.int_dstaddr);
824 if (!foundloopback) {
825 foundloopback = 1;
826 loopaddr = ifs.int_addr;
827 loop_rts.rts_gate = loopaddr;
828 loop_rts.rts_router = loopaddr;
829 }
830
831 } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
832 if (INFO_BRD(&info) == 0
833 || INFO_BRD(&info)->sa_family != AF_INET) {
834 if (iff_up(ifs.int_if_flags)) {
835 if (!(prev_complaints & COMP_NODST))
836 msglog("%s has a bad"
837 " destination address",
838 ifs.int_name);
839 complaints |= COMP_NODST;
840 }
841 continue;
842 }
843 ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
844 if (ntohl(ifs.int_dstaddr)>>24 == 0
845 || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
846 if (iff_up(ifs.int_if_flags)) {
847 if (!(prev_complaints & COMP_NODST))
848 msglog("%s has a bad"
849 " destination address",
850 ifs.int_name);
851 complaints |= COMP_NODST;
852 }
853 continue;
854 }
855 ifs.int_mask = HOST_MASK;
856 ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
857 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
858 ifs.int_net = ntohl(ifs.int_dstaddr);
859
860 } else {
861 if (INFO_MASK(&info) == 0) {
862 if (iff_up(ifs.int_if_flags)) {
863 if (!(prev_complaints & COMP_NOMASK))
864 msglog("%s has no netmask",
865 ifs.int_name);
866 complaints |= COMP_NOMASK;
867 }
868 continue;
869 }
870 ifs.int_dstaddr = ifs.int_addr;
871 ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
872 ifs.int_ripv1_mask = ifs.int_mask;
873 ifs.int_std_mask = std_mask(ifs.int_addr);
874 ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
875 if (ifs.int_mask != ifs.int_std_mask)
876 ifs.int_state |= IS_SUBNET;
877
878 if (ifs.int_if_flags & IFF_BROADCAST) {
879 if (INFO_BRD(&info) == 0) {
880 if (iff_up(ifs.int_if_flags)) {
881 if (!(prev_complaints
882 & COMP_NOBADR))
883 msglog("%s has"
884 "no broadcast address",
885 ifs.int_name);
886 complaints |= COMP_NOBADR;
887 }
888 continue;
889 }
890 ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
891 }
892 }
893 ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
894 ifs.int_std_addr = htonl(ifs.int_std_net);
895
896 /* Use a minimum metric of one. Treat the interface metric
897 * (default 0) as an increment to the hop count of one.
898 *
899 * The metric obtained from the routing socket dump of
900 * interface addresses is wrong. It is not set by the
901 * SIOCSIFMETRIC ioctl.
902 */
903 #ifdef SIOCGIFMETRIC
904 strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
905 if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
906 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
907 ifs.int_metric = 0;
908 } else {
909 ifs.int_metric = ifr.ifr_metric;
910 }
911 #else
912 ifs.int_metric = ifam->ifam_metric;
913 #endif
914 if (ifs.int_metric > HOPCNT_INFINITY) {
915 ifs.int_metric = 0;
916 if (!(prev_complaints & COMP_BAD_METRIC)
917 && iff_up(ifs.int_if_flags)) {
918 complaints |= COMP_BAD_METRIC;
919 msglog("%s has a metric of %d",
920 ifs.int_name, ifs.int_metric);
921 }
922 }
923
924 /* See if this is a familiar interface.
925 * If so, stop worrying about it if it is the same.
926 * Start it over if it now is to somewhere else, as happens
927 * frequently with PPP and SLIP.
928 */
929 ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
930 ? ifs.int_addr
931 : 0));
932 if (ifp != 0) {
933 ifp->int_state |= IS_CHECKED;
934
935 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
936 & (IFF_BROADCAST
937 | IFF_LOOPBACK
938 | IFF_POINTOPOINT
939 | IFF_MULTICAST))
940 || 0 != ((ifp->int_state ^ ifs.int_state)
941 & IS_ALIAS)
942 || ifp->int_addr != ifs.int_addr
943 || ifp->int_brdaddr != ifs.int_brdaddr
944 || ifp->int_dstaddr != ifs.int_dstaddr
945 || ifp->int_mask != ifs.int_mask
946 || ifp->int_metric != ifs.int_metric) {
947 /* Forget old information about
948 * a changed interface.
949 */
950 trace_act("interface %s has changed",
951 ifp->int_name);
952 ifdel(ifp);
953 ifp = 0;
954 }
955 }
956
957 if (ifp != 0) {
958 /* The primary representative of an alias worries
959 * about how things are working.
960 */
961 if (ifp->int_state & IS_ALIAS)
962 continue;
963
964 /* note interfaces that have been turned off
965 */
966 if (!iff_up(ifs.int_if_flags)) {
967 if (iff_up(ifp->int_if_flags)) {
968 msglog("interface %s to %s turned off",
969 ifp->int_name,
970 naddr_ntoa(ifp->int_dstaddr));
971 if_bad(ifp);
972 ifp->int_if_flags &= ~IFF_UP;
973 } else if (now.tv_sec>(ifp->int_data.ts
974 + CHECK_BAD_INTERVAL)) {
975 trace_act("interface %s has been off"
976 " %ld seconds; forget it",
977 ifp->int_name,
978 now.tv_sec-ifp->int_data.ts);
979 ifdel(ifp);
980 }
981 continue;
982 }
983 /* or that were off and are now ok */
984 if (!iff_up(ifp->int_if_flags)) {
985 ifp->int_if_flags |= IFF_UP;
986 (void)if_ok(ifp, "");
987 }
988
989 /* If it has been long enough,
990 * see if the interface is broken.
991 */
992 if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
993 continue;
994
995 in = ifs.int_data.ipackets - ifp->int_data.ipackets;
996 ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
997 out = ifs.int_data.opackets - ifp->int_data.opackets;
998 oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
999 #ifdef sgi
1000 /* Through at least IRIX 6.2, PPP and SLIP
1001 * count packets dropped by the filters.
1002 * But FDDI rings stuck non-operational count
1003 * dropped packets as they wait for improvement.
1004 */
1005 if (!(ifp->int_if_flags & IFF_POINTOPOINT))
1006 oerr += (ifs.int_data.odrops
1007 - ifp->int_data.odrops);
1008 #endif
1009 /* If the interface just awoke, restart the counters.
1010 */
1011 if (ifp->int_data.ts == 0) {
1012 ifp->int_data = ifs.int_data;
1013 continue;
1014 }
1015 ifp->int_data = ifs.int_data;
1016
1017 /* Withhold judgment when the short error
1018 * counters wrap or the interface is reset.
1019 */
1020 if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1021 LIM_SEC(ifinit_timer,
1022 now.tv_sec+CHECK_BAD_INTERVAL);
1023 continue;
1024 }
1025
1026 /* Withhold judgement when there is no traffic
1027 */
1028 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1029 continue;
1030
1031 /* It is bad if input or output is not working.
1032 * Require presistent problems before marking it dead.
1033 */
1034 if ((in <= ierr && ierr > 0)
1035 || (out <= oerr && oerr > 0)) {
1036 if (!(ifp->int_state & IS_SICK)) {
1037 trace_act("interface %s to %s"
1038 " sick: in=%d ierr=%d"
1039 " out=%d oerr=%d",
1040 ifp->int_name,
1041 naddr_ntoa(ifp->int_dstaddr),
1042 in, ierr, out, oerr);
1043 if_sick(ifp);
1044 continue;
1045 }
1046 if (!(ifp->int_state & IS_BROKE)) {
1047 msglog("interface %s to %s broken:"
1048 " in=%d ierr=%d out=%d oerr=%d",
1049 ifp->int_name,
1050 naddr_ntoa(ifp->int_dstaddr),
1051 in, ierr, out, oerr);
1052 if_bad(ifp);
1053 }
1054 continue;
1055 }
1056
1057 /* otherwise, it is active and healthy
1058 */
1059 ifp->int_act_time = now.tv_sec;
1060 (void)if_ok(ifp, "");
1061 continue;
1062 }
1063
1064 /* This is a new interface.
1065 * If it is dead, forget it.
1066 */
1067 if (!iff_up(ifs.int_if_flags))
1068 continue;
1069
1070 /* If it duplicates an existing interface,
1071 * complain about it, mark the other one
1072 * duplicated, and forget this one.
1073 */
1074 ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1075 ifs.int_if_flags);
1076 if (ifp != 0) {
1077 /* Ignore duplicates of itself, caused by having
1078 * IP aliases on the same network.
1079 */
1080 if (!strcmp(ifp->int_name, ifs.int_name))
1081 continue;
1082
1083 if (!(prev_complaints & COMP_DUP)) {
1084 complaints |= COMP_DUP;
1085 msglog("%s (%s%s%s) is duplicated by"
1086 " %s (%s%s%s)",
1087 ifs.int_name,
1088 addrname(ifs.int_addr,ifs.int_mask,1),
1089 ((ifs.int_if_flags & IFF_POINTOPOINT)
1090 ? "-->" : ""),
1091 ((ifs.int_if_flags & IFF_POINTOPOINT)
1092 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1093 ifp->int_name,
1094 addrname(ifp->int_addr,ifp->int_mask,1),
1095 ((ifp->int_if_flags & IFF_POINTOPOINT)
1096 ? "-->" : ""),
1097 ((ifp->int_if_flags & IFF_POINTOPOINT)
1098 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1099 }
1100 ifp->int_state |= IS_DUP;
1101 continue;
1102 }
1103
1104 if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST))
1105 && !(ifs.int_state & IS_PASSIVE)) {
1106 trace_act("%s is neither broadcast, point-to-point,"
1107 " nor loopback",
1108 ifs.int_name);
1109 if (!(ifs.int_state & IFF_MULTICAST))
1110 ifs.int_state |= IS_NO_RDISC;
1111 }
1112
1113
1114 /* It is new and ok. Add it to the list of interfaces
1115 */
1116 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1117 memcpy(ifp, &ifs, sizeof(*ifp));
1118 get_parms(ifp);
1119 if_link(ifp);
1120 trace_if("Add", ifp);
1121
1122 /* Notice likely bad netmask.
1123 */
1124 if (!(prev_complaints & COMP_NETMASK)
1125 && !(ifp->int_if_flags & IFF_POINTOPOINT)
1126 && ifp->int_addr != RIP_DEFAULT) {
1127 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
1128 if (ifp1->int_mask == ifp->int_mask)
1129 continue;
1130 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1131 continue;
1132 if (ifp1->int_dstaddr == RIP_DEFAULT)
1133 continue;
1134 if (on_net(ifp->int_dstaddr,
1135 ifp1->int_net, ifp1->int_mask)
1136 || on_net(ifp1->int_dstaddr,
1137 ifp->int_net, ifp->int_mask)) {
1138 msglog("possible netmask problem"
1139 " between %s:%s and %s:%s",
1140 ifp->int_name,
1141 addrname(htonl(ifp->int_net),
1142 ifp->int_mask, 1),
1143 ifp1->int_name,
1144 addrname(htonl(ifp1->int_net),
1145 ifp1->int_mask, 1));
1146 complaints |= COMP_NETMASK;
1147 }
1148 }
1149 }
1150
1151 if (!(ifp->int_state & IS_ALIAS)) {
1152 /* Count the # of directly connected networks.
1153 */
1154 if (!(ifp->int_if_flags & IFF_LOOPBACK))
1155 tot_interfaces++;
1156 if (!IS_RIP_OFF(ifp->int_state))
1157 rip_interfaces++;
1158
1159 /* turn on router discovery and RIP If needed */
1160 if_ok_rdisc(ifp);
1161 rip_on(ifp);
1162 }
1163 }
1164
1165 /* If we are multi-homed and have at least two interfaces
1166 * listening to RIP, then output by default.
1167 */
1168 if (!supplier_set && rip_interfaces > 1)
1169 set_supplier();
1170
1171 /* If we are multi-homed, optionally advertise a route to
1172 * our main address.
1173 */
1174 if (advertise_mhome
1175 || (tot_interfaces > 1
1176 && mhome
1177 && (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1178 && foundloopback)) {
1179 advertise_mhome = 1;
1180 rt = rtget(myaddr, HOST_MASK);
1181 if (rt != 0) {
1182 if (rt->rt_ifp != ifp
1183 || rt->rt_router != loopaddr) {
1184 rtdelete(rt);
1185 rt = 0;
1186 } else {
1187 loop_rts.rts_ifp = ifp;
1188 loop_rts.rts_metric = 0;
1189 loop_rts.rts_time = rt->rt_time;
1190 rtchange(rt, rt->rt_state | RS_MHOME,
1191 &loop_rts, 0);
1192 }
1193 }
1194 if (rt == 0) {
1195 loop_rts.rts_ifp = ifp;
1196 loop_rts.rts_metric = 0;
1197 rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1198 }
1199 }
1200
1201 for (ifp = ifnet; ifp != 0; ifp = ifp1) {
1202 ifp1 = ifp->int_next; /* because we may delete it */
1203
1204 /* Forget any interfaces that have disappeared.
1205 */
1206 if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1207 trace_act("interface %s has disappeared",
1208 ifp->int_name);
1209 ifdel(ifp);
1210 continue;
1211 }
1212
1213 if ((ifp->int_state & IS_BROKE)
1214 && !(ifp->int_state & IS_PASSIVE))
1215 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1216
1217 /* If we ever have a RIPv1 interface, assume we always will.
1218 * It might come back if it ever goes away.
1219 */
1220 if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1221 have_ripv1_out = 1;
1222 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1223 have_ripv1_in = 1;
1224 }
1225
1226 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1227 /* Ensure there is always a network route for interfaces,
1228 * after any dead interfaces have been deleted, which
1229 * might affect routes for point-to-point links.
1230 */
1231 if (!addrouteforif(ifp))
1232 continue;
1233
1234 /* Add routes to the local end of point-to-point interfaces
1235 * using loopback.
1236 */
1237 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1238 && !(ifp->int_state & IS_REMOTE)
1239 && foundloopback) {
1240 /* Delete any routes to the network address through
1241 * foreign routers. Remove even static routes.
1242 */
1243 del_static(ifp->int_addr, HOST_MASK, 0, 0);
1244 rt = rtget(ifp->int_addr, HOST_MASK);
1245 if (rt != 0 && rt->rt_router != loopaddr) {
1246 rtdelete(rt);
1247 rt = 0;
1248 }
1249 if (rt != 0) {
1250 if (!(rt->rt_state & RS_LOCAL)
1251 || rt->rt_metric > ifp->int_metric) {
1252 ifp1 = ifp;
1253 } else {
1254 ifp1 = rt->rt_ifp;
1255 }
1256 loop_rts.rts_ifp = ifp1;
1257 loop_rts.rts_metric = 0;
1258 loop_rts.rts_time = rt->rt_time;
1259 rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1260 | (RS_IF|RS_LOCAL)),
1261 &loop_rts, 0);
1262 } else {
1263 loop_rts.rts_ifp = ifp;
1264 loop_rts.rts_metric = 0;
1265 rtadd(ifp->int_addr, HOST_MASK,
1266 (RS_IF | RS_LOCAL), &loop_rts);
1267 }
1268 }
1269 }
1270
1271 /* add the authority routes */
1272 for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1273 rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1274 if (rt != 0
1275 && !(rt->rt_state & RS_NO_NET_SYN)
1276 && !(rt->rt_state & RS_NET_INT)) {
1277 rtdelete(rt);
1278 rt = 0;
1279 }
1280 if (rt == 0) {
1281 loop_rts.rts_ifp = 0;
1282 loop_rts.rts_metric = intnetp->intnet_metric-1;
1283 rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1284 RS_NET_SYN | RS_NET_INT, &loop_rts);
1285 }
1286 }
1287
1288 prev_complaints = complaints;
1289 }
1290
1291
1292 static void
1293 check_net_syn(struct interface *ifp)
1294 {
1295 struct rt_entry *rt;
1296 static struct rt_spare new;
1297
1298
1299 /* Turn on the need to automatically synthesize a network route
1300 * for this interface only if we are running RIPv1 on some other
1301 * interface that is on a different class-A,B,or C network.
1302 */
1303 if (have_ripv1_out || have_ripv1_in) {
1304 ifp->int_state |= IS_NEED_NET_SYN;
1305 rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1306 if (rt != 0
1307 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1308 && (!(rt->rt_state & RS_NET_SYN)
1309 || rt->rt_metric > ifp->int_metric)) {
1310 rtdelete(rt);
1311 rt = 0;
1312 }
1313 if (rt == 0) {
1314 new.rts_ifp = ifp;
1315 new.rts_gate = ifp->int_addr;
1316 new.rts_router = ifp->int_addr;
1317 new.rts_metric = ifp->int_metric;
1318 rtadd(ifp->int_std_addr, ifp->int_std_mask,
1319 RS_NET_SYN, &new);
1320 }
1321
1322 } else {
1323 ifp->int_state &= ~IS_NEED_NET_SYN;
1324
1325 rt = rtget(ifp->int_std_addr,
1326 ifp->int_std_mask);
1327 if (rt != 0
1328 && (rt->rt_state & RS_NET_SYN)
1329 && rt->rt_ifp == ifp)
1330 rtbad_sub(rt);
1331 }
1332 }
1333
1334
1335 /* Add route for interface if not currently installed.
1336 * Create route to other end if a point-to-point link,
1337 * otherwise a route to this (sub)network.
1338 */
1339 int /* 0=bad interface */
1340 addrouteforif(struct interface *ifp)
1341 {
1342 struct rt_entry *rt;
1343 static struct rt_spare new;
1344 naddr dst;
1345
1346
1347 /* skip sick interfaces
1348 */
1349 if (ifp->int_state & IS_BROKE)
1350 return 0;
1351
1352 /* If the interface on a subnet, then install a RIPv1 route to
1353 * the network as well (unless it is sick).
1354 */
1355 if (ifp->int_state & IS_SUBNET)
1356 check_net_syn(ifp);
1357
1358 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1359 ? ifp->int_dstaddr
1360 : htonl(ifp->int_net));
1361
1362 new.rts_ifp = ifp;
1363 new.rts_router = ifp->int_addr;
1364 new.rts_gate = ifp->int_addr;
1365 new.rts_metric = ifp->int_metric;
1366 new.rts_time = now.tv_sec;
1367
1368 /* If we are going to send packets to the gateway,
1369 * it must be reachable using our physical interfaces
1370 */
1371 if ((ifp->int_state & IS_REMOTE)
1372 && !(ifp->int_state & IS_EXTERNAL)
1373 && !check_remote(ifp))
1374 return 0;
1375
1376 /* We are finished if the correct main interface route exists.
1377 * The right route must be for the right interface, not synthesized
1378 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1379 */
1380 del_static(dst, ifp->int_mask, 0, 0);
1381 rt = rtget(dst, ifp->int_mask);
1382 if (rt != 0) {
1383 if ((rt->rt_ifp != ifp
1384 || rt->rt_router != ifp->int_addr)
1385 && (!(ifp->int_state & IS_DUP)
1386 || rt->rt_ifp == 0
1387 || (rt->rt_ifp->int_state & IS_BROKE))) {
1388 rtdelete(rt);
1389 rt = 0;
1390 } else {
1391 rtchange(rt, ((rt->rt_state | RS_IF)
1392 & ~(RS_NET_SYN | RS_LOCAL)),
1393 &new, 0);
1394 }
1395 }
1396 if (rt == 0) {
1397 if (ifp->int_transitions++ > 0)
1398 trace_act("re-install interface %s",
1399 ifp->int_name);
1400
1401 rtadd(dst, ifp->int_mask, RS_IF, &new);
1402 }
1403
1404 return 1;
1405 }
1406