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 #elif defined(__FreeBSD__)
44 __RCSID("$Revision: 2.27 $");
45 #ident "$Revision: 2.27 $"
48 struct ifhead ifnet = LIST_HEAD_INITIALIZER(ifnet); /* all interfaces */
49 struct ifhead remote_if = LIST_HEAD_INITIALIZER(remote_if); /* remote interfaces */
51 /* hash table for all interfaces, big enough to tolerate ridiculous
52 * numbers of IP aliases. Crazy numbers of aliases such as 7000
53 * still will not do well, but not just in looking up interfaces
56 #define AHASH_LEN 211 /* must be prime */
57 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
58 static struct interface *ahash_tbl[AHASH_LEN];
60 #define BHASH_LEN 211 /* must be prime */
61 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
62 static struct interface *bhash_tbl[BHASH_LEN];
65 /* hash for physical interface names.
66 * Assume there are never more 100 or 200 real interfaces, and that
67 * aliases are put on the end of the hash chains.
70 static struct interface *nhash_tbl[NHASH_LEN];
72 int tot_interfaces; /* # of remote and local interfaces */
73 int rip_interfaces; /* # of interfaces doing RIP */
74 static int foundloopback; /* valid flag for loopaddr */
75 naddr loopaddr; /* our address on loopback */
76 static struct rt_spare loop_rts;
78 struct timeval ifinit_timer;
79 static struct timeval last_ifinit;
80 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
81 && last_ifinit.tv_usec == now.tv_usec \
82 && timercmp(&ifinit_timer, &now, >))
84 int have_ripv1_out; /* have a RIPv1 interface */
85 static int have_ripv1_in;
88 static void if_bad(struct interface *);
89 static int addrouteforif(struct interface *);
91 static struct interface**
96 for (i = 0; *p != '\0'; p++) {
97 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
100 return &nhash_tbl[i % NHASH_LEN];
104 /* Link a new interface into the lists and hash tables.
107 if_link(struct interface *ifp)
109 struct interface **hifp;
111 LIST_INSERT_HEAD(&ifnet, ifp, int_list);
113 hifp = AHASH(ifp->int_addr);
114 ifp->int_ahash_prev = hifp;
115 if ((ifp->int_ahash = *hifp) != NULL)
116 (*hifp)->int_ahash_prev = &ifp->int_ahash;
119 if (ifp->int_if_flags & IFF_BROADCAST) {
120 hifp = BHASH(ifp->int_brdaddr);
121 ifp->int_bhash_prev = hifp;
122 if ((ifp->int_bhash = *hifp) != NULL)
123 (*hifp)->int_bhash_prev = &ifp->int_bhash;
127 if (ifp->int_state & IS_REMOTE)
128 LIST_INSERT_HEAD(&remote_if, ifp, remote_list);
130 hifp = nhash(ifp->int_name);
131 if (ifp->int_state & IS_ALIAS) {
132 /* put aliases on the end of the hash chain */
133 while (*hifp != NULL)
134 hifp = &(*hifp)->int_nhash;
136 ifp->int_nhash_prev = hifp;
137 if ((ifp->int_nhash = *hifp) != NULL)
138 (*hifp)->int_nhash_prev = &ifp->int_nhash;
143 /* Find the interface with an address
146 ifwithaddr(naddr addr,
147 int bcast, /* notice IFF_BROADCAST address */
148 int remote) /* include IS_REMOTE interfaces */
150 struct interface *ifp, *possible = NULL;
152 remote = (remote == 0) ? IS_REMOTE : 0;
154 for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
155 if (ifp->int_addr != addr)
157 if ((ifp->int_state & remote) != 0)
159 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
164 if (possible || !bcast)
167 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
168 if (ifp->int_brdaddr != addr)
170 if ((ifp->int_state & remote) != 0)
172 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
181 /* find the interface with a name
183 static struct interface *
184 ifwithname(char *name, /* "ec0" or whatever */
185 naddr addr) /* 0 or network address */
187 struct interface *ifp;
190 for (ifp = *nhash(name); ifp != NULL; ifp = ifp->int_nhash) {
191 /* If the network address is not specified,
192 * ignore any alias interfaces. Otherwise, look
193 * for the interface with the target name and address.
195 if (!strcmp(ifp->int_name, name)
196 && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
197 || (ifp->int_addr == addr)))
201 /* If there is no known interface, maybe there is a
202 * new interface. So just once look for new interfaces.
204 if (IF_RESCAN_DELAY())
212 ifwithindex(u_short ifindex,
215 struct interface *ifp;
218 LIST_FOREACH(ifp, &ifnet, int_list) {
219 if (ifp->int_index == ifindex)
223 /* If there is no known interface, maybe there is a
224 * new interface. So just once look for new interfaces.
227 || IF_RESCAN_DELAY())
234 /* Find an interface from which the specified address
235 * should have come from. Used for figuring out which
236 * interface a packet came in on.
241 struct interface *ifp, *maybe;
246 LIST_FOREACH(ifp, &ifnet, int_list) {
247 if (ifp->int_if_flags & IFF_POINTOPOINT) {
248 /* finished with a match */
249 if (ifp->int_dstaddr == addr)
253 /* finished with an exact match */
254 if (ifp->int_addr == addr)
257 /* Look for the longest approximate match.
259 if (on_net(addr, ifp->int_net, ifp->int_mask)
261 || ifp->int_mask > maybe->int_mask))
266 if (maybe != NULL || once || IF_RESCAN_DELAY())
270 /* If there is no known interface, maybe there is a
271 * new interface. So just once look for new interfaces.
278 /* Return the classical netmask for an IP address.
280 naddr /* host byte order */
281 std_mask(naddr addr) /* network byte order */
283 addr = ntohl(addr); /* was a host, not a network */
285 if (addr == 0) /* default route has mask 0 */
288 return IN_CLASSA_NET;
290 return IN_CLASSB_NET;
291 return IN_CLASSC_NET;
295 /* Find the netmask that would be inferred by RIPv1 listeners
296 * on the given interface for a given network.
297 * If no interface is specified, look for the best fitting interface.
300 ripv1_mask_net(naddr addr, /* in network byte order */
301 struct interface *ifp) /* as seen on this interface */
306 if (addr == 0) /* default always has 0 mask */
309 if (ifp != NULL && ifp->int_ripv1_mask != HOST_MASK) {
310 /* If the target network is that of the associated interface
311 * on which it arrived, then use the netmask of the interface.
313 if (on_net(addr, ifp->int_net, ifp->int_std_mask))
314 mask = ifp->int_ripv1_mask;
317 /* Examine all interfaces, and if it the target seems
318 * to have the same network number of an interface, use the
319 * netmask of that interface. If there is more than one
320 * such interface, prefer the interface with the longest
323 LIST_FOREACH(ifp, &ifnet, int_list) {
324 if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
325 && ifp->int_ripv1_mask > mask
326 && ifp->int_ripv1_mask != HOST_MASK)
327 mask = ifp->int_ripv1_mask;
332 /* check special definitions */
334 for (r1p = r1nets; r1p != NULL; r1p = r1p->r1net_next) {
335 if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
336 && r1p->r1net_mask > mask)
337 mask = r1p->r1net_mask;
340 /* Otherwise, make the classic A/B/C guess.
343 mask = std_mask(addr);
351 ripv1_mask_host(naddr addr, /* in network byte order */
352 struct interface *ifp) /* as seen on this interface */
354 naddr mask = ripv1_mask_net(addr, ifp);
357 /* If the computed netmask does not mask the address,
358 * then assume it is a host address
360 if ((ntohl(addr) & ~mask) != 0)
366 /* See if an IP address looks reasonable as a destination.
369 check_dst(naddr addr)
373 if (IN_CLASSA(addr)) {
375 return 1; /* default */
377 addr >>= IN_CLASSA_NSHIFT;
378 return (addr != 0 && addr != IN_LOOPBACKNET);
381 return (IN_CLASSB(addr) || IN_CLASSC(addr));
385 /* See a new interface duplicates an existing interface.
388 check_dup(naddr addr, /* IP address, so network byte order */
389 naddr dstaddr, /* ditto */
390 naddr mask, /* mask, so host byte order */
393 struct interface *ifp;
395 LIST_FOREACH(ifp, &ifnet, int_list) {
396 if (ifp->int_mask != mask)
399 if (!iff_up(ifp->int_if_flags))
402 /* The local address can only be shared with a point-to-point
405 if ((!(ifp->int_state & IS_REMOTE) || !(if_flags & IS_REMOTE))
406 && ifp->int_addr == addr
407 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
410 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
417 /* See that a remote gateway is reachable.
418 * Note that the answer can change as real interfaces come and go.
421 check_remote(struct interface *ifp)
425 /* do not worry about other kinds */
426 if (!(ifp->int_state & IS_REMOTE))
429 rt = rtfind(ifp->int_addr);
432 &&on_net(ifp->int_addr,
433 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
436 /* the gateway cannot be reached directly from one of our
439 if (!(ifp->int_state & IS_BROKE)) {
440 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
441 naddr_ntoa(ifp->int_addr));
448 /* Delete an interface.
451 ifdel(struct interface *ifp)
453 struct interface *ifp1;
456 trace_if("Del", ifp);
458 ifp->int_state |= IS_BROKE;
460 LIST_REMOVE(ifp, int_list);
461 *ifp->int_ahash_prev = ifp->int_ahash;
462 if (ifp->int_ahash != 0)
463 ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
464 *ifp->int_nhash_prev = ifp->int_nhash;
465 if (ifp->int_nhash != 0)
466 ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
467 if (ifp->int_if_flags & IFF_BROADCAST) {
468 *ifp->int_bhash_prev = ifp->int_bhash;
469 if (ifp->int_bhash != 0)
470 ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
472 if (ifp->int_state & IS_REMOTE)
473 LIST_REMOVE(ifp, remote_list);
475 if (!(ifp->int_state & IS_ALIAS)) {
476 /* delete aliases when the main interface dies
478 LIST_FOREACH(ifp1, &ifnet, int_list) {
480 && !strcmp(ifp->int_name, ifp1->int_name))
484 if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
486 struct sockaddr_in *sin;
488 memset(&gr, 0, sizeof(gr));
489 gr.gr_interface = ifp->int_index;
490 sin = (struct sockaddr_in *)&gr.gr_group;
491 sin->sin_family = AF_INET;
493 sin->sin_len = sizeof(struct sockaddr_in);
495 sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
496 if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
498 && errno != EADDRNOTAVAIL
500 LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
501 if (rip_sock_mcast == ifp)
502 rip_sock_mcast = NULL;
504 if (ifp->int_rip_sock >= 0) {
505 (void)close(ifp->int_rip_sock);
506 ifp->int_rip_sock = -1;
511 if (!IS_RIP_OFF(ifp->int_state))
514 /* Zap all routes associated with this interface.
515 * Assume routes just using gateways beyond this interface
516 * will timeout naturally, and have probably already died.
518 (void)rn_walktree(rhead, walk_bad, 0);
520 set_rdisc_mg(ifp, 0);
528 /* Mark an interface ill.
531 if_sick(struct interface *ifp)
533 if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
534 ifp->int_state |= IS_SICK;
535 ifp->int_act_time = NEVER;
536 trace_if("Chg", ifp);
538 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
543 /* Mark an interface dead.
546 if_bad(struct interface *ifp)
548 struct interface *ifp1;
551 if (ifp->int_state & IS_BROKE)
554 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
556 ifp->int_state |= (IS_BROKE | IS_SICK);
557 ifp->int_act_time = NEVER;
558 ifp->int_query_time = NEVER;
559 ifp->int_data.ts = now.tv_sec;
561 trace_if("Chg", ifp);
563 if (!(ifp->int_state & IS_ALIAS)) {
564 LIST_FOREACH(ifp1, &ifnet, int_list) {
566 && !strcmp(ifp->int_name, ifp1->int_name))
569 (void)rn_walktree(rhead, walk_bad, 0);
575 /* Mark an interface alive
577 int /* 1=it was dead */
578 if_ok(struct interface *ifp,
581 struct interface *ifp1;
584 if (!(ifp->int_state & IS_BROKE)) {
585 if (ifp->int_state & IS_SICK) {
586 trace_act("%sinterface %s to %s working better",
588 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
589 ifp->int_state &= ~IS_SICK;
594 msglog("%sinterface %s to %s restored",
595 type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
596 ifp->int_state &= ~(IS_BROKE | IS_SICK);
597 ifp->int_data.ts = 0;
599 if (!(ifp->int_state & IS_ALIAS)) {
600 LIST_FOREACH(ifp1, &ifnet, int_list) {
602 && !strcmp(ifp->int_name, ifp1->int_name))
608 if (ifp->int_state & IS_REMOTE) {
609 if (!addrouteforif(ifp))
616 /* disassemble routing message
619 rt_xaddrs(struct rt_addrinfo *info,
621 struct sockaddr *lim,
626 static struct sockaddr sa_zero;
629 memset(info, 0, sizeof(*info));
630 info->rti_addrs = addrs;
631 for (i = 0; i < RTAX_MAX && sa < lim; i++) {
632 if ((addrs & (1 << i)) == 0)
634 info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
635 sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
640 /* Find the network interfaces which have configured themselves.
641 * This must be done regularly, if only for extra addresses
642 * that come and go on interfaces.
647 static struct ifa_msghdr *sysctl_buf;
648 static size_t sysctl_buf_size = 0;
650 static u_int prev_complaints = 0;
651 # define COMP_NOT_INET 0x001
652 # define COMP_NOADDR 0x002
653 # define COMP_BADADDR 0x004
654 # define COMP_NODST 0x008
655 # define COMP_NOBADR 0x010
656 # define COMP_NOMASK 0x020
657 # define COMP_DUP 0x040
658 # define COMP_BAD_METRIC 0x080
659 # define COMP_NETMASK 0x100
661 struct interface ifs, ifs0, *ifp, *ifp1;
665 struct if_msghdr *ifm;
667 struct ifa_msghdr *ifam, *ifam2;
668 int in, ierr, out, oerr;
669 struct intnet *intnetp;
670 struct rt_addrinfo info;
677 ifinit_timer.tv_sec = now.tv_sec + (supplier
679 : CHECK_QUIET_INTERVAL);
681 /* mark all interfaces so we can get rid of those that disappear */
682 LIST_FOREACH(ifp, &ifnet, int_list)
683 ifp->int_state &= ~(IS_CHECKED | IS_DUP);
685 /* Fetch the interface list, without too many system calls
686 * since we do it repeatedly.
692 mib[4] = NET_RT_IFLIST;
695 if ((needed = sysctl_buf_size) != 0) {
696 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
698 /* retry if the table grew */
699 if (errno != ENOMEM && errno != EFAULT)
700 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
704 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
705 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
706 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
710 /* XXX: thanks to malloc(3), alignment can be presumed OK */
711 ifam_lim = (char *)sysctl_buf + needed;
712 for (ifam = sysctl_buf; (void *)ifam < ifam_lim; ifam = ifam2) {
714 ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
717 if (ifam->ifam_type == RTM_OIFINFO)
718 continue; /* just ignore compat message */
720 if (ifam->ifam_type == RTM_IFINFO) {
721 struct sockaddr_dl *sdl;
723 ifm = (struct if_msghdr *)ifam;
724 /* make prototype structure for the IP aliases
726 memset(&ifs0, 0, sizeof(ifs0));
727 ifs0.int_rip_sock = -1;
728 ifs0.int_index = ifm->ifm_index;
729 ifs0.int_if_flags = ifm->ifm_flags;
730 ifs0.int_state = IS_CHECKED;
731 ifs0.int_query_time = NEVER;
732 ifs0.int_act_time = now.tv_sec;
733 ifs0.int_data.ts = now.tv_sec;
734 ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
735 ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
736 ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
737 ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
739 ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
741 sdl = (struct sockaddr_dl *)(ifm + 1);
742 sdl->sdl_data[sdl->sdl_nlen] = 0;
743 strncpy(ifs0.int_name, sdl->sdl_data,
744 MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
747 if (ifam->ifam_type != RTM_NEWADDR) {
748 logbad(1,"ifinit: out of sync");
751 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
752 (struct sockaddr *)ifam2,
755 /* Prepare for the next address of this interface, which
757 * Do not output RIP or Router-Discovery packets via aliases.
759 memcpy(&ifs, &ifs0, sizeof(ifs));
760 ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
762 if (INFO_IFA(&info) == 0) {
763 if (iff_up(ifs.int_if_flags)) {
764 if (!(prev_complaints & COMP_NOADDR))
765 msglog("%s has no address",
767 complaints |= COMP_NOADDR;
771 if (INFO_IFA(&info)->sa_family != AF_INET) {
772 if (iff_up(ifs.int_if_flags)) {
773 if (!(prev_complaints & COMP_NOT_INET))
774 trace_act("%s: not AF_INET",
776 complaints |= COMP_NOT_INET;
781 ifs.int_addr = S_ADDR(INFO_IFA(&info));
783 if (ntohl(ifs.int_addr)>>24 == 0
784 || ntohl(ifs.int_addr)>>24 == 0xff) {
785 if (iff_up(ifs.int_if_flags)) {
786 if (!(prev_complaints & COMP_BADADDR))
787 msglog("%s has a bad address",
789 complaints |= COMP_BADADDR;
794 if (ifs.int_if_flags & IFF_LOOPBACK) {
795 ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
796 if (ifs.int_addr == htonl(INADDR_LOOPBACK))
797 ifs.int_state |= IS_PASSIVE;
798 ifs.int_dstaddr = ifs.int_addr;
799 ifs.int_mask = HOST_MASK;
800 ifs.int_ripv1_mask = HOST_MASK;
801 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
802 ifs.int_net = ntohl(ifs.int_dstaddr);
803 if (!foundloopback) {
805 loopaddr = ifs.int_addr;
806 loop_rts.rts_gate = loopaddr;
807 loop_rts.rts_router = loopaddr;
810 } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
811 if (INFO_BRD(&info) == 0
812 || INFO_BRD(&info)->sa_family != AF_INET) {
813 if (iff_up(ifs.int_if_flags)) {
814 if (!(prev_complaints & COMP_NODST))
815 msglog("%s has a bad"
816 " destination address",
818 complaints |= COMP_NODST;
822 ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
823 if (ntohl(ifs.int_dstaddr)>>24 == 0
824 || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
825 if (iff_up(ifs.int_if_flags)) {
826 if (!(prev_complaints & COMP_NODST))
827 msglog("%s has a bad"
828 " destination address",
830 complaints |= COMP_NODST;
834 ifs.int_mask = HOST_MASK;
835 ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
836 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
837 ifs.int_net = ntohl(ifs.int_dstaddr);
840 if (INFO_MASK(&info) == 0) {
841 if (iff_up(ifs.int_if_flags)) {
842 if (!(prev_complaints & COMP_NOMASK))
843 msglog("%s has no netmask",
845 complaints |= COMP_NOMASK;
849 ifs.int_dstaddr = ifs.int_addr;
850 ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
851 ifs.int_ripv1_mask = ifs.int_mask;
852 ifs.int_std_mask = std_mask(ifs.int_addr);
853 ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
854 if (ifs.int_mask != ifs.int_std_mask)
855 ifs.int_state |= IS_SUBNET;
857 if (ifs.int_if_flags & IFF_BROADCAST) {
858 if (INFO_BRD(&info) == 0) {
859 if (iff_up(ifs.int_if_flags)) {
860 if (!(prev_complaints
863 "no broadcast address",
865 complaints |= COMP_NOBADR;
869 ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
872 ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
873 ifs.int_std_addr = htonl(ifs.int_std_net);
875 /* Use a minimum metric of one. Treat the interface metric
876 * (default 0) as an increment to the hop count of one.
878 * The metric obtained from the routing socket dump of
879 * interface addresses is wrong. It is not set by the
880 * SIOCSIFMETRIC ioctl.
883 strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
884 if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
885 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
888 ifs.int_metric = ifr.ifr_metric;
891 ifs.int_metric = ifam->ifam_metric;
893 if (ifs.int_metric > HOPCNT_INFINITY) {
895 if (!(prev_complaints & COMP_BAD_METRIC)
896 && iff_up(ifs.int_if_flags)) {
897 complaints |= COMP_BAD_METRIC;
898 msglog("%s has a metric of %d",
899 ifs.int_name, ifs.int_metric);
903 /* See if this is a familiar interface.
904 * If so, stop worrying about it if it is the same.
905 * Start it over if it now is to somewhere else, as happens
906 * frequently with PPP and SLIP.
908 ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
912 ifp->int_state |= IS_CHECKED;
914 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
919 || 0 != ((ifp->int_state ^ ifs.int_state)
921 || ifp->int_addr != ifs.int_addr
922 || ifp->int_brdaddr != ifs.int_brdaddr
923 || ifp->int_dstaddr != ifs.int_dstaddr
924 || ifp->int_mask != ifs.int_mask
925 || ifp->int_metric != ifs.int_metric) {
926 /* Forget old information about
927 * a changed interface.
929 trace_act("interface %s has changed",
937 /* The primary representative of an alias worries
938 * about how things are working.
940 if (ifp->int_state & IS_ALIAS)
943 /* note interfaces that have been turned off
945 if (!iff_up(ifs.int_if_flags)) {
946 if (iff_up(ifp->int_if_flags)) {
947 msglog("interface %s to %s turned off",
949 naddr_ntoa(ifp->int_dstaddr));
951 ifp->int_if_flags &= ~IFF_UP;
952 } else if (now.tv_sec>(ifp->int_data.ts
953 + CHECK_BAD_INTERVAL)) {
954 trace_act("interface %s has been off"
955 " %jd seconds; forget it",
957 (intmax_t)now.tv_sec -
964 /* or that were off and are now ok */
965 if (!iff_up(ifp->int_if_flags)) {
966 ifp->int_if_flags |= IFF_UP;
967 (void)if_ok(ifp, "");
970 /* If it has been long enough,
971 * see if the interface is broken.
973 if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
976 in = ifs.int_data.ipackets - ifp->int_data.ipackets;
977 ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
978 out = ifs.int_data.opackets - ifp->int_data.opackets;
979 oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
981 /* Through at least IRIX 6.2, PPP and SLIP
982 * count packets dropped by the filters.
983 * But FDDI rings stuck non-operational count
984 * dropped packets as they wait for improvement.
986 if (!(ifp->int_if_flags & IFF_POINTOPOINT))
987 oerr += (ifs.int_data.odrops
988 - ifp->int_data.odrops);
990 /* If the interface just awoke, restart the counters.
992 if (ifp->int_data.ts == 0) {
993 ifp->int_data = ifs.int_data;
996 ifp->int_data = ifs.int_data;
998 /* Withhold judgment when the short error
999 * counters wrap or the interface is reset.
1001 if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1002 LIM_SEC(ifinit_timer,
1003 now.tv_sec+CHECK_BAD_INTERVAL);
1007 /* Withhold judgement when there is no traffic
1009 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1012 /* It is bad if input or output is not working.
1013 * Require presistent problems before marking it dead.
1015 if ((in <= ierr && ierr > 0)
1016 || (out <= oerr && oerr > 0)) {
1017 if (!(ifp->int_state & IS_SICK)) {
1018 trace_act("interface %s to %s"
1019 " sick: in=%d ierr=%d"
1022 naddr_ntoa(ifp->int_dstaddr),
1023 in, ierr, out, oerr);
1027 if (!(ifp->int_state & IS_BROKE)) {
1028 msglog("interface %s to %s broken:"
1029 " in=%d ierr=%d out=%d oerr=%d",
1031 naddr_ntoa(ifp->int_dstaddr),
1032 in, ierr, out, oerr);
1038 /* otherwise, it is active and healthy
1040 ifp->int_act_time = now.tv_sec;
1041 (void)if_ok(ifp, "");
1045 /* This is a new interface.
1046 * If it is dead, forget it.
1048 if (!iff_up(ifs.int_if_flags))
1051 /* If it duplicates an existing interface,
1052 * complain about it, mark the other one
1053 * duplicated, and forget this one.
1055 ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1058 /* Ignore duplicates of itself, caused by having
1059 * IP aliases on the same network.
1061 if (!strcmp(ifp->int_name, ifs.int_name))
1064 if (!(prev_complaints & COMP_DUP)) {
1065 complaints |= COMP_DUP;
1066 msglog("%s (%s%s%s) is duplicated by"
1069 addrname(ifs.int_addr,ifs.int_mask,1),
1070 ((ifs.int_if_flags & IFF_POINTOPOINT)
1072 ((ifs.int_if_flags & IFF_POINTOPOINT)
1073 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1075 addrname(ifp->int_addr,ifp->int_mask,1),
1076 ((ifp->int_if_flags & IFF_POINTOPOINT)
1078 ((ifp->int_if_flags & IFF_POINTOPOINT)
1079 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1081 ifp->int_state |= IS_DUP;
1085 if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
1086 trace_act("%s is neither broadcast, point-to-point,"
1089 if (!(ifs.int_state & IFF_MULTICAST))
1090 ifs.int_state |= IS_NO_RDISC;
1094 /* It is new and ok. Add it to the list of interfaces
1096 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1097 memcpy(ifp, &ifs, sizeof(*ifp));
1100 trace_if("Add", ifp);
1102 /* Notice likely bad netmask.
1104 if (!(prev_complaints & COMP_NETMASK)
1105 && !(ifp->int_if_flags & IFF_POINTOPOINT)
1106 && ifp->int_addr != RIP_DEFAULT) {
1107 LIST_FOREACH(ifp1, &ifnet, int_list) {
1108 if (ifp1->int_mask == ifp->int_mask)
1110 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1112 if (ifp1->int_dstaddr == RIP_DEFAULT)
1114 /* ignore aliases on the right network */
1115 if (!strcmp(ifp->int_name, ifp1->int_name))
1117 if (on_net(ifp->int_dstaddr,
1118 ifp1->int_net, ifp1->int_mask)
1119 || on_net(ifp1->int_dstaddr,
1120 ifp->int_net, ifp->int_mask)) {
1121 msglog("possible netmask problem"
1122 " between %s:%s and %s:%s",
1124 addrname(htonl(ifp->int_net),
1127 addrname(htonl(ifp1->int_net),
1128 ifp1->int_mask, 1));
1129 complaints |= COMP_NETMASK;
1134 if (!(ifp->int_state & IS_ALIAS)) {
1135 /* Count the # of directly connected networks.
1137 if (!(ifp->int_if_flags & IFF_LOOPBACK))
1139 if (!IS_RIP_OFF(ifp->int_state))
1142 /* turn on router discovery and RIP If needed */
1148 /* If we are multi-homed and have at least two interfaces
1149 * listening to RIP, then output by default.
1151 if (!supplier_set && rip_interfaces > 1)
1154 /* If we are multi-homed, optionally advertise a route to
1157 if ((advertise_mhome && ifp)
1158 || (tot_interfaces > 1
1160 && (ifp = ifwithaddr(myaddr, 0, 0)) != NULL
1161 && foundloopback)) {
1162 advertise_mhome = 1;
1163 rt = rtget(myaddr, HOST_MASK);
1165 if (rt->rt_ifp != ifp
1166 || rt->rt_router != loopaddr) {
1170 loop_rts.rts_ifp = ifp;
1171 loop_rts.rts_metric = 0;
1172 loop_rts.rts_time = rt->rt_time;
1173 rtchange(rt, rt->rt_state | RS_MHOME,
1178 loop_rts.rts_ifp = ifp;
1179 loop_rts.rts_metric = 0;
1180 rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1184 LIST_FOREACH_SAFE(ifp, &ifnet, int_list, ifp1) {
1185 /* Forget any interfaces that have disappeared.
1187 if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1188 trace_act("interface %s has disappeared",
1194 if ((ifp->int_state & IS_BROKE)
1195 && !(ifp->int_state & IS_PASSIVE))
1196 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1198 /* If we ever have a RIPv1 interface, assume we always will.
1199 * It might come back if it ever goes away.
1201 if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1203 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1207 LIST_FOREACH(ifp, &ifnet, int_list) {
1208 /* Ensure there is always a network route for interfaces,
1209 * after any dead interfaces have been deleted, which
1210 * might affect routes for point-to-point links.
1212 if (!addrouteforif(ifp))
1215 /* Add routes to the local end of point-to-point interfaces
1218 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1219 && !(ifp->int_state & IS_REMOTE)
1221 /* Delete any routes to the network address through
1222 * foreign routers. Remove even static routes.
1224 del_static(ifp->int_addr, HOST_MASK, 0, 0);
1225 rt = rtget(ifp->int_addr, HOST_MASK);
1226 if (rt != NULL && rt->rt_router != loopaddr) {
1231 if (!(rt->rt_state & RS_LOCAL)
1232 || rt->rt_metric > ifp->int_metric) {
1237 loop_rts.rts_ifp = ifp1;
1238 loop_rts.rts_metric = 0;
1239 loop_rts.rts_time = rt->rt_time;
1240 rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1241 | (RS_IF|RS_LOCAL)),
1244 loop_rts.rts_ifp = ifp;
1245 loop_rts.rts_metric = 0;
1246 rtadd(ifp->int_addr, HOST_MASK,
1247 (RS_IF | RS_LOCAL), &loop_rts);
1252 /* add the authority routes */
1253 for (intnetp = intnets; intnetp != NULL;
1254 intnetp = intnetp->intnet_next) {
1255 rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1257 && !(rt->rt_state & RS_NO_NET_SYN)
1258 && !(rt->rt_state & RS_NET_INT)) {
1263 loop_rts.rts_ifp = NULL;
1264 loop_rts.rts_metric = intnetp->intnet_metric-1;
1265 rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1266 RS_NET_SYN | RS_NET_INT, &loop_rts);
1270 prev_complaints = complaints;
1275 check_net_syn(struct interface *ifp)
1277 struct rt_entry *rt;
1278 static struct rt_spare new;
1281 /* Turn on the need to automatically synthesize a network route
1282 * for this interface only if we are running RIPv1 on some other
1283 * interface that is on a different class-A,B,or C network.
1285 if (have_ripv1_out || have_ripv1_in) {
1286 ifp->int_state |= IS_NEED_NET_SYN;
1287 rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1289 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1290 && (!(rt->rt_state & RS_NET_SYN)
1291 || rt->rt_metric > ifp->int_metric)) {
1297 new.rts_gate = ifp->int_addr;
1298 new.rts_router = ifp->int_addr;
1299 new.rts_metric = ifp->int_metric;
1300 rtadd(ifp->int_std_addr, ifp->int_std_mask,
1305 ifp->int_state &= ~IS_NEED_NET_SYN;
1307 rt = rtget(ifp->int_std_addr,
1310 && (rt->rt_state & RS_NET_SYN)
1311 && rt->rt_ifp == ifp)
1317 /* Add route for interface if not currently installed.
1318 * Create route to other end if a point-to-point link,
1319 * otherwise a route to this (sub)network.
1321 static int /* 0=bad interface */
1322 addrouteforif(struct interface *ifp)
1324 struct rt_entry *rt;
1325 static struct rt_spare new;
1329 /* skip sick interfaces
1331 if (ifp->int_state & IS_BROKE)
1334 /* If the interface on a subnet, then install a RIPv1 route to
1335 * the network as well (unless it is sick).
1337 if (ifp->int_state & IS_SUBNET)
1340 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1342 : htonl(ifp->int_net));
1345 new.rts_router = ifp->int_addr;
1346 new.rts_gate = ifp->int_addr;
1347 new.rts_metric = ifp->int_metric;
1348 new.rts_time = now.tv_sec;
1350 /* If we are going to send packets to the gateway,
1351 * it must be reachable using our physical interfaces
1353 if ((ifp->int_state & IS_REMOTE)
1354 && !(ifp->int_state & IS_EXTERNAL)
1355 && !check_remote(ifp))
1358 /* We are finished if the correct main interface route exists.
1359 * The right route must be for the right interface, not synthesized
1360 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1362 del_static(dst, ifp->int_mask, 0, 0);
1363 rt = rtget(dst, ifp->int_mask);
1365 if ((rt->rt_ifp != ifp
1366 || rt->rt_router != ifp->int_addr)
1367 && (!(ifp->int_state & IS_DUP)
1369 || (rt->rt_ifp->int_state & IS_BROKE))) {
1373 rtchange(rt, ((rt->rt_state | RS_IF)
1374 & ~(RS_NET_SYN | RS_LOCAL)),
1379 if (ifp->int_transitions++ > 0)
1380 trace_act("re-install interface %s",
1383 rtadd(dst, ifp->int_mask, RS_IF, &new);