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 $
37 #include "pathnames.h"
39 #if !defined(sgi) && !defined(__NetBSD__)
40 static char sccsid[] __attribute__((unused)) = "@(#)if.c 8.1 (Berkeley) 6/5/93";
41 #elif defined(__NetBSD__)
42 #include <sys/cdefs.h>
46 struct interface *ifnet; /* all interfaces */
48 /* hash table for all interfaces, big enough to tolerate ridiculous
49 * numbers of IP aliases. Crazy numbers of aliases such as 7000
50 * still will not do well, but not just in looking up interfaces
53 #define AHASH_LEN 211 /* must be prime */
54 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
55 struct interface *ahash_tbl[AHASH_LEN];
57 #define BHASH_LEN 211 /* must be prime */
58 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
59 struct interface *bhash_tbl[BHASH_LEN];
61 struct interface *remote_if; /* remote interfaces */
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 struct interface *nhash_tbl[NHASH_LEN];
70 int tot_interfaces; /* # of remote and local interfaces */
71 int rip_interfaces; /* # of interfaces doing RIP */
72 int foundloopback; /* valid flag for loopaddr */
73 naddr loopaddr; /* our address on loopback */
74 struct rt_spare loop_rts;
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 */
86 static struct interface**
91 for (i = 0; *p != '\0'; p++) {
92 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
95 return &nhash_tbl[i % NHASH_LEN];
99 /* Link a new interface into the lists and hash tables.
102 if_link(struct interface *ifp)
104 struct interface **hifp;
106 ifp->int_prev = &ifnet;
107 ifp->int_next = ifnet;
109 ifnet->int_prev = &ifp->int_next;
112 hifp = AHASH(ifp->int_addr);
113 ifp->int_ahash_prev = hifp;
114 if ((ifp->int_ahash = *hifp) != NULL)
115 (*hifp)->int_ahash_prev = &ifp->int_ahash;
118 if (ifp->int_if_flags & IFF_BROADCAST) {
119 hifp = BHASH(ifp->int_brdaddr);
120 ifp->int_bhash_prev = hifp;
121 if ((ifp->int_bhash = *hifp) != NULL)
122 (*hifp)->int_bhash_prev = &ifp->int_bhash;
126 if (ifp->int_state & IS_REMOTE) {
127 ifp->int_rlink_prev = &remote_if;
128 ifp->int_rlink = remote_if;
129 if (remote_if != NULL)
130 remote_if->int_rlink_prev = &ifp->int_rlink;
134 hifp = nhash(ifp->int_name);
135 if (ifp->int_state & IS_ALIAS) {
136 /* put aliases on the end of the hash chain */
137 while (*hifp != NULL)
138 hifp = &(*hifp)->int_nhash;
140 ifp->int_nhash_prev = hifp;
141 if ((ifp->int_nhash = *hifp) != NULL)
142 (*hifp)->int_nhash_prev = &ifp->int_nhash;
147 /* Find the interface with an address
150 ifwithaddr(naddr addr,
151 int bcast, /* notice IFF_BROADCAST address */
152 int remote) /* include IS_REMOTE interfaces */
154 struct interface *ifp, *possible = NULL;
156 remote = (remote == 0) ? IS_REMOTE : 0;
158 for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
159 if (ifp->int_addr != addr)
161 if ((ifp->int_state & remote) != 0)
163 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
168 if (possible || !bcast)
171 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
172 if (ifp->int_brdaddr != addr)
174 if ((ifp->int_state & remote) != 0)
176 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
185 /* find the interface with a name
188 ifwithname(char *name, /* "ec0" or whatever */
189 naddr addr) /* 0 or network address */
191 struct interface *ifp;
194 for (ifp = *nhash(name); ifp != NULL; ifp = ifp->int_nhash) {
195 /* If the network address is not specified,
196 * ignore any alias interfaces. Otherwise, look
197 * for the interface with the target name and address.
199 if (!strcmp(ifp->int_name, name)
200 && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
201 || (ifp->int_addr == addr)))
205 /* If there is no known interface, maybe there is a
206 * new interface. So just once look for new interfaces.
208 if (IF_RESCAN_DELAY())
216 ifwithindex(u_short idx, int rescan_ok)
218 struct interface *ifp;
221 for (ifp = ifnet; NULL != ifp; ifp = ifp->int_next) {
222 if (ifp->int_index == idx)
226 /* If there is no known interface, maybe there is a
227 * new interface. So just once look for new interfaces.
230 || IF_RESCAN_DELAY())
237 /* Find an interface from which the specified address
238 * should have come from. Used for figuring out which
239 * interface a packet came in on.
244 struct interface *ifp, *maybe;
248 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
249 if (ifp->int_if_flags & IFF_POINTOPOINT) {
250 /* finished with a match */
251 if (ifp->int_dstaddr == addr)
255 /* finished with an exact match */
256 if (ifp->int_addr == addr)
259 /* Look for the longest approximate match.
261 if (on_net(addr, ifp->int_net, ifp->int_mask)
263 || ifp->int_mask > maybe->int_mask))
269 || IF_RESCAN_DELAY())
272 /* If there is no known interface, maybe there is a
273 * new interface. So just once look for new interfaces.
280 /* Return the classical netmask for an IP address.
282 naddr /* host byte order */
283 std_mask(naddr addr) /* network byte order */
285 addr = ntohl(addr); /* was a host, not a network */
287 if (addr == 0) /* default route has mask 0 */
290 return IN_CLASSA_NET;
292 return IN_CLASSB_NET;
293 return IN_CLASSC_NET;
297 /* Find the netmask that would be inferred by RIPv1 listeners
298 * on the given interface for a given network.
299 * If no interface is specified, look for the best fitting interface.
302 ripv1_mask_net(naddr addr, /* in network byte order */
303 struct interface *ifp) /* as seen on this interface */
308 if (addr == 0) /* default always has 0 mask */
311 if (ifp != NULL && ifp->int_ripv1_mask != HOST_MASK) {
312 /* If the target network is that of the associated interface
313 * on which it arrived, then use the netmask of the interface.
315 if (on_net(addr, ifp->int_net, ifp->int_std_mask))
316 mask = ifp->int_ripv1_mask;
319 /* Examine all interfaces, and if it the target seems
320 * to have the same network number of an interface, use the
321 * netmask of that interface. If there is more than one
322 * such interface, prefer the interface with the longest
325 for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
326 if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
327 && ifp->int_ripv1_mask > mask
328 && ifp->int_ripv1_mask != HOST_MASK)
329 mask = ifp->int_ripv1_mask;
334 /* check special definitions */
336 for (r1p = r1nets; r1p != NULL; r1p = r1p->r1net_next) {
337 if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
338 && r1p->r1net_mask > mask)
339 mask = r1p->r1net_mask;
342 /* Otherwise, make the classic A/B/C guess.
345 mask = std_mask(addr);
353 ripv1_mask_host(naddr addr, /* in network byte order */
354 struct interface *ifp) /* as seen on this interface */
356 naddr mask = ripv1_mask_net(addr, ifp);
359 /* If the computed netmask does not mask the address,
360 * then assume it is a host address
362 if ((ntohl(addr) & ~mask) != 0)
368 /* See if a IP address looks reasonable as a destination
371 check_dst(naddr addr)
375 if (IN_CLASSA(addr)) {
377 return 1; /* default */
379 addr >>= IN_CLASSA_NSHIFT;
380 return (addr != 0 && addr != IN_LOOPBACKNET);
383 return (IN_CLASSB(addr) || IN_CLASSC(addr));
387 /* See a new interface duplicates an existing interface.
390 check_dup(naddr addr, /* IP address, so network byte order */
391 naddr dstaddr, /* ditto */
392 naddr mask, /* mask, so host byte order */
395 struct interface *ifp;
397 for (ifp = ifnet; NULL != ifp; ifp = ifp->int_next) {
398 if (ifp->int_mask != mask)
401 if (!iff_up(ifp->int_if_flags))
404 /* The local address can only be shared with a point-to-point
407 if (ifp->int_addr == addr
408 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
411 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
418 /* See that a remote gateway is reachable.
419 * Note that the answer can change as real interfaces come and go.
422 check_remote(struct interface *ifp)
426 /* do not worry about other kinds */
427 if (!(ifp->int_state & IS_REMOTE))
430 rt = rtfind(ifp->int_addr);
433 &&on_net(ifp->int_addr,
434 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
437 /* the gateway cannot be reached directly from one of our
440 if (!(ifp->int_state & IS_BROKE)) {
441 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
442 naddr_ntoa(ifp->int_addr));
449 /* Delete an interface.
452 ifdel(struct interface *ifp)
455 struct interface *ifp1;
458 trace_if("Del", ifp);
460 ifp->int_state |= IS_BROKE;
462 /* unlink the interface
464 *ifp->int_prev = ifp->int_next;
465 if (ifp->int_next != 0)
466 ifp->int_next->int_prev = ifp->int_prev;
467 *ifp->int_ahash_prev = ifp->int_ahash;
468 if (ifp->int_ahash != 0)
469 ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
470 *ifp->int_nhash_prev = ifp->int_nhash;
471 if (ifp->int_nhash != 0)
472 ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
473 if (ifp->int_if_flags & IFF_BROADCAST) {
474 *ifp->int_bhash_prev = ifp->int_bhash;
475 if (ifp->int_bhash != 0)
476 ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
478 if (ifp->int_state & IS_REMOTE) {
479 *ifp->int_rlink_prev = ifp->int_rlink;
480 if (ifp->int_rlink != 0)
481 ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev;
484 if (!(ifp->int_state & IS_ALIAS)) {
485 /* delete aliases when the main interface dies
487 for (ifp1 = ifnet; NULL != ifp1; ifp1 = ifp1->int_next) {
489 && !strcmp(ifp->int_name, ifp1->int_name))
493 if ((ifp->int_if_flags & IFF_MULTICAST)
495 && !(ifp->int_if_flags & IFF_POINTOPOINT)
498 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
499 m.imr_interface.s_addr = ((ifp->int_if_flags
503 if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP,
505 && errno != EADDRNOTAVAIL
507 LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)");
508 if (rip_sock_mcast == ifp)
511 if (ifp->int_rip_sock >= 0) {
512 close(ifp->int_rip_sock);
513 ifp->int_rip_sock = -1;
518 if (!IS_RIP_OFF(ifp->int_state))
521 /* Zap all routes associated with this interface.
522 * Assume routes just using gateways beyond this interface
523 * will timeout naturally, and have probably already died.
525 rn_walktree(rhead, walk_bad, 0);
527 set_rdisc_mg(ifp, 0);
535 /* Mark an interface ill.
538 if_sick(struct interface *ifp)
540 if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
541 ifp->int_state |= IS_SICK;
542 ifp->int_act_time = NEVER;
543 trace_if("Chg", ifp);
545 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
550 /* Mark an interface dead.
553 if_bad(struct interface *ifp)
555 struct interface *ifp1;
558 if (ifp->int_state & IS_BROKE)
561 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
563 ifp->int_state |= (IS_BROKE | IS_SICK);
564 ifp->int_act_time = NEVER;
565 ifp->int_query_time = NEVER;
566 ifp->int_data.ts = now.tv_sec;
568 trace_if("Chg", ifp);
570 if (!(ifp->int_state & IS_ALIAS)) {
571 for (ifp1 = ifnet; NULL != ifp1; ifp1 = ifp1->int_next) {
573 && !strcmp(ifp->int_name, ifp1->int_name))
576 rn_walktree(rhead, walk_bad, 0);
582 /* Mark an interface alive
584 int /* 1=it was dead */
585 if_ok(struct interface *ifp,
588 struct interface *ifp1;
591 if (!(ifp->int_state & IS_BROKE)) {
592 if (ifp->int_state & IS_SICK) {
593 trace_act("%sinterface %s to %s working better",
595 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
596 ifp->int_state &= ~IS_SICK;
601 msglog("%sinterface %s to %s restored",
602 type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
603 ifp->int_state &= ~(IS_BROKE | IS_SICK);
604 ifp->int_data.ts = 0;
606 if (!(ifp->int_state & IS_ALIAS)) {
607 for (ifp1 = ifnet; NULL != ifp1; ifp1 = ifp1->int_next) {
609 && !strcmp(ifp->int_name, ifp1->int_name))
615 if (ifp->int_state & IS_REMOTE) {
616 if (!addrouteforif(ifp))
623 /* disassemble routing message
626 rt_xaddrs(struct rt_addrinfo *info,
628 struct sockaddr *lim,
633 static struct sockaddr sa_zero;
636 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \
637 : sizeof(__uint64_t))
639 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
644 memset(info, 0, sizeof(*info));
645 info->rti_addrs = addrs;
646 for (i = 0; i < RTAX_MAX && sa < lim; i++) {
647 if ((addrs & (1 << i)) == 0)
650 info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
651 sa = (struct sockaddr *)((char*)(sa)
652 + ROUNDUP(sa->sa_len));
654 info->rti_info[i] = sa;
655 sa = (struct sockaddr *)((char*)(sa)
656 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
662 /* Find the network interfaces which have configured themselves.
663 * This must be done regularly, if only for extra addresses
664 * that come and go on interfaces.
669 static char *sysctl_buf;
670 static size_t sysctl_buf_size = 0;
672 static u_int prev_complaints = 0;
673 # define COMP_NOT_INET 0x001
674 # define COMP_NOADDR 0x002
675 # define COMP_BADADDR 0x004
676 # define COMP_NODST 0x008
677 # define COMP_NOBADR 0x010
678 # define COMP_NOMASK 0x020
679 # define COMP_DUP 0x040
680 # define COMP_BAD_METRIC 0x080
681 # define COMP_NETMASK 0x100
683 struct interface ifs, ifs0, *ifp, *ifp1;
687 struct if_msghdr *ifm;
688 struct ifa_msghdr *ifam, *ifam_lim, *ifam2;
689 int in, ierr, out, oerr;
690 struct intnet *intnetp;
691 struct rt_addrinfo info;
698 ifinit_timer.tv_sec = now.tv_sec + (supplier
700 : CHECK_QUIET_INTERVAL);
702 /* mark all interfaces so we can get rid of those that disappear */
703 for (ifp = ifnet; NULL != ifp; ifp = ifp->int_next)
704 ifp->int_state &= ~(IS_CHECKED | IS_DUP);
706 /* Fetch the interface list, without too many system calls
707 * since we do it repeatedly.
713 mib[4] = NET_RT_IFLIST;
716 if ((needed = sysctl_buf_size) != 0) {
717 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
719 /* retry if the table grew */
720 if (errno != ENOMEM && errno != EFAULT)
721 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
725 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
726 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
727 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
731 ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed);
732 for (ifam = (struct ifa_msghdr *)sysctl_buf;
736 ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
738 if (ifam->ifam_type == RTM_IFINFO) {
739 struct sockaddr_dl *sdl;
741 ifm = (struct if_msghdr *)ifam;
742 /* make prototype structure for the IP aliases
744 memset(&ifs0, 0, sizeof(ifs0));
745 ifs0.int_rip_sock = -1;
746 ifs0.int_index = ifm->ifm_index;
747 ifs0.int_if_flags = ifm->ifm_flags;
748 ifs0.int_state = IS_CHECKED;
749 ifs0.int_query_time = NEVER;
750 ifs0.int_act_time = now.tv_sec;
751 ifs0.int_data.ts = now.tv_sec;
752 ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
753 ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
754 ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
755 ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
757 ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
759 sdl = (struct sockaddr_dl *)(ifm + 1);
760 sdl->sdl_data[sdl->sdl_nlen] = 0;
761 strncpy(ifs0.int_name, sdl->sdl_data,
762 MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
765 if (ifam->ifam_type != RTM_NEWADDR) {
766 logbad(1,"ifinit: out of sync");
769 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
770 (struct sockaddr *)ifam2,
773 /* Prepare for the next address of this interface, which
775 * Do not output RIP or Router-Discovery packets via aliases.
777 memcpy(&ifs, &ifs0, sizeof(ifs));
778 ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
780 if (INFO_IFA(&info) == 0) {
781 if (iff_up(ifs.int_if_flags)) {
782 if (!(prev_complaints & COMP_NOADDR))
783 msglog("%s has no address",
785 complaints |= COMP_NOADDR;
789 if (INFO_IFA(&info)->sa_family != AF_INET) {
790 if (iff_up(ifs.int_if_flags)) {
791 if (!(prev_complaints & COMP_NOT_INET))
792 trace_act("%s: not AF_INET",
794 complaints |= COMP_NOT_INET;
799 ifs.int_addr = S_ADDR(INFO_IFA(&info));
801 if (ntohl(ifs.int_addr)>>24 == 0
802 || ntohl(ifs.int_addr)>>24 == 0xff) {
803 if (iff_up(ifs.int_if_flags)) {
804 if (!(prev_complaints & COMP_BADADDR))
805 msglog("%s has a bad address",
807 complaints |= COMP_BADADDR;
812 if (ifs.int_if_flags & IFF_LOOPBACK) {
813 ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC;
814 ifs.int_dstaddr = ifs.int_addr;
815 ifs.int_mask = HOST_MASK;
816 ifs.int_ripv1_mask = HOST_MASK;
817 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
818 ifs.int_net = ntohl(ifs.int_dstaddr);
819 if (!foundloopback) {
821 loopaddr = ifs.int_addr;
822 loop_rts.rts_gate = loopaddr;
823 loop_rts.rts_router = loopaddr;
826 } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
827 if (INFO_BRD(&info) == 0
828 || INFO_BRD(&info)->sa_family != AF_INET) {
829 if (iff_up(ifs.int_if_flags)) {
830 if (!(prev_complaints & COMP_NODST))
831 msglog("%s has a bad"
832 " destination address",
834 complaints |= COMP_NODST;
838 ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
839 if (ntohl(ifs.int_dstaddr)>>24 == 0
840 || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
841 if (iff_up(ifs.int_if_flags)) {
842 if (!(prev_complaints & COMP_NODST))
843 msglog("%s has a bad"
844 " destination address",
846 complaints |= COMP_NODST;
850 ifs.int_mask = HOST_MASK;
851 ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
852 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
853 ifs.int_net = ntohl(ifs.int_dstaddr);
856 if (INFO_MASK(&info) == 0) {
857 if (iff_up(ifs.int_if_flags)) {
858 if (!(prev_complaints & COMP_NOMASK))
859 msglog("%s has no netmask",
861 complaints |= COMP_NOMASK;
865 ifs.int_dstaddr = ifs.int_addr;
866 ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
867 ifs.int_ripv1_mask = ifs.int_mask;
868 ifs.int_std_mask = std_mask(ifs.int_addr);
869 ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
870 if (ifs.int_mask != ifs.int_std_mask)
871 ifs.int_state |= IS_SUBNET;
873 if (ifs.int_if_flags & IFF_BROADCAST) {
874 if (INFO_BRD(&info) == 0) {
875 if (iff_up(ifs.int_if_flags)) {
876 if (!(prev_complaints
879 "no broadcast address",
881 complaints |= COMP_NOBADR;
885 ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
888 ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
889 ifs.int_std_addr = htonl(ifs.int_std_net);
891 /* Use a minimum metric of one. Treat the interface metric
892 * (default 0) as an increment to the hop count of one.
894 * The metric obtained from the routing socket dump of
895 * interface addresses is wrong. It is not set by the
896 * SIOCSIFMETRIC ioctl.
899 strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
900 if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
901 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
904 ifs.int_metric = ifr.ifr_metric;
907 ifs.int_metric = ifam->ifam_metric;
909 if (ifs.int_metric > HOPCNT_INFINITY) {
911 if (!(prev_complaints & COMP_BAD_METRIC)
912 && iff_up(ifs.int_if_flags)) {
913 complaints |= COMP_BAD_METRIC;
914 msglog("%s has a metric of %d",
915 ifs.int_name, ifs.int_metric);
919 /* See if this is a familiar interface.
920 * If so, stop worrying about it if it is the same.
921 * Start it over if it now is to somewhere else, as happens
922 * frequently with PPP and SLIP.
924 ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
928 ifp->int_state |= IS_CHECKED;
930 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
935 || 0 != ((ifp->int_state ^ ifs.int_state)
937 || ifp->int_addr != ifs.int_addr
938 || ifp->int_brdaddr != ifs.int_brdaddr
939 || ifp->int_dstaddr != ifs.int_dstaddr
940 || ifp->int_mask != ifs.int_mask
941 || ifp->int_metric != ifs.int_metric) {
942 /* Forget old information about
943 * a changed interface.
945 trace_act("interface %s has changed",
953 /* The primary representative of an alias worries
954 * about how things are working.
956 if (ifp->int_state & IS_ALIAS)
959 /* note interfaces that have been turned off
961 if (!iff_up(ifs.int_if_flags)) {
962 if (iff_up(ifp->int_if_flags)) {
963 msglog("interface %s to %s turned off",
965 naddr_ntoa(ifp->int_dstaddr));
967 ifp->int_if_flags &= ~IFF_UP;
968 } else if (now.tv_sec>(ifp->int_data.ts
969 + CHECK_BAD_INTERVAL)) {
970 trace_act("interface %s has been off"
971 " %ld seconds; forget it",
973 now.tv_sec-ifp->int_data.ts);
978 /* or that were off and are now ok */
979 if (!iff_up(ifp->int_if_flags)) {
980 ifp->int_if_flags |= IFF_UP;
984 /* If it has been long enough,
985 * see if the interface is broken.
987 if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
990 in = ifs.int_data.ipackets - ifp->int_data.ipackets;
991 ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
992 out = ifs.int_data.opackets - ifp->int_data.opackets;
993 oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
995 /* Through at least IRIX 6.2, PPP and SLIP
996 * count packets dropped by the filters.
997 * But FDDI rings stuck non-operational count
998 * dropped packets as they wait for improvement.
1000 if (!(ifp->int_if_flags & IFF_POINTOPOINT))
1001 oerr += (ifs.int_data.odrops
1002 - ifp->int_data.odrops);
1004 /* If the interface just awoke, restart the counters.
1006 if (ifp->int_data.ts == 0) {
1007 ifp->int_data = ifs.int_data;
1010 ifp->int_data = ifs.int_data;
1012 /* Withhold judgment when the short error
1013 * counters wrap or the interface is reset.
1015 if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
1016 LIM_SEC(ifinit_timer,
1017 now.tv_sec+CHECK_BAD_INTERVAL);
1021 /* Withhold judgement when there is no traffic
1023 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1026 /* It is bad if input or output is not working.
1027 * Require presistent problems before marking it dead.
1029 if ((in <= ierr && ierr > 0)
1030 || (out <= oerr && oerr > 0)) {
1031 if (!(ifp->int_state & IS_SICK)) {
1032 trace_act("interface %s to %s"
1033 " sick: in=%d ierr=%d"
1036 naddr_ntoa(ifp->int_dstaddr),
1037 in, ierr, out, oerr);
1041 if (!(ifp->int_state & IS_BROKE)) {
1042 msglog("interface %s to %s broken:"
1043 " in=%d ierr=%d out=%d oerr=%d",
1045 naddr_ntoa(ifp->int_dstaddr),
1046 in, ierr, out, oerr);
1052 /* otherwise, it is active and healthy
1054 ifp->int_act_time = now.tv_sec;
1059 /* This is a new interface.
1060 * If it is dead, forget it.
1062 if (!iff_up(ifs.int_if_flags))
1065 /* If it duplicates an existing interface,
1066 * complain about it, mark the other one
1067 * duplicated, and forget this one.
1069 ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1072 /* Ignore duplicates of itself, caused by having
1073 * IP aliases on the same network.
1075 if (!strcmp(ifp->int_name, ifs.int_name))
1078 if (!(prev_complaints & COMP_DUP)) {
1079 complaints |= COMP_DUP;
1080 msglog("%s (%s%s%s) is duplicated by"
1083 addrname(ifs.int_addr,ifs.int_mask,1),
1084 ((ifs.int_if_flags & IFF_POINTOPOINT)
1086 ((ifs.int_if_flags & IFF_POINTOPOINT)
1087 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1089 addrname(ifp->int_addr,ifp->int_mask,1),
1090 ((ifp->int_if_flags & IFF_POINTOPOINT)
1092 ((ifp->int_if_flags & IFF_POINTOPOINT)
1093 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1095 ifp->int_state |= IS_DUP;
1099 if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST))
1100 && !(ifs.int_state & IS_PASSIVE)) {
1101 trace_act("%s is neither broadcast, point-to-point,"
1104 if (!(ifs.int_state & IFF_MULTICAST))
1105 ifs.int_state |= IS_NO_RDISC;
1109 /* It is new and ok. Add it to the list of interfaces
1111 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1112 memcpy(ifp, &ifs, sizeof(*ifp));
1115 trace_if("Add", ifp);
1117 /* Notice likely bad netmask.
1119 if (!(prev_complaints & COMP_NETMASK)
1120 && !(ifp->int_if_flags & IFF_POINTOPOINT)
1121 && ifp->int_addr != RIP_DEFAULT) {
1122 for (ifp1 = ifnet; NULL != ifp1; ifp1 = ifp1->int_next) {
1123 if (ifp1->int_mask == ifp->int_mask)
1125 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1127 if (ifp1->int_dstaddr == RIP_DEFAULT)
1129 /* ignore aliases on the right network */
1130 if (!strcmp(ifp->int_name, ifp1->int_name))
1132 if (on_net(ifp->int_dstaddr,
1133 ifp1->int_net, ifp1->int_mask)
1134 || on_net(ifp1->int_dstaddr,
1135 ifp->int_net, ifp->int_mask)) {
1136 msglog("possible netmask problem"
1137 " between %s:%s and %s:%s",
1139 addrname(htonl(ifp->int_net),
1142 addrname(htonl(ifp1->int_net),
1143 ifp1->int_mask, 1));
1144 complaints |= COMP_NETMASK;
1149 if (!(ifp->int_state & IS_ALIAS)) {
1150 /* Count the # of directly connected networks.
1152 if (!(ifp->int_if_flags & IFF_LOOPBACK))
1154 if (!IS_RIP_OFF(ifp->int_state))
1157 /* turn on router discovery and RIP If needed */
1163 /* If we are multi-homed and have at least two interfaces
1164 * listening to RIP, then output by default.
1166 if (!supplier_set && rip_interfaces > 1)
1169 /* If we are multi-homed, optionally advertise a route to
1173 || (tot_interfaces > 1
1175 && (ifp = ifwithaddr(myaddr, 0, 0)) != NULL
1176 && foundloopback)) {
1177 advertise_mhome = 1;
1178 rt = rtget(myaddr, HOST_MASK);
1180 if (rt->rt_ifp != ifp
1181 || rt->rt_router != loopaddr) {
1185 loop_rts.rts_ifp = ifp;
1186 loop_rts.rts_metric = 0;
1187 loop_rts.rts_time = rt->rt_time;
1188 rtchange(rt, rt->rt_state | RS_MHOME,
1193 loop_rts.rts_ifp = ifp;
1194 loop_rts.rts_metric = 0;
1195 rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1199 for (ifp = ifnet; ifp != NULL; ifp = ifp1) {
1200 ifp1 = ifp->int_next; /* because we may delete it */
1202 /* Forget any interfaces that have disappeared.
1204 if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1205 trace_act("interface %s has disappeared",
1211 if ((ifp->int_state & IS_BROKE)
1212 && !(ifp->int_state & IS_PASSIVE))
1213 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1215 /* If we ever have a RIPv1 interface, assume we always will.
1216 * It might come back if it ever goes away.
1218 if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1220 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1224 for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
1225 /* Ensure there is always a network route for interfaces,
1226 * after any dead interfaces have been deleted, which
1227 * might affect routes for point-to-point links.
1229 if (!addrouteforif(ifp))
1232 /* Add routes to the local end of point-to-point interfaces
1235 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1236 && !(ifp->int_state & IS_REMOTE)
1238 /* Delete any routes to the network address through
1239 * foreign routers. Remove even static routes.
1241 del_static(ifp->int_addr, HOST_MASK, 0, 0);
1242 rt = rtget(ifp->int_addr, HOST_MASK);
1243 if (rt != NULL && rt->rt_router != loopaddr) {
1248 if (!(rt->rt_state & RS_LOCAL)
1249 || rt->rt_metric > ifp->int_metric) {
1254 loop_rts.rts_ifp = ifp1;
1255 loop_rts.rts_metric = 0;
1256 loop_rts.rts_time = rt->rt_time;
1257 rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1258 | (RS_IF|RS_LOCAL)),
1261 loop_rts.rts_ifp = ifp;
1262 loop_rts.rts_metric = 0;
1263 rtadd(ifp->int_addr, HOST_MASK,
1264 (RS_IF | RS_LOCAL), &loop_rts);
1269 /* add the authority routes */
1270 for (intnetp = intnets; intnetp!=NULL; intnetp = intnetp->intnet_next) {
1271 rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1273 && !(rt->rt_state & RS_NO_NET_SYN)
1274 && !(rt->rt_state & RS_NET_INT)) {
1279 loop_rts.rts_ifp = 0;
1280 loop_rts.rts_metric = intnetp->intnet_metric-1;
1281 rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1282 RS_NET_SYN | RS_NET_INT, &loop_rts);
1286 prev_complaints = complaints;
1291 check_net_syn(struct interface *ifp)
1293 struct rt_entry *rt;
1294 static struct rt_spare new;
1297 /* Turn on the need to automatically synthesize a network route
1298 * for this interface only if we are running RIPv1 on some other
1299 * interface that is on a different class-A,B,or C network.
1301 if (have_ripv1_out || have_ripv1_in) {
1302 ifp->int_state |= IS_NEED_NET_SYN;
1303 rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1305 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1306 && (!(rt->rt_state & RS_NET_SYN)
1307 || rt->rt_metric > ifp->int_metric)) {
1313 new.rts_gate = ifp->int_addr;
1314 new.rts_router = ifp->int_addr;
1315 new.rts_metric = ifp->int_metric;
1316 rtadd(ifp->int_std_addr, ifp->int_std_mask,
1321 ifp->int_state &= ~IS_NEED_NET_SYN;
1323 rt = rtget(ifp->int_std_addr,
1326 && (rt->rt_state & RS_NET_SYN)
1327 && rt->rt_ifp == ifp)
1333 /* Add route for interface if not currently installed.
1334 * Create route to other end if a point-to-point link,
1335 * otherwise a route to this (sub)network.
1337 int /* 0=bad interface */
1338 addrouteforif(struct interface *ifp)
1340 struct rt_entry *rt;
1341 static struct rt_spare new;
1345 /* skip sick interfaces
1347 if (ifp->int_state & IS_BROKE)
1350 /* If the interface on a subnet, then install a RIPv1 route to
1351 * the network as well (unless it is sick).
1353 if (ifp->int_state & IS_SUBNET)
1356 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1358 : htonl(ifp->int_net));
1361 new.rts_router = ifp->int_addr;
1362 new.rts_gate = ifp->int_addr;
1363 new.rts_metric = ifp->int_metric;
1364 new.rts_time = now.tv_sec;
1366 /* If we are going to send packets to the gateway,
1367 * it must be reachable using our physical interfaces
1369 if ((ifp->int_state & IS_REMOTE)
1370 && !(ifp->int_state & IS_EXTERNAL)
1371 && !check_remote(ifp))
1374 /* We are finished if the correct main interface route exists.
1375 * The right route must be for the right interface, not synthesized
1376 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1378 del_static(dst, ifp->int_mask, 0, 0);
1379 rt = rtget(dst, ifp->int_mask);
1381 if ((rt->rt_ifp != ifp
1382 || rt->rt_router != ifp->int_addr)
1383 && (!(ifp->int_state & IS_DUP)
1385 || (rt->rt_ifp->int_state & IS_BROKE))) {
1389 rtchange(rt, ((rt->rt_state | RS_IF)
1390 & ~(RS_NET_SYN | RS_LOCAL)),
1395 if (ifp->int_transitions++ > 0)
1396 trace_act("re-install interface %s",
1399 rtadd(dst, ifp->int_mask, RS_IF, &new);