Index: head/sbin/routed/if.c =================================================================== --- head/sbin/routed/if.c (revision 27502) +++ head/sbin/routed/if.c (revision 27503) @@ -1,1341 +1,1361 @@ /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__) static char sccsid[] = "@(#)if.c 8.1 (Berkeley) 6/5/93"; #elif defined(__NetBSD__) static char rcsid[] = "$NetBSD$"; #endif -#ident "$Revision: 1.23 $" +#ident "$Revision: 1.1.1.5 $" #include "defs.h" #include "pathnames.h" struct interface *ifnet; /* all interfaces */ /* hash table for all interfaces, big enough to tolerate ridiculous * numbers of IP aliases. Crazy numbers of aliases such as 7000 * still will not do well, but not just in looking up interfaces * by name or address. */ #define AHASH_LEN 211 /* must be prime */ #define AHASH(a) &ahash_tbl[(a)%AHASH_LEN] struct interface *ahash_tbl[AHASH_LEN]; #define BHASH_LEN 211 /* must be prime */ #define BHASH(a) &bhash_tbl[(a)%BHASH_LEN] struct interface *bhash_tbl[BHASH_LEN]; struct interface *remote_if; /* remote interfaces */ /* hash for physical interface names. * Assume there are never more 100 or 200 real interfaces, and that * aliases are put on the end of the hash chains. */ #define NHASH_LEN 97 struct interface *nhash_tbl[NHASH_LEN]; int tot_interfaces; /* # of remote and local interfaces */ int rip_interfaces; /* # of interfaces doing RIP */ int foundloopback; /* valid flag for loopaddr */ naddr loopaddr; /* our address on loopback */ struct timeval ifinit_timer; static struct timeval last_ifinit; int have_ripv1_out; /* have a RIPv1 interface */ int have_ripv1_in; static struct interface** nhash(register char *p) { register u_int i; for (i = 0; *p != '\0'; p++) { i = ((i<<1) & 0x7fffffff) | ((i>>31) & 1); i ^= *p; } return &nhash_tbl[i % NHASH_LEN]; } /* Link a new interface into the lists and hash tables. */ void if_link(struct interface *ifp) { struct interface **hifp; ifp->int_prev = &ifnet; ifp->int_next = ifnet; if (ifnet != 0) ifnet->int_prev = &ifp->int_next; ifnet = ifp; hifp = AHASH(ifp->int_addr); ifp->int_ahash_prev = hifp; if ((ifp->int_ahash = *hifp) != 0) (*hifp)->int_ahash_prev = &ifp->int_ahash; *hifp = ifp; if (ifp->int_if_flags & IFF_BROADCAST) { hifp = BHASH(ifp->int_brdaddr); ifp->int_bhash_prev = hifp; if ((ifp->int_bhash = *hifp) != 0) (*hifp)->int_bhash_prev = &ifp->int_bhash; *hifp = ifp; } if (ifp->int_state & IS_REMOTE) { ifp->int_rlink_prev = &remote_if; ifp->int_rlink = remote_if; if (remote_if != 0) remote_if->int_rlink_prev = &ifp->int_rlink; remote_if = ifp; } hifp = nhash(ifp->int_name); if (ifp->int_state & IS_ALIAS) { /* put aliases on the end of the hash chain */ while (*hifp != 0) hifp = &(*hifp)->int_nhash; } ifp->int_nhash_prev = hifp; if ((ifp->int_nhash = *hifp) != 0) (*hifp)->int_nhash_prev = &ifp->int_nhash; *hifp = ifp; } /* Find the interface with an address */ struct interface * ifwithaddr(naddr addr, int bcast, /* notice IFF_BROADCAST address */ int remote) /* include IS_REMOTE interfaces */ { struct interface *ifp, *possible = 0; remote = (remote == 0) ? IS_REMOTE : 0; for (ifp = *AHASH(addr); ifp; ifp = ifp->int_ahash) { if (ifp->int_addr != addr) continue; if ((ifp->int_state & remote) != 0) continue; if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0) return ifp; possible = ifp; } if (possible || !bcast) return possible; for (ifp = *BHASH(addr); ifp; ifp = ifp->int_bhash) { if (ifp->int_brdaddr != addr) continue; if ((ifp->int_state & remote) != 0) continue; if ((ifp->int_state & (IS_BROKE | IS_PASSIVE)) == 0) return ifp; possible = ifp; } return possible; } /* find the interface with a name */ struct interface * ifwithname(char *name, /* "ec0" or whatever */ naddr addr) /* 0 or network address */ { struct interface *ifp; for (;;) { for (ifp = *nhash(name); ifp != 0; ifp = ifp->int_nhash) { /* If the network address is not specified, * ignore any alias interfaces. Otherwise, look * for the interface with the target name and address. */ if (!strcmp(ifp->int_name, name) && ((addr == 0 && !(ifp->int_state & IS_ALIAS)) || (ifp->int_addr == addr))) return ifp; } /* If there is no known interface, maybe there is a * new interface. So just once look for new interfaces. */ if (last_ifinit.tv_sec == now.tv_sec && last_ifinit.tv_usec == now.tv_usec) return 0; ifinit(); } } struct interface * ifwithindex(u_short index) { struct interface *ifp; for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) { if (ifp->int_index == index) return ifp; } return 0; } /* Find an interface from which the specified address * should have come from. Used for figuring out which * interface a packet came in on. */ struct interface * iflookup(naddr addr) { struct interface *ifp, *maybe; maybe = 0; for (;;) { for (ifp = ifnet; ifp; ifp = ifp->int_next) { if (ifp->int_if_flags & IFF_POINTOPOINT) { /* finished with a match */ if (ifp->int_dstaddr == addr) return ifp; } else { /* finished with an exact match */ if (ifp->int_addr == addr) return ifp; /* Look for the longest approximate match. */ if (on_net(addr, ifp->int_net, ifp->int_mask) && (maybe == 0 || ifp->int_mask > maybe->int_mask)) maybe = ifp; } } if (maybe != 0 || (last_ifinit.tv_sec == now.tv_sec && last_ifinit.tv_usec == now.tv_usec)) return maybe; /* If there is no known interface, maybe there is a * new interface. So just once look for new interfaces. */ ifinit(); } } /* Return the classical netmask for an IP address. */ naddr /* host byte order */ std_mask(naddr addr) /* network byte order */ { NTOHL(addr); /* was a host, not a network */ if (addr == 0) /* default route has mask 0 */ return 0; if (IN_CLASSA(addr)) return IN_CLASSA_NET; if (IN_CLASSB(addr)) return IN_CLASSB_NET; return IN_CLASSC_NET; } /* Find the netmask that would be inferred by RIPv1 listeners * on the given interface for a given network. * If no interface is specified, look for the best fitting interface. */ naddr ripv1_mask_net(naddr addr, /* in network byte order */ struct interface *ifp) /* as seen on this interface */ { naddr mask = 0; if (addr == 0) /* default always has 0 mask */ return mask; if (ifp != 0) { /* If the target network is that of the associated interface * on which it arrived, then use the netmask of the interface. */ if (on_net(addr, ifp->int_net, ifp->int_std_mask)) mask = ifp->int_ripv1_mask; } else { /* Examine all interfaces, and if it the target seems * to have the same network number of an interface, use the * netmask of that interface. If there is more than one * such interface, prefer the interface with the longest * match. */ for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { if (on_net(addr, ifp->int_std_net, ifp->int_std_mask) && ifp->int_ripv1_mask > mask) mask = ifp->int_ripv1_mask; } } /* Otherwise, make the classic A/B/C guess. */ if (mask == 0) mask = std_mask(addr); return mask; } naddr ripv1_mask_host(naddr addr, /* in network byte order */ struct interface *ifp) /* as seen on this interface */ { naddr mask = ripv1_mask_net(addr, ifp); /* If the computed netmask does not mask the address, * then assume it is a host address */ if ((ntohl(addr) & ~mask) != 0) mask = HOST_MASK; return mask; } /* See if a IP address looks reasonable as a destination */ int /* 0=bad */ check_dst(naddr addr) { NTOHL(addr); if (IN_CLASSA(addr)) { if (addr == 0) return 1; /* default */ addr >>= IN_CLASSA_NSHIFT; return (addr != 0 && addr != IN_LOOPBACKNET); } return (IN_CLASSB(addr) || IN_CLASSC(addr)); } /* See a new interface duplicates an existing interface. */ struct interface * check_dup(naddr addr, /* IP address, so network byte order */ naddr dstaddr, /* ditto */ naddr mask, /* mask, so host byte order */ int if_flags) { struct interface *ifp; for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) { if (ifp->int_mask != mask) continue; if (!iff_alive(ifp->int_if_flags)) continue; /* The local address can only be shared with a point-to- * point link. */ if (ifp->int_addr == addr && (((if_flags|ifp->int_if_flags) & IFF_POINTOPOINT) == 0)) return ifp; if (on_net(ifp->int_dstaddr, ntohl(dstaddr),mask)) return ifp; } return 0; } /* See that a remote gateway is reachable. * Note that the answer can change as real interfaces come and go. */ int /* 0=bad */ check_remote(struct interface *ifp) { struct rt_entry *rt; /* do not worry about other kinds */ if (!(ifp->int_state & IS_REMOTE)) return 1; rt = rtfind(ifp->int_addr); if (rt != 0 && rt->rt_ifp != 0 &&on_net(ifp->int_addr, rt->rt_ifp->int_net, rt->rt_ifp->int_mask)) return 1; /* the gateway cannot be reached directly from one of our * interfaces */ if (!(ifp->int_state & IS_BROKE)) { msglog("unreachable gateway %s in "_PATH_GATEWAYS, naddr_ntoa(ifp->int_addr)); if_bad(ifp); } return 0; } /* Delete an interface. */ static void ifdel(struct interface *ifp) { struct ip_mreq m; struct interface *ifp1; trace_if("Del", ifp); ifp->int_state |= IS_BROKE; /* unlink the interface */ *ifp->int_prev = ifp->int_next; if (ifp->int_next != 0) ifp->int_next->int_prev = ifp->int_prev; *ifp->int_ahash_prev = ifp->int_ahash; if (ifp->int_ahash != 0) ifp->int_ahash->int_ahash_prev = ifp->int_ahash_prev; *ifp->int_nhash_prev = ifp->int_nhash; if (ifp->int_nhash != 0) ifp->int_nhash->int_nhash_prev = ifp->int_nhash_prev; if (ifp->int_if_flags & IFF_BROADCAST) { *ifp->int_bhash_prev = ifp->int_bhash; if (ifp->int_bhash != 0) ifp->int_bhash->int_bhash_prev = ifp->int_bhash_prev; } if (ifp->int_state & IS_REMOTE) { *ifp->int_rlink_prev = ifp->int_rlink; if (ifp->int_rlink != 0) ifp->int_rlink->int_rlink_prev = ifp->int_rlink_prev; } if (!(ifp->int_state & IS_ALIAS)) { /* delete aliases when the main interface dies */ for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) { if (ifp1 != ifp && !strcmp(ifp->int_name, ifp1->int_name)) ifdel(ifp1); } if ((ifp->int_if_flags & IFF_MULTICAST) #ifdef MCAST_PPP_BUG && !(ifp->int_if_flags & IFF_POINTOPOINT) #endif && rip_sock >= 0) { m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP); m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT) ? ifp->int_dstaddr : ifp->int_addr); if (setsockopt(rip_sock,IPPROTO_IP,IP_DROP_MEMBERSHIP, &m, sizeof(m)) < 0 && errno != EADDRNOTAVAIL && !TRACEACTIONS) LOGERR("setsockopt(IP_DROP_MEMBERSHIP RIP)"); if (rip_sock_mcast == ifp) rip_sock_mcast = 0; } if (ifp->int_rip_sock >= 0) { (void)close(ifp->int_rip_sock); ifp->int_rip_sock = -1; fix_select(); } tot_interfaces--; if (!IS_RIP_OFF(ifp->int_state)) rip_interfaces--; /* Zap all routes associated with this interface. * Assume routes just using gateways beyond this interface will * timeout naturally, and have probably already died. */ (void)rn_walktree(rhead, walk_bad, 0); set_rdisc_mg(ifp, 0); if_bad_rdisc(ifp); } free(ifp); } /* Mark an interface ill. */ void if_sick(struct interface *ifp) { if (0 == (ifp->int_state & (IS_SICK | IS_BROKE))) { ifp->int_state |= IS_SICK; ifp->int_act_time = NEVER; trace_if("Chg", ifp); LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL); } } /* Mark an interface dead. */ void if_bad(struct interface *ifp) { struct interface *ifp1; if (ifp->int_state & IS_BROKE) return; LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL); ifp->int_state |= (IS_BROKE | IS_SICK); ifp->int_act_time = NEVER; ifp->int_query_time = NEVER; ifp->int_data.ts = 0; trace_if("Chg", ifp); if (!(ifp->int_state & IS_ALIAS)) { for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) { if (ifp1 != ifp && !strcmp(ifp->int_name, ifp1->int_name)) if_bad(ifp1); } (void)rn_walktree(rhead, walk_bad, 0); if_bad_rdisc(ifp); } } /* Mark an interface alive */ int /* 1=it was dead */ if_ok(struct interface *ifp, char *type) { struct interface *ifp1; if (!(ifp->int_state & IS_BROKE)) { if (ifp->int_state & IS_SICK) { trace_act("%sinterface %s to %s working better", type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr)); ifp->int_state &= ~IS_SICK; } return 0; } msglog("%sinterface %s to %s restored", type, ifp->int_name, naddr_ntoa(ifp->int_dstaddr)); ifp->int_state &= ~(IS_BROKE | IS_SICK); ifp->int_data.ts = 0; if (!(ifp->int_state & IS_ALIAS)) { for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) { if (ifp1 != ifp && !strcmp(ifp->int_name, ifp1->int_name)) if_ok(ifp1, type); } if_ok_rdisc(ifp); } if (ifp->int_state & IS_REMOTE) { if (!addrouteforif(ifp)) return 0; } return 1; } -/* disassemble routing message +#ifdef _HAVE_SA_LEN +static struct sockaddr sa_zero = { sizeof(struct sockaddr), AF_INET }; +#endif +/* + * disassemble routing message + * copied bug for bug from the BSD kernel */ void rt_xaddrs(struct rt_addrinfo *info, struct sockaddr *sa, struct sockaddr *lim, int addrs) { + char *sa_limit; /* next byte after the sockaddr */ int i; -#ifdef _HAVE_SA_LEN - static struct sockaddr sa_zero; -#endif + int len; #ifdef sgi #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) \ : sizeof(__uint64_t)) #else #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \ : sizeof(long)) #endif bzero(info, sizeof(*info)); info->rti_addrs = addrs; for (i = 0; i < RTAX_MAX && sa < lim; i++) { if ((addrs & (1 << i)) == 0) continue; #ifdef _HAVE_SA_LEN - info->rti_info[i] = (sa->sa_len != 0) ? sa : &sa_zero; - sa = (struct sockaddr *)((char*)(sa) - + ROUNDUP(sa->sa_len)); + len = sa->sa_len; + + /* Check the sockaddr doesn't go past the end of the buffer. */ + /* Cope with buggy (malicious?) sender.*/ + if (len) { + sa_limit = ((char*)sa) + len; + if ( sa_limit > (char *)lim ) /* equal is ok */ + return; + } else { + /* + * We allow the last broken sockaddr + * to be replaced by a good null one + * because some old versions of routing stuff + * would do this (4.4 route(1) for example). + * This should go away eventually. + */ + info->rti_info[i] = &sa_zero; + return; /* this one had unknown length */ + } #else - info->rti_info[i] = sa; - sa = (struct sockaddr *)((char*)(sa) - + ROUNDUP(_FAKE_SA_LEN_DST(sa))); + len = _FAKE_SA_LEN_DST(sa); #endif + info->rti_info[i] = sa; + sa = (struct sockaddr *)((char*)(sa) + ROUNDUP(len)); } } /* Find the network interfaces which have configured themselves. * This must be done regularly, if only for extra addresses * that come and go on interfaces. */ void ifinit(void) { static char *sysctl_buf; static size_t sysctl_buf_size = 0; uint complaints = 0; static u_int prev_complaints = 0; # define COMP_NOT_INET 0x001 # define COMP_NOADDR 0x002 # define COMP_BADADDR 0x004 # define COMP_NODST 0x008 # define COMP_NOBADR 0x010 # define COMP_NOMASK 0x020 # define COMP_DUP 0x040 # define COMP_BAD_METRIC 0x080 # define COMP_NETMASK 0x100 struct interface ifs, ifs0, *ifp, *ifp1; struct rt_entry *rt; size_t needed; int mib[6]; struct if_msghdr *ifm; struct ifa_msghdr *ifam, *ifam_lim, *ifam2; int in, ierr, out, oerr; struct intnet *intnetp; struct rt_addrinfo info; #ifdef SIOCGIFMETRIC struct ifreq ifr; #endif last_ifinit = now; ifinit_timer.tv_sec = now.tv_sec + (supplier ? CHECK_ACT_INTERVAL : CHECK_QUIET_INTERVAL); /* mark all interfaces so we can get rid of thost that disappear */ for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) ifp->int_state &= ~(IS_CHECKED | IS_DUP); /* Fetch the interface list, without too many system calls * since we do it repeatedly. */ mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; mib[3] = AF_INET; mib[4] = NET_RT_IFLIST; mib[5] = 0; for (;;) { if ((needed = sysctl_buf_size) != 0) { if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0) break; if (errno != ENOMEM && errno != EFAULT) BADERR(1, "ifinit: get interface table"); free(sysctl_buf); needed = 0; } if (sysctl(mib, 6, 0, &needed, 0, 0) < 0) BADERR(1,"ifinit: route-sysctl-estimate"); sysctl_buf = rtmalloc(sysctl_buf_size = needed, "ifinit"); } ifam_lim = (struct ifa_msghdr *)(sysctl_buf + needed); for (ifam = (struct ifa_msghdr *)sysctl_buf; ifam < ifam_lim; ifam = ifam2) { ifam2 = (struct ifa_msghdr*)((char*)ifam + ifam->ifam_msglen); if (ifam->ifam_type == RTM_IFINFO) { struct sockaddr_dl *sdl; ifm = (struct if_msghdr *)ifam; /* make prototype structure for the IP aliases */ bzero(&ifs0, sizeof(ifs0)); ifs0.int_rip_sock = -1; ifs0.int_index = ifm->ifm_index; ifs0.int_if_flags = ifm->ifm_flags; ifs0.int_state = IS_CHECKED; ifs0.int_query_time = NEVER; ifs0.int_act_time = now.tv_sec; ifs0.int_data.ts = now.tv_sec; ifs0.int_data.ipackets = ifm->ifm_data.ifi_ipackets; ifs0.int_data.ierrors = ifm->ifm_data.ifi_ierrors; ifs0.int_data.opackets = ifm->ifm_data.ifi_opackets; ifs0.int_data.oerrors = ifm->ifm_data.ifi_oerrors; #ifdef sgi ifs0.int_data.odrops = ifm->ifm_data.ifi_odrops; #endif sdl = (struct sockaddr_dl *)(ifm + 1); sdl->sdl_data[sdl->sdl_nlen] = 0; strncpy(ifs0.int_name, sdl->sdl_data, MIN(sizeof(ifs0.int_name), sdl->sdl_nlen)); continue; } if (ifam->ifam_type != RTM_NEWADDR) { logbad(1,"ifinit: out of sync"); continue; } rt_xaddrs(&info, (struct sockaddr *)(ifam+1), (struct sockaddr *)ifam2, ifam->ifam_addrs); /* Prepare for the next address of this interface, which * will be an alias. * Do not output RIP or Router-Discovery packets via aliases. */ bcopy(&ifs0, &ifs, sizeof(ifs)); ifs0.int_state |= (IS_ALIAS | IS_NO_RIP | IS_NO_RDISC); if (INFO_IFA(&info) == 0) { if (iff_alive(ifs.int_if_flags)) { if (!(prev_complaints & COMP_NOADDR)) msglog("%s has no address", ifs.int_name); complaints |= COMP_NOADDR; } continue; } if (INFO_IFA(&info)->sa_family != AF_INET) { if (iff_alive(ifs.int_if_flags)) { if (!(prev_complaints & COMP_NOT_INET)) trace_act("%s: not AF_INET", ifs.int_name); complaints |= COMP_NOT_INET; } continue; } ifs.int_addr = S_ADDR(INFO_IFA(&info)); if (ntohl(ifs.int_addr)>>24 == 0 || ntohl(ifs.int_addr)>>24 == 0xff) { if (iff_alive(ifs.int_if_flags)) { if (!(prev_complaints & COMP_BADADDR)) msglog("%s has a bad address", ifs.int_name); complaints |= COMP_BADADDR; } continue; } if (ifs.int_if_flags & IFF_LOOPBACK) { ifs.int_state |= IS_PASSIVE | IS_NO_RIP | IS_NO_RDISC; ifs.int_dstaddr = ifs.int_addr; ifs.int_mask = HOST_MASK; ifs.int_ripv1_mask = HOST_MASK; ifs.int_std_mask = std_mask(ifs.int_dstaddr); ifs.int_net = ntohl(ifs.int_dstaddr); if (!foundloopback) { foundloopback = 1; loopaddr = ifs.int_addr; } } else if (ifs.int_if_flags & IFF_POINTOPOINT) { if (INFO_BRD(&info) == 0 || INFO_BRD(&info)->sa_family != AF_INET) { if (iff_alive(ifs.int_if_flags)) { if (!(prev_complaints & COMP_NODST)) msglog("%s has a bad" " destination address", ifs.int_name); complaints |= COMP_NODST; } continue; } ifs.int_dstaddr = S_ADDR(INFO_BRD(&info)); if (ntohl(ifs.int_dstaddr)>>24 == 0 || ntohl(ifs.int_dstaddr)>>24 == 0xff) { if (iff_alive(ifs.int_if_flags)) { if (!(prev_complaints & COMP_NODST)) msglog("%s has a bad" " destination address", ifs.int_name); complaints |= COMP_NODST; } continue; } ifs.int_mask = HOST_MASK; ifs.int_ripv1_mask = ntohl(S_ADDR(INFO_MASK(&info))); ifs.int_std_mask = std_mask(ifs.int_dstaddr); ifs.int_net = ntohl(ifs.int_dstaddr); } else { if (INFO_MASK(&info) == 0) { if (iff_alive(ifs.int_if_flags)) { if (!(prev_complaints & COMP_NOMASK)) msglog("%s has no netmask", ifs.int_name); complaints |= COMP_NOMASK; } continue; } ifs.int_dstaddr = ifs.int_addr; ifs.int_mask = ntohl(S_ADDR(INFO_MASK(&info))); ifs.int_ripv1_mask = ifs.int_mask; ifs.int_std_mask = std_mask(ifs.int_addr); ifs.int_net = ntohl(ifs.int_addr) & ifs.int_mask; if (ifs.int_mask != ifs.int_std_mask) ifs.int_state |= IS_SUBNET; if (ifs.int_if_flags & IFF_BROADCAST) { if (INFO_BRD(&info) == 0) { if (iff_alive(ifs.int_if_flags)) { if (!(prev_complaints & COMP_NOBADR)) msglog("%s has" "no broadcast address", ifs.int_name); complaints |= COMP_NOBADR; } continue; } ifs.int_brdaddr = S_ADDR(INFO_BRD(&info)); } } ifs.int_std_net = ifs.int_net & ifs.int_std_mask; ifs.int_std_addr = htonl(ifs.int_std_net); /* Use a minimum metric of one. Treat the interface metric * (default 0) as an increment to the hop count of one. * * The metric obtained from the routing socket dump of * interface addresses is wrong. It is not set by the * SIOCSIFMETRIC ioctl. */ #ifdef SIOCGIFMETRIC strncpy(ifr.ifr_name, ifs.int_name, sizeof(ifr.ifr_name)); if (ioctl(rt_sock, SIOCGIFMETRIC, &ifr) < 0) { DBGERR(1, "ioctl(SIOCGIFMETRIC)"); ifs.int_metric = 0; } else { ifs.int_metric = ifr.ifr_metric; } #else ifs.int_metric = ifam->ifam_metric; #endif if (ifs.int_metric > HOPCNT_INFINITY) { ifs.int_metric = 0; if (!(prev_complaints & COMP_BAD_METRIC) && iff_alive(ifs.int_if_flags)) { complaints |= COMP_BAD_METRIC; msglog("%s has a metric of %d", ifs.int_name, ifs.int_metric); } } /* See if this is a familiar interface. * If so, stop worrying about it if it is the same. * Start it over if it now is to somewhere else, as happens * frequently with PPP and SLIP. */ ifp = ifwithname(ifs.int_name, ((ifs.int_state & IS_ALIAS) ? ifs.int_addr : 0)); if (ifp != 0) { ifp->int_state |= IS_CHECKED; if (0 != ((ifp->int_if_flags ^ ifs.int_if_flags) & (IFF_BROADCAST | IFF_LOOPBACK | IFF_POINTOPOINT | IFF_MULTICAST)) || 0 != ((ifp->int_state ^ ifs.int_state) & IS_ALIAS) || ifp->int_addr != ifs.int_addr || ifp->int_brdaddr != ifs.int_brdaddr || ifp->int_dstaddr != ifs.int_dstaddr || ifp->int_mask != ifs.int_mask || ifp->int_metric != ifs.int_metric) { /* Forget old information about * a changed interface. */ trace_act("interface %s has changed", ifp->int_name); ifdel(ifp); ifp = 0; } } if (ifp != 0) { /* The primary representative of an alias worries * about how things are working. */ if (ifp->int_state & IS_ALIAS) continue; /* note interfaces that have been turned off */ if (!iff_alive(ifs.int_if_flags)) { if (iff_alive(ifp->int_if_flags)) { msglog("interface %s to %s turned off", ifp->int_name, naddr_ntoa(ifp->int_dstaddr)); if_bad(ifp); ifp->int_if_flags &= ~IFF_UP_RUNNING; } continue; } /* or that were off and are now ok */ if (!iff_alive(ifp->int_if_flags)) { ifp->int_if_flags |= IFF_UP_RUNNING; (void)if_ok(ifp, ""); } /* If it has been long enough, * see if the interface is broken. */ if (now.tv_sec < ifp->int_data.ts+CHECK_BAD_INTERVAL) continue; in = ifs.int_data.ipackets - ifp->int_data.ipackets; ierr = ifs.int_data.ierrors - ifp->int_data.ierrors; out = ifs.int_data.opackets - ifp->int_data.opackets; oerr = ifs.int_data.oerrors - ifp->int_data.oerrors; #ifdef sgi /* Through at least IRIX 6.2, PPP and SLIP * count packets dropped by the filters. * But FDDI rings stuck non-operational count * dropped packets as they wait for improvement. */ if (!(ifp->int_if_flags & IFF_POINTOPOINT)) oerr += (ifs.int_data.odrops - ifp->int_data.odrops); #endif /* If the interface just awoke, restart the counters. */ if (ifp->int_data.ts == 0) { ifp->int_data = ifs.int_data; continue; } ifp->int_data = ifs.int_data; /* Withhold judgement when the short error * counters wrap or the interface is reset. */ if (ierr < 0 || in < 0 || oerr < 0 || out < 0) { LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL); continue; } /* Withhold judgement when there is no traffic */ if (in == 0 && out == 0 && ierr == 0 && oerr == 0) continue; /* It is bad if input or output is not working. * Require presistent problems before marking it dead. */ if ((in <= ierr && ierr > 0) || (out <= oerr && oerr > 0)) { if (!(ifp->int_state & IS_SICK)) { trace_act("interface %s to %s" " sick: in=%d ierr=%d" " out=%d oerr=%d", ifp->int_name, naddr_ntoa(ifp->int_dstaddr), in, ierr, out, oerr); if_sick(ifp); continue; } if (!(ifp->int_state & IS_BROKE)) { msglog("interface %s to %s broken:" " in=%d ierr=%d out=%d oerr=%d", ifp->int_name, naddr_ntoa(ifp->int_dstaddr), in, ierr, out, oerr); if_bad(ifp); } continue; } /* otherwise, it is active and healthy */ ifp->int_act_time = now.tv_sec; (void)if_ok(ifp, ""); continue; } /* This is a new interface. * If it is dead, forget it. */ if (!iff_alive(ifs.int_if_flags)) continue; /* If it duplicates an existing interface, * complain about it, mark the other one * duplicated, and forget this one. */ ifp = check_dup(ifs.int_addr,ifs.int_dstaddr,ifs.int_mask, ifs.int_if_flags); if (ifp != 0) { if (!(prev_complaints & COMP_DUP)) { complaints |= COMP_DUP; msglog("%s (%s%s%s) is duplicated by" " %s (%s%s%s)", ifs.int_name, addrname(ifs.int_addr,ifs.int_mask,1), ((ifs.int_if_flags & IFF_POINTOPOINT) ? "-->" : ""), ((ifs.int_if_flags & IFF_POINTOPOINT) ? naddr_ntoa(ifs.int_dstaddr) : ""), ifp->int_name, addrname(ifp->int_addr,ifp->int_mask,1), ((ifp->int_if_flags & IFF_POINTOPOINT) ? "-->" : ""), ((ifp->int_if_flags & IFF_POINTOPOINT) ? naddr_ntoa(ifp->int_dstaddr) : "")); } ifp->int_state |= IS_DUP; continue; } if (0 == (ifs.int_if_flags & (IFF_POINTOPOINT | IFF_BROADCAST)) && !(ifs.int_state & IS_PASSIVE)) { trace_act("%s is neither broadcast, point-to-point," " nor loopback", ifs.int_name); if (!(ifs.int_state & IFF_MULTICAST)) ifs.int_state |= IS_NO_RDISC; } /* It is new and ok. Add it to the list of interfaces */ ifp = (struct interface *)rtmalloc(sizeof(*ifp), "ifinit"); bcopy(&ifs, ifp, sizeof(*ifp)); get_parms(ifp); if_link(ifp); trace_if("Add", ifp); /* Notice likely bad netmask. */ if (!(prev_complaints & COMP_NETMASK) && !(ifp->int_if_flags & IFF_POINTOPOINT) && ifp->int_addr != RIP_DEFAULT) { for (ifp1 = ifnet; 0 != ifp1; ifp1 = ifp1->int_next) { if (ifp1->int_mask == ifp->int_mask) continue; if (ifp1->int_if_flags & IFF_POINTOPOINT) continue; if (ifp1->int_dstaddr == RIP_DEFAULT) continue; if (on_net(ifp->int_dstaddr, ifp1->int_net, ifp1->int_mask) || on_net(ifp1->int_dstaddr, ifp->int_net, ifp->int_mask)) { msglog("possible netmask problem" " between %s:%s and %s:%s", ifp->int_name, addrname(htonl(ifp->int_net), ifp->int_mask, 1), ifp1->int_name, addrname(htonl(ifp1->int_net), ifp1->int_mask, 1)); complaints |= COMP_NETMASK; } } } if (!(ifp->int_state & IS_ALIAS)) { /* Count the # of directly connected networks. */ if (!(ifp->int_if_flags & IFF_LOOPBACK)) tot_interfaces++; if (!IS_RIP_OFF(ifp->int_state)) rip_interfaces++; /* turn on router discovery and RIP If needed */ if_ok_rdisc(ifp); rip_on(ifp); } } /* If we are multi-homed and have at least two interfaces * listening to RIP, then output by default. */ if (!supplier_set && rip_interfaces > 1) set_supplier(); /* If we are multi-homed, optionally advertise a route to * our main address. */ if (advertise_mhome || (tot_interfaces > 1 && mhome && (ifp = ifwithaddr(myaddr, 0, 0)) != 0 && foundloopback)) { advertise_mhome = 1; rt = rtget(myaddr, HOST_MASK); if (rt != 0) { if (rt->rt_ifp != ifp || rt->rt_router != loopaddr) { rtdelete(rt); rt = 0; } else { rtchange(rt, rt->rt_state | RS_MHOME, loopaddr, loopaddr, 0, 0, ifp, rt->rt_time, 0); } } if (rt == 0) rtadd(myaddr, HOST_MASK, loopaddr, loopaddr, 0, 0, RS_MHOME, ifp); } for (ifp = ifnet; ifp != 0; ifp = ifp1) { ifp1 = ifp->int_next; /* because we may delete it */ /* Forget any interfaces that have disappeared. */ if (!(ifp->int_state & (IS_CHECKED | IS_REMOTE))) { trace_act("interface %s has disappeared", ifp->int_name); ifdel(ifp); continue; } if ((ifp->int_state & IS_BROKE) && !(ifp->int_state & IS_PASSIVE)) LIM_SEC(ifinit_timer, now.tv_sec+CHECK_BAD_INTERVAL); /* If we ever have a RIPv1 interface, assume we always will. * It might come back if it ever goes away. */ if (!(ifp->int_state & IS_NO_RIPV1_OUT) && supplier) have_ripv1_out = 1; if (!(ifp->int_state & IS_NO_RIPV1_IN)) have_ripv1_in = 1; } for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { /* Ensure there is always a network route for interfaces, * after any dead interfaces have been deleted, which * might affect routes for point-to-point links. */ if (!addrouteforif(ifp)) continue; /* Add routes to the local end of point-to-point interfaces * using loopback. */ if ((ifp->int_if_flags & IFF_POINTOPOINT) && !(ifp->int_state & IS_REMOTE) && foundloopback) { /* Delete any routes to the network address through * foreign routers. Remove even static routes. */ del_static(ifp->int_addr, HOST_MASK, 0); rt = rtget(ifp->int_addr, HOST_MASK); if (rt != 0 && rt->rt_router != loopaddr) { rtdelete(rt); rt = 0; } if (rt != 0) { if (!(rt->rt_state & RS_LOCAL) || rt->rt_metric > ifp->int_metric) { ifp1 = ifp; } else { ifp1 = rt->rt_ifp; } rtchange(rt,((rt->rt_state & ~RS_NET_SYN) | (RS_IF|RS_LOCAL)), loopaddr, loopaddr, 0, 0, ifp1, rt->rt_time, 0); } else { rtadd(ifp->int_addr, HOST_MASK, loopaddr, loopaddr, 0, 0, (RS_IF | RS_LOCAL), ifp); } } } /* add the authority routes */ for (intnetp = intnets; intnetp!=0; intnetp = intnetp->intnet_next) { rt = rtget(intnetp->intnet_addr, intnetp->intnet_mask); if (rt != 0 && !(rt->rt_state & RS_NO_NET_SYN) && !(rt->rt_state & RS_NET_INT)) { rtdelete(rt); rt = 0; } if (rt == 0) rtadd(intnetp->intnet_addr, intnetp->intnet_mask, loopaddr, loopaddr, intnetp->intnet_metric-1, 0, RS_NET_SYN | RS_NET_INT, 0); } prev_complaints = complaints; } static void check_net_syn(struct interface *ifp) { struct rt_entry *rt; /* Turn on the need to automatically synthesize a network route * for this interface only if we are running RIPv1 on some other * interface that is on a different class-A,B,or C network. */ if (have_ripv1_out || have_ripv1_in) { ifp->int_state |= IS_NEED_NET_SYN; rt = rtget(ifp->int_std_addr, ifp->int_std_mask); if (rt != 0 && 0 == (rt->rt_state & RS_NO_NET_SYN) && (!(rt->rt_state & RS_NET_SYN) || rt->rt_metric > ifp->int_metric)) { rtdelete(rt); rt = 0; } if (rt == 0) rtadd(ifp->int_std_addr, ifp->int_std_mask, ifp->int_addr, ifp->int_addr, ifp->int_metric, 0, RS_NET_SYN, ifp); } else { ifp->int_state &= ~IS_NEED_NET_SYN; rt = rtget(ifp->int_std_addr, ifp->int_std_mask); if (rt != 0 && (rt->rt_state & RS_NET_SYN) && rt->rt_ifp == ifp) rtbad_sub(rt); } } /* Add route for interface if not currently installed. * Create route to other end if a point-to-point link, * otherwise a route to this (sub)network. */ int /* 0=bad interface */ addrouteforif(struct interface *ifp) { struct rt_entry *rt; naddr dst, gate; /* skip sick interfaces */ if (ifp->int_state & IS_BROKE) return 0; /* If the interface on a subnet, then install a RIPv1 route to * the network as well (unless it is sick). */ if (ifp->int_state & IS_SUBNET) check_net_syn(ifp); gate = ifp->int_addr; dst = (0 != (ifp->int_if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) ? ifp->int_dstaddr : htonl(ifp->int_net)); /* If we are going to send packets to the gateway, * it must be reachable using our physical interfaces */ if ((ifp->int_state & IS_REMOTE) && !(ifp->int_state & IS_EXTERNAL) && !check_remote(ifp)) return 0; /* We are finished if the correct main interface route exists. * The right route must be for the right interface, not synthesized * from a subnet, be a "gateway" or not as appropriate, and so forth. */ del_static(dst, ifp->int_mask, 0); rt = rtget(dst, ifp->int_mask); if (rt != 0) { if ((rt->rt_ifp != ifp || rt->rt_router != ifp->int_addr) && (!(ifp->int_state & IS_DUP) || rt->rt_ifp == 0 || (rt->rt_ifp->int_state & IS_BROKE))) { rtdelete(rt); rt = 0; } else { rtchange(rt, ((rt->rt_state | RS_IF) & ~(RS_NET_SYN | RS_LOCAL)), ifp->int_addr, ifp->int_addr, ifp->int_metric, 0, ifp, now.tv_sec, 0); } } if (rt == 0) { if (ifp->int_transitions++ > 0) trace_act("re-install interface %s", ifp->int_name); rtadd(dst, ifp->int_mask, gate, gate, ifp->int_metric, 0, RS_IF, ifp); } return 1; } Index: head/sbin/routed/table.c =================================================================== --- head/sbin/routed/table.c (revision 27502) +++ head/sbin/routed/table.c (revision 27503) @@ -1,2001 +1,1997 @@ /* * Copyright (c) 1983, 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__) static char sccsid[] = "@(#)tables.c 8.1 (Berkeley) 6/5/93"; #elif defined(__NetBSD__) static char rcsid[] = "$NetBSD$"; #endif #include "defs.h" static struct rt_spare *rts_better(struct rt_entry *); struct radix_node_head *rhead; /* root of the radix tree */ int need_flash = 1; /* flash update needed * start =1 to suppress the 1st */ struct timeval age_timer; /* next check of old routes */ struct timeval need_kern = { /* need to update kernel table */ EPOCH+MIN_WAITTIME-1 }; int stopint; int total_routes; /* zap any old routes through this gateway */ naddr age_bad_gate; /* It is desirable to "aggregate" routes, to combine differing routes of * the same metric and next hop into a common route with a smaller netmask * or to suppress redundant routes, routes that add no information to * routes with smaller netmasks. * * A route is redundant if and only if any and all routes with smaller * but matching netmasks and nets are the same. Since routes are * kept sorted in the radix tree, redundant routes always come second. * * There are two kinds of aggregations. First, two routes of the same bit * mask and differing only in the least significant bit of the network * number can be combined into a single route with a coarser mask. * * Second, a route can be suppressed in favor of another route with a more * coarse mask provided no incompatible routes with intermediate masks * are present. The second kind of aggregation involves suppressing routes. * A route must not be suppressed if an incompatible route exists with * an intermediate mask, since the suppressed route would be covered * by the intermediate. * * This code relies on the radix tree walk encountering routes * sorted first by address, with the smallest address first. */ struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest, *ag_finest; /* #define DEBUG_AG */ #ifdef DEBUG_AG #define CHECK_AG() {int acnt = 0; struct ag_info *cag; \ for (cag = ag_avail; cag != 0; cag = cag->ag_fine) \ acnt++; \ for (cag = ag_corsest; cag != 0; cag = cag->ag_fine) \ acnt++; \ if (acnt != NUM_AG_SLOTS) { \ (void)fflush(stderr); \ abort(); \ } \ } #else #define CHECK_AG() #endif /* Output the contents of an aggregation table slot. * This function must always be immediately followed with the deletion * of the target slot. */ static void ag_out(struct ag_info *ag, void (*out)(struct ag_info *)) { struct ag_info *ag_cors; naddr bit; /* If we output both the even and odd twins, then the immediate parent, * if it is present, is redundant, unless the parent manages to * aggregate into something coarser. * On successive calls, this code detects the even and odd twins, * and marks the parent. * * Note that the order in which the radix tree code emits routes * ensures that the twins are seen before the parent is emitted. */ ag_cors = ag->ag_cors; if (ag_cors != 0 && ag_cors->ag_mask == ag->ag_mask<<1 && ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) { ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h) ? AGS_REDUN0 : AGS_REDUN1); } /* Skip it if this route is itself redundant. * * It is ok to change the contents of the slot here, since it is * always deleted next. */ if (ag->ag_state & AGS_REDUN0) { if (ag->ag_state & AGS_REDUN1) return; bit = (-ag->ag_mask) >> 1; ag->ag_dst_h |= bit; ag->ag_mask |= bit; } else if (ag->ag_state & AGS_REDUN1) { bit = (-ag->ag_mask) >> 1; ag->ag_mask |= bit; } out(ag); } static void ag_del(struct ag_info *ag) { CHECK_AG(); if (ag->ag_cors == 0) ag_corsest = ag->ag_fine; else ag->ag_cors->ag_fine = ag->ag_fine; if (ag->ag_fine == 0) ag_finest = ag->ag_cors; else ag->ag_fine->ag_cors = ag->ag_cors; ag->ag_fine = ag_avail; ag_avail = ag; CHECK_AG(); } /* Flush routes waiting for aggretation. * This must not suppress a route unless it is known that among all * routes with coarser masks that match it, the one with the longest * mask is appropriate. This is ensured by scanning the routes * in lexical order, and with the most restritive mask first * among routes to the same destination. */ void ag_flush(naddr lim_dst_h, /* flush routes to here */ naddr lim_mask, /* matching this mask */ void (*out)(struct ag_info *)) { struct ag_info *ag, *ag_cors; naddr dst_h; for (ag = ag_finest; ag != 0 && ag->ag_mask >= lim_mask; ag = ag_cors) { ag_cors = ag->ag_cors; /* work on only the specified routes */ dst_h = ag->ag_dst_h; if ((dst_h & lim_mask) != lim_dst_h) continue; if (!(ag->ag_state & AGS_SUPPRESS)) ag_out(ag, out); else for ( ; ; ag_cors = ag_cors->ag_cors) { /* Look for a route that can suppress the * current route */ if (ag_cors == 0) { /* failed, so output it and look for * another route to work on */ ag_out(ag, out); break; } if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) { /* We found a route with a coarser mask that * aggregates the current target. * * If it has a different next hop, it * cannot replace the target, so output * the target. */ if (ag->ag_gate != ag_cors->ag_gate && !(ag->ag_state & AGS_FINE_GATE) && !(ag_cors->ag_state & AGS_CORS_GATE)) { ag_out(ag, out); break; } /* If the coarse route has a good enough * metric, it suppresses the target. */ if (ag_cors->ag_pref <= ag->ag_pref) { if (ag_cors->ag_seqno > ag->ag_seqno) ag_cors->ag_seqno = ag->ag_seqno; if (AG_IS_REDUN(ag->ag_state) && ag_cors->ag_mask==ag->ag_mask<<1) { if (ag_cors->ag_dst_h == dst_h) ag_cors->ag_state |= AGS_REDUN0; else ag_cors->ag_state |= AGS_REDUN1; } if (ag->ag_tag != ag_cors->ag_tag) ag_cors->ag_tag = 0; if (ag->ag_nhop != ag_cors->ag_nhop) ag_cors->ag_nhop = 0; break; } } } /* That route has either been output or suppressed */ ag_cors = ag->ag_cors; ag_del(ag); } CHECK_AG(); } /* Try to aggregate a route with previous routes. */ void ag_check(naddr dst, naddr mask, naddr gate, naddr nhop, char metric, char pref, u_int seqno, u_short tag, u_short state, void (*out)(struct ag_info *)) /* output using this */ { struct ag_info *ag, *nag, *ag_cors; naddr xaddr; int x; NTOHL(dst); /* Punt non-contiguous subnet masks. * * (X & -X) contains a single bit if and only if X is a power of 2. * (X + (X & -X)) == 0 if and only if X is a power of 2. */ if ((mask & -mask) + mask != 0) { struct ag_info nc_ag; nc_ag.ag_dst_h = dst; nc_ag.ag_mask = mask; nc_ag.ag_gate = gate; nc_ag.ag_nhop = nhop; nc_ag.ag_metric = metric; nc_ag.ag_pref = pref; nc_ag.ag_tag = tag; nc_ag.ag_state = state; nc_ag.ag_seqno = seqno; out(&nc_ag); return; } /* Search for the right slot in the aggregation table. */ ag_cors = 0; ag = ag_corsest; while (ag != 0) { if (ag->ag_mask >= mask) break; /* Suppress old routes (i.e. combine with compatible routes * with coarser masks) as we look for the right slot in the * aggregation table for the new route. * A route to an address less than the current destination * will not be affected by the current route or any route * seen hereafter. That means it is safe to suppress it. * This check keeps poor routes (eg. with large hop counts) * from preventing suppresion of finer routes. */ if (ag_cors != 0 && ag->ag_dst_h < dst && (ag->ag_state & AGS_SUPPRESS) && ag_cors->ag_pref <= ag->ag_pref && (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h && (ag_cors->ag_gate == ag->ag_gate || (ag->ag_state & AGS_FINE_GATE) || (ag_cors->ag_state & AGS_CORS_GATE))) { if (ag_cors->ag_seqno > ag->ag_seqno) ag_cors->ag_seqno = ag->ag_seqno; if (AG_IS_REDUN(ag->ag_state) && ag_cors->ag_mask==ag->ag_mask<<1) { if (ag_cors->ag_dst_h == dst) ag_cors->ag_state |= AGS_REDUN0; else ag_cors->ag_state |= AGS_REDUN1; } if (ag->ag_tag != ag_cors->ag_tag) ag_cors->ag_tag = 0; if (ag->ag_nhop != ag_cors->ag_nhop) ag_cors->ag_nhop = 0; ag_del(ag); CHECK_AG(); } else { ag_cors = ag; } ag = ag_cors->ag_fine; } /* If we find the even/odd twin of the new route, and if the * masks and so forth are equal, we can aggregate them. * We can probably promote one of the pair. * * Since the routes are encountered in lexical order, * the new route must be odd. However, the second or later * times around this loop, it could be the even twin promoted * from the even/odd pair of twins of the finer route. */ while (ag != 0 && ag->ag_mask == mask && ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) { /* Here we know the target route and the route in the current * slot have the same netmasks and differ by at most the * last bit. They are either for the same destination, or * for an even/odd pair of destinations. */ if (ag->ag_dst_h == dst) { /* We have two routes to the same destination. * Routes are encountered in lexical order, so a * route is never promoted until the parent route is * already present. So we know that the new route is * a promoted pair and the route already in the slot * is the explicit route. * * Prefer the best route if their metrics differ, * or the promoted one if not, following a sort * of longest-match rule. */ if (pref <= ag->ag_pref) { ag->ag_gate = gate; ag->ag_nhop = nhop; ag->ag_tag = tag; ag->ag_metric = metric; ag->ag_pref = pref; x = ag->ag_state; ag->ag_state = state; state = x; } /* The sequence number controls flash updating, * and should be the smaller of the two. */ if (ag->ag_seqno > seqno) ag->ag_seqno = seqno; /* some bits are set if they are set on either route */ ag->ag_state |= (state & (AGS_PROMOTE_EITHER | AGS_REDUN0 | AGS_REDUN1)); return; } /* If one of the routes can be promoted and the other can * be suppressed, it may be possible to combine them or * worthwhile to promote one. * * Note that any route that can be promoted is always * marked to be eligible to be suppressed. */ if (!((state & AGS_PROMOTE) && (ag->ag_state & AGS_SUPPRESS)) && !((ag->ag_state & AGS_PROMOTE) && (state & AGS_SUPPRESS))) break; /* A pair of even/odd twin routes can be combined * if either is redundant, or if they are via the * same gateway and have the same metric. */ if (AG_IS_REDUN(ag->ag_state) || AG_IS_REDUN(state) || (ag->ag_gate == gate && ag->ag_pref == pref && (state & ag->ag_state & AGS_PROMOTE) != 0)) { /* We have both the even and odd pairs. * Since the routes are encountered in order, * the route in the slot must be the even twin. * * Combine and promote the pair of routes. */ if (seqno > ag->ag_seqno) seqno = ag->ag_seqno; if (!AG_IS_REDUN(state)) state &= ~AGS_REDUN1; if (AG_IS_REDUN(ag->ag_state)) state |= AGS_REDUN0; else state &= ~AGS_REDUN0; state |= (ag->ag_state & AGS_PROMOTE_EITHER); if (ag->ag_tag != tag) tag = 0; if (ag->ag_nhop != nhop) nhop = 0; /* Get rid of the even twin that was already * in the slot. */ ag_del(ag); } else if (ag->ag_pref >= pref && (ag->ag_state & AGS_PROMOTE)) { /* If we cannot combine the pair, maybe the route * with the worse metric can be promoted. * * Promote the old, even twin, by giving its slot * in the table to the new, odd twin. */ ag->ag_dst_h = dst; xaddr = ag->ag_gate; ag->ag_gate = gate; gate = xaddr; xaddr = ag->ag_nhop; ag->ag_nhop = nhop; nhop = xaddr; x = ag->ag_tag; ag->ag_tag = tag; tag = x; x = ag->ag_state; ag->ag_state = state; state = x; if (!AG_IS_REDUN(state)) state &= ~AGS_REDUN0; x = ag->ag_metric; ag->ag_metric = metric; metric = x; x = ag->ag_pref; ag->ag_pref = pref; pref = x; if (seqno >= ag->ag_seqno) seqno = ag->ag_seqno; else ag->ag_seqno = seqno; } else { if (!(state & AGS_PROMOTE)) break; /* cannot promote either twin */ /* promote the new, odd twin by shaving its * mask and address. */ if (seqno > ag->ag_seqno) seqno = ag->ag_seqno; else ag->ag_seqno = seqno; if (!AG_IS_REDUN(state)) state &= ~AGS_REDUN1; } mask <<= 1; dst &= mask; if (ag_cors == 0) { ag = ag_corsest; break; } ag = ag_cors; ag_cors = ag->ag_cors; } /* When we can no longer promote and combine routes, * flush the old route in the target slot. Also flush * any finer routes that we know will never be aggregated by * the new route. * * In case we moved toward coarser masks, * get back where we belong */ if (ag != 0 && ag->ag_mask < mask) { ag_cors = ag; ag = ag->ag_fine; } /* Empty the target slot */ if (ag != 0 && ag->ag_mask == mask) { ag_flush(ag->ag_dst_h, ag->ag_mask, out); ag = (ag_cors == 0) ? ag_corsest : ag_cors->ag_fine; } #ifdef DEBUG_AG (void)fflush(stderr); if (ag == 0 && ag_cors != ag_finest) abort(); if (ag_cors == 0 && ag != ag_corsest) abort(); if (ag != 0 && ag->ag_cors != ag_cors) abort(); if (ag_cors != 0 && ag_cors->ag_fine != ag) abort(); CHECK_AG(); #endif /* Save the new route on the end of the table. */ nag = ag_avail; ag_avail = nag->ag_fine; nag->ag_dst_h = dst; nag->ag_mask = mask; nag->ag_gate = gate; nag->ag_nhop = nhop; nag->ag_metric = metric; nag->ag_pref = pref; nag->ag_tag = tag; nag->ag_state = state; nag->ag_seqno = seqno; nag->ag_fine = ag; if (ag != 0) ag->ag_cors = nag; else ag_finest = nag; nag->ag_cors = ag_cors; if (ag_cors == 0) ag_corsest = nag; else ag_cors->ag_fine = nag; CHECK_AG(); } static char * rtm_type_name(u_char type) { static char *rtm_types[] = { "RTM_ADD", "RTM_DELETE", "RTM_CHANGE", "RTM_GET", "RTM_LOSING", "RTM_REDIRECT", "RTM_MISS", "RTM_LOCK", "RTM_OLDADD", "RTM_OLDDEL", "RTM_RESOLVE", "RTM_NEWADDR", "RTM_DELADDR", "RTM_IFINFO" }; static char name0[10]; if (type > sizeof(rtm_types)/sizeof(rtm_types[0]) || type == 0) { sprintf(name0, "RTM type %#x", type); return name0; } else { return rtm_types[type-1]; } } /* Trim a mask in a sockaddr - * Produce a length of 0 for an address of 0. - * Otherwise produce the index of the first zero byte. + * Produce the index of the first zero byte. + * i.e. Produce a index of 4 for an mask of 0. (default route) */ void #ifdef _HAVE_SIN_LEN masktrim(struct sockaddr_in *ap) #else masktrim(struct sockaddr_in_new *ap) #endif { register char *cp; - if (ap->sin_addr.s_addr == 0) { - ap->sin_len = 0; - return; - } + ap->sin_port = 0xffff; /* buffer zone for default route */ cp = (char *)(&ap->sin_addr.s_addr+1); while (*--cp == 0) continue; + /*ap->sin_port = 0x0;*/ /* may not be needed (who cares?)*/ ap->sin_len = cp - (char*)ap + 1; } /* Tell the kernel to add, delete or change a route */ static void rtioctl(int action, /* RTM_DELETE, etc */ naddr dst, naddr gate, naddr mask, int metric, int flags) { struct { struct rt_msghdr w_rtm; struct sockaddr_in w_dst; struct sockaddr_in w_gate; #ifdef _HAVE_SA_LEN struct sockaddr_in w_mask; #else struct sockaddr_in_new w_mask; #endif } w; long cc; again: bzero(&w, sizeof(w)); w.w_rtm.rtm_msglen = sizeof(w); w.w_rtm.rtm_version = RTM_VERSION; w.w_rtm.rtm_type = action; w.w_rtm.rtm_flags = flags; w.w_rtm.rtm_seq = ++rt_sock_seqno; w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY; if (metric != 0) { w.w_rtm.rtm_rmx.rmx_hopcount = metric; w.w_rtm.rtm_inits |= RTV_HOPCOUNT; } w.w_dst.sin_family = AF_INET; w.w_dst.sin_addr.s_addr = dst; w.w_gate.sin_family = AF_INET; w.w_gate.sin_addr.s_addr = gate; #ifdef _HAVE_SA_LEN w.w_dst.sin_len = sizeof(w.w_dst); w.w_gate.sin_len = sizeof(w.w_gate); #endif if (mask == HOST_MASK) { w.w_rtm.rtm_flags |= RTF_HOST; w.w_rtm.rtm_msglen -= sizeof(w.w_mask); } else { w.w_rtm.rtm_addrs |= RTA_NETMASK; w.w_mask.sin_addr.s_addr = htonl(mask); #ifdef _HAVE_SA_LEN masktrim(&w.w_mask); - if (w.w_mask.sin_len == 0) - w.w_mask.sin_len = sizeof(long); w.w_rtm.rtm_msglen -= (sizeof(w.w_mask) - w.w_mask.sin_len); #endif } if (TRACEKERNEL) trace_kernel("write kernel %s %s->%s metric=%d flags=%#x\n", rtm_type_name(action), addrname(dst, mask, 0), naddr_ntoa(gate), metric, flags); #ifndef NO_INSTALL cc = write(rt_sock, &w, w.w_rtm.rtm_msglen); if (cc == w.w_rtm.rtm_msglen) return; if (cc < 0) { if (errno == ESRCH && (action == RTM_CHANGE || action == RTM_DELETE)) { trace_act("route to %s disappeared before %s", addrname(dst, mask, 0), rtm_type_name(action)); if (action == RTM_CHANGE) { action = RTM_ADD; goto again; } return; } msglog("write(rt_sock) %s %s --> %s: %s", rtm_type_name(action), addrname(dst, mask, 0), naddr_ntoa(gate), strerror(errno)); } else { msglog("write(rt_sock) wrote %d instead of %d", cc, w.w_rtm.rtm_msglen); } #endif } #define KHASH_SIZE 71 /* should be prime */ #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE] static struct khash { struct khash *k_next; naddr k_dst; naddr k_mask; naddr k_gate; short k_metric; u_short k_state; #define KS_NEW 0x001 #define KS_DELETE 0x002 #define KS_ADD 0x004 /* add to the kernel */ #define KS_CHANGE 0x008 /* tell kernel to change the route */ #define KS_DEL_ADD 0x010 /* delete & add to change the kernel */ #define KS_STATIC 0x020 /* Static flag in kernel */ #define KS_GATEWAY 0x040 /* G flag in kernel */ #define KS_DYNAMIC 0x080 /* result of redirect */ #define KS_DELETED 0x100 /* already deleted */ time_t k_keep; #define K_KEEP_LIM 30 time_t k_redirect_time; /* when redirected route 1st seen */ } *khash_bins[KHASH_SIZE]; static struct khash* kern_find(naddr dst, naddr mask, struct khash ***ppk) { struct khash *k, **pk; for (pk = &KHASH(dst,mask); (k = *pk) != 0; pk = &k->k_next) { if (k->k_dst == dst && k->k_mask == mask) break; } if (ppk != 0) *ppk = pk; return k; } static struct khash* kern_add(naddr dst, naddr mask) { struct khash *k, **pk; k = kern_find(dst, mask, &pk); if (k != 0) return k; k = (struct khash *)malloc(sizeof(*k)); bzero(k, sizeof(*k)); k->k_dst = dst; k->k_mask = mask; k->k_state = KS_NEW; k->k_keep = now.tv_sec; *pk = k; return k; } /* If a kernel route has a non-zero metric, check that it is still in the * daemon table, and not deleted by interfaces coming and going. */ static void kern_check_static(struct khash *k, struct interface *ifp) { struct rt_entry *rt; naddr int_addr; if (k->k_metric == 0) return; int_addr = (ifp != 0) ? ifp->int_addr : loopaddr; rt = rtget(k->k_dst, k->k_mask); if (rt != 0) { if (!(rt->rt_state & RS_STATIC)) rtchange(rt, rt->rt_state | RS_STATIC, k->k_gate, int_addr, k->k_metric, 0, ifp, now.tv_sec, 0); } else { rtadd(k->k_dst, k->k_mask, k->k_gate, int_addr, k->k_metric, 0, RS_STATIC, ifp); } } /* add a route the kernel told us */ static void rtm_add(struct rt_msghdr *rtm, struct rt_addrinfo *info, time_t keep) { struct khash *k; struct interface *ifp; naddr mask; if (rtm->rtm_flags & RTF_HOST) { mask = HOST_MASK; } else if (INFO_MASK(info) != 0) { mask = ntohl(S_ADDR(INFO_MASK(info))); } else { msglog("ignore %s without mask", rtm_type_name(rtm->rtm_type)); return; } if (INFO_GATE(info) == 0 || INFO_GATE(info)->sa_family != AF_INET) { msglog("ignore %s without gateway", rtm_type_name(rtm->rtm_type)); return; } k = kern_add(S_ADDR(INFO_DST(info)), mask); if (k->k_state & KS_NEW) k->k_keep = now.tv_sec+keep; k->k_gate = S_ADDR(INFO_GATE(info)); k->k_metric = rtm->rtm_rmx.rmx_hopcount; if (k->k_metric < 0) k->k_metric = 0; else if (k->k_metric > HOPCNT_INFINITY) k->k_metric = HOPCNT_INFINITY; k->k_state &= ~(KS_DELETED | KS_GATEWAY | KS_STATIC | KS_NEW); if (rtm->rtm_flags & RTF_GATEWAY) k->k_state |= KS_GATEWAY; if (rtm->rtm_flags & RTF_STATIC) k->k_state |= KS_STATIC; if (0 != (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED))) { if (INFO_AUTHOR(info) != 0 && INFO_AUTHOR(info)->sa_family == AF_INET) ifp = iflookup(S_ADDR(INFO_AUTHOR(info))); else ifp = 0; if (supplier && (ifp == 0 || !(ifp->int_state & IS_REDIRECT_OK))) { /* Routers are not supposed to listen to redirects, * so delete it if it came via an unknown interface * or the interface does not have special permission. */ k->k_state &= ~KS_DYNAMIC; k->k_state |= KS_DELETE; LIM_SEC(need_kern, 0); trace_act("mark for deletion redirected %s --> %s" " via %s", addrname(k->k_dst, k->k_mask, 0), naddr_ntoa(k->k_gate), ifp ? ifp->int_name : "unknown interface"); } else { k->k_state |= KS_DYNAMIC; k->k_redirect_time = now.tv_sec; trace_act("accept redirected %s --> %s via %s", addrname(k->k_dst, k->k_mask, 0), naddr_ntoa(k->k_gate), ifp ? ifp->int_name : "unknown interface"); } return; } /* If it is not a static route, quit until the next comparison * between the kernel and daemon tables, when it will be deleted. */ if (!(k->k_state & KS_STATIC)) { k->k_state |= KS_DELETE; LIM_SEC(need_kern, k->k_keep); return; } /* Put static routes with real metrics into the daemon table so * they can be advertised. * * Find the interface toward the gateway. */ ifp = iflookup(k->k_gate); if (ifp == 0) msglog("static route %s --> %s impossibly lacks ifp", addrname(S_ADDR(INFO_DST(info)), mask, 0), naddr_ntoa(k->k_gate)); kern_check_static(k, ifp); } /* deal with packet loss */ static void rtm_lose(struct rt_msghdr *rtm, struct rt_addrinfo *info) { if (INFO_GATE(info) == 0 || INFO_GATE(info)->sa_family != AF_INET) { trace_act("ignore %s without gateway", rtm_type_name(rtm->rtm_type)); return; } if (!supplier) rdisc_age(S_ADDR(INFO_GATE(info))); age(S_ADDR(INFO_GATE(info))); } /* Clean the kernel table by copying it to the daemon image. * Eventually the daemon will delete any extra routes. */ void flush_kern(void) { size_t needed; int mib[6]; char *buf, *next, *lim; struct rt_msghdr *rtm; struct interface *ifp; static struct sockaddr_in gate_sa; struct rt_addrinfo info; mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; /* protocol */ mib[3] = 0; /* wildcard address family */ mib[4] = NET_RT_DUMP; mib[5] = 0; /* no flags */ if (sysctl(mib, 6, 0, &needed, 0, 0) < 0) { DBGERR(1,"RT_DUMP-sysctl-estimate"); return; } buf = malloc(needed); if (sysctl(mib, 6, buf, &needed, 0, 0) < 0) BADERR(1,"RT_DUMP"); lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; rt_xaddrs(&info, (struct sockaddr *)(rtm+1), (struct sockaddr *)(next + rtm->rtm_msglen), rtm->rtm_addrs); if (INFO_DST(&info) == 0 || INFO_DST(&info)->sa_family != AF_INET) continue; /* ignore ARP table entries on systems with a merged route * and ARP table. */ if (rtm->rtm_flags & RTF_LLINFO) continue; if (INFO_GATE(&info) == 0) continue; if (INFO_GATE(&info)->sa_family != AF_INET) { if (INFO_GATE(&info)->sa_family != AF_LINK) continue; ifp = ifwithindex(((struct sockaddr_dl *) INFO_GATE(&info))->sdl_index); if (ifp == 0) continue; if ((ifp->int_if_flags & IFF_POINTOPOINT) || S_ADDR(INFO_DST(&info)) == ifp->int_addr) gate_sa.sin_addr.s_addr = ifp->int_addr; else gate_sa.sin_addr.s_addr = htonl(ifp->int_net); #ifdef _HAVE_SA_LEN gate_sa.sin_len = sizeof(gate_sa); #endif gate_sa.sin_family = AF_INET; INFO_GATE(&info) = (struct sockaddr *)&gate_sa; } /* ignore multicast addresses */ if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) continue; /* Note static routes and interface routes, and also * preload the image of the kernel table so that * we can later clean it, as well as avoid making * unneeded changes. Keep the old kernel routes for a * few seconds to allow a RIP or router-discovery * response to be heard. */ rtm_add(rtm,&info,MIN_WAITTIME); } free(buf); } /* Listen to announcements from the kernel */ void read_rt(void) { long cc; struct interface *ifp; naddr mask; union { struct { struct rt_msghdr rtm; struct sockaddr addrs[RTAX_MAX]; } r; struct if_msghdr ifm; } m; char str[100], *strp; struct rt_addrinfo info; for (;;) { cc = read(rt_sock, &m, sizeof(m)); if (cc <= 0) { if (cc < 0 && errno != EWOULDBLOCK) LOGERR("read(rt_sock)"); return; } if (m.r.rtm.rtm_version != RTM_VERSION) { msglog("bogus routing message version %d", m.r.rtm.rtm_version); continue; } /* Ignore our own results. */ if (m.r.rtm.rtm_type <= RTM_CHANGE && m.r.rtm.rtm_pid == mypid) { static int complained = 0; if (!complained) { msglog("receiving our own change messages"); complained = 1; } continue; } if (m.r.rtm.rtm_type == RTM_IFINFO || m.r.rtm.rtm_type == RTM_NEWADDR || m.r.rtm.rtm_type == RTM_DELADDR) { ifp = ifwithindex(m.ifm.ifm_index); if (ifp == 0) trace_act("note %s with flags %#x" " for index #%d", rtm_type_name(m.r.rtm.rtm_type), m.ifm.ifm_flags, m.ifm.ifm_index); else trace_act("note %s with flags %#x for %s", rtm_type_name(m.r.rtm.rtm_type), m.ifm.ifm_flags, ifp->int_name); /* After being informed of a change to an interface, * check them all now if the check would otherwise * be a long time from now, if the interface is * not known, or if the interface has been turned * off or on. */ if (ifinit_timer.tv_sec-now.tv_sec>=CHECK_BAD_INTERVAL || ifp == 0 || ((ifp->int_if_flags ^ m.ifm.ifm_flags) & IFF_UP_RUNNING) != 0) ifinit_timer.tv_sec = now.tv_sec; continue; } strcpy(str, rtm_type_name(m.r.rtm.rtm_type)); strp = &str[strlen(str)]; if (m.r.rtm.rtm_type <= RTM_CHANGE) strp += sprintf(strp," from pid %d",m.r.rtm.rtm_pid); rt_xaddrs(&info, m.r.addrs, &m.r.addrs[RTAX_MAX], m.r.rtm.rtm_addrs); if (INFO_DST(&info) == 0) { trace_act("ignore %s without dst", str); continue; } if (INFO_DST(&info)->sa_family != AF_INET) { trace_act("ignore %s for AF %d", str, INFO_DST(&info)->sa_family); continue; } mask = ((INFO_MASK(&info) != 0) ? ntohl(S_ADDR(INFO_MASK(&info))) : (m.r.rtm.rtm_flags & RTF_HOST) ? HOST_MASK : std_mask(S_ADDR(INFO_DST(&info)))); strp += sprintf(strp, ": %s", addrname(S_ADDR(INFO_DST(&info)), mask, 0)); if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) { trace_act("ignore multicast %s", str); continue; } if (INFO_GATE(&info) != 0 && INFO_GATE(&info)->sa_family == AF_INET) strp += sprintf(strp, " --> %s", saddr_ntoa(INFO_GATE(&info))); if (INFO_AUTHOR(&info) != 0) strp += sprintf(strp, " by authority of %s", saddr_ntoa(INFO_AUTHOR(&info))); switch (m.r.rtm.rtm_type) { case RTM_ADD: case RTM_CHANGE: case RTM_REDIRECT: if (m.r.rtm.rtm_errno != 0) { trace_act("ignore %s with \"%s\" error", str, strerror(m.r.rtm.rtm_errno)); } else { trace_act("%s", str); rtm_add(&m.r.rtm,&info,0); } break; case RTM_DELETE: if (m.r.rtm.rtm_errno != 0) { trace_act("ignore %s with \"%s\" error", str, strerror(m.r.rtm.rtm_errno)); } else { trace_act("%s", str); del_static(S_ADDR(INFO_DST(&info)), mask, 1); } break; case RTM_LOSING: trace_act("%s", str); rtm_lose(&m.r.rtm,&info); break; default: trace_act("ignore %s", str); break; } } } /* after aggregating, note routes that belong in the kernel */ static void kern_out(struct ag_info *ag) { struct khash *k; /* Do not install bad routes if they are not already present. * This includes routes that had RS_NET_SYN for interfaces that * recently died. */ if (ag->ag_metric == HOPCNT_INFINITY) { k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 0); if (k == 0) return; } else { k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask); } if (k->k_state & KS_NEW) { /* will need to add new entry to the kernel table */ k->k_state = KS_ADD; if (ag->ag_state & AGS_GATEWAY) k->k_state |= KS_GATEWAY; k->k_gate = ag->ag_gate; k->k_metric = ag->ag_metric; return; } if (k->k_state & KS_STATIC) return; /* modify existing kernel entry if necessary */ if (k->k_gate != ag->ag_gate || k->k_metric != ag->ag_metric) { k->k_gate = ag->ag_gate; k->k_metric = ag->ag_metric; k->k_state |= KS_CHANGE; } if (k->k_state & KS_DYNAMIC) { k->k_state &= ~KS_DYNAMIC; k->k_state |= (KS_ADD | KS_DEL_ADD); } if ((k->k_state & KS_GATEWAY) && !(ag->ag_state & AGS_GATEWAY)) { k->k_state &= ~KS_GATEWAY; k->k_state |= (KS_ADD | KS_DEL_ADD); } else if (!(k->k_state & KS_GATEWAY) && (ag->ag_state & AGS_GATEWAY)) { k->k_state |= KS_GATEWAY; k->k_state |= (KS_ADD | KS_DEL_ADD); } /* Deleting-and-adding is necessary to change aspects of a route. * Just delete instead of deleting and then adding a bad route. * Otherwise, we want to keep the route in the kernel. */ if (k->k_metric == HOPCNT_INFINITY && (k->k_state & KS_DEL_ADD)) k->k_state |= KS_DELETE; else k->k_state &= ~KS_DELETE; #undef RT } /* ARGSUSED */ static int walk_kern(struct radix_node *rn, struct walkarg *w) { #define RT ((struct rt_entry *)rn) char metric, pref; u_int ags = 0; /* Do not install synthetic routes */ if (RT->rt_state & RS_NET_SYN) return 0; if (!(RT->rt_state & RS_IF)) { ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_PROMOTE); } else { /* Do not install routes for "external" remote interfaces. */ if (RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_EXTERNAL)) return 0; ags |= AGS_IF; /* If it is not an interface, or an alias for an interface, * it must be a "gateway." * * If it is a "remote" interface, it is also a "gateway" to * the kernel if is not a alias. */ if (RT->rt_ifp == 0 || (RT->rt_ifp->int_state & IS_REMOTE)) ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_PROMOTE); } if (RT->rt_state & RS_RDISC) ags |= AGS_CORS_GATE; /* aggregate good routes without regard to their metric */ pref = 1; metric = RT->rt_metric; if (metric == HOPCNT_INFINITY) { /* if the route is dead, so try hard to aggregate. */ pref = HOPCNT_INFINITY; ags |= (AGS_FINE_GATE | AGS_SUPPRESS); } ag_check(RT->rt_dst, RT->rt_mask, RT->rt_gate, 0, metric,pref, 0, 0, ags, kern_out); return 0; #undef RT } /* Update the kernel table to match the daemon table. */ static void fix_kern(void) { int i, flags; struct khash *k, **pk; need_kern = age_timer; /* Walk daemon table, updating the copy of the kernel table. */ (void)rn_walktree(rhead, walk_kern, 0); ag_flush(0,0,kern_out); for (i = 0; i < KHASH_SIZE; i++) { for (pk = &khash_bins[i]; (k = *pk) != 0; ) { /* Do not touch static routes */ if (k->k_state & KS_STATIC) { kern_check_static(k,0); pk = &k->k_next; continue; } /* check hold on routes deleted by the operator */ if (k->k_keep > now.tv_sec) { LIM_SEC(need_kern, k->k_keep); k->k_state |= KS_DELETE; pk = &k->k_next; continue; } if ((k->k_state & (KS_DELETE | KS_DYNAMIC)) == KS_DELETE) { if (!(k->k_state & KS_DELETED)) rtioctl(RTM_DELETE, k->k_dst, k->k_gate, k->k_mask, 0, 0); *pk = k->k_next; free(k); continue; } if (0 != (k->k_state&(KS_ADD|KS_CHANGE|KS_DEL_ADD))) { if (k->k_state & KS_DEL_ADD) { rtioctl(RTM_DELETE, k->k_dst,k->k_gate,k->k_mask, 0, 0); k->k_state &= ~KS_DYNAMIC; } flags = 0; if (0 != (k->k_state&(KS_GATEWAY|KS_DYNAMIC))) flags |= RTF_GATEWAY; if (k->k_state & KS_ADD) { rtioctl(RTM_ADD, k->k_dst, k->k_gate, k->k_mask, k->k_metric, flags); } else if (k->k_state & KS_CHANGE) { rtioctl(RTM_CHANGE, k->k_dst,k->k_gate,k->k_mask, k->k_metric, flags); } k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD); } /* Mark this route to be deleted in the next cycle. * This deletes routes that disappear from the * daemon table, since the normal aging code * will clear the bit for routes that have not * disappeared from the daemon table. */ k->k_state |= KS_DELETE; pk = &k->k_next; } } } /* Delete a static route in the image of the kernel table. */ void del_static(naddr dst, naddr mask, int gone) { struct khash *k; struct rt_entry *rt; /* Just mark it in the table to be deleted next time the kernel * table is updated. * If it has already been deleted, mark it as such, and set its * keep-timer so that it will not be deleted again for a while. * This lets the operator delete a route added by the daemon * and add a replacement. */ k = kern_find(dst, mask, 0); if (k != 0) { k->k_state &= ~(KS_STATIC | KS_DYNAMIC); k->k_state |= KS_DELETE; if (gone) { k->k_state |= KS_DELETED; k->k_keep = now.tv_sec + K_KEEP_LIM; } } rt = rtget(dst, mask); if (rt != 0 && (rt->rt_state & RS_STATIC)) rtbad(rt); } /* Delete all routes generated from ICMP Redirects that use a given gateway, * as well as old redirected routes. */ void del_redirects(naddr bad_gate, time_t old) { int i; struct khash *k; for (i = 0; i < KHASH_SIZE; i++) { for (k = khash_bins[i]; k != 0; k = k->k_next) { if (!(k->k_state & KS_DYNAMIC) || (k->k_state & KS_STATIC)) continue; if (k->k_gate != bad_gate && k->k_redirect_time > old && !supplier) continue; k->k_state |= KS_DELETE; k->k_state &= ~KS_DYNAMIC; need_kern.tv_sec = now.tv_sec; trace_act("mark redirected %s --> %s for deletion", addrname(k->k_dst, k->k_mask, 0), naddr_ntoa(k->k_gate)); } } } /* Start the daemon tables. */ void rtinit(void) { extern int max_keylen; int i; struct ag_info *ag; /* Initialize the radix trees */ max_keylen = sizeof(struct sockaddr_in); rn_init(); rn_inithead((void**)&rhead, 32); /* mark all of the slots in the table free */ ag_avail = ag_slots; for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) { ag->ag_fine = ag+1; ag++; } } #ifdef _HAVE_SIN_LEN static struct sockaddr_in dst_sock = {sizeof(dst_sock), AF_INET}; static struct sockaddr_in mask_sock = {sizeof(mask_sock), AF_INET}; #else static struct sockaddr_in_new dst_sock = {_SIN_ADDR_SIZE, AF_INET}; static struct sockaddr_in_new mask_sock = {_SIN_ADDR_SIZE, AF_INET}; #endif void set_need_flash(void) { if (!need_flash) { need_flash = 1; /* Do not send the flash update immediately. Wait a little * while to hear from other routers. */ no_flash.tv_sec = now.tv_sec + MIN_WAITTIME; } } /* Get a particular routing table entry */ struct rt_entry * rtget(naddr dst, naddr mask) { struct rt_entry *rt; dst_sock.sin_addr.s_addr = dst; mask_sock.sin_addr.s_addr = mask; masktrim(&mask_sock); rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock,&mask_sock,rhead); if (!rt || rt->rt_dst != dst || rt->rt_mask != mask) return 0; return rt; } /* Find a route to dst as the kernel would. */ struct rt_entry * rtfind(naddr dst) { dst_sock.sin_addr.s_addr = dst; return (struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead); } /* add a route to the table */ void rtadd(naddr dst, naddr mask, naddr gate, /* forward packets here */ naddr router, /* on the authority of this router */ int metric, u_short tag, u_int state, /* rs_state for the entry */ struct interface *ifp) { struct rt_entry *rt; naddr smask; int i; struct rt_spare *rts; rt = (struct rt_entry *)rtmalloc(sizeof (*rt), "rtadd"); bzero(rt, sizeof(*rt)); for (rts = rt->rt_spares, i = NUM_SPARES; i != 0; i--, rts++) rts->rts_metric = HOPCNT_INFINITY; rt->rt_nodes->rn_key = (caddr_t)&rt->rt_dst_sock; rt->rt_dst = dst; rt->rt_dst_sock.sin_family = AF_INET; #ifdef _HAVE_SIN_LEN rt->rt_dst_sock.sin_len = dst_sock.sin_len; #endif if (mask != HOST_MASK) { smask = std_mask(dst); if ((smask & ~mask) == 0 && mask > smask) state |= RS_SUBNET; } mask_sock.sin_addr.s_addr = mask; masktrim(&mask_sock); rt->rt_mask = mask; rt->rt_state = state; rt->rt_gate = gate; rt->rt_router = router; rt->rt_time = now.tv_sec; rt->rt_metric = metric; rt->rt_poison_metric = HOPCNT_INFINITY; rt->rt_tag = tag; rt->rt_ifp = ifp; rt->rt_seqno = update_seqno; if (++total_routes == MAX_ROUTES) msglog("have maximum (%d) routes", total_routes); if (TRACEACTIONS) trace_add_del("Add", rt); need_kern.tv_sec = now.tv_sec; set_need_flash(); if (0 == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock, rhead, rt->rt_nodes)) { /* * This will happen if RIP1 and RIP2 routeds talk to one another and * there are variable subnets. This is only good for filling up your * syslog. -jkh */ #if 0 msglog("rnh_addaddr() failed for %s mask=%#x", naddr_ntoa(dst), mask); #endif } } /* notice a changed route */ void rtchange(struct rt_entry *rt, u_int state, /* new state bits */ naddr gate, /* now forward packets here */ naddr router, /* on the authority of this router */ int metric, /* new metric */ u_short tag, struct interface *ifp, time_t new_time, char *label) { if (rt->rt_metric != metric) { /* Fix the kernel immediately if it seems the route * has gone bad, since there may be a working route that * aggregates this route. */ if (metric == HOPCNT_INFINITY) { need_kern.tv_sec = now.tv_sec; if (new_time >= now.tv_sec - EXPIRE_TIME) new_time = now.tv_sec - EXPIRE_TIME; } rt->rt_seqno = update_seqno; set_need_flash(); } if (rt->rt_gate != gate) { need_kern.tv_sec = now.tv_sec; rt->rt_seqno = update_seqno; set_need_flash(); } state |= (rt->rt_state & RS_SUBNET); /* Keep various things from deciding ageless routes are stale. */ if (!AGE_RT(state, ifp)) new_time = now.tv_sec; if (TRACEACTIONS) trace_change(rt, state, gate, router, metric, tag, ifp, new_time, label ? label : "Chg "); rt->rt_state = state; rt->rt_gate = gate; rt->rt_router = router; rt->rt_metric = metric; rt->rt_tag = tag; rt->rt_ifp = ifp; rt->rt_time = new_time; } /* check for a better route among the spares */ static struct rt_spare * rts_better(struct rt_entry *rt) { struct rt_spare *rts, *rts1; int i; /* find the best alternative among the spares */ rts = rt->rt_spares+1; for (i = NUM_SPARES, rts1 = rts+1; i > 2; i--, rts1++) { if (BETTER_LINK(rt,rts1,rts)) rts = rts1; } return rts; } /* switch to a backup route */ void rtswitch(struct rt_entry *rt, struct rt_spare *rts) { struct rt_spare swap; char label[10]; /* Do not change permanent routes */ if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | RS_RDISC | RS_NET_SYN | RS_IF))) return; /* find the best alternative among the spares */ if (rts == 0) rts = rts_better(rt); /* Do not bother if it is not worthwhile. */ if (!BETTER_LINK(rt, rts, rt->rt_spares)) return; swap = rt->rt_spares[0]; (void)sprintf(label, "Use #%d", rts - rt->rt_spares); rtchange(rt, rt->rt_state & ~(RS_NET_SYN | RS_RDISC), rts->rts_gate, rts->rts_router, rts->rts_metric, rts->rts_tag, rts->rts_ifp, rts->rts_time, label); *rts = swap; } void rtdelete(struct rt_entry *rt) { struct khash *k; if (TRACEACTIONS) trace_add_del("Del", rt); k = kern_find(rt->rt_dst, rt->rt_mask, 0); if (k != 0) { k->k_state |= KS_DELETE; need_kern.tv_sec = now.tv_sec; } dst_sock.sin_addr.s_addr = rt->rt_dst; mask_sock.sin_addr.s_addr = rt->rt_mask; masktrim(&mask_sock); if (rt != (struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock, rhead)) { msglog("rnh_deladdr() failed"); } else { free(rt); total_routes--; } } /* Get rid of a bad route, and try to switch to a replacement. */ void rtbad(struct rt_entry *rt) { /* Poison the route */ rtchange(rt, rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC), rt->rt_gate, rt->rt_router, HOPCNT_INFINITY, rt->rt_tag, 0, rt->rt_time, 0); rtswitch(rt, 0); } /* Junk a RS_NET_SYN or RS_LOCAL route, * unless it is needed by another interface. */ void rtbad_sub(struct rt_entry *rt) { struct interface *ifp, *ifp1; struct intnet *intnetp; u_int state; ifp1 = 0; state = 0; if (rt->rt_state & RS_LOCAL) { /* Is this the route through loopback for the interface? * If so, see if it is used by any other interfaces, such * as a point-to-point interface with the same local address. */ for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { /* Retain it if another interface needs it. */ if (ifp->int_addr == rt->rt_ifp->int_addr) { state |= RS_LOCAL; ifp1 = ifp; break; } } } if (!(state & RS_LOCAL)) { /* Retain RIPv1 logical network route if there is another * interface that justifies it. */ if (rt->rt_state & RS_NET_SYN) { for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { if ((ifp->int_state & IS_NEED_NET_SYN) && rt->rt_mask == ifp->int_std_mask && rt->rt_dst == ifp->int_std_addr) { state |= RS_NET_SYN; ifp1 = ifp; break; } } } /* or if there is an authority route that needs it. */ for (intnetp = intnets; intnetp != 0; intnetp = intnetp->intnet_next) { if (intnetp->intnet_addr == rt->rt_dst && intnetp->intnet_mask == rt->rt_mask) { state |= (RS_NET_SYN | RS_NET_INT); break; } } } if (ifp1 != 0 || (state & RS_NET_SYN)) { rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN | RS_LOCAL)) | state), rt->rt_gate, rt->rt_router, rt->rt_metric, rt->rt_tag, ifp1, rt->rt_time, 0); } else { rtbad(rt); } } /* Called while walking the table looking for sick interfaces * or after a time change. */ /* ARGSUSED */ int walk_bad(struct radix_node *rn, struct walkarg *w) { #define RT ((struct rt_entry *)rn) struct rt_spare *rts; int i; time_t new_time; /* fix any spare routes through the interface */ rts = RT->rt_spares; for (i = NUM_SPARES; i != 1; i--) { rts++; if (rts->rts_ifp != 0 && (rts->rts_ifp->int_state & IS_BROKE)) { /* mark the spare route to be deleted immediately */ new_time = rts->rts_time; if (new_time >= now_garbage) new_time = now_garbage-1; trace_upslot(RT, rts, rts->rts_gate, rts->rts_router, 0, HOPCNT_INFINITY, rts->rts_tag, new_time); rts->rts_ifp = 0; rts->rts_metric = HOPCNT_INFINITY; rts->rts_time = new_time; } } /* Deal with the main route */ /* finished if it has been handled before or if its interface is ok */ if (RT->rt_ifp == 0 || !(RT->rt_ifp->int_state & IS_BROKE)) return 0; /* Bad routes for other than interfaces are easy. */ if (0 == (RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) { rtbad(RT); return 0; } rtbad_sub(RT); return 0; #undef RT } /* Check the age of an individual route. */ /* ARGSUSED */ static int walk_age(struct radix_node *rn, struct walkarg *w) { #define RT ((struct rt_entry *)rn) struct interface *ifp; struct rt_spare *rts; int i; /* age all of the spare routes, including the primary route * currently in use */ rts = RT->rt_spares; for (i = NUM_SPARES; i != 0; i--, rts++) { ifp = rts->rts_ifp; if (i == NUM_SPARES) { if (!AGE_RT(RT->rt_state, ifp)) { /* Keep various things from deciding ageless * routes are stale */ rts->rts_time = now.tv_sec; continue; } /* forget RIP routes after RIP has been turned off. */ if (rip_sock < 0) { rtdelete(RT); return 0; } } /* age failing routes */ if (age_bad_gate == rts->rts_gate && rts->rts_time >= now_stale) { rts->rts_time -= SUPPLY_INTERVAL; } /* trash the spare routes when they go bad */ if (rts->rts_metric < HOPCNT_INFINITY && now_garbage > rts->rts_time) { trace_upslot(RT, rts, rts->rts_gate, rts->rts_router, rts->rts_ifp, HOPCNT_INFINITY, rts->rts_tag, rts->rts_time); rts->rts_metric = HOPCNT_INFINITY; } } /* finished if the active route is still fresh */ if (now_stale <= RT->rt_time) return 0; /* try to switch to an alternative */ rtswitch(RT, 0); /* Delete a dead route after it has been publically mourned. */ if (now_garbage > RT->rt_time) { rtdelete(RT); return 0; } /* Start poisoning a bad route before deleting it. */ if (now.tv_sec - RT->rt_time > EXPIRE_TIME) rtchange(RT, RT->rt_state, RT->rt_gate, RT->rt_router, HOPCNT_INFINITY, RT->rt_tag, RT->rt_ifp, RT->rt_time, 0); return 0; } /* Watch for dead routes and interfaces. */ void age(naddr bad_gate) { struct interface *ifp; int need_query = 0; /* If not listening to RIP, there is no need to age the routes in * the table. */ age_timer.tv_sec = (now.tv_sec + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL)); /* Check for dead IS_REMOTE interfaces by timing their * transmissions. */ for (ifp = ifnet; ifp; ifp = ifp->int_next) { if (!(ifp->int_state & IS_REMOTE)) continue; /* ignore unreachable remote interfaces */ if (!check_remote(ifp)) continue; /* Restore remote interface that has become reachable */ if (ifp->int_state & IS_BROKE) if_ok(ifp, "remote "); if (ifp->int_act_time != NEVER && now.tv_sec - ifp->int_act_time > EXPIRE_TIME) { msglog("remote interface %s to %s timed out after" " %d:%d", ifp->int_name, naddr_ntoa(ifp->int_dstaddr), (now.tv_sec - ifp->int_act_time)/60, (now.tv_sec - ifp->int_act_time)%60); if_sick(ifp); } /* If we have not heard from the other router * recently, ask it. */ if (now.tv_sec >= ifp->int_query_time) { ifp->int_query_time = NEVER; need_query = 1; } } /* Age routes. */ age_bad_gate = bad_gate; (void)rn_walktree(rhead, walk_age, 0); /* Update the kernel routing table. */ fix_kern(); /* poke reticent remote gateways */ if (need_query) rip_query(); }