2 * ntp_io.c - input/output routines for ntpd. The socket-opening code
3 * was shamelessly stolen from ntpd.
14 # if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE)
15 # define FNM_CASEFOLD FNM_IGNORECASE
18 #ifdef HAVE_SYS_PARAM_H
19 # include <sys/param.h>
21 #ifdef HAVE_SYS_IOCTL_H
22 # include <sys/ioctl.h>
24 #ifdef HAVE_SYS_SOCKIO_H /* UXPV: SIOC* #defines (Frank Vance <fvance@waii.com>) */
25 # include <sys/sockio.h>
31 #include "ntp_machine.h"
35 #include "ntp_lists.h"
36 #include "ntp_refclock.h"
37 #include "ntp_stdlib.h"
38 #include "ntp_worker.h"
39 #include "ntp_request.h"
40 #include "ntp_assert.h"
41 #include "timevalops.h"
42 #include "timespecops.h"
43 #include "ntpd-opts.h"
45 /* Don't include ISC's version of IPv6 variables and structures */
48 #include <isc/interfaceiter.h>
49 #include <isc/netaddr.h>
50 #include <isc/result.h>
51 #include <isc/sockaddr.h>
57 #ifdef HAS_ROUTING_SOCKET
58 # include <net/route.h>
59 # ifdef HAVE_RTNETLINK
60 # include <linux/rtnetlink.h>
66 * setsockopt does not always have the same arg declaration
67 * across all platforms. If it's not defined we make it empty
70 #ifndef SETSOCKOPT_ARG_CAST
71 #define SETSOCKOPT_ARG_CAST
74 extern int listen_to_virtual_ips;
77 #define IPTOS_DSCP_EF 0xb8
79 int qos = IPTOS_DSCP_EF; /* QoS RFC3246 */
82 /* TODO burnicki: This should be moved to ntp_timer.c, but if we do so
83 * we get a linker error. Since we're running out of time before the leap
84 * second occurs, we let it here where it just works.
92 typedef struct nic_rule_tag nic_rule;
96 nic_rule_action action;
97 nic_rule_match match_type;
104 * NIC rule listhead. Entries are added at the head so that the first
105 * match in the list is the last matching rule specified.
107 nic_rule *nic_rule_list;
110 #if defined(SO_BINTIME) && defined(SCM_BINTIME) && defined(CMSG_FIRSTHDR)
111 # define HAVE_PACKET_TIMESTAMP
112 # define HAVE_BINTIME
113 # ifdef BINTIME_CTLMSGBUF_SIZE
114 # define CMSG_BUFSIZE BINTIME_CTLMSGBUF_SIZE
116 # define CMSG_BUFSIZE 1536 /* moderate default */
118 #elif defined(SO_TIMESTAMPNS) && defined(SCM_TIMESTAMPNS) && defined(CMSG_FIRSTHDR)
119 # define HAVE_PACKET_TIMESTAMP
120 # define HAVE_TIMESTAMPNS
121 # ifdef TIMESTAMPNS_CTLMSGBUF_SIZE
122 # define CMSG_BUFSIZE TIMESTAMPNS_CTLMSGBUF_SIZE
124 # define CMSG_BUFSIZE 1536 /* moderate default */
126 #elif defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP) && defined(CMSG_FIRSTHDR)
127 # define HAVE_PACKET_TIMESTAMP
128 # define HAVE_TIMESTAMP
129 # ifdef TIMESTAMP_CTLMSGBUF_SIZE
130 # define CMSG_BUFSIZE TIMESTAMP_CTLMSGBUF_SIZE
132 # define CMSG_BUFSIZE 1536 /* moderate default */
135 /* fill in for old/other timestamp interfaces */
138 #if defined(SYS_WINNT)
139 #include "win32_io.h"
140 #include <isc/win32os.h>
144 * We do asynchronous input using the SIGIO facility. A number of
145 * recvbuf buffers are preallocated for input. In the signal
146 * handler we poll to see which sockets are ready and read the
147 * packets from them into the recvbuf's along with a time stamp and
148 * an indication of the source host and the interface it was received
149 * through. This allows us to get as accurate receive time stamps
150 * as possible independent of other processing going on.
152 * We watch the number of recvbufs available to the signal handler
153 * and allocate more when this number drops below the low water
154 * mark. If the signal handler should run out of buffers in the
155 * interim it will drop incoming frames, the idea being that it is
156 * better to drop a packet than to be inaccurate.
161 * Other statistics of possible interest
163 volatile u_long packets_dropped; /* total number of packets dropped on reception */
164 volatile u_long packets_ignored; /* packets received on wild card interface */
165 volatile u_long packets_received; /* total number of packets received */
166 u_long packets_sent; /* total number of packets sent */
167 u_long packets_notsent; /* total number of packets which couldn't be sent */
169 volatile u_long handler_calls; /* number of calls to interrupt handler */
170 volatile u_long handler_pkts; /* number of pkts received by handler */
171 u_long io_timereset; /* time counters were reset */
176 endpt * any_interface; /* wildcard ipv4 interface */
177 endpt * any6_interface; /* wildcard ipv6 interface */
178 endpt * loopback_interface; /* loopback ipv4 interface */
180 isc_boolean_t broadcast_client_enabled; /* is broadcast client enabled */
181 u_int sys_ifnum; /* next .ifnum to assign */
182 int ninterfaces; /* Total number of interfaces */
184 int disable_dynamic_updates; /* scan interfaces once only */
188 * Refclock stuff. We keep a chain of structures with data concerning
189 * the guys we are doing I/O for.
191 static struct refclockio *refio;
192 #endif /* REFCLOCK */
195 * File descriptor masks etc. for call to select
196 * Not needed for I/O Completion Ports or anything outside this file
198 static fd_set activefds;
199 static int maxactivefd;
202 * bit alternating value to detect verified interfaces during an update cycle
204 static u_short sys_interphase = 0;
206 static endpt * new_interface(endpt *);
207 static void add_interface(endpt *);
208 static int update_interfaces(u_short, interface_receiver_t,
210 static void remove_interface(endpt *);
211 static endpt * create_interface(u_short, endpt *);
213 static int is_wildcard_addr (const sockaddr_u *);
216 * Multicast functions
218 static isc_boolean_t addr_ismulticast (sockaddr_u *);
219 static isc_boolean_t is_anycast (sockaddr_u *,
223 * Not all platforms support multicast
226 static isc_boolean_t socket_multicast_enable (endpt *, sockaddr_u *);
227 static isc_boolean_t socket_multicast_disable(endpt *, sockaddr_u *);
231 static void interface_dump (const endpt *);
232 static void sockaddr_dump (const sockaddr_u *);
233 static void print_interface (const endpt *, const char *, const char *);
234 #define DPRINT_INTERFACE(level, args) do { if (debug >= (level)) { print_interface args; } } while (0)
236 #define DPRINT_INTERFACE(level, args) do {} while (0)
239 typedef struct vsock vsock_t;
240 enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE };
250 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
252 * async notification processing (e. g. routing sockets)
255 * support for receiving data on fd that is not a refclock or a socket
256 * like e. g. routing sockets
258 struct asyncio_reader {
259 struct asyncio_reader *link; /* the list this is being kept in */
260 SOCKET fd; /* fd to be read */
261 void *data; /* possibly local data */
262 void (*receiver)(struct asyncio_reader *); /* input handler */
265 struct asyncio_reader *asyncio_reader_list;
267 static void delete_asyncio_reader (struct asyncio_reader *);
268 static struct asyncio_reader *new_asyncio_reader (void);
269 static void add_asyncio_reader (struct asyncio_reader *, enum desc_type);
270 static void remove_asyncio_reader (struct asyncio_reader *);
272 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
274 static void init_async_notifications (void);
276 static int addr_eqprefix (const sockaddr_u *, const sockaddr_u *,
278 static int addr_samesubnet (const sockaddr_u *, const sockaddr_u *,
279 const sockaddr_u *, const sockaddr_u *);
280 static int create_sockets (u_short);
281 static SOCKET open_socket (sockaddr_u *, int, int, endpt *);
282 static char * fdbits (int, fd_set *);
283 static void set_reuseaddr (int);
284 static isc_boolean_t socket_broadcast_enable (struct interface *, SOCKET, sockaddr_u *);
285 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
286 static isc_boolean_t socket_broadcast_disable (struct interface *, sockaddr_u *);
289 typedef struct remaddr remaddr_t;
297 remaddr_t * remoteaddr_list;
298 endpt * ep_list; /* complete endpt list */
299 endpt * mc4_list; /* IPv4 mcast-capable unicast endpts */
300 endpt * mc6_list; /* IPv6 mcast-capable unicast endpts */
302 static endpt * wildipv4;
303 static endpt * wildipv6;
306 int accept_wildcard_if_for_winnt;
308 const int accept_wildcard_if_for_winnt = FALSE;
311 static void add_fd_to_list (SOCKET, enum desc_type);
312 static endpt * find_addr_in_list (sockaddr_u *);
313 static endpt * find_flagged_addr_in_list(sockaddr_u *, u_int32);
314 static void delete_addr_from_list (sockaddr_u *);
315 static void delete_interface_from_list(endpt *);
316 static void close_and_delete_fd_from_list(SOCKET);
317 static void add_addr_to_list (sockaddr_u *, endpt *);
318 static void create_wildcards (u_short);
319 static endpt * findlocalinterface (sockaddr_u *, int, int);
320 static endpt * findclosestinterface (sockaddr_u *, int);
322 static const char * action_text (nic_rule_action);
324 static nic_rule_action interface_action(char *, sockaddr_u *, u_int32);
325 static void convert_isc_if (isc_interface_t *,
327 static void calc_addr_distance(sockaddr_u *,
330 static int cmp_addr_distance(const sockaddr_u *,
334 * Routines to read the ntp packets
336 #if !defined(HAVE_IO_COMPLETION_PORT)
337 static inline int read_network_packet (SOCKET, struct interface *, l_fp);
338 static void ntpd_addremove_io_fd (int, int, int);
339 static input_handler_t input_handler;
341 static inline int read_refclock_packet (SOCKET, struct refclockio *, l_fp);
347 #ifndef HAVE_IO_COMPLETION_PORT
356 if (fd < 0 || fd >= FD_SETSIZE) {
358 "Too many sockets in use, FD_SETSIZE %d exceeded by fd %d",
364 FD_SET(fd, &activefds);
365 maxactivefd = max(fd, maxactivefd);
367 FD_CLR(fd, &activefds);
368 if (maxactivefd && fd == maxactivefd) {
369 for (i = maxactivefd - 1; i >= 0; i--)
370 if (FD_ISSET(i, &activefds)) {
374 INSIST(fd != maxactivefd);
378 #endif /* !HAVE_IO_COMPLETION_PORT */
383 * collect timing information for various processing
384 * paths. currently we only pass them on to the file
385 * for later processing. this could also do histogram
386 * based analysis in other to reduce the load (and skew)
387 * dur to the file output
390 collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
394 snprintf(buf, sizeof(buf), "%s %d %s %s",
396 ? ((rb->dstadr != NULL)
397 ? stoa(&rb->recv_srcadr)
400 count, lfptoa(dts, 9), tag);
401 record_timing_stats(buf);
406 * About dynamic interfaces, sockets, reception and more...
408 * the code solves following tasks:
410 * - keep a current list of active interfaces in order
411 * to bind to to the interface address on NTP_PORT so that
412 * all wild and specific bindings for NTP_PORT are taken by ntpd
413 * to avoid other daemons messing with the time or sockets.
414 * - all interfaces keep a list of peers that are referencing
415 * the interface in order to quickly re-assign the peers to
416 * new interface in case an interface is deleted (=> gone from system or
418 * - have a preconfigured socket ready with the right local address
419 * for transmission and reception
420 * - have an address list for all destination addresses used within ntpd
421 * to find the "right" preconfigured socket.
422 * - facilitate updating the internal interface list with respect to
423 * the current kernel state
427 * - mapping of multicast addresses to the interface affected is not always
428 * one to one - especially on hosts with multiple interfaces
429 * the code here currently allocates a separate interface entry for those
430 * multicast addresses
431 * iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF)
432 * in case of failure the multicast address is bound to an existing interface.
433 * - on some systems it is perfectly legal to assign the same address to
434 * multiple interfaces. Therefore this code does not keep a list of interfaces
435 * but a list of interfaces that represent a unique address as determined by the kernel
436 * by the procedure in findlocalinterface. Thus it is perfectly legal to see only
437 * one representative of a group of real interfaces if they share the same address.
439 * Frank Kardel 20050910
443 * init_io - initialize I/O module.
448 /* Init buffer free list and stat counters */
449 init_recvbuff(RECV_INIT);
450 /* update interface every 5 minutes as default */
451 interface_interval = 300;
454 addremove_io_fd = &ntpd_addremove_io_fd;
458 init_io_completion_port();
461 #if defined(HAVE_SIGNALED_IO)
462 (void) set_signal(input_handler);
468 ntpd_addremove_io_fd(
476 #ifdef HAVE_SIGNALED_IO
478 #endif /* not HAVE_SIGNALED_IO */
480 maintain_activefds(fd, remove_it);
485 * io_open_sockets - call socket creation routine
488 io_open_sockets(void)
490 static int already_opened;
492 if (already_opened || HAVE_OPT( SAVECONFIGQUIT ))
501 create_sockets(NTP_PORT);
504 init_async_notifications();
506 DPRINTF(3, ("io_open_sockets: maxactivefd %d\n", maxactivefd));
512 * function to dump the contents of the interface structure
513 * for debugging use only.
516 interface_dump(const endpt *itf)
518 printf("Dumping interface: %p\n", itf);
519 printf("fd = %d\n", itf->fd);
520 printf("bfd = %d\n", itf->bfd);
521 printf("sin = %s,\n", stoa(&itf->sin));
522 sockaddr_dump(&itf->sin);
523 printf("bcast = %s,\n", stoa(&itf->bcast));
524 sockaddr_dump(&itf->bcast);
525 printf("mask = %s,\n", stoa(&itf->mask));
526 sockaddr_dump(&itf->mask);
527 printf("name = %s\n", itf->name);
528 printf("flags = 0x%08x\n", itf->flags);
529 printf("last_ttl = %d\n", itf->last_ttl);
530 printf("addr_refid = %08x\n", itf->addr_refid);
531 printf("num_mcast = %d\n", itf->num_mcast);
532 printf("received = %ld\n", itf->received);
533 printf("sent = %ld\n", itf->sent);
534 printf("notsent = %ld\n", itf->notsent);
535 printf("ifindex = %u\n", itf->ifindex);
536 printf("peercnt = %u\n", itf->peercnt);
537 printf("phase = %u\n", itf->phase);
541 * sockaddr_dump - hex dump the start of a sockaddr_u
544 sockaddr_dump(const sockaddr_u *psau)
546 /* Limit the size of the sockaddr_in6 hex dump */
547 const int maxsize = min(32, sizeof(psau->sa6));
551 /* XXX: Should we limit maxsize based on psau->saX.sin_family? */
552 cp = (const void *)&psau->sa6;
554 for(i = 0; i < maxsize; i++) {
555 printf("%02x", *cp++);
563 * print_interface - helper to output debug information
566 print_interface(const endpt *iface, const char *pfx, const char *sfx)
568 printf("%sinterface #%d: fd=%d, bfd=%d, name=%s, flags=0x%x, ifindex=%u, sin=%s",
577 if (AF_INET == iface->family) {
578 if (iface->flags & INT_BROADCAST)
579 printf(", bcast=%s", stoa(&iface->bcast));
580 printf(", mask=%s", stoa(&iface->mask));
583 (iface->ignore_packets)
587 if (debug > 4) /* in-depth debugging only */
588 interface_dump(iface);
592 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
594 * create an asyncio_reader structure
596 static struct asyncio_reader *
597 new_asyncio_reader(void)
599 struct asyncio_reader *reader;
601 reader = emalloc_zero(sizeof(*reader));
602 reader->fd = INVALID_SOCKET;
611 delete_asyncio_reader(
612 struct asyncio_reader *reader
619 * add asynchio_reader
623 struct asyncio_reader * reader,
626 LINK_SLIST(asyncio_reader_list, reader, link);
627 add_fd_to_list(reader->fd, type);
631 * remove asynchio_reader
634 remove_asyncio_reader(
635 struct asyncio_reader *reader
638 struct asyncio_reader *unlinked;
640 UNLINK_SLIST(unlinked, asyncio_reader_list, reader, link,
641 struct asyncio_reader);
643 if (reader->fd != INVALID_SOCKET)
644 close_and_delete_fd_from_list(reader->fd);
646 reader->fd = INVALID_SOCKET;
648 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
651 /* compare two sockaddr prefixes */
654 const sockaddr_u * a,
655 const sockaddr_u * b,
661 isc_sockaddr_t isc_sa;
664 memcpy(&isc_sa.type, a, min(sizeof(isc_sa.type), sizeof(*a)));
665 isc_netaddr_fromsockaddr(&isc_a, &isc_sa);
668 memcpy(&isc_sa.type, b, min(sizeof(isc_sa.type), sizeof(*b)));
669 isc_netaddr_fromsockaddr(&isc_b, &isc_sa);
671 return (int)isc_netaddr_eqprefix(&isc_a, &isc_b,
678 const sockaddr_u * a,
679 const sockaddr_u * a_mask,
680 const sockaddr_u * b,
681 const sockaddr_u * b_mask
685 const u_int32 * pa_limit;
690 REQUIRE(AF(a) == AF(a_mask));
691 REQUIRE(AF(b) == AF(b_mask));
693 * With address and mask families verified to match, comparing
694 * the masks also validates the address's families match.
696 if (!SOCK_EQ(a_mask, b_mask))
700 loops = sizeof(NSRCADR6(a)) / sizeof(*pa);
701 pa = (const void *)&NSRCADR6(a);
702 pb = (const void *)&NSRCADR6(b);
703 pm = (const void *)&NSRCADR6(a_mask);
705 loops = sizeof(NSRCADR(a)) / sizeof(*pa);
706 pa = (const void *)&NSRCADR(a);
707 pb = (const void *)&NSRCADR(b);
708 pm = (const void *)&NSRCADR(a_mask);
710 for (pa_limit = pa + loops; pa < pa_limit; pa++, pb++, pm++)
711 if ((*pa & *pm) != (*pb & *pm))
719 * Code to tell if we have an IP address
720 * If we have then return the sockaddr structure
721 * and set the return value
722 * see the bind9/getaddresses.c for details
732 struct addrinfo hints;
733 struct addrinfo *result;
734 struct sockaddr_in6 *resaddr6;
738 REQUIRE(host != NULL);
739 REQUIRE(addr != NULL);
744 * Try IPv4, then IPv6. In order to handle the extended format
745 * for IPv6 scoped addresses (address%scope_ID), we'll use a local
746 * working buffer of 128 bytes. The length is an ad-hoc value, but
747 * should be enough for this purpose; the buffer can contain a string
748 * of at least 80 bytes for scope_ID in addition to any IPv6 numeric
749 * addresses (up to 46 bytes), the delimiter character and the
750 * terminating NULL character.
752 if (AF_UNSPEC == af || AF_INET == af)
753 if (inet_pton(AF_INET, host, &in4) == 1) {
755 SET_ADDR4N(addr, in4.s_addr);
760 if (AF_UNSPEC == af || AF_INET6 == af)
761 if (sizeof(tmpbuf) > strlen(host)) {
762 if ('[' == host[0]) {
763 strlcpy(tmpbuf, &host[1], sizeof(tmpbuf));
764 pch = strchr(tmpbuf, ']');
768 strlcpy(tmpbuf, host, sizeof(tmpbuf));
771 hints.ai_family = AF_INET6;
772 hints.ai_flags |= AI_NUMERICHOST;
773 if (getaddrinfo(tmpbuf, NULL, &hints, &result) == 0) {
775 resaddr6 = (struct sockaddr_in6 *)result->ai_addr;
776 SET_ADDR6N(addr, resaddr6->sin6_addr);
777 SET_SCOPE(addr, resaddr6->sin6_scope_id);
779 freeaddrinfo(result);
784 * If we got here it was not an IP address
791 * interface list enumerator - visitor pattern
795 interface_receiver_t receiver,
799 interface_info_t ifi;
801 ifi.action = IFS_EXISTS;
802 for (ifi.ep = ep_list; ifi.ep != NULL; ifi.ep = ifi.ep->elink)
803 (*receiver)(data, &ifi);
807 * do standard initialization of interface structure
815 ep->fd = INVALID_SOCKET;
816 ep->bfd = INVALID_SOCKET;
817 ep->phase = sys_interphase;
822 * create new interface structure initialize from
823 * template structure or via standard initialization
826 static struct interface *
828 struct interface *interface
831 struct interface * iface;
833 iface = emalloc(sizeof(*iface));
835 if (NULL == interface)
836 init_interface(iface);
837 else /* use the template */
838 memcpy(iface, interface, sizeof(*iface));
840 /* count every new instance of an interface in the system */
841 iface->ifnum = sys_ifnum++;
842 iface->starttime = current_time;
849 * return interface storage into free memory pool
861 * link interface into list of known interfaces
868 endpt ** pmclisthead;
876 int ep_univ_iid; /* iface ID from MAC address */
877 int scan_univ_iid; /* see RFC 4291 */
878 int ep_privacy; /* random local iface ID */
879 int scan_privacy; /* see RFC 4941 */
882 /* Calculate the refid */
883 ep->addr_refid = addr2refid(&ep->sin);
884 /* link at tail so ntpdc -c ifstats index increases each row */
885 LINK_TAIL_SLIST(ep_list, ep, elink, endpt);
888 /* the rest is for enabled multicast-capable addresses only */
889 if (ep->ignore_packets || !(INT_MULTICAST & ep->flags) ||
890 INT_LOOPBACK & ep->flags)
892 # ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
893 if (AF_INET6 == ep->family)
896 pmclisthead = (AF_INET == ep->family)
900 if (AF_INET6 == ep->family) {
902 IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&ep->sin)) ||
903 IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(&ep->sin));
904 ep_univ_iid = IS_IID_UNIV(&ep->sin);
905 ep_privacy = !!(INT_PRIVACY & ep->flags);
911 DPRINTF(4, ("add_interface mcast-capable %s%s%s%s\n",
913 (ep_local) ? " link/scope-local" : "",
914 (ep_univ_iid) ? " univ-IID" : "",
915 (ep_privacy) ? " privacy" : ""));
917 * If we have multiple local addresses on the same network
918 * interface, and some are link- or site-local, do not multicast
919 * out from the link-/site-local addresses by default, to avoid
920 * duplicate manycastclient associations between v6 peers using
921 * link-local and global addresses. link-local can still be
922 * chosen using "nic ignore myv6globalprefix::/64".
923 * Similarly, if we have multiple global addresses from the same
924 * prefix on the same network interface, multicast from one,
925 * preferring EUI-64, then static, then least RFC 4941 privacy
928 for (scan = *pmclisthead; scan != NULL; scan = scan_next) {
929 scan_next = scan->mclink;
930 if (ep->family != scan->family)
932 if (strcmp(ep->name, scan->name))
934 same_subnet = addr_samesubnet(&ep->sin, &ep->mask,
935 &scan->sin, &scan->mask);
936 if (AF_INET6 == ep->family) {
939 IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(addr)) ||
940 IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(addr));
941 scan_univ_iid = IS_IID_UNIV(addr);
942 scan_privacy = !!(INT_PRIVACY & scan->flags);
945 scan_univ_iid = FALSE;
946 scan_privacy = FALSE;
948 DPRINTF(4, ("add_interface mcast-capable scan %s%s%s%s\n",
950 (scan_local) ? " link/scope-local" : "",
951 (scan_univ_iid) ? " univ-IID" : "",
952 (scan_privacy) ? " privacy" : ""));
953 if ((ep_local && !scan_local) || (same_subnet &&
954 ((ep_privacy && !scan_privacy) ||
955 (!ep_univ_iid && scan_univ_iid)))) {
956 DPRINTF(4, ("did not add %s to %s of IPv6 multicast-capable list which already has %s\n",
964 if ((scan_local && !ep_local) || (same_subnet &&
965 ((scan_privacy && !ep_privacy) ||
966 (!scan_univ_iid && ep_univ_iid)))) {
967 UNLINK_SLIST(unlinked, *pmclisthead,
968 scan, mclink, endpt);
969 DPRINTF(4, ("%s %s from IPv6 multicast-capable list to add %s\n",
973 stoa(&scan->sin), stoa(&ep->sin)));
977 * Add link/site local at the tail of the multicast-
978 * capable unicast interfaces list, so that ntpd will
979 * send from global addresses before link-/site-local
983 LINK_TAIL_SLIST(*pmclisthead, ep, mclink, endpt);
985 LINK_SLIST(*pmclisthead, ep, mclink);
986 DPRINTF(4, ("added %s to %s of IPv%s multicast-capable unicast local address list\n",
991 (AF_INET == ep->family)
995 if (INVALID_SOCKET == ep->fd)
999 * select the local address from which to send to multicast.
1001 switch (AF(&ep->sin)) {
1004 rc = setsockopt(ep->fd, IPPROTO_IP,
1006 (void *)&NSRCADR(&ep->sin),
1007 sizeof(NSRCADR(&ep->sin)));
1010 "setsockopt IP_MULTICAST_IF %s fails: %m",
1014 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1016 rc = setsockopt(ep->fd, IPPROTO_IPV6,
1018 (void *)&ep->ifindex,
1019 sizeof(ep->ifindex));
1020 /* do not complain if bound addr scope is ifindex */
1021 if (rc && ep->ifindex != SCOPE(&ep->sin))
1023 "setsockopt IPV6_MULTICAST_IF %u for %s fails: %m",
1024 ep->ifindex, stoa(&ep->sin));
1033 * remove interface from known interface list and clean up
1034 * associated resources
1042 endpt ** pmclisthead;
1045 UNLINK_SLIST(unlinked, ep_list, ep, elink, endpt);
1046 if (!ep->ignore_packets && INT_MULTICAST & ep->flags) {
1047 pmclisthead = (AF_INET == ep->family)
1050 UNLINK_SLIST(unlinked, *pmclisthead, ep, mclink, endpt);
1051 DPRINTF(4, ("%s %s IPv%s multicast-capable unicast local address list\n",
1056 (AF_INET == ep->family)
1060 delete_interface_from_list(ep);
1062 if (ep->fd != INVALID_SOCKET) {
1064 "Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
1072 current_time - ep->starttime);
1073 close_and_delete_fd_from_list(ep->fd);
1074 ep->fd = INVALID_SOCKET;
1077 if (ep->bfd != INVALID_SOCKET) {
1079 "stop listening for broadcasts to %s on interface #%d %s",
1080 stoa(&ep->bcast), ep->ifnum, ep->name);
1081 close_and_delete_fd_from_list(ep->bfd);
1082 ep->bfd = INVALID_SOCKET;
1083 ep->flags &= ~INT_BCASTOPEN;
1087 mon_clearinterface(ep);
1089 /* remove restrict interface entry */
1090 SET_HOSTMASK(&resmask, AF(&ep->sin));
1091 hack_restrict(RESTRICT_REMOVEIF, &ep->sin, &resmask,
1092 RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
1101 msyslog(LOG_INFO, "%s on %d %s %s",
1102 (ep->ignore_packets)
1104 : "Listen normally",
1117 #ifdef INCLUDE_IPV6_SUPPORT
1120 sockaddr_u wildaddr;
1121 nic_rule_action action;
1122 struct interface * wildif;
1125 * silence "potentially uninitialized" warnings from VC9
1126 * failing to follow the logic. Ideally action could remain
1127 * uninitialized, and the memset be the first statement under
1128 * the first if (v4wild).
1130 action = ACTION_LISTEN;
1133 #ifdef INCLUDE_IPV6_SUPPORT
1135 * create pseudo-interface with wildcard IPv6 address
1137 v6wild = ipv6_works;
1139 /* set wildaddr to the v6 wildcard address :: */
1141 AF(&wildaddr) = AF_INET6;
1142 SET_ADDR6N(&wildaddr, in6addr_any);
1143 SET_PORT(&wildaddr, port);
1144 SET_SCOPE(&wildaddr, 0);
1146 /* check for interface/nic rules affecting the wildcard */
1147 action = interface_action(NULL, &wildaddr, 0);
1148 v6wild = (ACTION_IGNORE != action);
1151 wildif = new_interface(NULL);
1153 strlcpy(wildif->name, "v6wildcard", sizeof(wildif->name));
1154 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1155 wildif->family = AF_INET6;
1156 AF(&wildif->mask) = AF_INET6;
1157 SET_ONESMASK(&wildif->mask);
1159 wildif->flags = INT_UP | INT_WILDCARD;
1160 wildif->ignore_packets = (ACTION_DROP == action);
1162 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1164 if (wildif->fd != INVALID_SOCKET) {
1166 any6_interface = wildif;
1167 add_addr_to_list(&wildif->sin, wildif);
1168 add_interface(wildif);
1169 log_listen_address(wildif);
1172 "unable to bind to wildcard address %s - another process may be running - EXITING",
1173 stoa(&wildif->sin));
1176 DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1181 * create pseudo-interface with wildcard IPv4 address
1183 v4wild = ipv4_works;
1185 /* set wildaddr to the v4 wildcard address 0.0.0.0 */
1186 AF(&wildaddr) = AF_INET;
1187 SET_ADDR4N(&wildaddr, INADDR_ANY);
1188 SET_PORT(&wildaddr, port);
1190 /* check for interface/nic rules affecting the wildcard */
1191 action = interface_action(NULL, &wildaddr, 0);
1192 v4wild = (ACTION_IGNORE != action);
1195 wildif = new_interface(NULL);
1197 strlcpy(wildif->name, "v4wildcard", sizeof(wildif->name));
1198 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1199 wildif->family = AF_INET;
1200 AF(&wildif->mask) = AF_INET;
1201 SET_ONESMASK(&wildif->mask);
1203 wildif->flags = INT_BROADCAST | INT_UP | INT_WILDCARD;
1204 wildif->ignore_packets = (ACTION_DROP == action);
1207 * enable multicast reception on the broadcast socket
1209 AF(&wildif->bcast) = AF_INET;
1210 SET_ADDR4N(&wildif->bcast, INADDR_ANY);
1211 SET_PORT(&wildif->bcast, port);
1213 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1215 if (wildif->fd != INVALID_SOCKET) {
1217 any_interface = wildif;
1219 add_addr_to_list(&wildif->sin, wildif);
1220 add_interface(wildif);
1221 log_listen_address(wildif);
1224 "unable to bind to wildcard address %s - another process may be running - EXITING",
1225 stoa(&wildif->sin));
1228 DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1234 * add_nic_rule() -- insert a rule entry at the head of nic_rule_list.
1238 nic_rule_match match_type,
1239 const char * if_name, /* interface name or numeric address */
1241 nic_rule_action action
1245 isc_boolean_t is_ip;
1247 rule = emalloc_zero(sizeof(*rule));
1248 rule->match_type = match_type;
1249 rule->prefixlen = prefixlen;
1250 rule->action = action;
1252 if (MATCH_IFNAME == match_type) {
1253 REQUIRE(NULL != if_name);
1254 rule->if_name = estrdup(if_name);
1255 } else if (MATCH_IFADDR == match_type) {
1256 REQUIRE(NULL != if_name);
1257 /* set rule->addr */
1258 is_ip = is_ip_address(if_name, AF_UNSPEC, &rule->addr);
1261 REQUIRE(NULL == if_name);
1263 LINK_SLIST(nic_rule_list, rule, next);
1270 nic_rule_action action
1278 t = "ERROR"; /* quiet uninit warning */
1279 DPRINTF(1, ("fatal: unknown nic_rule_action %d\n",
1302 static nic_rule_action
1305 sockaddr_u * if_addr,
1313 DPRINTF(4, ("interface_action: interface %s ",
1314 (if_name != NULL) ? if_name : "wildcard"));
1316 iswildcard = is_wildcard_addr(if_addr);
1317 isloopback = !!(INT_LOOPBACK & if_flags);
1320 * Find any matching NIC rule from --interface / -I or ntp.conf
1321 * interface/nic rules.
1323 for (rule = nic_rule_list; rule != NULL; rule = rule->next) {
1325 switch (rule->match_type) {
1328 /* loopback and wildcard excluded from "all" */
1329 if (isloopback || iswildcard)
1331 DPRINTF(4, ("nic all %s\n",
1332 action_text(rule->action)));
1333 return rule->action;
1336 if (IS_IPV4(if_addr)) {
1337 DPRINTF(4, ("nic ipv4 %s\n",
1338 action_text(rule->action)));
1339 return rule->action;
1344 if (IS_IPV6(if_addr)) {
1345 DPRINTF(4, ("nic ipv6 %s\n",
1346 action_text(rule->action)));
1347 return rule->action;
1351 case MATCH_WILDCARD:
1353 DPRINTF(4, ("nic wildcard %s\n",
1354 action_text(rule->action)));
1355 return rule->action;
1360 if (rule->prefixlen != -1) {
1361 if (addr_eqprefix(if_addr, &rule->addr,
1364 DPRINTF(4, ("subnet address match - %s\n",
1365 action_text(rule->action)));
1366 return rule->action;
1369 if (SOCK_EQ(if_addr, &rule->addr)) {
1371 DPRINTF(4, ("address match - %s\n",
1372 action_text(rule->action)));
1373 return rule->action;
1379 #if defined(HAVE_FNMATCH) && defined(FNM_CASEFOLD)
1380 && !fnmatch(rule->if_name, if_name, FNM_CASEFOLD)
1382 && !strcasecmp(if_name, rule->if_name)
1386 DPRINTF(4, ("interface name match - %s\n",
1387 action_text(rule->action)));
1388 return rule->action;
1395 * Unless explicitly disabled such as with "nic ignore ::1"
1396 * listen on loopback addresses. Since ntpq and ntpdc query
1397 * "localhost" by default, which typically resolves to ::1 and
1398 * 127.0.0.1, it's useful to default to listening on both.
1401 DPRINTF(4, ("default loopback listen\n"));
1402 return ACTION_LISTEN;
1406 * Treat wildcard addresses specially. If there is no explicit
1407 * "nic ... wildcard" or "nic ... 0.0.0.0" or "nic ... ::" rule
1411 DPRINTF(4, ("default wildcard drop\n"));
1416 * Check for "virtual IP" (colon in the interface name) after
1417 * the rules so that "ntpd --interface eth0:1 -novirtualips"
1418 * does indeed listen on eth0:1's addresses.
1420 if (!listen_to_virtual_ips && if_name != NULL
1421 && (strchr(if_name, ':') != NULL)) {
1423 DPRINTF(4, ("virtual ip - ignore\n"));
1424 return ACTION_IGNORE;
1428 * If there are no --interface/-I command-line options and no
1429 * interface/nic rules in ntp.conf, the default action is to
1430 * listen. In the presence of rules from either, the default
1431 * is to ignore. This implements ntpd's traditional listen-
1432 * every default with no interface listen configuration, and
1433 * ensures a single -I eth0 or "nic listen eth0" means do not
1434 * listen on any other addresses.
1436 if (NULL == nic_rule_list) {
1437 DPRINTF(4, ("default listen\n"));
1438 return ACTION_LISTEN;
1441 DPRINTF(4, ("implicit ignore\n"));
1442 return ACTION_IGNORE;
1448 isc_interface_t *isc_if,
1453 const u_char v6loop[16] = {0, 0, 0, 0, 0, 0, 0, 0,
1454 0, 0, 0, 0, 0, 0, 0, 1};
1456 strlcpy(itf->name, isc_if->name, sizeof(itf->name));
1457 itf->ifindex = isc_if->ifindex;
1458 itf->family = (u_short)isc_if->af;
1459 AF(&itf->sin) = itf->family;
1460 AF(&itf->mask) = itf->family;
1461 AF(&itf->bcast) = itf->family;
1462 SET_PORT(&itf->sin, port);
1463 SET_PORT(&itf->mask, port);
1464 SET_PORT(&itf->bcast, port);
1466 if (IS_IPV4(&itf->sin)) {
1467 NSRCADR(&itf->sin) = isc_if->address.type.in.s_addr;
1468 NSRCADR(&itf->mask) = isc_if->netmask.type.in.s_addr;
1470 if (isc_if->flags & INTERFACE_F_BROADCAST) {
1471 itf->flags |= INT_BROADCAST;
1472 NSRCADR(&itf->bcast) =
1473 isc_if->broadcast.type.in.s_addr;
1476 #ifdef INCLUDE_IPV6_SUPPORT
1477 else if (IS_IPV6(&itf->sin)) {
1478 SET_ADDR6N(&itf->sin, isc_if->address.type.in6);
1479 SET_ADDR6N(&itf->mask, isc_if->netmask.type.in6);
1481 SET_SCOPE(&itf->sin, isc_if->address.zone);
1483 #endif /* INCLUDE_IPV6_SUPPORT */
1486 /* Process the rest of the flags */
1489 ((INTERFACE_F_UP & isc_if->flags)
1491 | ((INTERFACE_F_LOOPBACK & isc_if->flags)
1493 | ((INTERFACE_F_POINTTOPOINT & isc_if->flags)
1495 | ((INTERFACE_F_MULTICAST & isc_if->flags)
1496 ? INT_MULTICAST : 0)
1497 | ((INTERFACE_F_PRIVACY & isc_if->flags)
1502 * Clear the loopback flag if the address is not localhost.
1503 * http://bugs.ntp.org/1683
1505 if (INT_LOOPBACK & itf->flags) {
1506 if (AF_INET == itf->family) {
1507 if (127 != (SRCADR(&itf->sin) >> 24))
1508 itf->flags &= ~INT_LOOPBACK;
1510 if (memcmp(v6loop, NSRCADR6(&itf->sin),
1511 sizeof(NSRCADR6(&itf->sin))))
1512 itf->flags &= ~INT_LOOPBACK;
1521 * some OSes have been observed to keep
1522 * cached routes even when more specific routes
1524 * this can be mitigated by re-binding
1529 struct interface * interface
1532 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
1533 if (interface->fd != INVALID_SOCKET) {
1534 int bcast = (interface->flags & INT_BCASTXMIT) != 0;
1535 /* as we forcibly close() the socket remove the
1536 broadcast permission indication */
1538 socket_broadcast_disable(interface, &interface->sin);
1540 close_and_delete_fd_from_list(interface->fd);
1542 /* create new socket picking up a new first hop binding
1543 at connect() time */
1544 interface->fd = open_socket(&interface->sin,
1545 bcast, 0, interface);
1547 * reset TTL indication so TTL is is set again
1550 interface->last_ttl = 0;
1551 return (interface->fd != INVALID_SOCKET);
1553 return 0; /* invalid sockets are not refreshable */
1554 #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1555 return (interface->fd != INVALID_SOCKET);
1556 #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1560 * interface_update - externally callable update function
1564 interface_receiver_t receiver,
1567 int new_interface_found;
1569 if (disable_dynamic_updates)
1573 new_interface_found = update_interfaces(NTP_PORT, receiver, data);
1576 if (!new_interface_found)
1580 msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
1582 interrupt_worker_sleep();
1587 * sau_from_netaddr() - convert network address on-wire formats.
1588 * Convert from libisc's isc_netaddr_t to NTP's sockaddr_u
1593 const isc_netaddr_t *pna
1597 AF(psau) = (u_short)pna->family;
1598 switch (pna->family) {
1601 memcpy(&psau->sa4.sin_addr, &pna->type.in,
1602 sizeof(psau->sa4.sin_addr));
1606 memcpy(&psau->sa6.sin6_addr, &pna->type.in6,
1607 sizeof(psau->sa6.sin6_addr));
1615 const sockaddr_u *psau
1618 if (IS_IPV4(psau) && !NSRCADR(psau))
1621 #ifdef INCLUDE_IPV6_SUPPORT
1622 if (IS_IPV6(psau) && S_ADDR6_EQ(psau, &in6addr_any))
1630 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
1632 * enable/disable re-use of wildcard address socket
1640 struct interface *any;
1641 SOCKET fd = INVALID_SOCKET;
1643 any = ANY_INTERFACE_BYFAM(family);
1647 if (fd != INVALID_SOCKET) {
1648 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1649 (char *)&on, sizeof(on)))
1651 "set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m",
1654 DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n",
1659 #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
1662 static isc_boolean_t
1669 #if defined(INCLUDE_IPV6_SUPPORT) && defined(SIOCGIFAFLAG_IN6)
1670 struct in6_ifreq ifr6;
1673 if (psau->sa.sa_family != AF_INET6)
1675 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1678 memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr));
1679 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1680 if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1685 if ((ifr6.ifr_ifru.ifru_flags6 & flags6) != 0)
1687 #endif /* INCLUDE_IPV6_SUPPORT && SIOCGIFAFLAG_IN6 */
1691 static isc_boolean_t
1697 #ifdef IN6_IFF_ANYCAST
1698 return check_flags6(psau, name, IN6_IFF_ANYCAST);
1704 static isc_boolean_t
1713 #ifdef IN6_IFF_DEPARTED
1714 flags6 |= IN6_IFF_DEPARTED;
1716 #ifdef IN6_IFF_DETACHED
1717 flags6 |= IN6_IFF_DETACHED;
1719 #ifdef IN6_IFF_TENTATIVE
1720 flags6 |= IN6_IFF_TENTATIVE;
1722 return check_flags6(psau, name, flags6) ? ISC_FALSE : ISC_TRUE;
1726 * update_interface strategy
1728 * toggle configuration phase
1731 * forall currently existing interfaces
1732 * if address is known:
1733 * drop socket - rebind again
1735 * if address is NOT known:
1736 * attempt to create a new interface entry
1739 * forall currently known non MCAST and WILDCARD interfaces
1740 * if interface does not match configuration phase (not seen in phase 1):
1741 * remove interface from known interface list
1742 * forall peers associated with this interface
1743 * disconnect peer from this interface
1746 * attempt to re-assign interfaces to peers
1753 interface_receiver_t receiver,
1757 isc_mem_t * mctx = (void *)-1;
1758 interface_info_t ifi;
1759 isc_interfaceiter_t * iter;
1760 isc_result_t result;
1761 isc_interface_t isc_if;
1762 int new_interface_found;
1763 unsigned int family;
1768 DPRINTF(3, ("update_interfaces(%d)\n", port));
1771 * phase one - scan interfaces
1772 * - create those that are not found
1773 * - update those that are found
1776 new_interface_found = FALSE;
1778 result = isc_interfaceiter_create(mctx, &iter);
1780 if (result != ISC_R_SUCCESS)
1784 * Toggle system interface scan phase to find untouched
1785 * interfaces to be deleted.
1787 sys_interphase ^= 0x1;
1789 for (result = isc_interfaceiter_first(iter);
1790 ISC_R_SUCCESS == result;
1791 result = isc_interfaceiter_next(iter)) {
1793 result = isc_interfaceiter_current(iter, &isc_if);
1795 if (result != ISC_R_SUCCESS)
1798 /* See if we have a valid family to use */
1799 family = isc_if.address.family;
1800 if (AF_INET != family && AF_INET6 != family)
1802 if (AF_INET == family && !ipv4_works)
1804 if (AF_INET6 == family && !ipv6_works)
1807 /* create prototype */
1808 init_interface(&enumep);
1810 convert_isc_if(&isc_if, &enumep, port);
1812 DPRINT_INTERFACE(4, (&enumep, "examining ", "\n"));
1815 * Check if and how we are going to use the interface.
1817 switch (interface_action(enumep.name, &enumep.sin,
1821 DPRINTF(4, ("ignoring interface %s (%s) - by nic rules\n",
1822 enumep.name, stoa(&enumep.sin)));
1826 DPRINTF(4, ("listen interface %s (%s) - by nic rules\n",
1827 enumep.name, stoa(&enumep.sin)));
1828 enumep.ignore_packets = ISC_FALSE;
1832 DPRINTF(4, ("drop on interface %s (%s) - by nic rules\n",
1833 enumep.name, stoa(&enumep.sin)));
1834 enumep.ignore_packets = ISC_TRUE;
1838 /* interfaces must be UP to be usable */
1839 if (!(enumep.flags & INT_UP)) {
1840 DPRINTF(4, ("skipping interface %s (%s) - DOWN\n",
1841 enumep.name, stoa(&enumep.sin)));
1846 * skip any interfaces UP and bound to a wildcard
1847 * address - some dhcp clients produce that in the
1850 if (is_wildcard_addr(&enumep.sin))
1853 if (is_anycast(&enumep.sin, isc_if.name))
1857 * skip any address that is an invalid state to be used
1859 if (!is_valid(&enumep.sin, isc_if.name))
1863 * map to local *address* in order to map all duplicate
1864 * interfaces to an endpt structure with the appropriate
1865 * socket. Our name space is (ip-address), NOT
1866 * (interface name, ip-address).
1868 ep = getinterface(&enumep.sin, INT_WILDCARD);
1870 if (ep != NULL && refresh_interface(ep)) {
1872 * found existing and up to date interface -
1875 if (ep->phase != sys_interphase) {
1877 * On a new round we reset the name so
1878 * the interface name shows up again if
1879 * this address is no longer shared.
1880 * We reset ignore_packets from the
1881 * new prototype to respect any runtime
1882 * changes to the nic rules.
1884 strlcpy(ep->name, enumep.name,
1886 ep->ignore_packets =
1887 enumep.ignore_packets;
1889 /* name collision - rename interface */
1890 strlcpy(ep->name, "*multiple*",
1894 DPRINT_INTERFACE(4, (ep, "updating ",
1897 if (ep->ignore_packets !=
1898 enumep.ignore_packets) {
1900 * We have conflicting configurations
1901 * for the interface address. This is
1902 * caused by using -I <interfacename>
1903 * for an interface that shares its
1904 * address with other interfaces. We
1905 * can not disambiguate incoming
1906 * packets delivered to this socket
1907 * without extra syscalls/features.
1908 * These are not (commonly) available.
1909 * Note this is a more unusual
1910 * configuration where several
1911 * interfaces share an address but
1912 * filtering via interface name is
1913 * attempted. We resolve the
1914 * configuration conflict by disabling
1915 * the processing of received packets.
1916 * This leads to no service on the
1917 * interface address where the conflict
1921 "WARNING: conflicting enable configuration for interfaces %s and %s for address %s - unsupported configuration - address DISABLED",
1922 enumep.name, ep->name,
1925 ep->ignore_packets = ISC_TRUE;
1928 ep->phase = sys_interphase;
1930 ifi.action = IFS_EXISTS;
1932 if (receiver != NULL)
1933 (*receiver)(data, &ifi);
1936 * This is new or refreshing failed - add to
1937 * our interface list. If refreshing failed we
1938 * will delete the interface structure in phase
1939 * 2 as the interface was not marked current.
1940 * We can bind to the address as the refresh
1941 * code already closed the offending socket
1943 ep = create_interface(port, &enumep);
1946 ifi.action = IFS_CREATED;
1948 if (receiver != NULL)
1949 (*receiver)(data, &ifi);
1951 new_interface_found = TRUE;
1954 " new - created\n"));
1957 (&enumep, "updating ",
1958 " new - creation FAILED"));
1961 "failed to init interface for address %s",
1968 isc_interfaceiter_destroy(&iter);
1971 * phase 2 - delete gone interfaces - reassigning peers to
1974 for (ep = ep_list; ep != NULL; ep = next_ep) {
1975 next_ep = ep->elink;
1978 * if phase does not match sys_phase this interface was
1979 * not enumerated during the last interface scan - so it
1980 * is gone and will be deleted here unless it did not
1981 * originate from interface enumeration (INT_WILDCARD,
1984 if (((INT_WILDCARD | INT_MCASTIF) & ep->flags) ||
1985 ep->phase == sys_interphase)
1988 DPRINT_INTERFACE(3, (ep, "updating ",
1989 "GONE - deleting\n"));
1990 remove_interface(ep);
1992 ifi.action = IFS_DELETED;
1994 if (receiver != NULL)
1995 (*receiver)(data, &ifi);
1997 /* disconnect peers from deleted endpt. */
1998 while (ep->peers != NULL)
1999 set_peerdstadr(ep->peers, NULL);
2002 * update globals in case we lose
2003 * a loopback interface
2005 if (ep == loopback_interface)
2006 loopback_interface = NULL;
2008 delete_interface(ep);
2012 * phase 3 - re-configure as the world has possibly changed
2014 * never ever make this conditional again - it is needed to track
2015 * routing updates. see bug #2506
2017 refresh_all_peerinterfaces();
2019 if (broadcast_client_enabled)
2027 * Check multicast interfaces and try to join multicast groups if
2030 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2033 if (!(INT_MCASTIF & ep->flags) || (INT_MCASTOPEN & ep->flags))
2036 /* Find remote address that was linked to this interface */
2037 for (entry = remoteaddr_list;
2039 entry = entry->link) {
2040 if (entry->ep == ep) {
2041 if (socket_multicast_enable(ep, &entry->addr)) {
2043 "Joined %s socket to multicast group %s",
2045 stoa(&entry->addr));
2053 return new_interface_found;
2058 * create_sockets - create a socket for each interface plus a default
2059 * socket for when we don't know where to send
2066 #ifndef HAVE_IO_COMPLETION_PORT
2068 * I/O Completion Ports don't care about the select and FD_SET
2071 FD_ZERO(&activefds);
2074 DPRINTF(2, ("create_sockets(%d)\n", port));
2076 create_wildcards(port);
2078 update_interfaces(port, NULL, NULL);
2081 * Now that we have opened all the sockets, turn off the reuse
2082 * flag for security.
2086 DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
2092 * create_interface - create a new interface for a given prototype
2093 * binding the socket.
2095 static struct interface *
2098 struct interface * protot
2103 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2105 remaddr_t * next_entry;
2107 DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&protot->sin),
2110 /* build an interface */
2111 iface = new_interface(protot);
2116 iface->fd = open_socket(&iface->sin, 0, 0, iface);
2118 if (iface->fd != INVALID_SOCKET)
2119 log_listen_address(iface);
2121 if ((INT_BROADCAST & iface->flags)
2122 && iface->bfd != INVALID_SOCKET)
2123 msyslog(LOG_INFO, "Listening on broadcast address %s#%d",
2124 stoa((&iface->bcast)), port);
2126 if (INVALID_SOCKET == iface->fd
2127 && INVALID_SOCKET == iface->bfd) {
2128 msyslog(LOG_ERR, "unable to create socket on %s (%d) for %s#%d",
2131 stoa((&iface->sin)),
2133 delete_interface(iface);
2138 * Blacklist our own addresses, no use talking to ourself
2140 SET_HOSTMASK(&resmask, AF(&iface->sin));
2141 hack_restrict(RESTRICT_FLAGS, &iface->sin, &resmask,
2142 RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
2145 * set globals with the first found
2146 * loopback interface of the appropriate class
2148 if (NULL == loopback_interface && AF_INET == iface->family
2149 && (INT_LOOPBACK & iface->flags))
2150 loopback_interface = iface;
2153 * put into our interface list
2155 add_addr_to_list(&iface->sin, iface);
2156 add_interface(iface);
2158 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2160 * Join any previously-configured compatible multicast groups.
2162 if (INT_MULTICAST & iface->flags &&
2163 !((INT_LOOPBACK | INT_WILDCARD) & iface->flags) &&
2164 !iface->ignore_packets) {
2165 for (entry = remoteaddr_list;
2167 entry = next_entry) {
2168 next_entry = entry->link;
2169 if (AF(&iface->sin) != AF(&entry->addr) ||
2170 !IS_MCAST(&entry->addr))
2172 if (socket_multicast_enable(iface,
2175 "Joined %s socket to multicast group %s",
2177 stoa(&entry->addr));
2180 "Failed to join %s socket to multicast group %s",
2182 stoa(&entry->addr));
2185 #endif /* MCAST && MCAST_NONEWSOCKET */
2187 DPRINT_INTERFACE(2, (iface, "created ", "\n"));
2192 #ifdef SO_EXCLUSIVEADDRUSE
2204 failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
2205 (char *)&one, sizeof(one));
2212 * Prior to Windows XP setting SO_EXCLUSIVEADDRUSE can fail with
2213 * error WSAINVAL depending on service pack level and whether
2214 * the user account is in the Administrators group. Do not
2215 * complain if it fails that way on versions prior to XP (5.1).
2217 err = GetLastError();
2219 if (isc_win32os_versioncheck(5, 1, 0, 0) < 0 /* < 5.1/XP */
2220 && WSAEINVAL == err)
2226 "setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m",
2229 #endif /* SO_EXCLUSIVEADDRUSE */
2233 * set_reuseaddr() - set/clear REUSEADDR on all sockets
2234 * NB possible hole - should we be doing this on broadcast
2242 #ifndef SO_EXCLUSIVEADDRUSE
2245 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2246 if (ep->flags & INT_WILDCARD)
2250 * if ep->fd is INVALID_SOCKET, we might have a adapter
2251 * configured but not present
2253 DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n",
2254 ep->name, stoa(&ep->sin),
2255 flag ? "on" : "off"));
2257 if (ep->fd != INVALID_SOCKET) {
2258 if (setsockopt(ep->fd, SOL_SOCKET, SO_REUSEADDR,
2259 (char *)&flag, sizeof(flag))) {
2260 msyslog(LOG_ERR, "set_reuseaddr: setsockopt(%s, SO_REUSEADDR, %s) failed: %m",
2261 stoa(&ep->sin), flag ? "on" : "off");
2265 #endif /* ! SO_EXCLUSIVEADDRUSE */
2269 * This is just a wrapper around an internal function so we can
2270 * make other changes as necessary later on
2274 struct interface * iface,
2278 #ifdef OPEN_BCAST_SOCKET
2279 socket_broadcast_enable(iface, iface->fd, baddr);
2283 #ifdef OPEN_BCAST_SOCKET
2285 * Enable a broadcast address to a given socket
2286 * The socket is in the ep_list all we need to do is enable
2287 * broadcasting. It is not this function's job to select the socket
2289 static isc_boolean_t
2290 socket_broadcast_enable(
2291 struct interface * iface,
2299 if (IS_IPV4(baddr)) {
2300 /* if this interface can support broadcast, set SO_BROADCAST */
2301 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
2302 (char *)&on, sizeof(on)))
2304 "setsockopt(SO_BROADCAST) enable failure on address %s: %m",
2307 DPRINTF(2, ("Broadcast enabled on socket %d for address %s\n",
2310 iface->flags |= INT_BCASTXMIT;
2314 #endif /* SO_BROADCAST */
2317 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
2319 * Remove a broadcast address from a given socket
2320 * The socket is in the ep_list all we need to do is disable
2321 * broadcasting. It is not this function's job to select the socket
2323 static isc_boolean_t
2324 socket_broadcast_disable(
2325 struct interface * iface,
2330 int off = 0; /* This seems to be OK as an int */
2332 if (IS_IPV4(baddr) && setsockopt(iface->fd, SOL_SOCKET,
2333 SO_BROADCAST, (char *)&off, sizeof(off)))
2335 "setsockopt(SO_BROADCAST) disable failure on address %s: %m",
2338 iface->flags &= ~INT_BCASTXMIT;
2342 #endif /* SO_BROADCAST */
2344 #endif /* OS_MISSES_SPECIFIC_ROUTE_UPDATES */
2346 #endif /* OPEN_BCAST_SOCKET */
2349 * return the broadcast client flag value
2352 get_broadcastclient_flag(void)
2354 return (broadcast_client_enabled);
2357 * Check to see if the address is a multicast address
2359 static isc_boolean_t
2364 isc_boolean_t result;
2366 #ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
2368 * If we don't have IPV6 support any IPV6 addr is not multicast
2374 result = IS_MCAST(maddr);
2377 DPRINTF(4, ("address %s is not multicast\n",
2384 * Multicast servers need to set the appropriate Multicast interface
2385 * socket option in order for it to know which interface to use for
2386 * send the multicast packet.
2389 enable_multicast_if(
2390 struct interface * iface,
2395 #ifdef IP_MULTICAST_LOOP
2396 TYPEOF_IP_MULTICAST_LOOP off = 0;
2398 #if defined(INCLUDE_IPV6_MULTICAST_SUPPORT) && defined(IPV6_MULTICAST_LOOP)
2402 REQUIRE(AF(maddr) == AF(&iface->sin));
2404 switch (AF(&iface->sin)) {
2407 #ifdef IP_MULTICAST_LOOP
2409 * Don't send back to itself, but allow failure to set
2411 if (setsockopt(iface->fd, IPPROTO_IP,
2413 SETSOCKOPT_ARG_CAST &off,
2417 "setsockopt IP_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2418 iface->fd, stoa(&iface->sin),
2425 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2426 #ifdef IPV6_MULTICAST_LOOP
2428 * Don't send back to itself, but allow failure to set
2430 if (setsockopt(iface->fd, IPPROTO_IPV6,
2431 IPV6_MULTICAST_LOOP,
2432 (char *) &off6, sizeof(off6))) {
2435 "setsockopt IPV6_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2436 iface->fd, stoa(&iface->sin),
2443 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2450 * Add a multicast address to a given socket
2451 * The socket is in the ep_list all we need to do is enable
2452 * multicasting. It is not this function's job to select the socket
2455 static isc_boolean_t
2456 socket_multicast_enable(
2461 struct ip_mreq mreq;
2462 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2463 struct ipv6_mreq mreq6;
2465 switch (AF(maddr)) {
2469 mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2470 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
2471 if (setsockopt(iface->fd,
2477 "setsockopt IP_ADD_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2478 iface->fd, stoa(&iface->sin),
2479 mreq.imr_multiaddr.s_addr,
2480 mreq.imr_interface.s_addr,
2484 DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n",
2485 iface->fd, stoa(&iface->sin),
2486 mreq.imr_multiaddr.s_addr,
2487 mreq.imr_interface.s_addr, stoa(maddr)));
2491 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2493 * Enable reception of multicast packets.
2494 * If the address is link-local we can get the
2495 * interface index from the scope id. Don't do this
2496 * for other types of multicast addresses. For now let
2497 * the kernel figure it out.
2500 mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2501 mreq6.ipv6mr_interface = iface->ifindex;
2503 if (setsockopt(iface->fd, IPPROTO_IPV6,
2504 IPV6_JOIN_GROUP, (char *)&mreq6,
2507 "setsockopt IPV6_JOIN_GROUP failed: %m on socket %d, addr %s for interface %u (%s)",
2508 iface->fd, stoa(&iface->sin),
2509 mreq6.ipv6mr_interface, stoa(maddr)));
2512 DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %u (%s)\n",
2513 iface->fd, stoa(&iface->sin),
2514 mreq6.ipv6mr_interface, stoa(maddr)));
2517 # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2519 iface->flags |= INT_MCASTOPEN;
2528 * Remove a multicast address from a given socket
2529 * The socket is in the ep_list all we need to do is disable
2530 * multicasting. It is not this function's job to select the socket
2533 static isc_boolean_t
2534 socket_multicast_disable(
2535 struct interface * iface,
2539 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2540 struct ipv6_mreq mreq6;
2542 struct ip_mreq mreq;
2546 if (find_addr_in_list(maddr) == NULL) {
2547 DPRINTF(4, ("socket_multicast_disable(%s): not found\n",
2552 switch (AF(maddr)) {
2555 mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2556 mreq.imr_interface = SOCK_ADDR4(&iface->sin);
2557 if (setsockopt(iface->fd, IPPROTO_IP,
2558 IP_DROP_MEMBERSHIP, (char *)&mreq,
2562 "setsockopt IP_DROP_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2563 iface->fd, stoa(&iface->sin),
2564 SRCADR(maddr), SRCADR(&iface->sin),
2570 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2572 * Disable reception of multicast packets
2573 * If the address is link-local we can get the
2574 * interface index from the scope id. Don't do this
2575 * for other types of multicast addresses. For now let
2576 * the kernel figure it out.
2578 mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2579 mreq6.ipv6mr_interface = iface->ifindex;
2581 if (setsockopt(iface->fd, IPPROTO_IPV6,
2582 IPV6_LEAVE_GROUP, (char *)&mreq6,
2586 "setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d (%s)",
2587 iface->fd, stoa(&iface->sin),
2588 iface->ifindex, stoa(maddr));
2594 # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2598 if (!iface->num_mcast)
2599 iface->flags &= ~INT_MCASTOPEN;
2606 * io_setbclient - open the broadcast client sockets
2611 #ifdef OPEN_BCAST_SOCKET
2612 struct interface * interf;
2618 for (interf = ep_list;
2620 interf = interf->elink) {
2622 if (interf->flags & (INT_WILDCARD | INT_LOOPBACK))
2625 /* use only allowed addresses */
2626 if (interf->ignore_packets)
2629 /* Need a broadcast-capable interface */
2630 if (!(interf->flags & INT_BROADCAST))
2633 /* Only IPv4 addresses are valid for broadcast */
2634 REQUIRE(IS_IPV4(&interf->sin));
2636 /* Do we already have the broadcast address open? */
2637 if (interf->flags & INT_BCASTOPEN) {
2639 * account for already open interfaces to avoid
2640 * misleading warning below
2647 * Try to open the broadcast address
2649 interf->family = AF_INET;
2650 interf->bfd = open_socket(&interf->bcast, 1, 0, interf);
2653 * If we succeeded then we use it otherwise enable
2654 * broadcast on the interface address
2656 if (interf->bfd != INVALID_SOCKET) {
2658 interf->flags |= INT_BCASTOPEN;
2660 "Listen for broadcasts to %s on interface #%d %s",
2661 stoa(&interf->bcast), interf->ifnum, interf->name);
2663 /* silently ignore EADDRINUSE as we probably opened
2664 the socket already for an address in the same network */
2665 if (errno != EADDRINUSE)
2667 "failed to listen for broadcasts to %s on interface #%d %s",
2668 stoa(&interf->bcast), interf->ifnum, interf->name);
2673 broadcast_client_enabled = ISC_TRUE;
2674 DPRINTF(1, ("io_setbclient: listening to %d broadcast addresses\n", nif));
2677 broadcast_client_enabled = ISC_FALSE;
2679 "Unable to listen for broadcasts, no broadcast interfaces available");
2683 "io_setbclient: Broadcast Client disabled by build");
2684 #endif /* OPEN_BCAST_SOCKET */
2688 * io_unsetbclient - close the broadcast client sockets
2691 io_unsetbclient(void)
2695 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2696 if (INT_WILDCARD & ep->flags)
2698 if (!(INT_BCASTOPEN & ep->flags))
2701 if (ep->bfd != INVALID_SOCKET) {
2702 /* destroy broadcast listening socket */
2704 "stop listening for broadcasts to %s on interface #%d %s",
2705 stoa(&ep->bcast), ep->ifnum, ep->name);
2706 close_and_delete_fd_from_list(ep->bfd);
2707 ep->bfd = INVALID_SOCKET;
2708 ep->flags &= ~INT_BCASTOPEN;
2711 broadcast_client_enabled = ISC_FALSE;
2715 * io_multicast_add() - add multicast group address
2727 * Check to see if this is a multicast address
2729 if (!addr_ismulticast(addr))
2732 /* If we already have it we can just return */
2733 if (NULL != find_flagged_addr_in_list(addr, INT_MCASTOPEN)) {
2735 "Duplicate request found for multicast address %s",
2740 # ifndef MULTICAST_NONEWSOCKET
2741 ep = new_interface(NULL);
2744 * Open a new socket for the multicast address
2747 SET_PORT(&ep->sin, NTP_PORT);
2748 ep->family = AF(&ep->sin);
2749 AF(&ep->mask) = ep->family;
2750 SET_ONESMASK(&ep->mask);
2753 ep->bfd = INVALID_SOCKET;
2754 ep->fd = open_socket(&ep->sin, 0, 0, ep);
2755 if (ep->fd != INVALID_SOCKET) {
2756 ep->ignore_packets = ISC_FALSE;
2757 ep->flags |= INT_MCASTIF;
2759 strlcpy(ep->name, "multicast", sizeof(ep->name));
2760 DPRINT_INTERFACE(2, (ep, "multicast add ", "\n"));
2762 log_listen_address(ep);
2764 /* bind failed, re-use wildcard interface */
2765 delete_interface(ep);
2769 else if (IS_IPV6(addr))
2775 /* HACK ! -- stuff in an address */
2776 /* because we don't bind addr? DH */
2779 "multicast address %s using wildcard interface #%d %s",
2780 stoa(addr), ep->ifnum, ep->name);
2783 "No multicast socket available to use for address %s",
2788 { /* in place of the { following for in #else clause */
2790 # else /* MULTICAST_NONEWSOCKET follows */
2792 * For the case where we can't use a separate socket (Windows)
2793 * join each applicable endpoint socket to the group address.
2799 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2800 if (ep->ignore_packets || AF(&ep->sin) != AF(addr) ||
2801 !(INT_MULTICAST & ep->flags) ||
2802 (INT_LOOPBACK | INT_WILDCARD) & ep->flags)
2805 # endif /* MULTICAST_NONEWSOCKET */
2806 if (socket_multicast_enable(ep, addr))
2808 "Joined %s socket to multicast group %s",
2813 add_addr_to_list(addr, one_ep);
2814 #else /* !MCAST follows*/
2816 "Can not add multicast address %s: no multicast support",
2824 * io_multicast_del() - delete multicast group address
2835 * Check to see if this is a multicast address
2837 if (!addr_ismulticast(addr)) {
2838 msyslog(LOG_ERR, "invalid multicast address %s",
2844 * Disable reception of multicast packets
2846 while ((iface = find_flagged_addr_in_list(addr, INT_MCASTOPEN))
2848 socket_multicast_disable(iface, addr);
2850 delete_addr_from_list(addr);
2852 #else /* not MCAST */
2854 "Can not delete multicast address %s: no multicast support",
2856 #endif /* not MCAST */
2861 * open_socket - open a socket, returning the file descriptor
2875 * int is OK for REUSEADR per
2876 * http://www.kohala.com/start/mcast.api.txt
2881 if (IS_IPV6(addr) && !ipv6_works)
2882 return INVALID_SOCKET;
2884 /* create a datagram (UDP) socket */
2885 fd = socket(AF(addr), SOCK_DGRAM, 0);
2886 if (INVALID_SOCKET == fd) {
2887 errval = socket_errno();
2889 "socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
2890 IS_IPV6(addr) ? "6" : "", stoa(addr));
2892 if (errval == EPROTONOSUPPORT ||
2893 errval == EAFNOSUPPORT ||
2894 errval == EPFNOSUPPORT)
2895 return (INVALID_SOCKET);
2899 "unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - exiting",
2905 connection_reset_fix(fd, addr);
2908 * Fixup the file descriptor for some systems
2909 * See bug #530 for details of the issue.
2914 * set SO_REUSEADDR since we will be binding the same port
2915 * number on each interface according to turn_off_reuse.
2916 * This is undesirable on Windows versions starting with
2917 * Windows XP (numeric version 5.1).
2920 if (isc_win32os_versioncheck(5, 1, 0, 0) < 0) /* before 5.1 */
2922 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2923 (char *)((turn_off_reuse)
2929 "setsockopt SO_REUSEADDR %s fails for address %s: %m",
2935 return INVALID_SOCKET;
2937 #ifdef SO_EXCLUSIVEADDRUSE
2939 * setting SO_EXCLUSIVEADDRUSE on the wildcard we open
2940 * first will cause more specific binds to fail.
2942 if (!(interf->flags & INT_WILDCARD))
2943 set_excladdruse(fd);
2947 * IPv4 specific options go here
2949 if (IS_IPV4(addr)) {
2950 #if defined(IPPROTO_IP) && defined(IP_TOS)
2951 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char*)&qos,
2954 "setsockopt IP_TOS (%02x) fails on address %s: %m",
2956 #endif /* IPPROTO_IP && IP_TOS */
2958 socket_broadcast_enable(interf, fd, addr);
2962 * IPv6 specific options go here
2964 if (IS_IPV6(addr)) {
2965 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
2966 if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, (char*)&qos,
2969 "setsockopt IPV6_TCLASS (%02x) fails on address %s: %m",
2971 #endif /* IPPROTO_IPV6 && IPV6_TCLASS */
2973 if (isc_net_probe_ipv6only() == ISC_R_SUCCESS
2974 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
2975 (char*)&on, sizeof(on)))
2977 "setsockopt IPV6_V6ONLY on fails on address %s: %m",
2980 #ifdef IPV6_BINDV6ONLY
2981 if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
2982 (char*)&on, sizeof(on)))
2984 "setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
2989 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2991 * some OSes don't allow binding to more specific
2992 * addresses if a wildcard address already bound
2993 * to the port and SO_REUSEADDR is not set
2995 if (!is_wildcard_addr(addr))
2996 set_wildcard_reuse(AF(addr), 1);
3000 * bind the local address.
3002 errval = bind(fd, &addr->sa, SOCKLEN(addr));
3004 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
3005 if (!is_wildcard_addr(addr))
3006 set_wildcard_reuse(AF(addr), 0);
3011 * Don't log this under all conditions
3013 if (turn_off_reuse == 0
3019 "bind(%d) AF_INET%s %s#%d%s flags 0x%x failed: %m",
3020 fd, IS_IPV6(addr) ? "6" : "",
3021 stoa(addr), SRCPORT(addr),
3022 IS_MCAST(addr) ? " (multicast)" : "",
3028 return INVALID_SOCKET;
3031 #ifdef HAVE_TIMESTAMP
3033 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
3034 (char*)&on, sizeof(on)))
3036 "setsockopt SO_TIMESTAMP on fails on address %s: %m",
3039 DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
3043 #ifdef HAVE_TIMESTAMPNS
3045 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
3046 (char*)&on, sizeof(on)))
3048 "setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
3051 DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
3057 if (setsockopt(fd, SOL_SOCKET, SO_BINTIME,
3058 (char*)&on, sizeof(on)))
3060 "setsockopt SO_BINTIME on fails on address %s: %m",
3063 DPRINTF(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
3068 DPRINTF(4, ("bind(%d) AF_INET%s, addr %s%%%d#%d, flags 0x%x\n",
3069 fd, IS_IPV6(addr) ? "6" : "", stoa(addr),
3070 SCOPE(addr), SRCPORT(addr), interf->flags));
3072 make_socket_nonblocking(fd);
3074 #ifdef HAVE_SIGNALED_IO
3075 init_socket_sig(fd);
3076 #endif /* not HAVE_SIGNALED_IO */
3078 add_fd_to_list(fd, FD_TYPE_SOCKET);
3080 #if !defined(SYS_WINNT) && !defined(VMS)
3081 DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
3082 fcntl(fd, F_GETFL, 0)));
3083 #endif /* SYS_WINNT || VMS */
3085 #if defined (HAVE_IO_COMPLETION_PORT)
3087 * Add the socket to the completion port
3089 if (io_completion_port_add_socket(fd, interf)) {
3090 msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
3099 #define sendto(fd, buf, len, flags, dest, destsz) \
3100 io_completion_port_sendto(fd, buf, len, (sockaddr_u *)(dest))
3103 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
3105 * sendpkt - send a packet to the specified destination. Maintain a
3106 * send error cache so that only the first consecutive error for a
3107 * destination is logged.
3112 struct interface * ep,
3124 ismcast = IS_MCAST(dest);
3128 src = (IS_IPV4(dest))
3134 * unbound peer - drop request and wait for better
3135 * network conditions
3137 DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
3138 ismcast ? "\tMCAST\t***** " : "",
3139 stoa(dest), ttl, len));
3144 DPRINTF(2, ("%ssendpkt(%d, dst=%s, src=%s, ttl=%d, len=%d)\n",
3145 ismcast ? "\tMCAST\t***** " : "", src->fd,
3146 stoa(dest), stoa(&src->sin), ttl, len));
3149 * for the moment we use the bcast option to set multicast ttl
3151 if (ismcast && ttl > 0 && ttl != src->last_ttl) {
3153 * set the multicast ttl for outgoing packets
3155 switch (AF(&src->sin)) {
3159 rc = setsockopt(src->fd, IPPROTO_IP,
3165 # ifdef INCLUDE_IPV6_SUPPORT
3167 rc = setsockopt(src->fd, IPPROTO_IPV6,
3168 IPV6_MULTICAST_HOPS,
3172 # endif /* INCLUDE_IPV6_SUPPORT */
3179 src->last_ttl = ttl;
3182 "setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
3188 cc = simulate_server(dest, src, pkt);
3190 cc = sendto(src->fd, (char *)pkt, (u_int)len, 0,
3191 &dest->sa, SOCKLEN(dest));
3202 } while (ismcast && src != NULL);
3206 #if !defined(HAVE_IO_COMPLETION_PORT)
3208 * fdbits - generate ascii representation of fd_set (FAU debug support)
3209 * HFDF format - highest fd first.
3217 static char buffer[256];
3218 char * buf = buffer;
3220 count = min(count, 255);
3222 while (count >= 0) {
3223 *buf++ = FD_ISSET(count, set) ? '#' : '-';
3234 * Routine to read the refclock packets for a specific interface
3235 * Return the number of bytes read. That way we know if we should
3236 * read it again or go on to the next one if no bytes returned
3239 read_refclock_packet(
3241 struct refclockio * rp,
3249 struct recvbuf * rb;
3251 rb = get_free_recv_buffer();
3255 * No buffer space available - just drop the packet
3257 char buf[RX_BUFF_SIZE];
3259 buflen = read(fd, buf, sizeof buf);
3264 /* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead
3265 * to buffer overrun and memory corruption
3267 if (rp->datalen <= 0 || rp->datalen > sizeof(rb->recv_space))
3268 read_count = sizeof(rb->recv_space);
3270 read_count = (u_int)rp->datalen;
3272 buflen = read(fd, (char *)&rb->recv_space, read_count);
3273 } while (buflen < 0 && EINTR == errno);
3276 saved_errno = errno;
3278 errno = saved_errno;
3283 * Got one. Mark how and when it got here,
3284 * put it on the full list and do bookkeeping.
3286 rb->recv_length = buflen;
3287 rb->recv_peer = rp->srcclock;
3291 rb->receiver = rp->clock_recv;
3293 consumed = indicate_refclock_packet(rp, rb);
3301 #endif /* REFCLOCK */
3304 #ifdef HAVE_PACKET_TIMESTAMP
3306 * extract timestamps from control message buffer
3310 struct recvbuf * rb,
3311 struct msghdr * msghdr,
3315 struct cmsghdr * cmsghdr;
3317 struct bintime * btp;
3319 #ifdef HAVE_TIMESTAMPNS
3320 struct timespec * tsp;
3322 #ifdef HAVE_TIMESTAMP
3323 struct timeval * tvp;
3325 unsigned long ticks;
3333 cmsghdr = CMSG_FIRSTHDR(msghdr);
3334 while (cmsghdr != NULL) {
3335 switch (cmsghdr->cmsg_type)
3339 #endif /* HAVE_BINTIME */
3340 #ifdef HAVE_TIMESTAMPNS
3341 case SCM_TIMESTAMPNS:
3342 #endif /* HAVE_TIMESTAMPNS */
3343 #ifdef HAVE_TIMESTAMP
3345 #endif /* HAVE_TIMESTAMP */
3346 #if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP)
3347 switch (cmsghdr->cmsg_type)
3351 btp = (struct bintime *)CMSG_DATA(cmsghdr);
3353 * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
3355 nts.l_i = btp->sec + JAN_1970;
3356 nts.l_uf = (u_int32)(btp->frac >> 32);
3357 if (sys_tick > measured_tick &&
3359 ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC));
3360 nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC));
3362 DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
3363 btp->sec, (unsigned long)((nts.l_uf / FRAC) * 1e9)));
3365 #endif /* HAVE_BINTIME */
3366 #ifdef HAVE_TIMESTAMPNS
3367 case SCM_TIMESTAMPNS:
3368 tsp = (struct timespec *)CMSG_DATA(cmsghdr);
3369 if (sys_tick > measured_tick &&
3371 ticks = (unsigned long)((tsp->tv_nsec * 1e-9) /
3373 tsp->tv_nsec = (long)(ticks * 1e9 *
3376 DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
3377 tsp->tv_sec, tsp->tv_nsec));
3378 nts = tspec_stamp_to_lfp(*tsp);
3380 #endif /* HAVE_TIMESTAMPNS */
3381 #ifdef HAVE_TIMESTAMP
3383 tvp = (struct timeval *)CMSG_DATA(cmsghdr);
3384 if (sys_tick > measured_tick &&
3386 ticks = (unsigned long)((tvp->tv_usec * 1e-6) /
3388 tvp->tv_usec = (long)(ticks * 1e6 *
3391 DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
3392 (intmax_t)tvp->tv_sec, (long)tvp->tv_usec));
3393 nts = tval_stamp_to_lfp(*tvp);
3395 #endif /* HAVE_TIMESTAMP */
3397 fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
3398 DTOLFP(fuzz, &lfpfuzz);
3399 L_ADD(&nts, &lfpfuzz);
3403 collect_timing(rb, "input processing delay", 1,
3405 DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
3407 #endif /* DEBUG_TIMING */
3408 ts = nts; /* network time stamp */
3410 #endif /* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */
3413 DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n",
3414 cmsghdr->cmsg_type));
3416 cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
3420 #endif /* HAVE_PACKET_TIMESTAMP */
3424 * Routine to read the network NTP packets for a specific interface
3425 * Return the number of bytes read. That way we know if we should
3426 * read it again or go on to the next one if no bytes returned
3429 read_network_packet(
3431 struct interface * itf,
3435 GETSOCKNAME_SOCKLEN_TYPE fromlen;
3437 register struct recvbuf *rb;
3438 #ifdef HAVE_PACKET_TIMESTAMP
3439 struct msghdr msghdr;
3441 char control[CMSG_BUFSIZE];
3445 * Get a buffer and read the frame. If we
3446 * haven't got a buffer, or this is received
3447 * on a disallowed socket, just dump the
3451 rb = get_free_recv_buffer();
3452 if (NULL == rb || itf->ignore_packets) {
3453 char buf[RX_BUFF_SIZE];
3459 fromlen = sizeof(from);
3460 buflen = recvfrom(fd, buf, sizeof(buf), 0,
3461 &from.sa, &fromlen);
3462 DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
3463 (itf->ignore_packets)
3466 free_recvbuffs(), fd, stoa(&from)));
3467 if (itf->ignore_packets)
3474 fromlen = sizeof(rb->recv_srcadr);
3476 #ifndef HAVE_PACKET_TIMESTAMP
3477 rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
3478 sizeof(rb->recv_space), 0,
3479 &rb->recv_srcadr.sa, &fromlen);
3481 iovec.iov_base = &rb->recv_space;
3482 iovec.iov_len = sizeof(rb->recv_space);
3483 msghdr.msg_name = &rb->recv_srcadr;
3484 msghdr.msg_namelen = fromlen;
3485 msghdr.msg_iov = &iovec;
3486 msghdr.msg_iovlen = 1;
3487 msghdr.msg_control = (void *)&control;
3488 msghdr.msg_controllen = sizeof(control);
3489 msghdr.msg_flags = 0;
3490 rb->recv_length = recvmsg(fd, &msghdr, 0);
3493 buflen = rb->recv_length;
3495 if (buflen == 0 || (buflen == -1 &&
3496 (EWOULDBLOCK == errno
3503 } else if (buflen < 0) {
3504 msyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
3505 stoa(&rb->recv_srcadr), fd);
3506 DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n",
3512 DPRINTF(3, ("read_network_packet: fd=%d length %d from %s\n",
3513 fd, buflen, stoa(&rb->recv_srcadr)));
3516 ** Bug 2672: Some OSes (MacOSX and Linux) don't block spoofed ::1
3519 if (AF_INET6 == itf->family) {
3520 DPRINTF(2, ("Got an IPv6 packet, from <%s> (%d) to <%s> (%d)\n",
3521 stoa(&rb->recv_srcadr),
3522 IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr)),
3524 !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3527 if ( IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr))
3528 && !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3531 DPRINTF(2, ("DROPPING that packet\n"));
3535 DPRINTF(2, ("processing that packet\n"));
3539 * Got one. Mark how and when it got here,
3540 * put it on the full list and do bookkeeping.
3544 #ifdef HAVE_PACKET_TIMESTAMP
3545 /* pick up a network time stamp if possible */
3546 ts = fetch_timestamp(rb, &msghdr, ts);
3549 rb->receiver = receive;
3551 add_full_recv_buffer(rb);
3559 * attempt to handle io (select()/signaled IO)
3564 # ifndef HAVE_SIGNALED_IO
3569 * Use select() on all on all input fd's for unlimited
3570 * time. select() will terminate on SIGALARM or on the
3571 * reception of input. Using select() means we can't do
3572 * robust signal handling and we get a potential race
3573 * between checking for alarms and doing the select().
3574 * Mostly harmless, I think.
3577 * On VMS, I suspect that select() can't be interrupted
3578 * by a "signal" either, so I take the easy way out and
3579 * have select() time out after one second.
3580 * System clock updates really aren't time-critical,
3581 * and - lacking a hardware reference clock - I have
3582 * yet to learn about anything else that is.
3585 # if !defined(VMS) && !defined(SYS_VXWORKS)
3586 nfound = select(maxactivefd + 1, &rdfdes, NULL,
3588 # else /* VMS, VxWorks */
3589 /* make select() wake up after one second */
3595 nfound = select(maxactivefd + 1,
3596 &rdfdes, NULL, NULL,
3599 # endif /* VMS, VxWorks */
3606 } else if (nfound == -1 && errno != EINTR) {
3607 msyslog(LOG_ERR, "select() error: %m");
3610 else if (debug > 4) {
3611 msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
3613 DPRINTF(3, ("select() returned %d: %m\n", nfound));
3616 # else /* HAVE_SIGNALED_IO */
3618 # endif /* HAVE_SIGNALED_IO */
3622 * input_handler - receive packets asynchronously
3635 struct timeval tvzero;
3636 l_fp ts; /* Timestamp at BOselect() gob */
3638 l_fp ts_e; /* Timestamp at EOselect() gob */
3641 size_t select_count;
3644 struct refclockio *rp;
3648 #ifdef HAS_ROUTING_SOCKET
3649 struct asyncio_reader * asyncio_reader;
3650 struct asyncio_reader * next_asyncio_reader;
3657 * If we have something to do, freeze a timestamp.
3658 * See below for the other cases (nothing left to do or error)
3663 * Do a poll to see who has data
3667 tvzero.tv_sec = tvzero.tv_usec = 0;
3669 n = select(maxactivefd + 1, &fds, (fd_set *)0, (fd_set *)0,
3673 * If there are no packets waiting just return
3679 * extended FAU debugging output
3683 "select(%d, %s, 0L, 0L, &0.0) error: %m",
3685 fdbits(maxactivefd, &activefds));
3688 for (j = 0, prior = 0; j <= maxactivefd; j++) {
3689 if (FD_ISSET(j, &activefds)) {
3690 if (-1 != read(j, &b, 0)) {
3695 "Removing bad file descriptor %d from select set",
3697 FD_CLR(j, &activefds);
3698 if (j == maxactivefd)
3699 maxactivefd = prior;
3711 * Check out the reference clocks first, if any
3714 if (refio != NULL) {
3715 for (rp = refio; rp != NULL; rp = rp->next) {
3718 if (!FD_ISSET(fd, &fds))
3721 buflen = read_refclock_packet(fd, rp, ts);
3723 * The first read must succeed after select()
3724 * indicates readability, or we've reached
3725 * a permanent EOF. http://bugs.ntp.org/1732
3726 * reported ntpd munching CPU after a USB GPS
3727 * was unplugged because select was indicating
3728 * EOF but ntpd didn't remove the descriptor
3729 * from the activefds set.
3731 if (buflen < 0 && EAGAIN != errno) {
3732 saved_errno = errno;
3733 clk = refnumtoa(&rp->srcclock->srcadr);
3734 errno = saved_errno;
3735 msyslog(LOG_ERR, "%s read: %m", clk);
3736 maintain_activefds(fd, TRUE);
3737 } else if (0 == buflen) {
3738 clk = refnumtoa(&rp->srcclock->srcadr);
3739 msyslog(LOG_ERR, "%s read EOF", clk);
3740 maintain_activefds(fd, TRUE);
3742 /* drain any remaining refclock input */
3744 buflen = read_refclock_packet(fd, rp, ts);
3745 } while (buflen > 0);
3749 #endif /* REFCLOCK */
3752 * Loop through the interfaces looking for data to read.
3754 for (ep = ep_list; ep != NULL; ep = ep->elink) {
3755 for (doing = 0; doing < 2; doing++) {
3759 if (!(ep->flags & INT_BCASTOPEN))
3765 if (FD_ISSET(fd, &fds))
3768 buflen = read_network_packet(
3770 } while (buflen > 0);
3771 /* Check more interfaces */
3775 #ifdef HAS_ROUTING_SOCKET
3777 * scan list of asyncio readers - currently only used for routing sockets
3779 asyncio_reader = asyncio_reader_list;
3781 while (asyncio_reader != NULL) {
3782 /* callback may unlink and free asyncio_reader */
3783 next_asyncio_reader = asyncio_reader->link;
3784 if (FD_ISSET(asyncio_reader->fd, &fds)) {
3786 (*asyncio_reader->receiver)(asyncio_reader);
3788 asyncio_reader = next_asyncio_reader;
3790 #endif /* HAS_ROUTING_SOCKET */
3793 * Check for a response from a blocking child
3795 for (idx = 0; idx < blocking_children_alloc; idx++) {
3796 c = blocking_children[idx];
3797 if (NULL == c || -1 == c->resp_read_pipe)
3799 if (FD_ISSET(c->resp_read_pipe, &fds)) {
3801 process_blocking_resp(c);
3806 * Done everything from that select.
3807 * If nothing to do, just return.
3808 * If an error occurred, complain and return.
3810 if (select_count == 0) { /* We really had nothing to do */
3813 msyslog(LOG_DEBUG, "input_handler: select() returned 0");
3817 /* We've done our work */
3821 * (ts_e - ts) is the amount of time we spent
3822 * processing this gob of file descriptors. Log
3826 collect_timing(NULL, "input handler", 1, &ts_e);
3829 "input_handler: Processed a gob of fd's in %s msec",
3831 #endif /* DEBUG_TIMING */
3836 #endif /* !HAVE_IO_COMPLETION_PORT */
3840 * find an interface suitable for the src address
3843 select_peerinterface(
3845 sockaddr_u * srcadr,
3853 wild = ANY_INTERFACE_CHOOSE(srcadr);
3856 * Initialize the peer structure and dance the interface jig.
3857 * Reference clocks step the loopback waltz, the others
3858 * squaredance around the interface list looking for a buddy. If
3859 * the dance peters out, there is always the wildcard interface.
3860 * This might happen in some systems and would preclude proper
3861 * operation with public key cryptography.
3863 if (ISREFCLOCKADR(srcadr)) {
3864 ep = loopback_interface;
3865 } else if (peer->cast_flags &
3866 (MDF_BCLNT | MDF_ACAST | MDF_MCAST | MDF_BCAST)) {
3867 ep = findbcastinter(srcadr);
3869 DPRINTF(4, ("Found *-cast interface %s for address %s\n",
3870 stoa(&ep->sin), stoa(srcadr)));
3872 DPRINTF(4, ("No *-cast local address found for address %s\n",
3880 * If it is a multicast address, findbcastinter() may not find
3881 * it. For unicast, we get to find the interface when dstadr is
3882 * given to us as the wildcard (ANY_INTERFACE_CHOOSE). Either
3883 * way, try a little harder.
3886 ep = findinterface(srcadr);
3888 * we do not bind to the wildcard interfaces for output
3889 * as our (network) source address would be undefined and
3890 * crypto will not work without knowing the own transmit address
3892 if (ep != NULL && INT_WILDCARD & ep->flags)
3893 if (!accept_wildcard_if_for_winnt)
3895 #else /* SIM follows */
3896 ep = loopback_interface;
3904 * findinterface - find local interface corresponding to address
3913 iface = findlocalinterface(addr, INT_WILDCARD, 0);
3915 if (NULL == iface) {
3916 DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
3919 iface = ANY_INTERFACE_CHOOSE(addr);
3921 DPRINTF(4, ("Found interface #%d %s for address %s\n",
3922 iface->ifnum, iface->name, stoa(addr)));
3928 * findlocalinterface - find local interface corresponding to addr,
3929 * which does not have any of flags set. If bast is nonzero, addr is
3930 * a broadcast address.
3932 * This code attempts to find the local sending address for an outgoing
3933 * address by connecting a new socket to destinationaddress:NTP_PORT
3934 * and reading the sockname of the resulting connect.
3935 * the complicated sequence simulates the routing table lookup
3936 * for to first hop without duplicating any of the routing logic into
3937 * ntpd. preferably we would have used an API call - but its not there -
3938 * so this is the best we can do here short of duplicating to entire routing
3939 * logic in ntpd which would be a silly and really unportable thing to do.
3949 GETSOCKNAME_SOCKLEN_TYPE sockaddrlen;
3956 DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
3959 s = socket(AF(addr), SOCK_DGRAM, 0);
3960 if (INVALID_SOCKET == s)
3964 * If we are looking for broadcast interface we need to set this
3965 * socket to allow broadcast
3969 if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET,
3978 rtn = connect(s, &addr->sa, SOCKLEN(addr));
3979 if (SOCKET_ERROR == rtn) {
3984 sockaddrlen = sizeof(saddr);
3985 rtn = getsockname(s, &saddr.sa, &sockaddrlen);
3987 if (SOCKET_ERROR == rtn)
3990 DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n",
3991 stoa(addr), stoa(&saddr)));
3993 iface = getinterface(&saddr, flags);
3996 * if we didn't find an exact match on saddr, find the closest
3997 * available local address. This handles the case of the
3998 * address suggested by the kernel being excluded by nic rules
3999 * or the user's -I and -L options to ntpd.
4000 * See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683
4001 * for more background.
4003 if (NULL == iface || iface->ignore_packets)
4004 iface = findclosestinterface(&saddr,
4005 flags | INT_LOOPBACK);
4007 /* Don't use an interface which will ignore replies */
4008 if (iface != NULL && iface->ignore_packets)
4016 * findclosestinterface
4018 * If there are -I/--interface or -L/novirtualips command-line options,
4019 * or "nic" or "interface" rules in ntp.conf, findlocalinterface() may
4020 * find the kernel's preferred local address for a given peer address is
4021 * administratively unavailable to ntpd, and punt to this routine's more
4024 * Find the numerically closest local address to the one connect()
4025 * suggested. This matches an address on the same subnet first, as
4026 * needed by Bug 1184, and provides a consistent choice if there are
4027 * multiple feasible local addresses, regardless of the order ntpd
4031 findclosestinterface(
4038 sockaddr_u addr_dist;
4039 sockaddr_u min_dist;
4041 ZERO_SOCK(&min_dist);
4044 for (ep = ep_list; ep != NULL; ep = ep->elink) {
4045 if (ep->ignore_packets ||
4046 AF(addr) != ep->family ||
4050 calc_addr_distance(&addr_dist, addr, &ep->sin);
4051 if (NULL == winner ||
4052 -1 == cmp_addr_distance(&addr_dist, &min_dist)) {
4053 min_dist = addr_dist;
4058 DPRINTF(4, ("findclosestinterface(%s) failed\n",
4061 DPRINTF(4, ("findclosestinterface(%s) -> %s\n",
4062 stoa(addr), stoa(&winner->sin)));
4069 * calc_addr_distance - calculate the distance between two addresses,
4070 * the absolute value of the difference between
4071 * the addresses numerically, stored as an address.
4076 const sockaddr_u * a1,
4077 const sockaddr_u * a2
4087 REQUIRE(AF(a1) == AF(a2));
4092 /* v4 can be done a bit simpler */
4096 v4dist = (a1val > a2val)
4099 SET_ADDR4(dist, v4dist);
4104 found_greater = FALSE;
4105 a1_greater = FALSE; /* suppress pot. uninit. warning */
4106 for (i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
4107 if (!found_greater &&
4108 NSRCADR6(a1)[i] != NSRCADR6(a2)[i]) {
4109 found_greater = TRUE;
4110 a1_greater = (NSRCADR6(a1)[i] > NSRCADR6(a2)[i]);
4112 if (!found_greater) {
4113 NSRCADR6(dist)[i] = 0;
4116 NSRCADR6(dist)[i] = NSRCADR6(a1)[i] -
4119 NSRCADR6(dist)[i] = NSRCADR6(a2)[i] -
4127 * cmp_addr_distance - compare two address distances, returning -1, 0,
4128 * 1 to indicate their relationship.
4132 const sockaddr_u * d1,
4133 const sockaddr_u * d2
4138 REQUIRE(AF(d1) == AF(d2));
4141 if (SRCADR(d1) < SRCADR(d2))
4143 else if (SRCADR(d1) == SRCADR(d2))
4149 for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
4150 if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i])
4152 else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i])
4162 * fetch an interface structure the matches the
4163 * address and has the given flags NOT set
4173 iface = find_addr_in_list(addr);
4175 if (iface != NULL && (iface->flags & flags))
4183 * findbcastinter - find broadcast interface corresponding to address
4193 #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
4194 DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
4197 iface = findlocalinterface(addr, INT_LOOPBACK | INT_WILDCARD,
4199 if (iface != NULL) {
4200 DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n",
4201 iface->ifnum, iface->name));
4206 * plan B - try to find something reasonable in our lists in
4207 * case kernel lookup doesn't help
4209 for (iface = ep_list; iface != NULL; iface = iface->elink) {
4210 if (iface->flags & INT_WILDCARD)
4213 /* Don't bother with ignored interfaces */
4214 if (iface->ignore_packets)
4218 * First look if this is the correct family
4220 if(AF(&iface->sin) != AF(addr))
4223 /* Skip the loopback addresses */
4224 if (iface->flags & INT_LOOPBACK)
4228 * If we are looking to match a multicast address and
4229 * this interface is one...
4231 if (addr_ismulticast(addr)
4232 && (iface->flags & INT_MULTICAST)) {
4233 #ifdef INCLUDE_IPV6_SUPPORT
4235 * ...it is the winner unless we're looking for
4236 * an interface to use for link-local multicast
4237 * and its address is not link-local.
4240 && IN6_IS_ADDR_MC_LINKLOCAL(PSOCK_ADDR6(addr))
4241 && !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin)))
4248 * We match only those interfaces marked as
4249 * broadcastable and either the explicit broadcast
4250 * address or the network portion of the IP address.
4253 if (IS_IPV4(addr)) {
4254 if (SOCK_EQ(&iface->bcast, addr))
4257 if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask))
4258 == (NSRCADR(addr) & NSRCADR(&iface->mask)))
4261 #ifdef INCLUDE_IPV6_SUPPORT
4262 else if (IS_IPV6(addr)) {
4263 if (SOCK_EQ(&iface->bcast, addr))
4266 if (SOCK_EQ(netof(&iface->sin), netof(addr)))
4271 #endif /* SIOCGIFCONF */
4272 if (NULL == iface) {
4273 DPRINTF(4, ("No bcast interface found for %s\n",
4275 iface = ANY_INTERFACE_CHOOSE(addr);
4277 DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n",
4278 iface->ifnum, iface->name));
4286 * io_clr_stats - clear I/O module statistics
4291 packets_dropped = 0;
4292 packets_ignored = 0;
4293 packets_received = 0;
4295 packets_notsent = 0;
4299 io_timereset = current_time;
4305 * io_addclock - add a reference clock to the list and arrange that we
4306 * get SIGIO interrupts from it.
4310 struct refclockio *rio
4316 * Stuff the I/O structure in the list and mark the descriptor
4317 * in use. There is a harmless (I hope) race condition here.
4321 # ifdef HAVE_SIGNALED_IO
4322 if (init_clock_sig(rio)) {
4326 # elif defined(HAVE_IO_COMPLETION_PORT)
4327 if (io_completion_port_add_clock_io(rio)) {
4336 LINK_SLIST(refio, rio, next);
4341 add_fd_to_list(rio->fd, FD_TYPE_FILE);
4349 * io_closeclock - close the clock in the I/O structure given
4353 struct refclockio *rio
4356 struct refclockio *unlinked;
4361 * Remove structure from the list
4363 rio->active = FALSE;
4364 UNLINK_SLIST(unlinked, refio, rio, next, struct refclockio);
4365 if (NULL != unlinked) {
4366 purge_recv_buffers_for_fd(rio->fd);
4368 * Close the descriptor.
4370 close_and_delete_fd_from_list(rio->fd);
4376 #endif /* REFCLOCK */
4380 * On NT a SOCKET is an unsigned int so we cannot possibly keep it in
4381 * an array. So we use one of the ISC_LIST functions to hold the
4382 * socket value and use that when we want to enumerate it.
4384 * This routine is called by the forked intres child process to close
4385 * all open sockets. On Windows there's no need as intres runs in
4386 * the same process as a thread.
4397 * In the child process we do not maintain activefds and
4398 * maxactivefd. Zeroing maxactivefd disables code which
4399 * maintains it in close_and_delete_fd_from_list().
4403 while (fd_list != NULL)
4404 close_and_delete_fd_from_list(fd_list->fd);
4408 #endif /* !SYS_WINNT */
4412 * Add and delete functions for the list of open sockets
4420 vsock_t *lsock = emalloc(sizeof(*lsock));
4425 LINK_SLIST(fd_list, lsock, link);
4426 maintain_activefds(fd, 0);
4431 close_and_delete_fd_from_list(
4437 UNLINK_EXPR_SLIST(lsock, fd_list, fd ==
4438 UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t);
4443 switch (lsock->type) {
4445 case FD_TYPE_SOCKET:
4446 closesocket(lsock->fd);
4450 closeserial(lsock->fd);
4455 "internal error - illegal descriptor type %d - EXITING",
4462 * remove from activefds
4464 maintain_activefds(fd, 1);
4477 if (find_addr_in_list(addr) == NULL) {
4479 /* not there yet - add to list */
4480 laddr = emalloc(sizeof(*laddr));
4481 laddr->addr = *addr;
4484 LINK_SLIST(remoteaddr_list, laddr, link);
4486 DPRINTF(4, ("Added addr %s to list of addresses\n",
4490 DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
4497 delete_addr_from_list(
4501 remaddr_t *unlinked;
4503 UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr,
4504 &(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t);
4506 if (unlinked != NULL) {
4507 DPRINTF(4, ("Deleted addr %s from list of addresses\n",
4515 delete_interface_from_list(
4519 remaddr_t *unlinked;
4522 UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface ==
4523 UNLINK_EXPR_SLIST_CURRENT()->ep, link,
4526 if (unlinked == NULL)
4528 DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
4529 stoa(&unlinked->addr), iface->ifnum,
4536 static struct interface *
4543 DPRINTF(4, ("Searching for addr %s in list of addresses - ",
4546 for (entry = remoteaddr_list;
4548 entry = entry->link)
4549 if (SOCK_EQ(&entry->addr, addr)) {
4550 DPRINTF(4, ("FOUND\n"));
4554 DPRINTF(4, ("NOT FOUND\n"));
4560 * Find the given address with the all given flags set in the list
4563 find_flagged_addr_in_list(
4570 DPRINTF(4, ("Finding addr %s with flags %d in list: ",
4571 stoa(addr), flags));
4573 for (entry = remoteaddr_list;
4575 entry = entry->link)
4577 if (SOCK_EQ(&entry->addr, addr)
4578 && (entry->ep->flags & flags) == flags) {
4580 DPRINTF(4, ("FOUND\n"));
4584 DPRINTF(4, ("NOT FOUND\n"));
4600 #ifdef HAS_ROUTING_SOCKET
4601 # ifndef UPDATE_GRACE
4602 # define UPDATE_GRACE 2 /* wait UPDATE_GRACE seconds before scanning */
4606 process_routing_msgs(struct asyncio_reader *reader)
4610 #ifdef HAVE_RTNETLINK
4611 struct nlmsghdr *nh;
4613 struct rt_msghdr rtm;
4617 if (disable_dynamic_updates) {
4619 * discard ourselves if we are not needed any more
4620 * usually happens when running unprivileged
4622 remove_asyncio_reader(reader);
4623 delete_asyncio_reader(reader);
4627 cnt = read(reader->fd, buffer, sizeof(buffer));
4630 if (errno == ENOBUFS) {
4632 "routing socket reports: %m");
4635 "routing socket reports: %m - disabling");
4636 remove_asyncio_reader(reader);
4637 delete_asyncio_reader(reader);
4643 * process routing message
4645 #ifdef HAVE_RTNETLINK
4646 for (nh = (struct nlmsghdr *)buffer;
4648 nh = NLMSG_NEXT(nh, cnt)) {
4649 msg_type = nh->nlmsg_type;
4652 (p + sizeof(struct rt_msghdr)) <= (buffer + cnt);
4653 p += rtm.rtm_msglen) {
4654 memcpy(&rtm, p, sizeof(rtm));
4655 if (rtm.rtm_version != RTM_VERSION) {
4657 "version mismatch (got %d - expected %d) on routing socket - disabling",
4658 rtm.rtm_version, RTM_VERSION);
4660 remove_asyncio_reader(reader);
4661 delete_asyncio_reader(reader);
4664 msg_type = rtm.rtm_type;
4691 #ifdef RTM_IFANNOUNCE
4692 case RTM_IFANNOUNCE:
4707 * we are keen on new and deleted addresses and
4708 * if an interface goes up and down or routing
4711 DPRINTF(3, ("routing message op = %d: scheduling interface update\n",
4713 timer_interfacetimeout(current_time + UPDATE_GRACE);
4715 #ifdef HAVE_RTNETLINK
4717 /* end of multipart message */
4722 * the rest doesn't bother us.
4724 DPRINTF(4, ("routing message op = %d: ignored\n",
4732 * set up routing notifications
4735 init_async_notifications()
4737 struct asyncio_reader *reader;
4738 #ifdef HAVE_RTNETLINK
4739 int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
4740 struct sockaddr_nl sa;
4742 int fd = socket(PF_ROUTE, SOCK_RAW, 0);
4746 "unable to open routing socket (%m) - using polled interface update");
4751 #ifdef HAVE_RTNETLINK
4753 sa.nl_family = PF_NETLINK;
4754 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR
4755 | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE
4756 | RTMGRP_IPV4_MROUTE | RTMGRP_IPV6_ROUTE
4757 | RTMGRP_IPV6_MROUTE;
4758 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
4760 "bind failed on routing socket (%m) - using polled interface update");
4764 make_socket_nonblocking(fd);
4765 #if defined(HAVE_SIGNALED_IO)
4766 init_socket_sig(fd);
4767 #endif /* HAVE_SIGNALED_IO */
4769 reader = new_asyncio_reader();
4772 reader->receiver = process_routing_msgs;
4774 add_asyncio_reader(reader, FD_TYPE_SOCKET);
4776 "Listening on routing socket on fd #%d for interface updates",
4780 /* HAS_ROUTING_SOCKET not defined */
4782 init_async_notifications(void)