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