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"
29 * This macro defines the authentication state. If x is 1 authentication
30 * is required; othewise it is optional.
32 #define AUTH(x, y) ((x) ? (y) == AUTH_OK \
33 : (y) == AUTH_OK || (y) == AUTH_NONE)
35 #define AUTH_NONE 0 /* authentication not required */
36 #define AUTH_OK 1 /* authentication OK */
37 #define AUTH_ERROR 2 /* authentication error */
38 #define AUTH_CRYPTO 3 /* crypto_NAK */
41 * Set up Kiss Code values
45 NOKISS, /* No Kiss Code */
46 RATEKISS, /* Rate limit Kiss Code */
47 DENYKISS, /* Deny Kiss */
48 RSTRKISS, /* Restricted Kiss */
49 XKISS, /* Experimental Kiss */
50 UNKNOWNKISS /* Unknown Kiss Code */
54 * traffic shaping parameters
56 #define NTP_IBURST 6 /* packets in iburst */
57 #define RESP_DELAY 1 /* refclock burst delay (s) */
60 * pool soliciting restriction duration (s)
62 #define POOL_SOLICIT_WINDOW 8
65 * peer_select groups statistics for a peer used by clock_select() and
68 typedef struct peer_select_tag {
70 double synch; /* sync distance */
71 double error; /* jitter */
72 double seljit; /* selection jitter */
76 * System variables are declared here. Unless specified otherwise, all
77 * times are in seconds.
79 u_char sys_leap; /* system leap indicator, use set_sys_leap() to change this */
80 u_char xmt_leap; /* leap indicator sent in client requests, set up by set_sys_leap() */
81 u_char sys_stratum; /* system stratum */
82 s_char sys_precision; /* local clock precision (log2 s) */
83 double sys_rootdelay; /* roundtrip delay to primary source */
84 double sys_rootdisp; /* dispersion to primary source */
85 u_int32 sys_refid; /* reference id (network byte order) */
86 l_fp sys_reftime; /* last update time */
87 struct peer *sys_peer; /* current peer */
90 struct leap_smear_info leap_smear;
92 int leap_sec_in_progress;
95 * Rate controls. Leaky buckets are used to throttle the packet
96 * transmission rates in order to protect busy servers such as at NIST
97 * and USNO. There is a counter for each association and another for KoD
98 * packets. The association counter decrements each second, but not
99 * below zero. Each time a packet is sent the counter is incremented by
100 * a configurable value representing the average interval between
101 * packets. A packet is delayed as long as the counter is greater than
102 * zero. Note this does not affect the time value computations.
105 * Nonspecified system state variables
107 int sys_bclient; /* broadcast client enable */
108 double sys_bdelay; /* broadcast client default delay */
109 int sys_authenticate; /* requre authentication for config */
110 l_fp sys_authdelay; /* authentication delay */
111 double sys_offset; /* current local clock offset */
112 double sys_mindisp = MINDISPERSE; /* minimum distance (s) */
113 double sys_maxdist = MAXDISTANCE; /* selection threshold */
114 double sys_jitter; /* system jitter */
115 u_long sys_epoch; /* last clock update time */
116 static double sys_clockhop; /* clockhop threshold */
117 static int leap_vote_ins; /* leap consensus for insert */
118 static int leap_vote_del; /* leap consensus for delete */
119 keyid_t sys_private; /* private value for session seed */
120 int sys_manycastserver; /* respond to manycast client pkts */
121 int ntp_mode7; /* respond to ntpdc (mode7) */
122 int peer_ntpdate; /* active peers in ntpdate mode */
123 int sys_survivors; /* truest of the truechimers */
124 char *sys_ident = NULL; /* identity scheme */
127 * TOS and multicast mapping stuff
129 int sys_floor = 0; /* cluster stratum floor */
130 int sys_ceiling = STRATUM_UNSPEC - 1; /* cluster stratum ceiling */
131 int sys_minsane = 1; /* minimum candidates */
132 int sys_minclock = NTP_MINCLOCK; /* minimum candidates */
133 int sys_maxclock = NTP_MAXCLOCK; /* maximum candidates */
134 int sys_cohort = 0; /* cohort switch */
135 int sys_orphan = STRATUM_UNSPEC + 1; /* orphan stratum */
136 int sys_orphwait = NTP_ORPHWAIT; /* orphan wait */
137 int sys_beacon = BEACON; /* manycast beacon interval */
138 int sys_ttlmax; /* max ttl mapping vector index */
139 u_char sys_ttl[MAX_TTL]; /* ttl mapping vector */
142 * Statistics counters - first the good, then the bad
144 u_long sys_stattime; /* elapsed time */
145 u_long sys_received; /* packets received */
146 u_long sys_processed; /* packets for this host */
147 u_long sys_newversion; /* current version */
148 u_long sys_oldversion; /* old version */
149 u_long sys_restricted; /* access denied */
150 u_long sys_badlength; /* bad length or format */
151 u_long sys_badauth; /* bad authentication */
152 u_long sys_declined; /* declined */
153 u_long sys_limitrejected; /* rate exceeded */
154 u_long sys_kodsent; /* KoD sent */
157 * Mechanism knobs: how soon do we unpeer()?
159 * The default way is "on-receipt". If this was a packet from a
160 * well-behaved source, on-receipt will offer the fastest recovery.
161 * If this was from a DoS attack, the default way makes it easier
162 * for a bad-guy to DoS us. So look and see what bites you harder
163 * and choose according to your environment.
165 int unpeer_crypto_early = 1; /* bad crypto (TEST9) */
166 int unpeer_crypto_nak_early = 1; /* crypto_NAK (TEST5) */
167 int unpeer_digest_early = 1; /* bad digest (TEST5) */
169 static int kiss_code_check(u_char hisleap, u_char hisstratum, u_char hismode, u_int32 refid);
170 static double root_distance (struct peer *);
171 static void clock_combine (peer_select *, int, int);
172 static void peer_xmit (struct peer *);
173 static void fast_xmit (struct recvbuf *, int, keyid_t, int);
174 static void pool_xmit (struct peer *);
175 static void clock_update (struct peer *);
176 static void measure_precision(void);
177 static double measure_tick_fuzz(void);
178 static int local_refid (struct peer *);
179 static int peer_unfit (struct peer *);
181 static int group_test (char *, char *);
184 void pool_name_resolved (int, int, void *, const char *,
185 const char *, const struct addrinfo *,
186 const struct addrinfo *);
189 const char * amtoa (int am);
197 sys_leap = new_sys_leap;
201 * Under certain conditions we send faked leap bits to clients, so
202 * eventually change xmt_leap below, but never change LEAP_NOTINSYNC.
204 if (xmt_leap != LEAP_NOTINSYNC) {
205 if (leap_sec_in_progress) {
206 /* always send "not sync" */
207 xmt_leap = LEAP_NOTINSYNC;
212 * If leap smear is enabled in general we must
213 * never send a leap second warning to clients,
214 * so make sure we only send "in sync".
216 if (leap_smear.enabled)
217 xmt_leap = LEAP_NOWARNING;
219 #endif /* LEAP_SMEAR */
236 if ( hismode == MODE_SERVER
237 && hisleap == LEAP_NOTINSYNC
238 && hisstratum == STRATUM_UNSPEC) {
239 if(memcmp(&refid,"RATE", 4) == 0) {
241 } else if(memcmp(&refid,"DENY", 4) == 0) {
243 } else if(memcmp(&refid,"RSTR", 4) == 0) {
245 } else if(memcmp(&refid,"X", 1) == 0) {
248 return (UNKNOWNKISS);
257 * transmit - transmit procedure called by poll timeout
261 struct peer *peer /* peer structure pointer */
267 * The polling state machine. There are two kinds of machines,
268 * those that never expect a reply (broadcast and manycast
269 * server modes) and those that do (all other modes). The dance
275 * In broadcast mode the poll interval is never changed from
278 if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) {
279 peer->outdate = current_time;
280 if (sys_leap != LEAP_NOTINSYNC)
282 poll_update(peer, hpoll);
287 * In manycast mode we start with unity ttl. The ttl is
288 * increased by one for each poll until either sys_maxclock
289 * servers have been found or the maximum ttl is reached. When
290 * sys_maxclock servers are found we stop polling until one or
291 * more servers have timed out or until less than sys_minclock
292 * associations turn up. In this case additional better servers
293 * are dragged in and preempt the existing ones. Once every
294 * sys_beacon seconds we are to transmit unconditionally, but
295 * this code is not quite right -- peer->unreach counts polls
296 * and is being compared with sys_beacon, so the beacons happen
297 * every sys_beacon polls.
299 if (peer->cast_flags & MDF_ACAST) {
300 peer->outdate = current_time;
301 if (peer->unreach > sys_beacon) {
305 } else if ( sys_survivors < sys_minclock
306 || peer_associations < sys_maxclock) {
307 if (peer->ttl < (u_int32)sys_ttlmax)
312 poll_update(peer, hpoll);
317 * Pool associations transmit unicast solicitations when there
318 * are less than a hard limit of 2 * sys_maxclock associations,
319 * and either less than sys_minclock survivors or less than
320 * sys_maxclock associations. The hard limit prevents unbounded
321 * growth in associations if the system clock or network quality
322 * result in survivor count dipping below sys_minclock often.
323 * This was observed testing with pool, where sys_maxclock == 12
324 * resulted in 60 associations without the hard limit. A
325 * similar hard limit on manycastclient ephemeral associations
326 * may be appropriate.
328 if (peer->cast_flags & MDF_POOL) {
329 peer->outdate = current_time;
330 if ( (peer_associations <= 2 * sys_maxclock)
331 && ( peer_associations < sys_maxclock
332 || sys_survivors < sys_minclock))
334 poll_update(peer, hpoll);
339 * In unicast modes the dance is much more intricate. It is
340 * designed to back off whenever possible to minimize network
343 if (peer->burst == 0) {
347 * Update the reachability status. If not heard for
348 * three consecutive polls, stuff infinity in the clock
351 oreach = peer->reach;
352 peer->outdate = current_time;
358 * Here the peer is unreachable. If it was
359 * previously reachable raise a trap. Send a
362 clock_filter(peer, 0., 0., MAXDISPERSE);
365 report_event(PEVNT_UNREACH, peer, NULL);
367 if ( (peer->flags & FLAG_IBURST)
369 peer->retry = NTP_RETRY;
373 * Here the peer is reachable. Send a burst if
374 * enabled and the peer is fit. Reset unreach
375 * for persistent and ephemeral associations.
376 * Unreach is also reset for survivors in
380 if (!(peer->flags & FLAG_PREEMPT))
382 if ( (peer->flags & FLAG_BURST)
384 && !peer_unfit(peer))
385 peer->retry = NTP_RETRY;
389 * Watch for timeout. If ephemeral, toss the rascal;
390 * otherwise, bump the poll interval. Note the
391 * poll_update() routine will clamp it to maxpoll.
392 * If preemptible and we have more peers than maxclock,
393 * and this peer has the minimum score of preemptibles,
396 if (peer->unreach >= NTP_UNREACH) {
398 /* ephemeral: no FLAG_CONFIG nor FLAG_PREEMPT */
399 if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
400 report_event(PEVNT_RESTART, peer, "timeout");
401 peer_clear(peer, "TIME");
405 if ( (peer->flags & FLAG_PREEMPT)
406 && (peer_associations > sys_maxclock)
407 && score_all(peer)) {
408 report_event(PEVNT_RESTART, peer, "timeout");
409 peer_clear(peer, "TIME");
416 if (peer->burst == 0) {
419 * If ntpdate mode and the clock has not been
420 * set and all peers have completed the burst,
421 * we declare a successful failure.
425 if (peer_ntpdate == 0) {
427 "ntpd: no servers found");
430 "ntpd: no servers found\n");
440 * Do not transmit if in broadcast client mode.
442 if (peer->hmode != MODE_BCLIENT)
444 poll_update(peer, hpoll);
458 case AM_ERR: return "AM_ERR";
459 case AM_NOMATCH: return "AM_NOMATCH";
460 case AM_PROCPKT: return "AM_PROCPKT";
461 case AM_BCST: return "AM_BCST";
462 case AM_FXMIT: return "AM_FXMIT";
463 case AM_MANYCAST: return "AM_MANYCAST";
464 case AM_NEWPASS: return "AM_NEWPASS";
465 case AM_NEWBCL: return "AM_NEWBCL";
466 case AM_POSSBCL: return "AM_POSSBCL";
469 snprintf(bp, LIB_BUFLENGTH, "AM_#%d", am);
476 * receive - receive procedure called for each packet received
480 struct recvbuf *rbufp
483 register struct peer *peer; /* peer structure pointer */
484 register struct pkt *pkt; /* receive packet pointer */
485 u_char hisversion; /* packet version */
486 u_char hisleap; /* packet leap indicator */
487 u_char hismode; /* packet mode */
488 u_char hisstratum; /* packet stratum */
489 u_short restrict_mask; /* restrict bits */
490 const char *hm_str; /* hismode string */
491 const char *am_str; /* association match string */
492 int kissCode = NOKISS; /* Kiss Code */
493 int has_mac; /* length of MAC field */
494 int authlen; /* offset of MAC field */
495 int is_authentic = 0; /* cryptosum ok */
496 int retcode = AM_NOMATCH; /* match code */
497 keyid_t skeyid = 0; /* key IDs */
498 u_int32 opcode = 0; /* extension field opcode */
499 sockaddr_u *dstadr_sin; /* active runway */
500 struct peer *peer2; /* aux peer structure pointer */
501 endpt *match_ep; /* newpeer() local address */
502 l_fp p_org; /* origin timestamp */
503 l_fp p_rec; /* receive timestamp */
504 l_fp p_xmt; /* transmit timestamp */
506 char hostname[NTP_MAXSTRLEN + 1];
507 char *groupname = NULL;
508 struct autokey *ap; /* autokey structure pointer */
509 int rval; /* cookie snatcher */
510 keyid_t pkeyid = 0, tkeyid = 0; /* key IDs */
512 #ifdef HAVE_NTP_SIGND
513 static unsigned char zero_key[16];
514 #endif /* HAVE_NTP_SIGND */
517 * Monitor the packet and get restrictions. Note that the packet
518 * length for control and private mode packets must be checked
519 * by the service routines. Some restrictions have to be handled
520 * later in order to generate a kiss-o'-death packet.
523 * Bogus port check is before anything, since it probably
524 * reveals a clogging attack.
527 if (0 == SRCPORT(&rbufp->recv_srcadr)) {
529 return; /* bogus port */
531 restrict_mask = restrictions(&rbufp->recv_srcadr);
532 pkt = &rbufp->recv_pkt;
533 DPRINTF(2, ("receive: at %ld %s<-%s flags %x restrict %03x org %#010x.%08x xmt %#010x.%08x\n",
534 current_time, stoa(&rbufp->dstadr->sin),
535 stoa(&rbufp->recv_srcadr), rbufp->dstadr->flags,
536 restrict_mask, ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
537 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
538 hisversion = PKT_VERSION(pkt->li_vn_mode);
539 hisleap = PKT_LEAP(pkt->li_vn_mode);
540 hismode = (int)PKT_MODE(pkt->li_vn_mode);
541 hisstratum = PKT_TO_STRATUM(pkt->stratum);
542 if (restrict_mask & RES_IGNORE) {
544 return; /* ignore everything */
546 if (hismode == MODE_PRIVATE) {
547 if (!ntp_mode7 || (restrict_mask & RES_NOQUERY)) {
549 return; /* no query private */
551 process_private(rbufp, ((restrict_mask &
552 RES_NOMODIFY) == 0));
555 if (hismode == MODE_CONTROL) {
556 if (restrict_mask & RES_NOQUERY) {
558 return; /* no query control */
560 process_control(rbufp, restrict_mask);
563 if (restrict_mask & RES_DONTSERVE) {
565 return; /* no time serve */
569 * This is for testing. If restricted drop ten percent of
572 if (restrict_mask & RES_FLAKE) {
573 if ((double)ntp_random() / 0x7fffffff < .1) {
575 return; /* no flakeway */
580 * Version check must be after the query packets, since they
581 * intentionally use an early version.
583 if (hisversion == NTP_VERSION) {
584 sys_newversion++; /* new version */
585 } else if ( !(restrict_mask & RES_VERSION)
586 && hisversion >= NTP_OLDVERSION) {
587 sys_oldversion++; /* previous version */
590 return; /* old version */
594 * Figure out his mode and validate the packet. This has some
595 * legacy raunch that probably should be removed. In very early
596 * NTP versions mode 0 was equivalent to what later versions
597 * would interpret as client mode.
599 if (hismode == MODE_UNSPEC) {
600 if (hisversion == NTP_OLDVERSION) {
601 hismode = MODE_CLIENT;
604 return; /* invalid mode */
609 * Parse the extension field if present. We figure out whether
610 * an extension field is present by measuring the MAC size. If
611 * the number of words following the packet header is 0, no MAC
612 * is present and the packet is not authenticated. If 1, the
613 * packet is a crypto-NAK; if 3, the packet is authenticated
614 * with DES; if 5, the packet is authenticated with MD5; if 6,
615 * the packet is authenticated with SHA. If 2 or * 4, the packet
616 * is a runt and discarded forthwith. If greater than 6, an
617 * extension field is present, so we subtract the length of the
618 * field and go around again.
620 authlen = LEN_PKT_NOMAC;
621 has_mac = rbufp->recv_length - authlen;
622 while (has_mac > 0) {
629 if (has_mac % 4 != 0 || has_mac < (int)MIN_MAC_LEN) {
631 return; /* bad length */
633 if (has_mac <= (int)MAX_MAC_LEN) {
634 skeyid = ntohl(((u_int32 *)pkt)[authlen / 4]);
638 opcode = ntohl(((u_int32 *)pkt)[authlen / 4]);
639 len = opcode & 0xffff;
642 || (int)len + authlen > rbufp->recv_length) {
644 return; /* bad length */
648 * Extract calling group name for later. If
649 * sys_groupname is non-NULL, there must be
650 * a group name provided to elicit a response.
652 if ( (opcode & 0x3fff0000) == CRYPTO_ASSOC
653 && sys_groupname != NULL) {
654 ep = (struct exten *)&((u_int32 *)pkt)[authlen / 4];
655 hostlen = ntohl(ep->vallen);
656 if ( hostlen >= sizeof(hostname)
658 offsetof(struct exten, pkt)) {
660 return; /* bad length */
662 memcpy(hostname, &ep->pkt, hostlen);
663 hostname[hostlen] = '\0';
664 groupname = strchr(hostname, '@');
665 if (groupname == NULL) {
678 * If has_mac is < 0 we had a malformed packet.
682 return; /* bad length */
686 * If authentication required, a MAC must be present.
688 if (restrict_mask & RES_DONTTRUST && has_mac == 0) {
690 return; /* access denied */
694 * Update the MRU list and finger the cloggers. It can be a
695 * little expensive, so turn it off for production use.
696 * RES_LIMITED and RES_KOD will be cleared in the returned
697 * restrict_mask unless one or both actions are warranted.
699 restrict_mask = ntp_monitor(rbufp, restrict_mask);
700 if (restrict_mask & RES_LIMITED) {
702 if ( !(restrict_mask & RES_KOD)
703 || MODE_BROADCAST == hismode
704 || MODE_SERVER == hismode) {
705 if (MODE_SERVER == hismode)
706 DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
707 stoa(&rbufp->recv_srcadr)));
708 return; /* rate exceeded */
710 if (hismode == MODE_CLIENT)
711 fast_xmit(rbufp, MODE_SERVER, skeyid,
714 fast_xmit(rbufp, MODE_ACTIVE, skeyid,
716 return; /* rate exceeded */
718 restrict_mask &= ~RES_KOD;
721 * We have tossed out as many buggy packets as possible early in
722 * the game to reduce the exposure to a clogging attack. Now we
723 * have to burn some cycles to find the association and
724 * authenticate the packet if required. Note that we burn only
725 * digest cycles, again to reduce exposure. There may be no
726 * matching association and that's okay.
728 * More on the autokey mambo. Normally the local interface is
729 * found when the association was mobilized with respect to a
730 * designated remote address. We assume packets arriving from
731 * the remote address arrive via this interface and the local
732 * address used to construct the autokey is the unicast address
733 * of the interface. However, if the sender is a broadcaster,
734 * the interface broadcast address is used instead.
735 * Notwithstanding this technobabble, if the sender is a
736 * multicaster, the broadcast address is null, so we use the
737 * unicast address anyway. Don't ask.
739 peer = findpeer(rbufp, hismode, &retcode);
740 dstadr_sin = &rbufp->dstadr->sin;
741 NTOHL_FP(&pkt->org, &p_org);
742 NTOHL_FP(&pkt->rec, &p_rec);
743 NTOHL_FP(&pkt->xmt, &p_xmt);
744 hm_str = modetoa(hismode);
745 am_str = amtoa(retcode);
748 * Authentication is conditioned by three switches:
750 * NOPEER (RES_NOPEER) do not mobilize an association unless
752 * NOTRUST (RES_DONTTRUST) do not allow access unless
753 * authenticated (implies NOPEER)
754 * enable (sys_authenticate) master NOPEER switch, by default
757 * The NOPEER and NOTRUST can be specified on a per-client basis
758 * using the restrict command. The enable switch if on implies
759 * NOPEER for all clients. There are four outcomes:
761 * NONE The packet has no MAC.
762 * OK the packet has a MAC and authentication succeeds
763 * ERROR the packet has a MAC and authentication fails
764 * CRYPTO crypto-NAK. The MAC has four octets only.
766 * Note: The AUTH(x, y) macro is used to filter outcomes. If x
767 * is zero, acceptable outcomes of y are NONE and OK. If x is
768 * one, the only acceptable outcome of y is OK.
772 restrict_mask &= ~RES_MSSNTP;
773 is_authentic = AUTH_NONE; /* not required */
774 DPRINTF(2, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x NOMAC\n",
775 current_time, stoa(dstadr_sin),
776 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
778 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
779 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
780 } else if (has_mac == 4) {
781 restrict_mask &= ~RES_MSSNTP;
782 is_authentic = AUTH_CRYPTO; /* crypto-NAK */
783 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",
784 current_time, stoa(dstadr_sin),
785 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
786 skeyid, authlen + has_mac, is_authentic,
787 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
788 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
790 #ifdef HAVE_NTP_SIGND
792 * If the signature is 20 bytes long, the last 16 of
793 * which are zero, then this is a Microsoft client
794 * wanting AD-style authentication of the server's
797 * This is described in Microsoft's WSPP docs, in MS-SNTP:
798 * http://msdn.microsoft.com/en-us/library/cc212930.aspx
800 } else if ( has_mac == MAX_MD5_LEN
801 && (restrict_mask & RES_MSSNTP)
802 && (retcode == AM_FXMIT || retcode == AM_NEWPASS)
803 && (memcmp(zero_key, (char *)pkt + authlen + 4,
804 MAX_MD5_LEN - 4) == 0)) {
805 is_authentic = AUTH_NONE;
806 #endif /* HAVE_NTP_SIGND */
809 restrict_mask &= ~RES_MSSNTP;
812 * For autokey modes, generate the session key
813 * and install in the key cache. Use the socket
814 * broadcast or unicast address as appropriate.
816 if (crypto_flags && skeyid > NTP_MAXKEY) {
819 * More on the autokey dance (AKD). A cookie is
820 * constructed from public and private values.
821 * For broadcast packets, the cookie is public
822 * (zero). For packets that match no
823 * association, the cookie is hashed from the
824 * addresses and private value. For server
825 * packets, the cookie was previously obtained
826 * from the server. For symmetric modes, the
827 * cookie was previously constructed using an
828 * agreement protocol; however, should PKI be
829 * unavailable, we construct a fake agreement as
830 * the EXOR of the peer and host cookies.
832 * hismode ephemeral persistent
833 * =======================================
836 * client sys cookie 0%
837 * server 0% sys cookie
843 if (has_mac < (int)MAX_MD5_LEN) {
847 if (hismode == MODE_BROADCAST) {
850 * For broadcaster, use the interface
851 * broadcast address when available;
852 * otherwise, use the unicast address
853 * found when the association was
854 * mobilized. However, if this is from
855 * the wildcard interface, game over.
859 ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr)) {
861 return; /* no wildcard */
864 if (!SOCK_UNSPEC(&rbufp->dstadr->bcast))
866 &rbufp->dstadr->bcast;
867 } else if (peer == NULL) {
868 pkeyid = session_key(
869 &rbufp->recv_srcadr, dstadr_sin, 0,
872 pkeyid = peer->pcookie;
876 * The session key includes both the public
877 * values and cookie. In case of an extension
878 * field, the cookie used for authentication
879 * purposes is zero. Note the hash is saved for
880 * use later in the autokey mambo.
882 if (authlen > (int)LEN_PKT_NOMAC && pkeyid != 0) {
883 session_key(&rbufp->recv_srcadr,
884 dstadr_sin, skeyid, 0, 2);
885 tkeyid = session_key(
886 &rbufp->recv_srcadr, dstadr_sin,
889 tkeyid = session_key(
890 &rbufp->recv_srcadr, dstadr_sin,
898 * Compute the cryptosum. Note a clogging attack may
899 * succeed in bloating the key cache. If an autokey,
900 * purge it immediately, since we won't be needing it
901 * again. If the packet is authentic, it can mobilize an
902 * association. Note that there is no key zero.
904 if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
906 is_authentic = AUTH_ERROR;
908 is_authentic = AUTH_OK;
910 if (crypto_flags && skeyid > NTP_MAXKEY)
911 authtrust(skeyid, 0);
913 DPRINTF(2, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x\n",
914 current_time, stoa(dstadr_sin),
915 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
916 skeyid, authlen + has_mac, is_authentic,
917 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
918 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
922 * The association matching rules are implemented by a set of
923 * routines and an association table. A packet matching an
924 * association is processed by the peer process for that
925 * association. If there are no errors, an ephemeral association
926 * is mobilized: a broadcast packet mobilizes a broadcast client
927 * aassociation; a manycast server packet mobilizes a manycast
928 * client association; a symmetric active packet mobilizes a
929 * symmetric passive association.
934 * This is a client mode packet not matching any association. If
935 * an ordinary client, simply toss a server mode packet back
936 * over the fence. If a manycast client, we have to work a
942 * If authentication OK, send a server reply; otherwise,
945 if (!(rbufp->dstadr->flags & INT_MCASTOPEN)) {
946 if (AUTH(restrict_mask & RES_DONTTRUST,
948 fast_xmit(rbufp, MODE_SERVER, skeyid,
950 } else if (is_authentic == AUTH_ERROR) {
951 fast_xmit(rbufp, MODE_SERVER, 0,
961 * This must be manycast. Do not respond if not
962 * configured as a manycast server.
964 if (!sys_manycastserver) {
966 return; /* not enabled */
971 * Do not respond if not the same group.
973 if (group_test(groupname, NULL)) {
980 * Do not respond if we are not synchronized or our
981 * stratum is greater than the manycaster or the
982 * manycaster has already synchronized to us.
984 if ( sys_leap == LEAP_NOTINSYNC
985 || sys_stratum >= hisstratum
986 || (!sys_cohort && sys_stratum == hisstratum + 1)
987 || rbufp->dstadr->addr_refid == pkt->refid) {
989 return; /* no help */
993 * Respond only if authentication succeeds. Don't do a
994 * crypto-NAK, as that would not be useful.
996 if (AUTH(restrict_mask & RES_DONTTRUST, is_authentic))
997 fast_xmit(rbufp, MODE_SERVER, skeyid,
1002 * This is a server mode packet returned in response to a client
1003 * mode packet sent to a multicast group address (for
1004 * manycastclient) or to a unicast address (for pool). The
1005 * origin timestamp is a good nonce to reliably associate the
1006 * reply with what was sent. If there is no match, that's
1007 * curious and could be an intruder attempting to clog, so we
1010 * If the packet is authentic and the manycastclient or pool
1011 * association is found, we mobilize a client association and
1012 * copy pertinent variables from the manycastclient or pool
1013 * association to the new client association. If not, just
1014 * ignore the packet.
1016 * There is an implosion hazard at the manycast client, since
1017 * the manycast servers send the server packet immediately. If
1018 * the guy is already here, don't fire up a duplicate.
1024 * Do not respond if not the same group.
1026 if (group_test(groupname, NULL)) {
1030 #endif /* AUTOKEY */
1031 if ((peer2 = findmanycastpeer(rbufp)) == NULL) {
1033 return; /* not enabled */
1035 if (!AUTH( (!(peer2->cast_flags & MDF_POOL)
1036 && sys_authenticate)
1037 || (restrict_mask & (RES_NOPEER |
1038 RES_DONTTRUST)), is_authentic)) {
1040 return; /* access denied */
1044 * Do not respond if unsynchronized or stratum is below
1045 * the floor or at or above the ceiling.
1047 if ( hisleap == LEAP_NOTINSYNC
1048 || hisstratum < sys_floor
1049 || hisstratum >= sys_ceiling) {
1051 return; /* no help */
1053 peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1054 MODE_CLIENT, hisversion, peer2->minpoll,
1055 peer2->maxpoll, FLAG_PREEMPT |
1056 (FLAG_IBURST & peer2->flags), MDF_UCAST |
1057 MDF_UCLNT, 0, skeyid, sys_ident);
1060 return; /* ignore duplicate */
1064 * After each ephemeral pool association is spun,
1065 * accelerate the next poll for the pool solicitor so
1066 * the pool will fill promptly.
1068 if (peer2->cast_flags & MDF_POOL)
1069 peer2->nextdate = current_time + 1;
1072 * Further processing of the solicitation response would
1073 * simply detect its origin timestamp as bogus for the
1074 * brand-new association (it matches the prototype
1075 * association) and tinker with peer->nextdate delaying
1078 return; /* solicitation response handled */
1081 * This is the first packet received from a broadcast server. If
1082 * the packet is authentic and we are enabled as broadcast
1083 * client, mobilize a broadcast client association. We don't
1084 * kiss any frogs here.
1090 * Do not respond if not the same group.
1092 if (group_test(groupname, sys_ident)) {
1096 #endif /* AUTOKEY */
1097 if (sys_bclient == 0) {
1099 return; /* not enabled */
1101 if (!AUTH(sys_authenticate | (restrict_mask &
1102 (RES_NOPEER | RES_DONTTRUST)), is_authentic)) {
1104 return; /* access denied */
1108 * Do not respond if unsynchronized or stratum is below
1109 * the floor or at or above the ceiling.
1111 if ( hisleap == LEAP_NOTINSYNC
1112 || hisstratum < sys_floor
1113 || hisstratum >= sys_ceiling) {
1115 return; /* no help */
1120 * Do not respond if Autokey and the opcode is not a
1121 * CRYPTO_ASSOC response with association ID.
1123 if ( crypto_flags && skeyid > NTP_MAXKEY
1124 && (opcode & 0xffff0000) != (CRYPTO_ASSOC | CRYPTO_RESP)) {
1126 return; /* protocol error */
1128 #endif /* AUTOKEY */
1131 * Broadcasts received via a multicast address may
1132 * arrive after a unicast volley has begun
1133 * with the same remote address. newpeer() will not
1134 * find duplicate associations on other local endpoints
1135 * if a non-NULL endpoint is supplied. multicastclient
1136 * ephemeral associations are unique across all local
1139 if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
1140 match_ep = rbufp->dstadr;
1145 * Determine whether to execute the initial volley.
1147 if (sys_bdelay != 0) {
1150 * If a two-way exchange is not possible,
1151 * neither is Autokey.
1153 if (crypto_flags && skeyid > NTP_MAXKEY) {
1155 return; /* no autokey */
1157 #endif /* AUTOKEY */
1160 * Do not execute the volley. Start out in
1161 * broadcast client mode.
1163 peer = newpeer(&rbufp->recv_srcadr, NULL,
1164 match_ep, MODE_BCLIENT, hisversion,
1165 pkt->ppoll, pkt->ppoll, FLAG_PREEMPT,
1166 MDF_BCLNT, 0, skeyid, sys_ident);
1169 return; /* ignore duplicate */
1172 peer->delay = sys_bdelay;
1179 * Execute the initial volley in order to calibrate the
1180 * propagation delay and run the Autokey protocol.
1182 * Note that the minpoll is taken from the broadcast
1183 * packet, normally 6 (64 s) and that the poll interval
1184 * is fixed at this value.
1186 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1187 MODE_CLIENT, hisversion, pkt->ppoll, pkt->ppoll,
1188 FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
1189 0, skeyid, sys_ident);
1192 return; /* ignore duplicate */
1196 if (skeyid > NTP_MAXKEY)
1197 crypto_recv(peer, rbufp);
1198 #endif /* AUTOKEY */
1200 return; /* hooray */
1203 * This is the first packet received from a symmetric active
1204 * peer. If the packet is authentic and the first he sent,
1205 * mobilize a passive association. If not, kiss the frog.
1211 * Do not respond if not the same group.
1213 if (group_test(groupname, sys_ident)) {
1217 #endif /* AUTOKEY */
1218 if (!AUTH(sys_authenticate | (restrict_mask &
1219 (RES_NOPEER | RES_DONTTRUST)), is_authentic)) {
1222 * If authenticated but cannot mobilize an
1223 * association, send a symmetric passive
1224 * response without mobilizing an association.
1225 * This is for drat broken Windows clients. See
1226 * Microsoft KB 875424 for preferred workaround.
1228 if (AUTH(restrict_mask & RES_DONTTRUST,
1230 fast_xmit(rbufp, MODE_PASSIVE, skeyid,
1232 return; /* hooray */
1234 if (is_authentic == AUTH_ERROR) {
1235 fast_xmit(rbufp, MODE_ACTIVE, 0,
1241 * If we got here, the packet isn't part of an
1242 * existing association, it isn't correctly
1243 * authenticated, and it didn't meet either of
1244 * the previous two special cases so we should
1245 * just drop it on the floor. For example,
1246 * crypto-NAKs (is_authentic == AUTH_CRYPTO)
1247 * will make it this far. This is just
1248 * debug-printed and not logged to avoid log
1251 DPRINTF(2, ("receive: at %ld refusing to mobilize passive association"
1252 " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1253 current_time, stoa(&rbufp->recv_srcadr),
1254 hismode, hm_str, am_str, skeyid,
1255 (authlen + has_mac), is_authentic));
1261 * Do not respond if synchronized and if stratum is
1262 * below the floor or at or above the ceiling. Note,
1263 * this allows an unsynchronized peer to synchronize to
1264 * us. It would be very strange if he did and then was
1265 * nipped, but that could only happen if we were
1266 * operating at the top end of the range. It also means
1267 * we will spin an ephemeral association in response to
1268 * MODE_ACTIVE KoDs, which will time out eventually.
1270 if ( hisleap != LEAP_NOTINSYNC
1271 && (hisstratum < sys_floor || hisstratum >= sys_ceiling)) {
1273 return; /* no help */
1277 * The message is correctly authenticated and allowed.
1278 * Mobilize a symmetric passive association.
1280 if ((peer = newpeer(&rbufp->recv_srcadr, NULL,
1281 rbufp->dstadr, MODE_PASSIVE, hisversion, pkt->ppoll,
1282 NTP_MAXDPOLL, 0, MDF_UCAST, 0, skeyid,
1283 sys_ident)) == NULL) {
1285 return; /* ignore duplicate */
1291 * Process regular packet. Nothing special.
1297 * Do not respond if not the same group.
1299 if (group_test(groupname, peer->ident)) {
1303 #endif /* AUTOKEY */
1305 if (MODE_BROADCAST == hismode) {
1310 DPRINTF(2, ("receive: PROCPKT/BROADCAST: prev pkt %ld seconds ago, ppoll: %d, %d secs\n",
1311 (current_time - peer->timelastrec),
1312 peer->ppoll, (1 << peer->ppoll)
1314 /* Things we can check:
1316 * Did the poll interval change?
1317 * Is the poll interval in the packet in-range?
1318 * Did this packet arrive too soon?
1319 * Is the timestamp in this packet monotonic
1320 * with respect to the previous packet?
1323 /* This is noteworthy, not error-worthy */
1324 if (pkt->ppoll != peer->ppoll) {
1325 msyslog(LOG_INFO, "receive: broadcast poll from %s changed from %ud to %ud",
1326 stoa(&rbufp->recv_srcadr),
1327 peer->ppoll, pkt->ppoll);
1330 poll = min(peer->maxpoll,
1331 max(peer->minpoll, pkt->ppoll));
1333 /* This is error-worthy */
1334 if (pkt->ppoll != poll) {
1335 msyslog(LOG_INFO, "receive: broadcast poll of %ud from %s is out-of-range (%d to %d)!",
1336 pkt->ppoll, stoa(&rbufp->recv_srcadr),
1337 peer->minpoll, peer->maxpoll);
1341 if ( (current_time - peer->timelastrec)
1342 < (1 << pkt->ppoll)) {
1343 msyslog(LOG_INFO, "receive: broadcast packet from %s arrived after %ld, not %d seconds!",
1344 stoa(&rbufp->recv_srcadr),
1345 (current_time - peer->timelastrec),
1352 L_SUB(&tdiff, &peer->bxmt);
1353 if (tdiff.l_i < 0) {
1354 msyslog(LOG_INFO, "receive: broadcast packet from %s contains non-monotonic timestamp: %#010x.%08x -> %#010x.%08x",
1355 stoa(&rbufp->recv_srcadr),
1356 peer->bxmt.l_ui, peer->bxmt.l_uf,
1357 p_xmt.l_ui, p_xmt.l_uf
1365 peer->timelastrec = current_time;
1374 * A passive packet matches a passive association. This is
1375 * usually the result of reconfiguring a client on the fly. As
1376 * this association might be legitimate and this packet an
1377 * attempt to deny service, just ignore it.
1384 * For everything else there is the bit bucket.
1393 * If the association is configured for Autokey, the packet must
1394 * have a public key ID; if not, the packet must have a
1397 if ( is_authentic != AUTH_CRYPTO
1398 && ( ((peer->flags & FLAG_SKEY) && skeyid <= NTP_MAXKEY)
1399 || (!(peer->flags & FLAG_SKEY) && skeyid > NTP_MAXKEY))) {
1403 #endif /* AUTOKEY */
1405 peer->flash &= ~PKT_TEST_MASK;
1406 if (peer->flags & FLAG_XBOGUS) {
1407 peer->flags &= ~FLAG_XBOGUS;
1408 peer->flash |= TEST3;
1412 * Next comes a rigorous schedule of timestamp checking. If the
1413 * transmit timestamp is zero, the server has not initialized in
1414 * interleaved modes or is horribly broken.
1416 if (L_ISZERO(&p_xmt)) {
1417 peer->flash |= TEST3; /* unsynch */
1420 * If the transmit timestamp duplicates a previous one, the
1421 * packet is a replay. This prevents the bad guys from replaying
1422 * the most recent packet, authenticated or not.
1424 } else if (L_ISEQU(&peer->xmt, &p_xmt)) {
1425 peer->flash |= TEST1; /* duplicate */
1430 * If this is a broadcast mode packet, skip further checking. If
1431 * an initial volley, bail out now and let the client do its
1432 * stuff. If the origin timestamp is nonzero, this is an
1433 * interleaved broadcast. so restart the protocol.
1435 } else if (hismode == MODE_BROADCAST) {
1436 if (!L_ISZERO(&p_org) && !(peer->flags & FLAG_XB)) {
1437 peer->flags |= FLAG_XB;
1439 peer->borg = rbufp->recv_time;
1440 report_event(PEVNT_XLEAVE, peer, NULL);
1445 * Basic mode checks:
1447 * If there is no origin timestamp, it's either an initial packet
1448 * or we've already received a response to our query. Of course,
1449 * should 'aorg' be all-zero because this really was the original
1450 * transmit timestamp, we'll drop the reply. There is a window of
1451 * one nanosecond once every 136 years' time where this is possible.
1452 * We currently ignore this situation.
1454 * Otherwise, check for bogus packet in basic mode.
1455 * If it is bogus, switch to interleaved mode and resynchronize,
1456 * but only after confirming the packet is not bogus in
1457 * symmetric interleaved mode.
1459 * This could also mean somebody is forging packets claiming to
1460 * be from us, attempting to cause our server to KoD us.
1462 } else if (peer->flip == 0) {
1463 if (0 < hisstratum && L_ISZERO(&p_org)) {
1465 } else if ( L_ISZERO(&peer->aorg)
1466 || !L_ISEQU(&p_org, &peer->aorg)) {
1468 peer->flash |= TEST2; /* bogus */
1470 "receive: Unexpected origin timestamp %#010x.%08x from %s xmt %#010x.%08x",
1471 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1472 ntoa(&peer->srcadr),
1473 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
1474 if ( !L_ISZERO(&peer->dst)
1475 && L_ISEQU(&p_org, &peer->dst)) {
1476 /* Might be the start of an interleave */
1478 report_event(PEVNT_XLEAVE, peer, NULL);
1480 return; /* Bogus or possible interleave packet */
1486 * Check for valid nonzero timestamp fields.
1488 } else if (L_ISZERO(&p_org) || L_ISZERO(&p_rec) ||
1489 L_ISZERO(&peer->dst)) {
1490 peer->flash |= TEST3; /* unsynch */
1493 * Check for bogus packet in interleaved symmetric mode. This
1494 * can happen if a packet is lost, duplicated or crossed. If
1495 * found, flip and resynchronize.
1497 } else if ( !L_ISZERO(&peer->dst)
1498 && !L_ISEQU(&p_org, &peer->dst)) {
1500 peer->flags |= FLAG_XBOGUS;
1501 peer->flash |= TEST2; /* bogus */
1502 return; /* Bogus packet, we are done */
1506 * If this is a crypto_NAK, the server cannot authenticate a
1507 * client packet. The server might have just changed keys. Clear
1508 * the association and restart the protocol.
1510 if (is_authentic == AUTH_CRYPTO) {
1511 report_event(PEVNT_AUTH, peer, "crypto_NAK");
1512 peer->flash |= TEST5; /* bad auth */
1514 if (peer->flags & FLAG_PREEMPT) {
1515 if (unpeer_crypto_nak_early) {
1522 peer_clear(peer, "AUTH");
1523 #endif /* AUTOKEY */
1527 * If the digest fails or it's missing for authenticated
1528 * associations, the client cannot authenticate a server
1529 * reply to a client packet previously sent. The loopback check
1530 * is designed to avoid a bait-and-switch attack, which was
1531 * possible in past versions. If symmetric modes, return a
1532 * crypto-NAK. The peer should restart the protocol.
1534 } else if (!AUTH(peer->keyid || has_mac ||
1535 (restrict_mask & RES_DONTTRUST), is_authentic)) {
1536 report_event(PEVNT_AUTH, peer, "digest");
1537 peer->flash |= TEST5; /* bad auth */
1540 && (hismode == MODE_ACTIVE || hismode == MODE_PASSIVE))
1541 fast_xmit(rbufp, MODE_ACTIVE, 0, restrict_mask);
1542 if (peer->flags & FLAG_PREEMPT) {
1543 if (unpeer_digest_early) {
1550 peer_clear(peer, "AUTH");
1551 #endif /* AUTOKEY */
1556 * Update the state variables.
1558 if (peer->flip == 0) {
1559 if (hismode != MODE_BROADCAST)
1561 peer->dst = rbufp->recv_time;
1566 * Set the peer ppoll to the maximum of the packet ppoll and the
1567 * peer minpoll. If a kiss-o'-death, set the peer minpoll to
1568 * this maximum and advance the headway to give the sender some
1569 * headroom. Very intricate.
1573 * Check for any kiss codes. Note this is only used when a server
1574 * responds to a packet request
1577 kissCode = kiss_code_check(hisleap, hisstratum, hismode, pkt->refid);
1580 * Check to see if this is a RATE Kiss Code
1581 * Currently this kiss code will accept whatever poll
1582 * rate that the server sends
1584 peer->ppoll = max(peer->minpoll, pkt->ppoll);
1585 if (kissCode == RATEKISS) {
1586 peer->selbroken++; /* Increment the KoD count */
1587 report_event(PEVNT_RATE, peer, NULL);
1588 if (pkt->ppoll > peer->minpoll)
1589 peer->minpoll = peer->ppoll;
1590 peer->burst = peer->retry = 0;
1591 peer->throttle = (NTP_SHIFT + 1) * (1 << peer->minpoll);
1592 poll_update(peer, pkt->ppoll);
1593 return; /* kiss-o'-death */
1595 if (kissCode != NOKISS) {
1596 peer->selbroken++; /* Increment the KoD count */
1597 return; /* Drop any other kiss code packets */
1602 * - this is a *cast (uni-, broad-, or m-) server packet
1603 * - and it's authenticated
1604 * then see if the sender's IP is trusted for this keyid.
1605 * If it is, great - nothing special to do here.
1606 * Otherwise, we should report and bail.
1610 case MODE_SERVER: /* server mode */
1611 case MODE_BROADCAST: /* broadcast mode */
1612 case MODE_ACTIVE: /* symmetric active mode */
1613 if ( is_authentic == AUTH_OK
1614 && !authistrustedip(skeyid, &peer->srcadr)) {
1615 report_event(PEVNT_AUTH, peer, "authIP");
1621 case MODE_UNSPEC: /* unspecified (old version) */
1622 case MODE_PASSIVE: /* symmetric passive mode */
1623 case MODE_CLIENT: /* client mode */
1624 #if 0 /* At this point, MODE_CONTROL is overloaded by MODE_BCLIENT */
1625 case MODE_CONTROL: /* control mode */
1627 case MODE_PRIVATE: /* private mode */
1628 case MODE_BCLIENT: /* broadcast client mode */
1636 * That was hard and I am sweaty, but the packet is squeaky
1637 * clean. Get on with real work.
1639 peer->timereceived = current_time;
1640 peer->timelastrec = current_time;
1641 if (is_authentic == AUTH_OK)
1642 peer->flags |= FLAG_AUTHENTIC;
1644 peer->flags &= ~FLAG_AUTHENTIC;
1648 * More autokey dance. The rules of the cha-cha are as follows:
1650 * 1. If there is no key or the key is not auto, do nothing.
1652 * 2. If this packet is in response to the one just previously
1653 * sent or from a broadcast server, do the extension fields.
1654 * Otherwise, assume bogosity and bail out.
1656 * 3. If an extension field contains a verified signature, it is
1657 * self-authenticated and we sit the dance.
1659 * 4. If this is a server reply, check only to see that the
1660 * transmitted key ID matches the received key ID.
1662 * 5. Check to see that one or more hashes of the current key ID
1663 * matches the previous key ID or ultimate original key ID
1664 * obtained from the broadcaster or symmetric peer. If no
1665 * match, sit the dance and call for new autokey values.
1667 * In case of crypto error, fire the orchestra, stop dancing and
1668 * restart the protocol.
1670 if (peer->flags & FLAG_SKEY) {
1672 * Decrement remaining autokey hashes. This isn't
1673 * perfect if a packet is lost, but results in no harm.
1675 ap = (struct autokey *)peer->recval.ptr;
1680 peer->flash |= TEST8;
1681 rval = crypto_recv(peer, rbufp);
1682 if (rval == XEVNT_OK) {
1685 if (rval == XEVNT_ERR) {
1686 report_event(PEVNT_RESTART, peer,
1688 peer_clear(peer, "CRYP");
1689 peer->flash |= TEST9; /* bad crypt */
1690 if (peer->flags & FLAG_PREEMPT) {
1691 if (unpeer_crypto_early) {
1700 * If server mode, verify the receive key ID matches
1701 * the transmit key ID.
1703 if (hismode == MODE_SERVER) {
1704 if (skeyid == peer->keyid)
1705 peer->flash &= ~TEST8;
1708 * If an extension field is present, verify only that it
1709 * has been correctly signed. We don't need a sequence
1710 * check here, but the sequence continues.
1712 } else if (!(peer->flash & TEST8)) {
1713 peer->pkeyid = skeyid;
1716 * Now the fun part. Here, skeyid is the current ID in
1717 * the packet, pkeyid is the ID in the last packet and
1718 * tkeyid is the hash of skeyid. If the autokey values
1719 * have not been received, this is an automatic error.
1720 * If so, check that the tkeyid matches pkeyid. If not,
1721 * hash tkeyid and try again. If the number of hashes
1722 * exceeds the number remaining in the sequence, declare
1723 * a successful failure and refresh the autokey values.
1725 } else if (ap != NULL) {
1728 for (i = 0; ; i++) {
1729 if ( tkeyid == peer->pkeyid
1730 || tkeyid == ap->key) {
1731 peer->flash &= ~TEST8;
1732 peer->pkeyid = skeyid;
1741 tkeyid = session_key(
1742 &rbufp->recv_srcadr, dstadr_sin,
1745 if (peer->flash & TEST8)
1746 report_event(PEVNT_AUTH, peer, "keylist");
1748 if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
1749 peer->flash |= TEST8; /* bad autokey */
1752 * The maximum lifetime of the protocol is about one
1753 * week before restarting the Autokey protocol to
1754 * refresh certificates and leapseconds values.
1756 if (current_time > peer->refresh) {
1757 report_event(PEVNT_RESTART, peer,
1759 peer_clear(peer, "TIME");
1763 #endif /* AUTOKEY */
1766 * The dance is complete and the flash bits have been lit. Toss
1767 * the packet over the fence for processing, which may light up
1770 process_packet(peer, pkt, rbufp->recv_length);
1773 * In interleaved mode update the state variables. Also adjust the
1774 * transmit phase to avoid crossover.
1776 if (peer->flip != 0) {
1778 peer->dst = rbufp->recv_time;
1779 if (peer->nextdate - current_time < (1U << min(peer->ppoll,
1789 * process_packet - Packet Procedure, a la Section 3.4.4 of the
1790 * specification. Or almost, at least. If we're in here we have a
1791 * reasonable expectation that we will be having a long term
1792 * relationship with this host.
1796 register struct peer *peer,
1797 register struct pkt *pkt,
1802 double p_offset, p_del, p_disp;
1803 l_fp p_rec, p_xmt, p_org, p_reftime, ci;
1804 u_char pmode, pleap, pversion, pstratum;
1805 char statstr[NTP_MAXSTRLEN];
1808 double etemp, ftemp, td;
1813 p_del = FPTOD(NTOHS_FP(pkt->rootdelay));
1815 p_disp = FPTOD(NTOHS_FP(pkt->rootdisp));
1816 NTOHL_FP(&pkt->reftime, &p_reftime);
1817 NTOHL_FP(&pkt->org, &p_org);
1818 NTOHL_FP(&pkt->rec, &p_rec);
1819 NTOHL_FP(&pkt->xmt, &p_xmt);
1820 pmode = PKT_MODE(pkt->li_vn_mode);
1821 pleap = PKT_LEAP(pkt->li_vn_mode);
1822 pversion = PKT_VERSION(pkt->li_vn_mode);
1823 pstratum = PKT_TO_STRATUM(pkt->stratum);
1826 * Capture the header values in the client/peer association..
1828 record_raw_stats(&peer->srcadr, peer->dstadr ?
1829 &peer->dstadr->sin : NULL,
1830 &p_org, &p_rec, &p_xmt, &peer->dst,
1831 pleap, pversion, pmode, pstratum, pkt->ppoll, pkt->precision,
1832 p_del, p_disp, pkt->refid);
1834 peer->stratum = min(pstratum, STRATUM_UNSPEC);
1835 peer->pmode = pmode;
1836 peer->precision = pkt->precision;
1837 peer->rootdelay = p_del;
1838 peer->rootdisp = p_disp;
1839 peer->refid = pkt->refid; /* network byte order */
1840 peer->reftime = p_reftime;
1843 * First, if either burst mode is armed, enable the burst.
1844 * Compute the headway for the next packet and delay if
1845 * necessary to avoid exceeding the threshold.
1847 if (peer->retry > 0) {
1850 peer->burst = min(1 << (peer->hpoll -
1851 peer->minpoll), NTP_SHIFT) - 1;
1853 peer->burst = NTP_IBURST - 1;
1854 if (peer->burst > 0)
1855 peer->nextdate = current_time;
1857 poll_update(peer, peer->hpoll);
1860 * Verify the server is synchronized; that is, the leap bits,
1861 * stratum and root distance are valid.
1863 if ( pleap == LEAP_NOTINSYNC /* test 6 */
1864 || pstratum < sys_floor || pstratum >= sys_ceiling)
1865 peer->flash |= TEST6; /* bad synch or strat */
1866 if (p_del / 2 + p_disp >= MAXDISPERSE) /* test 7 */
1867 peer->flash |= TEST7; /* bad header */
1870 * If any tests fail at this point, the packet is discarded.
1871 * Note that some flashers may have already been set in the
1872 * receive() routine.
1874 if (peer->flash & PKT_TEST_MASK) {
1875 peer->seldisptoolarge++;
1876 DPRINTF(1, ("packet: flash header %04x\n",
1882 * If the peer was previously unreachable, raise a trap. In any
1883 * case, mark it reachable.
1886 report_event(PEVNT_REACH, peer, NULL);
1887 peer->timereachable = current_time;
1892 * For a client/server association, calculate the clock offset,
1893 * roundtrip delay and dispersion. The equations are reordered
1894 * from the spec for more efficient use of temporaries. For a
1895 * broadcast association, offset the last measurement by the
1896 * computed delay during the client/server volley. Note the
1897 * computation of dispersion includes the system precision plus
1898 * that due to the frequency error since the origin time.
1900 * It is very important to respect the hazards of overflow. The
1901 * only permitted operation on raw timestamps is subtraction,
1902 * where the result is a signed quantity spanning from 68 years
1903 * in the past to 68 years in the future. To avoid loss of
1904 * precision, these calculations are done using 64-bit integer
1905 * arithmetic. However, the offset and delay calculations are
1906 * sums and differences of these first-order differences, which
1907 * if done using 64-bit integer arithmetic, would be valid over
1908 * only half that span. Since the typical first-order
1909 * differences are usually very small, they are converted to 64-
1910 * bit doubles and all remaining calculations done in floating-
1911 * double arithmetic. This preserves the accuracy while
1912 * retaining the 68-year span.
1914 * There are three interleaving schemes, basic, interleaved
1915 * symmetric and interleaved broadcast. The timestamps are
1916 * idioscyncratically different. See the onwire briefing/white
1917 * paper at www.eecis.udel.edu/~mills for details.
1919 * Interleaved symmetric mode
1920 * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
1923 if (peer->flip != 0) {
1924 ci = p_xmt; /* t3 - t4 */
1925 L_SUB(&ci, &peer->dst);
1927 ci = p_rec; /* t2 - t1 */
1929 L_SUB(&ci, &peer->borg);
1931 L_SUB(&ci, &peer->aorg);
1934 p_offset = (t21 + t34) / 2.;
1935 if (p_del < 0 || p_del > 1.) {
1936 snprintf(statstr, sizeof(statstr),
1937 "t21 %.6f t34 %.6f", t21, t34);
1938 report_event(PEVNT_XERR, peer, statstr);
1945 } else if (peer->pmode == MODE_BROADCAST) {
1948 * Interleaved broadcast mode. Use interleaved timestamps.
1949 * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
1951 if (peer->flags & FLAG_XB) {
1952 ci = p_org; /* delay */
1953 L_SUB(&ci, &peer->aorg);
1955 ci = p_org; /* t2 - t1 */
1956 L_SUB(&ci, &peer->borg);
1959 peer->borg = peer->dst;
1960 if (t34 < 0 || t34 > 1.) {
1961 snprintf(statstr, sizeof(statstr),
1962 "offset %.6f delay %.6f", t21, t34);
1963 report_event(PEVNT_XERR, peer, statstr);
1970 * Basic broadcast - use direct timestamps.
1971 * t3 = p_xmt, t4 = peer->dst
1974 ci = p_xmt; /* t3 - t4 */
1975 L_SUB(&ci, &peer->dst);
1981 * When calibration is complete and the clock is
1982 * synchronized, the bias is calculated as the difference
1983 * between the unicast timestamp and the broadcast
1984 * timestamp. This works for both basic and interleaved
1987 if (FLAG_BC_VOL & peer->flags) {
1988 peer->flags &= ~FLAG_BC_VOL;
1989 peer->delay = fabs(peer->offset - p_offset) * 2;
1991 p_del = peer->delay;
1992 p_offset += p_del / 2;
1996 * Basic mode, otherwise known as the old fashioned way.
1998 * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2001 ci = p_xmt; /* t3 - t4 */
2002 L_SUB(&ci, &peer->dst);
2004 ci = p_rec; /* t2 - t1 */
2007 p_del = fabs(t21 - t34);
2008 p_offset = (t21 + t34) / 2.;
2010 p_del = max(p_del, LOGTOD(sys_precision));
2011 p_disp = LOGTOD(sys_precision) + LOGTOD(peer->precision) +
2016 * This code calculates the outbound and inbound data rates by
2017 * measuring the differences between timestamps at different
2018 * packet lengths. This is helpful in cases of large asymmetric
2019 * delays commonly experienced on deep space communication
2022 if (peer->t21_last > 0 && peer->t34_bytes > 0) {
2023 itemp = peer->t21_bytes - peer->t21_last;
2025 etemp = t21 - peer->t21;
2026 if (fabs(etemp) > 1e-6) {
2027 ftemp = itemp / etemp;
2032 itemp = len - peer->t34_bytes;
2034 etemp = -t34 - peer->t34;
2035 if (fabs(etemp) > 1e-6) {
2036 ftemp = itemp / etemp;
2044 * The following section compensates for different data rates on
2045 * the outbound (d21) and inbound (t34) directions. To do this,
2046 * it finds t such that r21 * t - r34 * (d - t) = 0, where d is
2047 * the roundtrip delay. Then it calculates the correction as a
2051 peer->t21_last = peer->t21_bytes;
2053 peer->t34_bytes = len;
2054 DPRINTF(2, ("packet: t21 %.9lf %d t34 %.9lf %d\n", peer->t21,
2055 peer->t21_bytes, peer->t34, peer->t34_bytes));
2056 if (peer->r21 > 0 && peer->r34 > 0 && p_del > 0) {
2057 if (peer->pmode != MODE_BROADCAST)
2058 td = (peer->r34 / (peer->r21 + peer->r34) -
2064 * Unfortunately, in many cases the errors are
2065 * unacceptable, so for the present the rates are not
2066 * used. In future, we might find conditions where the
2067 * calculations are useful, so this should be considered
2068 * a work in progress.
2072 DPRINTF(2, ("packet: del %.6lf r21 %.1lf r34 %.1lf %.6lf\n",
2073 p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2079 * That was awesome. Now hand off to the clock filter.
2081 clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2084 * If we are in broadcast calibrate mode, return to broadcast
2085 * client mode when the client is fit and the autokey dance is
2088 if ( (FLAG_BC_VOL & peer->flags)
2089 && MODE_CLIENT == peer->hmode
2090 && !(TEST11 & peer_unfit(peer))) { /* distance exceeded */
2092 if (peer->flags & FLAG_SKEY) {
2093 if (!(~peer->crypto & CRYPTO_FLAG_ALL))
2094 peer->hmode = MODE_BCLIENT;
2096 peer->hmode = MODE_BCLIENT;
2098 #else /* !AUTOKEY follows */
2099 peer->hmode = MODE_BCLIENT;
2100 #endif /* !AUTOKEY */
2106 * clock_update - Called at system process update intervals.
2110 struct peer *peer /* peer structure pointer */
2115 #ifdef HAVE_LIBSCF_H
2117 #endif /* HAVE_LIBSCF_H */
2120 * Update the system state variables. We do this very carefully,
2121 * as the poll interval might need to be clamped differently.
2124 sys_epoch = peer->epoch;
2125 if (sys_poll < peer->minpoll)
2126 sys_poll = peer->minpoll;
2127 if (sys_poll > peer->maxpoll)
2128 sys_poll = peer->maxpoll;
2129 poll_update(peer, sys_poll);
2130 sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
2131 if ( peer->stratum == STRATUM_REFCLOCK
2132 || peer->stratum == STRATUM_UNSPEC)
2133 sys_refid = peer->refid;
2135 sys_refid = addr2refid(&peer->srcadr);
2137 * Root Dispersion (E) is defined (in RFC 5905) as:
2139 * E = p.epsilon_r + p.epsilon + p.psi + PHI*(s.t - p.t) + |THETA|
2142 * p.epsilon_r is the PollProc's root dispersion
2143 * p.epsilon is the PollProc's dispersion
2144 * p.psi is the PollProc's jitter
2145 * THETA is the combined offset
2147 * NB: Think Hard about where these numbers come from and
2148 * what they mean. When did peer->update happen? Has anything
2149 * interesting happened since then? What values are the most
2152 * DLM thinks this equation is probably the best of all worse choices.
2154 dtemp = peer->rootdisp
2157 + clock_phi * (current_time - peer->update)
2160 if (dtemp > sys_mindisp)
2161 sys_rootdisp = dtemp;
2163 sys_rootdisp = sys_mindisp;
2164 sys_rootdelay = peer->delay + peer->rootdelay;
2165 sys_reftime = peer->dst;
2167 DPRINTF(1, ("clock_update: at %lu sample %lu associd %d\n",
2168 current_time, peer->epoch, peer->associd));
2171 * Comes now the moment of truth. Crank the clock discipline and
2172 * see what comes out.
2174 switch (local_clock(peer, sys_offset)) {
2177 * Clock exceeds panic threshold. Life as we know it ends.
2180 #ifdef HAVE_LIBSCF_H
2182 * For Solaris enter the maintenance mode.
2184 if ((fmri = getenv("SMF_FMRI")) != NULL) {
2185 if (smf_maintain_instance(fmri, 0) < 0) {
2186 printf("smf_maintain_instance: %s\n",
2187 scf_strerror(scf_error()));
2191 * Sleep until SMF kills us.
2196 #endif /* HAVE_LIBSCF_H */
2201 * Clock was stepped. Flush all time values of all peers.
2205 set_sys_leap(LEAP_NOTINSYNC);
2206 sys_stratum = STRATUM_UNSPEC;
2207 memcpy(&sys_refid, "STEP", 4);
2210 L_CLR(&sys_reftime);
2211 sys_jitter = LOGTOD(sys_precision);
2212 leapsec_reset_frame();
2216 * Clock was slewed. Handle the leapsecond stuff.
2221 * If this is the first time the clock is set, reset the
2222 * leap bits. If crypto, the timer will goose the setup
2225 if (sys_leap == LEAP_NOTINSYNC) {
2226 set_sys_leap(LEAP_NOWARNING);
2230 #endif /* AUTOKEY */
2232 * If our parent process is waiting for the
2233 * first clock sync, send them home satisfied.
2235 #ifdef HAVE_WORKING_FORK
2236 if (waitsync_fd_to_close != -1) {
2237 close(waitsync_fd_to_close);
2238 waitsync_fd_to_close = -1;
2239 DPRINTF(1, ("notified parent --wait-sync is done\n"));
2241 #endif /* HAVE_WORKING_FORK */
2246 * If there is no leap second pending and the number of
2247 * survivor leap bits is greater than half the number of
2248 * survivors, try to schedule a leap for the end of the
2249 * current month. (This only works if no leap second for
2250 * that range is in the table, so doing this more than
2251 * once is mostly harmless.)
2253 if (leapsec == LSPROX_NOWARN) {
2254 if ( leap_vote_ins > leap_vote_del
2255 && leap_vote_ins > sys_survivors / 2) {
2257 leapsec_add_dyn(TRUE, now.l_ui, NULL);
2259 if ( leap_vote_del > leap_vote_ins
2260 && leap_vote_del > sys_survivors / 2) {
2262 leapsec_add_dyn(FALSE, now.l_ui, NULL);
2268 * Popcorn spike or step threshold exceeded. Pretend it never
2278 * poll_update - update peer poll interval
2282 struct peer *peer, /* peer structure pointer */
2290 * This routine figures out when the next poll should be sent.
2291 * That turns out to be wickedly complicated. One problem is
2292 * that sometimes the time for the next poll is in the past when
2293 * the poll interval is reduced. We watch out for races here
2294 * between the receive process and the poll process.
2296 * Clamp the poll interval between minpoll and maxpoll.
2298 hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
2302 * If during the crypto protocol the poll interval has changed,
2303 * the lifetimes in the key list are probably bogus. Purge the
2304 * the key list and regenerate it later.
2306 if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
2308 #endif /* AUTOKEY */
2309 peer->hpoll = hpoll;
2312 * There are three variables important for poll scheduling, the
2313 * current time (current_time), next scheduled time (nextdate)
2314 * and the earliest time (utemp). The earliest time is 2 s
2315 * seconds, but could be more due to rate management. When
2316 * sending in a burst, use the earliest time. When not in a
2317 * burst but with a reply pending, send at the earliest time
2318 * unless the next scheduled time has not advanced. This can
2319 * only happen if multiple replies are pending in the same
2320 * response interval. Otherwise, send at the later of the next
2321 * scheduled time and the earliest time.
2323 * Now we figure out if there is an override. If a burst is in
2324 * progress and we get called from the receive process, just
2325 * slink away. If called from the poll process, delay 1 s for a
2326 * reference clock, otherwise 2 s.
2328 utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
2329 (1 << peer->minpoll), ntp_minpkt);
2330 if (peer->burst > 0) {
2331 if (peer->nextdate > current_time)
2334 else if (peer->flags & FLAG_REFCLOCK)
2335 peer->nextdate = current_time + RESP_DELAY;
2336 #endif /* REFCLOCK */
2338 peer->nextdate = utemp;
2342 * If a burst is not in progress and a crypto response message
2343 * is pending, delay 2 s, but only if this is a new interval.
2345 } else if (peer->cmmd != NULL) {
2346 if (peer->nextdate > current_time) {
2347 if (peer->nextdate + ntp_minpkt != utemp)
2348 peer->nextdate = utemp;
2350 peer->nextdate = utemp;
2352 #endif /* AUTOKEY */
2355 * The ordinary case. If a retry, use minpoll; if unreachable,
2356 * use host poll; otherwise, use the minimum of host and peer
2357 * polls; In other words, oversampling is okay but
2358 * understampling is evil. Use the maximum of this value and the
2359 * headway. If the average headway is greater than the headway
2360 * threshold, increase the headway by the minimum interval.
2363 if (peer->retry > 0)
2364 hpoll = peer->minpoll;
2365 else if (!(peer->reach))
2366 hpoll = peer->hpoll;
2368 hpoll = min(peer->ppoll, peer->hpoll);
2370 if (peer->flags & FLAG_REFCLOCK)
2373 #endif /* REFCLOCK */
2374 next = ((0x1000UL | (ntp_random() & 0x0ff)) <<
2376 next += peer->outdate;
2378 peer->nextdate = next;
2380 peer->nextdate = utemp;
2381 if (peer->throttle > (1 << peer->minpoll))
2382 peer->nextdate += ntp_minpkt;
2384 DPRINTF(2, ("poll_update: at %lu %s poll %d burst %d retry %d head %d early %lu next %lu\n",
2385 current_time, ntoa(&peer->srcadr), peer->hpoll,
2386 peer->burst, peer->retry, peer->throttle,
2387 utemp - current_time, peer->nextdate -
2393 * peer_clear - clear peer filter registers. See Section 3.4.8 of the
2398 struct peer *peer, /* peer structure */
2399 const char *ident /* tally lights */
2406 * If cryptographic credentials have been acquired, toss them to
2407 * Valhalla. Note that autokeys are ephemeral, in that they are
2408 * tossed immediately upon use. Therefore, the keylist can be
2409 * purged anytime without needing to preserve random keys. Note
2410 * that, if the peer is purged, the cryptographic variables are
2411 * purged, too. This makes it much harder to sneak in some
2412 * unauthenticated data in the clock filter.
2415 if (peer->iffval != NULL)
2416 BN_free(peer->iffval);
2417 value_free(&peer->cookval);
2418 value_free(&peer->recval);
2419 value_free(&peer->encrypt);
2420 value_free(&peer->sndval);
2421 if (peer->cmmd != NULL)
2423 if (peer->subject != NULL)
2424 free(peer->subject);
2425 if (peer->issuer != NULL)
2427 #endif /* AUTOKEY */
2430 * Clear all values, including the optional crypto values above.
2432 memset(CLEAR_TO_ZERO(peer), 0, LEN_CLEAR_TO_ZERO(peer));
2433 peer->ppoll = peer->maxpoll;
2434 peer->hpoll = peer->minpoll;
2435 peer->disp = MAXDISPERSE;
2436 peer->flash = peer_unfit(peer);
2437 peer->jitter = LOGTOD(sys_precision);
2440 * If interleave mode, initialize the alternate origin switch.
2442 if (peer->flags & FLAG_XLEAVE)
2444 for (u = 0; u < NTP_SHIFT; u++) {
2445 peer->filter_order[u] = u;
2446 peer->filter_disp[u] = MAXDISPERSE;
2449 if (!(peer->flags & FLAG_REFCLOCK)) {
2451 peer->leap = LEAP_NOTINSYNC;
2452 peer->stratum = STRATUM_UNSPEC;
2453 memcpy(&peer->refid, ident, 4);
2459 * During initialization use the association count to spread out
2460 * the polls at one-second intervals. Passive associations'
2461 * first poll is delayed by the "discard minimum" to avoid rate
2462 * limiting. Other post-startup new or cleared associations
2463 * randomize the first poll over the minimum poll interval to
2466 peer->nextdate = peer->update = peer->outdate = current_time;
2468 peer->nextdate += peer_associations;
2469 } else if (MODE_PASSIVE == peer->hmode) {
2470 peer->nextdate += ntp_minpkt;
2472 peer->nextdate += ntp_random() % peer->minpoll;
2475 peer->refresh = current_time + (1 << NTP_REFRESH);
2476 #endif /* AUTOKEY */
2477 DPRINTF(1, ("peer_clear: at %ld next %ld associd %d refid %s\n",
2478 current_time, peer->nextdate, peer->associd,
2484 * clock_filter - add incoming clock sample to filter register and run
2485 * the filter procedure to find the best sample.
2489 struct peer *peer, /* peer structure pointer */
2490 double sample_offset, /* clock offset */
2491 double sample_delay, /* roundtrip delay */
2492 double sample_disp /* dispersion */
2495 double dst[NTP_SHIFT]; /* distance vector */
2496 int ord[NTP_SHIFT]; /* index vector */
2498 double dtemp, etemp;
2502 * A sample consists of the offset, delay, dispersion and epoch
2503 * of arrival. The offset and delay are determined by the on-
2504 * wire protocol. The dispersion grows from the last outbound
2505 * packet to the arrival of this one increased by the sum of the
2506 * peer precision and the system precision as required by the
2507 * error budget. First, shift the new arrival into the shift
2508 * register discarding the oldest one.
2510 j = peer->filter_nextpt;
2511 peer->filter_offset[j] = sample_offset;
2512 peer->filter_delay[j] = sample_delay;
2513 peer->filter_disp[j] = sample_disp;
2514 peer->filter_epoch[j] = current_time;
2515 j = (j + 1) % NTP_SHIFT;
2516 peer->filter_nextpt = j;
2519 * Update dispersions since the last update and at the same
2520 * time initialize the distance and index lists. Since samples
2521 * become increasingly uncorrelated beyond the Allan intercept,
2522 * only under exceptional cases will an older sample be used.
2523 * Therefore, the distance list uses a compound metric. If the
2524 * dispersion is greater than the maximum dispersion, clamp the
2525 * distance at that value. If the time since the last update is
2526 * less than the Allan intercept use the delay; otherwise, use
2527 * the sum of the delay and dispersion.
2529 dtemp = clock_phi * (current_time - peer->update);
2530 peer->update = current_time;
2531 for (i = NTP_SHIFT - 1; i >= 0; i--) {
2533 peer->filter_disp[j] += dtemp;
2534 if (peer->filter_disp[j] >= MAXDISPERSE) {
2535 peer->filter_disp[j] = MAXDISPERSE;
2536 dst[i] = MAXDISPERSE;
2537 } else if (peer->update - peer->filter_epoch[j] >
2538 (u_long)ULOGTOD(allan_xpt)) {
2539 dst[i] = peer->filter_delay[j] +
2540 peer->filter_disp[j];
2542 dst[i] = peer->filter_delay[j];
2545 j = (j + 1) % NTP_SHIFT;
2549 * If the clock has stabilized, sort the samples by distance.
2551 if (freq_cnt == 0) {
2552 for (i = 1; i < NTP_SHIFT; i++) {
2553 for (j = 0; j < i; j++) {
2554 if (dst[j] > dst[i]) {
2567 * Copy the index list to the association structure so ntpq
2568 * can see it later. Prune the distance list to leave only
2569 * samples less than the maximum dispersion, which disfavors
2570 * uncorrelated samples older than the Allan intercept. To
2571 * further improve the jitter estimate, of the remainder leave
2572 * only samples less than the maximum distance, but keep at
2573 * least two samples for jitter calculation.
2576 for (i = 0; i < NTP_SHIFT; i++) {
2577 peer->filter_order[i] = (u_char) ord[i];
2578 if ( dst[i] >= MAXDISPERSE
2579 || (m >= 2 && dst[i] >= sys_maxdist))
2585 * Compute the dispersion and jitter. The dispersion is weighted
2586 * exponentially by NTP_FWEIGHT (0.5) so it is normalized close
2587 * to 1.0. The jitter is the RMS differences relative to the
2588 * lowest delay sample.
2590 peer->disp = peer->jitter = 0;
2592 for (i = NTP_SHIFT - 1; i >= 0; i--) {
2594 peer->disp = NTP_FWEIGHT * (peer->disp +
2595 peer->filter_disp[j]);
2597 peer->jitter += DIFF(peer->filter_offset[j],
2598 peer->filter_offset[k]);
2602 * If no acceptable samples remain in the shift register,
2603 * quietly tiptoe home leaving only the dispersion. Otherwise,
2604 * save the offset, delay and jitter. Note the jitter must not
2605 * be less than the precision.
2611 etemp = fabs(peer->offset - peer->filter_offset[k]);
2612 peer->offset = peer->filter_offset[k];
2613 peer->delay = peer->filter_delay[k];
2615 peer->jitter /= m - 1;
2616 peer->jitter = max(SQRT(peer->jitter), LOGTOD(sys_precision));
2619 * If the the new sample and the current sample are both valid
2620 * and the difference between their offsets exceeds CLOCK_SGATE
2621 * (3) times the jitter and the interval between them is less
2622 * than twice the host poll interval, consider the new sample
2623 * a popcorn spike and ignore it.
2625 if ( peer->disp < sys_maxdist
2626 && peer->filter_disp[k] < sys_maxdist
2627 && etemp > CLOCK_SGATE * peer->jitter
2628 && peer->filter_epoch[k] - peer->epoch
2629 < 2. * ULOGTOD(peer->hpoll)) {
2630 snprintf(tbuf, sizeof(tbuf), "%.6f s", etemp);
2631 report_event(PEVNT_POPCORN, peer, tbuf);
2636 * A new minimum sample is useful only if it is later than the
2637 * last one used. In this design the maximum lifetime of any
2638 * sample is not greater than eight times the poll interval, so
2639 * the maximum interval between minimum samples is eight
2642 if (peer->filter_epoch[k] <= peer->epoch) {
2643 DPRINTF(2, ("clock_filter: old sample %lu\n", current_time -
2644 peer->filter_epoch[k]));
2647 peer->epoch = peer->filter_epoch[k];
2650 * The mitigated sample statistics are saved for later
2651 * processing. If not synchronized or not in a burst, tickle the
2652 * clock select algorithm.
2654 record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
2655 peer->offset, peer->delay, peer->disp, peer->jitter);
2656 DPRINTF(1, ("clock_filter: n %d off %.6f del %.6f dsp %.6f jit %.6f\n",
2657 m, peer->offset, peer->delay, peer->disp,
2659 if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC)
2665 * clock_select - find the pick-of-the-litter clock
2667 * LOCKCLOCK: (1) If the local clock is the prefer peer, it will always
2668 * be enabled, even if declared falseticker, (2) only the prefer peer
2669 * can be selected as the system peer, (3) if the external source is
2670 * down, the system leap bits are set to 11 and the stratum set to
2684 double orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
2685 struct endpoint endp;
2686 struct peer *osys_peer;
2687 struct peer *sys_prefer = NULL; /* prefer peer */
2688 struct peer *typesystem = NULL;
2689 struct peer *typeorphan = NULL;
2691 struct peer *typeacts = NULL;
2692 struct peer *typelocal = NULL;
2693 struct peer *typepps = NULL;
2694 #endif /* REFCLOCK */
2695 static struct endpoint *endpoint = NULL;
2696 static int *indx = NULL;
2697 static peer_select *peers = NULL;
2698 static u_int endpoint_size = 0;
2699 static u_int peers_size = 0;
2700 static u_int indx_size = 0;
2704 * Initialize and create endpoint, index and peer lists big
2705 * enough to handle all associations.
2707 osys_peer = sys_peer;
2710 set_sys_leap(LEAP_NOTINSYNC);
2711 sys_stratum = STRATUM_UNSPEC;
2712 memcpy(&sys_refid, "DOWN", 4);
2713 #endif /* LOCKCLOCK */
2716 * Allocate dynamic space depending on the number of
2720 for (peer = peer_list; peer != NULL; peer = peer->p_link)
2722 endpoint_size = ALIGNED_SIZE(nlist * 2 * sizeof(*endpoint));
2723 peers_size = ALIGNED_SIZE(nlist * sizeof(*peers));
2724 indx_size = ALIGNED_SIZE(nlist * 2 * sizeof(*indx));
2725 octets = endpoint_size + peers_size + indx_size;
2726 endpoint = erealloc(endpoint, octets);
2727 peers = INC_ALIGNED_PTR(endpoint, endpoint_size);
2728 indx = INC_ALIGNED_PTR(peers, peers_size);
2731 * Initially, we populate the island with all the rifraff peers
2732 * that happen to be lying around. Those with seriously
2733 * defective clocks are immediately booted off the island. Then,
2734 * the falsetickers are culled and put to sea. The truechimers
2735 * remaining are subject to repeated rounds where the most
2736 * unpopular at each round is kicked off. When the population
2737 * has dwindled to sys_minclock, the survivors split a million
2738 * bucks and collectively crank the chimes.
2740 nlist = nl2 = 0; /* none yet */
2741 for (peer = peer_list; peer != NULL; peer = peer->p_link) {
2742 peer->new_status = CTL_PST_SEL_REJECT;
2745 * Leave the island immediately if the peer is
2746 * unfit to synchronize.
2748 if (peer_unfit(peer))
2752 * If this peer is an orphan parent, elect the
2753 * one with the lowest metric defined as the
2754 * IPv4 address or the first 64 bits of the
2755 * hashed IPv6 address. To ensure convergence
2756 * on the same selected orphan, consider as
2757 * well that this system may have the lowest
2758 * metric and be the orphan parent. If this
2759 * system wins, sys_peer will be NULL to trigger
2760 * orphan mode in timer().
2762 if (peer->stratum == sys_orphan) {
2766 if (peer->dstadr != NULL)
2767 localmet = ntohl(peer->dstadr->addr_refid);
2769 localmet = U_INT32_MAX;
2770 peermet = ntohl(addr2refid(&peer->srcadr));
2771 if (peermet < localmet && peermet < orphmet) {
2779 * If this peer could have the orphan parent
2780 * as a synchronization ancestor, exclude it
2781 * from selection to avoid forming a
2782 * synchronization loop within the orphan mesh,
2783 * triggering stratum climb to infinity
2784 * instability. Peers at stratum higher than
2785 * the orphan stratum could have the orphan
2786 * parent in ancestry so are excluded.
2787 * See http://bugs.ntp.org/2050
2789 if (peer->stratum > sys_orphan)
2793 * The following are special cases. We deal
2796 if (!(peer->flags & FLAG_PREFER)) {
2797 switch (peer->refclktype) {
2798 case REFCLK_LOCALCLOCK:
2799 if ( current_time > orphwait
2800 && typelocal == NULL)
2805 if ( current_time > orphwait
2806 && typeacts == NULL)
2811 #endif /* REFCLOCK */
2814 * If we get this far, the peer can stay on the
2815 * island, but does not yet have the immunity
2818 peer->new_status = CTL_PST_SEL_SANE;
2819 f = root_distance(peer);
2820 peers[nlist].peer = peer;
2821 peers[nlist].error = peer->jitter;
2822 peers[nlist].synch = f;
2826 * Insert each interval endpoint on the unsorted
2830 endpoint[nl2].type = -1; /* lower end */
2831 endpoint[nl2].val = e - f;
2833 endpoint[nl2].type = 1; /* upper end */
2834 endpoint[nl2].val = e + f;
2838 * Construct sorted indx[] of endpoint[] indexes ordered by
2841 for (i = 0; i < nl2; i++)
2843 for (i = 0; i < nl2; i++) {
2844 endp = endpoint[indx[i]];
2847 for (j = i + 1; j < nl2; j++) {
2848 endp = endpoint[indx[j]];
2860 for (i = 0; i < nl2; i++)
2861 DPRINTF(3, ("select: endpoint %2d %.6f\n",
2862 endpoint[indx[i]].type, endpoint[indx[i]].val));
2865 * This is the actual algorithm that cleaves the truechimers
2866 * from the falsetickers. The original algorithm was described
2867 * in Keith Marzullo's dissertation, but has been modified for
2870 * Briefly put, we first assume there are no falsetickers, then
2871 * scan the candidate list first from the low end upwards and
2872 * then from the high end downwards. The scans stop when the
2873 * number of intersections equals the number of candidates less
2874 * the number of falsetickers. If this doesn't happen for a
2875 * given number of falsetickers, we bump the number of
2876 * falsetickers and try again. If the number of falsetickers
2877 * becomes equal to or greater than half the number of
2878 * candidates, the Albanians have won the Byzantine wars and
2879 * correct synchronization is not possible.
2881 * Here, nlist is the number of candidates and allow is the
2882 * number of falsetickers. Upon exit, the truechimers are the
2883 * survivors with offsets not less than low and not greater than
2884 * high. There may be none of them.
2888 for (allow = 0; 2 * allow < nlist; allow++) {
2891 * Bound the interval (low, high) as the smallest
2892 * interval containing points from the most sources.
2895 for (i = 0; i < nl2; i++) {
2896 low = endpoint[indx[i]].val;
2897 n -= endpoint[indx[i]].type;
2898 if (n >= nlist - allow)
2902 for (j = nl2 - 1; j >= 0; j--) {
2903 high = endpoint[indx[j]].val;
2904 n += endpoint[indx[j]].type;
2905 if (n >= nlist - allow)
2910 * If an interval containing truechimers is found, stop.
2911 * If not, increase the number of falsetickers and go
2919 * Clustering algorithm. Whittle candidate list of falsetickers,
2920 * who leave the island immediately. The TRUE peer is always a
2921 * truechimer. We must leave at least one peer to collect the
2924 * We assert the correct time is contained in the interval, but
2925 * the best offset estimate for the interval might not be
2926 * contained in the interval. For this purpose, a truechimer is
2927 * defined as the midpoint of an interval that overlaps the
2928 * intersection interval.
2931 for (i = 0; i < nlist; i++) {
2934 peer = peers[i].peer;
2937 || peer->offset + h < low
2938 || peer->offset - h > high
2939 ) && !(peer->flags & FLAG_TRUE))
2944 * Eligible PPS peers must survive the intersection
2945 * algorithm. Use the first one found, but don't
2946 * include any of them in the cluster population.
2948 if (peer->flags & FLAG_PPS) {
2949 if (typepps == NULL)
2951 if (!(peer->flags & FLAG_TSTAMP_PPS))
2954 #endif /* REFCLOCK */
2957 peers[j] = peers[i];
2963 * If no survivors remain at this point, check if the modem
2964 * driver, local driver or orphan parent in that order. If so,
2965 * nominate the first one found as the only survivor.
2966 * Otherwise, give up and leave the island to the rats.
2970 peers[0].synch = sys_mindisp;
2972 if (typeacts != NULL) {
2973 peers[0].peer = typeacts;
2975 } else if (typelocal != NULL) {
2976 peers[0].peer = typelocal;
2979 #endif /* REFCLOCK */
2980 if (typeorphan != NULL) {
2981 peers[0].peer = typeorphan;
2987 * Mark the candidates at this point as truechimers.
2989 for (i = 0; i < nlist; i++) {
2990 peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
2991 DPRINTF(2, ("select: survivor %s %f\n",
2992 stoa(&peers[i].peer->srcadr), peers[i].synch));
2996 * Now, vote outliers off the island by select jitter weighted
2997 * by root distance. Continue voting as long as there are more
2998 * than sys_minclock survivors and the select jitter of the peer
2999 * with the worst metric is greater than the minimum peer
3000 * jitter. Stop if we are about to discard a TRUE or PREFER
3001 * peer, who of course have the immunity idol.
3008 for (i = 0; i < nlist; i++) {
3009 if (peers[i].error < d)
3011 peers[i].seljit = 0;
3014 for (j = 0; j < nlist; j++)
3015 f += DIFF(peers[j].peer->offset,
3016 peers[i].peer->offset);
3017 peers[i].seljit = SQRT(f / (nlist - 1));
3019 if (peers[i].seljit * peers[i].synch > e) {
3020 g = peers[i].seljit;
3021 e = peers[i].seljit * peers[i].synch;
3025 g = max(g, LOGTOD(sys_precision));
3026 if ( nlist <= max(1, sys_minclock)
3028 || ((FLAG_TRUE | FLAG_PREFER) & peers[k].peer->flags))
3031 DPRINTF(3, ("select: drop %s seljit %.6f jit %.6f\n",
3032 ntoa(&peers[k].peer->srcadr), g, d));
3033 if (nlist > sys_maxclock)
3034 peers[k].peer->new_status = CTL_PST_SEL_EXCESS;
3035 for (j = k + 1; j < nlist; j++)
3036 peers[j - 1] = peers[j];
3041 * What remains is a list usually not greater than sys_minclock
3042 * peers. Note that unsynchronized peers cannot survive this
3043 * far. Count and mark these survivors.
3045 * While at it, count the number of leap warning bits found.
3046 * This will be used later to vote the system leap warning bit.
3047 * If a leap warning bit is found on a reference clock, the vote
3050 * Choose the system peer using a hybrid metric composed of the
3051 * selection jitter scaled by the root distance augmented by
3052 * stratum scaled by sys_mindisp (.001 by default). The goal of
3053 * the small stratum factor is to avoid clockhop between a
3054 * reference clock and a network peer which has a refclock and
3055 * is using an older ntpd, which does not floor sys_rootdisp at
3058 * In contrast, ntpd 4.2.6 and earlier used stratum primarily
3059 * in selecting the system peer, using a weight of 1 second of
3060 * additional root distance per stratum. This heavy bias is no
3061 * longer appropriate, as the scaled root distance provides a
3062 * more rational metric carrying the cumulative error budget.
3068 for (i = 0; i < nlist; i++) {
3069 peer = peers[i].peer;
3071 peer->new_status = CTL_PST_SEL_SYNCCAND;
3073 if (peer->leap == LEAP_ADDSECOND) {
3074 if (peer->flags & FLAG_REFCLOCK)
3075 leap_vote_ins = nlist;
3076 else if (leap_vote_ins < nlist)
3079 if (peer->leap == LEAP_DELSECOND) {
3080 if (peer->flags & FLAG_REFCLOCK)
3081 leap_vote_del = nlist;
3082 else if (leap_vote_del < nlist)
3085 if (peer->flags & FLAG_PREFER)
3087 speermet = peers[i].seljit * peers[i].synch +
3088 peer->stratum * sys_mindisp;
3096 * Unless there are at least sys_misane survivors, leave the
3097 * building dark. Otherwise, do a clockhop dance. Ordinarily,
3098 * use the selected survivor speer. However, if the current
3099 * system peer is not speer, stay with the current system peer
3100 * as long as it doesn't get too old or too ugly.
3102 if (nlist > 0 && nlist >= sys_minsane) {
3105 typesystem = peers[speer].peer;
3106 if (osys_peer == NULL || osys_peer == typesystem) {
3108 } else if ((x = fabs(typesystem->offset -
3109 osys_peer->offset)) < sys_mindisp) {
3110 if (sys_clockhop == 0)
3111 sys_clockhop = sys_mindisp;
3114 DPRINTF(1, ("select: clockhop %d %.6f %.6f\n",
3115 j, x, sys_clockhop));
3116 if (fabs(x) < sys_clockhop)
3117 typesystem = osys_peer;
3126 * Mitigation rules of the game. We have the pick of the
3127 * litter in typesystem if any survivors are left. If
3128 * there is a prefer peer, use its offset and jitter.
3129 * Otherwise, use the combined offset and jitter of all kitters.
3131 if (typesystem != NULL) {
3132 if (sys_prefer == NULL) {
3133 typesystem->new_status = CTL_PST_SEL_SYSPEER;
3134 clock_combine(peers, sys_survivors, speer);
3136 typesystem = sys_prefer;
3138 typesystem->new_status = CTL_PST_SEL_SYSPEER;
3139 sys_offset = typesystem->offset;
3140 sys_jitter = typesystem->jitter;
3142 DPRINTF(1, ("select: combine offset %.9f jitter %.9f\n",
3143 sys_offset, sys_jitter));
3147 * If a PPS driver is lit and the combined offset is less than
3148 * 0.4 s, select the driver as the PPS peer and use its offset
3149 * and jitter. However, if this is the atom driver, use it only
3150 * if there is a prefer peer or there are no survivors and none
3153 if ( typepps != NULL
3154 && fabs(sys_offset) < 0.4
3155 && ( typepps->refclktype != REFCLK_ATOM_PPS
3156 || ( typepps->refclktype == REFCLK_ATOM_PPS
3157 && ( sys_prefer != NULL
3158 || (typesystem == NULL && sys_minsane == 0))))) {
3159 typesystem = typepps;
3161 typesystem->new_status = CTL_PST_SEL_PPS;
3162 sys_offset = typesystem->offset;
3163 sys_jitter = typesystem->jitter;
3164 DPRINTF(1, ("select: pps offset %.9f jitter %.9f\n",
3165 sys_offset, sys_jitter));
3167 #endif /* REFCLOCK */
3170 * If there are no survivors at this point, there is no
3171 * system peer. If so and this is an old update, keep the
3172 * current statistics, but do not update the clock.
3174 if (typesystem == NULL) {
3175 if (osys_peer != NULL) {
3176 if (sys_orphwait > 0)
3177 orphwait = current_time + sys_orphwait;
3178 report_event(EVNT_NOPEER, NULL, NULL);
3181 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3182 peer->status = peer->new_status;
3187 * Do not use old data, as this may mess up the clock discipline
3190 if (typesystem->epoch <= sys_epoch)
3194 * We have found the alpha male. Wind the clock.
3196 if (osys_peer != typesystem)
3197 report_event(PEVNT_NEWPEER, typesystem, NULL);
3198 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3199 peer->status = peer->new_status;
3200 clock_update(typesystem);
3206 peer_select * peers, /* survivor list */
3207 int npeers, /* number of survivors */
3208 int syspeer /* index of sys.peer */
3215 for (i = 0; i < npeers; i++) {
3216 x = 1. / peers[i].synch;
3218 z += x * peers[i].peer->offset;
3219 w += x * DIFF(peers[i].peer->offset,
3220 peers[syspeer].peer->offset);
3223 sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
3228 * root_distance - compute synchronization distance from peer to root
3232 struct peer *peer /* peer structure pointer */
3238 * Root Distance (LAMBDA) is defined as:
3239 * (delta + DELTA)/2 + epsilon + EPSILON + phi
3242 * delta is the round-trip delay
3243 * DELTA is the root delay
3244 * epsilon is the remote server precision + local precision
3245 * + (15 usec each second)
3246 * EPSILON is the root dispersion
3247 * phi is the peer jitter statistic
3249 * NB: Think hard about why we are using these values, and what
3250 * the alternatives are, and the various pros/cons.
3252 * DLM thinks these are probably the best choices from any of the
3253 * other worse choices.
3255 dtemp = (peer->delay + peer->rootdelay) / 2
3256 + LOGTOD(peer->precision)
3257 + LOGTOD(sys_precision)
3258 + clock_phi * (current_time - peer->update)
3262 * Careful squeak here. The value returned must be greater than
3263 * the minimum root dispersion in order to avoid clockhop with
3264 * highly precise reference clocks. Note that the root distance
3265 * cannot exceed the sys_maxdist, as this is the cutoff by the
3266 * selection algorithm.
3268 if (dtemp < sys_mindisp)
3269 dtemp = sys_mindisp;
3275 * peer_xmit - send packet for persistent association.
3279 struct peer *peer /* peer structure pointer */
3282 struct pkt xpkt; /* transmit packet */
3283 size_t sendlen, authlen;
3284 keyid_t xkeyid = 0; /* transmit key ID */
3285 l_fp xmt_tx, xmt_ty;
3287 if (!peer->dstadr) /* drop peers without interface */
3290 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, peer->version,
3292 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
3293 xpkt.ppoll = peer->hpoll;
3294 xpkt.precision = sys_precision;
3295 xpkt.refid = sys_refid;
3296 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
3297 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
3298 HTONL_FP(&sys_reftime, &xpkt.reftime);
3299 HTONL_FP(&peer->rec, &xpkt.org);
3300 HTONL_FP(&peer->dst, &xpkt.rec);
3303 * If the received packet contains a MAC, the transmitted packet
3304 * is authenticated and contains a MAC. If not, the transmitted
3305 * packet is not authenticated.
3307 * It is most important when autokey is in use that the local
3308 * interface IP address be known before the first packet is
3309 * sent. Otherwise, it is not possible to compute a correct MAC
3310 * the recipient will accept. Thus, the I/O semantics have to do
3311 * a little more work. In particular, the wildcard interface
3312 * might not be usable.
3314 sendlen = LEN_PKT_NOMAC;
3317 !(peer->flags & FLAG_SKEY) &&
3318 #endif /* !AUTOKEY */
3322 * Transmit a-priori timestamps
3324 get_systime(&xmt_tx);
3325 if (peer->flip == 0) { /* basic mode */
3326 peer->aorg = xmt_tx;
3327 HTONL_FP(&xmt_tx, &xpkt.xmt);
3328 } else { /* interleaved modes */
3329 if (peer->hmode == MODE_BROADCAST) { /* bcst */
3330 HTONL_FP(&xmt_tx, &xpkt.xmt);
3332 HTONL_FP(&peer->borg,
3335 HTONL_FP(&peer->aorg,
3337 } else { /* symmetric */
3339 HTONL_FP(&peer->borg,
3342 HTONL_FP(&peer->aorg,
3346 peer->t21_bytes = sendlen;
3347 sendpkt(&peer->srcadr, peer->dstadr, sys_ttl[peer->ttl],
3350 peer->throttle += (1 << peer->minpoll) - 2;
3353 * Capture a-posteriori timestamps
3355 get_systime(&xmt_ty);
3356 if (peer->flip != 0) { /* interleaved modes */
3358 peer->aorg = xmt_ty;
3360 peer->borg = xmt_ty;
3361 peer->flip = -peer->flip;
3363 L_SUB(&xmt_ty, &xmt_tx);
3364 LFPTOD(&xmt_ty, peer->xleave);
3365 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d len %zu xmt %#010x.%08x\n",
3367 peer->dstadr ? stoa(&peer->dstadr->sin) : "-",
3368 stoa(&peer->srcadr), peer->hmode, sendlen,
3369 xmt_tx.l_ui, xmt_tx.l_uf));
3374 * Authentication is enabled, so the transmitted packet must be
3375 * authenticated. If autokey is enabled, fuss with the various
3376 * modes; otherwise, symmetric key cryptography is used.
3379 if (peer->flags & FLAG_SKEY) {
3380 struct exten *exten; /* extension field */
3383 * The Public Key Dance (PKD): Cryptographic credentials
3384 * are contained in extension fields, each including a
3385 * 4-octet length/code word followed by a 4-octet
3386 * association ID and optional additional data. Optional
3387 * data includes a 4-octet data length field followed by
3388 * the data itself. Request messages are sent from a
3389 * configured association; response messages can be sent
3390 * from a configured association or can take the fast
3391 * path without ever matching an association. Response
3392 * messages have the same code as the request, but have
3393 * a response bit and possibly an error bit set. In this
3394 * implementation, a message may contain no more than
3395 * one command and one or more responses.
3397 * Cryptographic session keys include both a public and
3398 * a private componet. Request and response messages
3399 * using extension fields are always sent with the
3400 * private component set to zero. Packets without
3401 * extension fields indlude the private component when
3402 * the session key is generated.
3407 * Allocate and initialize a keylist if not
3408 * already done. Then, use the list in inverse
3409 * order, discarding keys once used. Keep the
3410 * latest key around until the next one, so
3411 * clients can use client/server packets to
3412 * compute propagation delay.
3414 * Note that once a key is used from the list,
3415 * it is retained in the key cache until the
3416 * next key is used. This is to allow a client
3417 * to retrieve the encrypted session key
3418 * identifier to verify authenticity.
3420 * If for some reason a key is no longer in the
3421 * key cache, a birthday has happened or the key
3422 * has expired, so the pseudo-random sequence is
3423 * broken. In that case, purge the keylist and
3426 if (peer->keynumber == 0)
3427 make_keylist(peer, peer->dstadr);
3430 xkeyid = peer->keylist[peer->keynumber];
3431 if (authistrusted(xkeyid))
3436 peer->keyid = xkeyid;
3438 switch (peer->hmode) {
3441 * In broadcast server mode the autokey values are
3442 * required by the broadcast clients. Push them when a
3443 * new keylist is generated; otherwise, push the
3444 * association message so the client can request them at
3447 case MODE_BROADCAST:
3448 if (peer->flags & FLAG_ASSOC)
3449 exten = crypto_args(peer, CRYPTO_AUTO |
3450 CRYPTO_RESP, peer->associd, NULL);
3452 exten = crypto_args(peer, CRYPTO_ASSOC |
3453 CRYPTO_RESP, peer->associd, NULL);
3457 * In symmetric modes the parameter, certificate,
3458 * identity, cookie and autokey exchanges are
3459 * required. The leapsecond exchange is optional. But, a
3460 * peer will not believe the other peer until the other
3461 * peer has synchronized, so the certificate exchange
3462 * might loop until then. If a peer finds a broken
3463 * autokey sequence, it uses the autokey exchange to
3464 * retrieve the autokey values. In any case, if a new
3465 * keylist is generated, the autokey values are pushed.
3471 * Parameter, certificate and identity.
3474 exten = crypto_args(peer, CRYPTO_ASSOC,
3475 peer->associd, hostval.ptr);
3476 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
3477 exten = crypto_args(peer, CRYPTO_CERT,
3478 peer->associd, peer->issuer);
3479 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
3480 exten = crypto_args(peer,
3481 crypto_ident(peer), peer->associd,
3485 * Cookie and autokey. We request the cookie
3486 * only when the this peer and the other peer
3487 * are synchronized. But, this peer needs the
3488 * autokey values when the cookie is zero. Any
3489 * time we regenerate the key list, we offer the
3490 * autokey values without being asked. If for
3491 * some reason either peer finds a broken
3492 * autokey sequence, the autokey exchange is
3493 * used to retrieve the autokey values.
3495 else if ( sys_leap != LEAP_NOTINSYNC
3496 && peer->leap != LEAP_NOTINSYNC
3497 && !(peer->crypto & CRYPTO_FLAG_COOK))
3498 exten = crypto_args(peer, CRYPTO_COOK,
3499 peer->associd, NULL);
3500 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
3501 exten = crypto_args(peer, CRYPTO_AUTO,
3502 peer->associd, NULL);
3503 else if ( peer->flags & FLAG_ASSOC
3504 && peer->crypto & CRYPTO_FLAG_SIGN)
3505 exten = crypto_args(peer, CRYPTO_AUTO |
3506 CRYPTO_RESP, peer->assoc, NULL);
3509 * Wait for clock sync, then sign the
3510 * certificate and retrieve the leapsecond
3513 else if (sys_leap == LEAP_NOTINSYNC)
3516 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
3517 exten = crypto_args(peer, CRYPTO_SIGN,
3518 peer->associd, hostval.ptr);
3519 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
3520 exten = crypto_args(peer, CRYPTO_LEAP,
3521 peer->associd, NULL);
3525 * In client mode the parameter, certificate, identity,
3526 * cookie and sign exchanges are required. The
3527 * leapsecond exchange is optional. If broadcast client
3528 * mode the same exchanges are required, except that the
3529 * autokey exchange is substitutes for the cookie
3530 * exchange, since the cookie is always zero. If the
3531 * broadcast client finds a broken autokey sequence, it
3532 * uses the autokey exchange to retrieve the autokey
3538 * Parameter, certificate and identity.
3541 exten = crypto_args(peer, CRYPTO_ASSOC,
3542 peer->associd, hostval.ptr);
3543 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
3544 exten = crypto_args(peer, CRYPTO_CERT,
3545 peer->associd, peer->issuer);
3546 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
3547 exten = crypto_args(peer,
3548 crypto_ident(peer), peer->associd,
3552 * Cookie and autokey. These are requests, but
3553 * we use the peer association ID with autokey
3554 * rather than our own.
3556 else if (!(peer->crypto & CRYPTO_FLAG_COOK))
3557 exten = crypto_args(peer, CRYPTO_COOK,
3558 peer->associd, NULL);
3559 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
3560 exten = crypto_args(peer, CRYPTO_AUTO,
3564 * Wait for clock sync, then sign the
3565 * certificate and retrieve the leapsecond
3568 else if (sys_leap == LEAP_NOTINSYNC)
3571 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
3572 exten = crypto_args(peer, CRYPTO_SIGN,
3573 peer->associd, hostval.ptr);
3574 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
3575 exten = crypto_args(peer, CRYPTO_LEAP,
3576 peer->associd, NULL);
3581 * Add a queued extension field if present. This is
3582 * always a request message, so the reply ID is already
3583 * in the message. If an error occurs, the error bit is
3584 * lit in the response.
3586 if (peer->cmmd != NULL) {
3589 temp32 = CRYPTO_RESP;
3590 peer->cmmd->opcode |= htonl(temp32);
3591 sendlen += crypto_xmit(peer, &xpkt, NULL,
3592 sendlen, peer->cmmd, 0);
3598 * Add an extension field created above. All but the
3599 * autokey response message are request messages.
3601 if (exten != NULL) {
3602 if (exten->opcode != 0)
3603 sendlen += crypto_xmit(peer, &xpkt,
3604 NULL, sendlen, exten, 0);
3609 * Calculate the next session key. Since extension
3610 * fields are present, the cookie value is zero.
3612 if (sendlen > (int)LEN_PKT_NOMAC) {
3613 session_key(&peer->dstadr->sin, &peer->srcadr,
3617 #endif /* AUTOKEY */
3620 * Transmit a-priori timestamps
3622 get_systime(&xmt_tx);
3623 if (peer->flip == 0) { /* basic mode */
3624 peer->aorg = xmt_tx;
3625 HTONL_FP(&xmt_tx, &xpkt.xmt);
3626 } else { /* interleaved modes */
3627 if (peer->hmode == MODE_BROADCAST) { /* bcst */
3628 HTONL_FP(&xmt_tx, &xpkt.xmt);
3630 HTONL_FP(&peer->borg, &xpkt.org);
3632 HTONL_FP(&peer->aorg, &xpkt.org);
3633 } else { /* symmetric */
3635 HTONL_FP(&peer->borg, &xpkt.xmt);
3637 HTONL_FP(&peer->aorg, &xpkt.xmt);
3640 xkeyid = peer->keyid;
3641 authlen = authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
3643 report_event(PEVNT_AUTH, peer, "no key");
3644 peer->flash |= TEST5; /* auth error */
3650 if (xkeyid > NTP_MAXKEY)
3651 authtrust(xkeyid, 0);
3652 #endif /* AUTOKEY */
3653 if (sendlen > sizeof(xpkt)) {
3654 msyslog(LOG_ERR, "peer_xmit: buffer overflow %zu", sendlen);
3657 peer->t21_bytes = sendlen;
3658 sendpkt(&peer->srcadr, peer->dstadr, sys_ttl[peer->ttl], &xpkt,
3661 peer->throttle += (1 << peer->minpoll) - 2;
3664 * Capture a-posteriori timestamps
3666 get_systime(&xmt_ty);
3667 if (peer->flip != 0) { /* interleaved modes */
3669 peer->aorg = xmt_ty;
3671 peer->borg = xmt_ty;
3672 peer->flip = -peer->flip;
3674 L_SUB(&xmt_ty, &xmt_tx);
3675 LFPTOD(&xmt_ty, peer->xleave);
3677 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu index %d\n",
3678 current_time, latoa(peer->dstadr),
3679 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen,
3681 #else /* !AUTOKEY follows */
3682 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %d\n",
3683 current_time, peer->dstadr ?
3684 ntoa(&peer->dstadr->sin) : "-",
3685 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
3686 #endif /* !AUTOKEY */
3695 leap_smear_add_offs(
3701 L_ADD(t, &leap_smear.offset);
3706 #endif /* LEAP_SMEAR */
3710 * fast_xmit - Send packet for nonpersistent association. Note that
3711 * neither the source or destination can be a broadcast address.
3715 struct recvbuf *rbufp, /* receive packet pointer */
3716 int xmode, /* receive mode */
3717 keyid_t xkeyid, /* transmit key ID */
3718 int flags /* restrict mask */
3721 struct pkt xpkt; /* transmit packet structure */
3722 struct pkt *rpkt; /* receive packet structure */
3723 l_fp xmt_tx, xmt_ty;
3730 * Initialize transmit packet header fields from the receive
3731 * buffer provided. We leave the fields intact as received, but
3732 * set the peer poll at the maximum of the receive peer poll and
3733 * the system minimum poll (ntp_minpoll). This is for KoD rate
3734 * control and not strictly specification compliant, but doesn't
3737 * If the gazinta was from a multicast address, the gazoutta
3738 * must go out another way.
3740 rpkt = &rbufp->recv_pkt;
3741 if (rbufp->dstadr->flags & INT_MCASTOPEN)
3742 rbufp->dstadr = findinterface(&rbufp->recv_srcadr);
3745 * If this is a kiss-o'-death (KoD) packet, show leap
3746 * unsynchronized, stratum zero, reference ID the four-character
3747 * kiss code and system root delay. Note we don't reveal the
3748 * local time, so these packets can't be used for
3751 if (flags & RES_KOD) {
3753 xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
3754 PKT_VERSION(rpkt->li_vn_mode), xmode);
3755 xpkt.stratum = STRATUM_PKT_UNSPEC;
3756 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
3757 xpkt.precision = rpkt->precision;
3758 memcpy(&xpkt.refid, "RATE", 4);
3759 xpkt.rootdelay = rpkt->rootdelay;
3760 xpkt.rootdisp = rpkt->rootdisp;
3761 xpkt.reftime = rpkt->reftime;
3762 xpkt.org = rpkt->xmt;
3763 xpkt.rec = rpkt->xmt;
3764 xpkt.xmt = rpkt->xmt;
3767 * This is a normal packet. Use the system variables.
3772 * Make copies of the variables which can be affected by smearing.
3775 l_fp this_recv_time;
3779 * If we are inside the leap smear interval we add the current smear offset to
3780 * the packet receive time, to the packet transmit time, and eventually to the
3781 * reftime to make sure the reftime isn't later than the transmit/receive times.
3783 xpkt.li_vn_mode = PKT_LI_VN_MODE(xmt_leap,
3784 PKT_VERSION(rpkt->li_vn_mode), xmode);
3786 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
3787 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
3788 xpkt.precision = sys_precision;
3789 xpkt.refid = sys_refid;
3790 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
3791 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
3794 this_ref_time = sys_reftime;
3795 if (leap_smear.in_progress) {
3796 leap_smear_add_offs(&this_ref_time, NULL);
3797 xpkt.refid = convertLFPToRefID(leap_smear.offset);
3798 DPRINTF(2, ("fast_xmit: leap_smear.in_progress: refid %8x, smear %s\n",
3800 lfptoa(&leap_smear.offset, 8)
3803 HTONL_FP(&this_ref_time, &xpkt.reftime);
3805 HTONL_FP(&sys_reftime, &xpkt.reftime);
3808 xpkt.org = rpkt->xmt;
3811 this_recv_time = rbufp->recv_time;
3812 if (leap_smear.in_progress)
3813 leap_smear_add_offs(&this_recv_time, NULL);
3814 HTONL_FP(&this_recv_time, &xpkt.rec);
3816 HTONL_FP(&rbufp->recv_time, &xpkt.rec);
3819 get_systime(&xmt_tx);
3821 if (leap_smear.in_progress)
3822 leap_smear_add_offs(&xmt_tx, &this_recv_time);
3824 HTONL_FP(&xmt_tx, &xpkt.xmt);
3827 #ifdef HAVE_NTP_SIGND
3828 if (flags & RES_MSSNTP) {
3829 send_via_ntp_signd(rbufp, xmode, xkeyid, flags, &xpkt);
3832 #endif /* HAVE_NTP_SIGND */
3835 * If the received packet contains a MAC, the transmitted packet
3836 * is authenticated and contains a MAC. If not, the transmitted
3837 * packet is not authenticated.
3839 sendlen = LEN_PKT_NOMAC;
3840 if (rbufp->recv_length == sendlen) {
3841 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt,
3843 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d len %lu\n",
3844 current_time, stoa(&rbufp->dstadr->sin),
3845 stoa(&rbufp->recv_srcadr), xmode,
3851 * The received packet contains a MAC, so the transmitted packet
3852 * must be authenticated. For symmetric key cryptography, use
3853 * the predefined and trusted symmetric keys to generate the
3854 * cryptosum. For autokey cryptography, use the server private
3855 * value to generate the cookie, which is unique for every
3856 * source-destination-key ID combination.
3859 if (xkeyid > NTP_MAXKEY) {
3863 * The only way to get here is a reply to a legitimate
3864 * client request message, so the mode must be
3865 * MODE_SERVER. If an extension field is present, there
3866 * can be only one and that must be a command. Do what
3867 * needs, but with private value of zero so the poor
3868 * jerk can decode it. If no extension field is present,
3869 * use the cookie to generate the session key.
3871 cookie = session_key(&rbufp->recv_srcadr,
3872 &rbufp->dstadr->sin, 0, sys_private, 0);
3873 if ((size_t)rbufp->recv_length > sendlen + MAX_MAC_LEN) {
3874 session_key(&rbufp->dstadr->sin,
3875 &rbufp->recv_srcadr, xkeyid, 0, 2);
3876 temp32 = CRYPTO_RESP;
3877 rpkt->exten[0] |= htonl(temp32);
3878 sendlen += crypto_xmit(NULL, &xpkt, rbufp,
3879 sendlen, (struct exten *)rpkt->exten,
3882 session_key(&rbufp->dstadr->sin,
3883 &rbufp->recv_srcadr, xkeyid, cookie, 2);
3886 #endif /* AUTOKEY */
3887 get_systime(&xmt_tx);
3888 sendlen += authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
3890 if (xkeyid > NTP_MAXKEY)
3891 authtrust(xkeyid, 0);
3892 #endif /* AUTOKEY */
3893 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt, sendlen);
3894 get_systime(&xmt_ty);
3895 L_SUB(&xmt_ty, &xmt_tx);
3896 sys_authdelay = xmt_ty;
3897 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d keyid %08x len %lu\n",
3898 current_time, ntoa(&rbufp->dstadr->sin),
3899 ntoa(&rbufp->recv_srcadr), xmode, xkeyid,
3905 * pool_xmit - resolve hostname or send unicast solicitation for pool.
3909 struct peer *pool /* pool solicitor association */
3913 struct pkt xpkt; /* transmit packet structure */
3914 struct addrinfo hints;
3916 struct interface * lcladr;
3917 sockaddr_u * rmtadr;
3922 if (NULL == pool->ai) {
3923 if (pool->addrs != NULL) {
3924 /* free() is used with copy_addrinfo_list() */
3929 hints.ai_family = AF(&pool->srcadr);
3930 hints.ai_socktype = SOCK_DGRAM;
3931 hints.ai_protocol = IPPROTO_UDP;
3932 /* ignore getaddrinfo_sometime() errors, we will retry */
3933 rc = getaddrinfo_sometime(
3938 &pool_name_resolved,
3939 (void *)(intptr_t)pool->associd);
3941 DPRINTF(1, ("pool DNS lookup %s started\n",
3945 "unable to start pool DNS %s: %m",
3951 /* copy_addrinfo_list ai_addr points to a sockaddr_u */
3952 rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
3953 pool->ai = pool->ai->ai_next;
3954 p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT, 0);
3955 } while (p != NULL && pool->ai != NULL);
3957 return; /* out of addresses, re-query DNS next poll */
3958 restrict_mask = restrictions(rmtadr);
3959 if (RES_FLAGS & restrict_mask)
3960 restrict_source(rmtadr, 0,
3961 current_time + POOL_SOLICIT_WINDOW + 1);
3962 lcladr = findinterface(rmtadr);
3963 memset(&xpkt, 0, sizeof(xpkt));
3964 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, pool->version,
3966 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
3967 xpkt.ppoll = pool->hpoll;
3968 xpkt.precision = sys_precision;
3969 xpkt.refid = sys_refid;
3970 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
3971 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
3972 HTONL_FP(&sys_reftime, &xpkt.reftime);
3973 get_systime(&xmt_tx);
3974 pool->aorg = xmt_tx;
3975 HTONL_FP(&xmt_tx, &xpkt.xmt);
3976 sendpkt(rmtadr, lcladr, sys_ttl[pool->ttl], &xpkt,
3979 pool->throttle += (1 << pool->minpoll) - 2;
3980 DPRINTF(1, ("pool_xmit: at %ld %s->%s pool\n",
3981 current_time, latoa(lcladr), stoa(rmtadr)));
3982 msyslog(LOG_INFO, "Soliciting pool server %s", stoa(rmtadr));
3989 * group_test - test if this is the same group
3991 * host assoc return action
3992 * none none 0 mobilize *
3993 * none group 0 mobilize *
3994 * group none 0 mobilize *
3995 * group group 1 mobilize
3996 * group different 1 ignore
3997 * * ignore if notrust
4008 if (strcmp(grp, sys_groupname) == 0)
4014 if (strcmp(grp, ident) == 0)
4019 #endif /* AUTOKEY */
4028 const char * service,
4029 const struct addrinfo * hints,
4030 const struct addrinfo * res
4033 struct peer * pool; /* pool solicitor association */
4038 "error resolving pool %s: %s (%d)",
4039 name, gai_strerror(rescode), rescode);
4043 assoc = (associd_t)(intptr_t)context;
4044 pool = findpeerbyassoc(assoc);
4047 "Could not find assoc %u for pool DNS %s",
4051 DPRINTF(1, ("pool DNS %s completed\n", name));
4052 pool->addrs = copy_addrinfo_list(res);
4053 pool->ai = pool->addrs;
4062 * key_expire - purge the key list
4066 struct peer *peer /* peer structure pointer */
4071 if (peer->keylist != NULL) {
4072 for (i = 0; i <= peer->keynumber; i++)
4073 authtrust(peer->keylist[i], 0);
4074 free(peer->keylist);
4075 peer->keylist = NULL;
4077 value_free(&peer->sndval);
4078 peer->keynumber = 0;
4079 peer->flags &= ~FLAG_ASSOC;
4080 DPRINTF(1, ("key_expire: at %lu associd %d\n", current_time,
4083 #endif /* AUTOKEY */
4087 * local_refid(peer) - check peer refid to avoid selecting peers
4088 * currently synced to this ntpd.
4097 if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
4098 unicast_ep = p->dstadr;
4100 unicast_ep = findinterface(&p->srcadr);
4102 if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
4110 * Determine if the peer is unfit for synchronization
4112 * A peer is unfit for synchronization if
4113 * > TEST10 bad leap or stratum below floor or at or above ceiling
4114 * > TEST11 root distance exceeded for remote peer
4115 * > TEST12 a direct or indirect synchronization loop would form
4116 * > TEST13 unreachable or noselect
4118 int /* FALSE if fit, TRUE if unfit */
4120 struct peer *peer /* peer structure pointer */
4126 * A stratum error occurs if (1) the server has never been
4127 * synchronized, (2) the server stratum is below the floor or
4128 * greater than or equal to the ceiling.
4130 if ( peer->leap == LEAP_NOTINSYNC
4131 || peer->stratum < sys_floor
4132 || peer->stratum >= sys_ceiling)
4133 rval |= TEST10; /* bad synch or stratum */
4136 * A distance error for a remote peer occurs if the root
4137 * distance is greater than or equal to the distance threshold
4138 * plus the increment due to one host poll interval.
4140 if ( !(peer->flags & FLAG_REFCLOCK)
4141 && root_distance(peer) >= sys_maxdist
4142 + clock_phi * ULOGTOD(peer->hpoll))
4143 rval |= TEST11; /* distance exceeded */
4146 * A loop error occurs if the remote peer is synchronized to the
4147 * local peer or if the remote peer is synchronized to the same
4148 * server as the local peer but only if the remote peer is
4149 * neither a reference clock nor an orphan.
4151 if (peer->stratum > 1 && local_refid(peer))
4152 rval |= TEST12; /* synchronization loop */
4155 * An unreachable error occurs if the server is unreachable or
4156 * the noselect bit is set.
4158 if (!peer->reach || (peer->flags & FLAG_NOSELECT))
4159 rval |= TEST13; /* unreachable */
4161 peer->flash &= ~PEER_TEST_MASK;
4162 peer->flash |= rval;
4168 * Find the precision of this particular machine
4170 #define MINSTEP 20e-9 /* minimum clock increment (s) */
4171 #define MAXSTEP 1 /* maximum clock increment (s) */
4172 #define MINCHANGES 12 /* minimum number of step samples */
4173 #define MAXLOOPS ((int)(1. / MINSTEP)) /* avoid infinite loop */
4176 * This routine measures the system precision defined as the minimum of
4177 * a sequence of differences between successive readings of the system
4178 * clock. However, if a difference is less than MINSTEP, the clock has
4179 * been read more than once during a clock tick and the difference is
4180 * ignored. We set MINSTEP greater than zero in case something happens
4181 * like a cache miss, and to tolerate underlying system clocks which
4182 * ensure each reading is strictly greater than prior readings while
4183 * using an underlying stepping (not interpolated) clock.
4185 * sys_tick and sys_precision represent the time to read the clock for
4186 * systems with high-precision clocks, and the tick interval or step
4187 * size for lower-precision stepping clocks.
4189 * This routine also measures the time to read the clock on stepping
4190 * system clocks by counting the number of readings between changes of
4191 * the underlying clock. With either type of clock, the minimum time
4192 * to read the clock is saved as sys_fuzz, and used to ensure the
4193 * get_systime() readings always increase and are fuzzed below sys_fuzz.
4196 measure_precision(void)
4199 * With sys_fuzz set to zero, get_systime() fuzzing of low bits
4200 * is effectively disabled. trunc_os_clock is FALSE to disable
4201 * get_ostime() simulation of a low-precision system clock.
4204 trunc_os_clock = FALSE;
4205 measured_tick = measure_tick_fuzz();
4206 set_sys_tick_precision(measured_tick);
4207 msyslog(LOG_INFO, "proto: precision = %.3f usec (%d)",
4208 sys_tick * 1e6, sys_precision);
4209 if (sys_fuzz < sys_tick) {
4210 msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec",
4217 * measure_tick_fuzz()
4219 * measures the minimum time to read the clock (stored in sys_fuzz)
4220 * and returns the tick, the larger of the minimum increment observed
4221 * between successive clock readings and the time to read the clock.
4224 measure_tick_fuzz(void)
4226 l_fp minstep; /* MINSTEP as l_fp */
4227 l_fp val; /* current seconds fraction */
4228 l_fp last; /* last seconds fraction */
4229 l_fp ldiff; /* val - last */
4230 double tick; /* computed tick value */
4235 int i; /* log2 precision */
4241 DTOLFP(MINSTEP, &minstep);
4243 for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) {
4246 L_SUB(&ldiff, &last);
4248 if (L_ISGT(&ldiff, &minstep)) {
4249 max_repeats = max(repeats, max_repeats);
4252 LFPTOD(&ldiff, diff);
4253 tick = min(diff, tick);
4258 if (changes < MINCHANGES) {
4259 msyslog(LOG_ERR, "Fatal error: precision could not be measured (MINSTEP too large?)");
4263 if (0 == max_repeats) {
4266 set_sys_fuzz(tick / max_repeats);
4274 set_sys_tick_precision(
4282 "unsupported tick %.3f > 1s ignored", tick);
4285 if (tick < measured_tick) {
4287 "proto: tick %.3f less than measured tick %.3f, ignored",
4288 tick, measured_tick);
4290 } else if (tick > measured_tick) {
4291 trunc_os_clock = TRUE;
4293 "proto: truncating system clock to multiples of %.9f",
4299 * Find the nearest power of two.
4301 for (i = 0; tick <= 1; i--)
4303 if (tick - 1 > 1 - tick / 2)
4306 sys_precision = (s_char)i;
4311 * init_proto - initialize the protocol module's data
4320 * Fill in the sys_* stuff. Default is don't listen to
4321 * broadcasting, require authentication.
4323 set_sys_leap(LEAP_NOTINSYNC);
4324 sys_stratum = STRATUM_UNSPEC;
4325 memcpy(&sys_refid, "INIT", 4);
4329 L_CLR(&sys_reftime);
4331 measure_precision();
4332 get_systime(&dummy);
4334 sys_manycastserver = 0;
4337 sys_authenticate = 1;
4338 sys_stattime = current_time;
4339 orphwait = current_time + sys_orphwait;
4341 for (i = 0; i < MAX_TTL; i++) {
4342 sys_ttl[i] = (u_char)((i * 256) / MAX_TTL);
4351 * proto_config - configure the protocol module
4362 * Figure out what he wants to change, then do it
4364 DPRINTF(2, ("proto_config: code %d value %lu dvalue %lf\n",
4365 item, value, dvalue));
4370 * enable and disable commands - arguments are Boolean.
4372 case PROTO_AUTHENTICATE: /* authentication (auth) */
4373 sys_authenticate = value;
4376 case PROTO_BROADCLIENT: /* broadcast client (bclient) */
4377 sys_bclient = (int)value;
4378 if (sys_bclient == 0)
4385 case PROTO_CAL: /* refclock calibrate (calibrate) */
4388 #endif /* REFCLOCK */
4390 case PROTO_KERNEL: /* kernel discipline (kernel) */
4394 case PROTO_MONITOR: /* monitoring (monitor) */
4400 msyslog(LOG_WARNING,
4401 "restrict: 'monitor' cannot be disabled while 'limited' is enabled");
4405 case PROTO_NTP: /* NTP discipline (ntp) */
4409 case PROTO_MODE7: /* mode7 management (ntpdc) */
4413 case PROTO_PPS: /* PPS discipline (pps) */
4414 hardpps_enable = value;
4417 case PROTO_FILEGEN: /* statistics (stats) */
4418 stats_control = value;
4422 * tos command - arguments are double, sometimes cast to int
4424 case PROTO_BEACON: /* manycast beacon (beacon) */
4425 sys_beacon = (int)dvalue;
4428 case PROTO_BROADDELAY: /* default broadcast delay (bdelay) */
4429 sys_bdelay = dvalue;
4432 case PROTO_CEILING: /* stratum ceiling (ceiling) */
4433 sys_ceiling = (int)dvalue;
4436 case PROTO_COHORT: /* cohort switch (cohort) */
4437 sys_cohort = (int)dvalue;
4440 case PROTO_FLOOR: /* stratum floor (floor) */
4441 sys_floor = (int)dvalue;
4444 case PROTO_MAXCLOCK: /* maximum candidates (maxclock) */
4445 sys_maxclock = (int)dvalue;
4448 case PROTO_MAXDIST: /* select threshold (maxdist) */
4449 sys_maxdist = dvalue;
4452 case PROTO_CALLDELAY: /* modem call delay (mdelay) */
4453 break; /* NOT USED */
4455 case PROTO_MINCLOCK: /* minimum candidates (minclock) */
4456 sys_minclock = (int)dvalue;
4459 case PROTO_MINDISP: /* minimum distance (mindist) */
4460 sys_mindisp = dvalue;
4463 case PROTO_MINSANE: /* minimum survivors (minsane) */
4464 sys_minsane = (int)dvalue;
4467 case PROTO_ORPHAN: /* orphan stratum (orphan) */
4468 sys_orphan = (int)dvalue;
4471 case PROTO_ORPHWAIT: /* orphan wait (orphwait) */
4472 orphwait -= sys_orphwait;
4473 sys_orphwait = (int)dvalue;
4474 orphwait += sys_orphwait;
4478 * Miscellaneous commands
4480 case PROTO_MULTICAST_ADD: /* add group address */
4482 io_multicast_add(svalue);
4486 case PROTO_MULTICAST_DEL: /* delete group address */
4488 io_multicast_del(svalue);
4492 * Unpeer Early policy choices
4495 case PROTO_UECRYPTO: /* Crypto */
4496 unpeer_crypto_early = value;
4499 case PROTO_UECRYPTONAK: /* Crypto_NAK */
4500 unpeer_crypto_nak_early = value;
4503 case PROTO_UEDIGEST: /* Digest */
4504 unpeer_digest_early = value;
4509 "proto: unsupported option %d", item);
4515 * proto_clr_stats - clear protocol stat counters
4518 proto_clr_stats(void)
4520 sys_stattime = current_time;
4529 sys_limitrejected = 0;