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