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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgment:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sbin/routed/if.c,v 1.6.2.1 2000/08/14 17:00:03 sheldonh Exp $
34 * $DragonFly: src/sbin/routed/if.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
38 #include "pathnames.h"
40 #if !defined(sgi) && !defined(__NetBSD__)
41 static char sccsid[] __attribute__((unused)) = "@(#)if.c 8.1 (Berkeley) 6/5/93";
42 #elif defined(__NetBSD__)
43 #include <sys/cdefs.h>
46 #ident "$FreeBSD: src/sbin/routed/if.c,v 1.6.2.1 2000/08/14 17:00:03 sheldonh Exp $"
48 struct interface *ifnet; /* all interfaces */
50 /* hash table for all interfaces, big enough to tolerate ridiculous
51 * numbers of IP aliases. Crazy numbers of aliases such as 7000
52 * still will not do well, but not just in looking up interfaces
55 #define AHASH_LEN 211 /* must be prime */
56 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
57 struct interface *ahash_tbl[AHASH_LEN];
59 #define BHASH_LEN 211 /* must be prime */
60 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
61 struct interface *bhash_tbl[BHASH_LEN];
63 struct interface *remote_if; /* remote interfaces */
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 struct interface *nhash_tbl[NHASH_LEN];
72 int tot_interfaces; /* # of remote and local interfaces */
73 int rip_interfaces; /* # of interfaces doing RIP */
74 int foundloopback; /* valid flag for loopaddr */
75 naddr loopaddr; /* our address on loopback */
76 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 */
88 static struct interface**
93 for (i = 0; *p != '\0'; p++) {
94 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
97 return &nhash_tbl[i % NHASH_LEN];
101 /* Link a new interface into the lists and hash tables.
104 if_link(struct interface *ifp)
106 struct interface **hifp;
108 ifp->int_prev = &ifnet;
109 ifp->int_next = ifnet;
111 ifnet->int_prev = &ifp->int_next;
114 hifp = AHASH(ifp->int_addr);
115 ifp->int_ahash_prev = hifp;
116 if ((ifp->int_ahash = *hifp) != 0)
117 (*hifp)->int_ahash_prev = &ifp->int_ahash;
120 if (ifp->int_if_flags & IFF_BROADCAST) {
121 hifp = BHASH(ifp->int_brdaddr);
122 ifp->int_bhash_prev = hifp;
123 if ((ifp->int_bhash = *hifp) != 0)
124 (*hifp)->int_bhash_prev = &ifp->int_bhash;
128 if (ifp->int_state & IS_REMOTE) {
129 ifp->int_rlink_prev = &remote_if;
130 ifp->int_rlink = remote_if;
132 remote_if->int_rlink_prev = &ifp->int_rlink;
136 hifp = nhash(ifp->int_name);
137 if (ifp->int_state & IS_ALIAS) {
138 /* put aliases on the end of the hash chain */
140 hifp = &(*hifp)->int_nhash;
142 ifp->int_nhash_prev = hifp;
143 if ((ifp->int_nhash = *hifp) != 0)
144 (*hifp)->int_nhash_prev = &ifp->int_nhash;
149 /* Find the interface with an address
152 ifwithaddr(naddr addr,
153 int bcast, /* notice IFF_BROADCAST address */
154 int remote) /* include IS_REMOTE interfaces */
156 struct interface *ifp, *possible = 0;
158 remote = (remote == 0) ? IS_REMOTE : 0;
160 for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
161 if (ifp->int_addr != addr)
163 if ((ifp->int_state & remote) != 0)
165 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
170 if (possible || !bcast)
173 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
174 if (ifp->int_brdaddr != addr)
176 if ((ifp->int_state & remote) != 0)
178 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
187 /* find the interface with a name
190 ifwithname(char *name, /* "ec0" or whatever */
191 naddr addr) /* 0 or network address */
193 struct interface *ifp;
196 for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
197 /* If the network address is not specified,
198 * ignore any alias interfaces. Otherwise, look
199 * for the interface with the target name and address.
201 if (!strcmp(ifp->int_name, name)
202 && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
203 || (ifp->int_addr == addr)))
207 /* If there is no known interface, maybe there is a
208 * new interface. So just once look for new interfaces.
210 if (IF_RESCAN_DELAY())
218 ifwithindex(u_short index,
221 struct interface *ifp;
224 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
225 if (ifp->int_index == index)
229 /* If there is no known interface, maybe there is a
230 * new interface. So just once look for new interfaces.
233 || IF_RESCAN_DELAY())
240 /* Find an interface from which the specified address
241 * should have come from. Used for figuring out which
242 * interface a packet came in on.
247 struct interface *ifp, *maybe;
251 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
252 if (ifp->int_if_flags & IFF_POINTOPOINT) {
253 /* finished with a match */
254 if (ifp->int_dstaddr == addr)
258 /* finished with an exact match */
259 if (ifp->int_addr == addr)
262 /* Look for the longest approximate match.
264 if (on_net(addr, ifp->int_net, ifp->int_mask)
266 || ifp->int_mask > maybe->int_mask))
272 || IF_RESCAN_DELAY())
275 /* If there is no known interface, maybe there is a
276 * new interface. So just once look for new interfaces.
283 /* Return the classical netmask for an IP address.
285 naddr /* host byte order */
286 std_mask(naddr addr) /* network byte order */
288 NTOHL(addr); /* was a host, not a network */
290 if (addr == 0) /* default route has mask 0 */
293 return IN_CLASSA_NET;
295 return IN_CLASSB_NET;
296 return IN_CLASSC_NET;
300 /* Find the netmask that would be inferred by RIPv1 listeners
301 * on the given interface for a given network.
302 * If no interface is specified, look for the best fitting interface.
305 ripv1_mask_net(naddr addr, /* in network byte order */
306 struct interface *ifp) /* as seen on this interface */
311 if (addr == 0) /* default always has 0 mask */
314 if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
315 /* If the target network is that of the associated interface
316 * on which it arrived, then use the netmask of the interface.
318 if (on_net(addr, ifp->int_net, ifp->int_std_mask))
319 mask = ifp->int_ripv1_mask;
322 /* Examine all interfaces, and if it the target seems
323 * to have the same network number of an interface, use the
324 * netmask of that interface. If there is more than one
325 * such interface, prefer the interface with the longest
328 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
329 if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
330 && ifp->int_ripv1_mask > mask
331 && ifp->int_ripv1_mask != HOST_MASK)
332 mask = ifp->int_ripv1_mask;
337 /* check special definitions */
339 for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
340 if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
341 && r1p->r1net_mask > mask)
342 mask = r1p->r1net_mask;
345 /* Otherwise, make the classic A/B/C guess.
348 mask = std_mask(addr);
356 ripv1_mask_host(naddr addr, /* in network byte order */
357 struct interface *ifp) /* as seen on this interface */
359 naddr mask = ripv1_mask_net(addr, ifp);
362 /* If the computed netmask does not mask the address,
363 * then assume it is a host address
365 if ((ntohl(addr) & ~mask) != 0)
371 /* See if a IP address looks reasonable as a destination
374 check_dst(naddr addr)
378 if (IN_CLASSA(addr)) {
380 return 1; /* default */
382 addr >>= IN_CLASSA_NSHIFT;
383 return (addr != 0 && addr != IN_LOOPBACKNET);
386 return (IN_CLASSB(addr) || IN_CLASSC(addr));
390 /* See a new interface duplicates an existing interface.
393 check_dup(naddr addr, /* IP address, so network byte order */
394 naddr dstaddr, /* ditto */
395 naddr mask, /* mask, so host byte order */
398 struct interface *ifp;
400 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
401 if (ifp->int_mask != mask)
404 if (!iff_up(ifp->int_if_flags))
407 /* The local address can only be shared with a point-to-point
410 if (ifp->int_addr == addr
411 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
414 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
421 /* See that a remote gateway is reachable.
422 * Note that the answer can change as real interfaces come and go.
425 check_remote(struct interface *ifp)
429 /* do not worry about other kinds */
430 if (!(ifp->int_state & IS_REMOTE))
433 rt = rtfind(ifp->int_addr);
436 &&on_net(ifp->int_addr,
437 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
440 /* the gateway cannot be reached directly from one of our
443 if (!(ifp->int_state & IS_BROKE)) {
444 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
445 naddr_ntoa(ifp->int_addr));
452 /* Delete an interface.
455 ifdel(struct interface *ifp)
458 struct interface *ifp1;
461 trace_if("Del", ifp);
463 ifp->int_state |= IS_BROKE;
465 /* unlink the interface
467 *ifp->int_prev = ifp->int_next;
468 if (ifp->int_next != 0)
469 ifp->int_next->int_prev = ifp->int_prev;
470 *ifp->int_ahash_prev = ifp->int_ahash;
471 if (ifp->int_ahash != 0)
472 ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
473 *ifp->int_nhash_prev = ifp->int_nhash;
474 if (ifp->int_nhash != 0)
475 ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
476 if (ifp->int_if_flags & IFF_BROADCAST) {
477 *ifp->int_bhash_prev = ifp->int_bhash;
478 if (ifp->int_bhash != 0)
479 ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
481 if (ifp->int_state & IS_REMOTE) {
482 *ifp->int_rlink_prev = ifp->int_rlink;
483 if (ifp->int_rlink != 0)
484 ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
487 if (!(ifp->int_state & IS_ALIAS)) {
488 /* delete aliases when the main interface dies
490 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
492 && !strcmp(ifp->int_name, ifp1->int_name))
496 if ((ifp->int_if_flags & IFF_MULTICAST)
498 && !(ifp->int_if_flags & IFF_POINTOPOINT)
501 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
502 m.imr_interface.s_addr = ((ifp->int_if_flags
506 if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP,
508 && errno != EADDRNOTAVAIL
510 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
511 if (rip_sock_mcast == ifp)
514 if (ifp->int_rip_sock >= 0) {
515 (void)close(ifp->int_rip_sock);
516 ifp->int_rip_sock = -1;
521 if (!IS_RIP_OFF(ifp->int_state))
524 /* Zap all routes associated with this interface.
525 * Assume routes just using gateways beyond this interface
526 * will timeout naturally, and have probably already died.
528 (void)rn_walktree(rhead, walk_bad, 0);
530 set_rdisc_mg(ifp, 0);
538 /* Mark an interface ill.
541 if_sick(struct interface *ifp)
543 if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
544 ifp->int_state |= IS_SICK;
545 ifp->int_act_time = NEVER;
546 trace_if("Chg", ifp);
548 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
553 /* Mark an interface dead.
556 if_bad(struct interface *ifp)
558 struct interface *ifp1;
561 if (ifp->int_state & IS_BROKE)
564 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
566 ifp->int_state |= (IS_BROKE | IS_SICK);
567 ifp->int_act_time = NEVER;
568 ifp->int_query_time = NEVER;
569 ifp->int_data.ts = now.tv_sec;
571 trace_if("Chg", ifp);
573 if (!(ifp->int_state & IS_ALIAS)) {
574 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
576 && !strcmp(ifp->int_name, ifp1->int_name))
579 (void)rn_walktree(rhead, walk_bad, 0);
585 /* Mark an interface alive
587 int /* 1=it was dead */
588 if_ok(struct interface *ifp,
591 struct interface *ifp1;
594 if (!(ifp->int_state & IS_BROKE)) {
595 if (ifp->int_state & IS_SICK) {
596 trace_act("%sinterface %s to %s working better",
598 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
599 ifp->int_state &= ~IS_SICK;
604 msglog("%sinterface %s to %s restored",
605 type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
606 ifp->int_state &= ~(IS_BROKE | IS_SICK);
607 ifp->int_data.ts = 0;
609 if (!(ifp->int_state & IS_ALIAS)) {
610 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
612 && !strcmp(ifp->int_name, ifp1->int_name))
618 if (ifp->int_state & IS_REMOTE) {
619 if (!addrouteforif(ifp))
626 /* disassemble routing message
629 rt_xaddrs(struct rt_addrinfo *info,
631 struct sockaddr *lim,
636 static struct sockaddr sa_zero;
639 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
640 : sizeof(__uint64_t))
642 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
647 memset(info, 0, sizeof(*info));
648 info->rti_addrs = addrs;
649 for (i = 0; i < RTAX_MAX && sa < lim; i++) {
650 if ((addrs & (1 << i)) == 0)
653 info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
654 sa = (struct sockaddr *)((char*)(sa)
655 + ROUNDUP(sa->sa_len));
657 info->rti_info[i] = sa;
658 sa = (struct sockaddr *)((char*)(sa)
659 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
665 /* Find the network interfaces which have configured themselves.
666 * This must be done regularly, if only for extra addresses
667 * that come and go on interfaces.
672 static char *sysctl_buf;
673 static size_t sysctl_buf_size = 0;
675 static u_int prev_complaints = 0;
676 # define COMP_NOT_INET 0x001
677 # define COMP_NOADDR 0x002
678 # define COMP_BADADDR 0x004
679 # define COMP_NODST 0x008
680 # define COMP_NOBADR 0x010
681 # define COMP_NOMASK 0x020
682 # define COMP_DUP 0x040
683 # define COMP_BAD_METRIC 0x080
684 # define COMP_NETMASK 0x100
686 struct interface ifs, ifs0, *ifp, *ifp1;
690 struct if_msghdr *ifm;
691 struct ifa_msghdr *ifam, *ifam_lim, *ifam2;
692 int in, ierr, out, oerr;
693 struct intnet *intnetp;
694 struct rt_addrinfo info;
701 ifinit_timer.tv_sec = now.tv_sec + (supplier
703 : CHECK_QUIET_INTERVAL);
705 /* mark all interfaces so we can get rid of those that disappear */
706 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next)
707 ifp->int_state &= ~(IS_CHECKED | IS_DUP);
709 /* Fetch the interface list, without too many system calls
710 * since we do it repeatedly.
716 mib[4] = NET_RT_IFLIST;
719 if ((needed = sysctl_buf_size) != 0) {
720 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
722 /* retry if the table grew */
723 if (errno != ENOMEM && errno != EFAULT)
724 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
728 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
729 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
730 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
734 ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
735 for (ifam = (struct ifa_msghdr *)sysctl_buf;
739 ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
741 if (ifam->ifam_type == RTM_IFINFO) {
742 struct sockaddr_dl *sdl;
744 ifm = (struct if_msghdr *)ifam;
745 /* make prototype structure for the IP aliases
747 memset(&ifs0, 0, sizeof(ifs0));
748 ifs0.int_rip_sock = -1;
749 ifs0.int_index = ifm->ifm_index;
750 ifs0.int_if_flags = ifm->ifm_flags;
751 ifs0.int_state = IS_CHECKED;
752 ifs0.int_query_time = NEVER;
753 ifs0.int_act_time = now.tv_sec;
754 ifs0.int_data.ts = now.tv_sec;
755 ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
756 ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
757 ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
758 ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
760 ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
762 sdl = (struct sockaddr_dl *)(ifm + 1);
763 sdl->sdl_data[sdl->sdl_nlen] = 0;
764 strncpy(ifs0.int_name, sdl->sdl_data,
765 MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
768 if (ifam->ifam_type != RTM_NEWADDR) {
769 logbad(1,"ifinit: out of sync");
772 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
773 (struct sockaddr *)ifam2,
776 /* Prepare for the next address of this interface, which
778 * Do not output RIP or Router-Discovery packets via aliases.
780 memcpy(&ifs, &ifs0, sizeof(ifs));
781 ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
783 if (INFO_IFA(&info) == 0) {
784 if (iff_up(ifs.int_if_flags)) {
785 if (!(prev_complaints & COMP_NOADDR))
786 msglog("%s has no address",
788 complaints |= COMP_NOADDR;
792 if (INFO_IFA(&info)->sa_family != AF_INET) {
793 if (iff_up(ifs.int_if_flags)) {
794 if (!(prev_complaints & COMP_NOT_INET))
795 trace_act("%s: not AF_INET",
797 complaints |= COMP_NOT_INET;
802 ifs.int_addr = S_ADDR(INFO_IFA(&info));
804 if (ntohl(ifs.int_addr)>>24 == 0
805 || ntohl(ifs.int_addr)>>24 == 0xff) {
806 if (iff_up(ifs.int_if_flags)) {
807 if (!(prev_complaints & COMP_BADADDR))
808 msglog("%s has a bad address",
810 complaints |= COMP_BADADDR;
815 if (ifs.int_if_flags & IFF_LOOPBACK) {
816 ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
817 ifs.int_dstaddr = ifs.int_addr;
818 ifs.int_mask = HOST_MASK;
819 ifs.int_ripv1_mask = HOST_MASK;
820 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
821 ifs.int_net = ntohl(ifs.int_dstaddr);
822 if (!foundloopback) {
824 loopaddr = ifs.int_addr;
825 loop_rts.rts_gate = loopaddr;
826 loop_rts.rts_router = loopaddr;
829 } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
830 if (INFO_BRD(&info) == 0
831 || INFO_BRD(&info)->sa_family != AF_INET) {
832 if (iff_up(ifs.int_if_flags)) {
833 if (!(prev_complaints & COMP_NODST))
834 msglog("%s has a bad"
835 " destination address",
837 complaints |= COMP_NODST;
841 ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
842 if (ntohl(ifs.int_dstaddr)>>24 == 0
843 || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
844 if (iff_up(ifs.int_if_flags)) {
845 if (!(prev_complaints & COMP_NODST))
846 msglog("%s has a bad"
847 " destination address",
849 complaints |= COMP_NODST;
853 ifs.int_mask = HOST_MASK;
854 ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
855 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
856 ifs.int_net = ntohl(ifs.int_dstaddr);
859 if (INFO_MASK(&info) == 0) {
860 if (iff_up(ifs.int_if_flags)) {
861 if (!(prev_complaints & COMP_NOMASK))
862 msglog("%s has no netmask",
864 complaints |= COMP_NOMASK;
868 ifs.int_dstaddr = ifs.int_addr;
869 ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
870 ifs.int_ripv1_mask = ifs.int_mask;
871 ifs.int_std_mask = std_mask(ifs.int_addr);
872 ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
873 if (ifs.int_mask != ifs.int_std_mask)
874 ifs.int_state |= IS_SUBNET;
876 if (ifs.int_if_flags & IFF_BROADCAST) {
877 if (INFO_BRD(&info) == 0) {
878 if (iff_up(ifs.int_if_flags)) {
879 if (!(prev_complaints
882 "no broadcast address",
884 complaints |= COMP_NOBADR;
888 ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
891 ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
892 ifs.int_std_addr = htonl(ifs.int_std_net);
894 /* Use a minimum metric of one. Treat the interface metric
895 * (default 0) as an increment to the hop count of one.
897 * The metric obtained from the routing socket dump of
898 * interface addresses is wrong. It is not set by the
899 * SIOCSIFMETRIC ioctl.
902 strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
903 if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
904 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
907 ifs.int_metric = ifr.ifr_metric;
910 ifs.int_metric = ifam->ifam_metric;
912 if (ifs.int_metric > HOPCNT_INFINITY) {
914 if (!(prev_complaints & COMP_BAD_METRIC)
915 && iff_up(ifs.int_if_flags)) {
916 complaints |= COMP_BAD_METRIC;
917 msglog("%s has a metric of %d",
918 ifs.int_name, ifs.int_metric);
922 /* See if this is a familiar interface.
923 * If so, stop worrying about it if it is the same.
924 * Start it over if it now is to somewhere else, as happens
925 * frequently with PPP and SLIP.
927 ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
931 ifp->int_state |= IS_CHECKED;
933 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
938 || 0 != ((ifp->int_state ^ ifs.int_state)
940 || ifp->int_addr != ifs.int_addr
941 || ifp->int_brdaddr != ifs.int_brdaddr
942 || ifp->int_dstaddr != ifs.int_dstaddr
943 || ifp->int_mask != ifs.int_mask
944 || ifp->int_metric != ifs.int_metric) {
945 /* Forget old information about
946 * a changed interface.
948 trace_act("interface %s has changed",
956 /* The primary representative of an alias worries
957 * about how things are working.
959 if (ifp->int_state & IS_ALIAS)
962 /* note interfaces that have been turned off
964 if (!iff_up(ifs.int_if_flags)) {
965 if (iff_up(ifp->int_if_flags)) {
966 msglog("interface %s to %s turned off",
968 naddr_ntoa(ifp->int_dstaddr));
970 ifp->int_if_flags &= ~IFF_UP;
971 } else if (now.tv_sec>(ifp->int_data.ts
972 + CHECK_BAD_INTERVAL)) {
973 trace_act("interface %s has been off"
974 " %ld seconds; forget it",
976 now.tv_sec-ifp->int_data.ts);
981 /* or that were off and are now ok */
982 if (!iff_up(ifp->int_if_flags)) {
983 ifp->int_if_flags |= IFF_UP;
984 (void)if_ok(ifp, "");
987 /* If it has been long enough,
988 * see if the interface is broken.
990 if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
993 in = ifs.int_data.ipackets - ifp->int_data.ipackets;
994 ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
995 out = ifs.int_data.opackets - ifp->int_data.opackets;
996 oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
998 /* Through at least IRIX 6.2, PPP and SLIP
999 * count packets dropped by the filters.
1000 * But FDDI rings stuck non-operational count
1001 * dropped packets as they wait for improvement.
1003 if (!(ifp->int_if_flags & IFF_POINTOPOINT))
1004 oerr += (ifs.int_data.odrops
1005 - ifp->int_data.odrops);
1007 /* If the interface just awoke, restart the counters.
1009 if (ifp->int_data.ts == 0) {
1010 ifp->int_data = ifs.int_data;
1013 ifp->int_data = ifs.int_data;
1015 /* Withhold judgment when the short error
1016 * counters wrap or the interface is reset.
1018 if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1019 LIM_SEC(ifinit_timer,
1020 now.tv_sec+CHECK_BAD_INTERVAL);
1024 /* Withhold judgement when there is no traffic
1026 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1029 /* It is bad if input or output is not working.
1030 * Require presistent problems before marking it dead.
1032 if ((in <= ierr && ierr > 0)
1033 || (out <= oerr && oerr > 0)) {
1034 if (!(ifp->int_state & IS_SICK)) {
1035 trace_act("interface %s to %s"
1036 " sick: in=%d ierr=%d"
1039 naddr_ntoa(ifp->int_dstaddr),
1040 in, ierr, out, oerr);
1044 if (!(ifp->int_state & IS_BROKE)) {
1045 msglog("interface %s to %s broken:"
1046 " in=%d ierr=%d out=%d oerr=%d",
1048 naddr_ntoa(ifp->int_dstaddr),
1049 in, ierr, out, oerr);
1055 /* otherwise, it is active and healthy
1057 ifp->int_act_time = now.tv_sec;
1058 (void)if_ok(ifp, "");
1062 /* This is a new interface.
1063 * If it is dead, forget it.
1065 if (!iff_up(ifs.int_if_flags))
1068 /* If it duplicates an existing interface,
1069 * complain about it, mark the other one
1070 * duplicated, and forget this one.
1072 ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1075 /* Ignore duplicates of itself, caused by having
1076 * IP aliases on the same network.
1078 if (!strcmp(ifp->int_name, ifs.int_name))
1081 if (!(prev_complaints & COMP_DUP)) {
1082 complaints |= COMP_DUP;
1083 msglog("%s (%s%s%s) is duplicated by"
1086 addrname(ifs.int_addr,ifs.int_mask,1),
1087 ((ifs.int_if_flags & IFF_POINTOPOINT)
1089 ((ifs.int_if_flags & IFF_POINTOPOINT)
1090 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1092 addrname(ifp->int_addr,ifp->int_mask,1),
1093 ((ifp->int_if_flags & IFF_POINTOPOINT)
1095 ((ifp->int_if_flags & IFF_POINTOPOINT)
1096 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1098 ifp->int_state |= IS_DUP;
1102 if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST))
1103 && !(ifs.int_state & IS_PASSIVE)) {
1104 trace_act("%s is neither broadcast, point-to-point,"
1107 if (!(ifs.int_state & IFF_MULTICAST))
1108 ifs.int_state |= IS_NO_RDISC;
1112 /* It is new and ok. Add it to the list of interfaces
1114 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1115 memcpy(ifp, &ifs, sizeof(*ifp));
1118 trace_if("Add", ifp);
1120 /* Notice likely bad netmask.
1122 if (!(prev_complaints & COMP_NETMASK)
1123 && !(ifp->int_if_flags & IFF_POINTOPOINT)
1124 && ifp->int_addr != RIP_DEFAULT) {
1125 for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) {
1126 if (ifp1->int_mask == ifp->int_mask)
1128 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1130 if (ifp1->int_dstaddr == RIP_DEFAULT)
1132 /* ignore aliases on the right network */
1133 if (!strcmp(ifp->int_name, ifp1->int_name))
1135 if (on_net(ifp->int_dstaddr,
1136 ifp1->int_net, ifp1->int_mask)
1137 || on_net(ifp1->int_dstaddr,
1138 ifp->int_net, ifp->int_mask)) {
1139 msglog("possible netmask problem"
1140 " between %s:%s and %s:%s",
1142 addrname(htonl(ifp->int_net),
1145 addrname(htonl(ifp1->int_net),
1146 ifp1->int_mask, 1));
1147 complaints |= COMP_NETMASK;
1152 if (!(ifp->int_state & IS_ALIAS)) {
1153 /* Count the # of directly connected networks.
1155 if (!(ifp->int_if_flags & IFF_LOOPBACK))
1157 if (!IS_RIP_OFF(ifp->int_state))
1160 /* turn on router discovery and RIP If needed */
1166 /* If we are multi-homed and have at least two interfaces
1167 * listening to RIP, then output by default.
1169 if (!supplier_set && rip_interfaces > 1)
1172 /* If we are multi-homed, optionally advertise a route to
1176 || (tot_interfaces > 1
1178 && (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1179 && foundloopback)) {
1180 advertise_mhome = 1;
1181 rt = rtget(myaddr, HOST_MASK);
1183 if (rt->rt_ifp != ifp
1184 || rt->rt_router != loopaddr) {
1188 loop_rts.rts_ifp = ifp;
1189 loop_rts.rts_metric = 0;
1190 loop_rts.rts_time = rt->rt_time;
1191 rtchange(rt, rt->rt_state | RS_MHOME,
1196 loop_rts.rts_ifp = ifp;
1197 loop_rts.rts_metric = 0;
1198 rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1202 for (ifp = ifnet; ifp != 0; ifp = ifp1) {
1203 ifp1 = ifp->int_next; /* because we may delete it */
1205 /* Forget any interfaces that have disappeared.
1207 if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1208 trace_act("interface %s has disappeared",
1214 if ((ifp->int_state & IS_BROKE)
1215 && !(ifp->int_state & IS_PASSIVE))
1216 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1218 /* If we ever have a RIPv1 interface, assume we always will.
1219 * It might come back if it ever goes away.
1221 if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1223 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1227 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1228 /* Ensure there is always a network route for interfaces,
1229 * after any dead interfaces have been deleted, which
1230 * might affect routes for point-to-point links.
1232 if (!addrouteforif(ifp))
1235 /* Add routes to the local end of point-to-point interfaces
1238 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1239 && !(ifp->int_state & IS_REMOTE)
1241 /* Delete any routes to the network address through
1242 * foreign routers. Remove even static routes.
1244 del_static(ifp->int_addr, HOST_MASK, 0, 0);
1245 rt = rtget(ifp->int_addr, HOST_MASK);
1246 if (rt != 0 && rt->rt_router != loopaddr) {
1251 if (!(rt->rt_state & RS_LOCAL)
1252 || rt->rt_metric > ifp->int_metric) {
1257 loop_rts.rts_ifp = ifp1;
1258 loop_rts.rts_metric = 0;
1259 loop_rts.rts_time = rt->rt_time;
1260 rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1261 | (RS_IF|RS_LOCAL)),
1264 loop_rts.rts_ifp = ifp;
1265 loop_rts.rts_metric = 0;
1266 rtadd(ifp->int_addr, HOST_MASK,
1267 (RS_IF | RS_LOCAL), &loop_rts);
1272 /* add the authority routes */
1273 for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1274 rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1276 && !(rt->rt_state & RS_NO_NET_SYN)
1277 && !(rt->rt_state & RS_NET_INT)) {
1282 loop_rts.rts_ifp = 0;
1283 loop_rts.rts_metric = intnetp->intnet_metric-1;
1284 rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1285 RS_NET_SYN | RS_NET_INT, &loop_rts);
1289 prev_complaints = complaints;
1294 check_net_syn(struct interface *ifp)
1296 struct rt_entry *rt;
1297 static struct rt_spare new;
1300 /* Turn on the need to automatically synthesize a network route
1301 * for this interface only if we are running RIPv1 on some other
1302 * interface that is on a different class-A,B,or C network.
1304 if (have_ripv1_out || have_ripv1_in) {
1305 ifp->int_state |= IS_NEED_NET_SYN;
1306 rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1308 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1309 && (!(rt->rt_state & RS_NET_SYN)
1310 || rt->rt_metric > ifp->int_metric)) {
1316 new.rts_gate = ifp->int_addr;
1317 new.rts_router = ifp->int_addr;
1318 new.rts_metric = ifp->int_metric;
1319 rtadd(ifp->int_std_addr, ifp->int_std_mask,
1324 ifp->int_state &= ~IS_NEED_NET_SYN;
1326 rt = rtget(ifp->int_std_addr,
1329 && (rt->rt_state & RS_NET_SYN)
1330 && rt->rt_ifp == ifp)
1336 /* Add route for interface if not currently installed.
1337 * Create route to other end if a point-to-point link,
1338 * otherwise a route to this (sub)network.
1340 int /* 0=bad interface */
1341 addrouteforif(struct interface *ifp)
1343 struct rt_entry *rt;
1344 static struct rt_spare new;
1348 /* skip sick interfaces
1350 if (ifp->int_state & IS_BROKE)
1353 /* If the interface on a subnet, then install a RIPv1 route to
1354 * the network as well (unless it is sick).
1356 if (ifp->int_state & IS_SUBNET)
1359 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1361 : htonl(ifp->int_net));
1364 new.rts_router = ifp->int_addr;
1365 new.rts_gate = ifp->int_addr;
1366 new.rts_metric = ifp->int_metric;
1367 new.rts_time = now.tv_sec;
1369 /* If we are going to send packets to the gateway,
1370 * it must be reachable using our physical interfaces
1372 if ((ifp->int_state & IS_REMOTE)
1373 && !(ifp->int_state & IS_EXTERNAL)
1374 && !check_remote(ifp))
1377 /* We are finished if the correct main interface route exists.
1378 * The right route must be for the right interface, not synthesized
1379 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1381 del_static(dst, ifp->int_mask, 0, 0);
1382 rt = rtget(dst, ifp->int_mask);
1384 if ((rt->rt_ifp != ifp
1385 || rt->rt_router != ifp->int_addr)
1386 && (!(ifp->int_state & IS_DUP)
1388 || (rt->rt_ifp->int_state & IS_BROKE))) {
1392 rtchange(rt, ((rt->rt_state | RS_IF)
1393 & ~(RS_NET_SYN | RS_LOCAL)),
1398 if (ifp->int_transitions++ > 0)
1399 trace_act("re-install interface %s",
1402 rtadd(dst, ifp->int_mask, RS_IF, &new);