ntp_io.c revision 316722
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		      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			       (char *)&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		      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			    (char *)&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				       (char *)&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			       (char *)&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, (char *)&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			       SETSOCKOPT_ARG_CAST &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			       (char *) &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			       (char *)&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, (char *)&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, (char *)&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, (char *)&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
2734		strlcpy(ep->name, "multicast", sizeof(ep->name));
2735		DPRINT_INTERFACE(2, (ep, "multicast add ", "\n"));
2736		add_interface(ep);
2737		log_listen_address(ep);
2738	} else {
2739		/* bind failed, re-use wildcard interface */
2740		delete_interface(ep);
2741
2742		if (IS_IPV4(addr))
2743			ep = wildipv4;
2744		else if (IS_IPV6(addr))
2745			ep = wildipv6;
2746		else
2747			ep = NULL;
2748
2749		if (ep != NULL) {
2750			/* HACK ! -- stuff in an address */
2751			/* because we don't bind addr? DH */
2752			ep->bcast = *addr;
2753			msyslog(LOG_ERR,
2754				"multicast address %s using wildcard interface #%d %s",
2755				stoa(addr), ep->ifnum, ep->name);
2756		} else {
2757			msyslog(LOG_ERR,
2758				"No multicast socket available to use for address %s",
2759				stoa(addr));
2760			return;
2761		}
2762	}
2763	{	/* in place of the { following for in #else clause */
2764		one_ep = ep;
2765# else	/* MULTICAST_NONEWSOCKET follows */
2766	/*
2767	 * For the case where we can't use a separate socket (Windows)
2768	 * join each applicable endpoint socket to the group address.
2769	 */
2770	if (IS_IPV4(addr))
2771		one_ep = wildipv4;
2772	else
2773		one_ep = wildipv6;
2774	for (ep = ep_list; ep != NULL; ep = ep->elink) {
2775		if (ep->ignore_packets || AF(&ep->sin) != AF(addr) ||
2776		    !(INT_MULTICAST & ep->flags) ||
2777		    (INT_LOOPBACK | INT_WILDCARD) & ep->flags)
2778			continue;
2779		one_ep = ep;
2780# endif	/* MULTICAST_NONEWSOCKET */
2781		if (socket_multicast_enable(ep, addr))
2782			msyslog(LOG_INFO,
2783				"Joined %s socket to multicast group %s",
2784				stoa(&ep->sin),
2785				stoa(addr));
2786	}
2787
2788	add_addr_to_list(addr, one_ep);
2789#else	/* !MCAST  follows*/
2790	msyslog(LOG_ERR,
2791		"Can not add multicast address %s: no multicast support",
2792		stoa(addr));
2793#endif
2794	return;
2795}
2796
2797
2798/*
2799 * io_multicast_del() - delete multicast group address
2800 */
2801void
2802io_multicast_del(
2803	sockaddr_u *	addr
2804	)
2805{
2806#ifdef MCAST
2807	endpt *iface;
2808
2809	/*
2810	 * Check to see if this is a multicast address
2811	 */
2812	if (!addr_ismulticast(addr)) {
2813		msyslog(LOG_ERR, "invalid multicast address %s",
2814			stoa(addr));
2815		return;
2816	}
2817
2818	/*
2819	 * Disable reception of multicast packets
2820	 */
2821	while ((iface = find_flagged_addr_in_list(addr, INT_MCASTOPEN))
2822	       != NULL)
2823		socket_multicast_disable(iface, addr);
2824
2825	delete_addr_from_list(addr);
2826
2827#else /* not MCAST */
2828	msyslog(LOG_ERR,
2829		"Can not delete multicast address %s: no multicast support",
2830		stoa(addr));
2831#endif /* not MCAST */
2832}
2833
2834
2835/*
2836 * open_socket - open a socket, returning the file descriptor
2837 */
2838
2839static SOCKET
2840open_socket(
2841	sockaddr_u *	addr,
2842	int		bcast,
2843	int		turn_off_reuse,
2844	endpt *		interf
2845	)
2846{
2847	SOCKET	fd;
2848	int	errval;
2849	/*
2850	 * int is OK for REUSEADR per
2851	 * http://www.kohala.com/start/mcast.api.txt
2852	 */
2853	int	on = 1;
2854	int	off = 0;
2855
2856	if (IS_IPV6(addr) && !ipv6_works)
2857		return INVALID_SOCKET;
2858
2859	/* create a datagram (UDP) socket */
2860	fd = socket(AF(addr), SOCK_DGRAM, 0);
2861	if (INVALID_SOCKET == fd) {
2862		errval = socket_errno();
2863		msyslog(LOG_ERR,
2864			"socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
2865			IS_IPV6(addr) ? "6" : "", stoa(addr));
2866
2867		if (errval == EPROTONOSUPPORT ||
2868		    errval == EAFNOSUPPORT ||
2869		    errval == EPFNOSUPPORT)
2870			return (INVALID_SOCKET);
2871
2872		errno = errval;
2873		msyslog(LOG_ERR,
2874			"unexpected socket() error %m code %d (not EPROTONOSUPPORT nor EAFNOSUPPORT nor EPFNOSUPPORT) - exiting",
2875			errno);
2876		exit(1);
2877	}
2878
2879#ifdef SYS_WINNT
2880	connection_reset_fix(fd, addr);
2881#endif
2882	/*
2883	 * Fixup the file descriptor for some systems
2884	 * See bug #530 for details of the issue.
2885	 */
2886	fd = move_fd(fd);
2887
2888	/*
2889	 * set SO_REUSEADDR since we will be binding the same port
2890	 * number on each interface according to turn_off_reuse.
2891	 * This is undesirable on Windows versions starting with
2892	 * Windows XP (numeric version 5.1).
2893	 */
2894#ifdef SYS_WINNT
2895	if (isc_win32os_versioncheck(5, 1, 0, 0) < 0)  /* before 5.1 */
2896#endif
2897		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2898			       (char *)((turn_off_reuse)
2899					    ? &off
2900					    : &on),
2901			       sizeof(on))) {
2902
2903			msyslog(LOG_ERR,
2904				"setsockopt SO_REUSEADDR %s fails for address %s: %m",
2905				(turn_off_reuse)
2906				    ? "off"
2907				    : "on",
2908				stoa(addr));
2909			closesocket(fd);
2910			return INVALID_SOCKET;
2911		}
2912#ifdef SO_EXCLUSIVEADDRUSE
2913	/*
2914	 * setting SO_EXCLUSIVEADDRUSE on the wildcard we open
2915	 * first will cause more specific binds to fail.
2916	 */
2917	if (!(interf->flags & INT_WILDCARD))
2918		set_excladdruse(fd);
2919#endif
2920
2921	/*
2922	 * IPv4 specific options go here
2923	 */
2924	if (IS_IPV4(addr)) {
2925#if defined(IPPROTO_IP) && defined(IP_TOS)
2926		if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char*)&qos,
2927			       sizeof(qos)))
2928			msyslog(LOG_ERR,
2929				"setsockopt IP_TOS (%02x) fails on address %s: %m",
2930				qos, stoa(addr));
2931#endif /* IPPROTO_IP && IP_TOS */
2932		if (bcast)
2933			socket_broadcast_enable(interf, fd, addr);
2934	}
2935
2936	/*
2937	 * IPv6 specific options go here
2938	 */
2939	if (IS_IPV6(addr)) {
2940#if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
2941		if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, (char*)&qos,
2942			       sizeof(qos)))
2943			msyslog(LOG_ERR,
2944				"setsockopt IPV6_TCLASS (%02x) fails on address %s: %m",
2945				qos, stoa(addr));
2946#endif /* IPPROTO_IPV6 && IPV6_TCLASS */
2947#ifdef IPV6_V6ONLY
2948		if (isc_net_probe_ipv6only() == ISC_R_SUCCESS
2949		    && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
2950		    (char*)&on, sizeof(on)))
2951			msyslog(LOG_ERR,
2952				"setsockopt IPV6_V6ONLY on fails on address %s: %m",
2953				stoa(addr));
2954#endif
2955#ifdef IPV6_BINDV6ONLY
2956		if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
2957		    (char*)&on, sizeof(on)))
2958			msyslog(LOG_ERR,
2959				"setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
2960				stoa(addr));
2961#endif
2962	}
2963
2964#ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2965	/*
2966	 * some OSes don't allow binding to more specific
2967	 * addresses if a wildcard address already bound
2968	 * to the port and SO_REUSEADDR is not set
2969	 */
2970	if (!is_wildcard_addr(addr))
2971		set_wildcard_reuse(AF(addr), 1);
2972#endif
2973
2974	/*
2975	 * bind the local address.
2976	 */
2977	errval = bind(fd, &addr->sa, SOCKLEN(addr));
2978
2979#ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2980	if (!is_wildcard_addr(addr))
2981		set_wildcard_reuse(AF(addr), 0);
2982#endif
2983
2984	if (errval < 0) {
2985		/*
2986		 * Don't log this under all conditions
2987		 */
2988		if (turn_off_reuse == 0
2989#ifdef DEBUG
2990		    || debug > 1
2991#endif
2992		    ) {
2993			msyslog(LOG_ERR,
2994				"bind(%d) AF_INET%s %s#%d%s flags 0x%x failed: %m",
2995				fd, IS_IPV6(addr) ? "6" : "",
2996				stoa(addr), SRCPORT(addr),
2997				IS_MCAST(addr) ? " (multicast)" : "",
2998				interf->flags);
2999		}
3000
3001		closesocket(fd);
3002
3003		return INVALID_SOCKET;
3004	}
3005
3006#ifdef HAVE_TIMESTAMP
3007	{
3008		if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
3009			       (char*)&on, sizeof(on)))
3010			msyslog(LOG_DEBUG,
3011				"setsockopt SO_TIMESTAMP on fails on address %s: %m",
3012				stoa(addr));
3013		else
3014			DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n",
3015				    fd, stoa(addr)));
3016	}
3017#endif
3018#ifdef HAVE_TIMESTAMPNS
3019	{
3020		if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
3021			       (char*)&on, sizeof(on)))
3022			msyslog(LOG_DEBUG,
3023				"setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
3024				stoa(addr));
3025		else
3026			DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
3027				    fd, stoa(addr)));
3028	}
3029#endif
3030#ifdef HAVE_BINTIME
3031	{
3032		if (setsockopt(fd, SOL_SOCKET, SO_BINTIME,
3033			       (char*)&on, sizeof(on)))
3034			msyslog(LOG_DEBUG,
3035				"setsockopt SO_BINTIME on fails on address %s: %m",
3036				stoa(addr));
3037		else
3038			DPRINTF(4, ("setsockopt SO_BINTIME enabled on fd %d address %s\n",
3039				    fd, stoa(addr)));
3040	}
3041#endif
3042
3043	DPRINTF(4, ("bind(%d) AF_INET%s, addr %s%%%d#%d, flags 0x%x\n",
3044		   fd, IS_IPV6(addr) ? "6" : "", stoa(addr),
3045		   SCOPE(addr), SRCPORT(addr), interf->flags));
3046
3047	make_socket_nonblocking(fd);
3048
3049#ifdef HAVE_SIGNALED_IO
3050	init_socket_sig(fd);
3051#endif /* not HAVE_SIGNALED_IO */
3052
3053	add_fd_to_list(fd, FD_TYPE_SOCKET);
3054
3055#if !defined(SYS_WINNT) && !defined(VMS)
3056	DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
3057		    fcntl(fd, F_GETFL, 0)));
3058#endif /* SYS_WINNT || VMS */
3059
3060#if defined(HAVE_IO_COMPLETION_PORT)
3061/*
3062 * Add the socket to the completion port
3063 */
3064	if (!io_completion_port_add_socket(fd, interf, bcast)) {
3065		msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
3066		exit(1);
3067	}
3068#endif
3069	return fd;
3070}
3071
3072
3073
3074/* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
3075/*
3076 * sendpkt - send a packet to the specified destination. Maintain a
3077 * send error cache so that only the first consecutive error for a
3078 * destination is logged.
3079 */
3080void
3081sendpkt(
3082	sockaddr_u *		dest,
3083	struct interface *	ep,
3084	int			ttl,
3085	struct pkt *		pkt,
3086	int			len
3087	)
3088{
3089	endpt *	src;
3090	int	ismcast;
3091	int	cc;
3092	int	rc;
3093	u_char	cttl;
3094
3095	ismcast = IS_MCAST(dest);
3096	if (!ismcast)
3097		src = ep;
3098	else
3099		src = (IS_IPV4(dest))
3100			  ? mc4_list
3101			  : mc6_list;
3102
3103	if (NULL == src) {
3104		/*
3105		 * unbound peer - drop request and wait for better
3106		 * network conditions
3107		 */
3108		DPRINTF(2, ("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
3109			    ismcast ? "\tMCAST\t***** " : "",
3110			    stoa(dest), ttl, len));
3111		return;
3112	}
3113
3114	do {
3115		DPRINTF(2, ("%ssendpkt(%d, dst=%s, src=%s, ttl=%d, len=%d)\n",
3116			    ismcast ? "\tMCAST\t***** " : "", src->fd,
3117			    stoa(dest), stoa(&src->sin), ttl, len));
3118#ifdef MCAST
3119		/*
3120		 * for the moment we use the bcast option to set multicast ttl
3121		 */
3122		if (ismcast && ttl > 0 && ttl != src->last_ttl) {
3123			/*
3124			 * set the multicast ttl for outgoing packets
3125			 */
3126			switch (AF(&src->sin)) {
3127
3128			case AF_INET :
3129				cttl = (u_char)ttl;
3130				rc = setsockopt(src->fd, IPPROTO_IP,
3131						IP_MULTICAST_TTL,
3132						(void *)&cttl,
3133						sizeof(cttl));
3134				break;
3135
3136# ifdef INCLUDE_IPV6_SUPPORT
3137			case AF_INET6 :
3138				rc = setsockopt(src->fd, IPPROTO_IPV6,
3139						 IPV6_MULTICAST_HOPS,
3140						 (void *)&ttl,
3141						 sizeof(ttl));
3142				break;
3143# endif	/* INCLUDE_IPV6_SUPPORT */
3144
3145			default:
3146				rc = 0;
3147			}
3148
3149			if (!rc)
3150				src->last_ttl = ttl;
3151			else
3152				msyslog(LOG_ERR,
3153					"setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
3154					stoa(&src->sin));
3155		}
3156#endif	/* MCAST */
3157
3158#ifdef SIM
3159		cc = simulate_server(dest, src, pkt);
3160#elif defined(HAVE_IO_COMPLETION_PORT)
3161		cc = io_completion_port_sendto(src, src->fd, pkt,
3162			(size_t)len, (sockaddr_u *)&dest->sa);
3163#else
3164		cc = sendto(src->fd, (char *)pkt, (u_int)len, 0,
3165			    &dest->sa, SOCKLEN(dest));
3166#endif
3167		if (cc == -1) {
3168			src->notsent++;
3169			packets_notsent++;
3170		} else	{
3171			src->sent++;
3172			packets_sent++;
3173		}
3174		if (ismcast)
3175			src = src->mclink;
3176	} while (ismcast && src != NULL);
3177}
3178
3179
3180#if !defined(HAVE_IO_COMPLETION_PORT)
3181#if !defined(HAVE_SIGNALED_IO)
3182/*
3183 * fdbits - generate ascii representation of fd_set (FAU debug support)
3184 * HFDF format - highest fd first.
3185 */
3186static char *
3187fdbits(
3188	int		count,
3189	const fd_set*	set
3190	)
3191{
3192	static char buffer[256];
3193	char * buf = buffer;
3194
3195	count = min(count,  255);
3196
3197	while (count >= 0) {
3198		*buf++ = FD_ISSET(count, set) ? '#' : '-';
3199		count--;
3200	}
3201	*buf = '\0';
3202
3203	return buffer;
3204}
3205#endif
3206
3207#ifdef REFCLOCK
3208/*
3209 * Routine to read the refclock packets for a specific interface
3210 * Return the number of bytes read. That way we know if we should
3211 * read it again or go on to the next one if no bytes returned
3212 */
3213static inline int
3214read_refclock_packet(
3215	SOCKET			fd,
3216	struct refclockio *	rp,
3217	l_fp			ts
3218	)
3219{
3220	u_int			read_count;
3221	int			buflen;
3222	int			saved_errno;
3223	int			consumed;
3224	struct recvbuf *	rb;
3225
3226	rb = get_free_recv_buffer();
3227
3228	if (NULL == rb) {
3229		/*
3230		 * No buffer space available - just drop the packet
3231		 */
3232		char buf[RX_BUFF_SIZE];
3233
3234		buflen = read(fd, buf, sizeof buf);
3235		packets_dropped++;
3236		return (buflen);
3237	}
3238
3239	/* TALOS-CAN-0064: avoid signed/unsigned clashes that can lead
3240	 * to buffer overrun and memory corruption
3241	 */
3242	if (rp->datalen <= 0 || (size_t)rp->datalen > sizeof(rb->recv_space))
3243		read_count = sizeof(rb->recv_space);
3244	else
3245		read_count = (u_int)rp->datalen;
3246	do {
3247		buflen = read(fd, (char *)&rb->recv_space, read_count);
3248	} while (buflen < 0 && EINTR == errno);
3249
3250	if (buflen <= 0) {
3251		saved_errno = errno;
3252		freerecvbuf(rb);
3253		errno = saved_errno;
3254		return buflen;
3255	}
3256
3257	/*
3258	 * Got one. Mark how and when it got here,
3259	 * put it on the full list and do bookkeeping.
3260	 */
3261	rb->recv_length = buflen;
3262	rb->recv_peer = rp->srcclock;
3263	rb->dstadr = 0;
3264	rb->fd = fd;
3265	rb->recv_time = ts;
3266	rb->receiver = rp->clock_recv;
3267
3268	consumed = indicate_refclock_packet(rp, rb);
3269	if (!consumed) {
3270		rp->recvcount++;
3271		packets_received++;
3272	}
3273
3274	return buflen;
3275}
3276#endif	/* REFCLOCK */
3277
3278
3279#ifdef HAVE_PACKET_TIMESTAMP
3280/*
3281 * extract timestamps from control message buffer
3282 */
3283static l_fp
3284fetch_timestamp(
3285	struct recvbuf *	rb,
3286	struct msghdr *		msghdr,
3287	l_fp			ts
3288	)
3289{
3290	struct cmsghdr *	cmsghdr;
3291	unsigned long		ticks;
3292	double			fuzz;
3293	l_fp			lfpfuzz;
3294	l_fp			nts;
3295#ifdef DEBUG_TIMING
3296	l_fp			dts;
3297#endif
3298
3299	cmsghdr = CMSG_FIRSTHDR(msghdr);
3300	while (cmsghdr != NULL) {
3301		switch (cmsghdr->cmsg_type)
3302		{
3303#ifdef HAVE_BINTIME
3304		case SCM_BINTIME:
3305#endif  /* HAVE_BINTIME */
3306#ifdef HAVE_TIMESTAMPNS
3307		case SCM_TIMESTAMPNS:
3308#endif	/* HAVE_TIMESTAMPNS */
3309#ifdef HAVE_TIMESTAMP
3310		case SCM_TIMESTAMP:
3311#endif	/* HAVE_TIMESTAMP */
3312#if defined(HAVE_BINTIME) || defined (HAVE_TIMESTAMPNS) || defined(HAVE_TIMESTAMP)
3313			switch (cmsghdr->cmsg_type)
3314			{
3315#ifdef HAVE_BINTIME
3316			case SCM_BINTIME:
3317				{
3318					struct bintime	pbt;
3319					memcpy(&pbt, CMSG_DATA(cmsghdr), sizeof(pbt));
3320					/*
3321					 * bintime documentation is at http://phk.freebsd.dk/pubs/timecounter.pdf
3322					 */
3323					nts.l_i = pbt.sec + JAN_1970;
3324					nts.l_uf = (u_int32)(pbt.frac >> 32);
3325					if (sys_tick > measured_tick &&
3326					    sys_tick > 1e-9) {
3327						ticks = (unsigned long)(nts.l_uf / (unsigned long)(sys_tick * FRAC));
3328						nts.l_uf = (unsigned long)(ticks * (unsigned long)(sys_tick * FRAC));
3329					}
3330					DPRINTF(4, ("fetch_timestamp: system bintime network time stamp: %ld.%09lu\n",
3331						    pbt.sec, (unsigned long)((nts.l_uf / FRAC) * 1e9)));
3332				}
3333				break;
3334#endif  /* HAVE_BINTIME */
3335#ifdef HAVE_TIMESTAMPNS
3336			case SCM_TIMESTAMPNS:
3337				{
3338					struct timespec	pts;
3339					memcpy(&pts, CMSG_DATA(cmsghdr), sizeof(pts));
3340					if (sys_tick > measured_tick &&
3341					    sys_tick > 1e-9) {
3342						ticks = (unsigned long)((pts.tv_nsec * 1e-9) /
3343									sys_tick);
3344						pts.tv_nsec = (long)(ticks * 1e9 *
3345								     sys_tick);
3346					}
3347					DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
3348						    pts.tv_sec, pts.tv_nsec));
3349					nts = tspec_stamp_to_lfp(pts);
3350				}
3351				break;
3352#endif	/* HAVE_TIMESTAMPNS */
3353#ifdef HAVE_TIMESTAMP
3354			case SCM_TIMESTAMP:
3355				{
3356					struct timeval	ptv;
3357					memcpy(&ptv, CMSG_DATA(cmsghdr), sizeof(ptv));
3358					if (sys_tick > measured_tick &&
3359					    sys_tick > 1e-6) {
3360						ticks = (unsigned long)((ptv.tv_usec * 1e-6) /
3361									sys_tick);
3362						ptv.tv_usec = (long)(ticks * 1e6 *
3363								    sys_tick);
3364					}
3365					DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %jd.%06ld\n",
3366						    (intmax_t)ptv.tv_sec, (long)ptv.tv_usec));
3367					nts = tval_stamp_to_lfp(ptv);
3368				}
3369				break;
3370#endif  /* HAVE_TIMESTAMP */
3371			}
3372			fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
3373			DTOLFP(fuzz, &lfpfuzz);
3374			L_ADD(&nts, &lfpfuzz);
3375#ifdef DEBUG_TIMING
3376			dts = ts;
3377			L_SUB(&dts, &nts);
3378			collect_timing(rb, "input processing delay", 1,
3379				       &dts);
3380			DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. fuzz)\n",
3381				    lfptoa(&dts, 9)));
3382#endif	/* DEBUG_TIMING */
3383			ts = nts;  /* network time stamp */
3384			break;
3385#endif	/* HAVE_BINTIME || HAVE_TIMESTAMPNS || HAVE_TIMESTAMP */
3386
3387		default:
3388			DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n",
3389				    cmsghdr->cmsg_type));
3390		}
3391		cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
3392	}
3393	return ts;
3394}
3395#endif	/* HAVE_PACKET_TIMESTAMP */
3396
3397
3398/*
3399 * Routine to read the network NTP packets for a specific interface
3400 * Return the number of bytes read. That way we know if we should
3401 * read it again or go on to the next one if no bytes returned
3402 */
3403static inline int
3404read_network_packet(
3405	SOCKET			fd,
3406	struct interface *	itf,
3407	l_fp			ts
3408	)
3409{
3410	GETSOCKNAME_SOCKLEN_TYPE fromlen;
3411	int buflen;
3412	register struct recvbuf *rb;
3413#ifdef HAVE_PACKET_TIMESTAMP
3414	struct msghdr msghdr;
3415	struct iovec iovec;
3416	char control[CMSG_BUFSIZE];
3417#endif
3418
3419	/*
3420	 * Get a buffer and read the frame.  If we
3421	 * haven't got a buffer, or this is received
3422	 * on a disallowed socket, just dump the
3423	 * packet.
3424	 */
3425
3426	rb = get_free_recv_buffer();
3427	if (NULL == rb || itf->ignore_packets) {
3428		char buf[RX_BUFF_SIZE];
3429		sockaddr_u from;
3430
3431		if (rb != NULL)
3432			freerecvbuf(rb);
3433
3434		fromlen = sizeof(from);
3435		buflen = recvfrom(fd, buf, sizeof(buf), 0,
3436				  &from.sa, &fromlen);
3437		DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
3438			(itf->ignore_packets)
3439			    ? "ignore"
3440			    : "drop",
3441			free_recvbuffs(), fd, stoa(&from)));
3442		if (itf->ignore_packets)
3443			packets_ignored++;
3444		else
3445			packets_dropped++;
3446		return (buflen);
3447	}
3448
3449	fromlen = sizeof(rb->recv_srcadr);
3450
3451#ifndef HAVE_PACKET_TIMESTAMP
3452	rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
3453				   sizeof(rb->recv_space), 0,
3454				   &rb->recv_srcadr.sa, &fromlen);
3455#else
3456	iovec.iov_base        = &rb->recv_space;
3457	iovec.iov_len         = sizeof(rb->recv_space);
3458	msghdr.msg_name       = &rb->recv_srcadr;
3459	msghdr.msg_namelen    = fromlen;
3460	msghdr.msg_iov        = &iovec;
3461	msghdr.msg_iovlen     = 1;
3462	msghdr.msg_control    = (void *)&control;
3463	msghdr.msg_controllen = sizeof(control);
3464	msghdr.msg_flags      = 0;
3465	rb->recv_length       = recvmsg(fd, &msghdr, 0);
3466#endif
3467
3468	buflen = rb->recv_length;
3469
3470	if (buflen == 0 || (buflen == -1 &&
3471	    (EWOULDBLOCK == errno
3472#ifdef EAGAIN
3473	     || EAGAIN == errno
3474#endif
3475	     ))) {
3476		freerecvbuf(rb);
3477		return (buflen);
3478	} else if (buflen < 0) {
3479		msyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
3480			stoa(&rb->recv_srcadr), fd);
3481		DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n",
3482			    fd));
3483		freerecvbuf(rb);
3484		return (buflen);
3485	}
3486
3487	DPRINTF(3, ("read_network_packet: fd=%d length %d from %s\n",
3488		    fd, buflen, stoa(&rb->recv_srcadr)));
3489
3490#ifdef ENABLE_BUG3020_FIX
3491	if (ISREFCLOCKADR(&rb->recv_srcadr)) {
3492		msyslog(LOG_ERR, "recvfrom(%s) fd=%d: refclock srcadr on a network interface!",
3493			stoa(&rb->recv_srcadr), fd);
3494		DPRINTF(1, ("read_network_packet: fd=%d dropped (refclock srcadr))\n",
3495			    fd));
3496		packets_dropped++;
3497		freerecvbuf(rb);
3498		return (buflen);
3499	}
3500#endif
3501
3502	/*
3503	** Bug 2672: Some OSes (MacOSX and Linux) don't block spoofed ::1
3504	*/
3505
3506	if (AF_INET6 == itf->family) {
3507		DPRINTF(2, ("Got an IPv6 packet, from <%s> (%d) to <%s> (%d)\n",
3508			stoa(&rb->recv_srcadr),
3509			IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr)),
3510			stoa(&itf->sin),
3511			!IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3512			));
3513
3514		if (   IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&rb->recv_srcadr))
3515		    && !IN6_IS_ADDR_LOOPBACK(PSOCK_ADDR6(&itf->sin))
3516		   ) {
3517			packets_dropped++;
3518			DPRINTF(2, ("DROPPING that packet\n"));
3519			freerecvbuf(rb);
3520			return buflen;
3521		}
3522		DPRINTF(2, ("processing that packet\n"));
3523	}
3524
3525	/*
3526	 * Got one.  Mark how and when it got here,
3527	 * put it on the full list and do bookkeeping.
3528	 */
3529	rb->dstadr = itf;
3530	rb->fd = fd;
3531#ifdef HAVE_PACKET_TIMESTAMP
3532	/* pick up a network time stamp if possible */
3533	ts = fetch_timestamp(rb, &msghdr, ts);
3534#endif
3535	rb->recv_time = ts;
3536	rb->receiver = receive;
3537
3538	add_full_recv_buffer(rb);
3539
3540	itf->received++;
3541	packets_received++;
3542	return (buflen);
3543}
3544
3545/*
3546 * attempt to handle io (select()/signaled IO)
3547 */
3548void
3549io_handler(void)
3550{
3551#  ifndef HAVE_SIGNALED_IO
3552	fd_set rdfdes;
3553	int nfound;
3554
3555	/*
3556	 * Use select() on all on all input fd's for unlimited
3557	 * time.  select() will terminate on SIGALARM or on the
3558	 * reception of input.	Using select() means we can't do
3559	 * robust signal handling and we get a potential race
3560	 * between checking for alarms and doing the select().
3561	 * Mostly harmless, I think.
3562	 */
3563	/*
3564	 * On VMS, I suspect that select() can't be interrupted
3565	 * by a "signal" either, so I take the easy way out and
3566	 * have select() time out after one second.
3567	 * System clock updates really aren't time-critical,
3568	 * and - lacking a hardware reference clock - I have
3569	 * yet to learn about anything else that is.
3570	 */
3571	++handler_calls;
3572	rdfdes = activefds;
3573#   if !defined(VMS) && !defined(SYS_VXWORKS)
3574	nfound = select(maxactivefd + 1, &rdfdes, NULL,
3575			NULL, NULL);
3576#   else	/* VMS, VxWorks */
3577	/* make select() wake up after one second */
3578	{
3579		struct timeval t1;
3580		t1.tv_sec  = 1;
3581		t1.tv_usec = 0;
3582		nfound = select(maxactivefd + 1,
3583				&rdfdes, NULL, NULL,
3584				&t1);
3585	}
3586#   endif	/* VMS, VxWorks */
3587	if (nfound < 0 && sanitize_fdset(errno)) {
3588		struct timeval t1;
3589		t1.tv_sec  = 0;
3590		t1.tv_usec = 0;
3591		rdfdes = activefds;
3592		nfound = select(maxactivefd + 1,
3593				&rdfdes, NULL, NULL,
3594				&t1);
3595	}
3596
3597	if (nfound > 0) {
3598		l_fp ts;
3599
3600		get_systime(&ts);
3601
3602		input_handler_scan(&ts, &rdfdes);
3603	} else if (nfound == -1 && errno != EINTR) {
3604		msyslog(LOG_ERR, "select() error: %m");
3605	}
3606#   ifdef DEBUG
3607	else if (debug > 4) {
3608		msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
3609	} else {
3610		DPRINTF(3, ("select() returned %d: %m\n", nfound));
3611	}
3612#   endif /* DEBUG */
3613#  else /* HAVE_SIGNALED_IO */
3614	wait_for_signal();
3615#  endif /* HAVE_SIGNALED_IO */
3616}
3617
3618#ifdef HAVE_SIGNALED_IO
3619/*
3620 * input_handler - receive packets asynchronously
3621 *
3622 * ALWAYS IN SIGNAL HANDLER CONTEXT -- only async-safe functions allowed!
3623 */
3624static RETSIGTYPE
3625input_handler(
3626	l_fp *	cts
3627	)
3628{
3629	int		n;
3630	struct timeval	tvzero;
3631	fd_set		fds;
3632
3633	++handler_calls;
3634
3635	/*
3636	 * Do a poll to see who has data
3637	 */
3638
3639	fds = activefds;
3640	tvzero.tv_sec = tvzero.tv_usec = 0;
3641
3642	n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3643	if (n < 0 && sanitize_fdset(errno)) {
3644		fds = activefds;
3645		tvzero.tv_sec = tvzero.tv_usec = 0;
3646		n = select(maxactivefd + 1, &fds, NULL, NULL, &tvzero);
3647	}
3648	if (n > 0)
3649		input_handler_scan(cts, &fds);
3650}
3651#endif /* HAVE_SIGNALED_IO */
3652
3653
3654/*
3655 * Try to sanitize the global FD set
3656 *
3657 * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3658 */
3659static int/*BOOL*/
3660sanitize_fdset(
3661	int	errc
3662	)
3663{
3664	int j, b, maxscan;
3665
3666#  ifndef HAVE_SIGNALED_IO
3667	/*
3668	 * extended FAU debugging output
3669	 */
3670	if (errc != EINTR) {
3671		msyslog(LOG_ERR,
3672			"select(%d, %s, 0L, 0L, &0.0) error: %m",
3673			maxactivefd + 1,
3674			fdbits(maxactivefd, &activefds));
3675	}
3676#   endif
3677
3678	if (errc != EBADF)
3679		return FALSE;
3680
3681	/* if we have oviously bad FDs, try to sanitize the FD set. */
3682	for (j = 0, maxscan = 0; j <= maxactivefd; j++) {
3683		if (FD_ISSET(j, &activefds)) {
3684			if (-1 != read(j, &b, 0)) {
3685				maxscan = j;
3686				continue;
3687			}
3688#		    ifndef HAVE_SIGNALED_IO
3689			msyslog(LOG_ERR,
3690				"Removing bad file descriptor %d from select set",
3691				j);
3692#		    endif
3693			FD_CLR(j, &activefds);
3694		}
3695	}
3696	if (maxactivefd != maxscan)
3697		maxactivefd = maxscan;
3698	return TRUE;
3699}
3700
3701/*
3702 * scan the known FDs (clocks, servers, ...) for presence in a 'fd_set'.
3703 *
3704 * SIGNAL HANDLER CONTEXT if HAVE_SIGNALED_IO, ordinary userspace otherwise
3705 */
3706static void
3707input_handler_scan(
3708	const l_fp *	cts,
3709	const fd_set *	pfds
3710	)
3711{
3712	int		buflen;
3713	u_int		idx;
3714	int		doing;
3715	SOCKET		fd;
3716	blocking_child *c;
3717	l_fp		ts;	/* Timestamp at BOselect() gob */
3718
3719#if defined(DEBUG_TIMING)
3720	l_fp		ts_e;	/* Timestamp at EOselect() gob */
3721#endif
3722	endpt *		ep;
3723#ifdef REFCLOCK
3724	struct refclockio *rp;
3725	int		saved_errno;
3726	const char *	clk;
3727#endif
3728#ifdef HAS_ROUTING_SOCKET
3729	struct asyncio_reader *	asyncio_reader;
3730	struct asyncio_reader *	next_asyncio_reader;
3731#endif
3732
3733	++handler_pkts;
3734	ts = *cts;
3735
3736#ifdef REFCLOCK
3737	/*
3738	 * Check out the reference clocks first, if any
3739	 */
3740
3741	for (rp = refio; rp != NULL; rp = rp->next) {
3742		fd = rp->fd;
3743
3744		if (!FD_ISSET(fd, pfds))
3745			continue;
3746		buflen = read_refclock_packet(fd, rp, ts);
3747		/*
3748		 * The first read must succeed after select() indicates
3749		 * readability, or we've reached a permanent EOF.
3750		 * http://bugs.ntp.org/1732 reported ntpd munching CPU
3751		 * after a USB GPS was unplugged because select was
3752		 * indicating EOF but ntpd didn't remove the descriptor
3753		 * from the activefds set.
3754		 */
3755		if (buflen < 0 && EAGAIN != errno) {
3756			saved_errno = errno;
3757			clk = refnumtoa(&rp->srcclock->srcadr);
3758			errno = saved_errno;
3759			msyslog(LOG_ERR, "%s read: %m", clk);
3760			maintain_activefds(fd, TRUE);
3761		} else if (0 == buflen) {
3762			clk = refnumtoa(&rp->srcclock->srcadr);
3763			msyslog(LOG_ERR, "%s read EOF", clk);
3764			maintain_activefds(fd, TRUE);
3765		} else {
3766			/* drain any remaining refclock input */
3767			do {
3768				buflen = read_refclock_packet(fd, rp, ts);
3769			} while (buflen > 0);
3770		}
3771	}
3772#endif /* REFCLOCK */
3773
3774	/*
3775	 * Loop through the interfaces looking for data to read.
3776	 */
3777	for (ep = ep_list; ep != NULL; ep = ep->elink) {
3778		for (doing = 0; doing < 2; doing++) {
3779			if (!doing) {
3780				fd = ep->fd;
3781			} else {
3782				if (!(ep->flags & INT_BCASTOPEN))
3783					break;
3784				fd = ep->bfd;
3785			}
3786			if (fd < 0)
3787				continue;
3788			if (FD_ISSET(fd, pfds))
3789				do {
3790					buflen = read_network_packet(
3791							fd, ep, ts);
3792				} while (buflen > 0);
3793			/* Check more interfaces */
3794		}
3795	}
3796
3797#ifdef HAS_ROUTING_SOCKET
3798	/*
3799	 * scan list of asyncio readers - currently only used for routing sockets
3800	 */
3801	asyncio_reader = asyncio_reader_list;
3802
3803	while (asyncio_reader != NULL) {
3804		/* callback may unlink and free asyncio_reader */
3805		next_asyncio_reader = asyncio_reader->link;
3806		if (FD_ISSET(asyncio_reader->fd, pfds))
3807			(*asyncio_reader->receiver)(asyncio_reader);
3808		asyncio_reader = next_asyncio_reader;
3809	}
3810#endif /* HAS_ROUTING_SOCKET */
3811
3812	/*
3813	 * Check for a response from a blocking child
3814	 */
3815	for (idx = 0; idx < blocking_children_alloc; idx++) {
3816		c = blocking_children[idx];
3817		if (NULL == c || -1 == c->resp_read_pipe)
3818			continue;
3819		if (FD_ISSET(c->resp_read_pipe, pfds)) {
3820			++c->resp_ready_seen;
3821			++blocking_child_ready_seen;
3822		}
3823	}
3824
3825	/* We've done our work */
3826#if defined(DEBUG_TIMING)
3827	get_systime(&ts_e);
3828	/*
3829	 * (ts_e - ts) is the amount of time we spent
3830	 * processing this gob of file descriptors.  Log
3831	 * it.
3832	 */
3833	L_SUB(&ts_e, &ts);
3834	collect_timing(NULL, "input handler", 1, &ts_e);
3835	if (debug > 3)
3836		msyslog(LOG_DEBUG,
3837			"input_handler: Processed a gob of fd's in %s msec",
3838			lfptoms(&ts_e, 6));
3839#endif /* DEBUG_TIMING */
3840}
3841#endif /* !HAVE_IO_COMPLETION_PORT */
3842
3843/*
3844 * find an interface suitable for the src address
3845 */
3846endpt *
3847select_peerinterface(
3848	struct peer *	peer,
3849	sockaddr_u *	srcadr,
3850	endpt *		dstadr
3851	)
3852{
3853	endpt *ep;
3854#ifndef SIM
3855	endpt *wild;
3856
3857	wild = ANY_INTERFACE_CHOOSE(srcadr);
3858
3859	/*
3860	 * Initialize the peer structure and dance the interface jig.
3861	 * Reference clocks step the loopback waltz, the others
3862	 * squaredance around the interface list looking for a buddy. If
3863	 * the dance peters out, there is always the wildcard interface.
3864	 * This might happen in some systems and would preclude proper
3865	 * operation with public key cryptography.
3866	 */
3867	if (ISREFCLOCKADR(srcadr)) {
3868		ep = loopback_interface;
3869	} else if (peer->cast_flags &
3870		   (MDF_BCLNT | MDF_ACAST | MDF_MCAST | MDF_BCAST)) {
3871		ep = findbcastinter(srcadr);
3872		if (ep != NULL)
3873			DPRINTF(4, ("Found *-cast interface %s for address %s\n",
3874				stoa(&ep->sin), stoa(srcadr)));
3875		else
3876			DPRINTF(4, ("No *-cast local address found for address %s\n",
3877				stoa(srcadr)));
3878	} else {
3879		ep = dstadr;
3880		if (NULL == ep)
3881			ep = wild;
3882	}
3883	/*
3884	 * If it is a multicast address, findbcastinter() may not find
3885	 * it.  For unicast, we get to find the interface when dstadr is
3886	 * given to us as the wildcard (ANY_INTERFACE_CHOOSE).  Either
3887	 * way, try a little harder.
3888	 */
3889	if (wild == ep)
3890		ep = findinterface(srcadr);
3891	/*
3892	 * we do not bind to the wildcard interfaces for output
3893	 * as our (network) source address would be undefined and
3894	 * crypto will not work without knowing the own transmit address
3895	 */
3896	if (ep != NULL && INT_WILDCARD & ep->flags)
3897		if (!accept_wildcard_if_for_winnt)
3898			ep = NULL;
3899#else	/* SIM follows */
3900	ep = loopback_interface;
3901#endif
3902
3903	return ep;
3904}
3905
3906
3907/*
3908 * findinterface - find local interface corresponding to address
3909 */
3910endpt *
3911findinterface(
3912	sockaddr_u *addr
3913	)
3914{
3915	endpt *iface;
3916
3917	iface = findlocalinterface(addr, INT_WILDCARD, 0);
3918
3919	if (NULL == iface) {
3920		DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
3921			    stoa(addr)));
3922
3923		iface = ANY_INTERFACE_CHOOSE(addr);
3924	} else
3925		DPRINTF(4, ("Found interface #%d %s for address %s\n",
3926			    iface->ifnum, iface->name, stoa(addr)));
3927
3928	return iface;
3929}
3930
3931/*
3932 * findlocalinterface - find local interface corresponding to addr,
3933 * which does not have any of flags set.  If bast is nonzero, addr is
3934 * a broadcast address.
3935 *
3936 * This code attempts to find the local sending address for an outgoing
3937 * address by connecting a new socket to destinationaddress:NTP_PORT
3938 * and reading the sockname of the resulting connect.
3939 * the complicated sequence simulates the routing table lookup
3940 * for to first hop without duplicating any of the routing logic into
3941 * ntpd. preferably we would have used an API call - but its not there -
3942 * so this is the best we can do here short of duplicating to entire routing
3943 * logic in ntpd which would be a silly and really unportable thing to do.
3944 *
3945 */
3946static endpt *
3947findlocalinterface(
3948	sockaddr_u *	addr,
3949	int		flags,
3950	int		bcast
3951	)
3952{
3953	GETSOCKNAME_SOCKLEN_TYPE	sockaddrlen;
3954	endpt *				iface;
3955	sockaddr_u			saddr;
3956	SOCKET				s;
3957	int				rtn;
3958	int				on;
3959
3960	DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
3961		    stoa(addr)));
3962
3963	s = socket(AF(addr), SOCK_DGRAM, 0);
3964	if (INVALID_SOCKET == s)
3965		return NULL;
3966
3967	/*
3968	 * If we are looking for broadcast interface we need to set this
3969	 * socket to allow broadcast
3970	 */
3971	if (bcast) {
3972		on = 1;
3973		if (SOCKET_ERROR == setsockopt(s, SOL_SOCKET,
3974						SO_BROADCAST,
3975						(char *)&on,
3976						sizeof(on))) {
3977			closesocket(s);
3978			return NULL;
3979		}
3980	}
3981
3982	rtn = connect(s, &addr->sa, SOCKLEN(addr));
3983	if (SOCKET_ERROR == rtn) {
3984		closesocket(s);
3985		return NULL;
3986	}
3987
3988	sockaddrlen = sizeof(saddr);
3989	rtn = getsockname(s, &saddr.sa, &sockaddrlen);
3990	closesocket(s);
3991	if (SOCKET_ERROR == rtn)
3992		return NULL;
3993
3994	DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n",
3995		    stoa(addr), stoa(&saddr)));
3996
3997	iface = getinterface(&saddr, flags);
3998
3999	/*
4000	 * if we didn't find an exact match on saddr, find the closest
4001	 * available local address.  This handles the case of the
4002	 * address suggested by the kernel being excluded by nic rules
4003	 * or the user's -I and -L options to ntpd.
4004	 * See http://bugs.ntp.org/1184 and http://bugs.ntp.org/1683
4005	 * for more background.
4006	 */
4007	if (NULL == iface || iface->ignore_packets)
4008		iface = findclosestinterface(&saddr,
4009					     flags | INT_LOOPBACK);
4010
4011	/* Don't use an interface which will ignore replies */
4012	if (iface != NULL && iface->ignore_packets)
4013		iface = NULL;
4014
4015	return iface;
4016}
4017
4018
4019/*
4020 * findclosestinterface
4021 *
4022 * If there are -I/--interface or -L/novirtualips command-line options,
4023 * or "nic" or "interface" rules in ntp.conf, findlocalinterface() may
4024 * find the kernel's preferred local address for a given peer address is
4025 * administratively unavailable to ntpd, and punt to this routine's more
4026 * expensive search.
4027 *
4028 * Find the numerically closest local address to the one connect()
4029 * suggested.  This matches an address on the same subnet first, as
4030 * needed by Bug 1184, and provides a consistent choice if there are
4031 * multiple feasible local addresses, regardless of the order ntpd
4032 * enumerated them.
4033 */
4034endpt *
4035findclosestinterface(
4036	sockaddr_u *	addr,
4037	int		flags
4038	)
4039{
4040	endpt *		ep;
4041	endpt *		winner;
4042	sockaddr_u	addr_dist;
4043	sockaddr_u	min_dist;
4044
4045	ZERO_SOCK(&min_dist);
4046	winner = NULL;
4047
4048	for (ep = ep_list; ep != NULL; ep = ep->elink) {
4049		if (ep->ignore_packets ||
4050		    AF(addr) != ep->family ||
4051		    flags & ep->flags)
4052			continue;
4053
4054		calc_addr_distance(&addr_dist, addr, &ep->sin);
4055		if (NULL == winner ||
4056		    -1 == cmp_addr_distance(&addr_dist, &min_dist)) {
4057			min_dist = addr_dist;
4058			winner = ep;
4059		}
4060	}
4061	if (NULL == winner)
4062		DPRINTF(4, ("findclosestinterface(%s) failed\n",
4063			    stoa(addr)));
4064	else
4065		DPRINTF(4, ("findclosestinterface(%s) -> %s\n",
4066			    stoa(addr), stoa(&winner->sin)));
4067
4068	return winner;
4069}
4070
4071
4072/*
4073 * calc_addr_distance - calculate the distance between two addresses,
4074 *			the absolute value of the difference between
4075 *			the addresses numerically, stored as an address.
4076 */
4077static void
4078calc_addr_distance(
4079	sockaddr_u *		dist,
4080	const sockaddr_u *	a1,
4081	const sockaddr_u *	a2
4082	)
4083{
4084	u_int32	a1val;
4085	u_int32	a2val;
4086	u_int32	v4dist;
4087	int	found_greater;
4088	int	a1_greater;
4089	int	i;
4090
4091	REQUIRE(AF(a1) == AF(a2));
4092
4093	ZERO_SOCK(dist);
4094	AF(dist) = AF(a1);
4095
4096	/* v4 can be done a bit simpler */
4097	if (IS_IPV4(a1)) {
4098		a1val = SRCADR(a1);
4099		a2val = SRCADR(a2);
4100		v4dist = (a1val > a2val)
4101			     ? a1val - a2val
4102			     : a2val - a1val;
4103		SET_ADDR4(dist, v4dist);
4104
4105		return;
4106	}
4107
4108	found_greater = FALSE;
4109	a1_greater = FALSE;	/* suppress pot. uninit. warning */
4110	for (i = 0; i < (int)sizeof(NSRCADR6(a1)); i++) {
4111		if (!found_greater &&
4112		    NSRCADR6(a1)[i] != NSRCADR6(a2)[i]) {
4113			found_greater = TRUE;
4114			a1_greater = (NSRCADR6(a1)[i] > NSRCADR6(a2)[i]);
4115		}
4116		if (!found_greater) {
4117			NSRCADR6(dist)[i] = 0;
4118		} else {
4119			if (a1_greater)
4120				NSRCADR6(dist)[i] = NSRCADR6(a1)[i] -
4121						    NSRCADR6(a2)[i];
4122			else
4123				NSRCADR6(dist)[i] = NSRCADR6(a2)[i] -
4124						    NSRCADR6(a1)[i];
4125		}
4126	}
4127}
4128
4129
4130/*
4131 * cmp_addr_distance - compare two address distances, returning -1, 0,
4132 *		       1 to indicate their relationship.
4133 */
4134static int
4135cmp_addr_distance(
4136	const sockaddr_u *	d1,
4137	const sockaddr_u *	d2
4138	)
4139{
4140	int	i;
4141
4142	REQUIRE(AF(d1) == AF(d2));
4143
4144	if (IS_IPV4(d1)) {
4145		if (SRCADR(d1) < SRCADR(d2))
4146			return -1;
4147		else if (SRCADR(d1) == SRCADR(d2))
4148			return 0;
4149		else
4150			return 1;
4151	}
4152
4153	for (i = 0; i < (int)sizeof(NSRCADR6(d1)); i++) {
4154		if (NSRCADR6(d1)[i] < NSRCADR6(d2)[i])
4155			return -1;
4156		else if (NSRCADR6(d1)[i] > NSRCADR6(d2)[i])
4157			return 1;
4158	}
4159
4160	return 0;
4161}
4162
4163
4164
4165/*
4166 * fetch an interface structure the matches the
4167 * address and has the given flags NOT set
4168 */
4169endpt *
4170getinterface(
4171	sockaddr_u *	addr,
4172	u_int32		flags
4173	)
4174{
4175	endpt *iface;
4176
4177	iface = find_addr_in_list(addr);
4178
4179	if (iface != NULL && (iface->flags & flags))
4180		iface = NULL;
4181
4182	return iface;
4183}
4184
4185
4186/*
4187 * findbcastinter - find broadcast interface corresponding to address
4188 */
4189endpt *
4190findbcastinter(
4191	sockaddr_u *addr
4192	)
4193{
4194	endpt *	iface;
4195
4196	iface = NULL;
4197#if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
4198	DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
4199		    stoa(addr)));
4200
4201	iface = findlocalinterface(addr, INT_LOOPBACK | INT_WILDCARD,
4202				   1);
4203	if (iface != NULL) {
4204		DPRINTF(4, ("Easily found bcast-/mcast- interface index #%d %s\n",
4205			    iface->ifnum, iface->name));
4206		return iface;
4207	}
4208
4209	/*
4210	 * plan B - try to find something reasonable in our lists in
4211	 * case kernel lookup doesn't help
4212	 */
4213	for (iface = ep_list; iface != NULL; iface = iface->elink) {
4214		if (iface->flags & INT_WILDCARD)
4215			continue;
4216
4217		/* Don't bother with ignored interfaces */
4218		if (iface->ignore_packets)
4219			continue;
4220
4221		/*
4222		 * First look if this is the correct family
4223		 */
4224		if(AF(&iface->sin) != AF(addr))
4225			continue;
4226
4227		/* Skip the loopback addresses */
4228		if (iface->flags & INT_LOOPBACK)
4229			continue;
4230
4231		/*
4232		 * If we are looking to match a multicast address and
4233		 * this interface is one...
4234		 */
4235		if (addr_ismulticast(addr)
4236		    && (iface->flags & INT_MULTICAST)) {
4237#ifdef INCLUDE_IPV6_SUPPORT
4238			/*
4239			 * ...it is the winner unless we're looking for
4240			 * an interface to use for link-local multicast
4241			 * and its address is not link-local.
4242			 */
4243			if (IS_IPV6(addr)
4244			    && IN6_IS_ADDR_MC_LINKLOCAL(PSOCK_ADDR6(addr))
4245			    && !IN6_IS_ADDR_LINKLOCAL(PSOCK_ADDR6(&iface->sin)))
4246				continue;
4247#endif
4248			break;
4249		}
4250
4251		/*
4252		 * We match only those interfaces marked as
4253		 * broadcastable and either the explicit broadcast
4254		 * address or the network portion of the IP address.
4255		 * Sloppy.
4256		 */
4257		if (IS_IPV4(addr)) {
4258			if (SOCK_EQ(&iface->bcast, addr))
4259				break;
4260
4261			if ((NSRCADR(&iface->sin) & NSRCADR(&iface->mask))
4262			    == (NSRCADR(addr)	  & NSRCADR(&iface->mask)))
4263				break;
4264		}
4265#ifdef INCLUDE_IPV6_SUPPORT
4266		else if (IS_IPV6(addr)) {
4267			if (SOCK_EQ(&iface->bcast, addr))
4268				break;
4269
4270			if (SOCK_EQ(netof(&iface->sin), netof(addr)))
4271				break;
4272		}
4273#endif
4274	}
4275#endif /* SIOCGIFCONF */
4276	if (NULL == iface) {
4277		DPRINTF(4, ("No bcast interface found for %s\n",
4278			    stoa(addr)));
4279		iface = ANY_INTERFACE_CHOOSE(addr);
4280	} else {
4281		DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n",
4282			    iface->ifnum, iface->name));
4283	}
4284
4285	return iface;
4286}
4287
4288
4289/*
4290 * io_clr_stats - clear I/O module statistics
4291 */
4292void
4293io_clr_stats(void)
4294{
4295	packets_dropped = 0;
4296	packets_ignored = 0;
4297	packets_received = 0;
4298	packets_sent = 0;
4299	packets_notsent = 0;
4300
4301	handler_calls = 0;
4302	handler_pkts = 0;
4303	io_timereset = current_time;
4304}
4305
4306
4307#ifdef REFCLOCK
4308/*
4309 * io_addclock - add a reference clock to the list and arrange that we
4310 *				 get SIGIO interrupts from it.
4311 */
4312int
4313io_addclock(
4314	struct refclockio *rio
4315	)
4316{
4317	BLOCKIO();
4318
4319	/*
4320	 * Stuff the I/O structure in the list and mark the descriptor
4321	 * in use.  There is a harmless (I hope) race condition here.
4322	 */
4323	rio->active = TRUE;
4324
4325# ifdef HAVE_SIGNALED_IO
4326	if (init_clock_sig(rio)) {
4327		UNBLOCKIO();
4328		return 0;
4329	}
4330# elif defined(HAVE_IO_COMPLETION_PORT)
4331	if (!io_completion_port_add_clock_io(rio)) {
4332		UNBLOCKIO();
4333		return 0;
4334	}
4335# endif
4336
4337	/*
4338	 * enqueue
4339	 */
4340	LINK_SLIST(refio, rio, next);
4341
4342	/*
4343	 * register fd
4344	 */
4345	add_fd_to_list(rio->fd, FD_TYPE_FILE);
4346
4347	UNBLOCKIO();
4348	return 1;
4349}
4350
4351
4352/*
4353 * io_closeclock - close the clock in the I/O structure given
4354 */
4355void
4356io_closeclock(
4357	struct refclockio *rio
4358	)
4359{
4360	struct refclockio *unlinked;
4361
4362	BLOCKIO();
4363
4364	/*
4365	 * Remove structure from the list
4366	 */
4367	rio->active = FALSE;
4368	UNLINK_SLIST(unlinked, refio, rio, next, struct refclockio);
4369	if (NULL != unlinked) {
4370		/* Close the descriptor. The order of operations is
4371		 * important here in case of async / overlapped IO:
4372		 * only after we have removed the clock from the
4373		 * IO completion port we can be sure no further
4374		 * input is queued. So...
4375		 *  - we first disable feeding to the queu by removing
4376		 *    the clock from the IO engine
4377		 *  - close the file (which brings down any IO on it)
4378		 *  - clear the buffer from results for this fd
4379		 */
4380#	    ifdef HAVE_IO_COMPLETION_PORT
4381		io_completion_port_remove_clock_io(rio);
4382#	    endif
4383		close_and_delete_fd_from_list(rio->fd);
4384		purge_recv_buffers_for_fd(rio->fd);
4385		rio->fd = -1;
4386	}
4387
4388	UNBLOCKIO();
4389}
4390#endif	/* REFCLOCK */
4391
4392
4393/*
4394 * On NT a SOCKET is an unsigned int so we cannot possibly keep it in
4395 * an array. So we use one of the ISC_LIST functions to hold the
4396 * socket value and use that when we want to enumerate it.
4397 *
4398 * This routine is called by the forked intres child process to close
4399 * all open sockets.  On Windows there's no need as intres runs in
4400 * the same process as a thread.
4401 */
4402#ifndef SYS_WINNT
4403void
4404kill_asyncio(
4405	int	startfd
4406	)
4407{
4408	BLOCKIO();
4409
4410	/*
4411	 * In the child process we do not maintain activefds and
4412	 * maxactivefd.  Zeroing maxactivefd disables code which
4413	 * maintains it in close_and_delete_fd_from_list().
4414	 */
4415	maxactivefd = 0;
4416
4417	while (fd_list != NULL)
4418		close_and_delete_fd_from_list(fd_list->fd);
4419
4420	UNBLOCKIO();
4421}
4422#endif	/* !SYS_WINNT */
4423
4424
4425/*
4426 * Add and delete functions for the list of open sockets
4427 */
4428static void
4429add_fd_to_list(
4430	SOCKET fd,
4431	enum desc_type type
4432	)
4433{
4434	vsock_t *lsock = emalloc(sizeof(*lsock));
4435
4436	lsock->fd = fd;
4437	lsock->type = type;
4438
4439	LINK_SLIST(fd_list, lsock, link);
4440	maintain_activefds(fd, 0);
4441}
4442
4443
4444static void
4445close_and_delete_fd_from_list(
4446	SOCKET fd
4447	)
4448{
4449	vsock_t *lsock;
4450
4451	UNLINK_EXPR_SLIST(lsock, fd_list, fd ==
4452	    UNLINK_EXPR_SLIST_CURRENT()->fd, link, vsock_t);
4453
4454	if (NULL == lsock)
4455		return;
4456
4457	switch (lsock->type) {
4458
4459	case FD_TYPE_SOCKET:
4460		closesocket(lsock->fd);
4461		break;
4462
4463	case FD_TYPE_FILE:
4464		closeserial((int)lsock->fd);
4465		break;
4466
4467	default:
4468		msyslog(LOG_ERR,
4469			"internal error - illegal descriptor type %d - EXITING",
4470			(int)lsock->type);
4471		exit(1);
4472	}
4473
4474	free(lsock);
4475	/*
4476	 * remove from activefds
4477	 */
4478	maintain_activefds(fd, 1);
4479}
4480
4481
4482static void
4483add_addr_to_list(
4484	sockaddr_u *	addr,
4485	endpt *		ep
4486	)
4487{
4488	remaddr_t *laddr;
4489
4490#ifdef DEBUG
4491	if (find_addr_in_list(addr) == NULL) {
4492#endif
4493		/* not there yet - add to list */
4494		laddr = emalloc(sizeof(*laddr));
4495		laddr->addr = *addr;
4496		laddr->ep = ep;
4497
4498		LINK_SLIST(remoteaddr_list, laddr, link);
4499
4500		DPRINTF(4, ("Added addr %s to list of addresses\n",
4501			    stoa(addr)));
4502#ifdef DEBUG
4503	} else
4504		DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
4505			    stoa(addr)));
4506#endif
4507}
4508
4509
4510static void
4511delete_addr_from_list(
4512	sockaddr_u *addr
4513	)
4514{
4515	remaddr_t *unlinked;
4516
4517	UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, SOCK_EQ(addr,
4518		&(UNLINK_EXPR_SLIST_CURRENT()->addr)), link, remaddr_t);
4519
4520	if (unlinked != NULL) {
4521		DPRINTF(4, ("Deleted addr %s from list of addresses\n",
4522			stoa(addr)));
4523		free(unlinked);
4524	}
4525}
4526
4527
4528static void
4529delete_interface_from_list(
4530	endpt *iface
4531	)
4532{
4533	remaddr_t *unlinked;
4534
4535	for (;;) {
4536		UNLINK_EXPR_SLIST(unlinked, remoteaddr_list, iface ==
4537		    UNLINK_EXPR_SLIST_CURRENT()->ep, link,
4538		    remaddr_t);
4539
4540		if (unlinked == NULL)
4541			break;
4542		DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
4543			    stoa(&unlinked->addr), iface->ifnum,
4544			    iface->name));
4545		free(unlinked);
4546	}
4547}
4548
4549
4550static struct interface *
4551find_addr_in_list(
4552	sockaddr_u *addr
4553	)
4554{
4555	remaddr_t *entry;
4556
4557	DPRINTF(4, ("Searching for addr %s in list of addresses - ",
4558		    stoa(addr)));
4559
4560	for (entry = remoteaddr_list;
4561	     entry != NULL;
4562	     entry = entry->link)
4563		if (SOCK_EQ(&entry->addr, addr)) {
4564			DPRINTF(4, ("FOUND\n"));
4565			return entry->ep;
4566		}
4567
4568	DPRINTF(4, ("NOT FOUND\n"));
4569	return NULL;
4570}
4571
4572
4573/*
4574 * Find the given address with the all given flags set in the list
4575 */
4576static endpt *
4577find_flagged_addr_in_list(
4578	sockaddr_u *	addr,
4579	u_int32		flags
4580	)
4581{
4582	remaddr_t *entry;
4583
4584	DPRINTF(4, ("Finding addr %s with flags %d in list: ",
4585		    stoa(addr), flags));
4586
4587	for (entry = remoteaddr_list;
4588	     entry != NULL;
4589	     entry = entry->link)
4590
4591		if (SOCK_EQ(&entry->addr, addr)
4592		    && (entry->ep->flags & flags) == flags) {
4593
4594			DPRINTF(4, ("FOUND\n"));
4595			return entry->ep;
4596		}
4597
4598	DPRINTF(4, ("NOT FOUND\n"));
4599	return NULL;
4600}
4601
4602
4603const char *
4604localaddrtoa(
4605	endpt *la
4606	)
4607{
4608	return (NULL == la)
4609		   ? "<null>"
4610		   : stoa(&la->sin);
4611}
4612
4613
4614#ifdef HAS_ROUTING_SOCKET
4615# ifndef UPDATE_GRACE
4616#  define UPDATE_GRACE	2	/* wait UPDATE_GRACE seconds before scanning */
4617# endif
4618
4619static void
4620process_routing_msgs(struct asyncio_reader *reader)
4621{
4622	char buffer[5120];
4623	int cnt, msg_type;
4624#ifdef HAVE_RTNETLINK
4625	struct nlmsghdr *nh;
4626#else
4627	struct rt_msghdr rtm;
4628	char *p;
4629#endif
4630
4631	if (disable_dynamic_updates) {
4632		/*
4633		 * discard ourselves if we are not needed any more
4634		 * usually happens when running unprivileged
4635		 */
4636		remove_asyncio_reader(reader);
4637		delete_asyncio_reader(reader);
4638		return;
4639	}
4640
4641	cnt = read(reader->fd, buffer, sizeof(buffer));
4642
4643	if (cnt < 0) {
4644		if (errno == ENOBUFS) {
4645			msyslog(LOG_ERR,
4646				"routing socket reports: %m");
4647		} else {
4648			msyslog(LOG_ERR,
4649				"routing socket reports: %m - disabling");
4650			remove_asyncio_reader(reader);
4651			delete_asyncio_reader(reader);
4652		}
4653		return;
4654	}
4655
4656	/*
4657	 * process routing message
4658	 */
4659#ifdef HAVE_RTNETLINK
4660	for (nh = UA_PTR(struct nlmsghdr, buffer);
4661	     NLMSG_OK(nh, cnt);
4662	     nh = NLMSG_NEXT(nh, cnt)) {
4663		msg_type = nh->nlmsg_type;
4664#else
4665	for (p = buffer;
4666	     (p + sizeof(struct rt_msghdr)) <= (buffer + cnt);
4667	     p += rtm.rtm_msglen) {
4668		memcpy(&rtm, p, sizeof(rtm));
4669		if (rtm.rtm_version != RTM_VERSION) {
4670			msyslog(LOG_ERR,
4671				"version mismatch (got %d - expected %d) on routing socket - disabling",
4672				rtm.rtm_version, RTM_VERSION);
4673
4674			remove_asyncio_reader(reader);
4675			delete_asyncio_reader(reader);
4676			return;
4677		}
4678		msg_type = rtm.rtm_type;
4679#endif
4680		switch (msg_type) {
4681#ifdef RTM_NEWADDR
4682		case RTM_NEWADDR:
4683#endif
4684#ifdef RTM_DELADDR
4685		case RTM_DELADDR:
4686#endif
4687#ifdef RTM_ADD
4688		case RTM_ADD:
4689#endif
4690#ifdef RTM_DELETE
4691		case RTM_DELETE:
4692#endif
4693#ifdef RTM_REDIRECT
4694		case RTM_REDIRECT:
4695#endif
4696#ifdef RTM_CHANGE
4697		case RTM_CHANGE:
4698#endif
4699#ifdef RTM_LOSING
4700		case RTM_LOSING:
4701#endif
4702#ifdef RTM_IFINFO
4703		case RTM_IFINFO:
4704#endif
4705#ifdef RTM_IFANNOUNCE
4706		case RTM_IFANNOUNCE:
4707#endif
4708#ifdef RTM_NEWLINK
4709		case RTM_NEWLINK:
4710#endif
4711#ifdef RTM_DELLINK
4712		case RTM_DELLINK:
4713#endif
4714#ifdef RTM_NEWROUTE
4715		case RTM_NEWROUTE:
4716#endif
4717#ifdef RTM_DELROUTE
4718		case RTM_DELROUTE:
4719#endif
4720			/*
4721			 * we are keen on new and deleted addresses and
4722			 * if an interface goes up and down or routing
4723			 * changes
4724			 */
4725			DPRINTF(3, ("routing message op = %d: scheduling interface update\n",
4726				    msg_type));
4727			timer_interfacetimeout(current_time + UPDATE_GRACE);
4728			break;
4729#ifdef HAVE_RTNETLINK
4730		case NLMSG_DONE:
4731			/* end of multipart message */
4732			return;
4733#endif
4734		default:
4735			/*
4736			 * the rest doesn't bother us.
4737			 */
4738			DPRINTF(4, ("routing message op = %d: ignored\n",
4739				    msg_type));
4740			break;
4741		}
4742	}
4743}
4744
4745/*
4746 * set up routing notifications
4747 */
4748static void
4749init_async_notifications()
4750{
4751	struct asyncio_reader *reader;
4752#ifdef HAVE_RTNETLINK
4753	int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
4754	struct sockaddr_nl sa;
4755#else
4756	int fd = socket(PF_ROUTE, SOCK_RAW, 0);
4757#endif
4758	if (fd < 0) {
4759		msyslog(LOG_ERR,
4760			"unable to open routing socket (%m) - using polled interface update");
4761		return;
4762	}
4763
4764	fd = move_fd(fd);
4765#ifdef HAVE_RTNETLINK
4766	ZERO(sa);
4767	sa.nl_family = PF_NETLINK;
4768	sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR
4769		       | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_ROUTE
4770		       | RTMGRP_IPV4_MROUTE | RTMGRP_IPV6_ROUTE
4771		       | RTMGRP_IPV6_MROUTE;
4772	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
4773		msyslog(LOG_ERR,
4774			"bind failed on routing socket (%m) - using polled interface update");
4775		return;
4776	}
4777#endif
4778	make_socket_nonblocking(fd);
4779#if defined(HAVE_SIGNALED_IO)
4780	init_socket_sig(fd);
4781#endif /* HAVE_SIGNALED_IO */
4782
4783	reader = new_asyncio_reader();
4784
4785	reader->fd = fd;
4786	reader->receiver = process_routing_msgs;
4787
4788	add_asyncio_reader(reader, FD_TYPE_SOCKET);
4789	msyslog(LOG_INFO,
4790		"Listening on routing socket on fd #%d for interface updates",
4791		fd);
4792}
4793#else
4794/* HAS_ROUTING_SOCKET not defined */
4795static void
4796init_async_notifications(void)
4797{
4798}
4799#endif
4800
4801