Index: head/usr.sbin/rtsold/if.c =================================================================== --- head/usr.sbin/rtsold/if.c (revision 339455) +++ head/usr.sbin/rtsold/if.c (revision 339456) @@ -1,430 +1,399 @@ /* $KAME: if.c,v 1.27 2003/10/05 00:09:36 itojun Exp $ */ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * 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. Neither the name of the project 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 PROJECT 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 PROJECT 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. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "rtsold.h" static int ifsock; static int get_llflag(const char *); static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **); int ifinit(void) { ifsock = rssock; return(0); } int interface_up(char *name) { struct ifreq ifr; struct in6_ndireq nd; int llflag; int s; memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); memset(&nd, 0, sizeof(nd)); strlcpy(nd.ifname, name, sizeof(nd.ifname)); if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s", strerror(errno)); return (-1); } if (!(ifr.ifr_flags & IFF_UP)) { ifr.ifr_flags |= IFF_UP; if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) warnmsg(LOG_ERR, __func__, "ioctl(SIOCSIFFLAGS): %s", strerror(errno)); return (-1); } if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { warnmsg(LOG_WARNING, __func__, "socket(AF_INET6, SOCK_DGRAM): %s", strerror(errno)); return (-1); } if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFINFO_IN6): %s", strerror(errno)); close(s); return (-1); } warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name); if (nd.ndi.flags & ND6_IFF_IFDISABLED) { if (Fflag) { nd.ndi.flags &= ~ND6_IFF_IFDISABLED; if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) { warnmsg(LOG_WARNING, __func__, "ioctl(SIOCSIFINFO_IN6): %s", strerror(errno)); close(s); return (-1); } } else { warnmsg(LOG_WARNING, __func__, "%s is disabled.", name); close(s); return (-1); } } if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) { if (Fflag) { nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV; if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) { warnmsg(LOG_WARNING, __func__, "ioctl(SIOCSIFINFO_IN6): %s", strerror(errno)); close(s); return (-1); } } else { warnmsg(LOG_WARNING, __func__, "%s does not accept Router Advertisement.", name); close(s); return (-1); } } close(s); llflag = get_llflag(name); if (llflag < 0) { warnmsg(LOG_WARNING, __func__, "get_llflag() failed, anyway I'll try"); return (0); } if (!(llflag & IN6_IFF_NOTREADY)) { warnmsg(LOG_DEBUG, __func__, "%s is ready", name); return (0); } else { if (llflag & IN6_IFF_TENTATIVE) { warnmsg(LOG_DEBUG, __func__, "%s is tentative", name); return (IFS_TENTATIVE); } if (llflag & IN6_IFF_DUPLICATED) warnmsg(LOG_DEBUG, __func__, "%s is duplicated", name); return (-1); } } int interface_status(struct ifinfo *ifinfo) { char *ifname = ifinfo->ifname; struct ifreq ifr; struct ifmediareq ifmr; /* get interface flags */ memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) { warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s", ifname, strerror(errno)); return (-1); } /* * if one of UP and RUNNING flags is dropped, * the interface is not active. */ if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) goto inactive; /* Next, check carrier on the interface, if possible */ if (!ifinfo->mediareqok) goto active; memset(&ifmr, 0, sizeof(ifmr)); strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { if (errno != EINVAL) { warnmsg(LOG_DEBUG, __func__, "ioctl(SIOCGIFMEDIA) on %s: %s", ifname, strerror(errno)); return(-1); } /* * EINVAL simply means that the interface does not support * the SIOCGIFMEDIA ioctl. We regard it alive. */ ifinfo->mediareqok = 0; goto active; } if (ifmr.ifm_status & IFM_AVALID) { switch (ifmr.ifm_active & IFM_NMASK) { case IFM_ETHER: case IFM_IEEE80211: if (ifmr.ifm_status & IFM_ACTIVE) goto active; else goto inactive; break; default: goto inactive; } } inactive: return (0); active: return (1); } #define ROUNDUP(a, size) \ (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a)) #define NEXT_SA(ap) (ap) = (struct sockaddr *) \ ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\ sizeof(u_long)) : sizeof(u_long))) #define ROUNDUP8(a) (1 + (((a) - 1) | 7)) int lladdropt_length(struct sockaddr_dl *sdl) { switch (sdl->sdl_type) { case IFT_ETHER: return (ROUNDUP8(ETHER_ADDR_LEN + 2)); default: return (0); } } void lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt) { char *addr; ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */ switch (sdl->sdl_type) { case IFT_ETHER: ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3; addr = (char *)(ndopt + 1); memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN); break; default: warnmsg(LOG_ERR, __func__, "unsupported link type(%d)", sdl->sdl_type); exit(1); } return; } struct sockaddr_dl * if_nametosdl(char *name) { int mib[] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0}; char *buf, *next, *lim; size_t len; struct if_msghdr *ifm; struct sockaddr *sa, *rti_info[RTAX_MAX]; struct sockaddr_dl *sdl = NULL, *ret_sdl; if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0) return(NULL); if ((buf = malloc(len)) == NULL) return(NULL); if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0) { free(buf); return (NULL); } lim = buf + len; for (next = buf; next < lim; next += ifm->ifm_msglen) { ifm = (struct if_msghdr *)(void *)next; if (ifm->ifm_type == RTM_IFINFO) { sa = (struct sockaddr *)(ifm + 1); get_rtaddrs(ifm->ifm_addrs, sa, rti_info); if ((sa = rti_info[RTAX_IFP]) != NULL) { if (sa->sa_family == AF_LINK) { sdl = (struct sockaddr_dl *)(void *)sa; if (strlen(name) != sdl->sdl_nlen) continue; /* not same len */ if (strncmp(&sdl->sdl_data[0], name, sdl->sdl_nlen) == 0) { break; } } } } } if (next == lim) { /* search failed */ free(buf); return (NULL); } if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) { free(buf); return (NULL); } memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len); free(buf); return (ret_sdl); } -int -getinet6sysctl(int code) -{ - int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 }; - int value; - size_t size; - - mib[3] = code; - size = sizeof(value); - if (sysctl(mib, nitems(mib), &value, &size, NULL, 0) < 0) - return (-1); - else - return (value); -} - -int -setinet6sysctl(int code, int newval) -{ - int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 }; - int value; - size_t size; - - mib[3] = code; - size = sizeof(value); - if (sysctl(mib, nitems(mib), &value, &size, - &newval, sizeof(newval)) < 0) - return (-1); - else - return (value); -} - /*------------------------------------------------------------*/ /* get ia6_flags for link-local addr on if. returns -1 on error. */ static int get_llflag(const char *name) { struct ifaddrs *ifap, *ifa; struct in6_ifreq ifr6; struct sockaddr_in6 *sin6; int s; if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s", strerror(errno)); exit(1); } if (getifaddrs(&ifap) != 0) { warnmsg(LOG_ERR, __func__, "getifaddrs: %s", strerror(errno)); exit(1); } for (ifa = ifap; ifa; ifa = ifa->ifa_next) { if (strlen(ifa->ifa_name) != strlen(name) || strncmp(ifa->ifa_name, name, strlen(name)) != 0) continue; if (ifa->ifa_addr->sa_family != AF_INET6) continue; sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr; if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) continue; memset(&ifr6, 0, sizeof(ifr6)); strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len); if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) { warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno)); exit(1); } freeifaddrs(ifap); close(s); return (ifr6.ifr_ifru.ifru_flags6); } freeifaddrs(ifap); close(s); return (-1); } static void get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info) { int i; for (i = 0; i < RTAX_MAX; i++) { if (addrs & (1 << i)) { rti_info[i] = sa; NEXT_SA(sa); } else rti_info[i] = NULL; } } Index: head/usr.sbin/rtsold/rtsock.c =================================================================== --- head/usr.sbin/rtsold/rtsock.c (revision 339455) +++ head/usr.sbin/rtsold/rtsock.c (revision 339456) @@ -1,171 +1,163 @@ /* $KAME: rtsock.c,v 1.3 2000/10/10 08:46:45 itojun Exp $ */ /* $FreeBSD$ */ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (C) 2000 WIDE Project. * 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. Neither the name of the project 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 PROJECT 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 PROJECT 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "rtsold.h" -#define ROUNDUP(a, size) \ - (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a)) - -#define NEXT_SA(ap) (ap) = (struct sockaddr *) \ - ((caddr_t)(ap) + \ - ((ap)->sa_len ? ROUNDUP((ap)->sa_len, sizeof(u_long)) \ - : sizeof(u_long))) - static int rtsock_input_ifannounce(int, struct rt_msghdr *, char *); static struct { u_char type; size_t minlen; int (*func)(int, struct rt_msghdr *, char *); } rtsock_dispatch[] = { { RTM_IFANNOUNCE, sizeof(struct if_announcemsghdr), rtsock_input_ifannounce }, { 0, 0, NULL }, }; int rtsock_open(void) { return (socket(PF_ROUTE, SOCK_RAW, 0)); } int rtsock_input(int s) { ssize_t n; char msg[2048]; char *lim, *next; struct rt_msghdr *rtm; int idx; ssize_t len; int ret = 0; const ssize_t lenlim = offsetof(struct rt_msghdr, rtm_msglen) + sizeof(rtm->rtm_msglen); n = read(s, msg, sizeof(msg)); lim = msg + n; for (next = msg; next < lim; next += len) { rtm = (struct rt_msghdr *)(void *)next; if (lim - next < lenlim) break; len = rtm->rtm_msglen; if (len < lenlim) break; if (dflag > 1) { warnmsg(LOG_INFO, __func__, "rtmsg type %d, len=%lu", rtm->rtm_type, (u_long)len); } for (idx = 0; rtsock_dispatch[idx].func; idx++) { if (rtm->rtm_type != rtsock_dispatch[idx].type) continue; if (rtm->rtm_msglen < rtsock_dispatch[idx].minlen) { warnmsg(LOG_INFO, __func__, "rtmsg type %d too short!", rtm->rtm_type); continue; } ret = (*rtsock_dispatch[idx].func)(s, rtm, lim); break; } } return (ret); } static int rtsock_input_ifannounce(int s __unused, struct rt_msghdr *rtm, char *lim) { struct if_announcemsghdr *ifan; struct ifinfo *ifi; ifan = (struct if_announcemsghdr *)rtm; if ((char *)(ifan + 1) > lim) return (-1); switch (ifan->ifan_what) { case IFAN_ARRIVAL: /* * XXX for NetBSD 1.5, interface index will monotonically be * increased as new pcmcia card gets inserted. * we may be able to do a name-based interface match, * and call ifreconfig() to enable the interface again. */ warnmsg(LOG_INFO, __func__, "interface %s inserted", ifan->ifan_name); break; case IFAN_DEPARTURE: warnmsg(LOG_WARNING, __func__, "interface %s removed", ifan->ifan_name); ifi = find_ifinfo(ifan->ifan_index); if (ifi) { if (dflag > 1) { warnmsg(LOG_INFO, __func__, "bring interface %s to DOWN state", ifan->ifan_name); } ifi->state = IFS_DOWN; } break; } return (0); } Index: head/usr.sbin/rtsold/rtsold.c =================================================================== --- head/usr.sbin/rtsold/rtsold.c (revision 339455) +++ head/usr.sbin/rtsold/rtsold.c (revision 339456) @@ -1,887 +1,869 @@ /* $KAME: rtsold.c,v 1.67 2003/05/17 18:16:15 itojun Exp $ */ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * 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. Neither the name of the project 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 PROJECT 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 PROJECT 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. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "rtsold.h" #define RTSOL_DUMPFILE "/var/run/rtsold.dump"; #define RTSOL_PIDFILE "/var/run/rtsold.pid"; struct timespec tm_max; static int log_upto = 999; static int fflag = 0; int Fflag = 0; /* force setting sysctl parameters */ int aflag = 0; int dflag = 0; int uflag = 0; const char *otherconf_script; const char *resolvconf_script = "/sbin/resolvconf"; /* protocol constants */ #define MAX_RTR_SOLICITATION_DELAY 1 /* second */ #define RTR_SOLICITATION_INTERVAL 4 /* seconds */ #define MAX_RTR_SOLICITATIONS 3 /* times */ /* * implementation dependent constants in seconds * XXX: should be configurable */ #define PROBE_INTERVAL 60 /* static variables and functions */ static int mobile_node = 0; static const char *pidfilename = RTSOL_PIDFILE; #ifndef SMALL static int do_dump; static const char *dumpfilename = RTSOL_DUMPFILE; #endif static char **autoifprobe(void); static int ifconfig(char *ifname); static int make_packet(struct ifinfo *); static struct timespec *rtsol_check_timer(void); #ifndef SMALL static void rtsold_set_dump_file(int); #endif static void usage(void); int main(int argc, char **argv) { int s, ch, once = 0; struct timespec *timeout; const char *opts; struct pollfd set[2]; int rtsock; char *argv0; #ifndef SMALL /* rtsold */ opts = "adDfFm1O:p:R:u"; #else /* rtsol */ opts = "adDFO:R:u"; fflag = 1; once = 1; #endif argv0 = argv[0]; while ((ch = getopt(argc, argv, opts)) != -1) { switch (ch) { case 'a': aflag = 1; break; case 'd': dflag += 1; break; case 'D': dflag += 2; break; case 'f': fflag = 1; break; case 'F': Fflag = 1; break; case 'm': mobile_node = 1; break; case '1': once = 1; break; case 'O': otherconf_script = optarg; break; case 'p': pidfilename = optarg; break; case 'R': resolvconf_script = optarg; break; case 'u': uflag = 1; break; default: usage(); exit(1); } } argc -= optind; argv += optind; if ((!aflag && argc == 0) || (aflag && argc != 0)) { usage(); exit(1); } /* Generate maximum time in timespec. */ tm_max.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1)); tm_max.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1)); /* set log level */ if (dflag > 1) log_upto = LOG_DEBUG; else if (dflag > 0) log_upto = LOG_INFO; else log_upto = LOG_NOTICE; if (!fflag) { char *ident; ident = strrchr(argv0, '/'); if (!ident) ident = argv0; else ident++; openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON); if (log_upto >= 0) setlogmask(LOG_UPTO(log_upto)); } if (otherconf_script && *otherconf_script != '/') { errx(1, "configuration script (%s) must be an absolute path", otherconf_script); } if (resolvconf_script && *resolvconf_script != '/') { errx(1, "configuration script (%s) must be an absolute path", resolvconf_script); } if (pidfilename && *pidfilename != '/') { errx(1, "pid filename (%s) must be an absolute path", pidfilename); } #ifndef SMALL /* initialization to dump internal status to a file */ signal(SIGUSR1, rtsold_set_dump_file); #endif if (!fflag) daemon(0, 0); /* act as a daemon */ /* * Open a socket for sending RS and receiving RA. * This should be done before calling ifinit(), since the function * uses the socket. */ if ((s = sockopen()) < 0) { warnmsg(LOG_ERR, __func__, "failed to open a socket"); exit(1); } set[0].fd = s; set[0].events = POLLIN; set[1].fd = -1; if ((rtsock = rtsock_open()) < 0) { warnmsg(LOG_ERR, __func__, "failed to open a socket"); exit(1); } set[1].fd = rtsock; set[1].events = POLLIN; /* configuration per interface */ if (ifinit()) { warnmsg(LOG_ERR, __func__, "failed to initialize interfaces"); exit(1); } if (aflag) argv = autoifprobe(); while (argv && *argv) { if (ifconfig(*argv)) { warnmsg(LOG_ERR, __func__, "failed to initialize %s", *argv); exit(1); } argv++; } /* setup for probing default routers */ if (probe_init()) { warnmsg(LOG_ERR, __func__, "failed to setup for probing routers"); exit(1); /*NOTREACHED*/ } /* dump the current pid */ if (!once) { pid_t pid = getpid(); FILE *fp; if ((fp = fopen(pidfilename, "w")) == NULL) warnmsg(LOG_ERR, __func__, "failed to open a pid log file(%s): %s", pidfilename, strerror(errno)); else { fprintf(fp, "%d\n", pid); fclose(fp); } } while (1) { /* main loop */ int e; #ifndef SMALL if (do_dump) { /* SIGUSR1 */ do_dump = 0; rtsold_dump_file(dumpfilename); } #endif timeout = rtsol_check_timer(); if (once) { struct ifinfo *ifi; /* if we have no timeout, we are done (or failed) */ if (timeout == NULL) break; /* if all interfaces have got RA packet, we are done */ TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) { if (ifi->state != IFS_DOWN && ifi->racnt == 0) break; } if (ifi == NULL) break; } e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM); if (e < 1) { if (e < 0 && errno != EINTR) { warnmsg(LOG_ERR, __func__, "select: %s", strerror(errno)); } continue; } /* packet reception */ if (set[1].revents & POLLIN) rtsock_input(rtsock); if (set[0].revents & POLLIN) rtsol_input(s); } /* NOTREACHED */ return (0); } static int ifconfig(char *ifname) { struct ifinfo *ifi; struct sockaddr_dl *sdl; int flags; if ((sdl = if_nametosdl(ifname)) == NULL) { warnmsg(LOG_ERR, __func__, "failed to get link layer information for %s", ifname); return (-1); } if (find_ifinfo(sdl->sdl_index)) { warnmsg(LOG_ERR, __func__, "interface %s was already configured", ifname); free(sdl); return (-1); } if (Fflag) { struct in6_ndireq nd; int s; if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { warnmsg(LOG_ERR, __func__, "socket() failed."); return (-1); } memset(&nd, 0, sizeof(nd)); strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { warnmsg(LOG_ERR, __func__, "cannot get accept_rtadv flag"); close(s); return (-1); } nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV; if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) { warnmsg(LOG_ERR, __func__, "cannot set accept_rtadv flag"); close(s); return (-1); } close(s); } if ((ifi = malloc(sizeof(*ifi))) == NULL) { warnmsg(LOG_ERR, __func__, "memory allocation failed"); free(sdl); return (-1); } memset(ifi, 0, sizeof(*ifi)); ifi->sdl = sdl; ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO; ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO; TAILQ_INIT(&ifi->ifi_rainfo); strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname)); /* construct a router solicitation message */ if (make_packet(ifi)) goto bad; /* set link ID of this interface. */ #ifdef HAVE_SCOPELIB if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid)) goto bad; #else /* XXX: assume interface IDs as link IDs */ ifi->linkid = ifi->sdl->sdl_index; #endif /* * check if the interface is available. * also check if SIOCGIFMEDIA ioctl is OK on the interface. */ ifi->mediareqok = 1; ifi->active = interface_status(ifi); if (!ifi->mediareqok) { /* * probe routers periodically even if the link status * does not change. */ ifi->probeinterval = PROBE_INTERVAL; } /* activate interface: interface_up returns 0 on success */ flags = interface_up(ifi->ifname); if (flags == 0) ifi->state = IFS_DELAY; else if (flags == IFS_TENTATIVE) ifi->state = IFS_TENTATIVE; else ifi->state = IFS_DOWN; rtsol_timer_update(ifi); TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next); return (0); bad: free(ifi->sdl); free(ifi); return (-1); } -void -iflist_init(void) -{ - struct ifinfo *ifi; - - while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) { - TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next); - if (ifi->sdl != NULL) - free(ifi->sdl); - if (ifi->rs_data != NULL) - free(ifi->rs_data); - free(ifi); - } -} - struct rainfo * find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6) { struct rainfo *rai; TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr, sizeof(rai->rai_saddr.sin6_addr)) == 0) return (rai); return (NULL); } struct ifinfo * find_ifinfo(int ifindex) { struct ifinfo *ifi; TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) { if (ifi->sdl->sdl_index == ifindex) return (ifi); } return (NULL); } static int make_packet(struct ifinfo *ifi) { size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0; struct nd_router_solicit *rs; char *buf; if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) { warnmsg(LOG_INFO, __func__, "link-layer address option has null length" " on %s. Treat as not included.", ifi->ifname); } packlen += lladdroptlen; ifi->rs_datalen = packlen; /* allocate buffer */ if ((buf = malloc(packlen)) == NULL) { warnmsg(LOG_ERR, __func__, "memory allocation failed for %s", ifi->ifname); return (-1); } ifi->rs_data = buf; /* fill in the message */ rs = (struct nd_router_solicit *)buf; rs->nd_rs_type = ND_ROUTER_SOLICIT; rs->nd_rs_code = 0; rs->nd_rs_cksum = 0; rs->nd_rs_reserved = 0; buf += sizeof(*rs); /* fill in source link-layer address option */ if (lladdroptlen) lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf); return (0); } static struct timespec * rtsol_check_timer(void) { static struct timespec returnval; struct timespec now, rtsol_timer; struct ifinfo *ifi; struct rainfo *rai; struct ra_opt *rao, *raotmp; int flags; clock_gettime(CLOCK_MONOTONIC_FAST, &now); rtsol_timer = tm_max; TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) { if (TS_CMP(&ifi->expire, &now, <=)) { warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, " "state = %d", ifi->ifname, ifi->state); while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) { /* Remove all RA options. */ TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next); while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) != NULL) { TAILQ_REMOVE(&rai->rai_ra_opt, rao, rao_next); if (rao->rao_msg != NULL) free(rao->rao_msg); free(rao); } free(rai); } switch (ifi->state) { case IFS_DOWN: case IFS_TENTATIVE: /* interface_up returns 0 on success */ flags = interface_up(ifi->ifname); if (flags == 0) ifi->state = IFS_DELAY; else if (flags == IFS_TENTATIVE) ifi->state = IFS_TENTATIVE; else ifi->state = IFS_DOWN; break; case IFS_IDLE: { int oldstatus = ifi->active; int probe = 0; ifi->active = interface_status(ifi); if (oldstatus != ifi->active) { warnmsg(LOG_DEBUG, __func__, "%s status is changed" " from %d to %d", ifi->ifname, oldstatus, ifi->active); probe = 1; ifi->state = IFS_DELAY; } else if (ifi->probeinterval && (ifi->probetimer -= ifi->timer.tv_sec) <= 0) { /* probe timer expired */ ifi->probetimer = ifi->probeinterval; probe = 1; ifi->state = IFS_PROBE; } /* * If we need a probe, clear the previous * status wrt the "other" configuration. */ if (probe) ifi->otherconfig = 0; if (probe && mobile_node) defrouter_probe(ifi); break; } case IFS_DELAY: ifi->state = IFS_PROBE; sendpacket(ifi); break; case IFS_PROBE: if (ifi->probes < MAX_RTR_SOLICITATIONS) sendpacket(ifi); else { warnmsg(LOG_INFO, __func__, "No answer after sending %d RSs", ifi->probes); ifi->probes = 0; ifi->state = IFS_IDLE; } break; } rtsol_timer_update(ifi); } else { /* Expiration check for RA options. */ int expire = 0; TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) { TAILQ_FOREACH_SAFE(rao, &rai->rai_ra_opt, rao_next, raotmp) { warnmsg(LOG_DEBUG, __func__, "RA expiration timer: " "type=%d, msg=%s, expire=%s", rao->rao_type, (char *)rao->rao_msg, sec2str(&rao->rao_expire)); if (TS_CMP(&now, &rao->rao_expire, >=)) { warnmsg(LOG_DEBUG, __func__, "RA expiration timer: " "expired."); TAILQ_REMOVE(&rai->rai_ra_opt, rao, rao_next); if (rao->rao_msg != NULL) free(rao->rao_msg); free(rao); expire = 1; } } } if (expire) ra_opt_handler(ifi); } if (TS_CMP(&ifi->expire, &rtsol_timer, <)) rtsol_timer = ifi->expire; } if (TS_CMP(&rtsol_timer, &tm_max, ==)) { warnmsg(LOG_DEBUG, __func__, "there is no timer"); return (NULL); } else if (TS_CMP(&rtsol_timer, &now, <)) /* this may occur when the interval is too small */ returnval.tv_sec = returnval.tv_nsec = 0; else TS_SUB(&rtsol_timer, &now, &returnval); now.tv_sec += returnval.tv_sec; now.tv_nsec += returnval.tv_nsec; warnmsg(LOG_DEBUG, __func__, "New timer is %s", sec2str(&now)); return (&returnval); } void rtsol_timer_update(struct ifinfo *ifi) { #define MILLION 1000000 #define DADRETRY 10 /* XXX: adhoc */ long interval; struct timespec now; bzero(&ifi->timer, sizeof(ifi->timer)); switch (ifi->state) { case IFS_DOWN: case IFS_TENTATIVE: if (++ifi->dadcount > DADRETRY) { ifi->dadcount = 0; ifi->timer.tv_sec = PROBE_INTERVAL; } else ifi->timer.tv_sec = 1; break; case IFS_IDLE: if (mobile_node) { /* XXX should be configurable */ ifi->timer.tv_sec = 3; } else ifi->timer = tm_max; /* stop timer(valid?) */ break; case IFS_DELAY: interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION); ifi->timer.tv_sec = interval / MILLION; ifi->timer.tv_nsec = (interval % MILLION) * 1000; break; case IFS_PROBE: if (ifi->probes < MAX_RTR_SOLICITATIONS) ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL; else { /* * After sending MAX_RTR_SOLICITATIONS solicitations, * we're just waiting for possible replies; there * will be no more solicitation. Thus, we change * the timer value to MAX_RTR_SOLICITATION_DELAY based * on RFC 2461, Section 6.3.7. */ ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY; } break; default: warnmsg(LOG_ERR, __func__, "illegal interface state(%d) on %s", ifi->state, ifi->ifname); return; } /* reset the timer */ if (TS_CMP(&ifi->timer, &tm_max, ==)) { ifi->expire = tm_max; warnmsg(LOG_DEBUG, __func__, "stop timer for %s", ifi->ifname); } else { clock_gettime(CLOCK_MONOTONIC_FAST, &now); TS_ADD(&now, &ifi->timer, &ifi->expire); now.tv_sec += ifi->timer.tv_sec; now.tv_nsec += ifi->timer.tv_nsec; warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s", ifi->ifname, sec2str(&now)); } #undef MILLION } - -/* timer related utility functions */ -#define MILLION 1000000 #ifndef SMALL static void rtsold_set_dump_file(int sig __unused) { do_dump = 1; } #endif static void usage(void) { #ifndef SMALL fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] " "[-p pidfile] [-R script-name] interface ...\n"); fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] " "[-p pidfile] [-R script-name] -a\n"); #else fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] " "[-p pidfile] [-R script-name] interface ...\n"); fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] " "[-p pidfile] [-R script-name] -a\n"); #endif } void warnmsg(int priority, const char *func, const char *msg, ...) { va_list ap; char buf[BUFSIZ]; va_start(ap, msg); if (fflag) { if (priority <= log_upto) { (void)vfprintf(stderr, msg, ap); (void)fprintf(stderr, "\n"); } } else { snprintf(buf, sizeof(buf), "<%s> %s", func, msg); msg = buf; vsyslog(priority, msg, ap); } va_end(ap); } /* * return a list of interfaces which is suitable to sending an RS. */ static char ** autoifprobe(void) { static char **argv = NULL; static int n = 0; char **a; int s = 0, i, found; struct ifaddrs *ifap, *ifa; struct in6_ndireq nd; /* initialize */ while (n--) free(argv[n]); if (argv) { free(argv); argv = NULL; } n = 0; if (getifaddrs(&ifap) != 0) return (NULL); if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { warnmsg(LOG_ERR, __func__, "socket"); exit(1); } /* find an ethernet */ for (ifa = ifap; ifa; ifa = ifa->ifa_next) { if ((ifa->ifa_flags & IFF_UP) == 0) continue; if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0) continue; if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) continue; if ((ifa->ifa_flags & IFF_MULTICAST) == 0) continue; if (ifa->ifa_addr->sa_family != AF_INET6) continue; found = 0; for (i = 0; i < n; i++) { if (strcmp(argv[i], ifa->ifa_name) == 0) { found++; break; } } if (found) continue; /* * Skip the interfaces which IPv6 and/or accepting RA * is disabled. */ if (!Fflag) { memset(&nd, 0, sizeof(nd)); strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname)); if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFINFO_IN6)"); exit(1); } if ((nd.ndi.flags & ND6_IFF_IFDISABLED)) continue; if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) continue; } /* if we find multiple candidates, just warn. */ if (n != 0 && dflag > 1) warnmsg(LOG_WARNING, __func__, "multiple interfaces found"); a = realloc(argv, (n + 1) * sizeof(char *)); if (a == NULL) { warnmsg(LOG_ERR, __func__, "realloc"); exit(1); } argv = a; argv[n] = strdup(ifa->ifa_name); if (!argv[n]) { warnmsg(LOG_ERR, __func__, "malloc"); exit(1); } n++; } if (n) { a = realloc(argv, (n + 1) * sizeof(char *)); if (a == NULL) { warnmsg(LOG_ERR, __func__, "realloc"); exit(1); } argv = a; argv[n] = NULL; if (dflag > 0) { for (i = 0; i < n; i++) warnmsg(LOG_WARNING, __func__, "probing %s", argv[i]); } } if (!Fflag) close(s); freeifaddrs(ifap); return (argv); } Index: head/usr.sbin/rtsold/rtsold.h =================================================================== --- head/usr.sbin/rtsold/rtsold.h (revision 339455) +++ head/usr.sbin/rtsold/rtsold.h (revision 339456) @@ -1,194 +1,191 @@ /* $KAME: rtsold.h,v 1.19 2003/04/16 09:48:15 itojun Exp $ */ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * 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. Neither the name of the project 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 PROJECT 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 PROJECT 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. * * $FreeBSD$ */ struct script_msg { TAILQ_ENTRY(script_msg) sm_next; char *sm_msg; }; TAILQ_HEAD(script_msg_head_t, script_msg); struct ra_opt { TAILQ_ENTRY(ra_opt) rao_next; u_int8_t rao_type; struct timespec rao_expire; size_t rao_len; void *rao_msg; }; TAILQ_HEAD(rainfo_head, ra_opt); struct rainfo { TAILQ_ENTRY(rainfo) rai_next; struct ifinfo *rai_ifinfo; struct sockaddr_in6 rai_saddr; TAILQ_HEAD(, ra_opt) rai_ra_opt; }; struct ifinfo { TAILQ_ENTRY(ifinfo) ifi_next; /* pointer to the next interface */ struct sockaddr_dl *sdl; /* link-layer address */ char ifname[IFNAMSIZ]; /* interface name */ u_int32_t linkid; /* link ID of this interface */ int active; /* interface status */ int probeinterval; /* interval of probe timer (if necessary) */ int probetimer; /* rest of probe timer */ int mediareqok; /* whether the IF supports SIOCGIFMEDIA */ int otherconfig; /* need a separate protocol for the "other" * configuration */ int state; int probes; int dadcount; struct timespec timer; struct timespec expire; int errors; /* # of errors we've got - detect wedge */ #define IFI_DNSOPT_STATE_NOINFO 0 #define IFI_DNSOPT_STATE_RECEIVED 1 int ifi_rdnss; /* RDNSS option state */ int ifi_dnssl; /* DNSSL option state */ int racnt; /* total # of valid RAs it have got */ TAILQ_HEAD(, rainfo) ifi_rainfo; size_t rs_datalen; u_char *rs_data; }; /* per interface status */ #define IFS_IDLE 0 #define IFS_DELAY 1 #define IFS_PROBE 2 #define IFS_DOWN 3 #define IFS_TENTATIVE 4 /* Interface list */ extern TAILQ_HEAD(ifinfo_head_t, ifinfo) ifinfo_head; #define DNSINFO_ORIGIN_LABEL "slaac" /* * RFC 3542 API deprecates IPV6_PKTINFO in favor of * IPV6_RECVPKTINFO */ #ifndef IPV6_RECVPKTINFO #ifdef IPV6_PKTINFO #define IPV6_RECVPKTINFO IPV6_PKTINFO #endif #endif /* * RFC 3542 API deprecates IPV6_HOPLIMIT in favor of * IPV6_RECVHOPLIMIT */ #ifndef IPV6_RECVHOPLIMIT #ifdef IPV6_HOPLIMIT #define IPV6_RECVHOPLIMIT IPV6_HOPLIMIT #endif #endif #ifndef IN6ADDR_LINKLOCAL_ALLROUTERS_INIT #define IN6ADDR_LINKLOCAL_ALLROUTERS_INIT \ {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }}} #endif #define TS_CMP(tsp, usp, cmp) \ (((tsp)->tv_sec == (usp)->tv_sec) ? \ ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \ ((tsp)->tv_sec cmp (usp)->tv_sec)) #define TS_ADD(tsp, usp, vsp) \ do { \ (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \ (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \ if ((vsp)->tv_nsec >= 1000000000L) { \ (vsp)->tv_sec++; \ (vsp)->tv_nsec -= 1000000000L; \ } \ } while (0) #define TS_SUB(tsp, usp, vsp) \ do { \ (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ if ((vsp)->tv_nsec < 0) { \ (vsp)->tv_sec--; \ (vsp)->tv_nsec += 1000000000L; \ } \ } while (0) /* rtsold.c */ extern struct timespec tm_max; extern int dflag; extern int aflag; extern int Fflag; extern int uflag; extern const char *otherconf_script; extern const char *resolvconf_script; -extern void iflist_init(void); struct ifinfo *find_ifinfo(int); struct rainfo *find_rainfo(struct ifinfo *, struct sockaddr_in6 *); void rtsol_timer_update(struct ifinfo *); extern void warnmsg(int, const char *, const char *, ...) __attribute__((__format__(__printf__, 3, 4))); extern int ra_opt_handler(struct ifinfo *); /* if.c */ extern int ifinit(void); extern int interface_up(char *); extern int interface_status(struct ifinfo *); extern int lladdropt_length(struct sockaddr_dl *); extern void lladdropt_fill(struct sockaddr_dl *, struct nd_opt_hdr *); extern struct sockaddr_dl *if_nametosdl(char *); -extern int getinet6sysctl(int); -extern int setinet6sysctl(int, int); /* rtsol.c */ extern int rssock; extern int sockopen(void); extern void sendpacket(struct ifinfo *); extern void rtsol_input(int); /* probe.c */ extern int probe_init(void); extern void defrouter_probe(struct ifinfo *); /* dump.c */ extern void rtsold_dump_file(const char *); extern const char *sec2str(const struct timespec *); /* rtsock.c */ extern int rtsock_open(void); extern int rtsock_input(int);