ntp_proto.c revision 358659
1/*
2 * ntp_proto.c - NTP version 4 protocol machinery
3 *
4 * ATTENTION: Get approval from Harlan on all changes to this file!
5 *	    (Harlan will be discussing these changes with Dave Mills.)
6 *
7 */
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11
12#include "ntpd.h"
13#include "ntp_stdlib.h"
14#include "ntp_unixtime.h"
15#include "ntp_control.h"
16#include "ntp_string.h"
17#include "ntp_leapsec.h"
18#include "ntp_psl.h"
19#include "refidsmear.h"
20#include "lib_strbuf.h"
21
22#include <stdio.h>
23#ifdef HAVE_LIBSCF_H
24#include <libscf.h>
25#endif
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29
30/* [Bug 3031] define automatic broadcastdelay cutoff preset */
31#ifndef BDELAY_DEFAULT
32# define BDELAY_DEFAULT (-0.050)
33#endif
34
35#define SRVFUZ_SHIFT	6	/* 64 seconds */
36#define SRVRSP_FUZZ(x)					\
37	do {						\
38		x.l_uf &= 0;				\
39		x.l_ui &= ~((1 << SRVFUZ_SHIFT) - 1U);	\
40	} while(0)
41
42/*
43 * This macro defines the authentication state. If x is 1 authentication
44 * is required; otherwise it is optional.
45 */
46#define	AUTH(x, y)	((x) ? (y) == AUTH_OK \
47			     : (y) == AUTH_OK || (y) == AUTH_NONE)
48
49typedef enum
50auth_state {
51	AUTH_UNKNOWN = -1,	/* Unknown */
52	AUTH_NONE,		/* authentication not required */
53	AUTH_OK,		/* authentication OK */
54	AUTH_ERROR,		/* authentication error */
55	AUTH_CRYPTO		/* crypto_NAK */
56} auth_code;
57
58/*
59 * Set up Kiss Code values
60 */
61
62typedef enum
63kiss_codes {
64	NOKISS,				/* No Kiss Code */
65	RATEKISS,			/* Rate limit Kiss Code */
66	DENYKISS,			/* Deny Kiss */
67	RSTRKISS,			/* Restricted Kiss */
68	XKISS				/* Experimental Kiss */
69} kiss_code;
70
71typedef enum
72nak_error_codes {
73	NONAK,				/* No NAK seen */
74	INVALIDNAK,			/* NAK cannot be used */
75	VALIDNAK			/* NAK is valid */
76} nak_code;
77
78/*
79 * traffic shaping parameters
80 */
81#define	NTP_IBURST	6	/* packets in iburst */
82#define	RESP_DELAY	1	/* refclock burst delay (s) */
83
84/*
85 * pool soliciting restriction duration (s)
86 */
87#define	POOL_SOLICIT_WINDOW	8
88
89/*
90 * flag bits propagated from pool to individual peers
91 */
92#define POOL_FLAG_PMASK		(FLAG_IBURST | FLAG_NOSELECT)
93
94/*
95 * peer_select groups statistics for a peer used by clock_select() and
96 * clock_cluster().
97 */
98typedef struct peer_select_tag {
99	struct peer *	peer;
100	double		synch;	/* sync distance */
101	double		error;	/* jitter */
102	double		seljit;	/* selection jitter */
103} peer_select;
104
105/*
106 * System variables are declared here. Unless specified otherwise, all
107 * times are in seconds.
108 */
109u_char	sys_leap;		/* system leap indicator, use set_sys_leap() to change this */
110u_char	xmt_leap;		/* leap indicator sent in client requests, set up by set_sys_leap() */
111u_char	sys_stratum;		/* system stratum */
112s_char	sys_precision;		/* local clock precision (log2 s) */
113double	sys_rootdelay;		/* roundtrip delay to root (primary source) */
114double	sys_rootdisp;		/* dispersion to root (primary source) */
115double	prev_rootdisp;		/* previous root dispersion */
116double	p2_rootdisp;		/* previous previous root dispersion */
117u_int32 sys_refid;		/* reference id (network byte order) */
118l_fp	sys_reftime;		/* last update time */
119l_fp	prev_reftime;		/* previous sys_reftime */
120l_fp	p2_reftime;		/* previous previous sys_reftime */
121u_long	prev_time;		/* "current_time" when saved prev_time */
122u_long	p2_time;		/* previous prev_time */
123struct	peer *sys_peer;		/* current peer */
124
125#ifdef LEAP_SMEAR
126struct leap_smear_info leap_smear;
127#endif
128int leap_sec_in_progress;
129
130/*
131 * Rate controls. Leaky buckets are used to throttle the packet
132 * transmission rates in order to protect busy servers such as at NIST
133 * and USNO. There is a counter for each association and another for KoD
134 * packets. The association counter decrements each second, but not
135 * below zero. Each time a packet is sent the counter is incremented by
136 * a configurable value representing the average interval between
137 * packets. A packet is delayed as long as the counter is greater than
138 * zero. Note this does not affect the time value computations.
139 */
140/*
141 * Nonspecified system state variables
142 */
143int	sys_bclient;		/* broadcast client enable */
144double	sys_bdelay;		/* broadcast client default delay */
145int	sys_authenticate;	/* requre authentication for config */
146l_fp	sys_authdelay;		/* authentication delay */
147double	sys_offset;	/* current local clock offset */
148double	sys_mindisp = MINDISPERSE; /* minimum distance (s) */
149double	sys_maxdist = MAXDISTANCE; /* selection threshold */
150double	sys_jitter;		/* system jitter */
151u_long	sys_epoch;		/* last clock update time */
152static	double sys_clockhop;	/* clockhop threshold */
153static int leap_vote_ins;	/* leap consensus for insert */
154static int leap_vote_del;	/* leap consensus for delete */
155keyid_t	sys_private;		/* private value for session seed */
156int	sys_manycastserver;	/* respond to manycast client pkts */
157int	ntp_mode7;		/* respond to ntpdc (mode7) */
158int	peer_ntpdate;		/* active peers in ntpdate mode */
159int	sys_survivors;		/* truest of the truechimers */
160char	*sys_ident = NULL;	/* identity scheme */
161
162/*
163 * TOS and multicast mapping stuff
164 */
165int	sys_floor = 0;		/* cluster stratum floor */
166u_char	sys_bcpollbstep = 0;	/* Broadcast Poll backstep gate */
167int	sys_ceiling = STRATUM_UNSPEC - 1; /* cluster stratum ceiling */
168int	sys_minsane = 1;	/* minimum candidates */
169int	sys_minclock = NTP_MINCLOCK; /* minimum candidates */
170int	sys_maxclock = NTP_MAXCLOCK; /* maximum candidates */
171int	sys_cohort = 0;		/* cohort switch */
172int	sys_orphan = STRATUM_UNSPEC + 1; /* orphan stratum */
173int	sys_orphwait = NTP_ORPHWAIT; /* orphan wait */
174int	sys_beacon = BEACON;	/* manycast beacon interval */
175u_int	sys_ttlmax;		/* max ttl mapping vector index */
176u_char	sys_ttl[MAX_TTL];	/* ttl mapping vector */
177
178/*
179 * Statistics counters - first the good, then the bad
180 */
181u_long	sys_stattime;		/* elapsed time */
182u_long	sys_received;		/* packets received */
183u_long	sys_processed;		/* packets for this host */
184u_long	sys_newversion;		/* current version */
185u_long	sys_oldversion;		/* old version */
186u_long	sys_restricted;		/* access denied */
187u_long	sys_badlength;		/* bad length or format */
188u_long	sys_badauth;		/* bad authentication */
189u_long	sys_declined;		/* declined */
190u_long	sys_limitrejected;	/* rate exceeded */
191u_long	sys_kodsent;		/* KoD sent */
192
193/*
194 * Mechanism knobs: how soon do we peer_clear() or unpeer()?
195 *
196 * The default way is "on-receipt".  If this was a packet from a
197 * well-behaved source, on-receipt will offer the fastest recovery.
198 * If this was from a DoS attack, the default way makes it easier
199 * for a bad-guy to DoS us.  So look and see what bites you harder
200 * and choose according to your environment.
201 */
202int peer_clear_digest_early	= 1;	/* bad digest (TEST5) and Autokey */
203int unpeer_crypto_early		= 1;	/* bad crypto (TEST9) */
204int unpeer_crypto_nak_early	= 1;	/* crypto_NAK (TEST5) */
205int unpeer_digest_early		= 1;	/* bad digest (TEST5) */
206
207int dynamic_interleave = DYNAMIC_INTERLEAVE;	/* Bug 2978 mitigation */
208
209int kiss_code_check(u_char hisleap, u_char hisstratum, u_char hismode, u_int32 refid);
210nak_code	valid_NAK	(struct peer *peer, struct recvbuf *rbufp, u_char hismode);
211static	double	root_distance	(struct peer *);
212static	void	clock_combine	(peer_select *, int, int);
213static	void	peer_xmit	(struct peer *);
214static	void	fast_xmit	(struct recvbuf *, int, keyid_t, int);
215static	void	pool_xmit	(struct peer *);
216static	void	clock_update	(struct peer *);
217static	void	measure_precision(void);
218static	double	measure_tick_fuzz(void);
219static	int	local_refid	(struct peer *);
220static	int	peer_unfit	(struct peer *);
221#ifdef AUTOKEY
222static	int	group_test	(char *, char *);
223#endif /* AUTOKEY */
224#ifdef WORKER
225void	pool_name_resolved	(int, int, void *, const char *,
226				 const char *, const struct addrinfo *,
227				 const struct addrinfo *);
228#endif /* WORKER */
229
230const char *	amtoa		(int am);
231
232
233void
234set_sys_leap(
235	u_char new_sys_leap
236	)
237{
238	sys_leap = new_sys_leap;
239	xmt_leap = sys_leap;
240
241	/*
242	 * Under certain conditions we send faked leap bits to clients, so
243	 * eventually change xmt_leap below, but never change LEAP_NOTINSYNC.
244	 */
245	if (xmt_leap != LEAP_NOTINSYNC) {
246		if (leap_sec_in_progress) {
247			/* always send "not sync" */
248			xmt_leap = LEAP_NOTINSYNC;
249		}
250#ifdef LEAP_SMEAR
251		else {
252			/*
253			 * If leap smear is enabled in general we must
254			 * never send a leap second warning to clients,
255			 * so make sure we only send "in sync".
256			 */
257			if (leap_smear.enabled)
258				xmt_leap = LEAP_NOWARNING;
259		}
260#endif	/* LEAP_SMEAR */
261	}
262}
263
264
265/*
266 * Kiss Code check
267 */
268int
269kiss_code_check(
270	u_char hisleap,
271	u_char hisstratum,
272	u_char hismode,
273	u_int32 refid
274	)
275{
276
277	if (   hismode == MODE_SERVER
278	    && hisleap == LEAP_NOTINSYNC
279	    && hisstratum == STRATUM_UNSPEC) {
280		if(memcmp(&refid,"RATE", 4) == 0) {
281			return (RATEKISS);
282		} else if(memcmp(&refid,"DENY", 4) == 0) {
283			return (DENYKISS);
284		} else if(memcmp(&refid,"RSTR", 4) == 0) {
285			return (RSTRKISS);
286		} else if(memcmp(&refid,"X", 1) == 0) {
287			return (XKISS);
288		}
289	}
290	return (NOKISS);
291}
292
293
294/*
295 * Check that NAK is valid
296 */
297nak_code
298valid_NAK(
299	  struct peer *peer,
300	  struct recvbuf *rbufp,
301	  u_char hismode
302	  )
303{
304	int		base_packet_length = MIN_V4_PKT_LEN;
305	int		remainder_size;
306	struct pkt *	rpkt;
307	int		keyid;
308	l_fp		p_org;	/* origin timestamp */
309	const l_fp *	myorg;	/* selected peer origin */
310
311	/*
312	 * Check to see if there is something beyond the basic packet
313	 */
314	if (rbufp->recv_length == base_packet_length) {
315		return NONAK;
316	}
317
318	remainder_size = rbufp->recv_length - base_packet_length;
319	/*
320	 * Is this a potential NAK?
321	 */
322	if (remainder_size != 4) {
323		return NONAK;
324	}
325
326	/*
327	 * Only server responses can contain NAK's
328	 */
329
330	if (hismode != MODE_SERVER &&
331	    hismode != MODE_ACTIVE &&
332	    hismode != MODE_PASSIVE
333	    ) {
334		return INVALIDNAK;
335	}
336
337	/*
338	 * Make sure that the extra field in the packet is all zeros
339	 */
340	rpkt = &rbufp->recv_pkt;
341	keyid = ntohl(((u_int32 *)rpkt)[base_packet_length / 4]);
342	if (keyid != 0) {
343		return INVALIDNAK;
344	}
345
346	/*
347	 * During the first few packets of the autokey dance there will
348	 * not (yet) be a keyid, but in this case FLAG_SKEY is set.
349	 * So the NAK is invalid if either there's no peer, or
350	 * if the keyid is 0 and FLAG_SKEY is not set.
351	 */
352	if (!peer || (!peer->keyid && !(peer->flags & FLAG_SKEY))) {
353		return INVALIDNAK;
354	}
355
356	/*
357	 * The ORIGIN must match, or this cannot be a valid NAK, either.
358	 */
359
360	if (FLAG_LOOPNONCE & peer->flags) {
361		myorg = &peer->nonce;
362	} else {
363		if (peer->flip > 0) {
364			myorg = &peer->borg;
365		} else {
366			myorg = &peer->aorg;
367		}
368	}
369
370	NTOHL_FP(&rpkt->org, &p_org);
371
372	if (L_ISZERO(&p_org) ||
373	    L_ISZERO( myorg) ||
374	    !L_ISEQU(&p_org, myorg)) {
375		return INVALIDNAK;
376	}
377
378	/* If we ever passed all that checks, we should be safe. Well,
379	 * as safe as we can ever be with an unauthenticated crypto-nak.
380	 */
381	return VALIDNAK;
382}
383
384
385/*
386 * transmit - transmit procedure called by poll timeout
387 */
388void
389transmit(
390	struct peer *peer	/* peer structure pointer */
391	)
392{
393	u_char	hpoll;
394
395	/*
396	 * The polling state machine. There are two kinds of machines,
397	 * those that never expect a reply (broadcast and manycast
398	 * server modes) and those that do (all other modes). The dance
399	 * is intricate...
400	 */
401	hpoll = peer->hpoll;
402
403	/*
404	 * If we haven't received anything (even if unsync) since last
405	 * send, reset ppoll.
406	 */
407	if (peer->outdate > peer->timelastrec && !peer->reach)
408		peer->ppoll = peer->maxpoll;
409
410	/*
411	 * In broadcast mode the poll interval is never changed from
412	 * minpoll.
413	 */
414	if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) {
415		peer->outdate = current_time;
416		poll_update(peer, hpoll, 0);
417		if (sys_leap != LEAP_NOTINSYNC)
418			peer_xmit(peer);
419		return;
420	}
421
422	/*
423	 * In manycast mode we start with unity ttl. The ttl is
424	 * increased by one for each poll until either sys_maxclock
425	 * servers have been found or the maximum ttl is reached. When
426	 * sys_maxclock servers are found we stop polling until one or
427	 * more servers have timed out or until less than sys_minclock
428	 * associations turn up. In this case additional better servers
429	 * are dragged in and preempt the existing ones.  Once every
430	 * sys_beacon seconds we are to transmit unconditionally, but
431	 * this code is not quite right -- peer->unreach counts polls
432	 * and is being compared with sys_beacon, so the beacons happen
433	 * every sys_beacon polls.
434	 */
435	if (peer->cast_flags & MDF_ACAST) {
436		peer->outdate = current_time;
437		poll_update(peer, hpoll, 0);
438		if (peer->unreach > sys_beacon) {
439			peer->unreach = 0;
440			peer->ttl = 0;
441			peer_xmit(peer);
442		} else if (   sys_survivors < sys_minclock
443			   || peer_associations < sys_maxclock) {
444			if (peer->ttl < sys_ttlmax)
445				peer->ttl++;
446			peer_xmit(peer);
447		}
448		peer->unreach++;
449		return;
450	}
451
452	/*
453	 * Pool associations transmit unicast solicitations when there
454	 * are less than a hard limit of 2 * sys_maxclock associations,
455	 * and either less than sys_minclock survivors or less than
456	 * sys_maxclock associations.  The hard limit prevents unbounded
457	 * growth in associations if the system clock or network quality
458	 * result in survivor count dipping below sys_minclock often.
459	 * This was observed testing with pool, where sys_maxclock == 12
460	 * resulted in 60 associations without the hard limit.  A
461	 * similar hard limit on manycastclient ephemeral associations
462	 * may be appropriate.
463	 */
464	if (peer->cast_flags & MDF_POOL) {
465		peer->outdate = current_time;
466		poll_update(peer, hpoll, 0);
467		if (   (peer_associations <= 2 * sys_maxclock)
468		    && (   peer_associations < sys_maxclock
469			|| sys_survivors < sys_minclock))
470			pool_xmit(peer);
471		return;
472	}
473
474	/*
475	 * In unicast modes the dance is much more intricate. It is
476	 * designed to back off whenever possible to minimize network
477	 * traffic.
478	 */
479	if (peer->burst == 0) {
480		u_char oreach;
481
482		/*
483		 * Update the reachability status. If not heard for
484		 * three consecutive polls, stuff infinity in the clock
485		 * filter.
486		 */
487		oreach = peer->reach;
488		peer->outdate = current_time;
489		peer->unreach++;
490		peer->reach <<= 1;
491		if (!peer->reach) {
492
493			/*
494			 * Here the peer is unreachable. If it was
495			 * previously reachable raise a trap. Send a
496			 * burst if enabled.
497			 */
498			clock_filter(peer, 0., 0., MAXDISPERSE);
499			if (oreach) {
500				peer_unfit(peer);
501				report_event(PEVNT_UNREACH, peer, NULL);
502			}
503			if (   (peer->flags & FLAG_IBURST)
504			    && peer->retry == 0)
505				peer->retry = NTP_RETRY;
506		} else {
507
508			/*
509			 * Here the peer is reachable. Send a burst if
510			 * enabled and the peer is fit.  Reset unreach
511			 * for persistent and ephemeral associations.
512			 * Unreach is also reset for survivors in
513			 * clock_select().
514			 */
515			hpoll = sys_poll;
516			if (!(peer->flags & FLAG_PREEMPT))
517				peer->unreach = 0;
518			if (   (peer->flags & FLAG_BURST)
519			    && peer->retry == 0
520			    && !peer_unfit(peer))
521				peer->retry = NTP_RETRY;
522		}
523
524		/*
525		 * Watch for timeout.  If ephemeral, toss the rascal;
526		 * otherwise, bump the poll interval. Note the
527		 * poll_update() routine will clamp it to maxpoll.
528		 * If preemptible and we have more peers than maxclock,
529		 * and this peer has the minimum score of preemptibles,
530		 * demobilize.
531		 */
532		if (peer->unreach >= NTP_UNREACH) {
533			hpoll++;
534			/* ephemeral: no FLAG_CONFIG nor FLAG_PREEMPT */
535			if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
536				report_event(PEVNT_RESTART, peer, "timeout");
537				peer_clear(peer, "TIME");
538				unpeer(peer);
539				return;
540			}
541			if (   (peer->flags & FLAG_PREEMPT)
542			    && (peer_associations > sys_maxclock)
543			    && score_all(peer)) {
544				report_event(PEVNT_RESTART, peer, "timeout");
545				peer_clear(peer, "TIME");
546				unpeer(peer);
547				return;
548			}
549		}
550	} else {
551		peer->burst--;
552		if (peer->burst == 0) {
553
554			/*
555			 * If ntpdate mode and the clock has not been
556			 * set and all peers have completed the burst,
557			 * we declare a successful failure.
558			 */
559			if (mode_ntpdate) {
560				peer_ntpdate--;
561				if (peer_ntpdate == 0) {
562					msyslog(LOG_NOTICE,
563					    "ntpd: no servers found");
564					if (!msyslog_term)
565						printf(
566						    "ntpd: no servers found\n");
567					exit (0);
568				}
569			}
570		}
571	}
572	if (peer->retry > 0)
573		peer->retry--;
574
575	/*
576	 * Do not transmit if in broadcast client mode.
577	 */
578	poll_update(peer, hpoll, (peer->hmode == MODE_CLIENT));
579	if (peer->hmode != MODE_BCLIENT)
580		peer_xmit(peer);
581
582	return;
583}
584
585
586const char *
587amtoa(
588	int am
589	)
590{
591	char *bp;
592
593	switch(am) {
594	    case AM_ERR:	return "AM_ERR";
595	    case AM_NOMATCH:	return "AM_NOMATCH";
596	    case AM_PROCPKT:	return "AM_PROCPKT";
597	    case AM_BCST:	return "AM_BCST";
598	    case AM_FXMIT:	return "AM_FXMIT";
599	    case AM_MANYCAST:	return "AM_MANYCAST";
600	    case AM_NEWPASS:	return "AM_NEWPASS";
601	    case AM_NEWBCL:	return "AM_NEWBCL";
602	    case AM_POSSBCL:	return "AM_POSSBCL";
603	    default:
604		LIB_GETBUF(bp);
605		snprintf(bp, LIB_BUFLENGTH, "AM_#%d", am);
606		return bp;
607	}
608}
609
610
611/*
612 * receive - receive procedure called for each packet received
613 */
614void
615receive(
616	struct recvbuf *rbufp
617	)
618{
619	register struct peer *peer;	/* peer structure pointer */
620	register struct pkt *pkt;	/* receive packet pointer */
621	u_char	hisversion;		/* packet version */
622	u_char	hisleap;		/* packet leap indicator */
623	u_char	hismode;		/* packet mode */
624	u_char	hisstratum;		/* packet stratum */
625	r4addr	r4a;			/* address restrictions */
626	u_short	restrict_mask;		/* restrict bits */
627	const char *hm_str;		/* hismode string */
628	const char *am_str;		/* association match string */
629	int	kissCode = NOKISS;	/* Kiss Code */
630	int	has_mac;		/* length of MAC field */
631	int	authlen;		/* offset of MAC field */
632	auth_code is_authentic = AUTH_UNKNOWN;	/* Was AUTH_NONE */
633	nak_code crypto_nak_test;	/* result of crypto-NAK check */
634	int	retcode = AM_NOMATCH;	/* match code */
635	keyid_t	skeyid = 0;		/* key IDs */
636	u_int32	opcode = 0;		/* extension field opcode */
637	sockaddr_u *dstadr_sin;		/* active runway */
638	struct peer *peer2;		/* aux peer structure pointer */
639	endpt	*match_ep;		/* newpeer() local address */
640	l_fp	p_org;			/* origin timestamp */
641	l_fp	p_rec;			/* receive timestamp */
642	l_fp	p_xmt;			/* transmit timestamp */
643#ifdef AUTOKEY
644	char	hostname[NTP_MAXSTRLEN + 1];
645	char	*groupname = NULL;
646	struct autokey *ap;		/* autokey structure pointer */
647	int	rval;			/* cookie snatcher */
648	keyid_t	pkeyid = 0, tkeyid = 0;	/* key IDs */
649#endif	/* AUTOKEY */
650#ifdef HAVE_NTP_SIGND
651	static unsigned char zero_key[16];
652#endif /* HAVE_NTP_SIGND */
653
654	/*
655	 * Note that there are many places we do not call record_raw_stats().
656	 *
657	 * We only want to call it *after* we've sent a response, or perhaps
658	 * when we've decided to drop a packet.
659	 */
660
661	/*
662	 * Monitor the packet and get restrictions. Note that the packet
663	 * length for control and private mode packets must be checked
664	 * by the service routines. Some restrictions have to be handled
665	 * later in order to generate a kiss-o'-death packet.
666	 */
667	/*
668	 * Bogus port check is before anything, since it probably
669	 * reveals a clogging attack. Likewise the mimimum packet size
670	 * of 2 bytes (for mode 6/7) must be checked first.
671	 */
672	sys_received++;
673	if (0 == SRCPORT(&rbufp->recv_srcadr) || rbufp->recv_length < 2) {
674		sys_badlength++;
675		return;				/* bogus port / length */
676	}
677	restrictions(&rbufp->recv_srcadr, &r4a);
678	restrict_mask = r4a.rflags;
679
680	pkt = &rbufp->recv_pkt;
681	hisversion = PKT_VERSION(pkt->li_vn_mode);
682	hismode = (int)PKT_MODE(pkt->li_vn_mode);
683
684	if (restrict_mask & RES_IGNORE) {
685		DPRINTF(2, ("receive: drop: RES_IGNORE\n"));
686		sys_restricted++;
687		return;				/* ignore everything */
688	}
689	if (hismode == MODE_PRIVATE) {
690		if (!ntp_mode7 || (restrict_mask & RES_NOQUERY)) {
691			DPRINTF(2, ("receive: drop: RES_NOQUERY\n"));
692			sys_restricted++;
693			return;			/* no query private */
694		}
695		process_private(rbufp, ((restrict_mask &
696		    RES_NOMODIFY) == 0));
697		return;
698	}
699	if (hismode == MODE_CONTROL) {
700		if (restrict_mask & RES_NOQUERY) {
701			DPRINTF(2, ("receive: drop: RES_NOQUERY\n"));
702			sys_restricted++;
703			return;			/* no query control */
704		}
705		process_control(rbufp, restrict_mask);
706		return;
707	}
708	if (restrict_mask & RES_DONTSERVE) {
709		DPRINTF(2, ("receive: drop: RES_DONTSERVE\n"));
710		sys_restricted++;
711		return;				/* no time serve */
712	}
713
714
715	/* If we arrive here, we should have a standard NTP packet. We
716	 * check that the minimum size is available and fetch some more
717	 * items from the packet once we can be sure they are indeed
718	 * there.
719	 */
720	if (rbufp->recv_length < LEN_PKT_NOMAC) {
721		sys_badlength++;
722		return;				/* bogus length */
723	}
724
725	hisleap = PKT_LEAP(pkt->li_vn_mode);
726	hisstratum = PKT_TO_STRATUM(pkt->stratum);
727	INSIST(0 != hisstratum); /* paranoia check PKT_TO_STRATUM result */
728
729	DPRINTF(1, ("receive: at %ld %s<-%s ippeerlimit %d mode %d iflags %s "
730		    "restrict %s org %#010x.%08x xmt %#010x.%08x\n",
731		    current_time, stoa(&rbufp->dstadr->sin),
732		    stoa(&rbufp->recv_srcadr), r4a.ippeerlimit, hismode,
733		    build_iflags(rbufp->dstadr->flags),
734		    build_rflags(restrict_mask),
735		    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
736		    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
737
738	/*
739	 * This is for testing. If restricted drop ten percent of
740	 * surviving packets.
741	 */
742	if (restrict_mask & RES_FLAKE) {
743		if ((double)ntp_random() / 0x7fffffff < .1) {
744			DPRINTF(2, ("receive: drop: RES_FLAKE\n"));
745			sys_restricted++;
746			return;			/* no flakeway */
747		}
748	}
749
750	/*
751	** Format Layer Checks
752	**
753	** Validate the packet format.  The packet size, packet header,
754	** and any extension field lengths are checked.  We identify
755	** the beginning of the MAC, to identify the upper limit of
756	** of the hash computation.
757	**
758	** In case of a format layer check violation, the packet is
759	** discarded with no further processing.
760	*/
761
762	/*
763	 * Version check must be after the query packets, since they
764	 * intentionally use an early version.
765	 */
766	if (hisversion == NTP_VERSION) {
767		sys_newversion++;		/* new version */
768	} else if (   !(restrict_mask & RES_VERSION)
769		   && hisversion >= NTP_OLDVERSION) {
770		sys_oldversion++;		/* previous version */
771	} else {
772		DPRINTF(2, ("receive: drop: RES_VERSION\n"));
773		sys_badlength++;
774		return;				/* old version */
775	}
776
777	/*
778	 * Figure out his mode and validate the packet. This has some
779	 * legacy raunch that probably should be removed. In very early
780	 * NTP versions mode 0 was equivalent to what later versions
781	 * would interpret as client mode.
782	 */
783	if (hismode == MODE_UNSPEC) {
784		if (hisversion == NTP_OLDVERSION) {
785			hismode = MODE_CLIENT;
786		} else {
787			DPRINTF(2, ("receive: drop: MODE_UNSPEC\n"));
788			sys_badlength++;
789			return;			/* invalid mode */
790		}
791	}
792
793	/*
794	 * Parse the extension field if present. We figure out whether
795	 * an extension field is present by measuring the MAC size. If
796	 * the number of words following the packet header is 0, no MAC
797	 * is present and the packet is not authenticated. If 1, the
798	 * packet is a crypto-NAK; if 3, the packet is authenticated
799	 * with DES; if 5, the packet is authenticated with MD5; if 6,
800	 * the packet is authenticated with SHA. If 2 or * 4, the packet
801	 * is a runt and discarded forthwith. If greater than 6, an
802	 * extension field is present, so we subtract the length of the
803	 * field and go around again.
804	 *
805	 * Note the above description is lame.  We should/could also check
806	 * the two bytes that make up the EF type and subtype, and then
807	 * check the two bytes that tell us the EF length.  A legacy MAC
808	 * has a 4 byte keyID, and for conforming symmetric keys its value
809	 * must be <= 64k, meaning the top two bytes will always be zero.
810	 * Since the EF Type of 0 is reserved/unused, there's no way a
811	 * conforming legacy MAC could ever be misinterpreted as an EF.
812	 *
813	 * There is more, but this isn't the place to document it.
814	 */
815
816	authlen = LEN_PKT_NOMAC;
817	has_mac = rbufp->recv_length - authlen;
818	while (has_mac > 0) {
819		u_int32	len;
820#ifdef AUTOKEY
821		u_int32	hostlen;
822		struct exten *ep;
823#endif /*AUTOKEY */
824
825		if (has_mac % 4 != 0 || has_mac < (int)MIN_MAC_LEN) {
826			DPRINTF(2, ("receive: drop: bad post-packet length\n"));
827			sys_badlength++;
828			return;			/* bad length */
829		}
830		/*
831		 * This next test is clearly wrong - it needlessly
832		 * prohibits short EFs (which don't yet exist)
833		 */
834		if (has_mac <= (int)MAX_MAC_LEN) {
835			skeyid = ntohl(((u_int32 *)pkt)[authlen / 4]);
836			break;
837
838		} else {
839			opcode = ntohl(((u_int32 *)pkt)[authlen / 4]);
840			len = opcode & 0xffff;
841			if (   len % 4 != 0
842			    || len < 4
843			    || (int)len + authlen > rbufp->recv_length) {
844				DPRINTF(2, ("receive: drop: bad EF length\n"));
845				sys_badlength++;
846				return;		/* bad length */
847			}
848#ifdef AUTOKEY
849			/*
850			 * Extract calling group name for later.  If
851			 * sys_groupname is non-NULL, there must be
852			 * a group name provided to elicit a response.
853			 */
854			if (   (opcode & 0x3fff0000) == CRYPTO_ASSOC
855			    && sys_groupname != NULL) {
856				ep = (struct exten *)&((u_int32 *)pkt)[authlen / 4];
857				hostlen = ntohl(ep->vallen);
858				if (   hostlen >= sizeof(hostname)
859				    || hostlen > len -
860						offsetof(struct exten, pkt)) {
861					DPRINTF(2, ("receive: drop: bad autokey hostname length\n"));
862					sys_badlength++;
863					return;		/* bad length */
864				}
865				memcpy(hostname, &ep->pkt, hostlen);
866				hostname[hostlen] = '\0';
867				groupname = strchr(hostname, '@');
868				if (groupname == NULL) {
869					DPRINTF(2, ("receive: drop: empty autokey groupname\n"));
870					sys_declined++;
871					return;
872				}
873				groupname++;
874			}
875#endif /* AUTOKEY */
876			authlen += len;
877			has_mac -= len;
878		}
879	}
880
881	/*
882	 * If has_mac is < 0 we had a malformed packet.
883	 */
884	if (has_mac < 0) {
885		DPRINTF(2, ("receive: drop: post-packet under-read\n"));
886		sys_badlength++;
887		return;		/* bad length */
888	}
889
890	/*
891	** Packet Data Verification Layer
892	**
893	** This layer verifies the packet data content.  If
894	** authentication is required, a MAC must be present.
895	** If a MAC is present, it must validate.
896	** Crypto-NAK?  Look - a shiny thing!
897	**
898	** If authentication fails, we're done.
899	*/
900
901	/*
902	 * If authentication is explicitly required, a MAC must be present.
903	 */
904	if (restrict_mask & RES_DONTTRUST && has_mac == 0) {
905		DPRINTF(2, ("receive: drop: RES_DONTTRUST\n"));
906		sys_restricted++;
907		return;				/* access denied */
908	}
909
910	/*
911	 * Update the MRU list and finger the cloggers. It can be a
912	 * little expensive, so turn it off for production use.
913	 * RES_LIMITED and RES_KOD will be cleared in the returned
914	 * restrict_mask unless one or both actions are warranted.
915	 */
916	restrict_mask = ntp_monitor(rbufp, restrict_mask);
917	if (restrict_mask & RES_LIMITED) {
918		sys_limitrejected++;
919		if (   !(restrict_mask & RES_KOD)
920		    || MODE_BROADCAST == hismode
921		    || MODE_SERVER == hismode) {
922			if (MODE_SERVER == hismode) {
923				DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
924					stoa(&rbufp->recv_srcadr)));
925			} else {
926				DPRINTF(2, ("receive: drop: RES_KOD\n"));
927			}
928			return;			/* rate exceeded */
929		}
930		if (hismode == MODE_CLIENT) {
931			fast_xmit(rbufp, MODE_SERVER, skeyid,
932			    restrict_mask);
933		} else {
934			fast_xmit(rbufp, MODE_ACTIVE, skeyid,
935			    restrict_mask);
936		}
937		return;				/* rate exceeded */
938	}
939	restrict_mask &= ~RES_KOD;
940
941	/*
942	 * We have tossed out as many buggy packets as possible early in
943	 * the game to reduce the exposure to a clogging attack. Now we
944	 * have to burn some cycles to find the association and
945	 * authenticate the packet if required. Note that we burn only
946	 * digest cycles, again to reduce exposure. There may be no
947	 * matching association and that's okay.
948	 *
949	 * More on the autokey mambo. Normally the local interface is
950	 * found when the association was mobilized with respect to a
951	 * designated remote address. We assume packets arriving from
952	 * the remote address arrive via this interface and the local
953	 * address used to construct the autokey is the unicast address
954	 * of the interface. However, if the sender is a broadcaster,
955	 * the interface broadcast address is used instead.
956	 * Notwithstanding this technobabble, if the sender is a
957	 * multicaster, the broadcast address is null, so we use the
958	 * unicast address anyway. Don't ask.
959	 */
960
961	peer = findpeer(rbufp,  hismode, &retcode);
962	dstadr_sin = &rbufp->dstadr->sin;
963	NTOHL_FP(&pkt->org, &p_org);
964	NTOHL_FP(&pkt->rec, &p_rec);
965	NTOHL_FP(&pkt->xmt, &p_xmt);
966	hm_str = modetoa(hismode);
967	am_str = amtoa(retcode);
968
969	/*
970	 * Authentication is conditioned by three switches:
971	 *
972	 * NOPEER  (RES_NOPEER) do not mobilize an association unless
973	 *         authenticated
974	 * NOTRUST (RES_DONTTRUST) do not allow access unless
975	 *         authenticated (implies NOPEER)
976	 * enable  (sys_authenticate) master NOPEER switch, by default
977	 *         on
978	 *
979	 * The NOPEER and NOTRUST can be specified on a per-client basis
980	 * using the restrict command. The enable switch if on implies
981	 * NOPEER for all clients. There are four outcomes:
982	 *
983	 * NONE    The packet has no MAC.
984	 * OK      the packet has a MAC and authentication succeeds
985	 * ERROR   the packet has a MAC and authentication fails
986	 * CRYPTO  crypto-NAK. The MAC has four octets only.
987	 *
988	 * Note: The AUTH(x, y) macro is used to filter outcomes. If x
989	 * is zero, acceptable outcomes of y are NONE and OK. If x is
990	 * one, the only acceptable outcome of y is OK.
991	 */
992	crypto_nak_test = valid_NAK(peer, rbufp, hismode);
993
994	/*
995	 * Drop any invalid crypto-NAKs
996	 */
997	if (crypto_nak_test == INVALIDNAK) {
998		report_event(PEVNT_AUTH, peer, "Invalid_NAK");
999		if (0 != peer) {
1000			peer->badNAK++;
1001		}
1002		msyslog(LOG_ERR, "Invalid-NAK error at %ld %s<-%s",
1003			current_time, stoa(dstadr_sin), stoa(&rbufp->recv_srcadr));
1004		return;
1005	}
1006
1007	if (has_mac == 0) {
1008		restrict_mask &= ~RES_MSSNTP;
1009		is_authentic = AUTH_NONE; /* not required */
1010		DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x NOMAC\n",
1011			    current_time, stoa(dstadr_sin),
1012			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1013			    authlen,
1014			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1015			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1016	} else if (crypto_nak_test == VALIDNAK) {
1017		restrict_mask &= ~RES_MSSNTP;
1018		is_authentic = AUTH_CRYPTO; /* crypto-NAK */
1019		DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x CRYPTONAK\n",
1020			    current_time, stoa(dstadr_sin),
1021			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1022			    skeyid, authlen + has_mac, is_authentic,
1023			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1024			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1025
1026#ifdef HAVE_NTP_SIGND
1027		/*
1028		 * If the signature is 20 bytes long, the last 16 of
1029		 * which are zero, then this is a Microsoft client
1030		 * wanting AD-style authentication of the server's
1031		 * reply.
1032		 *
1033		 * This is described in Microsoft's WSPP docs, in MS-SNTP:
1034		 * http://msdn.microsoft.com/en-us/library/cc212930.aspx
1035		 */
1036	} else if (   has_mac == MAX_MD5_LEN
1037		   && (restrict_mask & RES_MSSNTP)
1038		   && (retcode == AM_FXMIT || retcode == AM_NEWPASS)
1039		   && (memcmp(zero_key, (char *)pkt + authlen + 4,
1040			      MAX_MD5_LEN - 4) == 0)) {
1041		is_authentic = AUTH_NONE;
1042		DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x SIGND\n",
1043			    current_time, stoa(dstadr_sin),
1044			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1045			    authlen,
1046			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1047			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1048#endif /* HAVE_NTP_SIGND */
1049
1050	} else {
1051		/*
1052		 * has_mac is not 0
1053		 * Not a VALID_NAK
1054		 * Not an MS-SNTP SIGND packet
1055		 *
1056		 * So there is a MAC here.
1057		 */
1058
1059		restrict_mask &= ~RES_MSSNTP;
1060#ifdef AUTOKEY
1061		/*
1062		 * For autokey modes, generate the session key
1063		 * and install in the key cache. Use the socket
1064		 * broadcast or unicast address as appropriate.
1065		 */
1066		if (crypto_flags && skeyid > NTP_MAXKEY) {
1067
1068			/*
1069			 * More on the autokey dance (AKD). A cookie is
1070			 * constructed from public and private values.
1071			 * For broadcast packets, the cookie is public
1072			 * (zero). For packets that match no
1073			 * association, the cookie is hashed from the
1074			 * addresses and private value. For server
1075			 * packets, the cookie was previously obtained
1076			 * from the server. For symmetric modes, the
1077			 * cookie was previously constructed using an
1078			 * agreement protocol; however, should PKI be
1079			 * unavailable, we construct a fake agreement as
1080			 * the EXOR of the peer and host cookies.
1081			 *
1082			 * hismode	ephemeral	persistent
1083			 * =======================================
1084			 * active	0		cookie#
1085			 * passive	0%		cookie#
1086			 * client	sys cookie	0%
1087			 * server	0%		sys cookie
1088			 * broadcast	0		0
1089			 *
1090			 * # if unsync, 0
1091			 * % can't happen
1092			 */
1093			if (has_mac < (int)MAX_MD5_LEN) {
1094				DPRINTF(2, ("receive: drop: MD5 digest too short\n"));
1095				sys_badauth++;
1096				return;
1097			}
1098			if (hismode == MODE_BROADCAST) {
1099
1100				/*
1101				 * For broadcaster, use the interface
1102				 * broadcast address when available;
1103				 * otherwise, use the unicast address
1104				 * found when the association was
1105				 * mobilized. However, if this is from
1106				 * the wildcard interface, game over.
1107				 */
1108				if (   crypto_flags
1109				    && rbufp->dstadr ==
1110				       ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr)) {
1111					DPRINTF(2, ("receive: drop: BCAST from wildcard\n"));
1112					sys_restricted++;
1113					return;		/* no wildcard */
1114				}
1115				pkeyid = 0;
1116				if (!SOCK_UNSPEC(&rbufp->dstadr->bcast))
1117					dstadr_sin =
1118					    &rbufp->dstadr->bcast;
1119			} else if (peer == NULL) {
1120				pkeyid = session_key(
1121				    &rbufp->recv_srcadr, dstadr_sin, 0,
1122				    sys_private, 0);
1123			} else {
1124				pkeyid = peer->pcookie;
1125			}
1126
1127			/*
1128			 * The session key includes both the public
1129			 * values and cookie. In case of an extension
1130			 * field, the cookie used for authentication
1131			 * purposes is zero. Note the hash is saved for
1132			 * use later in the autokey mambo.
1133			 */
1134			if (authlen > (int)LEN_PKT_NOMAC && pkeyid != 0) {
1135				session_key(&rbufp->recv_srcadr,
1136				    dstadr_sin, skeyid, 0, 2);
1137				tkeyid = session_key(
1138				    &rbufp->recv_srcadr, dstadr_sin,
1139				    skeyid, pkeyid, 0);
1140			} else {
1141				tkeyid = session_key(
1142				    &rbufp->recv_srcadr, dstadr_sin,
1143				    skeyid, pkeyid, 2);
1144			}
1145
1146		}
1147#endif	/* AUTOKEY */
1148
1149		/*
1150		 * Compute the cryptosum. Note a clogging attack may
1151		 * succeed in bloating the key cache. If an autokey,
1152		 * purge it immediately, since we won't be needing it
1153		 * again. If the packet is authentic, it can mobilize an
1154		 * association. Note that there is no key zero.
1155		 */
1156		if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1157		    has_mac))
1158			is_authentic = AUTH_ERROR;
1159		else
1160			is_authentic = AUTH_OK;
1161#ifdef AUTOKEY
1162		if (crypto_flags && skeyid > NTP_MAXKEY)
1163			authtrust(skeyid, 0);
1164#endif	/* AUTOKEY */
1165		DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x MAC\n",
1166			    current_time, stoa(dstadr_sin),
1167			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1168			    skeyid, authlen + has_mac, is_authentic,
1169			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1170			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1171	}
1172
1173
1174	/*
1175	 * Bug 3454:
1176	 *
1177	 * Now come at this from a different perspective:
1178	 * - If we expect a MAC and it's not there, we drop it.
1179	 * - If we expect one keyID and get another, we drop it.
1180	 * - If we have a MAC ahd it hasn't been validated yet, try.
1181	 * - if the provided MAC doesn't validate, we drop it.
1182	 *
1183	 * There might be more to this.
1184	 */
1185	if (0 != peer && 0 != peer->keyid) {
1186		/* Should we msyslog() any of these? */
1187
1188		/*
1189		 * This should catch:
1190		 * - no keyID where one is expected,
1191		 * - different keyID than what we expect.
1192		 */
1193		if (peer->keyid != skeyid) {
1194			DPRINTF(2, ("receive: drop: Wanted keyID %d, got %d from %s\n",
1195				    peer->keyid, skeyid,
1196				    stoa(&rbufp->recv_srcadr)));
1197			sys_restricted++;
1198			return;			/* drop: access denied */
1199		}
1200
1201		/*
1202		 * if has_mac != 0 ...
1203		 * - If it has not yet been validated, do so.
1204		 *   (under what circumstances might that happen?)
1205		 * - if missing or bad MAC, log and drop.
1206		 */
1207		if (0 != has_mac) {
1208			if (is_authentic == AUTH_UNKNOWN) {
1209				/* How can this happen? */
1210				DPRINTF(2, ("receive: 3454 check: AUTH_UNKNOWN from %s\n",
1211				    stoa(&rbufp->recv_srcadr)));
1212				if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1213				    has_mac)) {
1214					/* MAC invalid or not found */
1215					is_authentic = AUTH_ERROR;
1216				} else {
1217					is_authentic = AUTH_OK;
1218				}
1219			}
1220			if (is_authentic != AUTH_OK) {
1221				DPRINTF(2, ("receive: drop: missing or bad MAC from %s\n",
1222					    stoa(&rbufp->recv_srcadr)));
1223				sys_restricted++;
1224				return;		/* drop: access denied */
1225			}
1226		}
1227	}
1228	/**/
1229
1230	/*
1231	** On-Wire Protocol Layer
1232	**
1233	** Verify protocol operations consistent with the on-wire protocol.
1234	** The protocol discards bogus and duplicate packets as well as
1235	** minimizes disruptions doe to protocol restarts and dropped
1236	** packets.  The operations are controlled by two timestamps:
1237	** the transmit timestamp saved in the client state variables,
1238	** and the origin timestamp in the server packet header.  The
1239	** comparison of these two timestamps is called the loopback test.
1240	** The transmit timestamp functions as a nonce to verify that the
1241	** response corresponds to the original request.  The transmit
1242	** timestamp also serves to discard replays of the most recent
1243	** packet.  Upon failure of either test, the packet is discarded
1244	** with no further action.
1245	*/
1246
1247	/*
1248	 * The association matching rules are implemented by a set of
1249	 * routines and an association table. A packet matching an
1250	 * association is processed by the peer process for that
1251	 * association. If there are no errors, an ephemeral association
1252	 * is mobilized: a broadcast packet mobilizes a broadcast client
1253	 * aassociation; a manycast server packet mobilizes a manycast
1254	 * client association; a symmetric active packet mobilizes a
1255	 * symmetric passive association.
1256	 */
1257	DPRINTF(1, ("receive: MATCH_ASSOC dispatch: mode %d/%s:%s \n",
1258		hismode, hm_str, am_str));
1259	switch (retcode) {
1260
1261	/*
1262	 * This is a client mode packet not matching any association. If
1263	 * an ordinary client, simply toss a server mode packet back
1264	 * over the fence. If a manycast client, we have to work a
1265	 * little harder.
1266	 *
1267	 * There are cases here where we do not call record_raw_stats().
1268	 */
1269	case AM_FXMIT:
1270
1271		/*
1272		 * If authentication OK, send a server reply; otherwise,
1273		 * send a crypto-NAK.
1274		 */
1275		if (!(rbufp->dstadr->flags & INT_MCASTOPEN)) {
1276			/* HMS: would be nice to log FAST_XMIT|BADAUTH|RESTRICTED */
1277			record_raw_stats(&rbufp->recv_srcadr,
1278			    &rbufp->dstadr->sin,
1279			    &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1280			    PKT_LEAP(pkt->li_vn_mode),
1281			    PKT_VERSION(pkt->li_vn_mode),
1282			    PKT_MODE(pkt->li_vn_mode),
1283			    PKT_TO_STRATUM(pkt->stratum),
1284			    pkt->ppoll,
1285			    pkt->precision,
1286			    FPTOD(NTOHS_FP(pkt->rootdelay)),
1287			    FPTOD(NTOHS_FP(pkt->rootdisp)),
1288			    pkt->refid,
1289			    rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1290
1291			if (AUTH(restrict_mask & RES_DONTTRUST,
1292			   is_authentic)) {
1293				/* Bug 3596: Do we want to fuzz the reftime? */
1294				fast_xmit(rbufp, MODE_SERVER, skeyid,
1295				    restrict_mask);
1296			} else if (is_authentic == AUTH_ERROR) {
1297				/* Bug 3596: Do we want to fuzz the reftime? */
1298				fast_xmit(rbufp, MODE_SERVER, 0,
1299				    restrict_mask);
1300				sys_badauth++;
1301			} else {
1302				DPRINTF(2, ("receive: AM_FXMIT drop: !mcast restricted\n"));
1303				sys_restricted++;
1304			}
1305
1306			return;			/* hooray */
1307		}
1308
1309		/*
1310		 * This must be manycast. Do not respond if not
1311		 * configured as a manycast server.
1312		 */
1313		if (!sys_manycastserver) {
1314			DPRINTF(2, ("receive: AM_FXMIT drop: Not manycastserver\n"));
1315			sys_restricted++;
1316			return;			/* not enabled */
1317		}
1318
1319#ifdef AUTOKEY
1320		/*
1321		 * Do not respond if not the same group.
1322		 */
1323		if (group_test(groupname, NULL)) {
1324			DPRINTF(2, ("receive: AM_FXMIT drop: empty groupname\n"));
1325			sys_declined++;
1326			return;
1327		}
1328#endif /* AUTOKEY */
1329
1330		/*
1331		 * Do not respond if we are not synchronized or our
1332		 * stratum is greater than the manycaster or the
1333		 * manycaster has already synchronized to us.
1334		 */
1335		if (   sys_leap == LEAP_NOTINSYNC
1336		    || sys_stratum >= hisstratum
1337		    || (!sys_cohort && sys_stratum == hisstratum + 1)
1338		    || rbufp->dstadr->addr_refid == pkt->refid) {
1339			DPRINTF(2, ("receive: AM_FXMIT drop: LEAP_NOTINSYNC || stratum || loop\n"));
1340			sys_declined++;
1341			return;			/* no help */
1342		}
1343
1344		/*
1345		 * Respond only if authentication succeeds. Don't do a
1346		 * crypto-NAK, as that would not be useful.
1347		 */
1348		if (AUTH(restrict_mask & RES_DONTTRUST, is_authentic)) {
1349			record_raw_stats(&rbufp->recv_srcadr,
1350			    &rbufp->dstadr->sin,
1351			    &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1352			    PKT_LEAP(pkt->li_vn_mode),
1353			    PKT_VERSION(pkt->li_vn_mode),
1354			    PKT_MODE(pkt->li_vn_mode),
1355			    PKT_TO_STRATUM(pkt->stratum),
1356			    pkt->ppoll,
1357			    pkt->precision,
1358			    FPTOD(NTOHS_FP(pkt->rootdelay)),
1359			    FPTOD(NTOHS_FP(pkt->rootdisp)),
1360			    pkt->refid,
1361			    rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1362
1363			/* Bug 3596: Do we want to fuzz the reftime? */
1364			fast_xmit(rbufp, MODE_SERVER, skeyid,
1365			    restrict_mask);
1366		}
1367		return;				/* hooray */
1368
1369	/*
1370	 * This is a server mode packet returned in response to a client
1371	 * mode packet sent to a multicast group address (for
1372	 * manycastclient) or to a unicast address (for pool). The
1373	 * origin timestamp is a good nonce to reliably associate the
1374	 * reply with what was sent. If there is no match, that's
1375	 * curious and could be an intruder attempting to clog, so we
1376	 * just ignore it.
1377	 *
1378	 * If the packet is authentic and the manycastclient or pool
1379	 * association is found, we mobilize a client association and
1380	 * copy pertinent variables from the manycastclient or pool
1381	 * association to the new client association. If not, just
1382	 * ignore the packet.
1383	 *
1384	 * There is an implosion hazard at the manycast client, since
1385	 * the manycast servers send the server packet immediately. If
1386	 * the guy is already here, don't fire up a duplicate.
1387	 *
1388	 * There are cases here where we do not call record_raw_stats().
1389	 */
1390	case AM_MANYCAST:
1391
1392#ifdef AUTOKEY
1393		/*
1394		 * Do not respond if not the same group.
1395		 */
1396		if (group_test(groupname, NULL)) {
1397			DPRINTF(2, ("receive: AM_MANYCAST drop: empty groupname\n"));
1398			sys_declined++;
1399			return;
1400		}
1401#endif /* AUTOKEY */
1402		if ((peer2 = findmanycastpeer(rbufp)) == NULL) {
1403			DPRINTF(2, ("receive: AM_MANYCAST drop: No manycast peer\n"));
1404			sys_restricted++;
1405			return;			/* not enabled */
1406		}
1407		if (!AUTH(  (!(peer2->cast_flags & MDF_POOL)
1408			     && sys_authenticate)
1409			  || (restrict_mask & (RES_NOPEER |
1410			      RES_DONTTRUST)), is_authentic)
1411		    /* MC: RES_NOEPEER? */
1412		   ) {
1413			DPRINTF(2, ("receive: AM_MANYCAST drop: bad auth || (NOPEER|DONTTRUST)\n"));
1414			sys_restricted++;
1415			return;			/* access denied */
1416		}
1417
1418		/*
1419		 * Do not respond if unsynchronized or stratum is below
1420		 * the floor or at or above the ceiling.
1421		 */
1422		if (   hisleap == LEAP_NOTINSYNC
1423		    || hisstratum < sys_floor
1424		    || hisstratum >= sys_ceiling) {
1425			DPRINTF(2, ("receive: AM_MANYCAST drop: unsync/stratum\n"));
1426			sys_declined++;
1427			return;			/* no help */
1428		}
1429		peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1430			       r4a.ippeerlimit, MODE_CLIENT, hisversion,
1431			       peer2->minpoll, peer2->maxpoll,
1432			       (FLAG_PREEMPT | (POOL_FLAG_PMASK & peer2->flags)),
1433			       (MDF_UCAST | MDF_UCLNT), 0, skeyid, sys_ident);
1434		if (NULL == peer) {
1435			DPRINTF(2, ("receive: AM_MANYCAST drop: duplicate\n"));
1436			sys_declined++;
1437			return;			/* ignore duplicate */
1438		}
1439
1440		/*
1441		 * After each ephemeral pool association is spun,
1442		 * accelerate the next poll for the pool solicitor so
1443		 * the pool will fill promptly.
1444		 */
1445		if (peer2->cast_flags & MDF_POOL)
1446			peer2->nextdate = current_time + 1;
1447
1448		/*
1449		 * Further processing of the solicitation response would
1450		 * simply detect its origin timestamp as bogus for the
1451		 * brand-new association (it matches the prototype
1452		 * association) and tinker with peer->nextdate delaying
1453		 * first sync.
1454		 */
1455		return;		/* solicitation response handled */
1456
1457	/*
1458	 * This is the first packet received from a broadcast server. If
1459	 * the packet is authentic and we are enabled as broadcast
1460	 * client, mobilize a broadcast client association. We don't
1461	 * kiss any frogs here.
1462	 *
1463	 * There are cases here where we do not call record_raw_stats().
1464	 */
1465	case AM_NEWBCL:
1466
1467#ifdef AUTOKEY
1468		/*
1469		 * Do not respond if not the same group.
1470		 */
1471		if (group_test(groupname, sys_ident)) {
1472			DPRINTF(2, ("receive: AM_NEWBCL drop: groupname mismatch\n"));
1473			sys_declined++;
1474			return;
1475		}
1476#endif /* AUTOKEY */
1477		if (sys_bclient == 0) {
1478			DPRINTF(2, ("receive: AM_NEWBCL drop: not a bclient\n"));
1479			sys_restricted++;
1480			return;			/* not enabled */
1481		}
1482		if (!AUTH(sys_authenticate | (restrict_mask &
1483			  (RES_NOPEER | RES_DONTTRUST)), is_authentic)
1484		    /* NEWBCL: RES_NOEPEER? */
1485		   ) {
1486			DPRINTF(2, ("receive: AM_NEWBCL drop: AUTH failed\n"));
1487			sys_restricted++;
1488			return;			/* access denied */
1489		}
1490
1491		/*
1492		 * Do not respond if unsynchronized or stratum is below
1493		 * the floor or at or above the ceiling.
1494		 */
1495		if (   hisleap == LEAP_NOTINSYNC
1496		    || hisstratum < sys_floor
1497		    || hisstratum >= sys_ceiling) {
1498			DPRINTF(2, ("receive: AM_NEWBCL drop: Unsync or bad stratum\n"));
1499			sys_declined++;
1500			return;			/* no help */
1501		}
1502
1503#ifdef AUTOKEY
1504		/*
1505		 * Do not respond if Autokey and the opcode is not a
1506		 * CRYPTO_ASSOC response with association ID.
1507		 */
1508		if (   crypto_flags && skeyid > NTP_MAXKEY
1509		    && (opcode & 0xffff0000) != (CRYPTO_ASSOC | CRYPTO_RESP)) {
1510			DPRINTF(2, ("receive: AM_NEWBCL drop: Autokey but not CRYPTO_ASSOC\n"));
1511			sys_declined++;
1512			return;			/* protocol error */
1513		}
1514#endif	/* AUTOKEY */
1515
1516		/*
1517		 * Broadcasts received via a multicast address may
1518		 * arrive after a unicast volley has begun
1519		 * with the same remote address.  newpeer() will not
1520		 * find duplicate associations on other local endpoints
1521		 * if a non-NULL endpoint is supplied.  multicastclient
1522		 * ephemeral associations are unique across all local
1523		 * endpoints.
1524		 */
1525		if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
1526			match_ep = rbufp->dstadr;
1527		else
1528			match_ep = NULL;
1529
1530		/*
1531		 * Determine whether to execute the initial volley.
1532		 */
1533		if (sys_bdelay > 0.0) {
1534#ifdef AUTOKEY
1535			/*
1536			 * If a two-way exchange is not possible,
1537			 * neither is Autokey.
1538			 */
1539			if (crypto_flags && skeyid > NTP_MAXKEY) {
1540				sys_restricted++;
1541				DPRINTF(2, ("receive: AM_NEWBCL drop: Autokey but not 2-way\n"));
1542				return;		/* no autokey */
1543			}
1544#endif	/* AUTOKEY */
1545
1546			/*
1547			 * Do not execute the volley. Start out in
1548			 * broadcast client mode.
1549			 */
1550			peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1551			    r4a.ippeerlimit, MODE_BCLIENT, hisversion,
1552			    pkt->ppoll, pkt->ppoll,
1553			    FLAG_PREEMPT, MDF_BCLNT, 0, skeyid, sys_ident);
1554			if (NULL == peer) {
1555				DPRINTF(2, ("receive: AM_NEWBCL drop: duplicate\n"));
1556				sys_restricted++;
1557				return;		/* ignore duplicate */
1558
1559			} else {
1560				peer->delay = sys_bdelay;
1561				peer->bxmt = p_xmt;
1562			}
1563			break;
1564		}
1565
1566		/*
1567		 * Execute the initial volley in order to calibrate the
1568		 * propagation delay and run the Autokey protocol.
1569		 *
1570		 * Note that the minpoll is taken from the broadcast
1571		 * packet, normally 6 (64 s) and that the poll interval
1572		 * is fixed at this value.
1573		 */
1574		peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1575			       r4a.ippeerlimit, MODE_CLIENT, hisversion,
1576			       pkt->ppoll, pkt->ppoll,
1577			       FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
1578			       0, skeyid, sys_ident);
1579		if (NULL == peer) {
1580			DPRINTF(2, ("receive: AM_NEWBCL drop: empty newpeer() failed\n"));
1581			sys_restricted++;
1582			return;			/* ignore duplicate */
1583		}
1584		peer->bxmt = p_xmt;
1585#ifdef AUTOKEY
1586		if (skeyid > NTP_MAXKEY)
1587			crypto_recv(peer, rbufp);
1588#endif	/* AUTOKEY */
1589
1590		return;				/* hooray */
1591
1592	/*
1593	 * This is the first packet received from a potential ephemeral
1594	 * symmetric active peer.  First, deal with broken Windows clients.
1595	 * Then, if NOEPEER is enabled, drop it.  If the packet meets our
1596	 * authenticty requirements and is the first he sent, mobilize
1597	 * a passive association.
1598	 * Otherwise, kiss the frog.
1599	 *
1600	 * There are cases here where we do not call record_raw_stats().
1601	 */
1602	case AM_NEWPASS:
1603
1604		DEBUG_REQUIRE(MODE_ACTIVE == hismode);
1605
1606#ifdef AUTOKEY
1607		/*
1608		 * Do not respond if not the same group.
1609		 */
1610		if (group_test(groupname, sys_ident)) {
1611			DPRINTF(2, ("receive: AM_NEWPASS drop: Autokey group mismatch\n"));
1612			sys_declined++;
1613			return;
1614		}
1615#endif /* AUTOKEY */
1616		if (!AUTH(sys_authenticate | (restrict_mask &
1617			  (RES_NOPEER | RES_DONTTRUST)), is_authentic)
1618		   ) {
1619			/*
1620			 * If authenticated but cannot mobilize an
1621			 * association, send a symmetric passive
1622			 * response without mobilizing an association.
1623			 * This is for drat broken Windows clients. See
1624			 * Microsoft KB 875424 for preferred workaround.
1625			 */
1626			if (AUTH(restrict_mask & RES_DONTTRUST,
1627				 is_authentic)) {
1628				fast_xmit(rbufp, MODE_PASSIVE, skeyid,
1629				    restrict_mask);
1630				return;			/* hooray */
1631			}
1632			/* HMS: Why is this next set of lines a feature? */
1633			if (is_authentic == AUTH_ERROR) {
1634				fast_xmit(rbufp, MODE_PASSIVE, 0,
1635				    restrict_mask);
1636				sys_restricted++;
1637				return;
1638			}
1639
1640			if (restrict_mask & RES_NOEPEER) {
1641				DPRINTF(2, ("receive: AM_NEWPASS drop: NOEPEER\n"));
1642				sys_declined++;
1643				return;
1644			}
1645
1646			/* [Bug 2941]
1647			 * If we got here, the packet isn't part of an
1648			 * existing association, either isn't correctly
1649			 * authenticated or it is but we are refusing
1650			 * ephemeral peer requests, and it didn't meet
1651			 * either of the previous two special cases so we
1652			 * should just drop it on the floor.  For example,
1653			 * crypto-NAKs (is_authentic == AUTH_CRYPTO)
1654			 * will make it this far.  This is just
1655			 * debug-printed and not logged to avoid log
1656			 * flooding.
1657			 */
1658			DPRINTF(2, ("receive: at %ld refusing to mobilize passive association"
1659				    " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1660				    current_time, stoa(&rbufp->recv_srcadr),
1661				    hismode, hm_str, am_str, skeyid,
1662				    (authlen + has_mac), is_authentic));
1663			sys_declined++;
1664			return;
1665		}
1666
1667		if (restrict_mask & RES_NOEPEER) {
1668			DPRINTF(2, ("receive: AM_NEWPASS drop: NOEPEER\n"));
1669			sys_declined++;
1670			return;
1671		}
1672
1673		/*
1674		 * Do not respond if synchronized and if stratum is
1675		 * below the floor or at or above the ceiling. Note,
1676		 * this allows an unsynchronized peer to synchronize to
1677		 * us. It would be very strange if he did and then was
1678		 * nipped, but that could only happen if we were
1679		 * operating at the top end of the range.  It also means
1680		 * we will spin an ephemeral association in response to
1681		 * MODE_ACTIVE KoDs, which will time out eventually.
1682		 */
1683		if (   hisleap != LEAP_NOTINSYNC
1684		    && (hisstratum < sys_floor || hisstratum >= sys_ceiling)) {
1685			DPRINTF(2, ("receive: AM_NEWPASS drop: Autokey group mismatch\n"));
1686			sys_declined++;
1687			return;			/* no help */
1688		}
1689
1690		/*
1691		 * The message is correctly authenticated and allowed.
1692		 * Mobilize a symmetric passive association, if we won't
1693		 * exceed the ippeerlimit.
1694		 */
1695		if ((peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1696				    r4a.ippeerlimit, MODE_PASSIVE, hisversion,
1697				    pkt->ppoll, NTP_MAXDPOLL, 0, MDF_UCAST, 0,
1698				    skeyid, sys_ident)) == NULL) {
1699			DPRINTF(2, ("receive: AM_NEWPASS drop: newpeer() failed\n"));
1700			sys_declined++;
1701			return;			/* ignore duplicate */
1702		}
1703		break;
1704
1705
1706	/*
1707	 * Process regular packet. Nothing special.
1708	 *
1709	 * There are cases here where we do not call record_raw_stats().
1710	 */
1711	case AM_PROCPKT:
1712
1713#ifdef AUTOKEY
1714		/*
1715		 * Do not respond if not the same group.
1716		 */
1717		if (group_test(groupname, peer->ident)) {
1718			DPRINTF(2, ("receive: AM_PROCPKT drop: Autokey group mismatch\n"));
1719			sys_declined++;
1720			return;
1721		}
1722#endif /* AUTOKEY */
1723
1724		if (MODE_BROADCAST == hismode) {
1725			int	bail = 0;
1726			l_fp	tdiff;
1727			u_long	deadband;
1728
1729			DPRINTF(2, ("receive: PROCPKT/BROADCAST: prev pkt %ld seconds ago, ppoll: %d, %d secs\n",
1730				    (current_time - peer->timelastrec),
1731				    peer->ppoll, (1 << peer->ppoll)
1732				    ));
1733			/* Things we can check:
1734			 *
1735			 * Did the poll interval change?
1736			 * Is the poll interval in the packet in-range?
1737			 * Did this packet arrive too soon?
1738			 * Is the timestamp in this packet monotonic
1739			 *  with respect to the previous packet?
1740			 */
1741
1742			/* This is noteworthy, not error-worthy */
1743			if (pkt->ppoll != peer->ppoll) {
1744				msyslog(LOG_INFO, "receive: broadcast poll from %s changed from %u to %u",
1745					stoa(&rbufp->recv_srcadr),
1746					peer->ppoll, pkt->ppoll);
1747			}
1748
1749			/* This is error-worthy */
1750			if (   pkt->ppoll < peer->minpoll
1751			    || pkt->ppoll > peer->maxpoll) {
1752				msyslog(LOG_INFO, "receive: broadcast poll of %u from %s is out-of-range (%d to %d)!",
1753					pkt->ppoll, stoa(&rbufp->recv_srcadr),
1754					peer->minpoll, peer->maxpoll);
1755				++bail;
1756			}
1757
1758			/* too early? worth an error, too!
1759			 *
1760			 * [Bug 3113] Ensure that at least one poll
1761			 * interval has elapsed since the last **clean**
1762			 * packet was received.  We limit the check to
1763			 * **clean** packets to prevent replayed packets
1764			 * and incorrectly authenticated packets, which
1765			 * we'll discard, from being used to create a
1766			 * denial of service condition.
1767			 */
1768			deadband = (1u << pkt->ppoll);
1769			if (FLAG_BC_VOL & peer->flags)
1770				deadband -= 3;	/* allow greater fuzz after volley */
1771			if ((current_time - peer->timereceived) < deadband) {
1772				msyslog(LOG_INFO, "receive: broadcast packet from %s arrived after %lu, not %lu seconds!",
1773					stoa(&rbufp->recv_srcadr),
1774					(current_time - peer->timereceived),
1775					deadband);
1776				++bail;
1777			}
1778
1779			/* Alert if time from the server is non-monotonic.
1780			 *
1781			 * [Bug 3114] is about Broadcast mode replay DoS.
1782			 *
1783			 * Broadcast mode *assumes* a trusted network.
1784			 * Even so, it's nice to be robust in the face
1785			 * of attacks.
1786			 *
1787			 * If we get an authenticated broadcast packet
1788			 * with an "earlier" timestamp, it means one of
1789			 * two things:
1790			 *
1791			 * - the broadcast server had a backward step.
1792			 *
1793			 * - somebody is trying a replay attack.
1794			 *
1795			 * deadband: By default, we assume the broadcast
1796			 * network is trustable, so we take our accepted
1797			 * broadcast packets as we receive them.  But
1798			 * some folks might want to take additional poll
1799			 * delays before believing a backward step.
1800			 */
1801			if (sys_bcpollbstep) {
1802				/* pkt->ppoll or peer->ppoll ? */
1803				deadband = (1u << pkt->ppoll)
1804					   * sys_bcpollbstep + 2;
1805			} else {
1806				deadband = 0;
1807			}
1808
1809			if (L_ISZERO(&peer->bxmt)) {
1810				tdiff.l_ui = tdiff.l_uf = 0;
1811			} else {
1812				tdiff = p_xmt;
1813				L_SUB(&tdiff, &peer->bxmt);
1814			}
1815			if (   tdiff.l_i < 0
1816			    && (current_time - peer->timereceived) < deadband)
1817			{
1818				msyslog(LOG_INFO, "receive: broadcast packet from %s contains non-monotonic timestamp: %#010x.%08x -> %#010x.%08x",
1819					stoa(&rbufp->recv_srcadr),
1820					peer->bxmt.l_ui, peer->bxmt.l_uf,
1821					p_xmt.l_ui, p_xmt.l_uf
1822					);
1823				++bail;
1824			}
1825
1826			if (bail) {
1827				DPRINTF(2, ("receive: AM_PROCPKT drop: bail\n"));
1828				peer->timelastrec = current_time;
1829				sys_declined++;
1830				return;
1831			}
1832		}
1833
1834		break;
1835
1836	/*
1837	 * A passive packet matches a passive association. This is
1838	 * usually the result of reconfiguring a client on the fly. As
1839	 * this association might be legitimate and this packet an
1840	 * attempt to deny service, just ignore it.
1841	 */
1842	case AM_ERR:
1843		DPRINTF(2, ("receive: AM_ERR drop.\n"));
1844		sys_declined++;
1845		return;
1846
1847	/*
1848	 * For everything else there is the bit bucket.
1849	 */
1850	default:
1851		DPRINTF(2, ("receive: default drop.\n"));
1852		sys_declined++;
1853		return;
1854	}
1855
1856#ifdef AUTOKEY
1857	/*
1858	 * If the association is configured for Autokey, the packet must
1859	 * have a public key ID; if not, the packet must have a
1860	 * symmetric key ID.
1861	 */
1862	if (   is_authentic != AUTH_CRYPTO
1863	    && (   ((peer->flags & FLAG_SKEY) && skeyid <= NTP_MAXKEY)
1864	        || (!(peer->flags & FLAG_SKEY) && skeyid > NTP_MAXKEY))) {
1865		DPRINTF(2, ("receive: drop: Autokey but wrong/bad auth\n"));
1866		sys_badauth++;
1867		return;
1868	}
1869#endif	/* AUTOKEY */
1870
1871	peer->received++;
1872	peer->flash &= ~PKT_TEST_MASK;
1873	if (peer->flags & FLAG_XBOGUS) {
1874		peer->flags &= ~FLAG_XBOGUS;
1875		peer->flash |= TEST3;
1876	}
1877
1878	/*
1879	 * Next comes a rigorous schedule of timestamp checking. If the
1880	 * transmit timestamp is zero, the server has not initialized in
1881	 * interleaved modes or is horribly broken.
1882	 *
1883	 * A KoD packet we pay attention to cannot have a 0 transmit
1884	 * timestamp.
1885	 */
1886
1887	kissCode = kiss_code_check(hisleap, hisstratum, hismode, pkt->refid);
1888
1889	if (L_ISZERO(&p_xmt)) {
1890		peer->flash |= TEST3;			/* unsynch */
1891		if (kissCode != NOKISS) {		/* KoD packet */
1892			peer->bogusorg++;		/* for TEST2 or TEST3 */
1893			msyslog(LOG_INFO,
1894				"receive: Unexpected zero transmit timestamp in KoD from %s",
1895				ntoa(&peer->srcadr));
1896			return;
1897		}
1898
1899	/*
1900	 * If the transmit timestamp duplicates our previous one, the
1901	 * packet is a replay. This prevents the bad guys from replaying
1902	 * the most recent packet, authenticated or not.
1903	 */
1904	} else if (   ((FLAG_LOOPNONCE & peer->flags) && L_ISEQU(&peer->nonce, &p_xmt))
1905		   || (!(FLAG_LOOPNONCE & peer->flags) && L_ISEQU(&peer->xmt, &p_xmt))
1906	) {
1907		DPRINTF(2, ("receive: drop: Duplicate xmit\n"));
1908		peer->flash |= TEST1;			/* duplicate */
1909		peer->oldpkt++;
1910		return;
1911
1912	/*
1913	 * If this is a broadcast mode packet, make sure hisstratum
1914	 * is appropriate.  Don't do anything else here - we wait to
1915	 * see if this is an interleave broadcast packet until after
1916	 * we've validated the MAC that SHOULD be provided.
1917	 *
1918	 * hisstratum cannot be 0 - see assertion above.
1919	 * If hisstratum is 15, then we'll advertise as UNSPEC but
1920	 * at least we'll be able to sync with the broadcast server.
1921	 */
1922	} else if (hismode == MODE_BROADCAST) {
1923		/* 0 is unexpected too, and impossible */
1924		if (STRATUM_UNSPEC <= hisstratum) {
1925			/* Is this a ++sys_declined or ??? */
1926			msyslog(LOG_INFO,
1927				"receive: Unexpected stratum (%d) in broadcast from %s",
1928				hisstratum, ntoa(&peer->srcadr));
1929			return;
1930		}
1931
1932	/*
1933	 * Basic KoD validation checking:
1934	 *
1935	 * KoD packets are a mixed-blessing.  Forged KoD packets
1936	 * are DoS attacks.  There are rare situations where we might
1937	 * get a valid KoD response, though.  Since KoD packets are
1938	 * a special case that complicate the checks we do next, we
1939	 * handle the basic KoD checks here.
1940	 *
1941	 * Note that we expect the incoming KoD packet to have its
1942	 * (nonzero) org, rec, and xmt timestamps set to the xmt timestamp
1943	 * that we have previously sent out.  Watch interleave mode.
1944	 */
1945	} else if (kissCode != NOKISS) {
1946		DEBUG_INSIST(!L_ISZERO(&p_xmt));
1947		if (   L_ISZERO(&p_org)		/* We checked p_xmt above */
1948		    || L_ISZERO(&p_rec)) {
1949			peer->bogusorg++;
1950			msyslog(LOG_INFO,
1951				"receive: KoD packet from %s has a zero org or rec timestamp.  Ignoring.",
1952				ntoa(&peer->srcadr));
1953			return;
1954		}
1955
1956		if (   !L_ISEQU(&p_xmt, &p_org)
1957		    || !L_ISEQU(&p_xmt, &p_rec)) {
1958			peer->bogusorg++;
1959			msyslog(LOG_INFO,
1960				"receive: KoD packet from %s has inconsistent xmt/org/rec timestamps.  Ignoring.",
1961				ntoa(&peer->srcadr));
1962			return;
1963		}
1964
1965		/* Be conservative */
1966		if (peer->flip == 0 && !L_ISEQU(&p_org, &peer->aorg)) {
1967			peer->bogusorg++;
1968			msyslog(LOG_INFO,
1969				"receive: flip 0 KoD origin timestamp %#010x.%08x from %s does not match %#010x.%08x - ignoring.",
1970				p_org.l_ui, p_org.l_uf,
1971				ntoa(&peer->srcadr),
1972				peer->aorg.l_ui, peer->aorg.l_uf);
1973			return;
1974		} else if (peer->flip == 1 && !L_ISEQU(&p_org, &peer->borg)) {
1975			peer->bogusorg++;
1976			msyslog(LOG_INFO,
1977				"receive: flip 1 KoD origin timestamp %#010x.%08x from %s does not match interleave %#010x.%08x - ignoring.",
1978				p_org.l_ui, p_org.l_uf,
1979				ntoa(&peer->srcadr),
1980				peer->borg.l_ui, peer->borg.l_uf);
1981			return;
1982		}
1983
1984	/*
1985	 * Basic mode checks:
1986	 *
1987	 * If there is no origin timestamp, it's either an initial packet
1988	 * or we've already received a response to our query.  Of course,
1989	 * should 'aorg' be all-zero because this really was the original
1990	 * transmit timestamp, we'll ignore this reply.  There is a window
1991	 * of one nanosecond once every 136 years' time where this is
1992	 * possible.  We currently ignore this situation, as a completely
1993	 * zero timestamp is (quietly?) disallowed.
1994	 *
1995	 * Otherwise, check for bogus packet in basic mode.
1996	 * If it is bogus, switch to interleaved mode and resynchronize,
1997	 * but only after confirming the packet is not bogus in
1998	 * symmetric interleaved mode.
1999	 *
2000	 * This could also mean somebody is forging packets claiming to
2001	 * be from us, attempting to cause our server to KoD us.
2002	 *
2003	 * We have earlier asserted that hisstratum cannot be 0.
2004	 * If hisstratum is STRATUM_UNSPEC, it means he's not sync'd.
2005	 */
2006
2007	/* XXX: FLAG_LOOPNONCE */
2008	DEBUG_INSIST(0 == (FLAG_LOOPNONCE & peer->flags));
2009
2010	} else if (peer->flip == 0) {
2011		if (0) {
2012		} else if (L_ISZERO(&p_org)) {
2013			const char *action;
2014
2015#ifdef BUG3361
2016			msyslog(LOG_INFO,
2017				"receive: BUG 3361: Clearing peer->aorg ");
2018			L_CLR(&peer->aorg);
2019			/* Clear peer->nonce, too? */
2020#endif
2021			/**/
2022			switch (hismode) {
2023			/* We allow 0org for: */
2024			    case UCHAR_MAX:
2025				action = "Allow";
2026				break;
2027			/* We disallow 0org for: */
2028			    case MODE_UNSPEC:
2029			    case MODE_ACTIVE:
2030			    case MODE_PASSIVE:
2031			    case MODE_CLIENT:
2032			    case MODE_SERVER:
2033			    case MODE_BROADCAST:
2034				action = "Drop";
2035				peer->bogusorg++;
2036				peer->flash |= TEST2;	/* bogus */
2037				break;
2038			    default:
2039				action = "";	/* for cranky compilers / MSVC */
2040				INSIST(!"receive(): impossible hismode");
2041				break;
2042			}
2043			/**/
2044			msyslog(LOG_INFO,
2045				"receive: %s 0 origin timestamp from %s@%s xmt %#010x.%08x",
2046				action, hm_str, ntoa(&peer->srcadr),
2047				ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
2048		} else if (!L_ISEQU(&p_org, &peer->aorg)) {
2049			/* are there cases here where we should bail? */
2050			/* Should we set TEST2 if we decide to try xleave? */
2051			peer->bogusorg++;
2052			peer->flash |= TEST2;	/* bogus */
2053			msyslog(LOG_INFO,
2054				"receive: Unexpected origin timestamp %#010x.%08x does not match aorg %#010x.%08x from %s@%s xmt %#010x.%08x",
2055				ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
2056				peer->aorg.l_ui, peer->aorg.l_uf,
2057				hm_str, ntoa(&peer->srcadr),
2058				ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
2059			if (  !L_ISZERO(&peer->dst)
2060			    && L_ISEQU(&p_org, &peer->dst)) {
2061				/* Might be the start of an interleave */
2062				if (dynamic_interleave) {
2063					peer->flip = 1;
2064					report_event(PEVNT_XLEAVE, peer, NULL);
2065				} else {
2066					msyslog(LOG_INFO,
2067						"receive: Dynamic interleave from %s@%s denied",
2068						hm_str, ntoa(&peer->srcadr));
2069				}
2070			}
2071		} else {
2072			L_CLR(&peer->aorg);
2073			/* XXX: FLAG_LOOPNONCE */
2074		}
2075
2076	/*
2077	 * Check for valid nonzero timestamp fields.
2078	 */
2079	} else if (   L_ISZERO(&p_org)
2080		   || L_ISZERO(&p_rec)
2081		   || L_ISZERO(&peer->dst)) {
2082		peer->flash |= TEST3;		/* unsynch */
2083
2084	/*
2085	 * Check for bogus packet in interleaved symmetric mode. This
2086	 * can happen if a packet is lost, duplicated or crossed. If
2087	 * found, flip and resynchronize.
2088	 */
2089	} else if (   !L_ISZERO(&peer->dst)
2090		   && !L_ISEQU(&p_org, &peer->dst)) {
2091		DPRINTF(2, ("receive: drop: Bogus packet in interleaved symmetric mode\n"));
2092		peer->bogusorg++;
2093		peer->flags |= FLAG_XBOGUS;
2094		peer->flash |= TEST2;		/* bogus */
2095#ifdef BUG3453
2096		return; /* Bogus packet, we are done */
2097#endif
2098	}
2099
2100	/**/
2101
2102	/*
2103	 * If this is a crypto_NAK, the server cannot authenticate a
2104	 * client packet. The server might have just changed keys. Clear
2105	 * the association and restart the protocol.
2106	 */
2107	if (crypto_nak_test == VALIDNAK) {
2108		report_event(PEVNT_AUTH, peer, "crypto_NAK");
2109		peer->flash |= TEST5;		/* bad auth */
2110		peer->badauth++;
2111		if (peer->flags & FLAG_PREEMPT) {
2112			if (unpeer_crypto_nak_early) {
2113				unpeer(peer);
2114			}
2115			DPRINTF(2, ("receive: drop: PREEMPT crypto_NAK\n"));
2116			return;
2117		}
2118#ifdef AUTOKEY
2119		if (peer->crypto) {
2120			peer_clear(peer, "AUTH");
2121		}
2122#endif	/* AUTOKEY */
2123		DPRINTF(2, ("receive: drop: crypto_NAK\n"));
2124		return;
2125
2126	/*
2127	 * If the digest fails or it's missing for authenticated
2128	 * associations, the client cannot authenticate a server
2129	 * reply to a client packet previously sent. The loopback check
2130	 * is designed to avoid a bait-and-switch attack, which was
2131	 * possible in past versions. If symmetric modes, return a
2132	 * crypto-NAK. The peer should restart the protocol.
2133	 */
2134	} else if (!AUTH(peer->keyid || has_mac ||
2135			 (restrict_mask & RES_DONTTRUST), is_authentic)) {
2136
2137		if (peer->flash & PKT_TEST_MASK) {
2138			msyslog(LOG_INFO,
2139				"receive: Bad auth in packet with bad timestamps from %s denied - spoof?",
2140				ntoa(&peer->srcadr));
2141			return;
2142		}
2143
2144		report_event(PEVNT_AUTH, peer, "digest");
2145		peer->flash |= TEST5;		/* bad auth */
2146		peer->badauth++;
2147		if (   has_mac
2148		    && (   hismode == MODE_ACTIVE
2149			|| hismode == MODE_PASSIVE))
2150			fast_xmit(rbufp, MODE_ACTIVE, 0, restrict_mask);
2151		if (peer->flags & FLAG_PREEMPT) {
2152			if (unpeer_digest_early) {
2153				unpeer(peer);
2154			}
2155		}
2156#ifdef AUTOKEY
2157		else if (peer_clear_digest_early && peer->crypto) {
2158			peer_clear(peer, "AUTH");
2159		}
2160#endif	/* AUTOKEY */
2161		DPRINTF(2, ("receive: drop: Bad or missing AUTH\n"));
2162		return;
2163	}
2164
2165	/*
2166	 * For broadcast packets:
2167	 *
2168	 * HMS: This next line never made much sense to me, even
2169	 * when it was up higher:
2170	 *   If an initial volley, bail out now and let the
2171	 *   client do its stuff.
2172	 *
2173	 * If the packet has not failed authentication, then
2174	 * - if the origin timestamp is nonzero this is an
2175	 *   interleaved broadcast, so restart the protocol.
2176	 * - else, this is not an interleaved broadcast packet.
2177	 */
2178	if (hismode == MODE_BROADCAST) {
2179		if (   is_authentic == AUTH_OK
2180		    || is_authentic == AUTH_NONE) {
2181			if (!L_ISZERO(&p_org)) {
2182				if (!(peer->flags & FLAG_XB)) {
2183					msyslog(LOG_INFO,
2184						"receive: Broadcast server at %s is in interleave mode",
2185						ntoa(&peer->srcadr));
2186					peer->flags |= FLAG_XB;
2187					peer->aorg = p_xmt;
2188					peer->borg = rbufp->recv_time;
2189					report_event(PEVNT_XLEAVE, peer, NULL);
2190					return;
2191				}
2192			} else if (peer->flags & FLAG_XB) {
2193				msyslog(LOG_INFO,
2194					"receive: Broadcast server at %s is no longer in interleave mode",
2195					ntoa(&peer->srcadr));
2196				peer->flags &= ~FLAG_XB;
2197			}
2198		} else {
2199			msyslog(LOG_INFO,
2200				"receive: Bad broadcast auth (%d) from %s",
2201				is_authentic, ntoa(&peer->srcadr));
2202		}
2203
2204		/*
2205		 * Now that we know the packet is correctly authenticated,
2206		 * update peer->bxmt.
2207		 */
2208		peer->bxmt = p_xmt;
2209	}
2210
2211
2212	/*
2213	** Update the state variables.
2214	*/
2215	if (peer->flip == 0) {
2216		if (hismode != MODE_BROADCAST)
2217			peer->rec = p_xmt;
2218		peer->dst = rbufp->recv_time;
2219	}
2220	peer->xmt = p_xmt;
2221
2222	/*
2223	 * Set the peer ppoll to the maximum of the packet ppoll and the
2224	 * peer minpoll. If a kiss-o'-death, set the peer minpoll to
2225	 * this maximum and advance the headway to give the sender some
2226	 * headroom. Very intricate.
2227	 */
2228
2229	/*
2230	 * Check for any kiss codes. Note this is only used when a server
2231	 * responds to a packet request.
2232	 */
2233
2234	/*
2235	 * Check to see if this is a RATE Kiss Code
2236	 * Currently this kiss code will accept whatever poll
2237	 * rate that the server sends
2238	 */
2239	peer->ppoll = max(peer->minpoll, pkt->ppoll);
2240	if (kissCode == RATEKISS) {
2241		peer->selbroken++;	/* Increment the KoD count */
2242		report_event(PEVNT_RATE, peer, NULL);
2243		if (pkt->ppoll > peer->minpoll)
2244			peer->minpoll = peer->ppoll;
2245		peer->burst = peer->retry = 0;
2246		peer->throttle = (NTP_SHIFT + 1) * (1 << peer->minpoll);
2247		poll_update(peer, pkt->ppoll, 0);
2248		return;				/* kiss-o'-death */
2249	}
2250	if (kissCode != NOKISS) {
2251		peer->selbroken++;	/* Increment the KoD count */
2252		return;		/* Drop any other kiss code packets */
2253	}
2254
2255
2256	/*
2257	 * XXX
2258	 */
2259
2260
2261	/*
2262	 * If:
2263	 *	- this is a *cast (uni-, broad-, or m-) server packet
2264	 *	- and it's symmetric-key authenticated
2265	 * then see if the sender's IP is trusted for this keyid.
2266	 * If it is, great - nothing special to do here.
2267	 * Otherwise, we should report and bail.
2268	 *
2269	 * Autokey-authenticated packets are accepted.
2270	 */
2271
2272	switch (hismode) {
2273	    case MODE_SERVER:		/* server mode */
2274	    case MODE_BROADCAST:	/* broadcast mode */
2275	    case MODE_ACTIVE:		/* symmetric active mode */
2276	    case MODE_PASSIVE:		/* symmetric passive mode */
2277		if (   is_authentic == AUTH_OK
2278		    && skeyid
2279		    && skeyid <= NTP_MAXKEY
2280		    && !authistrustedip(skeyid, &peer->srcadr)) {
2281			report_event(PEVNT_AUTH, peer, "authIP");
2282			peer->badauth++;
2283			return;
2284		}
2285		break;
2286
2287	    case MODE_CLIENT:		/* client mode */
2288#if 0		/* At this point, MODE_CONTROL is overloaded by MODE_BCLIENT */
2289	    case MODE_CONTROL:		/* control mode */
2290#endif
2291	    case MODE_PRIVATE:		/* private mode */
2292	    case MODE_BCLIENT:		/* broadcast client mode */
2293		break;
2294
2295	    case MODE_UNSPEC:		/* unspecified (old version) */
2296	    default:
2297		msyslog(LOG_INFO,
2298			"receive: Unexpected mode (%d) in packet from %s",
2299			hismode, ntoa(&peer->srcadr));
2300		break;
2301	}
2302
2303
2304	/*
2305	 * That was hard and I am sweaty, but the packet is squeaky
2306	 * clean. Get on with real work.
2307	 */
2308	peer->timereceived = current_time;
2309	peer->timelastrec = current_time;
2310	if (is_authentic == AUTH_OK)
2311		peer->flags |= FLAG_AUTHENTIC;
2312	else
2313		peer->flags &= ~FLAG_AUTHENTIC;
2314
2315#ifdef AUTOKEY
2316	/*
2317	 * More autokey dance. The rules of the cha-cha are as follows:
2318	 *
2319	 * 1. If there is no key or the key is not auto, do nothing.
2320	 *
2321	 * 2. If this packet is in response to the one just previously
2322	 *    sent or from a broadcast server, do the extension fields.
2323	 *    Otherwise, assume bogosity and bail out.
2324	 *
2325	 * 3. If an extension field contains a verified signature, it is
2326	 *    self-authenticated and we sit the dance.
2327	 *
2328	 * 4. If this is a server reply, check only to see that the
2329	 *    transmitted key ID matches the received key ID.
2330	 *
2331	 * 5. Check to see that one or more hashes of the current key ID
2332	 *    matches the previous key ID or ultimate original key ID
2333	 *    obtained from the broadcaster or symmetric peer. If no
2334	 *    match, sit the dance and call for new autokey values.
2335	 *
2336	 * In case of crypto error, fire the orchestra, stop dancing and
2337	 * restart the protocol.
2338	 */
2339	if (peer->flags & FLAG_SKEY) {
2340		/*
2341		 * Decrement remaining autokey hashes. This isn't
2342		 * perfect if a packet is lost, but results in no harm.
2343		 */
2344		ap = (struct autokey *)peer->recval.ptr;
2345		if (ap != NULL) {
2346			if (ap->seq > 0)
2347				ap->seq--;
2348		}
2349		peer->flash |= TEST8;
2350		rval = crypto_recv(peer, rbufp);
2351		if (rval == XEVNT_OK) {
2352			peer->unreach = 0;
2353		} else {
2354			if (rval == XEVNT_ERR) {
2355				report_event(PEVNT_RESTART, peer,
2356				    "crypto error");
2357				peer_clear(peer, "CRYP");
2358				peer->flash |= TEST9;	/* bad crypt */
2359				if (peer->flags & FLAG_PREEMPT) {
2360					if (unpeer_crypto_early) {
2361						unpeer(peer);
2362					}
2363				}
2364			}
2365			return;
2366		}
2367
2368		/*
2369		 * If server mode, verify the receive key ID matches
2370		 * the transmit key ID.
2371		 */
2372		if (hismode == MODE_SERVER) {
2373			if (skeyid == peer->keyid)
2374				peer->flash &= ~TEST8;
2375
2376		/*
2377		 * If an extension field is present, verify only that it
2378		 * has been correctly signed. We don't need a sequence
2379		 * check here, but the sequence continues.
2380		 */
2381		} else if (!(peer->flash & TEST8)) {
2382			peer->pkeyid = skeyid;
2383
2384		/*
2385		 * Now the fun part. Here, skeyid is the current ID in
2386		 * the packet, pkeyid is the ID in the last packet and
2387		 * tkeyid is the hash of skeyid. If the autokey values
2388		 * have not been received, this is an automatic error.
2389		 * If so, check that the tkeyid matches pkeyid. If not,
2390		 * hash tkeyid and try again. If the number of hashes
2391		 * exceeds the number remaining in the sequence, declare
2392		 * a successful failure and refresh the autokey values.
2393		 */
2394		} else if (ap != NULL) {
2395			int i;
2396
2397			for (i = 0; ; i++) {
2398				if (   tkeyid == peer->pkeyid
2399				    || tkeyid == ap->key) {
2400					peer->flash &= ~TEST8;
2401					peer->pkeyid = skeyid;
2402					ap->seq -= i;
2403					break;
2404				}
2405				if (i > ap->seq) {
2406					peer->crypto &=
2407					    ~CRYPTO_FLAG_AUTO;
2408					break;
2409				}
2410				tkeyid = session_key(
2411				    &rbufp->recv_srcadr, dstadr_sin,
2412				    tkeyid, pkeyid, 0);
2413			}
2414			if (peer->flash & TEST8)
2415				report_event(PEVNT_AUTH, peer, "keylist");
2416		}
2417		if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
2418			peer->flash |= TEST8;	/* bad autokey */
2419
2420		/*
2421		 * The maximum lifetime of the protocol is about one
2422		 * week before restarting the Autokey protocol to
2423		 * refresh certificates and leapseconds values.
2424		 */
2425		if (current_time > peer->refresh) {
2426			report_event(PEVNT_RESTART, peer,
2427			    "crypto refresh");
2428			peer_clear(peer, "TIME");
2429			return;
2430		}
2431	}
2432#endif	/* AUTOKEY */
2433
2434	/*
2435	 * The dance is complete and the flash bits have been lit. Toss
2436	 * the packet over the fence for processing, which may light up
2437	 * more flashers. Leave if the packet is not good.
2438	 */
2439	process_packet(peer, pkt, rbufp->recv_length);
2440	if (peer->flash & PKT_TEST_MASK)
2441		return;
2442
2443	/* [bug 3592] Update poll. Ideally this should not happen in a
2444	 * receive branch, but too much is going on here... at least we
2445	 * do it only if the packet was good!
2446	 */
2447	poll_update(peer, peer->hpoll, (peer->hmode == MODE_CLIENT));
2448
2449	/*
2450	 * In interleaved mode update the state variables. Also adjust the
2451	 * transmit phase to avoid crossover.
2452	 */
2453	if (peer->flip != 0) {
2454		peer->rec = p_rec;
2455		peer->dst = rbufp->recv_time;
2456		if (peer->nextdate - current_time < (1U << min(peer->ppoll,
2457		    peer->hpoll)) / 2)
2458			peer->nextdate++;
2459		else
2460			peer->nextdate--;
2461	}
2462}
2463
2464
2465/*
2466 * process_packet - Packet Procedure, a la Section 3.4.4 of RFC-1305
2467 *	Or almost, at least.  If we're in here we have a reasonable
2468 *	expectation that we will be having a long term
2469 *	relationship with this host.
2470 */
2471void
2472process_packet(
2473	register struct peer *peer,
2474	register struct pkt *pkt,
2475	u_int	len
2476	)
2477{
2478	double	t34, t21;
2479	double	p_offset, p_del, p_disp;
2480	l_fp	p_rec, p_xmt, p_org, p_reftime, ci;
2481	u_char	pmode, pleap, pversion, pstratum;
2482	char	statstr[NTP_MAXSTRLEN];
2483#ifdef ASSYM
2484	int	itemp;
2485	double	etemp, ftemp, td;
2486#endif /* ASSYM */
2487
2488#if 0
2489	sys_processed++;
2490	peer->processed++;
2491#endif
2492	p_del = FPTOD(NTOHS_FP(pkt->rootdelay));
2493	p_offset = 0;
2494	p_disp = FPTOD(NTOHS_FP(pkt->rootdisp));
2495	NTOHL_FP(&pkt->reftime, &p_reftime);
2496	NTOHL_FP(&pkt->org, &p_org);
2497	NTOHL_FP(&pkt->rec, &p_rec);
2498	NTOHL_FP(&pkt->xmt, &p_xmt);
2499	pmode = PKT_MODE(pkt->li_vn_mode);
2500	pleap = PKT_LEAP(pkt->li_vn_mode);
2501	pversion = PKT_VERSION(pkt->li_vn_mode);
2502	pstratum = PKT_TO_STRATUM(pkt->stratum);
2503
2504	/**/
2505
2506	/**/
2507
2508	/*
2509	 * Verify the server is synchronized; that is, the leap bits,
2510	 * stratum and root distance are valid.
2511	 */
2512	if (   pleap == LEAP_NOTINSYNC		/* test 6 */
2513	    || pstratum < sys_floor || pstratum >= sys_ceiling)
2514		peer->flash |= TEST6;		/* bad synch or strat */
2515	if (p_del / 2 + p_disp >= MAXDISPERSE)	/* test 7 */
2516		peer->flash |= TEST7;		/* bad header */
2517
2518	/*
2519	 * If any tests fail at this point, the packet is discarded.
2520	 * Note that some flashers may have already been set in the
2521	 * receive() routine.
2522	 */
2523	if (peer->flash & PKT_TEST_MASK) {
2524		peer->seldisptoolarge++;
2525		DPRINTF(1, ("packet: flash header %04x\n",
2526			    peer->flash));
2527
2528		/* ppoll updated? */
2529		/* XXX: Fuzz the poll? */
2530		poll_update(peer, peer->hpoll, (peer->hmode == MODE_CLIENT));
2531		return;
2532	}
2533
2534	/**/
2535
2536#if 1
2537	sys_processed++;
2538	peer->processed++;
2539#endif
2540
2541	/*
2542	 * Capture the header values in the client/peer association..
2543	 */
2544	record_raw_stats(&peer->srcadr,
2545	    peer->dstadr ? &peer->dstadr->sin : NULL,
2546	    &p_org, &p_rec, &p_xmt, &peer->dst,
2547	    pleap, pversion, pmode, pstratum, pkt->ppoll, pkt->precision,
2548	    p_del, p_disp, pkt->refid,
2549	    len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
2550	peer->leap = pleap;
2551	peer->stratum = min(pstratum, STRATUM_UNSPEC);
2552	peer->pmode = pmode;
2553	peer->precision = pkt->precision;
2554	peer->rootdelay = p_del;
2555	peer->rootdisp = p_disp;
2556	peer->refid = pkt->refid;		/* network byte order */
2557	peer->reftime = p_reftime;
2558
2559	/*
2560	 * First, if either burst mode is armed, enable the burst.
2561	 * Compute the headway for the next packet and delay if
2562	 * necessary to avoid exceeding the threshold.
2563	 */
2564	if (peer->retry > 0) {
2565		peer->retry = 0;
2566		if (peer->reach)
2567			peer->burst = min(1 << (peer->hpoll -
2568			    peer->minpoll), NTP_SHIFT) - 1;
2569		else
2570			peer->burst = NTP_IBURST - 1;
2571		if (peer->burst > 0)
2572			peer->nextdate = current_time;
2573	}
2574	poll_update(peer, peer->hpoll, (peer->hmode == MODE_CLIENT));
2575
2576	/**/
2577
2578	/*
2579	 * If the peer was previously unreachable, raise a trap. In any
2580	 * case, mark it reachable.
2581	 */
2582	if (!peer->reach) {
2583		report_event(PEVNT_REACH, peer, NULL);
2584		peer->timereachable = current_time;
2585	}
2586	peer->reach |= 1;
2587
2588	/*
2589	 * For a client/server association, calculate the clock offset,
2590	 * roundtrip delay and dispersion. The equations are reordered
2591	 * from the spec for more efficient use of temporaries. For a
2592	 * broadcast association, offset the last measurement by the
2593	 * computed delay during the client/server volley. Note the
2594	 * computation of dispersion includes the system precision plus
2595	 * that due to the frequency error since the origin time.
2596	 *
2597	 * It is very important to respect the hazards of overflow. The
2598	 * only permitted operation on raw timestamps is subtraction,
2599	 * where the result is a signed quantity spanning from 68 years
2600	 * in the past to 68 years in the future. To avoid loss of
2601	 * precision, these calculations are done using 64-bit integer
2602	 * arithmetic. However, the offset and delay calculations are
2603	 * sums and differences of these first-order differences, which
2604	 * if done using 64-bit integer arithmetic, would be valid over
2605	 * only half that span. Since the typical first-order
2606	 * differences are usually very small, they are converted to 64-
2607	 * bit doubles and all remaining calculations done in floating-
2608	 * double arithmetic. This preserves the accuracy while
2609	 * retaining the 68-year span.
2610	 *
2611	 * There are three interleaving schemes, basic, interleaved
2612	 * symmetric and interleaved broadcast. The timestamps are
2613	 * idioscyncratically different. See the onwire briefing/white
2614	 * paper at www.eecis.udel.edu/~mills for details.
2615	 *
2616	 * Interleaved symmetric mode
2617	 * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
2618	 * t4 = peer->dst
2619	 */
2620	if (peer->flip != 0) {
2621		ci = p_xmt;				/* t3 - t4 */
2622		L_SUB(&ci, &peer->dst);
2623		LFPTOD(&ci, t34);
2624		ci = p_rec;				/* t2 - t1 */
2625		if (peer->flip > 0)
2626			L_SUB(&ci, &peer->borg);
2627		else
2628			L_SUB(&ci, &peer->aorg);
2629		LFPTOD(&ci, t21);
2630		p_del = t21 - t34;
2631		p_offset = (t21 + t34) / 2.;
2632		if (p_del < 0 || p_del > 1.) {
2633			snprintf(statstr, sizeof(statstr),
2634			    "t21 %.6f t34 %.6f", t21, t34);
2635			report_event(PEVNT_XERR, peer, statstr);
2636			return;
2637		}
2638
2639	/*
2640	 * Broadcast modes
2641	 */
2642	} else if (peer->pmode == MODE_BROADCAST) {
2643
2644		/*
2645		 * Interleaved broadcast mode. Use interleaved timestamps.
2646		 * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
2647		 */
2648		if (peer->flags & FLAG_XB) {
2649			ci = p_org;			/* delay */
2650			L_SUB(&ci, &peer->aorg);
2651			LFPTOD(&ci, t34);
2652			ci = p_org;			/* t2 - t1 */
2653			L_SUB(&ci, &peer->borg);
2654			LFPTOD(&ci, t21);
2655			peer->aorg = p_xmt;
2656			peer->borg = peer->dst;
2657			if (t34 < 0 || t34 > 1.) {
2658				/* drop all if in the initial volley */
2659				if (FLAG_BC_VOL & peer->flags)
2660					goto bcc_init_volley_fail;
2661				snprintf(statstr, sizeof(statstr),
2662				    "offset %.6f delay %.6f", t21, t34);
2663				report_event(PEVNT_XERR, peer, statstr);
2664				return;
2665			}
2666			p_offset = t21;
2667			peer->xleave = t34;
2668
2669		/*
2670		 * Basic broadcast - use direct timestamps.
2671		 * t3 = p_xmt, t4 = peer->dst
2672		 */
2673		} else {
2674			ci = p_xmt;		/* t3 - t4 */
2675			L_SUB(&ci, &peer->dst);
2676			LFPTOD(&ci, t34);
2677			p_offset = t34;
2678		}
2679
2680		/*
2681		 * When calibration is complete and the clock is
2682		 * synchronized, the bias is calculated as the difference
2683		 * between the unicast timestamp and the broadcast
2684		 * timestamp. This works for both basic and interleaved
2685		 * modes.
2686		 * [Bug 3031] Don't keep this peer when the delay
2687		 * calculation gives reason to suspect clock steps.
2688		 * This is assumed for delays > 50ms.
2689		 */
2690		if (FLAG_BC_VOL & peer->flags) {
2691			peer->flags &= ~FLAG_BC_VOL;
2692			peer->delay = fabs(peer->offset - p_offset) * 2;
2693			DPRINTF(2, ("broadcast volley: initial delay=%.6f\n",
2694				peer->delay));
2695			if (peer->delay > fabs(sys_bdelay)) {
2696		bcc_init_volley_fail:
2697				DPRINTF(2, ("%s", "broadcast volley: initial delay exceeds limit\n"));
2698				unpeer(peer);
2699				return;
2700			}
2701		}
2702		peer->nextdate = current_time + (1u << peer->ppoll) - 2u;
2703		p_del = peer->delay;
2704		p_offset += p_del / 2;
2705
2706
2707	/*
2708	 * Basic mode, otherwise known as the old fashioned way.
2709	 *
2710	 * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2711	 */
2712	} else {
2713		ci = p_xmt;				/* t3 - t4 */
2714		L_SUB(&ci, &peer->dst);
2715		LFPTOD(&ci, t34);
2716		ci = p_rec;				/* t2 - t1 */
2717		L_SUB(&ci, &p_org);
2718		LFPTOD(&ci, t21);
2719		p_del = fabs(t21 - t34);
2720		p_offset = (t21 + t34) / 2.;
2721	}
2722	p_del = max(p_del, LOGTOD(sys_precision));
2723	p_disp = LOGTOD(sys_precision) + LOGTOD(peer->precision) +
2724	    clock_phi * p_del;
2725
2726#if ASSYM
2727	/*
2728	 * This code calculates the outbound and inbound data rates by
2729	 * measuring the differences between timestamps at different
2730	 * packet lengths. This is helpful in cases of large asymmetric
2731	 * delays commonly experienced on deep space communication
2732	 * links.
2733	 */
2734	if (peer->t21_last > 0 && peer->t34_bytes > 0) {
2735		itemp = peer->t21_bytes - peer->t21_last;
2736		if (itemp > 25) {
2737			etemp = t21 - peer->t21;
2738			if (fabs(etemp) > 1e-6) {
2739				ftemp = itemp / etemp;
2740				if (ftemp > 1000.)
2741					peer->r21 = ftemp;
2742			}
2743		}
2744		itemp = len - peer->t34_bytes;
2745		if (itemp > 25) {
2746			etemp = -t34 - peer->t34;
2747			if (fabs(etemp) > 1e-6) {
2748				ftemp = itemp / etemp;
2749				if (ftemp > 1000.)
2750					peer->r34 = ftemp;
2751			}
2752		}
2753	}
2754
2755	/*
2756	 * The following section compensates for different data rates on
2757	 * the outbound (d21) and inbound (t34) directions. To do this,
2758	 * it finds t such that r21 * t - r34 * (d - t) = 0, where d is
2759	 * the roundtrip delay. Then it calculates the correction as a
2760	 * fraction of d.
2761	 */
2762	peer->t21 = t21;
2763	peer->t21_last = peer->t21_bytes;
2764	peer->t34 = -t34;
2765	peer->t34_bytes = len;
2766	DPRINTF(2, ("packet: t21 %.9lf %d t34 %.9lf %d\n", peer->t21,
2767		    peer->t21_bytes, peer->t34, peer->t34_bytes));
2768	if (peer->r21 > 0 && peer->r34 > 0 && p_del > 0) {
2769		if (peer->pmode != MODE_BROADCAST)
2770			td = (peer->r34 / (peer->r21 + peer->r34) -
2771			    .5) * p_del;
2772		else
2773			td = 0;
2774
2775		/*
2776		 * Unfortunately, in many cases the errors are
2777		 * unacceptable, so for the present the rates are not
2778		 * used. In future, we might find conditions where the
2779		 * calculations are useful, so this should be considered
2780		 * a work in progress.
2781		 */
2782		t21 -= td;
2783		t34 -= td;
2784		DPRINTF(2, ("packet: del %.6lf r21 %.1lf r34 %.1lf %.6lf\n",
2785			    p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2786			    td));
2787	}
2788#endif /* ASSYM */
2789
2790	/*
2791	 * That was awesome. Now hand off to the clock filter.
2792	 */
2793	clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2794
2795	/*
2796	 * If we are in broadcast calibrate mode, return to broadcast
2797	 * client mode when the client is fit and the autokey dance is
2798	 * complete.
2799	 */
2800	if (   (FLAG_BC_VOL & peer->flags)
2801	    && MODE_CLIENT == peer->hmode
2802	    && !(TEST11 & peer_unfit(peer))) {	/* distance exceeded */
2803#ifdef AUTOKEY
2804		if (peer->flags & FLAG_SKEY) {
2805			if (!(~peer->crypto & CRYPTO_FLAG_ALL))
2806				peer->hmode = MODE_BCLIENT;
2807		} else {
2808			peer->hmode = MODE_BCLIENT;
2809		}
2810#else	/* !AUTOKEY follows */
2811		peer->hmode = MODE_BCLIENT;
2812#endif	/* !AUTOKEY */
2813	}
2814}
2815
2816
2817/*
2818 * clock_update - Called at system process update intervals.
2819 */
2820static void
2821clock_update(
2822	struct peer *peer	/* peer structure pointer */
2823	)
2824{
2825	double	dtemp;
2826	l_fp	now;
2827#ifdef HAVE_LIBSCF_H
2828	char	*fmri;
2829#endif /* HAVE_LIBSCF_H */
2830
2831	/*
2832	 * Update the system state variables. We do this very carefully,
2833	 * as the poll interval might need to be clamped differently.
2834	 */
2835	sys_peer = peer;
2836	sys_epoch = peer->epoch;
2837	if (sys_poll < peer->minpoll)
2838		sys_poll = peer->minpoll;
2839	if (sys_poll > peer->maxpoll)
2840		sys_poll = peer->maxpoll;
2841	poll_update(peer, sys_poll, 0);
2842	sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
2843	if (   peer->stratum == STRATUM_REFCLOCK
2844	    || peer->stratum == STRATUM_UNSPEC)
2845		sys_refid = peer->refid;
2846	else
2847		sys_refid = addr2refid(&peer->srcadr);
2848	/*
2849	 * Root Dispersion (E) is defined (in RFC 5905) as:
2850	 *
2851	 * E = p.epsilon_r + p.epsilon + p.psi + PHI*(s.t - p.t) + |THETA|
2852	 *
2853	 * where:
2854	 *  p.epsilon_r is the PollProc's root dispersion
2855	 *  p.epsilon   is the PollProc's dispersion
2856	 *  p.psi       is the PollProc's jitter
2857	 *  THETA       is the combined offset
2858	 *
2859	 * NB: Think Hard about where these numbers come from and
2860	 * what they mean.  When did peer->update happen?  Has anything
2861	 * interesting happened since then?  What values are the most
2862	 * defensible?  Why?
2863	 *
2864	 * DLM thinks this equation is probably the best of all worse choices.
2865	 */
2866	dtemp	= peer->rootdisp
2867		+ peer->disp
2868		+ sys_jitter
2869		+ clock_phi * (current_time - peer->update)
2870		+ fabs(sys_offset);
2871
2872	p2_rootdisp = prev_rootdisp;
2873	prev_rootdisp = sys_rootdisp;
2874	if (dtemp > sys_mindisp)
2875		sys_rootdisp = dtemp;
2876	else
2877		sys_rootdisp = sys_mindisp;
2878
2879	sys_rootdelay = peer->delay + peer->rootdelay;
2880
2881	p2_reftime = prev_reftime;
2882	p2_time = prev_time;
2883
2884	prev_reftime = sys_reftime;
2885	prev_time = current_time + 64 + (rand() & 0x3f);	/* 64-127 s */
2886
2887	sys_reftime = peer->dst;
2888
2889	DPRINTF(1, ("clock_update: at %lu sample %lu associd %d\n",
2890		    current_time, peer->epoch, peer->associd));
2891
2892	/*
2893	 * Comes now the moment of truth. Crank the clock discipline and
2894	 * see what comes out.
2895	 */
2896	switch (local_clock(peer, sys_offset)) {
2897
2898	/*
2899	 * Clock exceeds panic threshold. Life as we know it ends.
2900	 */
2901	case -1:
2902#ifdef HAVE_LIBSCF_H
2903		/*
2904		 * For Solaris enter the maintenance mode.
2905		 */
2906		if ((fmri = getenv("SMF_FMRI")) != NULL) {
2907			if (smf_maintain_instance(fmri, 0) < 0) {
2908				printf("smf_maintain_instance: %s\n",
2909				    scf_strerror(scf_error()));
2910				exit(1);
2911			}
2912			/*
2913			 * Sleep until SMF kills us.
2914			 */
2915			for (;;)
2916				pause();
2917		}
2918#endif /* HAVE_LIBSCF_H */
2919		exit (-1);
2920		/* not reached */
2921
2922	/*
2923	 * Clock was stepped. Flush all time values of all peers.
2924	 */
2925	case 2:
2926		clear_all();
2927		set_sys_leap(LEAP_NOTINSYNC);
2928		sys_stratum = STRATUM_UNSPEC;
2929		memcpy(&sys_refid, "STEP", 4);
2930		sys_rootdelay = 0;
2931		p2_rootdisp = 0;
2932		prev_rootdisp = 0;
2933		sys_rootdisp = 0;
2934		L_CLR(&p2_reftime);	/* Should we clear p2_reftime? */
2935		L_CLR(&prev_reftime);	/* Should we clear prev_reftime? */
2936		L_CLR(&sys_reftime);
2937		sys_jitter = LOGTOD(sys_precision);
2938		leapsec_reset_frame();
2939		break;
2940
2941	/*
2942	 * Clock was slewed. Handle the leapsecond stuff.
2943	 */
2944	case 1:
2945
2946		/*
2947		 * If this is the first time the clock is set, reset the
2948		 * leap bits. If crypto, the timer will goose the setup
2949		 * process.
2950		 */
2951		if (sys_leap == LEAP_NOTINSYNC) {
2952			set_sys_leap(LEAP_NOWARNING);
2953#ifdef AUTOKEY
2954			if (crypto_flags)
2955				crypto_update();
2956#endif	/* AUTOKEY */
2957			/*
2958			 * If our parent process is waiting for the
2959			 * first clock sync, send them home satisfied.
2960			 */
2961#ifdef HAVE_WORKING_FORK
2962			if (daemon_pipe[1] != -1) {
2963				write(daemon_pipe[1], "S\n", 2);
2964				close(daemon_pipe[1]);
2965				daemon_pipe[1] = -1;
2966				DPRINTF(1, ("notified parent --wait-sync is done\n"));
2967			}
2968#endif /* HAVE_WORKING_FORK */
2969
2970		}
2971
2972		/*
2973		 * If there is no leap second pending and the number of
2974		 * survivor leap bits is greater than half the number of
2975		 * survivors, try to schedule a leap for the end of the
2976		 * current month. (This only works if no leap second for
2977		 * that range is in the table, so doing this more than
2978		 * once is mostly harmless.)
2979		 */
2980		if (leapsec == LSPROX_NOWARN) {
2981			if (   leap_vote_ins > leap_vote_del
2982			    && leap_vote_ins > sys_survivors / 2) {
2983				get_systime(&now);
2984				leapsec_add_dyn(TRUE, now.l_ui, NULL);
2985			}
2986			if (   leap_vote_del > leap_vote_ins
2987			    && leap_vote_del > sys_survivors / 2) {
2988				get_systime(&now);
2989				leapsec_add_dyn(FALSE, now.l_ui, NULL);
2990			}
2991		}
2992		break;
2993
2994	/*
2995	 * Popcorn spike or step threshold exceeded. Pretend it never
2996	 * happened.
2997	 */
2998	default:
2999		break;
3000	}
3001}
3002
3003
3004/*
3005 * poll_update - update peer poll interval
3006 */
3007void
3008poll_update(
3009	struct peer *peer,	/* peer structure pointer */
3010	u_char	mpoll,
3011	u_char  skewpoll
3012	)
3013{
3014	u_long	next, utemp, limit;
3015	u_char	hpoll;
3016
3017	/*
3018	 * This routine figures out when the next poll should be sent.
3019	 * That turns out to be wickedly complicated. One problem is
3020	 * that sometimes the time for the next poll is in the past when
3021	 * the poll interval is reduced. We watch out for races here
3022	 * between the receive process and the poll process.
3023	 *
3024	 * Clamp the poll interval between minpoll and maxpoll.
3025	 */
3026	hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
3027
3028#ifdef AUTOKEY
3029	/*
3030	 * If during the crypto protocol the poll interval has changed,
3031	 * the lifetimes in the key list are probably bogus. Purge the
3032	 * the key list and regenerate it later.
3033	 */
3034	if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
3035		key_expire(peer);
3036#endif	/* AUTOKEY */
3037	peer->hpoll = hpoll;
3038
3039	/*
3040	 * There are three variables important for poll scheduling, the
3041	 * current time (current_time), next scheduled time (nextdate)
3042	 * and the earliest time (utemp). The earliest time is 2 s
3043	 * seconds, but could be more due to rate management. When
3044	 * sending in a burst, use the earliest time. When not in a
3045	 * burst but with a reply pending, send at the earliest time
3046	 * unless the next scheduled time has not advanced. This can
3047	 * only happen if multiple replies are pending in the same
3048	 * response interval. Otherwise, send at the later of the next
3049	 * scheduled time and the earliest time.
3050	 *
3051	 * Now we figure out if there is an override. If a burst is in
3052	 * progress and we get called from the receive process, just
3053	 * slink away. If called from the poll process, delay 1 s for a
3054	 * reference clock, otherwise 2 s.
3055	 */
3056	utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
3057	    (1 << peer->minpoll), ntp_minpkt);
3058
3059 	/*[Bug 3592] avoid unlimited postpone of next poll */
3060	limit = (2u << hpoll);
3061	if (limit > 64)
3062		limit -= (limit >> 2);
3063	limit += peer->outdate;
3064	if (limit < current_time)
3065		limit = current_time;
3066
3067	if (peer->burst > 0) {
3068		if (peer->nextdate > current_time)
3069			return;
3070#ifdef REFCLOCK
3071		else if (peer->flags & FLAG_REFCLOCK)
3072			peer->nextdate = current_time + RESP_DELAY;
3073#endif /* REFCLOCK */
3074		else
3075			peer->nextdate = utemp;
3076
3077#ifdef AUTOKEY
3078	/*
3079	 * If a burst is not in progress and a crypto response message
3080	 * is pending, delay 2 s, but only if this is a new interval.
3081	 */
3082	} else if (peer->cmmd != NULL) {
3083		if (peer->nextdate > current_time) {
3084			if (peer->nextdate + ntp_minpkt != utemp)
3085				peer->nextdate = utemp;
3086		} else {
3087			peer->nextdate = utemp;
3088		}
3089#endif	/* AUTOKEY */
3090
3091	/*
3092	 * The ordinary case. If a retry, use minpoll; if unreachable,
3093	 * use host poll; otherwise, use the minimum of host and peer
3094	 * polls; In other words, oversampling is okay but
3095	 * understampling is evil. Use the maximum of this value and the
3096	 * headway. If the average headway is greater than the headway
3097	 * threshold, increase the headway by the minimum interval.
3098	 */
3099	} else {
3100		if (peer->retry > 0)
3101			hpoll = peer->minpoll;
3102		else
3103			hpoll = min(peer->ppoll, peer->hpoll);
3104#ifdef REFCLOCK
3105		if (peer->flags & FLAG_REFCLOCK)
3106			next = 1 << hpoll;
3107		else
3108#endif /* REFCLOCK */
3109			next = ((0x1000UL | (ntp_random() & 0x0ff)) <<
3110			    hpoll) >> 12;
3111		next += peer->outdate;
3112		/* XXX: bug3596: Deal with poll skew list? */
3113		if (skewpoll) {
3114			psl_item psi;
3115
3116			if (0 == get_pollskew(hpoll, &psi)) {
3117				int sub = psi.sub;
3118				int qty = psi.qty;
3119				int msk = psi.msk;
3120				int val;
3121
3122				if (   0 != sub
3123				    || 0 != qty) {
3124				    	do {
3125						val = ntp_random() & msk;
3126					} while (val > qty);
3127
3128					next -= sub;
3129					next += val;
3130				}
3131			} else {
3132				/* get_pollskew() already logged this */
3133			}
3134		}
3135		if (next > utemp)
3136			peer->nextdate = next;
3137		else
3138			peer->nextdate = utemp;
3139		if (peer->throttle > (1 << peer->minpoll))
3140			peer->nextdate += ntp_minpkt;
3141	}
3142
3143 	/*[Bug 3592] avoid unlimited postpone of next poll */
3144	if (peer->nextdate > limit) {
3145		DPRINTF(1, ("poll_update: clamp reached; limit %lu next %lu\n",
3146			    limit, peer->nextdate));
3147		peer->nextdate = limit;
3148	}
3149	DPRINTF(2, ("poll_update: at %lu %s poll %d burst %d retry %d head %d early %lu next %lu\n",
3150		    current_time, ntoa(&peer->srcadr), peer->hpoll,
3151		    peer->burst, peer->retry, peer->throttle,
3152		    utemp - current_time, peer->nextdate -
3153		    current_time));
3154}
3155
3156
3157/*
3158 * peer_clear - clear peer filter registers.  See Section 3.4.8 of the
3159 * spec.
3160 */
3161void
3162peer_clear(
3163	struct peer *peer,		/* peer structure */
3164	const char *ident		/* tally lights */
3165	)
3166{
3167	u_char	u;
3168	l_fp	bxmt = peer->bxmt;	/* bcast clients retain this! */
3169
3170#ifdef AUTOKEY
3171	/*
3172	 * If cryptographic credentials have been acquired, toss them to
3173	 * Valhalla. Note that autokeys are ephemeral, in that they are
3174	 * tossed immediately upon use. Therefore, the keylist can be
3175	 * purged anytime without needing to preserve random keys. Note
3176	 * that, if the peer is purged, the cryptographic variables are
3177	 * purged, too. This makes it much harder to sneak in some
3178	 * unauthenticated data in the clock filter.
3179	 */
3180	key_expire(peer);
3181	if (peer->iffval != NULL)
3182		BN_free(peer->iffval);
3183	value_free(&peer->cookval);
3184	value_free(&peer->recval);
3185	value_free(&peer->encrypt);
3186	value_free(&peer->sndval);
3187	if (peer->cmmd != NULL)
3188		free(peer->cmmd);
3189	if (peer->subject != NULL)
3190		free(peer->subject);
3191	if (peer->issuer != NULL)
3192		free(peer->issuer);
3193#endif /* AUTOKEY */
3194
3195	/*
3196	 * Clear all values, including the optional crypto values above.
3197	 */
3198	memset(CLEAR_TO_ZERO(peer), 0, LEN_CLEAR_TO_ZERO(peer));
3199	peer->ppoll = peer->maxpoll;
3200	peer->hpoll = peer->minpoll;
3201	peer->disp = MAXDISPERSE;
3202	peer->flash = peer_unfit(peer);
3203	peer->jitter = LOGTOD(sys_precision);
3204
3205	/* Don't throw away our broadcast replay protection */
3206	if (peer->hmode == MODE_BCLIENT)
3207		peer->bxmt = bxmt;
3208
3209	/*
3210	 * If interleave mode, initialize the alternate origin switch.
3211	 */
3212	if (peer->flags & FLAG_XLEAVE)
3213		peer->flip = 1;
3214	for (u = 0; u < NTP_SHIFT; u++) {
3215		peer->filter_order[u] = u;
3216		peer->filter_disp[u] = MAXDISPERSE;
3217	}
3218#ifdef REFCLOCK
3219	if (!(peer->flags & FLAG_REFCLOCK)) {
3220#endif
3221		peer->leap = LEAP_NOTINSYNC;
3222		peer->stratum = STRATUM_UNSPEC;
3223		memcpy(&peer->refid, ident, 4);
3224#ifdef REFCLOCK
3225	} else {
3226		/* Clear refclock sample filter */
3227		peer->procptr->codeproc = 0;
3228		peer->procptr->coderecv = 0;
3229	}
3230#endif
3231
3232	/*
3233	 * During initialization use the association count to spread out
3234	 * the polls at one-second intervals. Passive associations'
3235	 * first poll is delayed by the "discard minimum" to avoid rate
3236	 * limiting. Other post-startup new or cleared associations
3237	 * randomize the first poll over the minimum poll interval to
3238	 * avoid implosion.
3239	 */
3240	peer->nextdate = peer->update = peer->outdate = current_time;
3241	if (initializing) {
3242		peer->nextdate += peer_associations;
3243	} else if (MODE_PASSIVE == peer->hmode) {
3244		peer->nextdate += ntp_minpkt;
3245	} else {
3246		peer->nextdate += ntp_random() % peer->minpoll;
3247	}
3248#ifdef AUTOKEY
3249	peer->refresh = current_time + (1 << NTP_REFRESH);
3250#endif	/* AUTOKEY */
3251	DPRINTF(1, ("peer_clear: at %ld next %ld associd %d refid %s\n",
3252		    current_time, peer->nextdate, peer->associd,
3253		    ident));
3254}
3255
3256
3257/*
3258 * clock_filter - add incoming clock sample to filter register and run
3259 *		  the filter procedure to find the best sample.
3260 */
3261void
3262clock_filter(
3263	struct peer *peer,		/* peer structure pointer */
3264	double	sample_offset,		/* clock offset */
3265	double	sample_delay,		/* roundtrip delay */
3266	double	sample_disp		/* dispersion */
3267	)
3268{
3269	double	dst[NTP_SHIFT];		/* distance vector */
3270	int	ord[NTP_SHIFT];		/* index vector */
3271	int	i, j, k, m;
3272	double	dtemp, etemp;
3273	char	tbuf[80];
3274
3275	/*
3276	 * A sample consists of the offset, delay, dispersion and epoch
3277	 * of arrival. The offset and delay are determined by the on-
3278	 * wire protocol. The dispersion grows from the last outbound
3279	 * packet to the arrival of this one increased by the sum of the
3280	 * peer precision and the system precision as required by the
3281	 * error budget. First, shift the new arrival into the shift
3282	 * register discarding the oldest one.
3283	 */
3284	j = peer->filter_nextpt;
3285	peer->filter_offset[j] = sample_offset;
3286	peer->filter_delay[j] = sample_delay;
3287	peer->filter_disp[j] = sample_disp;
3288	peer->filter_epoch[j] = current_time;
3289	j = (j + 1) % NTP_SHIFT;
3290	peer->filter_nextpt = j;
3291
3292	/*
3293	 * Update dispersions since the last update and at the same
3294	 * time initialize the distance and index lists. Since samples
3295	 * become increasingly uncorrelated beyond the Allan intercept,
3296	 * only under exceptional cases will an older sample be used.
3297	 * Therefore, the distance list uses a compound metric. If the
3298	 * dispersion is greater than the maximum dispersion, clamp the
3299	 * distance at that value. If the time since the last update is
3300	 * less than the Allan intercept use the delay; otherwise, use
3301	 * the sum of the delay and dispersion.
3302	 */
3303	dtemp = clock_phi * (current_time - peer->update);
3304	peer->update = current_time;
3305	for (i = NTP_SHIFT - 1; i >= 0; i--) {
3306		if (i != 0)
3307			peer->filter_disp[j] += dtemp;
3308		if (peer->filter_disp[j] >= MAXDISPERSE) {
3309			peer->filter_disp[j] = MAXDISPERSE;
3310			dst[i] = MAXDISPERSE;
3311		} else if (peer->update - peer->filter_epoch[j] >
3312		    (u_long)ULOGTOD(allan_xpt)) {
3313			dst[i] = peer->filter_delay[j] +
3314			    peer->filter_disp[j];
3315		} else {
3316			dst[i] = peer->filter_delay[j];
3317		}
3318		ord[i] = j;
3319		j = (j + 1) % NTP_SHIFT;
3320	}
3321
3322	/*
3323	 * If the clock has stabilized, sort the samples by distance.
3324	 */
3325	if (freq_cnt == 0) {
3326		for (i = 1; i < NTP_SHIFT; i++) {
3327			for (j = 0; j < i; j++) {
3328				if (dst[j] > dst[i]) {
3329					k = ord[j];
3330					ord[j] = ord[i];
3331					ord[i] = k;
3332					etemp = dst[j];
3333					dst[j] = dst[i];
3334					dst[i] = etemp;
3335				}
3336			}
3337		}
3338	}
3339
3340	/*
3341	 * Copy the index list to the association structure so ntpq
3342	 * can see it later. Prune the distance list to leave only
3343	 * samples less than the maximum dispersion, which disfavors
3344	 * uncorrelated samples older than the Allan intercept. To
3345	 * further improve the jitter estimate, of the remainder leave
3346	 * only samples less than the maximum distance, but keep at
3347	 * least two samples for jitter calculation.
3348	 */
3349	m = 0;
3350	for (i = 0; i < NTP_SHIFT; i++) {
3351		peer->filter_order[i] = (u_char) ord[i];
3352		if (   dst[i] >= MAXDISPERSE
3353		    || (m >= 2 && dst[i] >= sys_maxdist))
3354			continue;
3355		m++;
3356	}
3357
3358	/*
3359	 * Compute the dispersion and jitter. The dispersion is weighted
3360	 * exponentially by NTP_FWEIGHT (0.5) so it is normalized close
3361	 * to 1.0. The jitter is the RMS differences relative to the
3362	 * lowest delay sample.
3363	 */
3364	peer->disp = peer->jitter = 0;
3365	k = ord[0];
3366	for (i = NTP_SHIFT - 1; i >= 0; i--) {
3367		j = ord[i];
3368		peer->disp = NTP_FWEIGHT * (peer->disp +
3369		    peer->filter_disp[j]);
3370		if (i < m)
3371			peer->jitter += DIFF(peer->filter_offset[j],
3372			    peer->filter_offset[k]);
3373	}
3374
3375	/*
3376	 * If no acceptable samples remain in the shift register,
3377	 * quietly tiptoe home leaving only the dispersion. Otherwise,
3378	 * save the offset, delay and jitter. Note the jitter must not
3379	 * be less than the precision.
3380	 */
3381	if (m == 0) {
3382		clock_select();
3383		return;
3384	}
3385	etemp = fabs(peer->offset - peer->filter_offset[k]);
3386	peer->offset = peer->filter_offset[k];
3387	peer->delay = peer->filter_delay[k];
3388	if (m > 1)
3389		peer->jitter /= m - 1;
3390	peer->jitter = max(SQRT(peer->jitter), LOGTOD(sys_precision));
3391
3392	/*
3393	 * If the the new sample and the current sample are both valid
3394	 * and the difference between their offsets exceeds CLOCK_SGATE
3395	 * (3) times the jitter and the interval between them is less
3396	 * than twice the host poll interval, consider the new sample
3397	 * a popcorn spike and ignore it.
3398	 */
3399	if (   peer->disp < sys_maxdist
3400	    && peer->filter_disp[k] < sys_maxdist
3401	    && etemp > CLOCK_SGATE * peer->jitter
3402	    && peer->filter_epoch[k] - peer->epoch
3403	       < 2. * ULOGTOD(peer->hpoll)) {
3404		snprintf(tbuf, sizeof(tbuf), "%.6f s", etemp);
3405		report_event(PEVNT_POPCORN, peer, tbuf);
3406		return;
3407	}
3408
3409	/*
3410	 * A new minimum sample is useful only if it is later than the
3411	 * last one used. In this design the maximum lifetime of any
3412	 * sample is not greater than eight times the poll interval, so
3413	 * the maximum interval between minimum samples is eight
3414	 * packets.
3415	 */
3416	if (peer->filter_epoch[k] <= peer->epoch) {
3417	DPRINTF(2, ("clock_filter: old sample %lu\n", current_time -
3418		    peer->filter_epoch[k]));
3419		return;
3420	}
3421	peer->epoch = peer->filter_epoch[k];
3422
3423	/*
3424	 * The mitigated sample statistics are saved for later
3425	 * processing. If not synchronized or not in a burst, tickle the
3426	 * clock select algorithm.
3427	 */
3428	record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
3429	    peer->offset, peer->delay, peer->disp, peer->jitter);
3430	DPRINTF(1, ("clock_filter: n %d off %.6f del %.6f dsp %.6f jit %.6f\n",
3431		    m, peer->offset, peer->delay, peer->disp,
3432		    peer->jitter));
3433	if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC)
3434		clock_select();
3435}
3436
3437
3438/*
3439 * clock_select - find the pick-of-the-litter clock
3440 *
3441 * LOCKCLOCK: (1) If the local clock is the prefer peer, it will always
3442 * be enabled, even if declared falseticker, (2) only the prefer peer
3443 * can be selected as the system peer, (3) if the external source is
3444 * down, the system leap bits are set to 11 and the stratum set to
3445 * infinity.
3446 */
3447void
3448clock_select(void)
3449{
3450	struct peer *peer;
3451	int	i, j, k, n;
3452	int	nlist, nl2;
3453	int	allow;
3454	int	speer;
3455	double	d, e, f, g;
3456	double	high, low;
3457	double	speermet;
3458	double	orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
3459	struct endpoint endp;
3460	struct peer *osys_peer;
3461	struct peer *sys_prefer = NULL;	/* prefer peer */
3462	struct peer *typesystem = NULL;
3463	struct peer *typeorphan = NULL;
3464#ifdef REFCLOCK
3465	struct peer *typeacts = NULL;
3466	struct peer *typelocal = NULL;
3467	struct peer *typepps = NULL;
3468#endif /* REFCLOCK */
3469	static struct endpoint *endpoint = NULL;
3470	static int *indx = NULL;
3471	static peer_select *peers = NULL;
3472	static u_int endpoint_size = 0;
3473	static u_int peers_size = 0;
3474	static u_int indx_size = 0;
3475	size_t octets;
3476
3477	/*
3478	 * Initialize and create endpoint, index and peer lists big
3479	 * enough to handle all associations.
3480	 */
3481	osys_peer = sys_peer;
3482	sys_survivors = 0;
3483#ifdef LOCKCLOCK
3484	set_sys_leap(LEAP_NOTINSYNC);
3485	sys_stratum = STRATUM_UNSPEC;
3486	memcpy(&sys_refid, "DOWN", 4);
3487#endif /* LOCKCLOCK */
3488
3489	/*
3490	 * Allocate dynamic space depending on the number of
3491	 * associations.
3492	 */
3493	nlist = 1;
3494	for (peer = peer_list; peer != NULL; peer = peer->p_link)
3495		nlist++;
3496	endpoint_size = ALIGNED_SIZE(nlist * 2 * sizeof(*endpoint));
3497	peers_size = ALIGNED_SIZE(nlist * sizeof(*peers));
3498	indx_size = ALIGNED_SIZE(nlist * 2 * sizeof(*indx));
3499	octets = endpoint_size + peers_size + indx_size;
3500	endpoint = erealloc(endpoint, octets);
3501	peers = INC_ALIGNED_PTR(endpoint, endpoint_size);
3502	indx = INC_ALIGNED_PTR(peers, peers_size);
3503
3504	/*
3505	 * Initially, we populate the island with all the rifraff peers
3506	 * that happen to be lying around. Those with seriously
3507	 * defective clocks are immediately booted off the island. Then,
3508	 * the falsetickers are culled and put to sea. The truechimers
3509	 * remaining are subject to repeated rounds where the most
3510	 * unpopular at each round is kicked off. When the population
3511	 * has dwindled to sys_minclock, the survivors split a million
3512	 * bucks and collectively crank the chimes.
3513	 */
3514	nlist = nl2 = 0;	/* none yet */
3515	for (peer = peer_list; peer != NULL; peer = peer->p_link) {
3516		peer->new_status = CTL_PST_SEL_REJECT;
3517
3518		/*
3519		 * Leave the island immediately if the peer is
3520		 * unfit to synchronize.
3521		 */
3522		if (peer_unfit(peer)) {
3523			continue;
3524		}
3525
3526		/*
3527		 * If this peer is an orphan parent, elect the
3528		 * one with the lowest metric defined as the
3529		 * IPv4 address or the first 64 bits of the
3530		 * hashed IPv6 address.  To ensure convergence
3531		 * on the same selected orphan, consider as
3532		 * well that this system may have the lowest
3533		 * metric and be the orphan parent.  If this
3534		 * system wins, sys_peer will be NULL to trigger
3535		 * orphan mode in timer().
3536		 */
3537		if (peer->stratum == sys_orphan) {
3538			u_int32	localmet;
3539			u_int32 peermet;
3540
3541			if (peer->dstadr != NULL)
3542				localmet = ntohl(peer->dstadr->addr_refid);
3543			else
3544				localmet = U_INT32_MAX;
3545			peermet = ntohl(addr2refid(&peer->srcadr));
3546			if (peermet < localmet && peermet < orphmet) {
3547				typeorphan = peer;
3548				orphmet = peermet;
3549			}
3550			continue;
3551		}
3552
3553		/*
3554		 * If this peer could have the orphan parent
3555		 * as a synchronization ancestor, exclude it
3556		 * from selection to avoid forming a
3557		 * synchronization loop within the orphan mesh,
3558		 * triggering stratum climb to infinity
3559		 * instability.  Peers at stratum higher than
3560		 * the orphan stratum could have the orphan
3561		 * parent in ancestry so are excluded.
3562		 * See http://bugs.ntp.org/2050
3563		 */
3564		if (peer->stratum > sys_orphan) {
3565			continue;
3566		}
3567#ifdef REFCLOCK
3568		/*
3569		 * The following are special cases. We deal
3570		 * with them later.
3571		 */
3572		if (!(peer->flags & FLAG_PREFER)) {
3573			switch (peer->refclktype) {
3574			case REFCLK_LOCALCLOCK:
3575				if (   current_time > orphwait
3576				    && typelocal == NULL)
3577					typelocal = peer;
3578				continue;
3579
3580			case REFCLK_ACTS:
3581				if (   current_time > orphwait
3582				    && typeacts == NULL)
3583					typeacts = peer;
3584				continue;
3585			}
3586		}
3587#endif /* REFCLOCK */
3588
3589		/*
3590		 * If we get this far, the peer can stay on the
3591		 * island, but does not yet have the immunity
3592		 * idol.
3593		 */
3594		peer->new_status = CTL_PST_SEL_SANE;
3595		f = root_distance(peer);
3596		peers[nlist].peer = peer;
3597		peers[nlist].error = peer->jitter;
3598		peers[nlist].synch = f;
3599		nlist++;
3600
3601		/*
3602		 * Insert each interval endpoint on the unsorted
3603		 * endpoint[] list.
3604		 */
3605		e = peer->offset;
3606		endpoint[nl2].type = -1;	/* lower end */
3607		endpoint[nl2].val = e - f;
3608		nl2++;
3609		endpoint[nl2].type = 1;		/* upper end */
3610		endpoint[nl2].val = e + f;
3611		nl2++;
3612	}
3613	/*
3614	 * Construct sorted indx[] of endpoint[] indexes ordered by
3615	 * offset.
3616	 */
3617	for (i = 0; i < nl2; i++)
3618		indx[i] = i;
3619	for (i = 0; i < nl2; i++) {
3620		endp = endpoint[indx[i]];
3621		e = endp.val;
3622		k = i;
3623		for (j = i + 1; j < nl2; j++) {
3624			endp = endpoint[indx[j]];
3625			if (endp.val < e) {
3626				e = endp.val;
3627				k = j;
3628			}
3629		}
3630		if (k != i) {
3631			j = indx[k];
3632			indx[k] = indx[i];
3633			indx[i] = j;
3634		}
3635	}
3636	for (i = 0; i < nl2; i++)
3637		DPRINTF(3, ("select: endpoint %2d %.6f\n",
3638			endpoint[indx[i]].type, endpoint[indx[i]].val));
3639
3640	/*
3641	 * This is the actual algorithm that cleaves the truechimers
3642	 * from the falsetickers. The original algorithm was described
3643	 * in Keith Marzullo's dissertation, but has been modified for
3644	 * better accuracy.
3645	 *
3646	 * Briefly put, we first assume there are no falsetickers, then
3647	 * scan the candidate list first from the low end upwards and
3648	 * then from the high end downwards. The scans stop when the
3649	 * number of intersections equals the number of candidates less
3650	 * the number of falsetickers. If this doesn't happen for a
3651	 * given number of falsetickers, we bump the number of
3652	 * falsetickers and try again. If the number of falsetickers
3653	 * becomes equal to or greater than half the number of
3654	 * candidates, the Albanians have won the Byzantine wars and
3655	 * correct synchronization is not possible.
3656	 *
3657	 * Here, nlist is the number of candidates and allow is the
3658	 * number of falsetickers. Upon exit, the truechimers are the
3659	 * survivors with offsets not less than low and not greater than
3660	 * high. There may be none of them.
3661	 */
3662	low = 1e9;
3663	high = -1e9;
3664	for (allow = 0; 2 * allow < nlist; allow++) {
3665
3666		/*
3667		 * Bound the interval (low, high) as the smallest
3668		 * interval containing points from the most sources.
3669		 */
3670		n = 0;
3671		for (i = 0; i < nl2; i++) {
3672			low = endpoint[indx[i]].val;
3673			n -= endpoint[indx[i]].type;
3674			if (n >= nlist - allow)
3675				break;
3676		}
3677		n = 0;
3678		for (j = nl2 - 1; j >= 0; j--) {
3679			high = endpoint[indx[j]].val;
3680			n += endpoint[indx[j]].type;
3681			if (n >= nlist - allow)
3682				break;
3683		}
3684
3685		/*
3686		 * If an interval containing truechimers is found, stop.
3687		 * If not, increase the number of falsetickers and go
3688		 * around again.
3689		 */
3690		if (high > low)
3691			break;
3692	}
3693
3694	/*
3695	 * Clustering algorithm. Whittle candidate list of falsetickers,
3696	 * who leave the island immediately. The TRUE peer is always a
3697	 * truechimer. We must leave at least one peer to collect the
3698	 * million bucks.
3699	 *
3700	 * We assert the correct time is contained in the interval, but
3701	 * the best offset estimate for the interval might not be
3702	 * contained in the interval. For this purpose, a truechimer is
3703	 * defined as the midpoint of an interval that overlaps the
3704	 * intersection interval.
3705	 */
3706	j = 0;
3707	for (i = 0; i < nlist; i++) {
3708		double	h;
3709
3710		peer = peers[i].peer;
3711		h = peers[i].synch;
3712		if ((   high <= low
3713		     || peer->offset + h < low
3714		     || peer->offset - h > high
3715		    ) && !(peer->flags & FLAG_TRUE))
3716			continue;
3717
3718#ifdef REFCLOCK
3719		/*
3720		 * Eligible PPS peers must survive the intersection
3721		 * algorithm. Use the first one found, but don't
3722		 * include any of them in the cluster population.
3723		 */
3724		if (peer->flags & FLAG_PPS) {
3725			if (typepps == NULL)
3726				typepps = peer;
3727			if (!(peer->flags & FLAG_TSTAMP_PPS))
3728				continue;
3729		}
3730#endif /* REFCLOCK */
3731
3732		if (j != i)
3733			peers[j] = peers[i];
3734		j++;
3735	}
3736	nlist = j;
3737
3738	/*
3739	 * If no survivors remain at this point, check if the modem
3740	 * driver, local driver or orphan parent in that order. If so,
3741	 * nominate the first one found as the only survivor.
3742	 * Otherwise, give up and leave the island to the rats.
3743	 */
3744	if (nlist == 0) {
3745		peers[0].error = 0;
3746		peers[0].synch = sys_mindisp;
3747#ifdef REFCLOCK
3748		if (typeacts != NULL) {
3749			peers[0].peer = typeacts;
3750			nlist = 1;
3751		} else if (typelocal != NULL) {
3752			peers[0].peer = typelocal;
3753			nlist = 1;
3754		} else
3755#endif /* REFCLOCK */
3756		if (typeorphan != NULL) {
3757			peers[0].peer = typeorphan;
3758			nlist = 1;
3759		}
3760	}
3761
3762	/*
3763	 * Mark the candidates at this point as truechimers.
3764	 */
3765	for (i = 0; i < nlist; i++) {
3766		peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
3767		DPRINTF(2, ("select: survivor %s %f\n",
3768			stoa(&peers[i].peer->srcadr), peers[i].synch));
3769	}
3770
3771	/*
3772	 * Now, vote outliers off the island by select jitter weighted
3773	 * by root distance. Continue voting as long as there are more
3774	 * than sys_minclock survivors and the select jitter of the peer
3775	 * with the worst metric is greater than the minimum peer
3776	 * jitter. Stop if we are about to discard a TRUE or PREFER
3777	 * peer, who of course have the immunity idol.
3778	 */
3779	while (1) {
3780		d = 1e9;
3781		e = -1e9;
3782		g = 0;
3783		k = 0;
3784		for (i = 0; i < nlist; i++) {
3785			if (peers[i].error < d)
3786				d = peers[i].error;
3787			peers[i].seljit = 0;
3788			if (nlist > 1) {
3789				f = 0;
3790				for (j = 0; j < nlist; j++)
3791					f += DIFF(peers[j].peer->offset,
3792					    peers[i].peer->offset);
3793				peers[i].seljit = SQRT(f / (nlist - 1));
3794			}
3795			if (peers[i].seljit * peers[i].synch > e) {
3796				g = peers[i].seljit;
3797				e = peers[i].seljit * peers[i].synch;
3798				k = i;
3799			}
3800		}
3801		g = max(g, LOGTOD(sys_precision));
3802		if (   nlist <= max(1, sys_minclock)
3803		    || g <= d
3804		    || ((FLAG_TRUE | FLAG_PREFER) & peers[k].peer->flags))
3805			break;
3806
3807		DPRINTF(3, ("select: drop %s seljit %.6f jit %.6f\n",
3808			ntoa(&peers[k].peer->srcadr), g, d));
3809		if (nlist > sys_maxclock)
3810			peers[k].peer->new_status = CTL_PST_SEL_EXCESS;
3811		for (j = k + 1; j < nlist; j++)
3812			peers[j - 1] = peers[j];
3813		nlist--;
3814	}
3815
3816	/*
3817	 * What remains is a list usually not greater than sys_minclock
3818	 * peers. Note that unsynchronized peers cannot survive this
3819	 * far.  Count and mark these survivors.
3820	 *
3821	 * While at it, count the number of leap warning bits found.
3822	 * This will be used later to vote the system leap warning bit.
3823	 * If a leap warning bit is found on a reference clock, the vote
3824	 * is always won.
3825	 *
3826	 * Choose the system peer using a hybrid metric composed of the
3827	 * selection jitter scaled by the root distance augmented by
3828	 * stratum scaled by sys_mindisp (.001 by default). The goal of
3829	 * the small stratum factor is to avoid clockhop between a
3830	 * reference clock and a network peer which has a refclock and
3831	 * is using an older ntpd, which does not floor sys_rootdisp at
3832	 * sys_mindisp.
3833	 *
3834	 * In contrast, ntpd 4.2.6 and earlier used stratum primarily
3835	 * in selecting the system peer, using a weight of 1 second of
3836	 * additional root distance per stratum.  This heavy bias is no
3837	 * longer appropriate, as the scaled root distance provides a
3838	 * more rational metric carrying the cumulative error budget.
3839	 */
3840	e = 1e9;
3841	speer = 0;
3842	leap_vote_ins = 0;
3843	leap_vote_del = 0;
3844	for (i = 0; i < nlist; i++) {
3845		peer = peers[i].peer;
3846		peer->unreach = 0;
3847		peer->new_status = CTL_PST_SEL_SYNCCAND;
3848		sys_survivors++;
3849		if (peer->leap == LEAP_ADDSECOND) {
3850			if (peer->flags & FLAG_REFCLOCK)
3851				leap_vote_ins = nlist;
3852			else if (leap_vote_ins < nlist)
3853				leap_vote_ins++;
3854		}
3855		if (peer->leap == LEAP_DELSECOND) {
3856			if (peer->flags & FLAG_REFCLOCK)
3857				leap_vote_del = nlist;
3858			else if (leap_vote_del < nlist)
3859				leap_vote_del++;
3860		}
3861		if (peer->flags & FLAG_PREFER)
3862			sys_prefer = peer;
3863		speermet = peers[i].seljit * peers[i].synch +
3864		    peer->stratum * sys_mindisp;
3865		if (speermet < e) {
3866			e = speermet;
3867			speer = i;
3868		}
3869	}
3870
3871	/*
3872	 * Unless there are at least sys_misane survivors, leave the
3873	 * building dark. Otherwise, do a clockhop dance. Ordinarily,
3874	 * use the selected survivor speer. However, if the current
3875	 * system peer is not speer, stay with the current system peer
3876	 * as long as it doesn't get too old or too ugly.
3877	 */
3878	if (nlist > 0 && nlist >= sys_minsane) {
3879		double	x;
3880
3881		typesystem = peers[speer].peer;
3882		if (osys_peer == NULL || osys_peer == typesystem) {
3883			sys_clockhop = 0;
3884		} else if ((x = fabs(typesystem->offset -
3885		    osys_peer->offset)) < sys_mindisp) {
3886			if (sys_clockhop == 0)
3887				sys_clockhop = sys_mindisp;
3888			else
3889				sys_clockhop *= .5;
3890			DPRINTF(1, ("select: clockhop %d %.6f %.6f\n",
3891				j, x, sys_clockhop));
3892			if (fabs(x) < sys_clockhop)
3893				typesystem = osys_peer;
3894			else
3895				sys_clockhop = 0;
3896		} else {
3897			sys_clockhop = 0;
3898		}
3899	}
3900
3901	/*
3902	 * Mitigation rules of the game. We have the pick of the
3903	 * litter in typesystem if any survivors are left. If
3904	 * there is a prefer peer, use its offset and jitter.
3905	 * Otherwise, use the combined offset and jitter of all kitters.
3906	 */
3907	if (typesystem != NULL) {
3908		if (sys_prefer == NULL) {
3909			typesystem->new_status = CTL_PST_SEL_SYSPEER;
3910			clock_combine(peers, sys_survivors, speer);
3911		} else {
3912			typesystem = sys_prefer;
3913			sys_clockhop = 0;
3914			typesystem->new_status = CTL_PST_SEL_SYSPEER;
3915			sys_offset = typesystem->offset;
3916			sys_jitter = typesystem->jitter;
3917		}
3918		DPRINTF(1, ("select: combine offset %.9f jitter %.9f\n",
3919			sys_offset, sys_jitter));
3920	}
3921#ifdef REFCLOCK
3922	/*
3923	 * If a PPS driver is lit and the combined offset is less than
3924	 * 0.4 s, select the driver as the PPS peer and use its offset
3925	 * and jitter. However, if this is the atom driver, use it only
3926	 * if there is a prefer peer or there are no survivors and none
3927	 * are required.
3928	 */
3929	if (   typepps != NULL
3930	    && fabs(sys_offset) < 0.4
3931	    && (   typepps->refclktype != REFCLK_ATOM_PPS
3932		|| (   typepps->refclktype == REFCLK_ATOM_PPS
3933		    && (   sys_prefer != NULL
3934			|| (typesystem == NULL && sys_minsane == 0))))) {
3935		typesystem = typepps;
3936		sys_clockhop = 0;
3937		typesystem->new_status = CTL_PST_SEL_PPS;
3938		sys_offset = typesystem->offset;
3939		sys_jitter = typesystem->jitter;
3940		DPRINTF(1, ("select: pps offset %.9f jitter %.9f\n",
3941			sys_offset, sys_jitter));
3942	}
3943#endif /* REFCLOCK */
3944
3945	/*
3946	 * If there are no survivors at this point, there is no
3947	 * system peer. If so and this is an old update, keep the
3948	 * current statistics, but do not update the clock.
3949	 */
3950	if (typesystem == NULL) {
3951		if (osys_peer != NULL) {
3952			if (sys_orphwait > 0)
3953				orphwait = current_time + sys_orphwait;
3954			report_event(EVNT_NOPEER, NULL, NULL);
3955		}
3956		sys_peer = NULL;
3957		for (peer = peer_list; peer != NULL; peer = peer->p_link)
3958			peer->status = peer->new_status;
3959		return;
3960	}
3961
3962	/*
3963	 * Do not use old data, as this may mess up the clock discipline
3964	 * stability.
3965	 */
3966	if (typesystem->epoch <= sys_epoch)
3967		return;
3968
3969	/*
3970	 * We have found the alpha male. Wind the clock.
3971	 */
3972	if (osys_peer != typesystem)
3973		report_event(PEVNT_NEWPEER, typesystem, NULL);
3974	for (peer = peer_list; peer != NULL; peer = peer->p_link)
3975		peer->status = peer->new_status;
3976	clock_update(typesystem);
3977}
3978
3979
3980static void
3981clock_combine(
3982	peer_select *	peers,	/* survivor list */
3983	int		npeers,	/* number of survivors */
3984	int		syspeer	/* index of sys.peer */
3985	)
3986{
3987	int	i;
3988	double	x, y, z, w;
3989
3990	y = z = w = 0;
3991	for (i = 0; i < npeers; i++) {
3992		x = 1. / peers[i].synch;
3993		y += x;
3994		z += x * peers[i].peer->offset;
3995		w += x * DIFF(peers[i].peer->offset,
3996		    peers[syspeer].peer->offset);
3997	}
3998	sys_offset = z / y;
3999	sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
4000}
4001
4002
4003/*
4004 * root_distance - compute synchronization distance from peer to root
4005 */
4006static double
4007root_distance(
4008	struct peer *peer	/* peer structure pointer */
4009	)
4010{
4011	double	dtemp;
4012
4013	/*
4014	 * Root Distance (LAMBDA) is defined as:
4015	 * (delta + DELTA)/2 + epsilon + EPSILON + D
4016	 *
4017	 * where:
4018	 *  delta   is the round-trip delay
4019	 *  DELTA   is the root delay
4020	 *  epsilon is the peer dispersion
4021	 *	    + (15 usec each second)
4022	 *  EPSILON is the root dispersion
4023	 *  D       is sys_jitter
4024	 *
4025	 * NB: Think hard about why we are using these values, and what
4026	 * the alternatives are, and the various pros/cons.
4027	 *
4028	 * DLM thinks these are probably the best choices from any of the
4029	 * other worse choices.
4030	 */
4031	dtemp = (peer->delay + peer->rootdelay) / 2
4032		+ peer->disp
4033		  + clock_phi * (current_time - peer->update)
4034		+ peer->rootdisp
4035		+ peer->jitter;
4036	/*
4037	 * Careful squeak here. The value returned must be greater than
4038	 * the minimum root dispersion in order to avoid clockhop with
4039	 * highly precise reference clocks. Note that the root distance
4040	 * cannot exceed the sys_maxdist, as this is the cutoff by the
4041	 * selection algorithm.
4042	 */
4043	if (dtemp < sys_mindisp)
4044		dtemp = sys_mindisp;
4045	return (dtemp);
4046}
4047
4048
4049/*
4050 * peer_xmit - send packet for persistent association.
4051 */
4052static void
4053peer_xmit(
4054	struct peer *peer	/* peer structure pointer */
4055	)
4056{
4057	struct pkt xpkt;	/* transmit packet */
4058	size_t	sendlen, authlen;
4059	keyid_t	xkeyid = 0;	/* transmit key ID */
4060	l_fp	xmt_tx, xmt_ty;
4061
4062	if (!peer->dstadr)	/* drop peers without interface */
4063		return;
4064
4065	xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, peer->version,
4066	    peer->hmode);
4067	xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4068	xpkt.ppoll = peer->hpoll;
4069	xpkt.precision = sys_precision;
4070	xpkt.refid = sys_refid;
4071	xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4072	xpkt.rootdisp =  HTONS_FP(DTOUFP(sys_rootdisp));
4073	/* Use sys_reftime for peer exchanges */
4074	HTONL_FP(&sys_reftime, &xpkt.reftime);
4075	HTONL_FP(&peer->rec, &xpkt.org);
4076	HTONL_FP(&peer->dst, &xpkt.rec);
4077
4078	/*
4079	 * If the received packet contains a MAC, the transmitted packet
4080	 * is authenticated and contains a MAC. If not, the transmitted
4081	 * packet is not authenticated.
4082	 *
4083	 * It is most important when autokey is in use that the local
4084	 * interface IP address be known before the first packet is
4085	 * sent. Otherwise, it is not possible to compute a correct MAC
4086	 * the recipient will accept. Thus, the I/O semantics have to do
4087	 * a little more work. In particular, the wildcard interface
4088	 * might not be usable.
4089	 */
4090	sendlen = LEN_PKT_NOMAC;
4091	if (
4092#ifdef AUTOKEY
4093	    !(peer->flags & FLAG_SKEY) &&
4094#endif	/* !AUTOKEY */
4095	    peer->keyid == 0) {
4096
4097		/*
4098		 * Transmit a-priori timestamps
4099		 */
4100		get_systime(&xmt_tx);
4101		if (peer->flip == 0) {	/* basic mode */
4102			peer->aorg = xmt_tx;
4103			HTONL_FP(&xmt_tx, &xpkt.xmt);
4104		} else {		/* interleaved modes */
4105			if (peer->hmode == MODE_BROADCAST) { /* bcst */
4106				HTONL_FP(&xmt_tx, &xpkt.xmt);
4107				if (peer->flip > 0)
4108					HTONL_FP(&peer->borg,
4109					    &xpkt.org);
4110				else
4111					HTONL_FP(&peer->aorg,
4112					    &xpkt.org);
4113			} else {	/* symmetric */
4114				if (peer->flip > 0)
4115					HTONL_FP(&peer->borg,
4116					    &xpkt.xmt);
4117				else
4118					HTONL_FP(&peer->aorg,
4119					    &xpkt.xmt);
4120			}
4121		}
4122		peer->t21_bytes = sendlen;
4123		sendpkt(&peer->srcadr, peer->dstadr,
4124			sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4125			&xpkt, sendlen);
4126		peer->sent++;
4127		peer->throttle += (1 << peer->minpoll) - 2;
4128
4129		/*
4130		 * Capture a-posteriori timestamps
4131		 */
4132		get_systime(&xmt_ty);
4133		if (peer->flip != 0) {		/* interleaved modes */
4134			if (peer->flip > 0)
4135				peer->aorg = xmt_ty;
4136			else
4137				peer->borg = xmt_ty;
4138			peer->flip = -peer->flip;
4139		}
4140		L_SUB(&xmt_ty, &xmt_tx);
4141		LFPTOD(&xmt_ty, peer->xleave);
4142		DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d len %zu xmt %#010x.%08x\n",
4143			    current_time,
4144			    peer->dstadr ? stoa(&peer->dstadr->sin) : "-",
4145			    stoa(&peer->srcadr), peer->hmode, sendlen,
4146			    xmt_tx.l_ui, xmt_tx.l_uf));
4147		return;
4148	}
4149
4150	/*
4151	 * Authentication is enabled, so the transmitted packet must be
4152	 * authenticated. If autokey is enabled, fuss with the various
4153	 * modes; otherwise, symmetric key cryptography is used.
4154	 */
4155#ifdef AUTOKEY
4156	if (peer->flags & FLAG_SKEY) {
4157		struct exten *exten;	/* extension field */
4158
4159		/*
4160		 * The Public Key Dance (PKD): Cryptographic credentials
4161		 * are contained in extension fields, each including a
4162		 * 4-octet length/code word followed by a 4-octet
4163		 * association ID and optional additional data. Optional
4164		 * data includes a 4-octet data length field followed by
4165		 * the data itself. Request messages are sent from a
4166		 * configured association; response messages can be sent
4167		 * from a configured association or can take the fast
4168		 * path without ever matching an association. Response
4169		 * messages have the same code as the request, but have
4170		 * a response bit and possibly an error bit set. In this
4171		 * implementation, a message may contain no more than
4172		 * one command and one or more responses.
4173		 *
4174		 * Cryptographic session keys include both a public and
4175		 * a private componet. Request and response messages
4176		 * using extension fields are always sent with the
4177		 * private component set to zero. Packets without
4178		 * extension fields indlude the private component when
4179		 * the session key is generated.
4180		 */
4181		while (1) {
4182
4183			/*
4184			 * Allocate and initialize a keylist if not
4185			 * already done. Then, use the list in inverse
4186			 * order, discarding keys once used. Keep the
4187			 * latest key around until the next one, so
4188			 * clients can use client/server packets to
4189			 * compute propagation delay.
4190			 *
4191			 * Note that once a key is used from the list,
4192			 * it is retained in the key cache until the
4193			 * next key is used. This is to allow a client
4194			 * to retrieve the encrypted session key
4195			 * identifier to verify authenticity.
4196			 *
4197			 * If for some reason a key is no longer in the
4198			 * key cache, a birthday has happened or the key
4199			 * has expired, so the pseudo-random sequence is
4200			 * broken. In that case, purge the keylist and
4201			 * regenerate it.
4202			 */
4203			if (peer->keynumber == 0)
4204				make_keylist(peer, peer->dstadr);
4205			else
4206				peer->keynumber--;
4207			xkeyid = peer->keylist[peer->keynumber];
4208			if (authistrusted(xkeyid))
4209				break;
4210			else
4211				key_expire(peer);
4212		}
4213		peer->keyid = xkeyid;
4214		exten = NULL;
4215		switch (peer->hmode) {
4216
4217		/*
4218		 * In broadcast server mode the autokey values are
4219		 * required by the broadcast clients. Push them when a
4220		 * new keylist is generated; otherwise, push the
4221		 * association message so the client can request them at
4222		 * other times.
4223		 */
4224		case MODE_BROADCAST:
4225			if (peer->flags & FLAG_ASSOC)
4226				exten = crypto_args(peer, CRYPTO_AUTO |
4227				    CRYPTO_RESP, peer->associd, NULL);
4228			else
4229				exten = crypto_args(peer, CRYPTO_ASSOC |
4230				    CRYPTO_RESP, peer->associd, NULL);
4231			break;
4232
4233		/*
4234		 * In symmetric modes the parameter, certificate,
4235		 * identity, cookie and autokey exchanges are
4236		 * required. The leapsecond exchange is optional. But, a
4237		 * peer will not believe the other peer until the other
4238		 * peer has synchronized, so the certificate exchange
4239		 * might loop until then. If a peer finds a broken
4240		 * autokey sequence, it uses the autokey exchange to
4241		 * retrieve the autokey values. In any case, if a new
4242		 * keylist is generated, the autokey values are pushed.
4243		 */
4244		case MODE_ACTIVE:
4245		case MODE_PASSIVE:
4246
4247			/*
4248			 * Parameter, certificate and identity.
4249			 */
4250			if (!peer->crypto)
4251				exten = crypto_args(peer, CRYPTO_ASSOC,
4252				    peer->associd, hostval.ptr);
4253			else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4254				exten = crypto_args(peer, CRYPTO_CERT,
4255				    peer->associd, peer->issuer);
4256			else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4257				exten = crypto_args(peer,
4258				    crypto_ident(peer), peer->associd,
4259				    NULL);
4260
4261			/*
4262			 * Cookie and autokey. We request the cookie
4263			 * only when the this peer and the other peer
4264			 * are synchronized. But, this peer needs the
4265			 * autokey values when the cookie is zero. Any
4266			 * time we regenerate the key list, we offer the
4267			 * autokey values without being asked. If for
4268			 * some reason either peer finds a broken
4269			 * autokey sequence, the autokey exchange is
4270			 * used to retrieve the autokey values.
4271			 */
4272			else if (   sys_leap != LEAP_NOTINSYNC
4273				 && peer->leap != LEAP_NOTINSYNC
4274				 && !(peer->crypto & CRYPTO_FLAG_COOK))
4275				exten = crypto_args(peer, CRYPTO_COOK,
4276				    peer->associd, NULL);
4277			else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4278				exten = crypto_args(peer, CRYPTO_AUTO,
4279				    peer->associd, NULL);
4280			else if (   peer->flags & FLAG_ASSOC
4281				 && peer->crypto & CRYPTO_FLAG_SIGN)
4282				exten = crypto_args(peer, CRYPTO_AUTO |
4283				    CRYPTO_RESP, peer->assoc, NULL);
4284
4285			/*
4286			 * Wait for clock sync, then sign the
4287			 * certificate and retrieve the leapsecond
4288			 * values.
4289			 */
4290			else if (sys_leap == LEAP_NOTINSYNC)
4291				break;
4292
4293			else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4294				exten = crypto_args(peer, CRYPTO_SIGN,
4295				    peer->associd, hostval.ptr);
4296			else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4297				exten = crypto_args(peer, CRYPTO_LEAP,
4298				    peer->associd, NULL);
4299			break;
4300
4301		/*
4302		 * In client mode the parameter, certificate, identity,
4303		 * cookie and sign exchanges are required. The
4304		 * leapsecond exchange is optional. If broadcast client
4305		 * mode the same exchanges are required, except that the
4306		 * autokey exchange is substitutes for the cookie
4307		 * exchange, since the cookie is always zero. If the
4308		 * broadcast client finds a broken autokey sequence, it
4309		 * uses the autokey exchange to retrieve the autokey
4310		 * values.
4311		 */
4312		case MODE_CLIENT:
4313
4314			/*
4315			 * Parameter, certificate and identity.
4316			 */
4317			if (!peer->crypto)
4318				exten = crypto_args(peer, CRYPTO_ASSOC,
4319				    peer->associd, hostval.ptr);
4320			else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4321				exten = crypto_args(peer, CRYPTO_CERT,
4322				    peer->associd, peer->issuer);
4323			else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4324				exten = crypto_args(peer,
4325				    crypto_ident(peer), peer->associd,
4326				    NULL);
4327
4328			/*
4329			 * Cookie and autokey. These are requests, but
4330			 * we use the peer association ID with autokey
4331			 * rather than our own.
4332			 */
4333			else if (!(peer->crypto & CRYPTO_FLAG_COOK))
4334				exten = crypto_args(peer, CRYPTO_COOK,
4335				    peer->associd, NULL);
4336			else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4337				exten = crypto_args(peer, CRYPTO_AUTO,
4338				    peer->assoc, NULL);
4339
4340			/*
4341			 * Wait for clock sync, then sign the
4342			 * certificate and retrieve the leapsecond
4343			 * values.
4344			 */
4345			else if (sys_leap == LEAP_NOTINSYNC)
4346				break;
4347
4348			else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4349				exten = crypto_args(peer, CRYPTO_SIGN,
4350				    peer->associd, hostval.ptr);
4351			else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4352				exten = crypto_args(peer, CRYPTO_LEAP,
4353				    peer->associd, NULL);
4354			break;
4355		}
4356
4357		/*
4358		 * Add a queued extension field if present. This is
4359		 * always a request message, so the reply ID is already
4360		 * in the message. If an error occurs, the error bit is
4361		 * lit in the response.
4362		 */
4363		if (peer->cmmd != NULL) {
4364			u_int32 temp32;
4365
4366			temp32 = CRYPTO_RESP;
4367			peer->cmmd->opcode |= htonl(temp32);
4368			sendlen += crypto_xmit(peer, &xpkt, NULL,
4369			    sendlen, peer->cmmd, 0);
4370			free(peer->cmmd);
4371			peer->cmmd = NULL;
4372		}
4373
4374		/*
4375		 * Add an extension field created above. All but the
4376		 * autokey response message are request messages.
4377		 */
4378		if (exten != NULL) {
4379			if (exten->opcode != 0)
4380				sendlen += crypto_xmit(peer, &xpkt,
4381				    NULL, sendlen, exten, 0);
4382			free(exten);
4383		}
4384
4385		/*
4386		 * Calculate the next session key. Since extension
4387		 * fields are present, the cookie value is zero.
4388		 */
4389		if (sendlen > (int)LEN_PKT_NOMAC) {
4390			session_key(&peer->dstadr->sin, &peer->srcadr,
4391			    xkeyid, 0, 2);
4392		}
4393	}
4394#endif	/* AUTOKEY */
4395
4396	/*
4397	 * Transmit a-priori timestamps
4398	 */
4399	get_systime(&xmt_tx);
4400	if (peer->flip == 0) {		/* basic mode */
4401		peer->aorg = xmt_tx;
4402		HTONL_FP(&xmt_tx, &xpkt.xmt);
4403	} else {			/* interleaved modes */
4404		if (peer->hmode == MODE_BROADCAST) { /* bcst */
4405			HTONL_FP(&xmt_tx, &xpkt.xmt);
4406			if (peer->flip > 0)
4407				HTONL_FP(&peer->borg, &xpkt.org);
4408			else
4409				HTONL_FP(&peer->aorg, &xpkt.org);
4410		} else {		/* symmetric */
4411			if (peer->flip > 0)
4412				HTONL_FP(&peer->borg, &xpkt.xmt);
4413			else
4414				HTONL_FP(&peer->aorg, &xpkt.xmt);
4415		}
4416	}
4417	xkeyid = peer->keyid;
4418	authlen = authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4419	if (authlen == 0) {
4420		report_event(PEVNT_AUTH, peer, "no key");
4421		peer->flash |= TEST5;		/* auth error */
4422		peer->badauth++;
4423		return;
4424	}
4425	sendlen += authlen;
4426#ifdef AUTOKEY
4427	if (xkeyid > NTP_MAXKEY)
4428		authtrust(xkeyid, 0);
4429#endif	/* AUTOKEY */
4430	if (sendlen > sizeof(xpkt)) {
4431		msyslog(LOG_ERR, "peer_xmit: buffer overflow %zu", sendlen);
4432		exit (-1);
4433	}
4434	peer->t21_bytes = sendlen;
4435	sendpkt(&peer->srcadr, peer->dstadr,
4436		sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4437		&xpkt, sendlen);
4438	peer->sent++;
4439	peer->throttle += (1 << peer->minpoll) - 2;
4440
4441	/*
4442	 * Capture a-posteriori timestamps
4443	 */
4444	get_systime(&xmt_ty);
4445	if (peer->flip != 0) {			/* interleaved modes */
4446		if (peer->flip > 0)
4447			peer->aorg = xmt_ty;
4448		else
4449			peer->borg = xmt_ty;
4450		peer->flip = -peer->flip;
4451	}
4452	L_SUB(&xmt_ty, &xmt_tx);
4453	LFPTOD(&xmt_ty, peer->xleave);
4454#ifdef AUTOKEY
4455	DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu index %d\n",
4456		    current_time, latoa(peer->dstadr),
4457		    ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen,
4458		    peer->keynumber));
4459#else	/* !AUTOKEY follows */
4460	DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu\n",
4461		    current_time, peer->dstadr ?
4462		    ntoa(&peer->dstadr->sin) : "-",
4463		    ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
4464#endif	/* !AUTOKEY */
4465
4466	return;
4467}
4468
4469
4470#ifdef LEAP_SMEAR
4471
4472static void
4473leap_smear_add_offs(
4474	l_fp *t,
4475	l_fp *t_recv
4476	)
4477{
4478
4479	L_ADD(t, &leap_smear.offset);
4480
4481	/*
4482	** XXX: Should the smear be added to the root dispersion?
4483	*/
4484
4485	return;
4486}
4487
4488#endif /* LEAP_SMEAR */
4489
4490
4491/*
4492 * fast_xmit - Send packet for nonpersistent association. Note that
4493 * neither the source or destination can be a broadcast address.
4494 */
4495static void
4496fast_xmit(
4497	struct recvbuf *rbufp,	/* receive packet pointer */
4498	int	xmode,		/* receive mode */  /* XXX: HMS: really? */
4499	keyid_t	xkeyid,		/* transmit key ID */
4500	int	flags		/* restrict mask */
4501	)
4502{
4503	struct pkt xpkt;	/* transmit packet structure */
4504	struct pkt *rpkt;	/* receive packet structure */
4505	l_fp	xmt_tx, xmt_ty;
4506	size_t	sendlen;
4507#ifdef AUTOKEY
4508	u_int32	temp32;
4509#endif
4510
4511	/*
4512	 * Initialize transmit packet header fields from the receive
4513	 * buffer provided. We leave the fields intact as received, but
4514	 * set the peer poll at the maximum of the receive peer poll and
4515	 * the system minimum poll (ntp_minpoll). This is for KoD rate
4516	 * control and not strictly specification compliant, but doesn't
4517	 * break anything.
4518	 *
4519	 * If the gazinta was from a multicast address, the gazoutta
4520	 * must go out another way.
4521	 */
4522	rpkt = &rbufp->recv_pkt;
4523	if (rbufp->dstadr->flags & INT_MCASTOPEN)
4524		rbufp->dstadr = findinterface(&rbufp->recv_srcadr);
4525
4526	/*
4527	 * If this is a kiss-o'-death (KoD) packet, show leap
4528	 * unsynchronized, stratum zero, reference ID the four-character
4529	 * kiss code and (???) system root delay. Note we don't reveal
4530	 * the local time, so these packets can't be used for
4531	 * synchronization.
4532	 */
4533	if (flags & RES_KOD) {
4534		sys_kodsent++;
4535		xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
4536		    PKT_VERSION(rpkt->li_vn_mode), xmode);
4537		xpkt.stratum = STRATUM_PKT_UNSPEC;
4538		xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4539		xpkt.precision = rpkt->precision;
4540		memcpy(&xpkt.refid, "RATE", 4);
4541		xpkt.rootdelay = rpkt->rootdelay;
4542		xpkt.rootdisp = rpkt->rootdisp;
4543		xpkt.reftime = rpkt->reftime;
4544		xpkt.org = rpkt->xmt;
4545		xpkt.rec = rpkt->xmt;
4546		xpkt.xmt = rpkt->xmt;
4547
4548	/*
4549	 * This is a normal packet. Use the system variables.
4550	 */
4551	} else {
4552		double this_rootdisp;
4553		l_fp this_ref_time;
4554
4555#ifdef LEAP_SMEAR
4556		/*
4557		 * Make copies of the variables which can be affected by smearing.
4558		 */
4559		l_fp this_recv_time;
4560#endif
4561
4562		/*
4563		 * If we are inside the leap smear interval we add
4564		 * the current smear offset to:
4565		 * - the packet receive time,
4566		 * - the packet transmit time,
4567		 * - and eventually to the reftime to make sure the
4568		 *   reftime isn't later than the transmit/receive times.
4569		 */
4570		xpkt.li_vn_mode = PKT_LI_VN_MODE(xmt_leap,
4571		    PKT_VERSION(rpkt->li_vn_mode), xmode);
4572
4573		xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4574		xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4575		xpkt.precision = sys_precision;
4576		xpkt.refid = sys_refid;
4577		xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4578
4579		/*
4580		** Server Response Fuzzing
4581		**
4582		** Which values do we want to use for reftime and rootdisp?
4583		*/
4584
4585		if (   MODE_SERVER == xmode
4586		    && RES_SRVRSPFUZ & flags) {
4587			if (current_time < p2_time) {
4588				this_ref_time = p2_reftime;
4589				this_rootdisp = p2_rootdisp;
4590			} else if (current_time < prev_time) {
4591				this_ref_time = prev_reftime;
4592				this_rootdisp = prev_rootdisp;
4593			} else {
4594				this_ref_time = sys_reftime;
4595				this_rootdisp = sys_rootdisp;
4596			}
4597
4598			SRVRSP_FUZZ(this_ref_time);
4599		} else {
4600			this_ref_time = sys_reftime;
4601			this_rootdisp = sys_rootdisp;
4602		}
4603
4604		/*
4605		** ROOT DISPERSION
4606		*/
4607
4608		xpkt.rootdisp = HTONS_FP(DTOUFP(this_rootdisp));
4609
4610		/*
4611		** REFTIME
4612		*/
4613
4614#ifdef LEAP_SMEAR
4615		if (leap_smear.in_progress) {
4616			/* adjust the reftime by the same amount as the
4617			 * leap smear, as we don't want to risk the
4618			 * reftime being later than the transmit time.
4619			 */
4620			leap_smear_add_offs(&this_ref_time, NULL);
4621		}
4622#endif
4623
4624		HTONL_FP(&this_ref_time, &xpkt.reftime);
4625
4626		/*
4627		** REFID
4628		*/
4629
4630#ifdef LEAP_SMEAR
4631		if (leap_smear.in_progress) {
4632			xpkt.refid = convertLFPToRefID(leap_smear.offset);
4633			DPRINTF(2, ("fast_xmit: leap_smear.in_progress: refid %8x, smear %s\n",
4634				ntohl(xpkt.refid),
4635				lfptoa(&leap_smear.offset, 8)
4636				));
4637		}
4638#endif
4639
4640		/*
4641		** ORIGIN
4642		*/
4643
4644		xpkt.org = rpkt->xmt;
4645
4646		/*
4647		** RECEIVE
4648		*/
4649#ifdef LEAP_SMEAR
4650		this_recv_time = rbufp->recv_time;
4651		if (leap_smear.in_progress)
4652			leap_smear_add_offs(&this_recv_time, NULL);
4653		HTONL_FP(&this_recv_time, &xpkt.rec);
4654#else
4655		HTONL_FP(&rbufp->recv_time, &xpkt.rec);
4656#endif
4657
4658		/*
4659		** TRANSMIT
4660		*/
4661
4662		get_systime(&xmt_tx);
4663#ifdef LEAP_SMEAR
4664		if (leap_smear.in_progress)
4665			leap_smear_add_offs(&xmt_tx, &this_recv_time);
4666#endif
4667		HTONL_FP(&xmt_tx, &xpkt.xmt);
4668	}
4669
4670#ifdef HAVE_NTP_SIGND
4671	if (flags & RES_MSSNTP) {
4672		send_via_ntp_signd(rbufp, xmode, xkeyid, flags, &xpkt);
4673		return;
4674	}
4675#endif /* HAVE_NTP_SIGND */
4676
4677	/*
4678	 * If the received packet contains a MAC, the transmitted packet
4679	 * is authenticated and contains a MAC. If not, the transmitted
4680	 * packet is not authenticated.
4681	 */
4682	sendlen = LEN_PKT_NOMAC;
4683	if (rbufp->recv_length == sendlen) {
4684		sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt,
4685		    sendlen);
4686		DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d len %lu\n",
4687			    current_time, stoa(&rbufp->dstadr->sin),
4688			    stoa(&rbufp->recv_srcadr), xmode,
4689			    (u_long)sendlen));
4690		return;
4691	}
4692
4693	/*
4694	 * The received packet contains a MAC, so the transmitted packet
4695	 * must be authenticated. For symmetric key cryptography, use
4696	 * the predefined and trusted symmetric keys to generate the
4697	 * cryptosum. For autokey cryptography, use the server private
4698	 * value to generate the cookie, which is unique for every
4699	 * source-destination-key ID combination.
4700	 */
4701#ifdef AUTOKEY
4702	if (xkeyid > NTP_MAXKEY) {
4703		keyid_t cookie;
4704
4705		/*
4706		 * The only way to get here is a reply to a legitimate
4707		 * client request message, so the mode must be
4708		 * MODE_SERVER. If an extension field is present, there
4709		 * can be only one and that must be a command. Do what
4710		 * needs, but with private value of zero so the poor
4711		 * jerk can decode it. If no extension field is present,
4712		 * use the cookie to generate the session key.
4713		 */
4714		cookie = session_key(&rbufp->recv_srcadr,
4715		    &rbufp->dstadr->sin, 0, sys_private, 0);
4716		if ((size_t)rbufp->recv_length > sendlen + MAX_MAC_LEN) {
4717			session_key(&rbufp->dstadr->sin,
4718			    &rbufp->recv_srcadr, xkeyid, 0, 2);
4719			temp32 = CRYPTO_RESP;
4720			rpkt->exten[0] |= htonl(temp32);
4721			sendlen += crypto_xmit(NULL, &xpkt, rbufp,
4722			    sendlen, (struct exten *)rpkt->exten,
4723			    cookie);
4724		} else {
4725			session_key(&rbufp->dstadr->sin,
4726			    &rbufp->recv_srcadr, xkeyid, cookie, 2);
4727		}
4728	}
4729#endif	/* AUTOKEY */
4730	get_systime(&xmt_tx);
4731	sendlen += authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4732#ifdef AUTOKEY
4733	if (xkeyid > NTP_MAXKEY)
4734		authtrust(xkeyid, 0);
4735#endif	/* AUTOKEY */
4736	sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt, sendlen);
4737	get_systime(&xmt_ty);
4738	L_SUB(&xmt_ty, &xmt_tx);
4739	sys_authdelay = xmt_ty;
4740	DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d keyid %08x len %lu\n",
4741		    current_time, ntoa(&rbufp->dstadr->sin),
4742		    ntoa(&rbufp->recv_srcadr), xmode, xkeyid,
4743		    (u_long)sendlen));
4744}
4745
4746
4747/*
4748 * pool_xmit - resolve hostname or send unicast solicitation for pool.
4749 */
4750static void
4751pool_xmit(
4752	struct peer *pool	/* pool solicitor association */
4753	)
4754{
4755#ifdef WORKER
4756	struct pkt		xpkt;	/* transmit packet structure */
4757	struct addrinfo		hints;
4758	int			rc;
4759	struct interface *	lcladr;
4760	sockaddr_u *		rmtadr;
4761	r4addr			r4a;
4762	u_short			restrict_mask;
4763	struct peer *		p;
4764	l_fp			xmt_tx;
4765
4766	DEBUG_REQUIRE(pool);
4767	if (NULL == pool->ai) {
4768		if (pool->addrs != NULL) {
4769			/* free() is used with copy_addrinfo_list() */
4770			free(pool->addrs);
4771			pool->addrs = NULL;
4772		}
4773		ZERO(hints);
4774		hints.ai_family = AF(&pool->srcadr);
4775		hints.ai_socktype = SOCK_DGRAM;
4776		hints.ai_protocol = IPPROTO_UDP;
4777		/* ignore getaddrinfo_sometime() errors, we will retry */
4778		rc = getaddrinfo_sometime(
4779			pool->hostname,
4780			"ntp",
4781			&hints,
4782			0,			/* no retry */
4783			&pool_name_resolved,
4784			(void *)(intptr_t)pool->associd);
4785		if (!rc)
4786			DPRINTF(1, ("pool DNS lookup %s started\n",
4787				pool->hostname));
4788		else
4789			msyslog(LOG_ERR,
4790				"unable to start pool DNS %s: %m",
4791				pool->hostname);
4792		return;
4793	}
4794
4795	do {
4796		/* copy_addrinfo_list ai_addr points to a sockaddr_u */
4797		rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
4798		pool->ai = pool->ai->ai_next;
4799		p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT, 0, NULL);
4800	} while (p != NULL && pool->ai != NULL);
4801	if (p != NULL)
4802		return;	/* out of addresses, re-query DNS next poll */
4803	restrictions(rmtadr, &r4a);
4804	restrict_mask = r4a.rflags;
4805	if (RES_FLAGS & restrict_mask)
4806		restrict_source(rmtadr, 0,
4807				current_time + POOL_SOLICIT_WINDOW + 1);
4808	lcladr = findinterface(rmtadr);
4809	memset(&xpkt, 0, sizeof(xpkt));
4810	xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, pool->version,
4811					 MODE_CLIENT);
4812	xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4813	xpkt.ppoll = pool->hpoll;
4814	xpkt.precision = sys_precision;
4815	xpkt.refid = sys_refid;
4816	xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4817	xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4818	/* Bug 3596: What are the pros/cons of using sys_reftime here? */
4819	HTONL_FP(&sys_reftime, &xpkt.reftime);
4820
4821	/* HMS: the following is better done after the ntp_random() calls */
4822	get_systime(&xmt_tx);
4823	pool->aorg = xmt_tx;
4824
4825	if (FLAG_LOOPNONCE & pool->flags) {
4826		l_fp nonce;
4827
4828		do {
4829			nonce.l_ui = ntp_random();
4830		} while (0 == nonce.l_ui);
4831		do {
4832			nonce.l_uf = ntp_random();
4833		} while (0 == nonce.l_uf);
4834		pool->nonce = nonce;
4835		HTONL_FP(&nonce, &xpkt.xmt);
4836	} else {
4837		L_CLR(&pool->nonce);
4838		HTONL_FP(&xmt_tx, &xpkt.xmt);
4839	}
4840	sendpkt(rmtadr, lcladr,
4841		sys_ttl[(pool->ttl >= sys_ttlmax) ? sys_ttlmax : pool->ttl],
4842		&xpkt, LEN_PKT_NOMAC);
4843	pool->sent++;
4844	pool->throttle += (1 << pool->minpoll) - 2;
4845	DPRINTF(1, ("pool_xmit: at %ld %s->%s pool\n",
4846		    current_time, latoa(lcladr), stoa(rmtadr)));
4847	msyslog(LOG_INFO, "Soliciting pool server %s", stoa(rmtadr));
4848#endif	/* WORKER */
4849}
4850
4851
4852#ifdef AUTOKEY
4853	/*
4854	 * group_test - test if this is the same group
4855	 *
4856	 * host		assoc		return		action
4857	 * none		none		0		mobilize *
4858	 * none		group		0		mobilize *
4859	 * group	none		0		mobilize *
4860	 * group	group		1		mobilize
4861	 * group	different	1		ignore
4862	 * * ignore if notrust
4863	 */
4864int
4865group_test(
4866	char	*grp,
4867	char	*ident
4868	)
4869{
4870	if (grp == NULL)
4871		return (0);
4872
4873	if (strcmp(grp, sys_groupname) == 0)
4874		return (0);
4875
4876	if (ident == NULL)
4877		return (1);
4878
4879	if (strcmp(grp, ident) == 0)
4880		return (0);
4881
4882	return (1);
4883}
4884#endif /* AUTOKEY */
4885
4886
4887#ifdef WORKER
4888void
4889pool_name_resolved(
4890	int			rescode,
4891	int			gai_errno,
4892	void *			context,
4893	const char *		name,
4894	const char *		service,
4895	const struct addrinfo *	hints,
4896	const struct addrinfo *	res
4897	)
4898{
4899	struct peer *	pool;	/* pool solicitor association */
4900	associd_t	assoc;
4901
4902	if (rescode) {
4903		msyslog(LOG_ERR,
4904			"error resolving pool %s: %s (%d)",
4905			name, gai_strerror(rescode), rescode);
4906		return;
4907	}
4908
4909	assoc = (associd_t)(intptr_t)context;
4910	pool = findpeerbyassoc(assoc);
4911	if (NULL == pool) {
4912		msyslog(LOG_ERR,
4913			"Could not find assoc %u for pool DNS %s",
4914			assoc, name);
4915		return;
4916	}
4917	DPRINTF(1, ("pool DNS %s completed\n", name));
4918	pool->addrs = copy_addrinfo_list(res);
4919	pool->ai = pool->addrs;
4920	pool_xmit(pool);
4921
4922}
4923#endif	/* WORKER */
4924
4925
4926#ifdef AUTOKEY
4927/*
4928 * key_expire - purge the key list
4929 */
4930void
4931key_expire(
4932	struct peer *peer	/* peer structure pointer */
4933	)
4934{
4935	int i;
4936
4937	if (peer->keylist != NULL) {
4938		for (i = 0; i <= peer->keynumber; i++)
4939			authtrust(peer->keylist[i], 0);
4940		free(peer->keylist);
4941		peer->keylist = NULL;
4942	}
4943	value_free(&peer->sndval);
4944	peer->keynumber = 0;
4945	peer->flags &= ~FLAG_ASSOC;
4946	DPRINTF(1, ("key_expire: at %lu associd %d\n", current_time,
4947		    peer->associd));
4948}
4949#endif	/* AUTOKEY */
4950
4951
4952/*
4953 * local_refid(peer) - check peer refid to avoid selecting peers
4954 *		       currently synced to this ntpd.
4955 */
4956static int
4957local_refid(
4958	struct peer *	p
4959	)
4960{
4961	endpt *	unicast_ep;
4962
4963	if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
4964		unicast_ep = p->dstadr;
4965	else
4966		unicast_ep = findinterface(&p->srcadr);
4967
4968	if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
4969		return TRUE;
4970	else
4971		return FALSE;
4972}
4973
4974
4975/*
4976 * Determine if the peer is unfit for synchronization
4977 *
4978 * A peer is unfit for synchronization if
4979 * > TEST10 bad leap or stratum below floor or at or above ceiling
4980 * > TEST11 root distance exceeded for remote peer
4981 * > TEST12 a direct or indirect synchronization loop would form
4982 * > TEST13 unreachable or noselect
4983 */
4984int				/* FALSE if fit, TRUE if unfit */
4985peer_unfit(
4986	struct peer *peer	/* peer structure pointer */
4987	)
4988{
4989	int	rval = 0;
4990
4991	/*
4992	 * A stratum error occurs if (1) the server has never been
4993	 * synchronized, (2) the server stratum is below the floor or
4994	 * greater than or equal to the ceiling.
4995	 */
4996	if (   peer->leap == LEAP_NOTINSYNC
4997	    || peer->stratum < sys_floor
4998	    || peer->stratum >= sys_ceiling) {
4999		rval |= TEST10;		/* bad synch or stratum */
5000	}
5001
5002	/*
5003	 * A distance error for a remote peer occurs if the root
5004	 * distance is greater than or equal to the distance threshold
5005	 * plus the increment due to one host poll interval.
5006	 */
5007	if (   !(peer->flags & FLAG_REFCLOCK)
5008	    && root_distance(peer) >= sys_maxdist
5009				      + clock_phi * ULOGTOD(peer->hpoll)) {
5010		rval |= TEST11;		/* distance exceeded */
5011	}
5012
5013	/*
5014	 * A loop error occurs if the remote peer is synchronized to the
5015	 * local peer or if the remote peer is synchronized to the same
5016	 * server as the local peer but only if the remote peer is
5017	 * neither a reference clock nor an orphan.
5018	 */
5019	if (peer->stratum > 1 && local_refid(peer)) {
5020		rval |= TEST12;		/* synchronization loop */
5021	}
5022
5023	/*
5024	 * An unreachable error occurs if the server is unreachable or
5025	 * the noselect bit is set.
5026	 */
5027	if (!peer->reach || (peer->flags & FLAG_NOSELECT)) {
5028		rval |= TEST13;		/* unreachable */
5029	}
5030
5031	peer->flash &= ~PEER_TEST_MASK;
5032	peer->flash |= rval;
5033	return (rval);
5034}
5035
5036
5037/*
5038 * Find the precision of this particular machine
5039 */
5040#define MINSTEP		20e-9	/* minimum clock increment (s) */
5041#define MAXSTEP		1	/* maximum clock increment (s) */
5042#define MINCHANGES	12	/* minimum number of step samples */
5043#define MAXLOOPS	((int)(1. / MINSTEP))	/* avoid infinite loop */
5044
5045/*
5046 * This routine measures the system precision defined as the minimum of
5047 * a sequence of differences between successive readings of the system
5048 * clock. However, if a difference is less than MINSTEP, the clock has
5049 * been read more than once during a clock tick and the difference is
5050 * ignored. We set MINSTEP greater than zero in case something happens
5051 * like a cache miss, and to tolerate underlying system clocks which
5052 * ensure each reading is strictly greater than prior readings while
5053 * using an underlying stepping (not interpolated) clock.
5054 *
5055 * sys_tick and sys_precision represent the time to read the clock for
5056 * systems with high-precision clocks, and the tick interval or step
5057 * size for lower-precision stepping clocks.
5058 *
5059 * This routine also measures the time to read the clock on stepping
5060 * system clocks by counting the number of readings between changes of
5061 * the underlying clock.  With either type of clock, the minimum time
5062 * to read the clock is saved as sys_fuzz, and used to ensure the
5063 * get_systime() readings always increase and are fuzzed below sys_fuzz.
5064 */
5065void
5066measure_precision(void)
5067{
5068	/*
5069	 * With sys_fuzz set to zero, get_systime() fuzzing of low bits
5070	 * is effectively disabled.  trunc_os_clock is FALSE to disable
5071	 * get_ostime() simulation of a low-precision system clock.
5072	 */
5073	set_sys_fuzz(0.);
5074	trunc_os_clock = FALSE;
5075	measured_tick = measure_tick_fuzz();
5076	set_sys_tick_precision(measured_tick);
5077	msyslog(LOG_INFO, "proto: precision = %.3f usec (%d)",
5078		sys_tick * 1e6, sys_precision);
5079	if (sys_fuzz < sys_tick) {
5080		msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec",
5081			sys_fuzz * 1e6);
5082	}
5083}
5084
5085
5086/*
5087 * measure_tick_fuzz()
5088 *
5089 * measures the minimum time to read the clock (stored in sys_fuzz)
5090 * and returns the tick, the larger of the minimum increment observed
5091 * between successive clock readings and the time to read the clock.
5092 */
5093double
5094measure_tick_fuzz(void)
5095{
5096	l_fp	minstep;	/* MINSTEP as l_fp */
5097	l_fp	val;		/* current seconds fraction */
5098	l_fp	last;		/* last seconds fraction */
5099	l_fp	ldiff;		/* val - last */
5100	double	tick;		/* computed tick value */
5101	double	diff;
5102	long	repeats;
5103	long	max_repeats;
5104	int	changes;
5105	int	i;		/* log2 precision */
5106
5107	tick = MAXSTEP;
5108	max_repeats = 0;
5109	repeats = 0;
5110	changes = 0;
5111	DTOLFP(MINSTEP, &minstep);
5112	get_systime(&last);
5113	for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) {
5114		get_systime(&val);
5115		ldiff = val;
5116		L_SUB(&ldiff, &last);
5117		last = val;
5118		if (L_ISGT(&ldiff, &minstep)) {
5119			max_repeats = max(repeats, max_repeats);
5120			repeats = 0;
5121			changes++;
5122			LFPTOD(&ldiff, diff);
5123			tick = min(diff, tick);
5124		} else {
5125			repeats++;
5126		}
5127	}
5128	if (changes < MINCHANGES) {
5129		msyslog(LOG_ERR, "Fatal error: precision could not be measured (MINSTEP too large?)");
5130		exit(1);
5131	}
5132
5133	if (0 == max_repeats) {
5134		set_sys_fuzz(tick);
5135	} else {
5136		set_sys_fuzz(tick / max_repeats);
5137	}
5138
5139	return tick;
5140}
5141
5142
5143void
5144set_sys_tick_precision(
5145	double tick
5146	)
5147{
5148	int i;
5149
5150	if (tick > 1.) {
5151		msyslog(LOG_ERR,
5152			"unsupported tick %.3f > 1s ignored", tick);
5153		return;
5154	}
5155	if (tick < measured_tick) {
5156		msyslog(LOG_ERR,
5157			"proto: tick %.3f less than measured tick %.3f, ignored",
5158			tick, measured_tick);
5159		return;
5160	} else if (tick > measured_tick) {
5161		trunc_os_clock = TRUE;
5162		msyslog(LOG_NOTICE,
5163			"proto: truncating system clock to multiples of %.9f",
5164			tick);
5165	}
5166	sys_tick = tick;
5167
5168	/*
5169	 * Find the nearest power of two.
5170	 */
5171	for (i = 0; tick <= 1; i--)
5172		tick *= 2;
5173	if (tick - 1 > 1 - tick / 2)
5174		i++;
5175
5176	sys_precision = (s_char)i;
5177}
5178
5179
5180/*
5181 * init_proto - initialize the protocol module's data
5182 */
5183void
5184init_proto(void)
5185{
5186	l_fp	dummy;
5187	int	i;
5188
5189	/*
5190	 * Fill in the sys_* stuff.  Default is don't listen to
5191	 * broadcasting, require authentication.
5192	 */
5193	set_sys_leap(LEAP_NOTINSYNC);
5194	sys_stratum = STRATUM_UNSPEC;
5195	memcpy(&sys_refid, "INIT", 4);
5196	sys_peer = NULL;
5197	sys_rootdelay = 0;
5198	sys_rootdisp = 0;
5199	L_CLR(&sys_reftime);
5200	sys_jitter = 0;
5201	measure_precision();
5202	get_systime(&dummy);
5203	sys_survivors = 0;
5204	sys_manycastserver = 0;
5205	sys_bclient = 0;
5206	sys_bdelay = BDELAY_DEFAULT;	/*[Bug 3031] delay cutoff */
5207	sys_authenticate = 1;
5208	sys_stattime = current_time;
5209	orphwait = current_time + sys_orphwait;
5210	proto_clr_stats();
5211	for (i = 0; i < MAX_TTL; ++i)
5212		sys_ttl[i] = (u_char)((i * 256) / MAX_TTL);
5213	sys_ttlmax = (MAX_TTL - 1);
5214	hardpps_enable = 0;
5215	stats_control = 1;
5216}
5217
5218
5219/*
5220 * proto_config - configure the protocol module
5221 */
5222void
5223proto_config(
5224	int	item,
5225	u_long	value,
5226	double	dvalue,
5227	sockaddr_u *svalue
5228	)
5229{
5230	/*
5231	 * Figure out what he wants to change, then do it
5232	 */
5233	DPRINTF(2, ("proto_config: code %d value %lu dvalue %lf\n",
5234		    item, value, dvalue));
5235
5236	switch (item) {
5237
5238	/*
5239	 * enable and disable commands - arguments are Boolean.
5240	 */
5241	case PROTO_AUTHENTICATE: /* authentication (auth) */
5242		sys_authenticate = value;
5243		break;
5244
5245	case PROTO_BROADCLIENT: /* broadcast client (bclient) */
5246		sys_bclient = (int)value;
5247		if (sys_bclient == 0)
5248			io_unsetbclient();
5249		else
5250			io_setbclient();
5251		break;
5252
5253#ifdef REFCLOCK
5254	case PROTO_CAL:		/* refclock calibrate (calibrate) */
5255		cal_enable = value;
5256		break;
5257#endif /* REFCLOCK */
5258
5259	case PROTO_KERNEL:	/* kernel discipline (kernel) */
5260		select_loop(value);
5261		break;
5262
5263	case PROTO_MONITOR:	/* monitoring (monitor) */
5264		if (value)
5265			mon_start(MON_ON);
5266		else {
5267			mon_stop(MON_ON);
5268			if (mon_enabled)
5269				msyslog(LOG_WARNING,
5270					"restrict: 'monitor' cannot be disabled while 'limited' is enabled");
5271		}
5272		break;
5273
5274	case PROTO_NTP:		/* NTP discipline (ntp) */
5275		ntp_enable = value;
5276		break;
5277
5278	case PROTO_MODE7:	/* mode7 management (ntpdc) */
5279		ntp_mode7 = value;
5280		break;
5281
5282	case PROTO_PPS:		/* PPS discipline (pps) */
5283		hardpps_enable = value;
5284		break;
5285
5286	case PROTO_FILEGEN:	/* statistics (stats) */
5287		stats_control = value;
5288		break;
5289
5290	/*
5291	 * tos command - arguments are double, sometimes cast to int
5292	 */
5293
5294	case PROTO_BCPOLLBSTEP:	/* Broadcast Poll Backstep gate (bcpollbstep) */
5295		sys_bcpollbstep = (u_char)dvalue;
5296		break;
5297
5298	case PROTO_BEACON:	/* manycast beacon (beacon) */
5299		sys_beacon = (int)dvalue;
5300		break;
5301
5302	case PROTO_BROADDELAY:	/* default broadcast delay (bdelay) */
5303		sys_bdelay = (dvalue ? dvalue : BDELAY_DEFAULT);
5304		break;
5305
5306	case PROTO_CEILING:	/* stratum ceiling (ceiling) */
5307		sys_ceiling = (int)dvalue;
5308		break;
5309
5310	case PROTO_COHORT:	/* cohort switch (cohort) */
5311		sys_cohort = (int)dvalue;
5312		break;
5313
5314	case PROTO_FLOOR:	/* stratum floor (floor) */
5315		sys_floor = (int)dvalue;
5316		break;
5317
5318	case PROTO_MAXCLOCK:	/* maximum candidates (maxclock) */
5319		sys_maxclock = (int)dvalue;
5320		break;
5321
5322	case PROTO_MAXDIST:	/* select threshold (maxdist) */
5323		sys_maxdist = dvalue;
5324		break;
5325
5326	case PROTO_CALLDELAY:	/* modem call delay (mdelay) */
5327		break;		/* NOT USED */
5328
5329	case PROTO_MINCLOCK:	/* minimum candidates (minclock) */
5330		sys_minclock = (int)dvalue;
5331		break;
5332
5333	case PROTO_MINDISP:	/* minimum distance (mindist) */
5334		sys_mindisp = dvalue;
5335		break;
5336
5337	case PROTO_MINSANE:	/* minimum survivors (minsane) */
5338		sys_minsane = (int)dvalue;
5339		break;
5340
5341	case PROTO_ORPHAN:	/* orphan stratum (orphan) */
5342		sys_orphan = (int)dvalue;
5343		break;
5344
5345	case PROTO_ORPHWAIT:	/* orphan wait (orphwait) */
5346		orphwait -= sys_orphwait;
5347		sys_orphwait = (int)dvalue;
5348		orphwait += sys_orphwait;
5349		break;
5350
5351	/*
5352	 * Miscellaneous commands
5353	 */
5354	case PROTO_MULTICAST_ADD: /* add group address */
5355		if (svalue != NULL)
5356			io_multicast_add(svalue);
5357		sys_bclient = 1;
5358		break;
5359
5360	case PROTO_MULTICAST_DEL: /* delete group address */
5361		if (svalue != NULL)
5362			io_multicast_del(svalue);
5363		break;
5364
5365	/*
5366	 * Peer_clear Early policy choices
5367	 */
5368
5369	case PROTO_PCEDIGEST:	/* Digest */
5370		peer_clear_digest_early = value;
5371		break;
5372
5373	/*
5374	 * Unpeer Early policy choices
5375	 */
5376
5377	case PROTO_UECRYPTO:	/* Crypto */
5378		unpeer_crypto_early = value;
5379		break;
5380
5381	case PROTO_UECRYPTONAK:	/* Crypto_NAK */
5382		unpeer_crypto_nak_early = value;
5383		break;
5384
5385	case PROTO_UEDIGEST:	/* Digest */
5386		unpeer_digest_early = value;
5387		break;
5388
5389	default:
5390		msyslog(LOG_NOTICE,
5391		    "proto: unsupported option %d", item);
5392	}
5393}
5394
5395
5396/*
5397 * proto_clr_stats - clear protocol stat counters
5398 */
5399void
5400proto_clr_stats(void)
5401{
5402	sys_stattime = current_time;
5403	sys_received = 0;
5404	sys_processed = 0;
5405	sys_newversion = 0;
5406	sys_oldversion = 0;
5407	sys_declined = 0;
5408	sys_restricted = 0;
5409	sys_badlength = 0;
5410	sys_badauth = 0;
5411	sys_limitrejected = 0;
5412	sys_kodsent = 0;
5413	sys_lamport = 0;
5414	sys_tsrounding = 0;
5415}
5416