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"
46 /* Don't include ISC's version of IPv6 variables and structures */
49 #include <isc/interfaceiter.h>
50 #include <isc/netaddr.h>
51 #include <isc/result.h>
52 #include <isc/sockaddr.h>
58 #ifdef HAS_ROUTING_SOCKET
59 # include <net/route.h>
60 # ifdef HAVE_RTNETLINK
61 # include <linux/rtnetlink.h>
65 #if defined(HAVE_SIGNALED_IO) && defined(DEBUG_TIMING)
70 * setsockopt does not always have the same arg declaration
71 * across all platforms. If it's not defined we make it empty
74 #ifndef SETSOCKOPT_ARG_CAST
75 #define SETSOCKOPT_ARG_CAST
78 extern int listen_to_virtual_ips;
81 #define IPTOS_DSCP_EF 0xb8
83 int qos = IPTOS_DSCP_EF; /* QoS RFC3246 */
86 /* TODO burnicki: This should be moved to ntp_timer.c, but if we do so
87 * we get a linker error. Since we're running out of time before the leap
88 * second occurs, we let it here where it just works.
96 typedef struct nic_rule_tag nic_rule;
100 nic_rule_action action;
101 nic_rule_match match_type;
108 * NIC rule listhead. Entries are added at the head so that the first
109 * match in the list is the last matching rule specified.
111 nic_rule *nic_rule_list;
114 #if defined(SO_BINTIME) && defined(SCM_BINTIME) && defined(CMSG_FIRSTHDR)
115 # define HAVE_PACKET_TIMESTAMP
116 # define HAVE_BINTIME
117 # ifdef BINTIME_CTLMSGBUF_SIZE
118 # define CMSG_BUFSIZE BINTIME_CTLMSGBUF_SIZE
120 # define CMSG_BUFSIZE 1536 /* moderate default */
122 #elif defined(SO_TIMESTAMPNS) && defined(SCM_TIMESTAMPNS) && defined(CMSG_FIRSTHDR)
123 # define HAVE_PACKET_TIMESTAMP
124 # define HAVE_TIMESTAMPNS
125 # ifdef TIMESTAMPNS_CTLMSGBUF_SIZE
126 # define CMSG_BUFSIZE TIMESTAMPNS_CTLMSGBUF_SIZE
128 # define CMSG_BUFSIZE 1536 /* moderate default */
130 #elif defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP) && defined(CMSG_FIRSTHDR)
131 # define HAVE_PACKET_TIMESTAMP
132 # define HAVE_TIMESTAMP
133 # ifdef TIMESTAMP_CTLMSGBUF_SIZE
134 # define CMSG_BUFSIZE TIMESTAMP_CTLMSGBUF_SIZE
136 # define CMSG_BUFSIZE 1536 /* moderate default */
139 /* fill in for old/other timestamp interfaces */
142 #if defined(SYS_WINNT)
143 #include "win32_io.h"
144 #include <isc/win32os.h>
148 * We do asynchronous input using the SIGIO facility. A number of
149 * recvbuf buffers are preallocated for input. In the signal
150 * handler we poll to see which sockets are ready and read the
151 * packets from them into the recvbuf's along with a time stamp and
152 * an indication of the source host and the interface it was received
153 * through. This allows us to get as accurate receive time stamps
154 * as possible independent of other processing going on.
156 * We watch the number of recvbufs available to the signal handler
157 * and allocate more when this number drops below the low water
158 * mark. If the signal handler should run out of buffers in the
159 * interim it will drop incoming frames, the idea being that it is
160 * better to drop a packet than to be inaccurate.
165 * Other statistics of possible interest
167 volatile u_long packets_dropped; /* total number of packets dropped on reception */
168 volatile u_long packets_ignored; /* packets received on wild card interface */
169 volatile u_long packets_received; /* total number of packets received */
170 u_long packets_sent; /* total number of packets sent */
171 u_long packets_notsent; /* total number of packets which couldn't be sent */
173 volatile u_long handler_calls; /* number of calls to interrupt handler */
174 volatile u_long handler_pkts; /* number of pkts received by handler */
175 u_long io_timereset; /* time counters were reset */
180 endpt * any_interface; /* wildcard ipv4 interface */
181 endpt * any6_interface; /* wildcard ipv6 interface */
182 endpt * loopback_interface; /* loopback ipv4 interface */
184 isc_boolean_t broadcast_client_enabled; /* is broadcast client enabled */
185 u_int sys_ifnum; /* next .ifnum to assign */
186 int ninterfaces; /* Total number of interfaces */
188 int disable_dynamic_updates; /* scan interfaces once only */
192 * Refclock stuff. We keep a chain of structures with data concerning
193 * the guys we are doing I/O for.
195 static struct refclockio *refio;
196 #endif /* REFCLOCK */
199 * File descriptor masks etc. for call to select
200 * Not needed for I/O Completion Ports or anything outside this file
202 static fd_set activefds;
203 static int maxactivefd;
206 * bit alternating value to detect verified interfaces during an update cycle
208 static u_short sys_interphase = 0;
210 static endpt * new_interface(endpt *);
211 static void add_interface(endpt *);
212 static int update_interfaces(u_short, interface_receiver_t,
214 static void remove_interface(endpt *);
215 static endpt * create_interface(u_short, endpt *);
217 static int is_wildcard_addr (const sockaddr_u *);
220 * Multicast functions
222 static isc_boolean_t addr_ismulticast (sockaddr_u *);
223 static isc_boolean_t is_anycast (sockaddr_u *,
227 * Not all platforms support multicast
230 static isc_boolean_t socket_multicast_enable (endpt *, sockaddr_u *);
231 static isc_boolean_t socket_multicast_disable(endpt *, sockaddr_u *);
235 static void interface_dump (const endpt *);
236 static void sockaddr_dump (const sockaddr_u *);
237 static void print_interface (const endpt *, const char *, const char *);
238 #define DPRINT_INTERFACE(level, args) do { if (debug >= (level)) { print_interface args; } } while (0)
240 #define DPRINT_INTERFACE(level, args) do {} while (0)
243 typedef struct vsock vsock_t;
244 enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE };
254 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
256 * async notification processing (e. g. routing sockets)
259 * support for receiving data on fd that is not a refclock or a socket
260 * like e. g. routing sockets
262 struct asyncio_reader {
263 struct asyncio_reader *link; /* the list this is being kept in */
264 SOCKET fd; /* fd to be read */
265 void *data; /* possibly local data */
266 void (*receiver)(struct asyncio_reader *); /* input handler */
269 struct asyncio_reader *asyncio_reader_list;
271 static void delete_asyncio_reader (struct asyncio_reader *);
272 static struct asyncio_reader *new_asyncio_reader (void);
273 static void add_asyncio_reader (struct asyncio_reader *, enum desc_type);
274 static void remove_asyncio_reader (struct asyncio_reader *);
276 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
278 static void init_async_notifications (void);
280 static int addr_eqprefix (const sockaddr_u *, const sockaddr_u *,
282 static int addr_samesubnet (const sockaddr_u *, const sockaddr_u *,
283 const sockaddr_u *, const sockaddr_u *);
284 static int create_sockets (u_short);
285 static SOCKET open_socket (sockaddr_u *, int, int, endpt *);
286 static void set_reuseaddr (int);
287 static isc_boolean_t socket_broadcast_enable (struct interface *, SOCKET, sockaddr_u *);
289 #if !defined(HAVE_IO_COMPLETION_PORT) && !defined(HAVE_SIGNALED_IO)
290 static char * fdbits (int, const fd_set *);
292 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
293 static isc_boolean_t socket_broadcast_disable (struct interface *, sockaddr_u *);
296 typedef struct remaddr remaddr_t;
304 remaddr_t * remoteaddr_list;
305 endpt * ep_list; /* complete endpt list */
306 endpt * mc4_list; /* IPv4 mcast-capable unicast endpts */
307 endpt * mc6_list; /* IPv6 mcast-capable unicast endpts */
309 static endpt * wildipv4;
310 static endpt * wildipv6;
313 int accept_wildcard_if_for_winnt;
315 const int accept_wildcard_if_for_winnt = FALSE;
318 static void add_fd_to_list (SOCKET, enum desc_type);
319 static endpt * find_addr_in_list (sockaddr_u *);
320 static endpt * find_flagged_addr_in_list(sockaddr_u *, u_int32);
321 static void delete_addr_from_list (sockaddr_u *);
322 static void delete_interface_from_list(endpt *);
323 static void close_and_delete_fd_from_list(SOCKET);
324 static void add_addr_to_list (sockaddr_u *, endpt *);
325 static void create_wildcards (u_short);
326 static endpt * findlocalinterface (sockaddr_u *, int, int);
327 static endpt * findclosestinterface (sockaddr_u *, int);
329 static const char * action_text (nic_rule_action);
331 static nic_rule_action interface_action(char *, sockaddr_u *, u_int32);
332 static void convert_isc_if (isc_interface_t *,
334 static void calc_addr_distance(sockaddr_u *,
337 static int cmp_addr_distance(const sockaddr_u *,
341 * Routines to read the ntp packets
343 #if !defined(HAVE_IO_COMPLETION_PORT)
344 static inline int read_network_packet (SOCKET, struct interface *, l_fp);
345 static void ntpd_addremove_io_fd (int, int, int);
346 static void input_handler_scan (const l_fp*, const fd_set*);
347 static int/*BOOL*/ sanitize_fdset (int errc);
349 static inline int read_refclock_packet (SOCKET, struct refclockio *, l_fp);
351 #ifdef HAVE_SIGNALED_IO
352 static void input_handler (l_fp*);
357 #ifndef HAVE_IO_COMPLETION_PORT
366 if (fd < 0 || fd >= FD_SETSIZE) {
368 "Too many sockets in use, FD_SETSIZE %d exceeded by fd %d",
374 FD_SET(fd, &activefds);
375 maxactivefd = max(fd, maxactivefd);
377 FD_CLR(fd, &activefds);
378 if (maxactivefd && fd == maxactivefd) {
379 for (i = maxactivefd - 1; i >= 0; i--)
380 if (FD_ISSET(i, &activefds)) {
384 INSIST(fd != maxactivefd);
388 #endif /* !HAVE_IO_COMPLETION_PORT */
393 * collect timing information for various processing
394 * paths. currently we only pass them on to the file
395 * for later processing. this could also do histogram
396 * based analysis in other to reduce the load (and skew)
397 * dur to the file output
400 collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
404 snprintf(buf, sizeof(buf), "%s %d %s %s",
406 ? ((rb->dstadr != NULL)
407 ? stoa(&rb->recv_srcadr)
410 count, lfptoa(dts, 9), tag);
411 record_timing_stats(buf);
416 * About dynamic interfaces, sockets, reception and more...
418 * the code solves following tasks:
420 * - keep a current list of active interfaces in order
421 * to bind to to the interface address on NTP_PORT so that
422 * all wild and specific bindings for NTP_PORT are taken by ntpd
423 * to avoid other daemons messing with the time or sockets.
424 * - all interfaces keep a list of peers that are referencing
425 * the interface in order to quickly re-assign the peers to
426 * new interface in case an interface is deleted (=> gone from system or
428 * - have a preconfigured socket ready with the right local address
429 * for transmission and reception
430 * - have an address list for all destination addresses used within ntpd
431 * to find the "right" preconfigured socket.
432 * - facilitate updating the internal interface list with respect to
433 * the current kernel state
437 * - mapping of multicast addresses to the interface affected is not always
438 * one to one - especially on hosts with multiple interfaces
439 * the code here currently allocates a separate interface entry for those
440 * multicast addresses
441 * iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF)
442 * in case of failure the multicast address is bound to an existing interface.
443 * - on some systems it is perfectly legal to assign the same address to
444 * multiple interfaces. Therefore this code does not keep a list of interfaces
445 * but a list of interfaces that represent a unique address as determined by the kernel
446 * by the procedure in findlocalinterface. Thus it is perfectly legal to see only
447 * one representative of a group of real interfaces if they share the same address.
449 * Frank Kardel 20050910
453 * init_io - initialize I/O module.
458 /* Init buffer free list and stat counters */
459 init_recvbuff(RECV_INIT);
460 /* update interface every 5 minutes as default */
461 interface_interval = 300;
464 addremove_io_fd = &ntpd_addremove_io_fd;
467 #if defined(SYS_WINNT)
468 init_io_completion_port();
469 #elif defined(HAVE_SIGNALED_IO)
470 (void) set_signal(input_handler);
476 ntpd_addremove_io_fd(
484 #ifdef HAVE_SIGNALED_IO
487 #endif /* not HAVE_SIGNALED_IO */
489 maintain_activefds(fd, remove_it);
494 * io_open_sockets - call socket creation routine
497 io_open_sockets(void)
499 static int already_opened;
501 if (already_opened || HAVE_OPT( SAVECONFIGQUIT ))
510 create_sockets(NTP_PORT);
513 init_async_notifications();
515 DPRINTF(3, ("io_open_sockets: maxactivefd %d\n", maxactivefd));
521 * function to dump the contents of the interface structure
522 * for debugging use only.
525 interface_dump(const endpt *itf)
527 printf("Dumping interface: %p\n", itf);
528 printf("fd = %d\n", itf->fd);
529 printf("bfd = %d\n", itf->bfd);
530 printf("sin = %s,\n", stoa(&itf->sin));
531 sockaddr_dump(&itf->sin);
532 printf("bcast = %s,\n", stoa(&itf->bcast));
533 sockaddr_dump(&itf->bcast);
534 printf("mask = %s,\n", stoa(&itf->mask));
535 sockaddr_dump(&itf->mask);
536 printf("name = %s\n", itf->name);
537 printf("flags = 0x%08x\n", itf->flags);
538 printf("last_ttl = %d\n", itf->last_ttl);
539 printf("addr_refid = %08x\n", itf->addr_refid);
540 printf("num_mcast = %d\n", itf->num_mcast);
541 printf("received = %ld\n", itf->received);
542 printf("sent = %ld\n", itf->sent);
543 printf("notsent = %ld\n", itf->notsent);
544 printf("ifindex = %u\n", itf->ifindex);
545 printf("peercnt = %u\n", itf->peercnt);
546 printf("phase = %u\n", itf->phase);
550 * sockaddr_dump - hex dump the start of a sockaddr_u
553 sockaddr_dump(const sockaddr_u *psau)
555 /* Limit the size of the sockaddr_in6 hex dump */
556 const int maxsize = min(32, sizeof(psau->sa6));
560 /* XXX: Should we limit maxsize based on psau->saX.sin_family? */
561 cp = (const void *)&psau->sa6;
563 for(i = 0; i < maxsize; i++) {
564 printf("%02x", *cp++);
572 * print_interface - helper to output debug information
575 print_interface(const endpt *iface, const char *pfx, const char *sfx)
577 printf("%sinterface #%d: fd=%d, bfd=%d, name=%s, flags=0x%x, ifindex=%u, sin=%s",
586 if (AF_INET == iface->family) {
587 if (iface->flags & INT_BROADCAST)
588 printf(", bcast=%s", stoa(&iface->bcast));
589 printf(", mask=%s", stoa(&iface->mask));
592 (iface->ignore_packets)
596 if (debug > 4) /* in-depth debugging only */
597 interface_dump(iface);
601 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
603 * create an asyncio_reader structure
605 static struct asyncio_reader *
606 new_asyncio_reader(void)
608 struct asyncio_reader *reader;
610 reader = emalloc_zero(sizeof(*reader));
611 reader->fd = INVALID_SOCKET;
620 delete_asyncio_reader(
621 struct asyncio_reader *reader
628 * add asynchio_reader
632 struct asyncio_reader * reader,
635 LINK_SLIST(asyncio_reader_list, reader, link);
636 add_fd_to_list(reader->fd, type);
640 * remove asynchio_reader
643 remove_asyncio_reader(
644 struct asyncio_reader *reader
647 struct asyncio_reader *unlinked;
649 UNLINK_SLIST(unlinked, asyncio_reader_list, reader, link,
650 struct asyncio_reader);
652 if (reader->fd != INVALID_SOCKET)
653 close_and_delete_fd_from_list(reader->fd);
655 reader->fd = INVALID_SOCKET;
657 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
660 /* compare two sockaddr prefixes */
663 const sockaddr_u * a,
664 const sockaddr_u * b,
670 isc_sockaddr_t isc_sa;
673 memcpy(&isc_sa.type, a, min(sizeof(isc_sa.type), sizeof(*a)));
674 isc_netaddr_fromsockaddr(&isc_a, &isc_sa);
677 memcpy(&isc_sa.type, b, min(sizeof(isc_sa.type), sizeof(*b)));
678 isc_netaddr_fromsockaddr(&isc_b, &isc_sa);
680 return (int)isc_netaddr_eqprefix(&isc_a, &isc_b,
687 const sockaddr_u * a,
688 const sockaddr_u * a_mask,
689 const sockaddr_u * b,
690 const sockaddr_u * b_mask
694 const u_int32 * pa_limit;
699 REQUIRE(AF(a) == AF(a_mask));
700 REQUIRE(AF(b) == AF(b_mask));
702 * With address and mask families verified to match, comparing
703 * the masks also validates the address's families match.
705 if (!SOCK_EQ(a_mask, b_mask))
709 loops = sizeof(NSRCADR6(a)) / sizeof(*pa);
710 pa = (const void *)&NSRCADR6(a);
711 pb = (const void *)&NSRCADR6(b);
712 pm = (const void *)&NSRCADR6(a_mask);
714 loops = sizeof(NSRCADR(a)) / sizeof(*pa);
715 pa = (const void *)&NSRCADR(a);
716 pb = (const void *)&NSRCADR(b);
717 pm = (const void *)&NSRCADR(a_mask);
719 for (pa_limit = pa + loops; pa < pa_limit; pa++, pb++, pm++)
720 if ((*pa & *pm) != (*pb & *pm))
728 * interface list enumerator - visitor pattern
732 interface_receiver_t receiver,
736 interface_info_t ifi;
738 ifi.action = IFS_EXISTS;
739 for (ifi.ep = ep_list; ifi.ep != NULL; ifi.ep = ifi.ep->elink)
740 (*receiver)(data, &ifi);
744 * do standard initialization of interface structure
752 ep->fd = INVALID_SOCKET;
753 ep->bfd = INVALID_SOCKET;
754 ep->phase = sys_interphase;
759 * create new interface structure initialize from
760 * template structure or via standard initialization
763 static struct interface *
765 struct interface *interface
768 struct interface * iface;
770 iface = emalloc(sizeof(*iface));
772 if (NULL == interface)
773 init_interface(iface);
774 else /* use the template */
775 memcpy(iface, interface, sizeof(*iface));
777 /* count every new instance of an interface in the system */
778 iface->ifnum = sys_ifnum++;
779 iface->starttime = current_time;
786 * return interface storage into free memory pool
798 * link interface into list of known interfaces
805 endpt ** pmclisthead;
813 int ep_univ_iid; /* iface ID from MAC address */
814 int scan_univ_iid; /* see RFC 4291 */
815 int ep_privacy; /* random local iface ID */
816 int scan_privacy; /* see RFC 4941 */
819 /* Calculate the refid */
820 ep->addr_refid = addr2refid(&ep->sin);
821 /* link at tail so ntpdc -c ifstats index increases each row */
822 LINK_TAIL_SLIST(ep_list, ep, elink, endpt);
825 /* the rest is for enabled multicast-capable addresses only */
826 if (ep->ignore_packets || !(INT_MULTICAST & ep->flags) ||
827 INT_LOOPBACK & ep->flags)
829 # ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
830 if (AF_INET6 == ep->family)
833 pmclisthead = (AF_INET == ep->family)
837 if (AF_INET6 == ep->family) {
839 IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&ep->sin)) ||
840 IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(&ep->sin));
841 ep_univ_iid = IS_IID_UNIV(&ep->sin);
842 ep_privacy = !!(INT_PRIVACY & ep->flags);
848 DPRINTF(4, ("add_interface mcast-capable %s%s%s%s\n",
850 (ep_local) ? " link/scope-local" : "",
851 (ep_univ_iid) ? " univ-IID" : "",
852 (ep_privacy) ? " privacy" : ""));
854 * If we have multiple local addresses on the same network
855 * interface, and some are link- or site-local, do not multicast
856 * out from the link-/site-local addresses by default, to avoid
857 * duplicate manycastclient associations between v6 peers using
858 * link-local and global addresses. link-local can still be
859 * chosen using "nic ignore myv6globalprefix::/64".
860 * Similarly, if we have multiple global addresses from the same
861 * prefix on the same network interface, multicast from one,
862 * preferring EUI-64, then static, then least RFC 4941 privacy
865 for (scan = *pmclisthead; scan != NULL; scan = scan_next) {
866 scan_next = scan->mclink;
867 if (ep->family != scan->family)
869 if (strcmp(ep->name, scan->name))
871 same_subnet = addr_samesubnet(&ep->sin, &ep->mask,
872 &scan->sin, &scan->mask);
873 if (AF_INET6 == ep->family) {
876 IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(addr)) ||
877 IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(addr));
878 scan_univ_iid = IS_IID_UNIV(addr);
879 scan_privacy = !!(INT_PRIVACY & scan->flags);
882 scan_univ_iid = FALSE;
883 scan_privacy = FALSE;
885 DPRINTF(4, ("add_interface mcast-capable scan %s%s%s%s\n",
887 (scan_local) ? " link/scope-local" : "",
888 (scan_univ_iid) ? " univ-IID" : "",
889 (scan_privacy) ? " privacy" : ""));
890 if ((ep_local && !scan_local) || (same_subnet &&
891 ((ep_privacy && !scan_privacy) ||
892 (!ep_univ_iid && scan_univ_iid)))) {
893 DPRINTF(4, ("did not add %s to %s of IPv6 multicast-capable list which already has %s\n",
901 if ((scan_local && !ep_local) || (same_subnet &&
902 ((scan_privacy && !ep_privacy) ||
903 (!scan_univ_iid && ep_univ_iid)))) {
904 UNLINK_SLIST(unlinked, *pmclisthead,
905 scan, mclink, endpt);
906 DPRINTF(4, ("%s %s from IPv6 multicast-capable list to add %s\n",
910 stoa(&scan->sin), stoa(&ep->sin)));
914 * Add link/site local at the tail of the multicast-
915 * capable unicast interfaces list, so that ntpd will
916 * send from global addresses before link-/site-local
920 LINK_TAIL_SLIST(*pmclisthead, ep, mclink, endpt);
922 LINK_SLIST(*pmclisthead, ep, mclink);
923 DPRINTF(4, ("added %s to %s of IPv%s multicast-capable unicast local address list\n",
928 (AF_INET == ep->family)
932 if (INVALID_SOCKET == ep->fd)
936 * select the local address from which to send to multicast.
938 switch (AF(&ep->sin)) {
941 rc = setsockopt(ep->fd, IPPROTO_IP,
943 (void *)&NSRCADR(&ep->sin),
944 sizeof(NSRCADR(&ep->sin)));
947 "setsockopt IP_MULTICAST_IF %s fails: %m",
951 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
953 rc = setsockopt(ep->fd, IPPROTO_IPV6,
955 (void *)&ep->ifindex,
956 sizeof(ep->ifindex));
957 /* do not complain if bound addr scope is ifindex */
958 if (rc && ep->ifindex != SCOPE(&ep->sin))
960 "setsockopt IPV6_MULTICAST_IF %u for %s fails: %m",
961 ep->ifindex, stoa(&ep->sin));
970 * remove interface from known interface list and clean up
971 * associated resources
979 endpt ** pmclisthead;
982 UNLINK_SLIST(unlinked, ep_list, ep, elink, endpt);
983 if (!ep->ignore_packets && INT_MULTICAST & ep->flags) {
984 pmclisthead = (AF_INET == ep->family)
987 UNLINK_SLIST(unlinked, *pmclisthead, ep, mclink, endpt);
988 DPRINTF(4, ("%s %s IPv%s multicast-capable unicast local address list\n",
993 (AF_INET == ep->family)
997 delete_interface_from_list(ep);
999 if (ep->fd != INVALID_SOCKET) {
1001 "Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
1009 current_time - ep->starttime);
1010 close_and_delete_fd_from_list(ep->fd);
1011 ep->fd = INVALID_SOCKET;
1014 if (ep->bfd != INVALID_SOCKET) {
1016 "stop listening for broadcasts to %s on interface #%d %s",
1017 stoa(&ep->bcast), ep->ifnum, ep->name);
1018 close_and_delete_fd_from_list(ep->bfd);
1019 ep->bfd = INVALID_SOCKET;
1020 ep->flags &= ~INT_BCASTOPEN;
1024 mon_clearinterface(ep);
1026 /* remove restrict interface entry */
1027 SET_HOSTMASK(&resmask, AF(&ep->sin));
1028 hack_restrict(RESTRICT_REMOVEIF, &ep->sin, &resmask,
1029 RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
1038 msyslog(LOG_INFO, "%s on %d %s %s",
1039 (ep->ignore_packets)
1041 : "Listen normally",
1054 #ifdef INCLUDE_IPV6_SUPPORT
1057 sockaddr_u wildaddr;
1058 nic_rule_action action;
1059 struct interface * wildif;
1062 * silence "potentially uninitialized" warnings from VC9
1063 * failing to follow the logic. Ideally action could remain
1064 * uninitialized, and the memset be the first statement under
1065 * the first if (v4wild).
1067 action = ACTION_LISTEN;
1070 #ifdef INCLUDE_IPV6_SUPPORT
1072 * create pseudo-interface with wildcard IPv6 address
1074 v6wild = ipv6_works;
1076 /* set wildaddr to the v6 wildcard address :: */
1078 AF(&wildaddr) = AF_INET6;
1079 SET_ADDR6N(&wildaddr, in6addr_any);
1080 SET_PORT(&wildaddr, port);
1081 SET_SCOPE(&wildaddr, 0);
1083 /* check for interface/nic rules affecting the wildcard */
1084 action = interface_action(NULL, &wildaddr, 0);
1085 v6wild = (ACTION_IGNORE != action);
1088 wildif = new_interface(NULL);
1090 strlcpy(wildif->name, "v6wildcard", sizeof(wildif->name));
1091 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1092 wildif->family = AF_INET6;
1093 AF(&wildif->mask) = AF_INET6;
1094 SET_ONESMASK(&wildif->mask);
1096 wildif->flags = INT_UP | INT_WILDCARD;
1097 wildif->ignore_packets = (ACTION_DROP == action);
1099 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1101 if (wildif->fd != INVALID_SOCKET) {
1103 any6_interface = wildif;
1104 add_addr_to_list(&wildif->sin, wildif);
1105 add_interface(wildif);
1106 log_listen_address(wildif);
1109 "unable to bind to wildcard address %s - another process may be running - EXITING",
1110 stoa(&wildif->sin));
1113 DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1118 * create pseudo-interface with wildcard IPv4 address
1120 v4wild = ipv4_works;
1122 /* set wildaddr to the v4 wildcard address 0.0.0.0 */
1123 AF(&wildaddr) = AF_INET;
1124 SET_ADDR4N(&wildaddr, INADDR_ANY);
1125 SET_PORT(&wildaddr, port);
1127 /* check for interface/nic rules affecting the wildcard */
1128 action = interface_action(NULL, &wildaddr, 0);
1129 v4wild = (ACTION_IGNORE != action);
1132 wildif = new_interface(NULL);
1134 strlcpy(wildif->name, "v4wildcard", sizeof(wildif->name));
1135 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1136 wildif->family = AF_INET;
1137 AF(&wildif->mask) = AF_INET;
1138 SET_ONESMASK(&wildif->mask);
1140 wildif->flags = INT_BROADCAST | INT_UP | INT_WILDCARD;
1141 wildif->ignore_packets = (ACTION_DROP == action);
1144 * enable multicast reception on the broadcast socket
1146 AF(&wildif->bcast) = AF_INET;
1147 SET_ADDR4N(&wildif->bcast, INADDR_ANY);
1148 SET_PORT(&wildif->bcast, port);
1150 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1152 if (wildif->fd != INVALID_SOCKET) {
1154 any_interface = wildif;
1156 add_addr_to_list(&wildif->sin, wildif);
1157 add_interface(wildif);
1158 log_listen_address(wildif);
1161 "unable to bind to wildcard address %s - another process may be running - EXITING",
1162 stoa(&wildif->sin));
1165 DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1171 * add_nic_rule() -- insert a rule entry at the head of nic_rule_list.
1175 nic_rule_match match_type,
1176 const char * if_name, /* interface name or numeric address */
1178 nic_rule_action action
1182 isc_boolean_t is_ip;
1184 rule = emalloc_zero(sizeof(*rule));
1185 rule->match_type = match_type;
1186 rule->prefixlen = prefixlen;
1187 rule->action = action;
1189 if (MATCH_IFNAME == match_type) {
1190 REQUIRE(NULL != if_name);
1191 rule->if_name = estrdup(if_name);
1192 } else if (MATCH_IFADDR == match_type) {
1193 REQUIRE(NULL != if_name);
1194 /* set rule->addr */
1195 is_ip = is_ip_address(if_name, AF_UNSPEC, &rule->addr);
1198 REQUIRE(NULL == if_name);
1200 LINK_SLIST(nic_rule_list, rule, next);
1207 nic_rule_action action
1215 t = "ERROR"; /* quiet uninit warning */
1216 DPRINTF(1, ("fatal: unknown nic_rule_action %d\n",
1239 static nic_rule_action
1242 sockaddr_u * if_addr,
1250 DPRINTF(4, ("interface_action: interface %s ",
1251 (if_name != NULL) ? if_name : "wildcard"));
1253 iswildcard = is_wildcard_addr(if_addr);
1254 isloopback = !!(INT_LOOPBACK & if_flags);
1257 * Find any matching NIC rule from --interface / -I or ntp.conf
1258 * interface/nic rules.
1260 for (rule = nic_rule_list; rule != NULL; rule = rule->next) {
1262 switch (rule->match_type) {
1265 /* loopback and wildcard excluded from "all" */
1266 if (isloopback || iswildcard)
1268 DPRINTF(4, ("nic all %s\n",
1269 action_text(rule->action)));
1270 return rule->action;
1273 if (IS_IPV4(if_addr)) {
1274 DPRINTF(4, ("nic ipv4 %s\n",
1275 action_text(rule->action)));
1276 return rule->action;
1281 if (IS_IPV6(if_addr)) {
1282 DPRINTF(4, ("nic ipv6 %s\n",
1283 action_text(rule->action)));
1284 return rule->action;
1288 case MATCH_WILDCARD:
1290 DPRINTF(4, ("nic wildcard %s\n",
1291 action_text(rule->action)));
1292 return rule->action;
1297 if (rule->prefixlen != -1) {
1298 if (addr_eqprefix(if_addr, &rule->addr,
1301 DPRINTF(4, ("subnet address match - %s\n",
1302 action_text(rule->action)));
1303 return rule->action;
1306 if (SOCK_EQ(if_addr, &rule->addr)) {
1308 DPRINTF(4, ("address match - %s\n",
1309 action_text(rule->action)));
1310 return rule->action;
1316 #if defined(HAVE_FNMATCH) && defined(FNM_CASEFOLD)
1317 && !fnmatch(rule->if_name, if_name, FNM_CASEFOLD)
1319 && !strcasecmp(if_name, rule->if_name)
1323 DPRINTF(4, ("interface name match - %s\n",
1324 action_text(rule->action)));
1325 return rule->action;
1332 * Unless explicitly disabled such as with "nic ignore ::1"
1333 * listen on loopback addresses. Since ntpq and ntpdc query
1334 * "localhost" by default, which typically resolves to ::1 and
1335 * 127.0.0.1, it's useful to default to listening on both.
1338 DPRINTF(4, ("default loopback listen\n"));
1339 return ACTION_LISTEN;
1343 * Treat wildcard addresses specially. If there is no explicit
1344 * "nic ... wildcard" or "nic ... 0.0.0.0" or "nic ... ::" rule
1348 DPRINTF(4, ("default wildcard drop\n"));
1353 * Check for "virtual IP" (colon in the interface name) after
1354 * the rules so that "ntpd --interface eth0:1 -novirtualips"
1355 * does indeed listen on eth0:1's addresses.
1357 if (!listen_to_virtual_ips && if_name != NULL
1358 && (strchr(if_name, ':') != NULL)) {
1360 DPRINTF(4, ("virtual ip - ignore\n"));
1361 return ACTION_IGNORE;
1365 * If there are no --interface/-I command-line options and no
1366 * interface/nic rules in ntp.conf, the default action is to
1367 * listen. In the presence of rules from either, the default
1368 * is to ignore. This implements ntpd's traditional listen-
1369 * every default with no interface listen configuration, and
1370 * ensures a single -I eth0 or "nic listen eth0" means do not
1371 * listen on any other addresses.
1373 if (NULL == nic_rule_list) {
1374 DPRINTF(4, ("default listen\n"));
1375 return ACTION_LISTEN;
1378 DPRINTF(4, ("implicit ignore\n"));
1379 return ACTION_IGNORE;
1385 isc_interface_t *isc_if,
1390 const u_char v6loop[16] = {0, 0, 0, 0, 0, 0, 0, 0,
1391 0, 0, 0, 0, 0, 0, 0, 1};
1393 strlcpy(itf->name, isc_if->name, sizeof(itf->name));
1394 itf->ifindex = isc_if->ifindex;
1395 itf->family = (u_short)isc_if->af;
1396 AF(&itf->sin) = itf->family;
1397 AF(&itf->mask) = itf->family;
1398 AF(&itf->bcast) = itf->family;
1399 SET_PORT(&itf->sin, port);
1400 SET_PORT(&itf->mask, port);
1401 SET_PORT(&itf->bcast, port);
1403 if (IS_IPV4(&itf->sin)) {
1404 NSRCADR(&itf->sin) = isc_if->address.type.in.s_addr;
1405 NSRCADR(&itf->mask) = isc_if->netmask.type.in.s_addr;
1407 if (isc_if->flags & INTERFACE_F_BROADCAST) {
1408 itf->flags |= INT_BROADCAST;
1409 NSRCADR(&itf->bcast) =
1410 isc_if->broadcast.type.in.s_addr;
1413 #ifdef INCLUDE_IPV6_SUPPORT
1414 else if (IS_IPV6(&itf->sin)) {
1415 SET_ADDR6N(&itf->sin, isc_if->address.type.in6);
1416 SET_ADDR6N(&itf->mask, isc_if->netmask.type.in6);
1418 SET_SCOPE(&itf->sin, isc_if->address.zone);
1420 #endif /* INCLUDE_IPV6_SUPPORT */
1423 /* Process the rest of the flags */
1426 ((INTERFACE_F_UP & isc_if->flags)
1428 | ((INTERFACE_F_LOOPBACK & isc_if->flags)
1430 | ((INTERFACE_F_POINTTOPOINT & isc_if->flags)
1432 | ((INTERFACE_F_MULTICAST & isc_if->flags)
1433 ? INT_MULTICAST : 0)
1434 | ((INTERFACE_F_PRIVACY & isc_if->flags)
1439 * Clear the loopback flag if the address is not localhost.
1440 * http://bugs.ntp.org/1683
1442 if (INT_LOOPBACK & itf->flags) {
1443 if (AF_INET == itf->family) {
1444 if (127 != (SRCADR(&itf->sin) >> 24))
1445 itf->flags &= ~INT_LOOPBACK;
1447 if (memcmp(v6loop, NSRCADR6(&itf->sin),
1448 sizeof(NSRCADR6(&itf->sin))))
1449 itf->flags &= ~INT_LOOPBACK;
1458 * some OSes have been observed to keep
1459 * cached routes even when more specific routes
1461 * this can be mitigated by re-binding
1466 struct interface * interface
1469 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
1470 if (interface->fd != INVALID_SOCKET) {
1471 int bcast = (interface->flags & INT_BCASTXMIT) != 0;
1472 /* as we forcibly close() the socket remove the
1473 broadcast permission indication */
1475 socket_broadcast_disable(interface, &interface->sin);
1477 close_and_delete_fd_from_list(interface->fd);
1479 /* create new socket picking up a new first hop binding
1480 at connect() time */
1481 interface->fd = open_socket(&interface->sin,
1482 bcast, 0, interface);
1484 * reset TTL indication so TTL is is set again
1487 interface->last_ttl = 0;
1488 return (interface->fd != INVALID_SOCKET);
1490 return 0; /* invalid sockets are not refreshable */
1491 #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1492 return (interface->fd != INVALID_SOCKET);
1493 #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1497 * interface_update - externally callable update function
1501 interface_receiver_t receiver,
1504 int new_interface_found;
1506 if (disable_dynamic_updates)
1510 new_interface_found = update_interfaces(NTP_PORT, receiver, data);
1513 if (!new_interface_found)
1517 msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
1519 interrupt_worker_sleep();
1524 * sau_from_netaddr() - convert network address on-wire formats.
1525 * Convert from libisc's isc_netaddr_t to NTP's sockaddr_u
1530 const isc_netaddr_t *pna
1534 AF(psau) = (u_short)pna->family;
1535 switch (pna->family) {
1538 memcpy(&psau->sa4.sin_addr, &pna->type.in,
1539 sizeof(psau->sa4.sin_addr));
1543 memcpy(&psau->sa6.sin6_addr, &pna->type.in6,
1544 sizeof(psau->sa6.sin6_addr));
1552 const sockaddr_u *psau
1555 if (IS_IPV4(psau) && !NSRCADR(psau))
1558 #ifdef INCLUDE_IPV6_SUPPORT
1559 if (IS_IPV6(psau) && S_ADDR6_EQ(psau, &in6addr_any))
1567 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
1569 * enable/disable re-use of wildcard address socket
1577 struct interface *any;
1578 SOCKET fd = INVALID_SOCKET;
1580 any = ANY_INTERFACE_BYFAM(family);
1584 if (fd != INVALID_SOCKET) {
1585 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1586 (char *)&on, sizeof(on)))
1588 "set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m",
1591 DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n",
1596 #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
1599 static isc_boolean_t
1606 #if defined(INCLUDE_IPV6_SUPPORT) && defined(SIOCGIFAFLAG_IN6)
1607 struct in6_ifreq ifr6;
1610 if (psau->sa.sa_family != AF_INET6)
1612 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1615 memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr));
1616 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1617 if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1622 if ((ifr6.ifr_ifru.ifru_flags6 & flags6) != 0)
1624 #endif /* INCLUDE_IPV6_SUPPORT && SIOCGIFAFLAG_IN6 */
1628 static isc_boolean_t
1634 #ifdef IN6_IFF_ANYCAST
1635 return check_flags6(psau, name, IN6_IFF_ANYCAST);
1641 static isc_boolean_t
1650 #ifdef IN6_IFF_DEPARTED
1651 flags6 |= IN6_IFF_DEPARTED;
1653 #ifdef IN6_IFF_DETACHED
1654 flags6 |= IN6_IFF_DETACHED;
1656 #ifdef IN6_IFF_TENTATIVE
1657 flags6 |= IN6_IFF_TENTATIVE;
1659 return check_flags6(psau, name, flags6) ? ISC_FALSE : ISC_TRUE;
1663 * update_interface strategy
1665 * toggle configuration phase
1668 * forall currently existing interfaces
1669 * if address is known:
1670 * drop socket - rebind again
1672 * if address is NOT known:
1673 * attempt to create a new interface entry
1676 * forall currently known non MCAST and WILDCARD interfaces
1677 * if interface does not match configuration phase (not seen in phase 1):
1678 * remove interface from known interface list
1679 * forall peers associated with this interface
1680 * disconnect peer from this interface
1683 * attempt to re-assign interfaces to peers
1690 interface_receiver_t receiver,
1694 isc_mem_t * mctx = (void *)-1;
1695 interface_info_t ifi;
1696 isc_interfaceiter_t * iter;
1697 isc_result_t result;
1698 isc_interface_t isc_if;
1699 int new_interface_found;
1700 unsigned int family;
1705 DPRINTF(3, ("update_interfaces(%d)\n", port));
1708 * phase one - scan interfaces
1709 * - create those that are not found
1710 * - update those that are found
1713 new_interface_found = FALSE;
1715 result = isc_interfaceiter_create(mctx, &iter);
1717 if (result != ISC_R_SUCCESS)
1721 * Toggle system interface scan phase to find untouched
1722 * interfaces to be deleted.
1724 sys_interphase ^= 0x1;
1726 for (result = isc_interfaceiter_first(iter);
1727 ISC_R_SUCCESS == result;
1728 result = isc_interfaceiter_next(iter)) {
1730 result = isc_interfaceiter_current(iter, &isc_if);
1732 if (result != ISC_R_SUCCESS)
1735 /* See if we have a valid family to use */
1736 family = isc_if.address.family;
1737 if (AF_INET != family && AF_INET6 != family)
1739 if (AF_INET == family && !ipv4_works)
1741 if (AF_INET6 == family && !ipv6_works)
1744 /* create prototype */
1745 init_interface(&enumep);
1747 convert_isc_if(&isc_if, &enumep, port);
1749 DPRINT_INTERFACE(4, (&enumep, "examining ", "\n"));
1752 * Check if and how we are going to use the interface.
1754 switch (interface_action(enumep.name, &enumep.sin,
1758 DPRINTF(4, ("ignoring interface %s (%s) - by nic rules\n",
1759 enumep.name, stoa(&enumep.sin)));
1763 DPRINTF(4, ("listen interface %s (%s) - by nic rules\n",
1764 enumep.name, stoa(&enumep.sin)));
1765 enumep.ignore_packets = ISC_FALSE;
1769 DPRINTF(4, ("drop on interface %s (%s) - by nic rules\n",
1770 enumep.name, stoa(&enumep.sin)));
1771 enumep.ignore_packets = ISC_TRUE;
1775 /* interfaces must be UP to be usable */
1776 if (!(enumep.flags & INT_UP)) {
1777 DPRINTF(4, ("skipping interface %s (%s) - DOWN\n",
1778 enumep.name, stoa(&enumep.sin)));
1783 * skip any interfaces UP and bound to a wildcard
1784 * address - some dhcp clients produce that in the
1787 if (is_wildcard_addr(&enumep.sin))
1790 if (is_anycast(&enumep.sin, isc_if.name))
1794 * skip any address that is an invalid state to be used
1796 if (!is_valid(&enumep.sin, isc_if.name))
1800 * map to local *address* in order to map all duplicate
1801 * interfaces to an endpt structure with the appropriate
1802 * socket. Our name space is (ip-address), NOT
1803 * (interface name, ip-address).
1805 ep = getinterface(&enumep.sin, INT_WILDCARD);
1807 if (ep != NULL && refresh_interface(ep)) {
1809 * found existing and up to date interface -
1812 if (ep->phase != sys_interphase) {
1814 * On a new round we reset the name so
1815 * the interface name shows up again if
1816 * this address is no longer shared.
1817 * We reset ignore_packets from the
1818 * new prototype to respect any runtime
1819 * changes to the nic rules.
1821 strlcpy(ep->name, enumep.name,
1823 ep->ignore_packets =
1824 enumep.ignore_packets;
1826 /* name collision - rename interface */
1827 strlcpy(ep->name, "*multiple*",
1831 DPRINT_INTERFACE(4, (ep, "updating ",
1834 if (ep->ignore_packets !=
1835 enumep.ignore_packets) {
1837 * We have conflicting configurations
1838 * for the interface address. This is
1839 * caused by using -I <interfacename>
1840 * for an interface that shares its
1841 * address with other interfaces. We
1842 * can not disambiguate incoming
1843 * packets delivered to this socket
1844 * without extra syscalls/features.
1845 * These are not (commonly) available.
1846 * Note this is a more unusual
1847 * configuration where several
1848 * interfaces share an address but
1849 * filtering via interface name is
1850 * attempted. We resolve the
1851 * configuration conflict by disabling
1852 * the processing of received packets.
1853 * This leads to no service on the
1854 * interface address where the conflict
1858 "WARNING: conflicting enable configuration for interfaces %s and %s for address %s - unsupported configuration - address DISABLED",
1859 enumep.name, ep->name,
1862 ep->ignore_packets = ISC_TRUE;
1865 ep->phase = sys_interphase;
1867 ifi.action = IFS_EXISTS;
1869 if (receiver != NULL)
1870 (*receiver)(data, &ifi);
1873 * This is new or refreshing failed - add to
1874 * our interface list. If refreshing failed we
1875 * will delete the interface structure in phase
1876 * 2 as the interface was not marked current.
1877 * We can bind to the address as the refresh
1878 * code already closed the offending socket
1880 ep = create_interface(port, &enumep);
1883 ifi.action = IFS_CREATED;
1885 if (receiver != NULL)
1886 (*receiver)(data, &ifi);
1888 new_interface_found = TRUE;
1891 " new - created\n"));
1894 (&enumep, "updating ",
1895 " new - creation FAILED"));
1898 "failed to init interface for address %s",
1905 isc_interfaceiter_destroy(&iter);
1908 * phase 2 - delete gone interfaces - reassigning peers to
1911 for (ep = ep_list; ep != NULL; ep = next_ep) {
1912 next_ep = ep->elink;
1915 * if phase does not match sys_phase this interface was
1916 * not enumerated during the last interface scan - so it
1917 * is gone and will be deleted here unless it did not
1918 * originate from interface enumeration (INT_WILDCARD,
1921 if (((INT_WILDCARD | INT_MCASTIF) & ep->flags) ||
1922 ep->phase == sys_interphase)
1925 DPRINT_INTERFACE(3, (ep, "updating ",
1926 "GONE - deleting\n"));
1927 remove_interface(ep);
1929 ifi.action = IFS_DELETED;
1931 if (receiver != NULL)
1932 (*receiver)(data, &ifi);
1934 /* disconnect peers from deleted endpt. */
1935 while (ep->peers != NULL)
1936 set_peerdstadr(ep->peers, NULL);
1939 * update globals in case we lose
1940 * a loopback interface
1942 if (ep == loopback_interface)
1943 loopback_interface = NULL;
1945 delete_interface(ep);
1949 * phase 3 - re-configure as the world has possibly changed
1951 * never ever make this conditional again - it is needed to track
1952 * routing updates. see bug #2506
1954 refresh_all_peerinterfaces();
1956 if (broadcast_client_enabled)
1964 * Check multicast interfaces and try to join multicast groups if
1967 for (ep = ep_list; ep != NULL; ep = ep->elink) {
1970 if (!(INT_MCASTIF & ep->flags) || (INT_MCASTOPEN & ep->flags))
1973 /* Find remote address that was linked to this interface */
1974 for (entry = remoteaddr_list;
1976 entry = entry->link) {
1977 if (entry->ep == ep) {
1978 if (socket_multicast_enable(ep, &entry->addr)) {
1980 "Joined %s socket to multicast group %s",
1982 stoa(&entry->addr));
1990 return new_interface_found;
1995 * create_sockets - create a socket for each interface plus a default
1996 * socket for when we don't know where to send
2003 #ifndef HAVE_IO_COMPLETION_PORT
2005 * I/O Completion Ports don't care about the select and FD_SET
2008 FD_ZERO(&activefds);
2011 DPRINTF(2, ("create_sockets(%d)\n", port));
2013 create_wildcards(port);
2015 update_interfaces(port, NULL, NULL);
2018 * Now that we have opened all the sockets, turn off the reuse
2019 * flag for security.
2023 DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
2029 * create_interface - create a new interface for a given prototype
2030 * binding the socket.
2032 static struct interface *
2035 struct interface * protot
2040 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2042 remaddr_t * next_entry;
2044 DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&protot->sin),
2047 /* build an interface */
2048 iface = new_interface(protot);
2053 iface->fd = open_socket(&iface->sin, 0, 0, iface);
2055 if (iface->fd != INVALID_SOCKET)
2056 log_listen_address(iface);
2058 if ((INT_BROADCAST & iface->flags)
2059 && iface->bfd != INVALID_SOCKET)
2060 msyslog(LOG_INFO, "Listening on broadcast address %s#%d",
2061 stoa((&iface->bcast)), port);
2063 if (INVALID_SOCKET == iface->fd
2064 && INVALID_SOCKET == iface->bfd) {
2065 msyslog(LOG_ERR, "unable to create socket on %s (%d) for %s#%d",
2068 stoa((&iface->sin)),
2070 delete_interface(iface);
2075 * Blacklist our own addresses, no use talking to ourself
2077 SET_HOSTMASK(&resmask, AF(&iface->sin));
2078 hack_restrict(RESTRICT_FLAGS, &iface->sin, &resmask,
2079 RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
2082 * set globals with the first found
2083 * loopback interface of the appropriate class
2085 if (NULL == loopback_interface && AF_INET == iface->family
2086 && (INT_LOOPBACK & iface->flags))
2087 loopback_interface = iface;
2090 * put into our interface list
2092 add_addr_to_list(&iface->sin, iface);
2093 add_interface(iface);
2095 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2097 * Join any previously-configured compatible multicast groups.
2099 if (INT_MULTICAST & iface->flags &&
2100 !((INT_LOOPBACK | INT_WILDCARD) & iface->flags) &&
2101 !iface->ignore_packets) {
2102 for (entry = remoteaddr_list;
2104 entry = next_entry) {
2105 next_entry = entry->link;
2106 if (AF(&iface->sin) != AF(&entry->addr) ||
2107 !IS_MCAST(&entry->addr))
2109 if (socket_multicast_enable(iface,
2112 "Joined %s socket to multicast group %s",
2114 stoa(&entry->addr));
2117 "Failed to join %s socket to multicast group %s",
2119 stoa(&entry->addr));
2122 #endif /* MCAST && MCAST_NONEWSOCKET */
2124 DPRINT_INTERFACE(2, (iface, "created ", "\n"));
2129 #ifdef SO_EXCLUSIVEADDRUSE
2141 failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
2142 (char *)&one, sizeof(one));
2149 * Prior to Windows XP setting SO_EXCLUSIVEADDRUSE can fail with
2150 * error WSAINVAL depending on service pack level and whether
2151 * the user account is in the Administrators group. Do not
2152 * complain if it fails that way on versions prior to XP (5.1).
2154 err = GetLastError();
2156 if (isc_win32os_versioncheck(5, 1, 0, 0) < 0 /* < 5.1/XP */
2157 && WSAEINVAL == err)
2163 "setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m",
2166 #endif /* SO_EXCLUSIVEADDRUSE */
2170 * set_reuseaddr() - set/clear REUSEADDR on all sockets
2171 * NB possible hole - should we be doing this on broadcast
2179 #ifndef SO_EXCLUSIVEADDRUSE
2182 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2183 if (ep->flags & INT_WILDCARD)
2187 * if ep->fd is INVALID_SOCKET, we might have a adapter
2188 * configured but not present
2190 DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n",
2191 ep->name, stoa(&ep->sin),
2192 flag ? "on" : "off"));
2194 if (ep->fd != INVALID_SOCKET) {
2195 if (setsockopt(ep->fd, SOL_SOCKET, SO_REUSEADDR,
2196 (char *)&flag, sizeof(flag))) {
2197 msyslog(LOG_ERR, "set_reuseaddr: setsockopt(%s, SO_REUSEADDR, %s) failed: %m",
2198 stoa(&ep->sin), flag ? "on" : "off");
2202 #endif /* ! SO_EXCLUSIVEADDRUSE */
2206 * This is just a wrapper around an internal function so we can
2207 * make other changes as necessary later on
2211 struct interface * iface,
2215 #ifdef OPEN_BCAST_SOCKET
2216 socket_broadcast_enable(iface, iface->fd, baddr);
2220 #ifdef OPEN_BCAST_SOCKET
2222 * Enable a broadcast address to a given socket
2223 * The socket is in the ep_list all we need to do is enable
2224 * broadcasting. It is not this function's job to select the socket
2226 static isc_boolean_t
2227 socket_broadcast_enable(
2228 struct interface * iface,
2236 if (IS_IPV4(baddr)) {
2237 /* if this interface can support broadcast, set SO_BROADCAST */
2238 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
2239 (char *)&on, sizeof(on)))
2241 "setsockopt(SO_BROADCAST) enable failure on address %s: %m",
2244 DPRINTF(2, ("Broadcast enabled on socket %d for address %s\n",
2247 iface->flags |= INT_BCASTXMIT;
2251 #endif /* SO_BROADCAST */
2254 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
2256 * Remove a broadcast address from a given socket
2257 * The socket is in the ep_list all we need to do is disable
2258 * broadcasting. It is not this function's job to select the socket
2260 static isc_boolean_t
2261 socket_broadcast_disable(
2262 struct interface * iface,
2267 int off = 0; /* This seems to be OK as an int */
2269 if (IS_IPV4(baddr) && setsockopt(iface->fd, SOL_SOCKET,
2270 SO_BROADCAST, (char *)&off, sizeof(off)))
2272 "setsockopt(SO_BROADCAST) disable failure on address %s: %m",
2275 iface->flags &= ~INT_BCASTXMIT;
2279 #endif /* SO_BROADCAST */
2281 #endif /* OS_MISSES_SPECIFIC_ROUTE_UPDATES */
2283 #endif /* OPEN_BCAST_SOCKET */
2286 * return the broadcast client flag value
2289 get_broadcastclient_flag(void)
2291 return (broadcast_client_enabled);
2295 * Check to see if the address is a multicast address
2297 static isc_boolean_t
2302 isc_boolean_t result;
2304 #ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
2306 * If we don't have IPV6 support any IPV6 addr is not multicast
2312 result = IS_MCAST(maddr);
2315 DPRINTF(4, ("address %s is not multicast\n",
2322 * Multicast servers need to set the appropriate Multicast interface
2323 * socket option in order for it to know which interface to use for
2324 * send the multicast packet.
2327 enable_multicast_if(
2328 struct interface * iface,
2333 #ifdef IP_MULTICAST_LOOP
2334 TYPEOF_IP_MULTICAST_LOOP off = 0;
2336 #if defined(INCLUDE_IPV6_MULTICAST_SUPPORT) && defined(IPV6_MULTICAST_LOOP)
2340 REQUIRE(AF(maddr) == AF(&iface->sin));
2342 switch (AF(&iface->sin)) {
2345 #ifdef IP_MULTICAST_LOOP
2347 * Don't send back to itself, but allow failure to set
2349 if (setsockopt(iface->fd, IPPROTO_IP,
2351 SETSOCKOPT_ARG_CAST &off,
2355 "setsockopt IP_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2356 iface->fd, stoa(&iface->sin),
2363 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2364 #ifdef IPV6_MULTICAST_LOOP
2366 * Don't send back to itself, but allow failure to set
2368 if (setsockopt(iface->fd, IPPROTO_IPV6,
2369 IPV6_MULTICAST_LOOP,
2370 (char *) &off6, sizeof(off6))) {
2373 "setsockopt IPV6_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2374 iface->fd, stoa(&iface->sin),
2381 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2388 * Add a multicast address to a given socket
2389 * The socket is in the ep_list all we need to do is enable
2390 * multicasting. It is not this function's job to select the socket
2393 static isc_boolean_t
2394 socket_multicast_enable(
2399 struct ip_mreq mreq;
2400 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2401 struct ipv6_mreq mreq6;
2403 switch (AF(maddr)) {
2407 mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2408 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
2409 if (setsockopt(iface->fd,
2415 "setsockopt IP_ADD_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2416 iface->fd, stoa(&iface->sin),
2417 mreq.imr_multiaddr.s_addr,
2418 mreq.imr_interface.s_addr,
2422 DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n",
2423 iface->fd, stoa(&iface->sin),
2424 mreq.imr_multiaddr.s_addr,
2425 mreq.imr_interface.s_addr, stoa(maddr)));
2429 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2431 * Enable reception of multicast packets.
2432 * If the address is link-local we can get the
2433 * interface index from the scope id. Don't do this
2434 * for other types of multicast addresses. For now let
2435 * the kernel figure it out.
2438 mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2439 mreq6.ipv6mr_interface = iface->ifindex;
2441 if (setsockopt(iface->fd, IPPROTO_IPV6,
2442 IPV6_JOIN_GROUP, (char *)&mreq6,
2445 "setsockopt IPV6_JOIN_GROUP failed: %m on socket %d, addr %s for interface %u (%s)",
2446 iface->fd, stoa(&iface->sin),
2447 mreq6.ipv6mr_interface, stoa(maddr)));
2450 DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %u (%s)\n",
2451 iface->fd, stoa(&iface->sin),
2452 mreq6.ipv6mr_interface, stoa(maddr)));
2455 # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2457 iface->flags |= INT_MCASTOPEN;
2466 * Remove a multicast address from a given socket
2467 * The socket is in the ep_list all we need to do is disable
2468 * multicasting. It is not this function's job to select the socket
2471 static isc_boolean_t
2472 socket_multicast_disable(
2473 struct interface * iface,
2477 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2478 struct ipv6_mreq mreq6;
2480 struct ip_mreq mreq;
2484 if (find_addr_in_list(maddr) == NULL) {
2485 DPRINTF(4, ("socket_multicast_disable(%s): not found\n",
2490 switch (AF(maddr)) {
2493 mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2494 mreq.imr_interface = SOCK_ADDR4(&iface->sin);
2495 if (setsockopt(iface->fd, IPPROTO_IP,
2496 IP_DROP_MEMBERSHIP, (char *)&mreq,
2500 "setsockopt IP_DROP_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2501 iface->fd, stoa(&iface->sin),
2502 SRCADR(maddr), SRCADR(&iface->sin),
2508 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2510 * Disable reception of multicast packets
2511 * If the address is link-local we can get the
2512 * interface index from the scope id. Don't do this
2513 * for other types of multicast addresses. For now let
2514 * the kernel figure it out.
2516 mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2517 mreq6.ipv6mr_interface = iface->ifindex;
2519 if (setsockopt(iface->fd, IPPROTO_IPV6,
2520 IPV6_LEAVE_GROUP, (char *)&mreq6,
2524 "setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d (%s)",
2525 iface->fd, stoa(&iface->sin),
2526 iface->ifindex, stoa(maddr));
2532 # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2536 if (!iface->num_mcast)
2537 iface->flags &= ~INT_MCASTOPEN;
2544 * io_setbclient - open the broadcast client sockets
2549 #ifdef OPEN_BCAST_SOCKET
2550 struct interface * interf;
2556 for (interf = ep_list;
2558 interf = interf->elink) {
2560 if (interf->flags & (INT_WILDCARD | INT_LOOPBACK))
2563 /* use only allowed addresses */
2564 if (interf->ignore_packets)
2567 /* Need a broadcast-capable interface */
2568 if (!(interf->flags & INT_BROADCAST))
2571 /* Only IPv4 addresses are valid for broadcast */
2572 REQUIRE(IS_IPV4(&interf->sin));
2574 /* Do we already have the broadcast address open? */
2575 if (interf->flags & INT_BCASTOPEN) {
2577 * account for already open interfaces to avoid
2578 * misleading warning below
2585 * Try to open the broadcast address
2587 interf->family = AF_INET;
2588 interf->bfd = open_socket(&interf->bcast, 1, 0, interf);
2591 * If we succeeded then we use it otherwise enable
2592 * broadcast on the interface address
2594 if (interf->bfd != INVALID_SOCKET) {
2596 interf->flags |= INT_BCASTOPEN;
2598 "Listen for broadcasts to %s on interface #%d %s",
2599 stoa(&interf->bcast), interf->ifnum, interf->name);
2601 /* silently ignore EADDRINUSE as we probably opened
2602 the socket already for an address in the same network */
2603 if (errno != EADDRINUSE)
2605 "failed to listen for broadcasts to %s on interface #%d %s",
2606 stoa(&interf->bcast), interf->ifnum, interf->name);
2611 broadcast_client_enabled = ISC_TRUE;
2612 DPRINTF(1, ("io_setbclient: listening to %d broadcast addresses\n", nif));
2615 broadcast_client_enabled = ISC_FALSE;
2617 "Unable to listen for broadcasts, no broadcast interfaces available");
2621 "io_setbclient: Broadcast Client disabled by build");
2622 #endif /* OPEN_BCAST_SOCKET */
2626 * io_unsetbclient - close the broadcast client sockets
2629 io_unsetbclient(void)
2633 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2634 if (INT_WILDCARD & ep->flags)
2636 if (!(INT_BCASTOPEN & ep->flags))
2639 if (ep->bfd != INVALID_SOCKET) {
2640 /* destroy broadcast listening socket */
2642 "stop listening for broadcasts to %s on interface #%d %s",
2643 stoa(&ep->bcast), ep->ifnum, ep->name);
2644 close_and_delete_fd_from_list(ep->bfd);
2645 ep->bfd = INVALID_SOCKET;
2646 ep->flags &= ~INT_BCASTOPEN;
2649 broadcast_client_enabled = ISC_FALSE;
2653 * io_multicast_add() - add multicast group address
2665 * Check to see if this is a multicast address
2667 if (!addr_ismulticast(addr))
2670 /* If we already have it we can just return */
2671 if (NULL != find_flagged_addr_in_list(addr, INT_MCASTOPEN)) {
2673 "Duplicate request found for multicast address %s",
2678 # ifndef MULTICAST_NONEWSOCKET
2679 ep = new_interface(NULL);
2682 * Open a new socket for the multicast address
2685 SET_PORT(&ep->sin, NTP_PORT);
2686 ep->family = AF(&ep->sin);
2687 AF(&ep->mask) = ep->family;
2688 SET_ONESMASK(&ep->mask);
2691 ep->bfd = INVALID_SOCKET;
2692 ep->fd = open_socket(&ep->sin, 0, 0, ep);
2693 if (ep->fd != INVALID_SOCKET) {
2694 ep->ignore_packets = ISC_FALSE;
2695 ep->flags |= INT_MCASTIF;
2697 strlcpy(ep->name, "multicast", sizeof(ep->name));
2698 DPRINT_INTERFACE(2, (ep, "multicast add ", "\n"));
2700 log_listen_address(ep);
2702 /* bind failed, re-use wildcard interface */
2703 delete_interface(ep);
2707 else if (IS_IPV6(addr))
2713 /* HACK ! -- stuff in an address */
2714 /* because we don't bind addr? DH */
2717 "multicast address %s using wildcard interface #%d %s",
2718 stoa(addr), ep->ifnum, ep->name);
2721 "No multicast socket available to use for address %s",
2726 { /* in place of the { following for in #else clause */
2728 # else /* MULTICAST_NONEWSOCKET follows */
2730 * For the case where we can't use a separate socket (Windows)
2731 * join each applicable endpoint socket to the group address.
2737 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2738 if (ep->ignore_packets || AF(&ep->sin) != AF(addr) ||
2739 !(INT_MULTICAST & ep->flags) ||
2740 (INT_LOOPBACK | INT_WILDCARD) & ep->flags)
2743 # endif /* MULTICAST_NONEWSOCKET */
2744 if (socket_multicast_enable(ep, addr))
2746 "Joined %s socket to multicast group %s",
2751 add_addr_to_list(addr, one_ep);
2752 #else /* !MCAST follows*/
2754 "Can not add multicast address %s: no multicast support",
2762 * io_multicast_del() - delete multicast group address
2773 * Check to see if this is a multicast address
2775 if (!addr_ismulticast(addr)) {
2776 msyslog(LOG_ERR, "invalid multicast address %s",
2782 * Disable reception of multicast packets
2784 while ((iface = find_flagged_addr_in_list(addr, INT_MCASTOPEN))
2786 socket_multicast_disable(iface, addr);
2788 delete_addr_from_list(addr);
2790 #else /* not MCAST */
2792 "Can not delete multicast address %s: no multicast support",
2794 #endif /* not MCAST */
2799 * open_socket - open a socket, returning the file descriptor
2813 * int is OK for REUSEADR per
2814 * http://www.kohala.com/start/mcast.api.txt
2819 if (IS_IPV6(addr) && !ipv6_works)
2820 return INVALID_SOCKET;
2822 /* create a datagram (UDP) socket */
2823 fd = socket(AF(addr), SOCK_DGRAM, 0);
2824 if (INVALID_SOCKET == fd) {
2825 errval = socket_errno();
2827 "socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
2828 IS_IPV6(addr) ? "6" : "", stoa(addr));
2830 if (errval == EPROTONOSUPPORT ||
2831 errval == EAFNOSUPPORT ||
2832 errval == EPFNOSUPPORT)
2833 return (INVALID_SOCKET);
2837 "unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - exiting",
2843 connection_reset_fix(fd, addr);
2846 * Fixup the file descriptor for some systems
2847 * See bug #530 for details of the issue.
2852 * set SO_REUSEADDR since we will be binding the same port
2853 * number on each interface according to turn_off_reuse.
2854 * This is undesirable on Windows versions starting with
2855 * Windows XP (numeric version 5.1).
2858 if (isc_win32os_versioncheck(5, 1, 0, 0) < 0) /* before 5.1 */
2860 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2861 (char *)((turn_off_reuse)
2867 "setsockopt SO_REUSEADDR %s fails for address %s: %m",
2873 return INVALID_SOCKET;
2875 #ifdef SO_EXCLUSIVEADDRUSE
2877 * setting SO_EXCLUSIVEADDRUSE on the wildcard we open
2878 * first will cause more specific binds to fail.
2880 if (!(interf->flags & INT_WILDCARD))
2881 set_excladdruse(fd);
2885 * IPv4 specific options go here
2887 if (IS_IPV4(addr)) {
2888 #if defined(IPPROTO_IP) && defined(IP_TOS)
2889 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char*)&qos,
2892 "setsockopt IP_TOS (%02x) fails on address %s: %m",
2894 #endif /* IPPROTO_IP && IP_TOS */
2896 socket_broadcast_enable(interf, fd, addr);
2900 * IPv6 specific options go here
2902 if (IS_IPV6(addr)) {
2903 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
2904 if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, (char*)&qos,
2907 "setsockopt IPV6_TCLASS (%02x) fails on address %s: %m",
2909 #endif /* IPPROTO_IPV6 && IPV6_TCLASS */
2911 if (isc_net_probe_ipv6only() == ISC_R_SUCCESS
2912 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
2913 (char*)&on, sizeof(on)))
2915 "setsockopt IPV6_V6ONLY on fails on address %s: %m",
2918 #ifdef IPV6_BINDV6ONLY
2919 if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
2920 (char*)&on, sizeof(on)))
2922 "setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
2927 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2929 * some OSes don't allow binding to more specific
2930 * addresses if a wildcard address already bound
2931 * to the port and SO_REUSEADDR is not set
2933 if (!is_wildcard_addr(addr))
2934 set_wildcard_reuse(AF(addr), 1);
2938 * bind the local address.
2940 errval = bind(fd, &addr->sa, SOCKLEN(addr));
2942 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2943 if (!is_wildcard_addr(addr))
2944 set_wildcard_reuse(AF(addr), 0);
2949 * Don't log this under all conditions
2951 if (turn_off_reuse == 0
2957 "bind(%d) AF_INET%s %s#%d%s flags 0x%x failed: %m",
2958 fd, IS_IPV6(addr) ? "6" : "",
2959 stoa(addr), SRCPORT(addr),
2960 IS_MCAST(addr) ? " (multicast)" : "",
2966 return INVALID_SOCKET;
2969 #ifdef HAVE_TIMESTAMP
2971 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
2972 (char*)&on, sizeof(on)))
2974 "setsockopt SO_TIMESTAMP on fails on address %s: %m",
2977 DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
2981 #ifdef HAVE_TIMESTAMPNS
2983 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
2984 (char*)&on, sizeof(on)))
2986 "setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
2989 DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
2995 if (setsockopt(fd, SOL_SOCKET, SO_BINTIME,
2996 (char*)&on, sizeof(on)))
2998 "setsockopt SO_BINTIME on fails on address %s: %m",
3001 DPRINTF(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
3006 DPRINTF(4, ("bind(%d) AF_INET%s, addr %s%%%d#%d, flags 0x%x\n",
3007 fd, IS_IPV6(addr) ? "6" : "", stoa(addr),
3008 SCOPE(addr), SRCPORT(addr), interf->flags));
3010 make_socket_nonblocking(fd);
3012 #ifdef HAVE_SIGNALED_IO
3013 init_socket_sig(fd);
3014 #endif /* not HAVE_SIGNALED_IO */
3016 add_fd_to_list(fd, FD_TYPE_SOCKET);
3018 #if !defined(SYS_WINNT) && !defined(VMS)
3019 DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
3020 fcntl(fd, F_GETFL, 0)));
3021 #endif /* SYS_WINNT || VMS */
3023 #if defined (HAVE_IO_COMPLETION_PORT)
3025 * Add the socket to the completion port
3027 if (io_completion_port_add_socket(fd, interf)) {
3028 msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
3037 #define sendto(fd, buf, len, flags, dest, destsz) \
3038 io_completion_port_sendto(fd, buf, len, (sockaddr_u *)(dest))
3041 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
3043 * sendpkt - send a packet to the specified destination. Maintain a
3044 * send error cache so that only the first consecutive error for a
3045 * destination is logged.
3050 struct interface * ep,
3062 ismcast = IS_MCAST(dest);
3066 src = (IS_IPV4(dest))
3072 * unbound peer - drop request and wait for better
3073 * network conditions
3075 DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
3076 ismcast ? "\tMCAST\t***** " : "",
3077 stoa(dest), ttl, len));
3082 DPRINTF(2, ("%ssendpkt(%d, dst=%s, src=%s, ttl=%d, len=%d)\n",
3083 ismcast ? "\tMCAST\t***** " : "", src->fd,
3084 stoa(dest), stoa(&src->sin), ttl, len));
3087 * for the moment we use the bcast option to set multicast ttl
3089 if (ismcast && ttl > 0 && ttl != src->last_ttl) {
3091 * set the multicast ttl for outgoing packets
3093 switch (AF(&src->sin)) {
3097 rc = setsockopt(src->fd, IPPROTO_IP,
3103 # ifdef INCLUDE_IPV6_SUPPORT
3105 rc = setsockopt(src->fd, IPPROTO_IPV6,
3106 IPV6_MULTICAST_HOPS,
3110 # endif /* INCLUDE_IPV6_SUPPORT */
3117 src->last_ttl = ttl;
3120 "setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
3126 cc = simulate_server(dest, src, pkt);
3128 cc = sendto(src->fd, (char *)pkt, (u_int)len, 0,
3129 &dest->sa, SOCKLEN(dest));
3140 } while (ismcast && src != NULL);
3144 #if !defined(HAVE_IO_COMPLETION_PORT) && !defined(HAVE_SIGNALED_IO)
3146 * fdbits - generate ascii representation of fd_set (FAU debug support)
3147 * HFDF format - highest fd first.
3155 static char buffer[256];
3156 char * buf = buffer;
3158 count = min(count, 255);
3160 while (count >= 0) {
3161 *buf++ = FD_ISSET(count, set) ? '#' : '-';
3172 * Routine to read the refclock packets for a specific interface
3173 * Return the number of bytes read. That way we know if we should
3174 * read it again or go on to the next one if no bytes returned
3177 read_refclock_packet(
3179 struct refclockio * rp,
3187 struct recvbuf * rb;
3189 rb = get_free_recv_buffer();
3193 * No buffer space available - just drop the packet
3195 char buf[RX_BUFF_SIZE];
3197 buflen = read(fd, buf, sizeof buf);
3202 /* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead
3203 * to buffer overrun and memory corruption
3205 if (rp->datalen <= 0 || (size_t)rp->datalen > sizeof(rb->recv_space))
3206 read_count = sizeof(rb->recv_space);
3208 read_count = (u_int)rp->datalen;
3210 buflen = read(fd, (char *)&rb->recv_space, read_count);
3211 } while (buflen < 0 && EINTR == errno);
3214 saved_errno = errno;
3216 errno = saved_errno;
3221 * Got one. Mark how and when it got here,
3222 * put it on the full list and do bookkeeping.
3224 rb->recv_length = buflen;
3225 rb->recv_peer = rp->srcclock;
3229 rb->receiver = rp->clock_recv;
3231 consumed = indicate_refclock_packet(rp, rb);
3239 #endif /* REFCLOCK */
3242 #ifdef HAVE_PACKET_TIMESTAMP
3244 * extract timestamps from control message buffer
3248 struct recvbuf * rb,
3249 struct msghdr * msghdr,
3253 struct cmsghdr * cmsghdr;
3255 struct bintime * btp;
3257 #ifdef HAVE_TIMESTAMPNS
3258 struct timespec * tsp;
3260 #ifdef HAVE_TIMESTAMP
3261 struct timeval * tvp;
3263 unsigned long ticks;
3271 cmsghdr = CMSG_FIRSTHDR(msghdr);
3272 while (cmsghdr != NULL) {
3273 switch (cmsghdr->cmsg_type)
3277 #endif /* HAVE_BINTIME */
3278 #ifdef HAVE_TIMESTAMPNS
3279 case SCM_TIMESTAMPNS:
3280 #endif /* HAVE_TIMESTAMPNS */
3281 #ifdef HAVE_TIMESTAMP
3283 #endif /* HAVE_TIMESTAMP */
3284 #if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP)
3285 switch (cmsghdr->cmsg_type)
3289 btp = (struct bintime *)CMSG_DATA(cmsghdr);
3291 * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
3293 nts.l_i = btp->sec + JAN_1970;
3294 nts.l_uf = (u_int32)(btp->frac >> 32);
3295 if (sys_tick > measured_tick &&
3297 ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC));
3298 nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC));
3300 DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
3301 btp->sec, (unsigned long)((nts.l_uf / FRAC) * 1e9)));
3303 #endif /* HAVE_BINTIME */
3304 #ifdef HAVE_TIMESTAMPNS
3305 case SCM_TIMESTAMPNS:
3306 tsp = UA_PTR(struct timespec, CMSG_DATA(cmsghdr));
3307 if (sys_tick > measured_tick &&
3309 ticks = (unsigned long)((tsp->tv_nsec * 1e-9) /
3311 tsp->tv_nsec = (long)(ticks * 1e9 *
3314 DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
3315 tsp->tv_sec, tsp->tv_nsec));
3316 nts = tspec_stamp_to_lfp(*tsp);
3318 #endif /* HAVE_TIMESTAMPNS */
3319 #ifdef HAVE_TIMESTAMP
3321 tvp = (struct timeval *)CMSG_DATA(cmsghdr);
3322 if (sys_tick > measured_tick &&
3324 ticks = (unsigned long)((tvp->tv_usec * 1e-6) /
3326 tvp->tv_usec = (long)(ticks * 1e6 *
3329 DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
3330 (intmax_t)tvp->tv_sec, (long)tvp->tv_usec));
3331 nts = tval_stamp_to_lfp(*tvp);
3333 #endif /* HAVE_TIMESTAMP */
3335 fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
3336 DTOLFP(fuzz, &lfpfuzz);
3337 L_ADD(&nts, &lfpfuzz);
3341 collect_timing(rb, "input processing delay", 1,
3343 DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
3345 #endif /* DEBUG_TIMING */
3346 ts = nts; /* network time stamp */
3348 #endif /* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */
3351 DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n",
3352 cmsghdr->cmsg_type));
3354 cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
3358 #endif /* HAVE_PACKET_TIMESTAMP */
3362 * Routine to read the network NTP packets for a specific interface
3363 * Return the number of bytes read. That way we know if we should
3364 * read it again or go on to the next one if no bytes returned
3367 read_network_packet(
3369 struct interface * itf,
3373 GETSOCKNAME_SOCKLEN_TYPE fromlen;
3375 register struct recvbuf *rb;
3376 #ifdef HAVE_PACKET_TIMESTAMP
3377 struct msghdr msghdr;
3379 char control[CMSG_BUFSIZE];
3383 * Get a buffer and read the frame. If we
3384 * haven't got a buffer, or this is received
3385 * on a disallowed socket, just dump the
3389 rb = get_free_recv_buffer();
3390 if (NULL == rb || itf->ignore_packets) {
3391 char buf[RX_BUFF_SIZE];
3397 fromlen = sizeof(from);
3398 buflen = recvfrom(fd, buf, sizeof(buf), 0,
3399 &from.sa, &fromlen);
3400 DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
3401 (itf->ignore_packets)
3404 free_recvbuffs(), fd, stoa(&from)));
3405 if (itf->ignore_packets)
3412 fromlen = sizeof(rb->recv_srcadr);
3414 #ifndef HAVE_PACKET_TIMESTAMP
3415 rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
3416 sizeof(rb->recv_space), 0,
3417 &rb->recv_srcadr.sa, &fromlen);
3419 iovec.iov_base = &rb->recv_space;
3420 iovec.iov_len = sizeof(rb->recv_space);
3421 msghdr.msg_name = &rb->recv_srcadr;
3422 msghdr.msg_namelen = fromlen;
3423 msghdr.msg_iov = &iovec;
3424 msghdr.msg_iovlen = 1;
3425 msghdr.msg_control = (void *)&control;
3426 msghdr.msg_controllen = sizeof(control);
3427 msghdr.msg_flags = 0;
3428 rb->recv_length = recvmsg(fd, &msghdr, 0);
3431 buflen = rb->recv_length;
3433 if (buflen == 0 || (buflen == -1 &&
3434 (EWOULDBLOCK == errno
3441 } else if (buflen < 0) {
3442 msyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
3443 stoa(&rb->recv_srcadr), fd);
3444 DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n",
3450 DPRINTF(3, ("read_network_packet: fd=%d length %d from %s\n",
3451 fd, buflen, stoa(&rb->recv_srcadr)));
3454 ** Bug 2672: Some OSes (MacOSX and Linux) don't block spoofed ::1
3457 if (AF_INET6 == itf->family) {
3458 DPRINTF(2, ("Got an IPv6 packet, from <%s> (%d) to <%s> (%d)\n",
3459 stoa(&rb->recv_srcadr),
3460 IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr)),
3462 !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3465 if ( IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr))
3466 && !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3469 DPRINTF(2, ("DROPPING that packet\n"));
3473 DPRINTF(2, ("processing that packet\n"));
3477 * Got one. Mark how and when it got here,
3478 * put it on the full list and do bookkeeping.
3482 #ifdef HAVE_PACKET_TIMESTAMP
3483 /* pick up a network time stamp if possible */
3484 ts = fetch_timestamp(rb, &msghdr, ts);
3487 rb->receiver = receive;
3489 add_full_recv_buffer(rb);
3497 * attempt to handle io (select()/signaled IO)
3502 # ifndef HAVE_SIGNALED_IO
3507 * Use select() on all on all input fd's for unlimited
3508 * time. select() will terminate on SIGALARM or on the
3509 * reception of input. Using select() means we can't do
3510 * robust signal handling and we get a potential race
3511 * between checking for alarms and doing the select().
3512 * Mostly harmless, I think.
3515 * On VMS, I suspect that select() can't be interrupted
3516 * by a "signal" either, so I take the easy way out and
3517 * have select() time out after one second.
3518 * System clock updates really aren't time-critical,
3519 * and - lacking a hardware reference clock - I have
3520 * yet to learn about anything else that is.
3524 # if !defined(VMS) && !defined(SYS_VXWORKS)
3525 nfound = select(maxactivefd + 1, &rdfdes, NULL,
3527 # else /* VMS, VxWorks */
3528 /* make select() wake up after one second */
3533 nfound = select(maxactivefd + 1,
3534 &rdfdes, NULL, NULL,
3537 # endif /* VMS, VxWorks */
3538 if (nfound < 0 && sanitize_fdset(errno)) {
3543 nfound = select(maxactivefd + 1,
3544 &rdfdes, NULL, NULL,
3553 input_handler_scan(&ts, &rdfdes);
3554 } else if (nfound == -1 && errno != EINTR) {
3555 msyslog(LOG_ERR, "select() error: %m");
3558 else if (debug > 4) {
3559 msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
3561 DPRINTF(3, ("select() returned %d: %m\n", nfound));
3564 # else /* HAVE_SIGNALED_IO */
3566 # endif /* HAVE_SIGNALED_IO */
3569 #ifdef HAVE_SIGNALED_IO
3571 * input_handler - receive packets asynchronously
3573 * ALWAYS IN SIGNAL HANDLER CONTEXT -- only async-safe functions allowed!
3581 struct timeval tvzero;
3587 * Do a poll to see who has data
3591 tvzero.tv_sec = tvzero.tv_usec = 0;
3593 n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3594 if (n < 0 && sanitize_fdset(errno)) {
3596 tvzero.tv_sec = tvzero.tv_usec = 0;
3597 n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3600 input_handler_scan(cts, &fds);
3602 #endif /* HAVE_SIGNALED_IO */
3606 * Try to sanitize the global FD set
3608 * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3617 # ifndef HAVE_SIGNALED_IO
3619 * extended FAU debugging output
3621 if (errc != EINTR) {
3623 "select(%d, %s, 0L, 0L, &0.0) error: %m",
3625 fdbits(maxactivefd, &activefds));
3632 /* if we have oviously bad FDs, try to sanitize the FD set. */
3633 for (j = 0, maxscan = 0; j <= maxactivefd; j++) {
3634 if (FD_ISSET(j, &activefds)) {
3635 if (-1 != read(j, &b, 0)) {
3639 # ifndef HAVE_SIGNALED_IO
3641 "Removing bad file descriptor %d from select set",
3644 FD_CLR(j, &activefds);
3647 if (maxactivefd != maxscan)
3648 maxactivefd = maxscan;
3653 * scan the known FDs (clocks, servers, ...) for presence in a 'fd_set'.
3655 * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3668 l_fp ts; /* Timestamp at BOselect() gob */
3670 #if defined(DEBUG_TIMING)
3671 l_fp ts_e; /* Timestamp at EOselect() gob */
3675 struct refclockio *rp;
3679 #ifdef HAS_ROUTING_SOCKET
3680 struct asyncio_reader * asyncio_reader;
3681 struct asyncio_reader * next_asyncio_reader;
3689 * Check out the reference clocks first, if any
3692 for (rp = refio; rp != NULL; rp = rp->next) {
3695 if (!FD_ISSET(fd, pfds))
3697 buflen = read_refclock_packet(fd, rp, ts);
3699 * The first read must succeed after select() indicates
3700 * readability, or we've reached a permanent EOF.
3701 * http://bugs.ntp.org/1732 reported ntpd munching CPU
3702 * after a USB GPS was unplugged because select was
3703 * indicating EOF but ntpd didn't remove the descriptor
3704 * from the activefds set.
3706 if (buflen < 0 && EAGAIN != errno) {
3707 saved_errno = errno;
3708 clk = refnumtoa(&rp->srcclock->srcadr);
3709 errno = saved_errno;
3710 msyslog(LOG_ERR, "%s read: %m", clk);
3711 maintain_activefds(fd, TRUE);
3712 } else if (0 == buflen) {
3713 clk = refnumtoa(&rp->srcclock->srcadr);
3714 msyslog(LOG_ERR, "%s read EOF", clk);
3715 maintain_activefds(fd, TRUE);
3717 /* drain any remaining refclock input */
3719 buflen = read_refclock_packet(fd, rp, ts);
3720 } while (buflen > 0);
3723 #endif /* REFCLOCK */
3726 * Loop through the interfaces looking for data to read.
3728 for (ep = ep_list; ep != NULL; ep = ep->elink) {
3729 for (doing = 0; doing < 2; doing++) {
3733 if (!(ep->flags & INT_BCASTOPEN))
3739 if (FD_ISSET(fd, pfds))
3741 buflen = read_network_packet(
3743 } while (buflen > 0);
3744 /* Check more interfaces */
3748 #ifdef HAS_ROUTING_SOCKET
3750 * scan list of asyncio readers - currently only used for routing sockets
3752 asyncio_reader = asyncio_reader_list;
3754 while (asyncio_reader != NULL) {
3755 /* callback may unlink and free asyncio_reader */
3756 next_asyncio_reader = asyncio_reader->link;
3757 if (FD_ISSET(asyncio_reader->fd, pfds))
3758 (*asyncio_reader->receiver)(asyncio_reader);
3759 asyncio_reader = next_asyncio_reader;
3761 #endif /* HAS_ROUTING_SOCKET */
3764 * Check for a response from a blocking child
3766 for (idx = 0; idx < blocking_children_alloc; idx++) {
3767 c = blocking_children[idx];
3768 if (NULL == c || -1 == c->resp_read_pipe)
3770 if (FD_ISSET(c->resp_read_pipe, pfds)) {
3771 ++c->resp_ready_seen;
3772 ++blocking_child_ready_seen;
3776 /* We've done our work */
3777 #if defined(DEBUG_TIMING)
3780 * (ts_e - ts) is the amount of time we spent
3781 * processing this gob of file descriptors. Log
3785 collect_timing(NULL, "input handler", 1, &ts_e);
3788 "input_handler: Processed a gob of fd's in %s msec",
3790 #endif /* DEBUG_TIMING */
3795 * find an interface suitable for the src address
3798 select_peerinterface(
3800 sockaddr_u * srcadr,
3808 wild = ANY_INTERFACE_CHOOSE(srcadr);
3811 * Initialize the peer structure and dance the interface jig.
3812 * Reference clocks step the loopback waltz, the others
3813 * squaredance around the interface list looking for a buddy. If
3814 * the dance peters out, there is always the wildcard interface.
3815 * This might happen in some systems and would preclude proper
3816 * operation with public key cryptography.
3818 if (ISREFCLOCKADR(srcadr)) {
3819 ep = loopback_interface;
3820 } else if (peer->cast_flags &
3821 (MDF_BCLNT | MDF_ACAST | MDF_MCAST | MDF_BCAST)) {
3822 ep = findbcastinter(srcadr);
3824 DPRINTF(4, ("Found *-cast interface %s for address %s\n",
3825 stoa(&ep->sin), stoa(srcadr)));
3827 DPRINTF(4, ("No *-cast local address found for address %s\n",
3835 * If it is a multicast address, findbcastinter() may not find
3836 * it. For unicast, we get to find the interface when dstadr is
3837 * given to us as the wildcard (ANY_INTERFACE_CHOOSE). Either
3838 * way, try a little harder.
3841 ep = findinterface(srcadr);
3843 * we do not bind to the wildcard interfaces for output
3844 * as our (network) source address would be undefined and
3845 * crypto will not work without knowing the own transmit address
3847 if (ep != NULL && INT_WILDCARD & ep->flags)
3848 if (!accept_wildcard_if_for_winnt)
3850 #else /* SIM follows */
3851 ep = loopback_interface;
3859 * findinterface - find local interface corresponding to address
3868 iface = findlocalinterface(addr, INT_WILDCARD, 0);
3870 if (NULL == iface) {
3871 DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
3874 iface = ANY_INTERFACE_CHOOSE(addr);
3876 DPRINTF(4, ("Found interface #%d %s for address %s\n",
3877 iface->ifnum, iface->name, stoa(addr)));
3883 * findlocalinterface - find local interface corresponding to addr,
3884 * which does not have any of flags set. If bast is nonzero, addr is
3885 * a broadcast address.
3887 * This code attempts to find the local sending address for an outgoing
3888 * address by connecting a new socket to destinationaddress:NTP_PORT
3889 * and reading the sockname of the resulting connect.
3890 * the complicated sequence simulates the routing table lookup
3891 * for to first hop without duplicating any of the routing logic into
3892 * ntpd. preferably we would have used an API call - but its not there -
3893 * so this is the best we can do here short of duplicating to entire routing
3894 * logic in ntpd which would be a silly and really unportable thing to do.
3904 GETSOCKNAME_SOCKLEN_TYPE sockaddrlen;
3911 DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
3914 s = socket(AF(addr), SOCK_DGRAM, 0);
3915 if (INVALID_SOCKET == s)
3919 * If we are looking for broadcast interface we need to set this
3920 * socket to allow broadcast
3924 if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET,
3933 rtn = connect(s, &addr->sa, SOCKLEN(addr));
3934 if (SOCKET_ERROR == rtn) {
3939 sockaddrlen = sizeof(saddr);
3940 rtn = getsockname(s, &saddr.sa, &sockaddrlen);
3942 if (SOCKET_ERROR == rtn)
3945 DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n",
3946 stoa(addr), stoa(&saddr)));
3948 iface = getinterface(&saddr, flags);
3951 * if we didn't find an exact match on saddr, find the closest
3952 * available local address. This handles the case of the
3953 * address suggested by the kernel being excluded by nic rules
3954 * or the user's -I and -L options to ntpd.
3955 * See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683
3956 * for more background.
3958 if (NULL == iface || iface->ignore_packets)
3959 iface = findclosestinterface(&saddr,
3960 flags | INT_LOOPBACK);
3962 /* Don't use an interface which will ignore replies */
3963 if (iface != NULL && iface->ignore_packets)
3971 * findclosestinterface
3973 * If there are -I/--interface or -L/novirtualips command-line options,
3974 * or "nic" or "interface" rules in ntp.conf, findlocalinterface() may
3975 * find the kernel's preferred local address for a given peer address is
3976 * administratively unavailable to ntpd, and punt to this routine's more
3979 * Find the numerically closest local address to the one connect()
3980 * suggested. This matches an address on the same subnet first, as
3981 * needed by Bug 1184, and provides a consistent choice if there are
3982 * multiple feasible local addresses, regardless of the order ntpd
3986 findclosestinterface(
3993 sockaddr_u addr_dist;
3994 sockaddr_u min_dist;
3996 ZERO_SOCK(&min_dist);
3999 for (ep = ep_list; ep != NULL; ep = ep->elink) {
4000 if (ep->ignore_packets ||
4001 AF(addr) != ep->family ||
4005 calc_addr_distance(&addr_dist, addr, &ep->sin);
4006 if (NULL == winner ||
4007 -1 == cmp_addr_distance(&addr_dist, &min_dist)) {
4008 min_dist = addr_dist;
4013 DPRINTF(4, ("findclosestinterface(%s) failed\n",
4016 DPRINTF(4, ("findclosestinterface(%s) -> %s\n",
4017 stoa(addr), stoa(&winner->sin)));
4024 * calc_addr_distance - calculate the distance between two addresses,
4025 * the absolute value of the difference between
4026 * the addresses numerically, stored as an address.
4031 const sockaddr_u * a1,
4032 const sockaddr_u * a2
4042 REQUIRE(AF(a1) == AF(a2));
4047 /* v4 can be done a bit simpler */
4051 v4dist = (a1val > a2val)
4054 SET_ADDR4(dist, v4dist);
4059 found_greater = FALSE;
4060 a1_greater = FALSE; /* suppress pot. uninit. warning */
4061 for (i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
4062 if (!found_greater &&
4063 NSRCADR6(a1)[i] != NSRCADR6(a2)[i]) {
4064 found_greater = TRUE;
4065 a1_greater = (NSRCADR6(a1)[i] > NSRCADR6(a2)[i]);
4067 if (!found_greater) {
4068 NSRCADR6(dist)[i] = 0;
4071 NSRCADR6(dist)[i] = NSRCADR6(a1)[i] -
4074 NSRCADR6(dist)[i] = NSRCADR6(a2)[i] -
4082 * cmp_addr_distance - compare two address distances, returning -1, 0,
4083 * 1 to indicate their relationship.
4087 const sockaddr_u * d1,
4088 const sockaddr_u * d2
4093 REQUIRE(AF(d1) == AF(d2));
4096 if (SRCADR(d1) < SRCADR(d2))
4098 else if (SRCADR(d1) == SRCADR(d2))
4104 for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
4105 if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i])
4107 else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i])
4117 * fetch an interface structure the matches the
4118 * address and has the given flags NOT set
4128 iface = find_addr_in_list(addr);
4130 if (iface != NULL && (iface->flags & flags))
4138 * findbcastinter - find broadcast interface corresponding to address
4148 #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
4149 DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
4152 iface = findlocalinterface(addr, INT_LOOPBACK | INT_WILDCARD,
4154 if (iface != NULL) {
4155 DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n",
4156 iface->ifnum, iface->name));
4161 * plan B - try to find something reasonable in our lists in
4162 * case kernel lookup doesn't help
4164 for (iface = ep_list; iface != NULL; iface = iface->elink) {
4165 if (iface->flags & INT_WILDCARD)
4168 /* Don't bother with ignored interfaces */
4169 if (iface->ignore_packets)
4173 * First look if this is the correct family
4175 if(AF(&iface->sin) != AF(addr))
4178 /* Skip the loopback addresses */
4179 if (iface->flags & INT_LOOPBACK)
4183 * If we are looking to match a multicast address and
4184 * this interface is one...
4186 if (addr_ismulticast(addr)
4187 && (iface->flags & INT_MULTICAST)) {
4188 #ifdef INCLUDE_IPV6_SUPPORT
4190 * ...it is the winner unless we're looking for
4191 * an interface to use for link-local multicast
4192 * and its address is not link-local.
4195 && IN6_IS_ADDR_MC_LINKLOCAL(PSOCK_ADDR6(addr))
4196 && !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin)))
4203 * We match only those interfaces marked as
4204 * broadcastable and either the explicit broadcast
4205 * address or the network portion of the IP address.
4208 if (IS_IPV4(addr)) {
4209 if (SOCK_EQ(&iface->bcast, addr))
4212 if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask))
4213 == (NSRCADR(addr) & NSRCADR(&iface->mask)))
4216 #ifdef INCLUDE_IPV6_SUPPORT
4217 else if (IS_IPV6(addr)) {
4218 if (SOCK_EQ(&iface->bcast, addr))
4221 if (SOCK_EQ(netof(&iface->sin), netof(addr)))
4226 #endif /* SIOCGIFCONF */
4227 if (NULL == iface) {
4228 DPRINTF(4, ("No bcast interface found for %s\n",
4230 iface = ANY_INTERFACE_CHOOSE(addr);
4232 DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n",
4233 iface->ifnum, iface->name));
4241 * io_clr_stats - clear I/O module statistics
4246 packets_dropped = 0;
4247 packets_ignored = 0;
4248 packets_received = 0;
4250 packets_notsent = 0;
4254 io_timereset = current_time;
4260 * io_addclock - add a reference clock to the list and arrange that we
4261 * get SIGIO interrupts from it.
4265 struct refclockio *rio
4271 * Stuff the I/O structure in the list and mark the descriptor
4272 * in use. There is a harmless (I hope) race condition here.
4276 # ifdef HAVE_SIGNALED_IO
4277 if (init_clock_sig(rio)) {
4281 # elif defined(HAVE_IO_COMPLETION_PORT)
4282 if (io_completion_port_add_clock_io(rio)) {
4291 LINK_SLIST(refio, rio, next);
4296 add_fd_to_list(rio->fd, FD_TYPE_FILE);
4304 * io_closeclock - close the clock in the I/O structure given
4308 struct refclockio *rio
4311 struct refclockio *unlinked;
4316 * Remove structure from the list
4318 rio->active = FALSE;
4319 UNLINK_SLIST(unlinked, refio, rio, next, struct refclockio);
4320 if (NULL != unlinked) {
4321 purge_recv_buffers_for_fd(rio->fd);
4323 * Close the descriptor.
4325 close_and_delete_fd_from_list(rio->fd);
4331 #endif /* REFCLOCK */
4335 * On NT a SOCKET is an unsigned int so we cannot possibly keep it in
4336 * an array. So we use one of the ISC_LIST functions to hold the
4337 * socket value and use that when we want to enumerate it.
4339 * This routine is called by the forked intres child process to close
4340 * all open sockets. On Windows there's no need as intres runs in
4341 * the same process as a thread.
4352 * In the child process we do not maintain activefds and
4353 * maxactivefd. Zeroing maxactivefd disables code which
4354 * maintains it in close_and_delete_fd_from_list().
4358 while (fd_list != NULL)
4359 close_and_delete_fd_from_list(fd_list->fd);
4363 #endif /* !SYS_WINNT */
4367 * Add and delete functions for the list of open sockets
4375 vsock_t *lsock = emalloc(sizeof(*lsock));
4380 LINK_SLIST(fd_list, lsock, link);
4381 maintain_activefds(fd, 0);
4386 close_and_delete_fd_from_list(
4392 UNLINK_EXPR_SLIST(lsock, fd_list, fd ==
4393 UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t);
4398 switch (lsock->type) {
4400 case FD_TYPE_SOCKET:
4401 closesocket(lsock->fd);
4405 closeserial((int)lsock->fd);
4410 "internal error - illegal descriptor type %d - EXITING",
4417 * remove from activefds
4419 maintain_activefds(fd, 1);
4432 if (find_addr_in_list(addr) == NULL) {
4434 /* not there yet - add to list */
4435 laddr = emalloc(sizeof(*laddr));
4436 laddr->addr = *addr;
4439 LINK_SLIST(remoteaddr_list, laddr, link);
4441 DPRINTF(4, ("Added addr %s to list of addresses\n",
4445 DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
4452 delete_addr_from_list(
4456 remaddr_t *unlinked;
4458 UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr,
4459 &(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t);
4461 if (unlinked != NULL) {
4462 DPRINTF(4, ("Deleted addr %s from list of addresses\n",
4470 delete_interface_from_list(
4474 remaddr_t *unlinked;
4477 UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface ==
4478 UNLINK_EXPR_SLIST_CURRENT()->ep, link,
4481 if (unlinked == NULL)
4483 DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
4484 stoa(&unlinked->addr), iface->ifnum,
4491 static struct interface *
4498 DPRINTF(4, ("Searching for addr %s in list of addresses - ",
4501 for (entry = remoteaddr_list;
4503 entry = entry->link)
4504 if (SOCK_EQ(&entry->addr, addr)) {
4505 DPRINTF(4, ("FOUND\n"));
4509 DPRINTF(4, ("NOT FOUND\n"));
4515 * Find the given address with the all given flags set in the list
4518 find_flagged_addr_in_list(
4525 DPRINTF(4, ("Finding addr %s with flags %d in list: ",
4526 stoa(addr), flags));
4528 for (entry = remoteaddr_list;
4530 entry = entry->link)
4532 if (SOCK_EQ(&entry->addr, addr)
4533 && (entry->ep->flags & flags) == flags) {
4535 DPRINTF(4, ("FOUND\n"));
4539 DPRINTF(4, ("NOT FOUND\n"));
4555 #ifdef HAS_ROUTING_SOCKET
4556 # ifndef UPDATE_GRACE
4557 # define UPDATE_GRACE 2 /* wait UPDATE_GRACE seconds before scanning */
4561 process_routing_msgs(struct asyncio_reader *reader)
4565 #ifdef HAVE_RTNETLINK
4566 struct nlmsghdr *nh;
4568 struct rt_msghdr rtm;
4572 if (disable_dynamic_updates) {
4574 * discard ourselves if we are not needed any more
4575 * usually happens when running unprivileged
4577 remove_asyncio_reader(reader);
4578 delete_asyncio_reader(reader);
4582 cnt = read(reader->fd, buffer, sizeof(buffer));
4585 if (errno == ENOBUFS) {
4587 "routing socket reports: %m");
4590 "routing socket reports: %m - disabling");
4591 remove_asyncio_reader(reader);
4592 delete_asyncio_reader(reader);
4598 * process routing message
4600 #ifdef HAVE_RTNETLINK
4601 for (nh = UA_PTR(struct nlmsghdr, buffer);
4603 nh = NLMSG_NEXT(nh, cnt)) {
4604 msg_type = nh->nlmsg_type;
4607 (p + sizeof(struct rt_msghdr)) <= (buffer + cnt);
4608 p += rtm.rtm_msglen) {
4609 memcpy(&rtm, p, sizeof(rtm));
4610 if (rtm.rtm_version != RTM_VERSION) {
4612 "version mismatch (got %d - expected %d) on routing socket - disabling",
4613 rtm.rtm_version, RTM_VERSION);
4615 remove_asyncio_reader(reader);
4616 delete_asyncio_reader(reader);
4619 msg_type = rtm.rtm_type;
4646 #ifdef RTM_IFANNOUNCE
4647 case RTM_IFANNOUNCE:
4662 * we are keen on new and deleted addresses and
4663 * if an interface goes up and down or routing
4666 DPRINTF(3, ("routing message op = %d: scheduling interface update\n",
4668 timer_interfacetimeout(current_time + UPDATE_GRACE);
4670 #ifdef HAVE_RTNETLINK
4672 /* end of multipart message */
4677 * the rest doesn't bother us.
4679 DPRINTF(4, ("routing message op = %d: ignored\n",
4687 * set up routing notifications
4690 init_async_notifications()
4692 struct asyncio_reader *reader;
4693 #ifdef HAVE_RTNETLINK
4694 int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
4695 struct sockaddr_nl sa;
4697 int fd = socket(PF_ROUTE, SOCK_RAW, 0);
4701 "unable to open routing socket (%m) - using polled interface update");
4706 #ifdef HAVE_RTNETLINK
4708 sa.nl_family = PF_NETLINK;
4709 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR
4710 | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE
4711 | RTMGRP_IPV4_MROUTE | RTMGRP_IPV6_ROUTE
4712 | RTMGRP_IPV6_MROUTE;
4713 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
4715 "bind failed on routing socket (%m) - using polled interface update");
4719 make_socket_nonblocking(fd);
4720 #if defined(HAVE_SIGNALED_IO)
4721 init_socket_sig(fd);
4722 #endif /* HAVE_SIGNALED_IO */
4724 reader = new_asyncio_reader();
4727 reader->receiver = process_routing_msgs;
4729 add_asyncio_reader(reader, FD_TYPE_SOCKET);
4731 "Listening on routing socket on fd #%d for interface updates",
4735 /* HAS_ROUTING_SOCKET not defined */
4737 init_async_notifications(void)