xref: /netbsd-src/external/bsd/ntp/dist/ntpd/ntp_io.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ntp_io.c,v 1.26 2018/04/07 00:19:52 christos Exp $	*/
2 
3 /*
4  * ntp_io.c - input/output routines for ntpd.	The socket-opening code
5  *		   was shamelessly stolen from ntpd.
6  */
7 
8 #ifdef HAVE_CONFIG_H
9 # include <config.h>
10 #endif
11 
12 #include <stdio.h>
13 #include <signal.h>
14 #ifdef HAVE_FNMATCH_H
15 # include <fnmatch.h>
16 # if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE)
17 #  define FNM_CASEFOLD FNM_IGNORECASE
18 # endif
19 #endif
20 #ifdef HAVE_SYS_PARAM_H
21 # include <sys/param.h>
22 #endif
23 #ifdef HAVE_SYS_IOCTL_H
24 # include <sys/ioctl.h>
25 #endif
26 #ifdef HAVE_SYS_SOCKIO_H	/* UXPV: SIOC* #defines (Frank Vance <fvance@waii.com>) */
27 # include <sys/sockio.h>
28 #endif
29 #ifdef HAVE_SYS_UIO_H
30 # include <sys/uio.h>
31 #endif
32 
33 #include "ntp_machine.h"
34 #include "ntpd.h"
35 #include "ntp_io.h"
36 #include "iosignal.h"
37 #include "ntp_lists.h"
38 #include "ntp_refclock.h"
39 #include "ntp_stdlib.h"
40 #include "ntp_worker.h"
41 #include "ntp_request.h"
42 #include "ntp_assert.h"
43 #include "timevalops.h"
44 #include "timespecops.h"
45 #include "ntpd-opts.h"
46 #include "safecast.h"
47 
48 /* Don't include ISC's version of IPv6 variables and structures */
49 #define ISC_IPV6_H 1
50 #include <isc/mem.h>
51 #include <isc/interfaceiter.h>
52 #include <isc/netaddr.h>
53 #include <isc/result.h>
54 #include <isc/sockaddr.h>
55 
56 #ifdef SIM
57 #include "ntpsim.h"
58 #endif
59 
60 #ifdef HAS_ROUTING_SOCKET
61 # include <net/route.h>
62 # ifdef HAVE_RTNETLINK
63 #  include <linux/rtnetlink.h>
64 # endif
65 #endif
66 
67 /*
68  * setsockopt does not always have the same arg declaration
69  * across all platforms. If it's not defined we make it empty
70  */
71 
72 #ifndef SETSOCKOPT_ARG_CAST
73 #define SETSOCKOPT_ARG_CAST
74 #endif
75 
76 extern int listen_to_virtual_ips;
77 
78 #ifndef IPTOS_DSCP_EF
79 #define IPTOS_DSCP_EF 0xb8
80 #endif
81 int qos = IPTOS_DSCP_EF;	/* QoS RFC3246 */
82 
83 #ifdef LEAP_SMEAR
84 /* TODO burnicki: This should be moved to ntp_timer.c, but if we do so
85  * we get a linker error. Since we're running out of time before the leap
86  * second occurs, we let it here where it just works.
87  */
88 int leap_smear_intv;
89 #endif
90 
91 /*
92  * NIC rule entry
93  */
94 typedef struct nic_rule_tag nic_rule;
95 
96 struct nic_rule_tag {
97 	nic_rule *	next;
98 	nic_rule_action	action;
99 	nic_rule_match	match_type;
100 	char *		if_name;
101 	sockaddr_u	addr;
102 	int		prefixlen;
103 };
104 
105 /*
106  * NIC rule listhead.  Entries are added at the head so that the first
107  * match in the list is the last matching rule specified.
108  */
109 nic_rule *nic_rule_list;
110 
111 
112 #if defined(SO_BINTIME) && defined(SCM_BINTIME) && defined(CMSG_FIRSTHDR)
113 #  define HAVE_PACKET_TIMESTAMP
114 #  define HAVE_BINTIME
115 #  ifdef BINTIME_CTLMSGBUF_SIZE
116 #   define CMSG_BUFSIZE BINTIME_CTLMSGBUF_SIZE
117 #  else
118 #   define CMSG_BUFSIZE  1536 /* moderate default */
119 #  endif
120 #elif defined(SO_TIMESTAMPNS) && defined(SCM_TIMESTAMPNS) && defined(CMSG_FIRSTHDR)
121 #  define HAVE_PACKET_TIMESTAMP
122 #  define HAVE_TIMESTAMPNS
123 #  ifdef TIMESTAMPNS_CTLMSGBUF_SIZE
124 #   define CMSG_BUFSIZE TIMESTAMPNS_CTLMSGBUF_SIZE
125 #  else
126 #   define CMSG_BUFSIZE  1536 /* moderate default */
127 #  endif
128 #elif defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP) && defined(CMSG_FIRSTHDR)
129 #  define HAVE_PACKET_TIMESTAMP
130 #  define HAVE_TIMESTAMP
131 #  ifdef TIMESTAMP_CTLMSGBUF_SIZE
132 #   define CMSG_BUFSIZE TIMESTAMP_CTLMSGBUF_SIZE
133 #  else
134 #   define CMSG_BUFSIZE  1536 /* moderate default */
135 #  endif
136 #else
137 /* fill in for old/other timestamp interfaces */
138 #endif
139 
140 #if defined(SYS_WINNT)
141 #include "win32_io.h"
142 #include <isc/win32os.h>
143 #endif
144 
145 /*
146  * We do asynchronous input using the SIGIO facility.  A number of
147  * recvbuf buffers are preallocated for input.	In the signal
148  * handler we poll to see which sockets are ready and read the
149  * packets from them into the recvbuf's along with a time stamp and
150  * an indication of the source host and the interface it was received
151  * through.  This allows us to get as accurate receive time stamps
152  * as possible independent of other processing going on.
153  *
154  * We watch the number of recvbufs available to the signal handler
155  * and allocate more when this number drops below the low water
156  * mark.  If the signal handler should run out of buffers in the
157  * interim it will drop incoming frames, the idea being that it is
158  * better to drop a packet than to be inaccurate.
159  */
160 
161 
162 /*
163  * Other statistics of possible interest
164  */
165 volatile u_long packets_dropped;	/* total number of packets dropped on reception */
166 volatile u_long packets_ignored;	/* packets received on wild card interface */
167 volatile u_long packets_received;	/* total number of packets received */
168 	 u_long packets_sent;		/* total number of packets sent */
169 	 u_long packets_notsent;	/* total number of packets which couldn't be sent */
170 
171 volatile u_long handler_calls;	/* number of calls to interrupt handler */
172 volatile u_long handler_pkts;	/* number of pkts received by handler */
173 u_long io_timereset;		/* time counters were reset */
174 
175 /*
176  * Interface stuff
177  */
178 endpt *	any_interface;		/* wildcard ipv4 interface */
179 endpt *	any6_interface;		/* wildcard ipv6 interface */
180 endpt *	loopback_interface;	/* loopback ipv4 interface */
181 
182 isc_boolean_t broadcast_client_enabled;	/* is broadcast client enabled */
183 u_int sys_ifnum;			/* next .ifnum to assign */
184 int ninterfaces;			/* Total number of interfaces */
185 
186 int disable_dynamic_updates;		/* scan interfaces once only */
187 
188 #ifdef REFCLOCK
189 /*
190  * Refclock stuff.	We keep a chain of structures with data concerning
191  * the guys we are doing I/O for.
192  */
193 static	struct refclockio *refio;
194 #endif /* REFCLOCK */
195 
196 /*
197  * File descriptor masks etc. for call to select
198  * Not needed for I/O Completion Ports or anything outside this file
199  */
200 static fd_set activefds;
201 static int maxactivefd;
202 
203 /*
204  * bit alternating value to detect verified interfaces during an update cycle
205  */
206 static  u_short		sys_interphase = 0;
207 
208 static endpt *	new_interface(endpt *);
209 static void	add_interface(endpt *);
210 static int	update_interfaces(u_short, interface_receiver_t,
211 				  void *);
212 static void	remove_interface(endpt *);
213 static endpt *	create_interface(u_short, endpt *);
214 
215 static int	is_wildcard_addr	(const sockaddr_u *);
216 
217 /*
218  * Multicast functions
219  */
220 static	isc_boolean_t	addr_ismulticast	(sockaddr_u *);
221 static	isc_boolean_t	is_anycast		(sockaddr_u *,
222 						 const char *);
223 
224 /*
225  * Not all platforms support multicast
226  */
227 #ifdef MCAST
228 static	isc_boolean_t	socket_multicast_enable	(endpt *, sockaddr_u *);
229 static	isc_boolean_t	socket_multicast_disable(endpt *, sockaddr_u *);
230 #endif
231 
232 #ifdef DEBUG
233 static void interface_dump	(const endpt *);
234 static void sockaddr_dump	(const sockaddr_u *);
235 static void print_interface	(const endpt *, const char *, const char *);
236 #define DPRINT_INTERFACE(level, args) do { if (debug >= (level)) { print_interface args; } } while (0)
237 #else
238 #define DPRINT_INTERFACE(level, args) do {} while (0)
239 #endif
240 
241 typedef struct vsock vsock_t;
242 enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE };
243 
244 struct vsock {
245 	vsock_t	*	link;
246 	SOCKET		fd;
247 	enum desc_type	type;
248 };
249 
250 vsock_t	*fd_list;
251 
252 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
253 /*
254  * async notification processing (e. g. routing sockets)
255  */
256 /*
257  * support for receiving data on fd that is not a refclock or a socket
258  * like e. g. routing sockets
259  */
260 struct asyncio_reader {
261 	struct asyncio_reader *link;		    /* the list this is being kept in */
262 	SOCKET fd;				    /* fd to be read */
263 	void  *data;				    /* possibly local data */
264 	void (*receiver)(struct asyncio_reader *);  /* input handler */
265 };
266 
267 struct asyncio_reader *asyncio_reader_list;
268 
269 static void delete_asyncio_reader (struct asyncio_reader *);
270 static struct asyncio_reader *new_asyncio_reader (void);
271 static void add_asyncio_reader (struct asyncio_reader *, enum desc_type);
272 static void remove_asyncio_reader (struct asyncio_reader *);
273 
274 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
275 
276 static void init_async_notifications (void);
277 
278 static	int	addr_eqprefix	(const sockaddr_u *, const sockaddr_u *,
279 				 int);
280 static int	addr_samesubnet	(const sockaddr_u *, const sockaddr_u *,
281 				 const sockaddr_u *, const sockaddr_u *);
282 static	int	create_sockets	(u_short);
283 static	SOCKET	open_socket	(sockaddr_u *, int, int, endpt *);
284 static	void	set_reuseaddr	(int);
285 static	isc_boolean_t	socket_broadcast_enable	 (struct interface *, SOCKET, sockaddr_u *);
286 
287 #if !defined(HAVE_IO_COMPLETION_PORT) && !defined(HAVE_SIGNALED_IO)
288 static	char *	fdbits		(int, const fd_set *);
289 #endif
290 #ifdef  OS_MISSES_SPECIFIC_ROUTE_UPDATES
291 static	isc_boolean_t	socket_broadcast_disable (struct interface *, sockaddr_u *);
292 #endif
293 
294 typedef struct remaddr remaddr_t;
295 
296 struct remaddr {
297 	remaddr_t *		link;
298 	sockaddr_u		addr;
299 	endpt *			ep;
300 };
301 
302 remaddr_t *	remoteaddr_list;
303 endpt *		ep_list;	/* complete endpt list */
304 endpt *		mc4_list;	/* IPv4 mcast-capable unicast endpts */
305 endpt *		mc6_list;	/* IPv6 mcast-capable unicast endpts */
306 
307 static endpt *	wildipv4;
308 static endpt *	wildipv6;
309 
310 #ifdef SYS_WINNT
311 int accept_wildcard_if_for_winnt;
312 #else
313 const int accept_wildcard_if_for_winnt = FALSE;
314 #endif
315 
316 static void	add_fd_to_list		(SOCKET, enum desc_type);
317 static endpt *	find_addr_in_list	(sockaddr_u *);
318 static endpt *	find_flagged_addr_in_list(sockaddr_u *, u_int32);
319 static void	delete_addr_from_list	(sockaddr_u *);
320 static void	delete_interface_from_list(endpt *);
321 static void	close_and_delete_fd_from_list(SOCKET);
322 static void	add_addr_to_list	(sockaddr_u *, endpt *);
323 static void	create_wildcards	(u_short);
324 static endpt *	findlocalinterface	(sockaddr_u *, int, int);
325 static endpt *	findclosestinterface	(sockaddr_u *, int);
326 #ifdef DEBUG
327 static const char *	action_text	(nic_rule_action);
328 #endif
329 static nic_rule_action	interface_action(char *, sockaddr_u *, u_int32);
330 static void		convert_isc_if	(isc_interface_t *,
331 					 endpt *, u_short);
332 static void		calc_addr_distance(sockaddr_u *,
333 					   const sockaddr_u *,
334 					   const sockaddr_u *);
335 static int		cmp_addr_distance(const sockaddr_u *,
336 					  const sockaddr_u *);
337 
338 /*
339  * Routines to read the ntp packets
340  */
341 #if !defined(HAVE_IO_COMPLETION_PORT)
342 static inline int	read_network_packet	(SOCKET, struct interface *, l_fp);
343 static void		ntpd_addremove_io_fd	(int, int, int);
344 static void 		input_handler_scan	(const l_fp*, const fd_set*);
345 static int/*BOOL*/	sanitize_fdset		(int errc);
346 #ifdef REFCLOCK
347 static inline int	read_refclock_packet	(SOCKET, struct refclockio *, l_fp);
348 #endif
349 #ifdef HAVE_SIGNALED_IO
350 static void 		input_handler		(l_fp*);
351 #endif
352 #endif
353 
354 
355 #ifndef HAVE_IO_COMPLETION_PORT
356 void
357 maintain_activefds(
358 	int fd,
359 	int closing
360 	)
361 {
362 	int i;
363 
364 	if (fd < 0 || fd >= FD_SETSIZE) {
365 		msyslog(LOG_ERR,
366 			"Too many sockets in use, FD_SETSIZE %d exceeded by fd %d",
367 			FD_SETSIZE, fd);
368 		exit(1);
369 	}
370 
371 	if (!closing) {
372 		FD_SET(fd, &activefds);
373 		maxactivefd = max(fd, maxactivefd);
374 	} else {
375 		FD_CLR(fd, &activefds);
376 		if (maxactivefd && fd == maxactivefd) {
377 			for (i = maxactivefd - 1; i >= 0; i--)
378 				if (FD_ISSET(i, &activefds)) {
379 					maxactivefd = i;
380 					break;
381 				}
382 			INSIST(fd != maxactivefd);
383 		}
384 	}
385 }
386 #endif	/* !HAVE_IO_COMPLETION_PORT */
387 
388 
389 #ifdef DEBUG_TIMING
390 /*
391  * collect timing information for various processing
392  * paths. currently we only pass them on to the file
393  * for later processing. this could also do histogram
394  * based analysis in other to reduce the load (and skew)
395  * dur to the file output
396  */
397 void
398 collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
399 {
400 	char buf[256];
401 
402 	snprintf(buf, sizeof(buf), "%s %d %s %s",
403 		 (rb != NULL)
404 		     ? ((rb->dstadr != NULL)
405 			    ? stoa(&rb->recv_srcadr)
406 			    : "-REFCLOCK-")
407 		     : "-",
408 		 count, lfptoa(dts, 9), tag);
409 	record_timing_stats(buf);
410 }
411 #endif
412 
413 /*
414  * About dynamic interfaces, sockets, reception and more...
415  *
416  * the code solves following tasks:
417  *
418  *   - keep a current list of active interfaces in order
419  *     to bind to to the interface address on NTP_PORT so that
420  *     all wild and specific bindings for NTP_PORT are taken by ntpd
421  *     to avoid other daemons messing with the time or sockets.
422  *   - all interfaces keep a list of peers that are referencing
423  *     the interface in order to quickly re-assign the peers to
424  *     new interface in case an interface is deleted (=> gone from system or
425  *     down)
426  *   - have a preconfigured socket ready with the right local address
427  *     for transmission and reception
428  *   - have an address list for all destination addresses used within ntpd
429  *     to find the "right" preconfigured socket.
430  *   - facilitate updating the internal interface list with respect to
431  *     the current kernel state
432  *
433  * special issues:
434  *
435  *   - mapping of multicast addresses to the interface affected is not always
436  *     one to one - especially on hosts with multiple interfaces
437  *     the code here currently allocates a separate interface entry for those
438  *     multicast addresses
439  *     iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF)
440  *     in case of failure the multicast address is bound to an existing interface.
441  *   - on some systems it is perfectly legal to assign the same address to
442  *     multiple interfaces. Therefore this code does not keep a list of interfaces
443  *     but a list of interfaces that represent a unique address as determined by the kernel
444  *     by the procedure in findlocalinterface. Thus it is perfectly legal to see only
445  *     one representative of a group of real interfaces if they share the same address.
446  *
447  * Frank Kardel 20050910
448  */
449 
450 /*
451  * init_io - initialize I/O module.
452  */
453 void
454 init_io(void)
455 {
456 	/* Init buffer free list and stat counters */
457 	init_recvbuff(RECV_INIT);
458 	/* update interface every 5 minutes as default */
459 	interface_interval = 300;
460 
461 #ifdef WORK_PIPE
462 	addremove_io_fd = &ntpd_addremove_io_fd;
463 #endif
464 
465 #if defined(SYS_WINNT)
466 	init_io_completion_port();
467 #elif defined(HAVE_SIGNALED_IO)
468 	(void) set_signal(input_handler);
469 #endif
470 }
471 
472 
473 static void
474 ntpd_addremove_io_fd(
475 	int	fd,
476 	int	is_pipe,
477 	int	remove_it
478 	)
479 {
480 	UNUSED_ARG(is_pipe);
481 
482 #ifdef HAVE_SIGNALED_IO
483 	if (!remove_it)
484 		init_socket_sig(fd);
485 #endif /* not HAVE_SIGNALED_IO */
486 
487 	maintain_activefds(fd, remove_it);
488 }
489 
490 
491 /*
492  * io_open_sockets - call socket creation routine
493  */
494 void
495 io_open_sockets(void)
496 {
497 	static int already_opened;
498 
499 	if (already_opened || HAVE_OPT( SAVECONFIGQUIT ))
500 		return;
501 
502 	already_opened = 1;
503 
504 	/*
505 	 * Create the sockets
506 	 */
507 	BLOCKIO();
508 	create_sockets(NTP_PORT);
509 	UNBLOCKIO();
510 
511 	init_async_notifications();
512 
513 	DPRINTF(3, ("io_open_sockets: maxactivefd %d\n", maxactivefd));
514 }
515 
516 
517 #ifdef DEBUG
518 /*
519  * function to dump the contents of the interface structure
520  * for debugging use only.
521  * We face a dilemma here -- sockets are FDs under POSIX and
522  * actually HANDLES under Windows. So we use '%lld' as format
523  * and cast the value to 'long long'; this should not hurt
524  * with UNIX-like systems and does not truncate values on Win64.
525  */
526 void
527 interface_dump(const endpt *itf)
528 {
529 	printf("Dumping interface: %p\n", itf);
530 	printf("fd = %lld\n", (long long)itf->fd);
531 	printf("bfd = %lld\n", (long long)itf->bfd);
532 	printf("sin = %s,\n", stoa(&itf->sin));
533 	sockaddr_dump(&itf->sin);
534 	printf("bcast = %s,\n", stoa(&itf->bcast));
535 	sockaddr_dump(&itf->bcast);
536 	printf("mask = %s,\n", stoa(&itf->mask));
537 	sockaddr_dump(&itf->mask);
538 	printf("name = %s\n", itf->name);
539 	printf("flags = 0x%08x\n", itf->flags);
540 	printf("last_ttl = %d\n", itf->last_ttl);
541 	printf("addr_refid = %08x\n", itf->addr_refid);
542 	printf("num_mcast = %d\n", itf->num_mcast);
543 	printf("received = %ld\n", itf->received);
544 	printf("sent = %ld\n", itf->sent);
545 	printf("notsent = %ld\n", itf->notsent);
546 	printf("ifindex = %u\n", itf->ifindex);
547 	printf("peercnt = %u\n", itf->peercnt);
548 	printf("phase = %u\n", itf->phase);
549 }
550 
551 /*
552  * sockaddr_dump - hex dump the start of a sockaddr_u
553  */
554 static void
555 sockaddr_dump(const sockaddr_u *psau)
556 {
557 	/* Limit the size of the sockaddr_in6 hex dump */
558 	const int maxsize = min(32, sizeof(psau->sa6));
559 	const u_char *	cp;
560 	int		i;
561 
562 	/* XXX: Should we limit maxsize based on psau->saX.sin_family? */
563 	cp = (const void *)&psau->sa6;
564 
565 	for(i = 0; i < maxsize; i++) {
566 		printf("%02x", *cp++);
567 		if (!((i + 1) % 4))
568 			printf(" ");
569 	}
570 	printf("\n");
571 }
572 
573 /*
574  * print_interface - helper to output debug information
575  */
576 static void
577 print_interface(const endpt *iface, const char *pfx, const char *sfx)
578 {
579 	printf("%sinterface #%d: fd=%lld, bfd=%lld, name=%s, flags=0x%x, ifindex=%u, sin=%s",
580 	       pfx,
581 	       iface->ifnum,
582 	       (long long)iface->fd,
583 	       (long long)iface->bfd,
584 	       iface->name,
585 	       iface->flags,
586 	       iface->ifindex,
587 	       stoa(&iface->sin));
588 	if (AF_INET == iface->family) {
589 		if (iface->flags & INT_BROADCAST)
590 			printf(", bcast=%s", stoa(&iface->bcast));
591 		printf(", mask=%s", stoa(&iface->mask));
592 	}
593 	printf(", %s:%s",
594 	       (iface->ignore_packets)
595 		   ? "Disabled"
596 		   : "Enabled",
597 	       sfx);
598 	if (debug > 4)	/* in-depth debugging only */
599 		interface_dump(iface);
600 }
601 #endif
602 
603 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
604 /*
605  * create an asyncio_reader structure
606  */
607 static struct asyncio_reader *
608 new_asyncio_reader(void)
609 {
610 	struct asyncio_reader *reader;
611 
612 	reader = emalloc_zero(sizeof(*reader));
613 	reader->fd = INVALID_SOCKET;
614 
615 	return reader;
616 }
617 
618 /*
619  * delete a reader
620  */
621 static void
622 delete_asyncio_reader(
623 	struct asyncio_reader *reader
624 	)
625 {
626 	free(reader);
627 }
628 
629 /*
630  * add asynchio_reader
631  */
632 static void
633 add_asyncio_reader(
634 	struct asyncio_reader *	reader,
635 	enum desc_type		type)
636 {
637 	LINK_SLIST(asyncio_reader_list, reader, link);
638 	add_fd_to_list(reader->fd, type);
639 }
640 
641 /*
642  * remove asynchio_reader
643  */
644 static void
645 remove_asyncio_reader(
646 	struct asyncio_reader *reader
647 	)
648 {
649 	struct asyncio_reader *unlinked;
650 
651 	UNLINK_SLIST(unlinked, asyncio_reader_list, reader, link,
652 	    struct asyncio_reader);
653 
654 	if (reader->fd != INVALID_SOCKET)
655 		close_and_delete_fd_from_list(reader->fd);
656 
657 	reader->fd = INVALID_SOCKET;
658 }
659 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
660 
661 
662 /* compare two sockaddr prefixes */
663 static int
664 addr_eqprefix(
665 	const sockaddr_u *	a,
666 	const sockaddr_u *	b,
667 	int			prefixlen
668 	)
669 {
670 	isc_netaddr_t		isc_a;
671 	isc_netaddr_t		isc_b;
672 	isc_sockaddr_t		isc_sa;
673 
674 	ZERO(isc_sa);
675 	memcpy(&isc_sa.type, a, min(sizeof(isc_sa.type), sizeof(*a)));
676 	isc_netaddr_fromsockaddr(&isc_a, &isc_sa);
677 
678 	ZERO(isc_sa);
679 	memcpy(&isc_sa.type, b, min(sizeof(isc_sa.type), sizeof(*b)));
680 	isc_netaddr_fromsockaddr(&isc_b, &isc_sa);
681 
682 	return (int)isc_netaddr_eqprefix(&isc_a, &isc_b,
683 					 (u_int)prefixlen);
684 }
685 
686 
687 static int
688 addr_samesubnet(
689 	const sockaddr_u *	a,
690 	const sockaddr_u *	a_mask,
691 	const sockaddr_u *	b,
692 	const sockaddr_u *	b_mask
693 	)
694 {
695 	const u_int32 *	pa;
696 	const u_int32 *	pa_limit;
697 	const u_int32 *	pb;
698 	const u_int32 *	pm;
699 	size_t		loops;
700 
701 	REQUIRE(AF(a) == AF(a_mask));
702 	REQUIRE(AF(b) == AF(b_mask));
703 	/*
704 	 * With address and mask families verified to match, comparing
705 	 * the masks also validates the address's families match.
706 	 */
707 	if (!SOCK_EQ(a_mask, b_mask))
708 		return FALSE;
709 
710 	if (IS_IPV6(a)) {
711 		loops = sizeof(NSRCADR6(a)) / sizeof(*pa);
712 		pa = (const void *)&NSRCADR6(a);
713 		pb = (const void *)&NSRCADR6(b);
714 		pm = (const void *)&NSRCADR6(a_mask);
715 	} else {
716 		loops = sizeof(NSRCADR(a)) / sizeof(*pa);
717 		pa = (const void *)&NSRCADR(a);
718 		pb = (const void *)&NSRCADR(b);
719 		pm = (const void *)&NSRCADR(a_mask);
720 	}
721 	for (pa_limit = pa + loops; pa < pa_limit; pa++, pb++, pm++)
722 		if ((*pa & *pm) != (*pb & *pm))
723 			return FALSE;
724 
725 	return TRUE;
726 }
727 
728 
729 /*
730  * interface list enumerator - visitor pattern
731  */
732 void
733 interface_enumerate(
734 	interface_receiver_t	receiver,
735 	void *			data
736 	)
737 {
738 	interface_info_t ifi;
739 
740 	ifi.action = IFS_EXISTS;
741 	for (ifi.ep = ep_list; ifi.ep != NULL; ifi.ep = ifi.ep->elink)
742 		(*receiver)(data, &ifi);
743 }
744 
745 /*
746  * do standard initialization of interface structure
747  */
748 static void
749 init_interface(
750 	endpt *ep
751 	)
752 {
753 	ZERO(*ep);
754 	ep->fd = INVALID_SOCKET;
755 	ep->bfd = INVALID_SOCKET;
756 	ep->phase = sys_interphase;
757 }
758 
759 
760 /*
761  * create new interface structure initialize from
762  * template structure or via standard initialization
763  * function
764  */
765 static struct interface *
766 new_interface(
767 	struct interface *interface
768 	)
769 {
770 	struct interface *	iface;
771 
772 	iface = emalloc(sizeof(*iface));
773 
774 	if (NULL == interface)
775 		init_interface(iface);
776 	else				/* use the template */
777 		memcpy(iface, interface, sizeof(*iface));
778 
779 	/* count every new instance of an interface in the system */
780 	iface->ifnum = sys_ifnum++;
781 	iface->starttime = current_time;
782 
783 #   ifdef HAVE_IO_COMPLETION_PORT
784 	if (!io_completion_port_add_interface(iface)) {
785 		msyslog(LOG_EMERG, "cannot register interface with IO engine -- will exit now");
786 		exit(1);
787 	}
788 #   endif
789 	return iface;
790 }
791 
792 
793 /*
794  * return interface storage into free memory pool
795  */
796 static void
797 delete_interface(
798 	endpt *ep
799 	)
800 {
801 #    ifdef HAVE_IO_COMPLETION_PORT
802 	io_completion_port_remove_interface(ep);
803 #    endif
804 	free(ep);
805 }
806 
807 
808 /*
809  * link interface into list of known interfaces
810  */
811 static void
812 add_interface(
813 	endpt *	ep
814 	)
815 {
816 	endpt **	pmclisthead;
817 	endpt *		scan;
818 	endpt *		scan_next;
819 	endpt *		unlinked;
820 	sockaddr_u *	addr;
821 	int		ep_local;
822 	int		scan_local;
823 	int		same_subnet;
824 	int		ep_univ_iid;	/* iface ID from MAC address */
825 	int		scan_univ_iid;	/* see RFC 4291 */
826 	int		ep_privacy;	/* random local iface ID */
827 	int		scan_privacy;	/* see RFC 4941 */
828 	int		rc;
829 
830 	/* Calculate the refid */
831 	ep->addr_refid = addr2refid(&ep->sin);
832 	/* link at tail so ntpdc -c ifstats index increases each row */
833 	LINK_TAIL_SLIST(ep_list, ep, elink, endpt);
834 	ninterfaces++;
835 #ifdef MCAST
836 	/* the rest is for enabled multicast-capable addresses only */
837 	if (ep->ignore_packets || !(INT_MULTICAST & ep->flags) ||
838 	    INT_LOOPBACK & ep->flags)
839 		return;
840 # ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
841 	if (AF_INET6 == ep->family)
842 		return;
843 # endif
844 	pmclisthead = (AF_INET == ep->family)
845 			 ? &mc4_list
846 			 : &mc6_list;
847 
848 	if (AF_INET6 == ep->family) {
849 		ep_local =
850 		    IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&ep->sin)) ||
851 		    IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(&ep->sin));
852 		ep_univ_iid = IS_IID_UNIV(&ep->sin);
853 		ep_privacy = !!(INT_PRIVACY & ep->flags);
854 	} else {
855 		ep_local = FALSE;
856 		ep_univ_iid = FALSE;
857 		ep_privacy = FALSE;
858 	}
859 	DPRINTF(4, ("add_interface mcast-capable %s%s%s%s\n",
860 		    stoa(&ep->sin),
861 		    (ep_local) ? " link/scope-local" : "",
862 		    (ep_univ_iid) ? " univ-IID" : "",
863 		    (ep_privacy) ? " privacy" : ""));
864 	/*
865 	 * If we have multiple local addresses on the same network
866 	 * interface, and some are link- or site-local, do not multicast
867 	 * out from the link-/site-local addresses by default, to avoid
868 	 * duplicate manycastclient associations between v6 peers using
869 	 * link-local and global addresses.  link-local can still be
870 	 * chosen using "nic ignore myv6globalprefix::/64".
871 	 * Similarly, if we have multiple global addresses from the same
872 	 * prefix on the same network interface, multicast from one,
873 	 * preferring EUI-64, then static, then least RFC 4941 privacy
874 	 * addresses.
875 	 */
876 	for (scan = *pmclisthead; scan != NULL; scan = scan_next) {
877 		scan_next = scan->mclink;
878 		if (ep->family != scan->family)
879 			continue;
880 		if (strcmp(ep->name, scan->name))
881 			continue;
882 		same_subnet = addr_samesubnet(&ep->sin, &ep->mask,
883 					      &scan->sin, &scan->mask);
884 		if (AF_INET6 == ep->family) {
885 			addr = &scan->sin;
886 			scan_local =
887 			    IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(addr)) ||
888 			    IN6_IS_ADDR_SITELOCAL(PSOCK_ADDR6(addr));
889 			scan_univ_iid = IS_IID_UNIV(addr);
890 			scan_privacy = !!(INT_PRIVACY & scan->flags);
891 		} else {
892 			scan_local = FALSE;
893 			scan_univ_iid = FALSE;
894 			scan_privacy = FALSE;
895 		}
896 		DPRINTF(4, ("add_interface mcast-capable scan %s%s%s%s\n",
897 			    stoa(&scan->sin),
898 			    (scan_local) ? " link/scope-local" : "",
899 			    (scan_univ_iid) ? " univ-IID" : "",
900 			    (scan_privacy) ? " privacy" : ""));
901 		if ((ep_local && !scan_local) || (same_subnet &&
902 		    ((ep_privacy && !scan_privacy) ||
903 		     (!ep_univ_iid && scan_univ_iid)))) {
904 			DPRINTF(4, ("did not add %s to %s of IPv6 multicast-capable list which already has %s\n",
905 				stoa(&ep->sin),
906 				(ep_local)
907 				    ? "tail"
908 				    : "head",
909 				stoa(&scan->sin)));
910 			return;
911 		}
912 		if ((scan_local && !ep_local) || (same_subnet &&
913 		    ((scan_privacy && !ep_privacy) ||
914 		     (!scan_univ_iid && ep_univ_iid)))) {
915 			UNLINK_SLIST(unlinked, *pmclisthead,
916 				     scan, mclink, endpt);
917 			DPRINTF(4, ("%s %s from IPv6 multicast-capable list to add %s\n",
918 				(unlinked != scan)
919 				    ? "Failed to remove"
920 				    : "removed",
921 				stoa(&scan->sin), stoa(&ep->sin)));
922 		}
923 	}
924 	/*
925 	 * Add link/site local at the tail of the multicast-
926 	 * capable unicast interfaces list, so that ntpd will
927 	 * send from global addresses before link-/site-local
928 	 * ones.
929 	 */
930 	if (ep_local)
931 		LINK_TAIL_SLIST(*pmclisthead, ep, mclink, endpt);
932 	else
933 		LINK_SLIST(*pmclisthead, ep, mclink);
934 	DPRINTF(4, ("added %s to %s of IPv%s multicast-capable unicast local address list\n",
935 		stoa(&ep->sin),
936 		(ep_local)
937 		    ? "tail"
938 		    : "head",
939 		(AF_INET == ep->family)
940 		    ? "4"
941 		    : "6"));
942 
943 	if (INVALID_SOCKET == ep->fd)
944 		return;
945 
946 	/*
947 	 * select the local address from which to send to multicast.
948 	 */
949 	switch (AF(&ep->sin)) {
950 
951 	case AF_INET :
952 		rc = setsockopt(ep->fd, IPPROTO_IP,
953 				IP_MULTICAST_IF,
954 				(void *)&NSRCADR(&ep->sin),
955 				sizeof(NSRCADR(&ep->sin)));
956 		if (rc)
957 			msyslog(LOG_ERR,
958 				"setsockopt IP_MULTICAST_IF %s fails: %m",
959 				stoa(&ep->sin));
960 		break;
961 
962 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
963 	case AF_INET6 :
964 		rc = setsockopt(ep->fd, IPPROTO_IPV6,
965 				 IPV6_MULTICAST_IF,
966 				 (void *)&ep->ifindex,
967 				 sizeof(ep->ifindex));
968 		/* do not complain if bound addr scope is ifindex */
969 		if (rc && ep->ifindex != SCOPE(&ep->sin))
970 			msyslog(LOG_ERR,
971 				"setsockopt IPV6_MULTICAST_IF %u for %s fails: %m",
972 				ep->ifindex, stoa(&ep->sin));
973 		break;
974 # endif
975 	}
976 #endif	/* MCAST */
977 }
978 
979 
980 /*
981  * remove interface from known interface list and clean up
982  * associated resources
983  */
984 static void
985 remove_interface(
986 	endpt *	ep
987 	)
988 {
989 	endpt *		unlinked;
990 	endpt **	pmclisthead;
991 	sockaddr_u	resmask;
992 
993 	UNLINK_SLIST(unlinked, ep_list, ep, elink, endpt);
994 	if (!ep->ignore_packets && INT_MULTICAST & ep->flags) {
995 		pmclisthead = (AF_INET == ep->family)
996 				 ? &mc4_list
997 				 : &mc6_list;
998 		UNLINK_SLIST(unlinked, *pmclisthead, ep, mclink, endpt);
999 		DPRINTF(4, ("%s %s IPv%s multicast-capable unicast local address list\n",
1000 			stoa(&ep->sin),
1001 			(unlinked != NULL)
1002 			    ? "removed from"
1003 			    : "not found on",
1004 			(AF_INET == ep->family)
1005 			    ? "4"
1006 			    : "6"));
1007 	}
1008 	delete_interface_from_list(ep);
1009 
1010 	if (ep->fd != INVALID_SOCKET) {
1011 		msyslog(LOG_INFO,
1012 			"Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
1013 			ep->ifnum,
1014 			ep->name,
1015 			stoa(&ep->sin),
1016 			SRCPORT(&ep->sin),
1017 			ep->received,
1018 			ep->sent,
1019 			ep->notsent,
1020 			current_time - ep->starttime);
1021 #	    ifdef HAVE_IO_COMPLETION_PORT
1022 		io_completion_port_remove_socket(ep->fd, ep);
1023 #	    endif
1024 		close_and_delete_fd_from_list(ep->fd);
1025 		ep->fd = INVALID_SOCKET;
1026 	}
1027 
1028 	if (ep->bfd != INVALID_SOCKET) {
1029 		msyslog(LOG_INFO,
1030 			"stop listening for broadcasts to %s on interface #%d %s",
1031 			stoa(&ep->bcast), ep->ifnum, ep->name);
1032 #	    ifdef HAVE_IO_COMPLETION_PORT
1033 		io_completion_port_remove_socket(ep->bfd, ep);
1034 #	    endif
1035 		close_and_delete_fd_from_list(ep->bfd);
1036 		ep->bfd = INVALID_SOCKET;
1037 	}
1038 #   ifdef HAVE_IO_COMPLETION_PORT
1039 	io_completion_port_remove_interface(ep);
1040 #   endif
1041 
1042 	ninterfaces--;
1043 	mon_clearinterface(ep);
1044 
1045 	/* remove restrict interface entry */
1046 	SET_HOSTMASK(&resmask, AF(&ep->sin));
1047 	hack_restrict(RESTRICT_REMOVEIF, &ep->sin, &resmask,
1048 		      -3, RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
1049 }
1050 
1051 
1052 static void
1053 log_listen_address(
1054 	endpt *	ep
1055 	)
1056 {
1057 	msyslog(LOG_INFO, "%s on %d %s %s",
1058 		(ep->ignore_packets)
1059 		    ? "Listen and drop"
1060 		    : "Listen normally",
1061 		ep->ifnum,
1062 		ep->name,
1063 		sptoa(&ep->sin));
1064 }
1065 
1066 
1067 static void
1068 create_wildcards(
1069 	u_short	port
1070 	)
1071 {
1072 	int			v4wild;
1073 #ifdef INCLUDE_IPV6_SUPPORT
1074 	int			v6wild;
1075 #endif
1076 	sockaddr_u		wildaddr;
1077 	nic_rule_action		action;
1078 	struct interface *	wildif;
1079 
1080 	/*
1081 	 * silence "potentially uninitialized" warnings from VC9
1082 	 * failing to follow the logic.  Ideally action could remain
1083 	 * uninitialized, and the memset be the first statement under
1084 	 * the first if (v4wild).
1085 	 */
1086 	action = ACTION_LISTEN;
1087 	ZERO(wildaddr);
1088 
1089 #ifdef INCLUDE_IPV6_SUPPORT
1090 	/*
1091 	 * create pseudo-interface with wildcard IPv6 address
1092 	 */
1093 	v6wild = ipv6_works;
1094 	if (v6wild) {
1095 		/* set wildaddr to the v6 wildcard address :: */
1096 		ZERO(wildaddr);
1097 		AF(&wildaddr) = AF_INET6;
1098 		SET_ADDR6N(&wildaddr, in6addr_any);
1099 		SET_PORT(&wildaddr, port);
1100 		SET_SCOPE(&wildaddr, 0);
1101 
1102 		/* check for interface/nic rules affecting the wildcard */
1103 		action = interface_action(NULL, &wildaddr, 0);
1104 		v6wild = (ACTION_IGNORE != action);
1105 	}
1106 	if (v6wild) {
1107 		wildif = new_interface(NULL);
1108 
1109 		strlcpy(wildif->name, "v6wildcard", sizeof(wildif->name));
1110 		memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1111 		wildif->family = AF_INET6;
1112 		AF(&wildif->mask) = AF_INET6;
1113 		SET_ONESMASK(&wildif->mask);
1114 
1115 		wildif->flags = INT_UP | INT_WILDCARD;
1116 		wildif->ignore_packets = (ACTION_DROP == action);
1117 
1118 		wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1119 
1120 		if (wildif->fd != INVALID_SOCKET) {
1121 			wildipv6 = wildif;
1122 			any6_interface = wildif;
1123 			add_addr_to_list(&wildif->sin, wildif);
1124 			add_interface(wildif);
1125 			log_listen_address(wildif);
1126 		} else {
1127 			msyslog(LOG_ERR,
1128 				"unable to bind to wildcard address %s - another process may be running - EXITING",
1129 				stoa(&wildif->sin));
1130 			exit(1);
1131 		}
1132 		DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1133 	}
1134 #endif
1135 
1136 	/*
1137 	 * create pseudo-interface with wildcard IPv4 address
1138 	 */
1139 	v4wild = ipv4_works;
1140 	if (v4wild) {
1141 		/* set wildaddr to the v4 wildcard address 0.0.0.0 */
1142 		AF(&wildaddr) = AF_INET;
1143 		SET_ADDR4N(&wildaddr, INADDR_ANY);
1144 		SET_PORT(&wildaddr, port);
1145 
1146 		/* check for interface/nic rules affecting the wildcard */
1147 		action = interface_action(NULL, &wildaddr, 0);
1148 		v4wild = (ACTION_IGNORE != action);
1149 	}
1150 	if (v4wild) {
1151 		wildif = new_interface(NULL);
1152 
1153 		strlcpy(wildif->name, "v4wildcard", sizeof(wildif->name));
1154 		memcpy(&wildif->sin, &wildaddr, sizeof(wildif->sin));
1155 		wildif->family = AF_INET;
1156 		AF(&wildif->mask) = AF_INET;
1157 		SET_ONESMASK(&wildif->mask);
1158 
1159 		wildif->flags = INT_BROADCAST | INT_UP | INT_WILDCARD;
1160 		wildif->ignore_packets = (ACTION_DROP == action);
1161 #if defined(MCAST)
1162 		/*
1163 		 * enable multicast reception on the broadcast socket
1164 		 */
1165 		AF(&wildif->bcast) = AF_INET;
1166 		SET_ADDR4N(&wildif->bcast, INADDR_ANY);
1167 		SET_PORT(&wildif->bcast, port);
1168 #endif /* MCAST */
1169 		wildif->fd = open_socket(&wildif->sin, 0, 1, wildif);
1170 
1171 		if (wildif->fd != INVALID_SOCKET) {
1172 			wildipv4 = wildif;
1173 			any_interface = wildif;
1174 
1175 			add_addr_to_list(&wildif->sin, wildif);
1176 			add_interface(wildif);
1177 			log_listen_address(wildif);
1178 		} else {
1179 			msyslog(LOG_ERR,
1180 				"unable to bind to wildcard address %s - another process may be running - EXITING",
1181 				stoa(&wildif->sin));
1182 			exit(1);
1183 		}
1184 		DPRINT_INTERFACE(2, (wildif, "created ", "\n"));
1185 	}
1186 }
1187 
1188 
1189 /*
1190  * add_nic_rule() -- insert a rule entry at the head of nic_rule_list.
1191  */
1192 void
1193 add_nic_rule(
1194 	nic_rule_match	match_type,
1195 	const char *	if_name,	/* interface name or numeric address */
1196 	int		prefixlen,
1197 	nic_rule_action	action
1198 	)
1199 {
1200 	nic_rule *	rule;
1201 	isc_boolean_t	is_ip;
1202 
1203 	rule = emalloc_zero(sizeof(*rule));
1204 	rule->match_type = match_type;
1205 	rule->prefixlen = prefixlen;
1206 	rule->action = action;
1207 
1208 	if (MATCH_IFNAME == match_type) {
1209 		REQUIRE(NULL != if_name);
1210 		rule->if_name = estrdup(if_name);
1211 	} else if (MATCH_IFADDR == match_type) {
1212 		REQUIRE(NULL != if_name);
1213 		/* set rule->addr */
1214 		is_ip = is_ip_address(if_name, AF_UNSPEC, &rule->addr);
1215 		REQUIRE(is_ip);
1216 	} else
1217 		REQUIRE(NULL == if_name);
1218 
1219 	LINK_SLIST(nic_rule_list, rule, next);
1220 }
1221 
1222 
1223 #ifdef DEBUG
1224 static const char *
1225 action_text(
1226 	nic_rule_action	action
1227 	)
1228 {
1229 	const char *t;
1230 
1231 	switch (action) {
1232 
1233 	default:
1234 		t = "ERROR";	/* quiet uninit warning */
1235 		DPRINTF(1, ("fatal: unknown nic_rule_action %d\n",
1236 			    action));
1237 		ENSURE(0);
1238 		break;
1239 
1240 	case ACTION_LISTEN:
1241 		t = "listen";
1242 		break;
1243 
1244 	case ACTION_IGNORE:
1245 		t = "ignore";
1246 		break;
1247 
1248 	case ACTION_DROP:
1249 		t = "drop";
1250 		break;
1251 	}
1252 
1253 	return t;
1254 }
1255 #endif	/* DEBUG */
1256 
1257 
1258 static nic_rule_action
1259 interface_action(
1260 	char *		if_name,
1261 	sockaddr_u *	if_addr,
1262 	u_int32		if_flags
1263 	)
1264 {
1265 	nic_rule *	rule;
1266 	int		isloopback;
1267 	int		iswildcard;
1268 
1269 	DPRINTF(4, ("interface_action: interface %s ",
1270 		    (if_name != NULL) ? if_name : "wildcard"));
1271 
1272 	iswildcard = is_wildcard_addr(if_addr);
1273 	isloopback = !!(INT_LOOPBACK & if_flags);
1274 
1275 	/*
1276 	 * Find any matching NIC rule from --interface / -I or ntp.conf
1277 	 * interface/nic rules.
1278 	 */
1279 	for (rule = nic_rule_list; rule != NULL; rule = rule->next) {
1280 
1281 		switch (rule->match_type) {
1282 
1283 		case MATCH_ALL:
1284 			/* loopback and wildcard excluded from "all" */
1285 			if (isloopback || iswildcard)
1286 				break;
1287 			DPRINTF(4, ("nic all %s\n",
1288 			    action_text(rule->action)));
1289 			return rule->action;
1290 
1291 		case MATCH_IPV4:
1292 			if (IS_IPV4(if_addr)) {
1293 				DPRINTF(4, ("nic ipv4 %s\n",
1294 				    action_text(rule->action)));
1295 				return rule->action;
1296 			}
1297 			break;
1298 
1299 		case MATCH_IPV6:
1300 			if (IS_IPV6(if_addr)) {
1301 				DPRINTF(4, ("nic ipv6 %s\n",
1302 				    action_text(rule->action)));
1303 				return rule->action;
1304 			}
1305 			break;
1306 
1307 		case MATCH_WILDCARD:
1308 			if (iswildcard) {
1309 				DPRINTF(4, ("nic wildcard %s\n",
1310 				    action_text(rule->action)));
1311 				return rule->action;
1312 			}
1313 			break;
1314 
1315 		case MATCH_IFADDR:
1316 			if (rule->prefixlen != -1) {
1317 				if (addr_eqprefix(if_addr, &rule->addr,
1318 						  rule->prefixlen)) {
1319 
1320 					DPRINTF(4, ("subnet address match - %s\n",
1321 					    action_text(rule->action)));
1322 					return rule->action;
1323 				}
1324 			} else
1325 				if (SOCK_EQ(if_addr, &rule->addr)) {
1326 
1327 					DPRINTF(4, ("address match - %s\n",
1328 					    action_text(rule->action)));
1329 					return rule->action;
1330 				}
1331 			break;
1332 
1333 		case MATCH_IFNAME:
1334 			if (if_name != NULL
1335 #if defined(HAVE_FNMATCH) && defined(FNM_CASEFOLD)
1336 			    && !fnmatch(rule->if_name, if_name, FNM_CASEFOLD)
1337 #else
1338 			    && !strcasecmp(if_name, rule->if_name)
1339 #endif
1340 			    ) {
1341 
1342 				DPRINTF(4, ("interface name match - %s\n",
1343 				    action_text(rule->action)));
1344 				return rule->action;
1345 			}
1346 			break;
1347 		}
1348 	}
1349 
1350 	/*
1351 	 * Unless explicitly disabled such as with "nic ignore ::1"
1352 	 * listen on loopback addresses.  Since ntpq and ntpdc query
1353 	 * "localhost" by default, which typically resolves to ::1 and
1354 	 * 127.0.0.1, it's useful to default to listening on both.
1355 	 */
1356 	if (isloopback) {
1357 		DPRINTF(4, ("default loopback listen\n"));
1358 		return ACTION_LISTEN;
1359 	}
1360 
1361 	/*
1362 	 * Treat wildcard addresses specially.  If there is no explicit
1363 	 * "nic ... wildcard" or "nic ... 0.0.0.0" or "nic ... ::" rule
1364 	 * default to drop.
1365 	 */
1366 	if (iswildcard) {
1367 		DPRINTF(4, ("default wildcard drop\n"));
1368 		return ACTION_DROP;
1369 	}
1370 
1371 	/*
1372 	 * Check for "virtual IP" (colon in the interface name) after
1373 	 * the rules so that "ntpd --interface eth0:1 -novirtualips"
1374 	 * does indeed listen on eth0:1's addresses.
1375 	 */
1376 	if (!listen_to_virtual_ips && if_name != NULL
1377 	    && (strchr(if_name, ':') != NULL)) {
1378 
1379 		DPRINTF(4, ("virtual ip - ignore\n"));
1380 		return ACTION_IGNORE;
1381 	}
1382 
1383 	/*
1384 	 * If there are no --interface/-I command-line options and no
1385 	 * interface/nic rules in ntp.conf, the default action is to
1386 	 * listen.  In the presence of rules from either, the default
1387 	 * is to ignore.  This implements ntpd's traditional listen-
1388 	 * every default with no interface listen configuration, and
1389 	 * ensures a single -I eth0 or "nic listen eth0" means do not
1390 	 * listen on any other addresses.
1391 	 */
1392 	if (NULL == nic_rule_list) {
1393 		DPRINTF(4, ("default listen\n"));
1394 		return ACTION_LISTEN;
1395 	}
1396 
1397 	DPRINTF(4, ("implicit ignore\n"));
1398 	return ACTION_IGNORE;
1399 }
1400 
1401 
1402 static void
1403 convert_isc_if(
1404 	isc_interface_t *isc_if,
1405 	endpt *itf,
1406 	u_short port
1407 	)
1408 {
1409 	const u_char v6loop[16] = {0, 0, 0, 0, 0, 0, 0, 0,
1410 				   0, 0, 0, 0, 0, 0, 0, 1};
1411 
1412 	strlcpy(itf->name, isc_if->name, sizeof(itf->name));
1413 	itf->ifindex = isc_if->ifindex;
1414 	itf->family = (u_short)isc_if->af;
1415 	AF(&itf->sin) = itf->family;
1416 	AF(&itf->mask) = itf->family;
1417 	AF(&itf->bcast) = itf->family;
1418 	SET_PORT(&itf->sin, port);
1419 	SET_PORT(&itf->mask, port);
1420 	SET_PORT(&itf->bcast, port);
1421 
1422 	if (IS_IPV4(&itf->sin)) {
1423 		NSRCADR(&itf->sin) = isc_if->address.type.in.s_addr;
1424 		NSRCADR(&itf->mask) = isc_if->netmask.type.in.s_addr;
1425 
1426 		if (isc_if->flags & INTERFACE_F_BROADCAST) {
1427 			itf->flags |= INT_BROADCAST;
1428 			NSRCADR(&itf->bcast) =
1429 			    isc_if->broadcast.type.in.s_addr;
1430 		}
1431 	}
1432 #ifdef INCLUDE_IPV6_SUPPORT
1433 	else if (IS_IPV6(&itf->sin)) {
1434 		SET_ADDR6N(&itf->sin, isc_if->address.type.in6);
1435 		SET_ADDR6N(&itf->mask, isc_if->netmask.type.in6);
1436 
1437 		SET_SCOPE(&itf->sin, isc_if->address.zone);
1438 	}
1439 #endif /* INCLUDE_IPV6_SUPPORT */
1440 
1441 
1442 	/* Process the rest of the flags */
1443 
1444 	itf->flags |=
1445 		  ((INTERFACE_F_UP & isc_if->flags)
1446 			? INT_UP : 0)
1447 		| ((INTERFACE_F_LOOPBACK & isc_if->flags)
1448 			? INT_LOOPBACK : 0)
1449 		| ((INTERFACE_F_POINTTOPOINT & isc_if->flags)
1450 			? INT_PPP : 0)
1451 		| ((INTERFACE_F_MULTICAST & isc_if->flags)
1452 			? INT_MULTICAST : 0)
1453 		| ((INTERFACE_F_PRIVACY & isc_if->flags)
1454 			? INT_PRIVACY : 0)
1455 		;
1456 
1457 	/*
1458 	 * Clear the loopback flag if the address is not localhost.
1459 	 * http://bugs.ntp.org/1683
1460 	 */
1461 	if (INT_LOOPBACK & itf->flags) {
1462 		if (AF_INET == itf->family) {
1463 			if (127 != (SRCADR(&itf->sin) >> 24))
1464 				itf->flags &= ~INT_LOOPBACK;
1465 		} else {
1466 			if (memcmp(v6loop, NSRCADR6(&itf->sin),
1467 				   sizeof(NSRCADR6(&itf->sin))))
1468 				itf->flags &= ~INT_LOOPBACK;
1469 		}
1470 	}
1471 }
1472 
1473 
1474 /*
1475  * refresh_interface
1476  *
1477  * some OSes have been observed to keep
1478  * cached routes even when more specific routes
1479  * become available.
1480  * this can be mitigated by re-binding
1481  * the socket.
1482  */
1483 static int
1484 refresh_interface(
1485 	struct interface * interface
1486 	)
1487 {
1488 #ifdef  OS_MISSES_SPECIFIC_ROUTE_UPDATES
1489 	if (interface->fd != INVALID_SOCKET) {
1490 		int bcast = (interface->flags & INT_BCASTXMIT) != 0;
1491 		/* as we forcibly close() the socket remove the
1492 		   broadcast permission indication */
1493 		if (bcast)
1494 			socket_broadcast_disable(interface, &interface->sin);
1495 
1496 		close_and_delete_fd_from_list(interface->fd);
1497 
1498 		/* create new socket picking up a new first hop binding
1499 		   at connect() time */
1500 		interface->fd = open_socket(&interface->sin,
1501 					    bcast, 0, interface);
1502 		 /*
1503 		  * reset TTL indication so TTL is is set again
1504 		  * next time around
1505 		  */
1506 		interface->last_ttl = 0;
1507 		return (interface->fd != INVALID_SOCKET);
1508 	} else
1509 		return 0;	/* invalid sockets are not refreshable */
1510 #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1511 	return (interface->fd != INVALID_SOCKET);
1512 #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1513 }
1514 
1515 /*
1516  * interface_update - externally callable update function
1517  */
1518 void
1519 interface_update(
1520 	interface_receiver_t	receiver,
1521 	void *			data)
1522 {
1523 	int new_interface_found;
1524 
1525 	if (disable_dynamic_updates)
1526 		return;
1527 
1528 	BLOCKIO();
1529 	new_interface_found = update_interfaces(NTP_PORT, receiver, data);
1530 	UNBLOCKIO();
1531 
1532 	if (!new_interface_found)
1533 		return;
1534 
1535 #ifdef DEBUG
1536 	msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
1537 #endif
1538 	interrupt_worker_sleep();
1539 }
1540 
1541 
1542 /*
1543  * sau_from_netaddr() - convert network address on-wire formats.
1544  * Convert from libisc's isc_netaddr_t to NTP's sockaddr_u
1545  */
1546 void
1547 sau_from_netaddr(
1548 	sockaddr_u *psau,
1549 	const isc_netaddr_t *pna
1550 	)
1551 {
1552 	ZERO_SOCK(psau);
1553 	AF(psau) = (u_short)pna->family;
1554 	switch (pna->family) {
1555 
1556 	case AF_INET:
1557 		memcpy(&psau->sa4.sin_addr, &pna->type.in,
1558 		       sizeof(psau->sa4.sin_addr));
1559 		break;
1560 
1561 	case AF_INET6:
1562 		memcpy(&psau->sa6.sin6_addr, &pna->type.in6,
1563 		       sizeof(psau->sa6.sin6_addr));
1564 		break;
1565 	}
1566 }
1567 
1568 
1569 static int
1570 is_wildcard_addr(
1571 	const sockaddr_u *psau
1572 	)
1573 {
1574 	if (IS_IPV4(psau) && !NSRCADR(psau))
1575 		return 1;
1576 
1577 #ifdef INCLUDE_IPV6_SUPPORT
1578 	if (IS_IPV6(psau) && S_ADDR6_EQ(psau, &in6addr_any))
1579 		return 1;
1580 #endif
1581 
1582 	return 0;
1583 }
1584 
1585 
1586 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
1587 /*
1588  * enable/disable re-use of wildcard address socket
1589  */
1590 static void
1591 set_wildcard_reuse(
1592 	u_short	family,
1593 	int	on
1594 	)
1595 {
1596 	struct interface *any;
1597 	SOCKET fd = INVALID_SOCKET;
1598 
1599 	any = ANY_INTERFACE_BYFAM(family);
1600 	if (any != NULL)
1601 		fd = any->fd;
1602 
1603 	if (fd != INVALID_SOCKET) {
1604 		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1605 			       (void *)&on, sizeof(on)))
1606 			msyslog(LOG_ERR,
1607 				"set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m",
1608 				on ? "on" : "off");
1609 
1610 		DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n",
1611 			    on ? "on" : "off",
1612 			    stoa(&any->sin)));
1613 	}
1614 }
1615 #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
1616 
1617 static isc_boolean_t
1618 check_flags(
1619 	sockaddr_u *psau,
1620 	const char *name,
1621 	u_int32 flags
1622 	)
1623 {
1624 #if defined(SIOCGIFAFLAG_IN)
1625 	struct ifreq ifr;
1626 	int fd;
1627 
1628 	if (psau->sa.sa_family != AF_INET)
1629 		return ISC_FALSE;
1630 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1631 		return ISC_FALSE;
1632 	ZERO(ifr);
1633 	memcpy(&ifr.ifr_addr, &psau->sa, sizeof(ifr.ifr_addr));
1634 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1635 	if (ioctl(fd, SIOCGIFAFLAG_IN, &ifr) < 0) {
1636 		close(fd);
1637 		return ISC_FALSE;
1638 	}
1639 	close(fd);
1640 	if ((ifr.ifr_addrflags & flags) != 0)
1641 		return ISC_TRUE;
1642 #endif	/* SIOCGIFAFLAG_IN */
1643 	return ISC_FALSE;
1644 }
1645 
1646 static isc_boolean_t
1647 check_flags6(
1648 	sockaddr_u *psau,
1649 	const char *name,
1650 	u_int32 flags6
1651 	)
1652 {
1653 #if defined(INCLUDE_IPV6_SUPPORT) && defined(SIOCGIFAFLAG_IN6)
1654 	struct in6_ifreq ifr6;
1655 	int fd;
1656 
1657 	if (psau->sa.sa_family != AF_INET6)
1658 		return ISC_FALSE;
1659 	if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1660 		return ISC_FALSE;
1661 	ZERO(ifr6);
1662 	memcpy(&ifr6.ifr_addr, &psau->sa6, sizeof(ifr6.ifr_addr));
1663 	strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1664 	if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1665 		close(fd);
1666 		return ISC_FALSE;
1667 	}
1668 	close(fd);
1669 	if ((ifr6.ifr_ifru.ifru_flags6 & flags6) != 0)
1670 		return ISC_TRUE;
1671 #endif	/* INCLUDE_IPV6_SUPPORT && SIOCGIFAFLAG_IN6 */
1672 	return ISC_FALSE;
1673 }
1674 
1675 static isc_boolean_t
1676 is_anycast(
1677 	sockaddr_u *psau,
1678 	const char *name
1679 	)
1680 {
1681 #ifdef IN6_IFF_ANYCAST
1682 	return check_flags6(psau, name, IN6_IFF_ANYCAST);
1683 #else
1684 	return ISC_FALSE;
1685 #endif
1686 }
1687 
1688 static isc_boolean_t
1689 is_valid(
1690 	sockaddr_u *psau,
1691 	const char *name
1692 	)
1693 {
1694 	u_int32 flags;
1695 
1696 	flags = 0;
1697 	switch (psau->sa.sa_family) {
1698 	case AF_INET:
1699 #ifdef IN_IFF_DETACHED
1700 		flags |= IN_IFF_DETACHED;
1701 #endif
1702 #ifdef IN_IFF_TENTATIVE
1703 		flags |= IN_IFF_TENTATIVE;
1704 #endif
1705 		return check_flags(psau, name, flags) ? ISC_FALSE : ISC_TRUE;
1706 	case AF_INET6:
1707 #ifdef IN6_IFF_DEPARTED
1708 		flags |= IN6_IFF_DEPARTED;
1709 #endif
1710 #ifdef IN6_IFF_DETACHED
1711 		flags |= IN6_IFF_DETACHED;
1712 #endif
1713 #ifdef IN6_IFF_TENTATIVE
1714 		flags |= IN6_IFF_TENTATIVE;
1715 #endif
1716 		return check_flags6(psau, name, flags) ? ISC_FALSE : ISC_TRUE;
1717 	default:
1718 		return ISC_FALSE;
1719 	}
1720 }
1721 
1722 /*
1723  * update_interface strategy
1724  *
1725  * toggle configuration phase
1726  *
1727  * Phase 1:
1728  * forall currently existing interfaces
1729  *   if address is known:
1730  *	drop socket - rebind again
1731  *
1732  *   if address is NOT known:
1733  *	attempt to create a new interface entry
1734  *
1735  * Phase 2:
1736  * forall currently known non MCAST and WILDCARD interfaces
1737  *   if interface does not match configuration phase (not seen in phase 1):
1738  *	remove interface from known interface list
1739  *	forall peers associated with this interface
1740  *         disconnect peer from this interface
1741  *
1742  * Phase 3:
1743  *   attempt to re-assign interfaces to peers
1744  *
1745  */
1746 
1747 static int
1748 update_interfaces(
1749 	u_short			port,
1750 	interface_receiver_t	receiver,
1751 	void *			data
1752 	)
1753 {
1754 	isc_mem_t *		mctx = (void *)-1;
1755 	interface_info_t	ifi;
1756 	isc_interfaceiter_t *	iter;
1757 	isc_result_t		result;
1758 	isc_interface_t		isc_if;
1759 	int			new_interface_found;
1760 	unsigned int		family;
1761 	endpt			enumep;
1762 	endpt *			ep;
1763 	endpt *			next_ep;
1764 
1765 	DPRINTF(3, ("update_interfaces(%d)\n", port));
1766 
1767 	/*
1768 	 * phase one - scan interfaces
1769 	 * - create those that are not found
1770 	 * - update those that are found
1771 	 */
1772 
1773 	new_interface_found = FALSE;
1774 	iter = NULL;
1775 	result = isc_interfaceiter_create(mctx, &iter);
1776 
1777 	if (result != ISC_R_SUCCESS)
1778 		return 0;
1779 
1780 	/*
1781 	 * Toggle system interface scan phase to find untouched
1782 	 * interfaces to be deleted.
1783 	 */
1784 	sys_interphase ^= 0x1;
1785 
1786 	for (result = isc_interfaceiter_first(iter);
1787 	     ISC_R_SUCCESS == result;
1788 	     result = isc_interfaceiter_next(iter)) {
1789 
1790 		result = isc_interfaceiter_current(iter, &isc_if);
1791 
1792 		if (result != ISC_R_SUCCESS)
1793 			break;
1794 
1795 		/* See if we have a valid family to use */
1796 		family = isc_if.address.family;
1797 		if (AF_INET != family && AF_INET6 != family)
1798 			continue;
1799 		if (AF_INET == family && !ipv4_works)
1800 			continue;
1801 		if (AF_INET6 == family && !ipv6_works)
1802 			continue;
1803 
1804 		/* create prototype */
1805 		init_interface(&enumep);
1806 
1807 		convert_isc_if(&isc_if, &enumep, port);
1808 
1809 		DPRINT_INTERFACE(4, (&enumep, "examining ", "\n"));
1810 
1811 		/*
1812 		 * Check if and how we are going to use the interface.
1813 		 */
1814 		switch (interface_action(enumep.name, &enumep.sin,
1815 					 enumep.flags)) {
1816 
1817 		case ACTION_IGNORE:
1818 			DPRINTF(4, ("ignoring interface %s (%s) - by nic rules\n",
1819 				    enumep.name, stoa(&enumep.sin)));
1820 			continue;
1821 
1822 		case ACTION_LISTEN:
1823 			DPRINTF(4, ("listen interface %s (%s) - by nic rules\n",
1824 				    enumep.name, stoa(&enumep.sin)));
1825 			enumep.ignore_packets = ISC_FALSE;
1826 			break;
1827 
1828 		case ACTION_DROP:
1829 			DPRINTF(4, ("drop on interface %s (%s) - by nic rules\n",
1830 				    enumep.name, stoa(&enumep.sin)));
1831 			enumep.ignore_packets = ISC_TRUE;
1832 			break;
1833 		}
1834 
1835 		 /* interfaces must be UP to be usable */
1836 		if (!(enumep.flags & INT_UP)) {
1837 			DPRINTF(4, ("skipping interface %s (%s) - DOWN\n",
1838 				    enumep.name, stoa(&enumep.sin)));
1839 			continue;
1840 		}
1841 
1842 		/*
1843 		 * skip any interfaces UP and bound to a wildcard
1844 		 * address - some dhcp clients produce that in the
1845 		 * wild
1846 		 */
1847 		if (is_wildcard_addr(&enumep.sin))
1848 			continue;
1849 
1850 		if (is_anycast(&enumep.sin, isc_if.name))
1851 			continue;
1852 
1853 		/*
1854 		 * skip any address that is an invalid state to be used
1855 		 */
1856 		if (!is_valid(&enumep.sin, isc_if.name))
1857 			continue;
1858 
1859 		/*
1860 		 * map to local *address* in order to map all duplicate
1861 		 * interfaces to an endpt structure with the appropriate
1862 		 * socket.  Our name space is (ip-address), NOT
1863 		 * (interface name, ip-address).
1864 		 */
1865 		ep = getinterface(&enumep.sin, INT_WILDCARD);
1866 
1867 		if (ep != NULL && refresh_interface(ep)) {
1868 			/*
1869 			 * found existing and up to date interface -
1870 			 * mark present.
1871 			 */
1872 			if (ep->phase != sys_interphase) {
1873 				/*
1874 				 * On a new round we reset the name so
1875 				 * the interface name shows up again if
1876 				 * this address is no longer shared.
1877 				 * We reset ignore_packets from the
1878 				 * new prototype to respect any runtime
1879 				 * changes to the nic rules.
1880 				 */
1881 				strlcpy(ep->name, enumep.name,
1882 					sizeof(ep->name));
1883 				ep->ignore_packets =
1884 					    enumep.ignore_packets;
1885 			} else {
1886 				/* name collision - rename interface */
1887 				strlcpy(ep->name, "*multiple*",
1888 					sizeof(ep->name));
1889 			}
1890 
1891 			DPRINT_INTERFACE(4, (ep, "updating ",
1892 					     " present\n"));
1893 
1894 			if (ep->ignore_packets !=
1895 			    enumep.ignore_packets) {
1896 				/*
1897 				 * We have conflicting configurations
1898 				 * for the interface address. This is
1899 				 * caused by using -I <interfacename>
1900 				 * for an interface that shares its
1901 				 * address with other interfaces. We
1902 				 * can not disambiguate incoming
1903 				 * packets delivered to this socket
1904 				 * without extra syscalls/features.
1905 				 * These are not (commonly) available.
1906 				 * Note this is a more unusual
1907 				 * configuration where several
1908 				 * interfaces share an address but
1909 				 * filtering via interface name is
1910 				 * attempted.  We resolve the
1911 				 * configuration conflict by disabling
1912 				 * the processing of received packets.
1913 				 * This leads to no service on the
1914 				 * interface address where the conflict
1915 				 * occurs.
1916 				 */
1917 				msyslog(LOG_ERR,
1918 					"WARNING: conflicting enable configuration for interfaces %s and %s for address %s - unsupported configuration - address DISABLED",
1919 					enumep.name, ep->name,
1920 					stoa(&enumep.sin));
1921 
1922 				ep->ignore_packets = ISC_TRUE;
1923 			}
1924 
1925 			ep->phase = sys_interphase;
1926 
1927 			ifi.action = IFS_EXISTS;
1928 			ifi.ep = ep;
1929 			if (receiver != NULL)
1930 				(*receiver)(data, &ifi);
1931 		} else {
1932 			/*
1933 			 * This is new or refreshing failed - add to
1934 			 * our interface list.  If refreshing failed we
1935 			 * will delete the interface structure in phase
1936 			 * 2 as the interface was not marked current.
1937 			 * We can bind to the address as the refresh
1938 			 * code already closed the offending socket
1939 			 */
1940 			ep = create_interface(port, &enumep);
1941 
1942 			if (ep != NULL) {
1943 				ifi.action = IFS_CREATED;
1944 				ifi.ep = ep;
1945 				if (receiver != NULL)
1946 					(*receiver)(data, &ifi);
1947 
1948 				new_interface_found = TRUE;
1949 				DPRINT_INTERFACE(3,
1950 					(ep, "updating ",
1951 					 " new - created\n"));
1952 			} else {
1953 				DPRINT_INTERFACE(3,
1954 					(&enumep, "updating ",
1955 					 " new - creation FAILED"));
1956 
1957 				msyslog(LOG_INFO,
1958 					"failed to init interface for address %s",
1959 					stoa(&enumep.sin));
1960 				continue;
1961 			}
1962 		}
1963 	}
1964 
1965 	isc_interfaceiter_destroy(&iter);
1966 
1967 	/*
1968 	 * phase 2 - delete gone interfaces - reassigning peers to
1969 	 * other interfaces
1970 	 */
1971 	for (ep = ep_list; ep != NULL; ep = next_ep) {
1972 		next_ep = ep->elink;
1973 
1974 		/*
1975 		 * if phase does not match sys_phase this interface was
1976 		 * not enumerated during the last interface scan - so it
1977 		 * is gone and will be deleted here unless it did not
1978 		 * originate from interface enumeration (INT_WILDCARD,
1979 		 * INT_MCASTIF).
1980 		 */
1981 		if (((INT_WILDCARD | INT_MCASTIF) & ep->flags) ||
1982 		    ep->phase == sys_interphase)
1983 			continue;
1984 
1985 		DPRINT_INTERFACE(3, (ep, "updating ",
1986 				     "GONE - deleting\n"));
1987 		remove_interface(ep);
1988 
1989 		ifi.action = IFS_DELETED;
1990 		ifi.ep = ep;
1991 		if (receiver != NULL)
1992 			(*receiver)(data, &ifi);
1993 
1994 		/* disconnect peers from deleted endpt. */
1995 		while (ep->peers != NULL)
1996 			set_peerdstadr(ep->peers, NULL);
1997 
1998 		/*
1999 		 * update globals in case we lose
2000 		 * a loopback interface
2001 		 */
2002 		if (ep == loopback_interface)
2003 			loopback_interface = NULL;
2004 
2005 		delete_interface(ep);
2006 	}
2007 
2008 	/*
2009 	 * phase 3 - re-configure as the world has possibly changed
2010 	 *
2011 	 * never ever make this conditional again - it is needed to track
2012 	 * routing updates. see bug #2506
2013 	 */
2014 	refresh_all_peerinterfaces();
2015 
2016 	if (broadcast_client_enabled)
2017 		io_setbclient();
2018 
2019 	if (sys_bclient)
2020 		io_setbclient();
2021 
2022 #ifdef MCAST
2023 	/*
2024 	 * Check multicast interfaces and try to join multicast groups if
2025          * not joined yet.
2026          */
2027 	for (ep = ep_list; ep != NULL; ep = ep->elink) {
2028 		remaddr_t *entry;
2029 
2030 		if (!(INT_MCASTIF & ep->flags) || (INT_MCASTOPEN & ep->flags))
2031 			continue;
2032 
2033 		/* Find remote address that was linked to this interface */
2034 		for (entry = remoteaddr_list;
2035 		     entry != NULL;
2036 		     entry = entry->link) {
2037 			if (entry->ep == ep) {
2038 				if (socket_multicast_enable(ep, &entry->addr)) {
2039 					msyslog(LOG_INFO,
2040 						"Joined %s socket to multicast group %s",
2041 						stoa(&ep->sin),
2042 						stoa(&entry->addr));
2043 				}
2044 				break;
2045 			}
2046 		}
2047 	}
2048 #endif /* MCAST */
2049 
2050 	return new_interface_found;
2051 }
2052 
2053 
2054 /*
2055  * create_sockets - create a socket for each interface plus a default
2056  *			socket for when we don't know where to send
2057  */
2058 static int
2059 create_sockets(
2060 	u_short port
2061 	)
2062 {
2063 #ifndef HAVE_IO_COMPLETION_PORT
2064 	/*
2065 	 * I/O Completion Ports don't care about the select and FD_SET
2066 	 */
2067 	maxactivefd = 0;
2068 	FD_ZERO(&activefds);
2069 #endif
2070 
2071 	DPRINTF(2, ("create_sockets(%d)\n", port));
2072 
2073 	create_wildcards(port);
2074 
2075 	update_interfaces(port, NULL, NULL);
2076 
2077 	/*
2078 	 * Now that we have opened all the sockets, turn off the reuse
2079 	 * flag for security.
2080 	 */
2081 	set_reuseaddr(0);
2082 
2083 	DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
2084 
2085 	return ninterfaces;
2086 }
2087 
2088 /*
2089  * create_interface - create a new interface for a given prototype
2090  *		      binding the socket.
2091  */
2092 static struct interface *
2093 create_interface(
2094 	u_short			port,
2095 	struct interface *	protot
2096 	)
2097 {
2098 	sockaddr_u	resmask;
2099 	endpt *		iface;
2100 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2101 	remaddr_t *	entry;
2102 	remaddr_t *	next_entry;
2103 #endif
2104 	DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&protot->sin),
2105 		    port));
2106 
2107 	/* build an interface */
2108 	iface = new_interface(protot);
2109 
2110 	/*
2111 	 * create socket
2112 	 */
2113 	iface->fd = open_socket(&iface->sin, 0, 0, iface);
2114 
2115 	if (iface->fd != INVALID_SOCKET)
2116 		log_listen_address(iface);
2117 
2118 	if ((INT_BROADCAST & iface->flags)
2119 	    && iface->bfd != INVALID_SOCKET)
2120 		msyslog(LOG_INFO, "Listening on broadcast address %s#%d",
2121 			stoa((&iface->bcast)), port);
2122 
2123 	if (INVALID_SOCKET == iface->fd
2124 	    && INVALID_SOCKET == iface->bfd) {
2125 		msyslog(LOG_ERR, "unable to create socket on %s (%d) for %s#%d",
2126 			iface->name,
2127 			iface->ifnum,
2128 			stoa((&iface->sin)),
2129 			port);
2130 		delete_interface(iface);
2131 		return NULL;
2132 	}
2133 
2134 	/*
2135 	 * Blacklist our own addresses, no use talking to ourself
2136 	 */
2137 	SET_HOSTMASK(&resmask, AF(&iface->sin));
2138 	hack_restrict(RESTRICT_FLAGS, &iface->sin, &resmask,
2139 		      -4, RESM_NTPONLY | RESM_INTERFACE, RES_IGNORE, 0);
2140 
2141 	/*
2142 	 * set globals with the first found
2143 	 * loopback interface of the appropriate class
2144 	 */
2145 	if (NULL == loopback_interface && AF_INET == iface->family
2146 	    && (INT_LOOPBACK & iface->flags))
2147 		loopback_interface = iface;
2148 
2149 	/*
2150 	 * put into our interface list
2151 	 */
2152 	add_addr_to_list(&iface->sin, iface);
2153 	add_interface(iface);
2154 
2155 #if defined(MCAST) && defined(MULTICAST_NONEWSOCKET)
2156 	/*
2157 	 * Join any previously-configured compatible multicast groups.
2158 	 */
2159 	if (INT_MULTICAST & iface->flags &&
2160 	    !((INT_LOOPBACK | INT_WILDCARD) & iface->flags) &&
2161 	    !iface->ignore_packets) {
2162 		for (entry = remoteaddr_list;
2163 		     entry != NULL;
2164 		     entry = next_entry) {
2165 			next_entry = entry->link;
2166 			if (AF(&iface->sin) != AF(&entry->addr) ||
2167 			    !IS_MCAST(&entry->addr))
2168 				continue;
2169 			if (socket_multicast_enable(iface,
2170 						    &entry->addr))
2171 				msyslog(LOG_INFO,
2172 					"Joined %s socket to multicast group %s",
2173 					stoa(&iface->sin),
2174 					stoa(&entry->addr));
2175 			else
2176 				msyslog(LOG_ERR,
2177 					"Failed to join %s socket to multicast group %s",
2178 					stoa(&iface->sin),
2179 					stoa(&entry->addr));
2180 		}
2181 	}
2182 #endif	/* MCAST && MCAST_NONEWSOCKET */
2183 
2184 	DPRINT_INTERFACE(2, (iface, "created ", "\n"));
2185 	return iface;
2186 }
2187 
2188 
2189 #ifdef SO_EXCLUSIVEADDRUSE
2190 static void
2191 set_excladdruse(
2192 	SOCKET fd
2193 	)
2194 {
2195 	int one = 1;
2196 	int failed;
2197 #ifdef SYS_WINNT
2198 	DWORD err;
2199 #endif
2200 
2201 	failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
2202 			    (void *)&one, sizeof(one));
2203 
2204 	if (!failed)
2205 		return;
2206 
2207 #ifdef SYS_WINNT
2208 	/*
2209 	 * Prior to Windows XP setting SO_EXCLUSIVEADDRUSE can fail with
2210 	 * error WSAINVAL depending on service pack level and whether
2211 	 * the user account is in the Administrators group.  Do not
2212 	 * complain if it fails that way on versions prior to XP (5.1).
2213 	 */
2214 	err = GetLastError();
2215 
2216 	if (isc_win32os_versioncheck(5, 1, 0, 0) < 0	/* < 5.1/XP */
2217 	    && WSAEINVAL == err)
2218 		return;
2219 
2220 	SetLastError(err);
2221 #endif
2222 	msyslog(LOG_ERR,
2223 		"setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m",
2224 		(int)fd);
2225 }
2226 #endif  /* SO_EXCLUSIVEADDRUSE */
2227 
2228 
2229 /*
2230  * set_reuseaddr() - set/clear REUSEADDR on all sockets
2231  *			NB possible hole - should we be doing this on broadcast
2232  *			fd's also?
2233  */
2234 static void
2235 set_reuseaddr(
2236 	int flag
2237 	)
2238 {
2239 #ifndef SO_EXCLUSIVEADDRUSE
2240 	endpt *ep;
2241 
2242 	for (ep = ep_list; ep != NULL; ep = ep->elink) {
2243 		if (ep->flags & INT_WILDCARD)
2244 			continue;
2245 
2246 		/*
2247 		 * if ep->fd  is INVALID_SOCKET, we might have a adapter
2248 		 * configured but not present
2249 		 */
2250 		DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n",
2251 			    ep->name, stoa(&ep->sin),
2252 			    flag ? "on" : "off"));
2253 
2254 		if (ep->fd != INVALID_SOCKET) {
2255 			if (setsockopt(ep->fd, SOL_SOCKET, SO_REUSEADDR,
2256 				       (void *)&flag, sizeof(flag))) {
2257 				msyslog(LOG_ERR, "set_reuseaddr: setsockopt(%s, SO_REUSEADDR, %s) failed: %m",
2258 					stoa(&ep->sin), flag ? "on" : "off");
2259 			}
2260 		}
2261 	}
2262 #endif /* ! SO_EXCLUSIVEADDRUSE */
2263 }
2264 
2265 /*
2266  * This is just a wrapper around an internal function so we can
2267  * make other changes as necessary later on
2268  */
2269 void
2270 enable_broadcast(
2271 	struct interface *	iface,
2272 	sockaddr_u *		baddr
2273 	)
2274 {
2275 #ifdef OPEN_BCAST_SOCKET
2276 	socket_broadcast_enable(iface, iface->fd, baddr);
2277 #endif
2278 }
2279 
2280 #ifdef OPEN_BCAST_SOCKET
2281 /*
2282  * Enable a broadcast address to a given socket
2283  * The socket is in the ep_list all we need to do is enable
2284  * broadcasting. It is not this function's job to select the socket
2285  */
2286 static isc_boolean_t
2287 socket_broadcast_enable(
2288 	struct interface *	iface,
2289 	SOCKET			fd,
2290 	sockaddr_u *		baddr
2291 	)
2292 {
2293 #ifdef SO_BROADCAST
2294 	int on = 1;
2295 
2296 	if (IS_IPV4(baddr)) {
2297 		/* if this interface can support broadcast, set SO_BROADCAST */
2298 		if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
2299 			       (void *)&on, sizeof(on)))
2300 			msyslog(LOG_ERR,
2301 				"setsockopt(SO_BROADCAST) enable failure on address %s: %m",
2302 				stoa(baddr));
2303 		else
2304 			DPRINTF(2, ("Broadcast enabled on socket %d for address %s\n",
2305 				    fd, stoa(baddr)));
2306 	}
2307 	iface->flags |= INT_BCASTXMIT;
2308 	return ISC_TRUE;
2309 #else
2310 	return ISC_FALSE;
2311 #endif /* SO_BROADCAST */
2312 }
2313 
2314 #ifdef  OS_MISSES_SPECIFIC_ROUTE_UPDATES
2315 /*
2316  * Remove a broadcast address from a given socket
2317  * The socket is in the ep_list all we need to do is disable
2318  * broadcasting. It is not this function's job to select the socket
2319  */
2320 static isc_boolean_t
2321 socket_broadcast_disable(
2322 	struct interface *	iface,
2323 	sockaddr_u *		baddr
2324 	)
2325 {
2326 #ifdef SO_BROADCAST
2327 	int off = 0;	/* This seems to be OK as an int */
2328 
2329 	if (IS_IPV4(baddr) && setsockopt(iface->fd, SOL_SOCKET,
2330 	    SO_BROADCAST, (void *)&off, sizeof(off)))
2331 		msyslog(LOG_ERR,
2332 			"setsockopt(SO_BROADCAST) disable failure on address %s: %m",
2333 			stoa(baddr));
2334 
2335 	iface->flags &= ~INT_BCASTXMIT;
2336 	return ISC_TRUE;
2337 #else
2338 	return ISC_FALSE;
2339 #endif /* SO_BROADCAST */
2340 }
2341 #endif /* OS_MISSES_SPECIFIC_ROUTE_UPDATES */
2342 
2343 #endif /* OPEN_BCAST_SOCKET */
2344 
2345 /*
2346  * return the broadcast client flag value
2347  */
2348 isc_boolean_t
2349 get_broadcastclient_flag(void)
2350 {
2351 	return (broadcast_client_enabled);
2352 }
2353 
2354 /*
2355  * Check to see if the address is a multicast address
2356  */
2357 static isc_boolean_t
2358 addr_ismulticast(
2359 	sockaddr_u *maddr
2360 	)
2361 {
2362 	isc_boolean_t result;
2363 
2364 #ifndef INCLUDE_IPV6_MULTICAST_SUPPORT
2365 	/*
2366 	 * If we don't have IPV6 support any IPV6 addr is not multicast
2367 	 */
2368 	if (IS_IPV6(maddr))
2369 		result = ISC_FALSE;
2370 	else
2371 #endif
2372 		result = IS_MCAST(maddr);
2373 
2374 	if (!result)
2375 		DPRINTF(4, ("address %s is not multicast\n",
2376 			    stoa(maddr)));
2377 
2378 	return result;
2379 }
2380 
2381 /*
2382  * Multicast servers need to set the appropriate Multicast interface
2383  * socket option in order for it to know which interface to use for
2384  * send the multicast packet.
2385  */
2386 void
2387 enable_multicast_if(
2388 	struct interface *	iface,
2389 	sockaddr_u *		maddr
2390 	)
2391 {
2392 #ifdef MCAST
2393 #ifdef IP_MULTICAST_LOOP
2394 	TYPEOF_IP_MULTICAST_LOOP off = 0;
2395 #endif
2396 #if defined(INCLUDE_IPV6_MULTICAST_SUPPORT) && defined(IPV6_MULTICAST_LOOP)
2397 	u_int off6 = 0;
2398 #endif
2399 
2400 	REQUIRE(AF(maddr) == AF(&iface->sin));
2401 
2402 	switch (AF(&iface->sin)) {
2403 
2404 	case AF_INET:
2405 #ifdef IP_MULTICAST_LOOP
2406 		/*
2407 		 * Don't send back to itself, but allow failure to set
2408 		 */
2409 		if (setsockopt(iface->fd, IPPROTO_IP,
2410 			       IP_MULTICAST_LOOP,
2411 			       (void *)&off,
2412 			       sizeof(off))) {
2413 
2414 			msyslog(LOG_ERR,
2415 				"setsockopt IP_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2416 				iface->fd, stoa(&iface->sin),
2417 				stoa(maddr));
2418 		}
2419 #endif
2420 		break;
2421 
2422 	case AF_INET6:
2423 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2424 #ifdef IPV6_MULTICAST_LOOP
2425 		/*
2426 		 * Don't send back to itself, but allow failure to set
2427 		 */
2428 		if (setsockopt(iface->fd, IPPROTO_IPV6,
2429 			       IPV6_MULTICAST_LOOP,
2430 			       (void *) &off6, sizeof(off6))) {
2431 
2432 			msyslog(LOG_ERR,
2433 				"setsockopt IPV6_MULTICAST_LOOP failed: %m on socket %d, addr %s for multicast address %s",
2434 				iface->fd, stoa(&iface->sin),
2435 				stoa(maddr));
2436 		}
2437 #endif
2438 		break;
2439 #else
2440 		return;
2441 #endif	/* INCLUDE_IPV6_MULTICAST_SUPPORT */
2442 	}
2443 	return;
2444 #endif
2445 }
2446 
2447 /*
2448  * Add a multicast address to a given socket
2449  * The socket is in the ep_list all we need to do is enable
2450  * multicasting. It is not this function's job to select the socket
2451  */
2452 #if defined(MCAST)
2453 static isc_boolean_t
2454 socket_multicast_enable(
2455 	endpt *		iface,
2456 	sockaddr_u *	maddr
2457 	)
2458 {
2459 	struct ip_mreq		mreq;
2460 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2461 	struct ipv6_mreq	mreq6;
2462 # endif
2463 	switch (AF(maddr)) {
2464 
2465 	case AF_INET:
2466 		ZERO(mreq);
2467 		mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2468 		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
2469 		if (setsockopt(iface->fd,
2470 			       IPPROTO_IP,
2471 			       IP_ADD_MEMBERSHIP,
2472 			       (void *)&mreq,
2473 			       sizeof(mreq))) {
2474 			DPRINTF(2, (
2475 				"setsockopt IP_ADD_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2476 				iface->fd, stoa(&iface->sin),
2477 				mreq.imr_multiaddr.s_addr,
2478 				mreq.imr_interface.s_addr,
2479 				stoa(maddr)));
2480 			return ISC_FALSE;
2481 		}
2482 		DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n",
2483 			    iface->fd, stoa(&iface->sin),
2484 			    mreq.imr_multiaddr.s_addr,
2485 			    mreq.imr_interface.s_addr, stoa(maddr)));
2486 		break;
2487 
2488 	case AF_INET6:
2489 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2490 		/*
2491 		 * Enable reception of multicast packets.
2492 		 * If the address is link-local we can get the
2493 		 * interface index from the scope id. Don't do this
2494 		 * for other types of multicast addresses. For now let
2495 		 * the kernel figure it out.
2496 		 */
2497 		ZERO(mreq6);
2498 		mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2499 		mreq6.ipv6mr_interface = iface->ifindex;
2500 
2501 		if (setsockopt(iface->fd, IPPROTO_IPV6,
2502 			       IPV6_JOIN_GROUP, (void *)&mreq6,
2503 			       sizeof(mreq6))) {
2504 			DPRINTF(2, (
2505 				"setsockopt IPV6_JOIN_GROUP failed: %m on socket %d, addr %s for interface %u (%s)",
2506 				iface->fd, stoa(&iface->sin),
2507 				mreq6.ipv6mr_interface, stoa(maddr)));
2508 			return ISC_FALSE;
2509 		}
2510 		DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %u (%s)\n",
2511 			    iface->fd, stoa(&iface->sin),
2512 			    mreq6.ipv6mr_interface, stoa(maddr)));
2513 # else
2514 		return ISC_FALSE;
2515 # endif	/* INCLUDE_IPV6_MULTICAST_SUPPORT */
2516 	}
2517 	iface->flags |= INT_MCASTOPEN;
2518 	iface->num_mcast++;
2519 
2520 	return ISC_TRUE;
2521 }
2522 #endif	/* MCAST */
2523 
2524 
2525 /*
2526  * Remove a multicast address from a given socket
2527  * The socket is in the ep_list all we need to do is disable
2528  * multicasting. It is not this function's job to select the socket
2529  */
2530 #ifdef MCAST
2531 static isc_boolean_t
2532 socket_multicast_disable(
2533 	struct interface *	iface,
2534 	sockaddr_u *		maddr
2535 	)
2536 {
2537 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2538 	struct ipv6_mreq mreq6;
2539 # endif
2540 	struct ip_mreq mreq;
2541 
2542 	ZERO(mreq);
2543 
2544 	if (find_addr_in_list(maddr) == NULL) {
2545 		DPRINTF(4, ("socket_multicast_disable(%s): not found\n",
2546 			    stoa(maddr)));
2547 		return ISC_TRUE;
2548 	}
2549 
2550 	switch (AF(maddr)) {
2551 
2552 	case AF_INET:
2553 		mreq.imr_multiaddr = SOCK_ADDR4(maddr);
2554 		mreq.imr_interface = SOCK_ADDR4(&iface->sin);
2555 		if (setsockopt(iface->fd, IPPROTO_IP,
2556 			       IP_DROP_MEMBERSHIP, (void *)&mreq,
2557 			       sizeof(mreq))) {
2558 
2559 			msyslog(LOG_ERR,
2560 				"setsockopt IP_DROP_MEMBERSHIP failed: %m on socket %d, addr %s for %x / %x (%s)",
2561 				iface->fd, stoa(&iface->sin),
2562 				SRCADR(maddr), SRCADR(&iface->sin),
2563 				stoa(maddr));
2564 			return ISC_FALSE;
2565 		}
2566 		break;
2567 	case AF_INET6:
2568 # ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2569 		/*
2570 		 * Disable reception of multicast packets
2571 		 * If the address is link-local we can get the
2572 		 * interface index from the scope id.  Don't do this
2573 		 * for other types of multicast addresses. For now let
2574 		 * the kernel figure it out.
2575 		 */
2576 		mreq6.ipv6mr_multiaddr = SOCK_ADDR6(maddr);
2577 		mreq6.ipv6mr_interface = iface->ifindex;
2578 
2579 		if (setsockopt(iface->fd, IPPROTO_IPV6,
2580 			       IPV6_LEAVE_GROUP, (void *)&mreq6,
2581 			       sizeof(mreq6))) {
2582 
2583 			msyslog(LOG_ERR,
2584 				"setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d (%s)",
2585 				iface->fd, stoa(&iface->sin),
2586 				iface->ifindex, stoa(maddr));
2587 			return ISC_FALSE;
2588 		}
2589 		break;
2590 # else
2591 		return ISC_FALSE;
2592 # endif	/* INCLUDE_IPV6_MULTICAST_SUPPORT */
2593 	}
2594 
2595 	iface->num_mcast--;
2596 	if (!iface->num_mcast)
2597 		iface->flags &= ~INT_MCASTOPEN;
2598 
2599 	return ISC_TRUE;
2600 }
2601 #endif	/* MCAST */
2602 
2603 /*
2604  * io_setbclient - open the broadcast client sockets
2605  */
2606 void
2607 io_setbclient(void)
2608 {
2609 #ifdef OPEN_BCAST_SOCKET
2610 	struct interface *	interf;
2611 	unsigned int		nif;
2612 
2613 	nif = 0;
2614 	set_reuseaddr(1);
2615 
2616 	for (interf = ep_list;
2617 	     interf != NULL;
2618 	     interf = interf->elink) {
2619 
2620 		if (interf->flags & (INT_WILDCARD | INT_LOOPBACK))
2621 			continue;
2622 
2623 		/* use only allowed addresses */
2624 		if (interf->ignore_packets)
2625 			continue;
2626 
2627 		/* Need a broadcast-capable interface */
2628 		if (!(interf->flags & INT_BROADCAST))
2629 			continue;
2630 
2631 		/* Only IPv4 addresses are valid for broadcast */
2632 		REQUIRE(IS_IPV4(&interf->bcast));
2633 
2634 		/* Do we already have the broadcast address open? */
2635 		if (interf->flags & INT_BCASTOPEN) {
2636 			/*
2637 			 * account for already open interfaces to avoid
2638 			 * misleading warning below
2639 			 */
2640 			nif++;
2641 			continue;
2642 		}
2643 
2644 		/*
2645 		 * Try to open the broadcast address
2646 		 */
2647 		interf->family = AF_INET;
2648 		interf->bfd = open_socket(&interf->bcast, 1, 0, interf);
2649 
2650 		/*
2651 		 * If we succeeded then we use it otherwise enable
2652 		 * broadcast on the interface address
2653 		 */
2654 		if (interf->bfd != INVALID_SOCKET) {
2655 			nif++;
2656 			interf->flags |= INT_BCASTOPEN;
2657 			msyslog(LOG_INFO,
2658 				"Listen for broadcasts to %s on interface #%d %s",
2659 				stoa(&interf->bcast), interf->ifnum, interf->name);
2660 		} else switch (errno) {
2661 			/* Silently ignore EADDRINUSE as we probably
2662 			 * opened the socket already for an address in
2663 			 * the same network */
2664 		case EADDRINUSE:
2665 			/* Some systems cannot bind a socket to a broadcast
2666 			 * address, as that is not a valid host address. */
2667 		case EADDRNOTAVAIL:
2668 #		    ifdef SYS_WINNT	/*TODO: use for other systems, too? */
2669 			/* avoid recurrence here -- if we already have a
2670 			 * regular socket, it's quite useless to try this
2671 			 * again.
2672 			 */
2673 			if (interf->fd != INVALID_SOCKET) {
2674 				interf->flags |= INT_BCASTOPEN;
2675 				nif++;
2676 			}
2677 #		    endif
2678 			break;
2679 
2680 		default:
2681 			msyslog(LOG_INFO,
2682 				"failed to listen for broadcasts to %s on interface #%d %s",
2683 				stoa(&interf->bcast), interf->ifnum, interf->name);
2684 			break;
2685 		}
2686 	}
2687 	set_reuseaddr(0);
2688 	if (nif != 0) {
2689 		broadcast_client_enabled = ISC_TRUE;
2690 		DPRINTF(1, ("io_setbclient: listening to %d broadcast addresses\n", nif));
2691 	} else {
2692 		broadcast_client_enabled = ISC_FALSE;
2693 		msyslog(LOG_ERR,
2694 			"Unable to listen for broadcasts, no broadcast interfaces available");
2695 	}
2696 #else
2697 	msyslog(LOG_ERR,
2698 		"io_setbclient: Broadcast Client disabled by build");
2699 #endif	/* OPEN_BCAST_SOCKET */
2700 }
2701 
2702 /*
2703  * io_unsetbclient - close the broadcast client sockets
2704  */
2705 void
2706 io_unsetbclient(void)
2707 {
2708 	endpt *ep;
2709 
2710 	for (ep = ep_list; ep != NULL; ep = ep->elink) {
2711 		if (INT_WILDCARD & ep->flags)
2712 			continue;
2713 		if (!(INT_BCASTOPEN & ep->flags))
2714 			continue;
2715 
2716 		if (ep->bfd != INVALID_SOCKET) {
2717 			/* destroy broadcast listening socket */
2718 			msyslog(LOG_INFO,
2719 				"stop listening for broadcasts to %s on interface #%d %s",
2720 				stoa(&ep->bcast), ep->ifnum, ep->name);
2721 #		    ifdef HAVE_IO_COMPLETION_PORT
2722 			io_completion_port_remove_socket(ep->bfd, ep);
2723 #		    endif
2724 			close_and_delete_fd_from_list(ep->bfd);
2725 			ep->bfd = INVALID_SOCKET;
2726 		}
2727 		ep->flags &= ~INT_BCASTOPEN;
2728 	}
2729 	broadcast_client_enabled = ISC_FALSE;
2730 }
2731 
2732 /*
2733  * io_multicast_add() - add multicast group address
2734  */
2735 void
2736 io_multicast_add(
2737 	sockaddr_u *addr
2738 	)
2739 {
2740 #ifdef MCAST
2741 	endpt *	ep;
2742 	endpt *	one_ep;
2743 
2744 	/*
2745 	 * Check to see if this is a multicast address
2746 	 */
2747 	if (!addr_ismulticast(addr))
2748 		return;
2749 
2750 	/* If we already have it we can just return */
2751 	if (NULL != find_flagged_addr_in_list(addr, INT_MCASTOPEN)) {
2752 		msyslog(LOG_INFO,
2753 			"Duplicate request found for multicast address %s",
2754 			stoa(addr));
2755 		return;
2756 	}
2757 
2758 # ifndef MULTICAST_NONEWSOCKET
2759 	ep = new_interface(NULL);
2760 
2761 	/*
2762 	 * Open a new socket for the multicast address
2763 	 */
2764 	ep->sin = *addr;
2765 	SET_PORT(&ep->sin, NTP_PORT);
2766 	ep->family = AF(&ep->sin);
2767 	AF(&ep->mask) = ep->family;
2768 	SET_ONESMASK(&ep->mask);
2769 
2770 	set_reuseaddr(1);
2771 	ep->bfd = INVALID_SOCKET;
2772 	ep->fd = open_socket(&ep->sin, 0, 0, ep);
2773 	if (ep->fd != INVALID_SOCKET) {
2774 		ep->ignore_packets = ISC_FALSE;
2775 		ep->flags |= INT_MCASTIF;
2776 		ep->ifindex = SCOPE(addr);
2777 
2778 		strlcpy(ep->name, "multicast", sizeof(ep->name));
2779 		DPRINT_INTERFACE(2, (ep, "multicast add ", "\n"));
2780 		add_interface(ep);
2781 		log_listen_address(ep);
2782 	} else {
2783 		/* bind failed, re-use wildcard interface */
2784 		delete_interface(ep);
2785 
2786 		if (IS_IPV4(addr))
2787 			ep = wildipv4;
2788 		else if (IS_IPV6(addr))
2789 			ep = wildipv6;
2790 		else
2791 			ep = NULL;
2792 
2793 		if (ep != NULL) {
2794 			/* HACK ! -- stuff in an address */
2795 			/* because we don't bind addr? DH */
2796 			ep->bcast = *addr;
2797 			msyslog(LOG_ERR,
2798 				"multicast address %s using wildcard interface #%d %s",
2799 				stoa(addr), ep->ifnum, ep->name);
2800 		} else {
2801 			msyslog(LOG_ERR,
2802 				"No multicast socket available to use for address %s",
2803 				stoa(addr));
2804 			return;
2805 		}
2806 	}
2807 	{	/* in place of the { following for in #else clause */
2808 		one_ep = ep;
2809 # else	/* MULTICAST_NONEWSOCKET follows */
2810 	/*
2811 	 * For the case where we can't use a separate socket (Windows)
2812 	 * join each applicable endpoint socket to the group address.
2813 	 */
2814 	if (IS_IPV4(addr))
2815 		one_ep = wildipv4;
2816 	else
2817 		one_ep = wildipv6;
2818 	for (ep = ep_list; ep != NULL; ep = ep->elink) {
2819 		if (ep->ignore_packets || AF(&ep->sin) != AF(addr) ||
2820 		    !(INT_MULTICAST & ep->flags) ||
2821 		    (INT_LOOPBACK | INT_WILDCARD) & ep->flags)
2822 			continue;
2823 		one_ep = ep;
2824 # endif	/* MULTICAST_NONEWSOCKET */
2825 		if (socket_multicast_enable(ep, addr))
2826 			msyslog(LOG_INFO,
2827 				"Joined %s socket to multicast group %s",
2828 				stoa(&ep->sin),
2829 				stoa(addr));
2830 	}
2831 
2832 	add_addr_to_list(addr, one_ep);
2833 #else	/* !MCAST  follows*/
2834 	msyslog(LOG_ERR,
2835 		"Can not add multicast address %s: no multicast support",
2836 		stoa(addr));
2837 #endif
2838 	return;
2839 }
2840 
2841 
2842 /*
2843  * io_multicast_del() - delete multicast group address
2844  */
2845 void
2846 io_multicast_del(
2847 	sockaddr_u *	addr
2848 	)
2849 {
2850 #ifdef MCAST
2851 	endpt *iface;
2852 
2853 	/*
2854 	 * Check to see if this is a multicast address
2855 	 */
2856 	if (!addr_ismulticast(addr)) {
2857 		msyslog(LOG_ERR, "invalid multicast address %s",
2858 			stoa(addr));
2859 		return;
2860 	}
2861 
2862 	/*
2863 	 * Disable reception of multicast packets
2864 	 */
2865 	while ((iface = find_flagged_addr_in_list(addr, INT_MCASTOPEN))
2866 	       != NULL)
2867 		socket_multicast_disable(iface, addr);
2868 
2869 	delete_addr_from_list(addr);
2870 
2871 #else /* not MCAST */
2872 	msyslog(LOG_ERR,
2873 		"Can not delete multicast address %s: no multicast support",
2874 		stoa(addr));
2875 #endif /* not MCAST */
2876 }
2877 
2878 
2879 /*
2880  * open_socket - open a socket, returning the file descriptor
2881  */
2882 
2883 static SOCKET
2884 open_socket(
2885 	sockaddr_u *	addr,
2886 	int		bcast,
2887 	int		turn_off_reuse,
2888 	endpt *		interf
2889 	)
2890 {
2891 	SOCKET	fd;
2892 	int	errval;
2893 	/*
2894 	 * int is OK for REUSEADR per
2895 	 * http://www.kohala.com/start/mcast.api.txt
2896 	 */
2897 	int	on = 1;
2898 	int	off = 0;
2899 
2900 	if (IS_IPV6(addr) && !ipv6_works)
2901 		return INVALID_SOCKET;
2902 
2903 	/* create a datagram (UDP) socket */
2904 	fd = socket(AF(addr), SOCK_DGRAM, 0);
2905 	if (INVALID_SOCKET == fd) {
2906 		errval = socket_errno();
2907 		msyslog(LOG_ERR,
2908 			"socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
2909 			IS_IPV6(addr) ? "6" : "", stoa(addr));
2910 
2911 		if (errval == EPROTONOSUPPORT ||
2912 		    errval == EAFNOSUPPORT ||
2913 		    errval == EPFNOSUPPORT)
2914 			return (INVALID_SOCKET);
2915 
2916 		errno = errval;
2917 		msyslog(LOG_ERR,
2918 			"unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - exiting",
2919 			errno);
2920 		exit(1);
2921 	}
2922 
2923 #ifdef SYS_WINNT
2924 	connection_reset_fix(fd, addr);
2925 #endif
2926 	/*
2927 	 * Fixup the file descriptor for some systems
2928 	 * See bug #530 for details of the issue.
2929 	 */
2930 	fd = move_fd(fd);
2931 
2932 	/*
2933 	 * set SO_REUSEADDR since we will be binding the same port
2934 	 * number on each interface according to turn_off_reuse.
2935 	 * This is undesirable on Windows versions starting with
2936 	 * Windows XP (numeric version 5.1).
2937 	 */
2938 #ifdef SYS_WINNT
2939 	if (isc_win32os_versioncheck(5, 1, 0, 0) < 0)  /* before 5.1 */
2940 #endif
2941 		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2942 			       (void *)((turn_off_reuse)
2943 					    ? &off
2944 					    : &on),
2945 			       sizeof(on))) {
2946 
2947 			msyslog(LOG_ERR,
2948 				"setsockopt SO_REUSEADDR %s fails for address %s: %m",
2949 				(turn_off_reuse)
2950 				    ? "off"
2951 				    : "on",
2952 				stoa(addr));
2953 			closesocket(fd);
2954 			return INVALID_SOCKET;
2955 		}
2956 #ifdef SO_EXCLUSIVEADDRUSE
2957 	/*
2958 	 * setting SO_EXCLUSIVEADDRUSE on the wildcard we open
2959 	 * first will cause more specific binds to fail.
2960 	 */
2961 	if (!(interf->flags & INT_WILDCARD))
2962 		set_excladdruse(fd);
2963 #endif
2964 
2965 	/*
2966 	 * IPv4 specific options go here
2967 	 */
2968 	if (IS_IPV4(addr)) {
2969 #if defined(IPPROTO_IP) && defined(IP_TOS)
2970 		if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void *)&qos,
2971 			       sizeof(qos)))
2972 			msyslog(LOG_ERR,
2973 				"setsockopt IP_TOS (%02x) fails on address %s: %m",
2974 				qos, stoa(addr));
2975 #endif /* IPPROTO_IP && IP_TOS */
2976 		if (bcast)
2977 			socket_broadcast_enable(interf, fd, addr);
2978 	}
2979 
2980 	/*
2981 	 * IPv6 specific options go here
2982 	 */
2983 	if (IS_IPV6(addr)) {
2984 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
2985 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, (void *)&qos,
2986 			       sizeof(qos)))
2987 			msyslog(LOG_ERR,
2988 				"setsockopt IPV6_TCLASS (%02x) fails on address %s: %m",
2989 				qos, stoa(addr));
2990 #endif /* IPPROTO_IPV6 && IPV6_TCLASS */
2991 #ifdef IPV6_V6ONLY
2992 		if (isc_net_probe_ipv6only() == ISC_R_SUCCESS
2993 		    && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
2994 		    (void *)&on, sizeof(on)))
2995 			msyslog(LOG_ERR,
2996 				"setsockopt IPV6_V6ONLY on fails on address %s: %m",
2997 				stoa(addr));
2998 #endif
2999 #ifdef IPV6_BINDV6ONLY
3000 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
3001 		    (void *)&on, sizeof(on)))
3002 			msyslog(LOG_ERR,
3003 				"setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
3004 				stoa(addr));
3005 #endif
3006 	}
3007 
3008 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
3009 	/*
3010 	 * some OSes don't allow binding to more specific
3011 	 * addresses if a wildcard address already bound
3012 	 * to the port and SO_REUSEADDR is not set
3013 	 */
3014 	if (!is_wildcard_addr(addr))
3015 		set_wildcard_reuse(AF(addr), 1);
3016 #endif
3017 
3018 	/*
3019 	 * bind the local address.
3020 	 */
3021 	errval = bind(fd, &addr->sa, SOCKLEN(addr));
3022 
3023 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
3024 	if (!is_wildcard_addr(addr))
3025 		set_wildcard_reuse(AF(addr), 0);
3026 #endif
3027 
3028 	if (errval < 0) {
3029 		/*
3030 		 * Don't log this under all conditions
3031 		 */
3032 		if (turn_off_reuse == 0
3033 #ifdef DEBUG
3034 		    || debug > 1
3035 #endif
3036 		    ) {
3037 			msyslog(LOG_ERR,
3038 				"bind(%d) AF_INET%s %s#%d%s flags 0x%x failed: %m",
3039 				fd, IS_IPV6(addr) ? "6" : "",
3040 				stoa(addr), SRCPORT(addr),
3041 				IS_MCAST(addr) ? " (multicast)" : "",
3042 				interf->flags);
3043 		}
3044 
3045 		closesocket(fd);
3046 
3047 		return INVALID_SOCKET;
3048 	}
3049 
3050 #ifdef HAVE_TIMESTAMP
3051 	{
3052 		if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
3053 			       (void *)&on, sizeof(on)))
3054 			msyslog(LOG_DEBUG,
3055 				"setsockopt SO_TIMESTAMP on fails on address %s: %m",
3056 				stoa(addr));
3057 		else
3058 			DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
3059 				    fd, stoa(addr)));
3060 	}
3061 #endif
3062 #ifdef HAVE_TIMESTAMPNS
3063 	{
3064 		if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
3065 			       (void *)&on, sizeof(on)))
3066 			msyslog(LOG_DEBUG,
3067 				"setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
3068 				stoa(addr));
3069 		else
3070 			DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
3071 				    fd, stoa(addr)));
3072 	}
3073 #endif
3074 #ifdef HAVE_BINTIME
3075 	{
3076 		if (setsockopt(fd, SOL_SOCKET, SO_BINTIME,
3077 			       (void *)&on, sizeof(on)))
3078 			msyslog(LOG_DEBUG,
3079 				"setsockopt SO_BINTIME on fails on address %s: %m",
3080 				stoa(addr));
3081 		else
3082 			DPRINTF(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
3083 				    fd, stoa(addr)));
3084 	}
3085 #endif
3086 
3087 	DPRINTF(4, ("bind(%d) AF_INET%s, addr %s%%%d#%d, flags 0x%x\n",
3088 		   fd, IS_IPV6(addr) ? "6" : "", stoa(addr),
3089 		   SCOPE(addr), SRCPORT(addr), interf->flags));
3090 
3091 	make_socket_nonblocking(fd);
3092 
3093 #ifdef HAVE_SIGNALED_IO
3094 	init_socket_sig(fd);
3095 #endif /* not HAVE_SIGNALED_IO */
3096 
3097 	add_fd_to_list(fd, FD_TYPE_SOCKET);
3098 
3099 #if !defined(SYS_WINNT) && !defined(VMS)
3100 	DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
3101 		    fcntl(fd, F_GETFL, 0)));
3102 #endif /* SYS_WINNT || VMS */
3103 
3104 #if defined(HAVE_IO_COMPLETION_PORT)
3105 /*
3106  * Add the socket to the completion port
3107  */
3108 	if (!io_completion_port_add_socket(fd, interf, bcast)) {
3109 		msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
3110 		exit(1);
3111 	}
3112 #endif
3113 	return fd;
3114 }
3115 
3116 
3117 
3118 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
3119 /*
3120  * sendpkt - send a packet to the specified destination. Maintain a
3121  * send error cache so that only the first consecutive error for a
3122  * destination is logged.
3123  */
3124 void
3125 sendpkt(
3126 	sockaddr_u *		dest,
3127 	struct interface *	ep,
3128 	int			ttl,
3129 	struct pkt *		pkt,
3130 	int			len
3131 	)
3132 {
3133 	endpt *	src;
3134 	int	ismcast;
3135 	int	cc;
3136 	int	rc;
3137 	u_char	cttl;
3138 	l_fp	fp_zero = { { 0, }, 0, };
3139 
3140 	ismcast = IS_MCAST(dest);
3141 	if (!ismcast)
3142 		src = ep;
3143 	else
3144 		src = (IS_IPV4(dest))
3145 			  ? mc4_list
3146 			  : mc6_list;
3147 
3148 	if (NULL == src) {
3149 		/*
3150 		 * unbound peer - drop request and wait for better
3151 		 * network conditions
3152 		 */
3153 		DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
3154 			    ismcast ? "\tMCAST\t***** " : "",
3155 			    stoa(dest), ttl, len));
3156 		return;
3157 	}
3158 
3159 	do {
3160 		DPRINTF(2, ("%ssendpkt(%d, dst=%s, src=%s, ttl=%d, len=%d)\n",
3161 			    ismcast ? "\tMCAST\t***** " : "", src->fd,
3162 			    stoa(dest), stoa(&src->sin), ttl, len));
3163 #ifdef MCAST
3164 		/*
3165 		 * for the moment we use the bcast option to set multicast ttl
3166 		 */
3167 		if (ismcast && ttl > 0 && ttl != src->last_ttl) {
3168 			/*
3169 			 * set the multicast ttl for outgoing packets
3170 			 */
3171 			switch (AF(&src->sin)) {
3172 
3173 			case AF_INET :
3174 				cttl = (u_char)ttl;
3175 				rc = setsockopt(src->fd, IPPROTO_IP,
3176 						IP_MULTICAST_TTL,
3177 						(void *)&cttl,
3178 						sizeof(cttl));
3179 				break;
3180 
3181 # ifdef INCLUDE_IPV6_SUPPORT
3182 			case AF_INET6 :
3183 				rc = setsockopt(src->fd, IPPROTO_IPV6,
3184 						 IPV6_MULTICAST_HOPS,
3185 						 (void *)&ttl,
3186 						 sizeof(ttl));
3187 				break;
3188 # endif	/* INCLUDE_IPV6_SUPPORT */
3189 
3190 			default:
3191 				rc = 0;
3192 			}
3193 
3194 			if (!rc)
3195 				src->last_ttl = ttl;
3196 			else
3197 				msyslog(LOG_ERR,
3198 					"setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
3199 					stoa(&src->sin));
3200 		}
3201 #endif	/* MCAST */
3202 
3203 #ifdef SIM
3204 		cc = simulate_server(dest, src, pkt);
3205 #elif defined(HAVE_IO_COMPLETION_PORT)
3206 		cc = io_completion_port_sendto(src, src->fd, pkt,
3207 			(size_t)len, (sockaddr_u *)&dest->sa);
3208 #else
3209 		cc = sendto(src->fd, (char *)pkt, (u_int)len, 0,
3210 			    &dest->sa, SOCKLEN(dest));
3211 #endif
3212 		if (cc == -1) {
3213 			src->notsent++;
3214 			packets_notsent++;
3215 		} else	{
3216 			src->sent++;
3217 			packets_sent++;
3218 		}
3219 		if (ismcast)
3220 			src = src->mclink;
3221 	} while (ismcast && src != NULL);
3222 
3223 	/* HMS: pkt->rootdisp is usually random here */
3224 	record_raw_stats(src ? &src->sin : NULL, dest,
3225 			&pkt->org, &pkt->rec, &pkt->xmt, &fp_zero,
3226 			PKT_MODE(pkt->li_vn_mode),
3227 			PKT_VERSION(pkt->li_vn_mode),
3228 			PKT_LEAP(pkt->li_vn_mode),
3229 			pkt->stratum,
3230 			pkt->ppoll, pkt->precision,
3231 			pkt->rootdelay, pkt->rootdisp, pkt->refid,
3232 			len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
3233 
3234 	return;
3235 }
3236 
3237 
3238 #if !defined(HAVE_IO_COMPLETION_PORT)
3239 #if !defined(HAVE_SIGNALED_IO)
3240 /*
3241  * fdbits - generate ascii representation of fd_set (FAU debug support)
3242  * HFDF format - highest fd first.
3243  */
3244 static char *
3245 fdbits(
3246 	int		count,
3247 	const fd_set*	set
3248 	)
3249 {
3250 	static char buffer[256];
3251 	char * buf = buffer;
3252 
3253 	count = min(count,  255);
3254 
3255 	while (count >= 0) {
3256 		*buf++ = FD_ISSET(count, set) ? '#' : '-';
3257 		count--;
3258 	}
3259 	*buf = '\0';
3260 
3261 	return buffer;
3262 }
3263 #endif
3264 
3265 #ifdef REFCLOCK
3266 /*
3267  * Routine to read the refclock packets for a specific interface
3268  * Return the number of bytes read. That way we know if we should
3269  * read it again or go on to the next one if no bytes returned
3270  */
3271 static inline int
3272 read_refclock_packet(
3273 	SOCKET			fd,
3274 	struct refclockio *	rp,
3275 	l_fp			ts
3276 	)
3277 {
3278 	u_int			read_count;
3279 	int			buflen;
3280 	int			saved_errno;
3281 	int			consumed;
3282 	struct recvbuf *	rb;
3283 
3284 	rb = get_free_recv_buffer();
3285 
3286 	if (NULL == rb) {
3287 		/*
3288 		 * No buffer space available - just drop the packet
3289 		 */
3290 		char buf[RX_BUFF_SIZE];
3291 
3292 		buflen = read(fd, buf, sizeof buf);
3293 		packets_dropped++;
3294 		return (buflen);
3295 	}
3296 
3297 	/* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead
3298 	 * to buffer overrun and memory corruption
3299 	 */
3300 	if (rp->datalen <= 0 || (size_t)rp->datalen > sizeof(rb->recv_space))
3301 		read_count = sizeof(rb->recv_space);
3302 	else
3303 		read_count = (u_int)rp->datalen;
3304 	do {
3305 		buflen = read(fd, (char *)&rb->recv_space, read_count);
3306 	} while (buflen < 0 && EINTR == errno);
3307 
3308 	if (buflen <= 0) {
3309 		saved_errno = errno;
3310 		freerecvbuf(rb);
3311 		errno = saved_errno;
3312 		return buflen;
3313 	}
3314 
3315 	/*
3316 	 * Got one. Mark how and when it got here,
3317 	 * put it on the full list and do bookkeeping.
3318 	 */
3319 	rb->recv_length = buflen;
3320 	rb->recv_peer = rp->srcclock;
3321 	rb->dstadr = 0;
3322 	rb->fd = fd;
3323 	rb->recv_time = ts;
3324 	rb->receiver = rp->clock_recv;
3325 
3326 	consumed = indicate_refclock_packet(rp, rb);
3327 	if (!consumed) {
3328 		rp->recvcount++;
3329 		packets_received++;
3330 	}
3331 
3332 	return buflen;
3333 }
3334 #endif	/* REFCLOCK */
3335 
3336 
3337 #ifdef HAVE_PACKET_TIMESTAMP
3338 /*
3339  * extract timestamps from control message buffer
3340  */
3341 static l_fp
3342 fetch_timestamp(
3343 	struct recvbuf *	rb,
3344 	struct msghdr *		msghdr,
3345 	l_fp			ts
3346 	)
3347 {
3348 	struct cmsghdr *	cmsghdr;
3349 	unsigned long		ticks;
3350 	double			fuzz;
3351 	l_fp			lfpfuzz;
3352 	l_fp			nts;
3353 #ifdef DEBUG_TIMING
3354 	l_fp			dts;
3355 #endif
3356 
3357 	cmsghdr = CMSG_FIRSTHDR(msghdr);
3358 	while (cmsghdr != NULL) {
3359 		switch (cmsghdr->cmsg_type)
3360 		{
3361 #ifdef HAVE_BINTIME
3362 		case SCM_BINTIME:
3363 #endif  /* HAVE_BINTIME */
3364 #ifdef HAVE_TIMESTAMPNS
3365 		case SCM_TIMESTAMPNS:
3366 #endif	/* HAVE_TIMESTAMPNS */
3367 #ifdef HAVE_TIMESTAMP
3368 		case SCM_TIMESTAMP:
3369 #endif	/* HAVE_TIMESTAMP */
3370 #if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP)
3371 			switch (cmsghdr->cmsg_type)
3372 			{
3373 #ifdef HAVE_BINTIME
3374 			case SCM_BINTIME:
3375 				{
3376 					struct bintime	pbt;
3377 					memcpy(&pbt, CMSG_DATA(cmsghdr), sizeof(pbt));
3378 					/*
3379 					 * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
3380 					 */
3381 					nts.l_i = pbt.sec + JAN_1970;
3382 					nts.l_uf = (u_int32)(pbt.frac >> 32);
3383 					if (sys_tick > measured_tick &&
3384 					    sys_tick > 1e-9) {
3385 						ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC));
3386 						nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC));
3387 					}
3388 					DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
3389 						    pbt.sec, (unsigned long)((nts.l_uf / FRAC) * 1e9)));
3390 				}
3391 				break;
3392 #endif  /* HAVE_BINTIME */
3393 #ifdef HAVE_TIMESTAMPNS
3394 			case SCM_TIMESTAMPNS:
3395 				{
3396 					struct timespec	pts;
3397 					memcpy(&pts, CMSG_DATA(cmsghdr), sizeof(pts));
3398 					if (sys_tick > measured_tick &&
3399 					    sys_tick > 1e-9) {
3400 						ticks = (unsigned long)((pts.tv_nsec * 1e-9) /
3401 									sys_tick);
3402 						pts.tv_nsec = (long)(ticks * 1e9 *
3403 								     sys_tick);
3404 					}
3405 					DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
3406 						    pts.tv_sec, pts.tv_nsec));
3407 					nts = tspec_stamp_to_lfp(pts);
3408 				}
3409 				break;
3410 #endif	/* HAVE_TIMESTAMPNS */
3411 #ifdef HAVE_TIMESTAMP
3412 			case SCM_TIMESTAMP:
3413 				{
3414 					struct timeval	ptv;
3415 					memcpy(&ptv, CMSG_DATA(cmsghdr), sizeof(ptv));
3416 					if (sys_tick > measured_tick &&
3417 					    sys_tick > 1e-6) {
3418 						ticks = (unsigned long)((ptv.tv_usec * 1e-6) /
3419 									sys_tick);
3420 						ptv.tv_usec = (long)(ticks * 1e6 *
3421 								    sys_tick);
3422 					}
3423 					DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
3424 						    (intmax_t)ptv.tv_sec, (long)ptv.tv_usec));
3425 					nts = tval_stamp_to_lfp(ptv);
3426 				}
3427 				break;
3428 #endif  /* HAVE_TIMESTAMP */
3429 			}
3430 			fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
3431 			DTOLFP(fuzz, &lfpfuzz);
3432 			L_ADD(&nts, &lfpfuzz);
3433 #ifdef DEBUG_TIMING
3434 			dts = ts;
3435 			L_SUB(&dts, &nts);
3436 			collect_timing(rb, "input processing delay", 1,
3437 				       &dts);
3438 			DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
3439 				    lfptoa(&dts, 9)));
3440 #endif	/* DEBUG_TIMING */
3441 			ts = nts;  /* network time stamp */
3442 			break;
3443 #endif	/* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */
3444 
3445 		default:
3446 			DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n",
3447 				    cmsghdr->cmsg_type));
3448 		}
3449 		cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
3450 	}
3451 	return ts;
3452 }
3453 #endif	/* HAVE_PACKET_TIMESTAMP */
3454 
3455 
3456 /*
3457  * Routine to read the network NTP packets for a specific interface
3458  * Return the number of bytes read. That way we know if we should
3459  * read it again or go on to the next one if no bytes returned
3460  */
3461 static inline int
3462 read_network_packet(
3463 	SOCKET			fd,
3464 	struct interface *	itf,
3465 	l_fp			ts
3466 	)
3467 {
3468 	GETSOCKNAME_SOCKLEN_TYPE fromlen;
3469 	int buflen;
3470 	register struct recvbuf *rb;
3471 #ifdef HAVE_PACKET_TIMESTAMP
3472 	struct msghdr msghdr;
3473 	struct iovec iovec;
3474 	char control[CMSG_BUFSIZE];
3475 #endif
3476 
3477 	/*
3478 	 * Get a buffer and read the frame.  If we
3479 	 * haven't got a buffer, or this is received
3480 	 * on a disallowed socket, just dump the
3481 	 * packet.
3482 	 */
3483 
3484 	rb = get_free_recv_buffer();
3485 	if (NULL == rb || itf->ignore_packets) {
3486 		char buf[RX_BUFF_SIZE];
3487 		sockaddr_u from;
3488 
3489 		if (rb != NULL)
3490 			freerecvbuf(rb);
3491 
3492 		fromlen = sizeof(from);
3493 		buflen = recvfrom(fd, buf, sizeof(buf), 0,
3494 				  &from.sa, &fromlen);
3495 		DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
3496 			(itf->ignore_packets)
3497 			    ? "ignore"
3498 			    : "drop",
3499 			free_recvbuffs(), fd, stoa(&from)));
3500 		if (itf->ignore_packets)
3501 			packets_ignored++;
3502 		else
3503 			packets_dropped++;
3504 		return (buflen);
3505 	}
3506 
3507 	fromlen = sizeof(rb->recv_srcadr);
3508 
3509 #ifndef HAVE_PACKET_TIMESTAMP
3510 	rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
3511 				   sizeof(rb->recv_space), 0,
3512 				   &rb->recv_srcadr.sa, &fromlen);
3513 #else
3514 	iovec.iov_base        = &rb->recv_space;
3515 	iovec.iov_len         = sizeof(rb->recv_space);
3516 	msghdr.msg_name       = &rb->recv_srcadr;
3517 	msghdr.msg_namelen    = fromlen;
3518 	msghdr.msg_iov        = &iovec;
3519 	msghdr.msg_iovlen     = 1;
3520 	msghdr.msg_control    = (void *)&control;
3521 	msghdr.msg_controllen = sizeof(control);
3522 	msghdr.msg_flags      = 0;
3523 	rb->recv_length       = recvmsg(fd, &msghdr, 0);
3524 #endif
3525 
3526 	buflen = rb->recv_length;
3527 
3528 	if (buflen == 0 || (buflen == -1 &&
3529 	    (EWOULDBLOCK == errno
3530 #ifdef EAGAIN
3531 	     || EAGAIN == errno
3532 #endif
3533 	     ))) {
3534 		freerecvbuf(rb);
3535 		return (buflen);
3536 	} else if (buflen < 0) {
3537 		msyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
3538 			stoa(&rb->recv_srcadr), fd);
3539 		DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n",
3540 			    fd));
3541 		freerecvbuf(rb);
3542 		return (buflen);
3543 	}
3544 
3545 	DPRINTF(3, ("read_network_packet: fd=%d length %d from %s\n",
3546 		    fd, buflen, stoa(&rb->recv_srcadr)));
3547 
3548 #ifdef ENABLE_BUG3020_FIX
3549 	if (ISREFCLOCKADR(&rb->recv_srcadr)) {
3550 		msyslog(LOG_ERR, "recvfrom(%s) fd=%d: refclock srcadr on a network interface!",
3551 			stoa(&rb->recv_srcadr), fd);
3552 		DPRINTF(1, ("read_network_packet: fd=%d dropped (refclock srcadr))\n",
3553 			    fd));
3554 		packets_dropped++;
3555 		freerecvbuf(rb);
3556 		return (buflen);
3557 	}
3558 #endif
3559 
3560 	/*
3561 	** Bug 2672: Some OSes (MacOSX and Linux) don't block spoofed ::1
3562 	*/
3563 
3564 	if (AF_INET6 == itf->family) {
3565 		DPRINTF(2, ("Got an IPv6 packet, from <%s> (%d) to <%s> (%d)\n",
3566 			stoa(&rb->recv_srcadr),
3567 			IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr)),
3568 			stoa(&itf->sin),
3569 			!IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3570 			));
3571 
3572 		if (   IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr))
3573 		    && !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3574 		   ) {
3575 			packets_dropped++;
3576 			DPRINTF(2, ("DROPPING that packet\n"));
3577 			freerecvbuf(rb);
3578 			return buflen;
3579 		}
3580 		DPRINTF(2, ("processing that packet\n"));
3581 	}
3582 
3583 	/*
3584 	 * Got one.  Mark how and when it got here,
3585 	 * put it on the full list and do bookkeeping.
3586 	 */
3587 	rb->dstadr = itf;
3588 	rb->fd = fd;
3589 #ifdef HAVE_PACKET_TIMESTAMP
3590 	/* pick up a network time stamp if possible */
3591 	ts = fetch_timestamp(rb, &msghdr, ts);
3592 #endif
3593 	rb->recv_time = ts;
3594 	rb->receiver = receive;
3595 
3596 	add_full_recv_buffer(rb);
3597 
3598 	itf->received++;
3599 	packets_received++;
3600 	return (buflen);
3601 }
3602 
3603 /*
3604  * attempt to handle io (select()/signaled IO)
3605  */
3606 void
3607 io_handler(void)
3608 {
3609 #  ifndef HAVE_SIGNALED_IO
3610 	fd_set rdfdes;
3611 	int nfound;
3612 
3613 	/*
3614 	 * Use select() on all on all input fd's for unlimited
3615 	 * time.  select() will terminate on SIGALARM or on the
3616 	 * reception of input.	Using select() means we can't do
3617 	 * robust signal handling and we get a potential race
3618 	 * between checking for alarms and doing the select().
3619 	 * Mostly harmless, I think.
3620 	 */
3621 	/*
3622 	 * On VMS, I suspect that select() can't be interrupted
3623 	 * by a "signal" either, so I take the easy way out and
3624 	 * have select() time out after one second.
3625 	 * System clock updates really aren't time-critical,
3626 	 * and - lacking a hardware reference clock - I have
3627 	 * yet to learn about anything else that is.
3628 	 */
3629 	++handler_calls;
3630 	rdfdes = activefds;
3631 #   if !defined(VMS) && !defined(SYS_VXWORKS)
3632 	nfound = select(maxactivefd + 1, &rdfdes, NULL,
3633 			NULL, NULL);
3634 #   else	/* VMS, VxWorks */
3635 	/* make select() wake up after one second */
3636 	{
3637 		struct timeval t1;
3638 		t1.tv_sec  = 1;
3639 		t1.tv_usec = 0;
3640 		nfound = select(maxactivefd + 1,
3641 				&rdfdes, NULL, NULL,
3642 				&t1);
3643 	}
3644 #   endif	/* VMS, VxWorks */
3645 	if (nfound < 0 && sanitize_fdset(errno)) {
3646 		struct timeval t1;
3647 		t1.tv_sec  = 0;
3648 		t1.tv_usec = 0;
3649 		rdfdes = activefds;
3650 		nfound = select(maxactivefd + 1,
3651 				&rdfdes, NULL, NULL,
3652 				&t1);
3653 	}
3654 
3655 	if (nfound > 0) {
3656 		l_fp ts;
3657 
3658 		get_systime(&ts);
3659 
3660 		input_handler_scan(&ts, &rdfdes);
3661 	} else if (nfound == -1 && errno != EINTR) {
3662 		msyslog(LOG_ERR, "select() error: %m");
3663 	}
3664 #   ifdef DEBUG
3665 	else if (debug > 4) {
3666 		msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
3667 	} else {
3668 		DPRINTF(3, ("select() returned %d: %m\n", nfound));
3669 	}
3670 #   endif /* DEBUG */
3671 #  else /* HAVE_SIGNALED_IO */
3672 	wait_for_signal();
3673 #  endif /* HAVE_SIGNALED_IO */
3674 }
3675 
3676 #ifdef HAVE_SIGNALED_IO
3677 /*
3678  * input_handler - receive packets asynchronously
3679  *
3680  * ALWAYS IN SIGNAL HANDLER CONTEXT -- only async-safe functions allowed!
3681  */
3682 static RETSIGTYPE
3683 input_handler(
3684 	l_fp *	cts
3685 	)
3686 {
3687 	int		n;
3688 	struct timeval	tvzero;
3689 	fd_set		fds;
3690 
3691 	++handler_calls;
3692 
3693 	/*
3694 	 * Do a poll to see who has data
3695 	 */
3696 
3697 	fds = activefds;
3698 	tvzero.tv_sec = tvzero.tv_usec = 0;
3699 
3700 	n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3701 	if (n < 0 && sanitize_fdset(errno)) {
3702 		fds = activefds;
3703 		tvzero.tv_sec = tvzero.tv_usec = 0;
3704 		n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3705 	}
3706 	if (n > 0)
3707 		input_handler_scan(cts, &fds);
3708 }
3709 #endif /* HAVE_SIGNALED_IO */
3710 
3711 
3712 /*
3713  * Try to sanitize the global FD set
3714  *
3715  * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3716  */
3717 static int/*BOOL*/
3718 sanitize_fdset(
3719 	int	errc
3720 	)
3721 {
3722 	int j, b, maxscan;
3723 
3724 #  ifndef HAVE_SIGNALED_IO
3725 	/*
3726 	 * extended FAU debugging output
3727 	 */
3728 	if (errc != EINTR) {
3729 		msyslog(LOG_ERR,
3730 			"select(%d, %s, 0L, 0L, &0.0) error: %m",
3731 			maxactivefd + 1,
3732 			fdbits(maxactivefd, &activefds));
3733 	}
3734 #   endif
3735 
3736 	if (errc != EBADF)
3737 		return FALSE;
3738 
3739 	/* if we have oviously bad FDs, try to sanitize the FD set. */
3740 	for (j = 0, maxscan = 0; j <= maxactivefd; j++) {
3741 		if (FD_ISSET(j, &activefds)) {
3742 			if (-1 != read(j, &b, 0)) {
3743 				maxscan = j;
3744 				continue;
3745 			}
3746 #		    ifndef HAVE_SIGNALED_IO
3747 			msyslog(LOG_ERR,
3748 				"Removing bad file descriptor %d from select set",
3749 				j);
3750 #		    endif
3751 			FD_CLR(j, &activefds);
3752 		}
3753 	}
3754 	if (maxactivefd != maxscan)
3755 		maxactivefd = maxscan;
3756 	return TRUE;
3757 }
3758 
3759 /*
3760  * scan the known FDs (clocks, servers, ...) for presence in a 'fd_set'.
3761  *
3762  * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3763  */
3764 static void
3765 input_handler_scan(
3766 	const l_fp *	cts,
3767 	const fd_set *	pfds
3768 	)
3769 {
3770 	int		buflen;
3771 	u_int		idx;
3772 	int		doing;
3773 	SOCKET		fd;
3774 	blocking_child *c;
3775 	l_fp		ts;	/* Timestamp at BOselect() gob */
3776 
3777 #if defined(DEBUG_TIMING)
3778 	l_fp		ts_e;	/* Timestamp at EOselect() gob */
3779 #endif
3780 	endpt *		ep;
3781 #ifdef REFCLOCK
3782 	struct refclockio *rp;
3783 	int		saved_errno;
3784 	const char *	clk;
3785 #endif
3786 #ifdef HAS_ROUTING_SOCKET
3787 	struct asyncio_reader *	asyncio_reader;
3788 	struct asyncio_reader *	next_asyncio_reader;
3789 #endif
3790 
3791 	++handler_pkts;
3792 	ts = *cts;
3793 
3794 #ifdef REFCLOCK
3795 	/*
3796 	 * Check out the reference clocks first, if any
3797 	 */
3798 
3799 	for (rp = refio; rp != NULL; rp = rp->next) {
3800 		fd = rp->fd;
3801 
3802 		if (!FD_ISSET(fd, pfds))
3803 			continue;
3804 		buflen = read_refclock_packet(fd, rp, ts);
3805 		/*
3806 		 * The first read must succeed after select() indicates
3807 		 * readability, or we've reached a permanent EOF.
3808 		 * http://bugs.ntp.org/1732 reported ntpd munching CPU
3809 		 * after a USB GPS was unplugged because select was
3810 		 * indicating EOF but ntpd didn't remove the descriptor
3811 		 * from the activefds set.
3812 		 */
3813 		if (buflen < 0 && EAGAIN != errno) {
3814 			saved_errno = errno;
3815 			clk = refnumtoa(&rp->srcclock->srcadr);
3816 			errno = saved_errno;
3817 			msyslog(LOG_ERR, "%s read: %m", clk);
3818 			maintain_activefds(fd, TRUE);
3819 		} else if (0 == buflen) {
3820 			clk = refnumtoa(&rp->srcclock->srcadr);
3821 			msyslog(LOG_ERR, "%s read EOF", clk);
3822 			maintain_activefds(fd, TRUE);
3823 		} else {
3824 			/* drain any remaining refclock input */
3825 			do {
3826 				buflen = read_refclock_packet(fd, rp, ts);
3827 			} while (buflen > 0);
3828 		}
3829 	}
3830 #endif /* REFCLOCK */
3831 
3832 	/*
3833 	 * Loop through the interfaces looking for data to read.
3834 	 */
3835 	for (ep = ep_list; ep != NULL; ep = ep->elink) {
3836 		for (doing = 0; doing < 2; doing++) {
3837 			if (!doing) {
3838 				fd = ep->fd;
3839 			} else {
3840 				if (!(ep->flags & INT_BCASTOPEN))
3841 					break;
3842 				fd = ep->bfd;
3843 			}
3844 			if (fd < 0)
3845 				continue;
3846 			if (FD_ISSET(fd, pfds))
3847 				do {
3848 					buflen = read_network_packet(
3849 							fd, ep, ts);
3850 				} while (buflen > 0);
3851 			/* Check more interfaces */
3852 		}
3853 	}
3854 
3855 #ifdef HAS_ROUTING_SOCKET
3856 	/*
3857 	 * scan list of asyncio readers - currently only used for routing sockets
3858 	 */
3859 	asyncio_reader = asyncio_reader_list;
3860 
3861 	while (asyncio_reader != NULL) {
3862 		/* callback may unlink and free asyncio_reader */
3863 		next_asyncio_reader = asyncio_reader->link;
3864 		if (FD_ISSET(asyncio_reader->fd, pfds))
3865 			(*asyncio_reader->receiver)(asyncio_reader);
3866 		asyncio_reader = next_asyncio_reader;
3867 	}
3868 #endif /* HAS_ROUTING_SOCKET */
3869 
3870 	/*
3871 	 * Check for a response from a blocking child
3872 	 */
3873 	for (idx = 0; idx < blocking_children_alloc; idx++) {
3874 		c = blocking_children[idx];
3875 		if (NULL == c || -1 == c->resp_read_pipe)
3876 			continue;
3877 		if (FD_ISSET(c->resp_read_pipe, pfds)) {
3878 			++c->resp_ready_seen;
3879 			++blocking_child_ready_seen;
3880 		}
3881 	}
3882 
3883 	/* We've done our work */
3884 #if defined(DEBUG_TIMING)
3885 	get_systime(&ts_e);
3886 	/*
3887 	 * (ts_e - ts) is the amount of time we spent
3888 	 * processing this gob of file descriptors.  Log
3889 	 * it.
3890 	 */
3891 	L_SUB(&ts_e, &ts);
3892 	collect_timing(NULL, "input handler", 1, &ts_e);
3893 	if (debug > 3)
3894 		msyslog(LOG_DEBUG,
3895 			"input_handler: Processed a gob of fd's in %s msec",
3896 			lfptoms(&ts_e, 6));
3897 #endif /* DEBUG_TIMING */
3898 }
3899 #endif /* !HAVE_IO_COMPLETION_PORT */
3900 
3901 /*
3902  * find an interface suitable for the src address
3903  */
3904 endpt *
3905 select_peerinterface(
3906 	struct peer *	peer,
3907 	sockaddr_u *	srcadr,
3908 	endpt *		dstadr
3909 	)
3910 {
3911 	endpt *ep;
3912 #ifndef SIM
3913 	endpt *wild;
3914 
3915 	wild = ANY_INTERFACE_CHOOSE(srcadr);
3916 
3917 	/*
3918 	 * Initialize the peer structure and dance the interface jig.
3919 	 * Reference clocks step the loopback waltz, the others
3920 	 * squaredance around the interface list looking for a buddy. If
3921 	 * the dance peters out, there is always the wildcard interface.
3922 	 * This might happen in some systems and would preclude proper
3923 	 * operation with public key cryptography.
3924 	 */
3925 	if (ISREFCLOCKADR(srcadr)) {
3926 		ep = loopback_interface;
3927 	} else if (peer->cast_flags &
3928 		   (MDF_BCLNT | MDF_ACAST | MDF_MCAST | MDF_BCAST)) {
3929 		ep = findbcastinter(srcadr);
3930 		if (ep != NULL)
3931 			DPRINTF(4, ("Found *-cast interface %s for address %s\n",
3932 				stoa(&ep->sin), stoa(srcadr)));
3933 		else
3934 			DPRINTF(4, ("No *-cast local address found for address %s\n",
3935 				stoa(srcadr)));
3936 	} else {
3937 		ep = dstadr;
3938 		if (NULL == ep)
3939 			ep = wild;
3940 	}
3941 	/*
3942 	 * If it is a multicast address, findbcastinter() may not find
3943 	 * it.  For unicast, we get to find the interface when dstadr is
3944 	 * given to us as the wildcard (ANY_INTERFACE_CHOOSE).  Either
3945 	 * way, try a little harder.
3946 	 */
3947 	if (wild == ep)
3948 		ep = findinterface(srcadr);
3949 	/*
3950 	 * we do not bind to the wildcard interfaces for output
3951 	 * as our (network) source address would be undefined and
3952 	 * crypto will not work without knowing the own transmit address
3953 	 */
3954 	if (ep != NULL && INT_WILDCARD & ep->flags)
3955 		if (!accept_wildcard_if_for_winnt)
3956 			ep = NULL;
3957 #else	/* SIM follows */
3958 	ep = loopback_interface;
3959 #endif
3960 
3961 	return ep;
3962 }
3963 
3964 
3965 /*
3966  * findinterface - find local interface corresponding to address
3967  */
3968 endpt *
3969 findinterface(
3970 	sockaddr_u *addr
3971 	)
3972 {
3973 	endpt *iface;
3974 
3975 	iface = findlocalinterface(addr, INT_WILDCARD, 0);
3976 
3977 	if (NULL == iface) {
3978 		DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
3979 			    stoa(addr)));
3980 
3981 		iface = ANY_INTERFACE_CHOOSE(addr);
3982 	} else
3983 		DPRINTF(4, ("Found interface #%d %s for address %s\n",
3984 			    iface->ifnum, iface->name, stoa(addr)));
3985 
3986 	return iface;
3987 }
3988 
3989 /*
3990  * findlocalinterface - find local interface corresponding to addr,
3991  * which does not have any of flags set.  If bast is nonzero, addr is
3992  * a broadcast address.
3993  *
3994  * This code attempts to find the local sending address for an outgoing
3995  * address by connecting a new socket to destinationaddress:NTP_PORT
3996  * and reading the sockname of the resulting connect.
3997  * the complicated sequence simulates the routing table lookup
3998  * for to first hop without duplicating any of the routing logic into
3999  * ntpd. preferably we would have used an API call - but its not there -
4000  * so this is the best we can do here short of duplicating to entire routing
4001  * logic in ntpd which would be a silly and really unportable thing to do.
4002  *
4003  */
4004 static endpt *
4005 findlocalinterface(
4006 	sockaddr_u *	addr,
4007 	int		flags,
4008 	int		bcast
4009 	)
4010 {
4011 	GETSOCKNAME_SOCKLEN_TYPE	sockaddrlen;
4012 	endpt *				iface;
4013 	sockaddr_u			saddr;
4014 	SOCKET				s;
4015 	int				rtn;
4016 	int				on;
4017 
4018 	DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
4019 		    stoa(addr)));
4020 
4021 	/* [Bug 3437] The dummy POOL peer comes in with an AF of
4022 	 * zero. This is bound to fail, but on the way to nowhere it
4023 	 * triggers a security incident on SELinux.
4024 	 *
4025 	 * Checking the condition and failing early is probably a good
4026 	 * advice, and even saves us some syscalls in that case.
4027 	 * Thanks to Miroslav Lichvar for finding this.
4028 	 */
4029 	if (AF_UNSPEC == AF(addr))
4030 		return NULL;
4031 
4032 	s = socket(AF(addr), SOCK_DGRAM, 0);
4033 	if (INVALID_SOCKET == s)
4034 		return NULL;
4035 
4036 	/*
4037 	 * If we are looking for broadcast interface we need to set this
4038 	 * socket to allow broadcast
4039 	 */
4040 	if (bcast) {
4041 		on = 1;
4042 		if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET,
4043 						SO_BROADCAST,
4044 						(void *)&on,
4045 						sizeof(on))) {
4046 			closesocket(s);
4047 			return NULL;
4048 		}
4049 	}
4050 
4051 	rtn = connect(s, &addr->sa, SOCKLEN(addr));
4052 	if (SOCKET_ERROR == rtn) {
4053 		closesocket(s);
4054 		return NULL;
4055 	}
4056 
4057 	sockaddrlen = sizeof(saddr);
4058 	rtn = getsockname(s, &saddr.sa, &sockaddrlen);
4059 	closesocket(s);
4060 	if (SOCKET_ERROR == rtn)
4061 		return NULL;
4062 
4063 	DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n",
4064 		    stoa(addr), stoa(&saddr)));
4065 
4066 	iface = getinterface(&saddr, flags);
4067 
4068 	/*
4069 	 * if we didn't find an exact match on saddr, find the closest
4070 	 * available local address.  This handles the case of the
4071 	 * address suggested by the kernel being excluded by nic rules
4072 	 * or the user's -I and -L options to ntpd.
4073 	 * See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683
4074 	 * for more background.
4075 	 */
4076 	if (NULL == iface || iface->ignore_packets)
4077 		iface = findclosestinterface(&saddr,
4078 					     flags | INT_LOOPBACK);
4079 
4080 	/* Don't use an interface which will ignore replies */
4081 	if (iface != NULL && iface->ignore_packets)
4082 		iface = NULL;
4083 
4084 	return iface;
4085 }
4086 
4087 
4088 /*
4089  * findclosestinterface
4090  *
4091  * If there are -I/--interface or -L/novirtualips command-line options,
4092  * or "nic" or "interface" rules in ntp.conf, findlocalinterface() may
4093  * find the kernel's preferred local address for a given peer address is
4094  * administratively unavailable to ntpd, and punt to this routine's more
4095  * expensive search.
4096  *
4097  * Find the numerically closest local address to the one connect()
4098  * suggested.  This matches an address on the same subnet first, as
4099  * needed by Bug 1184, and provides a consistent choice if there are
4100  * multiple feasible local addresses, regardless of the order ntpd
4101  * enumerated them.
4102  */
4103 endpt *
4104 findclosestinterface(
4105 	sockaddr_u *	addr,
4106 	int		flags
4107 	)
4108 {
4109 	endpt *		ep;
4110 	endpt *		winner;
4111 	sockaddr_u	addr_dist;
4112 	sockaddr_u	min_dist;
4113 
4114 	ZERO_SOCK(&min_dist);
4115 	winner = NULL;
4116 
4117 	for (ep = ep_list; ep != NULL; ep = ep->elink) {
4118 		if (ep->ignore_packets ||
4119 		    AF(addr) != ep->family ||
4120 		    flags & ep->flags)
4121 			continue;
4122 
4123 		calc_addr_distance(&addr_dist, addr, &ep->sin);
4124 		if (NULL == winner ||
4125 		    -1 == cmp_addr_distance(&addr_dist, &min_dist)) {
4126 			min_dist = addr_dist;
4127 			winner = ep;
4128 		}
4129 	}
4130 	if (NULL == winner)
4131 		DPRINTF(4, ("findclosestinterface(%s) failed\n",
4132 			    stoa(addr)));
4133 	else
4134 		DPRINTF(4, ("findclosestinterface(%s) -> %s\n",
4135 			    stoa(addr), stoa(&winner->sin)));
4136 
4137 	return winner;
4138 }
4139 
4140 
4141 /*
4142  * calc_addr_distance - calculate the distance between two addresses,
4143  *			the absolute value of the difference between
4144  *			the addresses numerically, stored as an address.
4145  */
4146 static void
4147 calc_addr_distance(
4148 	sockaddr_u *		dist,
4149 	const sockaddr_u *	a1,
4150 	const sockaddr_u *	a2
4151 	)
4152 {
4153 	u_int32	a1val;
4154 	u_int32	a2val;
4155 	u_int32	v4dist;
4156 	int	found_greater;
4157 	int	a1_greater;
4158 	int	i;
4159 
4160 	REQUIRE(AF(a1) == AF(a2));
4161 
4162 	ZERO_SOCK(dist);
4163 	AF(dist) = AF(a1);
4164 
4165 	/* v4 can be done a bit simpler */
4166 	if (IS_IPV4(a1)) {
4167 		a1val = SRCADR(a1);
4168 		a2val = SRCADR(a2);
4169 		v4dist = (a1val > a2val)
4170 			     ? a1val - a2val
4171 			     : a2val - a1val;
4172 		SET_ADDR4(dist, v4dist);
4173 
4174 		return;
4175 	}
4176 
4177 	found_greater = FALSE;
4178 	a1_greater = FALSE;	/* suppress pot. uninit. warning */
4179 	for (i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
4180 		if (!found_greater &&
4181 		    NSRCADR6(a1)[i] != NSRCADR6(a2)[i]) {
4182 			found_greater = TRUE;
4183 			a1_greater = (NSRCADR6(a1)[i] > NSRCADR6(a2)[i]);
4184 		}
4185 		if (!found_greater) {
4186 			NSRCADR6(dist)[i] = 0;
4187 		} else {
4188 			if (a1_greater)
4189 				NSRCADR6(dist)[i] = NSRCADR6(a1)[i] -
4190 						    NSRCADR6(a2)[i];
4191 			else
4192 				NSRCADR6(dist)[i] = NSRCADR6(a2)[i] -
4193 						    NSRCADR6(a1)[i];
4194 		}
4195 	}
4196 }
4197 
4198 
4199 /*
4200  * cmp_addr_distance - compare two address distances, returning -1, 0,
4201  *		       1 to indicate their relationship.
4202  */
4203 static int
4204 cmp_addr_distance(
4205 	const sockaddr_u *	d1,
4206 	const sockaddr_u *	d2
4207 	)
4208 {
4209 	int	i;
4210 
4211 	REQUIRE(AF(d1) == AF(d2));
4212 
4213 	if (IS_IPV4(d1)) {
4214 		if (SRCADR(d1) < SRCADR(d2))
4215 			return -1;
4216 		else if (SRCADR(d1) == SRCADR(d2))
4217 			return 0;
4218 		else
4219 			return 1;
4220 	}
4221 
4222 	for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
4223 		if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i])
4224 			return -1;
4225 		else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i])
4226 			return 1;
4227 	}
4228 
4229 	return 0;
4230 }
4231 
4232 
4233 
4234 /*
4235  * fetch an interface structure the matches the
4236  * address and has the given flags NOT set
4237  */
4238 endpt *
4239 getinterface(
4240 	sockaddr_u *	addr,
4241 	u_int32		flags
4242 	)
4243 {
4244 	endpt *iface;
4245 
4246 	iface = find_addr_in_list(addr);
4247 
4248 	if (iface != NULL && (iface->flags & flags))
4249 		iface = NULL;
4250 
4251 	return iface;
4252 }
4253 
4254 
4255 /*
4256  * findbcastinter - find broadcast interface corresponding to address
4257  */
4258 endpt *
4259 findbcastinter(
4260 	sockaddr_u *addr
4261 	)
4262 {
4263 	endpt *	iface;
4264 
4265 	iface = NULL;
4266 #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
4267 	DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
4268 		    stoa(addr)));
4269 
4270 	iface = findlocalinterface(addr, INT_LOOPBACK | INT_WILDCARD,
4271 				   1);
4272 	if (iface != NULL) {
4273 		DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n",
4274 			    iface->ifnum, iface->name));
4275 		return iface;
4276 	}
4277 
4278 	/*
4279 	 * plan B - try to find something reasonable in our lists in
4280 	 * case kernel lookup doesn't help
4281 	 */
4282 	for (iface = ep_list; iface != NULL; iface = iface->elink) {
4283 		if (iface->flags & INT_WILDCARD)
4284 			continue;
4285 
4286 		/* Don't bother with ignored interfaces */
4287 		if (iface->ignore_packets)
4288 			continue;
4289 
4290 		/*
4291 		 * First look if this is the correct family
4292 		 */
4293 		if(AF(&iface->sin) != AF(addr))
4294 			continue;
4295 
4296 		/* Skip the loopback addresses */
4297 		if (iface->flags & INT_LOOPBACK)
4298 			continue;
4299 
4300 		/*
4301 		 * If we are looking to match a multicast address and
4302 		 * this interface is one...
4303 		 */
4304 		if (addr_ismulticast(addr)
4305 		    && (iface->flags & INT_MULTICAST)) {
4306 #ifdef INCLUDE_IPV6_SUPPORT
4307 			/*
4308 			 * ...it is the winner unless we're looking for
4309 			 * an interface to use for link-local multicast
4310 			 * and its address is not link-local.
4311 			 */
4312 			if (IS_IPV6(addr)
4313 			    && IN6_IS_ADDR_MC_LINKLOCAL(PSOCK_ADDR6(addr))
4314 			    && !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin)))
4315 				continue;
4316 #endif
4317 			break;
4318 		}
4319 
4320 		/*
4321 		 * We match only those interfaces marked as
4322 		 * broadcastable and either the explicit broadcast
4323 		 * address or the network portion of the IP address.
4324 		 * Sloppy.
4325 		 */
4326 		if (IS_IPV4(addr)) {
4327 			if (SOCK_EQ(&iface->bcast, addr))
4328 				break;
4329 
4330 			if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask))
4331 			    == (NSRCADR(addr)	  & NSRCADR(&iface->mask)))
4332 				break;
4333 		}
4334 #ifdef INCLUDE_IPV6_SUPPORT
4335 		else if (IS_IPV6(addr)) {
4336 			if (SOCK_EQ(&iface->bcast, addr))
4337 				break;
4338 
4339 			if (SOCK_EQ(netof(&iface->sin), netof(addr)))
4340 				break;
4341 		}
4342 #endif
4343 	}
4344 #endif /* SIOCGIFCONF */
4345 	if (NULL == iface) {
4346 		DPRINTF(4, ("No bcast interface found for %s\n",
4347 			    stoa(addr)));
4348 		iface = ANY_INTERFACE_CHOOSE(addr);
4349 	} else {
4350 		DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n",
4351 			    iface->ifnum, iface->name));
4352 	}
4353 
4354 	return iface;
4355 }
4356 
4357 
4358 /*
4359  * io_clr_stats - clear I/O module statistics
4360  */
4361 void
4362 io_clr_stats(void)
4363 {
4364 	packets_dropped = 0;
4365 	packets_ignored = 0;
4366 	packets_received = 0;
4367 	packets_sent = 0;
4368 	packets_notsent = 0;
4369 
4370 	handler_calls = 0;
4371 	handler_pkts = 0;
4372 	io_timereset = current_time;
4373 }
4374 
4375 
4376 #ifdef REFCLOCK
4377 /*
4378  * io_addclock - add a reference clock to the list and arrange that we
4379  *				 get SIGIO interrupts from it.
4380  */
4381 int
4382 io_addclock(
4383 	struct refclockio *rio
4384 	)
4385 {
4386 	BLOCKIO();
4387 
4388 	/*
4389 	 * Stuff the I/O structure in the list and mark the descriptor
4390 	 * in use.  There is a harmless (I hope) race condition here.
4391 	 */
4392 	rio->active = TRUE;
4393 
4394 # ifdef HAVE_SIGNALED_IO
4395 	if (init_clock_sig(rio)) {
4396 		UNBLOCKIO();
4397 		return 0;
4398 	}
4399 # elif defined(HAVE_IO_COMPLETION_PORT)
4400 	if (!io_completion_port_add_clock_io(rio)) {
4401 		UNBLOCKIO();
4402 		return 0;
4403 	}
4404 # endif
4405 
4406 	/*
4407 	 * enqueue
4408 	 */
4409 	LINK_SLIST(refio, rio, next);
4410 
4411 	/*
4412 	 * register fd
4413 	 */
4414 	add_fd_to_list(rio->fd, FD_TYPE_FILE);
4415 
4416 	UNBLOCKIO();
4417 	return 1;
4418 }
4419 
4420 
4421 /*
4422  * io_closeclock - close the clock in the I/O structure given
4423  */
4424 void
4425 io_closeclock(
4426 	struct refclockio *rio
4427 	)
4428 {
4429 	struct refclockio *unlinked;
4430 
4431 	BLOCKIO();
4432 
4433 	/*
4434 	 * Remove structure from the list
4435 	 */
4436 	rio->active = FALSE;
4437 	UNLINK_SLIST(unlinked, refio, rio, next, struct refclockio);
4438 	if (NULL != unlinked) {
4439 		/* Close the descriptor. The order of operations is
4440 		 * important here in case of async / overlapped IO:
4441 		 * only after we have removed the clock from the
4442 		 * IO completion port we can be sure no further
4443 		 * input is queued. So...
4444 		 *  - we first disable feeding to the queu by removing
4445 		 *    the clock from the IO engine
4446 		 *  - close the file (which brings down any IO on it)
4447 		 *  - clear the buffer from results for this fd
4448 		 */
4449 #	    ifdef HAVE_IO_COMPLETION_PORT
4450 		io_completion_port_remove_clock_io(rio);
4451 #	    endif
4452 		close_and_delete_fd_from_list(rio->fd);
4453 		purge_recv_buffers_for_fd(rio->fd);
4454 		rio->fd = -1;
4455 	}
4456 
4457 	UNBLOCKIO();
4458 }
4459 #endif	/* REFCLOCK */
4460 
4461 
4462 /*
4463  * On NT a SOCKET is an unsigned int so we cannot possibly keep it in
4464  * an array. So we use one of the ISC_LIST functions to hold the
4465  * socket value and use that when we want to enumerate it.
4466  *
4467  * This routine is called by the forked intres child process to close
4468  * all open sockets.  On Windows there's no need as intres runs in
4469  * the same process as a thread.
4470  */
4471 #ifndef SYS_WINNT
4472 void
4473 kill_asyncio(
4474 	int	startfd
4475 	)
4476 {
4477 	BLOCKIO();
4478 
4479 	/*
4480 	 * In the child process we do not maintain activefds and
4481 	 * maxactivefd.  Zeroing maxactivefd disables code which
4482 	 * maintains it in close_and_delete_fd_from_list().
4483 	 */
4484 	maxactivefd = 0;
4485 
4486 	while (fd_list != NULL)
4487 		close_and_delete_fd_from_list(fd_list->fd);
4488 
4489 	UNBLOCKIO();
4490 }
4491 #endif	/* !SYS_WINNT */
4492 
4493 
4494 /*
4495  * Add and delete functions for the list of open sockets
4496  */
4497 static void
4498 add_fd_to_list(
4499 	SOCKET fd,
4500 	enum desc_type type
4501 	)
4502 {
4503 	vsock_t *lsock = emalloc(sizeof(*lsock));
4504 
4505 	lsock->fd = fd;
4506 	lsock->type = type;
4507 
4508 	LINK_SLIST(fd_list, lsock, link);
4509 	maintain_activefds(fd, 0);
4510 }
4511 
4512 
4513 static void
4514 close_and_delete_fd_from_list(
4515 	SOCKET fd
4516 	)
4517 {
4518 	vsock_t *lsock;
4519 
4520 	UNLINK_EXPR_SLIST(lsock, fd_list, fd ==
4521 	    UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t);
4522 
4523 	if (NULL == lsock)
4524 		return;
4525 
4526 	switch (lsock->type) {
4527 
4528 	case FD_TYPE_SOCKET:
4529 		closesocket(lsock->fd);
4530 		break;
4531 
4532 	case FD_TYPE_FILE:
4533 		closeserial((int)lsock->fd);
4534 		break;
4535 
4536 	default:
4537 		msyslog(LOG_ERR,
4538 			"internal error - illegal descriptor type %d - EXITING",
4539 			(int)lsock->type);
4540 		exit(1);
4541 	}
4542 
4543 	free(lsock);
4544 	/*
4545 	 * remove from activefds
4546 	 */
4547 	maintain_activefds(fd, 1);
4548 }
4549 
4550 
4551 static void
4552 add_addr_to_list(
4553 	sockaddr_u *	addr,
4554 	endpt *		ep
4555 	)
4556 {
4557 	remaddr_t *laddr;
4558 
4559 #ifdef DEBUG
4560 	if (find_addr_in_list(addr) == NULL) {
4561 #endif
4562 		/* not there yet - add to list */
4563 		laddr = emalloc(sizeof(*laddr));
4564 		laddr->addr = *addr;
4565 		laddr->ep = ep;
4566 
4567 		LINK_SLIST(remoteaddr_list, laddr, link);
4568 
4569 		DPRINTF(4, ("Added addr %s to list of addresses\n",
4570 			    stoa(addr)));
4571 #ifdef DEBUG
4572 	} else
4573 		DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
4574 			    stoa(addr)));
4575 #endif
4576 }
4577 
4578 
4579 static void
4580 delete_addr_from_list(
4581 	sockaddr_u *addr
4582 	)
4583 {
4584 	remaddr_t *unlinked;
4585 
4586 	UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr,
4587 		&(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t);
4588 
4589 	if (unlinked != NULL) {
4590 		DPRINTF(4, ("Deleted addr %s from list of addresses\n",
4591 			stoa(addr)));
4592 		free(unlinked);
4593 	}
4594 }
4595 
4596 
4597 static void
4598 delete_interface_from_list(
4599 	endpt *iface
4600 	)
4601 {
4602 	remaddr_t *unlinked;
4603 
4604 	for (;;) {
4605 		UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface ==
4606 		    UNLINK_EXPR_SLIST_CURRENT()->ep, link,
4607 		    remaddr_t);
4608 
4609 		if (unlinked == NULL)
4610 			break;
4611 		DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
4612 			    stoa(&unlinked->addr), iface->ifnum,
4613 			    iface->name));
4614 		free(unlinked);
4615 	}
4616 }
4617 
4618 
4619 static struct interface *
4620 find_addr_in_list(
4621 	sockaddr_u *addr
4622 	)
4623 {
4624 	remaddr_t *entry;
4625 
4626 	DPRINTF(4, ("Searching for addr %s in list of addresses - ",
4627 		    stoa(addr)));
4628 
4629 	for (entry = remoteaddr_list;
4630 	     entry != NULL;
4631 	     entry = entry->link)
4632 		if (SOCK_EQ(&entry->addr, addr)) {
4633 			DPRINTF(4, ("FOUND\n"));
4634 			return entry->ep;
4635 		}
4636 
4637 	DPRINTF(4, ("NOT FOUND\n"));
4638 	return NULL;
4639 }
4640 
4641 
4642 /*
4643  * Find the given address with the all given flags set in the list
4644  */
4645 static endpt *
4646 find_flagged_addr_in_list(
4647 	sockaddr_u *	addr,
4648 	u_int32		flags
4649 	)
4650 {
4651 	remaddr_t *entry;
4652 
4653 	DPRINTF(4, ("Finding addr %s with flags %d in list: ",
4654 		    stoa(addr), flags));
4655 
4656 	for (entry = remoteaddr_list;
4657 	     entry != NULL;
4658 	     entry = entry->link)
4659 
4660 		if (SOCK_EQ(&entry->addr, addr)
4661 		    && (entry->ep->flags & flags) == flags) {
4662 
4663 			DPRINTF(4, ("FOUND\n"));
4664 			return entry->ep;
4665 		}
4666 
4667 	DPRINTF(4, ("NOT FOUND\n"));
4668 	return NULL;
4669 }
4670 
4671 
4672 const char *
4673 localaddrtoa(
4674 	endpt *la
4675 	)
4676 {
4677 	return (NULL == la)
4678 		   ? "<null>"
4679 		   : stoa(&la->sin);
4680 }
4681 
4682 
4683 #ifdef HAS_ROUTING_SOCKET
4684 # ifndef UPDATE_GRACE
4685 #  define UPDATE_GRACE	2	/* wait UPDATE_GRACE seconds before scanning */
4686 # endif
4687 
4688 static void
4689 process_routing_msgs(struct asyncio_reader *reader)
4690 {
4691 	char buffer[5120];
4692 	int cnt, msg_type;
4693 #ifdef HAVE_RTNETLINK
4694 	struct nlmsghdr *nh;
4695 #else
4696 	struct rt_msghdr rtm;
4697 	char *p;
4698 #endif
4699 
4700 	if (disable_dynamic_updates) {
4701 		/*
4702 		 * discard ourselves if we are not needed any more
4703 		 * usually happens when running unprivileged
4704 		 */
4705 		remove_asyncio_reader(reader);
4706 		delete_asyncio_reader(reader);
4707 		return;
4708 	}
4709 
4710 	cnt = read(reader->fd, buffer, sizeof(buffer));
4711 
4712 	if (cnt < 0) {
4713 		if (errno == ENOBUFS) {
4714 			msyslog(LOG_ERR,
4715 				"routing socket reports: %m");
4716 		} else {
4717 			msyslog(LOG_ERR,
4718 				"routing socket reports: %m - disabling");
4719 			remove_asyncio_reader(reader);
4720 			delete_asyncio_reader(reader);
4721 		}
4722 		return;
4723 	}
4724 
4725 	/*
4726 	 * process routing message
4727 	 */
4728 #ifdef HAVE_RTNETLINK
4729 	for (nh = UA_PTR(struct nlmsghdr, buffer);
4730 	     NLMSG_OK(nh, cnt);
4731 	     nh = NLMSG_NEXT(nh, cnt)) {
4732 		msg_type = nh->nlmsg_type;
4733 #else
4734 	for (p = buffer;
4735 	     (p + sizeof(struct rt_msghdr)) <= (buffer + cnt);
4736 	     p += rtm.rtm_msglen) {
4737 		memcpy(&rtm, p, sizeof(rtm));
4738 		if (rtm.rtm_version != RTM_VERSION) {
4739 			msyslog(LOG_ERR,
4740 				"version mismatch (got %d - expected %d) on routing socket - disabling",
4741 				rtm.rtm_version, RTM_VERSION);
4742 
4743 			remove_asyncio_reader(reader);
4744 			delete_asyncio_reader(reader);
4745 			return;
4746 		}
4747 		msg_type = rtm.rtm_type;
4748 #endif
4749 		switch (msg_type) {
4750 #ifdef RTM_NEWADDR
4751 		case RTM_NEWADDR:
4752 #endif
4753 #ifdef RTM_DELADDR
4754 		case RTM_DELADDR:
4755 #endif
4756 #ifdef RTM_ADD
4757 		case RTM_ADD:
4758 #endif
4759 #ifdef RTM_DELETE
4760 		case RTM_DELETE:
4761 #endif
4762 #ifdef RTM_REDIRECT
4763 		case RTM_REDIRECT:
4764 #endif
4765 #ifdef RTM_CHANGE
4766 		case RTM_CHANGE:
4767 #endif
4768 #ifdef RTM_LOSING
4769 		case RTM_LOSING:
4770 #endif
4771 #ifdef RTM_IFINFO
4772 		case RTM_IFINFO:
4773 #endif
4774 #ifdef RTM_IFANNOUNCE
4775 		case RTM_IFANNOUNCE:
4776 #endif
4777 #ifdef RTM_NEWLINK
4778 		case RTM_NEWLINK:
4779 #endif
4780 #ifdef RTM_DELLINK
4781 		case RTM_DELLINK:
4782 #endif
4783 #ifdef RTM_NEWROUTE
4784 		case RTM_NEWROUTE:
4785 #endif
4786 #ifdef RTM_DELROUTE
4787 		case RTM_DELROUTE:
4788 #endif
4789 			/*
4790 			 * we are keen on new and deleted addresses and
4791 			 * if an interface goes up and down or routing
4792 			 * changes
4793 			 */
4794 			DPRINTF(3, ("routing message op = %d: scheduling interface update\n",
4795 				    msg_type));
4796 			timer_interfacetimeout(current_time + UPDATE_GRACE);
4797 			break;
4798 #ifdef HAVE_RTNETLINK
4799 		case NLMSG_DONE:
4800 			/* end of multipart message */
4801 			return;
4802 #endif
4803 		default:
4804 			/*
4805 			 * the rest doesn't bother us.
4806 			 */
4807 			DPRINTF(4, ("routing message op = %d: ignored\n",
4808 				    msg_type));
4809 			break;
4810 		}
4811 	}
4812 }
4813 
4814 /*
4815  * set up routing notifications
4816  */
4817 static void
4818 init_async_notifications()
4819 {
4820 	struct asyncio_reader *reader;
4821 #ifdef HAVE_RTNETLINK
4822 	int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
4823 	struct sockaddr_nl sa;
4824 #else
4825 	int fd = socket(PF_ROUTE, SOCK_RAW, 0);
4826 #endif
4827 #ifdef RO_MSGFILTER
4828 	unsigned char msgfilter[] = {
4829 #ifdef RTM_NEWADDR
4830 		RTM_NEWADDR,
4831 #endif
4832 #ifdef RTM_DELADDR
4833 		RTM_DELADDR,
4834 #endif
4835 #ifdef RTM_ADD
4836 		RTM_ADD,
4837 #endif
4838 #ifdef RTM_DELETE
4839 		RTM_DELETE,
4840 #endif
4841 #ifdef RTM_REDIRECT
4842 		RTM_REDIRECT,
4843 #endif
4844 #ifdef RTM_CHANGE
4845 		RTM_CHANGE,
4846 #endif
4847 #ifdef RTM_LOSING
4848 		RTM_LOSING,
4849 #endif
4850 #ifdef RTM_IFINFO
4851 		RTM_IFINFO,
4852 #endif
4853 #ifdef RTM_IFANNOUNCE
4854 		RTM_IFANNOUNCE,
4855 #endif
4856 #ifdef RTM_NEWLINK
4857 		RTM_NEWLINK,
4858 #endif
4859 #ifdef RTM_DELLINK
4860 		RTM_DELLINK,
4861 #endif
4862 #ifdef RTM_NEWROUTE
4863 		RTM_NEWROUTE,
4864 #endif
4865 #ifdef RTM_DELROUTE
4866 		RTM_DELROUTE,
4867 #endif
4868 	};
4869 #endif /* !RO_MSGFILTER */
4870 
4871 	if (fd < 0) {
4872 		msyslog(LOG_ERR,
4873 			"unable to open routing socket (%m) - using polled interface update");
4874 		return;
4875 	}
4876 
4877 	fd = move_fd(fd);
4878 #ifdef HAVE_RTNETLINK
4879 	ZERO(sa);
4880 	sa.nl_family = PF_NETLINK;
4881 	sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR
4882 		       | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE
4883 		       | RTMGRP_IPV4_MROUTE | RTMGRP_IPV6_ROUTE
4884 		       | RTMGRP_IPV6_MROUTE;
4885 	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
4886 		msyslog(LOG_ERR,
4887 			"bind failed on routing socket (%m) - using polled interface update");
4888 		return;
4889 	}
4890 #endif
4891 #ifdef RO_MSGFILTER
4892 	if (setsockopt(fd, PF_ROUTE, RO_MSGFILTER,
4893 	    &msgfilter, sizeof(msgfilter)) == -1)
4894 		msyslog(LOG_ERR, "RO_MSGFILTER: %m");
4895 #endif
4896 	make_socket_nonblocking(fd);
4897 #if defined(HAVE_SIGNALED_IO)
4898 	init_socket_sig(fd);
4899 #endif /* HAVE_SIGNALED_IO */
4900 
4901 	reader = new_asyncio_reader();
4902 
4903 	reader->fd = fd;
4904 	reader->receiver = process_routing_msgs;
4905 
4906 	add_asyncio_reader(reader, FD_TYPE_SOCKET);
4907 	msyslog(LOG_INFO,
4908 		"Listening on routing socket on fd #%d for interface updates",
4909 		fd);
4910 }
4911 #else
4912 /* HAS_ROUTING_SOCKET not defined */
4913 static void
4914 init_async_notifications(void)
4915 {
4916 }
4917 #endif
4918 
4919