2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "pathnames.h"
37 #elif defined(__FreeBSD__)
40 __RCSID("$Revision: 2.27 $");
41 #ident "$Revision: 2.27 $"
44 struct ifhead ifnet = LIST_HEAD_INITIALIZER(ifnet); /* all interfaces */
45 struct ifhead remote_if = LIST_HEAD_INITIALIZER(remote_if); /* remote interfaces */
47 /* hash table for all interfaces, big enough to tolerate ridiculous
48 * numbers of IP aliases. Crazy numbers of aliases such as 7000
49 * still will not do well, but not just in looking up interfaces
52 #define AHASH_LEN 211 /* must be prime */
53 #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN]
54 static struct interface *ahash_tbl[AHASH_LEN];
56 #define BHASH_LEN 211 /* must be prime */
57 #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN]
58 static struct interface *bhash_tbl[BHASH_LEN];
61 /* hash for physical interface names.
62 * Assume there are never more 100 or 200 real interfaces, and that
63 * aliases are put on the end of the hash chains.
66 static struct interface *nhash_tbl[NHASH_LEN];
68 int tot_interfaces; /* # of remote and local interfaces */
69 int rip_interfaces; /* # of interfaces doing RIP */
70 static int foundloopback; /* valid flag for loopaddr */
71 naddr loopaddr; /* our address on loopback */
72 static struct rt_spare loop_rts;
74 struct timeval ifinit_timer;
75 static struct timeval last_ifinit;
76 #define IF_RESCAN_DELAY() (last_ifinit.tv_sec == now.tv_sec \
77 && last_ifinit.tv_usec == now.tv_usec \
78 && timercmp(&ifinit_timer, &now, >))
80 int have_ripv1_out; /* have a RIPv1 interface */
81 static int have_ripv1_in;
84 static void if_bad(struct interface *);
85 static int addrouteforif(struct interface *);
87 static struct interface**
92 for (i = 0; *p != '\0'; p++) {
93 i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1);
96 return &nhash_tbl[i % NHASH_LEN];
100 /* Link a new interface into the lists and hash tables.
103 if_link(struct interface *ifp)
105 struct interface **hifp;
107 LIST_INSERT_HEAD(&ifnet, ifp, int_list);
109 hifp = AHASH(ifp->int_addr);
110 ifp->int_ahash_prev = hifp;
111 if ((ifp->int_ahash = *hifp) != 0)
112 (*hifp)->int_ahash_prev = &ifp->int_ahash;
115 if (ifp->int_if_flags & IFF_BROADCAST) {
116 hifp = BHASH(ifp->int_brdaddr);
117 ifp->int_bhash_prev = hifp;
118 if ((ifp->int_bhash = *hifp) != 0)
119 (*hifp)->int_bhash_prev = &ifp->int_bhash;
123 if (ifp->int_state & IS_REMOTE)
124 LIST_INSERT_HEAD(&remote_if, ifp, remote_list);
126 hifp = nhash(ifp->int_name);
127 if (ifp->int_state & IS_ALIAS) {
128 /* put aliases on the end of the hash chain */
130 hifp = &(*hifp)->int_nhash;
132 ifp->int_nhash_prev = hifp;
133 if ((ifp->int_nhash = *hifp) != 0)
134 (*hifp)->int_nhash_prev = &ifp->int_nhash;
139 /* Find the interface with an address
142 ifwithaddr(naddr addr,
143 int bcast, /* notice IFF_BROADCAST address */
144 int remote) /* include IS_REMOTE interfaces */
146 struct interface *ifp, *possible = 0;
148 remote = (remote == 0) ? IS_REMOTE : 0;
150 for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) {
151 if (ifp->int_addr != addr)
153 if ((ifp->int_state & remote) != 0)
155 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
160 if (possible || !bcast)
163 for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) {
164 if (ifp->int_brdaddr != addr)
166 if ((ifp->int_state & remote) != 0)
168 if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0)
177 /* find the interface with a name
179 static struct interface *
180 ifwithname(char *name, /* "ec0" or whatever */
181 naddr addr) /* 0 or network address */
183 struct interface *ifp;
186 for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) {
187 /* If the network address is not specified,
188 * ignore any alias interfaces. Otherwise, look
189 * for the interface with the target name and address.
191 if (!strcmp(ifp->int_name, name)
192 && ((addr == 0 && !(ifp->int_state & IS_ALIAS))
193 || (ifp->int_addr == addr)))
197 /* If there is no known interface, maybe there is a
198 * new interface. So just once look for new interfaces.
200 if (IF_RESCAN_DELAY())
208 ifwithindex(u_short ifindex,
211 struct interface *ifp;
214 LIST_FOREACH(ifp, &ifnet, int_list) {
215 if (ifp->int_index == ifindex)
219 /* If there is no known interface, maybe there is a
220 * new interface. So just once look for new interfaces.
223 || IF_RESCAN_DELAY())
230 /* Find an interface from which the specified address
231 * should have come from. Used for figuring out which
232 * interface a packet came in on.
237 struct interface *ifp, *maybe;
242 LIST_FOREACH(ifp, &ifnet, int_list) {
243 if (ifp->int_if_flags & IFF_POINTOPOINT) {
244 /* finished with a match */
245 if (ifp->int_dstaddr == addr)
249 /* finished with an exact match */
250 if (ifp->int_addr == addr)
253 /* Look for the longest approximate match.
255 if (on_net(addr, ifp->int_net, ifp->int_mask)
257 || ifp->int_mask > maybe->int_mask))
262 if (maybe != 0 || once || IF_RESCAN_DELAY())
266 /* If there is no known interface, maybe there is a
267 * new interface. So just once look for new interfaces.
274 /* Return the classical netmask for an IP address.
276 naddr /* host byte order */
277 std_mask(naddr addr) /* network byte order */
279 addr = ntohl(addr); /* was a host, not a network */
281 if (addr == 0) /* default route has mask 0 */
284 return IN_CLASSA_NET;
286 return IN_CLASSB_NET;
287 return IN_CLASSC_NET;
291 /* Find the netmask that would be inferred by RIPv1 listeners
292 * on the given interface for a given network.
293 * If no interface is specified, look for the best fitting interface.
296 ripv1_mask_net(naddr addr, /* in network byte order */
297 struct interface *ifp) /* as seen on this interface */
302 if (addr == 0) /* default always has 0 mask */
305 if (ifp != 0 && ifp->int_ripv1_mask != HOST_MASK) {
306 /* If the target network is that of the associated interface
307 * on which it arrived, then use the netmask of the interface.
309 if (on_net(addr, ifp->int_net, ifp->int_std_mask))
310 mask = ifp->int_ripv1_mask;
313 /* Examine all interfaces, and if it the target seems
314 * to have the same network number of an interface, use the
315 * netmask of that interface. If there is more than one
316 * such interface, prefer the interface with the longest
319 LIST_FOREACH(ifp, &ifnet, int_list) {
320 if (on_net(addr, ifp->int_std_net, ifp->int_std_mask)
321 && ifp->int_ripv1_mask > mask
322 && ifp->int_ripv1_mask != HOST_MASK)
323 mask = ifp->int_ripv1_mask;
328 /* check special definitions */
330 for (r1p = r1nets; r1p != 0; r1p = r1p->r1net_next) {
331 if (on_net(addr, r1p->r1net_net, r1p->r1net_match)
332 && r1p->r1net_mask > mask)
333 mask = r1p->r1net_mask;
336 /* Otherwise, make the classic A/B/C guess.
339 mask = std_mask(addr);
347 ripv1_mask_host(naddr addr, /* in network byte order */
348 struct interface *ifp) /* as seen on this interface */
350 naddr mask = ripv1_mask_net(addr, ifp);
353 /* If the computed netmask does not mask the address,
354 * then assume it is a host address
356 if ((ntohl(addr) & ~mask) != 0)
362 /* See if an IP address looks reasonable as a destination.
365 check_dst(naddr addr)
369 if (IN_CLASSA(addr)) {
371 return 1; /* default */
373 addr >>= IN_CLASSA_NSHIFT;
374 return (addr != 0 && addr != IN_LOOPBACKNET);
377 return (IN_CLASSB(addr) || IN_CLASSC(addr));
381 /* See a new interface duplicates an existing interface.
384 check_dup(naddr addr, /* IP address, so network byte order */
385 naddr dstaddr, /* ditto */
386 naddr mask, /* mask, so host byte order */
389 struct interface *ifp;
391 LIST_FOREACH(ifp, &ifnet, int_list) {
392 if (ifp->int_mask != mask)
395 if (!iff_up(ifp->int_if_flags))
398 /* The local address can only be shared with a point-to-point
401 if ((!(ifp->int_state & IS_REMOTE) || !(if_flags & IS_REMOTE))
402 && ifp->int_addr == addr
403 && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0))
406 if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask))
413 /* See that a remote gateway is reachable.
414 * Note that the answer can change as real interfaces come and go.
417 check_remote(struct interface *ifp)
421 /* do not worry about other kinds */
422 if (!(ifp->int_state & IS_REMOTE))
425 rt = rtfind(ifp->int_addr);
428 &&on_net(ifp->int_addr,
429 rt->rt_ifp->int_net, rt->rt_ifp->int_mask))
432 /* the gateway cannot be reached directly from one of our
435 if (!(ifp->int_state & IS_BROKE)) {
436 msglog("unreachable gateway %s in "_PATH_GATEWAYS,
437 naddr_ntoa(ifp->int_addr));
444 /* Delete an interface.
447 ifdel(struct interface *ifp)
449 struct interface *ifp1;
452 trace_if("Del", ifp);
454 ifp->int_state |= IS_BROKE;
456 LIST_REMOVE(ifp, int_list);
457 *ifp->int_ahash_prev = ifp->int_ahash;
458 if (ifp->int_ahash != 0)
459 ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev;
460 *ifp->int_nhash_prev = ifp->int_nhash;
461 if (ifp->int_nhash != 0)
462 ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev;
463 if (ifp->int_if_flags & IFF_BROADCAST) {
464 *ifp->int_bhash_prev = ifp->int_bhash;
465 if (ifp->int_bhash != 0)
466 ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev;
468 if (ifp->int_state & IS_REMOTE)
469 LIST_REMOVE(ifp, remote_list);
471 if (!(ifp->int_state & IS_ALIAS)) {
472 /* delete aliases when the main interface dies
474 LIST_FOREACH(ifp1, &ifnet, int_list) {
476 && !strcmp(ifp->int_name, ifp1->int_name))
480 if ((ifp->int_if_flags & IFF_MULTICAST) && rip_sock >= 0) {
482 struct sockaddr_in *sin;
484 memset(&gr, 0, sizeof(gr));
485 gr.gr_interface = ifp->int_index;
486 sin = (struct sockaddr_in *)&gr.gr_group;
487 sin->sin_family = AF_INET;
489 sin->sin_len = sizeof(struct sockaddr_in);
491 sin->sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
492 if (setsockopt(rip_sock, IPPROTO_IP, MCAST_LEAVE_GROUP,
494 && errno != EADDRNOTAVAIL
496 LOGERR("setsockopt(MCAST_LEAVE_GROUP RIP)");
497 if (rip_sock_mcast == ifp)
500 if (ifp->int_rip_sock >= 0) {
501 (void)close(ifp->int_rip_sock);
502 ifp->int_rip_sock = -1;
507 if (!IS_RIP_OFF(ifp->int_state))
510 /* Zap all routes associated with this interface.
511 * Assume routes just using gateways beyond this interface
512 * will timeout naturally, and have probably already died.
514 (void)rn_walktree(rhead, walk_bad, 0);
516 set_rdisc_mg(ifp, 0);
524 /* Mark an interface ill.
527 if_sick(struct interface *ifp)
529 if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) {
530 ifp->int_state |= IS_SICK;
531 ifp->int_act_time = NEVER;
532 trace_if("Chg", ifp);
534 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
539 /* Mark an interface dead.
542 if_bad(struct interface *ifp)
544 struct interface *ifp1;
547 if (ifp->int_state & IS_BROKE)
550 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
552 ifp->int_state |= (IS_BROKE | IS_SICK);
553 ifp->int_act_time = NEVER;
554 ifp->int_query_time = NEVER;
555 ifp->int_data.ts = now.tv_sec;
557 trace_if("Chg", ifp);
559 if (!(ifp->int_state & IS_ALIAS)) {
560 LIST_FOREACH(ifp1, &ifnet, int_list) {
562 && !strcmp(ifp->int_name, ifp1->int_name))
565 (void)rn_walktree(rhead, walk_bad, 0);
571 /* Mark an interface alive
573 int /* 1=it was dead */
574 if_ok(struct interface *ifp,
577 struct interface *ifp1;
580 if (!(ifp->int_state & IS_BROKE)) {
581 if (ifp->int_state & IS_SICK) {
582 trace_act("%sinterface %s to %s working better",
584 ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
585 ifp->int_state &= ~IS_SICK;
590 msglog("%sinterface %s to %s restored",
591 type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr));
592 ifp->int_state &= ~(IS_BROKE | IS_SICK);
593 ifp->int_data.ts = 0;
595 if (!(ifp->int_state & IS_ALIAS)) {
596 LIST_FOREACH(ifp1, &ifnet, int_list) {
598 && !strcmp(ifp->int_name, ifp1->int_name))
604 if (ifp->int_state & IS_REMOTE) {
605 if (!addrouteforif(ifp))
612 /* disassemble routing message
615 rt_xaddrs(struct rt_addrinfo *info,
617 struct sockaddr *lim,
622 static struct sockaddr sa_zero;
625 memset(info, 0, sizeof(*info));
626 info->rti_addrs = addrs;
627 for (i = 0; i < RTAX_MAX && sa < lim; i++) {
628 if ((addrs & (1 << i)) == 0)
630 info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero;
631 sa = (struct sockaddr *)((char*)(sa) + SA_SIZE(sa));
636 /* Find the network interfaces which have configured themselves.
637 * This must be done regularly, if only for extra addresses
638 * that come and go on interfaces.
643 static struct ifa_msghdr *sysctl_buf;
644 static size_t sysctl_buf_size = 0;
646 static u_int prev_complaints = 0;
647 # define COMP_NOT_INET 0x001
648 # define COMP_NOADDR 0x002
649 # define COMP_BADADDR 0x004
650 # define COMP_NODST 0x008
651 # define COMP_NOBADR 0x010
652 # define COMP_NOMASK 0x020
653 # define COMP_DUP 0x040
654 # define COMP_BAD_METRIC 0x080
655 # define COMP_NETMASK 0x100
657 struct interface ifs, ifs0, *ifp, *ifp1;
661 struct if_msghdr *ifm;
663 struct ifa_msghdr *ifam, *ifam2;
664 int in, ierr, out, oerr;
665 struct intnet *intnetp;
666 struct rt_addrinfo info;
673 ifinit_timer.tv_sec = now.tv_sec + (supplier
675 : CHECK_QUIET_INTERVAL);
677 /* mark all interfaces so we can get rid of those that disappear */
678 LIST_FOREACH(ifp, &ifnet, int_list)
679 ifp->int_state &= ~(IS_CHECKED | IS_DUP);
681 /* Fetch the interface list, without too many system calls
682 * since we do it repeatedly.
688 mib[4] = NET_RT_IFLIST;
691 if ((needed = sysctl_buf_size) != 0) {
692 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
694 /* retry if the table grew */
695 if (errno != ENOMEM && errno != EFAULT)
696 BADERR(1, "ifinit: sysctl(RT_IFLIST)");
700 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
701 BADERR(1,"ifinit: sysctl(RT_IFLIST) estimate");
702 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
706 /* XXX: thanks to malloc(3), alignment can be presumed OK */
707 ifam_lim = (char *)sysctl_buf + needed;
708 for (ifam = sysctl_buf; (void *)ifam < ifam_lim; ifam = ifam2) {
710 ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen);
713 if (ifam->ifam_type == RTM_OIFINFO)
714 continue; /* just ignore compat message */
716 if (ifam->ifam_type == RTM_IFINFO) {
717 struct sockaddr_dl *sdl;
719 ifm = (struct if_msghdr *)ifam;
720 /* make prototype structure for the IP aliases
722 memset(&ifs0, 0, sizeof(ifs0));
723 ifs0.int_rip_sock = -1;
724 ifs0.int_index = ifm->ifm_index;
725 ifs0.int_if_flags = ifm->ifm_flags;
726 ifs0.int_state = IS_CHECKED;
727 ifs0.int_query_time = NEVER;
728 ifs0.int_act_time = now.tv_sec;
729 ifs0.int_data.ts = now.tv_sec;
730 ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets;
731 ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors;
732 ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets;
733 ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors;
735 ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops;
737 sdl = (struct sockaddr_dl *)(ifm + 1);
738 sdl->sdl_data[sdl->sdl_nlen] = 0;
739 strncpy(ifs0.int_name, sdl->sdl_data,
740 MIN(sizeof(ifs0.int_name), sdl->sdl_nlen));
743 if (ifam->ifam_type != RTM_NEWADDR) {
744 logbad(1,"ifinit: out of sync");
747 rt_xaddrs(&info, (struct sockaddr *)(ifam+1),
748 (struct sockaddr *)ifam2,
751 /* Prepare for the next address of this interface, which
753 * Do not output RIP or Router-Discovery packets via aliases.
755 memcpy(&ifs, &ifs0, sizeof(ifs));
756 ifs0.int_state |= (IS_ALIAS | IS_NO_RIP_OUT | IS_NO_RDISC);
758 if (INFO_IFA(&info) == 0) {
759 if (iff_up(ifs.int_if_flags)) {
760 if (!(prev_complaints & COMP_NOADDR))
761 msglog("%s has no address",
763 complaints |= COMP_NOADDR;
767 if (INFO_IFA(&info)->sa_family != AF_INET) {
768 if (iff_up(ifs.int_if_flags)) {
769 if (!(prev_complaints & COMP_NOT_INET))
770 trace_act("%s: not AF_INET",
772 complaints |= COMP_NOT_INET;
777 ifs.int_addr = S_ADDR(INFO_IFA(&info));
779 if (ntohl(ifs.int_addr)>>24 == 0
780 || ntohl(ifs.int_addr)>>24 == 0xff) {
781 if (iff_up(ifs.int_if_flags)) {
782 if (!(prev_complaints & COMP_BADADDR))
783 msglog("%s has a bad address",
785 complaints |= COMP_BADADDR;
790 if (ifs.int_if_flags & IFF_LOOPBACK) {
791 ifs.int_state |= IS_NO_RIP | IS_NO_RDISC;
792 if (ifs.int_addr == htonl(INADDR_LOOPBACK))
793 ifs.int_state |= IS_PASSIVE;
794 ifs.int_dstaddr = ifs.int_addr;
795 ifs.int_mask = HOST_MASK;
796 ifs.int_ripv1_mask = HOST_MASK;
797 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
798 ifs.int_net = ntohl(ifs.int_dstaddr);
799 if (!foundloopback) {
801 loopaddr = ifs.int_addr;
802 loop_rts.rts_gate = loopaddr;
803 loop_rts.rts_router = loopaddr;
806 } else if (ifs.int_if_flags & IFF_POINTOPOINT) {
807 if (INFO_BRD(&info) == 0
808 || INFO_BRD(&info)->sa_family != AF_INET) {
809 if (iff_up(ifs.int_if_flags)) {
810 if (!(prev_complaints & COMP_NODST))
811 msglog("%s has a bad"
812 " destination address",
814 complaints |= COMP_NODST;
818 ifs.int_dstaddr = S_ADDR(INFO_BRD(&info));
819 if (ntohl(ifs.int_dstaddr)>>24 == 0
820 || ntohl(ifs.int_dstaddr)>>24 == 0xff) {
821 if (iff_up(ifs.int_if_flags)) {
822 if (!(prev_complaints & COMP_NODST))
823 msglog("%s has a bad"
824 " destination address",
826 complaints |= COMP_NODST;
830 ifs.int_mask = HOST_MASK;
831 ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info)));
832 ifs.int_std_mask = std_mask(ifs.int_dstaddr);
833 ifs.int_net = ntohl(ifs.int_dstaddr);
836 if (INFO_MASK(&info) == 0) {
837 if (iff_up(ifs.int_if_flags)) {
838 if (!(prev_complaints & COMP_NOMASK))
839 msglog("%s has no netmask",
841 complaints |= COMP_NOMASK;
845 ifs.int_dstaddr = ifs.int_addr;
846 ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info)));
847 ifs.int_ripv1_mask = ifs.int_mask;
848 ifs.int_std_mask = std_mask(ifs.int_addr);
849 ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask;
850 if (ifs.int_mask != ifs.int_std_mask)
851 ifs.int_state |= IS_SUBNET;
853 if (ifs.int_if_flags & IFF_BROADCAST) {
854 if (INFO_BRD(&info) == 0) {
855 if (iff_up(ifs.int_if_flags)) {
856 if (!(prev_complaints
859 "no broadcast address",
861 complaints |= COMP_NOBADR;
865 ifs.int_brdaddr = S_ADDR(INFO_BRD(&info));
868 ifs.int_std_net = ifs.int_net & ifs.int_std_mask;
869 ifs.int_std_addr = htonl(ifs.int_std_net);
871 /* Use a minimum metric of one. Treat the interface metric
872 * (default 0) as an increment to the hop count of one.
874 * The metric obtained from the routing socket dump of
875 * interface addresses is wrong. It is not set by the
876 * SIOCSIFMETRIC ioctl.
879 strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name));
880 if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) {
881 DBGERR(1, "ioctl(SIOCGIFMETRIC)");
884 ifs.int_metric = ifr.ifr_metric;
887 ifs.int_metric = ifam->ifam_metric;
889 if (ifs.int_metric > HOPCNT_INFINITY) {
891 if (!(prev_complaints & COMP_BAD_METRIC)
892 && iff_up(ifs.int_if_flags)) {
893 complaints |= COMP_BAD_METRIC;
894 msglog("%s has a metric of %d",
895 ifs.int_name, ifs.int_metric);
899 /* See if this is a familiar interface.
900 * If so, stop worrying about it if it is the same.
901 * Start it over if it now is to somewhere else, as happens
902 * frequently with PPP and SLIP.
904 ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS)
908 ifp->int_state |= IS_CHECKED;
910 if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags)
915 || 0 != ((ifp->int_state ^ ifs.int_state)
917 || ifp->int_addr != ifs.int_addr
918 || ifp->int_brdaddr != ifs.int_brdaddr
919 || ifp->int_dstaddr != ifs.int_dstaddr
920 || ifp->int_mask != ifs.int_mask
921 || ifp->int_metric != ifs.int_metric) {
922 /* Forget old information about
923 * a changed interface.
925 trace_act("interface %s has changed",
933 /* The primary representative of an alias worries
934 * about how things are working.
936 if (ifp->int_state & IS_ALIAS)
939 /* note interfaces that have been turned off
941 if (!iff_up(ifs.int_if_flags)) {
942 if (iff_up(ifp->int_if_flags)) {
943 msglog("interface %s to %s turned off",
945 naddr_ntoa(ifp->int_dstaddr));
947 ifp->int_if_flags &= ~IFF_UP;
948 } else if (now.tv_sec>(ifp->int_data.ts
949 + CHECK_BAD_INTERVAL)) {
950 trace_act("interface %s has been off"
951 " %ld seconds; forget it",
959 /* or that were off and are now ok */
960 if (!iff_up(ifp->int_if_flags)) {
961 ifp->int_if_flags |= IFF_UP;
962 (void)if_ok(ifp, "");
965 /* If it has been long enough,
966 * see if the interface is broken.
968 if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL)
971 in = ifs.int_data.ipackets - ifp->int_data.ipackets;
972 ierr = ifs.int_data.ierrors - ifp->int_data.ierrors;
973 out = ifs.int_data.opackets - ifp->int_data.opackets;
974 oerr = ifs.int_data.oerrors - ifp->int_data.oerrors;
976 /* Through at least IRIX 6.2, PPP and SLIP
977 * count packets dropped by the filters.
978 * But FDDI rings stuck non-operational count
979 * dropped packets as they wait for improvement.
981 if (!(ifp->int_if_flags & IFF_POINTOPOINT))
982 oerr += (ifs.int_data.odrops
983 - ifp->int_data.odrops);
985 /* If the interface just awoke, restart the counters.
987 if (ifp->int_data.ts == 0) {
988 ifp->int_data = ifs.int_data;
991 ifp->int_data = ifs.int_data;
993 /* Withhold judgment when the short error
994 * counters wrap or the interface is reset.
996 if (ierr < 0 || in < 0 || oerr < 0 || out < 0) {
997 LIM_SEC(ifinit_timer,
998 now.tv_sec+CHECK_BAD_INTERVAL);
1002 /* Withhold judgement when there is no traffic
1004 if (in == 0 && out == 0 && ierr == 0 && oerr == 0)
1007 /* It is bad if input or output is not working.
1008 * Require presistent problems before marking it dead.
1010 if ((in <= ierr && ierr > 0)
1011 || (out <= oerr && oerr > 0)) {
1012 if (!(ifp->int_state & IS_SICK)) {
1013 trace_act("interface %s to %s"
1014 " sick: in=%d ierr=%d"
1017 naddr_ntoa(ifp->int_dstaddr),
1018 in, ierr, out, oerr);
1022 if (!(ifp->int_state & IS_BROKE)) {
1023 msglog("interface %s to %s broken:"
1024 " in=%d ierr=%d out=%d oerr=%d",
1026 naddr_ntoa(ifp->int_dstaddr),
1027 in, ierr, out, oerr);
1033 /* otherwise, it is active and healthy
1035 ifp->int_act_time = now.tv_sec;
1036 (void)if_ok(ifp, "");
1040 /* This is a new interface.
1041 * If it is dead, forget it.
1043 if (!iff_up(ifs.int_if_flags))
1046 /* If it duplicates an existing interface,
1047 * complain about it, mark the other one
1048 * duplicated, and forget this one.
1050 ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask,
1053 /* Ignore duplicates of itself, caused by having
1054 * IP aliases on the same network.
1056 if (!strcmp(ifp->int_name, ifs.int_name))
1059 if (!(prev_complaints & COMP_DUP)) {
1060 complaints |= COMP_DUP;
1061 msglog("%s (%s%s%s) is duplicated by"
1064 addrname(ifs.int_addr,ifs.int_mask,1),
1065 ((ifs.int_if_flags & IFF_POINTOPOINT)
1067 ((ifs.int_if_flags & IFF_POINTOPOINT)
1068 ? naddr_ntoa(ifs.int_dstaddr) : ""),
1070 addrname(ifp->int_addr,ifp->int_mask,1),
1071 ((ifp->int_if_flags & IFF_POINTOPOINT)
1073 ((ifp->int_if_flags & IFF_POINTOPOINT)
1074 ? naddr_ntoa(ifp->int_dstaddr) : ""));
1076 ifp->int_state |= IS_DUP;
1080 if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST | IFF_LOOPBACK))) {
1081 trace_act("%s is neither broadcast, point-to-point,"
1084 if (!(ifs.int_state & IFF_MULTICAST))
1085 ifs.int_state |= IS_NO_RDISC;
1089 /* It is new and ok. Add it to the list of interfaces
1091 ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit ifp");
1092 memcpy(ifp, &ifs, sizeof(*ifp));
1095 trace_if("Add", ifp);
1097 /* Notice likely bad netmask.
1099 if (!(prev_complaints & COMP_NETMASK)
1100 && !(ifp->int_if_flags & IFF_POINTOPOINT)
1101 && ifp->int_addr != RIP_DEFAULT) {
1102 LIST_FOREACH(ifp1, &ifnet, int_list) {
1103 if (ifp1->int_mask == ifp->int_mask)
1105 if (ifp1->int_if_flags & IFF_POINTOPOINT)
1107 if (ifp1->int_dstaddr == RIP_DEFAULT)
1109 /* ignore aliases on the right network */
1110 if (!strcmp(ifp->int_name, ifp1->int_name))
1112 if (on_net(ifp->int_dstaddr,
1113 ifp1->int_net, ifp1->int_mask)
1114 || on_net(ifp1->int_dstaddr,
1115 ifp->int_net, ifp->int_mask)) {
1116 msglog("possible netmask problem"
1117 " between %s:%s and %s:%s",
1119 addrname(htonl(ifp->int_net),
1122 addrname(htonl(ifp1->int_net),
1123 ifp1->int_mask, 1));
1124 complaints |= COMP_NETMASK;
1129 if (!(ifp->int_state & IS_ALIAS)) {
1130 /* Count the # of directly connected networks.
1132 if (!(ifp->int_if_flags & IFF_LOOPBACK))
1134 if (!IS_RIP_OFF(ifp->int_state))
1137 /* turn on router discovery and RIP If needed */
1143 /* If we are multi-homed and have at least two interfaces
1144 * listening to RIP, then output by default.
1146 if (!supplier_set && rip_interfaces > 1)
1149 /* If we are multi-homed, optionally advertise a route to
1153 || (tot_interfaces > 1
1155 && (ifp = ifwithaddr(myaddr, 0, 0)) != 0
1156 && foundloopback)) {
1157 advertise_mhome = 1;
1158 rt = rtget(myaddr, HOST_MASK);
1160 if (rt->rt_ifp != ifp
1161 || rt->rt_router != loopaddr) {
1165 loop_rts.rts_ifp = ifp;
1166 loop_rts.rts_metric = 0;
1167 loop_rts.rts_time = rt->rt_time;
1168 rtchange(rt, rt->rt_state | RS_MHOME,
1173 loop_rts.rts_ifp = ifp;
1174 loop_rts.rts_metric = 0;
1175 rtadd(myaddr, HOST_MASK, RS_MHOME, &loop_rts);
1179 LIST_FOREACH_SAFE(ifp, &ifnet, int_list, ifp1) {
1180 /* Forget any interfaces that have disappeared.
1182 if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) {
1183 trace_act("interface %s has disappeared",
1189 if ((ifp->int_state & IS_BROKE)
1190 && !(ifp->int_state & IS_PASSIVE))
1191 LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL);
1193 /* If we ever have a RIPv1 interface, assume we always will.
1194 * It might come back if it ever goes away.
1196 if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier)
1198 if (!(ifp->int_state & IS_NO_RIPV1_IN))
1202 LIST_FOREACH(ifp, &ifnet, int_list) {
1203 /* Ensure there is always a network route for interfaces,
1204 * after any dead interfaces have been deleted, which
1205 * might affect routes for point-to-point links.
1207 if (!addrouteforif(ifp))
1210 /* Add routes to the local end of point-to-point interfaces
1213 if ((ifp->int_if_flags & IFF_POINTOPOINT)
1214 && !(ifp->int_state & IS_REMOTE)
1216 /* Delete any routes to the network address through
1217 * foreign routers. Remove even static routes.
1219 del_static(ifp->int_addr, HOST_MASK, 0, 0);
1220 rt = rtget(ifp->int_addr, HOST_MASK);
1221 if (rt != 0 && rt->rt_router != loopaddr) {
1226 if (!(rt->rt_state & RS_LOCAL)
1227 || rt->rt_metric > ifp->int_metric) {
1232 loop_rts.rts_ifp = ifp1;
1233 loop_rts.rts_metric = 0;
1234 loop_rts.rts_time = rt->rt_time;
1235 rtchange(rt, ((rt->rt_state & ~RS_NET_SYN)
1236 | (RS_IF|RS_LOCAL)),
1239 loop_rts.rts_ifp = ifp;
1240 loop_rts.rts_metric = 0;
1241 rtadd(ifp->int_addr, HOST_MASK,
1242 (RS_IF | RS_LOCAL), &loop_rts);
1247 /* add the authority routes */
1248 for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) {
1249 rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask);
1251 && !(rt->rt_state & RS_NO_NET_SYN)
1252 && !(rt->rt_state & RS_NET_INT)) {
1257 loop_rts.rts_ifp = 0;
1258 loop_rts.rts_metric = intnetp->intnet_metric-1;
1259 rtadd(intnetp->intnet_addr, intnetp->intnet_mask,
1260 RS_NET_SYN | RS_NET_INT, &loop_rts);
1264 prev_complaints = complaints;
1269 check_net_syn(struct interface *ifp)
1271 struct rt_entry *rt;
1272 static struct rt_spare new;
1275 /* Turn on the need to automatically synthesize a network route
1276 * for this interface only if we are running RIPv1 on some other
1277 * interface that is on a different class-A,B,or C network.
1279 if (have_ripv1_out || have_ripv1_in) {
1280 ifp->int_state |= IS_NEED_NET_SYN;
1281 rt = rtget(ifp->int_std_addr, ifp->int_std_mask);
1283 && 0 == (rt->rt_state & RS_NO_NET_SYN)
1284 && (!(rt->rt_state & RS_NET_SYN)
1285 || rt->rt_metric > ifp->int_metric)) {
1291 new.rts_gate = ifp->int_addr;
1292 new.rts_router = ifp->int_addr;
1293 new.rts_metric = ifp->int_metric;
1294 rtadd(ifp->int_std_addr, ifp->int_std_mask,
1299 ifp->int_state &= ~IS_NEED_NET_SYN;
1301 rt = rtget(ifp->int_std_addr,
1304 && (rt->rt_state & RS_NET_SYN)
1305 && rt->rt_ifp == ifp)
1311 /* Add route for interface if not currently installed.
1312 * Create route to other end if a point-to-point link,
1313 * otherwise a route to this (sub)network.
1315 static int /* 0=bad interface */
1316 addrouteforif(struct interface *ifp)
1318 struct rt_entry *rt;
1319 static struct rt_spare new;
1323 /* skip sick interfaces
1325 if (ifp->int_state & IS_BROKE)
1328 /* If the interface on a subnet, then install a RIPv1 route to
1329 * the network as well (unless it is sick).
1331 if (ifp->int_state & IS_SUBNET)
1334 dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK))
1336 : htonl(ifp->int_net));
1339 new.rts_router = ifp->int_addr;
1340 new.rts_gate = ifp->int_addr;
1341 new.rts_metric = ifp->int_metric;
1342 new.rts_time = now.tv_sec;
1344 /* If we are going to send packets to the gateway,
1345 * it must be reachable using our physical interfaces
1347 if ((ifp->int_state & IS_REMOTE)
1348 && !(ifp->int_state & IS_EXTERNAL)
1349 && !check_remote(ifp))
1352 /* We are finished if the correct main interface route exists.
1353 * The right route must be for the right interface, not synthesized
1354 * from a subnet, be a "gateway" or not as appropriate, and so forth.
1356 del_static(dst, ifp->int_mask, 0, 0);
1357 rt = rtget(dst, ifp->int_mask);
1359 if ((rt->rt_ifp != ifp
1360 || rt->rt_router != ifp->int_addr)
1361 && (!(ifp->int_state & IS_DUP)
1363 || (rt->rt_ifp->int_state & IS_BROKE))) {
1367 rtchange(rt, ((rt->rt_state | RS_IF)
1368 & ~(RS_NET_SYN | RS_LOCAL)),
1373 if (ifp->int_transitions++ > 0)
1374 trace_act("re-install interface %s",
1377 rtadd(dst, ifp->int_mask, RS_IF, &new);