2 * ntp_proto.c - NTP version 4 protocol machinery
4 * ATTENTION: Get approval from Harlan on all changes to this file!
5 * (Harlan will be discussing these changes with Dave Mills.)
13 #include "ntp_stdlib.h"
14 #include "ntp_unixtime.h"
15 #include "ntp_control.h"
16 #include "ntp_string.h"
17 #include "ntp_leapsec.h"
19 #include "refidsmear.h"
20 #include "lib_strbuf.h"
30 /* [Bug 3031] define automatic broadcastdelay cutoff preset */
31 #ifndef BDELAY_DEFAULT
32 # define BDELAY_DEFAULT (-0.050)
35 #define SRVFUZ_SHIFT 6 /* 64 seconds */
36 #define SRVRSP_FUZZ(x) \
39 x.l_ui &= ~((1 << SRVFUZ_SHIFT) - 1U); \
43 * This macro defines the authentication state. If x is 1 authentication
44 * is required; otherwise it is optional.
46 #define AUTH(x, y) ((x) ? (y) == AUTH_OK \
47 : (y) == AUTH_OK || (y) == AUTH_NONE)
51 AUTH_UNKNOWN = -1, /* Unknown */
52 AUTH_NONE, /* authentication not required */
53 AUTH_OK, /* authentication OK */
54 AUTH_ERROR, /* authentication error */
55 AUTH_CRYPTO /* crypto_NAK */
59 * Set up Kiss Code values
64 NOKISS, /* No Kiss Code */
65 RATEKISS, /* Rate limit Kiss Code */
66 DENYKISS, /* Deny Kiss */
67 RSTRKISS, /* Restricted Kiss */
68 XKISS /* Experimental Kiss */
73 NONAK, /* No NAK seen */
74 INVALIDNAK, /* NAK cannot be used */
75 VALIDNAK /* NAK is valid */
79 * traffic shaping parameters
81 #define NTP_IBURST 6 /* packets in iburst */
82 #define RESP_DELAY 1 /* refclock burst delay (s) */
85 * pool soliciting restriction duration (s)
87 #define POOL_SOLICIT_WINDOW 8
90 * flag bits propagated from pool to individual peers
92 #define POOL_FLAG_PMASK (FLAG_IBURST | FLAG_NOSELECT)
95 * peer_select groups statistics for a peer used by clock_select() and
98 typedef struct peer_select_tag {
100 double synch; /* sync distance */
101 double error; /* jitter */
102 double seljit; /* selection jitter */
106 * System variables are declared here. Unless specified otherwise, all
107 * times are in seconds.
109 u_char sys_leap; /* system leap indicator, use set_sys_leap() to change this */
110 u_char xmt_leap; /* leap indicator sent in client requests, set up by set_sys_leap() */
111 u_char sys_stratum; /* system stratum */
112 s_char sys_precision; /* local clock precision (log2 s) */
113 double sys_rootdelay; /* roundtrip delay to root (primary source) */
114 double sys_rootdisp; /* dispersion to root (primary source) */
115 double prev_rootdisp; /* previous root dispersion */
116 double p2_rootdisp; /* previous previous root dispersion */
117 u_int32 sys_refid; /* reference id (network byte order) */
118 l_fp sys_reftime; /* last update time */
119 l_fp prev_reftime; /* previous sys_reftime */
120 l_fp p2_reftime; /* previous previous sys_reftime */
121 u_long prev_time; /* "current_time" when saved prev_time */
122 u_long p2_time; /* previous prev_time */
123 struct peer *sys_peer; /* current peer */
126 struct leap_smear_info leap_smear;
128 int leap_sec_in_progress;
131 * Rate controls. Leaky buckets are used to throttle the packet
132 * transmission rates in order to protect busy servers such as at NIST
133 * and USNO. There is a counter for each association and another for KoD
134 * packets. The association counter decrements each second, but not
135 * below zero. Each time a packet is sent the counter is incremented by
136 * a configurable value representing the average interval between
137 * packets. A packet is delayed as long as the counter is greater than
138 * zero. Note this does not affect the time value computations.
141 * Nonspecified system state variables
143 int sys_bclient; /* broadcast client enable */
144 double sys_bdelay; /* broadcast client default delay */
145 int sys_authenticate; /* requre authentication for config */
146 l_fp sys_authdelay; /* authentication delay */
147 double sys_offset; /* current local clock offset */
148 double sys_mindisp = MINDISPERSE; /* minimum distance (s) */
149 double sys_maxdist = MAXDISTANCE; /* selection threshold */
150 double sys_jitter; /* system jitter */
151 u_long sys_epoch; /* last clock update time */
152 static double sys_clockhop; /* clockhop threshold */
153 static int leap_vote_ins; /* leap consensus for insert */
154 static int leap_vote_del; /* leap consensus for delete */
155 keyid_t sys_private; /* private value for session seed */
156 int sys_manycastserver; /* respond to manycast client pkts */
157 int ntp_mode7; /* respond to ntpdc (mode7) */
158 int peer_ntpdate; /* active peers in ntpdate mode */
159 int sys_survivors; /* truest of the truechimers */
160 char *sys_ident = NULL; /* identity scheme */
163 * TOS and multicast mapping stuff
165 int sys_floor = 0; /* cluster stratum floor */
166 u_char sys_bcpollbstep = 0; /* Broadcast Poll backstep gate */
167 int sys_ceiling = STRATUM_UNSPEC - 1; /* cluster stratum ceiling */
168 int sys_minsane = 1; /* minimum candidates */
169 int sys_minclock = NTP_MINCLOCK; /* minimum candidates */
170 int sys_maxclock = NTP_MAXCLOCK; /* maximum candidates */
171 int sys_cohort = 0; /* cohort switch */
172 int sys_orphan = STRATUM_UNSPEC + 1; /* orphan stratum */
173 int sys_orphwait = NTP_ORPHWAIT; /* orphan wait */
174 int sys_beacon = BEACON; /* manycast beacon interval */
175 u_int sys_ttlmax; /* max ttl mapping vector index */
176 u_char sys_ttl[MAX_TTL]; /* ttl mapping vector */
179 * Statistics counters - first the good, then the bad
181 u_long sys_stattime; /* elapsed time */
182 u_long sys_received; /* packets received */
183 u_long sys_processed; /* packets for this host */
184 u_long sys_newversion; /* current version */
185 u_long sys_oldversion; /* old version */
186 u_long sys_restricted; /* access denied */
187 u_long sys_badlength; /* bad length or format */
188 u_long sys_badauth; /* bad authentication */
189 u_long sys_declined; /* declined */
190 u_long sys_limitrejected; /* rate exceeded */
191 u_long sys_kodsent; /* KoD sent */
194 * Mechanism knobs: how soon do we peer_clear() or unpeer()?
196 * The default way is "on-receipt". If this was a packet from a
197 * well-behaved source, on-receipt will offer the fastest recovery.
198 * If this was from a DoS attack, the default way makes it easier
199 * for a bad-guy to DoS us. So look and see what bites you harder
200 * and choose according to your environment.
202 int peer_clear_digest_early = 1; /* bad digest (TEST5) and Autokey */
203 int unpeer_crypto_early = 1; /* bad crypto (TEST9) */
204 int unpeer_crypto_nak_early = 1; /* crypto_NAK (TEST5) */
205 int unpeer_digest_early = 1; /* bad digest (TEST5) */
207 int dynamic_interleave = DYNAMIC_INTERLEAVE; /* Bug 2978 mitigation */
209 int kiss_code_check(u_char hisleap, u_char hisstratum, u_char hismode, u_int32 refid);
210 nak_code valid_NAK (struct peer *peer, struct recvbuf *rbufp, u_char hismode);
211 static double root_distance (struct peer *);
212 static void clock_combine (peer_select *, int, int);
213 static void peer_xmit (struct peer *);
214 static void fast_xmit (struct recvbuf *, int, keyid_t, int);
215 static void pool_xmit (struct peer *);
216 static void clock_update (struct peer *);
217 static void measure_precision(void);
218 static double measure_tick_fuzz(void);
219 static int local_refid (struct peer *);
220 static int peer_unfit (struct peer *);
222 static int group_test (char *, char *);
225 void pool_name_resolved (int, int, void *, const char *,
226 const char *, const struct addrinfo *,
227 const struct addrinfo *);
230 const char * amtoa (int am);
238 sys_leap = new_sys_leap;
242 * Under certain conditions we send faked leap bits to clients, so
243 * eventually change xmt_leap below, but never change LEAP_NOTINSYNC.
245 if (xmt_leap != LEAP_NOTINSYNC) {
246 if (leap_sec_in_progress) {
247 /* always send "not sync" */
248 xmt_leap = LEAP_NOTINSYNC;
253 * If leap smear is enabled in general we must
254 * never send a leap second warning to clients,
255 * so make sure we only send "in sync".
257 if (leap_smear.enabled)
258 xmt_leap = LEAP_NOWARNING;
260 #endif /* LEAP_SMEAR */
277 if ( hismode == MODE_SERVER
278 && hisleap == LEAP_NOTINSYNC
279 && hisstratum == STRATUM_UNSPEC) {
280 if(memcmp(&refid,"RATE", 4) == 0) {
282 } else if(memcmp(&refid,"DENY", 4) == 0) {
284 } else if(memcmp(&refid,"RSTR", 4) == 0) {
286 } else if(memcmp(&refid,"X", 1) == 0) {
295 * Check that NAK is valid
300 struct recvbuf *rbufp,
304 int base_packet_length = MIN_V4_PKT_LEN;
308 l_fp p_org; /* origin timestamp */
309 const l_fp * myorg; /* selected peer origin */
312 * Check to see if there is something beyond the basic packet
314 if (rbufp->recv_length == base_packet_length) {
318 remainder_size = rbufp->recv_length - base_packet_length;
320 * Is this a potential NAK?
322 if (remainder_size != 4) {
327 * Only server responses can contain NAK's
330 if (hismode != MODE_SERVER &&
331 hismode != MODE_ACTIVE &&
332 hismode != MODE_PASSIVE
338 * Make sure that the extra field in the packet is all zeros
340 rpkt = &rbufp->recv_pkt;
341 keyid = ntohl(((u_int32 *)rpkt)[base_packet_length / 4]);
347 * During the first few packets of the autokey dance there will
348 * not (yet) be a keyid, but in this case FLAG_SKEY is set.
349 * So the NAK is invalid if either there's no peer, or
350 * if the keyid is 0 and FLAG_SKEY is not set.
352 if (!peer || (!peer->keyid && !(peer->flags & FLAG_SKEY))) {
357 * The ORIGIN must match, or this cannot be a valid NAK, either.
360 if (FLAG_LOOPNONCE & peer->flags) {
361 myorg = &peer->nonce;
363 if (peer->flip > 0) {
370 NTOHL_FP(&rpkt->org, &p_org);
372 if (L_ISZERO(&p_org) ||
374 !L_ISEQU(&p_org, myorg)) {
378 /* If we ever passed all that checks, we should be safe. Well,
379 * as safe as we can ever be with an unauthenticated crypto-nak.
386 * transmit - transmit procedure called by poll timeout
390 struct peer *peer /* peer structure pointer */
396 * The polling state machine. There are two kinds of machines,
397 * those that never expect a reply (broadcast and manycast
398 * server modes) and those that do (all other modes). The dance
404 * If we haven't received anything (even if unsync) since last
407 if (peer->outdate > peer->timelastrec && !peer->reach)
408 peer->ppoll = peer->maxpoll;
411 * In broadcast mode the poll interval is never changed from
414 if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) {
415 peer->outdate = current_time;
416 poll_update(peer, hpoll, 0);
417 if (sys_leap != LEAP_NOTINSYNC)
423 * In manycast mode we start with unity ttl. The ttl is
424 * increased by one for each poll until either sys_maxclock
425 * servers have been found or the maximum ttl is reached. When
426 * sys_maxclock servers are found we stop polling until one or
427 * more servers have timed out or until less than sys_minclock
428 * associations turn up. In this case additional better servers
429 * are dragged in and preempt the existing ones. Once every
430 * sys_beacon seconds we are to transmit unconditionally, but
431 * this code is not quite right -- peer->unreach counts polls
432 * and is being compared with sys_beacon, so the beacons happen
433 * every sys_beacon polls.
435 if (peer->cast_flags & MDF_ACAST) {
436 peer->outdate = current_time;
437 poll_update(peer, hpoll, 0);
438 if (peer->unreach > sys_beacon) {
442 } else if ( sys_survivors < sys_minclock
443 || peer_associations < sys_maxclock) {
444 if (peer->ttl < sys_ttlmax)
453 * Pool associations transmit unicast solicitations when there
454 * are less than a hard limit of 2 * sys_maxclock associations,
455 * and either less than sys_minclock survivors or less than
456 * sys_maxclock associations. The hard limit prevents unbounded
457 * growth in associations if the system clock or network quality
458 * result in survivor count dipping below sys_minclock often.
459 * This was observed testing with pool, where sys_maxclock == 12
460 * resulted in 60 associations without the hard limit. A
461 * similar hard limit on manycastclient ephemeral associations
462 * may be appropriate.
464 if (peer->cast_flags & MDF_POOL) {
465 peer->outdate = current_time;
466 poll_update(peer, hpoll, 0);
467 if ( (peer_associations <= 2 * sys_maxclock)
468 && ( peer_associations < sys_maxclock
469 || sys_survivors < sys_minclock))
475 * In unicast modes the dance is much more intricate. It is
476 * designed to back off whenever possible to minimize network
479 if (peer->burst == 0) {
483 * Update the reachability status. If not heard for
484 * three consecutive polls, stuff infinity in the clock
487 oreach = peer->reach;
488 peer->outdate = current_time;
494 * Here the peer is unreachable. If it was
495 * previously reachable raise a trap. Send a
498 clock_filter(peer, 0., 0., MAXDISPERSE);
501 report_event(PEVNT_UNREACH, peer, NULL);
503 if ( (peer->flags & FLAG_IBURST)
505 peer->retry = NTP_RETRY;
509 * Here the peer is reachable. Send a burst if
510 * enabled and the peer is fit. Reset unreach
511 * for persistent and ephemeral associations.
512 * Unreach is also reset for survivors in
516 if (!(peer->flags & FLAG_PREEMPT))
518 if ( (peer->flags & FLAG_BURST)
520 && !peer_unfit(peer))
521 peer->retry = NTP_RETRY;
525 * Watch for timeout. If ephemeral, toss the rascal;
526 * otherwise, bump the poll interval. Note the
527 * poll_update() routine will clamp it to maxpoll.
528 * If preemptible and we have more peers than maxclock,
529 * and this peer has the minimum score of preemptibles,
532 if (peer->unreach >= NTP_UNREACH) {
534 /* ephemeral: no FLAG_CONFIG nor FLAG_PREEMPT */
535 if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
536 report_event(PEVNT_RESTART, peer, "timeout");
537 peer_clear(peer, "TIME");
541 if ( (peer->flags & FLAG_PREEMPT)
542 && (peer_associations > sys_maxclock)
543 && score_all(peer)) {
544 report_event(PEVNT_RESTART, peer, "timeout");
545 peer_clear(peer, "TIME");
552 if (peer->burst == 0) {
555 * If ntpdate mode and the clock has not been
556 * set and all peers have completed the burst,
557 * we declare a successful failure.
561 if (peer_ntpdate == 0) {
563 "ntpd: no servers found");
566 "ntpd: no servers found\n");
576 * Do not transmit if in broadcast client mode.
578 poll_update(peer, hpoll, (peer->hmode == MODE_CLIENT));
579 if (peer->hmode != MODE_BCLIENT)
594 case AM_ERR: return "AM_ERR";
595 case AM_NOMATCH: return "AM_NOMATCH";
596 case AM_PROCPKT: return "AM_PROCPKT";
597 case AM_BCST: return "AM_BCST";
598 case AM_FXMIT: return "AM_FXMIT";
599 case AM_MANYCAST: return "AM_MANYCAST";
600 case AM_NEWPASS: return "AM_NEWPASS";
601 case AM_NEWBCL: return "AM_NEWBCL";
602 case AM_POSSBCL: return "AM_POSSBCL";
605 snprintf(bp, LIB_BUFLENGTH, "AM_#%d", am);
612 * receive - receive procedure called for each packet received
616 struct recvbuf *rbufp
619 register struct peer *peer; /* peer structure pointer */
620 register struct pkt *pkt; /* receive packet pointer */
621 u_char hisversion; /* packet version */
622 u_char hisleap; /* packet leap indicator */
623 u_char hismode; /* packet mode */
624 u_char hisstratum; /* packet stratum */
625 r4addr r4a; /* address restrictions */
626 u_short restrict_mask; /* restrict bits */
627 const char *hm_str; /* hismode string */
628 const char *am_str; /* association match string */
629 int kissCode = NOKISS; /* Kiss Code */
630 int has_mac; /* length of MAC field */
631 int authlen; /* offset of MAC field */
632 auth_code is_authentic = AUTH_UNKNOWN; /* Was AUTH_NONE */
633 nak_code crypto_nak_test; /* result of crypto-NAK check */
634 int retcode = AM_NOMATCH; /* match code */
635 keyid_t skeyid = 0; /* key IDs */
636 u_int32 opcode = 0; /* extension field opcode */
637 sockaddr_u *dstadr_sin; /* active runway */
638 struct peer *peer2; /* aux peer structure pointer */
639 endpt *match_ep; /* newpeer() local address */
640 l_fp p_org; /* origin timestamp */
641 l_fp p_rec; /* receive timestamp */
642 l_fp p_xmt; /* transmit timestamp */
644 char hostname[NTP_MAXSTRLEN + 1];
645 char *groupname = NULL;
646 struct autokey *ap; /* autokey structure pointer */
647 int rval; /* cookie snatcher */
648 keyid_t pkeyid = 0, tkeyid = 0; /* key IDs */
650 #ifdef HAVE_NTP_SIGND
651 static unsigned char zero_key[16];
652 #endif /* HAVE_NTP_SIGND */
655 * Note that there are many places we do not call record_raw_stats().
657 * We only want to call it *after* we've sent a response, or perhaps
658 * when we've decided to drop a packet.
662 * Monitor the packet and get restrictions. Note that the packet
663 * length for control and private mode packets must be checked
664 * by the service routines. Some restrictions have to be handled
665 * later in order to generate a kiss-o'-death packet.
668 * Bogus port check is before anything, since it probably
669 * reveals a clogging attack. Likewise the mimimum packet size
670 * of 2 bytes (for mode 6/7) must be checked first.
673 if (0 == SRCPORT(&rbufp->recv_srcadr) || rbufp->recv_length < 2) {
675 return; /* bogus port / length */
677 restrictions(&rbufp->recv_srcadr, &r4a);
678 restrict_mask = r4a.rflags;
680 pkt = &rbufp->recv_pkt;
681 hisversion = PKT_VERSION(pkt->li_vn_mode);
682 hismode = (int)PKT_MODE(pkt->li_vn_mode);
684 if (restrict_mask & RES_IGNORE) {
685 DPRINTF(2, ("receive: drop: RES_IGNORE\n"));
687 return; /* ignore everything */
689 if (hismode == MODE_PRIVATE) {
690 if (!ntp_mode7 || (restrict_mask & RES_NOQUERY)) {
691 DPRINTF(2, ("receive: drop: RES_NOQUERY\n"));
693 return; /* no query private */
695 process_private(rbufp, ((restrict_mask &
696 RES_NOMODIFY) == 0));
699 if (hismode == MODE_CONTROL) {
700 if (restrict_mask & RES_NOQUERY) {
701 DPRINTF(2, ("receive: drop: RES_NOQUERY\n"));
703 return; /* no query control */
705 process_control(rbufp, restrict_mask);
708 if (restrict_mask & RES_DONTSERVE) {
709 DPRINTF(2, ("receive: drop: RES_DONTSERVE\n"));
711 return; /* no time serve */
715 /* If we arrive here, we should have a standard NTP packet. We
716 * check that the minimum size is available and fetch some more
717 * items from the packet once we can be sure they are indeed
720 if (rbufp->recv_length < LEN_PKT_NOMAC) {
722 return; /* bogus length */
725 hisleap = PKT_LEAP(pkt->li_vn_mode);
726 hisstratum = PKT_TO_STRATUM(pkt->stratum);
727 INSIST(0 != hisstratum); /* paranoia check PKT_TO_STRATUM result */
729 DPRINTF(1, ("receive: at %ld %s<-%s ippeerlimit %d mode %d iflags %s "
730 "restrict %s org %#010x.%08x xmt %#010x.%08x\n",
731 current_time, stoa(&rbufp->dstadr->sin),
732 stoa(&rbufp->recv_srcadr), r4a.ippeerlimit, hismode,
733 build_iflags(rbufp->dstadr->flags),
734 build_rflags(restrict_mask),
735 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
736 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
739 * This is for testing. If restricted drop ten percent of
742 if (restrict_mask & RES_FLAKE) {
743 if ((double)ntp_random() / 0x7fffffff < .1) {
744 DPRINTF(2, ("receive: drop: RES_FLAKE\n"));
746 return; /* no flakeway */
751 ** Format Layer Checks
753 ** Validate the packet format. The packet size, packet header,
754 ** and any extension field lengths are checked. We identify
755 ** the beginning of the MAC, to identify the upper limit of
756 ** of the hash computation.
758 ** In case of a format layer check violation, the packet is
759 ** discarded with no further processing.
763 * Version check must be after the query packets, since they
764 * intentionally use an early version.
766 if (hisversion == NTP_VERSION) {
767 sys_newversion++; /* new version */
768 } else if ( !(restrict_mask & RES_VERSION)
769 && hisversion >= NTP_OLDVERSION) {
770 sys_oldversion++; /* previous version */
772 DPRINTF(2, ("receive: drop: RES_VERSION\n"));
774 return; /* old version */
778 * Figure out his mode and validate the packet. This has some
779 * legacy raunch that probably should be removed. In very early
780 * NTP versions mode 0 was equivalent to what later versions
781 * would interpret as client mode.
783 if (hismode == MODE_UNSPEC) {
784 if (hisversion == NTP_OLDVERSION) {
785 hismode = MODE_CLIENT;
787 DPRINTF(2, ("receive: drop: MODE_UNSPEC\n"));
789 return; /* invalid mode */
794 * Parse the extension field if present. We figure out whether
795 * an extension field is present by measuring the MAC size. If
796 * the number of words following the packet header is 0, no MAC
797 * is present and the packet is not authenticated. If 1, the
798 * packet is a crypto-NAK; if 3, the packet is authenticated
799 * with DES; if 5, the packet is authenticated with MD5; if 6,
800 * the packet is authenticated with SHA. If 2 or * 4, the packet
801 * is a runt and discarded forthwith. If greater than 6, an
802 * extension field is present, so we subtract the length of the
803 * field and go around again.
805 * Note the above description is lame. We should/could also check
806 * the two bytes that make up the EF type and subtype, and then
807 * check the two bytes that tell us the EF length. A legacy MAC
808 * has a 4 byte keyID, and for conforming symmetric keys its value
809 * must be <= 64k, meaning the top two bytes will always be zero.
810 * Since the EF Type of 0 is reserved/unused, there's no way a
811 * conforming legacy MAC could ever be misinterpreted as an EF.
813 * There is more, but this isn't the place to document it.
816 authlen = LEN_PKT_NOMAC;
817 has_mac = rbufp->recv_length - authlen;
818 while (has_mac > 0) {
825 if (has_mac % 4 != 0 || has_mac < (int)MIN_MAC_LEN) {
826 DPRINTF(2, ("receive: drop: bad post-packet length\n"));
828 return; /* bad length */
831 * This next test is clearly wrong - it needlessly
832 * prohibits short EFs (which don't yet exist)
834 if (has_mac <= (int)MAX_MAC_LEN) {
835 skeyid = ntohl(((u_int32 *)pkt)[authlen / 4]);
839 opcode = ntohl(((u_int32 *)pkt)[authlen / 4]);
840 len = opcode & 0xffff;
843 || (int)len + authlen > rbufp->recv_length) {
844 DPRINTF(2, ("receive: drop: bad EF length\n"));
846 return; /* bad length */
850 * Extract calling group name for later. If
851 * sys_groupname is non-NULL, there must be
852 * a group name provided to elicit a response.
854 if ( (opcode & 0x3fff0000) == CRYPTO_ASSOC
855 && sys_groupname != NULL) {
856 ep = (struct exten *)&((u_int32 *)pkt)[authlen / 4];
857 hostlen = ntohl(ep->vallen);
858 if ( hostlen >= sizeof(hostname)
860 offsetof(struct exten, pkt)) {
861 DPRINTF(2, ("receive: drop: bad autokey hostname length\n"));
863 return; /* bad length */
865 memcpy(hostname, &ep->pkt, hostlen);
866 hostname[hostlen] = '\0';
867 groupname = strchr(hostname, '@');
868 if (groupname == NULL) {
869 DPRINTF(2, ("receive: drop: empty autokey groupname\n"));
882 * If has_mac is < 0 we had a malformed packet.
885 DPRINTF(2, ("receive: drop: post-packet under-read\n"));
887 return; /* bad length */
891 ** Packet Data Verification Layer
893 ** This layer verifies the packet data content. If
894 ** authentication is required, a MAC must be present.
895 ** If a MAC is present, it must validate.
896 ** Crypto-NAK? Look - a shiny thing!
898 ** If authentication fails, we're done.
902 * If authentication is explicitly required, a MAC must be present.
904 if (restrict_mask & RES_DONTTRUST && has_mac == 0) {
905 DPRINTF(2, ("receive: drop: RES_DONTTRUST\n"));
907 return; /* access denied */
911 * Update the MRU list and finger the cloggers. It can be a
912 * little expensive, so turn it off for production use.
913 * RES_LIMITED and RES_KOD will be cleared in the returned
914 * restrict_mask unless one or both actions are warranted.
916 restrict_mask = ntp_monitor(rbufp, restrict_mask);
917 if (restrict_mask & RES_LIMITED) {
919 if ( !(restrict_mask & RES_KOD)
920 || MODE_BROADCAST == hismode
921 || MODE_SERVER == hismode) {
922 if (MODE_SERVER == hismode) {
923 DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
924 stoa(&rbufp->recv_srcadr)));
926 DPRINTF(2, ("receive: drop: RES_KOD\n"));
928 return; /* rate exceeded */
930 if (hismode == MODE_CLIENT) {
931 fast_xmit(rbufp, MODE_SERVER, skeyid,
934 fast_xmit(rbufp, MODE_ACTIVE, skeyid,
937 return; /* rate exceeded */
939 restrict_mask &= ~RES_KOD;
942 * We have tossed out as many buggy packets as possible early in
943 * the game to reduce the exposure to a clogging attack. Now we
944 * have to burn some cycles to find the association and
945 * authenticate the packet if required. Note that we burn only
946 * digest cycles, again to reduce exposure. There may be no
947 * matching association and that's okay.
949 * More on the autokey mambo. Normally the local interface is
950 * found when the association was mobilized with respect to a
951 * designated remote address. We assume packets arriving from
952 * the remote address arrive via this interface and the local
953 * address used to construct the autokey is the unicast address
954 * of the interface. However, if the sender is a broadcaster,
955 * the interface broadcast address is used instead.
956 * Notwithstanding this technobabble, if the sender is a
957 * multicaster, the broadcast address is null, so we use the
958 * unicast address anyway. Don't ask.
961 peer = findpeer(rbufp, hismode, &retcode);
962 dstadr_sin = &rbufp->dstadr->sin;
963 NTOHL_FP(&pkt->org, &p_org);
964 NTOHL_FP(&pkt->rec, &p_rec);
965 NTOHL_FP(&pkt->xmt, &p_xmt);
966 hm_str = modetoa(hismode);
967 am_str = amtoa(retcode);
970 * Authentication is conditioned by three switches:
972 * NOPEER (RES_NOPEER) do not mobilize an association unless
974 * NOTRUST (RES_DONTTRUST) do not allow access unless
975 * authenticated (implies NOPEER)
976 * enable (sys_authenticate) master NOPEER switch, by default
979 * The NOPEER and NOTRUST can be specified on a per-client basis
980 * using the restrict command. The enable switch if on implies
981 * NOPEER for all clients. There are four outcomes:
983 * NONE The packet has no MAC.
984 * OK the packet has a MAC and authentication succeeds
985 * ERROR the packet has a MAC and authentication fails
986 * CRYPTO crypto-NAK. The MAC has four octets only.
988 * Note: The AUTH(x, y) macro is used to filter outcomes. If x
989 * is zero, acceptable outcomes of y are NONE and OK. If x is
990 * one, the only acceptable outcome of y is OK.
992 crypto_nak_test = valid_NAK(peer, rbufp, hismode);
995 * Drop any invalid crypto-NAKs
997 if (crypto_nak_test == INVALIDNAK) {
998 report_event(PEVNT_AUTH, peer, "Invalid_NAK");
1002 msyslog(LOG_ERR, "Invalid-NAK error at %ld %s<-%s",
1003 current_time, stoa(dstadr_sin), stoa(&rbufp->recv_srcadr));
1008 restrict_mask &= ~RES_MSSNTP;
1009 is_authentic = AUTH_NONE; /* not required */
1010 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x NOMAC\n",
1011 current_time, stoa(dstadr_sin),
1012 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1014 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1015 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1016 } else if (crypto_nak_test == VALIDNAK) {
1017 restrict_mask &= ~RES_MSSNTP;
1018 is_authentic = AUTH_CRYPTO; /* crypto-NAK */
1019 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x CRYPTONAK\n",
1020 current_time, stoa(dstadr_sin),
1021 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1022 skeyid, authlen + has_mac, is_authentic,
1023 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1024 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1026 #ifdef HAVE_NTP_SIGND
1028 * If the signature is 20 bytes long, the last 16 of
1029 * which are zero, then this is a Microsoft client
1030 * wanting AD-style authentication of the server's
1033 * This is described in Microsoft's WSPP docs, in MS-SNTP:
1034 * http://msdn.microsoft.com/en-us/library/cc212930.aspx
1036 } else if ( has_mac == MAX_MD5_LEN
1037 && (restrict_mask & RES_MSSNTP)
1038 && (retcode == AM_FXMIT || retcode == AM_NEWPASS)
1039 && (memcmp(zero_key, (char *)pkt + authlen + 4,
1040 MAX_MD5_LEN - 4) == 0)) {
1041 is_authentic = AUTH_NONE;
1042 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %#010x.%08x xmt %#010x.%08x SIGND\n",
1043 current_time, stoa(dstadr_sin),
1044 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1046 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1047 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1048 #endif /* HAVE_NTP_SIGND */
1054 * Not an MS-SNTP SIGND packet
1056 * So there is a MAC here.
1059 restrict_mask &= ~RES_MSSNTP;
1062 * For autokey modes, generate the session key
1063 * and install in the key cache. Use the socket
1064 * broadcast or unicast address as appropriate.
1066 if (crypto_flags && skeyid > NTP_MAXKEY) {
1069 * More on the autokey dance (AKD). A cookie is
1070 * constructed from public and private values.
1071 * For broadcast packets, the cookie is public
1072 * (zero). For packets that match no
1073 * association, the cookie is hashed from the
1074 * addresses and private value. For server
1075 * packets, the cookie was previously obtained
1076 * from the server. For symmetric modes, the
1077 * cookie was previously constructed using an
1078 * agreement protocol; however, should PKI be
1079 * unavailable, we construct a fake agreement as
1080 * the EXOR of the peer and host cookies.
1082 * hismode ephemeral persistent
1083 * =======================================
1085 * passive 0% cookie#
1086 * client sys cookie 0%
1087 * server 0% sys cookie
1093 if (has_mac < (int)MAX_MD5_LEN) {
1094 DPRINTF(2, ("receive: drop: MD5 digest too short\n"));
1098 if (hismode == MODE_BROADCAST) {
1101 * For broadcaster, use the interface
1102 * broadcast address when available;
1103 * otherwise, use the unicast address
1104 * found when the association was
1105 * mobilized. However, if this is from
1106 * the wildcard interface, game over.
1110 ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr)) {
1111 DPRINTF(2, ("receive: drop: BCAST from wildcard\n"));
1113 return; /* no wildcard */
1116 if (!SOCK_UNSPEC(&rbufp->dstadr->bcast))
1118 &rbufp->dstadr->bcast;
1119 } else if (peer == NULL) {
1120 pkeyid = session_key(
1121 &rbufp->recv_srcadr, dstadr_sin, 0,
1124 pkeyid = peer->pcookie;
1128 * The session key includes both the public
1129 * values and cookie. In case of an extension
1130 * field, the cookie used for authentication
1131 * purposes is zero. Note the hash is saved for
1132 * use later in the autokey mambo.
1134 if (authlen > (int)LEN_PKT_NOMAC && pkeyid != 0) {
1135 session_key(&rbufp->recv_srcadr,
1136 dstadr_sin, skeyid, 0, 2);
1137 tkeyid = session_key(
1138 &rbufp->recv_srcadr, dstadr_sin,
1141 tkeyid = session_key(
1142 &rbufp->recv_srcadr, dstadr_sin,
1147 #endif /* AUTOKEY */
1150 * Compute the cryptosum. Note a clogging attack may
1151 * succeed in bloating the key cache. If an autokey,
1152 * purge it immediately, since we won't be needing it
1153 * again. If the packet is authentic, it can mobilize an
1154 * association. Note that there is no key zero.
1156 if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1158 is_authentic = AUTH_ERROR;
1160 is_authentic = AUTH_OK;
1162 if (crypto_flags && skeyid > NTP_MAXKEY)
1163 authtrust(skeyid, 0);
1164 #endif /* AUTOKEY */
1165 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org %#010x.%08x xmt %#010x.%08x MAC\n",
1166 current_time, stoa(dstadr_sin),
1167 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1168 skeyid, authlen + has_mac, is_authentic,
1169 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1170 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1177 * Now come at this from a different perspective:
1178 * - If we expect a MAC and it's not there, we drop it.
1179 * - If we expect one keyID and get another, we drop it.
1180 * - If we have a MAC ahd it hasn't been validated yet, try.
1181 * - if the provided MAC doesn't validate, we drop it.
1183 * There might be more to this.
1185 if (0 != peer && 0 != peer->keyid) {
1186 /* Should we msyslog() any of these? */
1189 * This should catch:
1190 * - no keyID where one is expected,
1191 * - different keyID than what we expect.
1193 if (peer->keyid != skeyid) {
1194 DPRINTF(2, ("receive: drop: Wanted keyID %d, got %d from %s\n",
1195 peer->keyid, skeyid,
1196 stoa(&rbufp->recv_srcadr)));
1198 return; /* drop: access denied */
1202 * if has_mac != 0 ...
1203 * - If it has not yet been validated, do so.
1204 * (under what circumstances might that happen?)
1205 * - if missing or bad MAC, log and drop.
1208 if (is_authentic == AUTH_UNKNOWN) {
1209 /* How can this happen? */
1210 DPRINTF(2, ("receive: 3454 check: AUTH_UNKNOWN from %s\n",
1211 stoa(&rbufp->recv_srcadr)));
1212 if (!authdecrypt(skeyid, (u_int32 *)pkt, authlen,
1214 /* MAC invalid or not found */
1215 is_authentic = AUTH_ERROR;
1217 is_authentic = AUTH_OK;
1220 if (is_authentic != AUTH_OK) {
1221 DPRINTF(2, ("receive: drop: missing or bad MAC from %s\n",
1222 stoa(&rbufp->recv_srcadr)));
1224 return; /* drop: access denied */
1231 ** On-Wire Protocol Layer
1233 ** Verify protocol operations consistent with the on-wire protocol.
1234 ** The protocol discards bogus and duplicate packets as well as
1235 ** minimizes disruptions doe to protocol restarts and dropped
1236 ** packets. The operations are controlled by two timestamps:
1237 ** the transmit timestamp saved in the client state variables,
1238 ** and the origin timestamp in the server packet header. The
1239 ** comparison of these two timestamps is called the loopback test.
1240 ** The transmit timestamp functions as a nonce to verify that the
1241 ** response corresponds to the original request. The transmit
1242 ** timestamp also serves to discard replays of the most recent
1243 ** packet. Upon failure of either test, the packet is discarded
1244 ** with no further action.
1248 * The association matching rules are implemented by a set of
1249 * routines and an association table. A packet matching an
1250 * association is processed by the peer process for that
1251 * association. If there are no errors, an ephemeral association
1252 * is mobilized: a broadcast packet mobilizes a broadcast client
1253 * aassociation; a manycast server packet mobilizes a manycast
1254 * client association; a symmetric active packet mobilizes a
1255 * symmetric passive association.
1257 DPRINTF(1, ("receive: MATCH_ASSOC dispatch: mode %d/%s:%s \n",
1258 hismode, hm_str, am_str));
1262 * This is a client mode packet not matching any association. If
1263 * an ordinary client, simply toss a server mode packet back
1264 * over the fence. If a manycast client, we have to work a
1267 * There are cases here where we do not call record_raw_stats().
1272 * If authentication OK, send a server reply; otherwise,
1273 * send a crypto-NAK.
1275 if (!(rbufp->dstadr->flags & INT_MCASTOPEN)) {
1276 /* HMS: would be nice to log FAST_XMIT|BADAUTH|RESTRICTED */
1277 record_raw_stats(&rbufp->recv_srcadr,
1278 &rbufp->dstadr->sin,
1279 &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1280 PKT_LEAP(pkt->li_vn_mode),
1281 PKT_VERSION(pkt->li_vn_mode),
1282 PKT_MODE(pkt->li_vn_mode),
1283 PKT_TO_STRATUM(pkt->stratum),
1286 FPTOD(NTOHS_FP(pkt->rootdelay)),
1287 FPTOD(NTOHS_FP(pkt->rootdisp)),
1289 rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1291 if (AUTH(restrict_mask & RES_DONTTRUST,
1293 /* Bug 3596: Do we want to fuzz the reftime? */
1294 fast_xmit(rbufp, MODE_SERVER, skeyid,
1296 } else if (is_authentic == AUTH_ERROR) {
1297 /* Bug 3596: Do we want to fuzz the reftime? */
1298 fast_xmit(rbufp, MODE_SERVER, 0,
1302 DPRINTF(2, ("receive: AM_FXMIT drop: !mcast restricted\n"));
1306 return; /* hooray */
1310 * This must be manycast. Do not respond if not
1311 * configured as a manycast server.
1313 if (!sys_manycastserver) {
1314 DPRINTF(2, ("receive: AM_FXMIT drop: Not manycastserver\n"));
1316 return; /* not enabled */
1321 * Do not respond if not the same group.
1323 if (group_test(groupname, NULL)) {
1324 DPRINTF(2, ("receive: AM_FXMIT drop: empty groupname\n"));
1328 #endif /* AUTOKEY */
1331 * Do not respond if we are not synchronized or our
1332 * stratum is greater than the manycaster or the
1333 * manycaster has already synchronized to us.
1335 if ( sys_leap == LEAP_NOTINSYNC
1336 || sys_stratum >= hisstratum
1337 || (!sys_cohort && sys_stratum == hisstratum + 1)
1338 || rbufp->dstadr->addr_refid == pkt->refid) {
1339 DPRINTF(2, ("receive: AM_FXMIT drop: LEAP_NOTINSYNC || stratum || loop\n"));
1341 return; /* no help */
1345 * Respond only if authentication succeeds. Don't do a
1346 * crypto-NAK, as that would not be useful.
1348 if (AUTH(restrict_mask & RES_DONTTRUST, is_authentic)) {
1349 record_raw_stats(&rbufp->recv_srcadr,
1350 &rbufp->dstadr->sin,
1351 &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1352 PKT_LEAP(pkt->li_vn_mode),
1353 PKT_VERSION(pkt->li_vn_mode),
1354 PKT_MODE(pkt->li_vn_mode),
1355 PKT_TO_STRATUM(pkt->stratum),
1358 FPTOD(NTOHS_FP(pkt->rootdelay)),
1359 FPTOD(NTOHS_FP(pkt->rootdisp)),
1361 rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1363 /* Bug 3596: Do we want to fuzz the reftime? */
1364 fast_xmit(rbufp, MODE_SERVER, skeyid,
1367 return; /* hooray */
1370 * This is a server mode packet returned in response to a client
1371 * mode packet sent to a multicast group address (for
1372 * manycastclient) or to a unicast address (for pool). The
1373 * origin timestamp is a good nonce to reliably associate the
1374 * reply with what was sent. If there is no match, that's
1375 * curious and could be an intruder attempting to clog, so we
1378 * If the packet is authentic and the manycastclient or pool
1379 * association is found, we mobilize a client association and
1380 * copy pertinent variables from the manycastclient or pool
1381 * association to the new client association. If not, just
1382 * ignore the packet.
1384 * There is an implosion hazard at the manycast client, since
1385 * the manycast servers send the server packet immediately. If
1386 * the guy is already here, don't fire up a duplicate.
1388 * There are cases here where we do not call record_raw_stats().
1394 * Do not respond if not the same group.
1396 if (group_test(groupname, NULL)) {
1397 DPRINTF(2, ("receive: AM_MANYCAST drop: empty groupname\n"));
1401 #endif /* AUTOKEY */
1402 if ((peer2 = findmanycastpeer(rbufp)) == NULL) {
1403 DPRINTF(2, ("receive: AM_MANYCAST drop: No manycast peer\n"));
1405 return; /* not enabled */
1407 if (!AUTH( (!(peer2->cast_flags & MDF_POOL)
1408 && sys_authenticate)
1409 || (restrict_mask & (RES_NOPEER |
1410 RES_DONTTRUST)), is_authentic)
1411 /* MC: RES_NOEPEER? */
1413 DPRINTF(2, ("receive: AM_MANYCAST drop: bad auth || (NOPEER|DONTTRUST)\n"));
1415 return; /* access denied */
1419 * Do not respond if unsynchronized or stratum is below
1420 * the floor or at or above the ceiling.
1422 if ( hisleap == LEAP_NOTINSYNC
1423 || hisstratum < sys_floor
1424 || hisstratum >= sys_ceiling) {
1425 DPRINTF(2, ("receive: AM_MANYCAST drop: unsync/stratum\n"));
1427 return; /* no help */
1429 peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1430 r4a.ippeerlimit, MODE_CLIENT, hisversion,
1431 peer2->minpoll, peer2->maxpoll,
1432 (FLAG_PREEMPT | (POOL_FLAG_PMASK & peer2->flags)),
1433 (MDF_UCAST | MDF_UCLNT), 0, skeyid, sys_ident);
1435 DPRINTF(2, ("receive: AM_MANYCAST drop: duplicate\n"));
1437 return; /* ignore duplicate */
1441 * After each ephemeral pool association is spun,
1442 * accelerate the next poll for the pool solicitor so
1443 * the pool will fill promptly.
1445 if (peer2->cast_flags & MDF_POOL)
1446 peer2->nextdate = current_time + 1;
1449 * Further processing of the solicitation response would
1450 * simply detect its origin timestamp as bogus for the
1451 * brand-new association (it matches the prototype
1452 * association) and tinker with peer->nextdate delaying
1455 return; /* solicitation response handled */
1458 * This is the first packet received from a broadcast server. If
1459 * the packet is authentic and we are enabled as broadcast
1460 * client, mobilize a broadcast client association. We don't
1461 * kiss any frogs here.
1463 * There are cases here where we do not call record_raw_stats().
1469 * Do not respond if not the same group.
1471 if (group_test(groupname, sys_ident)) {
1472 DPRINTF(2, ("receive: AM_NEWBCL drop: groupname mismatch\n"));
1476 #endif /* AUTOKEY */
1477 if (sys_bclient == 0) {
1478 DPRINTF(2, ("receive: AM_NEWBCL drop: not a bclient\n"));
1480 return; /* not enabled */
1482 if (!AUTH(sys_authenticate | (restrict_mask &
1483 (RES_NOPEER | RES_DONTTRUST)), is_authentic)
1484 /* NEWBCL: RES_NOEPEER? */
1486 DPRINTF(2, ("receive: AM_NEWBCL drop: AUTH failed\n"));
1488 return; /* access denied */
1492 * Do not respond if unsynchronized or stratum is below
1493 * the floor or at or above the ceiling.
1495 if ( hisleap == LEAP_NOTINSYNC
1496 || hisstratum < sys_floor
1497 || hisstratum >= sys_ceiling) {
1498 DPRINTF(2, ("receive: AM_NEWBCL drop: Unsync or bad stratum\n"));
1500 return; /* no help */
1505 * Do not respond if Autokey and the opcode is not a
1506 * CRYPTO_ASSOC response with association ID.
1508 if ( crypto_flags && skeyid > NTP_MAXKEY
1509 && (opcode & 0xffff0000) != (CRYPTO_ASSOC | CRYPTO_RESP)) {
1510 DPRINTF(2, ("receive: AM_NEWBCL drop: Autokey but not CRYPTO_ASSOC\n"));
1512 return; /* protocol error */
1514 #endif /* AUTOKEY */
1517 * Broadcasts received via a multicast address may
1518 * arrive after a unicast volley has begun
1519 * with the same remote address. newpeer() will not
1520 * find duplicate associations on other local endpoints
1521 * if a non-NULL endpoint is supplied. multicastclient
1522 * ephemeral associations are unique across all local
1525 if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
1526 match_ep = rbufp->dstadr;
1531 * Determine whether to execute the initial volley.
1533 if (sys_bdelay > 0.0) {
1536 * If a two-way exchange is not possible,
1537 * neither is Autokey.
1539 if (crypto_flags && skeyid > NTP_MAXKEY) {
1541 DPRINTF(2, ("receive: AM_NEWBCL drop: Autokey but not 2-way\n"));
1542 return; /* no autokey */
1544 #endif /* AUTOKEY */
1547 * Do not execute the volley. Start out in
1548 * broadcast client mode.
1550 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1551 r4a.ippeerlimit, MODE_BCLIENT, hisversion,
1552 pkt->ppoll, pkt->ppoll,
1553 FLAG_PREEMPT, MDF_BCLNT, 0, skeyid, sys_ident);
1555 DPRINTF(2, ("receive: AM_NEWBCL drop: duplicate\n"));
1557 return; /* ignore duplicate */
1560 peer->delay = sys_bdelay;
1567 * Execute the initial volley in order to calibrate the
1568 * propagation delay and run the Autokey protocol.
1570 * Note that the minpoll is taken from the broadcast
1571 * packet, normally 6 (64 s) and that the poll interval
1572 * is fixed at this value.
1574 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1575 r4a.ippeerlimit, MODE_CLIENT, hisversion,
1576 pkt->ppoll, pkt->ppoll,
1577 FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
1578 0, skeyid, sys_ident);
1580 DPRINTF(2, ("receive: AM_NEWBCL drop: empty newpeer() failed\n"));
1582 return; /* ignore duplicate */
1586 if (skeyid > NTP_MAXKEY)
1587 crypto_recv(peer, rbufp);
1588 #endif /* AUTOKEY */
1590 return; /* hooray */
1593 * This is the first packet received from a potential ephemeral
1594 * symmetric active peer. First, deal with broken Windows clients.
1595 * Then, if NOEPEER is enabled, drop it. If the packet meets our
1596 * authenticty requirements and is the first he sent, mobilize
1597 * a passive association.
1598 * Otherwise, kiss the frog.
1600 * There are cases here where we do not call record_raw_stats().
1604 DEBUG_REQUIRE(MODE_ACTIVE == hismode);
1608 * Do not respond if not the same group.
1610 if (group_test(groupname, sys_ident)) {
1611 DPRINTF(2, ("receive: AM_NEWPASS drop: Autokey group mismatch\n"));
1615 #endif /* AUTOKEY */
1616 if (!AUTH(sys_authenticate | (restrict_mask &
1617 (RES_NOPEER | RES_DONTTRUST)), is_authentic)
1620 * If authenticated but cannot mobilize an
1621 * association, send a symmetric passive
1622 * response without mobilizing an association.
1623 * This is for drat broken Windows clients. See
1624 * Microsoft KB 875424 for preferred workaround.
1626 if (AUTH(restrict_mask & RES_DONTTRUST,
1628 fast_xmit(rbufp, MODE_PASSIVE, skeyid,
1630 return; /* hooray */
1632 /* HMS: Why is this next set of lines a feature? */
1633 if (is_authentic == AUTH_ERROR) {
1634 fast_xmit(rbufp, MODE_PASSIVE, 0,
1640 if (restrict_mask & RES_NOEPEER) {
1641 DPRINTF(2, ("receive: AM_NEWPASS drop: NOEPEER\n"));
1647 * If we got here, the packet isn't part of an
1648 * existing association, either isn't correctly
1649 * authenticated or it is but we are refusing
1650 * ephemeral peer requests, and it didn't meet
1651 * either of the previous two special cases so we
1652 * should just drop it on the floor. For example,
1653 * crypto-NAKs (is_authentic == AUTH_CRYPTO)
1654 * will make it this far. This is just
1655 * debug-printed and not logged to avoid log
1658 DPRINTF(2, ("receive: at %ld refusing to mobilize passive association"
1659 " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1660 current_time, stoa(&rbufp->recv_srcadr),
1661 hismode, hm_str, am_str, skeyid,
1662 (authlen + has_mac), is_authentic));
1667 if (restrict_mask & RES_NOEPEER) {
1668 DPRINTF(2, ("receive: AM_NEWPASS drop: NOEPEER\n"));
1674 * Do not respond if synchronized and if stratum is
1675 * below the floor or at or above the ceiling. Note,
1676 * this allows an unsynchronized peer to synchronize to
1677 * us. It would be very strange if he did and then was
1678 * nipped, but that could only happen if we were
1679 * operating at the top end of the range. It also means
1680 * we will spin an ephemeral association in response to
1681 * MODE_ACTIVE KoDs, which will time out eventually.
1683 if ( hisleap != LEAP_NOTINSYNC
1684 && (hisstratum < sys_floor || hisstratum >= sys_ceiling)) {
1685 DPRINTF(2, ("receive: AM_NEWPASS drop: Autokey group mismatch\n"));
1687 return; /* no help */
1691 * The message is correctly authenticated and allowed.
1692 * Mobilize a symmetric passive association, if we won't
1693 * exceed the ippeerlimit.
1695 if ((peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1696 r4a.ippeerlimit, MODE_PASSIVE, hisversion,
1697 pkt->ppoll, NTP_MAXDPOLL, 0, MDF_UCAST, 0,
1698 skeyid, sys_ident)) == NULL) {
1699 DPRINTF(2, ("receive: AM_NEWPASS drop: newpeer() failed\n"));
1701 return; /* ignore duplicate */
1707 * Process regular packet. Nothing special.
1709 * There are cases here where we do not call record_raw_stats().
1715 * Do not respond if not the same group.
1717 if (group_test(groupname, peer->ident)) {
1718 DPRINTF(2, ("receive: AM_PROCPKT drop: Autokey group mismatch\n"));
1722 #endif /* AUTOKEY */
1724 if (MODE_BROADCAST == hismode) {
1729 DPRINTF(2, ("receive: PROCPKT/BROADCAST: prev pkt %ld seconds ago, ppoll: %d, %d secs\n",
1730 (current_time - peer->timelastrec),
1731 peer->ppoll, (1 << peer->ppoll)
1733 /* Things we can check:
1735 * Did the poll interval change?
1736 * Is the poll interval in the packet in-range?
1737 * Did this packet arrive too soon?
1738 * Is the timestamp in this packet monotonic
1739 * with respect to the previous packet?
1742 /* This is noteworthy, not error-worthy */
1743 if (pkt->ppoll != peer->ppoll) {
1744 msyslog(LOG_INFO, "receive: broadcast poll from %s changed from %u to %u",
1745 stoa(&rbufp->recv_srcadr),
1746 peer->ppoll, pkt->ppoll);
1749 /* This is error-worthy */
1750 if ( pkt->ppoll < peer->minpoll
1751 || pkt->ppoll > peer->maxpoll) {
1752 msyslog(LOG_INFO, "receive: broadcast poll of %u from %s is out-of-range (%d to %d)!",
1753 pkt->ppoll, stoa(&rbufp->recv_srcadr),
1754 peer->minpoll, peer->maxpoll);
1758 /* too early? worth an error, too!
1760 * [Bug 3113] Ensure that at least one poll
1761 * interval has elapsed since the last **clean**
1762 * packet was received. We limit the check to
1763 * **clean** packets to prevent replayed packets
1764 * and incorrectly authenticated packets, which
1765 * we'll discard, from being used to create a
1766 * denial of service condition.
1768 deadband = (1u << pkt->ppoll);
1769 if (FLAG_BC_VOL & peer->flags)
1770 deadband -= 3; /* allow greater fuzz after volley */
1771 if ((current_time - peer->timereceived) < deadband) {
1772 msyslog(LOG_INFO, "receive: broadcast packet from %s arrived after %lu, not %lu seconds!",
1773 stoa(&rbufp->recv_srcadr),
1774 (current_time - peer->timereceived),
1779 /* Alert if time from the server is non-monotonic.
1781 * [Bug 3114] is about Broadcast mode replay DoS.
1783 * Broadcast mode *assumes* a trusted network.
1784 * Even so, it's nice to be robust in the face
1787 * If we get an authenticated broadcast packet
1788 * with an "earlier" timestamp, it means one of
1791 * - the broadcast server had a backward step.
1793 * - somebody is trying a replay attack.
1795 * deadband: By default, we assume the broadcast
1796 * network is trustable, so we take our accepted
1797 * broadcast packets as we receive them. But
1798 * some folks might want to take additional poll
1799 * delays before believing a backward step.
1801 if (sys_bcpollbstep) {
1802 /* pkt->ppoll or peer->ppoll ? */
1803 deadband = (1u << pkt->ppoll)
1804 * sys_bcpollbstep + 2;
1809 if (L_ISZERO(&peer->bxmt)) {
1810 tdiff.l_ui = tdiff.l_uf = 0;
1813 L_SUB(&tdiff, &peer->bxmt);
1816 && (current_time - peer->timereceived) < deadband)
1818 msyslog(LOG_INFO, "receive: broadcast packet from %s contains non-monotonic timestamp: %#010x.%08x -> %#010x.%08x",
1819 stoa(&rbufp->recv_srcadr),
1820 peer->bxmt.l_ui, peer->bxmt.l_uf,
1821 p_xmt.l_ui, p_xmt.l_uf
1827 DPRINTF(2, ("receive: AM_PROCPKT drop: bail\n"));
1828 peer->timelastrec = current_time;
1837 * A passive packet matches a passive association. This is
1838 * usually the result of reconfiguring a client on the fly. As
1839 * this association might be legitimate and this packet an
1840 * attempt to deny service, just ignore it.
1843 DPRINTF(2, ("receive: AM_ERR drop.\n"));
1848 * For everything else there is the bit bucket.
1851 DPRINTF(2, ("receive: default drop.\n"));
1858 * If the association is configured for Autokey, the packet must
1859 * have a public key ID; if not, the packet must have a
1862 if ( is_authentic != AUTH_CRYPTO
1863 && ( ((peer->flags & FLAG_SKEY) && skeyid <= NTP_MAXKEY)
1864 || (!(peer->flags & FLAG_SKEY) && skeyid > NTP_MAXKEY))) {
1865 DPRINTF(2, ("receive: drop: Autokey but wrong/bad auth\n"));
1869 #endif /* AUTOKEY */
1872 peer->flash &= ~PKT_TEST_MASK;
1873 if (peer->flags & FLAG_XBOGUS) {
1874 peer->flags &= ~FLAG_XBOGUS;
1875 peer->flash |= TEST3;
1879 * Next comes a rigorous schedule of timestamp checking. If the
1880 * transmit timestamp is zero, the server has not initialized in
1881 * interleaved modes or is horribly broken.
1883 * A KoD packet we pay attention to cannot have a 0 transmit
1887 kissCode = kiss_code_check(hisleap, hisstratum, hismode, pkt->refid);
1889 if (L_ISZERO(&p_xmt)) {
1890 peer->flash |= TEST3; /* unsynch */
1891 if (kissCode != NOKISS) { /* KoD packet */
1892 peer->bogusorg++; /* for TEST2 or TEST3 */
1894 "receive: Unexpected zero transmit timestamp in KoD from %s",
1895 ntoa(&peer->srcadr));
1900 * If the transmit timestamp duplicates our previous one, the
1901 * packet is a replay. This prevents the bad guys from replaying
1902 * the most recent packet, authenticated or not.
1904 } else if ( ((FLAG_LOOPNONCE & peer->flags) && L_ISEQU(&peer->nonce, &p_xmt))
1905 || (!(FLAG_LOOPNONCE & peer->flags) && L_ISEQU(&peer->xmt, &p_xmt))
1907 DPRINTF(2, ("receive: drop: Duplicate xmit\n"));
1908 peer->flash |= TEST1; /* duplicate */
1913 * If this is a broadcast mode packet, make sure hisstratum
1914 * is appropriate. Don't do anything else here - we wait to
1915 * see if this is an interleave broadcast packet until after
1916 * we've validated the MAC that SHOULD be provided.
1918 * hisstratum cannot be 0 - see assertion above.
1919 * If hisstratum is 15, then we'll advertise as UNSPEC but
1920 * at least we'll be able to sync with the broadcast server.
1922 } else if (hismode == MODE_BROADCAST) {
1923 /* 0 is unexpected too, and impossible */
1924 if (STRATUM_UNSPEC <= hisstratum) {
1925 /* Is this a ++sys_declined or ??? */
1927 "receive: Unexpected stratum (%d) in broadcast from %s",
1928 hisstratum, ntoa(&peer->srcadr));
1933 * Basic KoD validation checking:
1935 * KoD packets are a mixed-blessing. Forged KoD packets
1936 * are DoS attacks. There are rare situations where we might
1937 * get a valid KoD response, though. Since KoD packets are
1938 * a special case that complicate the checks we do next, we
1939 * handle the basic KoD checks here.
1941 * Note that we expect the incoming KoD packet to have its
1942 * (nonzero) org, rec, and xmt timestamps set to the xmt timestamp
1943 * that we have previously sent out. Watch interleave mode.
1945 } else if (kissCode != NOKISS) {
1946 DEBUG_INSIST(!L_ISZERO(&p_xmt));
1947 if ( L_ISZERO(&p_org) /* We checked p_xmt above */
1948 || L_ISZERO(&p_rec)) {
1951 "receive: KoD packet from %s has a zero org or rec timestamp. Ignoring.",
1952 ntoa(&peer->srcadr));
1956 if ( !L_ISEQU(&p_xmt, &p_org)
1957 || !L_ISEQU(&p_xmt, &p_rec)) {
1960 "receive: KoD packet from %s has inconsistent xmt/org/rec timestamps. Ignoring.",
1961 ntoa(&peer->srcadr));
1965 /* Be conservative */
1966 if (peer->flip == 0 && !L_ISEQU(&p_org, &peer->aorg)) {
1969 "receive: flip 0 KoD origin timestamp %#010x.%08x from %s does not match %#010x.%08x - ignoring.",
1970 p_org.l_ui, p_org.l_uf,
1971 ntoa(&peer->srcadr),
1972 peer->aorg.l_ui, peer->aorg.l_uf);
1974 } else if (peer->flip == 1 && !L_ISEQU(&p_org, &peer->borg)) {
1977 "receive: flip 1 KoD origin timestamp %#010x.%08x from %s does not match interleave %#010x.%08x - ignoring.",
1978 p_org.l_ui, p_org.l_uf,
1979 ntoa(&peer->srcadr),
1980 peer->borg.l_ui, peer->borg.l_uf);
1985 * Basic mode checks:
1987 * If there is no origin timestamp, it's either an initial packet
1988 * or we've already received a response to our query. Of course,
1989 * should 'aorg' be all-zero because this really was the original
1990 * transmit timestamp, we'll ignore this reply. There is a window
1991 * of one nanosecond once every 136 years' time where this is
1992 * possible. We currently ignore this situation, as a completely
1993 * zero timestamp is (quietly?) disallowed.
1995 * Otherwise, check for bogus packet in basic mode.
1996 * If it is bogus, switch to interleaved mode and resynchronize,
1997 * but only after confirming the packet is not bogus in
1998 * symmetric interleaved mode.
2000 * This could also mean somebody is forging packets claiming to
2001 * be from us, attempting to cause our server to KoD us.
2003 * We have earlier asserted that hisstratum cannot be 0.
2004 * If hisstratum is STRATUM_UNSPEC, it means he's not sync'd.
2007 /* XXX: FLAG_LOOPNONCE */
2008 DEBUG_INSIST(0 == (FLAG_LOOPNONCE & peer->flags));
2010 } else if (peer->flip == 0) {
2012 } else if (L_ISZERO(&p_org)) {
2017 "receive: BUG 3361: Clearing peer->aorg ");
2019 /* Clear peer->nonce, too? */
2023 /* We allow 0org for: */
2027 /* We disallow 0org for: */
2033 case MODE_BROADCAST:
2036 peer->flash |= TEST2; /* bogus */
2039 action = ""; /* for cranky compilers / MSVC */
2040 INSIST(!"receive(): impossible hismode");
2045 "receive: %s 0 origin timestamp from %s@%s xmt %#010x.%08x",
2046 action, hm_str, ntoa(&peer->srcadr),
2047 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
2048 } else if (!L_ISEQU(&p_org, &peer->aorg)) {
2049 /* are there cases here where we should bail? */
2050 /* Should we set TEST2 if we decide to try xleave? */
2052 peer->flash |= TEST2; /* bogus */
2054 "receive: Unexpected origin timestamp %#010x.%08x does not match aorg %#010x.%08x from %s@%s xmt %#010x.%08x",
2055 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
2056 peer->aorg.l_ui, peer->aorg.l_uf,
2057 hm_str, ntoa(&peer->srcadr),
2058 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
2059 if ( !L_ISZERO(&peer->dst)
2060 && L_ISEQU(&p_org, &peer->dst)) {
2061 /* Might be the start of an interleave */
2062 if (dynamic_interleave) {
2064 report_event(PEVNT_XLEAVE, peer, NULL);
2067 "receive: Dynamic interleave from %s@%s denied",
2068 hm_str, ntoa(&peer->srcadr));
2073 /* XXX: FLAG_LOOPNONCE */
2077 * Check for valid nonzero timestamp fields.
2079 } else if ( L_ISZERO(&p_org)
2081 || L_ISZERO(&peer->dst)) {
2082 peer->flash |= TEST3; /* unsynch */
2085 * Check for bogus packet in interleaved symmetric mode. This
2086 * can happen if a packet is lost, duplicated or crossed. If
2087 * found, flip and resynchronize.
2089 } else if ( !L_ISZERO(&peer->dst)
2090 && !L_ISEQU(&p_org, &peer->dst)) {
2091 DPRINTF(2, ("receive: drop: Bogus packet in interleaved symmetric mode\n"));
2093 peer->flags |= FLAG_XBOGUS;
2094 peer->flash |= TEST2; /* bogus */
2096 return; /* Bogus packet, we are done */
2103 * If this is a crypto_NAK, the server cannot authenticate a
2104 * client packet. The server might have just changed keys. Clear
2105 * the association and restart the protocol.
2107 if (crypto_nak_test == VALIDNAK) {
2108 report_event(PEVNT_AUTH, peer, "crypto_NAK");
2109 peer->flash |= TEST5; /* bad auth */
2111 if (peer->flags & FLAG_PREEMPT) {
2112 if (unpeer_crypto_nak_early) {
2115 DPRINTF(2, ("receive: drop: PREEMPT crypto_NAK\n"));
2120 peer_clear(peer, "AUTH");
2122 #endif /* AUTOKEY */
2123 DPRINTF(2, ("receive: drop: crypto_NAK\n"));
2127 * If the digest fails or it's missing for authenticated
2128 * associations, the client cannot authenticate a server
2129 * reply to a client packet previously sent. The loopback check
2130 * is designed to avoid a bait-and-switch attack, which was
2131 * possible in past versions. If symmetric modes, return a
2132 * crypto-NAK. The peer should restart the protocol.
2134 } else if (!AUTH(peer->keyid || has_mac ||
2135 (restrict_mask & RES_DONTTRUST), is_authentic)) {
2137 if (peer->flash & PKT_TEST_MASK) {
2139 "receive: Bad auth in packet with bad timestamps from %s denied - spoof?",
2140 ntoa(&peer->srcadr));
2144 report_event(PEVNT_AUTH, peer, "digest");
2145 peer->flash |= TEST5; /* bad auth */
2148 && ( hismode == MODE_ACTIVE
2149 || hismode == MODE_PASSIVE))
2150 fast_xmit(rbufp, MODE_ACTIVE, 0, restrict_mask);
2151 if (peer->flags & FLAG_PREEMPT) {
2152 if (unpeer_digest_early) {
2157 else if (peer_clear_digest_early && peer->crypto) {
2158 peer_clear(peer, "AUTH");
2160 #endif /* AUTOKEY */
2161 DPRINTF(2, ("receive: drop: Bad or missing AUTH\n"));
2166 * For broadcast packets:
2168 * HMS: This next line never made much sense to me, even
2169 * when it was up higher:
2170 * If an initial volley, bail out now and let the
2171 * client do its stuff.
2173 * If the packet has not failed authentication, then
2174 * - if the origin timestamp is nonzero this is an
2175 * interleaved broadcast, so restart the protocol.
2176 * - else, this is not an interleaved broadcast packet.
2178 if (hismode == MODE_BROADCAST) {
2179 if ( is_authentic == AUTH_OK
2180 || is_authentic == AUTH_NONE) {
2181 if (!L_ISZERO(&p_org)) {
2182 if (!(peer->flags & FLAG_XB)) {
2184 "receive: Broadcast server at %s is in interleave mode",
2185 ntoa(&peer->srcadr));
2186 peer->flags |= FLAG_XB;
2188 peer->borg = rbufp->recv_time;
2189 report_event(PEVNT_XLEAVE, peer, NULL);
2192 } else if (peer->flags & FLAG_XB) {
2194 "receive: Broadcast server at %s is no longer in interleave mode",
2195 ntoa(&peer->srcadr));
2196 peer->flags &= ~FLAG_XB;
2200 "receive: Bad broadcast auth (%d) from %s",
2201 is_authentic, ntoa(&peer->srcadr));
2205 * Now that we know the packet is correctly authenticated,
2206 * update peer->bxmt.
2213 ** Update the state variables.
2215 if (peer->flip == 0) {
2216 if (hismode != MODE_BROADCAST)
2218 peer->dst = rbufp->recv_time;
2223 * Set the peer ppoll to the maximum of the packet ppoll and the
2224 * peer minpoll. If a kiss-o'-death, set the peer minpoll to
2225 * this maximum and advance the headway to give the sender some
2226 * headroom. Very intricate.
2230 * Check for any kiss codes. Note this is only used when a server
2231 * responds to a packet request.
2235 * Check to see if this is a RATE Kiss Code
2236 * Currently this kiss code will accept whatever poll
2237 * rate that the server sends
2239 peer->ppoll = max(peer->minpoll, pkt->ppoll);
2240 if (kissCode == RATEKISS) {
2241 peer->selbroken++; /* Increment the KoD count */
2242 report_event(PEVNT_RATE, peer, NULL);
2243 if (pkt->ppoll > peer->minpoll)
2244 peer->minpoll = peer->ppoll;
2245 peer->burst = peer->retry = 0;
2246 peer->throttle = (NTP_SHIFT + 1) * (1 << peer->minpoll);
2247 poll_update(peer, pkt->ppoll, 0);
2248 return; /* kiss-o'-death */
2250 if (kissCode != NOKISS) {
2251 peer->selbroken++; /* Increment the KoD count */
2252 return; /* Drop any other kiss code packets */
2263 * - this is a *cast (uni-, broad-, or m-) server packet
2264 * - and it's symmetric-key authenticated
2265 * then see if the sender's IP is trusted for this keyid.
2266 * If it is, great - nothing special to do here.
2267 * Otherwise, we should report and bail.
2269 * Autokey-authenticated packets are accepted.
2273 case MODE_SERVER: /* server mode */
2274 case MODE_BROADCAST: /* broadcast mode */
2275 case MODE_ACTIVE: /* symmetric active mode */
2276 case MODE_PASSIVE: /* symmetric passive mode */
2277 if ( is_authentic == AUTH_OK
2279 && skeyid <= NTP_MAXKEY
2280 && !authistrustedip(skeyid, &peer->srcadr)) {
2281 report_event(PEVNT_AUTH, peer, "authIP");
2287 case MODE_CLIENT: /* client mode */
2288 #if 0 /* At this point, MODE_CONTROL is overloaded by MODE_BCLIENT */
2289 case MODE_CONTROL: /* control mode */
2291 case MODE_PRIVATE: /* private mode */
2292 case MODE_BCLIENT: /* broadcast client mode */
2295 case MODE_UNSPEC: /* unspecified (old version) */
2298 "receive: Unexpected mode (%d) in packet from %s",
2299 hismode, ntoa(&peer->srcadr));
2305 * That was hard and I am sweaty, but the packet is squeaky
2306 * clean. Get on with real work.
2308 peer->timereceived = current_time;
2309 peer->timelastrec = current_time;
2310 if (is_authentic == AUTH_OK)
2311 peer->flags |= FLAG_AUTHENTIC;
2313 peer->flags &= ~FLAG_AUTHENTIC;
2317 * More autokey dance. The rules of the cha-cha are as follows:
2319 * 1. If there is no key or the key is not auto, do nothing.
2321 * 2. If this packet is in response to the one just previously
2322 * sent or from a broadcast server, do the extension fields.
2323 * Otherwise, assume bogosity and bail out.
2325 * 3. If an extension field contains a verified signature, it is
2326 * self-authenticated and we sit the dance.
2328 * 4. If this is a server reply, check only to see that the
2329 * transmitted key ID matches the received key ID.
2331 * 5. Check to see that one or more hashes of the current key ID
2332 * matches the previous key ID or ultimate original key ID
2333 * obtained from the broadcaster or symmetric peer. If no
2334 * match, sit the dance and call for new autokey values.
2336 * In case of crypto error, fire the orchestra, stop dancing and
2337 * restart the protocol.
2339 if (peer->flags & FLAG_SKEY) {
2341 * Decrement remaining autokey hashes. This isn't
2342 * perfect if a packet is lost, but results in no harm.
2344 ap = (struct autokey *)peer->recval.ptr;
2349 peer->flash |= TEST8;
2350 rval = crypto_recv(peer, rbufp);
2351 if (rval == XEVNT_OK) {
2354 if (rval == XEVNT_ERR) {
2355 report_event(PEVNT_RESTART, peer,
2357 peer_clear(peer, "CRYP");
2358 peer->flash |= TEST9; /* bad crypt */
2359 if (peer->flags & FLAG_PREEMPT) {
2360 if (unpeer_crypto_early) {
2369 * If server mode, verify the receive key ID matches
2370 * the transmit key ID.
2372 if (hismode == MODE_SERVER) {
2373 if (skeyid == peer->keyid)
2374 peer->flash &= ~TEST8;
2377 * If an extension field is present, verify only that it
2378 * has been correctly signed. We don't need a sequence
2379 * check here, but the sequence continues.
2381 } else if (!(peer->flash & TEST8)) {
2382 peer->pkeyid = skeyid;
2385 * Now the fun part. Here, skeyid is the current ID in
2386 * the packet, pkeyid is the ID in the last packet and
2387 * tkeyid is the hash of skeyid. If the autokey values
2388 * have not been received, this is an automatic error.
2389 * If so, check that the tkeyid matches pkeyid. If not,
2390 * hash tkeyid and try again. If the number of hashes
2391 * exceeds the number remaining in the sequence, declare
2392 * a successful failure and refresh the autokey values.
2394 } else if (ap != NULL) {
2397 for (i = 0; ; i++) {
2398 if ( tkeyid == peer->pkeyid
2399 || tkeyid == ap->key) {
2400 peer->flash &= ~TEST8;
2401 peer->pkeyid = skeyid;
2410 tkeyid = session_key(
2411 &rbufp->recv_srcadr, dstadr_sin,
2414 if (peer->flash & TEST8)
2415 report_event(PEVNT_AUTH, peer, "keylist");
2417 if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
2418 peer->flash |= TEST8; /* bad autokey */
2421 * The maximum lifetime of the protocol is about one
2422 * week before restarting the Autokey protocol to
2423 * refresh certificates and leapseconds values.
2425 if (current_time > peer->refresh) {
2426 report_event(PEVNT_RESTART, peer,
2428 peer_clear(peer, "TIME");
2432 #endif /* AUTOKEY */
2435 * The dance is complete and the flash bits have been lit. Toss
2436 * the packet over the fence for processing, which may light up
2437 * more flashers. Leave if the packet is not good.
2439 process_packet(peer, pkt, rbufp->recv_length);
2440 if (peer->flash & PKT_TEST_MASK)
2443 /* [bug 3592] Update poll. Ideally this should not happen in a
2444 * receive branch, but too much is going on here... at least we
2445 * do it only if the packet was good!
2447 poll_update(peer, peer->hpoll, (peer->hmode == MODE_CLIENT));
2450 * In interleaved mode update the state variables. Also adjust the
2451 * transmit phase to avoid crossover.
2453 if (peer->flip != 0) {
2455 peer->dst = rbufp->recv_time;
2456 if (peer->nextdate - current_time < (1U << min(peer->ppoll,
2466 * process_packet - Packet Procedure, a la Section 3.4.4 of RFC-1305
2467 * Or almost, at least. If we're in here we have a reasonable
2468 * expectation that we will be having a long term
2469 * relationship with this host.
2473 register struct peer *peer,
2474 register struct pkt *pkt,
2479 double p_offset, p_del, p_disp;
2480 l_fp p_rec, p_xmt, p_org, p_reftime, ci;
2481 u_char pmode, pleap, pversion, pstratum;
2482 char statstr[NTP_MAXSTRLEN];
2485 double etemp, ftemp, td;
2492 p_del = FPTOD(NTOHS_FP(pkt->rootdelay));
2494 p_disp = FPTOD(NTOHS_FP(pkt->rootdisp));
2495 NTOHL_FP(&pkt->reftime, &p_reftime);
2496 NTOHL_FP(&pkt->org, &p_org);
2497 NTOHL_FP(&pkt->rec, &p_rec);
2498 NTOHL_FP(&pkt->xmt, &p_xmt);
2499 pmode = PKT_MODE(pkt->li_vn_mode);
2500 pleap = PKT_LEAP(pkt->li_vn_mode);
2501 pversion = PKT_VERSION(pkt->li_vn_mode);
2502 pstratum = PKT_TO_STRATUM(pkt->stratum);
2509 * Verify the server is synchronized; that is, the leap bits,
2510 * stratum and root distance are valid.
2512 if ( pleap == LEAP_NOTINSYNC /* test 6 */
2513 || pstratum < sys_floor || pstratum >= sys_ceiling)
2514 peer->flash |= TEST6; /* bad synch or strat */
2515 if (p_del / 2 + p_disp >= MAXDISPERSE) /* test 7 */
2516 peer->flash |= TEST7; /* bad header */
2519 * If any tests fail at this point, the packet is discarded.
2520 * Note that some flashers may have already been set in the
2521 * receive() routine.
2523 if (peer->flash & PKT_TEST_MASK) {
2524 peer->seldisptoolarge++;
2525 DPRINTF(1, ("packet: flash header %04x\n",
2528 /* ppoll updated? */
2529 /* XXX: Fuzz the poll? */
2530 poll_update(peer, peer->hpoll, (peer->hmode == MODE_CLIENT));
2542 * Capture the header values in the client/peer association..
2544 record_raw_stats(&peer->srcadr,
2545 peer->dstadr ? &peer->dstadr->sin : NULL,
2546 &p_org, &p_rec, &p_xmt, &peer->dst,
2547 pleap, pversion, pmode, pstratum, pkt->ppoll, pkt->precision,
2548 p_del, p_disp, pkt->refid,
2549 len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
2551 peer->stratum = min(pstratum, STRATUM_UNSPEC);
2552 peer->pmode = pmode;
2553 peer->precision = pkt->precision;
2554 peer->rootdelay = p_del;
2555 peer->rootdisp = p_disp;
2556 peer->refid = pkt->refid; /* network byte order */
2557 peer->reftime = p_reftime;
2560 * First, if either burst mode is armed, enable the burst.
2561 * Compute the headway for the next packet and delay if
2562 * necessary to avoid exceeding the threshold.
2564 if (peer->retry > 0) {
2567 peer->burst = min(1 << (peer->hpoll -
2568 peer->minpoll), NTP_SHIFT) - 1;
2570 peer->burst = NTP_IBURST - 1;
2571 if (peer->burst > 0)
2572 peer->nextdate = current_time;
2574 poll_update(peer, peer->hpoll, (peer->hmode == MODE_CLIENT));
2579 * If the peer was previously unreachable, raise a trap. In any
2580 * case, mark it reachable.
2583 report_event(PEVNT_REACH, peer, NULL);
2584 peer->timereachable = current_time;
2589 * For a client/server association, calculate the clock offset,
2590 * roundtrip delay and dispersion. The equations are reordered
2591 * from the spec for more efficient use of temporaries. For a
2592 * broadcast association, offset the last measurement by the
2593 * computed delay during the client/server volley. Note the
2594 * computation of dispersion includes the system precision plus
2595 * that due to the frequency error since the origin time.
2597 * It is very important to respect the hazards of overflow. The
2598 * only permitted operation on raw timestamps is subtraction,
2599 * where the result is a signed quantity spanning from 68 years
2600 * in the past to 68 years in the future. To avoid loss of
2601 * precision, these calculations are done using 64-bit integer
2602 * arithmetic. However, the offset and delay calculations are
2603 * sums and differences of these first-order differences, which
2604 * if done using 64-bit integer arithmetic, would be valid over
2605 * only half that span. Since the typical first-order
2606 * differences are usually very small, they are converted to 64-
2607 * bit doubles and all remaining calculations done in floating-
2608 * double arithmetic. This preserves the accuracy while
2609 * retaining the 68-year span.
2611 * There are three interleaving schemes, basic, interleaved
2612 * symmetric and interleaved broadcast. The timestamps are
2613 * idioscyncratically different. See the onwire briefing/white
2614 * paper at www.eecis.udel.edu/~mills for details.
2616 * Interleaved symmetric mode
2617 * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
2620 if (peer->flip != 0) {
2621 ci = p_xmt; /* t3 - t4 */
2622 L_SUB(&ci, &peer->dst);
2624 ci = p_rec; /* t2 - t1 */
2626 L_SUB(&ci, &peer->borg);
2628 L_SUB(&ci, &peer->aorg);
2631 p_offset = (t21 + t34) / 2.;
2632 if (p_del < 0 || p_del > 1.) {
2633 snprintf(statstr, sizeof(statstr),
2634 "t21 %.6f t34 %.6f", t21, t34);
2635 report_event(PEVNT_XERR, peer, statstr);
2642 } else if (peer->pmode == MODE_BROADCAST) {
2645 * Interleaved broadcast mode. Use interleaved timestamps.
2646 * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
2648 if (peer->flags & FLAG_XB) {
2649 ci = p_org; /* delay */
2650 L_SUB(&ci, &peer->aorg);
2652 ci = p_org; /* t2 - t1 */
2653 L_SUB(&ci, &peer->borg);
2656 peer->borg = peer->dst;
2657 if (t34 < 0 || t34 > 1.) {
2658 /* drop all if in the initial volley */
2659 if (FLAG_BC_VOL & peer->flags)
2660 goto bcc_init_volley_fail;
2661 snprintf(statstr, sizeof(statstr),
2662 "offset %.6f delay %.6f", t21, t34);
2663 report_event(PEVNT_XERR, peer, statstr);
2670 * Basic broadcast - use direct timestamps.
2671 * t3 = p_xmt, t4 = peer->dst
2674 ci = p_xmt; /* t3 - t4 */
2675 L_SUB(&ci, &peer->dst);
2681 * When calibration is complete and the clock is
2682 * synchronized, the bias is calculated as the difference
2683 * between the unicast timestamp and the broadcast
2684 * timestamp. This works for both basic and interleaved
2686 * [Bug 3031] Don't keep this peer when the delay
2687 * calculation gives reason to suspect clock steps.
2688 * This is assumed for delays > 50ms.
2690 if (FLAG_BC_VOL & peer->flags) {
2691 peer->flags &= ~FLAG_BC_VOL;
2692 peer->delay = fabs(peer->offset - p_offset) * 2;
2693 DPRINTF(2, ("broadcast volley: initial delay=%.6f\n",
2695 if (peer->delay > fabs(sys_bdelay)) {
2696 bcc_init_volley_fail:
2697 DPRINTF(2, ("%s", "broadcast volley: initial delay exceeds limit\n"));
2702 peer->nextdate = current_time + (1u << peer->ppoll) - 2u;
2703 p_del = peer->delay;
2704 p_offset += p_del / 2;
2708 * Basic mode, otherwise known as the old fashioned way.
2710 * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2713 ci = p_xmt; /* t3 - t4 */
2714 L_SUB(&ci, &peer->dst);
2716 ci = p_rec; /* t2 - t1 */
2719 p_del = fabs(t21 - t34);
2720 p_offset = (t21 + t34) / 2.;
2722 p_del = max(p_del, LOGTOD(sys_precision));
2723 p_disp = LOGTOD(sys_precision) + LOGTOD(peer->precision) +
2728 * This code calculates the outbound and inbound data rates by
2729 * measuring the differences between timestamps at different
2730 * packet lengths. This is helpful in cases of large asymmetric
2731 * delays commonly experienced on deep space communication
2734 if (peer->t21_last > 0 && peer->t34_bytes > 0) {
2735 itemp = peer->t21_bytes - peer->t21_last;
2737 etemp = t21 - peer->t21;
2738 if (fabs(etemp) > 1e-6) {
2739 ftemp = itemp / etemp;
2744 itemp = len - peer->t34_bytes;
2746 etemp = -t34 - peer->t34;
2747 if (fabs(etemp) > 1e-6) {
2748 ftemp = itemp / etemp;
2756 * The following section compensates for different data rates on
2757 * the outbound (d21) and inbound (t34) directions. To do this,
2758 * it finds t such that r21 * t - r34 * (d - t) = 0, where d is
2759 * the roundtrip delay. Then it calculates the correction as a
2763 peer->t21_last = peer->t21_bytes;
2765 peer->t34_bytes = len;
2766 DPRINTF(2, ("packet: t21 %.9lf %d t34 %.9lf %d\n", peer->t21,
2767 peer->t21_bytes, peer->t34, peer->t34_bytes));
2768 if (peer->r21 > 0 && peer->r34 > 0 && p_del > 0) {
2769 if (peer->pmode != MODE_BROADCAST)
2770 td = (peer->r34 / (peer->r21 + peer->r34) -
2776 * Unfortunately, in many cases the errors are
2777 * unacceptable, so for the present the rates are not
2778 * used. In future, we might find conditions where the
2779 * calculations are useful, so this should be considered
2780 * a work in progress.
2784 DPRINTF(2, ("packet: del %.6lf r21 %.1lf r34 %.1lf %.6lf\n",
2785 p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2791 * That was awesome. Now hand off to the clock filter.
2793 clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2796 * If we are in broadcast calibrate mode, return to broadcast
2797 * client mode when the client is fit and the autokey dance is
2800 if ( (FLAG_BC_VOL & peer->flags)
2801 && MODE_CLIENT == peer->hmode
2802 && !(TEST11 & peer_unfit(peer))) { /* distance exceeded */
2804 if (peer->flags & FLAG_SKEY) {
2805 if (!(~peer->crypto & CRYPTO_FLAG_ALL))
2806 peer->hmode = MODE_BCLIENT;
2808 peer->hmode = MODE_BCLIENT;
2810 #else /* !AUTOKEY follows */
2811 peer->hmode = MODE_BCLIENT;
2812 #endif /* !AUTOKEY */
2818 * clock_update - Called at system process update intervals.
2822 struct peer *peer /* peer structure pointer */
2827 #ifdef HAVE_LIBSCF_H
2829 #endif /* HAVE_LIBSCF_H */
2832 * Update the system state variables. We do this very carefully,
2833 * as the poll interval might need to be clamped differently.
2836 sys_epoch = peer->epoch;
2837 if (sys_poll < peer->minpoll)
2838 sys_poll = peer->minpoll;
2839 if (sys_poll > peer->maxpoll)
2840 sys_poll = peer->maxpoll;
2841 poll_update(peer, sys_poll, 0);
2842 sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
2843 if ( peer->stratum == STRATUM_REFCLOCK
2844 || peer->stratum == STRATUM_UNSPEC)
2845 sys_refid = peer->refid;
2847 sys_refid = addr2refid(&peer->srcadr);
2849 * Root Dispersion (E) is defined (in RFC 5905) as:
2851 * E = p.epsilon_r + p.epsilon + p.psi + PHI*(s.t - p.t) + |THETA|
2854 * p.epsilon_r is the PollProc's root dispersion
2855 * p.epsilon is the PollProc's dispersion
2856 * p.psi is the PollProc's jitter
2857 * THETA is the combined offset
2859 * NB: Think Hard about where these numbers come from and
2860 * what they mean. When did peer->update happen? Has anything
2861 * interesting happened since then? What values are the most
2864 * DLM thinks this equation is probably the best of all worse choices.
2866 dtemp = peer->rootdisp
2869 + clock_phi * (current_time - peer->update)
2872 p2_rootdisp = prev_rootdisp;
2873 prev_rootdisp = sys_rootdisp;
2874 if (dtemp > sys_mindisp)
2875 sys_rootdisp = dtemp;
2877 sys_rootdisp = sys_mindisp;
2879 sys_rootdelay = peer->delay + peer->rootdelay;
2881 p2_reftime = prev_reftime;
2882 p2_time = prev_time;
2884 prev_reftime = sys_reftime;
2885 prev_time = current_time + 64 + (rand() & 0x3f); /* 64-127 s */
2887 sys_reftime = peer->dst;
2889 DPRINTF(1, ("clock_update: at %lu sample %lu associd %d\n",
2890 current_time, peer->epoch, peer->associd));
2893 * Comes now the moment of truth. Crank the clock discipline and
2894 * see what comes out.
2896 switch (local_clock(peer, sys_offset)) {
2899 * Clock exceeds panic threshold. Life as we know it ends.
2902 #ifdef HAVE_LIBSCF_H
2904 * For Solaris enter the maintenance mode.
2906 if ((fmri = getenv("SMF_FMRI")) != NULL) {
2907 if (smf_maintain_instance(fmri, 0) < 0) {
2908 printf("smf_maintain_instance: %s\n",
2909 scf_strerror(scf_error()));
2913 * Sleep until SMF kills us.
2918 #endif /* HAVE_LIBSCF_H */
2923 * Clock was stepped. Flush all time values of all peers.
2927 set_sys_leap(LEAP_NOTINSYNC);
2928 sys_stratum = STRATUM_UNSPEC;
2929 memcpy(&sys_refid, "STEP", 4);
2934 L_CLR(&p2_reftime); /* Should we clear p2_reftime? */
2935 L_CLR(&prev_reftime); /* Should we clear prev_reftime? */
2936 L_CLR(&sys_reftime);
2937 sys_jitter = LOGTOD(sys_precision);
2938 leapsec_reset_frame();
2942 * Clock was slewed. Handle the leapsecond stuff.
2947 * If this is the first time the clock is set, reset the
2948 * leap bits. If crypto, the timer will goose the setup
2951 if (sys_leap == LEAP_NOTINSYNC) {
2952 set_sys_leap(LEAP_NOWARNING);
2956 #endif /* AUTOKEY */
2958 * If our parent process is waiting for the
2959 * first clock sync, send them home satisfied.
2961 #ifdef HAVE_WORKING_FORK
2962 if (daemon_pipe[1] != -1) {
2963 write(daemon_pipe[1], "S\n", 2);
2964 close(daemon_pipe[1]);
2965 daemon_pipe[1] = -1;
2966 DPRINTF(1, ("notified parent --wait-sync is done\n"));
2968 #endif /* HAVE_WORKING_FORK */
2973 * If there is no leap second pending and the number of
2974 * survivor leap bits is greater than half the number of
2975 * survivors, try to schedule a leap for the end of the
2976 * current month. (This only works if no leap second for
2977 * that range is in the table, so doing this more than
2978 * once is mostly harmless.)
2980 if (leapsec == LSPROX_NOWARN) {
2981 if ( leap_vote_ins > leap_vote_del
2982 && leap_vote_ins > sys_survivors / 2) {
2984 leapsec_add_dyn(TRUE, now.l_ui, NULL);
2986 if ( leap_vote_del > leap_vote_ins
2987 && leap_vote_del > sys_survivors / 2) {
2989 leapsec_add_dyn(FALSE, now.l_ui, NULL);
2995 * Popcorn spike or step threshold exceeded. Pretend it never
3005 * poll_update - update peer poll interval
3009 struct peer *peer, /* peer structure pointer */
3014 u_long next, utemp, limit;
3018 * This routine figures out when the next poll should be sent.
3019 * That turns out to be wickedly complicated. One problem is
3020 * that sometimes the time for the next poll is in the past when
3021 * the poll interval is reduced. We watch out for races here
3022 * between the receive process and the poll process.
3024 * Clamp the poll interval between minpoll and maxpoll.
3026 hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
3030 * If during the crypto protocol the poll interval has changed,
3031 * the lifetimes in the key list are probably bogus. Purge the
3032 * the key list and regenerate it later.
3034 if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
3036 #endif /* AUTOKEY */
3037 peer->hpoll = hpoll;
3040 * There are three variables important for poll scheduling, the
3041 * current time (current_time), next scheduled time (nextdate)
3042 * and the earliest time (utemp). The earliest time is 2 s
3043 * seconds, but could be more due to rate management. When
3044 * sending in a burst, use the earliest time. When not in a
3045 * burst but with a reply pending, send at the earliest time
3046 * unless the next scheduled time has not advanced. This can
3047 * only happen if multiple replies are pending in the same
3048 * response interval. Otherwise, send at the later of the next
3049 * scheduled time and the earliest time.
3051 * Now we figure out if there is an override. If a burst is in
3052 * progress and we get called from the receive process, just
3053 * slink away. If called from the poll process, delay 1 s for a
3054 * reference clock, otherwise 2 s.
3056 utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
3057 (1 << peer->minpoll), ntp_minpkt);
3059 /*[Bug 3592] avoid unlimited postpone of next poll */
3060 limit = (2u << hpoll);
3062 limit -= (limit >> 2);
3063 limit += peer->outdate;
3064 if (limit < current_time)
3065 limit = current_time;
3067 if (peer->burst > 0) {
3068 if (peer->nextdate > current_time)
3071 else if (peer->flags & FLAG_REFCLOCK)
3072 peer->nextdate = current_time + RESP_DELAY;
3073 #endif /* REFCLOCK */
3075 peer->nextdate = utemp;
3079 * If a burst is not in progress and a crypto response message
3080 * is pending, delay 2 s, but only if this is a new interval.
3082 } else if (peer->cmmd != NULL) {
3083 if (peer->nextdate > current_time) {
3084 if (peer->nextdate + ntp_minpkt != utemp)
3085 peer->nextdate = utemp;
3087 peer->nextdate = utemp;
3089 #endif /* AUTOKEY */
3092 * The ordinary case. If a retry, use minpoll; if unreachable,
3093 * use host poll; otherwise, use the minimum of host and peer
3094 * polls; In other words, oversampling is okay but
3095 * understampling is evil. Use the maximum of this value and the
3096 * headway. If the average headway is greater than the headway
3097 * threshold, increase the headway by the minimum interval.
3100 if (peer->retry > 0)
3101 hpoll = peer->minpoll;
3103 hpoll = min(peer->ppoll, peer->hpoll);
3105 if (peer->flags & FLAG_REFCLOCK)
3108 #endif /* REFCLOCK */
3109 next = ((0x1000UL | (ntp_random() & 0x0ff)) <<
3111 next += peer->outdate;
3112 /* XXX: bug3596: Deal with poll skew list? */
3116 if (0 == get_pollskew(hpoll, &psi)) {
3125 val = ntp_random() & msk;
3126 } while (val > qty);
3132 /* get_pollskew() already logged this */
3136 peer->nextdate = next;
3138 peer->nextdate = utemp;
3139 if (peer->throttle > (1 << peer->minpoll))
3140 peer->nextdate += ntp_minpkt;
3143 /*[Bug 3592] avoid unlimited postpone of next poll */
3144 if (peer->nextdate > limit) {
3145 DPRINTF(1, ("poll_update: clamp reached; limit %lu next %lu\n",
3146 limit, peer->nextdate));
3147 peer->nextdate = limit;
3149 DPRINTF(2, ("poll_update: at %lu %s poll %d burst %d retry %d head %d early %lu next %lu\n",
3150 current_time, ntoa(&peer->srcadr), peer->hpoll,
3151 peer->burst, peer->retry, peer->throttle,
3152 utemp - current_time, peer->nextdate -
3158 * peer_clear - clear peer filter registers. See Section 3.4.8 of the
3163 struct peer *peer, /* peer structure */
3164 const char *ident /* tally lights */
3168 l_fp bxmt = peer->bxmt; /* bcast clients retain this! */
3172 * If cryptographic credentials have been acquired, toss them to
3173 * Valhalla. Note that autokeys are ephemeral, in that they are
3174 * tossed immediately upon use. Therefore, the keylist can be
3175 * purged anytime without needing to preserve random keys. Note
3176 * that, if the peer is purged, the cryptographic variables are
3177 * purged, too. This makes it much harder to sneak in some
3178 * unauthenticated data in the clock filter.
3181 if (peer->iffval != NULL)
3182 BN_free(peer->iffval);
3183 value_free(&peer->cookval);
3184 value_free(&peer->recval);
3185 value_free(&peer->encrypt);
3186 value_free(&peer->sndval);
3187 if (peer->cmmd != NULL)
3189 if (peer->subject != NULL)
3190 free(peer->subject);
3191 if (peer->issuer != NULL)
3193 #endif /* AUTOKEY */
3196 * Clear all values, including the optional crypto values above.
3198 memset(CLEAR_TO_ZERO(peer), 0, LEN_CLEAR_TO_ZERO(peer));
3199 peer->ppoll = peer->maxpoll;
3200 peer->hpoll = peer->minpoll;
3201 peer->disp = MAXDISPERSE;
3202 peer->flash = peer_unfit(peer);
3203 peer->jitter = LOGTOD(sys_precision);
3205 /* Don't throw away our broadcast replay protection */
3206 if (peer->hmode == MODE_BCLIENT)
3210 * If interleave mode, initialize the alternate origin switch.
3212 if (peer->flags & FLAG_XLEAVE)
3214 for (u = 0; u < NTP_SHIFT; u++) {
3215 peer->filter_order[u] = u;
3216 peer->filter_disp[u] = MAXDISPERSE;
3219 if (!(peer->flags & FLAG_REFCLOCK)) {
3221 peer->leap = LEAP_NOTINSYNC;
3222 peer->stratum = STRATUM_UNSPEC;
3223 memcpy(&peer->refid, ident, 4);
3226 /* Clear refclock sample filter */
3227 peer->procptr->codeproc = 0;
3228 peer->procptr->coderecv = 0;
3233 * During initialization use the association count to spread out
3234 * the polls at one-second intervals. Passive associations'
3235 * first poll is delayed by the "discard minimum" to avoid rate
3236 * limiting. Other post-startup new or cleared associations
3237 * randomize the first poll over the minimum poll interval to
3240 peer->nextdate = peer->update = peer->outdate = current_time;
3242 peer->nextdate += peer_associations;
3243 } else if (MODE_PASSIVE == peer->hmode) {
3244 peer->nextdate += ntp_minpkt;
3246 peer->nextdate += ntp_random() % peer->minpoll;
3249 peer->refresh = current_time + (1 << NTP_REFRESH);
3250 #endif /* AUTOKEY */
3251 DPRINTF(1, ("peer_clear: at %ld next %ld associd %d refid %s\n",
3252 current_time, peer->nextdate, peer->associd,
3258 * clock_filter - add incoming clock sample to filter register and run
3259 * the filter procedure to find the best sample.
3263 struct peer *peer, /* peer structure pointer */
3264 double sample_offset, /* clock offset */
3265 double sample_delay, /* roundtrip delay */
3266 double sample_disp /* dispersion */
3269 double dst[NTP_SHIFT]; /* distance vector */
3270 int ord[NTP_SHIFT]; /* index vector */
3272 double dtemp, etemp;
3276 * A sample consists of the offset, delay, dispersion and epoch
3277 * of arrival. The offset and delay are determined by the on-
3278 * wire protocol. The dispersion grows from the last outbound
3279 * packet to the arrival of this one increased by the sum of the
3280 * peer precision and the system precision as required by the
3281 * error budget. First, shift the new arrival into the shift
3282 * register discarding the oldest one.
3284 j = peer->filter_nextpt;
3285 peer->filter_offset[j] = sample_offset;
3286 peer->filter_delay[j] = sample_delay;
3287 peer->filter_disp[j] = sample_disp;
3288 peer->filter_epoch[j] = current_time;
3289 j = (j + 1) % NTP_SHIFT;
3290 peer->filter_nextpt = j;
3293 * Update dispersions since the last update and at the same
3294 * time initialize the distance and index lists. Since samples
3295 * become increasingly uncorrelated beyond the Allan intercept,
3296 * only under exceptional cases will an older sample be used.
3297 * Therefore, the distance list uses a compound metric. If the
3298 * dispersion is greater than the maximum dispersion, clamp the
3299 * distance at that value. If the time since the last update is
3300 * less than the Allan intercept use the delay; otherwise, use
3301 * the sum of the delay and dispersion.
3303 dtemp = clock_phi * (current_time - peer->update);
3304 peer->update = current_time;
3305 for (i = NTP_SHIFT - 1; i >= 0; i--) {
3307 peer->filter_disp[j] += dtemp;
3308 if (peer->filter_disp[j] >= MAXDISPERSE) {
3309 peer->filter_disp[j] = MAXDISPERSE;
3310 dst[i] = MAXDISPERSE;
3311 } else if (peer->update - peer->filter_epoch[j] >
3312 (u_long)ULOGTOD(allan_xpt)) {
3313 dst[i] = peer->filter_delay[j] +
3314 peer->filter_disp[j];
3316 dst[i] = peer->filter_delay[j];
3319 j = (j + 1) % NTP_SHIFT;
3323 * If the clock has stabilized, sort the samples by distance.
3325 if (freq_cnt == 0) {
3326 for (i = 1; i < NTP_SHIFT; i++) {
3327 for (j = 0; j < i; j++) {
3328 if (dst[j] > dst[i]) {
3341 * Copy the index list to the association structure so ntpq
3342 * can see it later. Prune the distance list to leave only
3343 * samples less than the maximum dispersion, which disfavors
3344 * uncorrelated samples older than the Allan intercept. To
3345 * further improve the jitter estimate, of the remainder leave
3346 * only samples less than the maximum distance, but keep at
3347 * least two samples for jitter calculation.
3350 for (i = 0; i < NTP_SHIFT; i++) {
3351 peer->filter_order[i] = (u_char) ord[i];
3352 if ( dst[i] >= MAXDISPERSE
3353 || (m >= 2 && dst[i] >= sys_maxdist))
3359 * Compute the dispersion and jitter. The dispersion is weighted
3360 * exponentially by NTP_FWEIGHT (0.5) so it is normalized close
3361 * to 1.0. The jitter is the RMS differences relative to the
3362 * lowest delay sample.
3364 peer->disp = peer->jitter = 0;
3366 for (i = NTP_SHIFT - 1; i >= 0; i--) {
3368 peer->disp = NTP_FWEIGHT * (peer->disp +
3369 peer->filter_disp[j]);
3371 peer->jitter += DIFF(peer->filter_offset[j],
3372 peer->filter_offset[k]);
3376 * If no acceptable samples remain in the shift register,
3377 * quietly tiptoe home leaving only the dispersion. Otherwise,
3378 * save the offset, delay and jitter. Note the jitter must not
3379 * be less than the precision.
3385 etemp = fabs(peer->offset - peer->filter_offset[k]);
3386 peer->offset = peer->filter_offset[k];
3387 peer->delay = peer->filter_delay[k];
3389 peer->jitter /= m - 1;
3390 peer->jitter = max(SQRT(peer->jitter), LOGTOD(sys_precision));
3393 * If the the new sample and the current sample are both valid
3394 * and the difference between their offsets exceeds CLOCK_SGATE
3395 * (3) times the jitter and the interval between them is less
3396 * than twice the host poll interval, consider the new sample
3397 * a popcorn spike and ignore it.
3399 if ( peer->disp < sys_maxdist
3400 && peer->filter_disp[k] < sys_maxdist
3401 && etemp > CLOCK_SGATE * peer->jitter
3402 && peer->filter_epoch[k] - peer->epoch
3403 < 2. * ULOGTOD(peer->hpoll)) {
3404 snprintf(tbuf, sizeof(tbuf), "%.6f s", etemp);
3405 report_event(PEVNT_POPCORN, peer, tbuf);
3410 * A new minimum sample is useful only if it is later than the
3411 * last one used. In this design the maximum lifetime of any
3412 * sample is not greater than eight times the poll interval, so
3413 * the maximum interval between minimum samples is eight
3416 if (peer->filter_epoch[k] <= peer->epoch) {
3417 DPRINTF(2, ("clock_filter: old sample %lu\n", current_time -
3418 peer->filter_epoch[k]));
3421 peer->epoch = peer->filter_epoch[k];
3424 * The mitigated sample statistics are saved for later
3425 * processing. If not synchronized or not in a burst, tickle the
3426 * clock select algorithm.
3428 record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
3429 peer->offset, peer->delay, peer->disp, peer->jitter);
3430 DPRINTF(1, ("clock_filter: n %d off %.6f del %.6f dsp %.6f jit %.6f\n",
3431 m, peer->offset, peer->delay, peer->disp,
3433 if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC)
3439 * clock_select - find the pick-of-the-litter clock
3441 * LOCKCLOCK: (1) If the local clock is the prefer peer, it will always
3442 * be enabled, even if declared falseticker, (2) only the prefer peer
3443 * can be selected as the system peer, (3) if the external source is
3444 * down, the system leap bits are set to 11 and the stratum set to
3458 double orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
3459 struct endpoint endp;
3460 struct peer *osys_peer;
3461 struct peer *sys_prefer = NULL; /* prefer peer */
3462 struct peer *typesystem = NULL;
3463 struct peer *typeorphan = NULL;
3465 struct peer *typeacts = NULL;
3466 struct peer *typelocal = NULL;
3467 struct peer *typepps = NULL;
3468 #endif /* REFCLOCK */
3469 static struct endpoint *endpoint = NULL;
3470 static int *indx = NULL;
3471 static peer_select *peers = NULL;
3472 static u_int endpoint_size = 0;
3473 static u_int peers_size = 0;
3474 static u_int indx_size = 0;
3478 * Initialize and create endpoint, index and peer lists big
3479 * enough to handle all associations.
3481 osys_peer = sys_peer;
3484 set_sys_leap(LEAP_NOTINSYNC);
3485 sys_stratum = STRATUM_UNSPEC;
3486 memcpy(&sys_refid, "DOWN", 4);
3487 #endif /* LOCKCLOCK */
3490 * Allocate dynamic space depending on the number of
3494 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3496 endpoint_size = ALIGNED_SIZE(nlist * 2 * sizeof(*endpoint));
3497 peers_size = ALIGNED_SIZE(nlist * sizeof(*peers));
3498 indx_size = ALIGNED_SIZE(nlist * 2 * sizeof(*indx));
3499 octets = endpoint_size + peers_size + indx_size;
3500 endpoint = erealloc(endpoint, octets);
3501 peers = INC_ALIGNED_PTR(endpoint, endpoint_size);
3502 indx = INC_ALIGNED_PTR(peers, peers_size);
3505 * Initially, we populate the island with all the rifraff peers
3506 * that happen to be lying around. Those with seriously
3507 * defective clocks are immediately booted off the island. Then,
3508 * the falsetickers are culled and put to sea. The truechimers
3509 * remaining are subject to repeated rounds where the most
3510 * unpopular at each round is kicked off. When the population
3511 * has dwindled to sys_minclock, the survivors split a million
3512 * bucks and collectively crank the chimes.
3514 nlist = nl2 = 0; /* none yet */
3515 for (peer = peer_list; peer != NULL; peer = peer->p_link) {
3516 peer->new_status = CTL_PST_SEL_REJECT;
3519 * Leave the island immediately if the peer is
3520 * unfit to synchronize.
3522 if (peer_unfit(peer)) {
3527 * If this peer is an orphan parent, elect the
3528 * one with the lowest metric defined as the
3529 * IPv4 address or the first 64 bits of the
3530 * hashed IPv6 address. To ensure convergence
3531 * on the same selected orphan, consider as
3532 * well that this system may have the lowest
3533 * metric and be the orphan parent. If this
3534 * system wins, sys_peer will be NULL to trigger
3535 * orphan mode in timer().
3537 if (peer->stratum == sys_orphan) {
3541 if (peer->dstadr != NULL)
3542 localmet = ntohl(peer->dstadr->addr_refid);
3544 localmet = U_INT32_MAX;
3545 peermet = ntohl(addr2refid(&peer->srcadr));
3546 if (peermet < localmet && peermet < orphmet) {
3554 * If this peer could have the orphan parent
3555 * as a synchronization ancestor, exclude it
3556 * from selection to avoid forming a
3557 * synchronization loop within the orphan mesh,
3558 * triggering stratum climb to infinity
3559 * instability. Peers at stratum higher than
3560 * the orphan stratum could have the orphan
3561 * parent in ancestry so are excluded.
3562 * See http://bugs.ntp.org/2050
3564 if (peer->stratum > sys_orphan) {
3569 * The following are special cases. We deal
3572 if (!(peer->flags & FLAG_PREFER)) {
3573 switch (peer->refclktype) {
3574 case REFCLK_LOCALCLOCK:
3575 if ( current_time > orphwait
3576 && typelocal == NULL)
3581 if ( current_time > orphwait
3582 && typeacts == NULL)
3587 #endif /* REFCLOCK */
3590 * If we get this far, the peer can stay on the
3591 * island, but does not yet have the immunity
3594 peer->new_status = CTL_PST_SEL_SANE;
3595 f = root_distance(peer);
3596 peers[nlist].peer = peer;
3597 peers[nlist].error = peer->jitter;
3598 peers[nlist].synch = f;
3602 * Insert each interval endpoint on the unsorted
3606 endpoint[nl2].type = -1; /* lower end */
3607 endpoint[nl2].val = e - f;
3609 endpoint[nl2].type = 1; /* upper end */
3610 endpoint[nl2].val = e + f;
3614 * Construct sorted indx[] of endpoint[] indexes ordered by
3617 for (i = 0; i < nl2; i++)
3619 for (i = 0; i < nl2; i++) {
3620 endp = endpoint[indx[i]];
3623 for (j = i + 1; j < nl2; j++) {
3624 endp = endpoint[indx[j]];
3636 for (i = 0; i < nl2; i++)
3637 DPRINTF(3, ("select: endpoint %2d %.6f\n",
3638 endpoint[indx[i]].type, endpoint[indx[i]].val));
3641 * This is the actual algorithm that cleaves the truechimers
3642 * from the falsetickers. The original algorithm was described
3643 * in Keith Marzullo's dissertation, but has been modified for
3646 * Briefly put, we first assume there are no falsetickers, then
3647 * scan the candidate list first from the low end upwards and
3648 * then from the high end downwards. The scans stop when the
3649 * number of intersections equals the number of candidates less
3650 * the number of falsetickers. If this doesn't happen for a
3651 * given number of falsetickers, we bump the number of
3652 * falsetickers and try again. If the number of falsetickers
3653 * becomes equal to or greater than half the number of
3654 * candidates, the Albanians have won the Byzantine wars and
3655 * correct synchronization is not possible.
3657 * Here, nlist is the number of candidates and allow is the
3658 * number of falsetickers. Upon exit, the truechimers are the
3659 * survivors with offsets not less than low and not greater than
3660 * high. There may be none of them.
3664 for (allow = 0; 2 * allow < nlist; allow++) {
3667 * Bound the interval (low, high) as the smallest
3668 * interval containing points from the most sources.
3671 for (i = 0; i < nl2; i++) {
3672 low = endpoint[indx[i]].val;
3673 n -= endpoint[indx[i]].type;
3674 if (n >= nlist - allow)
3678 for (j = nl2 - 1; j >= 0; j--) {
3679 high = endpoint[indx[j]].val;
3680 n += endpoint[indx[j]].type;
3681 if (n >= nlist - allow)
3686 * If an interval containing truechimers is found, stop.
3687 * If not, increase the number of falsetickers and go
3695 * Clustering algorithm. Whittle candidate list of falsetickers,
3696 * who leave the island immediately. The TRUE peer is always a
3697 * truechimer. We must leave at least one peer to collect the
3700 * We assert the correct time is contained in the interval, but
3701 * the best offset estimate for the interval might not be
3702 * contained in the interval. For this purpose, a truechimer is
3703 * defined as the midpoint of an interval that overlaps the
3704 * intersection interval.
3707 for (i = 0; i < nlist; i++) {
3710 peer = peers[i].peer;
3713 || peer->offset + h < low
3714 || peer->offset - h > high
3715 ) && !(peer->flags & FLAG_TRUE))
3720 * Eligible PPS peers must survive the intersection
3721 * algorithm. Use the first one found, but don't
3722 * include any of them in the cluster population.
3724 if (peer->flags & FLAG_PPS) {
3725 if (typepps == NULL)
3727 if (!(peer->flags & FLAG_TSTAMP_PPS))
3730 #endif /* REFCLOCK */
3733 peers[j] = peers[i];
3739 * If no survivors remain at this point, check if the modem
3740 * driver, local driver or orphan parent in that order. If so,
3741 * nominate the first one found as the only survivor.
3742 * Otherwise, give up and leave the island to the rats.
3746 peers[0].synch = sys_mindisp;
3748 if (typeacts != NULL) {
3749 peers[0].peer = typeacts;
3751 } else if (typelocal != NULL) {
3752 peers[0].peer = typelocal;
3755 #endif /* REFCLOCK */
3756 if (typeorphan != NULL) {
3757 peers[0].peer = typeorphan;
3763 * Mark the candidates at this point as truechimers.
3765 for (i = 0; i < nlist; i++) {
3766 peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
3767 DPRINTF(2, ("select: survivor %s %f\n",
3768 stoa(&peers[i].peer->srcadr), peers[i].synch));
3772 * Now, vote outliers off the island by select jitter weighted
3773 * by root distance. Continue voting as long as there are more
3774 * than sys_minclock survivors and the select jitter of the peer
3775 * with the worst metric is greater than the minimum peer
3776 * jitter. Stop if we are about to discard a TRUE or PREFER
3777 * peer, who of course have the immunity idol.
3784 for (i = 0; i < nlist; i++) {
3785 if (peers[i].error < d)
3787 peers[i].seljit = 0;
3790 for (j = 0; j < nlist; j++)
3791 f += DIFF(peers[j].peer->offset,
3792 peers[i].peer->offset);
3793 peers[i].seljit = SQRT(f / (nlist - 1));
3795 if (peers[i].seljit * peers[i].synch > e) {
3796 g = peers[i].seljit;
3797 e = peers[i].seljit * peers[i].synch;
3801 g = max(g, LOGTOD(sys_precision));
3802 if ( nlist <= max(1, sys_minclock)
3804 || ((FLAG_TRUE | FLAG_PREFER) & peers[k].peer->flags))
3807 DPRINTF(3, ("select: drop %s seljit %.6f jit %.6f\n",
3808 ntoa(&peers[k].peer->srcadr), g, d));
3809 if (nlist > sys_maxclock)
3810 peers[k].peer->new_status = CTL_PST_SEL_EXCESS;
3811 for (j = k + 1; j < nlist; j++)
3812 peers[j - 1] = peers[j];
3817 * What remains is a list usually not greater than sys_minclock
3818 * peers. Note that unsynchronized peers cannot survive this
3819 * far. Count and mark these survivors.
3821 * While at it, count the number of leap warning bits found.
3822 * This will be used later to vote the system leap warning bit.
3823 * If a leap warning bit is found on a reference clock, the vote
3826 * Choose the system peer using a hybrid metric composed of the
3827 * selection jitter scaled by the root distance augmented by
3828 * stratum scaled by sys_mindisp (.001 by default). The goal of
3829 * the small stratum factor is to avoid clockhop between a
3830 * reference clock and a network peer which has a refclock and
3831 * is using an older ntpd, which does not floor sys_rootdisp at
3834 * In contrast, ntpd 4.2.6 and earlier used stratum primarily
3835 * in selecting the system peer, using a weight of 1 second of
3836 * additional root distance per stratum. This heavy bias is no
3837 * longer appropriate, as the scaled root distance provides a
3838 * more rational metric carrying the cumulative error budget.
3844 for (i = 0; i < nlist; i++) {
3845 peer = peers[i].peer;
3847 peer->new_status = CTL_PST_SEL_SYNCCAND;
3849 if (peer->leap == LEAP_ADDSECOND) {
3850 if (peer->flags & FLAG_REFCLOCK)
3851 leap_vote_ins = nlist;
3852 else if (leap_vote_ins < nlist)
3855 if (peer->leap == LEAP_DELSECOND) {
3856 if (peer->flags & FLAG_REFCLOCK)
3857 leap_vote_del = nlist;
3858 else if (leap_vote_del < nlist)
3861 if (peer->flags & FLAG_PREFER)
3863 speermet = peers[i].seljit * peers[i].synch +
3864 peer->stratum * sys_mindisp;
3872 * Unless there are at least sys_misane survivors, leave the
3873 * building dark. Otherwise, do a clockhop dance. Ordinarily,
3874 * use the selected survivor speer. However, if the current
3875 * system peer is not speer, stay with the current system peer
3876 * as long as it doesn't get too old or too ugly.
3878 if (nlist > 0 && nlist >= sys_minsane) {
3881 typesystem = peers[speer].peer;
3882 if (osys_peer == NULL || osys_peer == typesystem) {
3884 } else if ((x = fabs(typesystem->offset -
3885 osys_peer->offset)) < sys_mindisp) {
3886 if (sys_clockhop == 0)
3887 sys_clockhop = sys_mindisp;
3890 DPRINTF(1, ("select: clockhop %d %.6f %.6f\n",
3891 j, x, sys_clockhop));
3892 if (fabs(x) < sys_clockhop)
3893 typesystem = osys_peer;
3902 * Mitigation rules of the game. We have the pick of the
3903 * litter in typesystem if any survivors are left. If
3904 * there is a prefer peer, use its offset and jitter.
3905 * Otherwise, use the combined offset and jitter of all kitters.
3907 if (typesystem != NULL) {
3908 if (sys_prefer == NULL) {
3909 typesystem->new_status = CTL_PST_SEL_SYSPEER;
3910 clock_combine(peers, sys_survivors, speer);
3912 typesystem = sys_prefer;
3914 typesystem->new_status = CTL_PST_SEL_SYSPEER;
3915 sys_offset = typesystem->offset;
3916 sys_jitter = typesystem->jitter;
3918 DPRINTF(1, ("select: combine offset %.9f jitter %.9f\n",
3919 sys_offset, sys_jitter));
3923 * If a PPS driver is lit and the combined offset is less than
3924 * 0.4 s, select the driver as the PPS peer and use its offset
3925 * and jitter. However, if this is the atom driver, use it only
3926 * if there is a prefer peer or there are no survivors and none
3929 if ( typepps != NULL
3930 && fabs(sys_offset) < 0.4
3931 && ( typepps->refclktype != REFCLK_ATOM_PPS
3932 || ( typepps->refclktype == REFCLK_ATOM_PPS
3933 && ( sys_prefer != NULL
3934 || (typesystem == NULL && sys_minsane == 0))))) {
3935 typesystem = typepps;
3937 typesystem->new_status = CTL_PST_SEL_PPS;
3938 sys_offset = typesystem->offset;
3939 sys_jitter = typesystem->jitter;
3940 DPRINTF(1, ("select: pps offset %.9f jitter %.9f\n",
3941 sys_offset, sys_jitter));
3943 #endif /* REFCLOCK */
3946 * If there are no survivors at this point, there is no
3947 * system peer. If so and this is an old update, keep the
3948 * current statistics, but do not update the clock.
3950 if (typesystem == NULL) {
3951 if (osys_peer != NULL) {
3952 if (sys_orphwait > 0)
3953 orphwait = current_time + sys_orphwait;
3954 report_event(EVNT_NOPEER, NULL, NULL);
3957 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3958 peer->status = peer->new_status;
3963 * Do not use old data, as this may mess up the clock discipline
3966 if (typesystem->epoch <= sys_epoch)
3970 * We have found the alpha male. Wind the clock.
3972 if (osys_peer != typesystem)
3973 report_event(PEVNT_NEWPEER, typesystem, NULL);
3974 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3975 peer->status = peer->new_status;
3976 clock_update(typesystem);
3982 peer_select * peers, /* survivor list */
3983 int npeers, /* number of survivors */
3984 int syspeer /* index of sys.peer */
3991 for (i = 0; i < npeers; i++) {
3992 x = 1. / peers[i].synch;
3994 z += x * peers[i].peer->offset;
3995 w += x * DIFF(peers[i].peer->offset,
3996 peers[syspeer].peer->offset);
3999 sys_jitter = SQRT(w / y + SQUARE(peers[syspeer].seljit));
4004 * root_distance - compute synchronization distance from peer to root
4008 struct peer *peer /* peer structure pointer */
4014 * Root Distance (LAMBDA) is defined as:
4015 * (delta + DELTA)/2 + epsilon + EPSILON + D
4018 * delta is the round-trip delay
4019 * DELTA is the root delay
4020 * epsilon is the peer dispersion
4021 * + (15 usec each second)
4022 * EPSILON is the root dispersion
4025 * NB: Think hard about why we are using these values, and what
4026 * the alternatives are, and the various pros/cons.
4028 * DLM thinks these are probably the best choices from any of the
4029 * other worse choices.
4031 dtemp = (peer->delay + peer->rootdelay) / 2
4033 + clock_phi * (current_time - peer->update)
4037 * Careful squeak here. The value returned must be greater than
4038 * the minimum root dispersion in order to avoid clockhop with
4039 * highly precise reference clocks. Note that the root distance
4040 * cannot exceed the sys_maxdist, as this is the cutoff by the
4041 * selection algorithm.
4043 if (dtemp < sys_mindisp)
4044 dtemp = sys_mindisp;
4050 * peer_xmit - send packet for persistent association.
4054 struct peer *peer /* peer structure pointer */
4057 struct pkt xpkt; /* transmit packet */
4058 size_t sendlen, authlen;
4059 keyid_t xkeyid = 0; /* transmit key ID */
4060 l_fp xmt_tx, xmt_ty;
4062 if (!peer->dstadr) /* drop peers without interface */
4065 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, peer->version,
4067 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4068 xpkt.ppoll = peer->hpoll;
4069 xpkt.precision = sys_precision;
4070 xpkt.refid = sys_refid;
4071 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4072 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4073 /* Use sys_reftime for peer exchanges */
4074 HTONL_FP(&sys_reftime, &xpkt.reftime);
4075 HTONL_FP(&peer->rec, &xpkt.org);
4076 HTONL_FP(&peer->dst, &xpkt.rec);
4079 * If the received packet contains a MAC, the transmitted packet
4080 * is authenticated and contains a MAC. If not, the transmitted
4081 * packet is not authenticated.
4083 * It is most important when autokey is in use that the local
4084 * interface IP address be known before the first packet is
4085 * sent. Otherwise, it is not possible to compute a correct MAC
4086 * the recipient will accept. Thus, the I/O semantics have to do
4087 * a little more work. In particular, the wildcard interface
4088 * might not be usable.
4090 sendlen = LEN_PKT_NOMAC;
4093 !(peer->flags & FLAG_SKEY) &&
4094 #endif /* !AUTOKEY */
4098 * Transmit a-priori timestamps
4100 get_systime(&xmt_tx);
4101 if (peer->flip == 0) { /* basic mode */
4102 peer->aorg = xmt_tx;
4103 HTONL_FP(&xmt_tx, &xpkt.xmt);
4104 } else { /* interleaved modes */
4105 if (peer->hmode == MODE_BROADCAST) { /* bcst */
4106 HTONL_FP(&xmt_tx, &xpkt.xmt);
4108 HTONL_FP(&peer->borg,
4111 HTONL_FP(&peer->aorg,
4113 } else { /* symmetric */
4115 HTONL_FP(&peer->borg,
4118 HTONL_FP(&peer->aorg,
4122 peer->t21_bytes = sendlen;
4123 sendpkt(&peer->srcadr, peer->dstadr,
4124 sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4127 peer->throttle += (1 << peer->minpoll) - 2;
4130 * Capture a-posteriori timestamps
4132 get_systime(&xmt_ty);
4133 if (peer->flip != 0) { /* interleaved modes */
4135 peer->aorg = xmt_ty;
4137 peer->borg = xmt_ty;
4138 peer->flip = -peer->flip;
4140 L_SUB(&xmt_ty, &xmt_tx);
4141 LFPTOD(&xmt_ty, peer->xleave);
4142 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d len %zu xmt %#010x.%08x\n",
4144 peer->dstadr ? stoa(&peer->dstadr->sin) : "-",
4145 stoa(&peer->srcadr), peer->hmode, sendlen,
4146 xmt_tx.l_ui, xmt_tx.l_uf));
4151 * Authentication is enabled, so the transmitted packet must be
4152 * authenticated. If autokey is enabled, fuss with the various
4153 * modes; otherwise, symmetric key cryptography is used.
4156 if (peer->flags & FLAG_SKEY) {
4157 struct exten *exten; /* extension field */
4160 * The Public Key Dance (PKD): Cryptographic credentials
4161 * are contained in extension fields, each including a
4162 * 4-octet length/code word followed by a 4-octet
4163 * association ID and optional additional data. Optional
4164 * data includes a 4-octet data length field followed by
4165 * the data itself. Request messages are sent from a
4166 * configured association; response messages can be sent
4167 * from a configured association or can take the fast
4168 * path without ever matching an association. Response
4169 * messages have the same code as the request, but have
4170 * a response bit and possibly an error bit set. In this
4171 * implementation, a message may contain no more than
4172 * one command and one or more responses.
4174 * Cryptographic session keys include both a public and
4175 * a private componet. Request and response messages
4176 * using extension fields are always sent with the
4177 * private component set to zero. Packets without
4178 * extension fields indlude the private component when
4179 * the session key is generated.
4184 * Allocate and initialize a keylist if not
4185 * already done. Then, use the list in inverse
4186 * order, discarding keys once used. Keep the
4187 * latest key around until the next one, so
4188 * clients can use client/server packets to
4189 * compute propagation delay.
4191 * Note that once a key is used from the list,
4192 * it is retained in the key cache until the
4193 * next key is used. This is to allow a client
4194 * to retrieve the encrypted session key
4195 * identifier to verify authenticity.
4197 * If for some reason a key is no longer in the
4198 * key cache, a birthday has happened or the key
4199 * has expired, so the pseudo-random sequence is
4200 * broken. In that case, purge the keylist and
4203 if (peer->keynumber == 0)
4204 make_keylist(peer, peer->dstadr);
4207 xkeyid = peer->keylist[peer->keynumber];
4208 if (authistrusted(xkeyid))
4213 peer->keyid = xkeyid;
4215 switch (peer->hmode) {
4218 * In broadcast server mode the autokey values are
4219 * required by the broadcast clients. Push them when a
4220 * new keylist is generated; otherwise, push the
4221 * association message so the client can request them at
4224 case MODE_BROADCAST:
4225 if (peer->flags & FLAG_ASSOC)
4226 exten = crypto_args(peer, CRYPTO_AUTO |
4227 CRYPTO_RESP, peer->associd, NULL);
4229 exten = crypto_args(peer, CRYPTO_ASSOC |
4230 CRYPTO_RESP, peer->associd, NULL);
4234 * In symmetric modes the parameter, certificate,
4235 * identity, cookie and autokey exchanges are
4236 * required. The leapsecond exchange is optional. But, a
4237 * peer will not believe the other peer until the other
4238 * peer has synchronized, so the certificate exchange
4239 * might loop until then. If a peer finds a broken
4240 * autokey sequence, it uses the autokey exchange to
4241 * retrieve the autokey values. In any case, if a new
4242 * keylist is generated, the autokey values are pushed.
4248 * Parameter, certificate and identity.
4251 exten = crypto_args(peer, CRYPTO_ASSOC,
4252 peer->associd, hostval.ptr);
4253 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4254 exten = crypto_args(peer, CRYPTO_CERT,
4255 peer->associd, peer->issuer);
4256 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4257 exten = crypto_args(peer,
4258 crypto_ident(peer), peer->associd,
4262 * Cookie and autokey. We request the cookie
4263 * only when the this peer and the other peer
4264 * are synchronized. But, this peer needs the
4265 * autokey values when the cookie is zero. Any
4266 * time we regenerate the key list, we offer the
4267 * autokey values without being asked. If for
4268 * some reason either peer finds a broken
4269 * autokey sequence, the autokey exchange is
4270 * used to retrieve the autokey values.
4272 else if ( sys_leap != LEAP_NOTINSYNC
4273 && peer->leap != LEAP_NOTINSYNC
4274 && !(peer->crypto & CRYPTO_FLAG_COOK))
4275 exten = crypto_args(peer, CRYPTO_COOK,
4276 peer->associd, NULL);
4277 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4278 exten = crypto_args(peer, CRYPTO_AUTO,
4279 peer->associd, NULL);
4280 else if ( peer->flags & FLAG_ASSOC
4281 && peer->crypto & CRYPTO_FLAG_SIGN)
4282 exten = crypto_args(peer, CRYPTO_AUTO |
4283 CRYPTO_RESP, peer->assoc, NULL);
4286 * Wait for clock sync, then sign the
4287 * certificate and retrieve the leapsecond
4290 else if (sys_leap == LEAP_NOTINSYNC)
4293 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4294 exten = crypto_args(peer, CRYPTO_SIGN,
4295 peer->associd, hostval.ptr);
4296 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4297 exten = crypto_args(peer, CRYPTO_LEAP,
4298 peer->associd, NULL);
4302 * In client mode the parameter, certificate, identity,
4303 * cookie and sign exchanges are required. The
4304 * leapsecond exchange is optional. If broadcast client
4305 * mode the same exchanges are required, except that the
4306 * autokey exchange is substitutes for the cookie
4307 * exchange, since the cookie is always zero. If the
4308 * broadcast client finds a broken autokey sequence, it
4309 * uses the autokey exchange to retrieve the autokey
4315 * Parameter, certificate and identity.
4318 exten = crypto_args(peer, CRYPTO_ASSOC,
4319 peer->associd, hostval.ptr);
4320 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4321 exten = crypto_args(peer, CRYPTO_CERT,
4322 peer->associd, peer->issuer);
4323 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4324 exten = crypto_args(peer,
4325 crypto_ident(peer), peer->associd,
4329 * Cookie and autokey. These are requests, but
4330 * we use the peer association ID with autokey
4331 * rather than our own.
4333 else if (!(peer->crypto & CRYPTO_FLAG_COOK))
4334 exten = crypto_args(peer, CRYPTO_COOK,
4335 peer->associd, NULL);
4336 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4337 exten = crypto_args(peer, CRYPTO_AUTO,
4341 * Wait for clock sync, then sign the
4342 * certificate and retrieve the leapsecond
4345 else if (sys_leap == LEAP_NOTINSYNC)
4348 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4349 exten = crypto_args(peer, CRYPTO_SIGN,
4350 peer->associd, hostval.ptr);
4351 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4352 exten = crypto_args(peer, CRYPTO_LEAP,
4353 peer->associd, NULL);
4358 * Add a queued extension field if present. This is
4359 * always a request message, so the reply ID is already
4360 * in the message. If an error occurs, the error bit is
4361 * lit in the response.
4363 if (peer->cmmd != NULL) {
4366 temp32 = CRYPTO_RESP;
4367 peer->cmmd->opcode |= htonl(temp32);
4368 sendlen += crypto_xmit(peer, &xpkt, NULL,
4369 sendlen, peer->cmmd, 0);
4375 * Add an extension field created above. All but the
4376 * autokey response message are request messages.
4378 if (exten != NULL) {
4379 if (exten->opcode != 0)
4380 sendlen += crypto_xmit(peer, &xpkt,
4381 NULL, sendlen, exten, 0);
4386 * Calculate the next session key. Since extension
4387 * fields are present, the cookie value is zero.
4389 if (sendlen > (int)LEN_PKT_NOMAC) {
4390 session_key(&peer->dstadr->sin, &peer->srcadr,
4394 #endif /* AUTOKEY */
4397 * Transmit a-priori timestamps
4399 get_systime(&xmt_tx);
4400 if (peer->flip == 0) { /* basic mode */
4401 peer->aorg = xmt_tx;
4402 HTONL_FP(&xmt_tx, &xpkt.xmt);
4403 } else { /* interleaved modes */
4404 if (peer->hmode == MODE_BROADCAST) { /* bcst */
4405 HTONL_FP(&xmt_tx, &xpkt.xmt);
4407 HTONL_FP(&peer->borg, &xpkt.org);
4409 HTONL_FP(&peer->aorg, &xpkt.org);
4410 } else { /* symmetric */
4412 HTONL_FP(&peer->borg, &xpkt.xmt);
4414 HTONL_FP(&peer->aorg, &xpkt.xmt);
4417 xkeyid = peer->keyid;
4418 authlen = authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4420 report_event(PEVNT_AUTH, peer, "no key");
4421 peer->flash |= TEST5; /* auth error */
4427 if (xkeyid > NTP_MAXKEY)
4428 authtrust(xkeyid, 0);
4429 #endif /* AUTOKEY */
4430 if (sendlen > sizeof(xpkt)) {
4431 msyslog(LOG_ERR, "peer_xmit: buffer overflow %zu", sendlen);
4434 peer->t21_bytes = sendlen;
4435 sendpkt(&peer->srcadr, peer->dstadr,
4436 sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4439 peer->throttle += (1 << peer->minpoll) - 2;
4442 * Capture a-posteriori timestamps
4444 get_systime(&xmt_ty);
4445 if (peer->flip != 0) { /* interleaved modes */
4447 peer->aorg = xmt_ty;
4449 peer->borg = xmt_ty;
4450 peer->flip = -peer->flip;
4452 L_SUB(&xmt_ty, &xmt_tx);
4453 LFPTOD(&xmt_ty, peer->xleave);
4455 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu index %d\n",
4456 current_time, latoa(peer->dstadr),
4457 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen,
4459 #else /* !AUTOKEY follows */
4460 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu\n",
4461 current_time, peer->dstadr ?
4462 ntoa(&peer->dstadr->sin) : "-",
4463 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
4464 #endif /* !AUTOKEY */
4473 leap_smear_add_offs(
4479 L_ADD(t, &leap_smear.offset);
4482 ** XXX: Should the smear be added to the root dispersion?
4488 #endif /* LEAP_SMEAR */
4492 * fast_xmit - Send packet for nonpersistent association. Note that
4493 * neither the source or destination can be a broadcast address.
4497 struct recvbuf *rbufp, /* receive packet pointer */
4498 int xmode, /* receive mode */ /* XXX: HMS: really? */
4499 keyid_t xkeyid, /* transmit key ID */
4500 int flags /* restrict mask */
4503 struct pkt xpkt; /* transmit packet structure */
4504 struct pkt *rpkt; /* receive packet structure */
4505 l_fp xmt_tx, xmt_ty;
4512 * Initialize transmit packet header fields from the receive
4513 * buffer provided. We leave the fields intact as received, but
4514 * set the peer poll at the maximum of the receive peer poll and
4515 * the system minimum poll (ntp_minpoll). This is for KoD rate
4516 * control and not strictly specification compliant, but doesn't
4519 * If the gazinta was from a multicast address, the gazoutta
4520 * must go out another way.
4522 rpkt = &rbufp->recv_pkt;
4523 if (rbufp->dstadr->flags & INT_MCASTOPEN)
4524 rbufp->dstadr = findinterface(&rbufp->recv_srcadr);
4527 * If this is a kiss-o'-death (KoD) packet, show leap
4528 * unsynchronized, stratum zero, reference ID the four-character
4529 * kiss code and (???) system root delay. Note we don't reveal
4530 * the local time, so these packets can't be used for
4533 if (flags & RES_KOD) {
4535 xpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
4536 PKT_VERSION(rpkt->li_vn_mode), xmode);
4537 xpkt.stratum = STRATUM_PKT_UNSPEC;
4538 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4539 xpkt.precision = rpkt->precision;
4540 memcpy(&xpkt.refid, "RATE", 4);
4541 xpkt.rootdelay = rpkt->rootdelay;
4542 xpkt.rootdisp = rpkt->rootdisp;
4543 xpkt.reftime = rpkt->reftime;
4544 xpkt.org = rpkt->xmt;
4545 xpkt.rec = rpkt->xmt;
4546 xpkt.xmt = rpkt->xmt;
4549 * This is a normal packet. Use the system variables.
4552 double this_rootdisp;
4557 * Make copies of the variables which can be affected by smearing.
4559 l_fp this_recv_time;
4563 * If we are inside the leap smear interval we add
4564 * the current smear offset to:
4565 * - the packet receive time,
4566 * - the packet transmit time,
4567 * - and eventually to the reftime to make sure the
4568 * reftime isn't later than the transmit/receive times.
4570 xpkt.li_vn_mode = PKT_LI_VN_MODE(xmt_leap,
4571 PKT_VERSION(rpkt->li_vn_mode), xmode);
4573 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4574 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4575 xpkt.precision = sys_precision;
4576 xpkt.refid = sys_refid;
4577 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4580 ** Server Response Fuzzing
4582 ** Which values do we want to use for reftime and rootdisp?
4585 if ( MODE_SERVER == xmode
4586 && RES_SRVRSPFUZ & flags) {
4587 if (current_time < p2_time) {
4588 this_ref_time = p2_reftime;
4589 this_rootdisp = p2_rootdisp;
4590 } else if (current_time < prev_time) {
4591 this_ref_time = prev_reftime;
4592 this_rootdisp = prev_rootdisp;
4594 this_ref_time = sys_reftime;
4595 this_rootdisp = sys_rootdisp;
4598 SRVRSP_FUZZ(this_ref_time);
4600 this_ref_time = sys_reftime;
4601 this_rootdisp = sys_rootdisp;
4608 xpkt.rootdisp = HTONS_FP(DTOUFP(this_rootdisp));
4615 if (leap_smear.in_progress) {
4616 /* adjust the reftime by the same amount as the
4617 * leap smear, as we don't want to risk the
4618 * reftime being later than the transmit time.
4620 leap_smear_add_offs(&this_ref_time, NULL);
4624 HTONL_FP(&this_ref_time, &xpkt.reftime);
4631 if (leap_smear.in_progress) {
4632 xpkt.refid = convertLFPToRefID(leap_smear.offset);
4633 DPRINTF(2, ("fast_xmit: leap_smear.in_progress: refid %8x, smear %s\n",
4635 lfptoa(&leap_smear.offset, 8)
4644 xpkt.org = rpkt->xmt;
4650 this_recv_time = rbufp->recv_time;
4651 if (leap_smear.in_progress)
4652 leap_smear_add_offs(&this_recv_time, NULL);
4653 HTONL_FP(&this_recv_time, &xpkt.rec);
4655 HTONL_FP(&rbufp->recv_time, &xpkt.rec);
4662 get_systime(&xmt_tx);
4664 if (leap_smear.in_progress)
4665 leap_smear_add_offs(&xmt_tx, &this_recv_time);
4667 HTONL_FP(&xmt_tx, &xpkt.xmt);
4670 #ifdef HAVE_NTP_SIGND
4671 if (flags & RES_MSSNTP) {
4672 send_via_ntp_signd(rbufp, xmode, xkeyid, flags, &xpkt);
4675 #endif /* HAVE_NTP_SIGND */
4678 * If the received packet contains a MAC, the transmitted packet
4679 * is authenticated and contains a MAC. If not, the transmitted
4680 * packet is not authenticated.
4682 sendlen = LEN_PKT_NOMAC;
4683 if (rbufp->recv_length == sendlen) {
4684 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt,
4686 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d len %lu\n",
4687 current_time, stoa(&rbufp->dstadr->sin),
4688 stoa(&rbufp->recv_srcadr), xmode,
4694 * The received packet contains a MAC, so the transmitted packet
4695 * must be authenticated. For symmetric key cryptography, use
4696 * the predefined and trusted symmetric keys to generate the
4697 * cryptosum. For autokey cryptography, use the server private
4698 * value to generate the cookie, which is unique for every
4699 * source-destination-key ID combination.
4702 if (xkeyid > NTP_MAXKEY) {
4706 * The only way to get here is a reply to a legitimate
4707 * client request message, so the mode must be
4708 * MODE_SERVER. If an extension field is present, there
4709 * can be only one and that must be a command. Do what
4710 * needs, but with private value of zero so the poor
4711 * jerk can decode it. If no extension field is present,
4712 * use the cookie to generate the session key.
4714 cookie = session_key(&rbufp->recv_srcadr,
4715 &rbufp->dstadr->sin, 0, sys_private, 0);
4716 if ((size_t)rbufp->recv_length > sendlen + MAX_MAC_LEN) {
4717 session_key(&rbufp->dstadr->sin,
4718 &rbufp->recv_srcadr, xkeyid, 0, 2);
4719 temp32 = CRYPTO_RESP;
4720 rpkt->exten[0] |= htonl(temp32);
4721 sendlen += crypto_xmit(NULL, &xpkt, rbufp,
4722 sendlen, (struct exten *)rpkt->exten,
4725 session_key(&rbufp->dstadr->sin,
4726 &rbufp->recv_srcadr, xkeyid, cookie, 2);
4729 #endif /* AUTOKEY */
4730 get_systime(&xmt_tx);
4731 sendlen += authencrypt(xkeyid, (u_int32 *)&xpkt, sendlen);
4733 if (xkeyid > NTP_MAXKEY)
4734 authtrust(xkeyid, 0);
4735 #endif /* AUTOKEY */
4736 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt, sendlen);
4737 get_systime(&xmt_ty);
4738 L_SUB(&xmt_ty, &xmt_tx);
4739 sys_authdelay = xmt_ty;
4740 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d keyid %08x len %lu\n",
4741 current_time, ntoa(&rbufp->dstadr->sin),
4742 ntoa(&rbufp->recv_srcadr), xmode, xkeyid,
4748 * pool_xmit - resolve hostname or send unicast solicitation for pool.
4752 struct peer *pool /* pool solicitor association */
4756 struct pkt xpkt; /* transmit packet structure */
4757 struct addrinfo hints;
4759 struct interface * lcladr;
4760 sockaddr_u * rmtadr;
4762 u_short restrict_mask;
4766 DEBUG_REQUIRE(pool);
4767 if (NULL == pool->ai) {
4768 if (pool->addrs != NULL) {
4769 /* free() is used with copy_addrinfo_list() */
4774 hints.ai_family = AF(&pool->srcadr);
4775 hints.ai_socktype = SOCK_DGRAM;
4776 hints.ai_protocol = IPPROTO_UDP;
4777 /* ignore getaddrinfo_sometime() errors, we will retry */
4778 rc = getaddrinfo_sometime(
4783 &pool_name_resolved,
4784 (void *)(intptr_t)pool->associd);
4786 DPRINTF(1, ("pool DNS lookup %s started\n",
4790 "unable to start pool DNS %s: %m",
4796 /* copy_addrinfo_list ai_addr points to a sockaddr_u */
4797 rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
4798 pool->ai = pool->ai->ai_next;
4799 p = findexistingpeer(rmtadr, NULL, NULL, MODE_CLIENT, 0, NULL);
4800 } while (p != NULL && pool->ai != NULL);
4802 return; /* out of addresses, re-query DNS next poll */
4803 restrictions(rmtadr, &r4a);
4804 restrict_mask = r4a.rflags;
4805 if (RES_FLAGS & restrict_mask)
4806 restrict_source(rmtadr, 0,
4807 current_time + POOL_SOLICIT_WINDOW + 1);
4808 lcladr = findinterface(rmtadr);
4809 memset(&xpkt, 0, sizeof(xpkt));
4810 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, pool->version,
4812 xpkt.stratum = STRATUM_TO_PKT(sys_stratum);
4813 xpkt.ppoll = pool->hpoll;
4814 xpkt.precision = sys_precision;
4815 xpkt.refid = sys_refid;
4816 xpkt.rootdelay = HTONS_FP(DTOFP(sys_rootdelay));
4817 xpkt.rootdisp = HTONS_FP(DTOUFP(sys_rootdisp));
4818 /* Bug 3596: What are the pros/cons of using sys_reftime here? */
4819 HTONL_FP(&sys_reftime, &xpkt.reftime);
4821 /* HMS: the following is better done after the ntp_random() calls */
4822 get_systime(&xmt_tx);
4823 pool->aorg = xmt_tx;
4825 if (FLAG_LOOPNONCE & pool->flags) {
4829 nonce.l_ui = ntp_random();
4830 } while (0 == nonce.l_ui);
4832 nonce.l_uf = ntp_random();
4833 } while (0 == nonce.l_uf);
4834 pool->nonce = nonce;
4835 HTONL_FP(&nonce, &xpkt.xmt);
4837 L_CLR(&pool->nonce);
4838 HTONL_FP(&xmt_tx, &xpkt.xmt);
4840 sendpkt(rmtadr, lcladr,
4841 sys_ttl[(pool->ttl >= sys_ttlmax) ? sys_ttlmax : pool->ttl],
4842 &xpkt, LEN_PKT_NOMAC);
4844 pool->throttle += (1 << pool->minpoll) - 2;
4845 DPRINTF(1, ("pool_xmit: at %ld %s->%s pool\n",
4846 current_time, latoa(lcladr), stoa(rmtadr)));
4847 msyslog(LOG_INFO, "Soliciting pool server %s", stoa(rmtadr));
4854 * group_test - test if this is the same group
4856 * host assoc return action
4857 * none none 0 mobilize *
4858 * none group 0 mobilize *
4859 * group none 0 mobilize *
4860 * group group 1 mobilize
4861 * group different 1 ignore
4862 * * ignore if notrust
4873 if (strcmp(grp, sys_groupname) == 0)
4879 if (strcmp(grp, ident) == 0)
4884 #endif /* AUTOKEY */
4894 const char * service,
4895 const struct addrinfo * hints,
4896 const struct addrinfo * res
4899 struct peer * pool; /* pool solicitor association */
4904 "error resolving pool %s: %s (%d)",
4905 name, gai_strerror(rescode), rescode);
4909 assoc = (associd_t)(intptr_t)context;
4910 pool = findpeerbyassoc(assoc);
4913 "Could not find assoc %u for pool DNS %s",
4917 DPRINTF(1, ("pool DNS %s completed\n", name));
4918 pool->addrs = copy_addrinfo_list(res);
4919 pool->ai = pool->addrs;
4928 * key_expire - purge the key list
4932 struct peer *peer /* peer structure pointer */
4937 if (peer->keylist != NULL) {
4938 for (i = 0; i <= peer->keynumber; i++)
4939 authtrust(peer->keylist[i], 0);
4940 free(peer->keylist);
4941 peer->keylist = NULL;
4943 value_free(&peer->sndval);
4944 peer->keynumber = 0;
4945 peer->flags &= ~FLAG_ASSOC;
4946 DPRINTF(1, ("key_expire: at %lu associd %d\n", current_time,
4949 #endif /* AUTOKEY */
4953 * local_refid(peer) - check peer refid to avoid selecting peers
4954 * currently synced to this ntpd.
4963 if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
4964 unicast_ep = p->dstadr;
4966 unicast_ep = findinterface(&p->srcadr);
4968 if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
4976 * Determine if the peer is unfit for synchronization
4978 * A peer is unfit for synchronization if
4979 * > TEST10 bad leap or stratum below floor or at or above ceiling
4980 * > TEST11 root distance exceeded for remote peer
4981 * > TEST12 a direct or indirect synchronization loop would form
4982 * > TEST13 unreachable or noselect
4984 int /* FALSE if fit, TRUE if unfit */
4986 struct peer *peer /* peer structure pointer */
4992 * A stratum error occurs if (1) the server has never been
4993 * synchronized, (2) the server stratum is below the floor or
4994 * greater than or equal to the ceiling.
4996 if ( peer->leap == LEAP_NOTINSYNC
4997 || peer->stratum < sys_floor
4998 || peer->stratum >= sys_ceiling) {
4999 rval |= TEST10; /* bad synch or stratum */
5003 * A distance error for a remote peer occurs if the root
5004 * distance is greater than or equal to the distance threshold
5005 * plus the increment due to one host poll interval.
5007 if ( !(peer->flags & FLAG_REFCLOCK)
5008 && root_distance(peer) >= sys_maxdist
5009 + clock_phi * ULOGTOD(peer->hpoll)) {
5010 rval |= TEST11; /* distance exceeded */
5014 * A loop error occurs if the remote peer is synchronized to the
5015 * local peer or if the remote peer is synchronized to the same
5016 * server as the local peer but only if the remote peer is
5017 * neither a reference clock nor an orphan.
5019 if (peer->stratum > 1 && local_refid(peer)) {
5020 rval |= TEST12; /* synchronization loop */
5024 * An unreachable error occurs if the server is unreachable or
5025 * the noselect bit is set.
5027 if (!peer->reach || (peer->flags & FLAG_NOSELECT)) {
5028 rval |= TEST13; /* unreachable */
5031 peer->flash &= ~PEER_TEST_MASK;
5032 peer->flash |= rval;
5038 * Find the precision of this particular machine
5040 #define MINSTEP 20e-9 /* minimum clock increment (s) */
5041 #define MAXSTEP 1 /* maximum clock increment (s) */
5042 #define MINCHANGES 12 /* minimum number of step samples */
5043 #define MAXLOOPS ((int)(1. / MINSTEP)) /* avoid infinite loop */
5046 * This routine measures the system precision defined as the minimum of
5047 * a sequence of differences between successive readings of the system
5048 * clock. However, if a difference is less than MINSTEP, the clock has
5049 * been read more than once during a clock tick and the difference is
5050 * ignored. We set MINSTEP greater than zero in case something happens
5051 * like a cache miss, and to tolerate underlying system clocks which
5052 * ensure each reading is strictly greater than prior readings while
5053 * using an underlying stepping (not interpolated) clock.
5055 * sys_tick and sys_precision represent the time to read the clock for
5056 * systems with high-precision clocks, and the tick interval or step
5057 * size for lower-precision stepping clocks.
5059 * This routine also measures the time to read the clock on stepping
5060 * system clocks by counting the number of readings between changes of
5061 * the underlying clock. With either type of clock, the minimum time
5062 * to read the clock is saved as sys_fuzz, and used to ensure the
5063 * get_systime() readings always increase and are fuzzed below sys_fuzz.
5066 measure_precision(void)
5069 * With sys_fuzz set to zero, get_systime() fuzzing of low bits
5070 * is effectively disabled. trunc_os_clock is FALSE to disable
5071 * get_ostime() simulation of a low-precision system clock.
5074 trunc_os_clock = FALSE;
5075 measured_tick = measure_tick_fuzz();
5076 set_sys_tick_precision(measured_tick);
5077 msyslog(LOG_INFO, "proto: precision = %.3f usec (%d)",
5078 sys_tick * 1e6, sys_precision);
5079 if (sys_fuzz < sys_tick) {
5080 msyslog(LOG_NOTICE, "proto: fuzz beneath %.3f usec",
5087 * measure_tick_fuzz()
5089 * measures the minimum time to read the clock (stored in sys_fuzz)
5090 * and returns the tick, the larger of the minimum increment observed
5091 * between successive clock readings and the time to read the clock.
5094 measure_tick_fuzz(void)
5096 l_fp minstep; /* MINSTEP as l_fp */
5097 l_fp val; /* current seconds fraction */
5098 l_fp last; /* last seconds fraction */
5099 l_fp ldiff; /* val - last */
5100 double tick; /* computed tick value */
5105 int i; /* log2 precision */
5111 DTOLFP(MINSTEP, &minstep);
5113 for (i = 0; i < MAXLOOPS && changes < MINCHANGES; i++) {
5116 L_SUB(&ldiff, &last);
5118 if (L_ISGT(&ldiff, &minstep)) {
5119 max_repeats = max(repeats, max_repeats);
5122 LFPTOD(&ldiff, diff);
5123 tick = min(diff, tick);
5128 if (changes < MINCHANGES) {
5129 msyslog(LOG_ERR, "Fatal error: precision could not be measured (MINSTEP too large?)");
5133 if (0 == max_repeats) {
5136 set_sys_fuzz(tick / max_repeats);
5144 set_sys_tick_precision(
5152 "unsupported tick %.3f > 1s ignored", tick);
5155 if (tick < measured_tick) {
5157 "proto: tick %.3f less than measured tick %.3f, ignored",
5158 tick, measured_tick);
5160 } else if (tick > measured_tick) {
5161 trunc_os_clock = TRUE;
5163 "proto: truncating system clock to multiples of %.9f",
5169 * Find the nearest power of two.
5171 for (i = 0; tick <= 1; i--)
5173 if (tick - 1 > 1 - tick / 2)
5176 sys_precision = (s_char)i;
5181 * init_proto - initialize the protocol module's data
5190 * Fill in the sys_* stuff. Default is don't listen to
5191 * broadcasting, require authentication.
5193 set_sys_leap(LEAP_NOTINSYNC);
5194 sys_stratum = STRATUM_UNSPEC;
5195 memcpy(&sys_refid, "INIT", 4);
5199 L_CLR(&sys_reftime);
5201 measure_precision();
5202 get_systime(&dummy);
5204 sys_manycastserver = 0;
5206 sys_bdelay = BDELAY_DEFAULT; /*[Bug 3031] delay cutoff */
5207 sys_authenticate = 1;
5208 sys_stattime = current_time;
5209 orphwait = current_time + sys_orphwait;
5211 for (i = 0; i < MAX_TTL; ++i)
5212 sys_ttl[i] = (u_char)((i * 256) / MAX_TTL);
5213 sys_ttlmax = (MAX_TTL - 1);
5220 * proto_config - configure the protocol module
5231 * Figure out what he wants to change, then do it
5233 DPRINTF(2, ("proto_config: code %d value %lu dvalue %lf\n",
5234 item, value, dvalue));
5239 * enable and disable commands - arguments are Boolean.
5241 case PROTO_AUTHENTICATE: /* authentication (auth) */
5242 sys_authenticate = value;
5245 case PROTO_BROADCLIENT: /* broadcast client (bclient) */
5246 sys_bclient = (int)value;
5247 if (sys_bclient == 0)
5254 case PROTO_CAL: /* refclock calibrate (calibrate) */
5257 #endif /* REFCLOCK */
5259 case PROTO_KERNEL: /* kernel discipline (kernel) */
5263 case PROTO_MONITOR: /* monitoring (monitor) */
5269 msyslog(LOG_WARNING,
5270 "restrict: 'monitor' cannot be disabled while 'limited' is enabled");
5274 case PROTO_NTP: /* NTP discipline (ntp) */
5278 case PROTO_MODE7: /* mode7 management (ntpdc) */
5282 case PROTO_PPS: /* PPS discipline (pps) */
5283 hardpps_enable = value;
5286 case PROTO_FILEGEN: /* statistics (stats) */
5287 stats_control = value;
5291 * tos command - arguments are double, sometimes cast to int
5294 case PROTO_BCPOLLBSTEP: /* Broadcast Poll Backstep gate (bcpollbstep) */
5295 sys_bcpollbstep = (u_char)dvalue;
5298 case PROTO_BEACON: /* manycast beacon (beacon) */
5299 sys_beacon = (int)dvalue;
5302 case PROTO_BROADDELAY: /* default broadcast delay (bdelay) */
5303 sys_bdelay = (dvalue ? dvalue : BDELAY_DEFAULT);
5306 case PROTO_CEILING: /* stratum ceiling (ceiling) */
5307 sys_ceiling = (int)dvalue;
5310 case PROTO_COHORT: /* cohort switch (cohort) */
5311 sys_cohort = (int)dvalue;
5314 case PROTO_FLOOR: /* stratum floor (floor) */
5315 sys_floor = (int)dvalue;
5318 case PROTO_MAXCLOCK: /* maximum candidates (maxclock) */
5319 sys_maxclock = (int)dvalue;
5322 case PROTO_MAXDIST: /* select threshold (maxdist) */
5323 sys_maxdist = dvalue;
5326 case PROTO_CALLDELAY: /* modem call delay (mdelay) */
5327 break; /* NOT USED */
5329 case PROTO_MINCLOCK: /* minimum candidates (minclock) */
5330 sys_minclock = (int)dvalue;
5333 case PROTO_MINDISP: /* minimum distance (mindist) */
5334 sys_mindisp = dvalue;
5337 case PROTO_MINSANE: /* minimum survivors (minsane) */
5338 sys_minsane = (int)dvalue;
5341 case PROTO_ORPHAN: /* orphan stratum (orphan) */
5342 sys_orphan = (int)dvalue;
5345 case PROTO_ORPHWAIT: /* orphan wait (orphwait) */
5346 orphwait -= sys_orphwait;
5347 sys_orphwait = (int)dvalue;
5348 orphwait += sys_orphwait;
5352 * Miscellaneous commands
5354 case PROTO_MULTICAST_ADD: /* add group address */
5356 io_multicast_add(svalue);
5360 case PROTO_MULTICAST_DEL: /* delete group address */
5362 io_multicast_del(svalue);
5366 * Peer_clear Early policy choices
5369 case PROTO_PCEDIGEST: /* Digest */
5370 peer_clear_digest_early = value;
5374 * Unpeer Early policy choices
5377 case PROTO_UECRYPTO: /* Crypto */
5378 unpeer_crypto_early = value;
5381 case PROTO_UECRYPTONAK: /* Crypto_NAK */
5382 unpeer_crypto_nak_early = value;
5385 case PROTO_UEDIGEST: /* Digest */
5386 unpeer_digest_early = value;
5391 "proto: unsupported option %d", item);
5397 * proto_clr_stats - clear protocol stat counters
5400 proto_clr_stats(void)
5402 sys_stattime = current_time;
5411 sys_limitrejected = 0;