ntp_proto.c revision 338531
1250003Sadrian/*
2250003Sadrian * ntp_proto.c - NTP version 4 protocol machinery
3250003Sadrian *
4250003Sadrian * ATTENTION: Get approval from Harlan on all changes to this file!
5250003Sadrian *	    (Harlan will be discussing these changes with Dave Mills.)
6250003Sadrian *
7250003Sadrian */
8250003Sadrian#ifdef HAVE_CONFIG_H
9250003Sadrian#include <config.h>
10250003Sadrian#endif
11250003Sadrian
12250003Sadrian#include "ntpd.h"
13250003Sadrian#include "ntp_stdlib.h"
14250003Sadrian#include "ntp_unixtime.h"
15250003Sadrian#include "ntp_control.h"
16250003Sadrian#include "ntp_string.h"
17250003Sadrian#include "ntp_leapsec.h"
18250003Sadrian#include "refidsmear.h"
19250003Sadrian#include "lib_strbuf.h"
20250003Sadrian
21250003Sadrian#include <stdio.h>
22250003Sadrian#ifdef HAVE_LIBSCF_H
23250003Sadrian#include <libscf.h>
24250003Sadrian#endif
25250003Sadrian#ifdef HAVE_UNISTD_H
26250003Sadrian#include <unistd.h>
27250003Sadrian#endif
28250003Sadrian
29250003Sadrian/* [Bug 3031] define automatic broadcastdelay cutoff preset */
30250003Sadrian#ifndef BDELAY_DEFAULT
31250003Sadrian# define BDELAY_DEFAULT (-0.050)
32250003Sadrian#endif
33250003Sadrian
34250003Sadrian/*
35250003Sadrian * This macro defines the authentication state. If x is 1 authentication
36250003Sadrian * is required; otherwise it is optional.
37250003Sadrian */
38250003Sadrian#define	AUTH(x, y)	((x) ? (y) == AUTH_OK \
39250003Sadrian			     : (y) == AUTH_OK || (y) == AUTH_NONE)
40250003Sadrian
41250003Sadriantypedef enum
42250003Sadrianauth_state {
43250003Sadrian	AUTH_UNKNOWN = -1,	/* Unknown */
44250003Sadrian	AUTH_NONE,		/* authentication not required */
45250003Sadrian	AUTH_OK,		/* authentication OK */
46250003Sadrian	AUTH_ERROR,		/* authentication error */
47250003Sadrian	AUTH_CRYPTO		/* crypto_NAK */
48250003Sadrian} auth_code;
49250003Sadrian
50250003Sadrian/*
51250008Sadrian * Set up Kiss Code values
52250003Sadrian */
53250008Sadrian
54250008Sadriantypedef enum
55250003Sadriankiss_codes {
56250003Sadrian	NOKISS,				/* No Kiss Code */
57250003Sadrian	RATEKISS,			/* Rate limit Kiss Code */
58250003Sadrian	DENYKISS,			/* Deny Kiss */
59250003Sadrian	RSTRKISS,			/* Restricted Kiss */
60250003Sadrian	XKISS				/* Experimental Kiss */
61250003Sadrian} kiss_code;
62250003Sadrian
63250003Sadriantypedef enum
64250003Sadriannak_error_codes {
65250003Sadrian	NONAK,				/* No NAK seen */
66250003Sadrian	INVALIDNAK,			/* NAK cannot be used */
67250003Sadrian	VALIDNAK			/* NAK is valid */
68250003Sadrian} nak_code;
69250003Sadrian
70250003Sadrian/*
71250003Sadrian * traffic shaping parameters
72250003Sadrian */
73250003Sadrian#define	NTP_IBURST	6	/* packets in iburst */
74250003Sadrian#define	RESP_DELAY	1	/* refclock burst delay (s) */
75250003Sadrian
76250003Sadrian/*
77250003Sadrian * pool soliciting restriction duration (s)
78250008Sadrian */
79250003Sadrian#define	POOL_SOLICIT_WINDOW	8
80250003Sadrian
81250003Sadrian/*
82250003Sadrian * peer_select groups statistics for a peer used by clock_select() and
83250003Sadrian * clock_cluster().
84250003Sadrian */
85250003Sadriantypedef struct peer_select_tag {
86250003Sadrian	struct peer *	peer;
87250003Sadrian	double		synch;	/* sync distance */
88250003Sadrian	double		error;	/* jitter */
89250003Sadrian	double		seljit;	/* selection jitter */
90250003Sadrian} peer_select;
91250003Sadrian
92250003Sadrian/*
93250003Sadrian * System variables are declared here. Unless specified otherwise, all
94250003Sadrian * times are in seconds.
95250003Sadrian */
96250008Sadrianu_char	sys_leap;		/* system leap indicator, use set_sys_leap() to change this */
97250003Sadrianu_char	xmt_leap;		/* leap indicator sent in client requests, set up by set_sys_leap() */
98250003Sadrianu_char	sys_stratum;		/* system stratum */
99250003Sadrians_char	sys_precision;		/* local clock precision (log2 s) */
100250003Sadriandouble	sys_rootdelay;		/* roundtrip delay to primary source */
101250003Sadriandouble	sys_rootdisp;		/* dispersion to primary source */
102250003Sadrianu_int32 sys_refid;		/* reference id (network byte order) */
103250003Sadrianl_fp	sys_reftime;		/* last update time */
104250003Sadrianstruct	peer *sys_peer;		/* current peer */
105250003Sadrian
106250003Sadrian#ifdef LEAP_SMEAR
107250003Sadrianstruct leap_smear_info leap_smear;
108250003Sadrian#endif
109250003Sadrianint leap_sec_in_progress;
110250003Sadrian
111250003Sadrian/*
112250003Sadrian * Rate controls. Leaky buckets are used to throttle the packet
113250003Sadrian * transmission rates in order to protect busy servers such as at NIST
114250003Sadrian * and USNO. There is a counter for each association and another for KoD
115250003Sadrian * packets. The association counter decrements each second, but not
116250003Sadrian * below zero. Each time a packet is sent the counter is incremented by
117250003Sadrian * a configurable value representing the average interval between
118250003Sadrian * packets. A packet is delayed as long as the counter is greater than
119250003Sadrian * zero. Note this does not affect the time value computations.
120250003Sadrian */
121250003Sadrian/*
122250003Sadrian * Nonspecified system state variables
123250003Sadrian */
124250008Sadrianint	sys_bclient;		/* broadcast client enable */
125250003Sadriandouble	sys_bdelay;		/* broadcast client default delay */
126250003Sadrianint	sys_authenticate;	/* requre authentication for config */
127250003Sadrianl_fp	sys_authdelay;		/* authentication delay */
128250003Sadriandouble	sys_offset;	/* current local clock offset */
129250003Sadriandouble	sys_mindisp = MINDISPERSE; /* minimum distance (s) */
130250003Sadriandouble	sys_maxdist = MAXDISTANCE; /* selection threshold */
131250003Sadriandouble	sys_jitter;		/* system jitter */
132250003Sadrianu_long	sys_epoch;		/* last clock update time */
133250003Sadrianstatic	double sys_clockhop;	/* clockhop threshold */
134250003Sadrianstatic int leap_vote_ins;	/* leap consensus for insert */
135250003Sadrianstatic int leap_vote_del;	/* leap consensus for delete */
136250003Sadriankeyid_t	sys_private;		/* private value for session seed */
137250003Sadrianint	sys_manycastserver;	/* respond to manycast client pkts */
138250003Sadrianint	ntp_mode7;		/* respond to ntpdc (mode7) */
139250003Sadrianint	peer_ntpdate;		/* active peers in ntpdate mode */
140250003Sadrianint	sys_survivors;		/* truest of the truechimers */
141250003Sadrianchar	*sys_ident = NULL;	/* identity scheme */
142250003Sadrian
143250003Sadrian/*
144250003Sadrian * TOS and multicast mapping stuff
145250003Sadrian */
146250003Sadrianint	sys_floor = 0;		/* cluster stratum floor */
147250003Sadrianu_char	sys_bcpollbstep = 0;	/* Broadcast Poll backstep gate */
148250003Sadrianint	sys_ceiling = STRATUM_UNSPEC - 1; /* cluster stratum ceiling */
149250003Sadrianint	sys_minsane = 1;	/* minimum candidates */
150250003Sadrianint	sys_minclock = NTP_MINCLOCK; /* minimum candidates */
151250003Sadrianint	sys_maxclock = NTP_MAXCLOCK; /* maximum candidates */
152250003Sadrianint	sys_cohort = 0;		/* cohort switch */
153250003Sadrianint	sys_orphan = STRATUM_UNSPEC + 1; /* orphan stratum */
154250003Sadrianint	sys_orphwait = NTP_ORPHWAIT; /* orphan wait */
155250003Sadrianint	sys_beacon = BEACON;	/* manycast beacon interval */
156250003Sadrianu_int	sys_ttlmax;		/* max ttl mapping vector index */
157250003Sadrianu_char	sys_ttl[MAX_TTL];	/* ttl mapping vector */
158250003Sadrian
159250003Sadrian/*
160250003Sadrian * Statistics counters - first the good, then the bad
161250003Sadrian */
162250003Sadrianu_long	sys_stattime;		/* elapsed time */
163250003Sadrianu_long	sys_received;		/* packets received */
164250003Sadrianu_long	sys_processed;		/* packets for this host */
165250003Sadrianu_long	sys_newversion;		/* current version */
166250003Sadrianu_long	sys_oldversion;		/* old version */
167250003Sadrianu_long	sys_restricted;		/* access denied */
168250003Sadrianu_long	sys_badlength;		/* bad length or format */
169250003Sadrianu_long	sys_badauth;		/* bad authentication */
170250003Sadrianu_long	sys_declined;		/* declined */
171250003Sadrianu_long	sys_limitrejected;	/* rate exceeded */
172250003Sadrianu_long	sys_kodsent;		/* KoD sent */
173250003Sadrian
174250003Sadrian/*
175250003Sadrian * Mechanism knobs: how soon do we peer_clear() or unpeer()?
176250003Sadrian *
177250003Sadrian * The default way is "on-receipt".  If this was a packet from a
178250003Sadrian * well-behaved source, on-receipt will offer the fastest recovery.
179250003Sadrian * If this was from a DoS attack, the default way makes it easier
180250003Sadrian * for a bad-guy to DoS us.  So look and see what bites you harder
181250003Sadrian * and choose according to your environment.
182250003Sadrian */
183250003Sadrianint peer_clear_digest_early	= 1;	/* bad digest (TEST5) and Autokey */
184250003Sadrianint unpeer_crypto_early		= 1;	/* bad crypto (TEST9) */
185250003Sadrianint unpeer_crypto_nak_early	= 1;	/* crypto_NAK (TEST5) */
186250003Sadrianint unpeer_digest_early		= 1;	/* bad digest (TEST5) */
187250003Sadrian
188250003Sadrianint dynamic_interleave = DYNAMIC_INTERLEAVE;	/* Bug 2978 mitigation */
189250003Sadrian
190250003Sadrianint kiss_code_check(u_char hisleap, u_char hisstratum, u_char hismode, u_int32 refid);
191250003Sadriannak_code	valid_NAK	(struct peer *peer, struct recvbuf *rbufp, u_char hismode);
192250003Sadrianstatic	double	root_distance	(struct peer *);
193250003Sadrianstatic	void	clock_combine	(peer_select *, int, int);
194250003Sadrianstatic	void	peer_xmit	(struct peer *);
195250008Sadrianstatic	void	fast_xmit	(struct recvbuf *, int, keyid_t, int);
196250003Sadrianstatic	void	pool_xmit	(struct peer *);
197250003Sadrianstatic	void	clock_update	(struct peer *);
198250003Sadrianstatic	void	measure_precision(void);
199250003Sadrianstatic	double	measure_tick_fuzz(void);
200250003Sadrianstatic	int	local_refid	(struct peer *);
201250003Sadrianstatic	int	peer_unfit	(struct peer *);
202250003Sadrian#ifdef AUTOKEY
203250003Sadrianstatic	int	group_test	(char *, char *);
204250003Sadrian#endif /* AUTOKEY */
205250003Sadrian#ifdef WORKER
206250003Sadrianvoid	pool_name_resolved	(int, int, void *, const char *,
207250003Sadrian				 const char *, const struct addrinfo *,
208250003Sadrian				 const struct addrinfo *);
209250003Sadrian#endif /* WORKER */
210250003Sadrian
211250003Sadrianconst char *	amtoa		(int am);
212250003Sadrian
213250003Sadrian
214250003Sadrianvoid
215250003Sadrianset_sys_leap(
216250003Sadrian	u_char new_sys_leap
217250003Sadrian	)
218250003Sadrian{
219250003Sadrian	sys_leap = new_sys_leap;
220250003Sadrian	xmt_leap = sys_leap;
221250003Sadrian
222250003Sadrian	/*
223250003Sadrian	 * Under certain conditions we send faked leap bits to clients, so
224250003Sadrian	 * eventually change xmt_leap below, but never change LEAP_NOTINSYNC.
225250003Sadrian	 */
226250003Sadrian	if (xmt_leap != LEAP_NOTINSYNC) {
227250003Sadrian		if (leap_sec_in_progress) {
228250003Sadrian			/* always send "not sync" */
229250003Sadrian			xmt_leap = LEAP_NOTINSYNC;
230250003Sadrian		}
231250003Sadrian#ifdef LEAP_SMEAR
232250003Sadrian		else {
233250003Sadrian			/*
234250003Sadrian			 * If leap smear is enabled in general we must
235250003Sadrian			 * never send a leap second warning to clients,
236250003Sadrian			 * so make sure we only send "in sync".
237250003Sadrian			 */
238250003Sadrian			if (leap_smear.enabled)
239250003Sadrian				xmt_leap = LEAP_NOWARNING;
240250003Sadrian		}
241250003Sadrian#endif	/* LEAP_SMEAR */
242250003Sadrian	}
243250003Sadrian}
244250003Sadrian
245250003Sadrian
246250003Sadrian/*
247250003Sadrian * Kiss Code check
248250003Sadrian */
249250003Sadrianint
250250003Sadriankiss_code_check(
251250003Sadrian	u_char hisleap,
252250003Sadrian	u_char hisstratum,
253250003Sadrian	u_char hismode,
254250003Sadrian	u_int32 refid
255250003Sadrian	)
256250003Sadrian{
257250003Sadrian
258250003Sadrian	if (   hismode == MODE_SERVER
259250003Sadrian	    && hisleap == LEAP_NOTINSYNC
260250003Sadrian	    && hisstratum == STRATUM_UNSPEC) {
261250003Sadrian		if(memcmp(&refid,"RATE", 4) == 0) {
262250003Sadrian			return (RATEKISS);
263250003Sadrian		} else if(memcmp(&refid,"DENY", 4) == 0) {
264250003Sadrian			return (DENYKISS);
265250003Sadrian		} else if(memcmp(&refid,"RSTR", 4) == 0) {
266250003Sadrian			return (RSTRKISS);
267250003Sadrian		} else if(memcmp(&refid,"X", 1) == 0) {
268250003Sadrian			return (XKISS);
269250003Sadrian		}
270250003Sadrian	}
271250003Sadrian	return (NOKISS);
272250003Sadrian}
273250003Sadrian
274250003Sadrian
275250003Sadrian/*
276250003Sadrian * Check that NAK is valid
277250003Sadrian */
278250003Sadriannak_code
279250003Sadrianvalid_NAK(
280250003Sadrian	  struct peer *peer,
281250003Sadrian	  struct recvbuf *rbufp,
282250003Sadrian	  u_char hismode
283250003Sadrian	  )
284250003Sadrian{
285250003Sadrian	int		base_packet_length = MIN_V4_PKT_LEN;
286250003Sadrian	int		remainder_size;
287250003Sadrian	struct pkt *	rpkt;
288250003Sadrian	int		keyid;
289250003Sadrian	l_fp		p_org;	/* origin timestamp */
290250003Sadrian	const l_fp *	myorg;	/* selected peer origin */
291250003Sadrian
292250003Sadrian	/*
293250003Sadrian	 * Check to see if there is something beyond the basic packet
294250003Sadrian	 */
295250003Sadrian	if (rbufp->recv_length == base_packet_length) {
296250003Sadrian		return NONAK;
297250003Sadrian	}
298250003Sadrian
299250003Sadrian	remainder_size = rbufp->recv_length - base_packet_length;
300250003Sadrian	/*
301250003Sadrian	 * Is this a potential NAK?
302250003Sadrian	 */
303250003Sadrian	if (remainder_size != 4) {
304250003Sadrian		return NONAK;
305250003Sadrian	}
306250003Sadrian
307250003Sadrian	/*
308250003Sadrian	 * Only server responses can contain NAK's
309250003Sadrian	 */
310250003Sadrian
311250003Sadrian	if (hismode != MODE_SERVER &&
312250003Sadrian	    hismode != MODE_ACTIVE &&
313250003Sadrian	    hismode != MODE_PASSIVE
314250003Sadrian	    ) {
315250003Sadrian		return INVALIDNAK;
316250003Sadrian	}
317250003Sadrian
318250003Sadrian	/*
319250003Sadrian	 * Make sure that the extra field in the packet is all zeros
320250003Sadrian	 */
321250003Sadrian	rpkt = &rbufp->recv_pkt;
322250003Sadrian	keyid = ntohl(((u_int32 *)rpkt)[base_packet_length / 4]);
323250003Sadrian	if (keyid != 0) {
324250003Sadrian		return INVALIDNAK;
325250003Sadrian	}
326250003Sadrian
327250003Sadrian	/*
328250003Sadrian	 * During the first few packets of the autokey dance there will
329250003Sadrian	 * not (yet) be a keyid, but in this case FLAG_SKEY is set.
330250003Sadrian	 * So the NAK is invalid if either there's no peer, or
331250003Sadrian	 * if the keyid is 0 and FLAG_SKEY is not set.
332250003Sadrian	 */
333250003Sadrian	if (!peer || (!peer->keyid && !(peer->flags & FLAG_SKEY))) {
334250003Sadrian		return INVALIDNAK;
335250003Sadrian	}
336250003Sadrian
337250003Sadrian	/*
338250003Sadrian	 * The ORIGIN must match, or this cannot be a valid NAK, either.
339250003Sadrian	 */
340250003Sadrian	NTOHL_FP(&rpkt->org, &p_org);
341250003Sadrian	if (peer->flip > 0)
342250003Sadrian		myorg = &peer->borg;
343250003Sadrian	else
344250003Sadrian		myorg = &peer->aorg;
345250003Sadrian
346250003Sadrian	if (L_ISZERO(&p_org) ||
347250003Sadrian	    L_ISZERO( myorg) ||
348250003Sadrian	    !L_ISEQU(&p_org, myorg)) {
349250003Sadrian		return INVALIDNAK;
350250003Sadrian	}
351250003Sadrian
352250003Sadrian	/* If we ever passed all that checks, we should be safe. Well,
353250003Sadrian	 * as safe as we can ever be with an unauthenticated crypto-nak.
354250003Sadrian	 */
355250003Sadrian	return VALIDNAK;
356250003Sadrian}
357250003Sadrian
358250003Sadrian
359250003Sadrian/*
360250003Sadrian * transmit - transmit procedure called by poll timeout
361250003Sadrian */
362250003Sadrianvoid
363250003Sadriantransmit(
364250003Sadrian	struct peer *peer	/* peer structure pointer */
365250003Sadrian	)
366250003Sadrian{
367250003Sadrian	u_char	hpoll;
368250003Sadrian
369250003Sadrian	/*
370250003Sadrian	 * The polling state machine. There are two kinds of machines,
371250003Sadrian	 * those that never expect a reply (broadcast and manycast
372250003Sadrian	 * server modes) and those that do (all other modes). The dance
373250003Sadrian	 * is intricate...
374250003Sadrian	 */
375250003Sadrian	hpoll = peer->hpoll;
376250003Sadrian
377250003Sadrian	/*
378250003Sadrian	 * If we haven't received anything (even if unsync) since last
379250003Sadrian	 * send, reset ppoll.
380250003Sadrian	 */
381250003Sadrian	if (peer->outdate > peer->timelastrec && !peer->reach)
382250003Sadrian		peer->ppoll = peer->maxpoll;
383250003Sadrian
384250003Sadrian	/*
385250003Sadrian	 * In broadcast mode the poll interval is never changed from
386250003Sadrian	 * minpoll.
387250003Sadrian	 */
388250003Sadrian	if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) {
389250003Sadrian		peer->outdate = current_time;
390250003Sadrian		if (sys_leap != LEAP_NOTINSYNC)
391250003Sadrian			peer_xmit(peer);
392250003Sadrian		poll_update(peer, hpoll);
393250003Sadrian		return;
394250003Sadrian	}
395250003Sadrian
396250003Sadrian	/*
397250003Sadrian	 * In manycast mode we start with unity ttl. The ttl is
398250003Sadrian	 * increased by one for each poll until either sys_maxclock
399250003Sadrian	 * servers have been found or the maximum ttl is reached. When
400250003Sadrian	 * sys_maxclock servers are found we stop polling until one or
401250003Sadrian	 * more servers have timed out or until less than sys_minclock
402250003Sadrian	 * associations turn up. In this case additional better servers
403250003Sadrian	 * are dragged in and preempt the existing ones.  Once every
404250003Sadrian	 * sys_beacon seconds we are to transmit unconditionally, but
405250003Sadrian	 * this code is not quite right -- peer->unreach counts polls
406250003Sadrian	 * and is being compared with sys_beacon, so the beacons happen
407250008Sadrian	 * every sys_beacon polls.
408250008Sadrian	 */
409250008Sadrian	if (peer->cast_flags & MDF_ACAST) {
410250008Sadrian		peer->outdate = current_time;
411250003Sadrian		if (peer->unreach > sys_beacon) {
412250003Sadrian			peer->unreach = 0;
413250003Sadrian			peer->ttl = 0;
414250003Sadrian			peer_xmit(peer);
415250003Sadrian		} else if (   sys_survivors < sys_minclock
416250003Sadrian			   || peer_associations < sys_maxclock) {
417250003Sadrian			if (peer->ttl < sys_ttlmax)
418250003Sadrian				peer->ttl++;
419250003Sadrian			peer_xmit(peer);
420250003Sadrian		}
421250003Sadrian		peer->unreach++;
422250003Sadrian		poll_update(peer, hpoll);
423250003Sadrian		return;
424250003Sadrian	}
425250003Sadrian
426250003Sadrian	/*
427250003Sadrian	 * Pool associations transmit unicast solicitations when there
428250003Sadrian	 * are less than a hard limit of 2 * sys_maxclock associations,
429250003Sadrian	 * and either less than sys_minclock survivors or less than
430250003Sadrian	 * sys_maxclock associations.  The hard limit prevents unbounded
431250003Sadrian	 * growth in associations if the system clock or network quality
432250003Sadrian	 * result in survivor count dipping below sys_minclock often.
433250003Sadrian	 * This was observed testing with pool, where sys_maxclock == 12
434250003Sadrian	 * resulted in 60 associations without the hard limit.  A
435250003Sadrian	 * similar hard limit on manycastclient ephemeral associations
436250003Sadrian	 * may be appropriate.
437250003Sadrian	 */
438250008Sadrian	if (peer->cast_flags & MDF_POOL) {
439250003Sadrian		peer->outdate = current_time;
440250003Sadrian		if (   (peer_associations <= 2 * sys_maxclock)
441250008Sadrian		    && (   peer_associations < sys_maxclock
442250003Sadrian			|| sys_survivors < sys_minclock))
443250003Sadrian			pool_xmit(peer);
444250003Sadrian		poll_update(peer, hpoll);
445250003Sadrian		return;
446250008Sadrian	}
447250008Sadrian
448250003Sadrian	/*
449250003Sadrian	 * In unicast modes the dance is much more intricate. It is
450250003Sadrian	 * designed to back off whenever possible to minimize network
451250003Sadrian	 * traffic.
452250003Sadrian	 */
453250003Sadrian	if (peer->burst == 0) {
454250003Sadrian		u_char oreach;
455250003Sadrian
456250003Sadrian		/*
457250003Sadrian		 * Update the reachability status. If not heard for
458250003Sadrian		 * three consecutive polls, stuff infinity in the clock
459250003Sadrian		 * filter.
460250003Sadrian		 */
461250003Sadrian		oreach = peer->reach;
462250003Sadrian		peer->outdate = current_time;
463250003Sadrian		peer->unreach++;
464250003Sadrian		peer->reach <<= 1;
465250003Sadrian		if (!peer->reach) {
466250003Sadrian
467250003Sadrian			/*
468250003Sadrian			 * Here the peer is unreachable. If it was
469250003Sadrian			 * previously reachable raise a trap. Send a
470250008Sadrian			 * burst if enabled.
471250003Sadrian			 */
472250003Sadrian			clock_filter(peer, 0., 0., MAXDISPERSE);
473250003Sadrian			if (oreach) {
474250008Sadrian				peer_unfit(peer);
475250003Sadrian				report_event(PEVNT_UNREACH, peer, NULL);
476250003Sadrian			}
477250003Sadrian			if (   (peer->flags & FLAG_IBURST)
478250003Sadrian			    && peer->retry == 0)
479250003Sadrian				peer->retry = NTP_RETRY;
480250003Sadrian		} else {
481250003Sadrian
482250003Sadrian			/*
483250003Sadrian			 * Here the peer is reachable. Send a burst if
484250003Sadrian			 * enabled and the peer is fit.  Reset unreach
485250003Sadrian			 * for persistent and ephemeral associations.
486250003Sadrian			 * Unreach is also reset for survivors in
487250003Sadrian			 * clock_select().
488250003Sadrian			 */
489250003Sadrian			hpoll = sys_poll;
490250003Sadrian			if (!(peer->flags & FLAG_PREEMPT))
491250003Sadrian				peer->unreach = 0;
492250003Sadrian			if (   (peer->flags & FLAG_BURST)
493250003Sadrian			    && peer->retry == 0
494250003Sadrian			    && !peer_unfit(peer))
495250003Sadrian				peer->retry = NTP_RETRY;
496250003Sadrian		}
497250003Sadrian
498250003Sadrian		/*
499250003Sadrian		 * Watch for timeout.  If ephemeral, toss the rascal;
500250003Sadrian		 * otherwise, bump the poll interval. Note the
501250003Sadrian		 * poll_update() routine will clamp it to maxpoll.
502250003Sadrian		 * If preemptible and we have more peers than maxclock,
503250003Sadrian		 * and this peer has the minimum score of preemptibles,
504250003Sadrian		 * demobilize.
505250003Sadrian		 */
506250003Sadrian		if (peer->unreach >= NTP_UNREACH) {
507250003Sadrian			hpoll++;
508250003Sadrian			/* ephemeral: no FLAG_CONFIG nor FLAG_PREEMPT */
509250003Sadrian			if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
510250003Sadrian				report_event(PEVNT_RESTART, peer, "timeout");
511250003Sadrian				peer_clear(peer, "TIME");
512250003Sadrian				unpeer(peer);
513250003Sadrian				return;
514250003Sadrian			}
515250003Sadrian			if (   (peer->flags & FLAG_PREEMPT)
516250003Sadrian			    && (peer_associations > sys_maxclock)
517250008Sadrian			    && score_all(peer)) {
518250003Sadrian				report_event(PEVNT_RESTART, peer, "timeout");
519250003Sadrian				peer_clear(peer, "TIME");
520250003Sadrian				unpeer(peer);
521250003Sadrian				return;
522250008Sadrian			}
523250003Sadrian		}
524250003Sadrian	} else {
525250008Sadrian		peer->burst--;
526250003Sadrian		if (peer->burst == 0) {
527250008Sadrian
528250003Sadrian			/*
529250003Sadrian			 * If ntpdate mode and the clock has not been
530250003Sadrian			 * set and all peers have completed the burst,
531250003Sadrian			 * we declare a successful failure.
532250003Sadrian			 */
533250003Sadrian			if (mode_ntpdate) {
534250003Sadrian				peer_ntpdate--;
535250003Sadrian				if (peer_ntpdate == 0) {
536250003Sadrian					msyslog(LOG_NOTICE,
537250003Sadrian					    "ntpd: no servers found");
538250003Sadrian					if (!msyslog_term)
539250003Sadrian						printf(
540250003Sadrian						    "ntpd: no servers found\n");
541250003Sadrian					exit (0);
542250003Sadrian				}
543250003Sadrian			}
544250003Sadrian		}
545250003Sadrian	}
546250003Sadrian	if (peer->retry > 0)
547250003Sadrian		peer->retry--;
548250003Sadrian
549250003Sadrian	/*
550250003Sadrian	 * Do not transmit if in broadcast client mode.
551250003Sadrian	 */
552250003Sadrian	if (peer->hmode != MODE_BCLIENT)
553250003Sadrian		peer_xmit(peer);
554250003Sadrian	poll_update(peer, hpoll);
555250003Sadrian
556250003Sadrian	return;
557250003Sadrian}
558250003Sadrian
559250003Sadrian
560250003Sadrianconst char *
561250003Sadrianamtoa(
562250003Sadrian	int am
563250003Sadrian	)
564250003Sadrian{
565250003Sadrian	char *bp;
566250003Sadrian
567250003Sadrian	switch(am) {
568250003Sadrian	    case AM_ERR:	return "AM_ERR";
569250003Sadrian	    case AM_NOMATCH:	return "AM_NOMATCH";
570250003Sadrian	    case AM_PROCPKT:	return "AM_PROCPKT";
571250003Sadrian	    case AM_BCST:	return "AM_BCST";
572250003Sadrian	    case AM_FXMIT:	return "AM_FXMIT";
573250003Sadrian	    case AM_MANYCAST:	return "AM_MANYCAST";
574250003Sadrian	    case AM_NEWPASS:	return "AM_NEWPASS";
575250003Sadrian	    case AM_NEWBCL:	return "AM_NEWBCL";
576250003Sadrian	    case AM_POSSBCL:	return "AM_POSSBCL";
577250003Sadrian	    default:
578250003Sadrian		LIB_GETBUF(bp);
579250003Sadrian		snprintf(bp, LIB_BUFLENGTH, "AM_#%d", am);
580250003Sadrian		return bp;
581250003Sadrian	}
582250003Sadrian}
583250003Sadrian
584250003Sadrian
585250003Sadrian/*
586250003Sadrian * receive - receive procedure called for each packet received
587250003Sadrian */
588250003Sadrianvoid
589250003Sadrianreceive(
590250003Sadrian	struct recvbuf *rbufp
591250003Sadrian	)
592250003Sadrian{
593250003Sadrian	register struct peer *peer;	/* peer structure pointer */
594250003Sadrian	register struct pkt *pkt;	/* receive packet pointer */
595250003Sadrian	u_char	hisversion;		/* packet version */
596250003Sadrian	u_char	hisleap;		/* packet leap indicator */
597250003Sadrian	u_char	hismode;		/* packet mode */
598250003Sadrian	u_char	hisstratum;		/* packet stratum */
599250003Sadrian	r4addr	r4a;			/* address restrictions */
600250003Sadrian	u_short	restrict_mask;		/* restrict bits */
601250003Sadrian	const char *hm_str;		/* hismode string */
602250003Sadrian	const char *am_str;		/* association match string */
603250003Sadrian	int	kissCode = NOKISS;	/* Kiss Code */
604250003Sadrian	int	has_mac;		/* length of MAC field */
605250003Sadrian	int	authlen;		/* offset of MAC field */
606250003Sadrian	auth_code is_authentic = AUTH_UNKNOWN;	/* Was AUTH_NONE */
607250003Sadrian	nak_code crypto_nak_test;	/* result of crypto-NAK check */
608250003Sadrian	int	retcode = AM_NOMATCH;	/* match code */
609250003Sadrian	keyid_t	skeyid = 0;		/* key IDs */
610250003Sadrian	u_int32	opcode = 0;		/* extension field opcode */
611250003Sadrian	sockaddr_u *dstadr_sin;		/* active runway */
612250003Sadrian	struct peer *peer2;		/* aux peer structure pointer */
613250003Sadrian	endpt	*match_ep;		/* newpeer() local address */
614250003Sadrian	l_fp	p_org;			/* origin timestamp */
615250003Sadrian	l_fp	p_rec;			/* receive timestamp */
616250003Sadrian	l_fp	p_xmt;			/* transmit timestamp */
617250003Sadrian#ifdef AUTOKEY
618250003Sadrian	char	hostname[NTP_MAXSTRLEN + 1];
619250003Sadrian	char	*groupname = NULL;
620250003Sadrian	struct autokey *ap;		/* autokey structure pointer */
621250003Sadrian	int	rval;			/* cookie snatcher */
622250003Sadrian	keyid_t	pkeyid = 0, tkeyid = 0;	/* key IDs */
623250003Sadrian#endif	/* AUTOKEY */
624250003Sadrian#ifdef HAVE_NTP_SIGND
625250003Sadrian	static unsigned char zero_key[16];
626250003Sadrian#endif /* HAVE_NTP_SIGND */
627250003Sadrian
628250003Sadrian	/*
629250003Sadrian	 * Note that there are many places we do not call record_raw_stats().
630250003Sadrian	 *
631250003Sadrian	 * We only want to call it *after* we've sent a response, or perhaps
632250003Sadrian	 * when we've decided to drop a packet.
633250003Sadrian	 */
634250003Sadrian
635250003Sadrian	/*
636250003Sadrian	 * Monitor the packet and get restrictions. Note that the packet
637250003Sadrian	 * length for control and private mode packets must be checked
638250003Sadrian	 * by the service routines. Some restrictions have to be handled
639250003Sadrian	 * later in order to generate a kiss-o'-death packet.
640250003Sadrian	 */
641250003Sadrian	/*
642250003Sadrian	 * Bogus port check is before anything, since it probably
643250003Sadrian	 * reveals a clogging attack.
644250003Sadrian	 */
645250003Sadrian	sys_received++;
646250003Sadrian	if (0 == SRCPORT(&rbufp->recv_srcadr)) {
647250003Sadrian		sys_badlength++;
648250003Sadrian		return;				/* bogus port */
649250003Sadrian	}
650250003Sadrian	restrictions(&rbufp->recv_srcadr, &r4a);
651250003Sadrian	restrict_mask = r4a.rflags;
652250003Sadrian
653250003Sadrian	pkt = &rbufp->recv_pkt;
654250003Sadrian	hisversion = PKT_VERSION(pkt->li_vn_mode);
655250003Sadrian	hisleap = PKT_LEAP(pkt->li_vn_mode);
656250003Sadrian	hismode = (int)PKT_MODE(pkt->li_vn_mode);
657250003Sadrian	hisstratum = PKT_TO_STRATUM(pkt->stratum);
658250003Sadrian	DPRINTF(1, ("receive: at %ld %s<-%s ippeerlimit %d mode %d iflags %s restrict %s org %#010x.%08x xmt %#010x.%08x\n",
659250003Sadrian		    current_time, stoa(&rbufp->dstadr->sin),
660250003Sadrian		    stoa(&rbufp->recv_srcadr), r4a.ippeerlimit, hismode,
661250003Sadrian		    build_iflags(rbufp->dstadr->flags),
662250003Sadrian		    build_rflags(restrict_mask),
663250003Sadrian		    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
664250003Sadrian		    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
665250003Sadrian
666250003Sadrian	/* See basic mode and broadcast checks, below */
667250003Sadrian	INSIST(0 != hisstratum);
668250003Sadrian
669250003Sadrian	if (restrict_mask & RES_IGNORE) {
670250003Sadrian		DPRINTF(2, ("receive: drop: RES_IGNORE\n"));
671250003Sadrian		sys_restricted++;
672250003Sadrian		return;				/* ignore everything */
673250003Sadrian	}
674250003Sadrian	if (hismode == MODE_PRIVATE) {
675250003Sadrian		if (!ntp_mode7 || (restrict_mask & RES_NOQUERY)) {
676250003Sadrian			DPRINTF(2, ("receive: drop: RES_NOQUERY\n"));
677250003Sadrian			sys_restricted++;
678250003Sadrian			return;			/* no query private */
679250003Sadrian		}
680250003Sadrian		process_private(rbufp, ((restrict_mask &
681250003Sadrian		    RES_NOMODIFY) == 0));
682250003Sadrian		return;
683250003Sadrian	}
684250003Sadrian	if (hismode == MODE_CONTROL) {
685250003Sadrian		if (restrict_mask & RES_NOQUERY) {
686250003Sadrian			DPRINTF(2, ("receive: drop: RES_NOQUERY\n"));
687250003Sadrian			sys_restricted++;
688250003Sadrian			return;			/* no query control */
689250003Sadrian		}
690250003Sadrian		process_control(rbufp, restrict_mask);
691250003Sadrian		return;
692250003Sadrian	}
693250003Sadrian	if (restrict_mask & RES_DONTSERVE) {
694250003Sadrian		DPRINTF(2, ("receive: drop: RES_DONTSERVE\n"));
695250003Sadrian		sys_restricted++;
696250003Sadrian		return;				/* no time serve */
697250003Sadrian	}
698250003Sadrian
699250003Sadrian	/*
700250003Sadrian	 * This is for testing. If restricted drop ten percent of
701250003Sadrian	 * surviving packets.
702250003Sadrian	 */
703250003Sadrian	if (restrict_mask & RES_FLAKE) {
704250003Sadrian		if ((double)ntp_random() / 0x7fffffff < .1) {
705250003Sadrian			DPRINTF(2, ("receive: drop: RES_FLAKE\n"));
706250003Sadrian			sys_restricted++;
707250003Sadrian			return;			/* no flakeway */
708250003Sadrian		}
709250003Sadrian	}
710250003Sadrian
711250003Sadrian	/*
712250003Sadrian	** Format Layer Checks
713250003Sadrian	**
714250003Sadrian	** Validate the packet format.  The packet size, packet header,
715250003Sadrian	** and any extension field lengths are checked.  We identify
716250003Sadrian	** the beginning of the MAC, to identify the upper limit of
717250003Sadrian	** of the hash computation.
718250003Sadrian	**
719250003Sadrian	** In case of a format layer check violation, the packet is
720250003Sadrian	** discarded with no further processing.
721250003Sadrian	*/
722250003Sadrian
723250003Sadrian	/*
724250003Sadrian	 * Version check must be after the query packets, since they
725250003Sadrian	 * intentionally use an early version.
726250003Sadrian	 */
727250003Sadrian	if (hisversion == NTP_VERSION) {
728250003Sadrian		sys_newversion++;		/* new version */
729250003Sadrian	} else if (   !(restrict_mask & RES_VERSION)
730250003Sadrian		   && hisversion >= NTP_OLDVERSION) {
731250003Sadrian		sys_oldversion++;		/* previous version */
732250003Sadrian	} else {
733250003Sadrian		DPRINTF(2, ("receive: drop: RES_VERSION\n"));
734250003Sadrian		sys_badlength++;
735250003Sadrian		return;				/* old version */
736250003Sadrian	}
737250003Sadrian
738250003Sadrian	/*
739250003Sadrian	 * Figure out his mode and validate the packet. This has some
740250003Sadrian	 * legacy raunch that probably should be removed. In very early
741250003Sadrian	 * NTP versions mode 0 was equivalent to what later versions
742250003Sadrian	 * would interpret as client mode.
743250003Sadrian	 */
744250003Sadrian	if (hismode == MODE_UNSPEC) {
745250003Sadrian		if (hisversion == NTP_OLDVERSION) {
746250003Sadrian			hismode = MODE_CLIENT;
747250003Sadrian		} else {
748250003Sadrian			DPRINTF(2, ("receive: drop: MODE_UNSPEC\n"));
749250003Sadrian			sys_badlength++;
750250003Sadrian			return;			/* invalid mode */
751250003Sadrian		}
752250003Sadrian	}
753250003Sadrian
754250003Sadrian	/*
755250003Sadrian	 * Parse the extension field if present. We figure out whether
756250003Sadrian	 * an extension field is present by measuring the MAC size. If
757250003Sadrian	 * the number of words following the packet header is 0, no MAC
758250003Sadrian	 * is present and the packet is not authenticated. If 1, the
759250003Sadrian	 * packet is a crypto-NAK; if 3, the packet is authenticated
760250003Sadrian	 * with DES; if 5, the packet is authenticated with MD5; if 6,
761250003Sadrian	 * the packet is authenticated with SHA. If 2 or * 4, the packet
762250003Sadrian	 * is a runt and discarded forthwith. If greater than 6, an
763250003Sadrian	 * extension field is present, so we subtract the length of the
764250003Sadrian	 * field and go around again.
765250003Sadrian	 *
766250003Sadrian	 * Note the above description is lame.  We should/could also check
767250003Sadrian	 * the two bytes that make up the EF type and subtype, and then
768250003Sadrian	 * check the two bytes that tell us the EF length.  A legacy MAC
769250003Sadrian	 * has a 4 byte keyID, and for conforming symmetric keys its value
770250003Sadrian	 * must be <= 64k, meaning the top two bytes will always be zero.
771250003Sadrian	 * Since the EF Type of 0 is reserved/unused, there's no way a
772250003Sadrian	 * conforming legacy MAC could ever be misinterpreted as an EF.
773250003Sadrian	 *
774250003Sadrian	 * There is more, but this isn't the place to document it.
775250003Sadrian	 */
776250003Sadrian
777250003Sadrian	authlen = LEN_PKT_NOMAC;
778250003Sadrian	has_mac = rbufp->recv_length - authlen;
779250003Sadrian	while (has_mac > 0) {
780250003Sadrian		u_int32	len;
781250003Sadrian#ifdef AUTOKEY
782250003Sadrian		u_int32	hostlen;
783250003Sadrian		struct exten *ep;
784250003Sadrian#endif /*AUTOKEY */
785250003Sadrian
786250003Sadrian		if (has_mac % 4 != 0 || has_mac < (int)MIN_MAC_LEN) {
787250003Sadrian			DPRINTF(2, ("receive: drop: bad post-packet length\n"));
788250003Sadrian			sys_badlength++;
789250003Sadrian			return;			/* bad length */
790250003Sadrian		}
791250003Sadrian		/*
792250003Sadrian		 * This next test is clearly wrong - it needlessly
793250003Sadrian		 * prohibits short EFs (which don't yet exist)
794250003Sadrian		 */
795250003Sadrian		if (has_mac <= (int)MAX_MAC_LEN) {
796250003Sadrian			skeyid = ntohl(((u_int32 *)pkt)[authlen / 4]);
797250003Sadrian			break;
798250003Sadrian
799250003Sadrian		} else {
800250003Sadrian			opcode = ntohl(((u_int32 *)pkt)[authlen / 4]);
801250003Sadrian			len = opcode & 0xffff;
802250003Sadrian			if (   len % 4 != 0
803250003Sadrian			    || len < 4
804250003Sadrian			    || (int)len + authlen > rbufp->recv_length) {
805250003Sadrian				DPRINTF(2, ("receive: drop: bad EF length\n"));
806250003Sadrian				sys_badlength++;
807250003Sadrian				return;		/* bad length */
808250003Sadrian			}
809250003Sadrian#ifdef AUTOKEY
810250003Sadrian			/*
811250003Sadrian			 * Extract calling group name for later.  If
812250003Sadrian			 * sys_groupname is non-NULL, there must be
813250003Sadrian			 * a group name provided to elicit a response.
814250003Sadrian			 */
815250003Sadrian			if (   (opcode & 0x3fff0000) == CRYPTO_ASSOC
816250003Sadrian			    && sys_groupname != NULL) {
817250003Sadrian				ep = (struct exten *)&((u_int32 *)pkt)[authlen / 4];
818250003Sadrian				hostlen = ntohl(ep->vallen);
819250003Sadrian				if (   hostlen >= sizeof(hostname)
820250003Sadrian				    || hostlen > len -
821250003Sadrian						offsetof(struct exten, pkt)) {
822250003Sadrian					DPRINTF(2, ("receive: drop: bad autokey hostname length\n"));
823250003Sadrian					sys_badlength++;
824250003Sadrian					return;		/* bad length */
825250003Sadrian				}
826250003Sadrian				memcpy(hostname, &ep->pkt, hostlen);
827250003Sadrian				hostname[hostlen] = '\0';
828250003Sadrian				groupname = strchr(hostname, '@');
829250003Sadrian				if (groupname == NULL) {
830250003Sadrian					DPRINTF(2, ("receive: drop: empty autokey groupname\n"));
831250003Sadrian					sys_declined++;
832250003Sadrian					return;
833250003Sadrian				}
834250003Sadrian				groupname++;
835250003Sadrian			}
836250003Sadrian#endif /* AUTOKEY */
837250003Sadrian			authlen += len;
838250003Sadrian			has_mac -= len;
839250003Sadrian		}
840250003Sadrian	}
841250003Sadrian
842250003Sadrian	/*
843250003Sadrian	 * If has_mac is < 0 we had a malformed packet.
844250003Sadrian	 */
845250003Sadrian	if (has_mac < 0) {
846250003Sadrian		DPRINTF(2, ("receive: drop: post-packet under-read\n"));
847250003Sadrian		sys_badlength++;
848250003Sadrian		return;		/* bad length */
849250003Sadrian	}
850250003Sadrian
851250003Sadrian	/*
852250003Sadrian	** Packet Data Verification Layer
853250003Sadrian	**
854250003Sadrian	** This layer verifies the packet data content.  If
855250003Sadrian	** authentication is required, a MAC must be present.
856250003Sadrian	** If a MAC is present, it must validate.
857250003Sadrian	** Crypto-NAK?  Look - a shiny thing!
858250003Sadrian	**
859250003Sadrian	** If authentication fails, we're done.
860250003Sadrian	*/
861250003Sadrian
862250003Sadrian	/*
863250003Sadrian	 * If authentication is explicitly required, a MAC must be present.
864250003Sadrian	 */
865250003Sadrian	if (restrict_mask & RES_DONTTRUST && has_mac == 0) {
866250003Sadrian		DPRINTF(2, ("receive: drop: RES_DONTTRUST\n"));
867250003Sadrian		sys_restricted++;
868250003Sadrian		return;				/* access denied */
869250003Sadrian	}
870250003Sadrian
871250003Sadrian	/*
872250003Sadrian	 * Update the MRU list and finger the cloggers. It can be a
873250003Sadrian	 * little expensive, so turn it off for production use.
874250003Sadrian	 * RES_LIMITED and RES_KOD will be cleared in the returned
875250003Sadrian	 * restrict_mask unless one or both actions are warranted.
876250003Sadrian	 */
877250003Sadrian	restrict_mask = ntp_monitor(rbufp, restrict_mask);
878250003Sadrian	if (restrict_mask & RES_LIMITED) {
879250003Sadrian		sys_limitrejected++;
880250003Sadrian		if (   !(restrict_mask & RES_KOD)
881250003Sadrian		    || MODE_BROADCAST == hismode
882250003Sadrian		    || MODE_SERVER == hismode) {
883250003Sadrian			if (MODE_SERVER == hismode) {
884250003Sadrian				DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
885250003Sadrian					stoa(&rbufp->recv_srcadr)));
886250003Sadrian			} else {
887250003Sadrian				DPRINTF(2, ("receive: drop: RES_KOD\n"));
888250003Sadrian			}
889250003Sadrian			return;			/* rate exceeded */
890250003Sadrian		}
891250003Sadrian		if (hismode == MODE_CLIENT)
892250003Sadrian			fast_xmit(rbufp, MODE_SERVER, skeyid,
893250003Sadrian			    restrict_mask);
894250003Sadrian		else
895250003Sadrian			fast_xmit(rbufp, MODE_ACTIVE, skeyid,
896250003Sadrian			    restrict_mask);
897250003Sadrian		return;				/* rate exceeded */
898250003Sadrian	}
899250003Sadrian	restrict_mask &= ~RES_KOD;
900250003Sadrian
901250003Sadrian	/*
902250003Sadrian	 * We have tossed out as many buggy packets as possible early in
903250003Sadrian	 * the game to reduce the exposure to a clogging attack. Now we
904250003Sadrian	 * have to burn some cycles to find the association and
905250003Sadrian	 * authenticate the packet if required. Note that we burn only
906250003Sadrian	 * digest cycles, again to reduce exposure. There may be no
907250003Sadrian	 * matching association and that's okay.
908250003Sadrian	 *
909250003Sadrian	 * More on the autokey mambo. Normally the local interface is
910250003Sadrian	 * found when the association was mobilized with respect to a
911250003Sadrian	 * designated remote address. We assume packets arriving from
912250003Sadrian	 * the remote address arrive via this interface and the local
913250003Sadrian	 * address used to construct the autokey is the unicast address
914250003Sadrian	 * of the interface. However, if the sender is a broadcaster,
915250003Sadrian	 * the interface broadcast address is used instead.
916250003Sadrian	 * Notwithstanding this technobabble, if the sender is a
917250003Sadrian	 * multicaster, the broadcast address is null, so we use the
918250003Sadrian	 * unicast address anyway. Don't ask.
919250003Sadrian	 */
920250003Sadrian
921250003Sadrian	peer = findpeer(rbufp,  hismode, &retcode);
922250003Sadrian	dstadr_sin = &rbufp->dstadr->sin;
923250003Sadrian	NTOHL_FP(&pkt->org, &p_org);
924250003Sadrian	NTOHL_FP(&pkt->rec, &p_rec);
925250003Sadrian	NTOHL_FP(&pkt->xmt, &p_xmt);
926250003Sadrian	hm_str = modetoa(hismode);
927250003Sadrian	am_str = amtoa(retcode);
928250003Sadrian
929250003Sadrian	/*
930250003Sadrian	 * Authentication is conditioned by three switches:
931250003Sadrian	 *
932250003Sadrian	 * NOPEER  (RES_NOPEER) do not mobilize an association unless
933250003Sadrian	 *         authenticated
934250003Sadrian	 * NOTRUST (RES_DONTTRUST) do not allow access unless
935250003Sadrian	 *         authenticated (implies NOPEER)
936250003Sadrian	 * enable  (sys_authenticate) master NOPEER switch, by default
937250003Sadrian	 *         on
938250003Sadrian	 *
939250003Sadrian	 * The NOPEER and NOTRUST can be specified on a per-client basis
940250003Sadrian	 * using the restrict command. The enable switch if on implies
941250003Sadrian	 * NOPEER for all clients. There are four outcomes:
942250003Sadrian	 *
943250003Sadrian	 * NONE    The packet has no MAC.
944250003Sadrian	 * OK      the packet has a MAC and authentication succeeds
945250003Sadrian	 * ERROR   the packet has a MAC and authentication fails
946250003Sadrian	 * CRYPTO  crypto-NAK. The MAC has four octets only.
947250003Sadrian	 *
948250003Sadrian	 * Note: The AUTH(x, y) macro is used to filter outcomes. If x
949250003Sadrian	 * is zero, acceptable outcomes of y are NONE and OK. If x is
950250003Sadrian	 * one, the only acceptable outcome of y is OK.
951250003Sadrian	 */
952250003Sadrian	crypto_nak_test = valid_NAK(peer, rbufp, hismode);
953250003Sadrian
954250003Sadrian	/*
955250003Sadrian	 * Drop any invalid crypto-NAKs
956250003Sadrian	 */
957250003Sadrian	if (crypto_nak_test == INVALIDNAK) {
958250003Sadrian		report_event(PEVNT_AUTH, peer, "Invalid_NAK");
959250003Sadrian		if (0 != peer) {
960250003Sadrian			peer->badNAK++;
961250003Sadrian		}
962250003Sadrian		msyslog(LOG_ERR, "Invalid-NAK error at %ld %s<-%s",
963250003Sadrian			current_time, stoa(dstadr_sin), stoa(&rbufp->recv_srcadr));
964250003Sadrian		return;
965250003Sadrian	}
966250003Sadrian
967250003Sadrian	if (has_mac == 0) {
968250003Sadrian		restrict_mask &= ~RES_MSSNTP;
969250003Sadrian		is_authentic = AUTH_NONE; /* not required */
970250003Sadrian		DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x NOMAC\n",
971250003Sadrian			    current_time, stoa(dstadr_sin),
972250003Sadrian			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
973250003Sadrian			    authlen,
974250003Sadrian			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
975250003Sadrian			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
976250003Sadrian	} else if (crypto_nak_test == VALIDNAK) {
977250003Sadrian		restrict_mask &= ~RES_MSSNTP;
978250003Sadrian		is_authentic = AUTH_CRYPTO; /* crypto-NAK */
979250003Sadrian		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",
980250003Sadrian			    current_time, stoa(dstadr_sin),
981250003Sadrian			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
982250003Sadrian			    skeyid, authlen + has_mac, is_authentic,
983250003Sadrian			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
984250003Sadrian			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
985250003Sadrian
986250003Sadrian#ifdef HAVE_NTP_SIGND
987250003Sadrian		/*
988250003Sadrian		 * If the signature is 20 bytes long, the last 16 of
989250003Sadrian		 * which are zero, then this is a Microsoft client
990250003Sadrian		 * wanting AD-style authentication of the server's
991250003Sadrian		 * reply.
992250003Sadrian		 *
993250003Sadrian		 * This is described in Microsoft's WSPP docs, in MS-SNTP:
994250003Sadrian		 * http://msdn.microsoft.com/en-us/library/cc212930.aspx
995250003Sadrian		 */
996250003Sadrian	} else if (   has_mac == MAX_MD5_LEN
997250003Sadrian		   && (restrict_mask & RES_MSSNTP)
998250003Sadrian		   && (retcode == AM_FXMIT || retcode == AM_NEWPASS)
999250003Sadrian		   && (memcmp(zero_key, (char *)pkt + authlen + 4,
1000250003Sadrian			      MAX_MD5_LEN - 4) == 0)) {
1001250003Sadrian		is_authentic = AUTH_NONE;
1002250003Sadrian		DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x SIGND\n",
1003250003Sadrian			    current_time, stoa(dstadr_sin),
1004250003Sadrian			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1005250003Sadrian			    authlen,
1006250003Sadrian			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1007250003Sadrian			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1008250003Sadrian#endif /* HAVE_NTP_SIGND */
1009250003Sadrian
1010250003Sadrian	} else {
1011250003Sadrian		/*
1012250003Sadrian		 * has_mac is not 0
1013250003Sadrian		 * Not a VALID_NAK
1014250003Sadrian		 * Not an MS-SNTP SIGND packet
1015250003Sadrian		 *
1016250003Sadrian		 * So there is a MAC here.
1017250003Sadrian		 */
1018250003Sadrian
1019250003Sadrian		restrict_mask &= ~RES_MSSNTP;
1020250003Sadrian#ifdef AUTOKEY
1021250003Sadrian		/*
1022250003Sadrian		 * For autokey modes, generate the session key
1023250003Sadrian		 * and install in the key cache. Use the socket
1024250003Sadrian		 * broadcast or unicast address as appropriate.
1025250003Sadrian		 */
1026250003Sadrian		if (crypto_flags && skeyid > NTP_MAXKEY) {
1027250003Sadrian
1028250003Sadrian			/*
1029250003Sadrian			 * More on the autokey dance (AKD). A cookie is
1030250003Sadrian			 * constructed from public and private values.
1031250003Sadrian			 * For broadcast packets, the cookie is public
1032250003Sadrian			 * (zero). For packets that match no
1033250003Sadrian			 * association, the cookie is hashed from the
1034250003Sadrian			 * addresses and private value. For server
1035250003Sadrian			 * packets, the cookie was previously obtained
1036250003Sadrian			 * from the server. For symmetric modes, the
1037250003Sadrian			 * cookie was previously constructed using an
1038250003Sadrian			 * agreement protocol; however, should PKI be
1039250003Sadrian			 * unavailable, we construct a fake agreement as
1040250003Sadrian			 * the EXOR of the peer and host cookies.
1041250003Sadrian			 *
1042250003Sadrian			 * hismode	ephemeral	persistent
1043250003Sadrian			 * =======================================
1044250003Sadrian			 * active	0		cookie#
1045250003Sadrian			 * passive	0%		cookie#
1046250003Sadrian			 * client	sys cookie	0%
1047250003Sadrian			 * server	0%		sys cookie
1048250003Sadrian			 * broadcast	0		0
1049250003Sadrian			 *
1050250003Sadrian			 * # if unsync, 0
1051250003Sadrian			 * % can't happen
1052250003Sadrian			 */
1053250003Sadrian			if (has_mac < (int)MAX_MD5_LEN) {
1054250003Sadrian				DPRINTF(2, ("receive: drop: MD5 digest too short\n"));
1055250003Sadrian				sys_badauth++;
1056250003Sadrian				return;
1057250003Sadrian			}
1058250003Sadrian			if (hismode == MODE_BROADCAST) {
1059250003Sadrian
1060250003Sadrian				/*
1061250003Sadrian				 * For broadcaster, use the interface
1062250003Sadrian				 * broadcast address when available;
1063250003Sadrian				 * otherwise, use the unicast address
1064250003Sadrian				 * found when the association was
1065250003Sadrian				 * mobilized. However, if this is from
1066250003Sadrian				 * the wildcard interface, game over.
1067250003Sadrian				 */
1068250003Sadrian				if (   crypto_flags
1069250003Sadrian				    && rbufp->dstadr ==
1070250003Sadrian				       ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr)) {
1071250003Sadrian					DPRINTF(2, ("receive: drop: BCAST from wildcard\n"));
1072250003Sadrian					sys_restricted++;
1073250003Sadrian					return;		/* no wildcard */
1074250003Sadrian				}
1075250003Sadrian				pkeyid = 0;
1076250003Sadrian				if (!SOCK_UNSPEC(&rbufp->dstadr->bcast))
1077250003Sadrian					dstadr_sin =
1078250003Sadrian					    &rbufp->dstadr->bcast;
1079250003Sadrian			} else if (peer == NULL) {
1080250003Sadrian				pkeyid = session_key(
1081250003Sadrian				    &rbufp->recv_srcadr, dstadr_sin, 0,
1082250003Sadrian				    sys_private, 0);
1083250003Sadrian			} else {
1084250003Sadrian				pkeyid = peer->pcookie;
1085250003Sadrian			}
1086250003Sadrian
1087250003Sadrian			/*
1088250003Sadrian			 * The session key includes both the public
1089250003Sadrian			 * values and cookie. In case of an extension
1090250003Sadrian			 * field, the cookie used for authentication
1091250003Sadrian			 * purposes is zero. Note the hash is saved for
1092250003Sadrian			 * use later in the autokey mambo.
1093250003Sadrian			 */
1094250003Sadrian			if (authlen > (int)LEN_PKT_NOMAC && pkeyid != 0) {
1095250003Sadrian				session_key(&rbufp->recv_srcadr,
1096250003Sadrian				    dstadr_sin, skeyid, 0, 2);
1097250003Sadrian				tkeyid = session_key(
1098250003Sadrian				    &rbufp->recv_srcadr, dstadr_sin,
1099250003Sadrian				    skeyid, pkeyid, 0);
1100250003Sadrian			} else {
1101250003Sadrian				tkeyid = session_key(
1102250003Sadrian				    &rbufp->recv_srcadr, dstadr_sin,
1103250003Sadrian				    skeyid, pkeyid, 2);
1104250003Sadrian			}
1105250003Sadrian
1106250003Sadrian		}
1107250003Sadrian#endif	/* AUTOKEY */
1108250003Sadrian
1109250003Sadrian		/*
1110250003Sadrian		 * Compute the cryptosum. Note a clogging attack may
1111250003Sadrian		 * succeed in bloating the key cache. If an autokey,
1112250003Sadrian		 * purge it immediately, since we won't be needing it
1113250003Sadrian		 * again. If the packet is authentic, it can mobilize an
1114250003Sadrian		 * association. Note that there is no key zero.
1115250003Sadrian		 */
1116250003Sadrian		if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1117250003Sadrian		    has_mac))
1118250003Sadrian			is_authentic = AUTH_ERROR;
1119250003Sadrian		else
1120250003Sadrian			is_authentic = AUTH_OK;
1121250003Sadrian#ifdef AUTOKEY
1122250003Sadrian		if (crypto_flags && skeyid > NTP_MAXKEY)
1123250003Sadrian			authtrust(skeyid, 0);
1124250003Sadrian#endif	/* AUTOKEY */
1125250003Sadrian		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",
1126250003Sadrian			    current_time, stoa(dstadr_sin),
1127250003Sadrian			    stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1128250003Sadrian			    skeyid, authlen + has_mac, is_authentic,
1129250003Sadrian			    ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1130250003Sadrian			    ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1131250003Sadrian	}
1132250003Sadrian
1133250003Sadrian
1134250003Sadrian	/*
1135250003Sadrian	 * Bug 3454:
1136250003Sadrian	 *
1137250003Sadrian	 * Now come at this from a different perspective:
1138250003Sadrian	 * - If we expect a MAC and it's not there, we drop it.
1139250003Sadrian	 * - If we expect one keyID and get another, we drop it.
1140250003Sadrian	 * - If we have a MAC ahd it hasn't been validated yet, try.
1141250003Sadrian	 * - if the provided MAC doesn't validate, we drop it.
1142250003Sadrian	 *
1143250003Sadrian	 * There might be more to this.
1144250003Sadrian	 */
1145250003Sadrian	if (0 != peer && 0 != peer->keyid) {
1146250003Sadrian		/* Should we msyslog() any of these? */
1147250003Sadrian
1148250003Sadrian		/*
1149250003Sadrian		 * This should catch:
1150250003Sadrian		 * - no keyID where one is expected,
1151250003Sadrian		 * - different keyID than what we expect.
1152250003Sadrian		 */
1153250003Sadrian		if (peer->keyid != skeyid) {
1154250003Sadrian			DPRINTF(2, ("receive: drop: Wanted keyID %d, got %d from %s\n",
1155250003Sadrian				    peer->keyid, skeyid,
1156250003Sadrian				    stoa(&rbufp->recv_srcadr)));
1157250003Sadrian			sys_restricted++;
1158250003Sadrian			return;			/* drop: access denied */
1159250003Sadrian		}
1160250003Sadrian
1161250003Sadrian		/*
1162250003Sadrian		 * if has_mac != 0 ...
1163250003Sadrian		 * - If it has not yet been validated, do so.
1164250003Sadrian		 *   (under what circumstances might that happen?)
1165250003Sadrian		 * - if missing or bad MAC, log and drop.
1166250003Sadrian		 */
1167250003Sadrian		if (0 != has_mac) {
1168250003Sadrian			if (is_authentic == AUTH_UNKNOWN) {
1169250003Sadrian				/* How can this happen? */
1170250003Sadrian				DPRINTF(2, ("receive: 3454 check: AUTH_UNKNOWN from %s\n",
1171250003Sadrian				    stoa(&rbufp->recv_srcadr)));
1172250003Sadrian				if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1173250003Sadrian				    has_mac)) {
1174250003Sadrian					/* MAC invalid or not found */
1175250003Sadrian					is_authentic = AUTH_ERROR;
1176250003Sadrian				} else {
1177250003Sadrian					is_authentic = AUTH_OK;
1178250003Sadrian				}
1179250003Sadrian			}
1180250003Sadrian			if (is_authentic != AUTH_OK) {
1181250003Sadrian				DPRINTF(2, ("receive: drop: missing or bad MAC from %s\n",
1182250003Sadrian					    stoa(&rbufp->recv_srcadr)));
1183250003Sadrian				sys_restricted++;
1184250003Sadrian				return;		/* drop: access denied */
1185250003Sadrian			}
1186250003Sadrian		}
1187250003Sadrian	}
1188250003Sadrian	/**/
1189250003Sadrian
1190250003Sadrian	/*
1191250003Sadrian	** On-Wire Protocol Layer
1192250003Sadrian	**
1193250003Sadrian	** Verify protocol operations consistent with the on-wire protocol.
1194250003Sadrian	** The protocol discards bogus and duplicate packets as well as
1195250003Sadrian	** minimizes disruptions doe to protocol restarts and dropped
1196250003Sadrian	** packets.  The operations are controlled by two timestamps:
1197250003Sadrian	** the transmit timestamp saved in the client state variables,
1198250003Sadrian	** and the origin timestamp in the server packet header.  The
1199250003Sadrian	** comparison of these two timestamps is called the loopback test.
1200250003Sadrian	** The transmit timestamp functions as a nonce to verify that the
1201250003Sadrian	** response corresponds to the original request.  The transmit
1202250003Sadrian	** timestamp also serves to discard replays of the most recent
1203250003Sadrian	** packet.  Upon failure of either test, the packet is discarded
1204250003Sadrian	** with no further action.
1205250003Sadrian	*/
1206250003Sadrian
1207250003Sadrian	/*
1208250003Sadrian	 * The association matching rules are implemented by a set of
1209250003Sadrian	 * routines and an association table. A packet matching an
1210250003Sadrian	 * association is processed by the peer process for that
1211250003Sadrian	 * association. If there are no errors, an ephemeral association
1212250003Sadrian	 * is mobilized: a broadcast packet mobilizes a broadcast client
1213250003Sadrian	 * aassociation; a manycast server packet mobilizes a manycast
1214250003Sadrian	 * client association; a symmetric active packet mobilizes a
1215250003Sadrian	 * symmetric passive association.
1216250003Sadrian	 */
1217250003Sadrian	DPRINTF(1, ("receive: MATCH_ASSOC dispatch: mode %d/%s:%s \n",
1218250003Sadrian		hismode, hm_str, am_str));
1219250003Sadrian	switch (retcode) {
1220250003Sadrian
1221250003Sadrian	/*
1222250003Sadrian	 * This is a client mode packet not matching any association. If
1223250003Sadrian	 * an ordinary client, simply toss a server mode packet back
1224250003Sadrian	 * over the fence. If a manycast client, we have to work a
1225250003Sadrian	 * little harder.
1226250003Sadrian	 *
1227250003Sadrian	 * There are cases here where we do not call record_raw_stats().
1228250003Sadrian	 */
1229250003Sadrian	case AM_FXMIT:
1230250003Sadrian
1231250003Sadrian		/*
1232250003Sadrian		 * If authentication OK, send a server reply; otherwise,
1233250003Sadrian		 * send a crypto-NAK.
1234250003Sadrian		 */
1235250003Sadrian		if (!(rbufp->dstadr->flags & INT_MCASTOPEN)) {
1236250003Sadrian			/* HMS: would be nice to log FAST_XMIT|BADAUTH|RESTRICTED */
1237250003Sadrian			record_raw_stats(&rbufp->recv_srcadr,
1238250003Sadrian			    &rbufp->dstadr->sin,
1239250003Sadrian			    &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1240250003Sadrian			    PKT_LEAP(pkt->li_vn_mode),
1241250003Sadrian			    PKT_VERSION(pkt->li_vn_mode),
1242250003Sadrian			    PKT_MODE(pkt->li_vn_mode),
1243250003Sadrian			    PKT_TO_STRATUM(pkt->stratum),
1244250003Sadrian			    pkt->ppoll,
1245250003Sadrian			    pkt->precision,
1246250003Sadrian			    FPTOD(NTOHS_FP(pkt->rootdelay)),
1247250003Sadrian			    FPTOD(NTOHS_FP(pkt->rootdisp)),
1248250003Sadrian			    pkt->refid,
1249250003Sadrian			    rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1250250003Sadrian
1251250003Sadrian			if (AUTH(restrict_mask & RES_DONTTRUST,
1252250003Sadrian			   is_authentic)) {
1253250003Sadrian				fast_xmit(rbufp, MODE_SERVER, skeyid,
1254250003Sadrian				    restrict_mask);
1255250003Sadrian			} else if (is_authentic == AUTH_ERROR) {
1256250003Sadrian				fast_xmit(rbufp, MODE_SERVER, 0,
1257250003Sadrian				    restrict_mask);
1258250003Sadrian				sys_badauth++;
1259250003Sadrian			} else {
1260250003Sadrian				DPRINTF(2, ("receive: AM_FXMIT drop: !mcast restricted\n"));
1261250003Sadrian				sys_restricted++;
1262250003Sadrian			}
1263250003Sadrian
1264250003Sadrian			return;			/* hooray */
1265250003Sadrian		}
1266250003Sadrian
1267250003Sadrian		/*
1268250003Sadrian		 * This must be manycast. Do not respond if not
1269250003Sadrian		 * configured as a manycast server.
1270250003Sadrian		 */
1271250003Sadrian		if (!sys_manycastserver) {
1272250003Sadrian			DPRINTF(2, ("receive: AM_FXMIT drop: Not manycastserver\n"));
1273250003Sadrian			sys_restricted++;
1274250003Sadrian			return;			/* not enabled */
1275250003Sadrian		}
1276250003Sadrian
1277250003Sadrian#ifdef AUTOKEY
1278250003Sadrian		/*
1279250003Sadrian		 * Do not respond if not the same group.
1280250003Sadrian		 */
1281250003Sadrian		if (group_test(groupname, NULL)) {
1282250003Sadrian			DPRINTF(2, ("receive: AM_FXMIT drop: empty groupname\n"));
1283250003Sadrian			sys_declined++;
1284250003Sadrian			return;
1285250003Sadrian		}
1286250003Sadrian#endif /* AUTOKEY */
1287250003Sadrian
1288250003Sadrian		/*
1289250003Sadrian		 * Do not respond if we are not synchronized or our
1290250003Sadrian		 * stratum is greater than the manycaster or the
1291250003Sadrian		 * manycaster has already synchronized to us.
1292250003Sadrian		 */
1293250003Sadrian		if (   sys_leap == LEAP_NOTINSYNC
1294250003Sadrian		    || sys_stratum >= hisstratum
1295250003Sadrian		    || (!sys_cohort && sys_stratum == hisstratum + 1)
1296250003Sadrian		    || rbufp->dstadr->addr_refid == pkt->refid) {
1297250003Sadrian			DPRINTF(2, ("receive: AM_FXMIT drop: LEAP_NOTINSYNC || stratum || loop\n"));
1298250003Sadrian			sys_declined++;
1299250003Sadrian			return;			/* no help */
1300250003Sadrian		}
1301250003Sadrian
1302250003Sadrian		/*
1303250003Sadrian		 * Respond only if authentication succeeds. Don't do a
1304250003Sadrian		 * crypto-NAK, as that would not be useful.
1305250003Sadrian		 */
1306250003Sadrian		if (AUTH(restrict_mask & RES_DONTTRUST, is_authentic)) {
1307250003Sadrian			record_raw_stats(&rbufp->recv_srcadr,
1308250003Sadrian			    &rbufp->dstadr->sin,
1309250003Sadrian			    &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1310250003Sadrian			    PKT_LEAP(pkt->li_vn_mode),
1311250003Sadrian			    PKT_VERSION(pkt->li_vn_mode),
1312250003Sadrian			    PKT_MODE(pkt->li_vn_mode),
1313250003Sadrian			    PKT_TO_STRATUM(pkt->stratum),
1314250003Sadrian			    pkt->ppoll,
1315250003Sadrian			    pkt->precision,
1316250003Sadrian			    FPTOD(NTOHS_FP(pkt->rootdelay)),
1317250003Sadrian			    FPTOD(NTOHS_FP(pkt->rootdisp)),
1318250003Sadrian			    pkt->refid,
1319250003Sadrian			    rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1320250003Sadrian
1321250003Sadrian			fast_xmit(rbufp, MODE_SERVER, skeyid,
1322250003Sadrian			    restrict_mask);
1323250003Sadrian		}
1324250003Sadrian		return;				/* hooray */
1325250003Sadrian
1326250003Sadrian	/*
1327250003Sadrian	 * This is a server mode packet returned in response to a client
1328250003Sadrian	 * mode packet sent to a multicast group address (for
1329250003Sadrian	 * manycastclient) or to a unicast address (for pool). The
1330250003Sadrian	 * origin timestamp is a good nonce to reliably associate the
1331250003Sadrian	 * reply with what was sent. If there is no match, that's
1332250003Sadrian	 * curious and could be an intruder attempting to clog, so we
1333250003Sadrian	 * just ignore it.
1334250003Sadrian	 *
1335250003Sadrian	 * If the packet is authentic and the manycastclient or pool
1336250003Sadrian	 * association is found, we mobilize a client association and
1337250003Sadrian	 * copy pertinent variables from the manycastclient or pool
1338250003Sadrian	 * association to the new client association. If not, just
1339250003Sadrian	 * ignore the packet.
1340250003Sadrian	 *
1341250003Sadrian	 * There is an implosion hazard at the manycast client, since
1342250003Sadrian	 * the manycast servers send the server packet immediately. If
1343250003Sadrian	 * the guy is already here, don't fire up a duplicate.
1344250003Sadrian	 *
1345250003Sadrian	 * There are cases here where we do not call record_raw_stats().
1346250003Sadrian	 */
1347250003Sadrian	case AM_MANYCAST:
1348250003Sadrian
1349250003Sadrian#ifdef AUTOKEY
1350250003Sadrian		/*
1351250003Sadrian		 * Do not respond if not the same group.
1352250003Sadrian		 */
1353250003Sadrian		if (group_test(groupname, NULL)) {
1354250003Sadrian			DPRINTF(2, ("receive: AM_MANYCAST drop: empty groupname\n"));
1355250003Sadrian			sys_declined++;
1356250003Sadrian			return;
1357250003Sadrian		}
1358250003Sadrian#endif /* AUTOKEY */
1359250003Sadrian		if ((peer2 = findmanycastpeer(rbufp)) == NULL) {
1360250003Sadrian			DPRINTF(2, ("receive: AM_MANYCAST drop: No manycast peer\n"));
1361250003Sadrian			sys_restricted++;
1362250003Sadrian			return;			/* not enabled */
1363250003Sadrian		}
1364250003Sadrian		if (!AUTH(  (!(peer2->cast_flags & MDF_POOL)
1365250003Sadrian			     && sys_authenticate)
1366250003Sadrian			  || (restrict_mask & (RES_NOPEER |
1367250003Sadrian			      RES_DONTTRUST)), is_authentic)
1368250003Sadrian		    /* MC: RES_NOEPEER? */
1369250003Sadrian		   ) {
1370250003Sadrian			DPRINTF(2, ("receive: AM_MANYCAST drop: bad auth || (NOPEER|DONTTRUST)\n"));
1371250003Sadrian			sys_restricted++;
1372250003Sadrian			return;			/* access denied */
1373250003Sadrian		}
1374250003Sadrian
1375250003Sadrian		/*
1376250003Sadrian		 * Do not respond if unsynchronized or stratum is below
1377250003Sadrian		 * the floor or at or above the ceiling.
1378250003Sadrian		 */
1379250003Sadrian		if (   hisleap == LEAP_NOTINSYNC
1380250003Sadrian		    || hisstratum < sys_floor
1381250003Sadrian		    || hisstratum >= sys_ceiling) {
1382250003Sadrian			DPRINTF(2, ("receive: AM_MANYCAST drop: unsync/stratum\n"));
1383250003Sadrian			sys_declined++;
1384250003Sadrian			return;			/* no help */
1385250003Sadrian		}
1386250003Sadrian		peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1387250003Sadrian			       r4a.ippeerlimit, MODE_CLIENT, hisversion,
1388250003Sadrian			       peer2->minpoll, peer2->maxpoll,
1389250003Sadrian			       FLAG_PREEMPT | (FLAG_IBURST & peer2->flags),
1390250003Sadrian			       MDF_UCAST | MDF_UCLNT, 0, skeyid, sys_ident);
1391250003Sadrian		if (NULL == peer) {
1392250003Sadrian			DPRINTF(2, ("receive: AM_MANYCAST drop: duplicate\n"));
1393250003Sadrian			sys_declined++;
1394250003Sadrian			return;			/* ignore duplicate */
1395250003Sadrian		}
1396250003Sadrian
1397250003Sadrian		/*
1398250003Sadrian		 * After each ephemeral pool association is spun,
1399250003Sadrian		 * accelerate the next poll for the pool solicitor so
1400250003Sadrian		 * the pool will fill promptly.
1401250003Sadrian		 */
1402250003Sadrian		if (peer2->cast_flags & MDF_POOL)
1403250003Sadrian			peer2->nextdate = current_time + 1;
1404250003Sadrian
1405250003Sadrian		/*
1406250003Sadrian		 * Further processing of the solicitation response would
1407250003Sadrian		 * simply detect its origin timestamp as bogus for the
1408250003Sadrian		 * brand-new association (it matches the prototype
1409250003Sadrian		 * association) and tinker with peer->nextdate delaying
1410250003Sadrian		 * first sync.
1411250003Sadrian		 */
1412250003Sadrian		return;		/* solicitation response handled */
1413250003Sadrian
1414250003Sadrian	/*
1415250003Sadrian	 * This is the first packet received from a broadcast server. If
1416250003Sadrian	 * the packet is authentic and we are enabled as broadcast
1417250003Sadrian	 * client, mobilize a broadcast client association. We don't
1418250003Sadrian	 * kiss any frogs here.
1419250003Sadrian	 *
1420250003Sadrian	 * There are cases here where we do not call record_raw_stats().
1421250003Sadrian	 */
1422250003Sadrian	case AM_NEWBCL:
1423250003Sadrian
1424250003Sadrian#ifdef AUTOKEY
1425250003Sadrian		/*
1426250003Sadrian		 * Do not respond if not the same group.
1427250003Sadrian		 */
1428250003Sadrian		if (group_test(groupname, sys_ident)) {
1429250003Sadrian			DPRINTF(2, ("receive: AM_NEWBCL drop: groupname mismatch\n"));
1430250003Sadrian			sys_declined++;
1431250003Sadrian			return;
1432250003Sadrian		}
1433250003Sadrian#endif /* AUTOKEY */
1434250003Sadrian		if (sys_bclient == 0) {
1435250003Sadrian			DPRINTF(2, ("receive: AM_NEWBCL drop: not a bclient\n"));
1436250003Sadrian			sys_restricted++;
1437250003Sadrian			return;			/* not enabled */
1438250003Sadrian		}
1439250003Sadrian		if (!AUTH(sys_authenticate | (restrict_mask &
1440250003Sadrian			  (RES_NOPEER | RES_DONTTRUST)), is_authentic)
1441250003Sadrian		    /* NEWBCL: RES_NOEPEER? */
1442250003Sadrian		   ) {
1443250003Sadrian			DPRINTF(2, ("receive: AM_NEWBCL drop: AUTH failed\n"));
1444250003Sadrian			sys_restricted++;
1445250003Sadrian			return;			/* access denied */
1446250003Sadrian		}
1447250003Sadrian
1448250003Sadrian		/*
1449250003Sadrian		 * Do not respond if unsynchronized or stratum is below
1450250003Sadrian		 * the floor or at or above the ceiling.
1451250003Sadrian		 */
1452250003Sadrian		if (   hisleap == LEAP_NOTINSYNC
1453250003Sadrian		    || hisstratum < sys_floor
1454250003Sadrian		    || hisstratum >= sys_ceiling) {
1455250003Sadrian			DPRINTF(2, ("receive: AM_NEWBCL drop: Unsync or bad stratum\n"));
1456250003Sadrian			sys_declined++;
1457250003Sadrian			return;			/* no help */
1458250003Sadrian		}
1459250003Sadrian
1460250003Sadrian#ifdef AUTOKEY
1461250003Sadrian		/*
1462250003Sadrian		 * Do not respond if Autokey and the opcode is not a
1463250003Sadrian		 * CRYPTO_ASSOC response with association ID.
1464250003Sadrian		 */
1465250003Sadrian		if (   crypto_flags && skeyid > NTP_MAXKEY
1466250003Sadrian		    && (opcode & 0xffff0000) != (CRYPTO_ASSOC | CRYPTO_RESP)) {
1467250003Sadrian			DPRINTF(2, ("receive: AM_NEWBCL drop: Autokey but not CRYPTO_ASSOC\n"));
1468250003Sadrian			sys_declined++;
1469250003Sadrian			return;			/* protocol error */
1470250003Sadrian		}
1471250003Sadrian#endif	/* AUTOKEY */
1472250003Sadrian
1473250003Sadrian		/*
1474250003Sadrian		 * Broadcasts received via a multicast address may
1475250003Sadrian		 * arrive after a unicast volley has begun
1476250003Sadrian		 * with the same remote address.  newpeer() will not
1477250003Sadrian		 * find duplicate associations on other local endpoints
1478250003Sadrian		 * if a non-NULL endpoint is supplied.  multicastclient
1479250003Sadrian		 * ephemeral associations are unique across all local
1480250003Sadrian		 * endpoints.
1481250003Sadrian		 */
1482250003Sadrian		if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
1483250003Sadrian			match_ep = rbufp->dstadr;
1484250003Sadrian		else
1485250003Sadrian			match_ep = NULL;
1486250003Sadrian
1487250003Sadrian		/*
1488250003Sadrian		 * Determine whether to execute the initial volley.
1489250003Sadrian		 */
1490250003Sadrian		if (sys_bdelay > 0.0) {
1491250003Sadrian#ifdef AUTOKEY
1492250003Sadrian			/*
1493250003Sadrian			 * If a two-way exchange is not possible,
1494250003Sadrian			 * neither is Autokey.
1495250003Sadrian			 */
1496250003Sadrian			if (crypto_flags && skeyid > NTP_MAXKEY) {
1497250003Sadrian				sys_restricted++;
1498250003Sadrian				DPRINTF(2, ("receive: AM_NEWBCL drop: Autokey but not 2-way\n"));
1499250003Sadrian				return;		/* no autokey */
1500250003Sadrian			}
1501250003Sadrian#endif	/* AUTOKEY */
1502250003Sadrian
1503250003Sadrian			/*
1504250003Sadrian			 * Do not execute the volley. Start out in
1505250003Sadrian			 * broadcast client mode.
1506250003Sadrian			 */
1507250003Sadrian			peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1508250003Sadrian			    r4a.ippeerlimit, MODE_BCLIENT, hisversion,
1509250003Sadrian			    pkt->ppoll, pkt->ppoll,
1510250003Sadrian			    FLAG_PREEMPT, MDF_BCLNT, 0, skeyid, sys_ident);
1511250003Sadrian			if (NULL == peer) {
1512250003Sadrian				DPRINTF(2, ("receive: AM_NEWBCL drop: duplicate\n"));
1513250003Sadrian				sys_restricted++;
1514250003Sadrian				return;		/* ignore duplicate */
1515250003Sadrian
1516250003Sadrian			} else {
1517250003Sadrian				peer->delay = sys_bdelay;
1518250003Sadrian				peer->bxmt = p_xmt;
1519250003Sadrian			}
1520250003Sadrian			break;
1521250003Sadrian		}
1522250003Sadrian
1523250003Sadrian		/*
1524250003Sadrian		 * Execute the initial volley in order to calibrate the
1525250003Sadrian		 * propagation delay and run the Autokey protocol.
1526250003Sadrian		 *
1527250003Sadrian		 * Note that the minpoll is taken from the broadcast
1528250003Sadrian		 * packet, normally 6 (64 s) and that the poll interval
1529250003Sadrian		 * is fixed at this value.
1530250003Sadrian		 */
1531250003Sadrian		peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1532250003Sadrian			       r4a.ippeerlimit, MODE_CLIENT, hisversion,
1533250003Sadrian			       pkt->ppoll, pkt->ppoll,
1534250003Sadrian			       FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
1535250003Sadrian			       0, skeyid, sys_ident);
1536250003Sadrian		if (NULL == peer) {
1537250003Sadrian			DPRINTF(2, ("receive: AM_NEWBCL drop: empty newpeer() failed\n"));
1538250003Sadrian			sys_restricted++;
1539250003Sadrian			return;			/* ignore duplicate */
1540250003Sadrian		}
1541250003Sadrian		peer->bxmt = p_xmt;
1542250003Sadrian#ifdef AUTOKEY
1543250003Sadrian		if (skeyid > NTP_MAXKEY)
1544250003Sadrian			crypto_recv(peer, rbufp);
1545250003Sadrian#endif	/* AUTOKEY */
1546250003Sadrian
1547250003Sadrian		return;				/* hooray */
1548250003Sadrian
1549250003Sadrian	/*
1550250003Sadrian	 * This is the first packet received from a potential ephemeral
1551250003Sadrian	 * symmetric active peer.  First, deal with broken Windows clients.
1552250003Sadrian	 * Then, if NOEPEER is enabled, drop it.  If the packet meets our
1553250003Sadrian	 * authenticty requirements and is the first he sent, mobilize
1554250003Sadrian	 * a passive association.
1555250003Sadrian	 * Otherwise, kiss the frog.
1556250003Sadrian	 *
1557250003Sadrian	 * There are cases here where we do not call record_raw_stats().
1558250003Sadrian	 */
1559250003Sadrian	case AM_NEWPASS:
1560250003Sadrian
1561250003Sadrian		DEBUG_REQUIRE(MODE_ACTIVE == hismode);
1562250003Sadrian
1563250003Sadrian#ifdef AUTOKEY
1564250003Sadrian		/*
1565250003Sadrian		 * Do not respond if not the same group.
1566250003Sadrian		 */
1567250003Sadrian		if (group_test(groupname, sys_ident)) {
1568250003Sadrian			DPRINTF(2, ("receive: AM_NEWPASS drop: Autokey group mismatch\n"));
1569250003Sadrian			sys_declined++;
1570250003Sadrian			return;
1571250003Sadrian		}
1572250003Sadrian#endif /* AUTOKEY */
1573250003Sadrian		if (!AUTH(sys_authenticate | (restrict_mask &
1574250003Sadrian			  (RES_NOPEER | RES_DONTTRUST)), is_authentic)
1575250003Sadrian		   ) {
1576250003Sadrian			/*
1577250003Sadrian			 * If authenticated but cannot mobilize an
1578250003Sadrian			 * association, send a symmetric passive
1579250003Sadrian			 * response without mobilizing an association.
1580250003Sadrian			 * This is for drat broken Windows clients. See
1581250003Sadrian			 * Microsoft KB 875424 for preferred workaround.
1582250003Sadrian			 */
1583250003Sadrian			if (AUTH(restrict_mask & RES_DONTTRUST,
1584250003Sadrian				 is_authentic)) {
1585250003Sadrian				fast_xmit(rbufp, MODE_PASSIVE, skeyid,
1586250003Sadrian				    restrict_mask);
1587250003Sadrian				return;			/* hooray */
1588250003Sadrian			}
1589250003Sadrian			/* HMS: Why is this next set of lines a feature? */
1590250003Sadrian			if (is_authentic == AUTH_ERROR) {
1591250003Sadrian				fast_xmit(rbufp, MODE_PASSIVE, 0,
1592250003Sadrian				    restrict_mask);
1593250003Sadrian				sys_restricted++;
1594250003Sadrian				return;
1595250003Sadrian			}
1596250003Sadrian
1597250003Sadrian			if (restrict_mask & RES_NOEPEER) {
1598250003Sadrian				DPRINTF(2, ("receive: AM_NEWPASS drop: NOEPEER\n"));
1599250003Sadrian				sys_declined++;
1600250003Sadrian				return;
1601250003Sadrian			}
1602250003Sadrian
1603250003Sadrian			/* [Bug 2941]
1604250003Sadrian			 * If we got here, the packet isn't part of an
1605250003Sadrian			 * existing association, either isn't correctly
1606250003Sadrian			 * authenticated or it is but we are refusing
1607250003Sadrian			 * ephemeral peer requests, and it didn't meet
1608250003Sadrian			 * either of the previous two special cases so we
1609250003Sadrian			 * should just drop it on the floor.  For example,
1610250003Sadrian			 * crypto-NAKs (is_authentic == AUTH_CRYPTO)
1611250003Sadrian			 * will make it this far.  This is just
1612250003Sadrian			 * debug-printed and not logged to avoid log
1613250003Sadrian			 * flooding.
1614250003Sadrian			 */
1615250003Sadrian			DPRINTF(2, ("receive: at %ld refusing to mobilize passive association"
1616250003Sadrian				    " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1617250003Sadrian				    current_time, stoa(&rbufp->recv_srcadr),
1618250003Sadrian				    hismode, hm_str, am_str, skeyid,
1619250003Sadrian				    (authlen + has_mac), is_authentic));
1620250003Sadrian			sys_declined++;
1621250003Sadrian			return;
1622250003Sadrian		}
1623250003Sadrian
1624250003Sadrian		if (restrict_mask & RES_NOEPEER) {
1625250003Sadrian			DPRINTF(2, ("receive: AM_NEWPASS drop: NOEPEER\n"));
1626250003Sadrian			sys_declined++;
1627250003Sadrian			return;
1628250003Sadrian		}
1629250003Sadrian
1630250003Sadrian		/*
1631250003Sadrian		 * Do not respond if synchronized and if stratum is
1632250003Sadrian		 * below the floor or at or above the ceiling. Note,
1633250003Sadrian		 * this allows an unsynchronized peer to synchronize to
1634250003Sadrian		 * us. It would be very strange if he did and then was
1635250003Sadrian		 * nipped, but that could only happen if we were
1636250003Sadrian		 * operating at the top end of the range.  It also means
1637250003Sadrian		 * we will spin an ephemeral association in response to
1638250003Sadrian		 * MODE_ACTIVE KoDs, which will time out eventually.
1639250003Sadrian		 */
1640250003Sadrian		if (   hisleap != LEAP_NOTINSYNC
1641250003Sadrian		    && (hisstratum < sys_floor || hisstratum >= sys_ceiling)) {
1642250003Sadrian			DPRINTF(2, ("receive: AM_NEWPASS drop: Autokey group mismatch\n"));
1643250003Sadrian			sys_declined++;
1644250003Sadrian			return;			/* no help */
1645250003Sadrian		}
1646250003Sadrian
1647250003Sadrian		/*
1648250003Sadrian		 * The message is correctly authenticated and allowed.
1649250003Sadrian		 * Mobilize a symmetric passive association, if we won't
1650250003Sadrian		 * exceed the ippeerlimit.
1651250003Sadrian		 */
1652250003Sadrian		if ((peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1653250003Sadrian				    r4a.ippeerlimit, MODE_PASSIVE, hisversion,
1654250003Sadrian				    pkt->ppoll, NTP_MAXDPOLL, 0, MDF_UCAST, 0,
1655250003Sadrian				    skeyid, sys_ident)) == NULL) {
1656250003Sadrian			DPRINTF(2, ("receive: AM_NEWPASS drop: newpeer() failed\n"));
1657250003Sadrian			sys_declined++;
1658250003Sadrian			return;			/* ignore duplicate */
1659250003Sadrian		}
1660250003Sadrian		break;
1661250003Sadrian
1662250003Sadrian
1663250003Sadrian	/*
1664250003Sadrian	 * Process regular packet. Nothing special.
1665250003Sadrian	 *
1666250003Sadrian	 * There are cases here where we do not call record_raw_stats().
1667250003Sadrian	 */
1668250003Sadrian	case AM_PROCPKT:
1669250003Sadrian
1670250003Sadrian#ifdef AUTOKEY
1671250003Sadrian		/*
1672250003Sadrian		 * Do not respond if not the same group.
1673250003Sadrian		 */
1674250003Sadrian		if (group_test(groupname, peer->ident)) {
1675250003Sadrian			DPRINTF(2, ("receive: AM_PROCPKT drop: Autokey group mismatch\n"));
1676250003Sadrian			sys_declined++;
1677250003Sadrian			return;
1678250003Sadrian		}
1679250003Sadrian#endif /* AUTOKEY */
1680250003Sadrian
1681250003Sadrian		if (MODE_BROADCAST == hismode) {
1682250003Sadrian			int	bail = 0;
1683250003Sadrian			l_fp	tdiff;
1684250003Sadrian			u_long	deadband;
1685250003Sadrian
1686250003Sadrian			DPRINTF(2, ("receive: PROCPKT/BROADCAST: prev pkt %ld seconds ago, ppoll: %d, %d secs\n",
1687250003Sadrian				    (current_time - peer->timelastrec),
1688250003Sadrian				    peer->ppoll, (1 << peer->ppoll)
1689250003Sadrian				    ));
1690250003Sadrian			/* Things we can check:
1691250003Sadrian			 *
1692250003Sadrian			 * Did the poll interval change?
1693250003Sadrian			 * Is the poll interval in the packet in-range?
1694250003Sadrian			 * Did this packet arrive too soon?
1695250003Sadrian			 * Is the timestamp in this packet monotonic
1696250003Sadrian			 *  with respect to the previous packet?
1697250003Sadrian			 */
1698250003Sadrian
1699250003Sadrian			/* This is noteworthy, not error-worthy */
1700250003Sadrian			if (pkt->ppoll != peer->ppoll) {
1701250003Sadrian				msyslog(LOG_INFO, "receive: broadcast poll from %s changed from %u to %u",
1702250003Sadrian					stoa(&rbufp->recv_srcadr),
1703250003Sadrian					peer->ppoll, pkt->ppoll);
1704250003Sadrian			}
1705250003Sadrian
1706250003Sadrian			/* This is error-worthy */
1707250003Sadrian			if (   pkt->ppoll < peer->minpoll
1708250003Sadrian			    || pkt->ppoll > peer->maxpoll) {
1709250003Sadrian				msyslog(LOG_INFO, "receive: broadcast poll of %u from %s is out-of-range (%d to %d)!",
1710250003Sadrian					pkt->ppoll, stoa(&rbufp->recv_srcadr),
1711250003Sadrian					peer->minpoll, peer->maxpoll);
1712250003Sadrian				++bail;
1713250003Sadrian			}
1714250003Sadrian
1715250003Sadrian			/* too early? worth an error, too!
1716250003Sadrian			 *
1717250003Sadrian			 * [Bug 3113] Ensure that at least one poll
1718250003Sadrian			 * interval has elapsed since the last **clean**
1719250003Sadrian			 * packet was received.  We limit the check to
1720250003Sadrian			 * **clean** packets to prevent replayed packets
1721250003Sadrian			 * and incorrectly authenticated packets, which
1722250003Sadrian			 * we'll discard, from being used to create a
1723250003Sadrian			 * denial of service condition.
1724250003Sadrian			 */
1725250003Sadrian			deadband = (1u << pkt->ppoll);
1726250003Sadrian			if (FLAG_BC_VOL & peer->flags)
1727250003Sadrian				deadband -= 3;	/* allow greater fuzz after volley */
1728250003Sadrian			if ((current_time - peer->timereceived) < deadband) {
1729250003Sadrian				msyslog(LOG_INFO, "receive: broadcast packet from %s arrived after %lu, not %lu seconds!",
1730250003Sadrian					stoa(&rbufp->recv_srcadr),
1731250003Sadrian					(current_time - peer->timereceived),
1732250003Sadrian					deadband);
1733250003Sadrian				++bail;
1734250003Sadrian			}
1735250003Sadrian
1736250003Sadrian			/* Alert if time from the server is non-monotonic.
1737250003Sadrian			 *
1738250003Sadrian			 * [Bug 3114] is about Broadcast mode replay DoS.
1739250003Sadrian			 *
1740250003Sadrian			 * Broadcast mode *assumes* a trusted network.
1741250003Sadrian			 * Even so, it's nice to be robust in the face
1742250003Sadrian			 * of attacks.
1743250003Sadrian			 *
1744250003Sadrian			 * If we get an authenticated broadcast packet
1745250003Sadrian			 * with an "earlier" timestamp, it means one of
1746250003Sadrian			 * two things:
1747250003Sadrian			 *
1748250003Sadrian			 * - the broadcast server had a backward step.
1749250003Sadrian			 *
1750250003Sadrian			 * - somebody is trying a replay attack.
1751250003Sadrian			 *
1752250003Sadrian			 * deadband: By default, we assume the broadcast
1753250003Sadrian			 * network is trustable, so we take our accepted
1754250003Sadrian			 * broadcast packets as we receive them.  But
1755250003Sadrian			 * some folks might want to take additional poll
1756250003Sadrian			 * delays before believing a backward step.
1757250003Sadrian			 */
1758250003Sadrian			if (sys_bcpollbstep) {
1759250003Sadrian				/* pkt->ppoll or peer->ppoll ? */
1760250003Sadrian				deadband = (1u << pkt->ppoll)
1761250003Sadrian					   * sys_bcpollbstep + 2;
1762250003Sadrian			} else {
1763250003Sadrian				deadband = 0;
1764250003Sadrian			}
1765250003Sadrian
1766250003Sadrian			if (L_ISZERO(&peer->bxmt)) {
1767250003Sadrian				tdiff.l_ui = tdiff.l_uf = 0;
1768250003Sadrian			} else {
1769250003Sadrian				tdiff = p_xmt;
1770250003Sadrian				L_SUB(&tdiff, &peer->bxmt);
1771250003Sadrian			}
1772250003Sadrian			if (   tdiff.l_i < 0
1773250003Sadrian			    && (current_time - peer->timereceived) < deadband)
1774250003Sadrian			{
1775250003Sadrian				msyslog(LOG_INFO, "receive: broadcast packet from %s contains non-monotonic timestamp: %#010x.%08x -> %#010x.%08x",
1776250003Sadrian					stoa(&rbufp->recv_srcadr),
1777250003Sadrian					peer->bxmt.l_ui, peer->bxmt.l_uf,
1778250003Sadrian					p_xmt.l_ui, p_xmt.l_uf
1779250003Sadrian					);
1780250003Sadrian				++bail;
1781250003Sadrian			}
1782250003Sadrian
1783250003Sadrian			if (bail) {
1784250003Sadrian				DPRINTF(2, ("receive: AM_PROCPKT drop: bail\n"));
1785250003Sadrian				peer->timelastrec = current_time;
1786250003Sadrian				sys_declined++;
1787250003Sadrian				return;
1788250003Sadrian			}
1789250003Sadrian		}
1790250003Sadrian
1791250003Sadrian		break;
1792250003Sadrian
1793250003Sadrian	/*
1794250003Sadrian	 * A passive packet matches a passive association. This is
1795250003Sadrian	 * usually the result of reconfiguring a client on the fly. As
1796250003Sadrian	 * this association might be legitimate and this packet an
1797250003Sadrian	 * attempt to deny service, just ignore it.
1798250003Sadrian	 */
1799250003Sadrian	case AM_ERR:
1800250003Sadrian		DPRINTF(2, ("receive: AM_ERR drop.\n"));
1801250003Sadrian		sys_declined++;
1802250003Sadrian		return;
1803250003Sadrian
1804250003Sadrian	/*
1805250003Sadrian	 * For everything else there is the bit bucket.
1806250003Sadrian	 */
1807250003Sadrian	default:
1808250003Sadrian		DPRINTF(2, ("receive: default drop.\n"));
1809250003Sadrian		sys_declined++;
1810250003Sadrian		return;
1811250003Sadrian	}
1812250003Sadrian
1813250003Sadrian#ifdef AUTOKEY
1814250003Sadrian	/*
1815250003Sadrian	 * If the association is configured for Autokey, the packet must
1816250003Sadrian	 * have a public key ID; if not, the packet must have a
1817250003Sadrian	 * symmetric key ID.
1818250003Sadrian	 */
1819250003Sadrian	if (   is_authentic != AUTH_CRYPTO
1820250003Sadrian	    && (   ((peer->flags & FLAG_SKEY) && skeyid <= NTP_MAXKEY)
1821250003Sadrian	        || (!(peer->flags & FLAG_SKEY) && skeyid > NTP_MAXKEY))) {
1822250003Sadrian		DPRINTF(2, ("receive: drop: Autokey but wrong/bad auth\n"));
1823250003Sadrian		sys_badauth++;
1824250003Sadrian		return;
1825250003Sadrian	}
1826250003Sadrian#endif	/* AUTOKEY */
1827250003Sadrian
1828250003Sadrian	peer->received++;
1829250003Sadrian	peer->flash &= ~PKT_TEST_MASK;
1830250003Sadrian	if (peer->flags & FLAG_XBOGUS) {
1831250003Sadrian		peer->flags &= ~FLAG_XBOGUS;
1832250003Sadrian		peer->flash |= TEST3;
1833250003Sadrian	}
1834250003Sadrian
1835250003Sadrian	/*
1836250003Sadrian	 * Next comes a rigorous schedule of timestamp checking. If the
1837250003Sadrian	 * transmit timestamp is zero, the server has not initialized in
1838250003Sadrian	 * interleaved modes or is horribly broken.
1839250003Sadrian	 *
1840250003Sadrian	 * A KoD packet we pay attention to cannot have a 0 transmit
1841250003Sadrian	 * timestamp.
1842250003Sadrian	 */
1843250003Sadrian
1844250003Sadrian	kissCode = kiss_code_check(hisleap, hisstratum, hismode, pkt->refid);
1845250003Sadrian
1846250003Sadrian	if (L_ISZERO(&p_xmt)) {
1847250003Sadrian		peer->flash |= TEST3;			/* unsynch */
1848250003Sadrian		if (kissCode != NOKISS) {		/* KoD packet */
1849250003Sadrian			peer->bogusorg++;		/* for TEST2 or TEST3 */
1850250003Sadrian			msyslog(LOG_INFO,
1851250003Sadrian				"receive: Unexpected zero transmit timestamp in KoD from %s",
1852250003Sadrian				ntoa(&peer->srcadr));
1853250003Sadrian			return;
1854250003Sadrian		}
1855250003Sadrian
1856250008Sadrian	/*
1857250008Sadrian	 * If the transmit timestamp duplicates our previous one, the
1858250003Sadrian	 * packet is a replay. This prevents the bad guys from replaying
1859250003Sadrian	 * the most recent packet, authenticated or not.
1860250008Sadrian	 */
1861250008Sadrian	} else if (L_ISEQU(&peer->xmt, &p_xmt)) {
1862250008Sadrian		DPRINTF(2, ("receive: drop: Duplicate xmit\n"));
1863250003Sadrian		peer->flash |= TEST1;			/* duplicate */
1864250003Sadrian		peer->oldpkt++;
1865250003Sadrian		return;
1866250003Sadrian
1867250003Sadrian	/*
1868250003Sadrian	 * If this is a broadcast mode packet, make sure hisstratum
1869250008Sadrian	 * is appropriate.  Don't do anything else here - we wait to
1870250003Sadrian	 * see if this is an interleave broadcast packet until after
1871250003Sadrian	 * we've validated the MAC that SHOULD be provided.
1872250003Sadrian	 *
1873250003Sadrian	 * hisstratum cannot be 0 - see assertion above.
1874250003Sadrian	 * If hisstratum is 15, then we'll advertise as UNSPEC but
1875250003Sadrian	 * at least we'll be able to sync with the broadcast server.
1876250003Sadrian	 */
1877250003Sadrian	} else if (hismode == MODE_BROADCAST) {
1878250003Sadrian		/* 0 is unexpected too, and impossible */
1879250003Sadrian		if (STRATUM_UNSPEC <= hisstratum) {
1880250003Sadrian			/* Is this a ++sys_declined or ??? */
1881250003Sadrian			msyslog(LOG_INFO,
1882250003Sadrian				"receive: Unexpected stratum (%d) in broadcast from %s",
1883250003Sadrian				hisstratum, ntoa(&peer->srcadr));
1884250003Sadrian			return;
1885250003Sadrian		}
1886250003Sadrian
1887250003Sadrian	/*
1888250003Sadrian	 * Basic KoD validation checking:
1889250003Sadrian	 *
1890250003Sadrian	 * KoD packets are a mixed-blessing.  Forged KoD packets
1891250003Sadrian	 * are DoS attacks.  There are rare situations where we might
1892250003Sadrian	 * get a valid KoD response, though.  Since KoD packets are
1893250003Sadrian	 * a special case that complicate the checks we do next, we
1894250003Sadrian	 * handle the basic KoD checks here.
1895250003Sadrian	 *
1896250003Sadrian	 * Note that we expect the incoming KoD packet to have its
1897250003Sadrian	 * (nonzero) org, rec, and xmt timestamps set to the xmt timestamp
1898250003Sadrian	 * that we have previously sent out.  Watch interleave mode.
1899250003Sadrian	 */
1900250003Sadrian	} else if (kissCode != NOKISS) {
1901250003Sadrian		DEBUG_INSIST(!L_ISZERO(&p_xmt));
1902250003Sadrian		if (   L_ISZERO(&p_org)		/* We checked p_xmt above */
1903250003Sadrian		    || L_ISZERO(&p_rec)) {
1904250003Sadrian			peer->bogusorg++;
1905250003Sadrian			msyslog(LOG_INFO,
1906250003Sadrian				"receive: KoD packet from %s has a zero org or rec timestamp.  Ignoring.",
1907250003Sadrian				ntoa(&peer->srcadr));
1908250003Sadrian			return;
1909250003Sadrian		}
1910250003Sadrian
1911250003Sadrian		if (   !L_ISEQU(&p_xmt, &p_org)
1912250003Sadrian		    || !L_ISEQU(&p_xmt, &p_rec)) {
1913250003Sadrian			peer->bogusorg++;
1914250003Sadrian			msyslog(LOG_INFO,
1915250003Sadrian				"receive: KoD packet from %s has inconsistent xmt/org/rec timestamps.  Ignoring.",
1916250003Sadrian				ntoa(&peer->srcadr));
1917250003Sadrian			return;
1918250003Sadrian		}
1919250003Sadrian
1920250003Sadrian		/* Be conservative */
1921250003Sadrian		if (peer->flip == 0 && !L_ISEQU(&p_org, &peer->aorg)) {
1922250003Sadrian			peer->bogusorg++;
1923250003Sadrian			msyslog(LOG_INFO,
1924250003Sadrian				"receive: flip 0 KoD origin timestamp %#010x.%08x from %s does not match %#010x.%08x - ignoring.",
1925250003Sadrian				p_org.l_ui, p_org.l_uf,
1926250003Sadrian				ntoa(&peer->srcadr),
1927250003Sadrian				peer->aorg.l_ui, peer->aorg.l_uf);
1928250003Sadrian			return;
1929250003Sadrian		} else if (peer->flip == 1 && !L_ISEQU(&p_org, &peer->borg)) {
1930250003Sadrian			peer->bogusorg++;
1931250003Sadrian			msyslog(LOG_INFO,
1932250003Sadrian				"receive: flip 1 KoD origin timestamp %#010x.%08x from %s does not match interleave %#010x.%08x - ignoring.",
1933250003Sadrian				p_org.l_ui, p_org.l_uf,
1934250003Sadrian				ntoa(&peer->srcadr),
1935250003Sadrian				peer->borg.l_ui, peer->borg.l_uf);
1936250003Sadrian			return;
1937250003Sadrian		}
1938250003Sadrian
1939250003Sadrian	/*
1940250003Sadrian	 * Basic mode checks:
1941250003Sadrian	 *
1942250003Sadrian	 * If there is no origin timestamp, it's either an initial packet
1943250003Sadrian	 * or we've already received a response to our query.  Of course,
1944250003Sadrian	 * should 'aorg' be all-zero because this really was the original
1945250003Sadrian	 * transmit timestamp, we'll ignore this reply.  There is a window
1946250003Sadrian	 * of one nanosecond once every 136 years' time where this is
1947250003Sadrian	 * possible.  We currently ignore this situation, as a completely
1948250003Sadrian	 * zero timestamp is (quietly?) disallowed.
1949250003Sadrian	 *
1950250003Sadrian	 * Otherwise, check for bogus packet in basic mode.
1951250003Sadrian	 * If it is bogus, switch to interleaved mode and resynchronize,
1952250003Sadrian	 * but only after confirming the packet is not bogus in
1953250003Sadrian	 * symmetric interleaved mode.
1954250003Sadrian	 *
1955250003Sadrian	 * This could also mean somebody is forging packets claiming to
1956250003Sadrian	 * be from us, attempting to cause our server to KoD us.
1957250003Sadrian	 *
1958250003Sadrian	 * We have earlier asserted that hisstratum cannot be 0.
1959250003Sadrian	 * If hisstratum is STRATUM_UNSPEC, it means he's not sync'd.
1960250003Sadrian	 */
1961250003Sadrian	} else if (peer->flip == 0) {
1962250003Sadrian		if (0) {
1963250003Sadrian		} else if (L_ISZERO(&p_org)) {
1964250003Sadrian			const char *action;
1965250003Sadrian
1966250003Sadrian#ifdef BUG3361
1967250003Sadrian			msyslog(LOG_INFO,
1968250003Sadrian				"receive: BUG 3361: Clearing peer->aorg ");
1969250003Sadrian			L_CLR(&peer->aorg);
1970250003Sadrian#endif
1971250003Sadrian			/**/
1972250003Sadrian			switch (hismode) {
1973250003Sadrian			/* We allow 0org for: */
1974250003Sadrian			    case UCHAR_MAX:
1975250003Sadrian				action = "Allow";
1976250003Sadrian				break;
1977250003Sadrian			/* We disallow 0org for: */
1978250003Sadrian			    case MODE_UNSPEC:
1979250003Sadrian			    case MODE_ACTIVE:
1980250003Sadrian			    case MODE_PASSIVE:
1981250003Sadrian			    case MODE_CLIENT:
1982250003Sadrian			    case MODE_SERVER:
1983250003Sadrian			    case MODE_BROADCAST:
1984250003Sadrian				action = "Drop";
1985250003Sadrian				peer->bogusorg++;
1986250003Sadrian				peer->flash |= TEST2;	/* bogus */
1987250003Sadrian				break;
1988250003Sadrian			    default:
1989250003Sadrian				action = "";	/* for cranky compilers / MSVC */
1990250003Sadrian				INSIST(!"receive(): impossible hismode");
1991250003Sadrian				break;
1992250003Sadrian			}
1993250003Sadrian			/**/
1994250003Sadrian			msyslog(LOG_INFO,
1995250003Sadrian				"receive: %s 0 origin timestamp from %s@%s xmt %#010x.%08x",
1996250003Sadrian				action, hm_str, ntoa(&peer->srcadr),
1997250003Sadrian				ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
1998250003Sadrian		} else if (!L_ISEQU(&p_org, &peer->aorg)) {
1999250003Sadrian			/* are there cases here where we should bail? */
2000250003Sadrian			/* Should we set TEST2 if we decide to try xleave? */
2001250003Sadrian			peer->bogusorg++;
2002250003Sadrian			peer->flash |= TEST2;	/* bogus */
2003250003Sadrian			msyslog(LOG_INFO,
2004250003Sadrian				"receive: Unexpected origin timestamp %#010x.%08x does not match aorg %#010x.%08x from %s@%s xmt %#010x.%08x",
2005250003Sadrian				ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
2006250003Sadrian				peer->aorg.l_ui, peer->aorg.l_uf,
2007250003Sadrian				hm_str, ntoa(&peer->srcadr),
2008250003Sadrian				ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
2009250003Sadrian			if (  !L_ISZERO(&peer->dst)
2010250003Sadrian			    && L_ISEQU(&p_org, &peer->dst)) {
2011250003Sadrian				/* Might be the start of an interleave */
2012250003Sadrian				if (dynamic_interleave) {
2013250003Sadrian					peer->flip = 1;
2014250003Sadrian					report_event(PEVNT_XLEAVE, peer, NULL);
2015250003Sadrian				} else {
2016250003Sadrian					msyslog(LOG_INFO,
2017250003Sadrian						"receive: Dynamic interleave from %s@%s denied",
2018250003Sadrian						hm_str, ntoa(&peer->srcadr));
2019250003Sadrian				}
2020250003Sadrian			}
2021250003Sadrian		} else {
2022250003Sadrian			L_CLR(&peer->aorg);
2023250003Sadrian		}
2024250003Sadrian
2025250003Sadrian	/*
2026250003Sadrian	 * Check for valid nonzero timestamp fields.
2027250003Sadrian	 */
2028250003Sadrian	} else if (   L_ISZERO(&p_org)
2029250003Sadrian		   || L_ISZERO(&p_rec)
2030250003Sadrian		   || L_ISZERO(&peer->dst)) {
2031250003Sadrian		peer->flash |= TEST3;		/* unsynch */
2032250003Sadrian
2033250003Sadrian	/*
2034250003Sadrian	 * Check for bogus packet in interleaved symmetric mode. This
2035250003Sadrian	 * can happen if a packet is lost, duplicated or crossed. If
2036250003Sadrian	 * found, flip and resynchronize.
2037250003Sadrian	 */
2038250003Sadrian	} else if (   !L_ISZERO(&peer->dst)
2039250003Sadrian		   && !L_ISEQU(&p_org, &peer->dst)) {
2040250003Sadrian		DPRINTF(2, ("receive: drop: Bogus packet in interleaved symmetric mode\n"));
2041250003Sadrian		peer->bogusorg++;
2042250003Sadrian		peer->flags |= FLAG_XBOGUS;
2043250003Sadrian		peer->flash |= TEST2;		/* bogus */
2044250003Sadrian#ifdef BUG3453
2045250003Sadrian		return; /* Bogus packet, we are done */
2046250003Sadrian#endif
2047250003Sadrian	}
2048250003Sadrian
2049250003Sadrian	/**/
2050250003Sadrian
2051250003Sadrian	/*
2052250003Sadrian	 * If this is a crypto_NAK, the server cannot authenticate a
2053250003Sadrian	 * client packet. The server might have just changed keys. Clear
2054250003Sadrian	 * the association and restart the protocol.
2055250003Sadrian	 */
2056250003Sadrian	if (crypto_nak_test == VALIDNAK) {
2057250003Sadrian		report_event(PEVNT_AUTH, peer, "crypto_NAK");
2058250003Sadrian		peer->flash |= TEST5;		/* bad auth */
2059250003Sadrian		peer->badauth++;
2060250003Sadrian		if (peer->flags & FLAG_PREEMPT) {
2061250003Sadrian			if (unpeer_crypto_nak_early) {
2062250003Sadrian				unpeer(peer);
2063250003Sadrian			}
2064250003Sadrian			DPRINTF(2, ("receive: drop: PREEMPT crypto_NAK\n"));
2065250003Sadrian			return;
2066250003Sadrian		}
2067250003Sadrian#ifdef AUTOKEY
2068250003Sadrian		if (peer->crypto) {
2069250003Sadrian			peer_clear(peer, "AUTH");
2070250003Sadrian		}
2071250003Sadrian#endif	/* AUTOKEY */
2072250003Sadrian		DPRINTF(2, ("receive: drop: crypto_NAK\n"));
2073250003Sadrian		return;
2074250003Sadrian
2075250003Sadrian	/*
2076250003Sadrian	 * If the digest fails or it's missing for authenticated
2077250003Sadrian	 * associations, the client cannot authenticate a server
2078250003Sadrian	 * reply to a client packet previously sent. The loopback check
2079250003Sadrian	 * is designed to avoid a bait-and-switch attack, which was
2080250003Sadrian	 * possible in past versions. If symmetric modes, return a
2081250003Sadrian	 * crypto-NAK. The peer should restart the protocol.
2082250003Sadrian	 */
2083250003Sadrian	} else if (!AUTH(peer->keyid || has_mac ||
2084250003Sadrian			 (restrict_mask & RES_DONTTRUST), is_authentic)) {
2085250003Sadrian
2086250003Sadrian		if (peer->flash & PKT_TEST_MASK) {
2087250003Sadrian			msyslog(LOG_INFO,
2088250003Sadrian				"receive: Bad auth in packet with bad timestamps from %s denied - spoof?",
2089250003Sadrian				ntoa(&peer->srcadr));
2090250003Sadrian			return;
2091250003Sadrian		}
2092250003Sadrian
2093250003Sadrian		report_event(PEVNT_AUTH, peer, "digest");
2094250003Sadrian		peer->flash |= TEST5;		/* bad auth */
2095250003Sadrian		peer->badauth++;
2096250003Sadrian		if (   has_mac
2097250003Sadrian		    && (   hismode == MODE_ACTIVE
2098250003Sadrian			|| hismode == MODE_PASSIVE))
2099250003Sadrian			fast_xmit(rbufp, MODE_ACTIVE, 0, restrict_mask);
2100250003Sadrian		if (peer->flags & FLAG_PREEMPT) {
2101250003Sadrian			if (unpeer_digest_early) {
2102250003Sadrian				unpeer(peer);
2103250003Sadrian			}
2104250003Sadrian		}
2105250003Sadrian#ifdef AUTOKEY
2106250003Sadrian		else if (peer_clear_digest_early && peer->crypto) {
2107250003Sadrian			peer_clear(peer, "AUTH");
2108250003Sadrian		}
2109250003Sadrian#endif	/* AUTOKEY */
2110250003Sadrian		DPRINTF(2, ("receive: drop: Bad or missing AUTH\n"));
2111250003Sadrian		return;
2112250003Sadrian	}
2113250003Sadrian
2114250003Sadrian	/*
2115250003Sadrian	 * For broadcast packets:
2116250003Sadrian	 *
2117250003Sadrian	 * HMS: This next line never made much sense to me, even
2118250003Sadrian	 * when it was up higher:
2119250003Sadrian	 *   If an initial volley, bail out now and let the
2120250003Sadrian	 *   client do its stuff.
2121250003Sadrian	 *
2122250003Sadrian	 * If the packet has not failed authentication, then
2123250003Sadrian	 * - if the origin timestamp is nonzero this is an
2124250003Sadrian	 *   interleaved broadcast, so restart the protocol.
2125250003Sadrian	 * - else, this is not an interleaved broadcast packet.
2126250003Sadrian	 */
2127250003Sadrian	if (hismode == MODE_BROADCAST) {
2128250003Sadrian		if (   is_authentic == AUTH_OK
2129250003Sadrian		    || is_authentic == AUTH_NONE) {
2130250003Sadrian			if (!L_ISZERO(&p_org)) {
2131250003Sadrian				if (!(peer->flags & FLAG_XB)) {
2132250003Sadrian					msyslog(LOG_INFO,
2133250003Sadrian						"receive: Broadcast server at %s is in interleave mode",
2134250003Sadrian						ntoa(&peer->srcadr));
2135250003Sadrian					peer->flags |= FLAG_XB;
2136250003Sadrian					peer->aorg = p_xmt;
2137250003Sadrian					peer->borg = rbufp->recv_time;
2138250003Sadrian					report_event(PEVNT_XLEAVE, peer, NULL);
2139250003Sadrian					return;
2140250003Sadrian				}
2141250003Sadrian			} else if (peer->flags & FLAG_XB) {
2142250003Sadrian				msyslog(LOG_INFO,
2143250003Sadrian					"receive: Broadcast server at %s is no longer in interleave mode",
2144250003Sadrian					ntoa(&peer->srcadr));
2145250003Sadrian				peer->flags &= ~FLAG_XB;
2146250003Sadrian			}
2147250003Sadrian		} else {
2148250003Sadrian			msyslog(LOG_INFO,
2149250003Sadrian				"receive: Bad broadcast auth (%d) from %s",
2150250003Sadrian				is_authentic, ntoa(&peer->srcadr));
2151250003Sadrian		}
2152250003Sadrian
2153250003Sadrian		/*
2154250003Sadrian		 * Now that we know the packet is correctly authenticated,
2155250003Sadrian		 * update peer->bxmt.
2156250003Sadrian		 */
2157250003Sadrian		peer->bxmt = p_xmt;
2158250003Sadrian	}
2159250003Sadrian
2160250003Sadrian
2161250003Sadrian	/*
2162250003Sadrian	** Update the state variables.
2163250003Sadrian	*/
2164250003Sadrian	if (peer->flip == 0) {
2165250003Sadrian		if (hismode != MODE_BROADCAST)
2166250003Sadrian			peer->rec = p_xmt;
2167250003Sadrian		peer->dst = rbufp->recv_time;
2168250003Sadrian	}
2169250003Sadrian	peer->xmt = p_xmt;
2170250003Sadrian
2171250003Sadrian	/*
2172250003Sadrian	 * Set the peer ppoll to the maximum of the packet ppoll and the
2173250003Sadrian	 * peer minpoll. If a kiss-o'-death, set the peer minpoll to
2174250003Sadrian	 * this maximum and advance the headway to give the sender some
2175250003Sadrian	 * headroom. Very intricate.
2176250003Sadrian	 */
2177250003Sadrian
2178250003Sadrian	/*
2179250003Sadrian	 * Check for any kiss codes. Note this is only used when a server
2180250003Sadrian	 * responds to a packet request.
2181250003Sadrian	 */
2182250003Sadrian
2183250003Sadrian	/*
2184250003Sadrian	 * Check to see if this is a RATE Kiss Code
2185250003Sadrian	 * Currently this kiss code will accept whatever poll
2186250003Sadrian	 * rate that the server sends
2187250003Sadrian	 */
2188250003Sadrian	peer->ppoll = max(peer->minpoll, pkt->ppoll);
2189250003Sadrian	if (kissCode == RATEKISS) {
2190250003Sadrian		peer->selbroken++;	/* Increment the KoD count */
2191250008Sadrian		report_event(PEVNT_RATE, peer, NULL);
2192250008Sadrian		if (pkt->ppoll > peer->minpoll)
2193250003Sadrian			peer->minpoll = peer->ppoll;
2194250003Sadrian		peer->burst = peer->retry = 0;
2195250003Sadrian		peer->throttle = (NTP_SHIFT + 1) * (1 << peer->minpoll);
2196250003Sadrian		poll_update(peer, pkt->ppoll);
2197250003Sadrian		return;				/* kiss-o'-death */
2198250003Sadrian	}
2199250003Sadrian	if (kissCode != NOKISS) {
2200250003Sadrian		peer->selbroken++;	/* Increment the KoD count */
2201250003Sadrian		return;		/* Drop any other kiss code packets */
2202250003Sadrian	}
2203250003Sadrian
2204250003Sadrian
2205250003Sadrian	/*
2206250003Sadrian	 * XXX
2207250003Sadrian	 */
2208250003Sadrian
2209250003Sadrian
2210250003Sadrian	/*
2211250003Sadrian	 * If:
2212250003Sadrian	 *	- this is a *cast (uni-, broad-, or m-) server packet
2213250003Sadrian	 *	- and it's symmetric-key authenticated
2214250003Sadrian	 * then see if the sender's IP is trusted for this keyid.
2215250003Sadrian	 * If it is, great - nothing special to do here.
2216250003Sadrian	 * Otherwise, we should report and bail.
2217250003Sadrian	 *
2218250003Sadrian	 * Autokey-authenticated packets are accepted.
2219250003Sadrian	 */
2220250003Sadrian
2221250003Sadrian	switch (hismode) {
2222250003Sadrian	    case MODE_SERVER:		/* server mode */
2223250003Sadrian	    case MODE_BROADCAST:	/* broadcast mode */
2224250003Sadrian	    case MODE_ACTIVE:		/* symmetric active mode */
2225250008Sadrian	    case MODE_PASSIVE:		/* symmetric passive mode */
2226250003Sadrian		if (   is_authentic == AUTH_OK
2227250008Sadrian		    && skeyid
2228250008Sadrian		    && skeyid <= NTP_MAXKEY
2229250008Sadrian		    && !authistrustedip(skeyid, &peer->srcadr)) {
2230250003Sadrian			report_event(PEVNT_AUTH, peer, "authIP");
2231250003Sadrian			peer->badauth++;
2232250003Sadrian			return;
2233250003Sadrian		}
2234250003Sadrian		break;
2235250003Sadrian
2236250003Sadrian	    case MODE_CLIENT:		/* client mode */
2237250003Sadrian#if 0		/* At this point, MODE_CONTROL is overloaded by MODE_BCLIENT */
2238250003Sadrian	    case MODE_CONTROL:		/* control mode */
2239250003Sadrian#endif
2240250003Sadrian	    case MODE_PRIVATE:		/* private mode */
2241250003Sadrian	    case MODE_BCLIENT:		/* broadcast client mode */
2242250003Sadrian		break;
2243250003Sadrian
2244250003Sadrian	    case MODE_UNSPEC:		/* unspecified (old version) */
2245250003Sadrian	    default:
2246250003Sadrian		msyslog(LOG_INFO,
2247250003Sadrian			"receive: Unexpected mode (%d) in packet from %s",
2248250003Sadrian			hismode, ntoa(&peer->srcadr));
2249250003Sadrian		break;
2250250003Sadrian	}
2251250003Sadrian
2252250003Sadrian
2253250003Sadrian	/*
2254250003Sadrian	 * That was hard and I am sweaty, but the packet is squeaky
2255250003Sadrian	 * clean. Get on with real work.
2256250003Sadrian	 */
2257250003Sadrian	peer->timereceived = current_time;
2258250003Sadrian	peer->timelastrec = current_time;
2259250003Sadrian	if (is_authentic == AUTH_OK)
2260250003Sadrian		peer->flags |= FLAG_AUTHENTIC;
2261250003Sadrian	else
2262250003Sadrian		peer->flags &= ~FLAG_AUTHENTIC;
2263250003Sadrian
2264250003Sadrian#ifdef AUTOKEY
2265250003Sadrian	/*
2266250003Sadrian	 * More autokey dance. The rules of the cha-cha are as follows:
2267250003Sadrian	 *
2268250003Sadrian	 * 1. If there is no key or the key is not auto, do nothing.
2269250003Sadrian	 *
2270250003Sadrian	 * 2. If this packet is in response to the one just previously
2271250003Sadrian	 *    sent or from a broadcast server, do the extension fields.
2272250003Sadrian	 *    Otherwise, assume bogosity and bail out.
2273250003Sadrian	 *
2274250003Sadrian	 * 3. If an extension field contains a verified signature, it is
2275250003Sadrian	 *    self-authenticated and we sit the dance.
2276250003Sadrian	 *
2277250003Sadrian	 * 4. If this is a server reply, check only to see that the
2278250003Sadrian	 *    transmitted key ID matches the received key ID.
2279250003Sadrian	 *
2280250003Sadrian	 * 5. Check to see that one or more hashes of the current key ID
2281250003Sadrian	 *    matches the previous key ID or ultimate original key ID
2282250003Sadrian	 *    obtained from the broadcaster or symmetric peer. If no
2283250003Sadrian	 *    match, sit the dance and call for new autokey values.
2284250003Sadrian	 *
2285250003Sadrian	 * In case of crypto error, fire the orchestra, stop dancing and
2286250003Sadrian	 * restart the protocol.
2287250003Sadrian	 */
2288250003Sadrian	if (peer->flags & FLAG_SKEY) {
2289250003Sadrian		/*
2290250003Sadrian		 * Decrement remaining autokey hashes. This isn't
2291250003Sadrian		 * perfect if a packet is lost, but results in no harm.
2292250003Sadrian		 */
2293250003Sadrian		ap = (struct autokey *)peer->recval.ptr;
2294250003Sadrian		if (ap != NULL) {
2295250003Sadrian			if (ap->seq > 0)
2296250003Sadrian				ap->seq--;
2297250003Sadrian		}
2298250003Sadrian		peer->flash |= TEST8;
2299250003Sadrian		rval = crypto_recv(peer, rbufp);
2300250003Sadrian		if (rval == XEVNT_OK) {
2301250003Sadrian			peer->unreach = 0;
2302250003Sadrian		} else {
2303250003Sadrian			if (rval == XEVNT_ERR) {
2304250003Sadrian				report_event(PEVNT_RESTART, peer,
2305250003Sadrian				    "crypto error");
2306250003Sadrian				peer_clear(peer, "CRYP");
2307250003Sadrian				peer->flash |= TEST9;	/* bad crypt */
2308250003Sadrian				if (peer->flags & FLAG_PREEMPT) {
2309250003Sadrian					if (unpeer_crypto_early) {
2310250003Sadrian						unpeer(peer);
2311250003Sadrian					}
2312250003Sadrian				}
2313250003Sadrian			}
2314250003Sadrian			return;
2315250003Sadrian		}
2316250003Sadrian
2317250003Sadrian		/*
2318250003Sadrian		 * If server mode, verify the receive key ID matches
2319250003Sadrian		 * the transmit key ID.
2320250003Sadrian		 */
2321250003Sadrian		if (hismode == MODE_SERVER) {
2322250003Sadrian			if (skeyid == peer->keyid)
2323250003Sadrian				peer->flash &= ~TEST8;
2324250003Sadrian
2325250003Sadrian		/*
2326250003Sadrian		 * If an extension field is present, verify only that it
2327250003Sadrian		 * has been correctly signed. We don't need a sequence
2328250003Sadrian		 * check here, but the sequence continues.
2329250003Sadrian		 */
2330250003Sadrian		} else if (!(peer->flash & TEST8)) {
2331250003Sadrian			peer->pkeyid = skeyid;
2332250003Sadrian
2333250003Sadrian		/*
2334250003Sadrian		 * Now the fun part. Here, skeyid is the current ID in
2335250003Sadrian		 * the packet, pkeyid is the ID in the last packet and
2336250003Sadrian		 * tkeyid is the hash of skeyid. If the autokey values
2337250003Sadrian		 * have not been received, this is an automatic error.
2338250003Sadrian		 * If so, check that the tkeyid matches pkeyid. If not,
2339250003Sadrian		 * hash tkeyid and try again. If the number of hashes
2340250003Sadrian		 * exceeds the number remaining in the sequence, declare
2341250003Sadrian		 * a successful failure and refresh the autokey values.
2342250003Sadrian		 */
2343250003Sadrian		} else if (ap != NULL) {
2344250003Sadrian			int i;
2345250003Sadrian
2346250003Sadrian			for (i = 0; ; i++) {
2347250003Sadrian				if (   tkeyid == peer->pkeyid
2348250003Sadrian				    || tkeyid == ap->key) {
2349250003Sadrian					peer->flash &= ~TEST8;
2350250003Sadrian					peer->pkeyid = skeyid;
2351250003Sadrian					ap->seq -= i;
2352250003Sadrian					break;
2353250003Sadrian				}
2354250003Sadrian				if (i > ap->seq) {
2355250003Sadrian					peer->crypto &=
2356250003Sadrian					    ~CRYPTO_FLAG_AUTO;
2357250003Sadrian					break;
2358250003Sadrian				}
2359250003Sadrian				tkeyid = session_key(
2360250003Sadrian				    &rbufp->recv_srcadr, dstadr_sin,
2361250003Sadrian				    tkeyid, pkeyid, 0);
2362250003Sadrian			}
2363250003Sadrian			if (peer->flash & TEST8)
2364250003Sadrian				report_event(PEVNT_AUTH, peer, "keylist");
2365250003Sadrian		}
2366250003Sadrian		if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
2367250003Sadrian			peer->flash |= TEST8;	/* bad autokey */
2368250003Sadrian
2369250003Sadrian		/*
2370250003Sadrian		 * The maximum lifetime of the protocol is about one
2371250003Sadrian		 * week before restarting the Autokey protocol to
2372250003Sadrian		 * refresh certificates and leapseconds values.
2373250003Sadrian		 */
2374250003Sadrian		if (current_time > peer->refresh) {
2375250003Sadrian			report_event(PEVNT_RESTART, peer,
2376250003Sadrian			    "crypto refresh");
2377250003Sadrian			peer_clear(peer, "TIME");
2378250003Sadrian			return;
2379250003Sadrian		}
2380250003Sadrian	}
2381250003Sadrian#endif	/* AUTOKEY */
2382250003Sadrian
2383250003Sadrian	/*
2384250003Sadrian	 * The dance is complete and the flash bits have been lit. Toss
2385250003Sadrian	 * the packet over the fence for processing, which may light up
2386250003Sadrian	 * more flashers.
2387250003Sadrian	 */
2388250003Sadrian	process_packet(peer, pkt, rbufp->recv_length);
2389250003Sadrian
2390250003Sadrian	/*
2391250003Sadrian	 * In interleaved mode update the state variables. Also adjust the
2392250003Sadrian	 * transmit phase to avoid crossover.
2393250003Sadrian	 */
2394250003Sadrian	if (peer->flip != 0) {
2395250003Sadrian		peer->rec = p_rec;
2396250003Sadrian		peer->dst = rbufp->recv_time;
2397250003Sadrian		if (peer->nextdate - current_time < (1U << min(peer->ppoll,
2398250003Sadrian		    peer->hpoll)) / 2)
2399250003Sadrian			peer->nextdate++;
2400250003Sadrian		else
2401250003Sadrian			peer->nextdate--;
2402250003Sadrian	}
2403250003Sadrian}
2404250003Sadrian
2405250003Sadrian
2406250003Sadrian/*
2407250003Sadrian * process_packet - Packet Procedure, a la Section 3.4.4 of RFC-1305
2408250003Sadrian *	Or almost, at least.  If we're in here we have a reasonable
2409250003Sadrian *	expectation that we will be having a long term
2410250003Sadrian *	relationship with this host.
2411250003Sadrian */
2412250003Sadrianvoid
2413250003Sadrianprocess_packet(
2414250003Sadrian	register struct peer *peer,
2415250003Sadrian	register struct pkt *pkt,
2416250003Sadrian	u_int	len
2417250003Sadrian	)
2418250003Sadrian{
2419250003Sadrian	double	t34, t21;
2420250003Sadrian	double	p_offset, p_del, p_disp;
2421250003Sadrian	l_fp	p_rec, p_xmt, p_org, p_reftime, ci;
2422250003Sadrian	u_char	pmode, pleap, pversion, pstratum;
2423250003Sadrian	char	statstr[NTP_MAXSTRLEN];
2424250003Sadrian#ifdef ASSYM
2425250003Sadrian	int	itemp;
2426250003Sadrian	double	etemp, ftemp, td;
2427250003Sadrian#endif /* ASSYM */
2428250003Sadrian
2429250003Sadrian#if 0
2430250003Sadrian	sys_processed++;
2431250003Sadrian	peer->processed++;
2432250003Sadrian#endif
2433250003Sadrian	p_del = FPTOD(NTOHS_FP(pkt->rootdelay));
2434250003Sadrian	p_offset = 0;
2435250003Sadrian	p_disp = FPTOD(NTOHS_FP(pkt->rootdisp));
2436250003Sadrian	NTOHL_FP(&pkt->reftime, &p_reftime);
2437250003Sadrian	NTOHL_FP(&pkt->org, &p_org);
2438250003Sadrian	NTOHL_FP(&pkt->rec, &p_rec);
2439250003Sadrian	NTOHL_FP(&pkt->xmt, &p_xmt);
2440250003Sadrian	pmode = PKT_MODE(pkt->li_vn_mode);
2441250003Sadrian	pleap = PKT_LEAP(pkt->li_vn_mode);
2442250003Sadrian	pversion = PKT_VERSION(pkt->li_vn_mode);
2443250003Sadrian	pstratum = PKT_TO_STRATUM(pkt->stratum);
2444250003Sadrian
2445250003Sadrian	/**/
2446250003Sadrian
2447250003Sadrian	/**/
2448250003Sadrian
2449250003Sadrian	/*
2450250003Sadrian	 * Verify the server is synchronized; that is, the leap bits,
2451250003Sadrian	 * stratum and root distance are valid.
2452250003Sadrian	 */
2453250003Sadrian	if (   pleap == LEAP_NOTINSYNC		/* test 6 */
2454	    || pstratum < sys_floor || pstratum >= sys_ceiling)
2455		peer->flash |= TEST6;		/* bad synch or strat */
2456	if (p_del / 2 + p_disp >= MAXDISPERSE)	/* test 7 */
2457		peer->flash |= TEST7;		/* bad header */
2458
2459	/*
2460	 * If any tests fail at this point, the packet is discarded.
2461	 * Note that some flashers may have already been set in the
2462	 * receive() routine.
2463	 */
2464	if (peer->flash & PKT_TEST_MASK) {
2465		peer->seldisptoolarge++;
2466		DPRINTF(1, ("packet: flash header %04x\n",
2467			    peer->flash));
2468		poll_update(peer, peer->hpoll);	/* ppoll updated? */
2469		return;
2470	}
2471
2472	/**/
2473
2474#if 1
2475	sys_processed++;
2476	peer->processed++;
2477#endif
2478
2479	/*
2480	 * Capture the header values in the client/peer association..
2481	 */
2482	record_raw_stats(&peer->srcadr,
2483	    peer->dstadr ? &peer->dstadr->sin : NULL,
2484	    &p_org, &p_rec, &p_xmt, &peer->dst,
2485	    pleap, pversion, pmode, pstratum, pkt->ppoll, pkt->precision,
2486	    p_del, p_disp, pkt->refid,
2487	    len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
2488	peer->leap = pleap;
2489	peer->stratum = min(pstratum, STRATUM_UNSPEC);
2490	peer->pmode = pmode;
2491	peer->precision = pkt->precision;
2492	peer->rootdelay = p_del;
2493	peer->rootdisp = p_disp;
2494	peer->refid = pkt->refid;		/* network byte order */
2495	peer->reftime = p_reftime;
2496
2497	/*
2498	 * First, if either burst mode is armed, enable the burst.
2499	 * Compute the headway for the next packet and delay if
2500	 * necessary to avoid exceeding the threshold.
2501	 */
2502	if (peer->retry > 0) {
2503		peer->retry = 0;
2504		if (peer->reach)
2505			peer->burst = min(1 << (peer->hpoll -
2506			    peer->minpoll), NTP_SHIFT) - 1;
2507		else
2508			peer->burst = NTP_IBURST - 1;
2509		if (peer->burst > 0)
2510			peer->nextdate = current_time;
2511	}
2512	poll_update(peer, peer->hpoll);
2513
2514	/**/
2515
2516	/*
2517	 * If the peer was previously unreachable, raise a trap. In any
2518	 * case, mark it reachable.
2519	 */
2520	if (!peer->reach) {
2521		report_event(PEVNT_REACH, peer, NULL);
2522		peer->timereachable = current_time;
2523	}
2524	peer->reach |= 1;
2525
2526	/*
2527	 * For a client/server association, calculate the clock offset,
2528	 * roundtrip delay and dispersion. The equations are reordered
2529	 * from the spec for more efficient use of temporaries. For a
2530	 * broadcast association, offset the last measurement by the
2531	 * computed delay during the client/server volley. Note the
2532	 * computation of dispersion includes the system precision plus
2533	 * that due to the frequency error since the origin time.
2534	 *
2535	 * It is very important to respect the hazards of overflow. The
2536	 * only permitted operation on raw timestamps is subtraction,
2537	 * where the result is a signed quantity spanning from 68 years
2538	 * in the past to 68 years in the future. To avoid loss of
2539	 * precision, these calculations are done using 64-bit integer
2540	 * arithmetic. However, the offset and delay calculations are
2541	 * sums and differences of these first-order differences, which
2542	 * if done using 64-bit integer arithmetic, would be valid over
2543	 * only half that span. Since the typical first-order
2544	 * differences are usually very small, they are converted to 64-
2545	 * bit doubles and all remaining calculations done in floating-
2546	 * double arithmetic. This preserves the accuracy while
2547	 * retaining the 68-year span.
2548	 *
2549	 * There are three interleaving schemes, basic, interleaved
2550	 * symmetric and interleaved broadcast. The timestamps are
2551	 * idioscyncratically different. See the onwire briefing/white
2552	 * paper at www.eecis.udel.edu/~mills for details.
2553	 *
2554	 * Interleaved symmetric mode
2555	 * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
2556	 * t4 = peer->dst
2557	 */
2558	if (peer->flip != 0) {
2559		ci = p_xmt;				/* t3 - t4 */
2560		L_SUB(&ci, &peer->dst);
2561		LFPTOD(&ci, t34);
2562		ci = p_rec;				/* t2 - t1 */
2563		if (peer->flip > 0)
2564			L_SUB(&ci, &peer->borg);
2565		else
2566			L_SUB(&ci, &peer->aorg);
2567		LFPTOD(&ci, t21);
2568		p_del = t21 - t34;
2569		p_offset = (t21 + t34) / 2.;
2570		if (p_del < 0 || p_del > 1.) {
2571			snprintf(statstr, sizeof(statstr),
2572			    "t21 %.6f t34 %.6f", t21, t34);
2573			report_event(PEVNT_XERR, peer, statstr);
2574			return;
2575		}
2576
2577	/*
2578	 * Broadcast modes
2579	 */
2580	} else if (peer->pmode == MODE_BROADCAST) {
2581
2582		/*
2583		 * Interleaved broadcast mode. Use interleaved timestamps.
2584		 * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
2585		 */
2586		if (peer->flags & FLAG_XB) {
2587			ci = p_org;			/* delay */
2588			L_SUB(&ci, &peer->aorg);
2589			LFPTOD(&ci, t34);
2590			ci = p_org;			/* t2 - t1 */
2591			L_SUB(&ci, &peer->borg);
2592			LFPTOD(&ci, t21);
2593			peer->aorg = p_xmt;
2594			peer->borg = peer->dst;
2595			if (t34 < 0 || t34 > 1.) {
2596				/* drop all if in the initial volley */
2597				if (FLAG_BC_VOL & peer->flags)
2598					goto bcc_init_volley_fail;
2599				snprintf(statstr, sizeof(statstr),
2600				    "offset %.6f delay %.6f", t21, t34);
2601				report_event(PEVNT_XERR, peer, statstr);
2602				return;
2603			}
2604			p_offset = t21;
2605			peer->xleave = t34;
2606
2607		/*
2608		 * Basic broadcast - use direct timestamps.
2609		 * t3 = p_xmt, t4 = peer->dst
2610		 */
2611		} else {
2612			ci = p_xmt;		/* t3 - t4 */
2613			L_SUB(&ci, &peer->dst);
2614			LFPTOD(&ci, t34);
2615			p_offset = t34;
2616		}
2617
2618		/*
2619		 * When calibration is complete and the clock is
2620		 * synchronized, the bias is calculated as the difference
2621		 * between the unicast timestamp and the broadcast
2622		 * timestamp. This works for both basic and interleaved
2623		 * modes.
2624		 * [Bug 3031] Don't keep this peer when the delay
2625		 * calculation gives reason to suspect clock steps.
2626		 * This is assumed for delays > 50ms.
2627		 */
2628		if (FLAG_BC_VOL & peer->flags) {
2629			peer->flags &= ~FLAG_BC_VOL;
2630			peer->delay = fabs(peer->offset - p_offset) * 2;
2631			DPRINTF(2, ("broadcast volley: initial delay=%.6f\n",
2632				peer->delay));
2633			if (peer->delay > fabs(sys_bdelay)) {
2634		bcc_init_volley_fail:
2635				DPRINTF(2, ("%s", "broadcast volley: initial delay exceeds limit\n"));
2636				unpeer(peer);
2637				return;
2638			}
2639		}
2640		peer->nextdate = current_time + (1u << peer->ppoll) - 2u;
2641		p_del = peer->delay;
2642		p_offset += p_del / 2;
2643
2644
2645	/*
2646	 * Basic mode, otherwise known as the old fashioned way.
2647	 *
2648	 * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2649	 */
2650	} else {
2651		ci = p_xmt;				/* t3 - t4 */
2652		L_SUB(&ci, &peer->dst);
2653		LFPTOD(&ci, t34);
2654		ci = p_rec;				/* t2 - t1 */
2655		L_SUB(&ci, &p_org);
2656		LFPTOD(&ci, t21);
2657		p_del = fabs(t21 - t34);
2658		p_offset = (t21 + t34) / 2.;
2659	}
2660	p_del = max(p_del, LOGTOD(sys_precision));
2661	p_disp = LOGTOD(sys_precision) + LOGTOD(peer->precision) +
2662	    clock_phi * p_del;
2663
2664#if ASSYM
2665	/*
2666	 * This code calculates the outbound and inbound data rates by
2667	 * measuring the differences between timestamps at different
2668	 * packet lengths. This is helpful in cases of large asymmetric
2669	 * delays commonly experienced on deep space communication
2670	 * links.
2671	 */
2672	if (peer->t21_last > 0 && peer->t34_bytes > 0) {
2673		itemp = peer->t21_bytes - peer->t21_last;
2674		if (itemp > 25) {
2675			etemp = t21 - peer->t21;
2676			if (fabs(etemp) > 1e-6) {
2677				ftemp = itemp / etemp;
2678				if (ftemp > 1000.)
2679					peer->r21 = ftemp;
2680			}
2681		}
2682		itemp = len - peer->t34_bytes;
2683		if (itemp > 25) {
2684			etemp = -t34 - peer->t34;
2685			if (fabs(etemp) > 1e-6) {
2686				ftemp = itemp / etemp;
2687				if (ftemp > 1000.)
2688					peer->r34 = ftemp;
2689			}
2690		}
2691	}
2692
2693	/*
2694	 * The following section compensates for different data rates on
2695	 * the outbound (d21) and inbound (t34) directions. To do this,
2696	 * it finds t such that r21 * t - r34 * (d - t) = 0, where d is
2697	 * the roundtrip delay. Then it calculates the correction as a
2698	 * fraction of d.
2699	 */
2700	peer->t21 = t21;
2701	peer->t21_last = peer->t21_bytes;
2702	peer->t34 = -t34;
2703	peer->t34_bytes = len;
2704	DPRINTF(2, ("packet: t21 %.9lf %d t34 %.9lf %d\n", peer->t21,
2705		    peer->t21_bytes, peer->t34, peer->t34_bytes));
2706	if (peer->r21 > 0 && peer->r34 > 0 && p_del > 0) {
2707		if (peer->pmode != MODE_BROADCAST)
2708			td = (peer->r34 / (peer->r21 + peer->r34) -
2709			    .5) * p_del;
2710		else
2711			td = 0;
2712
2713		/*
2714		 * Unfortunately, in many cases the errors are
2715		 * unacceptable, so for the present the rates are not
2716		 * used. In future, we might find conditions where the
2717		 * calculations are useful, so this should be considered
2718		 * a work in progress.
2719		 */
2720		t21 -= td;
2721		t34 -= td;
2722		DPRINTF(2, ("packet: del %.6lf r21 %.1lf r34 %.1lf %.6lf\n",
2723			    p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2724			    td));
2725	}
2726#endif /* ASSYM */
2727
2728	/*
2729	 * That was awesome. Now hand off to the clock filter.
2730	 */
2731	clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2732
2733	/*
2734	 * If we are in broadcast calibrate mode, return to broadcast
2735	 * client mode when the client is fit and the autokey dance is
2736	 * complete.
2737	 */
2738	if (   (FLAG_BC_VOL & peer->flags)
2739	    && MODE_CLIENT == peer->hmode
2740	    && !(TEST11 & peer_unfit(peer))) {	/* distance exceeded */
2741#ifdef AUTOKEY
2742		if (peer->flags & FLAG_SKEY) {
2743			if (!(~peer->crypto & CRYPTO_FLAG_ALL))
2744				peer->hmode = MODE_BCLIENT;
2745		} else {
2746			peer->hmode = MODE_BCLIENT;
2747		}
2748#else	/* !AUTOKEY follows */
2749		peer->hmode = MODE_BCLIENT;
2750#endif	/* !AUTOKEY */
2751	}
2752}
2753
2754
2755/*
2756 * clock_update - Called at system process update intervals.
2757 */
2758static void
2759clock_update(
2760	struct peer *peer	/* peer structure pointer */
2761	)
2762{
2763	double	dtemp;
2764	l_fp	now;
2765#ifdef HAVE_LIBSCF_H
2766	char	*fmri;
2767#endif /* HAVE_LIBSCF_H */
2768
2769	/*
2770	 * Update the system state variables. We do this very carefully,
2771	 * as the poll interval might need to be clamped differently.
2772	 */
2773	sys_peer = peer;
2774	sys_epoch = peer->epoch;
2775	if (sys_poll < peer->minpoll)
2776		sys_poll = peer->minpoll;
2777	if (sys_poll > peer->maxpoll)
2778		sys_poll = peer->maxpoll;
2779	poll_update(peer, sys_poll);
2780	sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
2781	if (   peer->stratum == STRATUM_REFCLOCK
2782	    || peer->stratum == STRATUM_UNSPEC)
2783		sys_refid = peer->refid;
2784	else
2785		sys_refid = addr2refid(&peer->srcadr);
2786	/*
2787	 * Root Dispersion (E) is defined (in RFC 5905) as:
2788	 *
2789	 * E = p.epsilon_r + p.epsilon + p.psi + PHI*(s.t - p.t) + |THETA|
2790	 *
2791	 * where:
2792	 *  p.epsilon_r is the PollProc's root dispersion
2793	 *  p.epsilon   is the PollProc's dispersion
2794	 *  p.psi       is the PollProc's jitter
2795	 *  THETA       is the combined offset
2796	 *
2797	 * NB: Think Hard about where these numbers come from and
2798	 * what they mean.  When did peer->update happen?  Has anything
2799	 * interesting happened since then?  What values are the most
2800	 * defensible?  Why?
2801	 *
2802	 * DLM thinks this equation is probably the best of all worse choices.
2803	 */
2804	dtemp	= peer->rootdisp
2805		+ peer->disp
2806		+ sys_jitter
2807		+ clock_phi * (current_time - peer->update)
2808		+ fabs(sys_offset);
2809
2810	if (dtemp > sys_mindisp)
2811		sys_rootdisp = dtemp;
2812	else
2813		sys_rootdisp = sys_mindisp;
2814	sys_rootdelay = peer->delay + peer->rootdelay;
2815	sys_reftime = peer->dst;
2816
2817	DPRINTF(1, ("clock_update: at %lu sample %lu associd %d\n",
2818		    current_time, peer->epoch, peer->associd));
2819
2820	/*
2821	 * Comes now the moment of truth. Crank the clock discipline and
2822	 * see what comes out.
2823	 */
2824	switch (local_clock(peer, sys_offset)) {
2825
2826	/*
2827	 * Clock exceeds panic threshold. Life as we know it ends.
2828	 */
2829	case -1:
2830#ifdef HAVE_LIBSCF_H
2831		/*
2832		 * For Solaris enter the maintenance mode.
2833		 */
2834		if ((fmri = getenv("SMF_FMRI")) != NULL) {
2835			if (smf_maintain_instance(fmri, 0) < 0) {
2836				printf("smf_maintain_instance: %s\n",
2837				    scf_strerror(scf_error()));
2838				exit(1);
2839			}
2840			/*
2841			 * Sleep until SMF kills us.
2842			 */
2843			for (;;)
2844				pause();
2845		}
2846#endif /* HAVE_LIBSCF_H */
2847		exit (-1);
2848		/* not reached */
2849
2850	/*
2851	 * Clock was stepped. Flush all time values of all peers.
2852	 */
2853	case 2:
2854		clear_all();
2855		set_sys_leap(LEAP_NOTINSYNC);
2856		sys_stratum = STRATUM_UNSPEC;
2857		memcpy(&sys_refid, "STEP", 4);
2858		sys_rootdelay = 0;
2859		sys_rootdisp = 0;
2860		L_CLR(&sys_reftime);
2861		sys_jitter = LOGTOD(sys_precision);
2862		leapsec_reset_frame();
2863		break;
2864
2865	/*
2866	 * Clock was slewed. Handle the leapsecond stuff.
2867	 */
2868	case 1:
2869
2870		/*
2871		 * If this is the first time the clock is set, reset the
2872		 * leap bits. If crypto, the timer will goose the setup
2873		 * process.
2874		 */
2875		if (sys_leap == LEAP_NOTINSYNC) {
2876			set_sys_leap(LEAP_NOWARNING);
2877#ifdef AUTOKEY
2878			if (crypto_flags)
2879				crypto_update();
2880#endif	/* AUTOKEY */
2881			/*
2882			 * If our parent process is waiting for the
2883			 * first clock sync, send them home satisfied.
2884			 */
2885#ifdef HAVE_WORKING_FORK
2886			if (waitsync_fd_to_close != -1) {
2887				close(waitsync_fd_to_close);
2888				waitsync_fd_to_close = -1;
2889				DPRINTF(1, ("notified parent --wait-sync is done\n"));
2890			}
2891#endif /* HAVE_WORKING_FORK */
2892
2893		}
2894
2895		/*
2896		 * If there is no leap second pending and the number of
2897		 * survivor leap bits is greater than half the number of
2898		 * survivors, try to schedule a leap for the end of the
2899		 * current month. (This only works if no leap second for
2900		 * that range is in the table, so doing this more than
2901		 * once is mostly harmless.)
2902		 */
2903		if (leapsec == LSPROX_NOWARN) {
2904			if (   leap_vote_ins > leap_vote_del
2905			    && leap_vote_ins > sys_survivors / 2) {
2906				get_systime(&now);
2907				leapsec_add_dyn(TRUE, now.l_ui, NULL);
2908			}
2909			if (   leap_vote_del > leap_vote_ins
2910			    && leap_vote_del > sys_survivors / 2) {
2911				get_systime(&now);
2912				leapsec_add_dyn(FALSE, now.l_ui, NULL);
2913			}
2914		}
2915		break;
2916
2917	/*
2918	 * Popcorn spike or step threshold exceeded. Pretend it never
2919	 * happened.
2920	 */
2921	default:
2922		break;
2923	}
2924}
2925
2926
2927/*
2928 * poll_update - update peer poll interval
2929 */
2930void
2931poll_update(
2932	struct peer *peer,	/* peer structure pointer */
2933	u_char	mpoll
2934	)
2935{
2936	u_long	next, utemp;
2937	u_char	hpoll;
2938
2939	/*
2940	 * This routine figures out when the next poll should be sent.
2941	 * That turns out to be wickedly complicated. One problem is
2942	 * that sometimes the time for the next poll is in the past when
2943	 * the poll interval is reduced. We watch out for races here
2944	 * between the receive process and the poll process.
2945	 *
2946	 * Clamp the poll interval between minpoll and maxpoll.
2947	 */
2948	hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
2949
2950#ifdef AUTOKEY
2951	/*
2952	 * If during the crypto protocol the poll interval has changed,
2953	 * the lifetimes in the key list are probably bogus. Purge the
2954	 * the key list and regenerate it later.
2955	 */
2956	if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
2957		key_expire(peer);
2958#endif	/* AUTOKEY */
2959	peer->hpoll = hpoll;
2960
2961	/*
2962	 * There are three variables important for poll scheduling, the
2963	 * current time (current_time), next scheduled time (nextdate)
2964	 * and the earliest time (utemp). The earliest time is 2 s
2965	 * seconds, but could be more due to rate management. When
2966	 * sending in a burst, use the earliest time. When not in a
2967	 * burst but with a reply pending, send at the earliest time
2968	 * unless the next scheduled time has not advanced. This can
2969	 * only happen if multiple replies are pending in the same
2970	 * response interval. Otherwise, send at the later of the next
2971	 * scheduled time and the earliest time.
2972	 *
2973	 * Now we figure out if there is an override. If a burst is in
2974	 * progress and we get called from the receive process, just
2975	 * slink away. If called from the poll process, delay 1 s for a
2976	 * reference clock, otherwise 2 s.
2977	 */
2978	utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
2979	    (1 << peer->minpoll), ntp_minpkt);
2980	if (peer->burst > 0) {
2981		if (peer->nextdate > current_time)
2982			return;
2983#ifdef REFCLOCK
2984		else if (peer->flags & FLAG_REFCLOCK)
2985			peer->nextdate = current_time + RESP_DELAY;
2986#endif /* REFCLOCK */
2987		else
2988			peer->nextdate = utemp;
2989
2990#ifdef AUTOKEY
2991	/*
2992	 * If a burst is not in progress and a crypto response message
2993	 * is pending, delay 2 s, but only if this is a new interval.
2994	 */
2995	} else if (peer->cmmd != NULL) {
2996		if (peer->nextdate > current_time) {
2997			if (peer->nextdate + ntp_minpkt != utemp)
2998				peer->nextdate = utemp;
2999		} else {
3000			peer->nextdate = utemp;
3001		}
3002#endif	/* AUTOKEY */
3003
3004	/*
3005	 * The ordinary case. If a retry, use minpoll; if unreachable,
3006	 * use host poll; otherwise, use the minimum of host and peer
3007	 * polls; In other words, oversampling is okay but
3008	 * understampling is evil. Use the maximum of this value and the
3009	 * headway. If the average headway is greater than the headway
3010	 * threshold, increase the headway by the minimum interval.
3011	 */
3012	} else {
3013		if (peer->retry > 0)
3014			hpoll = peer->minpoll;
3015		else
3016			hpoll = min(peer->ppoll, peer->hpoll);
3017#ifdef REFCLOCK
3018		if (peer->flags & FLAG_REFCLOCK)
3019			next = 1 << hpoll;
3020		else
3021#endif /* REFCLOCK */
3022			next = ((0x1000UL | (ntp_random() & 0x0ff)) <<
3023			    hpoll) >> 12;
3024		next += peer->outdate;
3025		if (next > utemp)
3026			peer->nextdate = next;
3027		else
3028			peer->nextdate = utemp;
3029		if (peer->throttle > (1 << peer->minpoll))
3030			peer->nextdate += ntp_minpkt;
3031	}
3032	DPRINTF(2, ("poll_update: at %lu %s poll %d burst %d retry %d head %d early %lu next %lu\n",
3033		    current_time, ntoa(&peer->srcadr), peer->hpoll,
3034		    peer->burst, peer->retry, peer->throttle,
3035		    utemp - current_time, peer->nextdate -
3036		    current_time));
3037}
3038
3039
3040/*
3041 * peer_clear - clear peer filter registers.  See Section 3.4.8 of the
3042 * spec.
3043 */
3044void
3045peer_clear(
3046	struct peer *peer,		/* peer structure */
3047	const char *ident		/* tally lights */
3048	)
3049{
3050	u_char	u;
3051	l_fp	bxmt = peer->bxmt;	/* bcast clients retain this! */
3052
3053#ifdef AUTOKEY
3054	/*
3055	 * If cryptographic credentials have been acquired, toss them to
3056	 * Valhalla. Note that autokeys are ephemeral, in that they are
3057	 * tossed immediately upon use. Therefore, the keylist can be
3058	 * purged anytime without needing to preserve random keys. Note
3059	 * that, if the peer is purged, the cryptographic variables are
3060	 * purged, too. This makes it much harder to sneak in some
3061	 * unauthenticated data in the clock filter.
3062	 */
3063	key_expire(peer);
3064	if (peer->iffval != NULL)
3065		BN_free(peer->iffval);
3066	value_free(&peer->cookval);
3067	value_free(&peer->recval);
3068	value_free(&peer->encrypt);
3069	value_free(&peer->sndval);
3070	if (peer->cmmd != NULL)
3071		free(peer->cmmd);
3072	if (peer->subject != NULL)
3073		free(peer->subject);
3074	if (peer->issuer != NULL)
3075		free(peer->issuer);
3076#endif /* AUTOKEY */
3077
3078	/*
3079	 * Clear all values, including the optional crypto values above.
3080	 */
3081	memset(CLEAR_TO_ZERO(peer), 0, LEN_CLEAR_TO_ZERO(peer));
3082	peer->ppoll = peer->maxpoll;
3083	peer->hpoll = peer->minpoll;
3084	peer->disp = MAXDISPERSE;
3085	peer->flash = peer_unfit(peer);
3086	peer->jitter = LOGTOD(sys_precision);
3087
3088	/* Don't throw away our broadcast replay protection */
3089	if (peer->hmode == MODE_BCLIENT)
3090		peer->bxmt = bxmt;
3091
3092	/*
3093	 * If interleave mode, initialize the alternate origin switch.
3094	 */
3095	if (peer->flags & FLAG_XLEAVE)
3096		peer->flip = 1;
3097	for (u = 0; u < NTP_SHIFT; u++) {
3098		peer->filter_order[u] = u;
3099		peer->filter_disp[u] = MAXDISPERSE;
3100	}
3101#ifdef REFCLOCK
3102	if (!(peer->flags & FLAG_REFCLOCK)) {
3103#endif
3104		peer->leap = LEAP_NOTINSYNC;
3105		peer->stratum = STRATUM_UNSPEC;
3106		memcpy(&peer->refid, ident, 4);
3107#ifdef REFCLOCK
3108	} else {
3109		/* Clear refclock sample filter */
3110		peer->procptr->codeproc = 0;
3111		peer->procptr->coderecv = 0;
3112	}
3113#endif
3114
3115	/*
3116	 * During initialization use the association count to spread out
3117	 * the polls at one-second intervals. Passive associations'
3118	 * first poll is delayed by the "discard minimum" to avoid rate
3119	 * limiting. Other post-startup new or cleared associations
3120	 * randomize the first poll over the minimum poll interval to
3121	 * avoid implosion.
3122	 */
3123	peer->nextdate = peer->update = peer->outdate = current_time;
3124	if (initializing) {
3125		peer->nextdate += peer_associations;
3126	} else if (MODE_PASSIVE == peer->hmode) {
3127		peer->nextdate += ntp_minpkt;
3128	} else {
3129		peer->nextdate += ntp_random() % peer->minpoll;
3130	}
3131#ifdef AUTOKEY
3132	peer->refresh = current_time + (1 << NTP_REFRESH);
3133#endif	/* AUTOKEY */
3134	DPRINTF(1, ("peer_clear: at %ld next %ld associd %d refid %s\n",
3135		    current_time, peer->nextdate, peer->associd,
3136		    ident));
3137}
3138
3139
3140/*
3141 * clock_filter - add incoming clock sample to filter register and run
3142 *		  the filter procedure to find the best sample.
3143 */
3144void
3145clock_filter(
3146	struct peer *peer,		/* peer structure pointer */
3147	double	sample_offset,		/* clock offset */
3148	double	sample_delay,		/* roundtrip delay */
3149	double	sample_disp		/* dispersion */
3150	)
3151{
3152	double	dst[NTP_SHIFT];		/* distance vector */
3153	int	ord[NTP_SHIFT];		/* index vector */
3154	int	i, j, k, m;
3155	double	dtemp, etemp;
3156	char	tbuf[80];
3157
3158	/*
3159	 * A sample consists of the offset, delay, dispersion and epoch
3160	 * of arrival. The offset and delay are determined by the on-
3161	 * wire protocol. The dispersion grows from the last outbound
3162	 * packet to the arrival of this one increased by the sum of the
3163	 * peer precision and the system precision as required by the
3164	 * error budget. First, shift the new arrival into the shift
3165	 * register discarding the oldest one.
3166	 */
3167	j = peer->filter_nextpt;
3168	peer->filter_offset[j] = sample_offset;
3169	peer->filter_delay[j] = sample_delay;
3170	peer->filter_disp[j] = sample_disp;
3171	peer->filter_epoch[j] = current_time;
3172	j = (j + 1) % NTP_SHIFT;
3173	peer->filter_nextpt = j;
3174
3175	/*
3176	 * Update dispersions since the last update and at the same
3177	 * time initialize the distance and index lists. Since samples
3178	 * become increasingly uncorrelated beyond the Allan intercept,
3179	 * only under exceptional cases will an older sample be used.
3180	 * Therefore, the distance list uses a compound metric. If the
3181	 * dispersion is greater than the maximum dispersion, clamp the
3182	 * distance at that value. If the time since the last update is
3183	 * less than the Allan intercept use the delay; otherwise, use
3184	 * the sum of the delay and dispersion.
3185	 */
3186	dtemp = clock_phi * (current_time - peer->update);
3187	peer->update = current_time;
3188	for (i = NTP_SHIFT - 1; i >= 0; i--) {
3189		if (i != 0)
3190			peer->filter_disp[j] += dtemp;
3191		if (peer->filter_disp[j] >= MAXDISPERSE) {
3192			peer->filter_disp[j] = MAXDISPERSE;
3193			dst[i] = MAXDISPERSE;
3194		} else if (peer->update - peer->filter_epoch[j] >
3195		    (u_long)ULOGTOD(allan_xpt)) {
3196			dst[i] = peer->filter_delay[j] +
3197			    peer->filter_disp[j];
3198		} else {
3199			dst[i] = peer->filter_delay[j];
3200		}
3201		ord[i] = j;
3202		j = (j + 1) % NTP_SHIFT;
3203	}
3204
3205	/*
3206	 * If the clock has stabilized, sort the samples by distance.
3207	 */
3208	if (freq_cnt == 0) {
3209		for (i = 1; i < NTP_SHIFT; i++) {
3210			for (j = 0; j < i; j++) {
3211				if (dst[j] > dst[i]) {
3212					k = ord[j];
3213					ord[j] = ord[i];
3214					ord[i] = k;
3215					etemp = dst[j];
3216					dst[j] = dst[i];
3217					dst[i] = etemp;
3218				}
3219			}
3220		}
3221	}
3222
3223	/*
3224	 * Copy the index list to the association structure so ntpq
3225	 * can see it later. Prune the distance list to leave only
3226	 * samples less than the maximum dispersion, which disfavors
3227	 * uncorrelated samples older than the Allan intercept. To
3228	 * further improve the jitter estimate, of the remainder leave
3229	 * only samples less than the maximum distance, but keep at
3230	 * least two samples for jitter calculation.
3231	 */
3232	m = 0;
3233	for (i = 0; i < NTP_SHIFT; i++) {
3234		peer->filter_order[i] = (u_char) ord[i];
3235		if (   dst[i] >= MAXDISPERSE
3236		    || (m >= 2 && dst[i] >= sys_maxdist))
3237			continue;
3238		m++;
3239	}
3240
3241	/*
3242	 * Compute the dispersion and jitter. The dispersion is weighted
3243	 * exponentially by NTP_FWEIGHT (0.5) so it is normalized close
3244	 * to 1.0. The jitter is the RMS differences relative to the
3245	 * lowest delay sample.
3246	 */
3247	peer->disp = peer->jitter = 0;
3248	k = ord[0];
3249	for (i = NTP_SHIFT - 1; i >= 0; i--) {
3250		j = ord[i];
3251		peer->disp = NTP_FWEIGHT * (peer->disp +
3252		    peer->filter_disp[j]);
3253		if (i < m)
3254			peer->jitter += DIFF(peer->filter_offset[j],
3255			    peer->filter_offset[k]);
3256	}
3257
3258	/*
3259	 * If no acceptable samples remain in the shift register,
3260	 * quietly tiptoe home leaving only the dispersion. Otherwise,
3261	 * save the offset, delay and jitter. Note the jitter must not
3262	 * be less than the precision.
3263	 */
3264	if (m == 0) {
3265		clock_select();
3266		return;
3267	}
3268	etemp = fabs(peer->offset - peer->filter_offset[k]);
3269	peer->offset = peer->filter_offset[k];
3270	peer->delay = peer->filter_delay[k];
3271	if (m > 1)
3272		peer->jitter /= m - 1;
3273	peer->jitter = max(SQRT(peer->jitter), LOGTOD(sys_precision));
3274
3275	/*
3276	 * If the the new sample and the current sample are both valid
3277	 * and the difference between their offsets exceeds CLOCK_SGATE
3278	 * (3) times the jitter and the interval between them is less
3279	 * than twice the host poll interval, consider the new sample
3280	 * a popcorn spike and ignore it.
3281	 */
3282	if (   peer->disp < sys_maxdist
3283	    && peer->filter_disp[k] < sys_maxdist
3284	    && etemp > CLOCK_SGATE * peer->jitter
3285	    && peer->filter_epoch[k] - peer->epoch
3286	       < 2. * ULOGTOD(peer->hpoll)) {
3287		snprintf(tbuf, sizeof(tbuf), "%.6f s", etemp);
3288		report_event(PEVNT_POPCORN, peer, tbuf);
3289		return;
3290	}
3291
3292	/*
3293	 * A new minimum sample is useful only if it is later than the
3294	 * last one used. In this design the maximum lifetime of any
3295	 * sample is not greater than eight times the poll interval, so
3296	 * the maximum interval between minimum samples is eight
3297	 * packets.
3298	 */
3299	if (peer->filter_epoch[k] <= peer->epoch) {
3300	DPRINTF(2, ("clock_filter: old sample %lu\n", current_time -
3301		    peer->filter_epoch[k]));
3302		return;
3303	}
3304	peer->epoch = peer->filter_epoch[k];
3305
3306	/*
3307	 * The mitigated sample statistics are saved for later
3308	 * processing. If not synchronized or not in a burst, tickle the
3309	 * clock select algorithm.
3310	 */
3311	record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
3312	    peer->offset, peer->delay, peer->disp, peer->jitter);
3313	DPRINTF(1, ("clock_filter: n %d off %.6f del %.6f dsp %.6f jit %.6f\n",
3314		    m, peer->offset, peer->delay, peer->disp,
3315		    peer->jitter));
3316	if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC)
3317		clock_select();
3318}
3319
3320
3321/*
3322 * clock_select - find the pick-of-the-litter clock
3323 *
3324 * LOCKCLOCK: (1) If the local clock is the prefer peer, it will always
3325 * be enabled, even if declared falseticker, (2) only the prefer peer
3326 * can be selected as the system peer, (3) if the external source is
3327 * down, the system leap bits are set to 11 and the stratum set to
3328 * infinity.
3329 */
3330void
3331clock_select(void)
3332{
3333	struct peer *peer;
3334	int	i, j, k, n;
3335	int	nlist, nl2;
3336	int	allow;
3337	int	speer;
3338	double	d, e, f, g;
3339	double	high, low;
3340	double	speermet;
3341	double	orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
3342	struct endpoint endp;
3343	struct peer *osys_peer;
3344	struct peer *sys_prefer = NULL;	/* prefer peer */
3345	struct peer *typesystem = NULL;
3346	struct peer *typeorphan = NULL;
3347#ifdef REFCLOCK
3348	struct peer *typeacts = NULL;
3349	struct peer *typelocal = NULL;
3350	struct peer *typepps = NULL;
3351#endif /* REFCLOCK */
3352	static struct endpoint *endpoint = NULL;
3353	static int *indx = NULL;
3354	static peer_select *peers = NULL;
3355	static u_int endpoint_size = 0;
3356	static u_int peers_size = 0;
3357	static u_int indx_size = 0;
3358	size_t octets;
3359
3360	/*
3361	 * Initialize and create endpoint, index and peer lists big
3362	 * enough to handle all associations.
3363	 */
3364	osys_peer = sys_peer;
3365	sys_survivors = 0;
3366#ifdef LOCKCLOCK
3367	set_sys_leap(LEAP_NOTINSYNC);
3368	sys_stratum = STRATUM_UNSPEC;
3369	memcpy(&sys_refid, "DOWN", 4);
3370#endif /* LOCKCLOCK */
3371
3372	/*
3373	 * Allocate dynamic space depending on the number of
3374	 * associations.
3375	 */
3376	nlist = 1;
3377	for (peer = peer_list; peer != NULL; peer = peer->p_link)
3378		nlist++;
3379	endpoint_size = ALIGNED_SIZE(nlist * 2 * sizeof(*endpoint));
3380	peers_size = ALIGNED_SIZE(nlist * sizeof(*peers));
3381	indx_size = ALIGNED_SIZE(nlist * 2 * sizeof(*indx));
3382	octets = endpoint_size + peers_size + indx_size;
3383	endpoint = erealloc(endpoint, octets);
3384	peers = INC_ALIGNED_PTR(endpoint, endpoint_size);
3385	indx = INC_ALIGNED_PTR(peers, peers_size);
3386
3387	/*
3388	 * Initially, we populate the island with all the rifraff peers
3389	 * that happen to be lying around. Those with seriously
3390	 * defective clocks are immediately booted off the island. Then,
3391	 * the falsetickers are culled and put to sea. The truechimers
3392	 * remaining are subject to repeated rounds where the most
3393	 * unpopular at each round is kicked off. When the population
3394	 * has dwindled to sys_minclock, the survivors split a million
3395	 * bucks and collectively crank the chimes.
3396	 */
3397	nlist = nl2 = 0;	/* none yet */
3398	for (peer = peer_list; peer != NULL; peer = peer->p_link) {
3399		peer->new_status = CTL_PST_SEL_REJECT;
3400
3401		/*
3402		 * Leave the island immediately if the peer is
3403		 * unfit to synchronize.
3404		 */
3405		if (peer_unfit(peer)) {
3406			continue;
3407		}
3408
3409		/*
3410		 * If this peer is an orphan parent, elect the
3411		 * one with the lowest metric defined as the
3412		 * IPv4 address or the first 64 bits of the
3413		 * hashed IPv6 address.  To ensure convergence
3414		 * on the same selected orphan, consider as
3415		 * well that this system may have the lowest
3416		 * metric and be the orphan parent.  If this
3417		 * system wins, sys_peer will be NULL to trigger
3418		 * orphan mode in timer().
3419		 */
3420		if (peer->stratum == sys_orphan) {
3421			u_int32	localmet;
3422			u_int32 peermet;
3423
3424			if (peer->dstadr != NULL)
3425				localmet = ntohl(peer->dstadr->addr_refid);
3426			else
3427				localmet = U_INT32_MAX;
3428			peermet = ntohl(addr2refid(&peer->srcadr));
3429			if (peermet < localmet && peermet < orphmet) {
3430				typeorphan = peer;
3431				orphmet = peermet;
3432			}
3433			continue;
3434		}
3435
3436		/*
3437		 * If this peer could have the orphan parent
3438		 * as a synchronization ancestor, exclude it
3439		 * from selection to avoid forming a
3440		 * synchronization loop within the orphan mesh,
3441		 * triggering stratum climb to infinity
3442		 * instability.  Peers at stratum higher than
3443		 * the orphan stratum could have the orphan
3444		 * parent in ancestry so are excluded.
3445		 * See http://bugs.ntp.org/2050
3446		 */
3447		if (peer->stratum > sys_orphan) {
3448			continue;
3449		}
3450#ifdef REFCLOCK
3451		/*
3452		 * The following are special cases. We deal
3453		 * with them later.
3454		 */
3455		if (!(peer->flags & FLAG_PREFER)) {
3456			switch (peer->refclktype) {
3457			case REFCLK_LOCALCLOCK:
3458				if (   current_time > orphwait
3459				    && typelocal == NULL)
3460					typelocal = peer;
3461				continue;
3462
3463			case REFCLK_ACTS:
3464				if (   current_time > orphwait
3465				    && typeacts == NULL)
3466					typeacts = peer;
3467				continue;
3468			}
3469		}
3470#endif /* REFCLOCK */
3471
3472		/*
3473		 * If we get this far, the peer can stay on the
3474		 * island, but does not yet have the immunity
3475		 * idol.
3476		 */
3477		peer->new_status = CTL_PST_SEL_SANE;
3478		f = root_distance(peer);
3479		peers[nlist].peer = peer;
3480		peers[nlist].error = peer->jitter;
3481		peers[nlist].synch = f;
3482		nlist++;
3483
3484		/*
3485		 * Insert each interval endpoint on the unsorted
3486		 * endpoint[] list.
3487		 */
3488		e = peer->offset;
3489		endpoint[nl2].type = -1;	/* lower end */
3490		endpoint[nl2].val = e - f;
3491		nl2++;
3492		endpoint[nl2].type = 1;		/* upper end */
3493		endpoint[nl2].val = e + f;
3494		nl2++;
3495	}
3496	/*
3497	 * Construct sorted indx[] of endpoint[] indexes ordered by
3498	 * offset.
3499	 */
3500	for (i = 0; i < nl2; i++)
3501		indx[i] = i;
3502	for (i = 0; i < nl2; i++) {
3503		endp = endpoint[indx[i]];
3504		e = endp.val;
3505		k = i;
3506		for (j = i + 1; j < nl2; j++) {
3507			endp = endpoint[indx[j]];
3508			if (endp.val < e) {
3509				e = endp.val;
3510				k = j;
3511			}
3512		}
3513		if (k != i) {
3514			j = indx[k];
3515			indx[k] = indx[i];
3516			indx[i] = j;
3517		}
3518	}
3519	for (i = 0; i < nl2; i++)
3520		DPRINTF(3, ("select: endpoint %2d %.6f\n",
3521			endpoint[indx[i]].type, endpoint[indx[i]].val));
3522
3523	/*
3524	 * This is the actual algorithm that cleaves the truechimers
3525	 * from the falsetickers. The original algorithm was described
3526	 * in Keith Marzullo's dissertation, but has been modified for
3527	 * better accuracy.
3528	 *
3529	 * Briefly put, we first assume there are no falsetickers, then
3530	 * scan the candidate list first from the low end upwards and
3531	 * then from the high end downwards. The scans stop when the
3532	 * number of intersections equals the number of candidates less
3533	 * the number of falsetickers. If this doesn't happen for a
3534	 * given number of falsetickers, we bump the number of
3535	 * falsetickers and try again. If the number of falsetickers
3536	 * becomes equal to or greater than half the number of
3537	 * candidates, the Albanians have won the Byzantine wars and
3538	 * correct synchronization is not possible.
3539	 *
3540	 * Here, nlist is the number of candidates and allow is the
3541	 * number of falsetickers. Upon exit, the truechimers are the
3542	 * survivors with offsets not less than low and not greater than
3543	 * high. There may be none of them.
3544	 */
3545	low = 1e9;
3546	high = -1e9;
3547	for (allow = 0; 2 * allow < nlist; allow++) {
3548
3549		/*
3550		 * Bound the interval (low, high) as the smallest
3551		 * interval containing points from the most sources.
3552		 */
3553		n = 0;
3554		for (i = 0; i < nl2; i++) {
3555			low = endpoint[indx[i]].val;
3556			n -= endpoint[indx[i]].type;
3557			if (n >= nlist - allow)
3558				break;
3559		}
3560		n = 0;
3561		for (j = nl2 - 1; j >= 0; j--) {
3562			high = endpoint[indx[j]].val;
3563			n += endpoint[indx[j]].type;
3564			if (n >= nlist - allow)
3565				break;
3566		}
3567
3568		/*
3569		 * If an interval containing truechimers is found, stop.
3570		 * If not, increase the number of falsetickers and go
3571		 * around again.
3572		 */
3573		if (high > low)
3574			break;
3575	}
3576
3577	/*
3578	 * Clustering algorithm. Whittle candidate list of falsetickers,
3579	 * who leave the island immediately. The TRUE peer is always a
3580	 * truechimer. We must leave at least one peer to collect the
3581	 * million bucks.
3582	 *
3583	 * We assert the correct time is contained in the interval, but
3584	 * the best offset estimate for the interval might not be
3585	 * contained in the interval. For this purpose, a truechimer is
3586	 * defined as the midpoint of an interval that overlaps the
3587	 * intersection interval.
3588	 */
3589	j = 0;
3590	for (i = 0; i < nlist; i++) {
3591		double	h;
3592
3593		peer = peers[i].peer;
3594		h = peers[i].synch;
3595		if ((   high <= low
3596		     || peer->offset + h < low
3597		     || peer->offset - h > high
3598		    ) && !(peer->flags & FLAG_TRUE))
3599			continue;
3600
3601#ifdef REFCLOCK
3602		/*
3603		 * Eligible PPS peers must survive the intersection
3604		 * algorithm. Use the first one found, but don't
3605		 * include any of them in the cluster population.
3606		 */
3607		if (peer->flags & FLAG_PPS) {
3608			if (typepps == NULL)
3609				typepps = peer;
3610			if (!(peer->flags & FLAG_TSTAMP_PPS))
3611				continue;
3612		}
3613#endif /* REFCLOCK */
3614
3615		if (j != i)
3616			peers[j] = peers[i];
3617		j++;
3618	}
3619	nlist = j;
3620
3621	/*
3622	 * If no survivors remain at this point, check if the modem
3623	 * driver, local driver or orphan parent in that order. If so,
3624	 * nominate the first one found as the only survivor.
3625	 * Otherwise, give up and leave the island to the rats.
3626	 */
3627	if (nlist == 0) {
3628		peers[0].error = 0;
3629		peers[0].synch = sys_mindisp;
3630#ifdef REFCLOCK
3631		if (typeacts != NULL) {
3632			peers[0].peer = typeacts;
3633			nlist = 1;
3634		} else if (typelocal != NULL) {
3635			peers[0].peer = typelocal;
3636			nlist = 1;
3637		} else
3638#endif /* REFCLOCK */
3639		if (typeorphan != NULL) {
3640			peers[0].peer = typeorphan;
3641			nlist = 1;
3642		}
3643	}
3644
3645	/*
3646	 * Mark the candidates at this point as truechimers.
3647	 */
3648	for (i = 0; i < nlist; i++) {
3649		peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
3650		DPRINTF(2, ("select: survivor %s %f\n",
3651			stoa(&peers[i].peer->srcadr), peers[i].synch));
3652	}
3653
3654	/*
3655	 * Now, vote outliers off the island by select jitter weighted
3656	 * by root distance. Continue voting as long as there are more
3657	 * than sys_minclock survivors and the select jitter of the peer
3658	 * with the worst metric is greater than the minimum peer
3659	 * jitter. Stop if we are about to discard a TRUE or PREFER
3660	 * peer, who of course have the immunity idol.
3661	 */
3662	while (1) {
3663		d = 1e9;
3664		e = -1e9;
3665		g = 0;
3666		k = 0;
3667		for (i = 0; i < nlist; i++) {
3668			if (peers[i].error < d)
3669				d = peers[i].error;
3670			peers[i].seljit = 0;
3671			if (nlist > 1) {
3672				f = 0;
3673				for (j = 0; j < nlist; j++)
3674					f += DIFF(peers[j].peer->offset,
3675					    peers[i].peer->offset);
3676				peers[i].seljit = SQRT(f / (nlist - 1));
3677			}
3678			if (peers[i].seljit * peers[i].synch > e) {
3679				g = peers[i].seljit;
3680				e = peers[i].seljit * peers[i].synch;
3681				k = i;
3682			}
3683		}
3684		g = max(g, LOGTOD(sys_precision));
3685		if (   nlist <= max(1, sys_minclock)
3686		    || g <= d
3687		    || ((FLAG_TRUE | FLAG_PREFER) & peers[k].peer->flags))
3688			break;
3689
3690		DPRINTF(3, ("select: drop %s seljit %.6f jit %.6f\n",
3691			ntoa(&peers[k].peer->srcadr), g, d));
3692		if (nlist > sys_maxclock)
3693			peers[k].peer->new_status = CTL_PST_SEL_EXCESS;
3694		for (j = k + 1; j < nlist; j++)
3695			peers[j - 1] = peers[j];
3696		nlist--;
3697	}
3698
3699	/*
3700	 * What remains is a list usually not greater than sys_minclock
3701	 * peers. Note that unsynchronized peers cannot survive this
3702	 * far.  Count and mark these survivors.
3703	 *
3704	 * While at it, count the number of leap warning bits found.
3705	 * This will be used later to vote the system leap warning bit.
3706	 * If a leap warning bit is found on a reference clock, the vote
3707	 * is always won.
3708	 *
3709	 * Choose the system peer using a hybrid metric composed of the
3710	 * selection jitter scaled by the root distance augmented by
3711	 * stratum scaled by sys_mindisp (.001 by default). The goal of
3712	 * the small stratum factor is to avoid clockhop between a
3713	 * reference clock and a network peer which has a refclock and
3714	 * is using an older ntpd, which does not floor sys_rootdisp at
3715	 * sys_mindisp.
3716	 *
3717	 * In contrast, ntpd 4.2.6 and earlier used stratum primarily
3718	 * in selecting the system peer, using a weight of 1 second of
3719	 * additional root distance per stratum.  This heavy bias is no
3720	 * longer appropriate, as the scaled root distance provides a
3721	 * more rational metric carrying the cumulative error budget.
3722	 */
3723	e = 1e9;
3724	speer = 0;
3725	leap_vote_ins = 0;
3726	leap_vote_del = 0;
3727	for (i = 0; i < nlist; i++) {
3728		peer = peers[i].peer;
3729		peer->unreach = 0;
3730		peer->new_status = CTL_PST_SEL_SYNCCAND;
3731		sys_survivors++;
3732		if (peer->leap == LEAP_ADDSECOND) {
3733			if (peer->flags & FLAG_REFCLOCK)
3734				leap_vote_ins = nlist;
3735			else if (leap_vote_ins < nlist)
3736				leap_vote_ins++;
3737		}
3738		if (peer->leap == LEAP_DELSECOND) {
3739			if (peer->flags & FLAG_REFCLOCK)
3740				leap_vote_del = nlist;
3741			else if (leap_vote_del < nlist)
3742				leap_vote_del++;
3743		}
3744		if (peer->flags & FLAG_PREFER)
3745			sys_prefer = peer;
3746		speermet = peers[i].seljit * peers[i].synch +
3747		    peer->stratum * sys_mindisp;
3748		if (speermet < e) {
3749			e = speermet;
3750			speer = i;
3751		}
3752	}
3753
3754	/*
3755	 * Unless there are at least sys_misane survivors, leave the
3756	 * building dark. Otherwise, do a clockhop dance. Ordinarily,
3757	 * use the selected survivor speer. However, if the current
3758	 * system peer is not speer, stay with the current system peer
3759	 * as long as it doesn't get too old or too ugly.
3760	 */
3761	if (nlist > 0 && nlist >= sys_minsane) {
3762		double	x;
3763
3764		typesystem = peers[speer].peer;
3765		if (osys_peer == NULL || osys_peer == typesystem) {
3766			sys_clockhop = 0;
3767		} else if ((x = fabs(typesystem->offset -
3768		    osys_peer->offset)) < sys_mindisp) {
3769			if (sys_clockhop == 0)
3770				sys_clockhop = sys_mindisp;
3771			else
3772				sys_clockhop *= .5;
3773			DPRINTF(1, ("select: clockhop %d %.6f %.6f\n",
3774				j, x, sys_clockhop));
3775			if (fabs(x) < sys_clockhop)
3776				typesystem = osys_peer;
3777			else
3778				sys_clockhop = 0;
3779		} else {
3780			sys_clockhop = 0;
3781		}
3782	}
3783
3784	/*
3785	 * Mitigation rules of the game. We have the pick of the
3786	 * litter in typesystem if any survivors are left. If
3787	 * there is a prefer peer, use its offset and jitter.
3788	 * Otherwise, use the combined offset and jitter of all kitters.
3789	 */
3790	if (typesystem != NULL) {
3791		if (sys_prefer == NULL) {
3792			typesystem->new_status = CTL_PST_SEL_SYSPEER;
3793			clock_combine(peers, sys_survivors, speer);
3794		} else {
3795			typesystem = sys_prefer;
3796			sys_clockhop = 0;
3797			typesystem->new_status = CTL_PST_SEL_SYSPEER;
3798			sys_offset = typesystem->offset;
3799			sys_jitter = typesystem->jitter;
3800		}
3801		DPRINTF(1, ("select: combine offset %.9f jitter %.9f\n",
3802			sys_offset, sys_jitter));
3803	}
3804#ifdef REFCLOCK
3805	/*
3806	 * If a PPS driver is lit and the combined offset is less than
3807	 * 0.4 s, select the driver as the PPS peer and use its offset
3808	 * and jitter. However, if this is the atom driver, use it only
3809	 * if there is a prefer peer or there are no survivors and none
3810	 * are required.
3811	 */
3812	if (   typepps != NULL
3813	    && fabs(sys_offset) < 0.4
3814	    && (   typepps->refclktype != REFCLK_ATOM_PPS
3815		|| (   typepps->refclktype == REFCLK_ATOM_PPS
3816		    && (   sys_prefer != NULL
3817			|| (typesystem == NULL && sys_minsane == 0))))) {
3818		typesystem = typepps;
3819		sys_clockhop = 0;
3820		typesystem->new_status = CTL_PST_SEL_PPS;
3821		sys_offset = typesystem->offset;
3822		sys_jitter = typesystem->jitter;
3823		DPRINTF(1, ("select: pps offset %.9f jitter %.9f\n",
3824			sys_offset, sys_jitter));
3825	}
3826#endif /* REFCLOCK */
3827
3828	/*
3829	 * If there are no survivors at this point, there is no
3830	 * system peer. If so and this is an old update, keep the
3831	 * current statistics, but do not update the clock.
3832	 */
3833	if (typesystem == NULL) {
3834		if (osys_peer != NULL) {
3835			if (sys_orphwait > 0)
3836				orphwait = current_time + sys_orphwait;
3837			report_event(EVNT_NOPEER, NULL, NULL);
3838		}
3839		sys_peer = NULL;
3840		for (peer = peer_list; peer != NULL; peer = peer->p_link)
3841			peer->status = peer->new_status;
3842		return;
3843	}
3844
3845	/*
3846	 * Do not use old data, as this may mess up the clock discipline
3847	 * stability.
3848	 */
3849	if (typesystem->epoch <= sys_epoch)
3850		return;
3851
3852	/*
3853	 * We have found the alpha male. Wind the clock.
3854	 */
3855	if (osys_peer != typesystem)
3856		report_event(PEVNT_NEWPEER, typesystem, NULL);
3857	for (peer = peer_list; peer != NULL; peer = peer->p_link)
3858		peer->status = peer->new_status;
3859	clock_update(typesystem);
3860}
3861
3862
3863static void
3864clock_combine(
3865	peer_select *	peers,	/* survivor list */
3866	int		npeers,	/* number of survivors */
3867	int		syspeer	/* index of sys.peer */
3868	)
3869{
3870	int	i;
3871	double	x, y, z, w;
3872
3873	y = z = w = 0;
3874	for (i = 0; i < npeers; i++) {
3875		x = 1. / peers[i].synch;
3876		y += x;
3877		z += x * peers[i].peer->offset;
3878		w += x * DIFF(peers[i].peer->offset,
3879		    peers[syspeer].peer->offset);
3880	}
3881	sys_offset = z / y;
3882	sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
3883}
3884
3885
3886/*
3887 * root_distance - compute synchronization distance from peer to root
3888 */
3889static double
3890root_distance(
3891	struct peer *peer	/* peer structure pointer */
3892	)
3893{
3894	double	dtemp;
3895
3896	/*
3897	 * Root Distance (LAMBDA) is defined as:
3898	 * (delta + DELTA)/2 + epsilon + EPSILON + D
3899	 *
3900	 * where:
3901	 *  delta   is the round-trip delay
3902	 *  DELTA   is the root delay
3903	 *  epsilon is the peer dispersion
3904	 *	    + (15 usec each second)
3905	 *  EPSILON is the root dispersion
3906	 *  D       is sys_jitter
3907	 *
3908	 * NB: Think hard about why we are using these values, and what
3909	 * the alternatives are, and the various pros/cons.
3910	 *
3911	 * DLM thinks these are probably the best choices from any of the
3912	 * other worse choices.
3913	 */
3914	dtemp = (peer->delay + peer->rootdelay) / 2
3915		+ peer->disp
3916		  + clock_phi * (current_time - peer->update)
3917		+ peer->rootdisp
3918		+ peer->jitter;
3919	/*
3920	 * Careful squeak here. The value returned must be greater than
3921	 * the minimum root dispersion in order to avoid clockhop with
3922	 * highly precise reference clocks. Note that the root distance
3923	 * cannot exceed the sys_maxdist, as this is the cutoff by the
3924	 * selection algorithm.
3925	 */
3926	if (dtemp < sys_mindisp)
3927		dtemp = sys_mindisp;
3928	return (dtemp);
3929}
3930
3931
3932/*
3933 * peer_xmit - send packet for persistent association.
3934 */
3935static void
3936peer_xmit(
3937	struct peer *peer	/* peer structure pointer */
3938	)
3939{
3940	struct pkt xpkt;	/* transmit packet */
3941	size_t	sendlen, authlen;
3942	keyid_t	xkeyid = 0;	/* transmit key ID */
3943	l_fp	xmt_tx, xmt_ty;
3944
3945	if (!peer->dstadr)	/* drop peers without interface */
3946		return;
3947
3948	xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, peer->version,
3949	    peer->hmode);
3950	xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
3951	xpkt.ppoll = peer->hpoll;
3952	xpkt.precision = sys_precision;
3953	xpkt.refid = sys_refid;
3954	xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
3955	xpkt.rootdisp =  HTONS_FP(DTOUFP(sys_rootdisp));
3956	HTONL_FP(&sys_reftime, &xpkt.reftime);
3957	HTONL_FP(&peer->rec, &xpkt.org);
3958	HTONL_FP(&peer->dst, &xpkt.rec);
3959
3960	/*
3961	 * If the received packet contains a MAC, the transmitted packet
3962	 * is authenticated and contains a MAC. If not, the transmitted
3963	 * packet is not authenticated.
3964	 *
3965	 * It is most important when autokey is in use that the local
3966	 * interface IP address be known before the first packet is
3967	 * sent. Otherwise, it is not possible to compute a correct MAC
3968	 * the recipient will accept. Thus, the I/O semantics have to do
3969	 * a little more work. In particular, the wildcard interface
3970	 * might not be usable.
3971	 */
3972	sendlen = LEN_PKT_NOMAC;
3973	if (
3974#ifdef AUTOKEY
3975	    !(peer->flags & FLAG_SKEY) &&
3976#endif	/* !AUTOKEY */
3977	    peer->keyid == 0) {
3978
3979		/*
3980		 * Transmit a-priori timestamps
3981		 */
3982		get_systime(&xmt_tx);
3983		if (peer->flip == 0) {	/* basic mode */
3984			peer->aorg = xmt_tx;
3985			HTONL_FP(&xmt_tx, &xpkt.xmt);
3986		} else {		/* interleaved modes */
3987			if (peer->hmode == MODE_BROADCAST) { /* bcst */
3988				HTONL_FP(&xmt_tx, &xpkt.xmt);
3989				if (peer->flip > 0)
3990					HTONL_FP(&peer->borg,
3991					    &xpkt.org);
3992				else
3993					HTONL_FP(&peer->aorg,
3994					    &xpkt.org);
3995			} else {	/* symmetric */
3996				if (peer->flip > 0)
3997					HTONL_FP(&peer->borg,
3998					    &xpkt.xmt);
3999				else
4000					HTONL_FP(&peer->aorg,
4001					    &xpkt.xmt);
4002			}
4003		}
4004		peer->t21_bytes = sendlen;
4005		sendpkt(&peer->srcadr, peer->dstadr,
4006			sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4007			&xpkt, sendlen);
4008		peer->sent++;
4009		peer->throttle += (1 << peer->minpoll) - 2;
4010
4011		/*
4012		 * Capture a-posteriori timestamps
4013		 */
4014		get_systime(&xmt_ty);
4015		if (peer->flip != 0) {		/* interleaved modes */
4016			if (peer->flip > 0)
4017				peer->aorg = xmt_ty;
4018			else
4019				peer->borg = xmt_ty;
4020			peer->flip = -peer->flip;
4021		}
4022		L_SUB(&xmt_ty, &xmt_tx);
4023		LFPTOD(&xmt_ty, peer->xleave);
4024		DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d len %zu xmt %#010x.%08x\n",
4025			    current_time,
4026			    peer->dstadr ? stoa(&peer->dstadr->sin) : "-",
4027			    stoa(&peer->srcadr), peer->hmode, sendlen,
4028			    xmt_tx.l_ui, xmt_tx.l_uf));
4029		return;
4030	}
4031
4032	/*
4033	 * Authentication is enabled, so the transmitted packet must be
4034	 * authenticated. If autokey is enabled, fuss with the various
4035	 * modes; otherwise, symmetric key cryptography is used.
4036	 */
4037#ifdef AUTOKEY
4038	if (peer->flags & FLAG_SKEY) {
4039		struct exten *exten;	/* extension field */
4040
4041		/*
4042		 * The Public Key Dance (PKD): Cryptographic credentials
4043		 * are contained in extension fields, each including a
4044		 * 4-octet length/code word followed by a 4-octet
4045		 * association ID and optional additional data. Optional
4046		 * data includes a 4-octet data length field followed by
4047		 * the data itself. Request messages are sent from a
4048		 * configured association; response messages can be sent
4049		 * from a configured association or can take the fast
4050		 * path without ever matching an association. Response
4051		 * messages have the same code as the request, but have
4052		 * a response bit and possibly an error bit set. In this
4053		 * implementation, a message may contain no more than
4054		 * one command and one or more responses.
4055		 *
4056		 * Cryptographic session keys include both a public and
4057		 * a private componet. Request and response messages
4058		 * using extension fields are always sent with the
4059		 * private component set to zero. Packets without
4060		 * extension fields indlude the private component when
4061		 * the session key is generated.
4062		 */
4063		while (1) {
4064
4065			/*
4066			 * Allocate and initialize a keylist if not
4067			 * already done. Then, use the list in inverse
4068			 * order, discarding keys once used. Keep the
4069			 * latest key around until the next one, so
4070			 * clients can use client/server packets to
4071			 * compute propagation delay.
4072			 *
4073			 * Note that once a key is used from the list,
4074			 * it is retained in the key cache until the
4075			 * next key is used. This is to allow a client
4076			 * to retrieve the encrypted session key
4077			 * identifier to verify authenticity.
4078			 *
4079			 * If for some reason a key is no longer in the
4080			 * key cache, a birthday has happened or the key
4081			 * has expired, so the pseudo-random sequence is
4082			 * broken. In that case, purge the keylist and
4083			 * regenerate it.
4084			 */
4085			if (peer->keynumber == 0)
4086				make_keylist(peer, peer->dstadr);
4087			else
4088				peer->keynumber--;
4089			xkeyid = peer->keylist[peer->keynumber];
4090			if (authistrusted(xkeyid))
4091				break;
4092			else
4093				key_expire(peer);
4094		}
4095		peer->keyid = xkeyid;
4096		exten = NULL;
4097		switch (peer->hmode) {
4098
4099		/*
4100		 * In broadcast server mode the autokey values are
4101		 * required by the broadcast clients. Push them when a
4102		 * new keylist is generated; otherwise, push the
4103		 * association message so the client can request them at
4104		 * other times.
4105		 */
4106		case MODE_BROADCAST:
4107			if (peer->flags & FLAG_ASSOC)
4108				exten = crypto_args(peer, CRYPTO_AUTO |
4109				    CRYPTO_RESP, peer->associd, NULL);
4110			else
4111				exten = crypto_args(peer, CRYPTO_ASSOC |
4112				    CRYPTO_RESP, peer->associd, NULL);
4113			break;
4114
4115		/*
4116		 * In symmetric modes the parameter, certificate,
4117		 * identity, cookie and autokey exchanges are
4118		 * required. The leapsecond exchange is optional. But, a
4119		 * peer will not believe the other peer until the other
4120		 * peer has synchronized, so the certificate exchange
4121		 * might loop until then. If a peer finds a broken
4122		 * autokey sequence, it uses the autokey exchange to
4123		 * retrieve the autokey values. In any case, if a new
4124		 * keylist is generated, the autokey values are pushed.
4125		 */
4126		case MODE_ACTIVE:
4127		case MODE_PASSIVE:
4128
4129			/*
4130			 * Parameter, certificate and identity.
4131			 */
4132			if (!peer->crypto)
4133				exten = crypto_args(peer, CRYPTO_ASSOC,
4134				    peer->associd, hostval.ptr);
4135			else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4136				exten = crypto_args(peer, CRYPTO_CERT,
4137				    peer->associd, peer->issuer);
4138			else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4139				exten = crypto_args(peer,
4140				    crypto_ident(peer), peer->associd,
4141				    NULL);
4142
4143			/*
4144			 * Cookie and autokey. We request the cookie
4145			 * only when the this peer and the other peer
4146			 * are synchronized. But, this peer needs the
4147			 * autokey values when the cookie is zero. Any
4148			 * time we regenerate the key list, we offer the
4149			 * autokey values without being asked. If for
4150			 * some reason either peer finds a broken
4151			 * autokey sequence, the autokey exchange is
4152			 * used to retrieve the autokey values.
4153			 */
4154			else if (   sys_leap != LEAP_NOTINSYNC
4155				 && peer->leap != LEAP_NOTINSYNC
4156				 && !(peer->crypto & CRYPTO_FLAG_COOK))
4157				exten = crypto_args(peer, CRYPTO_COOK,
4158				    peer->associd, NULL);
4159			else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4160				exten = crypto_args(peer, CRYPTO_AUTO,
4161				    peer->associd, NULL);
4162			else if (   peer->flags & FLAG_ASSOC
4163				 && peer->crypto & CRYPTO_FLAG_SIGN)
4164				exten = crypto_args(peer, CRYPTO_AUTO |
4165				    CRYPTO_RESP, peer->assoc, NULL);
4166
4167			/*
4168			 * Wait for clock sync, then sign the
4169			 * certificate and retrieve the leapsecond
4170			 * values.
4171			 */
4172			else if (sys_leap == LEAP_NOTINSYNC)
4173				break;
4174
4175			else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4176				exten = crypto_args(peer, CRYPTO_SIGN,
4177				    peer->associd, hostval.ptr);
4178			else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4179				exten = crypto_args(peer, CRYPTO_LEAP,
4180				    peer->associd, NULL);
4181			break;
4182
4183		/*
4184		 * In client mode the parameter, certificate, identity,
4185		 * cookie and sign exchanges are required. The
4186		 * leapsecond exchange is optional. If broadcast client
4187		 * mode the same exchanges are required, except that the
4188		 * autokey exchange is substitutes for the cookie
4189		 * exchange, since the cookie is always zero. If the
4190		 * broadcast client finds a broken autokey sequence, it
4191		 * uses the autokey exchange to retrieve the autokey
4192		 * values.
4193		 */
4194		case MODE_CLIENT:
4195
4196			/*
4197			 * Parameter, certificate and identity.
4198			 */
4199			if (!peer->crypto)
4200				exten = crypto_args(peer, CRYPTO_ASSOC,
4201				    peer->associd, hostval.ptr);
4202			else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4203				exten = crypto_args(peer, CRYPTO_CERT,
4204				    peer->associd, peer->issuer);
4205			else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4206				exten = crypto_args(peer,
4207				    crypto_ident(peer), peer->associd,
4208				    NULL);
4209
4210			/*
4211			 * Cookie and autokey. These are requests, but
4212			 * we use the peer association ID with autokey
4213			 * rather than our own.
4214			 */
4215			else if (!(peer->crypto & CRYPTO_FLAG_COOK))
4216				exten = crypto_args(peer, CRYPTO_COOK,
4217				    peer->associd, NULL);
4218			else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4219				exten = crypto_args(peer, CRYPTO_AUTO,
4220				    peer->assoc, NULL);
4221
4222			/*
4223			 * Wait for clock sync, then sign the
4224			 * certificate and retrieve the leapsecond
4225			 * values.
4226			 */
4227			else if (sys_leap == LEAP_NOTINSYNC)
4228				break;
4229
4230			else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4231				exten = crypto_args(peer, CRYPTO_SIGN,
4232				    peer->associd, hostval.ptr);
4233			else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4234				exten = crypto_args(peer, CRYPTO_LEAP,
4235				    peer->associd, NULL);
4236			break;
4237		}
4238
4239		/*
4240		 * Add a queued extension field if present. This is
4241		 * always a request message, so the reply ID is already
4242		 * in the message. If an error occurs, the error bit is
4243		 * lit in the response.
4244		 */
4245		if (peer->cmmd != NULL) {
4246			u_int32 temp32;
4247
4248			temp32 = CRYPTO_RESP;
4249			peer->cmmd->opcode |= htonl(temp32);
4250			sendlen += crypto_xmit(peer, &xpkt, NULL,
4251			    sendlen, peer->cmmd, 0);
4252			free(peer->cmmd);
4253			peer->cmmd = NULL;
4254		}
4255
4256		/*
4257		 * Add an extension field created above. All but the
4258		 * autokey response message are request messages.
4259		 */
4260		if (exten != NULL) {
4261			if (exten->opcode != 0)
4262				sendlen += crypto_xmit(peer, &xpkt,
4263				    NULL, sendlen, exten, 0);
4264			free(exten);
4265		}
4266
4267		/*
4268		 * Calculate the next session key. Since extension
4269		 * fields are present, the cookie value is zero.
4270		 */
4271		if (sendlen > (int)LEN_PKT_NOMAC) {
4272			session_key(&peer->dstadr->sin, &peer->srcadr,
4273			    xkeyid, 0, 2);
4274		}
4275	}
4276#endif	/* AUTOKEY */
4277
4278	/*
4279	 * Transmit a-priori timestamps
4280	 */
4281	get_systime(&xmt_tx);
4282	if (peer->flip == 0) {		/* basic mode */
4283		peer->aorg = xmt_tx;
4284		HTONL_FP(&xmt_tx, &xpkt.xmt);
4285	} else {			/* interleaved modes */
4286		if (peer->hmode == MODE_BROADCAST) { /* bcst */
4287			HTONL_FP(&xmt_tx, &xpkt.xmt);
4288			if (peer->flip > 0)
4289				HTONL_FP(&peer->borg, &xpkt.org);
4290			else
4291				HTONL_FP(&peer->aorg, &xpkt.org);
4292		} else {		/* symmetric */
4293			if (peer->flip > 0)
4294				HTONL_FP(&peer->borg, &xpkt.xmt);
4295			else
4296				HTONL_FP(&peer->aorg, &xpkt.xmt);
4297		}
4298	}
4299	xkeyid = peer->keyid;
4300	authlen = authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4301	if (authlen == 0) {
4302		report_event(PEVNT_AUTH, peer, "no key");
4303		peer->flash |= TEST5;		/* auth error */
4304		peer->badauth++;
4305		return;
4306	}
4307	sendlen += authlen;
4308#ifdef AUTOKEY
4309	if (xkeyid > NTP_MAXKEY)
4310		authtrust(xkeyid, 0);
4311#endif	/* AUTOKEY */
4312	if (sendlen > sizeof(xpkt)) {
4313		msyslog(LOG_ERR, "peer_xmit: buffer overflow %zu", sendlen);
4314		exit (-1);
4315	}
4316	peer->t21_bytes = sendlen;
4317	sendpkt(&peer->srcadr, peer->dstadr,
4318		sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4319		&xpkt, sendlen);
4320	peer->sent++;
4321	peer->throttle += (1 << peer->minpoll) - 2;
4322
4323	/*
4324	 * Capture a-posteriori timestamps
4325	 */
4326	get_systime(&xmt_ty);
4327	if (peer->flip != 0) {			/* interleaved modes */
4328		if (peer->flip > 0)
4329			peer->aorg = xmt_ty;
4330		else
4331			peer->borg = xmt_ty;
4332		peer->flip = -peer->flip;
4333	}
4334	L_SUB(&xmt_ty, &xmt_tx);
4335	LFPTOD(&xmt_ty, peer->xleave);
4336#ifdef AUTOKEY
4337	DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu index %d\n",
4338		    current_time, latoa(peer->dstadr),
4339		    ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen,
4340		    peer->keynumber));
4341#else	/* !AUTOKEY follows */
4342	DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu\n",
4343		    current_time, peer->dstadr ?
4344		    ntoa(&peer->dstadr->sin) : "-",
4345		    ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
4346#endif	/* !AUTOKEY */
4347
4348	return;
4349}
4350
4351
4352#ifdef LEAP_SMEAR
4353
4354static void
4355leap_smear_add_offs(
4356	l_fp *t,
4357	l_fp *t_recv
4358	)
4359{
4360
4361	L_ADD(t, &leap_smear.offset);
4362
4363	/*
4364	** XXX: Should the smear be added to the root dispersion?
4365	*/
4366
4367	return;
4368}
4369
4370#endif /* LEAP_SMEAR */
4371
4372
4373/*
4374 * fast_xmit - Send packet for nonpersistent association. Note that
4375 * neither the source or destination can be a broadcast address.
4376 */
4377static void
4378fast_xmit(
4379	struct recvbuf *rbufp,	/* receive packet pointer */
4380	int	xmode,		/* receive mode */
4381	keyid_t	xkeyid,		/* transmit key ID */
4382	int	flags		/* restrict mask */
4383	)
4384{
4385	struct pkt xpkt;	/* transmit packet structure */
4386	struct pkt *rpkt;	/* receive packet structure */
4387	l_fp	xmt_tx, xmt_ty;
4388	size_t	sendlen;
4389#ifdef AUTOKEY
4390	u_int32	temp32;
4391#endif
4392
4393	/*
4394	 * Initialize transmit packet header fields from the receive
4395	 * buffer provided. We leave the fields intact as received, but
4396	 * set the peer poll at the maximum of the receive peer poll and
4397	 * the system minimum poll (ntp_minpoll). This is for KoD rate
4398	 * control and not strictly specification compliant, but doesn't
4399	 * break anything.
4400	 *
4401	 * If the gazinta was from a multicast address, the gazoutta
4402	 * must go out another way.
4403	 */
4404	rpkt = &rbufp->recv_pkt;
4405	if (rbufp->dstadr->flags & INT_MCASTOPEN)
4406		rbufp->dstadr = findinterface(&rbufp->recv_srcadr);
4407
4408	/*
4409	 * If this is a kiss-o'-death (KoD) packet, show leap
4410	 * unsynchronized, stratum zero, reference ID the four-character
4411	 * kiss code and system root delay. Note we don't reveal the
4412	 * local time, so these packets can't be used for
4413	 * synchronization.
4414	 */
4415	if (flags & RES_KOD) {
4416		sys_kodsent++;
4417		xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
4418		    PKT_VERSION(rpkt->li_vn_mode), xmode);
4419		xpkt.stratum = STRATUM_PKT_UNSPEC;
4420		xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4421		xpkt.precision = rpkt->precision;
4422		memcpy(&xpkt.refid, "RATE", 4);
4423		xpkt.rootdelay = rpkt->rootdelay;
4424		xpkt.rootdisp = rpkt->rootdisp;
4425		xpkt.reftime = rpkt->reftime;
4426		xpkt.org = rpkt->xmt;
4427		xpkt.rec = rpkt->xmt;
4428		xpkt.xmt = rpkt->xmt;
4429
4430	/*
4431	 * This is a normal packet. Use the system variables.
4432	 */
4433	} else {
4434#ifdef LEAP_SMEAR
4435		/*
4436		 * Make copies of the variables which can be affected by smearing.
4437		 */
4438		l_fp this_ref_time;
4439		l_fp this_recv_time;
4440#endif
4441
4442		/*
4443		 * If we are inside the leap smear interval we add the current smear offset to
4444		 * the packet receive time, to the packet transmit time, and eventually to the
4445		 * reftime to make sure the reftime isn't later than the transmit/receive times.
4446		 */
4447		xpkt.li_vn_mode = PKT_LI_VN_MODE(xmt_leap,
4448		    PKT_VERSION(rpkt->li_vn_mode), xmode);
4449
4450		xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4451		xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4452		xpkt.precision = sys_precision;
4453		xpkt.refid = sys_refid;
4454		xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4455		xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4456
4457#ifdef LEAP_SMEAR
4458		this_ref_time = sys_reftime;
4459		if (leap_smear.in_progress) {
4460			leap_smear_add_offs(&this_ref_time, NULL);
4461			xpkt.refid = convertLFPToRefID(leap_smear.offset);
4462			DPRINTF(2, ("fast_xmit: leap_smear.in_progress: refid %8x, smear %s\n",
4463				ntohl(xpkt.refid),
4464				lfptoa(&leap_smear.offset, 8)
4465				));
4466		}
4467		HTONL_FP(&this_ref_time, &xpkt.reftime);
4468#else
4469		HTONL_FP(&sys_reftime, &xpkt.reftime);
4470#endif
4471
4472		xpkt.org = rpkt->xmt;
4473
4474#ifdef LEAP_SMEAR
4475		this_recv_time = rbufp->recv_time;
4476		if (leap_smear.in_progress)
4477			leap_smear_add_offs(&this_recv_time, NULL);
4478		HTONL_FP(&this_recv_time, &xpkt.rec);
4479#else
4480		HTONL_FP(&rbufp->recv_time, &xpkt.rec);
4481#endif
4482
4483		get_systime(&xmt_tx);
4484#ifdef LEAP_SMEAR
4485		if (leap_smear.in_progress)
4486			leap_smear_add_offs(&xmt_tx, &this_recv_time);
4487#endif
4488		HTONL_FP(&xmt_tx, &xpkt.xmt);
4489	}
4490
4491#ifdef HAVE_NTP_SIGND
4492	if (flags & RES_MSSNTP) {
4493		send_via_ntp_signd(rbufp, xmode, xkeyid, flags, &xpkt);
4494		return;
4495	}
4496#endif /* HAVE_NTP_SIGND */
4497
4498	/*
4499	 * If the received packet contains a MAC, the transmitted packet
4500	 * is authenticated and contains a MAC. If not, the transmitted
4501	 * packet is not authenticated.
4502	 */
4503	sendlen = LEN_PKT_NOMAC;
4504	if (rbufp->recv_length == sendlen) {
4505		sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt,
4506		    sendlen);
4507		DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d len %lu\n",
4508			    current_time, stoa(&rbufp->dstadr->sin),
4509			    stoa(&rbufp->recv_srcadr), xmode,
4510			    (u_long)sendlen));
4511		return;
4512	}
4513
4514	/*
4515	 * The received packet contains a MAC, so the transmitted packet
4516	 * must be authenticated. For symmetric key cryptography, use
4517	 * the predefined and trusted symmetric keys to generate the
4518	 * cryptosum. For autokey cryptography, use the server private
4519	 * value to generate the cookie, which is unique for every
4520	 * source-destination-key ID combination.
4521	 */
4522#ifdef AUTOKEY
4523	if (xkeyid > NTP_MAXKEY) {
4524		keyid_t cookie;
4525
4526		/*
4527		 * The only way to get here is a reply to a legitimate
4528		 * client request message, so the mode must be
4529		 * MODE_SERVER. If an extension field is present, there
4530		 * can be only one and that must be a command. Do what
4531		 * needs, but with private value of zero so the poor
4532		 * jerk can decode it. If no extension field is present,
4533		 * use the cookie to generate the session key.
4534		 */
4535		cookie = session_key(&rbufp->recv_srcadr,
4536		    &rbufp->dstadr->sin, 0, sys_private, 0);
4537		if ((size_t)rbufp->recv_length > sendlen + MAX_MAC_LEN) {
4538			session_key(&rbufp->dstadr->sin,
4539			    &rbufp->recv_srcadr, xkeyid, 0, 2);
4540			temp32 = CRYPTO_RESP;
4541			rpkt->exten[0] |= htonl(temp32);
4542			sendlen += crypto_xmit(NULL, &xpkt, rbufp,
4543			    sendlen, (struct exten *)rpkt->exten,
4544			    cookie);
4545		} else {
4546			session_key(&rbufp->dstadr->sin,
4547			    &rbufp->recv_srcadr, xkeyid, cookie, 2);
4548		}
4549	}
4550#endif	/* AUTOKEY */
4551	get_systime(&xmt_tx);
4552	sendlen += authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4553#ifdef AUTOKEY
4554	if (xkeyid > NTP_MAXKEY)
4555		authtrust(xkeyid, 0);
4556#endif	/* AUTOKEY */
4557	sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt, sendlen);
4558	get_systime(&xmt_ty);
4559	L_SUB(&xmt_ty, &xmt_tx);
4560	sys_authdelay = xmt_ty;
4561	DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d keyid %08x len %lu\n",
4562		    current_time, ntoa(&rbufp->dstadr->sin),
4563		    ntoa(&rbufp->recv_srcadr), xmode, xkeyid,
4564		    (u_long)sendlen));
4565}
4566
4567
4568/*
4569 * pool_xmit - resolve hostname or send unicast solicitation for pool.
4570 */
4571static void
4572pool_xmit(
4573	struct peer *pool	/* pool solicitor association */
4574	)
4575{
4576#ifdef WORKER
4577	struct pkt		xpkt;	/* transmit packet structure */
4578	struct addrinfo		hints;
4579	int			rc;
4580	struct interface *	lcladr;
4581	sockaddr_u *		rmtadr;
4582	r4addr			r4a;
4583	int			restrict_mask;
4584	struct peer *		p;
4585	l_fp			xmt_tx;
4586
4587	if (NULL == pool->ai) {
4588		if (pool->addrs != NULL) {
4589			/* free() is used with copy_addrinfo_list() */
4590			free(pool->addrs);
4591			pool->addrs = NULL;
4592		}
4593		ZERO(hints);
4594		hints.ai_family = AF(&pool->srcadr);
4595		hints.ai_socktype = SOCK_DGRAM;
4596		hints.ai_protocol = IPPROTO_UDP;
4597		/* ignore getaddrinfo_sometime() errors, we will retry */
4598		rc = getaddrinfo_sometime(
4599			pool->hostname,
4600			"ntp",
4601			&hints,
4602			0,			/* no retry */
4603			&pool_name_resolved,
4604			(void *)(intptr_t)pool->associd);
4605		if (!rc)
4606			DPRINTF(1, ("pool DNS lookup %s started\n",
4607				pool->hostname));
4608		else
4609			msyslog(LOG_ERR,
4610				"unable to start pool DNS %s: %m",
4611				pool->hostname);
4612		return;
4613	}
4614
4615	do {
4616		/* copy_addrinfo_list ai_addr points to a sockaddr_u */
4617		rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
4618		pool->ai = pool->ai->ai_next;
4619		p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT, 0, NULL);
4620	} while (p != NULL && pool->ai != NULL);
4621	if (p != NULL)
4622		return;	/* out of addresses, re-query DNS next poll */
4623	restrictions(rmtadr, &r4a);
4624	restrict_mask = r4a.rflags;
4625	if (RES_FLAGS & restrict_mask)
4626		restrict_source(rmtadr, 0,
4627				current_time + POOL_SOLICIT_WINDOW + 1);
4628	lcladr = findinterface(rmtadr);
4629	memset(&xpkt, 0, sizeof(xpkt));
4630	xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, pool->version,
4631					 MODE_CLIENT);
4632	xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4633	xpkt.ppoll = pool->hpoll;
4634	xpkt.precision = sys_precision;
4635	xpkt.refid = sys_refid;
4636	xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4637	xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4638	HTONL_FP(&sys_reftime, &xpkt.reftime);
4639	get_systime(&xmt_tx);
4640	pool->aorg = xmt_tx;
4641	HTONL_FP(&xmt_tx, &xpkt.xmt);
4642	sendpkt(rmtadr, lcladr,
4643		sys_ttl[(pool->ttl >= sys_ttlmax) ? sys_ttlmax : pool->ttl],
4644		&xpkt, LEN_PKT_NOMAC);
4645	pool->sent++;
4646	pool->throttle += (1 << pool->minpoll) - 2;
4647	DPRINTF(1, ("pool_xmit: at %ld %s->%s pool\n",
4648		    current_time, latoa(lcladr), stoa(rmtadr)));
4649	msyslog(LOG_INFO, "Soliciting pool server %s", stoa(rmtadr));
4650#endif	/* WORKER */
4651}
4652
4653
4654#ifdef AUTOKEY
4655	/*
4656	 * group_test - test if this is the same group
4657	 *
4658	 * host		assoc		return		action
4659	 * none		none		0		mobilize *
4660	 * none		group		0		mobilize *
4661	 * group	none		0		mobilize *
4662	 * group	group		1		mobilize
4663	 * group	different	1		ignore
4664	 * * ignore if notrust
4665	 */
4666int
4667group_test(
4668	char	*grp,
4669	char	*ident
4670	)
4671{
4672	if (grp == NULL)
4673		return (0);
4674
4675	if (strcmp(grp, sys_groupname) == 0)
4676		return (0);
4677
4678	if (ident == NULL)
4679		return (1);
4680
4681	if (strcmp(grp, ident) == 0)
4682		return (0);
4683
4684	return (1);
4685}
4686#endif /* AUTOKEY */
4687
4688
4689#ifdef WORKER
4690void
4691pool_name_resolved(
4692	int			rescode,
4693	int			gai_errno,
4694	void *			context,
4695	const char *		name,
4696	const char *		service,
4697	const struct addrinfo *	hints,
4698	const struct addrinfo *	res
4699	)
4700{
4701	struct peer *	pool;	/* pool solicitor association */
4702	associd_t	assoc;
4703
4704	if (rescode) {
4705		msyslog(LOG_ERR,
4706			"error resolving pool %s: %s (%d)",
4707			name, gai_strerror(rescode), rescode);
4708		return;
4709	}
4710
4711	assoc = (associd_t)(intptr_t)context;
4712	pool = findpeerbyassoc(assoc);
4713	if (NULL == pool) {
4714		msyslog(LOG_ERR,
4715			"Could not find assoc %u for pool DNS %s",
4716			assoc, name);
4717		return;
4718	}
4719	DPRINTF(1, ("pool DNS %s completed\n", name));
4720	pool->addrs = copy_addrinfo_list(res);
4721	pool->ai = pool->addrs;
4722	pool_xmit(pool);
4723
4724}
4725#endif	/* WORKER */
4726
4727
4728#ifdef AUTOKEY
4729/*
4730 * key_expire - purge the key list
4731 */
4732void
4733key_expire(
4734	struct peer *peer	/* peer structure pointer */
4735	)
4736{
4737	int i;
4738
4739	if (peer->keylist != NULL) {
4740		for (i = 0; i <= peer->keynumber; i++)
4741			authtrust(peer->keylist[i], 0);
4742		free(peer->keylist);
4743		peer->keylist = NULL;
4744	}
4745	value_free(&peer->sndval);
4746	peer->keynumber = 0;
4747	peer->flags &= ~FLAG_ASSOC;
4748	DPRINTF(1, ("key_expire: at %lu associd %d\n", current_time,
4749		    peer->associd));
4750}
4751#endif	/* AUTOKEY */
4752
4753
4754/*
4755 * local_refid(peer) - check peer refid to avoid selecting peers
4756 *		       currently synced to this ntpd.
4757 */
4758static int
4759local_refid(
4760	struct peer *	p
4761	)
4762{
4763	endpt *	unicast_ep;
4764
4765	if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
4766		unicast_ep = p->dstadr;
4767	else
4768		unicast_ep = findinterface(&p->srcadr);
4769
4770	if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
4771		return TRUE;
4772	else
4773		return FALSE;
4774}
4775
4776
4777/*
4778 * Determine if the peer is unfit for synchronization
4779 *
4780 * A peer is unfit for synchronization if
4781 * > TEST10 bad leap or stratum below floor or at or above ceiling
4782 * > TEST11 root distance exceeded for remote peer
4783 * > TEST12 a direct or indirect synchronization loop would form
4784 * > TEST13 unreachable or noselect
4785 */
4786int				/* FALSE if fit, TRUE if unfit */
4787peer_unfit(
4788	struct peer *peer	/* peer structure pointer */
4789	)
4790{
4791	int	rval = 0;
4792
4793	/*
4794	 * A stratum error occurs if (1) the server has never been
4795	 * synchronized, (2) the server stratum is below the floor or
4796	 * greater than or equal to the ceiling.
4797	 */
4798	if (   peer->leap == LEAP_NOTINSYNC
4799	    || peer->stratum < sys_floor
4800	    || peer->stratum >= sys_ceiling) {
4801		rval |= TEST10;		/* bad synch or stratum */
4802	}
4803
4804	/*
4805	 * A distance error for a remote peer occurs if the root
4806	 * distance is greater than or equal to the distance threshold
4807	 * plus the increment due to one host poll interval.
4808	 */
4809	if (   !(peer->flags & FLAG_REFCLOCK)
4810	    && root_distance(peer) >= sys_maxdist
4811				      + clock_phi * ULOGTOD(peer->hpoll)) {
4812		rval |= TEST11;		/* distance exceeded */
4813	}
4814
4815	/*
4816	 * A loop error occurs if the remote peer is synchronized to the
4817	 * local peer or if the remote peer is synchronized to the same
4818	 * server as the local peer but only if the remote peer is
4819	 * neither a reference clock nor an orphan.
4820	 */
4821	if (peer->stratum > 1 && local_refid(peer)) {
4822		rval |= TEST12;		/* synchronization loop */
4823	}
4824
4825	/*
4826	 * An unreachable error occurs if the server is unreachable or
4827	 * the noselect bit is set.
4828	 */
4829	if (!peer->reach || (peer->flags & FLAG_NOSELECT)) {
4830		rval |= TEST13;		/* unreachable */
4831	}
4832
4833	peer->flash &= ~PEER_TEST_MASK;
4834	peer->flash |= rval;
4835	return (rval);
4836}
4837
4838
4839/*
4840 * Find the precision of this particular machine
4841 */
4842#define MINSTEP		20e-9	/* minimum clock increment (s) */
4843#define MAXSTEP		1	/* maximum clock increment (s) */
4844#define MINCHANGES	12	/* minimum number of step samples */
4845#define MAXLOOPS	((int)(1. / MINSTEP))	/* avoid infinite loop */
4846
4847/*
4848 * This routine measures the system precision defined as the minimum of
4849 * a sequence of differences between successive readings of the system
4850 * clock. However, if a difference is less than MINSTEP, the clock has
4851 * been read more than once during a clock tick and the difference is
4852 * ignored. We set MINSTEP greater than zero in case something happens
4853 * like a cache miss, and to tolerate underlying system clocks which
4854 * ensure each reading is strictly greater than prior readings while
4855 * using an underlying stepping (not interpolated) clock.
4856 *
4857 * sys_tick and sys_precision represent the time to read the clock for
4858 * systems with high-precision clocks, and the tick interval or step
4859 * size for lower-precision stepping clocks.
4860 *
4861 * This routine also measures the time to read the clock on stepping
4862 * system clocks by counting the number of readings between changes of
4863 * the underlying clock.  With either type of clock, the minimum time
4864 * to read the clock is saved as sys_fuzz, and used to ensure the
4865 * get_systime() readings always increase and are fuzzed below sys_fuzz.
4866 */
4867void
4868measure_precision(void)
4869{
4870	/*
4871	 * With sys_fuzz set to zero, get_systime() fuzzing of low bits
4872	 * is effectively disabled.  trunc_os_clock is FALSE to disable
4873	 * get_ostime() simulation of a low-precision system clock.
4874	 */
4875	set_sys_fuzz(0.);
4876	trunc_os_clock = FALSE;
4877	measured_tick = measure_tick_fuzz();
4878	set_sys_tick_precision(measured_tick);
4879	msyslog(LOG_INFO, "proto: precision = %.3f usec (%d)",
4880		sys_tick * 1e6, sys_precision);
4881	if (sys_fuzz < sys_tick) {
4882		msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec",
4883			sys_fuzz * 1e6);
4884	}
4885}
4886
4887
4888/*
4889 * measure_tick_fuzz()
4890 *
4891 * measures the minimum time to read the clock (stored in sys_fuzz)
4892 * and returns the tick, the larger of the minimum increment observed
4893 * between successive clock readings and the time to read the clock.
4894 */
4895double
4896measure_tick_fuzz(void)
4897{
4898	l_fp	minstep;	/* MINSTEP as l_fp */
4899	l_fp	val;		/* current seconds fraction */
4900	l_fp	last;		/* last seconds fraction */
4901	l_fp	ldiff;		/* val - last */
4902	double	tick;		/* computed tick value */
4903	double	diff;
4904	long	repeats;
4905	long	max_repeats;
4906	int	changes;
4907	int	i;		/* log2 precision */
4908
4909	tick = MAXSTEP;
4910	max_repeats = 0;
4911	repeats = 0;
4912	changes = 0;
4913	DTOLFP(MINSTEP, &minstep);
4914	get_systime(&last);
4915	for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) {
4916		get_systime(&val);
4917		ldiff = val;
4918		L_SUB(&ldiff, &last);
4919		last = val;
4920		if (L_ISGT(&ldiff, &minstep)) {
4921			max_repeats = max(repeats, max_repeats);
4922			repeats = 0;
4923			changes++;
4924			LFPTOD(&ldiff, diff);
4925			tick = min(diff, tick);
4926		} else {
4927			repeats++;
4928		}
4929	}
4930	if (changes < MINCHANGES) {
4931		msyslog(LOG_ERR, "Fatal error: precision could not be measured (MINSTEP too large?)");
4932		exit(1);
4933	}
4934
4935	if (0 == max_repeats) {
4936		set_sys_fuzz(tick);
4937	} else {
4938		set_sys_fuzz(tick / max_repeats);
4939	}
4940
4941	return tick;
4942}
4943
4944
4945void
4946set_sys_tick_precision(
4947	double tick
4948	)
4949{
4950	int i;
4951
4952	if (tick > 1.) {
4953		msyslog(LOG_ERR,
4954			"unsupported tick %.3f > 1s ignored", tick);
4955		return;
4956	}
4957	if (tick < measured_tick) {
4958		msyslog(LOG_ERR,
4959			"proto: tick %.3f less than measured tick %.3f, ignored",
4960			tick, measured_tick);
4961		return;
4962	} else if (tick > measured_tick) {
4963		trunc_os_clock = TRUE;
4964		msyslog(LOG_NOTICE,
4965			"proto: truncating system clock to multiples of %.9f",
4966			tick);
4967	}
4968	sys_tick = tick;
4969
4970	/*
4971	 * Find the nearest power of two.
4972	 */
4973	for (i = 0; tick <= 1; i--)
4974		tick *= 2;
4975	if (tick - 1 > 1 - tick / 2)
4976		i++;
4977
4978	sys_precision = (s_char)i;
4979}
4980
4981
4982/*
4983 * init_proto - initialize the protocol module's data
4984 */
4985void
4986init_proto(void)
4987{
4988	l_fp	dummy;
4989	int	i;
4990
4991	/*
4992	 * Fill in the sys_* stuff.  Default is don't listen to
4993	 * broadcasting, require authentication.
4994	 */
4995	set_sys_leap(LEAP_NOTINSYNC);
4996	sys_stratum = STRATUM_UNSPEC;
4997	memcpy(&sys_refid, "INIT", 4);
4998	sys_peer = NULL;
4999	sys_rootdelay = 0;
5000	sys_rootdisp = 0;
5001	L_CLR(&sys_reftime);
5002	sys_jitter = 0;
5003	measure_precision();
5004	get_systime(&dummy);
5005	sys_survivors = 0;
5006	sys_manycastserver = 0;
5007	sys_bclient = 0;
5008	sys_bdelay = BDELAY_DEFAULT;	/*[Bug 3031] delay cutoff */
5009	sys_authenticate = 1;
5010	sys_stattime = current_time;
5011	orphwait = current_time + sys_orphwait;
5012	proto_clr_stats();
5013	for (i = 0; i < MAX_TTL; ++i)
5014		sys_ttl[i] = (u_char)((i * 256) / MAX_TTL);
5015	sys_ttlmax = (MAX_TTL - 1);
5016	hardpps_enable = 0;
5017	stats_control = 1;
5018}
5019
5020
5021/*
5022 * proto_config - configure the protocol module
5023 */
5024void
5025proto_config(
5026	int	item,
5027	u_long	value,
5028	double	dvalue,
5029	sockaddr_u *svalue
5030	)
5031{
5032	/*
5033	 * Figure out what he wants to change, then do it
5034	 */
5035	DPRINTF(2, ("proto_config: code %d value %lu dvalue %lf\n",
5036		    item, value, dvalue));
5037
5038	switch (item) {
5039
5040	/*
5041	 * enable and disable commands - arguments are Boolean.
5042	 */
5043	case PROTO_AUTHENTICATE: /* authentication (auth) */
5044		sys_authenticate = value;
5045		break;
5046
5047	case PROTO_BROADCLIENT: /* broadcast client (bclient) */
5048		sys_bclient = (int)value;
5049		if (sys_bclient == 0)
5050			io_unsetbclient();
5051		else
5052			io_setbclient();
5053		break;
5054
5055#ifdef REFCLOCK
5056	case PROTO_CAL:		/* refclock calibrate (calibrate) */
5057		cal_enable = value;
5058		break;
5059#endif /* REFCLOCK */
5060
5061	case PROTO_KERNEL:	/* kernel discipline (kernel) */
5062		select_loop(value);
5063		break;
5064
5065	case PROTO_MONITOR:	/* monitoring (monitor) */
5066		if (value)
5067			mon_start(MON_ON);
5068		else {
5069			mon_stop(MON_ON);
5070			if (mon_enabled)
5071				msyslog(LOG_WARNING,
5072					"restrict: 'monitor' cannot be disabled while 'limited' is enabled");
5073		}
5074		break;
5075
5076	case PROTO_NTP:		/* NTP discipline (ntp) */
5077		ntp_enable = value;
5078		break;
5079
5080	case PROTO_MODE7:	/* mode7 management (ntpdc) */
5081		ntp_mode7 = value;
5082		break;
5083
5084	case PROTO_PPS:		/* PPS discipline (pps) */
5085		hardpps_enable = value;
5086		break;
5087
5088	case PROTO_FILEGEN:	/* statistics (stats) */
5089		stats_control = value;
5090		break;
5091
5092	/*
5093	 * tos command - arguments are double, sometimes cast to int
5094	 */
5095
5096	case PROTO_BCPOLLBSTEP:	/* Broadcast Poll Backstep gate (bcpollbstep) */
5097		sys_bcpollbstep = (u_char)dvalue;
5098		break;
5099
5100	case PROTO_BEACON:	/* manycast beacon (beacon) */
5101		sys_beacon = (int)dvalue;
5102		break;
5103
5104	case PROTO_BROADDELAY:	/* default broadcast delay (bdelay) */
5105		sys_bdelay = (dvalue ? dvalue : BDELAY_DEFAULT);
5106		break;
5107
5108	case PROTO_CEILING:	/* stratum ceiling (ceiling) */
5109		sys_ceiling = (int)dvalue;
5110		break;
5111
5112	case PROTO_COHORT:	/* cohort switch (cohort) */
5113		sys_cohort = (int)dvalue;
5114		break;
5115
5116	case PROTO_FLOOR:	/* stratum floor (floor) */
5117		sys_floor = (int)dvalue;
5118		break;
5119
5120	case PROTO_MAXCLOCK:	/* maximum candidates (maxclock) */
5121		sys_maxclock = (int)dvalue;
5122		break;
5123
5124	case PROTO_MAXDIST:	/* select threshold (maxdist) */
5125		sys_maxdist = dvalue;
5126		break;
5127
5128	case PROTO_CALLDELAY:	/* modem call delay (mdelay) */
5129		break;		/* NOT USED */
5130
5131	case PROTO_MINCLOCK:	/* minimum candidates (minclock) */
5132		sys_minclock = (int)dvalue;
5133		break;
5134
5135	case PROTO_MINDISP:	/* minimum distance (mindist) */
5136		sys_mindisp = dvalue;
5137		break;
5138
5139	case PROTO_MINSANE:	/* minimum survivors (minsane) */
5140		sys_minsane = (int)dvalue;
5141		break;
5142
5143	case PROTO_ORPHAN:	/* orphan stratum (orphan) */
5144		sys_orphan = (int)dvalue;
5145		break;
5146
5147	case PROTO_ORPHWAIT:	/* orphan wait (orphwait) */
5148		orphwait -= sys_orphwait;
5149		sys_orphwait = (int)dvalue;
5150		orphwait += sys_orphwait;
5151		break;
5152
5153	/*
5154	 * Miscellaneous commands
5155	 */
5156	case PROTO_MULTICAST_ADD: /* add group address */
5157		if (svalue != NULL)
5158			io_multicast_add(svalue);
5159		sys_bclient = 1;
5160		break;
5161
5162	case PROTO_MULTICAST_DEL: /* delete group address */
5163		if (svalue != NULL)
5164			io_multicast_del(svalue);
5165		break;
5166
5167	/*
5168	 * Peer_clear Early policy choices
5169	 */
5170
5171	case PROTO_PCEDIGEST:	/* Digest */
5172		peer_clear_digest_early = value;
5173		break;
5174
5175	/*
5176	 * Unpeer Early policy choices
5177	 */
5178
5179	case PROTO_UECRYPTO:	/* Crypto */
5180		unpeer_crypto_early = value;
5181		break;
5182
5183	case PROTO_UECRYPTONAK:	/* Crypto_NAK */
5184		unpeer_crypto_nak_early = value;
5185		break;
5186
5187	case PROTO_UEDIGEST:	/* Digest */
5188		unpeer_digest_early = value;
5189		break;
5190
5191	default:
5192		msyslog(LOG_NOTICE,
5193		    "proto: unsupported option %d", item);
5194	}
5195}
5196
5197
5198/*
5199 * proto_clr_stats - clear protocol stat counters
5200 */
5201void
5202proto_clr_stats(void)
5203{
5204	sys_stattime = current_time;
5205	sys_received = 0;
5206	sys_processed = 0;
5207	sys_newversion = 0;
5208	sys_oldversion = 0;
5209	sys_declined = 0;
5210	sys_restricted = 0;
5211	sys_badlength = 0;
5212	sys_badauth = 0;
5213	sys_limitrejected = 0;
5214	sys_kodsent = 0;
5215	sys_lamport = 0;
5216	sys_tsrounding = 0;
5217}
5218