2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "pathnames.h"
39 #elif defined(__FreeBSD__)
42 __RCSID("$Revision: 2.27 $");
43 #ident "$Revision: 2.27 $"
46 struct ifhead ifnet = LIST_HEAD_INITIALIZER(ifnet); /* all interfaces */
47 struct ifhead remote_if = LIST_HEAD_INITIALIZER(remote_if); /* remote interfaces */
49 /* hash table for all interfaces, big enough to tolerate ridiculous
50 * numbers of IP aliases. Crazy numbers of aliases such as 7000
51 * still will not do well, but not just in looking up interfaces
54 #define AHASH_LEN 211 /* must be prime */
55 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
56 static struct interface *ahash_tbl[AHASH_LEN];
58 #define BHASH_LEN 211 /* must be prime */
59 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
60 static struct interface *bhash_tbl[BHASH_LEN];
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.
68 static struct interface *nhash_tbl[NHASH_LEN];
70 int tot_interfaces; /* # of remote and local interfaces */
71 int rip_interfaces; /* # of interfaces doing RIP */
72 static int foundloopback; /* valid flag for loopaddr */
73 naddr loopaddr; /* our address on loopback */
74 static struct rt_spare loop_rts;
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, >))
82 int have_ripv1_out; /* have a RIPv1 interface */
83 static int have_ripv1_in;
86 static void if_bad(struct interface *);
87 static int addrouteforif(struct interface *);
89 static struct interface**
94 for (i = 0; *p != '\0'; p++) {
95 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
98 return &nhash_tbl[i % NHASH_LEN];
102 /* Link a new interface into the lists and hash tables.
105 if_link(struct interface *ifp)
107 struct interface **hifp;
109 LIST_INSERT_HEAD(&ifnet, ifp, int_list);
111 hifp = AHASH(ifp->int_addr);
112 ifp->int_ahash_prev = hifp;
113 if ((ifp->int_ahash = *hifp) != 0)
114 (*hifp)->int_ahash_prev = &ifp->int_ahash;
117 if (ifp->int_if_flags & IFF_BROADCAST) {
118 hifp = BHASH(ifp->int_brdaddr);
119 ifp->int_bhash_prev = hifp;
120 if ((ifp->int_bhash = *hifp) != 0)
121 (*hifp)->int_bhash_prev = &ifp->int_bhash;
125 if (ifp->int_state & IS_REMOTE)
126 LIST_INSERT_HEAD(&remote_if, ifp, remote_list);
128 hifp = nhash(ifp->int_name);
129 if (ifp->int_state & IS_ALIAS) {
130 /* put aliases on the end of the hash chain */
132 hifp = &(*hifp)->int_nhash;
134 ifp->int_nhash_prev = hifp;
135 if ((ifp->int_nhash = *hifp) != 0)
136 (*hifp)->int_nhash_prev = &ifp->int_nhash;
141 /* Find the interface with an address
144 ifwithaddr(naddr addr,
145 int bcast, /* notice IFF_BROADCAST address */
146 int remote) /* include IS_REMOTE interfaces */
148 struct interface *ifp, *possible = 0;
150 remote = (remote == 0) ? IS_REMOTE : 0;
152 for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
153 if (ifp->int_addr != addr)
155 if ((ifp->int_state & remote) != 0)
157 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
162 if (possible || !bcast)
165 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
166 if (ifp->int_brdaddr != addr)
168 if ((ifp->int_state & remote) != 0)
170 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
179 /* find the interface with a name
181 static struct interface *
182 ifwithname(char *name, /* "ec0" or whatever */
183 naddr addr) /* 0 or network address */
185 struct interface *ifp;
188 for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
189 /* If the network address is not specified,
190 * ignore any alias interfaces. Otherwise, look
191 * for the interface with the target name and address.
193 if (!strcmp(ifp->int_name, name)
194 && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
195 || (ifp->int_addr == addr)))
199 /* If there is no known interface, maybe there is a
200 * new interface. So just once look for new interfaces.
202 if (IF_RESCAN_DELAY())
210 ifwithindex(u_short ifindex,
213 struct interface *ifp;
216 LIST_FOREACH(ifp, &ifnet, int_list) {
217 if (ifp->int_index == ifindex)
221 /* If there is no known interface, maybe there is a
222 * new interface. So just once look for new interfaces.
225 || IF_RESCAN_DELAY())
232 /* Find an interface from which the specified address
233 * should have come from. Used for figuring out which
234 * interface a packet came in on.
239 struct interface *ifp, *maybe;
244 LIST_FOREACH(ifp, &ifnet, int_list) {
245 if (ifp->int_if_flags & IFF_POINTOPOINT) {
246 /* finished with a match */
247 if (ifp->int_dstaddr == addr)
251 /* finished with an exact match */
252 if (ifp->int_addr == addr)
255 /* Look for the longest approximate match.
257 if (on_net(addr, ifp->int_net, ifp->int_mask)
259 || ifp->int_mask > maybe->int_mask))
264 if (maybe != 0 || once || IF_RESCAN_DELAY())
268 /* If there is no known interface, maybe there is a
269 * new interface. So just once look for new interfaces.
276 /* Return the classical netmask for an IP address.
278 naddr /* host byte order */
279 std_mask(naddr addr) /* network byte order */
281 addr = ntohl(addr); /* was a host, not a network */
283 if (addr == 0) /* default route has mask 0 */
286 return IN_CLASSA_NET;
288 return IN_CLASSB_NET;
289 return IN_CLASSC_NET;
293 /* Find the netmask that would be inferred by RIPv1 listeners
294 * on the given interface for a given network.
295 * If no interface is specified, look for the best fitting interface.
298 ripv1_mask_net(naddr addr, /* in network byte order */
299 struct interface *ifp) /* as seen on this interface */
304 if (addr == 0) /* default always has 0 mask */
307 if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
308 /* If the target network is that of the associated interface
309 * on which it arrived, then use the netmask of the interface.
311 if (on_net(addr, ifp->int_net, ifp->int_std_mask))
312 mask = ifp->int_ripv1_mask;
315 /* Examine all interfaces, and if it the target seems
316 * to have the same network number of an interface, use the
317 * netmask of that interface. If there is more than one
318 * such interface, prefer the interface with the longest
321 LIST_FOREACH(ifp, &ifnet, int_list) {
322 if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
323 && ifp->int_ripv1_mask > mask
324 && ifp->int_ripv1_mask != HOST_MASK)
325 mask = ifp->int_ripv1_mask;
330 /* check special definitions */
332 for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
333 if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
334 && r1p->r1net_mask > mask)
335 mask = r1p->r1net_mask;
338 /* Otherwise, make the classic A/B/C guess.
341 mask = std_mask(addr);
349 ripv1_mask_host(naddr addr, /* in network byte order */
350 struct interface *ifp) /* as seen on this interface */
352 naddr mask = ripv1_mask_net(addr, ifp);
355 /* If the computed netmask does not mask the address,
356 * then assume it is a host address
358 if ((ntohl(addr) & ~mask) != 0)
364 /* See if an IP address looks reasonable as a destination.
367 check_dst(naddr addr)
371 if (IN_CLASSA(addr)) {
373 return 1; /* default */
375 addr >>= IN_CLASSA_NSHIFT;
376 return (addr != 0 && addr != IN_LOOPBACKNET);
379 return (IN_CLASSB(addr) || IN_CLASSC(addr));
383 /* See a new interface duplicates an existing interface.
386 check_dup(naddr addr, /* IP address, so network byte order */
387 naddr dstaddr, /* ditto */
388 naddr mask, /* mask, so host byte order */
391 struct interface *ifp;
393 LIST_FOREACH(ifp, &ifnet, int_list) {
394 if (ifp->int_mask != mask)
397 if (!iff_up(ifp->int_if_flags))
400 /* The local address can only be shared with a point-to-point
403 if ((!(ifp->int_state & IS_REMOTE) || !(if_flags & IS_REMOTE))
404 && ifp->int_addr == addr
405 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
408 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
415 /* See that a remote gateway is reachable.
416 * Note that the answer can change as real interfaces come and go.
419 check_remote(struct interface *ifp)
423 /* do not worry about other kinds */
424 if (!(ifp->int_state & IS_REMOTE))
427 rt = rtfind(ifp->int_addr);
430 &&on_net(ifp->int_addr,
431 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
434 /* the gateway cannot be reached directly from one of our
437 if (!(ifp->int_state & IS_BROKE)) {
438 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
439 naddr_ntoa(ifp->int_addr));
446 /* Delete an interface.
449 ifdel(struct interface *ifp)
451 struct interface *ifp1;
454 trace_if("Del", ifp);
456 ifp->int_state |= IS_BROKE;
458 LIST_REMOVE(ifp, int_list);
459 *ifp->int_ahash_prev = ifp->int_ahash;
460 if (ifp->int_ahash != 0)
461 ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
462 *ifp->int_nhash_prev = ifp->int_nhash;
463 if (ifp->int_nhash != 0)
464 ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
465 if (ifp->int_if_flags & IFF_BROADCAST) {
466 *ifp->int_bhash_prev = ifp->int_bhash;
467 if (ifp->int_bhash != 0)
468 ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
470 if (ifp->int_state & IS_REMOTE)
471 LIST_REMOVE(ifp, remote_list);
473 if (!(ifp->int_state & IS_ALIAS)) {
474 /* delete aliases when the main interface dies
476 LIST_FOREACH(ifp1, &ifnet, int_list) {
478 && !strcmp(ifp->int_name, ifp1->int_name))
482 if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
484 struct sockaddr_in *sin;
486 memset(&gr, 0, sizeof(gr));
487 gr.gr_interface = ifp->int_index;
488 sin = (struct sockaddr_in *)&gr.gr_group;
489 sin->sin_family = AF_INET;
491 sin->sin_len = sizeof(struct sockaddr_in);
493 sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
494 if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
496 && errno != EADDRNOTAVAIL
498 LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
499 if (rip_sock_mcast == ifp)
502 if (ifp->int_rip_sock >= 0) {
503 (void)close(ifp->int_rip_sock);
504 ifp->int_rip_sock = -1;
509 if (!IS_RIP_OFF(ifp->int_state))
512 /* Zap all routes associated with this interface.
513 * Assume routes just using gateways beyond this interface
514 * will timeout naturally, and have probably already died.
516 (void)rn_walktree(rhead, walk_bad, 0);
518 set_rdisc_mg(ifp, 0);
526 /* Mark an interface ill.
529 if_sick(struct interface *ifp)
531 if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
532 ifp->int_state |= IS_SICK;
533 ifp->int_act_time = NEVER;
534 trace_if("Chg", ifp);
536 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
541 /* Mark an interface dead.
544 if_bad(struct interface *ifp)
546 struct interface *ifp1;
549 if (ifp->int_state & IS_BROKE)
552 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
554 ifp->int_state |= (IS_BROKE | IS_SICK);
555 ifp->int_act_time = NEVER;
556 ifp->int_query_time = NEVER;
557 ifp->int_data.ts = now.tv_sec;
559 trace_if("Chg", ifp);
561 if (!(ifp->int_state & IS_ALIAS)) {
562 LIST_FOREACH(ifp1, &ifnet, int_list) {
564 && !strcmp(ifp->int_name, ifp1->int_name))
567 (void)rn_walktree(rhead, walk_bad, 0);
573 /* Mark an interface alive
575 int /* 1=it was dead */
576 if_ok(struct interface *ifp,
579 struct interface *ifp1;
582 if (!(ifp->int_state & IS_BROKE)) {
583 if (ifp->int_state & IS_SICK) {
584 trace_act("%sinterface %s to %s working better",
586 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
587 ifp->int_state &= ~IS_SICK;
592 msglog("%sinterface %s to %s restored",
593 type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
594 ifp->int_state &= ~(IS_BROKE | IS_SICK);
595 ifp->int_data.ts = 0;
597 if (!(ifp->int_state & IS_ALIAS)) {
598 LIST_FOREACH(ifp1, &ifnet, int_list) {
600 && !strcmp(ifp->int_name, ifp1->int_name))
606 if (ifp->int_state & IS_REMOTE) {
607 if (!addrouteforif(ifp))
614 /* disassemble routing message
617 rt_xaddrs(struct rt_addrinfo *info,
619 struct sockaddr *lim,
624 static struct sockaddr sa_zero;
627 memset(info, 0, sizeof(*info));
628 info->rti_addrs = addrs;
629 for (i = 0; i < RTAX_MAX && sa < lim; i++) {
630 if ((addrs & (1 << i)) == 0)
632 info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
633 sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
638 /* Find the network interfaces which have configured themselves.
639 * This must be done regularly, if only for extra addresses
640 * that come and go on interfaces.
645 static struct ifa_msghdr *sysctl_buf;
646 static size_t sysctl_buf_size = 0;
648 static u_int prev_complaints = 0;
649 # define COMP_NOT_INET 0x001
650 # define COMP_NOADDR 0x002
651 # define COMP_BADADDR 0x004
652 # define COMP_NODST 0x008
653 # define COMP_NOBADR 0x010
654 # define COMP_NOMASK 0x020
655 # define COMP_DUP 0x040
656 # define COMP_BAD_METRIC 0x080
657 # define COMP_NETMASK 0x100
659 struct interface ifs, ifs0, *ifp, *ifp1;
663 struct if_msghdr *ifm;
665 struct ifa_msghdr *ifam, *ifam2;
666 int in, ierr, out, oerr;
667 struct intnet *intnetp;
668 struct rt_addrinfo info;
675 ifinit_timer.tv_sec = now.tv_sec + (supplier
677 : CHECK_QUIET_INTERVAL);
679 /* mark all interfaces so we can get rid of those that disappear */
680 LIST_FOREACH(ifp, &ifnet, int_list)
681 ifp->int_state &= ~(IS_CHECKED | IS_DUP);
683 /* Fetch the interface list, without too many system calls
684 * since we do it repeatedly.
690 mib[4] = NET_RT_IFLIST;
693 if ((needed = sysctl_buf_size) != 0) {
694 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
696 /* retry if the table grew */
697 if (errno != ENOMEM && errno != EFAULT)
698 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
702 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
703 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
704 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
708 /* XXX: thanks to malloc(3), alignment can be presumed OK */
709 ifam_lim = (char *)sysctl_buf + needed;
710 for (ifam = sysctl_buf; (void *)ifam < ifam_lim; ifam = ifam2) {
712 ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
715 if (ifam->ifam_type == RTM_OIFINFO)
716 continue; /* just ignore compat message */
718 if (ifam->ifam_type == RTM_IFINFO) {
719 struct sockaddr_dl *sdl;
721 ifm = (struct if_msghdr *)ifam;
722 /* make prototype structure for the IP aliases
724 memset(&ifs0, 0, sizeof(ifs0));
725 ifs0.int_rip_sock = -1;
726 ifs0.int_index = ifm->ifm_index;
727 ifs0.int_if_flags = ifm->ifm_flags;
728 ifs0.int_state = IS_CHECKED;
729 ifs0.int_query_time = NEVER;
730 ifs0.int_act_time = now.tv_sec;
731 ifs0.int_data.ts = now.tv_sec;
732 ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
733 ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
734 ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
735 ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
737 ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
739 sdl = (struct sockaddr_dl *)(ifm + 1);
740 sdl->sdl_data[sdl->sdl_nlen] = 0;
741 strncpy(ifs0.int_name, sdl->sdl_data,
742 MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
745 if (ifam->ifam_type != RTM_NEWADDR) {
746 logbad(1,"ifinit: out of sync");
749 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
750 (struct sockaddr *)ifam2,
753 /* Prepare for the next address of this interface, which
755 * Do not output RIP or Router-Discovery packets via aliases.
757 memcpy(&ifs, &ifs0, sizeof(ifs));
758 ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
760 if (INFO_IFA(&info) == 0) {
761 if (iff_up(ifs.int_if_flags)) {
762 if (!(prev_complaints & COMP_NOADDR))
763 msglog("%s has no address",
765 complaints |= COMP_NOADDR;
769 if (INFO_IFA(&info)->sa_family != AF_INET) {
770 if (iff_up(ifs.int_if_flags)) {
771 if (!(prev_complaints & COMP_NOT_INET))
772 trace_act("%s: not AF_INET",
774 complaints |= COMP_NOT_INET;
779 ifs.int_addr = S_ADDR(INFO_IFA(&info));
781 if (ntohl(ifs.int_addr)>>24 == 0
782 || ntohl(ifs.int_addr)>>24 == 0xff) {
783 if (iff_up(ifs.int_if_flags)) {
784 if (!(prev_complaints & COMP_BADADDR))
785 msglog("%s has a bad address",
787 complaints |= COMP_BADADDR;
792 if (ifs.int_if_flags & IFF_LOOPBACK) {
793 ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
794 if (ifs.int_addr == htonl(INADDR_LOOPBACK))
795 ifs.int_state |= IS_PASSIVE;
796 ifs.int_dstaddr = ifs.int_addr;
797 ifs.int_mask = HOST_MASK;
798 ifs.int_ripv1_mask = HOST_MASK;
799 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
800 ifs.int_net = ntohl(ifs.int_dstaddr);
801 if (!foundloopback) {
803 loopaddr = ifs.int_addr;
804 loop_rts.rts_gate = loopaddr;
805 loop_rts.rts_router = loopaddr;
808 } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
809 if (INFO_BRD(&info) == 0
810 || INFO_BRD(&info)->sa_family != AF_INET) {
811 if (iff_up(ifs.int_if_flags)) {
812 if (!(prev_complaints & COMP_NODST))
813 msglog("%s has a bad"
814 " destination address",
816 complaints |= COMP_NODST;
820 ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
821 if (ntohl(ifs.int_dstaddr)>>24 == 0
822 || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
823 if (iff_up(ifs.int_if_flags)) {
824 if (!(prev_complaints & COMP_NODST))
825 msglog("%s has a bad"
826 " destination address",
828 complaints |= COMP_NODST;
832 ifs.int_mask = HOST_MASK;
833 ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
834 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
835 ifs.int_net = ntohl(ifs.int_dstaddr);
838 if (INFO_MASK(&info) == 0) {
839 if (iff_up(ifs.int_if_flags)) {
840 if (!(prev_complaints & COMP_NOMASK))
841 msglog("%s has no netmask",
843 complaints |= COMP_NOMASK;
847 ifs.int_dstaddr = ifs.int_addr;
848 ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
849 ifs.int_ripv1_mask = ifs.int_mask;
850 ifs.int_std_mask = std_mask(ifs.int_addr);
851 ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
852 if (ifs.int_mask != ifs.int_std_mask)
853 ifs.int_state |= IS_SUBNET;
855 if (ifs.int_if_flags & IFF_BROADCAST) {
856 if (INFO_BRD(&info) == 0) {
857 if (iff_up(ifs.int_if_flags)) {
858 if (!(prev_complaints
861 "no broadcast address",
863 complaints |= COMP_NOBADR;
867 ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
870 ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
871 ifs.int_std_addr = htonl(ifs.int_std_net);
873 /* Use a minimum metric of one. Treat the interface metric
874 * (default 0) as an increment to the hop count of one.
876 * The metric obtained from the routing socket dump of
877 * interface addresses is wrong. It is not set by the
878 * SIOCSIFMETRIC ioctl.
881 strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
882 if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
883 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
886 ifs.int_metric = ifr.ifr_metric;
889 ifs.int_metric = ifam->ifam_metric;
891 if (ifs.int_metric > HOPCNT_INFINITY) {
893 if (!(prev_complaints & COMP_BAD_METRIC)
894 && iff_up(ifs.int_if_flags)) {
895 complaints |= COMP_BAD_METRIC;
896 msglog("%s has a metric of %d",
897 ifs.int_name, ifs.int_metric);
901 /* See if this is a familiar interface.
902 * If so, stop worrying about it if it is the same.
903 * Start it over if it now is to somewhere else, as happens
904 * frequently with PPP and SLIP.
906 ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
910 ifp->int_state |= IS_CHECKED;
912 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
917 || 0 != ((ifp->int_state ^ ifs.int_state)
919 || ifp->int_addr != ifs.int_addr
920 || ifp->int_brdaddr != ifs.int_brdaddr
921 || ifp->int_dstaddr != ifs.int_dstaddr
922 || ifp->int_mask != ifs.int_mask
923 || ifp->int_metric != ifs.int_metric) {
924 /* Forget old information about
925 * a changed interface.
927 trace_act("interface %s has changed",
935 /* The primary representative of an alias worries
936 * about how things are working.
938 if (ifp->int_state & IS_ALIAS)
941 /* note interfaces that have been turned off
943 if (!iff_up(ifs.int_if_flags)) {
944 if (iff_up(ifp->int_if_flags)) {
945 msglog("interface %s to %s turned off",
947 naddr_ntoa(ifp->int_dstaddr));
949 ifp->int_if_flags &= ~IFF_UP;
950 } else if (now.tv_sec>(ifp->int_data.ts
951 + CHECK_BAD_INTERVAL)) {
952 trace_act("interface %s has been off"
953 " %jd seconds; forget it",
955 (intmax_t)now.tv_sec -
961 /* or that were off and are now ok */
962 if (!iff_up(ifp->int_if_flags)) {
963 ifp->int_if_flags |= IFF_UP;
964 (void)if_ok(ifp, "");
967 /* If it has been long enough,
968 * see if the interface is broken.
970 if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
973 in = ifs.int_data.ipackets - ifp->int_data.ipackets;
974 ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
975 out = ifs.int_data.opackets - ifp->int_data.opackets;
976 oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
978 /* Through at least IRIX 6.2, PPP and SLIP
979 * count packets dropped by the filters.
980 * But FDDI rings stuck non-operational count
981 * dropped packets as they wait for improvement.
983 if (!(ifp->int_if_flags & IFF_POINTOPOINT))
984 oerr += (ifs.int_data.odrops
985 - ifp->int_data.odrops);
987 /* If the interface just awoke, restart the counters.
989 if (ifp->int_data.ts == 0) {
990 ifp->int_data = ifs.int_data;
993 ifp->int_data = ifs.int_data;
995 /* Withhold judgment when the short error
996 * counters wrap or the interface is reset.
998 if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
999 LIM_SEC(ifinit_timer,
1000 now.tv_sec+CHECK_BAD_INTERVAL);
1004 /* Withhold judgement when there is no traffic
1006 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1009 /* It is bad if input or output is not working.
1010 * Require presistent problems before marking it dead.
1012 if ((in <= ierr && ierr > 0)
1013 || (out <= oerr && oerr > 0)) {
1014 if (!(ifp->int_state & IS_SICK)) {
1015 trace_act("interface %s to %s"
1016 " sick: in=%d ierr=%d"
1019 naddr_ntoa(ifp->int_dstaddr),
1020 in, ierr, out, oerr);
1024 if (!(ifp->int_state & IS_BROKE)) {
1025 msglog("interface %s to %s broken:"
1026 " in=%d ierr=%d out=%d oerr=%d",
1028 naddr_ntoa(ifp->int_dstaddr),
1029 in, ierr, out, oerr);
1035 /* otherwise, it is active and healthy
1037 ifp->int_act_time = now.tv_sec;
1038 (void)if_ok(ifp, "");
1042 /* This is a new interface.
1043 * If it is dead, forget it.
1045 if (!iff_up(ifs.int_if_flags))
1048 /* If it duplicates an existing interface,
1049 * complain about it, mark the other one
1050 * duplicated, and forget this one.
1052 ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1055 /* Ignore duplicates of itself, caused by having
1056 * IP aliases on the same network.
1058 if (!strcmp(ifp->int_name, ifs.int_name))
1061 if (!(prev_complaints & COMP_DUP)) {
1062 complaints |= COMP_DUP;
1063 msglog("%s (%s%s%s) is duplicated by"
1066 addrname(ifs.int_addr,ifs.int_mask,1),
1067 ((ifs.int_if_flags & IFF_POINTOPOINT)
1069 ((ifs.int_if_flags & IFF_POINTOPOINT)
1070 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1072 addrname(ifp->int_addr,ifp->int_mask,1),
1073 ((ifp->int_if_flags & IFF_POINTOPOINT)
1075 ((ifp->int_if_flags & IFF_POINTOPOINT)
1076 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1078 ifp->int_state |= IS_DUP;
1082 if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
1083 trace_act("%s is neither broadcast, point-to-point,"
1086 if (!(ifs.int_state & IFF_MULTICAST))
1087 ifs.int_state |= IS_NO_RDISC;
1091 /* It is new and ok. Add it to the list of interfaces
1093 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1094 memcpy(ifp, &ifs, sizeof(*ifp));
1097 trace_if("Add", ifp);
1099 /* Notice likely bad netmask.
1101 if (!(prev_complaints & COMP_NETMASK)
1102 && !(ifp->int_if_flags & IFF_POINTOPOINT)
1103 && ifp->int_addr != RIP_DEFAULT) {
1104 LIST_FOREACH(ifp1, &ifnet, int_list) {
1105 if (ifp1->int_mask == ifp->int_mask)
1107 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1109 if (ifp1->int_dstaddr == RIP_DEFAULT)
1111 /* ignore aliases on the right network */
1112 if (!strcmp(ifp->int_name, ifp1->int_name))
1114 if (on_net(ifp->int_dstaddr,
1115 ifp1->int_net, ifp1->int_mask)
1116 || on_net(ifp1->int_dstaddr,
1117 ifp->int_net, ifp->int_mask)) {
1118 msglog("possible netmask problem"
1119 " between %s:%s and %s:%s",
1121 addrname(htonl(ifp->int_net),
1124 addrname(htonl(ifp1->int_net),
1125 ifp1->int_mask, 1));
1126 complaints |= COMP_NETMASK;
1131 if (!(ifp->int_state & IS_ALIAS)) {
1132 /* Count the # of directly connected networks.
1134 if (!(ifp->int_if_flags & IFF_LOOPBACK))
1136 if (!IS_RIP_OFF(ifp->int_state))
1139 /* turn on router discovery and RIP If needed */
1145 /* If we are multi-homed and have at least two interfaces
1146 * listening to RIP, then output by default.
1148 if (!supplier_set && rip_interfaces > 1)
1151 /* If we are multi-homed, optionally advertise a route to
1155 || (tot_interfaces > 1
1157 && (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1158 && foundloopback)) {
1159 advertise_mhome = 1;
1160 rt = rtget(myaddr, HOST_MASK);
1162 if (rt->rt_ifp != ifp
1163 || rt->rt_router != loopaddr) {
1167 loop_rts.rts_ifp = ifp;
1168 loop_rts.rts_metric = 0;
1169 loop_rts.rts_time = rt->rt_time;
1170 rtchange(rt, rt->rt_state | RS_MHOME,
1175 loop_rts.rts_ifp = ifp;
1176 loop_rts.rts_metric = 0;
1177 rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1181 LIST_FOREACH_SAFE(ifp, &ifnet, int_list, ifp1) {
1182 /* Forget any interfaces that have disappeared.
1184 if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1185 trace_act("interface %s has disappeared",
1191 if ((ifp->int_state & IS_BROKE)
1192 && !(ifp->int_state & IS_PASSIVE))
1193 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1195 /* If we ever have a RIPv1 interface, assume we always will.
1196 * It might come back if it ever goes away.
1198 if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1200 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1204 LIST_FOREACH(ifp, &ifnet, int_list) {
1205 /* Ensure there is always a network route for interfaces,
1206 * after any dead interfaces have been deleted, which
1207 * might affect routes for point-to-point links.
1209 if (!addrouteforif(ifp))
1212 /* Add routes to the local end of point-to-point interfaces
1215 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1216 && !(ifp->int_state & IS_REMOTE)
1218 /* Delete any routes to the network address through
1219 * foreign routers. Remove even static routes.
1221 del_static(ifp->int_addr, HOST_MASK, 0, 0);
1222 rt = rtget(ifp->int_addr, HOST_MASK);
1223 if (rt != 0 && rt->rt_router != loopaddr) {
1228 if (!(rt->rt_state & RS_LOCAL)
1229 || rt->rt_metric > ifp->int_metric) {
1234 loop_rts.rts_ifp = ifp1;
1235 loop_rts.rts_metric = 0;
1236 loop_rts.rts_time = rt->rt_time;
1237 rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1238 | (RS_IF|RS_LOCAL)),
1241 loop_rts.rts_ifp = ifp;
1242 loop_rts.rts_metric = 0;
1243 rtadd(ifp->int_addr, HOST_MASK,
1244 (RS_IF | RS_LOCAL), &loop_rts);
1249 /* add the authority routes */
1250 for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1251 rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1253 && !(rt->rt_state & RS_NO_NET_SYN)
1254 && !(rt->rt_state & RS_NET_INT)) {
1259 loop_rts.rts_ifp = 0;
1260 loop_rts.rts_metric = intnetp->intnet_metric-1;
1261 rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1262 RS_NET_SYN | RS_NET_INT, &loop_rts);
1266 prev_complaints = complaints;
1271 check_net_syn(struct interface *ifp)
1273 struct rt_entry *rt;
1274 static struct rt_spare new;
1277 /* Turn on the need to automatically synthesize a network route
1278 * for this interface only if we are running RIPv1 on some other
1279 * interface that is on a different class-A,B,or C network.
1281 if (have_ripv1_out || have_ripv1_in) {
1282 ifp->int_state |= IS_NEED_NET_SYN;
1283 rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1285 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1286 && (!(rt->rt_state & RS_NET_SYN)
1287 || rt->rt_metric > ifp->int_metric)) {
1293 new.rts_gate = ifp->int_addr;
1294 new.rts_router = ifp->int_addr;
1295 new.rts_metric = ifp->int_metric;
1296 rtadd(ifp->int_std_addr, ifp->int_std_mask,
1301 ifp->int_state &= ~IS_NEED_NET_SYN;
1303 rt = rtget(ifp->int_std_addr,
1306 && (rt->rt_state & RS_NET_SYN)
1307 && rt->rt_ifp == ifp)
1313 /* Add route for interface if not currently installed.
1314 * Create route to other end if a point-to-point link,
1315 * otherwise a route to this (sub)network.
1317 static int /* 0=bad interface */
1318 addrouteforif(struct interface *ifp)
1320 struct rt_entry *rt;
1321 static struct rt_spare new;
1325 /* skip sick interfaces
1327 if (ifp->int_state & IS_BROKE)
1330 /* If the interface on a subnet, then install a RIPv1 route to
1331 * the network as well (unless it is sick).
1333 if (ifp->int_state & IS_SUBNET)
1336 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1338 : htonl(ifp->int_net));
1341 new.rts_router = ifp->int_addr;
1342 new.rts_gate = ifp->int_addr;
1343 new.rts_metric = ifp->int_metric;
1344 new.rts_time = now.tv_sec;
1346 /* If we are going to send packets to the gateway,
1347 * it must be reachable using our physical interfaces
1349 if ((ifp->int_state & IS_REMOTE)
1350 && !(ifp->int_state & IS_EXTERNAL)
1351 && !check_remote(ifp))
1354 /* We are finished if the correct main interface route exists.
1355 * The right route must be for the right interface, not synthesized
1356 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1358 del_static(dst, ifp->int_mask, 0, 0);
1359 rt = rtget(dst, ifp->int_mask);
1361 if ((rt->rt_ifp != ifp
1362 || rt->rt_router != ifp->int_addr)
1363 && (!(ifp->int_state & IS_DUP)
1365 || (rt->rt_ifp->int_state & IS_BROKE))) {
1369 rtchange(rt, ((rt->rt_state | RS_IF)
1370 & ~(RS_NET_SYN | RS_LOCAL)),
1375 if (ifp->int_transitions++ > 0)
1376 trace_act("re-install interface %s",
1379 rtadd(dst, ifp->int_mask, RS_IF, &new);