Lines Matching +full:nic +full:- +full:301
2 * ntp_io.c - input/output routines for ntpd. The socket-opening code
43 #include "ntpd-opts.h"
90 * NIC rule entry
104 * NIC rule listhead. Entries are added at the head so that the first
304 endpt * mc4_list; /* IPv4 mcast-capable unicast endpts */
305 endpt * mc6_list; /* IPv6 mcast-capable unicast endpts */
383 for (i = maxactivefd - 1; i >= 0; i--) in maintain_activefds()
410 ? ((rb->dstadr != NULL) in collect_timing()
411 ? stoa(&rb->recv_srcadr) in collect_timing()
412 : "-REFCLOCK-") in collect_timing()
413 : "-", in collect_timing()
424 * - keep a current list of active interfaces in order
428 * - all interfaces keep a list of peers that are referencing
429 * the interface in order to quickly re-assign the peers to
432 * - have a preconfigured socket ready with the right local address
434 * - have an address list for all destination addresses used within ntpd
436 * - facilitate updating the internal interface list with respect to
441 * - mapping of multicast addresses to the interface affected is not always
442 * one to one - especially on hosts with multiple interfaces
447 * - on some systems it is perfectly legal to assign the same address to
457 * init_io - initialize I/O module.
465 endpt_scan_period = 301; in init_io()
497 * io_open_sockets - call socket creation routine
526 * We face a dilemma here -- sockets are FDs under POSIX and
529 * with UNIX-like systems and does not truncate values on Win64.
535 printf("fd = %lld\n", (long long)itf->fd); in interface_dump()
536 printf("bfd = %lld\n", (long long)itf->bfd); in interface_dump()
537 printf("sin = %s,\n", stoa(&itf->sin)); in interface_dump()
538 printf("bcast = %s,\n", stoa(&itf->bcast)); in interface_dump()
539 printf("mask = %s,\n", stoa(&itf->mask)); in interface_dump()
540 printf("name = %s\n", itf->name); in interface_dump()
541 printf("flags = 0x%08x\n", itf->flags); in interface_dump()
542 printf("last_ttl = %d\n", itf->last_ttl); in interface_dump()
543 printf("addr_refid = %08x\n", itf->addr_refid); in interface_dump()
544 printf("num_mcast = %d\n", itf->num_mcast); in interface_dump()
545 printf("received = %ld\n", itf->received); in interface_dump()
546 printf("sent = %ld\n", itf->sent); in interface_dump()
547 printf("notsent = %ld\n", itf->notsent); in interface_dump()
548 printf("ifindex = %u\n", itf->ifindex); in interface_dump()
549 printf("peercnt = %u\n", itf->peercnt); in interface_dump()
550 printf("phase = %u\n", itf->phase); in interface_dump()
555 * print_interface - helper to output debug information
562 iface->ifnum, in print_interface()
563 (long long)iface->fd, in print_interface()
564 (long long)iface->bfd, in print_interface()
565 iface->name, in print_interface()
566 iface->flags, in print_interface()
567 iface->ifindex, in print_interface()
568 stoa(&iface->sin)); in print_interface()
569 if (AF_INET == iface->family) { in print_interface()
570 if (iface->flags & INT_BROADCAST) in print_interface()
571 printf(", bcast=%s", stoa(&iface->bcast)); in print_interface()
572 printf(", mask=%s", stoa(&iface->mask)); in print_interface()
575 (iface->ignore_packets) in print_interface()
579 if (debug > 4) /* in-depth debugging only */ in print_interface()
594 reader->fd = INVALID_SOCKET; in new_asyncio_reader()
619 add_fd_to_list(reader->fd, type); in add_asyncio_reader()
635 if (reader->fd != INVALID_SOCKET) { in remove_asyncio_reader()
636 close_and_delete_fd_from_list(reader->fd, NULL); in remove_asyncio_reader()
638 reader->fd = INVALID_SOCKET; in remove_asyncio_reader()
711 * interface list enumerator - visitor pattern
722 for (ifi.ep = ep_list; ifi.ep != NULL; ifi.ep = ifi.ep->elink) in interface_enumerate()
735 ep->fd = INVALID_SOCKET; in init_interface()
736 ep->bfd = INVALID_SOCKET; in init_interface()
737 ep->phase = sys_interphase; in init_interface()
760 iface->ifnum = sys_ifnum++; in new_interface()
761 iface->starttime = current_time; in new_interface()
765 msyslog(LOG_EMERG, "cannot register interface with IO engine -- will exit now"); in new_interface()
803 ep->addr_refid = addr2refid(&ep->sin); in add_interface()
805 if (IS_IPV6(&ep->sin)) { in add_interface()
806 ep->old_refid = BYTESWAP32(ep->addr_refid); in add_interface()
809 /* link at tail so ntpdc -c ifstats index increases each row */ in add_interface()
813 /* the rest is for enabled multicast-capable addresses only */ in add_interface()
814 if (ep->ignore_packets || !(INT_MULTICAST & ep->flags) || in add_interface()
815 INT_LOOPBACK & ep->flags) in add_interface()
818 if (AF_INET6 == ep->family) in add_interface()
821 pmclisthead = (AF_INET == ep->family) in add_interface()
830 scan_next = scan->mclink; in add_interface()
831 if ( ep->family != scan->family in add_interface()
832 || ep->ifindex != scan->ifindex) { in add_interface()
835 same_subnet = addr_samesubnet(&ep->sin, &ep->mask, in add_interface()
836 &scan->sin, &scan->mask); in add_interface()
838 DPRINTF(4, ("did not add %s to multicast-capable list" in add_interface()
840 stoa(&ep->sin), stoa(&scan->sin))); in add_interface()
845 if (INVALID_SOCKET == ep->fd) in add_interface()
851 switch (AF(&ep->sin)) { in add_interface()
854 rc = setsockopt(ep->fd, IPPROTO_IP, in add_interface()
856 (void *)&NSRCADR(&ep->sin), in add_interface()
857 sizeof(NSRCADR(&ep->sin))); in add_interface()
861 stoa(&ep->sin)); in add_interface()
866 rc = setsockopt(ep->fd, IPPROTO_IPV6, in add_interface()
868 (void *)&ep->ifindex, in add_interface()
869 sizeof(ep->ifindex)); in add_interface()
871 if (rc && ep->ifindex != SCOPE(&ep->sin)) in add_interface()
874 ep->ifindex, stoa(&ep->sin)); in add_interface()
897 if (!ep->ignore_packets && INT_MULTICAST & ep->flags) { in remove_interface()
898 pmclisthead = (AF_INET == ep->family) in remove_interface()
902 DPRINTF(4, ("%s %s IPv%s multicast-capable unicast local address list\n", in remove_interface()
903 stoa(&ep->sin), in remove_interface()
907 (AF_INET == ep->family) in remove_interface()
913 if (ep->fd != INVALID_SOCKET) { in remove_interface()
918 ep->ifnum, in remove_interface()
919 ep->name, in remove_interface()
920 stoa(&ep->sin), in remove_interface()
921 SRCPORT(&ep->sin), in remove_interface()
922 ep->received, in remove_interface()
923 ep->sent, in remove_interface()
924 ep->notsent, in remove_interface()
925 current_time - ep->starttime); in remove_interface()
926 close_and_delete_fd_from_list(ep->fd, ep); in remove_interface()
927 ep->fd = INVALID_SOCKET; in remove_interface()
930 if (ep->bfd != INVALID_SOCKET) { in remove_interface()
933 stoa(&ep->bcast), ep->ifnum, ep->name); in remove_interface()
934 close_and_delete_fd_from_list(ep->bfd, ep); in remove_interface()
935 ep->bfd = INVALID_SOCKET; in remove_interface()
941 ninterfaces--; in remove_interface()
945 SET_HOSTMASK(&resmask, AF(&ep->sin)); in remove_interface()
946 success = hack_restrict(RESTRICT_REMOVEIF, &ep->sin, &resmask, 0, in remove_interface()
950 "unable to remove self-restriction for %s", in remove_interface()
951 stoa(&ep->sin)); in remove_interface()
963 (ep->ignore_packets) in log_listen_address()
966 ep->ifnum, in log_listen_address()
967 ep->name, in log_listen_address()
968 sptoa(&ep->sin)); in log_listen_address()
996 * create pseudo-interface with wildcard IPv6 address in create_wildcards()
1007 /* check for interface/nic rules affecting the wildcard */ in create_wildcards()
1014 strlcpy(wildif->name, "v6wildcard", sizeof(wildif->name)); in create_wildcards()
1015 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin)); in create_wildcards()
1016 wildif->family = AF_INET6; in create_wildcards()
1017 AF(&wildif->mask) = AF_INET6; in create_wildcards()
1018 SET_ONESMASK(&wildif->mask); in create_wildcards()
1020 wildif->flags = INT_UP | INT_WILDCARD; in create_wildcards()
1021 wildif->ignore_packets = (ACTION_DROP == action); in create_wildcards()
1023 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif); in create_wildcards()
1025 if (wildif->fd != INVALID_SOCKET) { in create_wildcards()
1028 add_addr_to_list(&wildif->sin, wildif); in create_wildcards()
1033 "unable to bind to wildcard address %s - another process may be running - EXITING", in create_wildcards()
1034 stoa(&wildif->sin)); in create_wildcards()
1042 * create pseudo-interface with wildcard IPv4 address in create_wildcards()
1051 /* check for interface/nic rules affecting the wildcard */ in create_wildcards()
1058 strlcpy(wildif->name, "v4wildcard", sizeof(wildif->name)); in create_wildcards()
1059 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin)); in create_wildcards()
1060 wildif->family = AF_INET; in create_wildcards()
1061 AF(&wildif->mask) = AF_INET; in create_wildcards()
1062 SET_ONESMASK(&wildif->mask); in create_wildcards()
1064 wildif->flags = INT_BROADCAST | INT_UP | INT_WILDCARD; in create_wildcards()
1065 wildif->ignore_packets = (ACTION_DROP == action); in create_wildcards()
1070 AF(&wildif->bcast) = AF_INET; in create_wildcards()
1071 SET_ADDR4N(&wildif->bcast, INADDR_ANY); in create_wildcards()
1072 SET_PORT(&wildif->bcast, port); in create_wildcards()
1074 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif); in create_wildcards()
1076 if (wildif->fd != INVALID_SOCKET) { in create_wildcards()
1080 add_addr_to_list(&wildif->sin, wildif); in create_wildcards()
1085 "unable to bind to wildcard address %s - another process may be running - EXITING", in create_wildcards()
1086 stoa(&wildif->sin)); in create_wildcards()
1095 * add_nic_rule() -- insert a rule entry at the head of nic_rule_list.
1109 rule->match_type = match_type; in add_nic_rule()
1110 rule->prefixlen = prefixlen; in add_nic_rule()
1111 rule->action = action; in add_nic_rule()
1115 rule->if_name = estrdup(if_name); in add_nic_rule()
1118 /* set rule->addr */ in add_nic_rule()
1119 is_ip = is_ip_address(if_name, AF_UNSPEC, &rule->addr); in add_nic_rule()
1181 * Find any matching NIC rule from --interface / -I or ntp.conf in interface_action()
1182 * interface/nic rules. in interface_action()
1184 for (rule = nic_rule_list; rule != NULL; rule = rule->next) { in interface_action()
1186 switch (rule->match_type) { in interface_action()
1192 DPRINTF(4, ("nic all %s\n", in interface_action()
1193 action_text(rule->action))); in interface_action()
1194 return rule->action; in interface_action()
1198 DPRINTF(4, ("nic ipv4 %s\n", in interface_action()
1199 action_text(rule->action))); in interface_action()
1200 return rule->action; in interface_action()
1206 DPRINTF(4, ("nic ipv6 %s\n", in interface_action()
1207 action_text(rule->action))); in interface_action()
1208 return rule->action; in interface_action()
1214 DPRINTF(4, ("nic wildcard %s\n", in interface_action()
1215 action_text(rule->action))); in interface_action()
1216 return rule->action; in interface_action()
1221 if (rule->prefixlen != -1) { in interface_action()
1222 if (addr_eqprefix(if_addr, &rule->addr, in interface_action()
1223 rule->prefixlen)) { in interface_action()
1225 DPRINTF(4, ("subnet address match - %s\n", in interface_action()
1226 action_text(rule->action))); in interface_action()
1227 return rule->action; in interface_action()
1230 if (SOCK_EQ(if_addr, &rule->addr)) { in interface_action()
1232 DPRINTF(4, ("address match - %s\n", in interface_action()
1233 action_text(rule->action))); in interface_action()
1234 return rule->action; in interface_action()
1241 && !fnmatch(rule->if_name, if_name, FNM_CASEFOLD) in interface_action()
1243 && !strcasecmp(if_name, rule->if_name) in interface_action()
1247 DPRINTF(4, ("interface name match - %s\n", in interface_action()
1248 action_text(rule->action))); in interface_action()
1249 return rule->action; in interface_action()
1256 * Unless explicitly disabled such as with "nic ignore ::1" in interface_action()
1268 * "nic ... wildcard" or "nic ... 0.0.0.0" or "nic ... ::" rule in interface_action()
1278 * the rules so that "ntpd --interface eth0:1 -novirtualips" in interface_action()
1284 DPRINTF(4, ("virtual ip - ignore\n")); in interface_action()
1289 * If there are no --interface/-I command-line options and no in interface_action()
1290 * interface/nic rules in ntp.conf, the default action is to in interface_action()
1292 * is to ignore. This implements ntpd's traditional listen- in interface_action()
1294 * ensures a single -I eth0 or "nic listen eth0" means do not in interface_action()
1314 strlcpy(itf->name, isc_if->name, sizeof(itf->name)); in convert_isc_if()
1315 itf->ifindex = isc_if->ifindex; in convert_isc_if()
1316 itf->family = (u_short)isc_if->af; in convert_isc_if()
1317 AF(&itf->sin) = itf->family; in convert_isc_if()
1318 AF(&itf->mask) = itf->family; in convert_isc_if()
1319 AF(&itf->bcast) = itf->family; in convert_isc_if()
1320 SET_PORT(&itf->sin, port); in convert_isc_if()
1321 SET_PORT(&itf->mask, port); in convert_isc_if()
1322 SET_PORT(&itf->bcast, port); in convert_isc_if()
1324 if (IS_IPV4(&itf->sin)) { in convert_isc_if()
1325 NSRCADR(&itf->sin) = isc_if->address.type.in.s_addr; in convert_isc_if()
1326 NSRCADR(&itf->mask) = isc_if->netmask.type.in.s_addr; in convert_isc_if()
1328 if (isc_if->flags & INTERFACE_F_BROADCAST) { in convert_isc_if()
1329 itf->flags |= INT_BROADCAST; in convert_isc_if()
1330 NSRCADR(&itf->bcast) = in convert_isc_if()
1331 isc_if->broadcast.type.in.s_addr; in convert_isc_if()
1335 else if (IS_IPV6(&itf->sin)) { in convert_isc_if()
1336 SET_ADDR6N(&itf->sin, isc_if->address.type.in6); in convert_isc_if()
1337 SET_ADDR6N(&itf->mask, isc_if->netmask.type.in6); in convert_isc_if()
1339 SET_SCOPE(&itf->sin, isc_if->address.zone); in convert_isc_if()
1346 itf->flags |= in convert_isc_if()
1347 ((INTERFACE_F_UP & isc_if->flags) in convert_isc_if()
1349 | ((INTERFACE_F_LOOPBACK & isc_if->flags) in convert_isc_if()
1351 | ((INTERFACE_F_POINTTOPOINT & isc_if->flags) in convert_isc_if()
1353 | ((INTERFACE_F_MULTICAST & isc_if->flags) in convert_isc_if()
1355 | ((INTERFACE_F_PRIVACY & isc_if->flags) in convert_isc_if()
1363 if ((INT_LOOPBACK & itf->flags) && !IS_LOOPBACK_ADDR(&itf->sin)) { in convert_isc_if()
1364 itf->flags &= ~INT_LOOPBACK; in convert_isc_if()
1375 * this can be mitigated by re-binding
1384 if (iface->fd != INVALID_SOCKET) { in refresh_interface()
1385 int bcast = (iface->flags & INT_BCASTXMIT) != 0; in refresh_interface()
1389 socket_broadcast_disable(iface, &iface->sin); in refresh_interface()
1391 close_and_delete_fd_from_list(iface->fd); in refresh_interface()
1395 iface->fd = open_socket(&iface->sin, in refresh_interface()
1401 iface->last_ttl = 0; in refresh_interface()
1402 return (iface->fd != INVALID_SOCKET); in refresh_interface()
1406 return (iface->fd != INVALID_SOCKET); in refresh_interface()
1411 * interface_update - externally callable update function
1439 * sau_from_netaddr() - convert network address on-wire formats.
1449 AF(psau) = (u_short)pna->family; in sau_from_netaddr()
1450 switch (pna->family) { in sau_from_netaddr()
1453 psau->sa4.sin_addr = pna->type.in; in sau_from_netaddr()
1457 psau->sa6.sin6_addr = pna->type.in6; in sau_from_netaddr()
1488 p6addr = &psau->sa6.sin6_addr; in is_linklocal()
1493 /* autoconf are link-local 169.254.0.0/16 */ in is_linklocal()
1504 * enable/disable re-use of wildcard address socket
1517 fd = any->fd; in set_wildcard_reuse()
1528 stoa(&any->sin))); in set_wildcard_reuse()
1544 if (psau->sa.sa_family != AF_INET) in check_flags()
1549 memcpy(&ifr.ifr_addr, &psau->sa, sizeof(ifr.ifr_addr)); in check_flags()
1573 if (psau->sa.sa_family != AF_INET6) in check_flags6()
1578 memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr)); in check_flags6()
1613 switch (psau->sa.sa_family) { in is_valid()
1646 * drop socket - rebind again
1652 * Scan the list of new addresses marking IPv6 link-local addresses
1664 * attempt to re-assign interfaces to peers
1675 isc_mem_t * mctx = (void *)-1; in update_interfaces()
1692 * phase 1a - scan OS local addresses in update_interfaces()
1693 * - update those that ntpd already knows in update_interfaces()
1694 * - build a list of newly-discovered addresses. in update_interfaces()
1744 DPRINTF(4, ("ignoring interface %s (%s) - by nic rules\n", in update_interfaces()
1749 DPRINTF(4, ("listen interface %s (%s) - by nic rules\n", in update_interfaces()
1755 DPRINTF(4, ("drop on interface %s (%s) - by nic rules\n", in update_interfaces()
1763 DPRINTF(4, ("skipping interface %s (%s) - DOWN\n", in update_interfaces()
1770 * address - some dhcp clients produce that in the in update_interfaces()
1786 * Keep track of having non-linklocal connectivity in update_interfaces()
1801 * socket. Our name space is (ip-address), NOT in update_interfaces()
1802 * (interface name, ip-address). in update_interfaces()
1810 newaddrs_tail->elink = ep; in update_interfaces()
1828 * found existing and up to date interface - in update_interfaces()
1831 if (ep->phase != sys_interphase) { in update_interfaces()
1838 * changes to the nic rules. in update_interfaces()
1840 strlcpy(ep->name, enumep.name, sizeof(ep->name)); in update_interfaces()
1841 ep->ignore_packets = enumep.ignore_packets; in update_interfaces()
1847 msyslog(LOG_INFO, "%s on %u %s -> *multiple*", in update_interfaces()
1848 stoa(&ep->sin), ep->ifnum, ep->name); in update_interfaces()
1849 /* name collision - rename interface */ in update_interfaces()
1850 strlcpy(ep->name, "*multiple*", sizeof(ep->name)); in update_interfaces()
1855 if (ep->ignore_packets != enumep.ignore_packets) { in update_interfaces()
1859 * -I <interfacename> on the command line for an in update_interfaces()
1874 enumep.name, ep->name, stoa(&enumep.sin)); in update_interfaces()
1876 ep->ignore_packets = TRUE; in update_interfaces()
1879 ep->phase = sys_interphase; in update_interfaces()
1893 for (ep = newaddrs; ep != NULL; ep = ep->elink) { in update_interfaces()
1894 if (IS_IPV6(&ep->sin) && is_linklocal(&ep->sin)) { in update_interfaces()
1895 for (ep2 = newaddrs; ep2 != NULL; ep2 = ep2->elink) { in update_interfaces()
1896 if ( IS_IPV6(&ep2->sin) in update_interfaces()
1898 && !is_linklocal(&ep2->sin)) { in update_interfaces()
1900 ep->flags |= INT_LL_OF_GLOB; in update_interfaces()
1907 next_ep = ep2->elink; in update_interfaces()
1908 ep2->elink = NULL; in update_interfaces()
1918 (ep, "updating ", " new - created\n")); in update_interfaces()
1922 (ep2, "updating ", " new - FAILED")); in update_interfaces()
1926 stoa(&ep->sin)); in update_interfaces()
1932 * phase 2 - delete gone interfaces - reassigning peers to in update_interfaces()
1936 next_ep = ep->elink; in update_interfaces()
1940 * not enumerated during the last interface scan - so it in update_interfaces()
1945 if (((INT_WILDCARD | INT_MCASTIF) & ep->flags) || in update_interfaces()
1946 ep->phase == sys_interphase) in update_interfaces()
1950 "GONE - deleting\n")); in update_interfaces()
1959 while (ep->peers != NULL) { in update_interfaces()
1960 set_peerdstadr(ep->peers, NULL); in update_interfaces()
1973 * phase 3 - re-configure as the world has possibly changed in update_interfaces()
1975 * never ever make this conditional again - it is needed to track in update_interfaces()
1988 for (ep = ep_list; ep != NULL; ep = ep->elink) { in update_interfaces()
1991 if (!(INT_MCASTIF & ep->flags) || (INT_MCASTOPEN & ep->flags)) { in update_interfaces()
1997 entry = entry->link) { in update_interfaces()
1998 if (entry->ep == ep) { in update_interfaces()
1999 if (socket_multicast_enable(ep, &entry->addr)) { in update_interfaces()
2002 stoa(&ep->sin), in update_interfaces()
2003 stoa(&entry->addr)); in update_interfaces()
2016 * create_sockets - create a socket for each interface plus a default
2050 * create_interface - create a new interface for a given prototype
2066 DPRINTF(2, ("create_interface(%s)\n", sptoa(&protot->sin))); in create_interface()
2074 iface->fd = open_socket(&iface->sin, 0, 0, iface); in create_interface()
2076 if (iface->fd != INVALID_SOCKET) in create_interface()
2079 if ((INT_BROADCAST & iface->flags) in create_interface()
2080 && iface->bfd != INVALID_SOCKET) in create_interface()
2082 sptoa(&iface->bcast)); in create_interface()
2084 if (INVALID_SOCKET == iface->fd in create_interface()
2085 && INVALID_SOCKET == iface->bfd) { in create_interface()
2087 iface->name, in create_interface()
2088 iface->ifnum, in create_interface()
2089 sptoa(&iface->sin)); in create_interface()
2097 SET_HOSTMASK(&resmask, AF(&iface->sin)); in create_interface()
2098 success = hack_restrict(RESTRICT_FLAGS, &iface->sin, &resmask, in create_interface()
2099 -4, RESM_NTPONLY | RESM_INTERFACE, in create_interface()
2103 "unable to self-restrict %s", stoa(&iface->sin)); in create_interface()
2110 if (NULL == loopback_interface && AF_INET == iface->family in create_interface()
2111 && (INT_LOOPBACK & iface->flags)) in create_interface()
2117 add_addr_to_list(&iface->sin, iface); in create_interface()
2122 * Join any previously-configured compatible multicast groups. in create_interface()
2124 if (INT_MULTICAST & iface->flags && in create_interface()
2125 !((INT_LOOPBACK | INT_WILDCARD) & iface->flags) && in create_interface()
2126 !iface->ignore_packets) { in create_interface()
2130 next_entry = entry->link; in create_interface()
2131 if (AF(&iface->sin) != AF(&entry->addr) || in create_interface()
2132 !IS_MCAST(&entry->addr)) in create_interface()
2135 &entry->addr)) in create_interface()
2138 stoa(&iface->sin), in create_interface()
2139 stoa(&entry->addr)); in create_interface()
2143 stoa(&iface->sin), in create_interface()
2144 stoa(&entry->addr)); in create_interface()
2223 append_flagstr(ifs, sz, "linklocal-w-global"); in iflags_str()
2274 * set_reuseaddr() - set/clear REUSEADDR on all sockets
2275 * NB possible hole - should we be doing this on broadcast
2286 for (ep = ep_list; ep != NULL; ep = ep->elink) { in set_reuseaddr()
2287 if (ep->flags & INT_WILDCARD) in set_reuseaddr()
2291 * if ep->fd is INVALID_SOCKET, we might have a adapter in set_reuseaddr()
2295 ep->name, stoa(&ep->sin), in set_reuseaddr()
2298 if (ep->fd != INVALID_SOCKET) { in set_reuseaddr()
2299 if (setsockopt(ep->fd, SOL_SOCKET, SO_REUSEADDR, in set_reuseaddr()
2302 stoa(&ep->sin), flag ? "on" : "off"); in set_reuseaddr()
2320 socket_broadcast_enable(iface, iface->fd, baddr); in enable_broadcast()
2351 iface->flags |= INT_BCASTXMIT; in socket_broadcast_enable()
2373 if (IS_IPV4(baddr) && setsockopt(iface->fd, SOL_SOCKET, in socket_broadcast_disable()
2379 iface->flags &= ~INT_BCASTXMIT; in socket_broadcast_disable()
2436 REQUIRE(AF(maddr) == AF(&iface->sin)); in enable_multicast_if()
2438 switch (AF(&iface->sin)) { in enable_multicast_if()
2445 if (setsockopt(iface->fd, IPPROTO_IP, in enable_multicast_if()
2452 iface->fd, stoa(&iface->sin), in enable_multicast_if()
2464 if (setsockopt(iface->fd, IPPROTO_IPV6, in enable_multicast_if()
2470 iface->fd, stoa(&iface->sin), in enable_multicast_if()
2505 if (setsockopt(iface->fd, in socket_multicast_enable()
2512 iface->fd, stoa(&iface->sin), in socket_multicast_enable()
2519 iface->fd, stoa(&iface->sin), in socket_multicast_enable()
2528 * If the address is link-local we can get the in socket_multicast_enable()
2535 mreq6.ipv6mr_interface = iface->ifindex; in socket_multicast_enable()
2537 if (setsockopt(iface->fd, IPPROTO_IPV6, in socket_multicast_enable()
2542 iface->fd, stoa(&iface->sin), in socket_multicast_enable()
2547 iface->fd, stoa(&iface->sin), in socket_multicast_enable()
2553 iface->flags |= INT_MCASTOPEN; in socket_multicast_enable()
2554 iface->num_mcast++; in socket_multicast_enable()
2589 mreq.imr_interface = SOCK_ADDR4(&iface->sin); in socket_multicast_disable()
2590 if (setsockopt(iface->fd, IPPROTO_IP, in socket_multicast_disable()
2596 iface->fd, stoa(&iface->sin), in socket_multicast_disable()
2597 SRCADR(maddr), SRCADR(&iface->sin), in socket_multicast_disable()
2606 * If the address is link-local we can get the in socket_multicast_disable()
2613 mreq6.ipv6mr_interface = iface->ifindex; in socket_multicast_disable()
2615 if (setsockopt(iface->fd, IPPROTO_IPV6, in socket_multicast_disable()
2621 iface->fd, stoa(&iface->sin), in socket_multicast_disable()
2622 iface->ifindex, stoa(maddr)); in socket_multicast_disable()
2631 iface->num_mcast--; in socket_multicast_disable()
2632 if (iface->num_mcast <= 0) { in socket_multicast_disable()
2633 iface->flags &= ~INT_MCASTOPEN; in socket_multicast_disable()
2641 * io_setbclient - open the broadcast client sockets
2653 for (ep = ep_list; ep != NULL; ep = ep->elink) { in io_setbclient()
2657 if (AF_INET == ep->family) { in io_setbclient()
2661 if (ep->flags & (INT_WILDCARD | INT_LOOPBACK)) in io_setbclient()
2665 if (ep->ignore_packets) in io_setbclient()
2668 /* Need a broadcast-capable interface */ in io_setbclient()
2669 if (!(ep->flags & INT_BROADCAST)) in io_setbclient()
2673 REQUIRE(IS_IPV4(&ep->bcast)); in io_setbclient()
2676 if (ep->flags & INT_BCASTOPEN) { in io_setbclient()
2688 ep->family = AF_INET; in io_setbclient()
2689 ep->bfd = open_socket(&ep->bcast, 1, 0, ep); in io_setbclient()
2695 if (ep->bfd != INVALID_SOCKET) { in io_setbclient()
2697 ep->flags |= INT_BCASTOPEN; in io_setbclient()
2700 stoa(&ep->bcast), ep->ifnum, ep->name); in io_setbclient()
2710 /* avoid recurrence here -- if we already have a in io_setbclient()
2714 if (ep->fd != INVALID_SOCKET) { in io_setbclient()
2715 ep->flags |= INT_BCASTOPEN; in io_setbclient()
2724 stoa(&ep->bcast), ep->ifnum, ep->name); in io_setbclient()
2751 * io_unsetbclient - close the broadcast client sockets
2758 for (ep = ep_list; ep != NULL; ep = ep->elink) { in io_unsetbclient()
2759 if (INT_WILDCARD & ep->flags) in io_unsetbclient()
2761 if (!(INT_BCASTOPEN & ep->flags)) in io_unsetbclient()
2764 if (ep->bfd != INVALID_SOCKET) { in io_unsetbclient()
2768 stoa(&ep->bcast), ep->ifnum, ep->name); in io_unsetbclient()
2769 close_and_delete_fd_from_list(ep->bfd, ep); in io_unsetbclient()
2770 ep->bfd = INVALID_SOCKET; in io_unsetbclient()
2772 ep->flags &= ~INT_BCASTOPEN; in io_unsetbclient()
2779 * io_multicast_add() - add multicast group address
2807 ep->sin = *addr;
2808 SET_PORT(&ep->sin, NTP_PORT);
2809 ep->family = AF(&ep->sin);
2810 AF(&ep->mask) = ep->family;
2811 SET_ONESMASK(&ep->mask);
2814 ep->bfd = INVALID_SOCKET;
2815 ep->fd = open_socket(&ep->sin, 0, 0, ep);
2816 if (ep->fd != INVALID_SOCKET) {
2817 ep->ignore_packets = ISC_FALSE;
2818 ep->flags |= INT_MCASTIF;
2819 ep->ifindex = SCOPE(addr);
2821 strlcpy(ep->name, "multicast", sizeof(ep->name));
2826 /* bind failed, re-use wildcard interface */
2837 /* HACK ! -- stuff in an address */
2839 ep->bcast = *addr;
2842 stoa(addr), ep->ifnum, ep->name);
2861 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2862 if (ep->ignore_packets || AF(&ep->sin) != AF(addr) ||
2863 !(INT_MULTICAST & ep->flags) ||
2864 (INT_LOOPBACK | INT_WILDCARD) & ep->flags)
2871 stoa(&ep->sin),
2886 * io_multicast_del() - delete multicast group address
2923 * open_socket - open a socket, returning the file descriptor
2961 …"unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - ex…
3004 if (!(interf->flags & INT_WILDCARD))
3064 errval = bind(fd, &addr->sa, SOCKLEN(addr));
3085 interf->flags);
3131 fd, sptoa(addr), interf->flags));
3151 msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
3162 * sendpkt - send a packet to the specified destination from the given endpt
3196 * unbound peer - drop request and wait for better
3199 DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
3206 if (ismcast && INT_LL_OF_GLOB & src->flags) {
3211 ismcast ? "\tMCAST\t***** " : "", src->fd,
3212 stoa(dest), stoa(&src->sin), ttl, len));
3214 if (ismcast && ttl > 0 && ttl != src->last_ttl) {
3218 switch (AF(&src->sin)) {
3222 rc = setsockopt(src->fd, IPPROTO_IP,
3230 rc = setsockopt(src->fd, IPPROTO_IPV6,
3242 src->last_ttl = ttl;
3246 stoa(&src->sin));
3253 cc = io_completion_port_sendto(src, src->fd, pkt,
3256 cc = sendto(src->fd, (char *)pkt, (u_int)len, 0,
3257 &dest->sa, SOCKLEN(dest));
3259 if (cc == -1) {
3260 src->notsent++;
3263 src->sent++;
3268 src = src->mclink;
3271 /* HMS: pkt->rootdisp is usually random here */
3272 NTOHL_FP(&pkt->org, &org);
3273 NTOHL_FP(&pkt->rec, &rec);
3274 NTOHL_FP(&pkt->xmt, &xmt);
3275 record_raw_stats(src ? &src->sin : NULL, dest,
3277 PKT_LEAP(pkt->li_vn_mode),
3278 PKT_VERSION(pkt->li_vn_mode),
3279 PKT_MODE(pkt->li_vn_mode),
3280 pkt->stratum,
3281 pkt->ppoll, pkt->precision,
3282 FPTOD(NTOHS_FP(pkt->rootdelay)),
3283 FPTOD(NTOHS_FP(pkt->rootdisp)), pkt->refid,
3284 len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
3291 * fdbits - generate ascii representation of fd_set (FAU debug support)
3292 * HFDF format - highest fd first.
3303 count = min(count, sizeof(buffer) - 1);
3306 *buf++ = FD_ISSET(count, set) ? '#' : '-';
3307 count--;
3338 * No buffer space available - just drop the 'packet'.
3339 * Since this is a non-blocking character stream we read
3352 /* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead
3355 if (rp->datalen <= 0 || (size_t)rp->datalen > sizeof(rb->recv_space))
3356 read_count = sizeof(rb->recv_space);
3358 read_count = (u_int)rp->datalen;
3360 buflen = read(fd, (char *)&rb->recv_space, read_count);
3374 rb->recv_length = buflen;
3375 rb->recv_peer = rp->srcclock;
3376 rb->dstadr = NULL;
3377 rb->fd = fd;
3378 rb->recv_time = ts;
3379 rb->receiver = rp->clock_recv;
3383 rp->recvcount++;
3414 switch (cmsghdr->cmsg_type)
3426 switch (cmsghdr->cmsg_type)
3439 sys_tick > 1e-9) {
3454 sys_tick > 1e-9) {
3455 ticks = (unsigned long)((pts.tv_nsec * 1e-9) /
3472 sys_tick > 1e-6) {
3473 ticks = (unsigned long)((ptv.tv_usec * 1e-6) /
3502 cmsghdr->cmsg_type));
3538 rb = itf->ignore_packets ? NULL : get_free_recv_buffer(FALSE);
3554 (itf->ignore_packets)
3558 if (itf->ignore_packets)
3565 fromlen = sizeof(rb->recv_srcadr);
3568 rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
3569 sizeof(rb->recv_space), 0,
3570 &rb->recv_srcadr.sa, &fromlen);
3572 iovec.iov_base = &rb->recv_space;
3573 iovec.iov_len = sizeof(rb->recv_space);
3574 msghdr.msg_name = &rb->recv_srcadr;
3581 rb->recv_length = recvmsg(fd, &msghdr, 0);
3584 buflen = rb->recv_length;
3586 if (buflen == 0 || (buflen == -1 &&
3596 stoa(&rb->recv_srcadr), fd);
3604 fd, buflen, stoa(&rb->recv_srcadr)));
3607 if (ISREFCLOCKADR(&rb->recv_srcadr)) {
3609 stoa(&rb->recv_srcadr), fd);
3622 if ( IS_IPV6(&rb->recv_srcadr)
3623 && IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr))
3624 && !(INT_LOOPBACK & itf->flags)) {
3629 return -1;
3636 rb->dstadr = itf;
3637 rb->fd = fd;
3642 rb->recv_time = ts;
3643 rb->receiver = receive;
3647 itf->received++;
3674 * System clock updates really aren't time-critical,
3675 * and - lacking a hardware reference clock - I have
3710 } else if (nfound == -1 && errno != EINTR) {
3727 * input_handler - receive packets asynchronously
3729 * ALWAYS IN SIGNAL HANDLER CONTEXT -- only async-safe functions allowed!
3791 if (-1 != read(j, &b, 0)) {
3848 for (rp = refio; rp != NULL; rp = rp->next) {
3849 fd = rp->fd;
3864 clk = refnumtoa(&rp->srcclock->srcadr);
3869 clk = refnumtoa(&rp->srcclock->srcadr);
3884 for (ep = ep_list; ep != NULL; ep = ep->elink) {
3887 fd = ep->fd;
3889 if (!(ep->flags & INT_BCASTOPEN))
3891 fd = ep->bfd;
3906 * scan list of asyncio readers - currently only used for routing sockets
3912 next_asyncio_reader = asyncio_reader->link;
3913 if (FD_ISSET(asyncio_reader->fd, pfds))
3914 (*asyncio_reader->receiver)(asyncio_reader);
3924 if (NULL == c || -1 == c->resp_read_pipe)
3926 if (FD_ISSET(c->resp_read_pipe, pfds)) {
3927 ++c->resp_ready_seen;
3936 * (ts_e - ts) is the amount of time we spent
3976 } else if (peer->cast_flags &
3980 DPRINTF(4, ("Found *-cast interface %s for address %s\n",
3981 stoa(&ep->sin), stoa(srcadr)));
3983 DPRINTF(4, ("No *-cast local address found for address %s\n",
4005 if (ep != NULL && (INT_WILDCARD & ep->flags)) {
4019 * findinterface - find local interface corresponding to address
4031 DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
4037 iface->ifnum, iface->name, stoa(addr)));
4043 * findlocalinterface - find local interface corresponding to addr,
4052 * ntpd. preferably we would have used an API call - but its not there -
4104 rtn = connect(s, &addr->sa, SOCKLEN(addr));
4124 * address suggested by the kernel being excluded by nic rules
4125 * or the user's -I and -L options to ntpd.
4129 if (NULL == iface || iface->ignore_packets) {
4138 && (iface->ignore_packets || (INT_MCASTIF & iface->flags))) {
4148 * If there are -I/--interface or -L/novirtualips command-line options,
4149 * or "nic" or "interface" rules in ntp.conf, findlocalinterface() may
4174 for (ep = ep_list; ep != NULL; ep = ep->elink) {
4175 if (ep->ignore_packets ||
4176 AF(addr) != ep->family ||
4177 flags & ep->flags)
4180 calc_addr_distance(&addr_dist, addr, &ep->sin);
4182 -1 == cmp_addr_distance(&addr_dist, &min_dist)) {
4191 DPRINTF(4, ("findclosestinterface(%s) -> %s\n",
4192 stoa(addr), stoa(&winner->sin)));
4199 * calc_addr_distance - calculate the distance between two addresses,
4241 pdist[u] = p1[u] - p2[u];
4243 pdist[u] = p2[u] - p1[u];
4250 * cmp_addr_distance - compare two address distances, returning -1, 0,
4265 return -1;
4274 return -1;
4298 if (iface != NULL && (iface->flags & flags))
4306 * findbcastinter - find broadcast interface corresponding to address
4323 DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n",
4324 iface->ifnum, iface->name));
4329 * plan B - try to find something reasonable in our lists in
4332 for (iface = ep_list; iface != NULL; iface = iface->elink) {
4333 if (iface->flags & INT_WILDCARD)
4337 if (iface->ignore_packets)
4343 if(AF(&iface->sin) != AF(addr))
4347 if (iface->flags & INT_LOOPBACK)
4355 && (iface->flags & INT_MULTICAST)) {
4359 * an interface to use for link-local multicast
4360 * and its address is not link-local.
4364 && !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin)))
4377 if (SOCK_EQ(&iface->bcast, addr))
4380 if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask))
4381 == (NSRCADR(addr) & NSRCADR(&iface->mask)))
4386 if (SOCK_EQ(&iface->bcast, addr))
4389 if (SOCK_EQ(netof(&iface->sin), netof(addr)))
4400 DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n",
4401 iface->ifnum, iface->name));
4409 * io_clr_stats - clear I/O module statistics
4428 * io_addclock - add a reference clock to the list and arrange that we
4442 rio->active = TRUE;
4464 add_fd_to_list(rio->fd, FD_TYPE_FILE);
4472 * io_closeclock - close the clock in the I/O structure given
4486 rio->active = FALSE;
4494 * - we first disable feeding to the queu by removing
4496 * - close the file (which brings down any IO on it)
4497 * - clear the buffer from results for this fd
4502 close_and_delete_fd_from_list(rio->fd, NULL);
4503 purge_recv_buffers_for_fd(rio->fd);
4504 rio->fd = -1;
4537 close_and_delete_fd_from_list(fd_list->fd, NULL);
4555 lsock->fd = fd;
4556 lsock->type = type;
4572 UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t);
4577 switch (lsock->type) {
4585 closesocket(lsock->fd);
4589 closeserial((int)lsock->fd);
4594 "internal error - illegal descriptor type %d - EXITING",
4595 (int)lsock->type);
4618 /* not there yet - add to list */
4620 laddr->addr = *addr;
4621 laddr->ep = ep;
4643 &(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t);
4662 UNLINK_EXPR_SLIST_CURRENT()->ep, link,
4668 stoa(&unlinked->addr), iface->ifnum,
4669 iface->name));
4682 DPRINTF(4, ("Searching for addr %s in list of addresses - ",
4687 entry = entry->link)
4688 if (SOCK_EQ(&entry->addr, addr)) {
4690 return entry->ep;
4714 entry = entry->link)
4716 if (SOCK_EQ(&entry->addr, addr)
4717 && (entry->ep->flags & flags) == flags) {
4720 return entry->ep;
4735 : stoa(&la->sin);
4741 # define UPDATE_GRACE 3 /* min. UPDATE_GRACE - 1 seconds before scanning */
4771 cnt = read(reader->fd, buffer, buffsz);
4777 if (0 > getsockopt(reader->fd, SOL_SOCKET, SO_RCVBUF, &cnt, &len) ||
4780 "routing getsockopt SO_RCVBUF %u %u: %m - disabling",
4785 if (0 > setsockopt(reader->fd, SOL_SOCKET, SO_RCVBUF, &new, sizeof new)) {
4787 "routing setsockopt SO_RCVBUF %d -> %d: %m - disabling",
4793 "routing socket reports: %m - disabling");
4807 msg_type = nh->nlmsg_type;
4816 "version mismatch (got %d - expected %d) on routing socket - disabling",
4905 "unable to open routing socket (%m) - using polled interface update");
4919 "bind failed on routing socket (%m) - using polled interface update");
4930 reader->fd = fd;
4931 reader->receiver = process_routing_msgs;