Lines Matching +full:cm +full:- +full:poll +full:- +full:interval
3 /*-
4 * SPDX-License-Identifier: BSD-3-Clause
70 #include <poll.h>
172 fprintf(stderr, "usage: rtadvd [-dDfRs] "
173 "[-c configfile] [-C ctlsock] [-M ifname] [-p pidfile]\n");
188 while ((ch = getopt(argc, argv, "c:C:dDfhM:p:Rs")) != -1) {
210 "the -R option is currently ignored.\n");
224 argc -= optind;
264 if (ctrlsock.si_fd == -1) {
281 set[PFD_RTSOCK].fd = -1;
324 (long int)timeout->tv_sec,
325 (long int)timeout->tv_nsec / 1000);
331 if ((i = poll(set, nitems(set),
332 timeout ? (timeout->tv_sec * 1000 +
333 timeout->tv_nsec / 1000 / 1000) : INFTIM)) < 0) {
337 syslog(LOG_ERR, "poll() failed: %s",
343 if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN)
353 if (fd == -1)
382 if (!(ifi->ifi_flags & IFF_UP) &&
383 ifi->ifi_ra_timer != NULL) {
384 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
385 rtadvd_remove_timer(ifi->ifi_ra_timer);
386 ifi->ifi_ra_timer = NULL;
389 __func__, ifi->ifi_ifname,
390 ifi->ifi_ifindex);
394 if (ifi->ifi_ra_timer != NULL)
412 rai->rai_lifetime = 0;
413 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next)
414 rdn->rd_ltime = 0;
415 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next)
416 dns->dn_ltime = 0;
419 if (!ifi->ifi_persist)
421 if (ifi->ifi_state == IFI_STATE_UNCONFIGURED)
423 if (ifi->ifi_ra_timer == NULL)
425 if (ifi->ifi_ra_lastsent.tv_sec == 0 &&
426 ifi->ifi_ra_lastsent.tv_nsec == 0 &&
427 ifi->ifi_ra_timer != NULL) {
432 rtadvd_remove_timer(ifi->ifi_ra_timer);
433 ifi->ifi_ra_timer = NULL;
434 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
438 ifi->ifi_state = IFI_STATE_TRANSITIVE;
440 /* Mark as the shut-down state. */
441 ifi->ifi_rainfo_trans = ifi->ifi_rainfo;
442 ifi->ifi_rainfo = NULL;
444 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
445 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
447 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
448 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
449 ifi->ifi_ra_timer);
477 n = read(s->si_fd, msg, sizeof(msg));
480 "(type = %d, len = %d)", __func__, rtm->rtm_type, n);
482 if (n > rtm->rtm_msglen) {
491 __func__, n, rtm->rtm_msglen);
494 n = rtm->rtm_msglen;
511 type = ((struct rt_msghdr *)next)->rtm_type;
519 ifindex = (int)((struct ifa_msghdr *)next)->ifam_index;
522 ifindex = (int)((struct if_msghdr *)next)->ifm_index;
526 switch (ifan->ifan_what) {
533 __func__, __LINE__, ifan->ifan_what);
538 __func__, ifan->ifan_index, ifan->ifan_what);
539 switch (ifan->ifan_what) {
543 ifan->ifan_index);
544 update_ifinfo(&ifilist, ifan->ifan_index);
545 loadconfig_index(ifan->ifan_index);
550 ifan->ifan_index);
551 rm_ifinfo_index(ifan->ifan_index);
555 if (ifi->ifi_ifindex
556 == ifan->ifan_index) {
557 ifi->ifi_ifindex = 0;
561 update_ifinfo(&ifilist, ifan->ifan_index);
580 rai = ifi->ifi_rainfo;
585 __func__, ifi->ifi_ifname);
589 oldifflags = ifi->ifi_flags;
610 if (pfx->pfx_timer) {
626 plen, ifi->ifi_ifname);
654 plen, ifi->ifi_ifname);
675 !(ifi->ifi_flags & IFF_UP)) {
678 ifi->ifi_ifname);
679 rtadvd_remove_timer(ifi->ifi_ra_timer);
680 ifi->ifi_ra_timer = NULL;
682 (ifi->ifi_flags & IFF_UP)) {
685 ifi->ifi_ifname);
687 ifi->ifi_state = IFI_STATE_TRANSITIVE;
688 ifi->ifi_burstcount =
690 ifi->ifi_burstinterval =
693 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
695 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
696 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
697 ifi->ifi_ra_timer);
699 (ifi->ifi_flags & IFF_UP)) {
719 struct cmsghdr *cm;
737 if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0)
741 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
742 cm;
743 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
744 if (cm->cmsg_level == IPPROTO_IPV6 &&
745 cm->cmsg_type == IPV6_PKTINFO &&
746 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
747 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
748 ifindex = pi->ipi6_ifindex;
749 dst = pi->ipi6_addr;
751 if (cm->cmsg_level == IPPROTO_IPV6 &&
752 cm->cmsg_type == IPV6_HOPLIMIT &&
753 cm->cmsg_len == CMSG_LEN(sizeof(int)))
754 hlimp = (int *)CMSG_DATA(cm);
769 ifi = if_indextoifinfo(pi->ipi6_ifindex);
770 if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) {
774 (ifi == NULL) ? "[gone]" : ifi->ifi_ifname);
796 switch (icp->icmp6_type) {
799 * Message verification - RFC 4861 6.1.1
810 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
813 if (icp->icmp6_code) {
817 icp->icmp6_code,
820 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
829 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
836 * Message verification - RFC 4861 6.1.2
841 "RA with non-linklocal source address "
845 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
855 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
858 if (icp->icmp6_code) {
862 icp->icmp6_code,
865 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
874 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
895 syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type);
914 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
915 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
921 len - sizeof(struct nd_router_solicit),
926 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
928 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
934 * must be no source link-layer address option in the message.
937 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
940 "<%s> RS from unspecified src on %s has a link-layer"
942 __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf));
946 ifi = if_indextoifinfo(pi->ipi6_ifindex);
950 __func__, pi->ipi6_ifindex);
953 rai = ifi->ifi_rainfo;
958 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
962 rai->rai_ifinfo->ifi_rsinput++;
965 * Decide whether to send RA according to the rate-limit
972 sol->sol_addr = *from;
974 sol->sol_addr.sin6_flowinfo = 0;
975 TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next);
982 if (ifi->ifi_rs_waitcount++)
995 struct timespec interval, now, min_delay, tm_tmp, *rest;
997 if (ifi->ifi_ra_timer == NULL)
1004 * already-scheduled time. RFC 4861 6.2.6
1007 interval.tv_sec = 0;
1008 interval.tv_nsec = delay * 1000;
1009 rest = rtadvd_timer_rest(ifi->ifi_ra_timer);
1010 if (TS_CMP(rest, &interval, <)) {
1013 interval = *rest;
1024 TS_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp);
1029 TS_ADD(&min_delay, &interval, &interval);
1031 rtadvd_set_timer(&interval, ifi->ifi_ra_timer);
1040 if (ifi->ifi_ifindex == idx)
1059 return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV);
1077 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
1078 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
1084 len - sizeof(struct nd_router_advert), &ndopts,
1091 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1092 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1100 ifi = if_indextoifinfo(pi->ipi6_ifindex);
1101 if (ifi->ifi_rainfo == NULL) {
1103 "<%s> received RA from %s on non-advertising"
1106 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1107 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1111 rai = ifi->ifi_rainfo;
1112 ifi->ifi_rainput++;
1113 syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__,
1114 ifi->ifi_rainput);
1117 if (nra->nd_ra_curhoplimit && rai->rai_hoplimit &&
1118 nra->nd_ra_curhoplimit != rai->rai_hoplimit) {
1122 ifi->ifi_ifname, nra->nd_ra_curhoplimit,
1123 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1124 sizeof(ntopbuf)), rai->rai_hoplimit);
1128 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
1129 rai->rai_managedflg) {
1133 ifi->ifi_ifname, on_off[!rai->rai_managedflg],
1134 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1135 sizeof(ntopbuf)), on_off[rai->rai_managedflg]);
1139 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1140 rai->rai_otherflg) {
1144 ifi->ifi_ifname, on_off[!rai->rai_otherflg],
1145 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1146 sizeof(ntopbuf)), on_off[rai->rai_otherflg]);
1150 /* S "IPv6-Only" (Six, Silence-IPv4) flag */
1151 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_IPV6_ONLY) !=
1152 rai->rai_ipv6onlyflg) {
1156 ifi->ifi_ifname, on_off[!rai->rai_ipv6onlyflg],
1157 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1158 sizeof(ntopbuf)), on_off[rai->rai_ipv6onlyflg]);
1163 reachabletime = ntohl(nra->nd_ra_reachable);
1164 if (reachabletime && rai->rai_reachabletime &&
1165 reachabletime != rai->rai_reachabletime) {
1169 ifi->ifi_ifname, reachabletime,
1170 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1171 sizeof(ntopbuf)), rai->rai_reachabletime);
1175 retranstimer = ntohl(nra->nd_ra_retransmit);
1176 if (retranstimer && rai->rai_retranstimer &&
1177 retranstimer != rai->rai_retranstimer) {
1181 ifi->ifi_ifname, retranstimer,
1182 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1183 sizeof(ntopbuf)), rai->rai_retranstimer);
1188 mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu);
1189 if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) {
1193 ifi->ifi_ifname, mtu,
1194 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1195 sizeof(ntopbuf)), rai->rai_linkmtu);
1208 if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt,
1214 ifi->ifi_rainconsistent++;
1223 return (u >= v ? u - v : v - u);
1226 /* return a non-zero value if the received prefix is inconsistent with ours */
1240 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1243 ifi = rai->rai_ifinfo;
1245 * log if the adveritsed prefix has link-local scope(sanity check?)
1247 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
1249 "<%s> link-local prefix %s/%d is advertised "
1252 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1254 pinfo->nd_opt_pi_prefix_len,
1255 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1256 sizeof(ntopbuf)), ifi->ifi_ifname);
1258 if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1259 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1263 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1265 pinfo->nd_opt_pi_prefix_len,
1266 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1267 sizeof(ntopbuf)), ifi->ifi_ifname);
1271 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1272 if (pfx->pfx_pltimeexpire) {
1283 if (!pfx->pfx_timer && rai->rai_clockskew &&
1284 udiff(preferred_time, pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
1290 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1292 pinfo->nd_opt_pi_prefix_len,
1293 ifi->ifi_ifname, preferred_time,
1294 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1295 sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
1298 } else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
1304 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1306 pinfo->nd_opt_pi_prefix_len,
1307 ifi->ifi_ifname, preferred_time,
1308 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1309 sizeof(ntopbuf)), pfx->pfx_preflifetime);
1311 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1312 if (pfx->pfx_vltimeexpire) {
1316 if (!pfx->pfx_timer && rai->rai_clockskew &&
1317 udiff(valid_time, pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
1323 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1325 pinfo->nd_opt_pi_prefix_len,
1326 ifi->ifi_ifname, preferred_time,
1327 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1328 sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
1331 } else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
1337 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1339 pinfo->nd_opt_pi_prefix_len,
1340 ifi->ifi_ifname, valid_time,
1341 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1342 sizeof(ntopbuf)), pfx->pfx_validlifetime);
1356 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1357 if (plen != pfx->pfx_prefixlen)
1362 bitmask = 0xff << (8 - bitlen);
1364 if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
1368 ((prefix->s6_addr[bytelen] & bitmask) ==
1369 (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1390 bitmask = 0xff << (8 - bitlen);
1396 ((p0->s6_addr[bytelen] & bitmask) ==
1397 (p1->s6_addr[bytelen] & bitmask))) {
1410 for (; limit > 0; limit -= optlen) {
1417 if (hdr->nd_opt_len == 0) {
1420 __func__, hdr->nd_opt_type);
1423 optlen = hdr->nd_opt_len << 3;
1429 if (hdr->nd_opt_type > ND_OPT_MTU &&
1430 hdr->nd_opt_type != ND_OPT_RDNSS &&
1431 hdr->nd_opt_type != ND_OPT_DNSSL) {
1433 __func__, hdr->nd_opt_type);
1437 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1439 __func__, hdr->nd_opt_type);
1444 * Option length check. Do it here for all fixed-length
1447 switch (hdr->nd_opt_type) {
1454 (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1459 (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1471 switch (hdr->nd_opt_type) {
1479 if (ndopts->opt_array[hdr->nd_opt_type]) {
1482 __func__, hdr->nd_opt_type);
1484 ndopts->opt_array[hdr->nd_opt_type] = hdr;
1490 if (ndopts->opt_pi == 0) {
1491 ndopts->opt_pi =
1501 nol->nol_opt = hdr;
1502 TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
1516 return (-1);
1524 while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1525 TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1560 if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1566 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1574 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1586 if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1619 if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1637 if (ifi->ifi_ifindex == idx)
1655 struct cmsghdr *cm;
1660 switch (ifi->ifi_state) {
1662 rai = ifi->ifi_rainfo;
1665 rai = ifi->ifi_rainfo_trans;
1670 __func__, ifi->ifi_ifname);
1678 __func__, ifi->ifi_ifname);
1681 if (!(ifi->ifi_flags & IFF_UP)) {
1684 __func__, ifi->ifi_ifname);
1704 rai->rai_lifetime,
1705 check_accept_rtadv(ifi->ifi_ifindex),
1707 ifi->ifi_ifname);
1709 if (rai->rai_lifetime != 0) {
1712 "non-zero lifetime RA "
1717 if (check_accept_rtadv(ifi->ifi_ifindex)) {
1719 "non-zero lifetime RA "
1721 " Ignored.", ifi->ifi_ifname);
1729 sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1730 sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1732 cm = CMSG_FIRSTHDR(&sndmhdr);
1734 cm->cmsg_level = IPPROTO_IPV6;
1735 cm->cmsg_type = IPV6_PKTINFO;
1736 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1737 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1738 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1739 pi->ipi6_ifindex = ifi->ifi_ifindex;
1745 cm = CMSG_NXTHDR(&sndmhdr, cm);
1746 cm->cmsg_level = IPPROTO_IPV6;
1747 cm->cmsg_type = IPV6_HOPLIMIT;
1748 cm->cmsg_len = CMSG_LEN(sizeof(int));
1749 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1754 __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
1758 if (i < 0 || (size_t)i != rai->rai_ra_datalen) {
1761 __func__, ifi->ifi_ifname,
1771 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1772 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
1777 clock_gettime(CLOCK_MONOTONIC_FAST, &ifi->ifi_ra_lastsent);
1780 ifi->ifi_rs_waitcount = 0;
1781 ifi->ifi_raoutput++;
1783 switch (ifi->ifi_state) {
1785 if (ifi->ifi_burstcount > 0)
1786 ifi->ifi_burstcount--;
1789 ifi->ifi_burstcount--;
1790 if (ifi->ifi_burstcount == 0) {
1791 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1793 if (ifi->ifi_rainfo_trans != NULL)
1794 ifi->ifi_rainfo_trans = NULL;
1798 if (ifi->ifi_rainfo_trans != NULL) {
1799 rm_rainfo(ifi->ifi_rainfo_trans);
1800 ifi->ifi_rainfo_trans = NULL;
1803 if (ifi->ifi_rainfo != NULL) {
1805 * TRANSITIVE -> CONFIGURED
1812 ifi->ifi_state = IFI_STATE_CONFIGURED;
1816 ifi->ifi_ifname);
1819 * TRANSITIVE -> UNCONFIGURED
1827 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1831 ifi->ifi_ifname);
1833 ifi->ifi_ifindex);
1850 __func__, ifi->ifi_ifname);
1854 return (ifi->ifi_ra_timer);
1861 uint16_t interval;
1866 rai = ifi->ifi_rainfo;
1867 interval = 0;
1869 switch (ifi->ifi_state) {
1877 * uniformly-distributed random value between the
1881 interval = rai->rai_mininterval;
1882 interval += arc4random_uniform(rai->rai_maxinterval -
1883 rai->rai_mininterval);
1889 * interval is greater than
1899 interval = ifi->ifi_burstinterval;
1903 tm->tv_sec = interval;
1904 tm->tv_nsec = 0;
1908 __func__, ifi->ifi_ifname,
1909 (long int)tm->tv_sec, (long int)tm->tv_nsec / 1000);