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>
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 void set_reuseaddr (int);
283 static isc_boolean_t socket_broadcast_enable (struct interface *, SOCKET, sockaddr_u *);
285 #if !defined(HAVE_IO_COMPLETION_PORT) && !defined(HAVE_SIGNALED_IO)
286 static char * fdbits (int, const fd_set *);
288 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
289 static isc_boolean_t socket_broadcast_disable (struct interface *, sockaddr_u *);
292 typedef struct remaddr remaddr_t;
300 remaddr_t * remoteaddr_list;
301 endpt * ep_list; /* complete endpt list */
302 endpt * mc4_list; /* IPv4 mcast-capable unicast endpts */
303 endpt * mc6_list; /* IPv6 mcast-capable unicast endpts */
305 static endpt * wildipv4;
306 static endpt * wildipv6;
309 int accept_wildcard_if_for_winnt;
311 const int accept_wildcard_if_for_winnt = FALSE;
314 static void add_fd_to_list (SOCKET, enum desc_type);
315 static endpt * find_addr_in_list (sockaddr_u *);
316 static endpt * find_flagged_addr_in_list(sockaddr_u *, u_int32);
317 static void delete_addr_from_list (sockaddr_u *);
318 static void delete_interface_from_list(endpt *);
319 static void close_and_delete_fd_from_list(SOCKET);
320 static void add_addr_to_list (sockaddr_u *, endpt *);
321 static void create_wildcards (u_short);
322 static endpt * findlocalinterface (sockaddr_u *, int, int);
323 static endpt * findclosestinterface (sockaddr_u *, int);
325 static const char * action_text (nic_rule_action);
327 static nic_rule_action interface_action(char *, sockaddr_u *, u_int32);
328 static void convert_isc_if (isc_interface_t *,
330 static void calc_addr_distance(sockaddr_u *,
333 static int cmp_addr_distance(const sockaddr_u *,
337 * Routines to read the ntp packets
339 #if !defined(HAVE_IO_COMPLETION_PORT)
340 static inline int read_network_packet (SOCKET, struct interface *, l_fp);
341 static void ntpd_addremove_io_fd (int, int, int);
342 static void input_handler_scan (const l_fp*, const fd_set*);
343 static int/*BOOL*/ sanitize_fdset (int errc);
345 static inline int read_refclock_packet (SOCKET, struct refclockio *, l_fp);
347 #ifdef HAVE_SIGNALED_IO
348 static void input_handler (l_fp*);
353 #ifndef HAVE_IO_COMPLETION_PORT
362 if (fd < 0 || fd >= FD_SETSIZE) {
364 "Too many sockets in use, FD_SETSIZE %d exceeded by fd %d",
370 FD_SET(fd, &activefds);
371 maxactivefd = max(fd, maxactivefd);
373 FD_CLR(fd, &activefds);
374 if (maxactivefd && fd == maxactivefd) {
375 for (i = maxactivefd - 1; i >= 0; i--)
376 if (FD_ISSET(i, &activefds)) {
380 INSIST(fd != maxactivefd);
384 #endif /* !HAVE_IO_COMPLETION_PORT */
389 * collect timing information for various processing
390 * paths. currently we only pass them on to the file
391 * for later processing. this could also do histogram
392 * based analysis in other to reduce the load (and skew)
393 * dur to the file output
396 collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
400 snprintf(buf, sizeof(buf), "%s %d %s %s",
402 ? ((rb->dstadr != NULL)
403 ? stoa(&rb->recv_srcadr)
406 count, lfptoa(dts, 9), tag);
407 record_timing_stats(buf);
412 * About dynamic interfaces, sockets, reception and more...
414 * the code solves following tasks:
416 * - keep a current list of active interfaces in order
417 * to bind to to the interface address on NTP_PORT so that
418 * all wild and specific bindings for NTP_PORT are taken by ntpd
419 * to avoid other daemons messing with the time or sockets.
420 * - all interfaces keep a list of peers that are referencing
421 * the interface in order to quickly re-assign the peers to
422 * new interface in case an interface is deleted (=> gone from system or
424 * - have a preconfigured socket ready with the right local address
425 * for transmission and reception
426 * - have an address list for all destination addresses used within ntpd
427 * to find the "right" preconfigured socket.
428 * - facilitate updating the internal interface list with respect to
429 * the current kernel state
433 * - mapping of multicast addresses to the interface affected is not always
434 * one to one - especially on hosts with multiple interfaces
435 * the code here currently allocates a separate interface entry for those
436 * multicast addresses
437 * iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF)
438 * in case of failure the multicast address is bound to an existing interface.
439 * - on some systems it is perfectly legal to assign the same address to
440 * multiple interfaces. Therefore this code does not keep a list of interfaces
441 * but a list of interfaces that represent a unique address as determined by the kernel
442 * by the procedure in findlocalinterface. Thus it is perfectly legal to see only
443 * one representative of a group of real interfaces if they share the same address.
445 * Frank Kardel 20050910
449 * init_io - initialize I/O module.
454 /* Init buffer free list and stat counters */
455 init_recvbuff(RECV_INIT);
456 /* update interface every 5 minutes as default */
457 interface_interval = 300;
460 addremove_io_fd = &ntpd_addremove_io_fd;
463 #if defined(SYS_WINNT)
464 init_io_completion_port();
465 #elif defined(HAVE_SIGNALED_IO)
466 (void) set_signal(input_handler);
472 ntpd_addremove_io_fd(
480 #ifdef HAVE_SIGNALED_IO
483 #endif /* not HAVE_SIGNALED_IO */
485 maintain_activefds(fd, remove_it);
490 * io_open_sockets - call socket creation routine
493 io_open_sockets(void)
495 static int already_opened;
497 if (already_opened || HAVE_OPT( SAVECONFIGQUIT ))
506 create_sockets(NTP_PORT);
509 init_async_notifications();
511 DPRINTF(3, ("io_open_sockets: maxactivefd %d\n", maxactivefd));
517 * function to dump the contents of the interface structure
518 * for debugging use only.
519 * We face a dilemma here -- sockets are FDs under POSIX and
520 * actually HANDLES under Windows. So we use '%lld' as format
521 * and cast the value to 'long long'; this should not hurt
522 * with UNIX-like systems and does not truncate values on Win64.
525 interface_dump(const endpt *itf)
527 printf("Dumping interface: %p\n", itf);
528 printf("fd = %lld\n", (long long)itf->fd);
529 printf("bfd = %lld\n", (long long)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=%lld, bfd=%lld, name=%s, flags=0x%x, ifindex=%u, sin=%s",
580 (long long)iface->fd,
581 (long long)iface->bfd,
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;
781 # ifdef HAVE_IO_COMPLETION_PORT
782 if (!io_completion_port_add_interface(iface)) {
783 msyslog(LOG_EMERG, "cannot register interface with IO engine -- will exit now");
792 * return interface storage into free memory pool
799 # ifdef HAVE_IO_COMPLETION_PORT
800 io_completion_port_remove_interface(ep);
807 * link interface into list of known interfaces
814 endpt ** pmclisthead;
822 int ep_univ_iid; /* iface ID from MAC address */
823 int scan_univ_iid; /* see RFC 4291 */
824 int ep_privacy; /* random local iface ID */
825 int scan_privacy; /* see RFC 4941 */
828 /* Calculate the refid */
829 ep->addr_refid = addr2refid(&ep->sin);
830 /* link at tail so ntpdc -c ifstats index increases each row */
831 LINK_TAIL_SLIST(ep_list, ep, elink, endpt);
834 /* the rest is for enabled multicast-capable addresses only */
835 if (ep->ignore_packets || !(INT_MULTICAST & ep->flags) ||
836 INT_LOOPBACK & ep->flags)
838 # ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
839 if (AF_INET6 == ep->family)
842 pmclisthead = (AF_INET == ep->family)
846 if (AF_INET6 == ep->family) {
848 IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&ep->sin)) ||
849 IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(&ep->sin));
850 ep_univ_iid = IS_IID_UNIV(&ep->sin);
851 ep_privacy = !!(INT_PRIVACY & ep->flags);
857 DPRINTF(4, ("add_interface mcast-capable %s%s%s%s\n",
859 (ep_local) ? " link/scope-local" : "",
860 (ep_univ_iid) ? " univ-IID" : "",
861 (ep_privacy) ? " privacy" : ""));
863 * If we have multiple local addresses on the same network
864 * interface, and some are link- or site-local, do not multicast
865 * out from the link-/site-local addresses by default, to avoid
866 * duplicate manycastclient associations between v6 peers using
867 * link-local and global addresses. link-local can still be
868 * chosen using "nic ignore myv6globalprefix::/64".
869 * Similarly, if we have multiple global addresses from the same
870 * prefix on the same network interface, multicast from one,
871 * preferring EUI-64, then static, then least RFC 4941 privacy
874 for (scan = *pmclisthead; scan != NULL; scan = scan_next) {
875 scan_next = scan->mclink;
876 if (ep->family != scan->family)
878 if (strcmp(ep->name, scan->name))
880 same_subnet = addr_samesubnet(&ep->sin, &ep->mask,
881 &scan->sin, &scan->mask);
882 if (AF_INET6 == ep->family) {
885 IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(addr)) ||
886 IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(addr));
887 scan_univ_iid = IS_IID_UNIV(addr);
888 scan_privacy = !!(INT_PRIVACY & scan->flags);
891 scan_univ_iid = FALSE;
892 scan_privacy = FALSE;
894 DPRINTF(4, ("add_interface mcast-capable scan %s%s%s%s\n",
896 (scan_local) ? " link/scope-local" : "",
897 (scan_univ_iid) ? " univ-IID" : "",
898 (scan_privacy) ? " privacy" : ""));
899 if ((ep_local && !scan_local) || (same_subnet &&
900 ((ep_privacy && !scan_privacy) ||
901 (!ep_univ_iid && scan_univ_iid)))) {
902 DPRINTF(4, ("did not add %s to %s of IPv6 multicast-capable list which already has %s\n",
910 if ((scan_local && !ep_local) || (same_subnet &&
911 ((scan_privacy && !ep_privacy) ||
912 (!scan_univ_iid && ep_univ_iid)))) {
913 UNLINK_SLIST(unlinked, *pmclisthead,
914 scan, mclink, endpt);
915 DPRINTF(4, ("%s %s from IPv6 multicast-capable list to add %s\n",
919 stoa(&scan->sin), stoa(&ep->sin)));
923 * Add link/site local at the tail of the multicast-
924 * capable unicast interfaces list, so that ntpd will
925 * send from global addresses before link-/site-local
929 LINK_TAIL_SLIST(*pmclisthead, ep, mclink, endpt);
931 LINK_SLIST(*pmclisthead, ep, mclink);
932 DPRINTF(4, ("added %s to %s of IPv%s multicast-capable unicast local address list\n",
937 (AF_INET == ep->family)
941 if (INVALID_SOCKET == ep->fd)
945 * select the local address from which to send to multicast.
947 switch (AF(&ep->sin)) {
950 rc = setsockopt(ep->fd, IPPROTO_IP,
952 (void *)&NSRCADR(&ep->sin),
953 sizeof(NSRCADR(&ep->sin)));
956 "setsockopt IP_MULTICAST_IF %s fails: %m",
960 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
962 rc = setsockopt(ep->fd, IPPROTO_IPV6,
964 (void *)&ep->ifindex,
965 sizeof(ep->ifindex));
966 /* do not complain if bound addr scope is ifindex */
967 if (rc && ep->ifindex != SCOPE(&ep->sin))
969 "setsockopt IPV6_MULTICAST_IF %u for %s fails: %m",
970 ep->ifindex, stoa(&ep->sin));
979 * remove interface from known interface list and clean up
980 * associated resources
988 endpt ** pmclisthead;
991 UNLINK_SLIST(unlinked, ep_list, ep, elink, endpt);
992 if (!ep->ignore_packets && INT_MULTICAST & ep->flags) {
993 pmclisthead = (AF_INET == ep->family)
996 UNLINK_SLIST(unlinked, *pmclisthead, ep, mclink, endpt);
997 DPRINTF(4, ("%s %s IPv%s multicast-capable unicast local address list\n",
1002 (AF_INET == ep->family)
1006 delete_interface_from_list(ep);
1008 if (ep->fd != INVALID_SOCKET) {
1010 "Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
1018 current_time - ep->starttime);
1019 # ifdef HAVE_IO_COMPLETION_PORT
1020 io_completion_port_remove_socket(ep->fd, ep);
1022 close_and_delete_fd_from_list(ep->fd);
1023 ep->fd = INVALID_SOCKET;
1026 if (ep->bfd != INVALID_SOCKET) {
1028 "stop listening for broadcasts to %s on interface #%d %s",
1029 stoa(&ep->bcast), ep->ifnum, ep->name);
1030 # ifdef HAVE_IO_COMPLETION_PORT
1031 io_completion_port_remove_socket(ep->bfd, ep);
1033 close_and_delete_fd_from_list(ep->bfd);
1034 ep->bfd = INVALID_SOCKET;
1036 # ifdef HAVE_IO_COMPLETION_PORT
1037 io_completion_port_remove_interface(ep);
1041 mon_clearinterface(ep);
1043 /* remove restrict interface entry */
1044 SET_HOSTMASK(&resmask, AF(&ep->sin));
1045 hack_restrict(RESTRICT_REMOVEIF, &ep->sin, &resmask,
1046 RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
1055 msyslog(LOG_INFO, "%s on %d %s %s",
1056 (ep->ignore_packets)
1058 : "Listen normally",
1071 #ifdef INCLUDE_IPV6_SUPPORT
1074 sockaddr_u wildaddr;
1075 nic_rule_action action;
1076 struct interface * wildif;
1079 * silence "potentially uninitialized" warnings from VC9
1080 * failing to follow the logic. Ideally action could remain
1081 * uninitialized, and the memset be the first statement under
1082 * the first if (v4wild).
1084 action = ACTION_LISTEN;
1087 #ifdef INCLUDE_IPV6_SUPPORT
1089 * create pseudo-interface with wildcard IPv6 address
1091 v6wild = ipv6_works;
1093 /* set wildaddr to the v6 wildcard address :: */
1095 AF(&wildaddr) = AF_INET6;
1096 SET_ADDR6N(&wildaddr, in6addr_any);
1097 SET_PORT(&wildaddr, port);
1098 SET_SCOPE(&wildaddr, 0);
1100 /* check for interface/nic rules affecting the wildcard */
1101 action = interface_action(NULL, &wildaddr, 0);
1102 v6wild = (ACTION_IGNORE != action);
1105 wildif = new_interface(NULL);
1107 strlcpy(wildif->name, "v6wildcard", sizeof(wildif->name));
1108 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1109 wildif->family = AF_INET6;
1110 AF(&wildif->mask) = AF_INET6;
1111 SET_ONESMASK(&wildif->mask);
1113 wildif->flags = INT_UP | INT_WILDCARD;
1114 wildif->ignore_packets = (ACTION_DROP == action);
1116 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1118 if (wildif->fd != INVALID_SOCKET) {
1120 any6_interface = wildif;
1121 add_addr_to_list(&wildif->sin, wildif);
1122 add_interface(wildif);
1123 log_listen_address(wildif);
1126 "unable to bind to wildcard address %s - another process may be running - EXITING",
1127 stoa(&wildif->sin));
1130 DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1135 * create pseudo-interface with wildcard IPv4 address
1137 v4wild = ipv4_works;
1139 /* set wildaddr to the v4 wildcard address 0.0.0.0 */
1140 AF(&wildaddr) = AF_INET;
1141 SET_ADDR4N(&wildaddr, INADDR_ANY);
1142 SET_PORT(&wildaddr, port);
1144 /* check for interface/nic rules affecting the wildcard */
1145 action = interface_action(NULL, &wildaddr, 0);
1146 v4wild = (ACTION_IGNORE != action);
1149 wildif = new_interface(NULL);
1151 strlcpy(wildif->name, "v4wildcard", sizeof(wildif->name));
1152 memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1153 wildif->family = AF_INET;
1154 AF(&wildif->mask) = AF_INET;
1155 SET_ONESMASK(&wildif->mask);
1157 wildif->flags = INT_BROADCAST | INT_UP | INT_WILDCARD;
1158 wildif->ignore_packets = (ACTION_DROP == action);
1161 * enable multicast reception on the broadcast socket
1163 AF(&wildif->bcast) = AF_INET;
1164 SET_ADDR4N(&wildif->bcast, INADDR_ANY);
1165 SET_PORT(&wildif->bcast, port);
1167 wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1169 if (wildif->fd != INVALID_SOCKET) {
1171 any_interface = wildif;
1173 add_addr_to_list(&wildif->sin, wildif);
1174 add_interface(wildif);
1175 log_listen_address(wildif);
1178 "unable to bind to wildcard address %s - another process may be running - EXITING",
1179 stoa(&wildif->sin));
1182 DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1188 * add_nic_rule() -- insert a rule entry at the head of nic_rule_list.
1192 nic_rule_match match_type,
1193 const char * if_name, /* interface name or numeric address */
1195 nic_rule_action action
1199 isc_boolean_t is_ip;
1201 rule = emalloc_zero(sizeof(*rule));
1202 rule->match_type = match_type;
1203 rule->prefixlen = prefixlen;
1204 rule->action = action;
1206 if (MATCH_IFNAME == match_type) {
1207 REQUIRE(NULL != if_name);
1208 rule->if_name = estrdup(if_name);
1209 } else if (MATCH_IFADDR == match_type) {
1210 REQUIRE(NULL != if_name);
1211 /* set rule->addr */
1212 is_ip = is_ip_address(if_name, AF_UNSPEC, &rule->addr);
1215 REQUIRE(NULL == if_name);
1217 LINK_SLIST(nic_rule_list, rule, next);
1224 nic_rule_action action
1232 t = "ERROR"; /* quiet uninit warning */
1233 DPRINTF(1, ("fatal: unknown nic_rule_action %d\n",
1256 static nic_rule_action
1259 sockaddr_u * if_addr,
1267 DPRINTF(4, ("interface_action: interface %s ",
1268 (if_name != NULL) ? if_name : "wildcard"));
1270 iswildcard = is_wildcard_addr(if_addr);
1271 isloopback = !!(INT_LOOPBACK & if_flags);
1274 * Find any matching NIC rule from --interface / -I or ntp.conf
1275 * interface/nic rules.
1277 for (rule = nic_rule_list; rule != NULL; rule = rule->next) {
1279 switch (rule->match_type) {
1282 /* loopback and wildcard excluded from "all" */
1283 if (isloopback || iswildcard)
1285 DPRINTF(4, ("nic all %s\n",
1286 action_text(rule->action)));
1287 return rule->action;
1290 if (IS_IPV4(if_addr)) {
1291 DPRINTF(4, ("nic ipv4 %s\n",
1292 action_text(rule->action)));
1293 return rule->action;
1298 if (IS_IPV6(if_addr)) {
1299 DPRINTF(4, ("nic ipv6 %s\n",
1300 action_text(rule->action)));
1301 return rule->action;
1305 case MATCH_WILDCARD:
1307 DPRINTF(4, ("nic wildcard %s\n",
1308 action_text(rule->action)));
1309 return rule->action;
1314 if (rule->prefixlen != -1) {
1315 if (addr_eqprefix(if_addr, &rule->addr,
1318 DPRINTF(4, ("subnet address match - %s\n",
1319 action_text(rule->action)));
1320 return rule->action;
1323 if (SOCK_EQ(if_addr, &rule->addr)) {
1325 DPRINTF(4, ("address match - %s\n",
1326 action_text(rule->action)));
1327 return rule->action;
1333 #if defined(HAVE_FNMATCH) && defined(FNM_CASEFOLD)
1334 && !fnmatch(rule->if_name, if_name, FNM_CASEFOLD)
1336 && !strcasecmp(if_name, rule->if_name)
1340 DPRINTF(4, ("interface name match - %s\n",
1341 action_text(rule->action)));
1342 return rule->action;
1349 * Unless explicitly disabled such as with "nic ignore ::1"
1350 * listen on loopback addresses. Since ntpq and ntpdc query
1351 * "localhost" by default, which typically resolves to ::1 and
1352 * 127.0.0.1, it's useful to default to listening on both.
1355 DPRINTF(4, ("default loopback listen\n"));
1356 return ACTION_LISTEN;
1360 * Treat wildcard addresses specially. If there is no explicit
1361 * "nic ... wildcard" or "nic ... 0.0.0.0" or "nic ... ::" rule
1365 DPRINTF(4, ("default wildcard drop\n"));
1370 * Check for "virtual IP" (colon in the interface name) after
1371 * the rules so that "ntpd --interface eth0:1 -novirtualips"
1372 * does indeed listen on eth0:1's addresses.
1374 if (!listen_to_virtual_ips && if_name != NULL
1375 && (strchr(if_name, ':') != NULL)) {
1377 DPRINTF(4, ("virtual ip - ignore\n"));
1378 return ACTION_IGNORE;
1382 * If there are no --interface/-I command-line options and no
1383 * interface/nic rules in ntp.conf, the default action is to
1384 * listen. In the presence of rules from either, the default
1385 * is to ignore. This implements ntpd's traditional listen-
1386 * every default with no interface listen configuration, and
1387 * ensures a single -I eth0 or "nic listen eth0" means do not
1388 * listen on any other addresses.
1390 if (NULL == nic_rule_list) {
1391 DPRINTF(4, ("default listen\n"));
1392 return ACTION_LISTEN;
1395 DPRINTF(4, ("implicit ignore\n"));
1396 return ACTION_IGNORE;
1402 isc_interface_t *isc_if,
1407 const u_char v6loop[16] = {0, 0, 0, 0, 0, 0, 0, 0,
1408 0, 0, 0, 0, 0, 0, 0, 1};
1410 strlcpy(itf->name, isc_if->name, sizeof(itf->name));
1411 itf->ifindex = isc_if->ifindex;
1412 itf->family = (u_short)isc_if->af;
1413 AF(&itf->sin) = itf->family;
1414 AF(&itf->mask) = itf->family;
1415 AF(&itf->bcast) = itf->family;
1416 SET_PORT(&itf->sin, port);
1417 SET_PORT(&itf->mask, port);
1418 SET_PORT(&itf->bcast, port);
1420 if (IS_IPV4(&itf->sin)) {
1421 NSRCADR(&itf->sin) = isc_if->address.type.in.s_addr;
1422 NSRCADR(&itf->mask) = isc_if->netmask.type.in.s_addr;
1424 if (isc_if->flags & INTERFACE_F_BROADCAST) {
1425 itf->flags |= INT_BROADCAST;
1426 NSRCADR(&itf->bcast) =
1427 isc_if->broadcast.type.in.s_addr;
1430 #ifdef INCLUDE_IPV6_SUPPORT
1431 else if (IS_IPV6(&itf->sin)) {
1432 SET_ADDR6N(&itf->sin, isc_if->address.type.in6);
1433 SET_ADDR6N(&itf->mask, isc_if->netmask.type.in6);
1435 SET_SCOPE(&itf->sin, isc_if->address.zone);
1437 #endif /* INCLUDE_IPV6_SUPPORT */
1440 /* Process the rest of the flags */
1443 ((INTERFACE_F_UP & isc_if->flags)
1445 | ((INTERFACE_F_LOOPBACK & isc_if->flags)
1447 | ((INTERFACE_F_POINTTOPOINT & isc_if->flags)
1449 | ((INTERFACE_F_MULTICAST & isc_if->flags)
1450 ? INT_MULTICAST : 0)
1451 | ((INTERFACE_F_PRIVACY & isc_if->flags)
1456 * Clear the loopback flag if the address is not localhost.
1457 * http://bugs.ntp.org/1683
1459 if (INT_LOOPBACK & itf->flags) {
1460 if (AF_INET == itf->family) {
1461 if (127 != (SRCADR(&itf->sin) >> 24))
1462 itf->flags &= ~INT_LOOPBACK;
1464 if (memcmp(v6loop, NSRCADR6(&itf->sin),
1465 sizeof(NSRCADR6(&itf->sin))))
1466 itf->flags &= ~INT_LOOPBACK;
1475 * some OSes have been observed to keep
1476 * cached routes even when more specific routes
1478 * this can be mitigated by re-binding
1483 struct interface * interface
1486 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
1487 if (interface->fd != INVALID_SOCKET) {
1488 int bcast = (interface->flags & INT_BCASTXMIT) != 0;
1489 /* as we forcibly close() the socket remove the
1490 broadcast permission indication */
1492 socket_broadcast_disable(interface, &interface->sin);
1494 close_and_delete_fd_from_list(interface->fd);
1496 /* create new socket picking up a new first hop binding
1497 at connect() time */
1498 interface->fd = open_socket(&interface->sin,
1499 bcast, 0, interface);
1501 * reset TTL indication so TTL is is set again
1504 interface->last_ttl = 0;
1505 return (interface->fd != INVALID_SOCKET);
1507 return 0; /* invalid sockets are not refreshable */
1508 #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1509 return (interface->fd != INVALID_SOCKET);
1510 #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1514 * interface_update - externally callable update function
1518 interface_receiver_t receiver,
1521 int new_interface_found;
1523 if (disable_dynamic_updates)
1527 new_interface_found = update_interfaces(NTP_PORT, receiver, data);
1530 if (!new_interface_found)
1534 msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
1536 interrupt_worker_sleep();
1541 * sau_from_netaddr() - convert network address on-wire formats.
1542 * Convert from libisc's isc_netaddr_t to NTP's sockaddr_u
1547 const isc_netaddr_t *pna
1551 AF(psau) = (u_short)pna->family;
1552 switch (pna->family) {
1555 memcpy(&psau->sa4.sin_addr, &pna->type.in,
1556 sizeof(psau->sa4.sin_addr));
1560 memcpy(&psau->sa6.sin6_addr, &pna->type.in6,
1561 sizeof(psau->sa6.sin6_addr));
1569 const sockaddr_u *psau
1572 if (IS_IPV4(psau) && !NSRCADR(psau))
1575 #ifdef INCLUDE_IPV6_SUPPORT
1576 if (IS_IPV6(psau) && S_ADDR6_EQ(psau, &in6addr_any))
1584 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
1586 * enable/disable re-use of wildcard address socket
1594 struct interface *any;
1595 SOCKET fd = INVALID_SOCKET;
1597 any = ANY_INTERFACE_BYFAM(family);
1601 if (fd != INVALID_SOCKET) {
1602 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1603 (char *)&on, sizeof(on)))
1605 "set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m",
1608 DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n",
1613 #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
1616 static isc_boolean_t
1623 #if defined(INCLUDE_IPV6_SUPPORT) && defined(SIOCGIFAFLAG_IN6)
1624 struct in6_ifreq ifr6;
1627 if (psau->sa.sa_family != AF_INET6)
1629 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1632 memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr));
1633 strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1634 if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1639 if ((ifr6.ifr_ifru.ifru_flags6 & flags6) != 0)
1641 #endif /* INCLUDE_IPV6_SUPPORT && SIOCGIFAFLAG_IN6 */
1645 static isc_boolean_t
1651 #ifdef IN6_IFF_ANYCAST
1652 return check_flags6(psau, name, IN6_IFF_ANYCAST);
1658 static isc_boolean_t
1667 #ifdef IN6_IFF_DEPARTED
1668 flags6 |= IN6_IFF_DEPARTED;
1670 #ifdef IN6_IFF_DETACHED
1671 flags6 |= IN6_IFF_DETACHED;
1673 #ifdef IN6_IFF_TENTATIVE
1674 flags6 |= IN6_IFF_TENTATIVE;
1676 return check_flags6(psau, name, flags6) ? ISC_FALSE : ISC_TRUE;
1680 * update_interface strategy
1682 * toggle configuration phase
1685 * forall currently existing interfaces
1686 * if address is known:
1687 * drop socket - rebind again
1689 * if address is NOT known:
1690 * attempt to create a new interface entry
1693 * forall currently known non MCAST and WILDCARD interfaces
1694 * if interface does not match configuration phase (not seen in phase 1):
1695 * remove interface from known interface list
1696 * forall peers associated with this interface
1697 * disconnect peer from this interface
1700 * attempt to re-assign interfaces to peers
1707 interface_receiver_t receiver,
1711 isc_mem_t * mctx = (void *)-1;
1712 interface_info_t ifi;
1713 isc_interfaceiter_t * iter;
1714 isc_result_t result;
1715 isc_interface_t isc_if;
1716 int new_interface_found;
1717 unsigned int family;
1722 DPRINTF(3, ("update_interfaces(%d)\n", port));
1725 * phase one - scan interfaces
1726 * - create those that are not found
1727 * - update those that are found
1730 new_interface_found = FALSE;
1732 result = isc_interfaceiter_create(mctx, &iter);
1734 if (result != ISC_R_SUCCESS)
1738 * Toggle system interface scan phase to find untouched
1739 * interfaces to be deleted.
1741 sys_interphase ^= 0x1;
1743 for (result = isc_interfaceiter_first(iter);
1744 ISC_R_SUCCESS == result;
1745 result = isc_interfaceiter_next(iter)) {
1747 result = isc_interfaceiter_current(iter, &isc_if);
1749 if (result != ISC_R_SUCCESS)
1752 /* See if we have a valid family to use */
1753 family = isc_if.address.family;
1754 if (AF_INET != family && AF_INET6 != family)
1756 if (AF_INET == family && !ipv4_works)
1758 if (AF_INET6 == family && !ipv6_works)
1761 /* create prototype */
1762 init_interface(&enumep);
1764 convert_isc_if(&isc_if, &enumep, port);
1766 DPRINT_INTERFACE(4, (&enumep, "examining ", "\n"));
1769 * Check if and how we are going to use the interface.
1771 switch (interface_action(enumep.name, &enumep.sin,
1775 DPRINTF(4, ("ignoring interface %s (%s) - by nic rules\n",
1776 enumep.name, stoa(&enumep.sin)));
1780 DPRINTF(4, ("listen interface %s (%s) - by nic rules\n",
1781 enumep.name, stoa(&enumep.sin)));
1782 enumep.ignore_packets = ISC_FALSE;
1786 DPRINTF(4, ("drop on interface %s (%s) - by nic rules\n",
1787 enumep.name, stoa(&enumep.sin)));
1788 enumep.ignore_packets = ISC_TRUE;
1792 /* interfaces must be UP to be usable */
1793 if (!(enumep.flags & INT_UP)) {
1794 DPRINTF(4, ("skipping interface %s (%s) - DOWN\n",
1795 enumep.name, stoa(&enumep.sin)));
1800 * skip any interfaces UP and bound to a wildcard
1801 * address - some dhcp clients produce that in the
1804 if (is_wildcard_addr(&enumep.sin))
1807 if (is_anycast(&enumep.sin, isc_if.name))
1811 * skip any address that is an invalid state to be used
1813 if (!is_valid(&enumep.sin, isc_if.name))
1817 * map to local *address* in order to map all duplicate
1818 * interfaces to an endpt structure with the appropriate
1819 * socket. Our name space is (ip-address), NOT
1820 * (interface name, ip-address).
1822 ep = getinterface(&enumep.sin, INT_WILDCARD);
1824 if (ep != NULL && refresh_interface(ep)) {
1826 * found existing and up to date interface -
1829 if (ep->phase != sys_interphase) {
1831 * On a new round we reset the name so
1832 * the interface name shows up again if
1833 * this address is no longer shared.
1834 * We reset ignore_packets from the
1835 * new prototype to respect any runtime
1836 * changes to the nic rules.
1838 strlcpy(ep->name, enumep.name,
1840 ep->ignore_packets =
1841 enumep.ignore_packets;
1843 /* name collision - rename interface */
1844 strlcpy(ep->name, "*multiple*",
1848 DPRINT_INTERFACE(4, (ep, "updating ",
1851 if (ep->ignore_packets !=
1852 enumep.ignore_packets) {
1854 * We have conflicting configurations
1855 * for the interface address. This is
1856 * caused by using -I <interfacename>
1857 * for an interface that shares its
1858 * address with other interfaces. We
1859 * can not disambiguate incoming
1860 * packets delivered to this socket
1861 * without extra syscalls/features.
1862 * These are not (commonly) available.
1863 * Note this is a more unusual
1864 * configuration where several
1865 * interfaces share an address but
1866 * filtering via interface name is
1867 * attempted. We resolve the
1868 * configuration conflict by disabling
1869 * the processing of received packets.
1870 * This leads to no service on the
1871 * interface address where the conflict
1875 "WARNING: conflicting enable configuration for interfaces %s and %s for address %s - unsupported configuration - address DISABLED",
1876 enumep.name, ep->name,
1879 ep->ignore_packets = ISC_TRUE;
1882 ep->phase = sys_interphase;
1884 ifi.action = IFS_EXISTS;
1886 if (receiver != NULL)
1887 (*receiver)(data, &ifi);
1890 * This is new or refreshing failed - add to
1891 * our interface list. If refreshing failed we
1892 * will delete the interface structure in phase
1893 * 2 as the interface was not marked current.
1894 * We can bind to the address as the refresh
1895 * code already closed the offending socket
1897 ep = create_interface(port, &enumep);
1900 ifi.action = IFS_CREATED;
1902 if (receiver != NULL)
1903 (*receiver)(data, &ifi);
1905 new_interface_found = TRUE;
1908 " new - created\n"));
1911 (&enumep, "updating ",
1912 " new - creation FAILED"));
1915 "failed to init interface for address %s",
1922 isc_interfaceiter_destroy(&iter);
1925 * phase 2 - delete gone interfaces - reassigning peers to
1928 for (ep = ep_list; ep != NULL; ep = next_ep) {
1929 next_ep = ep->elink;
1932 * if phase does not match sys_phase this interface was
1933 * not enumerated during the last interface scan - so it
1934 * is gone and will be deleted here unless it did not
1935 * originate from interface enumeration (INT_WILDCARD,
1938 if (((INT_WILDCARD | INT_MCASTIF) & ep->flags) ||
1939 ep->phase == sys_interphase)
1942 DPRINT_INTERFACE(3, (ep, "updating ",
1943 "GONE - deleting\n"));
1944 remove_interface(ep);
1946 ifi.action = IFS_DELETED;
1948 if (receiver != NULL)
1949 (*receiver)(data, &ifi);
1951 /* disconnect peers from deleted endpt. */
1952 while (ep->peers != NULL)
1953 set_peerdstadr(ep->peers, NULL);
1956 * update globals in case we lose
1957 * a loopback interface
1959 if (ep == loopback_interface)
1960 loopback_interface = NULL;
1962 delete_interface(ep);
1966 * phase 3 - re-configure as the world has possibly changed
1968 * never ever make this conditional again - it is needed to track
1969 * routing updates. see bug #2506
1971 refresh_all_peerinterfaces();
1973 if (broadcast_client_enabled)
1981 * Check multicast interfaces and try to join multicast groups if
1984 for (ep = ep_list; ep != NULL; ep = ep->elink) {
1987 if (!(INT_MCASTIF & ep->flags) || (INT_MCASTOPEN & ep->flags))
1990 /* Find remote address that was linked to this interface */
1991 for (entry = remoteaddr_list;
1993 entry = entry->link) {
1994 if (entry->ep == ep) {
1995 if (socket_multicast_enable(ep, &entry->addr)) {
1997 "Joined %s socket to multicast group %s",
1999 stoa(&entry->addr));
2007 return new_interface_found;
2012 * create_sockets - create a socket for each interface plus a default
2013 * socket for when we don't know where to send
2020 #ifndef HAVE_IO_COMPLETION_PORT
2022 * I/O Completion Ports don't care about the select and FD_SET
2025 FD_ZERO(&activefds);
2028 DPRINTF(2, ("create_sockets(%d)\n", port));
2030 create_wildcards(port);
2032 update_interfaces(port, NULL, NULL);
2035 * Now that we have opened all the sockets, turn off the reuse
2036 * flag for security.
2040 DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
2046 * create_interface - create a new interface for a given prototype
2047 * binding the socket.
2049 static struct interface *
2052 struct interface * protot
2057 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2059 remaddr_t * next_entry;
2061 DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&protot->sin),
2064 /* build an interface */
2065 iface = new_interface(protot);
2070 iface->fd = open_socket(&iface->sin, 0, 0, iface);
2072 if (iface->fd != INVALID_SOCKET)
2073 log_listen_address(iface);
2075 if ((INT_BROADCAST & iface->flags)
2076 && iface->bfd != INVALID_SOCKET)
2077 msyslog(LOG_INFO, "Listening on broadcast address %s#%d",
2078 stoa((&iface->bcast)), port);
2080 if (INVALID_SOCKET == iface->fd
2081 && INVALID_SOCKET == iface->bfd) {
2082 msyslog(LOG_ERR, "unable to create socket on %s (%d) for %s#%d",
2085 stoa((&iface->sin)),
2087 delete_interface(iface);
2092 * Blacklist our own addresses, no use talking to ourself
2094 SET_HOSTMASK(&resmask, AF(&iface->sin));
2095 hack_restrict(RESTRICT_FLAGS, &iface->sin, &resmask,
2096 RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
2099 * set globals with the first found
2100 * loopback interface of the appropriate class
2102 if (NULL == loopback_interface && AF_INET == iface->family
2103 && (INT_LOOPBACK & iface->flags))
2104 loopback_interface = iface;
2107 * put into our interface list
2109 add_addr_to_list(&iface->sin, iface);
2110 add_interface(iface);
2112 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2114 * Join any previously-configured compatible multicast groups.
2116 if (INT_MULTICAST & iface->flags &&
2117 !((INT_LOOPBACK | INT_WILDCARD) & iface->flags) &&
2118 !iface->ignore_packets) {
2119 for (entry = remoteaddr_list;
2121 entry = next_entry) {
2122 next_entry = entry->link;
2123 if (AF(&iface->sin) != AF(&entry->addr) ||
2124 !IS_MCAST(&entry->addr))
2126 if (socket_multicast_enable(iface,
2129 "Joined %s socket to multicast group %s",
2131 stoa(&entry->addr));
2134 "Failed to join %s socket to multicast group %s",
2136 stoa(&entry->addr));
2139 #endif /* MCAST && MCAST_NONEWSOCKET */
2141 DPRINT_INTERFACE(2, (iface, "created ", "\n"));
2146 #ifdef SO_EXCLUSIVEADDRUSE
2158 failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
2159 (char *)&one, sizeof(one));
2166 * Prior to Windows XP setting SO_EXCLUSIVEADDRUSE can fail with
2167 * error WSAINVAL depending on service pack level and whether
2168 * the user account is in the Administrators group. Do not
2169 * complain if it fails that way on versions prior to XP (5.1).
2171 err = GetLastError();
2173 if (isc_win32os_versioncheck(5, 1, 0, 0) < 0 /* < 5.1/XP */
2174 && WSAEINVAL == err)
2180 "setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m",
2183 #endif /* SO_EXCLUSIVEADDRUSE */
2187 * set_reuseaddr() - set/clear REUSEADDR on all sockets
2188 * NB possible hole - should we be doing this on broadcast
2196 #ifndef SO_EXCLUSIVEADDRUSE
2199 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2200 if (ep->flags & INT_WILDCARD)
2204 * if ep->fd is INVALID_SOCKET, we might have a adapter
2205 * configured but not present
2207 DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n",
2208 ep->name, stoa(&ep->sin),
2209 flag ? "on" : "off"));
2211 if (ep->fd != INVALID_SOCKET) {
2212 if (setsockopt(ep->fd, SOL_SOCKET, SO_REUSEADDR,
2213 (char *)&flag, sizeof(flag))) {
2214 msyslog(LOG_ERR, "set_reuseaddr: setsockopt(%s, SO_REUSEADDR, %s) failed: %m",
2215 stoa(&ep->sin), flag ? "on" : "off");
2219 #endif /* ! SO_EXCLUSIVEADDRUSE */
2223 * This is just a wrapper around an internal function so we can
2224 * make other changes as necessary later on
2228 struct interface * iface,
2232 #ifdef OPEN_BCAST_SOCKET
2233 socket_broadcast_enable(iface, iface->fd, baddr);
2237 #ifdef OPEN_BCAST_SOCKET
2239 * Enable a broadcast address to a given socket
2240 * The socket is in the ep_list all we need to do is enable
2241 * broadcasting. It is not this function's job to select the socket
2243 static isc_boolean_t
2244 socket_broadcast_enable(
2245 struct interface * iface,
2253 if (IS_IPV4(baddr)) {
2254 /* if this interface can support broadcast, set SO_BROADCAST */
2255 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
2256 (char *)&on, sizeof(on)))
2258 "setsockopt(SO_BROADCAST) enable failure on address %s: %m",
2261 DPRINTF(2, ("Broadcast enabled on socket %d for address %s\n",
2264 iface->flags |= INT_BCASTXMIT;
2268 #endif /* SO_BROADCAST */
2271 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
2273 * Remove a broadcast address from a given socket
2274 * The socket is in the ep_list all we need to do is disable
2275 * broadcasting. It is not this function's job to select the socket
2277 static isc_boolean_t
2278 socket_broadcast_disable(
2279 struct interface * iface,
2284 int off = 0; /* This seems to be OK as an int */
2286 if (IS_IPV4(baddr) && setsockopt(iface->fd, SOL_SOCKET,
2287 SO_BROADCAST, (char *)&off, sizeof(off)))
2289 "setsockopt(SO_BROADCAST) disable failure on address %s: %m",
2292 iface->flags &= ~INT_BCASTXMIT;
2296 #endif /* SO_BROADCAST */
2298 #endif /* OS_MISSES_SPECIFIC_ROUTE_UPDATES */
2300 #endif /* OPEN_BCAST_SOCKET */
2303 * return the broadcast client flag value
2306 get_broadcastclient_flag(void)
2308 return (broadcast_client_enabled);
2312 * Check to see if the address is a multicast address
2314 static isc_boolean_t
2319 isc_boolean_t result;
2321 #ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
2323 * If we don't have IPV6 support any IPV6 addr is not multicast
2329 result = IS_MCAST(maddr);
2332 DPRINTF(4, ("address %s is not multicast\n",
2339 * Multicast servers need to set the appropriate Multicast interface
2340 * socket option in order for it to know which interface to use for
2341 * send the multicast packet.
2344 enable_multicast_if(
2345 struct interface * iface,
2350 #ifdef IP_MULTICAST_LOOP
2351 TYPEOF_IP_MULTICAST_LOOP off = 0;
2353 #if defined(INCLUDE_IPV6_MULTICAST_SUPPORT) && defined(IPV6_MULTICAST_LOOP)
2357 REQUIRE(AF(maddr) == AF(&iface->sin));
2359 switch (AF(&iface->sin)) {
2362 #ifdef IP_MULTICAST_LOOP
2364 * Don't send back to itself, but allow failure to set
2366 if (setsockopt(iface->fd, IPPROTO_IP,
2368 SETSOCKOPT_ARG_CAST &off,
2372 "setsockopt IP_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2373 iface->fd, stoa(&iface->sin),
2380 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2381 #ifdef IPV6_MULTICAST_LOOP
2383 * Don't send back to itself, but allow failure to set
2385 if (setsockopt(iface->fd, IPPROTO_IPV6,
2386 IPV6_MULTICAST_LOOP,
2387 (char *) &off6, sizeof(off6))) {
2390 "setsockopt IPV6_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2391 iface->fd, stoa(&iface->sin),
2398 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2405 * Add a multicast address to a given socket
2406 * The socket is in the ep_list all we need to do is enable
2407 * multicasting. It is not this function's job to select the socket
2410 static isc_boolean_t
2411 socket_multicast_enable(
2416 struct ip_mreq mreq;
2417 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2418 struct ipv6_mreq mreq6;
2420 switch (AF(maddr)) {
2424 mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2425 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
2426 if (setsockopt(iface->fd,
2432 "setsockopt IP_ADD_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2433 iface->fd, stoa(&iface->sin),
2434 mreq.imr_multiaddr.s_addr,
2435 mreq.imr_interface.s_addr,
2439 DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n",
2440 iface->fd, stoa(&iface->sin),
2441 mreq.imr_multiaddr.s_addr,
2442 mreq.imr_interface.s_addr, stoa(maddr)));
2446 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2448 * Enable reception of multicast packets.
2449 * If the address is link-local we can get the
2450 * interface index from the scope id. Don't do this
2451 * for other types of multicast addresses. For now let
2452 * the kernel figure it out.
2455 mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2456 mreq6.ipv6mr_interface = iface->ifindex;
2458 if (setsockopt(iface->fd, IPPROTO_IPV6,
2459 IPV6_JOIN_GROUP, (char *)&mreq6,
2462 "setsockopt IPV6_JOIN_GROUP failed: %m on socket %d, addr %s for interface %u (%s)",
2463 iface->fd, stoa(&iface->sin),
2464 mreq6.ipv6mr_interface, stoa(maddr)));
2467 DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %u (%s)\n",
2468 iface->fd, stoa(&iface->sin),
2469 mreq6.ipv6mr_interface, stoa(maddr)));
2472 # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2474 iface->flags |= INT_MCASTOPEN;
2483 * Remove a multicast address from a given socket
2484 * The socket is in the ep_list all we need to do is disable
2485 * multicasting. It is not this function's job to select the socket
2488 static isc_boolean_t
2489 socket_multicast_disable(
2490 struct interface * iface,
2494 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2495 struct ipv6_mreq mreq6;
2497 struct ip_mreq mreq;
2501 if (find_addr_in_list(maddr) == NULL) {
2502 DPRINTF(4, ("socket_multicast_disable(%s): not found\n",
2507 switch (AF(maddr)) {
2510 mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2511 mreq.imr_interface = SOCK_ADDR4(&iface->sin);
2512 if (setsockopt(iface->fd, IPPROTO_IP,
2513 IP_DROP_MEMBERSHIP, (char *)&mreq,
2517 "setsockopt IP_DROP_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2518 iface->fd, stoa(&iface->sin),
2519 SRCADR(maddr), SRCADR(&iface->sin),
2525 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2527 * Disable reception of multicast packets
2528 * If the address is link-local we can get the
2529 * interface index from the scope id. Don't do this
2530 * for other types of multicast addresses. For now let
2531 * the kernel figure it out.
2533 mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2534 mreq6.ipv6mr_interface = iface->ifindex;
2536 if (setsockopt(iface->fd, IPPROTO_IPV6,
2537 IPV6_LEAVE_GROUP, (char *)&mreq6,
2541 "setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d (%s)",
2542 iface->fd, stoa(&iface->sin),
2543 iface->ifindex, stoa(maddr));
2549 # endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2553 if (!iface->num_mcast)
2554 iface->flags &= ~INT_MCASTOPEN;
2561 * io_setbclient - open the broadcast client sockets
2566 #ifdef OPEN_BCAST_SOCKET
2567 struct interface * interf;
2573 for (interf = ep_list;
2575 interf = interf->elink) {
2577 if (interf->flags & (INT_WILDCARD | INT_LOOPBACK))
2580 /* use only allowed addresses */
2581 if (interf->ignore_packets)
2584 /* Need a broadcast-capable interface */
2585 if (!(interf->flags & INT_BROADCAST))
2588 /* Only IPv4 addresses are valid for broadcast */
2589 REQUIRE(IS_IPV4(&interf->bcast));
2591 /* Do we already have the broadcast address open? */
2592 if (interf->flags & INT_BCASTOPEN) {
2594 * account for already open interfaces to avoid
2595 * misleading warning below
2602 * Try to open the broadcast address
2604 interf->family = AF_INET;
2605 interf->bfd = open_socket(&interf->bcast, 1, 0, interf);
2608 * If we succeeded then we use it otherwise enable
2609 * broadcast on the interface address
2611 if (interf->bfd != INVALID_SOCKET) {
2613 interf->flags |= INT_BCASTOPEN;
2615 "Listen for broadcasts to %s on interface #%d %s",
2616 stoa(&interf->bcast), interf->ifnum, interf->name);
2617 } else switch (errno) {
2618 /* Silently ignore EADDRINUSE as we probably
2619 * opened the socket already for an address in
2620 * the same network */
2622 /* Some systems cannot bind a socket to a broadcast
2623 * address, as that is not a valid host address. */
2625 # ifdef SYS_WINNT /*TODO: use for other systems, too? */
2626 /* avoid recurrence here -- if we already have a
2627 * regular socket, it's quite useless to try this
2630 if (interf->fd != INVALID_SOCKET) {
2631 interf->flags |= INT_BCASTOPEN;
2639 "failed to listen for broadcasts to %s on interface #%d %s",
2640 stoa(&interf->bcast), interf->ifnum, interf->name);
2646 broadcast_client_enabled = ISC_TRUE;
2647 DPRINTF(1, ("io_setbclient: listening to %d broadcast addresses\n", nif));
2649 broadcast_client_enabled = ISC_FALSE;
2651 "Unable to listen for broadcasts, no broadcast interfaces available");
2655 "io_setbclient: Broadcast Client disabled by build");
2656 #endif /* OPEN_BCAST_SOCKET */
2660 * io_unsetbclient - close the broadcast client sockets
2663 io_unsetbclient(void)
2667 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2668 if (INT_WILDCARD & ep->flags)
2670 if (!(INT_BCASTOPEN & ep->flags))
2673 if (ep->bfd != INVALID_SOCKET) {
2674 /* destroy broadcast listening socket */
2676 "stop listening for broadcasts to %s on interface #%d %s",
2677 stoa(&ep->bcast), ep->ifnum, ep->name);
2678 # ifdef HAVE_IO_COMPLETION_PORT
2679 io_completion_port_remove_socket(ep->bfd, ep);
2681 close_and_delete_fd_from_list(ep->bfd);
2682 ep->bfd = INVALID_SOCKET;
2684 ep->flags &= ~INT_BCASTOPEN;
2686 broadcast_client_enabled = ISC_FALSE;
2690 * io_multicast_add() - add multicast group address
2702 * Check to see if this is a multicast address
2704 if (!addr_ismulticast(addr))
2707 /* If we already have it we can just return */
2708 if (NULL != find_flagged_addr_in_list(addr, INT_MCASTOPEN)) {
2710 "Duplicate request found for multicast address %s",
2715 # ifndef MULTICAST_NONEWSOCKET
2716 ep = new_interface(NULL);
2719 * Open a new socket for the multicast address
2722 SET_PORT(&ep->sin, NTP_PORT);
2723 ep->family = AF(&ep->sin);
2724 AF(&ep->mask) = ep->family;
2725 SET_ONESMASK(&ep->mask);
2728 ep->bfd = INVALID_SOCKET;
2729 ep->fd = open_socket(&ep->sin, 0, 0, ep);
2730 if (ep->fd != INVALID_SOCKET) {
2731 ep->ignore_packets = ISC_FALSE;
2732 ep->flags |= INT_MCASTIF;
2734 strlcpy(ep->name, "multicast", sizeof(ep->name));
2735 DPRINT_INTERFACE(2, (ep, "multicast add ", "\n"));
2737 log_listen_address(ep);
2739 /* bind failed, re-use wildcard interface */
2740 delete_interface(ep);
2744 else if (IS_IPV6(addr))
2750 /* HACK ! -- stuff in an address */
2751 /* because we don't bind addr? DH */
2754 "multicast address %s using wildcard interface #%d %s",
2755 stoa(addr), ep->ifnum, ep->name);
2758 "No multicast socket available to use for address %s",
2763 { /* in place of the { following for in #else clause */
2765 # else /* MULTICAST_NONEWSOCKET follows */
2767 * For the case where we can't use a separate socket (Windows)
2768 * join each applicable endpoint socket to the group address.
2774 for (ep = ep_list; ep != NULL; ep = ep->elink) {
2775 if (ep->ignore_packets || AF(&ep->sin) != AF(addr) ||
2776 !(INT_MULTICAST & ep->flags) ||
2777 (INT_LOOPBACK | INT_WILDCARD) & ep->flags)
2780 # endif /* MULTICAST_NONEWSOCKET */
2781 if (socket_multicast_enable(ep, addr))
2783 "Joined %s socket to multicast group %s",
2788 add_addr_to_list(addr, one_ep);
2789 #else /* !MCAST follows*/
2791 "Can not add multicast address %s: no multicast support",
2799 * io_multicast_del() - delete multicast group address
2810 * Check to see if this is a multicast address
2812 if (!addr_ismulticast(addr)) {
2813 msyslog(LOG_ERR, "invalid multicast address %s",
2819 * Disable reception of multicast packets
2821 while ((iface = find_flagged_addr_in_list(addr, INT_MCASTOPEN))
2823 socket_multicast_disable(iface, addr);
2825 delete_addr_from_list(addr);
2827 #else /* not MCAST */
2829 "Can not delete multicast address %s: no multicast support",
2831 #endif /* not MCAST */
2836 * open_socket - open a socket, returning the file descriptor
2850 * int is OK for REUSEADR per
2851 * http://www.kohala.com/start/mcast.api.txt
2856 if (IS_IPV6(addr) && !ipv6_works)
2857 return INVALID_SOCKET;
2859 /* create a datagram (UDP) socket */
2860 fd = socket(AF(addr), SOCK_DGRAM, 0);
2861 if (INVALID_SOCKET == fd) {
2862 errval = socket_errno();
2864 "socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
2865 IS_IPV6(addr) ? "6" : "", stoa(addr));
2867 if (errval == EPROTONOSUPPORT ||
2868 errval == EAFNOSUPPORT ||
2869 errval == EPFNOSUPPORT)
2870 return (INVALID_SOCKET);
2874 "unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - exiting",
2880 connection_reset_fix(fd, addr);
2883 * Fixup the file descriptor for some systems
2884 * See bug #530 for details of the issue.
2889 * set SO_REUSEADDR since we will be binding the same port
2890 * number on each interface according to turn_off_reuse.
2891 * This is undesirable on Windows versions starting with
2892 * Windows XP (numeric version 5.1).
2895 if (isc_win32os_versioncheck(5, 1, 0, 0) < 0) /* before 5.1 */
2897 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2898 (char *)((turn_off_reuse)
2904 "setsockopt SO_REUSEADDR %s fails for address %s: %m",
2910 return INVALID_SOCKET;
2912 #ifdef SO_EXCLUSIVEADDRUSE
2914 * setting SO_EXCLUSIVEADDRUSE on the wildcard we open
2915 * first will cause more specific binds to fail.
2917 if (!(interf->flags & INT_WILDCARD))
2918 set_excladdruse(fd);
2922 * IPv4 specific options go here
2924 if (IS_IPV4(addr)) {
2925 #if defined(IPPROTO_IP) && defined(IP_TOS)
2926 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char*)&qos,
2929 "setsockopt IP_TOS (%02x) fails on address %s: %m",
2931 #endif /* IPPROTO_IP && IP_TOS */
2933 socket_broadcast_enable(interf, fd, addr);
2937 * IPv6 specific options go here
2939 if (IS_IPV6(addr)) {
2940 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
2941 if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, (char*)&qos,
2944 "setsockopt IPV6_TCLASS (%02x) fails on address %s: %m",
2946 #endif /* IPPROTO_IPV6 && IPV6_TCLASS */
2948 if (isc_net_probe_ipv6only() == ISC_R_SUCCESS
2949 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
2950 (char*)&on, sizeof(on)))
2952 "setsockopt IPV6_V6ONLY on fails on address %s: %m",
2955 #ifdef IPV6_BINDV6ONLY
2956 if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
2957 (char*)&on, sizeof(on)))
2959 "setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
2964 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2966 * some OSes don't allow binding to more specific
2967 * addresses if a wildcard address already bound
2968 * to the port and SO_REUSEADDR is not set
2970 if (!is_wildcard_addr(addr))
2971 set_wildcard_reuse(AF(addr), 1);
2975 * bind the local address.
2977 errval = bind(fd, &addr->sa, SOCKLEN(addr));
2979 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2980 if (!is_wildcard_addr(addr))
2981 set_wildcard_reuse(AF(addr), 0);
2986 * Don't log this under all conditions
2988 if (turn_off_reuse == 0
2994 "bind(%d) AF_INET%s %s#%d%s flags 0x%x failed: %m",
2995 fd, IS_IPV6(addr) ? "6" : "",
2996 stoa(addr), SRCPORT(addr),
2997 IS_MCAST(addr) ? " (multicast)" : "",
3003 return INVALID_SOCKET;
3006 #ifdef HAVE_TIMESTAMP
3008 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
3009 (char*)&on, sizeof(on)))
3011 "setsockopt SO_TIMESTAMP on fails on address %s: %m",
3014 DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
3018 #ifdef HAVE_TIMESTAMPNS
3020 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
3021 (char*)&on, sizeof(on)))
3023 "setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
3026 DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
3032 if (setsockopt(fd, SOL_SOCKET, SO_BINTIME,
3033 (char*)&on, sizeof(on)))
3035 "setsockopt SO_BINTIME on fails on address %s: %m",
3038 DPRINTF(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
3043 DPRINTF(4, ("bind(%d) AF_INET%s, addr %s%%%d#%d, flags 0x%x\n",
3044 fd, IS_IPV6(addr) ? "6" : "", stoa(addr),
3045 SCOPE(addr), SRCPORT(addr), interf->flags));
3047 make_socket_nonblocking(fd);
3049 #ifdef HAVE_SIGNALED_IO
3050 init_socket_sig(fd);
3051 #endif /* not HAVE_SIGNALED_IO */
3053 add_fd_to_list(fd, FD_TYPE_SOCKET);
3055 #if !defined(SYS_WINNT) && !defined(VMS)
3056 DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
3057 fcntl(fd, F_GETFL, 0)));
3058 #endif /* SYS_WINNT || VMS */
3060 #if defined(HAVE_IO_COMPLETION_PORT)
3062 * Add the socket to the completion port
3064 if (!io_completion_port_add_socket(fd, interf, bcast)) {
3065 msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
3074 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
3076 * sendpkt - send a packet to the specified destination. Maintain a
3077 * send error cache so that only the first consecutive error for a
3078 * destination is logged.
3083 struct interface * ep,
3095 ismcast = IS_MCAST(dest);
3099 src = (IS_IPV4(dest))
3105 * unbound peer - drop request and wait for better
3106 * network conditions
3108 DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
3109 ismcast ? "\tMCAST\t***** " : "",
3110 stoa(dest), ttl, len));
3115 DPRINTF(2, ("%ssendpkt(%d, dst=%s, src=%s, ttl=%d, len=%d)\n",
3116 ismcast ? "\tMCAST\t***** " : "", src->fd,
3117 stoa(dest), stoa(&src->sin), ttl, len));
3120 * for the moment we use the bcast option to set multicast ttl
3122 if (ismcast && ttl > 0 && ttl != src->last_ttl) {
3124 * set the multicast ttl for outgoing packets
3126 switch (AF(&src->sin)) {
3130 rc = setsockopt(src->fd, IPPROTO_IP,
3136 # ifdef INCLUDE_IPV6_SUPPORT
3138 rc = setsockopt(src->fd, IPPROTO_IPV6,
3139 IPV6_MULTICAST_HOPS,
3143 # endif /* INCLUDE_IPV6_SUPPORT */
3150 src->last_ttl = ttl;
3153 "setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
3159 cc = simulate_server(dest, src, pkt);
3160 #elif defined(HAVE_IO_COMPLETION_PORT)
3161 cc = io_completion_port_sendto(src, src->fd, pkt,
3162 (size_t)len, (sockaddr_u *)&dest->sa);
3164 cc = sendto(src->fd, (char *)pkt, (u_int)len, 0,
3165 &dest->sa, SOCKLEN(dest));
3176 } while (ismcast && src != NULL);
3180 #if !defined(HAVE_IO_COMPLETION_PORT)
3181 #if !defined(HAVE_SIGNALED_IO)
3183 * fdbits - generate ascii representation of fd_set (FAU debug support)
3184 * HFDF format - highest fd first.
3192 static char buffer[256];
3193 char * buf = buffer;
3195 count = min(count, 255);
3197 while (count >= 0) {
3198 *buf++ = FD_ISSET(count, set) ? '#' : '-';
3209 * Routine to read the refclock packets for a specific interface
3210 * Return the number of bytes read. That way we know if we should
3211 * read it again or go on to the next one if no bytes returned
3214 read_refclock_packet(
3216 struct refclockio * rp,
3224 struct recvbuf * rb;
3226 rb = get_free_recv_buffer();
3230 * No buffer space available - just drop the packet
3232 char buf[RX_BUFF_SIZE];
3234 buflen = read(fd, buf, sizeof buf);
3239 /* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead
3240 * to buffer overrun and memory corruption
3242 if (rp->datalen <= 0 || (size_t)rp->datalen > sizeof(rb->recv_space))
3243 read_count = sizeof(rb->recv_space);
3245 read_count = (u_int)rp->datalen;
3247 buflen = read(fd, (char *)&rb->recv_space, read_count);
3248 } while (buflen < 0 && EINTR == errno);
3251 saved_errno = errno;
3253 errno = saved_errno;
3258 * Got one. Mark how and when it got here,
3259 * put it on the full list and do bookkeeping.
3261 rb->recv_length = buflen;
3262 rb->recv_peer = rp->srcclock;
3266 rb->receiver = rp->clock_recv;
3268 consumed = indicate_refclock_packet(rp, rb);
3276 #endif /* REFCLOCK */
3279 #ifdef HAVE_PACKET_TIMESTAMP
3281 * extract timestamps from control message buffer
3285 struct recvbuf * rb,
3286 struct msghdr * msghdr,
3290 struct cmsghdr * cmsghdr;
3291 unsigned long ticks;
3299 cmsghdr = CMSG_FIRSTHDR(msghdr);
3300 while (cmsghdr != NULL) {
3301 switch (cmsghdr->cmsg_type)
3305 #endif /* HAVE_BINTIME */
3306 #ifdef HAVE_TIMESTAMPNS
3307 case SCM_TIMESTAMPNS:
3308 #endif /* HAVE_TIMESTAMPNS */
3309 #ifdef HAVE_TIMESTAMP
3311 #endif /* HAVE_TIMESTAMP */
3312 #if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP)
3313 switch (cmsghdr->cmsg_type)
3319 memcpy(&pbt, CMSG_DATA(cmsghdr), sizeof(pbt));
3321 * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
3323 nts.l_i = pbt.sec + JAN_1970;
3324 nts.l_uf = (u_int32)(pbt.frac >> 32);
3325 if (sys_tick > measured_tick &&
3327 ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC));
3328 nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC));
3330 DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
3331 pbt.sec, (unsigned long)((nts.l_uf / FRAC) * 1e9)));
3334 #endif /* HAVE_BINTIME */
3335 #ifdef HAVE_TIMESTAMPNS
3336 case SCM_TIMESTAMPNS:
3338 struct timespec pts;
3339 memcpy(&pts, CMSG_DATA(cmsghdr), sizeof(pts));
3340 if (sys_tick > measured_tick &&
3342 ticks = (unsigned long)((pts.tv_nsec * 1e-9) /
3344 pts.tv_nsec = (long)(ticks * 1e9 *
3347 DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
3348 pts.tv_sec, pts.tv_nsec));
3349 nts = tspec_stamp_to_lfp(pts);
3352 #endif /* HAVE_TIMESTAMPNS */
3353 #ifdef HAVE_TIMESTAMP
3357 memcpy(&ptv, CMSG_DATA(cmsghdr), sizeof(ptv));
3358 if (sys_tick > measured_tick &&
3360 ticks = (unsigned long)((ptv.tv_usec * 1e-6) /
3362 ptv.tv_usec = (long)(ticks * 1e6 *
3365 DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
3366 (intmax_t)ptv.tv_sec, (long)ptv.tv_usec));
3367 nts = tval_stamp_to_lfp(ptv);
3370 #endif /* HAVE_TIMESTAMP */
3372 fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
3373 DTOLFP(fuzz, &lfpfuzz);
3374 L_ADD(&nts, &lfpfuzz);
3378 collect_timing(rb, "input processing delay", 1,
3380 DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
3382 #endif /* DEBUG_TIMING */
3383 ts = nts; /* network time stamp */
3385 #endif /* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */
3388 DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n",
3389 cmsghdr->cmsg_type));
3391 cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
3395 #endif /* HAVE_PACKET_TIMESTAMP */
3399 * Routine to read the network NTP packets for a specific interface
3400 * Return the number of bytes read. That way we know if we should
3401 * read it again or go on to the next one if no bytes returned
3404 read_network_packet(
3406 struct interface * itf,
3410 GETSOCKNAME_SOCKLEN_TYPE fromlen;
3412 register struct recvbuf *rb;
3413 #ifdef HAVE_PACKET_TIMESTAMP
3414 struct msghdr msghdr;
3416 char control[CMSG_BUFSIZE];
3420 * Get a buffer and read the frame. If we
3421 * haven't got a buffer, or this is received
3422 * on a disallowed socket, just dump the
3426 rb = get_free_recv_buffer();
3427 if (NULL == rb || itf->ignore_packets) {
3428 char buf[RX_BUFF_SIZE];
3434 fromlen = sizeof(from);
3435 buflen = recvfrom(fd, buf, sizeof(buf), 0,
3436 &from.sa, &fromlen);
3437 DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
3438 (itf->ignore_packets)
3441 free_recvbuffs(), fd, stoa(&from)));
3442 if (itf->ignore_packets)
3449 fromlen = sizeof(rb->recv_srcadr);
3451 #ifndef HAVE_PACKET_TIMESTAMP
3452 rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
3453 sizeof(rb->recv_space), 0,
3454 &rb->recv_srcadr.sa, &fromlen);
3456 iovec.iov_base = &rb->recv_space;
3457 iovec.iov_len = sizeof(rb->recv_space);
3458 msghdr.msg_name = &rb->recv_srcadr;
3459 msghdr.msg_namelen = fromlen;
3460 msghdr.msg_iov = &iovec;
3461 msghdr.msg_iovlen = 1;
3462 msghdr.msg_control = (void *)&control;
3463 msghdr.msg_controllen = sizeof(control);
3464 msghdr.msg_flags = 0;
3465 rb->recv_length = recvmsg(fd, &msghdr, 0);
3468 buflen = rb->recv_length;
3470 if (buflen == 0 || (buflen == -1 &&
3471 (EWOULDBLOCK == errno
3478 } else if (buflen < 0) {
3479 msyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
3480 stoa(&rb->recv_srcadr), fd);
3481 DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n",
3487 DPRINTF(3, ("read_network_packet: fd=%d length %d from %s\n",
3488 fd, buflen, stoa(&rb->recv_srcadr)));
3490 #ifdef ENABLE_BUG3020_FIX
3491 if (ISREFCLOCKADR(&rb->recv_srcadr)) {
3492 msyslog(LOG_ERR, "recvfrom(%s) fd=%d: refclock srcadr on a network interface!",
3493 stoa(&rb->recv_srcadr), fd);
3494 DPRINTF(1, ("read_network_packet: fd=%d dropped (refclock srcadr))\n",
3503 ** Bug 2672: Some OSes (MacOSX and Linux) don't block spoofed ::1
3506 if (AF_INET6 == itf->family) {
3507 DPRINTF(2, ("Got an IPv6 packet, from <%s> (%d) to <%s> (%d)\n",
3508 stoa(&rb->recv_srcadr),
3509 IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr)),
3511 !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3514 if ( IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr))
3515 && !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3518 DPRINTF(2, ("DROPPING that packet\n"));
3522 DPRINTF(2, ("processing that packet\n"));
3526 * Got one. Mark how and when it got here,
3527 * put it on the full list and do bookkeeping.
3531 #ifdef HAVE_PACKET_TIMESTAMP
3532 /* pick up a network time stamp if possible */
3533 ts = fetch_timestamp(rb, &msghdr, ts);
3536 rb->receiver = receive;
3538 add_full_recv_buffer(rb);
3546 * attempt to handle io (select()/signaled IO)
3551 # ifndef HAVE_SIGNALED_IO
3556 * Use select() on all on all input fd's for unlimited
3557 * time. select() will terminate on SIGALARM or on the
3558 * reception of input. Using select() means we can't do
3559 * robust signal handling and we get a potential race
3560 * between checking for alarms and doing the select().
3561 * Mostly harmless, I think.
3564 * On VMS, I suspect that select() can't be interrupted
3565 * by a "signal" either, so I take the easy way out and
3566 * have select() time out after one second.
3567 * System clock updates really aren't time-critical,
3568 * and - lacking a hardware reference clock - I have
3569 * yet to learn about anything else that is.
3573 # if !defined(VMS) && !defined(SYS_VXWORKS)
3574 nfound = select(maxactivefd + 1, &rdfdes, NULL,
3576 # else /* VMS, VxWorks */
3577 /* make select() wake up after one second */
3582 nfound = select(maxactivefd + 1,
3583 &rdfdes, NULL, NULL,
3586 # endif /* VMS, VxWorks */
3587 if (nfound < 0 && sanitize_fdset(errno)) {
3592 nfound = select(maxactivefd + 1,
3593 &rdfdes, NULL, NULL,
3602 input_handler_scan(&ts, &rdfdes);
3603 } else if (nfound == -1 && errno != EINTR) {
3604 msyslog(LOG_ERR, "select() error: %m");
3607 else if (debug > 4) {
3608 msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
3610 DPRINTF(3, ("select() returned %d: %m\n", nfound));
3613 # else /* HAVE_SIGNALED_IO */
3615 # endif /* HAVE_SIGNALED_IO */
3618 #ifdef HAVE_SIGNALED_IO
3620 * input_handler - receive packets asynchronously
3622 * ALWAYS IN SIGNAL HANDLER CONTEXT -- only async-safe functions allowed!
3630 struct timeval tvzero;
3636 * Do a poll to see who has data
3640 tvzero.tv_sec = tvzero.tv_usec = 0;
3642 n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3643 if (n < 0 && sanitize_fdset(errno)) {
3645 tvzero.tv_sec = tvzero.tv_usec = 0;
3646 n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3649 input_handler_scan(cts, &fds);
3651 #endif /* HAVE_SIGNALED_IO */
3655 * Try to sanitize the global FD set
3657 * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3666 # ifndef HAVE_SIGNALED_IO
3668 * extended FAU debugging output
3670 if (errc != EINTR) {
3672 "select(%d, %s, 0L, 0L, &0.0) error: %m",
3674 fdbits(maxactivefd, &activefds));
3681 /* if we have oviously bad FDs, try to sanitize the FD set. */
3682 for (j = 0, maxscan = 0; j <= maxactivefd; j++) {
3683 if (FD_ISSET(j, &activefds)) {
3684 if (-1 != read(j, &b, 0)) {
3688 # ifndef HAVE_SIGNALED_IO
3690 "Removing bad file descriptor %d from select set",
3693 FD_CLR(j, &activefds);
3696 if (maxactivefd != maxscan)
3697 maxactivefd = maxscan;
3702 * scan the known FDs (clocks, servers, ...) for presence in a 'fd_set'.
3704 * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3717 l_fp ts; /* Timestamp at BOselect() gob */
3719 #if defined(DEBUG_TIMING)
3720 l_fp ts_e; /* Timestamp at EOselect() gob */
3724 struct refclockio *rp;
3728 #ifdef HAS_ROUTING_SOCKET
3729 struct asyncio_reader * asyncio_reader;
3730 struct asyncio_reader * next_asyncio_reader;
3738 * Check out the reference clocks first, if any
3741 for (rp = refio; rp != NULL; rp = rp->next) {
3744 if (!FD_ISSET(fd, pfds))
3746 buflen = read_refclock_packet(fd, rp, ts);
3748 * The first read must succeed after select() indicates
3749 * readability, or we've reached a permanent EOF.
3750 * http://bugs.ntp.org/1732 reported ntpd munching CPU
3751 * after a USB GPS was unplugged because select was
3752 * indicating EOF but ntpd didn't remove the descriptor
3753 * from the activefds set.
3755 if (buflen < 0 && EAGAIN != errno) {
3756 saved_errno = errno;
3757 clk = refnumtoa(&rp->srcclock->srcadr);
3758 errno = saved_errno;
3759 msyslog(LOG_ERR, "%s read: %m", clk);
3760 maintain_activefds(fd, TRUE);
3761 } else if (0 == buflen) {
3762 clk = refnumtoa(&rp->srcclock->srcadr);
3763 msyslog(LOG_ERR, "%s read EOF", clk);
3764 maintain_activefds(fd, TRUE);
3766 /* drain any remaining refclock input */
3768 buflen = read_refclock_packet(fd, rp, ts);
3769 } while (buflen > 0);
3772 #endif /* REFCLOCK */
3775 * Loop through the interfaces looking for data to read.
3777 for (ep = ep_list; ep != NULL; ep = ep->elink) {
3778 for (doing = 0; doing < 2; doing++) {
3782 if (!(ep->flags & INT_BCASTOPEN))
3788 if (FD_ISSET(fd, pfds))
3790 buflen = read_network_packet(
3792 } while (buflen > 0);
3793 /* Check more interfaces */
3797 #ifdef HAS_ROUTING_SOCKET
3799 * scan list of asyncio readers - currently only used for routing sockets
3801 asyncio_reader = asyncio_reader_list;
3803 while (asyncio_reader != NULL) {
3804 /* callback may unlink and free asyncio_reader */
3805 next_asyncio_reader = asyncio_reader->link;
3806 if (FD_ISSET(asyncio_reader->fd, pfds))
3807 (*asyncio_reader->receiver)(asyncio_reader);
3808 asyncio_reader = next_asyncio_reader;
3810 #endif /* HAS_ROUTING_SOCKET */
3813 * Check for a response from a blocking child
3815 for (idx = 0; idx < blocking_children_alloc; idx++) {
3816 c = blocking_children[idx];
3817 if (NULL == c || -1 == c->resp_read_pipe)
3819 if (FD_ISSET(c->resp_read_pipe, pfds)) {
3820 ++c->resp_ready_seen;
3821 ++blocking_child_ready_seen;
3825 /* We've done our work */
3826 #if defined(DEBUG_TIMING)
3829 * (ts_e - ts) is the amount of time we spent
3830 * processing this gob of file descriptors. Log
3834 collect_timing(NULL, "input handler", 1, &ts_e);
3837 "input_handler: Processed a gob of fd's in %s msec",
3839 #endif /* DEBUG_TIMING */
3841 #endif /* !HAVE_IO_COMPLETION_PORT */
3844 * find an interface suitable for the src address
3847 select_peerinterface(
3849 sockaddr_u * srcadr,
3857 wild = ANY_INTERFACE_CHOOSE(srcadr);
3860 * Initialize the peer structure and dance the interface jig.
3861 * Reference clocks step the loopback waltz, the others
3862 * squaredance around the interface list looking for a buddy. If
3863 * the dance peters out, there is always the wildcard interface.
3864 * This might happen in some systems and would preclude proper
3865 * operation with public key cryptography.
3867 if (ISREFCLOCKADR(srcadr)) {
3868 ep = loopback_interface;
3869 } else if (peer->cast_flags &
3870 (MDF_BCLNT | MDF_ACAST | MDF_MCAST | MDF_BCAST)) {
3871 ep = findbcastinter(srcadr);
3873 DPRINTF(4, ("Found *-cast interface %s for address %s\n",
3874 stoa(&ep->sin), stoa(srcadr)));
3876 DPRINTF(4, ("No *-cast local address found for address %s\n",
3884 * If it is a multicast address, findbcastinter() may not find
3885 * it. For unicast, we get to find the interface when dstadr is
3886 * given to us as the wildcard (ANY_INTERFACE_CHOOSE). Either
3887 * way, try a little harder.
3890 ep = findinterface(srcadr);
3892 * we do not bind to the wildcard interfaces for output
3893 * as our (network) source address would be undefined and
3894 * crypto will not work without knowing the own transmit address
3896 if (ep != NULL && INT_WILDCARD & ep->flags)
3897 if (!accept_wildcard_if_for_winnt)
3899 #else /* SIM follows */
3900 ep = loopback_interface;
3908 * findinterface - find local interface corresponding to address
3917 iface = findlocalinterface(addr, INT_WILDCARD, 0);
3919 if (NULL == iface) {
3920 DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
3923 iface = ANY_INTERFACE_CHOOSE(addr);
3925 DPRINTF(4, ("Found interface #%d %s for address %s\n",
3926 iface->ifnum, iface->name, stoa(addr)));
3932 * findlocalinterface - find local interface corresponding to addr,
3933 * which does not have any of flags set. If bast is nonzero, addr is
3934 * a broadcast address.
3936 * This code attempts to find the local sending address for an outgoing
3937 * address by connecting a new socket to destinationaddress:NTP_PORT
3938 * and reading the sockname of the resulting connect.
3939 * the complicated sequence simulates the routing table lookup
3940 * for to first hop without duplicating any of the routing logic into
3941 * ntpd. preferably we would have used an API call - but its not there -
3942 * so this is the best we can do here short of duplicating to entire routing
3943 * logic in ntpd which would be a silly and really unportable thing to do.
3953 GETSOCKNAME_SOCKLEN_TYPE sockaddrlen;
3960 DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
3963 s = socket(AF(addr), SOCK_DGRAM, 0);
3964 if (INVALID_SOCKET == s)
3968 * If we are looking for broadcast interface we need to set this
3969 * socket to allow broadcast
3973 if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET,
3982 rtn = connect(s, &addr->sa, SOCKLEN(addr));
3983 if (SOCKET_ERROR == rtn) {
3988 sockaddrlen = sizeof(saddr);
3989 rtn = getsockname(s, &saddr.sa, &sockaddrlen);
3991 if (SOCKET_ERROR == rtn)
3994 DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n",
3995 stoa(addr), stoa(&saddr)));
3997 iface = getinterface(&saddr, flags);
4000 * if we didn't find an exact match on saddr, find the closest
4001 * available local address. This handles the case of the
4002 * address suggested by the kernel being excluded by nic rules
4003 * or the user's -I and -L options to ntpd.
4004 * See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683
4005 * for more background.
4007 if (NULL == iface || iface->ignore_packets)
4008 iface = findclosestinterface(&saddr,
4009 flags | INT_LOOPBACK);
4011 /* Don't use an interface which will ignore replies */
4012 if (iface != NULL && iface->ignore_packets)
4020 * findclosestinterface
4022 * If there are -I/--interface or -L/novirtualips command-line options,
4023 * or "nic" or "interface" rules in ntp.conf, findlocalinterface() may
4024 * find the kernel's preferred local address for a given peer address is
4025 * administratively unavailable to ntpd, and punt to this routine's more
4028 * Find the numerically closest local address to the one connect()
4029 * suggested. This matches an address on the same subnet first, as
4030 * needed by Bug 1184, and provides a consistent choice if there are
4031 * multiple feasible local addresses, regardless of the order ntpd
4035 findclosestinterface(
4042 sockaddr_u addr_dist;
4043 sockaddr_u min_dist;
4045 ZERO_SOCK(&min_dist);
4048 for (ep = ep_list; ep != NULL; ep = ep->elink) {
4049 if (ep->ignore_packets ||
4050 AF(addr) != ep->family ||
4054 calc_addr_distance(&addr_dist, addr, &ep->sin);
4055 if (NULL == winner ||
4056 -1 == cmp_addr_distance(&addr_dist, &min_dist)) {
4057 min_dist = addr_dist;
4062 DPRINTF(4, ("findclosestinterface(%s) failed\n",
4065 DPRINTF(4, ("findclosestinterface(%s) -> %s\n",
4066 stoa(addr), stoa(&winner->sin)));
4073 * calc_addr_distance - calculate the distance between two addresses,
4074 * the absolute value of the difference between
4075 * the addresses numerically, stored as an address.
4080 const sockaddr_u * a1,
4081 const sockaddr_u * a2
4091 REQUIRE(AF(a1) == AF(a2));
4096 /* v4 can be done a bit simpler */
4100 v4dist = (a1val > a2val)
4103 SET_ADDR4(dist, v4dist);
4108 found_greater = FALSE;
4109 a1_greater = FALSE; /* suppress pot. uninit. warning */
4110 for (i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
4111 if (!found_greater &&
4112 NSRCADR6(a1)[i] != NSRCADR6(a2)[i]) {
4113 found_greater = TRUE;
4114 a1_greater = (NSRCADR6(a1)[i] > NSRCADR6(a2)[i]);
4116 if (!found_greater) {
4117 NSRCADR6(dist)[i] = 0;
4120 NSRCADR6(dist)[i] = NSRCADR6(a1)[i] -
4123 NSRCADR6(dist)[i] = NSRCADR6(a2)[i] -
4131 * cmp_addr_distance - compare two address distances, returning -1, 0,
4132 * 1 to indicate their relationship.
4136 const sockaddr_u * d1,
4137 const sockaddr_u * d2
4142 REQUIRE(AF(d1) == AF(d2));
4145 if (SRCADR(d1) < SRCADR(d2))
4147 else if (SRCADR(d1) == SRCADR(d2))
4153 for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
4154 if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i])
4156 else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i])
4166 * fetch an interface structure the matches the
4167 * address and has the given flags NOT set
4177 iface = find_addr_in_list(addr);
4179 if (iface != NULL && (iface->flags & flags))
4187 * findbcastinter - find broadcast interface corresponding to address
4197 #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
4198 DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
4201 iface = findlocalinterface(addr, INT_LOOPBACK | INT_WILDCARD,
4203 if (iface != NULL) {
4204 DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n",
4205 iface->ifnum, iface->name));
4210 * plan B - try to find something reasonable in our lists in
4211 * case kernel lookup doesn't help
4213 for (iface = ep_list; iface != NULL; iface = iface->elink) {
4214 if (iface->flags & INT_WILDCARD)
4217 /* Don't bother with ignored interfaces */
4218 if (iface->ignore_packets)
4222 * First look if this is the correct family
4224 if(AF(&iface->sin) != AF(addr))
4227 /* Skip the loopback addresses */
4228 if (iface->flags & INT_LOOPBACK)
4232 * If we are looking to match a multicast address and
4233 * this interface is one...
4235 if (addr_ismulticast(addr)
4236 && (iface->flags & INT_MULTICAST)) {
4237 #ifdef INCLUDE_IPV6_SUPPORT
4239 * ...it is the winner unless we're looking for
4240 * an interface to use for link-local multicast
4241 * and its address is not link-local.
4244 && IN6_IS_ADDR_MC_LINKLOCAL(PSOCK_ADDR6(addr))
4245 && !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin)))
4252 * We match only those interfaces marked as
4253 * broadcastable and either the explicit broadcast
4254 * address or the network portion of the IP address.
4257 if (IS_IPV4(addr)) {
4258 if (SOCK_EQ(&iface->bcast, addr))
4261 if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask))
4262 == (NSRCADR(addr) & NSRCADR(&iface->mask)))
4265 #ifdef INCLUDE_IPV6_SUPPORT
4266 else if (IS_IPV6(addr)) {
4267 if (SOCK_EQ(&iface->bcast, addr))
4270 if (SOCK_EQ(netof(&iface->sin), netof(addr)))
4275 #endif /* SIOCGIFCONF */
4276 if (NULL == iface) {
4277 DPRINTF(4, ("No bcast interface found for %s\n",
4279 iface = ANY_INTERFACE_CHOOSE(addr);
4281 DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n",
4282 iface->ifnum, iface->name));
4290 * io_clr_stats - clear I/O module statistics
4295 packets_dropped = 0;
4296 packets_ignored = 0;
4297 packets_received = 0;
4299 packets_notsent = 0;
4303 io_timereset = current_time;
4309 * io_addclock - add a reference clock to the list and arrange that we
4310 * get SIGIO interrupts from it.
4314 struct refclockio *rio
4320 * Stuff the I/O structure in the list and mark the descriptor
4321 * in use. There is a harmless (I hope) race condition here.
4325 # ifdef HAVE_SIGNALED_IO
4326 if (init_clock_sig(rio)) {
4330 # elif defined(HAVE_IO_COMPLETION_PORT)
4331 if (!io_completion_port_add_clock_io(rio)) {
4340 LINK_SLIST(refio, rio, next);
4345 add_fd_to_list(rio->fd, FD_TYPE_FILE);
4353 * io_closeclock - close the clock in the I/O structure given
4357 struct refclockio *rio
4360 struct refclockio *unlinked;
4365 * Remove structure from the list
4367 rio->active = FALSE;
4368 UNLINK_SLIST(unlinked, refio, rio, next, struct refclockio);
4369 if (NULL != unlinked) {
4370 /* Close the descriptor. The order of operations is
4371 * important here in case of async / overlapped IO:
4372 * only after we have removed the clock from the
4373 * IO completion port we can be sure no further
4374 * input is queued. So...
4375 * - we first disable feeding to the queu by removing
4376 * the clock from the IO engine
4377 * - close the file (which brings down any IO on it)
4378 * - clear the buffer from results for this fd
4380 # ifdef HAVE_IO_COMPLETION_PORT
4381 io_completion_port_remove_clock_io(rio);
4383 close_and_delete_fd_from_list(rio->fd);
4384 purge_recv_buffers_for_fd(rio->fd);
4390 #endif /* REFCLOCK */
4394 * On NT a SOCKET is an unsigned int so we cannot possibly keep it in
4395 * an array. So we use one of the ISC_LIST functions to hold the
4396 * socket value and use that when we want to enumerate it.
4398 * This routine is called by the forked intres child process to close
4399 * all open sockets. On Windows there's no need as intres runs in
4400 * the same process as a thread.
4411 * In the child process we do not maintain activefds and
4412 * maxactivefd. Zeroing maxactivefd disables code which
4413 * maintains it in close_and_delete_fd_from_list().
4417 while (fd_list != NULL)
4418 close_and_delete_fd_from_list(fd_list->fd);
4422 #endif /* !SYS_WINNT */
4426 * Add and delete functions for the list of open sockets
4434 vsock_t *lsock = emalloc(sizeof(*lsock));
4439 LINK_SLIST(fd_list, lsock, link);
4440 maintain_activefds(fd, 0);
4445 close_and_delete_fd_from_list(
4451 UNLINK_EXPR_SLIST(lsock, fd_list, fd ==
4452 UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t);
4457 switch (lsock->type) {
4459 case FD_TYPE_SOCKET:
4460 closesocket(lsock->fd);
4464 closeserial((int)lsock->fd);
4469 "internal error - illegal descriptor type %d - EXITING",
4476 * remove from activefds
4478 maintain_activefds(fd, 1);
4491 if (find_addr_in_list(addr) == NULL) {
4493 /* not there yet - add to list */
4494 laddr = emalloc(sizeof(*laddr));
4495 laddr->addr = *addr;
4498 LINK_SLIST(remoteaddr_list, laddr, link);
4500 DPRINTF(4, ("Added addr %s to list of addresses\n",
4504 DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
4511 delete_addr_from_list(
4515 remaddr_t *unlinked;
4517 UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr,
4518 &(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t);
4520 if (unlinked != NULL) {
4521 DPRINTF(4, ("Deleted addr %s from list of addresses\n",
4529 delete_interface_from_list(
4533 remaddr_t *unlinked;
4536 UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface ==
4537 UNLINK_EXPR_SLIST_CURRENT()->ep, link,
4540 if (unlinked == NULL)
4542 DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
4543 stoa(&unlinked->addr), iface->ifnum,
4550 static struct interface *
4557 DPRINTF(4, ("Searching for addr %s in list of addresses - ",
4560 for (entry = remoteaddr_list;
4562 entry = entry->link)
4563 if (SOCK_EQ(&entry->addr, addr)) {
4564 DPRINTF(4, ("FOUND\n"));
4568 DPRINTF(4, ("NOT FOUND\n"));
4574 * Find the given address with the all given flags set in the list
4577 find_flagged_addr_in_list(
4584 DPRINTF(4, ("Finding addr %s with flags %d in list: ",
4585 stoa(addr), flags));
4587 for (entry = remoteaddr_list;
4589 entry = entry->link)
4591 if (SOCK_EQ(&entry->addr, addr)
4592 && (entry->ep->flags & flags) == flags) {
4594 DPRINTF(4, ("FOUND\n"));
4598 DPRINTF(4, ("NOT FOUND\n"));
4614 #ifdef HAS_ROUTING_SOCKET
4615 # ifndef UPDATE_GRACE
4616 # define UPDATE_GRACE 2 /* wait UPDATE_GRACE seconds before scanning */
4620 process_routing_msgs(struct asyncio_reader *reader)
4624 #ifdef HAVE_RTNETLINK
4625 struct nlmsghdr *nh;
4627 struct rt_msghdr rtm;
4631 if (disable_dynamic_updates) {
4633 * discard ourselves if we are not needed any more
4634 * usually happens when running unprivileged
4636 remove_asyncio_reader(reader);
4637 delete_asyncio_reader(reader);
4641 cnt = read(reader->fd, buffer, sizeof(buffer));
4644 if (errno == ENOBUFS) {
4646 "routing socket reports: %m");
4649 "routing socket reports: %m - disabling");
4650 remove_asyncio_reader(reader);
4651 delete_asyncio_reader(reader);
4657 * process routing message
4659 #ifdef HAVE_RTNETLINK
4660 for (nh = UA_PTR(struct nlmsghdr, buffer);
4662 nh = NLMSG_NEXT(nh, cnt)) {
4663 msg_type = nh->nlmsg_type;
4666 (p + sizeof(struct rt_msghdr)) <= (buffer + cnt);
4667 p += rtm.rtm_msglen) {
4668 memcpy(&rtm, p, sizeof(rtm));
4669 if (rtm.rtm_version != RTM_VERSION) {
4671 "version mismatch (got %d - expected %d) on routing socket - disabling",
4672 rtm.rtm_version, RTM_VERSION);
4674 remove_asyncio_reader(reader);
4675 delete_asyncio_reader(reader);
4678 msg_type = rtm.rtm_type;
4705 #ifdef RTM_IFANNOUNCE
4706 case RTM_IFANNOUNCE:
4721 * we are keen on new and deleted addresses and
4722 * if an interface goes up and down or routing
4725 DPRINTF(3, ("routing message op = %d: scheduling interface update\n",
4727 timer_interfacetimeout(current_time + UPDATE_GRACE);
4729 #ifdef HAVE_RTNETLINK
4731 /* end of multipart message */
4736 * the rest doesn't bother us.
4738 DPRINTF(4, ("routing message op = %d: ignored\n",
4746 * set up routing notifications
4749 init_async_notifications()
4751 struct asyncio_reader *reader;
4752 #ifdef HAVE_RTNETLINK
4753 int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
4754 struct sockaddr_nl sa;
4756 int fd = socket(PF_ROUTE, SOCK_RAW, 0);
4760 "unable to open routing socket (%m) - using polled interface update");
4765 #ifdef HAVE_RTNETLINK
4767 sa.nl_family = PF_NETLINK;
4768 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR
4769 | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE
4770 | RTMGRP_IPV4_MROUTE | RTMGRP_IPV6_ROUTE
4771 | RTMGRP_IPV6_MROUTE;
4772 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
4774 "bind failed on routing socket (%m) - using polled interface update");
4778 make_socket_nonblocking(fd);
4779 #if defined(HAVE_SIGNALED_IO)
4780 init_socket_sig(fd);
4781 #endif /* HAVE_SIGNALED_IO */
4783 reader = new_asyncio_reader();
4786 reader->receiver = process_routing_msgs;
4788 add_asyncio_reader(reader, FD_TYPE_SOCKET);
4790 "Listening on routing socket on fd #%d for interface updates",
4794 /* HAS_ROUTING_SOCKET not defined */
4796 init_async_notifications(void)