2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "pathnames.h"
41 struct ifhead ifnet = LIST_HEAD_INITIALIZER(ifnet); /* all interfaces */
42 struct ifhead remote_if = LIST_HEAD_INITIALIZER(remote_if); /* remote interfaces */
44 /* hash table for all interfaces, big enough to tolerate ridiculous
45 * numbers of IP aliases. Crazy numbers of aliases such as 7000
46 * still will not do well, but not just in looking up interfaces
49 #define AHASH_LEN 211 /* must be prime */
50 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
51 static struct interface *ahash_tbl[AHASH_LEN];
53 #define BHASH_LEN 211 /* must be prime */
54 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
55 static struct interface *bhash_tbl[BHASH_LEN];
58 /* hash for physical interface names.
59 * Assume there are never more 100 or 200 real interfaces, and that
60 * aliases are put on the end of the hash chains.
63 static struct interface *nhash_tbl[NHASH_LEN];
65 int tot_interfaces; /* # of remote and local interfaces */
66 int rip_interfaces; /* # of interfaces doing RIP */
67 static int foundloopback; /* valid flag for loopaddr */
68 naddr loopaddr; /* our address on loopback */
69 static struct rt_spare loop_rts;
71 struct timeval ifinit_timer;
72 static struct timeval last_ifinit;
73 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
74 && last_ifinit.tv_usec == now.tv_usec \
75 && timercmp(&ifinit_timer, &now, >))
77 int have_ripv1_out; /* have a RIPv1 interface */
78 static int have_ripv1_in;
81 static void if_bad(struct interface *);
82 static int addrouteforif(struct interface *);
84 static struct interface**
89 for (i = 0; *p != '\0'; p++) {
90 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
93 return &nhash_tbl[i % NHASH_LEN];
97 /* Link a new interface into the lists and hash tables.
100 if_link(struct interface *ifp)
102 struct interface **hifp;
104 LIST_INSERT_HEAD(&ifnet, ifp, int_list);
106 hifp = AHASH(ifp->int_addr);
107 ifp->int_ahash_prev = hifp;
108 if ((ifp->int_ahash = *hifp) != NULL)
109 (*hifp)->int_ahash_prev = &ifp->int_ahash;
112 if (ifp->int_if_flags & IFF_BROADCAST) {
113 hifp = BHASH(ifp->int_brdaddr);
114 ifp->int_bhash_prev = hifp;
115 if ((ifp->int_bhash = *hifp) != NULL)
116 (*hifp)->int_bhash_prev = &ifp->int_bhash;
120 if (ifp->int_state & IS_REMOTE)
121 LIST_INSERT_HEAD(&remote_if, ifp, remote_list);
123 hifp = nhash(ifp->int_name);
124 if (ifp->int_state & IS_ALIAS) {
125 /* put aliases on the end of the hash chain */
126 while (*hifp != NULL)
127 hifp = &(*hifp)->int_nhash;
129 ifp->int_nhash_prev = hifp;
130 if ((ifp->int_nhash = *hifp) != NULL)
131 (*hifp)->int_nhash_prev = &ifp->int_nhash;
136 /* Find the interface with an address
139 ifwithaddr(naddr addr,
140 int bcast, /* notice IFF_BROADCAST address */
141 int remote) /* include IS_REMOTE interfaces */
143 struct interface *ifp, *possible = NULL;
145 remote = (remote == 0) ? IS_REMOTE : 0;
147 for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
148 if (ifp->int_addr != addr)
150 if ((ifp->int_state & remote) != 0)
152 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
157 if (possible || !bcast)
160 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
161 if (ifp->int_brdaddr != addr)
163 if ((ifp->int_state & remote) != 0)
165 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
174 /* find the interface with a name
176 static struct interface *
177 ifwithname(char *name, /* "ec0" or whatever */
178 naddr addr) /* 0 or network address */
180 struct interface *ifp;
183 for (ifp = *nhash(name); ifp != NULL; ifp = ifp->int_nhash) {
184 /* If the network address is not specified,
185 * ignore any alias interfaces. Otherwise, look
186 * for the interface with the target name and address.
188 if (!strcmp(ifp->int_name, name)
189 && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
190 || (ifp->int_addr == addr)))
194 /* If there is no known interface, maybe there is a
195 * new interface. So just once look for new interfaces.
197 if (IF_RESCAN_DELAY())
205 ifwithindex(u_short ifindex,
208 struct interface *ifp;
211 LIST_FOREACH(ifp, &ifnet, int_list) {
212 if (ifp->int_index == ifindex)
216 /* If there is no known interface, maybe there is a
217 * new interface. So just once look for new interfaces.
220 || IF_RESCAN_DELAY())
227 /* Find an interface from which the specified address
228 * should have come from. Used for figuring out which
229 * interface a packet came in on.
234 struct interface *ifp, *maybe;
239 LIST_FOREACH(ifp, &ifnet, int_list) {
240 if (ifp->int_if_flags & IFF_POINTOPOINT) {
241 /* finished with a match */
242 if (ifp->int_dstaddr == addr)
246 /* finished with an exact match */
247 if (ifp->int_addr == addr)
250 /* Look for the longest approximate match.
252 if (on_net(addr, ifp->int_net, ifp->int_mask)
254 || ifp->int_mask > maybe->int_mask))
259 if (maybe != NULL || once || IF_RESCAN_DELAY())
263 /* If there is no known interface, maybe there is a
264 * new interface. So just once look for new interfaces.
271 /* Return the classical netmask for an IP address.
273 naddr /* host byte order */
274 std_mask(naddr addr) /* network byte order */
276 addr = ntohl(addr); /* was a host, not a network */
278 if (addr == 0) /* default route has mask 0 */
281 return IN_CLASSA_NET;
283 return IN_CLASSB_NET;
284 return IN_CLASSC_NET;
288 /* Find the netmask that would be inferred by RIPv1 listeners
289 * on the given interface for a given network.
290 * If no interface is specified, look for the best fitting interface.
293 ripv1_mask_net(naddr addr, /* in network byte order */
294 struct interface *ifp) /* as seen on this interface */
299 if (addr == 0) /* default always has 0 mask */
302 if (ifp != NULL && ifp->int_ripv1_mask != HOST_MASK) {
303 /* If the target network is that of the associated interface
304 * on which it arrived, then use the netmask of the interface.
306 if (on_net(addr, ifp->int_net, ifp->int_std_mask))
307 mask = ifp->int_ripv1_mask;
310 /* Examine all interfaces, and if it the target seems
311 * to have the same network number of an interface, use the
312 * netmask of that interface. If there is more than one
313 * such interface, prefer the interface with the longest
316 LIST_FOREACH(ifp, &ifnet, int_list) {
317 if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
318 && ifp->int_ripv1_mask > mask
319 && ifp->int_ripv1_mask != HOST_MASK)
320 mask = ifp->int_ripv1_mask;
325 /* check special definitions */
327 for (r1p = r1nets; r1p != NULL; r1p = r1p->r1net_next) {
328 if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
329 && r1p->r1net_mask > mask)
330 mask = r1p->r1net_mask;
333 /* Otherwise, make the classic A/B/C guess.
336 mask = std_mask(addr);
344 ripv1_mask_host(naddr addr, /* in network byte order */
345 struct interface *ifp) /* as seen on this interface */
347 naddr mask = ripv1_mask_net(addr, ifp);
350 /* If the computed netmask does not mask the address,
351 * then assume it is a host address
353 if ((ntohl(addr) & ~mask) != 0)
359 /* See if an IP address looks reasonable as a destination.
362 check_dst(naddr addr)
366 if (IN_CLASSA(addr)) {
368 return 1; /* default */
370 addr >>= IN_CLASSA_NSHIFT;
371 return (addr != 0 && addr != IN_LOOPBACKNET);
374 return (IN_CLASSB(addr) || IN_CLASSC(addr));
378 /* See a new interface duplicates an existing interface.
381 check_dup(naddr addr, /* IP address, so network byte order */
382 naddr dstaddr, /* ditto */
383 naddr mask, /* mask, so host byte order */
386 struct interface *ifp;
388 LIST_FOREACH(ifp, &ifnet, int_list) {
389 if (ifp->int_mask != mask)
392 if (!iff_up(ifp->int_if_flags))
395 /* The local address can only be shared with a point-to-point
398 if ((!(ifp->int_state & IS_REMOTE) || !(if_flags & IS_REMOTE))
399 && ifp->int_addr == addr
400 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
403 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
410 /* See that a remote gateway is reachable.
411 * Note that the answer can change as real interfaces come and go.
414 check_remote(struct interface *ifp)
418 /* do not worry about other kinds */
419 if (!(ifp->int_state & IS_REMOTE))
422 rt = rtfind(ifp->int_addr);
425 &&on_net(ifp->int_addr,
426 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
429 /* the gateway cannot be reached directly from one of our
432 if (!(ifp->int_state & IS_BROKE)) {
433 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
434 naddr_ntoa(ifp->int_addr));
441 /* Delete an interface.
444 ifdel(struct interface *ifp)
446 struct interface *ifp1;
449 trace_if("Del", ifp);
451 ifp->int_state |= IS_BROKE;
453 LIST_REMOVE(ifp, int_list);
454 *ifp->int_ahash_prev = ifp->int_ahash;
455 if (ifp->int_ahash != 0)
456 ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
457 *ifp->int_nhash_prev = ifp->int_nhash;
458 if (ifp->int_nhash != 0)
459 ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
460 if (ifp->int_if_flags & IFF_BROADCAST) {
461 *ifp->int_bhash_prev = ifp->int_bhash;
462 if (ifp->int_bhash != 0)
463 ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
465 if (ifp->int_state & IS_REMOTE)
466 LIST_REMOVE(ifp, remote_list);
468 if (!(ifp->int_state & IS_ALIAS)) {
469 /* delete aliases when the main interface dies
471 LIST_FOREACH(ifp1, &ifnet, int_list) {
473 && !strcmp(ifp->int_name, ifp1->int_name))
477 if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
479 struct sockaddr_in *sin;
481 memset(&gr, 0, sizeof(gr));
482 gr.gr_interface = ifp->int_index;
483 sin = (struct sockaddr_in *)&gr.gr_group;
484 sin->sin_family = AF_INET;
486 sin->sin_len = sizeof(struct sockaddr_in);
488 sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
489 if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
491 && errno != EADDRNOTAVAIL
493 LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
494 if (rip_sock_mcast == ifp)
495 rip_sock_mcast = NULL;
497 if (ifp->int_rip_sock >= 0) {
498 (void)close(ifp->int_rip_sock);
499 ifp->int_rip_sock = -1;
504 if (!IS_RIP_OFF(ifp->int_state))
507 /* Zap all routes associated with this interface.
508 * Assume routes just using gateways beyond this interface
509 * will timeout naturally, and have probably already died.
511 (void)rn_walktree(rhead, walk_bad, 0);
513 set_rdisc_mg(ifp, 0);
521 /* Mark an interface ill.
524 if_sick(struct interface *ifp)
526 if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
527 ifp->int_state |= IS_SICK;
528 ifp->int_act_time = NEVER;
529 trace_if("Chg", ifp);
531 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
536 /* Mark an interface dead.
539 if_bad(struct interface *ifp)
541 struct interface *ifp1;
544 if (ifp->int_state & IS_BROKE)
547 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
549 ifp->int_state |= (IS_BROKE | IS_SICK);
550 ifp->int_act_time = NEVER;
551 ifp->int_query_time = NEVER;
552 ifp->int_data.ts = now.tv_sec;
554 trace_if("Chg", ifp);
556 if (!(ifp->int_state & IS_ALIAS)) {
557 LIST_FOREACH(ifp1, &ifnet, int_list) {
559 && !strcmp(ifp->int_name, ifp1->int_name))
562 (void)rn_walktree(rhead, walk_bad, 0);
568 /* Mark an interface alive
570 int /* 1=it was dead */
571 if_ok(struct interface *ifp,
574 struct interface *ifp1;
577 if (!(ifp->int_state & IS_BROKE)) {
578 if (ifp->int_state & IS_SICK) {
579 trace_act("%sinterface %s to %s working better",
581 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
582 ifp->int_state &= ~IS_SICK;
587 msglog("%sinterface %s to %s restored",
588 type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
589 ifp->int_state &= ~(IS_BROKE | IS_SICK);
590 ifp->int_data.ts = 0;
592 if (!(ifp->int_state & IS_ALIAS)) {
593 LIST_FOREACH(ifp1, &ifnet, int_list) {
595 && !strcmp(ifp->int_name, ifp1->int_name))
601 if (ifp->int_state & IS_REMOTE) {
602 if (!addrouteforif(ifp))
609 /* disassemble routing message
612 rt_xaddrs(struct rt_addrinfo *info,
614 struct sockaddr *lim,
619 static struct sockaddr sa_zero;
622 memset(info, 0, sizeof(*info));
623 info->rti_addrs = addrs;
624 for (i = 0; i < RTAX_MAX && sa < lim; i++) {
625 if ((addrs & (1 << i)) == 0)
627 info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
628 sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
633 /* Find the network interfaces which have configured themselves.
634 * This must be done regularly, if only for extra addresses
635 * that come and go on interfaces.
640 static struct ifa_msghdr *sysctl_buf;
641 static size_t sysctl_buf_size = 0;
643 static u_int prev_complaints = 0;
644 # define COMP_NOT_INET 0x001
645 # define COMP_NOADDR 0x002
646 # define COMP_BADADDR 0x004
647 # define COMP_NODST 0x008
648 # define COMP_NOBADR 0x010
649 # define COMP_NOMASK 0x020
650 # define COMP_DUP 0x040
651 # define COMP_BAD_METRIC 0x080
652 # define COMP_NETMASK 0x100
654 struct interface ifs, ifs0, *ifp, *ifp1;
658 struct if_msghdr *ifm;
660 struct ifa_msghdr *ifam, *ifam2;
661 int in, ierr, out, oerr;
662 struct intnet *intnetp;
663 struct rt_addrinfo info;
670 ifinit_timer.tv_sec = now.tv_sec + (supplier
672 : CHECK_QUIET_INTERVAL);
674 /* mark all interfaces so we can get rid of those that disappear */
675 LIST_FOREACH(ifp, &ifnet, int_list)
676 ifp->int_state &= ~(IS_CHECKED | IS_DUP);
678 /* Fetch the interface list, without too many system calls
679 * since we do it repeatedly.
685 mib[4] = NET_RT_IFLIST;
688 if ((needed = sysctl_buf_size) != 0) {
689 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
691 /* retry if the table grew */
692 if (errno != ENOMEM && errno != EFAULT)
693 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
697 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
698 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
699 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
703 /* XXX: thanks to malloc(3), alignment can be presumed OK */
704 ifam_lim = (char *)sysctl_buf + needed;
705 for (ifam = sysctl_buf; (void *)ifam < ifam_lim; ifam = ifam2) {
707 ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
710 if (ifam->ifam_type == RTM_OIFINFO)
711 continue; /* just ignore compat message */
713 if (ifam->ifam_type == RTM_IFINFO) {
714 struct sockaddr_dl *sdl;
716 ifm = (struct if_msghdr *)ifam;
717 /* make prototype structure for the IP aliases
719 memset(&ifs0, 0, sizeof(ifs0));
720 ifs0.int_rip_sock = -1;
721 ifs0.int_index = ifm->ifm_index;
722 ifs0.int_if_flags = ifm->ifm_flags;
723 ifs0.int_state = IS_CHECKED;
724 ifs0.int_query_time = NEVER;
725 ifs0.int_act_time = now.tv_sec;
726 ifs0.int_data.ts = now.tv_sec;
727 ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
728 ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
729 ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
730 ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
731 sdl = (struct sockaddr_dl *)(ifm + 1);
732 sdl->sdl_data[sdl->sdl_nlen] = 0;
733 strncpy(ifs0.int_name, sdl->sdl_data,
734 MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
737 if (ifam->ifam_type != RTM_NEWADDR) {
738 logbad(1,"ifinit: out of sync");
741 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
742 (struct sockaddr *)ifam2,
745 /* Prepare for the next address of this interface, which
747 * Do not output RIP or Router-Discovery packets via aliases.
749 memcpy(&ifs, &ifs0, sizeof(ifs));
750 ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
752 if (INFO_IFA(&info) == 0) {
753 if (iff_up(ifs.int_if_flags)) {
754 if (!(prev_complaints & COMP_NOADDR))
755 msglog("%s has no address",
757 complaints |= COMP_NOADDR;
761 if (INFO_IFA(&info)->sa_family != AF_INET) {
762 if (iff_up(ifs.int_if_flags)) {
763 if (!(prev_complaints & COMP_NOT_INET))
764 trace_act("%s: not AF_INET",
766 complaints |= COMP_NOT_INET;
771 ifs.int_addr = S_ADDR(INFO_IFA(&info));
773 if (ntohl(ifs.int_addr)>>24 == 0
774 || ntohl(ifs.int_addr)>>24 == 0xff) {
775 if (iff_up(ifs.int_if_flags)) {
776 if (!(prev_complaints & COMP_BADADDR))
777 msglog("%s has a bad address",
779 complaints |= COMP_BADADDR;
784 if (ifs.int_if_flags & IFF_LOOPBACK) {
785 ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
786 if (ifs.int_addr == htonl(INADDR_LOOPBACK))
787 ifs.int_state |= IS_PASSIVE;
788 ifs.int_dstaddr = ifs.int_addr;
789 ifs.int_mask = HOST_MASK;
790 ifs.int_ripv1_mask = HOST_MASK;
791 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
792 ifs.int_net = ntohl(ifs.int_dstaddr);
793 if (!foundloopback) {
795 loopaddr = ifs.int_addr;
796 loop_rts.rts_gate = loopaddr;
797 loop_rts.rts_router = loopaddr;
800 } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
801 if (INFO_BRD(&info) == 0
802 || INFO_BRD(&info)->sa_family != AF_INET) {
803 if (iff_up(ifs.int_if_flags)) {
804 if (!(prev_complaints & COMP_NODST))
805 msglog("%s has a bad"
806 " destination address",
808 complaints |= COMP_NODST;
812 ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
813 if (ntohl(ifs.int_dstaddr)>>24 == 0
814 || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
815 if (iff_up(ifs.int_if_flags)) {
816 if (!(prev_complaints & COMP_NODST))
817 msglog("%s has a bad"
818 " destination address",
820 complaints |= COMP_NODST;
824 ifs.int_mask = HOST_MASK;
825 ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
826 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
827 ifs.int_net = ntohl(ifs.int_dstaddr);
830 if (INFO_MASK(&info) == 0) {
831 if (iff_up(ifs.int_if_flags)) {
832 if (!(prev_complaints & COMP_NOMASK))
833 msglog("%s has no netmask",
835 complaints |= COMP_NOMASK;
839 ifs.int_dstaddr = ifs.int_addr;
840 ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
841 ifs.int_ripv1_mask = ifs.int_mask;
842 ifs.int_std_mask = std_mask(ifs.int_addr);
843 ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
844 if (ifs.int_mask != ifs.int_std_mask)
845 ifs.int_state |= IS_SUBNET;
847 if (ifs.int_if_flags & IFF_BROADCAST) {
848 if (INFO_BRD(&info) == 0) {
849 if (iff_up(ifs.int_if_flags)) {
850 if (!(prev_complaints
853 "no broadcast address",
855 complaints |= COMP_NOBADR;
859 ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
862 ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
863 ifs.int_std_addr = htonl(ifs.int_std_net);
865 /* Use a minimum metric of one. Treat the interface metric
866 * (default 0) as an increment to the hop count of one.
868 * The metric obtained from the routing socket dump of
869 * interface addresses is wrong. It is not set by the
870 * SIOCSIFMETRIC ioctl.
873 strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
874 if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
875 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
878 ifs.int_metric = ifr.ifr_metric;
881 ifs.int_metric = ifam->ifam_metric;
883 if (ifs.int_metric > HOPCNT_INFINITY) {
885 if (!(prev_complaints & COMP_BAD_METRIC)
886 && iff_up(ifs.int_if_flags)) {
887 complaints |= COMP_BAD_METRIC;
888 msglog("%s has a metric of %d",
889 ifs.int_name, ifs.int_metric);
893 /* See if this is a familiar interface.
894 * If so, stop worrying about it if it is the same.
895 * Start it over if it now is to somewhere else, as happens
896 * frequently with PPP and SLIP.
898 ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
902 ifp->int_state |= IS_CHECKED;
904 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
909 || 0 != ((ifp->int_state ^ ifs.int_state)
911 || ifp->int_addr != ifs.int_addr
912 || ifp->int_brdaddr != ifs.int_brdaddr
913 || ifp->int_dstaddr != ifs.int_dstaddr
914 || ifp->int_mask != ifs.int_mask
915 || ifp->int_metric != ifs.int_metric) {
916 /* Forget old information about
917 * a changed interface.
919 trace_act("interface %s has changed",
927 /* The primary representative of an alias worries
928 * about how things are working.
930 if (ifp->int_state & IS_ALIAS)
933 /* note interfaces that have been turned off
935 if (!iff_up(ifs.int_if_flags)) {
936 if (iff_up(ifp->int_if_flags)) {
937 msglog("interface %s to %s turned off",
939 naddr_ntoa(ifp->int_dstaddr));
941 ifp->int_if_flags &= ~IFF_UP;
942 } else if (now.tv_sec>(ifp->int_data.ts
943 + CHECK_BAD_INTERVAL)) {
944 trace_act("interface %s has been off"
945 " %jd seconds; forget it",
947 (intmax_t)now.tv_sec -
954 /* or that were off and are now ok */
955 if (!iff_up(ifp->int_if_flags)) {
956 ifp->int_if_flags |= IFF_UP;
957 (void)if_ok(ifp, "");
960 /* If it has been long enough,
961 * see if the interface is broken.
963 if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
966 in = ifs.int_data.ipackets - ifp->int_data.ipackets;
967 ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
968 out = ifs.int_data.opackets - ifp->int_data.opackets;
969 oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
970 /* If the interface just awoke, restart the counters.
972 if (ifp->int_data.ts == 0) {
973 ifp->int_data = ifs.int_data;
976 ifp->int_data = ifs.int_data;
978 /* Withhold judgment when the short error
979 * counters wrap or the interface is reset.
981 if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
982 LIM_SEC(ifinit_timer,
983 now.tv_sec+CHECK_BAD_INTERVAL);
987 /* Withhold judgement when there is no traffic
989 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
992 /* It is bad if input or output is not working.
993 * Require presistent problems before marking it dead.
995 if ((in <= ierr && ierr > 0)
996 || (out <= oerr && oerr > 0)) {
997 if (!(ifp->int_state & IS_SICK)) {
998 trace_act("interface %s to %s"
999 " sick: in=%d ierr=%d"
1002 naddr_ntoa(ifp->int_dstaddr),
1003 in, ierr, out, oerr);
1007 if (!(ifp->int_state & IS_BROKE)) {
1008 msglog("interface %s to %s broken:"
1009 " in=%d ierr=%d out=%d oerr=%d",
1011 naddr_ntoa(ifp->int_dstaddr),
1012 in, ierr, out, oerr);
1018 /* otherwise, it is active and healthy
1020 ifp->int_act_time = now.tv_sec;
1021 (void)if_ok(ifp, "");
1025 /* This is a new interface.
1026 * If it is dead, forget it.
1028 if (!iff_up(ifs.int_if_flags))
1031 /* If it duplicates an existing interface,
1032 * complain about it, mark the other one
1033 * duplicated, and forget this one.
1035 ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1038 /* Ignore duplicates of itself, caused by having
1039 * IP aliases on the same network.
1041 if (!strcmp(ifp->int_name, ifs.int_name))
1044 if (!(prev_complaints & COMP_DUP)) {
1045 complaints |= COMP_DUP;
1046 msglog("%s (%s%s%s) is duplicated by"
1049 addrname(ifs.int_addr,ifs.int_mask,1),
1050 ((ifs.int_if_flags & IFF_POINTOPOINT)
1052 ((ifs.int_if_flags & IFF_POINTOPOINT)
1053 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1055 addrname(ifp->int_addr,ifp->int_mask,1),
1056 ((ifp->int_if_flags & IFF_POINTOPOINT)
1058 ((ifp->int_if_flags & IFF_POINTOPOINT)
1059 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1061 ifp->int_state |= IS_DUP;
1065 if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
1066 trace_act("%s is neither broadcast, point-to-point,"
1069 if (!(ifs.int_state & IFF_MULTICAST))
1070 ifs.int_state |= IS_NO_RDISC;
1074 /* It is new and ok. Add it to the list of interfaces
1076 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1077 memcpy(ifp, &ifs, sizeof(*ifp));
1080 trace_if("Add", ifp);
1082 /* Notice likely bad netmask.
1084 if (!(prev_complaints & COMP_NETMASK)
1085 && !(ifp->int_if_flags & IFF_POINTOPOINT)
1086 && ifp->int_addr != RIP_DEFAULT) {
1087 LIST_FOREACH(ifp1, &ifnet, int_list) {
1088 if (ifp1->int_mask == ifp->int_mask)
1090 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1092 if (ifp1->int_dstaddr == RIP_DEFAULT)
1094 /* ignore aliases on the right network */
1095 if (!strcmp(ifp->int_name, ifp1->int_name))
1097 if (on_net(ifp->int_dstaddr,
1098 ifp1->int_net, ifp1->int_mask)
1099 || on_net(ifp1->int_dstaddr,
1100 ifp->int_net, ifp->int_mask)) {
1101 msglog("possible netmask problem"
1102 " between %s:%s and %s:%s",
1104 addrname(htonl(ifp->int_net),
1107 addrname(htonl(ifp1->int_net),
1108 ifp1->int_mask, 1));
1109 complaints |= COMP_NETMASK;
1114 if (!(ifp->int_state & IS_ALIAS)) {
1115 /* Count the # of directly connected networks.
1117 if (!(ifp->int_if_flags & IFF_LOOPBACK))
1119 if (!IS_RIP_OFF(ifp->int_state))
1122 /* turn on router discovery and RIP If needed */
1128 /* If we are multi-homed and have at least two interfaces
1129 * listening to RIP, then output by default.
1131 if (!supplier_set && rip_interfaces > 1)
1134 /* If we are multi-homed, optionally advertise a route to
1137 if ((advertise_mhome && ifp)
1138 || (tot_interfaces > 1
1140 && (ifp = ifwithaddr(myaddr, 0, 0)) != NULL
1141 && foundloopback)) {
1142 advertise_mhome = 1;
1143 rt = rtget(myaddr, HOST_MASK);
1145 if (rt->rt_ifp != ifp
1146 || rt->rt_router != loopaddr) {
1150 loop_rts.rts_ifp = ifp;
1151 loop_rts.rts_metric = 0;
1152 loop_rts.rts_time = rt->rt_time;
1153 rtchange(rt, rt->rt_state | RS_MHOME,
1158 loop_rts.rts_ifp = ifp;
1159 loop_rts.rts_metric = 0;
1160 rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1164 LIST_FOREACH_SAFE(ifp, &ifnet, int_list, ifp1) {
1165 /* Forget any interfaces that have disappeared.
1167 if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1168 trace_act("interface %s has disappeared",
1174 if ((ifp->int_state & IS_BROKE)
1175 && !(ifp->int_state & IS_PASSIVE))
1176 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1178 /* If we ever have a RIPv1 interface, assume we always will.
1179 * It might come back if it ever goes away.
1181 if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1183 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1187 LIST_FOREACH(ifp, &ifnet, int_list) {
1188 /* Ensure there is always a network route for interfaces,
1189 * after any dead interfaces have been deleted, which
1190 * might affect routes for point-to-point links.
1192 if (!addrouteforif(ifp))
1195 /* Add routes to the local end of point-to-point interfaces
1198 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1199 && !(ifp->int_state & IS_REMOTE)
1201 /* Delete any routes to the network address through
1202 * foreign routers. Remove even static routes.
1204 del_static(ifp->int_addr, HOST_MASK, 0, 0);
1205 rt = rtget(ifp->int_addr, HOST_MASK);
1206 if (rt != NULL && rt->rt_router != loopaddr) {
1211 if (!(rt->rt_state & RS_LOCAL)
1212 || rt->rt_metric > ifp->int_metric) {
1217 loop_rts.rts_ifp = ifp1;
1218 loop_rts.rts_metric = 0;
1219 loop_rts.rts_time = rt->rt_time;
1220 rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1221 | (RS_IF|RS_LOCAL)),
1224 loop_rts.rts_ifp = ifp;
1225 loop_rts.rts_metric = 0;
1226 rtadd(ifp->int_addr, HOST_MASK,
1227 (RS_IF | RS_LOCAL), &loop_rts);
1232 /* add the authority routes */
1233 for (intnetp = intnets; intnetp != NULL;
1234 intnetp = intnetp->intnet_next) {
1235 rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1237 && !(rt->rt_state & RS_NO_NET_SYN)
1238 && !(rt->rt_state & RS_NET_INT)) {
1243 loop_rts.rts_ifp = NULL;
1244 loop_rts.rts_metric = intnetp->intnet_metric-1;
1245 rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1246 RS_NET_SYN | RS_NET_INT, &loop_rts);
1250 prev_complaints = complaints;
1255 check_net_syn(struct interface *ifp)
1257 struct rt_entry *rt;
1258 static struct rt_spare new;
1261 /* Turn on the need to automatically synthesize a network route
1262 * for this interface only if we are running RIPv1 on some other
1263 * interface that is on a different class-A,B,or C network.
1265 if (have_ripv1_out || have_ripv1_in) {
1266 ifp->int_state |= IS_NEED_NET_SYN;
1267 rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1269 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1270 && (!(rt->rt_state & RS_NET_SYN)
1271 || rt->rt_metric > ifp->int_metric)) {
1277 new.rts_gate = ifp->int_addr;
1278 new.rts_router = ifp->int_addr;
1279 new.rts_metric = ifp->int_metric;
1280 rtadd(ifp->int_std_addr, ifp->int_std_mask,
1285 ifp->int_state &= ~IS_NEED_NET_SYN;
1287 rt = rtget(ifp->int_std_addr,
1290 && (rt->rt_state & RS_NET_SYN)
1291 && rt->rt_ifp == ifp)
1297 /* Add route for interface if not currently installed.
1298 * Create route to other end if a point-to-point link,
1299 * otherwise a route to this (sub)network.
1301 static int /* 0=bad interface */
1302 addrouteforif(struct interface *ifp)
1304 struct rt_entry *rt;
1305 static struct rt_spare new;
1309 /* skip sick interfaces
1311 if (ifp->int_state & IS_BROKE)
1314 /* If the interface on a subnet, then install a RIPv1 route to
1315 * the network as well (unless it is sick).
1317 if (ifp->int_state & IS_SUBNET)
1320 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1322 : htonl(ifp->int_net));
1325 new.rts_router = ifp->int_addr;
1326 new.rts_gate = ifp->int_addr;
1327 new.rts_metric = ifp->int_metric;
1328 new.rts_time = now.tv_sec;
1330 /* If we are going to send packets to the gateway,
1331 * it must be reachable using our physical interfaces
1333 if ((ifp->int_state & IS_REMOTE)
1334 && !(ifp->int_state & IS_EXTERNAL)
1335 && !check_remote(ifp))
1338 /* We are finished if the correct main interface route exists.
1339 * The right route must be for the right interface, not synthesized
1340 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1342 del_static(dst, ifp->int_mask, 0, 0);
1343 rt = rtget(dst, ifp->int_mask);
1345 if ((rt->rt_ifp != ifp
1346 || rt->rt_router != ifp->int_addr)
1347 && (!(ifp->int_state & IS_DUP)
1349 || (rt->rt_ifp->int_state & IS_BROKE))) {
1353 rtchange(rt, ((rt->rt_state | RS_IF)
1354 & ~(RS_NET_SYN | RS_LOCAL)),
1359 if (ifp->int_transitions++ > 0)
1360 trace_act("re-install interface %s",
1363 rtadd(dst, ifp->int_mask, RS_IF, &new);