2 * ntp_proto.c - NTP version 4 protocol machinery
4 * ATTENTION: Get approval from Dave Mills on all changes to this file!
12 #include "ntp_stdlib.h"
13 #include "ntp_unixtime.h"
14 #include "ntp_control.h"
15 #include "ntp_string.h"
16 #include "ntp_leapsec.h"
17 #include "refidsmear.h"
18 #include "lib_strbuf.h"
28 /* [Bug 3031] define automatic broadcastdelay cutoff preset */
29 #ifndef BDELAY_DEFAULT
30 # define BDELAY_DEFAULT (-0.050)
34 * This macro defines the authentication state. If x is 1 authentication
35 * is required; othewise it is optional.
37 #define AUTH(x, y) ((x) ? (y) == AUTH_OK \
38 : (y) == AUTH_OK || (y) == AUTH_NONE)
40 #define AUTH_NONE 0 /* authentication not required */
41 #define AUTH_OK 1 /* authentication OK */
42 #define AUTH_ERROR 2 /* authentication error */
43 #define AUTH_CRYPTO 3 /* crypto_NAK */
46 * Set up Kiss Code values
50 NOKISS, /* No Kiss Code */
51 RATEKISS, /* Rate limit Kiss Code */
52 DENYKISS, /* Deny Kiss */
53 RSTRKISS, /* Restricted Kiss */
54 XKISS, /* Experimental Kiss */
55 UNKNOWNKISS /* Unknown Kiss Code */
58 enum nak_error_codes {
59 NONAK, /* No NAK seen */
60 INVALIDNAK, /* NAK cannot be used */
61 VALIDNAK /* NAK is valid */
65 * traffic shaping parameters
67 #define NTP_IBURST 6 /* packets in iburst */
68 #define RESP_DELAY 1 /* refclock burst delay (s) */
71 * pool soliciting restriction duration (s)
73 #define POOL_SOLICIT_WINDOW 8
76 * peer_select groups statistics for a peer used by clock_select() and
79 typedef struct peer_select_tag {
81 double synch; /* sync distance */
82 double error; /* jitter */
83 double seljit; /* selection jitter */
87 * System variables are declared here. Unless specified otherwise, all
88 * times are in seconds.
90 u_char sys_leap; /* system leap indicator, use set_sys_leap() to change this */
91 u_char xmt_leap; /* leap indicator sent in client requests, set up by set_sys_leap() */
92 u_char sys_stratum; /* system stratum */
93 s_char sys_precision; /* local clock precision (log2 s) */
94 double sys_rootdelay; /* roundtrip delay to primary source */
95 double sys_rootdisp; /* dispersion to primary source */
96 u_int32 sys_refid; /* reference id (network byte order) */
97 l_fp sys_reftime; /* last update time */
98 struct peer *sys_peer; /* current peer */
101 struct leap_smear_info leap_smear;
103 int leap_sec_in_progress;
106 * Rate controls. Leaky buckets are used to throttle the packet
107 * transmission rates in order to protect busy servers such as at NIST
108 * and USNO. There is a counter for each association and another for KoD
109 * packets. The association counter decrements each second, but not
110 * below zero. Each time a packet is sent the counter is incremented by
111 * a configurable value representing the average interval between
112 * packets. A packet is delayed as long as the counter is greater than
113 * zero. Note this does not affect the time value computations.
116 * Nonspecified system state variables
118 int sys_bclient; /* broadcast client enable */
119 double sys_bdelay; /* broadcast client default delay */
120 int sys_authenticate; /* requre authentication for config */
121 l_fp sys_authdelay; /* authentication delay */
122 double sys_offset; /* current local clock offset */
123 double sys_mindisp = MINDISPERSE; /* minimum distance (s) */
124 double sys_maxdist = MAXDISTANCE; /* selection threshold */
125 double sys_jitter; /* system jitter */
126 u_long sys_epoch; /* last clock update time */
127 static double sys_clockhop; /* clockhop threshold */
128 static int leap_vote_ins; /* leap consensus for insert */
129 static int leap_vote_del; /* leap consensus for delete */
130 keyid_t sys_private; /* private value for session seed */
131 int sys_manycastserver; /* respond to manycast client pkts */
132 int ntp_mode7; /* respond to ntpdc (mode7) */
133 int peer_ntpdate; /* active peers in ntpdate mode */
134 int sys_survivors; /* truest of the truechimers */
135 char *sys_ident = NULL; /* identity scheme */
138 * TOS and multicast mapping stuff
140 int sys_floor = 0; /* cluster stratum floor */
141 u_char sys_bcpollbstep = 0; /* Broadcast Poll backstep gate */
142 int sys_ceiling = STRATUM_UNSPEC - 1; /* cluster stratum ceiling */
143 int sys_minsane = 1; /* minimum candidates */
144 int sys_minclock = NTP_MINCLOCK; /* minimum candidates */
145 int sys_maxclock = NTP_MAXCLOCK; /* maximum candidates */
146 int sys_cohort = 0; /* cohort switch */
147 int sys_orphan = STRATUM_UNSPEC + 1; /* orphan stratum */
148 int sys_orphwait = NTP_ORPHWAIT; /* orphan wait */
149 int sys_beacon = BEACON; /* manycast beacon interval */
150 u_int sys_ttlmax; /* max ttl mapping vector index */
151 u_char sys_ttl[MAX_TTL]; /* ttl mapping vector */
154 * Statistics counters - first the good, then the bad
156 u_long sys_stattime; /* elapsed time */
157 u_long sys_received; /* packets received */
158 u_long sys_processed; /* packets for this host */
159 u_long sys_newversion; /* current version */
160 u_long sys_oldversion; /* old version */
161 u_long sys_restricted; /* access denied */
162 u_long sys_badlength; /* bad length or format */
163 u_long sys_badauth; /* bad authentication */
164 u_long sys_declined; /* declined */
165 u_long sys_limitrejected; /* rate exceeded */
166 u_long sys_kodsent; /* KoD sent */
169 * Mechanism knobs: how soon do we peer_clear() or unpeer()?
171 * The default way is "on-receipt". If this was a packet from a
172 * well-behaved source, on-receipt will offer the fastest recovery.
173 * If this was from a DoS attack, the default way makes it easier
174 * for a bad-guy to DoS us. So look and see what bites you harder
175 * and choose according to your environment.
177 int peer_clear_digest_early = 1; /* bad digest (TEST5) and Autokey */
178 int unpeer_crypto_early = 1; /* bad crypto (TEST9) */
179 int unpeer_crypto_nak_early = 1; /* crypto_NAK (TEST5) */
180 int unpeer_digest_early = 1; /* bad digest (TEST5) */
182 int dynamic_interleave = DYNAMIC_INTERLEAVE; /* Bug 2978 mitigation */
184 int kiss_code_check(u_char hisleap, u_char hisstratum, u_char hismode, u_int32 refid);
185 enum nak_error_codes valid_NAK(struct peer *peer, struct recvbuf *rbufp, u_char hismode);
186 static double root_distance (struct peer *);
187 static void clock_combine (peer_select *, int, int);
188 static void peer_xmit (struct peer *);
189 static void fast_xmit (struct recvbuf *, int, keyid_t, int);
190 static void pool_xmit (struct peer *);
191 static void clock_update (struct peer *);
192 static void measure_precision(void);
193 static double measure_tick_fuzz(void);
194 static int local_refid (struct peer *);
195 static int peer_unfit (struct peer *);
197 static int group_test (char *, char *);
200 void pool_name_resolved (int, int, void *, const char *,
201 const char *, const struct addrinfo *,
202 const struct addrinfo *);
205 const char * amtoa (int am);
213 sys_leap = new_sys_leap;
217 * Under certain conditions we send faked leap bits to clients, so
218 * eventually change xmt_leap below, but never change LEAP_NOTINSYNC.
220 if (xmt_leap != LEAP_NOTINSYNC) {
221 if (leap_sec_in_progress) {
222 /* always send "not sync" */
223 xmt_leap = LEAP_NOTINSYNC;
228 * If leap smear is enabled in general we must
229 * never send a leap second warning to clients,
230 * so make sure we only send "in sync".
232 if (leap_smear.enabled)
233 xmt_leap = LEAP_NOWARNING;
235 #endif /* LEAP_SMEAR */
252 if ( hismode == MODE_SERVER
253 && hisleap == LEAP_NOTINSYNC
254 && hisstratum == STRATUM_UNSPEC) {
255 if(memcmp(&refid,"RATE", 4) == 0) {
257 } else if(memcmp(&refid,"DENY", 4) == 0) {
259 } else if(memcmp(&refid,"RSTR", 4) == 0) {
261 } else if(memcmp(&refid,"X", 1) == 0) {
264 return (UNKNOWNKISS);
273 * Check that NAK is valid
278 struct recvbuf *rbufp,
282 int base_packet_length = MIN_V4_PKT_LEN;
286 l_fp p_org; /* origin timestamp */
287 const l_fp * myorg; /* selected peer origin */
290 * Check to see if there is something beyond the basic packet
292 if (rbufp->recv_length == base_packet_length) {
296 remainder_size = rbufp->recv_length - base_packet_length;
298 * Is this a potential NAK?
300 if (remainder_size != 4) {
305 * Only server responses can contain NAK's
308 if (hismode != MODE_SERVER &&
309 hismode != MODE_ACTIVE &&
310 hismode != MODE_PASSIVE
316 * Make sure that the extra field in the packet is all zeros
318 rpkt = &rbufp->recv_pkt;
319 keyid = ntohl(((u_int32 *)rpkt)[base_packet_length / 4]);
325 * Only valid if peer uses a key
327 if (!peer || !peer->keyid || !(peer->flags & FLAG_SKEY)) {
332 * The ORIGIN must match, or this cannot be a valid NAK, either.
334 NTOHL_FP(&rpkt->org, &p_org);
340 if (L_ISZERO(&p_org) ||
342 !L_ISEQU(&p_org, myorg)) {
346 /* If we ever passed all that checks, we should be safe. Well,
347 * as safe as we can ever be with an unauthenticated crypto-nak.
354 * transmit - transmit procedure called by poll timeout
358 struct peer *peer /* peer structure pointer */
364 * The polling state machine. There are two kinds of machines,
365 * those that never expect a reply (broadcast and manycast
366 * server modes) and those that do (all other modes). The dance
372 * In broadcast mode the poll interval is never changed from
375 if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) {
376 peer->outdate = current_time;
377 if (sys_leap != LEAP_NOTINSYNC)
379 poll_update(peer, hpoll);
384 * In manycast mode we start with unity ttl. The ttl is
385 * increased by one for each poll until either sys_maxclock
386 * servers have been found or the maximum ttl is reached. When
387 * sys_maxclock servers are found we stop polling until one or
388 * more servers have timed out or until less than sys_minclock
389 * associations turn up. In this case additional better servers
390 * are dragged in and preempt the existing ones. Once every
391 * sys_beacon seconds we are to transmit unconditionally, but
392 * this code is not quite right -- peer->unreach counts polls
393 * and is being compared with sys_beacon, so the beacons happen
394 * every sys_beacon polls.
396 if (peer->cast_flags & MDF_ACAST) {
397 peer->outdate = current_time;
398 if (peer->unreach > sys_beacon) {
402 } else if ( sys_survivors < sys_minclock
403 || peer_associations < sys_maxclock) {
404 if (peer->ttl < sys_ttlmax)
409 poll_update(peer, hpoll);
414 * Pool associations transmit unicast solicitations when there
415 * are less than a hard limit of 2 * sys_maxclock associations,
416 * and either less than sys_minclock survivors or less than
417 * sys_maxclock associations. The hard limit prevents unbounded
418 * growth in associations if the system clock or network quality
419 * result in survivor count dipping below sys_minclock often.
420 * This was observed testing with pool, where sys_maxclock == 12
421 * resulted in 60 associations without the hard limit. A
422 * similar hard limit on manycastclient ephemeral associations
423 * may be appropriate.
425 if (peer->cast_flags & MDF_POOL) {
426 peer->outdate = current_time;
427 if ( (peer_associations <= 2 * sys_maxclock)
428 && ( peer_associations < sys_maxclock
429 || sys_survivors < sys_minclock))
431 poll_update(peer, hpoll);
436 * In unicast modes the dance is much more intricate. It is
437 * designed to back off whenever possible to minimize network
440 if (peer->burst == 0) {
444 * Update the reachability status. If not heard for
445 * three consecutive polls, stuff infinity in the clock
448 oreach = peer->reach;
449 peer->outdate = current_time;
455 * Here the peer is unreachable. If it was
456 * previously reachable raise a trap. Send a
459 clock_filter(peer, 0., 0., MAXDISPERSE);
462 report_event(PEVNT_UNREACH, peer, NULL);
464 if ( (peer->flags & FLAG_IBURST)
466 peer->retry = NTP_RETRY;
470 * Here the peer is reachable. Send a burst if
471 * enabled and the peer is fit. Reset unreach
472 * for persistent and ephemeral associations.
473 * Unreach is also reset for survivors in
477 if (!(peer->flags & FLAG_PREEMPT))
479 if ( (peer->flags & FLAG_BURST)
481 && !peer_unfit(peer))
482 peer->retry = NTP_RETRY;
486 * Watch for timeout. If ephemeral, toss the rascal;
487 * otherwise, bump the poll interval. Note the
488 * poll_update() routine will clamp it to maxpoll.
489 * If preemptible and we have more peers than maxclock,
490 * and this peer has the minimum score of preemptibles,
493 if (peer->unreach >= NTP_UNREACH) {
495 /* ephemeral: no FLAG_CONFIG nor FLAG_PREEMPT */
496 if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
497 report_event(PEVNT_RESTART, peer, "timeout");
498 peer_clear(peer, "TIME");
502 if ( (peer->flags & FLAG_PREEMPT)
503 && (peer_associations > sys_maxclock)
504 && score_all(peer)) {
505 report_event(PEVNT_RESTART, peer, "timeout");
506 peer_clear(peer, "TIME");
513 if (peer->burst == 0) {
516 * If ntpdate mode and the clock has not been
517 * set and all peers have completed the burst,
518 * we declare a successful failure.
522 if (peer_ntpdate == 0) {
524 "ntpd: no servers found");
527 "ntpd: no servers found\n");
537 * Do not transmit if in broadcast client mode.
539 if (peer->hmode != MODE_BCLIENT)
541 poll_update(peer, hpoll);
555 case AM_ERR: return "AM_ERR";
556 case AM_NOMATCH: return "AM_NOMATCH";
557 case AM_PROCPKT: return "AM_PROCPKT";
558 case AM_BCST: return "AM_BCST";
559 case AM_FXMIT: return "AM_FXMIT";
560 case AM_MANYCAST: return "AM_MANYCAST";
561 case AM_NEWPASS: return "AM_NEWPASS";
562 case AM_NEWBCL: return "AM_NEWBCL";
563 case AM_POSSBCL: return "AM_POSSBCL";
566 snprintf(bp, LIB_BUFLENGTH, "AM_#%d", am);
573 * receive - receive procedure called for each packet received
577 struct recvbuf *rbufp
580 register struct peer *peer; /* peer structure pointer */
581 register struct pkt *pkt; /* receive packet pointer */
582 u_char hisversion; /* packet version */
583 u_char hisleap; /* packet leap indicator */
584 u_char hismode; /* packet mode */
585 u_char hisstratum; /* packet stratum */
586 u_short restrict_mask; /* restrict bits */
587 const char *hm_str; /* hismode string */
588 const char *am_str; /* association match string */
589 int kissCode = NOKISS; /* Kiss Code */
590 int has_mac; /* length of MAC field */
591 int authlen; /* offset of MAC field */
592 int is_authentic = AUTH_NONE; /* cryptosum ok */
593 int crypto_nak_test; /* result of crypto-NAK check */
594 int retcode = AM_NOMATCH; /* match code */
595 keyid_t skeyid = 0; /* key IDs */
596 u_int32 opcode = 0; /* extension field opcode */
597 sockaddr_u *dstadr_sin; /* active runway */
598 struct peer *peer2; /* aux peer structure pointer */
599 endpt *match_ep; /* newpeer() local address */
600 l_fp p_org; /* origin timestamp */
601 l_fp p_rec; /* receive timestamp */
602 l_fp p_xmt; /* transmit timestamp */
604 char hostname[NTP_MAXSTRLEN + 1];
605 char *groupname = NULL;
606 struct autokey *ap; /* autokey structure pointer */
607 int rval; /* cookie snatcher */
608 keyid_t pkeyid = 0, tkeyid = 0; /* key IDs */
610 #ifdef HAVE_NTP_SIGND
611 static unsigned char zero_key[16];
612 #endif /* HAVE_NTP_SIGND */
615 * Monitor the packet and get restrictions. Note that the packet
616 * length for control and private mode packets must be checked
617 * by the service routines. Some restrictions have to be handled
618 * later in order to generate a kiss-o'-death packet.
621 * Bogus port check is before anything, since it probably
622 * reveals a clogging attack.
625 if (0 == SRCPORT(&rbufp->recv_srcadr)) {
627 return; /* bogus port */
629 restrict_mask = restrictions(&rbufp->recv_srcadr);
630 pkt = &rbufp->recv_pkt;
631 DPRINTF(2, ("receive: at %ld %s<-%s flags %x restrict %03x org %#010x.%08x xmt %#010x.%08x\n",
632 current_time, stoa(&rbufp->dstadr->sin),
633 stoa(&rbufp->recv_srcadr), rbufp->dstadr->flags,
634 restrict_mask, ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
635 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
636 hisversion = PKT_VERSION(pkt->li_vn_mode);
637 hisleap = PKT_LEAP(pkt->li_vn_mode);
638 hismode = (int)PKT_MODE(pkt->li_vn_mode);
639 hisstratum = PKT_TO_STRATUM(pkt->stratum);
640 INSIST(0 != hisstratum);
642 if (restrict_mask & RES_IGNORE) {
644 return; /* ignore everything */
646 if (hismode == MODE_PRIVATE) {
647 if (!ntp_mode7 || (restrict_mask & RES_NOQUERY)) {
649 return; /* no query private */
651 process_private(rbufp, ((restrict_mask &
652 RES_NOMODIFY) == 0));
655 if (hismode == MODE_CONTROL) {
656 if (restrict_mask & RES_NOQUERY) {
658 return; /* no query control */
660 process_control(rbufp, restrict_mask);
663 if (restrict_mask & RES_DONTSERVE) {
665 return; /* no time serve */
669 * This is for testing. If restricted drop ten percent of
672 if (restrict_mask & RES_FLAKE) {
673 if ((double)ntp_random() / 0x7fffffff < .1) {
675 return; /* no flakeway */
680 * Version check must be after the query packets, since they
681 * intentionally use an early version.
683 if (hisversion == NTP_VERSION) {
684 sys_newversion++; /* new version */
685 } else if ( !(restrict_mask & RES_VERSION)
686 && hisversion >= NTP_OLDVERSION) {
687 sys_oldversion++; /* previous version */
690 return; /* old version */
694 * Figure out his mode and validate the packet. This has some
695 * legacy raunch that probably should be removed. In very early
696 * NTP versions mode 0 was equivalent to what later versions
697 * would interpret as client mode.
699 if (hismode == MODE_UNSPEC) {
700 if (hisversion == NTP_OLDVERSION) {
701 hismode = MODE_CLIENT;
704 return; /* invalid mode */
709 * Parse the extension field if present. We figure out whether
710 * an extension field is present by measuring the MAC size. If
711 * the number of words following the packet header is 0, no MAC
712 * is present and the packet is not authenticated. If 1, the
713 * packet is a crypto-NAK; if 3, the packet is authenticated
714 * with DES; if 5, the packet is authenticated with MD5; if 6,
715 * the packet is authenticated with SHA. If 2 or * 4, the packet
716 * is a runt and discarded forthwith. If greater than 6, an
717 * extension field is present, so we subtract the length of the
718 * field and go around again.
721 authlen = LEN_PKT_NOMAC;
722 has_mac = rbufp->recv_length - authlen;
723 while (has_mac > 0) {
730 if (has_mac % 4 != 0 || has_mac < (int)MIN_MAC_LEN) {
732 return; /* bad length */
734 if (has_mac <= (int)MAX_MAC_LEN) {
735 skeyid = ntohl(((u_int32 *)pkt)[authlen / 4]);
739 opcode = ntohl(((u_int32 *)pkt)[authlen / 4]);
740 len = opcode & 0xffff;
743 || (int)len + authlen > rbufp->recv_length) {
745 return; /* bad length */
749 * Extract calling group name for later. If
750 * sys_groupname is non-NULL, there must be
751 * a group name provided to elicit a response.
753 if ( (opcode & 0x3fff0000) == CRYPTO_ASSOC
754 && sys_groupname != NULL) {
755 ep = (struct exten *)&((u_int32 *)pkt)[authlen / 4];
756 hostlen = ntohl(ep->vallen);
757 if ( hostlen >= sizeof(hostname)
759 offsetof(struct exten, pkt)) {
761 return; /* bad length */
763 memcpy(hostname, &ep->pkt, hostlen);
764 hostname[hostlen] = '\0';
765 groupname = strchr(hostname, '@');
766 if (groupname == NULL) {
779 * If has_mac is < 0 we had a malformed packet.
783 return; /* bad length */
787 * If authentication required, a MAC must be present.
789 if (restrict_mask & RES_DONTTRUST && has_mac == 0) {
791 return; /* access denied */
795 * Update the MRU list and finger the cloggers. It can be a
796 * little expensive, so turn it off for production use.
797 * RES_LIMITED and RES_KOD will be cleared in the returned
798 * restrict_mask unless one or both actions are warranted.
800 restrict_mask = ntp_monitor(rbufp, restrict_mask);
801 if (restrict_mask & RES_LIMITED) {
803 if ( !(restrict_mask & RES_KOD)
804 || MODE_BROADCAST == hismode
805 || MODE_SERVER == hismode) {
806 if (MODE_SERVER == hismode)
807 DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
808 stoa(&rbufp->recv_srcadr)));
809 return; /* rate exceeded */
811 if (hismode == MODE_CLIENT)
812 fast_xmit(rbufp, MODE_SERVER, skeyid,
815 fast_xmit(rbufp, MODE_ACTIVE, skeyid,
817 return; /* rate exceeded */
819 restrict_mask &= ~RES_KOD;
822 * We have tossed out as many buggy packets as possible early in
823 * the game to reduce the exposure to a clogging attack. Now we
824 * have to burn some cycles to find the association and
825 * authenticate the packet if required. Note that we burn only
826 * digest cycles, again to reduce exposure. There may be no
827 * matching association and that's okay.
829 * More on the autokey mambo. Normally the local interface is
830 * found when the association was mobilized with respect to a
831 * designated remote address. We assume packets arriving from
832 * the remote address arrive via this interface and the local
833 * address used to construct the autokey is the unicast address
834 * of the interface. However, if the sender is a broadcaster,
835 * the interface broadcast address is used instead.
836 * Notwithstanding this technobabble, if the sender is a
837 * multicaster, the broadcast address is null, so we use the
838 * unicast address anyway. Don't ask.
840 peer = findpeer(rbufp, hismode, &retcode);
841 dstadr_sin = &rbufp->dstadr->sin;
842 NTOHL_FP(&pkt->org, &p_org);
843 NTOHL_FP(&pkt->rec, &p_rec);
844 NTOHL_FP(&pkt->xmt, &p_xmt);
845 hm_str = modetoa(hismode);
846 am_str = amtoa(retcode);
849 * Authentication is conditioned by three switches:
851 * NOPEER (RES_NOPEER) do not mobilize an association unless
853 * NOTRUST (RES_DONTTRUST) do not allow access unless
854 * authenticated (implies NOPEER)
855 * enable (sys_authenticate) master NOPEER switch, by default
858 * The NOPEER and NOTRUST can be specified on a per-client basis
859 * using the restrict command. The enable switch if on implies
860 * NOPEER for all clients. There are four outcomes:
862 * NONE The packet has no MAC.
863 * OK the packet has a MAC and authentication succeeds
864 * ERROR the packet has a MAC and authentication fails
865 * CRYPTO crypto-NAK. The MAC has four octets only.
867 * Note: The AUTH(x, y) macro is used to filter outcomes. If x
868 * is zero, acceptable outcomes of y are NONE and OK. If x is
869 * one, the only acceptable outcome of y is OK.
871 crypto_nak_test = valid_NAK(peer, rbufp, hismode);
874 * Drop any invalid crypto-NAKs
876 if (crypto_nak_test == INVALIDNAK) {
877 report_event(PEVNT_AUTH, peer, "Invalid_NAK");
881 msyslog(LOG_ERR, "Invalid-NAK error at %ld %s<-%s",
882 current_time, stoa(dstadr_sin), stoa(&rbufp->recv_srcadr));
887 restrict_mask &= ~RES_MSSNTP;
888 is_authentic = AUTH_NONE; /* not required */
889 DPRINTF(2, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x NOMAC\n",
890 current_time, stoa(dstadr_sin),
891 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
893 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
894 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
895 } else if (crypto_nak_test == VALIDNAK) {
896 restrict_mask &= ~RES_MSSNTP;
897 is_authentic = AUTH_CRYPTO; /* crypto-NAK */
898 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",
899 current_time, stoa(dstadr_sin),
900 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
901 skeyid, authlen + has_mac, is_authentic,
902 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
903 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
905 #ifdef HAVE_NTP_SIGND
907 * If the signature is 20 bytes long, the last 16 of
908 * which are zero, then this is a Microsoft client
909 * wanting AD-style authentication of the server's
912 * This is described in Microsoft's WSPP docs, in MS-SNTP:
913 * http://msdn.microsoft.com/en-us/library/cc212930.aspx
915 } else if ( has_mac == MAX_MD5_LEN
916 && (restrict_mask & RES_MSSNTP)
917 && (retcode == AM_FXMIT || retcode == AM_NEWPASS)
918 && (memcmp(zero_key, (char *)pkt + authlen + 4,
919 MAX_MD5_LEN - 4) == 0)) {
920 is_authentic = AUTH_NONE;
921 #endif /* HAVE_NTP_SIGND */
924 restrict_mask &= ~RES_MSSNTP;
927 * For autokey modes, generate the session key
928 * and install in the key cache. Use the socket
929 * broadcast or unicast address as appropriate.
931 if (crypto_flags && skeyid > NTP_MAXKEY) {
934 * More on the autokey dance (AKD). A cookie is
935 * constructed from public and private values.
936 * For broadcast packets, the cookie is public
937 * (zero). For packets that match no
938 * association, the cookie is hashed from the
939 * addresses and private value. For server
940 * packets, the cookie was previously obtained
941 * from the server. For symmetric modes, the
942 * cookie was previously constructed using an
943 * agreement protocol; however, should PKI be
944 * unavailable, we construct a fake agreement as
945 * the EXOR of the peer and host cookies.
947 * hismode ephemeral persistent
948 * =======================================
951 * client sys cookie 0%
952 * server 0% sys cookie
958 if (has_mac < (int)MAX_MD5_LEN) {
962 if (hismode == MODE_BROADCAST) {
965 * For broadcaster, use the interface
966 * broadcast address when available;
967 * otherwise, use the unicast address
968 * found when the association was
969 * mobilized. However, if this is from
970 * the wildcard interface, game over.
974 ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr)) {
976 return; /* no wildcard */
979 if (!SOCK_UNSPEC(&rbufp->dstadr->bcast))
981 &rbufp->dstadr->bcast;
982 } else if (peer == NULL) {
983 pkeyid = session_key(
984 &rbufp->recv_srcadr, dstadr_sin, 0,
987 pkeyid = peer->pcookie;
991 * The session key includes both the public
992 * values and cookie. In case of an extension
993 * field, the cookie used for authentication
994 * purposes is zero. Note the hash is saved for
995 * use later in the autokey mambo.
997 if (authlen > (int)LEN_PKT_NOMAC && pkeyid != 0) {
998 session_key(&rbufp->recv_srcadr,
999 dstadr_sin, skeyid, 0, 2);
1000 tkeyid = session_key(
1001 &rbufp->recv_srcadr, dstadr_sin,
1004 tkeyid = session_key(
1005 &rbufp->recv_srcadr, dstadr_sin,
1010 #endif /* AUTOKEY */
1013 * Compute the cryptosum. Note a clogging attack may
1014 * succeed in bloating the key cache. If an autokey,
1015 * purge it immediately, since we won't be needing it
1016 * again. If the packet is authentic, it can mobilize an
1017 * association. Note that there is no key zero.
1019 if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1021 is_authentic = AUTH_ERROR;
1023 is_authentic = AUTH_OK;
1025 if (crypto_flags && skeyid > NTP_MAXKEY)
1026 authtrust(skeyid, 0);
1027 #endif /* AUTOKEY */
1028 DPRINTF(2, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x\n",
1029 current_time, stoa(dstadr_sin),
1030 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1031 skeyid, authlen + has_mac, is_authentic,
1032 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1033 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1037 * The association matching rules are implemented by a set of
1038 * routines and an association table. A packet matching an
1039 * association is processed by the peer process for that
1040 * association. If there are no errors, an ephemeral association
1041 * is mobilized: a broadcast packet mobilizes a broadcast client
1042 * aassociation; a manycast server packet mobilizes a manycast
1043 * client association; a symmetric active packet mobilizes a
1044 * symmetric passive association.
1049 * This is a client mode packet not matching any association. If
1050 * an ordinary client, simply toss a server mode packet back
1051 * over the fence. If a manycast client, we have to work a
1057 * If authentication OK, send a server reply; otherwise,
1058 * send a crypto-NAK.
1060 if (!(rbufp->dstadr->flags & INT_MCASTOPEN)) {
1061 if (AUTH(restrict_mask & RES_DONTTRUST,
1063 fast_xmit(rbufp, MODE_SERVER, skeyid,
1065 } else if (is_authentic == AUTH_ERROR) {
1066 fast_xmit(rbufp, MODE_SERVER, 0,
1072 return; /* hooray */
1076 * This must be manycast. Do not respond if not
1077 * configured as a manycast server.
1079 if (!sys_manycastserver) {
1081 return; /* not enabled */
1086 * Do not respond if not the same group.
1088 if (group_test(groupname, NULL)) {
1092 #endif /* AUTOKEY */
1095 * Do not respond if we are not synchronized or our
1096 * stratum is greater than the manycaster or the
1097 * manycaster has already synchronized to us.
1099 if ( sys_leap == LEAP_NOTINSYNC
1100 || sys_stratum >= hisstratum
1101 || (!sys_cohort && sys_stratum == hisstratum + 1)
1102 || rbufp->dstadr->addr_refid == pkt->refid) {
1104 return; /* no help */
1108 * Respond only if authentication succeeds. Don't do a
1109 * crypto-NAK, as that would not be useful.
1111 if (AUTH(restrict_mask & RES_DONTTRUST, is_authentic))
1112 fast_xmit(rbufp, MODE_SERVER, skeyid,
1114 return; /* hooray */
1117 * This is a server mode packet returned in response to a client
1118 * mode packet sent to a multicast group address (for
1119 * manycastclient) or to a unicast address (for pool). The
1120 * origin timestamp is a good nonce to reliably associate the
1121 * reply with what was sent. If there is no match, that's
1122 * curious and could be an intruder attempting to clog, so we
1125 * If the packet is authentic and the manycastclient or pool
1126 * association is found, we mobilize a client association and
1127 * copy pertinent variables from the manycastclient or pool
1128 * association to the new client association. If not, just
1129 * ignore the packet.
1131 * There is an implosion hazard at the manycast client, since
1132 * the manycast servers send the server packet immediately. If
1133 * the guy is already here, don't fire up a duplicate.
1139 * Do not respond if not the same group.
1141 if (group_test(groupname, NULL)) {
1145 #endif /* AUTOKEY */
1146 if ((peer2 = findmanycastpeer(rbufp)) == NULL) {
1148 return; /* not enabled */
1150 if (!AUTH( (!(peer2->cast_flags & MDF_POOL)
1151 && sys_authenticate)
1152 || (restrict_mask & (RES_NOPEER |
1153 RES_DONTTRUST)), is_authentic)) {
1155 return; /* access denied */
1159 * Do not respond if unsynchronized or stratum is below
1160 * the floor or at or above the ceiling.
1162 if ( hisleap == LEAP_NOTINSYNC
1163 || hisstratum < sys_floor
1164 || hisstratum >= sys_ceiling) {
1166 return; /* no help */
1168 peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1169 MODE_CLIENT, hisversion, peer2->minpoll,
1170 peer2->maxpoll, FLAG_PREEMPT |
1171 (FLAG_IBURST & peer2->flags), MDF_UCAST |
1172 MDF_UCLNT, 0, skeyid, sys_ident);
1175 return; /* ignore duplicate */
1179 * After each ephemeral pool association is spun,
1180 * accelerate the next poll for the pool solicitor so
1181 * the pool will fill promptly.
1183 if (peer2->cast_flags & MDF_POOL)
1184 peer2->nextdate = current_time + 1;
1187 * Further processing of the solicitation response would
1188 * simply detect its origin timestamp as bogus for the
1189 * brand-new association (it matches the prototype
1190 * association) and tinker with peer->nextdate delaying
1193 return; /* solicitation response handled */
1196 * This is the first packet received from a broadcast server. If
1197 * the packet is authentic and we are enabled as broadcast
1198 * client, mobilize a broadcast client association. We don't
1199 * kiss any frogs here.
1205 * Do not respond if not the same group.
1207 if (group_test(groupname, sys_ident)) {
1211 #endif /* AUTOKEY */
1212 if (sys_bclient == 0) {
1214 return; /* not enabled */
1216 if (!AUTH(sys_authenticate | (restrict_mask &
1217 (RES_NOPEER | RES_DONTTRUST)), is_authentic)) {
1219 return; /* access denied */
1223 * Do not respond if unsynchronized or stratum is below
1224 * the floor or at or above the ceiling.
1226 if ( hisleap == LEAP_NOTINSYNC
1227 || hisstratum < sys_floor
1228 || hisstratum >= sys_ceiling) {
1230 return; /* no help */
1235 * Do not respond if Autokey and the opcode is not a
1236 * CRYPTO_ASSOC response with association ID.
1238 if ( crypto_flags && skeyid > NTP_MAXKEY
1239 && (opcode & 0xffff0000) != (CRYPTO_ASSOC | CRYPTO_RESP)) {
1241 return; /* protocol error */
1243 #endif /* AUTOKEY */
1246 * Broadcasts received via a multicast address may
1247 * arrive after a unicast volley has begun
1248 * with the same remote address. newpeer() will not
1249 * find duplicate associations on other local endpoints
1250 * if a non-NULL endpoint is supplied. multicastclient
1251 * ephemeral associations are unique across all local
1254 if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
1255 match_ep = rbufp->dstadr;
1260 * Determine whether to execute the initial volley.
1262 if (sys_bdelay > 0.0) {
1265 * If a two-way exchange is not possible,
1266 * neither is Autokey.
1268 if (crypto_flags && skeyid > NTP_MAXKEY) {
1270 return; /* no autokey */
1272 #endif /* AUTOKEY */
1275 * Do not execute the volley. Start out in
1276 * broadcast client mode.
1278 peer = newpeer(&rbufp->recv_srcadr, NULL,
1279 match_ep, MODE_BCLIENT, hisversion,
1280 pkt->ppoll, pkt->ppoll, FLAG_PREEMPT,
1281 MDF_BCLNT, 0, skeyid, sys_ident);
1284 return; /* ignore duplicate */
1287 peer->delay = sys_bdelay;
1294 * Execute the initial volley in order to calibrate the
1295 * propagation delay and run the Autokey protocol.
1297 * Note that the minpoll is taken from the broadcast
1298 * packet, normally 6 (64 s) and that the poll interval
1299 * is fixed at this value.
1301 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1302 MODE_CLIENT, hisversion, pkt->ppoll, pkt->ppoll,
1303 FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
1304 0, skeyid, sys_ident);
1307 return; /* ignore duplicate */
1311 if (skeyid > NTP_MAXKEY)
1312 crypto_recv(peer, rbufp);
1313 #endif /* AUTOKEY */
1315 return; /* hooray */
1318 * This is the first packet received from a symmetric active
1319 * peer. If the packet is authentic and the first he sent,
1320 * mobilize a passive association. If not, kiss the frog.
1326 * Do not respond if not the same group.
1328 if (group_test(groupname, sys_ident)) {
1332 #endif /* AUTOKEY */
1333 if (!AUTH(sys_authenticate | (restrict_mask &
1334 (RES_NOPEER | RES_DONTTRUST)), is_authentic)) {
1337 * If authenticated but cannot mobilize an
1338 * association, send a symmetric passive
1339 * response without mobilizing an association.
1340 * This is for drat broken Windows clients. See
1341 * Microsoft KB 875424 for preferred workaround.
1343 if (AUTH(restrict_mask & RES_DONTTRUST,
1345 fast_xmit(rbufp, MODE_PASSIVE, skeyid,
1347 return; /* hooray */
1349 if (is_authentic == AUTH_ERROR) {
1350 fast_xmit(rbufp, MODE_ACTIVE, 0,
1356 * If we got here, the packet isn't part of an
1357 * existing association, it isn't correctly
1358 * authenticated, and it didn't meet either of
1359 * the previous two special cases so we should
1360 * just drop it on the floor. For example,
1361 * crypto-NAKs (is_authentic == AUTH_CRYPTO)
1362 * will make it this far. This is just
1363 * debug-printed and not logged to avoid log
1366 DPRINTF(2, ("receive: at %ld refusing to mobilize passive association"
1367 " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1368 current_time, stoa(&rbufp->recv_srcadr),
1369 hismode, hm_str, am_str, skeyid,
1370 (authlen + has_mac), is_authentic));
1376 * Do not respond if synchronized and if stratum is
1377 * below the floor or at or above the ceiling. Note,
1378 * this allows an unsynchronized peer to synchronize to
1379 * us. It would be very strange if he did and then was
1380 * nipped, but that could only happen if we were
1381 * operating at the top end of the range. It also means
1382 * we will spin an ephemeral association in response to
1383 * MODE_ACTIVE KoDs, which will time out eventually.
1385 if ( hisleap != LEAP_NOTINSYNC
1386 && (hisstratum < sys_floor || hisstratum >= sys_ceiling)) {
1388 return; /* no help */
1392 * The message is correctly authenticated and allowed.
1393 * Mobilize a symmetric passive association.
1395 if ((peer = newpeer(&rbufp->recv_srcadr, NULL,
1396 rbufp->dstadr, MODE_PASSIVE, hisversion, pkt->ppoll,
1397 NTP_MAXDPOLL, 0, MDF_UCAST, 0, skeyid,
1398 sys_ident)) == NULL) {
1400 return; /* ignore duplicate */
1406 * Process regular packet. Nothing special.
1412 * Do not respond if not the same group.
1414 if (group_test(groupname, peer->ident)) {
1418 #endif /* AUTOKEY */
1420 if (MODE_BROADCAST == hismode) {
1425 DPRINTF(2, ("receive: PROCPKT/BROADCAST: prev pkt %ld seconds ago, ppoll: %d, %d secs\n",
1426 (current_time - peer->timelastrec),
1427 peer->ppoll, (1 << peer->ppoll)
1429 /* Things we can check:
1431 * Did the poll interval change?
1432 * Is the poll interval in the packet in-range?
1433 * Did this packet arrive too soon?
1434 * Is the timestamp in this packet monotonic
1435 * with respect to the previous packet?
1438 /* This is noteworthy, not error-worthy */
1439 if (pkt->ppoll != peer->ppoll) {
1440 msyslog(LOG_INFO, "receive: broadcast poll from %s changed from %ud to %ud",
1441 stoa(&rbufp->recv_srcadr),
1442 peer->ppoll, pkt->ppoll);
1445 /* This is error-worthy */
1446 if (pkt->ppoll < peer->minpoll ||
1447 pkt->ppoll > peer->maxpoll ) {
1448 msyslog(LOG_INFO, "receive: broadcast poll of %ud from %s is out-of-range (%d to %d)!",
1449 pkt->ppoll, stoa(&rbufp->recv_srcadr),
1450 peer->minpoll, peer->maxpoll);
1454 /* too early? worth an error, too!
1456 * [Bug 3113] Ensure that at least one poll
1457 * interval has elapsed since the last **clean**
1458 * packet was received. We limit the check to
1459 * **clean** packets to prevent replayed packets
1460 * and incorrectly authenticated packets, which
1461 * we'll discard, from being used to create a
1462 * denial of service condition.
1464 deadband = (1u << pkt->ppoll);
1465 if (FLAG_BC_VOL & peer->flags)
1466 deadband -= 3; /* allow greater fuzz after volley */
1467 if ((current_time - peer->timereceived) < deadband) {
1468 msyslog(LOG_INFO, "receive: broadcast packet from %s arrived after %lu, not %lu seconds!",
1469 stoa(&rbufp->recv_srcadr),
1470 (current_time - peer->timereceived),
1475 /* Alert if time from the server is non-monotonic.
1477 * [Bug 3114] is about Broadcast mode replay DoS.
1479 * Broadcast mode *assumes* a trusted network.
1480 * Even so, it's nice to be robust in the face
1483 * If we get an authenticated broadcast packet
1484 * with an "earlier" timestamp, it means one of
1487 * - the broadcast server had a backward step.
1489 * - somebody is trying a replay attack.
1491 * deadband: By default, we assume the broadcast
1492 * network is trustable, so we take our accepted
1493 * broadcast packets as we receive them. But
1494 * some folks might want to take additional poll
1495 * delays before believing a backward step.
1497 if (sys_bcpollbstep) {
1498 /* pkt->ppoll or peer->ppoll ? */
1499 deadband = (1u << pkt->ppoll)
1500 * sys_bcpollbstep + 2;
1505 if (L_ISZERO(&peer->bxmt)) {
1506 tdiff.l_ui = tdiff.l_uf = 0;
1509 L_SUB(&tdiff, &peer->bxmt);
1511 if (tdiff.l_i < 0 &&
1512 (current_time - peer->timereceived) < deadband)
1514 msyslog(LOG_INFO, "receive: broadcast packet from %s contains non-monotonic timestamp: %#010x.%08x -> %#010x.%08x",
1515 stoa(&rbufp->recv_srcadr),
1516 peer->bxmt.l_ui, peer->bxmt.l_uf,
1517 p_xmt.l_ui, p_xmt.l_uf
1523 peer->timelastrec = current_time;
1532 * A passive packet matches a passive association. This is
1533 * usually the result of reconfiguring a client on the fly. As
1534 * this association might be legitimate and this packet an
1535 * attempt to deny service, just ignore it.
1542 * For everything else there is the bit bucket.
1551 * If the association is configured for Autokey, the packet must
1552 * have a public key ID; if not, the packet must have a
1555 if ( is_authentic != AUTH_CRYPTO
1556 && ( ((peer->flags & FLAG_SKEY) && skeyid <= NTP_MAXKEY)
1557 || (!(peer->flags & FLAG_SKEY) && skeyid > NTP_MAXKEY))) {
1561 #endif /* AUTOKEY */
1564 peer->flash &= ~PKT_TEST_MASK;
1565 if (peer->flags & FLAG_XBOGUS) {
1566 peer->flags &= ~FLAG_XBOGUS;
1567 peer->flash |= TEST3;
1571 * Next comes a rigorous schedule of timestamp checking. If the
1572 * transmit timestamp is zero, the server has not initialized in
1573 * interleaved modes or is horribly broken.
1575 * A KoD packet we pay attention to cannot have a 0 transmit
1578 if (L_ISZERO(&p_xmt)) {
1579 peer->flash |= TEST3; /* unsynch */
1580 if (STRATUM_UNSPEC == hisstratum) { /* KoD packet */
1581 peer->bogusorg++; /* for TEST2 or TEST3 */
1583 "receive: Unexpected zero transmit timestamp in KoD from %s",
1584 ntoa(&peer->srcadr));
1589 * If the transmit timestamp duplicates our previous one, the
1590 * packet is a replay. This prevents the bad guys from replaying
1591 * the most recent packet, authenticated or not.
1593 } else if (L_ISEQU(&peer->xmt, &p_xmt)) {
1594 peer->flash |= TEST1; /* duplicate */
1599 * If this is a broadcast mode packet, make sure hisstratum
1600 * is appropriate. Don't do anything else here - we wait to
1601 * see if this is an interleave broadcast packet until after
1602 * we've validated the MAC that SHOULD be provided.
1604 * hisstratum should never be 0.
1605 * If hisstratum is 15, then we'll advertise as UNSPEC but
1606 * at least we'll be able to sync with the broadcast server.
1608 } else if (hismode == MODE_BROADCAST) {
1609 if ( 0 == hisstratum
1610 || STRATUM_UNSPEC <= hisstratum) {
1611 /* Is this a ++sys_declined or ??? */
1613 "receive: Unexpected stratum (%d) in broadcast from %s",
1614 hisstratum, ntoa(&peer->srcadr));
1619 * Basic KoD validation checking:
1621 * KoD packets are a mixed-blessing. Forged KoD packets
1622 * are DoS attacks. There are rare situations where we might
1623 * get a valid KoD response, though. Since KoD packets are
1624 * a special case that complicate the checks we do next, we
1625 * handle the basic KoD checks here.
1627 * Note that we expect the incoming KoD packet to have its
1628 * (nonzero) org, rec, and xmt timestamps set to the xmt timestamp
1629 * that we have previously sent out. Watch interleave mode.
1631 } else if (STRATUM_UNSPEC == hisstratum) {
1632 DEBUG_INSIST(!L_ISZERO(&p_xmt));
1633 if ( L_ISZERO(&p_org) /* We checked p_xmt above */
1634 || L_ISZERO(&p_rec)) {
1637 "receive: KoD packet from %s has a zero org or rec timestamp. Ignoring.",
1638 ntoa(&peer->srcadr));
1642 if ( !L_ISEQU(&p_xmt, &p_org)
1643 || !L_ISEQU(&p_xmt, &p_rec)) {
1646 "receive: KoD packet from %s has inconsistent xmt/org/rec timestamps. Ignoring.",
1647 ntoa(&peer->srcadr));
1651 /* Be conservative */
1652 if (peer->flip == 0 && !L_ISEQU(&p_org, &peer->aorg)) {
1655 "receive: flip 0 KoD origin timestamp %#010x.%08x from %s does not match %#010x.%08x - ignoring.",
1656 p_org.l_ui, p_org.l_uf,
1657 ntoa(&peer->srcadr),
1658 peer->aorg.l_ui, peer->aorg.l_uf);
1660 } else if (peer->flip == 1 && !L_ISEQU(&p_org, &peer->borg)) {
1663 "receive: flip 1 KoD origin timestamp %#010x.%08x from %s does not match interleave %#010x.%08x - ignoring.",
1664 p_org.l_ui, p_org.l_uf,
1665 ntoa(&peer->srcadr),
1666 peer->borg.l_ui, peer->borg.l_uf);
1671 * Basic mode checks:
1673 * If there is no origin timestamp, it's either an initial packet
1674 * or we've already received a response to our query. Of course,
1675 * should 'aorg' be all-zero because this really was the original
1676 * transmit timestamp, we'll ignore this reply. There is a window
1677 * of one nanosecond once every 136 years' time where this is
1678 * possible. We currently ignore this situation.
1680 * Otherwise, check for bogus packet in basic mode.
1681 * If it is bogus, switch to interleaved mode and resynchronize,
1682 * but only after confirming the packet is not bogus in
1683 * symmetric interleaved mode.
1685 * This could also mean somebody is forging packets claiming to
1686 * be from us, attempting to cause our server to KoD us.
1688 } else if (peer->flip == 0) {
1689 INSIST(0 != hisstratum);
1690 INSIST(STRATUM_UNSPEC != hisstratum);
1693 } else if (L_ISZERO(&p_org)) {
1698 "receive: BUG 3361: Clearing peer->aorg ");
1703 /* We allow 0org for: */
1707 /* We disallow 0org for: */
1713 case MODE_BROADCAST:
1716 peer->flash |= TEST2; /* bogus */
1719 action = ""; /* for cranky compilers / MSVC */
1720 INSIST(!"receive(): impossible hismode");
1725 "receive: %s 0 origin timestamp from %s@%s xmt %#010x.%08x",
1726 action, hm_str, ntoa(&peer->srcadr),
1727 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
1728 } else if (!L_ISEQU(&p_org, &peer->aorg)) {
1729 /* are there cases here where we should bail? */
1730 /* Should we set TEST2 if we decide to try xleave? */
1732 peer->flash |= TEST2; /* bogus */
1734 "receive: Unexpected origin timestamp %#010x.%08x does not match aorg %#010x.%08x from %s@%s xmt %#010x.%08x",
1735 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1736 peer->aorg.l_ui, peer->aorg.l_uf,
1737 hm_str, ntoa(&peer->srcadr),
1738 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
1739 if ( !L_ISZERO(&peer->dst)
1740 && L_ISEQU(&p_org, &peer->dst)) {
1741 /* Might be the start of an interleave */
1742 if (dynamic_interleave) {
1744 report_event(PEVNT_XLEAVE, peer, NULL);
1747 "receive: Dynamic interleave from %s@%s denied",
1748 hm_str, ntoa(&peer->srcadr));
1756 * Check for valid nonzero timestamp fields.
1758 } else if ( L_ISZERO(&p_org)
1760 || L_ISZERO(&peer->dst)) {
1761 peer->flash |= TEST3; /* unsynch */
1764 * Check for bogus packet in interleaved symmetric mode. This
1765 * can happen if a packet is lost, duplicated or crossed. If
1766 * found, flip and resynchronize.
1768 } else if ( !L_ISZERO(&peer->dst)
1769 && !L_ISEQU(&p_org, &peer->dst)) {
1771 peer->flags |= FLAG_XBOGUS;
1772 peer->flash |= TEST2; /* bogus */
1773 return; /* Bogus packet, we are done */
1779 * If this is a crypto_NAK, the server cannot authenticate a
1780 * client packet. The server might have just changed keys. Clear
1781 * the association and restart the protocol.
1783 if (crypto_nak_test == VALIDNAK) {
1784 report_event(PEVNT_AUTH, peer, "crypto_NAK");
1785 peer->flash |= TEST5; /* bad auth */
1787 if (peer->flags & FLAG_PREEMPT) {
1788 if (unpeer_crypto_nak_early) {
1795 peer_clear(peer, "AUTH");
1797 #endif /* AUTOKEY */
1801 * If the digest fails or it's missing for authenticated
1802 * associations, the client cannot authenticate a server
1803 * reply to a client packet previously sent. The loopback check
1804 * is designed to avoid a bait-and-switch attack, which was
1805 * possible in past versions. If symmetric modes, return a
1806 * crypto-NAK. The peer should restart the protocol.
1808 } else if (!AUTH(peer->keyid || has_mac ||
1809 (restrict_mask & RES_DONTTRUST), is_authentic)) {
1811 if (peer->flash & PKT_TEST_MASK) {
1813 "receive: Bad auth in packet with bad timestamps from %s denied - spoof?",
1814 ntoa(&peer->srcadr));
1818 report_event(PEVNT_AUTH, peer, "digest");
1819 peer->flash |= TEST5; /* bad auth */
1822 && ( hismode == MODE_ACTIVE
1823 || hismode == MODE_PASSIVE))
1824 fast_xmit(rbufp, MODE_ACTIVE, 0, restrict_mask);
1825 if (peer->flags & FLAG_PREEMPT) {
1826 if (unpeer_digest_early) {
1831 else if (peer_clear_digest_early && peer->crypto) {
1832 peer_clear(peer, "AUTH");
1834 #endif /* AUTOKEY */
1839 * For broadcast packets:
1841 * HMS: This next line never made much sense to me, even
1842 * when it was up higher:
1843 * If an initial volley, bail out now and let the
1844 * client do its stuff.
1846 * If the packet has not failed authentication, then
1847 * - if the origin timestamp is nonzero this is an
1848 * interleaved broadcast, so restart the protocol.
1849 * - else, this is not an interleaved broadcast packet.
1851 if (hismode == MODE_BROADCAST) {
1852 if ( is_authentic == AUTH_OK
1853 || is_authentic == AUTH_NONE) {
1854 if (!L_ISZERO(&p_org)) {
1855 if (!(peer->flags & FLAG_XB)) {
1857 "receive: Broadcast server at %s is in interleave mode",
1858 ntoa(&peer->srcadr));
1859 peer->flags |= FLAG_XB;
1861 peer->borg = rbufp->recv_time;
1862 report_event(PEVNT_XLEAVE, peer, NULL);
1865 } else if (peer->flags & FLAG_XB) {
1867 "receive: Broadcast server at %s is no longer in interleave mode",
1868 ntoa(&peer->srcadr));
1869 peer->flags &= ~FLAG_XB;
1873 "receive: Bad broadcast auth (%d) from %s",
1874 is_authentic, ntoa(&peer->srcadr));
1878 * Now that we know the packet is correctly authenticated,
1879 * update peer->bxmt.
1886 ** Update the state variables.
1888 if (peer->flip == 0) {
1889 if (hismode != MODE_BROADCAST)
1891 peer->dst = rbufp->recv_time;
1896 * Set the peer ppoll to the maximum of the packet ppoll and the
1897 * peer minpoll. If a kiss-o'-death, set the peer minpoll to
1898 * this maximum and advance the headway to give the sender some
1899 * headroom. Very intricate.
1903 * Check for any kiss codes. Note this is only used when a server
1904 * responds to a packet request
1907 kissCode = kiss_code_check(hisleap, hisstratum, hismode, pkt->refid);
1910 * Check to see if this is a RATE Kiss Code
1911 * Currently this kiss code will accept whatever poll
1912 * rate that the server sends
1914 peer->ppoll = max(peer->minpoll, pkt->ppoll);
1915 if (kissCode == RATEKISS) {
1916 peer->selbroken++; /* Increment the KoD count */
1917 report_event(PEVNT_RATE, peer, NULL);
1918 if (pkt->ppoll > peer->minpoll)
1919 peer->minpoll = peer->ppoll;
1920 peer->burst = peer->retry = 0;
1921 peer->throttle = (NTP_SHIFT + 1) * (1 << peer->minpoll);
1922 poll_update(peer, pkt->ppoll);
1923 return; /* kiss-o'-death */
1925 if (kissCode != NOKISS) {
1926 peer->selbroken++; /* Increment the KoD count */
1927 return; /* Drop any other kiss code packets */
1938 * - this is a *cast (uni-, broad-, or m-) server packet
1939 * - and it's symmetric-key authenticated
1940 * then see if the sender's IP is trusted for this keyid.
1941 * If it is, great - nothing special to do here.
1942 * Otherwise, we should report and bail.
1944 * Autokey-authenticated packets are accepted.
1948 case MODE_SERVER: /* server mode */
1949 case MODE_BROADCAST: /* broadcast mode */
1950 case MODE_ACTIVE: /* symmetric active mode */
1951 case MODE_PASSIVE: /* symmetric passive mode */
1952 if ( is_authentic == AUTH_OK
1954 && skeyid <= NTP_MAXKEY
1955 && !authistrustedip(skeyid, &peer->srcadr)) {
1956 report_event(PEVNT_AUTH, peer, "authIP");
1962 case MODE_CLIENT: /* client mode */
1963 #if 0 /* At this point, MODE_CONTROL is overloaded by MODE_BCLIENT */
1964 case MODE_CONTROL: /* control mode */
1966 case MODE_PRIVATE: /* private mode */
1967 case MODE_BCLIENT: /* broadcast client mode */
1970 case MODE_UNSPEC: /* unspecified (old version) */
1973 "receive: Unexpected mode (%d) in packet from %s",
1974 hismode, ntoa(&peer->srcadr));
1980 * That was hard and I am sweaty, but the packet is squeaky
1981 * clean. Get on with real work.
1983 peer->timereceived = current_time;
1984 peer->timelastrec = current_time;
1985 if (is_authentic == AUTH_OK)
1986 peer->flags |= FLAG_AUTHENTIC;
1988 peer->flags &= ~FLAG_AUTHENTIC;
1992 * More autokey dance. The rules of the cha-cha are as follows:
1994 * 1. If there is no key or the key is not auto, do nothing.
1996 * 2. If this packet is in response to the one just previously
1997 * sent or from a broadcast server, do the extension fields.
1998 * Otherwise, assume bogosity and bail out.
2000 * 3. If an extension field contains a verified signature, it is
2001 * self-authenticated and we sit the dance.
2003 * 4. If this is a server reply, check only to see that the
2004 * transmitted key ID matches the received key ID.
2006 * 5. Check to see that one or more hashes of the current key ID
2007 * matches the previous key ID or ultimate original key ID
2008 * obtained from the broadcaster or symmetric peer. If no
2009 * match, sit the dance and call for new autokey values.
2011 * In case of crypto error, fire the orchestra, stop dancing and
2012 * restart the protocol.
2014 if (peer->flags & FLAG_SKEY) {
2016 * Decrement remaining autokey hashes. This isn't
2017 * perfect if a packet is lost, but results in no harm.
2019 ap = (struct autokey *)peer->recval.ptr;
2024 peer->flash |= TEST8;
2025 rval = crypto_recv(peer, rbufp);
2026 if (rval == XEVNT_OK) {
2029 if (rval == XEVNT_ERR) {
2030 report_event(PEVNT_RESTART, peer,
2032 peer_clear(peer, "CRYP");
2033 peer->flash |= TEST9; /* bad crypt */
2034 if (peer->flags & FLAG_PREEMPT) {
2035 if (unpeer_crypto_early) {
2044 * If server mode, verify the receive key ID matches
2045 * the transmit key ID.
2047 if (hismode == MODE_SERVER) {
2048 if (skeyid == peer->keyid)
2049 peer->flash &= ~TEST8;
2052 * If an extension field is present, verify only that it
2053 * has been correctly signed. We don't need a sequence
2054 * check here, but the sequence continues.
2056 } else if (!(peer->flash & TEST8)) {
2057 peer->pkeyid = skeyid;
2060 * Now the fun part. Here, skeyid is the current ID in
2061 * the packet, pkeyid is the ID in the last packet and
2062 * tkeyid is the hash of skeyid. If the autokey values
2063 * have not been received, this is an automatic error.
2064 * If so, check that the tkeyid matches pkeyid. If not,
2065 * hash tkeyid and try again. If the number of hashes
2066 * exceeds the number remaining in the sequence, declare
2067 * a successful failure and refresh the autokey values.
2069 } else if (ap != NULL) {
2072 for (i = 0; ; i++) {
2073 if ( tkeyid == peer->pkeyid
2074 || tkeyid == ap->key) {
2075 peer->flash &= ~TEST8;
2076 peer->pkeyid = skeyid;
2085 tkeyid = session_key(
2086 &rbufp->recv_srcadr, dstadr_sin,
2089 if (peer->flash & TEST8)
2090 report_event(PEVNT_AUTH, peer, "keylist");
2092 if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
2093 peer->flash |= TEST8; /* bad autokey */
2096 * The maximum lifetime of the protocol is about one
2097 * week before restarting the Autokey protocol to
2098 * refresh certificates and leapseconds values.
2100 if (current_time > peer->refresh) {
2101 report_event(PEVNT_RESTART, peer,
2103 peer_clear(peer, "TIME");
2107 #endif /* AUTOKEY */
2110 * The dance is complete and the flash bits have been lit. Toss
2111 * the packet over the fence for processing, which may light up
2114 process_packet(peer, pkt, rbufp->recv_length);
2117 * In interleaved mode update the state variables. Also adjust the
2118 * transmit phase to avoid crossover.
2120 if (peer->flip != 0) {
2122 peer->dst = rbufp->recv_time;
2123 if (peer->nextdate - current_time < (1U << min(peer->ppoll,
2133 * process_packet - Packet Procedure, a la Section 3.4.4 of RFC-1305
2134 * Or almost, at least. If we're in here we have a reasonable
2135 * expectation that we will be having a long term
2136 * relationship with this host.
2140 register struct peer *peer,
2141 register struct pkt *pkt,
2146 double p_offset, p_del, p_disp;
2147 l_fp p_rec, p_xmt, p_org, p_reftime, ci;
2148 u_char pmode, pleap, pversion, pstratum;
2149 char statstr[NTP_MAXSTRLEN];
2152 double etemp, ftemp, td;
2159 p_del = FPTOD(NTOHS_FP(pkt->rootdelay));
2161 p_disp = FPTOD(NTOHS_FP(pkt->rootdisp));
2162 NTOHL_FP(&pkt->reftime, &p_reftime);
2163 NTOHL_FP(&pkt->org, &p_org);
2164 NTOHL_FP(&pkt->rec, &p_rec);
2165 NTOHL_FP(&pkt->xmt, &p_xmt);
2166 pmode = PKT_MODE(pkt->li_vn_mode);
2167 pleap = PKT_LEAP(pkt->li_vn_mode);
2168 pversion = PKT_VERSION(pkt->li_vn_mode);
2169 pstratum = PKT_TO_STRATUM(pkt->stratum);
2176 * Verify the server is synchronized; that is, the leap bits,
2177 * stratum and root distance are valid.
2179 if ( pleap == LEAP_NOTINSYNC /* test 6 */
2180 || pstratum < sys_floor || pstratum >= sys_ceiling)
2181 peer->flash |= TEST6; /* bad synch or strat */
2182 if (p_del / 2 + p_disp >= MAXDISPERSE) /* test 7 */
2183 peer->flash |= TEST7; /* bad header */
2186 * If any tests fail at this point, the packet is discarded.
2187 * Note that some flashers may have already been set in the
2188 * receive() routine.
2190 if (peer->flash & PKT_TEST_MASK) {
2191 peer->seldisptoolarge++;
2192 DPRINTF(1, ("packet: flash header %04x\n",
2205 * Capture the header values in the client/peer association..
2207 record_raw_stats(&peer->srcadr, peer->dstadr ?
2208 &peer->dstadr->sin : NULL,
2209 &p_org, &p_rec, &p_xmt, &peer->dst,
2210 pleap, pversion, pmode, pstratum, pkt->ppoll, pkt->precision,
2211 p_del, p_disp, pkt->refid);
2213 peer->stratum = min(pstratum, STRATUM_UNSPEC);
2214 peer->pmode = pmode;
2215 peer->precision = pkt->precision;
2216 peer->rootdelay = p_del;
2217 peer->rootdisp = p_disp;
2218 peer->refid = pkt->refid; /* network byte order */
2219 peer->reftime = p_reftime;
2222 * First, if either burst mode is armed, enable the burst.
2223 * Compute the headway for the next packet and delay if
2224 * necessary to avoid exceeding the threshold.
2226 if (peer->retry > 0) {
2229 peer->burst = min(1 << (peer->hpoll -
2230 peer->minpoll), NTP_SHIFT) - 1;
2232 peer->burst = NTP_IBURST - 1;
2233 if (peer->burst > 0)
2234 peer->nextdate = current_time;
2236 poll_update(peer, peer->hpoll);
2241 * If the peer was previously unreachable, raise a trap. In any
2242 * case, mark it reachable.
2245 report_event(PEVNT_REACH, peer, NULL);
2246 peer->timereachable = current_time;
2251 * For a client/server association, calculate the clock offset,
2252 * roundtrip delay and dispersion. The equations are reordered
2253 * from the spec for more efficient use of temporaries. For a
2254 * broadcast association, offset the last measurement by the
2255 * computed delay during the client/server volley. Note the
2256 * computation of dispersion includes the system precision plus
2257 * that due to the frequency error since the origin time.
2259 * It is very important to respect the hazards of overflow. The
2260 * only permitted operation on raw timestamps is subtraction,
2261 * where the result is a signed quantity spanning from 68 years
2262 * in the past to 68 years in the future. To avoid loss of
2263 * precision, these calculations are done using 64-bit integer
2264 * arithmetic. However, the offset and delay calculations are
2265 * sums and differences of these first-order differences, which
2266 * if done using 64-bit integer arithmetic, would be valid over
2267 * only half that span. Since the typical first-order
2268 * differences are usually very small, they are converted to 64-
2269 * bit doubles and all remaining calculations done in floating-
2270 * double arithmetic. This preserves the accuracy while
2271 * retaining the 68-year span.
2273 * There are three interleaving schemes, basic, interleaved
2274 * symmetric and interleaved broadcast. The timestamps are
2275 * idioscyncratically different. See the onwire briefing/white
2276 * paper at www.eecis.udel.edu/~mills for details.
2278 * Interleaved symmetric mode
2279 * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
2282 if (peer->flip != 0) {
2283 ci = p_xmt; /* t3 - t4 */
2284 L_SUB(&ci, &peer->dst);
2286 ci = p_rec; /* t2 - t1 */
2288 L_SUB(&ci, &peer->borg);
2290 L_SUB(&ci, &peer->aorg);
2293 p_offset = (t21 + t34) / 2.;
2294 if (p_del < 0 || p_del > 1.) {
2295 snprintf(statstr, sizeof(statstr),
2296 "t21 %.6f t34 %.6f", t21, t34);
2297 report_event(PEVNT_XERR, peer, statstr);
2304 } else if (peer->pmode == MODE_BROADCAST) {
2307 * Interleaved broadcast mode. Use interleaved timestamps.
2308 * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
2310 if (peer->flags & FLAG_XB) {
2311 ci = p_org; /* delay */
2312 L_SUB(&ci, &peer->aorg);
2314 ci = p_org; /* t2 - t1 */
2315 L_SUB(&ci, &peer->borg);
2318 peer->borg = peer->dst;
2319 if (t34 < 0 || t34 > 1.) {
2320 /* drop all if in the initial volley */
2321 if (FLAG_BC_VOL & peer->flags)
2322 goto bcc_init_volley_fail;
2323 snprintf(statstr, sizeof(statstr),
2324 "offset %.6f delay %.6f", t21, t34);
2325 report_event(PEVNT_XERR, peer, statstr);
2332 * Basic broadcast - use direct timestamps.
2333 * t3 = p_xmt, t4 = peer->dst
2336 ci = p_xmt; /* t3 - t4 */
2337 L_SUB(&ci, &peer->dst);
2343 * When calibration is complete and the clock is
2344 * synchronized, the bias is calculated as the difference
2345 * between the unicast timestamp and the broadcast
2346 * timestamp. This works for both basic and interleaved
2348 * [Bug 3031] Don't keep this peer when the delay
2349 * calculation gives reason to suspect clock steps.
2350 * This is assumed for delays > 50ms.
2352 if (FLAG_BC_VOL & peer->flags) {
2353 peer->flags &= ~FLAG_BC_VOL;
2354 peer->delay = fabs(peer->offset - p_offset) * 2;
2355 DPRINTF(2, ("broadcast volley: initial delay=%.6f\n",
2357 if (peer->delay > fabs(sys_bdelay)) {
2358 bcc_init_volley_fail:
2359 DPRINTF(2, ("%s", "broadcast volley: initial delay exceeds limit\n"));
2364 peer->nextdate = current_time + (1u << peer->ppoll) - 2u;
2365 p_del = peer->delay;
2366 p_offset += p_del / 2;
2370 * Basic mode, otherwise known as the old fashioned way.
2372 * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2375 ci = p_xmt; /* t3 - t4 */
2376 L_SUB(&ci, &peer->dst);
2378 ci = p_rec; /* t2 - t1 */
2381 p_del = fabs(t21 - t34);
2382 p_offset = (t21 + t34) / 2.;
2384 p_del = max(p_del, LOGTOD(sys_precision));
2385 p_disp = LOGTOD(sys_precision) + LOGTOD(peer->precision) +
2390 * This code calculates the outbound and inbound data rates by
2391 * measuring the differences between timestamps at different
2392 * packet lengths. This is helpful in cases of large asymmetric
2393 * delays commonly experienced on deep space communication
2396 if (peer->t21_last > 0 && peer->t34_bytes > 0) {
2397 itemp = peer->t21_bytes - peer->t21_last;
2399 etemp = t21 - peer->t21;
2400 if (fabs(etemp) > 1e-6) {
2401 ftemp = itemp / etemp;
2406 itemp = len - peer->t34_bytes;
2408 etemp = -t34 - peer->t34;
2409 if (fabs(etemp) > 1e-6) {
2410 ftemp = itemp / etemp;
2418 * The following section compensates for different data rates on
2419 * the outbound (d21) and inbound (t34) directions. To do this,
2420 * it finds t such that r21 * t - r34 * (d - t) = 0, where d is
2421 * the roundtrip delay. Then it calculates the correction as a
2425 peer->t21_last = peer->t21_bytes;
2427 peer->t34_bytes = len;
2428 DPRINTF(2, ("packet: t21 %.9lf %d t34 %.9lf %d\n", peer->t21,
2429 peer->t21_bytes, peer->t34, peer->t34_bytes));
2430 if (peer->r21 > 0 && peer->r34 > 0 && p_del > 0) {
2431 if (peer->pmode != MODE_BROADCAST)
2432 td = (peer->r34 / (peer->r21 + peer->r34) -
2438 * Unfortunately, in many cases the errors are
2439 * unacceptable, so for the present the rates are not
2440 * used. In future, we might find conditions where the
2441 * calculations are useful, so this should be considered
2442 * a work in progress.
2446 DPRINTF(2, ("packet: del %.6lf r21 %.1lf r34 %.1lf %.6lf\n",
2447 p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2453 * That was awesome. Now hand off to the clock filter.
2455 clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2458 * If we are in broadcast calibrate mode, return to broadcast
2459 * client mode when the client is fit and the autokey dance is
2462 if ( (FLAG_BC_VOL & peer->flags)
2463 && MODE_CLIENT == peer->hmode
2464 && !(TEST11 & peer_unfit(peer))) { /* distance exceeded */
2466 if (peer->flags & FLAG_SKEY) {
2467 if (!(~peer->crypto & CRYPTO_FLAG_ALL))
2468 peer->hmode = MODE_BCLIENT;
2470 peer->hmode = MODE_BCLIENT;
2472 #else /* !AUTOKEY follows */
2473 peer->hmode = MODE_BCLIENT;
2474 #endif /* !AUTOKEY */
2480 * clock_update - Called at system process update intervals.
2484 struct peer *peer /* peer structure pointer */
2489 #ifdef HAVE_LIBSCF_H
2491 #endif /* HAVE_LIBSCF_H */
2494 * Update the system state variables. We do this very carefully,
2495 * as the poll interval might need to be clamped differently.
2498 sys_epoch = peer->epoch;
2499 if (sys_poll < peer->minpoll)
2500 sys_poll = peer->minpoll;
2501 if (sys_poll > peer->maxpoll)
2502 sys_poll = peer->maxpoll;
2503 poll_update(peer, sys_poll);
2504 sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
2505 if ( peer->stratum == STRATUM_REFCLOCK
2506 || peer->stratum == STRATUM_UNSPEC)
2507 sys_refid = peer->refid;
2509 sys_refid = addr2refid(&peer->srcadr);
2511 * Root Dispersion (E) is defined (in RFC 5905) as:
2513 * E = p.epsilon_r + p.epsilon + p.psi + PHI*(s.t - p.t) + |THETA|
2516 * p.epsilon_r is the PollProc's root dispersion
2517 * p.epsilon is the PollProc's dispersion
2518 * p.psi is the PollProc's jitter
2519 * THETA is the combined offset
2521 * NB: Think Hard about where these numbers come from and
2522 * what they mean. When did peer->update happen? Has anything
2523 * interesting happened since then? What values are the most
2526 * DLM thinks this equation is probably the best of all worse choices.
2528 dtemp = peer->rootdisp
2531 + clock_phi * (current_time - peer->update)
2534 if (dtemp > sys_mindisp)
2535 sys_rootdisp = dtemp;
2537 sys_rootdisp = sys_mindisp;
2538 sys_rootdelay = peer->delay + peer->rootdelay;
2539 sys_reftime = peer->dst;
2541 DPRINTF(1, ("clock_update: at %lu sample %lu associd %d\n",
2542 current_time, peer->epoch, peer->associd));
2545 * Comes now the moment of truth. Crank the clock discipline and
2546 * see what comes out.
2548 switch (local_clock(peer, sys_offset)) {
2551 * Clock exceeds panic threshold. Life as we know it ends.
2554 #ifdef HAVE_LIBSCF_H
2556 * For Solaris enter the maintenance mode.
2558 if ((fmri = getenv("SMF_FMRI")) != NULL) {
2559 if (smf_maintain_instance(fmri, 0) < 0) {
2560 printf("smf_maintain_instance: %s\n",
2561 scf_strerror(scf_error()));
2565 * Sleep until SMF kills us.
2570 #endif /* HAVE_LIBSCF_H */
2575 * Clock was stepped. Flush all time values of all peers.
2579 set_sys_leap(LEAP_NOTINSYNC);
2580 sys_stratum = STRATUM_UNSPEC;
2581 memcpy(&sys_refid, "STEP", 4);
2584 L_CLR(&sys_reftime);
2585 sys_jitter = LOGTOD(sys_precision);
2586 leapsec_reset_frame();
2590 * Clock was slewed. Handle the leapsecond stuff.
2595 * If this is the first time the clock is set, reset the
2596 * leap bits. If crypto, the timer will goose the setup
2599 if (sys_leap == LEAP_NOTINSYNC) {
2600 set_sys_leap(LEAP_NOWARNING);
2604 #endif /* AUTOKEY */
2606 * If our parent process is waiting for the
2607 * first clock sync, send them home satisfied.
2609 #ifdef HAVE_WORKING_FORK
2610 if (waitsync_fd_to_close != -1) {
2611 close(waitsync_fd_to_close);
2612 waitsync_fd_to_close = -1;
2613 DPRINTF(1, ("notified parent --wait-sync is done\n"));
2615 #endif /* HAVE_WORKING_FORK */
2620 * If there is no leap second pending and the number of
2621 * survivor leap bits is greater than half the number of
2622 * survivors, try to schedule a leap for the end of the
2623 * current month. (This only works if no leap second for
2624 * that range is in the table, so doing this more than
2625 * once is mostly harmless.)
2627 if (leapsec == LSPROX_NOWARN) {
2628 if ( leap_vote_ins > leap_vote_del
2629 && leap_vote_ins > sys_survivors / 2) {
2631 leapsec_add_dyn(TRUE, now.l_ui, NULL);
2633 if ( leap_vote_del > leap_vote_ins
2634 && leap_vote_del > sys_survivors / 2) {
2636 leapsec_add_dyn(FALSE, now.l_ui, NULL);
2642 * Popcorn spike or step threshold exceeded. Pretend it never
2652 * poll_update - update peer poll interval
2656 struct peer *peer, /* peer structure pointer */
2664 * This routine figures out when the next poll should be sent.
2665 * That turns out to be wickedly complicated. One problem is
2666 * that sometimes the time for the next poll is in the past when
2667 * the poll interval is reduced. We watch out for races here
2668 * between the receive process and the poll process.
2670 * Clamp the poll interval between minpoll and maxpoll.
2672 hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
2676 * If during the crypto protocol the poll interval has changed,
2677 * the lifetimes in the key list are probably bogus. Purge the
2678 * the key list and regenerate it later.
2680 if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
2682 #endif /* AUTOKEY */
2683 peer->hpoll = hpoll;
2686 * There are three variables important for poll scheduling, the
2687 * current time (current_time), next scheduled time (nextdate)
2688 * and the earliest time (utemp). The earliest time is 2 s
2689 * seconds, but could be more due to rate management. When
2690 * sending in a burst, use the earliest time. When not in a
2691 * burst but with a reply pending, send at the earliest time
2692 * unless the next scheduled time has not advanced. This can
2693 * only happen if multiple replies are pending in the same
2694 * response interval. Otherwise, send at the later of the next
2695 * scheduled time and the earliest time.
2697 * Now we figure out if there is an override. If a burst is in
2698 * progress and we get called from the receive process, just
2699 * slink away. If called from the poll process, delay 1 s for a
2700 * reference clock, otherwise 2 s.
2702 utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
2703 (1 << peer->minpoll), ntp_minpkt);
2704 if (peer->burst > 0) {
2705 if (peer->nextdate > current_time)
2708 else if (peer->flags & FLAG_REFCLOCK)
2709 peer->nextdate = current_time + RESP_DELAY;
2710 #endif /* REFCLOCK */
2712 peer->nextdate = utemp;
2716 * If a burst is not in progress and a crypto response message
2717 * is pending, delay 2 s, but only if this is a new interval.
2719 } else if (peer->cmmd != NULL) {
2720 if (peer->nextdate > current_time) {
2721 if (peer->nextdate + ntp_minpkt != utemp)
2722 peer->nextdate = utemp;
2724 peer->nextdate = utemp;
2726 #endif /* AUTOKEY */
2729 * The ordinary case. If a retry, use minpoll; if unreachable,
2730 * use host poll; otherwise, use the minimum of host and peer
2731 * polls; In other words, oversampling is okay but
2732 * understampling is evil. Use the maximum of this value and the
2733 * headway. If the average headway is greater than the headway
2734 * threshold, increase the headway by the minimum interval.
2737 if (peer->retry > 0)
2738 hpoll = peer->minpoll;
2739 else if (!(peer->reach))
2740 hpoll = peer->hpoll;
2742 hpoll = min(peer->ppoll, peer->hpoll);
2744 if (peer->flags & FLAG_REFCLOCK)
2747 #endif /* REFCLOCK */
2748 next = ((0x1000UL | (ntp_random() & 0x0ff)) <<
2750 next += peer->outdate;
2752 peer->nextdate = next;
2754 peer->nextdate = utemp;
2755 if (peer->throttle > (1 << peer->minpoll))
2756 peer->nextdate += ntp_minpkt;
2758 DPRINTF(2, ("poll_update: at %lu %s poll %d burst %d retry %d head %d early %lu next %lu\n",
2759 current_time, ntoa(&peer->srcadr), peer->hpoll,
2760 peer->burst, peer->retry, peer->throttle,
2761 utemp - current_time, peer->nextdate -
2767 * peer_clear - clear peer filter registers. See Section 3.4.8 of the
2772 struct peer *peer, /* peer structure */
2773 const char *ident /* tally lights */
2777 l_fp bxmt = peer->bxmt; /* bcast clients retain this! */
2781 * If cryptographic credentials have been acquired, toss them to
2782 * Valhalla. Note that autokeys are ephemeral, in that they are
2783 * tossed immediately upon use. Therefore, the keylist can be
2784 * purged anytime without needing to preserve random keys. Note
2785 * that, if the peer is purged, the cryptographic variables are
2786 * purged, too. This makes it much harder to sneak in some
2787 * unauthenticated data in the clock filter.
2790 if (peer->iffval != NULL)
2791 BN_free(peer->iffval);
2792 value_free(&peer->cookval);
2793 value_free(&peer->recval);
2794 value_free(&peer->encrypt);
2795 value_free(&peer->sndval);
2796 if (peer->cmmd != NULL)
2798 if (peer->subject != NULL)
2799 free(peer->subject);
2800 if (peer->issuer != NULL)
2802 #endif /* AUTOKEY */
2805 * Clear all values, including the optional crypto values above.
2807 memset(CLEAR_TO_ZERO(peer), 0, LEN_CLEAR_TO_ZERO(peer));
2808 peer->ppoll = peer->maxpoll;
2809 peer->hpoll = peer->minpoll;
2810 peer->disp = MAXDISPERSE;
2811 peer->flash = peer_unfit(peer);
2812 peer->jitter = LOGTOD(sys_precision);
2814 /* Don't throw away our broadcast replay protection */
2815 if (peer->hmode == MODE_BCLIENT)
2819 * If interleave mode, initialize the alternate origin switch.
2821 if (peer->flags & FLAG_XLEAVE)
2823 for (u = 0; u < NTP_SHIFT; u++) {
2824 peer->filter_order[u] = u;
2825 peer->filter_disp[u] = MAXDISPERSE;
2828 if (!(peer->flags & FLAG_REFCLOCK)) {
2830 peer->leap = LEAP_NOTINSYNC;
2831 peer->stratum = STRATUM_UNSPEC;
2832 memcpy(&peer->refid, ident, 4);
2838 * During initialization use the association count to spread out
2839 * the polls at one-second intervals. Passive associations'
2840 * first poll is delayed by the "discard minimum" to avoid rate
2841 * limiting. Other post-startup new or cleared associations
2842 * randomize the first poll over the minimum poll interval to
2845 peer->nextdate = peer->update = peer->outdate = current_time;
2847 peer->nextdate += peer_associations;
2848 } else if (MODE_PASSIVE == peer->hmode) {
2849 peer->nextdate += ntp_minpkt;
2851 peer->nextdate += ntp_random() % peer->minpoll;
2854 peer->refresh = current_time + (1 << NTP_REFRESH);
2855 #endif /* AUTOKEY */
2856 DPRINTF(1, ("peer_clear: at %ld next %ld associd %d refid %s\n",
2857 current_time, peer->nextdate, peer->associd,
2863 * clock_filter - add incoming clock sample to filter register and run
2864 * the filter procedure to find the best sample.
2868 struct peer *peer, /* peer structure pointer */
2869 double sample_offset, /* clock offset */
2870 double sample_delay, /* roundtrip delay */
2871 double sample_disp /* dispersion */
2874 double dst[NTP_SHIFT]; /* distance vector */
2875 int ord[NTP_SHIFT]; /* index vector */
2877 double dtemp, etemp;
2881 * A sample consists of the offset, delay, dispersion and epoch
2882 * of arrival. The offset and delay are determined by the on-
2883 * wire protocol. The dispersion grows from the last outbound
2884 * packet to the arrival of this one increased by the sum of the
2885 * peer precision and the system precision as required by the
2886 * error budget. First, shift the new arrival into the shift
2887 * register discarding the oldest one.
2889 j = peer->filter_nextpt;
2890 peer->filter_offset[j] = sample_offset;
2891 peer->filter_delay[j] = sample_delay;
2892 peer->filter_disp[j] = sample_disp;
2893 peer->filter_epoch[j] = current_time;
2894 j = (j + 1) % NTP_SHIFT;
2895 peer->filter_nextpt = j;
2898 * Update dispersions since the last update and at the same
2899 * time initialize the distance and index lists. Since samples
2900 * become increasingly uncorrelated beyond the Allan intercept,
2901 * only under exceptional cases will an older sample be used.
2902 * Therefore, the distance list uses a compound metric. If the
2903 * dispersion is greater than the maximum dispersion, clamp the
2904 * distance at that value. If the time since the last update is
2905 * less than the Allan intercept use the delay; otherwise, use
2906 * the sum of the delay and dispersion.
2908 dtemp = clock_phi * (current_time - peer->update);
2909 peer->update = current_time;
2910 for (i = NTP_SHIFT - 1; i >= 0; i--) {
2912 peer->filter_disp[j] += dtemp;
2913 if (peer->filter_disp[j] >= MAXDISPERSE) {
2914 peer->filter_disp[j] = MAXDISPERSE;
2915 dst[i] = MAXDISPERSE;
2916 } else if (peer->update - peer->filter_epoch[j] >
2917 (u_long)ULOGTOD(allan_xpt)) {
2918 dst[i] = peer->filter_delay[j] +
2919 peer->filter_disp[j];
2921 dst[i] = peer->filter_delay[j];
2924 j = (j + 1) % NTP_SHIFT;
2928 * If the clock has stabilized, sort the samples by distance.
2930 if (freq_cnt == 0) {
2931 for (i = 1; i < NTP_SHIFT; i++) {
2932 for (j = 0; j < i; j++) {
2933 if (dst[j] > dst[i]) {
2946 * Copy the index list to the association structure so ntpq
2947 * can see it later. Prune the distance list to leave only
2948 * samples less than the maximum dispersion, which disfavors
2949 * uncorrelated samples older than the Allan intercept. To
2950 * further improve the jitter estimate, of the remainder leave
2951 * only samples less than the maximum distance, but keep at
2952 * least two samples for jitter calculation.
2955 for (i = 0; i < NTP_SHIFT; i++) {
2956 peer->filter_order[i] = (u_char) ord[i];
2957 if ( dst[i] >= MAXDISPERSE
2958 || (m >= 2 && dst[i] >= sys_maxdist))
2964 * Compute the dispersion and jitter. The dispersion is weighted
2965 * exponentially by NTP_FWEIGHT (0.5) so it is normalized close
2966 * to 1.0. The jitter is the RMS differences relative to the
2967 * lowest delay sample.
2969 peer->disp = peer->jitter = 0;
2971 for (i = NTP_SHIFT - 1; i >= 0; i--) {
2973 peer->disp = NTP_FWEIGHT * (peer->disp +
2974 peer->filter_disp[j]);
2976 peer->jitter += DIFF(peer->filter_offset[j],
2977 peer->filter_offset[k]);
2981 * If no acceptable samples remain in the shift register,
2982 * quietly tiptoe home leaving only the dispersion. Otherwise,
2983 * save the offset, delay and jitter. Note the jitter must not
2984 * be less than the precision.
2990 etemp = fabs(peer->offset - peer->filter_offset[k]);
2991 peer->offset = peer->filter_offset[k];
2992 peer->delay = peer->filter_delay[k];
2994 peer->jitter /= m - 1;
2995 peer->jitter = max(SQRT(peer->jitter), LOGTOD(sys_precision));
2998 * If the the new sample and the current sample are both valid
2999 * and the difference between their offsets exceeds CLOCK_SGATE
3000 * (3) times the jitter and the interval between them is less
3001 * than twice the host poll interval, consider the new sample
3002 * a popcorn spike and ignore it.
3004 if ( peer->disp < sys_maxdist
3005 && peer->filter_disp[k] < sys_maxdist
3006 && etemp > CLOCK_SGATE * peer->jitter
3007 && peer->filter_epoch[k] - peer->epoch
3008 < 2. * ULOGTOD(peer->hpoll)) {
3009 snprintf(tbuf, sizeof(tbuf), "%.6f s", etemp);
3010 report_event(PEVNT_POPCORN, peer, tbuf);
3015 * A new minimum sample is useful only if it is later than the
3016 * last one used. In this design the maximum lifetime of any
3017 * sample is not greater than eight times the poll interval, so
3018 * the maximum interval between minimum samples is eight
3021 if (peer->filter_epoch[k] <= peer->epoch) {
3022 DPRINTF(2, ("clock_filter: old sample %lu\n", current_time -
3023 peer->filter_epoch[k]));
3026 peer->epoch = peer->filter_epoch[k];
3029 * The mitigated sample statistics are saved for later
3030 * processing. If not synchronized or not in a burst, tickle the
3031 * clock select algorithm.
3033 record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
3034 peer->offset, peer->delay, peer->disp, peer->jitter);
3035 DPRINTF(1, ("clock_filter: n %d off %.6f del %.6f dsp %.6f jit %.6f\n",
3036 m, peer->offset, peer->delay, peer->disp,
3038 if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC)
3044 * clock_select - find the pick-of-the-litter clock
3046 * LOCKCLOCK: (1) If the local clock is the prefer peer, it will always
3047 * be enabled, even if declared falseticker, (2) only the prefer peer
3048 * can be selected as the system peer, (3) if the external source is
3049 * down, the system leap bits are set to 11 and the stratum set to
3063 double orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
3064 struct endpoint endp;
3065 struct peer *osys_peer;
3066 struct peer *sys_prefer = NULL; /* prefer peer */
3067 struct peer *typesystem = NULL;
3068 struct peer *typeorphan = NULL;
3070 struct peer *typeacts = NULL;
3071 struct peer *typelocal = NULL;
3072 struct peer *typepps = NULL;
3073 #endif /* REFCLOCK */
3074 static struct endpoint *endpoint = NULL;
3075 static int *indx = NULL;
3076 static peer_select *peers = NULL;
3077 static u_int endpoint_size = 0;
3078 static u_int peers_size = 0;
3079 static u_int indx_size = 0;
3083 * Initialize and create endpoint, index and peer lists big
3084 * enough to handle all associations.
3086 osys_peer = sys_peer;
3089 set_sys_leap(LEAP_NOTINSYNC);
3090 sys_stratum = STRATUM_UNSPEC;
3091 memcpy(&sys_refid, "DOWN", 4);
3092 #endif /* LOCKCLOCK */
3095 * Allocate dynamic space depending on the number of
3099 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3101 endpoint_size = ALIGNED_SIZE(nlist * 2 * sizeof(*endpoint));
3102 peers_size = ALIGNED_SIZE(nlist * sizeof(*peers));
3103 indx_size = ALIGNED_SIZE(nlist * 2 * sizeof(*indx));
3104 octets = endpoint_size + peers_size + indx_size;
3105 endpoint = erealloc(endpoint, octets);
3106 peers = INC_ALIGNED_PTR(endpoint, endpoint_size);
3107 indx = INC_ALIGNED_PTR(peers, peers_size);
3110 * Initially, we populate the island with all the rifraff peers
3111 * that happen to be lying around. Those with seriously
3112 * defective clocks are immediately booted off the island. Then,
3113 * the falsetickers are culled and put to sea. The truechimers
3114 * remaining are subject to repeated rounds where the most
3115 * unpopular at each round is kicked off. When the population
3116 * has dwindled to sys_minclock, the survivors split a million
3117 * bucks and collectively crank the chimes.
3119 nlist = nl2 = 0; /* none yet */
3120 for (peer = peer_list; peer != NULL; peer = peer->p_link) {
3121 peer->new_status = CTL_PST_SEL_REJECT;
3124 * Leave the island immediately if the peer is
3125 * unfit to synchronize.
3127 if (peer_unfit(peer)) {
3132 * If this peer is an orphan parent, elect the
3133 * one with the lowest metric defined as the
3134 * IPv4 address or the first 64 bits of the
3135 * hashed IPv6 address. To ensure convergence
3136 * on the same selected orphan, consider as
3137 * well that this system may have the lowest
3138 * metric and be the orphan parent. If this
3139 * system wins, sys_peer will be NULL to trigger
3140 * orphan mode in timer().
3142 if (peer->stratum == sys_orphan) {
3146 if (peer->dstadr != NULL)
3147 localmet = ntohl(peer->dstadr->addr_refid);
3149 localmet = U_INT32_MAX;
3150 peermet = ntohl(addr2refid(&peer->srcadr));
3151 if (peermet < localmet && peermet < orphmet) {
3159 * If this peer could have the orphan parent
3160 * as a synchronization ancestor, exclude it
3161 * from selection to avoid forming a
3162 * synchronization loop within the orphan mesh,
3163 * triggering stratum climb to infinity
3164 * instability. Peers at stratum higher than
3165 * the orphan stratum could have the orphan
3166 * parent in ancestry so are excluded.
3167 * See http://bugs.ntp.org/2050
3169 if (peer->stratum > sys_orphan) {
3174 * The following are special cases. We deal
3177 if (!(peer->flags & FLAG_PREFER)) {
3178 switch (peer->refclktype) {
3179 case REFCLK_LOCALCLOCK:
3180 if ( current_time > orphwait
3181 && typelocal == NULL)
3186 if ( current_time > orphwait
3187 && typeacts == NULL)
3192 #endif /* REFCLOCK */
3195 * If we get this far, the peer can stay on the
3196 * island, but does not yet have the immunity
3199 peer->new_status = CTL_PST_SEL_SANE;
3200 f = root_distance(peer);
3201 peers[nlist].peer = peer;
3202 peers[nlist].error = peer->jitter;
3203 peers[nlist].synch = f;
3207 * Insert each interval endpoint on the unsorted
3211 endpoint[nl2].type = -1; /* lower end */
3212 endpoint[nl2].val = e - f;
3214 endpoint[nl2].type = 1; /* upper end */
3215 endpoint[nl2].val = e + f;
3219 * Construct sorted indx[] of endpoint[] indexes ordered by
3222 for (i = 0; i < nl2; i++)
3224 for (i = 0; i < nl2; i++) {
3225 endp = endpoint[indx[i]];
3228 for (j = i + 1; j < nl2; j++) {
3229 endp = endpoint[indx[j]];
3241 for (i = 0; i < nl2; i++)
3242 DPRINTF(3, ("select: endpoint %2d %.6f\n",
3243 endpoint[indx[i]].type, endpoint[indx[i]].val));
3246 * This is the actual algorithm that cleaves the truechimers
3247 * from the falsetickers. The original algorithm was described
3248 * in Keith Marzullo's dissertation, but has been modified for
3251 * Briefly put, we first assume there are no falsetickers, then
3252 * scan the candidate list first from the low end upwards and
3253 * then from the high end downwards. The scans stop when the
3254 * number of intersections equals the number of candidates less
3255 * the number of falsetickers. If this doesn't happen for a
3256 * given number of falsetickers, we bump the number of
3257 * falsetickers and try again. If the number of falsetickers
3258 * becomes equal to or greater than half the number of
3259 * candidates, the Albanians have won the Byzantine wars and
3260 * correct synchronization is not possible.
3262 * Here, nlist is the number of candidates and allow is the
3263 * number of falsetickers. Upon exit, the truechimers are the
3264 * survivors with offsets not less than low and not greater than
3265 * high. There may be none of them.
3269 for (allow = 0; 2 * allow < nlist; allow++) {
3272 * Bound the interval (low, high) as the smallest
3273 * interval containing points from the most sources.
3276 for (i = 0; i < nl2; i++) {
3277 low = endpoint[indx[i]].val;
3278 n -= endpoint[indx[i]].type;
3279 if (n >= nlist - allow)
3283 for (j = nl2 - 1; j >= 0; j--) {
3284 high = endpoint[indx[j]].val;
3285 n += endpoint[indx[j]].type;
3286 if (n >= nlist - allow)
3291 * If an interval containing truechimers is found, stop.
3292 * If not, increase the number of falsetickers and go
3300 * Clustering algorithm. Whittle candidate list of falsetickers,
3301 * who leave the island immediately. The TRUE peer is always a
3302 * truechimer. We must leave at least one peer to collect the
3305 * We assert the correct time is contained in the interval, but
3306 * the best offset estimate for the interval might not be
3307 * contained in the interval. For this purpose, a truechimer is
3308 * defined as the midpoint of an interval that overlaps the
3309 * intersection interval.
3312 for (i = 0; i < nlist; i++) {
3315 peer = peers[i].peer;
3318 || peer->offset + h < low
3319 || peer->offset - h > high
3320 ) && !(peer->flags & FLAG_TRUE))
3325 * Eligible PPS peers must survive the intersection
3326 * algorithm. Use the first one found, but don't
3327 * include any of them in the cluster population.
3329 if (peer->flags & FLAG_PPS) {
3330 if (typepps == NULL)
3332 if (!(peer->flags & FLAG_TSTAMP_PPS))
3335 #endif /* REFCLOCK */
3338 peers[j] = peers[i];
3344 * If no survivors remain at this point, check if the modem
3345 * driver, local driver or orphan parent in that order. If so,
3346 * nominate the first one found as the only survivor.
3347 * Otherwise, give up and leave the island to the rats.
3351 peers[0].synch = sys_mindisp;
3353 if (typeacts != NULL) {
3354 peers[0].peer = typeacts;
3356 } else if (typelocal != NULL) {
3357 peers[0].peer = typelocal;
3360 #endif /* REFCLOCK */
3361 if (typeorphan != NULL) {
3362 peers[0].peer = typeorphan;
3368 * Mark the candidates at this point as truechimers.
3370 for (i = 0; i < nlist; i++) {
3371 peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
3372 DPRINTF(2, ("select: survivor %s %f\n",
3373 stoa(&peers[i].peer->srcadr), peers[i].synch));
3377 * Now, vote outliers off the island by select jitter weighted
3378 * by root distance. Continue voting as long as there are more
3379 * than sys_minclock survivors and the select jitter of the peer
3380 * with the worst metric is greater than the minimum peer
3381 * jitter. Stop if we are about to discard a TRUE or PREFER
3382 * peer, who of course have the immunity idol.
3389 for (i = 0; i < nlist; i++) {
3390 if (peers[i].error < d)
3392 peers[i].seljit = 0;
3395 for (j = 0; j < nlist; j++)
3396 f += DIFF(peers[j].peer->offset,
3397 peers[i].peer->offset);
3398 peers[i].seljit = SQRT(f / (nlist - 1));
3400 if (peers[i].seljit * peers[i].synch > e) {
3401 g = peers[i].seljit;
3402 e = peers[i].seljit * peers[i].synch;
3406 g = max(g, LOGTOD(sys_precision));
3407 if ( nlist <= max(1, sys_minclock)
3409 || ((FLAG_TRUE | FLAG_PREFER) & peers[k].peer->flags))
3412 DPRINTF(3, ("select: drop %s seljit %.6f jit %.6f\n",
3413 ntoa(&peers[k].peer->srcadr), g, d));
3414 if (nlist > sys_maxclock)
3415 peers[k].peer->new_status = CTL_PST_SEL_EXCESS;
3416 for (j = k + 1; j < nlist; j++)
3417 peers[j - 1] = peers[j];
3422 * What remains is a list usually not greater than sys_minclock
3423 * peers. Note that unsynchronized peers cannot survive this
3424 * far. Count and mark these survivors.
3426 * While at it, count the number of leap warning bits found.
3427 * This will be used later to vote the system leap warning bit.
3428 * If a leap warning bit is found on a reference clock, the vote
3431 * Choose the system peer using a hybrid metric composed of the
3432 * selection jitter scaled by the root distance augmented by
3433 * stratum scaled by sys_mindisp (.001 by default). The goal of
3434 * the small stratum factor is to avoid clockhop between a
3435 * reference clock and a network peer which has a refclock and
3436 * is using an older ntpd, which does not floor sys_rootdisp at
3439 * In contrast, ntpd 4.2.6 and earlier used stratum primarily
3440 * in selecting the system peer, using a weight of 1 second of
3441 * additional root distance per stratum. This heavy bias is no
3442 * longer appropriate, as the scaled root distance provides a
3443 * more rational metric carrying the cumulative error budget.
3449 for (i = 0; i < nlist; i++) {
3450 peer = peers[i].peer;
3452 peer->new_status = CTL_PST_SEL_SYNCCAND;
3454 if (peer->leap == LEAP_ADDSECOND) {
3455 if (peer->flags & FLAG_REFCLOCK)
3456 leap_vote_ins = nlist;
3457 else if (leap_vote_ins < nlist)
3460 if (peer->leap == LEAP_DELSECOND) {
3461 if (peer->flags & FLAG_REFCLOCK)
3462 leap_vote_del = nlist;
3463 else if (leap_vote_del < nlist)
3466 if (peer->flags & FLAG_PREFER)
3468 speermet = peers[i].seljit * peers[i].synch +
3469 peer->stratum * sys_mindisp;
3477 * Unless there are at least sys_misane survivors, leave the
3478 * building dark. Otherwise, do a clockhop dance. Ordinarily,
3479 * use the selected survivor speer. However, if the current
3480 * system peer is not speer, stay with the current system peer
3481 * as long as it doesn't get too old or too ugly.
3483 if (nlist > 0 && nlist >= sys_minsane) {
3486 typesystem = peers[speer].peer;
3487 if (osys_peer == NULL || osys_peer == typesystem) {
3489 } else if ((x = fabs(typesystem->offset -
3490 osys_peer->offset)) < sys_mindisp) {
3491 if (sys_clockhop == 0)
3492 sys_clockhop = sys_mindisp;
3495 DPRINTF(1, ("select: clockhop %d %.6f %.6f\n",
3496 j, x, sys_clockhop));
3497 if (fabs(x) < sys_clockhop)
3498 typesystem = osys_peer;
3507 * Mitigation rules of the game. We have the pick of the
3508 * litter in typesystem if any survivors are left. If
3509 * there is a prefer peer, use its offset and jitter.
3510 * Otherwise, use the combined offset and jitter of all kitters.
3512 if (typesystem != NULL) {
3513 if (sys_prefer == NULL) {
3514 typesystem->new_status = CTL_PST_SEL_SYSPEER;
3515 clock_combine(peers, sys_survivors, speer);
3517 typesystem = sys_prefer;
3519 typesystem->new_status = CTL_PST_SEL_SYSPEER;
3520 sys_offset = typesystem->offset;
3521 sys_jitter = typesystem->jitter;
3523 DPRINTF(1, ("select: combine offset %.9f jitter %.9f\n",
3524 sys_offset, sys_jitter));
3528 * If a PPS driver is lit and the combined offset is less than
3529 * 0.4 s, select the driver as the PPS peer and use its offset
3530 * and jitter. However, if this is the atom driver, use it only
3531 * if there is a prefer peer or there are no survivors and none
3534 if ( typepps != NULL
3535 && fabs(sys_offset) < 0.4
3536 && ( typepps->refclktype != REFCLK_ATOM_PPS
3537 || ( typepps->refclktype == REFCLK_ATOM_PPS
3538 && ( sys_prefer != NULL
3539 || (typesystem == NULL && sys_minsane == 0))))) {
3540 typesystem = typepps;
3542 typesystem->new_status = CTL_PST_SEL_PPS;
3543 sys_offset = typesystem->offset;
3544 sys_jitter = typesystem->jitter;
3545 DPRINTF(1, ("select: pps offset %.9f jitter %.9f\n",
3546 sys_offset, sys_jitter));
3548 #endif /* REFCLOCK */
3551 * If there are no survivors at this point, there is no
3552 * system peer. If so and this is an old update, keep the
3553 * current statistics, but do not update the clock.
3555 if (typesystem == NULL) {
3556 if (osys_peer != NULL) {
3557 if (sys_orphwait > 0)
3558 orphwait = current_time + sys_orphwait;
3559 report_event(EVNT_NOPEER, NULL, NULL);
3562 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3563 peer->status = peer->new_status;
3568 * Do not use old data, as this may mess up the clock discipline
3571 if (typesystem->epoch <= sys_epoch)
3575 * We have found the alpha male. Wind the clock.
3577 if (osys_peer != typesystem)
3578 report_event(PEVNT_NEWPEER, typesystem, NULL);
3579 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3580 peer->status = peer->new_status;
3581 clock_update(typesystem);
3587 peer_select * peers, /* survivor list */
3588 int npeers, /* number of survivors */
3589 int syspeer /* index of sys.peer */
3596 for (i = 0; i < npeers; i++) {
3597 x = 1. / peers[i].synch;
3599 z += x * peers[i].peer->offset;
3600 w += x * DIFF(peers[i].peer->offset,
3601 peers[syspeer].peer->offset);
3604 sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
3609 * root_distance - compute synchronization distance from peer to root
3613 struct peer *peer /* peer structure pointer */
3619 * Root Distance (LAMBDA) is defined as:
3620 * (delta + DELTA)/2 + epsilon + EPSILON + D
3623 * delta is the round-trip delay
3624 * DELTA is the root delay
3625 * epsilon is the peer dispersion
3626 * + (15 usec each second)
3627 * EPSILON is the root dispersion
3630 * NB: Think hard about why we are using these values, and what
3631 * the alternatives are, and the various pros/cons.
3633 * DLM thinks these are probably the best choices from any of the
3634 * other worse choices.
3636 dtemp = (peer->delay + peer->rootdelay) / 2
3638 + clock_phi * (current_time - peer->update)
3642 * Careful squeak here. The value returned must be greater than
3643 * the minimum root dispersion in order to avoid clockhop with
3644 * highly precise reference clocks. Note that the root distance
3645 * cannot exceed the sys_maxdist, as this is the cutoff by the
3646 * selection algorithm.
3648 if (dtemp < sys_mindisp)
3649 dtemp = sys_mindisp;
3655 * peer_xmit - send packet for persistent association.
3659 struct peer *peer /* peer structure pointer */
3662 struct pkt xpkt; /* transmit packet */
3663 size_t sendlen, authlen;
3664 keyid_t xkeyid = 0; /* transmit key ID */
3665 l_fp xmt_tx, xmt_ty;
3667 if (!peer->dstadr) /* drop peers without interface */
3670 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, peer->version,
3672 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
3673 xpkt.ppoll = peer->hpoll;
3674 xpkt.precision = sys_precision;
3675 xpkt.refid = sys_refid;
3676 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
3677 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
3678 HTONL_FP(&sys_reftime, &xpkt.reftime);
3679 HTONL_FP(&peer->rec, &xpkt.org);
3680 HTONL_FP(&peer->dst, &xpkt.rec);
3683 * If the received packet contains a MAC, the transmitted packet
3684 * is authenticated and contains a MAC. If not, the transmitted
3685 * packet is not authenticated.
3687 * It is most important when autokey is in use that the local
3688 * interface IP address be known before the first packet is
3689 * sent. Otherwise, it is not possible to compute a correct MAC
3690 * the recipient will accept. Thus, the I/O semantics have to do
3691 * a little more work. In particular, the wildcard interface
3692 * might not be usable.
3694 sendlen = LEN_PKT_NOMAC;
3697 !(peer->flags & FLAG_SKEY) &&
3698 #endif /* !AUTOKEY */
3702 * Transmit a-priori timestamps
3704 get_systime(&xmt_tx);
3705 if (peer->flip == 0) { /* basic mode */
3706 peer->aorg = xmt_tx;
3707 HTONL_FP(&xmt_tx, &xpkt.xmt);
3708 } else { /* interleaved modes */
3709 if (peer->hmode == MODE_BROADCAST) { /* bcst */
3710 HTONL_FP(&xmt_tx, &xpkt.xmt);
3712 HTONL_FP(&peer->borg,
3715 HTONL_FP(&peer->aorg,
3717 } else { /* symmetric */
3719 HTONL_FP(&peer->borg,
3722 HTONL_FP(&peer->aorg,
3726 peer->t21_bytes = sendlen;
3727 sendpkt(&peer->srcadr, peer->dstadr,
3728 sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
3731 peer->throttle += (1 << peer->minpoll) - 2;
3734 * Capture a-posteriori timestamps
3736 get_systime(&xmt_ty);
3737 if (peer->flip != 0) { /* interleaved modes */
3739 peer->aorg = xmt_ty;
3741 peer->borg = xmt_ty;
3742 peer->flip = -peer->flip;
3744 L_SUB(&xmt_ty, &xmt_tx);
3745 LFPTOD(&xmt_ty, peer->xleave);
3746 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d len %zu xmt %#010x.%08x\n",
3748 peer->dstadr ? stoa(&peer->dstadr->sin) : "-",
3749 stoa(&peer->srcadr), peer->hmode, sendlen,
3750 xmt_tx.l_ui, xmt_tx.l_uf));
3755 * Authentication is enabled, so the transmitted packet must be
3756 * authenticated. If autokey is enabled, fuss with the various
3757 * modes; otherwise, symmetric key cryptography is used.
3760 if (peer->flags & FLAG_SKEY) {
3761 struct exten *exten; /* extension field */
3764 * The Public Key Dance (PKD): Cryptographic credentials
3765 * are contained in extension fields, each including a
3766 * 4-octet length/code word followed by a 4-octet
3767 * association ID and optional additional data. Optional
3768 * data includes a 4-octet data length field followed by
3769 * the data itself. Request messages are sent from a
3770 * configured association; response messages can be sent
3771 * from a configured association or can take the fast
3772 * path without ever matching an association. Response
3773 * messages have the same code as the request, but have
3774 * a response bit and possibly an error bit set. In this
3775 * implementation, a message may contain no more than
3776 * one command and one or more responses.
3778 * Cryptographic session keys include both a public and
3779 * a private componet. Request and response messages
3780 * using extension fields are always sent with the
3781 * private component set to zero. Packets without
3782 * extension fields indlude the private component when
3783 * the session key is generated.
3788 * Allocate and initialize a keylist if not
3789 * already done. Then, use the list in inverse
3790 * order, discarding keys once used. Keep the
3791 * latest key around until the next one, so
3792 * clients can use client/server packets to
3793 * compute propagation delay.
3795 * Note that once a key is used from the list,
3796 * it is retained in the key cache until the
3797 * next key is used. This is to allow a client
3798 * to retrieve the encrypted session key
3799 * identifier to verify authenticity.
3801 * If for some reason a key is no longer in the
3802 * key cache, a birthday has happened or the key
3803 * has expired, so the pseudo-random sequence is
3804 * broken. In that case, purge the keylist and
3807 if (peer->keynumber == 0)
3808 make_keylist(peer, peer->dstadr);
3811 xkeyid = peer->keylist[peer->keynumber];
3812 if (authistrusted(xkeyid))
3817 peer->keyid = xkeyid;
3819 switch (peer->hmode) {
3822 * In broadcast server mode the autokey values are
3823 * required by the broadcast clients. Push them when a
3824 * new keylist is generated; otherwise, push the
3825 * association message so the client can request them at
3828 case MODE_BROADCAST:
3829 if (peer->flags & FLAG_ASSOC)
3830 exten = crypto_args(peer, CRYPTO_AUTO |
3831 CRYPTO_RESP, peer->associd, NULL);
3833 exten = crypto_args(peer, CRYPTO_ASSOC |
3834 CRYPTO_RESP, peer->associd, NULL);
3838 * In symmetric modes the parameter, certificate,
3839 * identity, cookie and autokey exchanges are
3840 * required. The leapsecond exchange is optional. But, a
3841 * peer will not believe the other peer until the other
3842 * peer has synchronized, so the certificate exchange
3843 * might loop until then. If a peer finds a broken
3844 * autokey sequence, it uses the autokey exchange to
3845 * retrieve the autokey values. In any case, if a new
3846 * keylist is generated, the autokey values are pushed.
3852 * Parameter, certificate and identity.
3855 exten = crypto_args(peer, CRYPTO_ASSOC,
3856 peer->associd, hostval.ptr);
3857 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
3858 exten = crypto_args(peer, CRYPTO_CERT,
3859 peer->associd, peer->issuer);
3860 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
3861 exten = crypto_args(peer,
3862 crypto_ident(peer), peer->associd,
3866 * Cookie and autokey. We request the cookie
3867 * only when the this peer and the other peer
3868 * are synchronized. But, this peer needs the
3869 * autokey values when the cookie is zero. Any
3870 * time we regenerate the key list, we offer the
3871 * autokey values without being asked. If for
3872 * some reason either peer finds a broken
3873 * autokey sequence, the autokey exchange is
3874 * used to retrieve the autokey values.
3876 else if ( sys_leap != LEAP_NOTINSYNC
3877 && peer->leap != LEAP_NOTINSYNC
3878 && !(peer->crypto & CRYPTO_FLAG_COOK))
3879 exten = crypto_args(peer, CRYPTO_COOK,
3880 peer->associd, NULL);
3881 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
3882 exten = crypto_args(peer, CRYPTO_AUTO,
3883 peer->associd, NULL);
3884 else if ( peer->flags & FLAG_ASSOC
3885 && peer->crypto & CRYPTO_FLAG_SIGN)
3886 exten = crypto_args(peer, CRYPTO_AUTO |
3887 CRYPTO_RESP, peer->assoc, NULL);
3890 * Wait for clock sync, then sign the
3891 * certificate and retrieve the leapsecond
3894 else if (sys_leap == LEAP_NOTINSYNC)
3897 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
3898 exten = crypto_args(peer, CRYPTO_SIGN,
3899 peer->associd, hostval.ptr);
3900 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
3901 exten = crypto_args(peer, CRYPTO_LEAP,
3902 peer->associd, NULL);
3906 * In client mode the parameter, certificate, identity,
3907 * cookie and sign exchanges are required. The
3908 * leapsecond exchange is optional. If broadcast client
3909 * mode the same exchanges are required, except that the
3910 * autokey exchange is substitutes for the cookie
3911 * exchange, since the cookie is always zero. If the
3912 * broadcast client finds a broken autokey sequence, it
3913 * uses the autokey exchange to retrieve the autokey
3919 * Parameter, certificate and identity.
3922 exten = crypto_args(peer, CRYPTO_ASSOC,
3923 peer->associd, hostval.ptr);
3924 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
3925 exten = crypto_args(peer, CRYPTO_CERT,
3926 peer->associd, peer->issuer);
3927 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
3928 exten = crypto_args(peer,
3929 crypto_ident(peer), peer->associd,
3933 * Cookie and autokey. These are requests, but
3934 * we use the peer association ID with autokey
3935 * rather than our own.
3937 else if (!(peer->crypto & CRYPTO_FLAG_COOK))
3938 exten = crypto_args(peer, CRYPTO_COOK,
3939 peer->associd, NULL);
3940 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
3941 exten = crypto_args(peer, CRYPTO_AUTO,
3945 * Wait for clock sync, then sign the
3946 * certificate and retrieve the leapsecond
3949 else if (sys_leap == LEAP_NOTINSYNC)
3952 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
3953 exten = crypto_args(peer, CRYPTO_SIGN,
3954 peer->associd, hostval.ptr);
3955 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
3956 exten = crypto_args(peer, CRYPTO_LEAP,
3957 peer->associd, NULL);
3962 * Add a queued extension field if present. This is
3963 * always a request message, so the reply ID is already
3964 * in the message. If an error occurs, the error bit is
3965 * lit in the response.
3967 if (peer->cmmd != NULL) {
3970 temp32 = CRYPTO_RESP;
3971 peer->cmmd->opcode |= htonl(temp32);
3972 sendlen += crypto_xmit(peer, &xpkt, NULL,
3973 sendlen, peer->cmmd, 0);
3979 * Add an extension field created above. All but the
3980 * autokey response message are request messages.
3982 if (exten != NULL) {
3983 if (exten->opcode != 0)
3984 sendlen += crypto_xmit(peer, &xpkt,
3985 NULL, sendlen, exten, 0);
3990 * Calculate the next session key. Since extension
3991 * fields are present, the cookie value is zero.
3993 if (sendlen > (int)LEN_PKT_NOMAC) {
3994 session_key(&peer->dstadr->sin, &peer->srcadr,
3998 #endif /* AUTOKEY */
4001 * Transmit a-priori timestamps
4003 get_systime(&xmt_tx);
4004 if (peer->flip == 0) { /* basic mode */
4005 peer->aorg = xmt_tx;
4006 HTONL_FP(&xmt_tx, &xpkt.xmt);
4007 } else { /* interleaved modes */
4008 if (peer->hmode == MODE_BROADCAST) { /* bcst */
4009 HTONL_FP(&xmt_tx, &xpkt.xmt);
4011 HTONL_FP(&peer->borg, &xpkt.org);
4013 HTONL_FP(&peer->aorg, &xpkt.org);
4014 } else { /* symmetric */
4016 HTONL_FP(&peer->borg, &xpkt.xmt);
4018 HTONL_FP(&peer->aorg, &xpkt.xmt);
4021 xkeyid = peer->keyid;
4022 authlen = authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4024 report_event(PEVNT_AUTH, peer, "no key");
4025 peer->flash |= TEST5; /* auth error */
4031 if (xkeyid > NTP_MAXKEY)
4032 authtrust(xkeyid, 0);
4033 #endif /* AUTOKEY */
4034 if (sendlen > sizeof(xpkt)) {
4035 msyslog(LOG_ERR, "peer_xmit: buffer overflow %zu", sendlen);
4038 peer->t21_bytes = sendlen;
4039 sendpkt(&peer->srcadr, peer->dstadr,
4040 sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4043 peer->throttle += (1 << peer->minpoll) - 2;
4046 * Capture a-posteriori timestamps
4048 get_systime(&xmt_ty);
4049 if (peer->flip != 0) { /* interleaved modes */
4051 peer->aorg = xmt_ty;
4053 peer->borg = xmt_ty;
4054 peer->flip = -peer->flip;
4056 L_SUB(&xmt_ty, &xmt_tx);
4057 LFPTOD(&xmt_ty, peer->xleave);
4059 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu index %d\n",
4060 current_time, latoa(peer->dstadr),
4061 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen,
4063 #else /* !AUTOKEY follows */
4064 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu\n",
4065 current_time, peer->dstadr ?
4066 ntoa(&peer->dstadr->sin) : "-",
4067 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
4068 #endif /* !AUTOKEY */
4077 leap_smear_add_offs(
4083 L_ADD(t, &leap_smear.offset);
4086 ** XXX: Should the smear be added to the root dispersion?
4092 #endif /* LEAP_SMEAR */
4096 * fast_xmit - Send packet for nonpersistent association. Note that
4097 * neither the source or destination can be a broadcast address.
4101 struct recvbuf *rbufp, /* receive packet pointer */
4102 int xmode, /* receive mode */
4103 keyid_t xkeyid, /* transmit key ID */
4104 int flags /* restrict mask */
4107 struct pkt xpkt; /* transmit packet structure */
4108 struct pkt *rpkt; /* receive packet structure */
4109 l_fp xmt_tx, xmt_ty;
4116 * Initialize transmit packet header fields from the receive
4117 * buffer provided. We leave the fields intact as received, but
4118 * set the peer poll at the maximum of the receive peer poll and
4119 * the system minimum poll (ntp_minpoll). This is for KoD rate
4120 * control and not strictly specification compliant, but doesn't
4123 * If the gazinta was from a multicast address, the gazoutta
4124 * must go out another way.
4126 rpkt = &rbufp->recv_pkt;
4127 if (rbufp->dstadr->flags & INT_MCASTOPEN)
4128 rbufp->dstadr = findinterface(&rbufp->recv_srcadr);
4131 * If this is a kiss-o'-death (KoD) packet, show leap
4132 * unsynchronized, stratum zero, reference ID the four-character
4133 * kiss code and system root delay. Note we don't reveal the
4134 * local time, so these packets can't be used for
4137 if (flags & RES_KOD) {
4139 xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
4140 PKT_VERSION(rpkt->li_vn_mode), xmode);
4141 xpkt.stratum = STRATUM_PKT_UNSPEC;
4142 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4143 xpkt.precision = rpkt->precision;
4144 memcpy(&xpkt.refid, "RATE", 4);
4145 xpkt.rootdelay = rpkt->rootdelay;
4146 xpkt.rootdisp = rpkt->rootdisp;
4147 xpkt.reftime = rpkt->reftime;
4148 xpkt.org = rpkt->xmt;
4149 xpkt.rec = rpkt->xmt;
4150 xpkt.xmt = rpkt->xmt;
4153 * This is a normal packet. Use the system variables.
4158 * Make copies of the variables which can be affected by smearing.
4161 l_fp this_recv_time;
4165 * If we are inside the leap smear interval we add the current smear offset to
4166 * the packet receive time, to the packet transmit time, and eventually to the
4167 * reftime to make sure the reftime isn't later than the transmit/receive times.
4169 xpkt.li_vn_mode = PKT_LI_VN_MODE(xmt_leap,
4170 PKT_VERSION(rpkt->li_vn_mode), xmode);
4172 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4173 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4174 xpkt.precision = sys_precision;
4175 xpkt.refid = sys_refid;
4176 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4177 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4180 this_ref_time = sys_reftime;
4181 if (leap_smear.in_progress) {
4182 leap_smear_add_offs(&this_ref_time, NULL);
4183 xpkt.refid = convertLFPToRefID(leap_smear.offset);
4184 DPRINTF(2, ("fast_xmit: leap_smear.in_progress: refid %8x, smear %s\n",
4186 lfptoa(&leap_smear.offset, 8)
4189 HTONL_FP(&this_ref_time, &xpkt.reftime);
4191 HTONL_FP(&sys_reftime, &xpkt.reftime);
4194 xpkt.org = rpkt->xmt;
4197 this_recv_time = rbufp->recv_time;
4198 if (leap_smear.in_progress)
4199 leap_smear_add_offs(&this_recv_time, NULL);
4200 HTONL_FP(&this_recv_time, &xpkt.rec);
4202 HTONL_FP(&rbufp->recv_time, &xpkt.rec);
4205 get_systime(&xmt_tx);
4207 if (leap_smear.in_progress)
4208 leap_smear_add_offs(&xmt_tx, &this_recv_time);
4210 HTONL_FP(&xmt_tx, &xpkt.xmt);
4213 #ifdef HAVE_NTP_SIGND
4214 if (flags & RES_MSSNTP) {
4215 send_via_ntp_signd(rbufp, xmode, xkeyid, flags, &xpkt);
4218 #endif /* HAVE_NTP_SIGND */
4221 * If the received packet contains a MAC, the transmitted packet
4222 * is authenticated and contains a MAC. If not, the transmitted
4223 * packet is not authenticated.
4225 sendlen = LEN_PKT_NOMAC;
4226 if (rbufp->recv_length == sendlen) {
4227 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt,
4229 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d len %lu\n",
4230 current_time, stoa(&rbufp->dstadr->sin),
4231 stoa(&rbufp->recv_srcadr), xmode,
4237 * The received packet contains a MAC, so the transmitted packet
4238 * must be authenticated. For symmetric key cryptography, use
4239 * the predefined and trusted symmetric keys to generate the
4240 * cryptosum. For autokey cryptography, use the server private
4241 * value to generate the cookie, which is unique for every
4242 * source-destination-key ID combination.
4245 if (xkeyid > NTP_MAXKEY) {
4249 * The only way to get here is a reply to a legitimate
4250 * client request message, so the mode must be
4251 * MODE_SERVER. If an extension field is present, there
4252 * can be only one and that must be a command. Do what
4253 * needs, but with private value of zero so the poor
4254 * jerk can decode it. If no extension field is present,
4255 * use the cookie to generate the session key.
4257 cookie = session_key(&rbufp->recv_srcadr,
4258 &rbufp->dstadr->sin, 0, sys_private, 0);
4259 if ((size_t)rbufp->recv_length > sendlen + MAX_MAC_LEN) {
4260 session_key(&rbufp->dstadr->sin,
4261 &rbufp->recv_srcadr, xkeyid, 0, 2);
4262 temp32 = CRYPTO_RESP;
4263 rpkt->exten[0] |= htonl(temp32);
4264 sendlen += crypto_xmit(NULL, &xpkt, rbufp,
4265 sendlen, (struct exten *)rpkt->exten,
4268 session_key(&rbufp->dstadr->sin,
4269 &rbufp->recv_srcadr, xkeyid, cookie, 2);
4272 #endif /* AUTOKEY */
4273 get_systime(&xmt_tx);
4274 sendlen += authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4276 if (xkeyid > NTP_MAXKEY)
4277 authtrust(xkeyid, 0);
4278 #endif /* AUTOKEY */
4279 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt, sendlen);
4280 get_systime(&xmt_ty);
4281 L_SUB(&xmt_ty, &xmt_tx);
4282 sys_authdelay = xmt_ty;
4283 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d keyid %08x len %lu\n",
4284 current_time, ntoa(&rbufp->dstadr->sin),
4285 ntoa(&rbufp->recv_srcadr), xmode, xkeyid,
4291 * pool_xmit - resolve hostname or send unicast solicitation for pool.
4295 struct peer *pool /* pool solicitor association */
4299 struct pkt xpkt; /* transmit packet structure */
4300 struct addrinfo hints;
4302 struct interface * lcladr;
4303 sockaddr_u * rmtadr;
4308 if (NULL == pool->ai) {
4309 if (pool->addrs != NULL) {
4310 /* free() is used with copy_addrinfo_list() */
4315 hints.ai_family = AF(&pool->srcadr);
4316 hints.ai_socktype = SOCK_DGRAM;
4317 hints.ai_protocol = IPPROTO_UDP;
4318 /* ignore getaddrinfo_sometime() errors, we will retry */
4319 rc = getaddrinfo_sometime(
4324 &pool_name_resolved,
4325 (void *)(intptr_t)pool->associd);
4327 DPRINTF(1, ("pool DNS lookup %s started\n",
4331 "unable to start pool DNS %s: %m",
4337 /* copy_addrinfo_list ai_addr points to a sockaddr_u */
4338 rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
4339 pool->ai = pool->ai->ai_next;
4340 p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT, 0);
4341 } while (p != NULL && pool->ai != NULL);
4343 return; /* out of addresses, re-query DNS next poll */
4344 restrict_mask = restrictions(rmtadr);
4345 if (RES_FLAGS & restrict_mask)
4346 restrict_source(rmtadr, 0,
4347 current_time + POOL_SOLICIT_WINDOW + 1);
4348 lcladr = findinterface(rmtadr);
4349 memset(&xpkt, 0, sizeof(xpkt));
4350 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, pool->version,
4352 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4353 xpkt.ppoll = pool->hpoll;
4354 xpkt.precision = sys_precision;
4355 xpkt.refid = sys_refid;
4356 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4357 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4358 HTONL_FP(&sys_reftime, &xpkt.reftime);
4359 get_systime(&xmt_tx);
4360 pool->aorg = xmt_tx;
4361 HTONL_FP(&xmt_tx, &xpkt.xmt);
4362 sendpkt(rmtadr, lcladr,
4363 sys_ttl[(pool->ttl >= sys_ttlmax) ? sys_ttlmax : pool->ttl],
4364 &xpkt, LEN_PKT_NOMAC);
4366 pool->throttle += (1 << pool->minpoll) - 2;
4367 DPRINTF(1, ("pool_xmit: at %ld %s->%s pool\n",
4368 current_time, latoa(lcladr), stoa(rmtadr)));
4369 msyslog(LOG_INFO, "Soliciting pool server %s", stoa(rmtadr));
4376 * group_test - test if this is the same group
4378 * host assoc return action
4379 * none none 0 mobilize *
4380 * none group 0 mobilize *
4381 * group none 0 mobilize *
4382 * group group 1 mobilize
4383 * group different 1 ignore
4384 * * ignore if notrust
4395 if (strcmp(grp, sys_groupname) == 0)
4401 if (strcmp(grp, ident) == 0)
4406 #endif /* AUTOKEY */
4416 const char * service,
4417 const struct addrinfo * hints,
4418 const struct addrinfo * res
4421 struct peer * pool; /* pool solicitor association */
4426 "error resolving pool %s: %s (%d)",
4427 name, gai_strerror(rescode), rescode);
4431 assoc = (associd_t)(intptr_t)context;
4432 pool = findpeerbyassoc(assoc);
4435 "Could not find assoc %u for pool DNS %s",
4439 DPRINTF(1, ("pool DNS %s completed\n", name));
4440 pool->addrs = copy_addrinfo_list(res);
4441 pool->ai = pool->addrs;
4450 * key_expire - purge the key list
4454 struct peer *peer /* peer structure pointer */
4459 if (peer->keylist != NULL) {
4460 for (i = 0; i <= peer->keynumber; i++)
4461 authtrust(peer->keylist[i], 0);
4462 free(peer->keylist);
4463 peer->keylist = NULL;
4465 value_free(&peer->sndval);
4466 peer->keynumber = 0;
4467 peer->flags &= ~FLAG_ASSOC;
4468 DPRINTF(1, ("key_expire: at %lu associd %d\n", current_time,
4471 #endif /* AUTOKEY */
4475 * local_refid(peer) - check peer refid to avoid selecting peers
4476 * currently synced to this ntpd.
4485 if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
4486 unicast_ep = p->dstadr;
4488 unicast_ep = findinterface(&p->srcadr);
4490 if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
4498 * Determine if the peer is unfit for synchronization
4500 * A peer is unfit for synchronization if
4501 * > TEST10 bad leap or stratum below floor or at or above ceiling
4502 * > TEST11 root distance exceeded for remote peer
4503 * > TEST12 a direct or indirect synchronization loop would form
4504 * > TEST13 unreachable or noselect
4506 int /* FALSE if fit, TRUE if unfit */
4508 struct peer *peer /* peer structure pointer */
4514 * A stratum error occurs if (1) the server has never been
4515 * synchronized, (2) the server stratum is below the floor or
4516 * greater than or equal to the ceiling.
4518 if ( peer->leap == LEAP_NOTINSYNC
4519 || peer->stratum < sys_floor
4520 || peer->stratum >= sys_ceiling) {
4521 rval |= TEST10; /* bad synch or stratum */
4525 * A distance error for a remote peer occurs if the root
4526 * distance is greater than or equal to the distance threshold
4527 * plus the increment due to one host poll interval.
4529 if ( !(peer->flags & FLAG_REFCLOCK)
4530 && root_distance(peer) >= sys_maxdist
4531 + clock_phi * ULOGTOD(peer->hpoll)) {
4532 rval |= TEST11; /* distance exceeded */
4536 * A loop error occurs if the remote peer is synchronized to the
4537 * local peer or if the remote peer is synchronized to the same
4538 * server as the local peer but only if the remote peer is
4539 * neither a reference clock nor an orphan.
4541 if (peer->stratum > 1 && local_refid(peer)) {
4542 rval |= TEST12; /* synchronization loop */
4546 * An unreachable error occurs if the server is unreachable or
4547 * the noselect bit is set.
4549 if (!peer->reach || (peer->flags & FLAG_NOSELECT)) {
4550 rval |= TEST13; /* unreachable */
4553 peer->flash &= ~PEER_TEST_MASK;
4554 peer->flash |= rval;
4560 * Find the precision of this particular machine
4562 #define MINSTEP 20e-9 /* minimum clock increment (s) */
4563 #define MAXSTEP 1 /* maximum clock increment (s) */
4564 #define MINCHANGES 12 /* minimum number of step samples */
4565 #define MAXLOOPS ((int)(1. / MINSTEP)) /* avoid infinite loop */
4568 * This routine measures the system precision defined as the minimum of
4569 * a sequence of differences between successive readings of the system
4570 * clock. However, if a difference is less than MINSTEP, the clock has
4571 * been read more than once during a clock tick and the difference is
4572 * ignored. We set MINSTEP greater than zero in case something happens
4573 * like a cache miss, and to tolerate underlying system clocks which
4574 * ensure each reading is strictly greater than prior readings while
4575 * using an underlying stepping (not interpolated) clock.
4577 * sys_tick and sys_precision represent the time to read the clock for
4578 * systems with high-precision clocks, and the tick interval or step
4579 * size for lower-precision stepping clocks.
4581 * This routine also measures the time to read the clock on stepping
4582 * system clocks by counting the number of readings between changes of
4583 * the underlying clock. With either type of clock, the minimum time
4584 * to read the clock is saved as sys_fuzz, and used to ensure the
4585 * get_systime() readings always increase and are fuzzed below sys_fuzz.
4588 measure_precision(void)
4591 * With sys_fuzz set to zero, get_systime() fuzzing of low bits
4592 * is effectively disabled. trunc_os_clock is FALSE to disable
4593 * get_ostime() simulation of a low-precision system clock.
4596 trunc_os_clock = FALSE;
4597 measured_tick = measure_tick_fuzz();
4598 set_sys_tick_precision(measured_tick);
4599 msyslog(LOG_INFO, "proto: precision = %.3f usec (%d)",
4600 sys_tick * 1e6, sys_precision);
4601 if (sys_fuzz < sys_tick) {
4602 msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec",
4609 * measure_tick_fuzz()
4611 * measures the minimum time to read the clock (stored in sys_fuzz)
4612 * and returns the tick, the larger of the minimum increment observed
4613 * between successive clock readings and the time to read the clock.
4616 measure_tick_fuzz(void)
4618 l_fp minstep; /* MINSTEP as l_fp */
4619 l_fp val; /* current seconds fraction */
4620 l_fp last; /* last seconds fraction */
4621 l_fp ldiff; /* val - last */
4622 double tick; /* computed tick value */
4627 int i; /* log2 precision */
4633 DTOLFP(MINSTEP, &minstep);
4635 for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) {
4638 L_SUB(&ldiff, &last);
4640 if (L_ISGT(&ldiff, &minstep)) {
4641 max_repeats = max(repeats, max_repeats);
4644 LFPTOD(&ldiff, diff);
4645 tick = min(diff, tick);
4650 if (changes < MINCHANGES) {
4651 msyslog(LOG_ERR, "Fatal error: precision could not be measured (MINSTEP too large?)");
4655 if (0 == max_repeats) {
4658 set_sys_fuzz(tick / max_repeats);
4666 set_sys_tick_precision(
4674 "unsupported tick %.3f > 1s ignored", tick);
4677 if (tick < measured_tick) {
4679 "proto: tick %.3f less than measured tick %.3f, ignored",
4680 tick, measured_tick);
4682 } else if (tick > measured_tick) {
4683 trunc_os_clock = TRUE;
4685 "proto: truncating system clock to multiples of %.9f",
4691 * Find the nearest power of two.
4693 for (i = 0; tick <= 1; i--)
4695 if (tick - 1 > 1 - tick / 2)
4698 sys_precision = (s_char)i;
4703 * init_proto - initialize the protocol module's data
4712 * Fill in the sys_* stuff. Default is don't listen to
4713 * broadcasting, require authentication.
4715 set_sys_leap(LEAP_NOTINSYNC);
4716 sys_stratum = STRATUM_UNSPEC;
4717 memcpy(&sys_refid, "INIT", 4);
4721 L_CLR(&sys_reftime);
4723 measure_precision();
4724 get_systime(&dummy);
4726 sys_manycastserver = 0;
4728 sys_bdelay = BDELAY_DEFAULT; /*[Bug 3031] delay cutoff */
4729 sys_authenticate = 1;
4730 sys_stattime = current_time;
4731 orphwait = current_time + sys_orphwait;
4733 for (i = 0; i < MAX_TTL; ++i)
4734 sys_ttl[i] = (u_char)((i * 256) / MAX_TTL);
4735 sys_ttlmax = (MAX_TTL - 1);
4742 * proto_config - configure the protocol module
4753 * Figure out what he wants to change, then do it
4755 DPRINTF(2, ("proto_config: code %d value %lu dvalue %lf\n",
4756 item, value, dvalue));
4761 * enable and disable commands - arguments are Boolean.
4763 case PROTO_AUTHENTICATE: /* authentication (auth) */
4764 sys_authenticate = value;
4767 case PROTO_BROADCLIENT: /* broadcast client (bclient) */
4768 sys_bclient = (int)value;
4769 if (sys_bclient == 0)
4776 case PROTO_CAL: /* refclock calibrate (calibrate) */
4779 #endif /* REFCLOCK */
4781 case PROTO_KERNEL: /* kernel discipline (kernel) */
4785 case PROTO_MONITOR: /* monitoring (monitor) */
4791 msyslog(LOG_WARNING,
4792 "restrict: 'monitor' cannot be disabled while 'limited' is enabled");
4796 case PROTO_NTP: /* NTP discipline (ntp) */
4800 case PROTO_MODE7: /* mode7 management (ntpdc) */
4804 case PROTO_PPS: /* PPS discipline (pps) */
4805 hardpps_enable = value;
4808 case PROTO_FILEGEN: /* statistics (stats) */
4809 stats_control = value;
4813 * tos command - arguments are double, sometimes cast to int
4816 case PROTO_BCPOLLBSTEP: /* Broadcast Poll Backstep gate (bcpollbstep) */
4817 sys_bcpollbstep = (u_char)dvalue;
4820 case PROTO_BEACON: /* manycast beacon (beacon) */
4821 sys_beacon = (int)dvalue;
4824 case PROTO_BROADDELAY: /* default broadcast delay (bdelay) */
4825 sys_bdelay = (dvalue ? dvalue : BDELAY_DEFAULT);
4828 case PROTO_CEILING: /* stratum ceiling (ceiling) */
4829 sys_ceiling = (int)dvalue;
4832 case PROTO_COHORT: /* cohort switch (cohort) */
4833 sys_cohort = (int)dvalue;
4836 case PROTO_FLOOR: /* stratum floor (floor) */
4837 sys_floor = (int)dvalue;
4840 case PROTO_MAXCLOCK: /* maximum candidates (maxclock) */
4841 sys_maxclock = (int)dvalue;
4844 case PROTO_MAXDIST: /* select threshold (maxdist) */
4845 sys_maxdist = dvalue;
4848 case PROTO_CALLDELAY: /* modem call delay (mdelay) */
4849 break; /* NOT USED */
4851 case PROTO_MINCLOCK: /* minimum candidates (minclock) */
4852 sys_minclock = (int)dvalue;
4855 case PROTO_MINDISP: /* minimum distance (mindist) */
4856 sys_mindisp = dvalue;
4859 case PROTO_MINSANE: /* minimum survivors (minsane) */
4860 sys_minsane = (int)dvalue;
4863 case PROTO_ORPHAN: /* orphan stratum (orphan) */
4864 sys_orphan = (int)dvalue;
4867 case PROTO_ORPHWAIT: /* orphan wait (orphwait) */
4868 orphwait -= sys_orphwait;
4869 sys_orphwait = (int)dvalue;
4870 orphwait += sys_orphwait;
4874 * Miscellaneous commands
4876 case PROTO_MULTICAST_ADD: /* add group address */
4878 io_multicast_add(svalue);
4882 case PROTO_MULTICAST_DEL: /* delete group address */
4884 io_multicast_del(svalue);
4888 * Peer_clear Early policy choices
4891 case PROTO_PCEDIGEST: /* Digest */
4892 peer_clear_digest_early = value;
4896 * Unpeer Early policy choices
4899 case PROTO_UECRYPTO: /* Crypto */
4900 unpeer_crypto_early = value;
4903 case PROTO_UECRYPTONAK: /* Crypto_NAK */
4904 unpeer_crypto_nak_early = value;
4907 case PROTO_UEDIGEST: /* Digest */
4908 unpeer_digest_early = value;
4913 "proto: unsupported option %d", item);
4919 * proto_clr_stats - clear protocol stat counters
4922 proto_clr_stats(void)
4924 sys_stattime = current_time;
4933 sys_limitrejected = 0;