Lines Matching +full:layer +full:- +full:alpha +full:- +full:mode
2 * ntp_proto.c - NTP version 4 protocol machinery
31 # define BDELAY_DEFAULT (-0.050)
38 x.l_ui &= ~((1 << SRVFUZ_SHIFT) - 1U); \
50 AUTH_UNKNOWN = -1, /* Unknown */
158 int peer_ntpdate; /* active peers in ntpdate mode */
167 int sys_ceiling = STRATUM_UNSPEC - 1; /* cluster stratum ceiling */
179 * Statistics counters - first the good, then the bad
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.
199 * for a bad-guy to DoS us. So look and see what bites you harder
314 if (rbufp->recv_length == base_packet_length) {
318 remainder_size = rbufp->recv_length - base_packet_length;
340 rpkt = &rbufp->recv_pkt;
352 if (!peer || (!peer->keyid && !(peer->flags & FLAG_SKEY))) {
360 if (FLAG_LOOPNONCE & peer->flags) {
361 myorg = &peer->nonce;
363 if (peer->flip > 0) {
364 myorg = &peer->borg;
366 myorg = &peer->aorg;
370 NTOHL_FP(&rpkt->org, &p_org);
379 * as safe as we can ever be with an unauthenticated crypto-nak.
386 * transmit - transmit procedure called by poll timeout
401 hpoll = peer->hpoll;
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;
423 * In manycast mode we start with unity ttl. The ttl is
431 * this code is not quite right -- peer->unreach counts polls
435 if (peer->cast_flags & MDF_ACAST) {
436 peer->outdate = current_time;
438 if (peer->unreach > sys_beacon) {
439 peer->unreach = 0;
440 peer->ttl = 0;
444 if (peer->ttl < sys_ttlmax)
445 peer->ttl++;
448 peer->unreach++;
464 if (peer->cast_flags & MDF_POOL) {
465 peer->outdate = current_time;
476 if (MDF_PCLNT & peer->cast_flags) {
477 if ( (IS_IPV6(&peer->srcadr) && !nonlocal_v6_addr_up)
490 if (peer->burst == 0) {
498 oreach = peer->reach;
499 peer->outdate = current_time;
500 peer->unreach++;
501 peer->reach <<= 1;
502 if (!peer->reach) {
514 if ( (peer->flags & FLAG_IBURST)
515 && peer->retry == 0)
516 peer->retry = NTP_RETRY;
527 if (!(peer->flags & FLAG_PREEMPT))
528 peer->unreach = 0;
529 if ( (peer->flags & FLAG_BURST)
530 && peer->retry == 0
532 peer->retry = NTP_RETRY;
543 if (peer->unreach >= NTP_UNREACH) {
546 if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
552 if ( (peer->flags & FLAG_PREEMPT)
562 peer->burst--;
563 if (peer->burst == 0) {
566 * If ntpdate mode and the clock has not been
571 peer_ntpdate--;
583 if (peer->retry > 0)
584 peer->retry--;
587 * Do not transmit if in broadcast client mode.
589 poll_update(peer, hpoll, (peer->hmode == MODE_CLIENT));
590 if (peer->hmode != MODE_BCLIENT)
625 * receive - receive procedure called for each packet received
636 u_char hismode; /* packet mode */
645 nak_code crypto_nak_test; /* result of crypto-NAK check */
679 * length for control and private mode packets must be checked
681 * later in order to generate a kiss-o'-death packet.
686 * of 2 bytes (for mode 6/7) must be checked first.
689 if (0 == SRCPORT(&rbufp->recv_srcadr) || rbufp->recv_length < 2) {
693 restrictions(&rbufp->recv_srcadr, &r4a);
696 pkt = &rbufp->recv_pkt;
697 hisversion = PKT_VERSION(pkt->li_vn_mode);
698 hismode = (int)PKT_MODE(pkt->li_vn_mode);
735 if (rbufp->recv_length < LEN_PKT_NOMAC) {
740 hisleap = PKT_LEAP(pkt->li_vn_mode);
741 hisstratum = PKT_TO_STRATUM(pkt->stratum);
744 DPRINTF(1, ("receive: at %ld %s<-%s ippeerlimit %d mode %d iflags %s "
746 current_time, stoa(&rbufp->dstadr->sin),
747 stoa(&rbufp->recv_srcadr), r4a.ippeerlimit, hismode,
748 iflags_str(rbufp->dstadr->flags),
750 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
751 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
766 ** Format Layer Checks
773 ** In case of a format layer check violation, the packet is
793 * Figure out his mode and validate the packet. This has some
795 * NTP versions mode 0 was equivalent to what later versions
796 * would interpret as client mode.
804 return; /* invalid mode */
810 * 0 probably indicates a data-minimized packet.
817 * At this point, the packet cannot be a mode[67] packet.
819 if ( pkt->ppoll
820 && ( (NTP_MINPOLL > pkt->ppoll)
821 || (NTP_MAXPOLL < pkt->ppoll)
826 pkt->ppoll, stoa(&rbufp->recv_srcadr)));
831 pkt->ppoll, stoa(&rbufp->recv_srcadr)));
840 * packet is a crypto-NAK; if 3, the packet is authenticated
859 has_mac = rbufp->recv_length - authlen;
868 DPRINTF(2, ("receive: drop: bad post-packet length\n"));
873 * This next test is clearly wrong - it needlessly
885 || (int)len + authlen > rbufp->recv_length) {
893 * sys_groupname is non-NULL, there must be
899 hostlen = ntohl(ep->vallen);
901 || hostlen > len -
907 memcpy(hostname, &ep->pkt, hostlen);
919 has_mac -= len;
927 DPRINTF(2, ("receive: drop: post-packet under-read\n"));
933 ** Packet Data Verification Layer
935 ** This layer verifies the packet data content. If
938 ** Crypto-NAK? Look - a shiny thing!
965 DPRINTF(1, ("Possibly self-induced rate limiting of MODE_SERVER from %s\n",
966 stoa(&rbufp->recv_srcadr)));
1004 dstadr_sin = &rbufp->dstadr->sin;
1005 NTOHL_FP(&pkt->org, &p_org);
1006 NTOHL_FP(&pkt->rec, &p_rec);
1007 NTOHL_FP(&pkt->xmt, &p_xmt);
1023 * The NOPEER and NOTRUST can be specified on a per-client basis
1030 * CRYPTO crypto-NAK. The MAC has four octets only.
1039 * Drop any invalid crypto-NAKs
1044 peer->badNAK++;
1046 msyslog(LOG_ERR, "Invalid-NAK error at %ld %s<-%s",
1047 current_time, stoa(dstadr_sin), stoa(&rbufp->recv_srcadr));
1054 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org 0x%x.%08x xmt 0x%x.%08x NOMAC\n",
1056 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1058 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1059 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1062 is_authentic = AUTH_CRYPTO; /* crypto-NAK */
1063 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org 0x%x.%08x xmt 0x%x.%08x CRYPTONAK\n",
1065 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1067 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1068 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1074 * wanting AD-style authentication of the server's
1077 * This is described in Microsoft's WSPP docs, in MS-SNTP:
1078 * http://msdn.microsoft.com/en-us/library/cc212930.aspx
1084 MAX_MD5_LEN - 4) == 0)) {
1086 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s len %d org %x.%08x xmt %x.%08x SIGND\n",
1088 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1090 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1091 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1098 * Not an MS-SNTP SIGND packet
1153 && rbufp->dstadr ==
1154 ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr)) {
1160 if (!SOCK_UNSPEC(&rbufp->dstadr->bcast))
1162 &rbufp->dstadr->bcast;
1165 &rbufp->recv_srcadr, dstadr_sin, 0,
1168 pkeyid = peer->pcookie;
1179 session_key(&rbufp->recv_srcadr,
1182 &rbufp->recv_srcadr, dstadr_sin,
1186 &rbufp->recv_srcadr, dstadr_sin,
1209 DPRINTF(1, ("receive: at %ld %s<-%s mode %d/%s:%s keyid %08x len %d auth %d org 0x%x.%08x xmt 0x%x.%08x MAC\n",
1211 stoa(&rbufp->recv_srcadr), hismode, hm_str, am_str,
1213 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
1214 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf)));
1222 * - If we expect a MAC and it's not there, we drop it.
1223 * - If we expect one keyID and get another, we drop it.
1224 * - If we have a MAC ahd it hasn't been validated yet, try.
1225 * - if the provided MAC doesn't validate, we drop it.
1229 if (0 != peer && 0 != peer->keyid) {
1234 * - no keyID where one is expected,
1235 * - different keyID than what we expect.
1237 if (peer->keyid != skeyid) {
1239 peer->keyid, skeyid,
1240 stoa(&rbufp->recv_srcadr)));
1247 * - If it has not yet been validated, do so.
1249 * - if missing or bad MAC, log and drop.
1255 stoa(&rbufp->recv_srcadr)));
1266 stoa(&rbufp->recv_srcadr)));
1275 ** On-Wire Protocol Layer
1277 ** Verify protocol operations consistent with the on-wire protocol.
1301 DPRINTF(1, ("receive: MATCH_ASSOC dispatch: mode %d/%s:%s \n",
1306 * This is a client mode packet not matching any association. If
1307 * an ordinary client, simply toss a server mode packet back
1317 * send a crypto-NAK.
1319 if (!(rbufp->dstadr->flags & INT_MCASTOPEN)) {
1321 record_raw_stats(&rbufp->recv_srcadr,
1322 &rbufp->dstadr->sin,
1323 &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1324 PKT_LEAP(pkt->li_vn_mode),
1325 PKT_VERSION(pkt->li_vn_mode),
1326 PKT_MODE(pkt->li_vn_mode),
1327 PKT_TO_STRATUM(pkt->stratum),
1328 pkt->ppoll,
1329 pkt->precision,
1330 FPTOD(NTOHS_FP(pkt->rootdelay)),
1331 FPTOD(NTOHS_FP(pkt->rootdisp)),
1332 pkt->refid,
1333 rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1382 || rbufp->dstadr->addr_refid == pkt->refid
1384 || ( IS_IPV6(&rbufp->dstadr->sin)
1385 &&rbufp->dstadr->old_refid == pkt->refid)
1388 DPRINTF(2, ("receive: sys leap: %0x, sys_stratum %d > hisstratum+1 %d, !sys_cohort %d && sys_stratum == hisstratum+1, loop refid %#x == pkt refid %#x\n", sys_leap, sys_stratum, hisstratum + 1, !sys_cohort, rbufp->dstadr->addr_refid, pkt->refid));
1395 * Do not respond if the packet came into an IPv6 link-local
1399 if (INT_LL_OF_GLOB & rbufp->dstadr->flags) {
1400 DPRINTF(2, ("receive: declining manycast solicitation on link-local IPv6\n"));
1407 * crypto-NAK, as that would not be useful.
1410 record_raw_stats(&rbufp->recv_srcadr,
1411 &rbufp->dstadr->sin,
1412 &p_org, &p_rec, &p_xmt, &rbufp->recv_time,
1413 PKT_LEAP(pkt->li_vn_mode),
1414 PKT_VERSION(pkt->li_vn_mode),
1415 PKT_MODE(pkt->li_vn_mode),
1416 PKT_TO_STRATUM(pkt->stratum),
1417 pkt->ppoll,
1418 pkt->precision,
1419 FPTOD(NTOHS_FP(pkt->rootdelay)),
1420 FPTOD(NTOHS_FP(pkt->rootdisp)),
1421 pkt->refid,
1422 rbufp->recv_length - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
1431 * This is a server mode packet returned in response to a client
1432 * mode packet sent to a multicast group address (for
1464 if (INT_LL_OF_GLOB & rbufp->dstadr->flags) {
1465 DPRINTF(2, ("receive: AM_MANYCAST drop: link-local server\n"));
1474 if (!AUTH( (!(peer2->cast_flags & MDF_POOL)
1497 if (MDF_POOL & peer2->cast_flags) {
1500 peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1502 peer2->minpoll, peer2->maxpoll,
1503 (FLAG_PREEMPT | (POOL_FLAG_PMASK & peer2->flags)),
1516 if (peer2->cast_flags & MDF_POOL)
1517 peer2->nextdate = current_time + 1;
1522 * brand-new association (it matches the prototype
1523 * association) and tinker with peer->nextdate delaying
1592 * if a non-NULL endpoint is supplied. multicastclient
1596 if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
1597 match_ep = rbufp->dstadr;
1607 * If a two-way exchange is not possible,
1612 DPRINTF(2, ("receive: AM_NEWBCL drop: Autokey but not 2-way\n"));
1619 * broadcast client mode.
1621 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1623 pkt->ppoll, pkt->ppoll,
1631 peer->delay = sys_bdelay;
1632 peer->bxmt = p_xmt;
1645 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1647 pkt->ppoll, pkt->ppoll,
1655 peer->bxmt = p_xmt;
1724 * crypto-NAKs (is_authentic == AUTH_CRYPTO)
1726 * debug-printed and not logged to avoid log
1730 " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1731 current_time, stoa(&rbufp->recv_srcadr),
1767 if ((peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1769 pkt->ppoll, NTP_MAXDPOLL, 0, MDF_UCAST, 0,
1789 if (group_test(groupname, peer->ident)) {
1802 (current_time - peer->timelastrec),
1803 peer->ppoll, (1 << peer->ppoll)
1808 * Is the poll interval in the packet in-range?
1814 /* This is noteworthy, not error-worthy */
1815 if (pkt->ppoll != peer->ppoll) {
1817 stoa(&rbufp->recv_srcadr),
1818 peer->ppoll, pkt->ppoll);
1821 /* This is error-worthy */
1822 if ( pkt->ppoll < peer->minpoll
1823 || pkt->ppoll > peer->maxpoll) {
1824 msyslog(LOG_INFO, "receive: broadcast poll of %u from %s is out-of-range (%d to %d)!",
1825 pkt->ppoll, stoa(&rbufp->recv_srcadr),
1826 peer->minpoll, peer->maxpoll);
1840 deadband = (1u << pkt->ppoll);
1841 if (FLAG_BC_VOL & peer->flags)
1842 deadband -= 3; /* allow greater fuzz after volley */
1843 if ((current_time - peer->timereceived) < deadband) {
1845 stoa(&rbufp->recv_srcadr),
1846 (current_time - peer->timereceived),
1851 /* Alert if time from the server is non-monotonic.
1853 * [Bug 3114] is about Broadcast mode replay DoS.
1855 * Broadcast mode *assumes* a trusted network.
1863 * - the broadcast server had a backward step.
1865 * - somebody is trying a replay attack.
1874 /* pkt->ppoll or peer->ppoll ? */
1875 deadband = (1u << pkt->ppoll)
1881 if (L_ISZERO(&peer->bxmt)) {
1885 L_SUB(&tdiff, &peer->bxmt);
1888 && (current_time - peer->timereceived) < deadband)
1890 msyslog(LOG_INFO, "receive: broadcast packet from %s contains non-monotonic timestamp: 0x%x.%08x -> 0x%x.%08x",
1891 stoa(&rbufp->recv_srcadr),
1892 peer->bxmt.l_ui, peer->bxmt.l_uf,
1900 peer->timelastrec = current_time;
1935 && ( ((peer->flags & FLAG_SKEY) && skeyid <= NTP_MAXKEY)
1936 || (!(peer->flags & FLAG_SKEY) && skeyid > NTP_MAXKEY))) {
1943 peer->received++;
1944 peer->flash &= ~PKT_TEST_MASK;
1945 if (peer->flags & FLAG_XBOGUS) {
1946 peer->flags &= ~FLAG_XBOGUS;
1947 peer->flash |= TEST3;
1959 kissCode = kiss_code_check(hisleap, hisstratum, hismode, pkt->refid);
1962 peer->flash |= TEST3; /* unsynch */
1964 peer->bogusorg++; /* for TEST2 or TEST3 */
1967 ntoa(&peer->srcadr));
1976 } else if ( ((FLAG_LOOPNONCE & peer->flags) && L_ISEQU(&peer->nonce, &p_xmt))
1977 || (!(FLAG_LOOPNONCE & peer->flags) && L_ISEQU(&peer->xmt, &p_xmt))
1980 peer->flash |= TEST1; /* duplicate */
1981 peer->oldpkt++;
1985 * If this is a broadcast mode packet, make sure hisstratum
1986 * is appropriate. Don't do anything else here - we wait to
1990 * hisstratum cannot be 0 - see assertion above.
2000 hisstratum, ntoa(&peer->srcadr));
2007 * KoD packets are a mixed-blessing. Forged KoD packets
2015 * that we have previously sent out. Watch interleave mode.
2021 peer->bogusorg++;
2024 ntoa(&peer->srcadr));
2030 peer->bogusorg++;
2033 ntoa(&peer->srcadr));
2038 if (peer->flip == 0 && !L_ISEQU(&p_org, &peer->aorg)) {
2039 peer->bogusorg++;
2041 "receive: flip 0 KoD origin timestamp 0x%x.%08x from %s does not match 0x%x.%08x - ignoring.",
2043 ntoa(&peer->srcadr),
2044 peer->aorg.l_ui, peer->aorg.l_uf);
2046 } else if (peer->flip == 1 && !L_ISEQU(&p_org, &peer->borg)) {
2047 peer->bogusorg++;
2049 "receive: flip 1 KoD origin timestamp 0x%x.%08x from %s does not match interleave 0x%x.%08x - ignoring.",
2051 ntoa(&peer->srcadr),
2052 peer->borg.l_ui, peer->borg.l_uf);
2057 * Basic mode checks:
2061 * Of course, should 'aorg' be all-zero because this really
2068 * Otherwise, check for bogus packet in basic mode.
2069 * If it is bogus, switch to interleaved mode and
2071 * not bogus in symmetric interleaved mode.
2081 DEBUG_INSIST(0 == (FLAG_LOOPNONCE & peer->flags));
2085 ntoa(&peer->srcadr), 1u << pkt->ppoll);
2088 refid_str(pkt->refid, -1),
2089 ntoa(&peer->srcadr));
2091 } else if (peer->flip == 0) {
2098 "receive: BUG 3361: Clearing peer->aorg ");
2099 L_CLR(&peer->aorg);
2100 /* Clear peer->nonce, too? */
2116 peer->bogusorg++;
2117 peer->flash |= TEST2; /* bogus */
2127 action, hm_str, ntoa(&peer->srcadr),
2128 ntohl(pkt->xmt.l_ui), ntohl(pkt->xmt.l_uf));
2129 } else if ( L_ISZERO(&peer->aorg) && MODE_CLIENT != hismode
2130 && !memcmp("STEP", &peer->refid, 4)) {
2132 } else if (!L_ISEQU(&p_org, &peer->aorg)) {
2135 peer->bogusorg++;
2136 peer->flash |= TEST2; /* bogus */
2139 ntohl(pkt->org.l_ui), ntohl(pkt->org.l_uf),
2140 peer->aorg.l_ui, peer->aorg.l_uf,
2141 hm_str, ntoa(&peer->srcadr));
2142 if ( !L_ISZERO(&peer->dst)
2143 && L_ISEQU(&p_org, &peer->dst)) {
2146 peer->flip = 1;
2151 hm_str, ntoa(&peer->srcadr));
2155 L_CLR(&peer->aorg);
2164 || L_ISZERO(&peer->dst)) {
2165 peer->flash |= TEST3; /* unsynch */
2168 * Check for bogus packet in interleaved symmetric mode. This
2172 } else if ( !L_ISZERO(&peer->dst)
2173 && !L_ISEQU(&p_org, &peer->dst)) {
2174 DPRINTF(2, ("receive: drop: Bogus packet in interleaved symmetric mode\n"));
2175 peer->bogusorg++;
2176 peer->flags |= FLAG_XBOGUS;
2177 peer->flash |= TEST2; /* bogus */
2192 peer->flash |= TEST5; /* bad auth */
2193 peer->badauth++;
2194 if (peer->flags & FLAG_PREEMPT) {
2202 if (peer->crypto) {
2213 * is designed to avoid a bait-and-switch attack, which was
2215 * crypto-NAK. The peer should restart the protocol.
2217 } else if (!AUTH(peer->keyid || has_mac ||
2220 if (peer->flash & PKT_TEST_MASK) {
2222 "receive: Bad auth in packet with bad timestamps from %s denied - spoof?",
2223 ntoa(&peer->srcadr));
2228 peer->flash |= TEST5; /* bad auth */
2229 peer->badauth++;
2234 if (peer->flags & FLAG_PREEMPT) {
2240 else if (peer_clear_digest_early && peer->crypto) {
2257 * - if the origin timestamp is nonzero this is an
2259 * - else, this is not an interleaved broadcast packet.
2265 if (!(peer->flags & FLAG_XB)) {
2267 "receive: Broadcast server at %s is in interleave mode",
2268 ntoa(&peer->srcadr));
2269 peer->flags |= FLAG_XB;
2270 peer->aorg = p_xmt;
2271 peer->borg = rbufp->recv_time;
2275 } else if (peer->flags & FLAG_XB) {
2277 "receive: Broadcast server at %s is no longer in interleave mode",
2278 ntoa(&peer->srcadr));
2279 peer->flags &= ~FLAG_XB;
2284 is_authentic, ntoa(&peer->srcadr));
2289 * update peer->bxmt.
2291 peer->bxmt = p_xmt;
2298 if (peer->flip == 0) {
2300 peer->rec = p_xmt;
2301 peer->dst = rbufp->recv_time;
2303 peer->xmt = p_xmt;
2307 * peer minpoll. If a kiss-o'-death, set the peer minpoll to
2322 if ( (NTP_MINPOLL > pkt->ppoll)
2323 || (NTP_MAXPOLL < pkt->ppoll)
2326 pkt->ppoll, stoa(&rbufp->recv_srcadr)));
2330 peer->ppoll = max(peer->minpoll, pkt->ppoll);
2332 peer->selbroken++; /* Increment the KoD count */
2334 if (pkt->ppoll > peer->minpoll)
2335 peer->minpoll = peer->ppoll;
2336 peer->burst = peer->retry = 0;
2337 peer->throttle = (NTP_SHIFT + 1) * (1 << peer->minpoll);
2338 poll_update(peer, pkt->ppoll, 0);
2339 return; /* kiss-o'-death */
2342 peer->selbroken++; /* Increment the KoD count */
2354 * - this is a *cast (uni-, broad-, or m-) server packet
2355 * - and it's symmetric-key authenticated
2357 * If it is, great - nothing special to do here.
2360 * Autokey-authenticated packets are accepted.
2364 case MODE_SERVER: /* server mode */
2365 case MODE_BROADCAST: /* broadcast mode */
2366 case MODE_ACTIVE: /* symmetric active mode */
2367 case MODE_PASSIVE: /* symmetric passive mode */
2371 && !authistrustedip(skeyid, &peer->srcadr)) {
2373 peer->badauth++;
2378 case MODE_CLIENT: /* client mode */
2380 case MODE_CONTROL: /* control mode */
2382 case MODE_PRIVATE: /* private mode */
2383 case MODE_BCLIENT: /* broadcast client mode */
2389 "receive: Unexpected mode (%d) in packet from %s",
2390 hismode, ntoa(&peer->srcadr));
2399 peer->timereceived = current_time;
2400 peer->timelastrec = current_time;
2402 peer->flags |= FLAG_AUTHENTIC;
2404 peer->flags &= ~FLAG_AUTHENTIC;
2408 * More autokey dance. The rules of the cha-cha are as follows:
2417 * self-authenticated and we sit the dance.
2430 if (peer->flags & FLAG_SKEY) {
2435 ap = (struct autokey *)peer->recval.ptr;
2437 if (ap->seq > 0)
2438 ap->seq--;
2440 peer->flash |= TEST8;
2443 peer->unreach = 0;
2449 peer->flash |= TEST9; /* bad crypt */
2450 if (peer->flags & FLAG_PREEMPT) {
2460 * If server mode, verify the receive key ID matches
2464 if (skeyid == peer->keyid)
2465 peer->flash &= ~TEST8;
2472 } else if (!(peer->flash & TEST8)) {
2473 peer->pkeyid = skeyid;
2489 if ( tkeyid == peer->pkeyid
2490 || tkeyid == ap->key) {
2491 peer->flash &= ~TEST8;
2492 peer->pkeyid = skeyid;
2493 ap->seq -= i;
2496 if (i > ap->seq) {
2497 peer->crypto &=
2502 &rbufp->recv_srcadr, dstadr_sin,
2505 if (peer->flash & TEST8)
2508 if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
2509 peer->flash |= TEST8; /* bad autokey */
2516 if (current_time > peer->refresh) {
2530 process_packet(peer, pkt, rbufp->recv_length);
2532 if ((~TEST3 & peer->flash) & PKT_TEST_MASK) {
2540 poll_update(peer, peer->hpoll, (peer->hmode == MODE_CLIENT));
2543 * In interleaved mode update the state variables. Also adjust the
2546 if (peer->flip != 0) {
2547 peer->rec = p_rec;
2548 peer->dst = rbufp->recv_time;
2549 if (peer->nextdate - current_time < (1U << min(peer->ppoll,
2550 peer->hpoll)) / 2)
2551 peer->nextdate++;
2553 peer->nextdate--;
2559 * process_packet - Packet Procedure, a la Section 3.4.4 of RFC-1305
2580 p_del = FPTOD(NTOHS_FP(pkt->rootdelay));
2582 p_disp = FPTOD(NTOHS_FP(pkt->rootdisp));
2583 NTOHL_FP(&pkt->reftime, &p_reftime);
2584 NTOHL_FP(&pkt->org, &p_org);
2585 NTOHL_FP(&pkt->rec, &p_rec);
2586 NTOHL_FP(&pkt->xmt, &p_xmt);
2587 pmode = PKT_MODE(pkt->li_vn_mode);
2588 pleap = PKT_LEAP(pkt->li_vn_mode);
2589 pversion = PKT_VERSION(pkt->li_vn_mode);
2590 pstratum = PKT_TO_STRATUM(pkt->stratum);
2598 peer->flash |= TEST6; /* bad synch or strat */
2600 peer->flash |= TEST7; /* bad header */
2607 if (peer->flash & PKT_TEST_MASK) {
2608 peer->seldisptoolarge++;
2610 peer->flash));
2619 peer->processed++;
2624 record_raw_stats(&peer->srcadr,
2625 peer->dstadr ? &peer->dstadr->sin : NULL,
2626 &p_org, &p_rec, &p_xmt, &peer->dst,
2627 pleap, pversion, pmode, pstratum, pkt->ppoll, pkt->precision,
2628 p_del, p_disp, pkt->refid,
2629 len - MIN_V4_PKT_LEN, (u_char *)&pkt->exten);
2630 peer->leap = pleap;
2631 peer->stratum = min(pstratum, STRATUM_UNSPEC);
2632 peer->pmode = pmode;
2633 peer->precision = pkt->precision;
2634 peer->rootdelay = p_del;
2635 peer->rootdisp = p_disp;
2636 peer->refid = pkt->refid; /* network byte order */
2637 peer->reftime = p_reftime;
2640 * First, if either burst mode is armed, enable the burst.
2644 if (peer->retry > 0) {
2645 peer->retry = 0;
2646 if (peer->reach)
2647 peer->burst = min(1 << (peer->hpoll -
2648 peer->minpoll), NTP_SHIFT) - 1;
2650 peer->burst = NTP_IBURST - 1;
2651 if (peer->burst > 0)
2652 peer->nextdate = current_time;
2659 if (!peer->reach) {
2661 peer->timereachable = current_time;
2663 peer->reach |= 1;
2678 * precision, these calculations are done using 64-bit integer
2680 * sums and differences of these first-order differences, which
2681 * if done using 64-bit integer arithmetic, would be valid over
2682 * only half that span. Since the typical first-order
2683 * differences are usually very small, they are converted to 64-
2684 * bit doubles and all remaining calculations done in floating-
2686 * retaining the 68-year span.
2693 * Interleaved symmetric mode
2694 * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
2695 * t4 = peer->dst
2697 if (peer->flip != 0) {
2698 ci = p_xmt; /* t3 - t4 */
2699 L_SUB(&ci, &peer->dst);
2701 ci = p_rec; /* t2 - t1 */
2702 if (peer->flip > 0)
2703 L_SUB(&ci, &peer->borg);
2705 L_SUB(&ci, &peer->aorg);
2707 p_del = t21 - t34;
2718 } else if (peer->pmode == MODE_BROADCAST) {
2721 * Interleaved broadcast mode. Use interleaved timestamps.
2722 * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
2724 if (peer->flags & FLAG_XB) {
2726 L_SUB(&ci, &peer->aorg);
2728 ci = p_org; /* t2 - t1 */
2729 L_SUB(&ci, &peer->borg);
2731 peer->aorg = p_xmt;
2732 peer->borg = peer->dst;
2735 if (FLAG_BC_VOL & peer->flags)
2743 peer->xleave = t34;
2746 * Basic broadcast - use direct timestamps.
2747 * t3 = p_xmt, t4 = peer->dst
2750 ci = p_xmt; /* t3 - t4 */
2751 L_SUB(&ci, &peer->dst);
2766 if (FLAG_BC_VOL & peer->flags) {
2767 peer->flags &= ~FLAG_BC_VOL;
2768 peer->delay = fabs(peer->offset - p_offset) * 2;
2770 peer->delay));
2771 if (peer->delay > fabs(sys_bdelay)) {
2778 peer->nextdate = current_time + (1u << peer->ppoll) - 2u;
2779 p_del = peer->delay;
2784 * Basic mode, otherwise known as the old fashioned way.
2786 * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2789 ci = p_xmt; /* t3 - t4 */
2790 L_SUB(&ci, &peer->dst);
2792 ci = p_rec; /* t2 - t1 */
2795 p_del = fabs(t21 - t34);
2799 p_disp = LOGTOD(sys_precision) + LOGTOD(peer->precision) +
2810 if (peer->t21_last > 0 && peer->t34_bytes > 0) {
2811 itemp = peer->t21_bytes - peer->t21_last;
2813 etemp = t21 - peer->t21;
2814 if (fabs(etemp) > 1e-6) {
2817 peer->r21 = ftemp;
2820 itemp = len - peer->t34_bytes;
2822 etemp = -t34 - peer->t34;
2823 if (fabs(etemp) > 1e-6) {
2826 peer->r34 = ftemp;
2834 * it finds t such that r21 * t - r34 * (d - t) = 0, where d is
2838 peer->t21 = t21;
2839 peer->t21_last = peer->t21_bytes;
2840 peer->t34 = -t34;
2841 peer->t34_bytes = len;
2842 DPRINTF(2, ("packet: t21 %.9lf %d t34 %.9lf %d\n", peer->t21,
2843 peer->t21_bytes, peer->t34, peer->t34_bytes));
2844 if (peer->r21 > 0 && peer->r34 > 0 && p_del > 0) {
2845 if (peer->pmode != MODE_BROADCAST)
2846 td = (peer->r34 / (peer->r21 + peer->r34) -
2858 t21 -= td;
2859 t34 -= td;
2861 p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2869 clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2872 * If we are in broadcast calibrate mode, return to broadcast
2873 * client mode when the client is fit and the autokey dance is
2876 if ( (FLAG_BC_VOL & peer->flags)
2877 && MODE_CLIENT == peer->hmode
2880 if (peer->flags & FLAG_SKEY) {
2881 if (!(~peer->crypto & CRYPTO_FLAG_ALL))
2882 peer->hmode = MODE_BCLIENT;
2884 peer->hmode = MODE_BCLIENT;
2887 peer->hmode = MODE_BCLIENT;
2894 * clock_update - Called at system process update intervals.
2912 sys_epoch = peer->epoch;
2913 if (sys_poll < peer->minpoll)
2914 sys_poll = peer->minpoll;
2915 if (sys_poll > peer->maxpoll)
2916 sys_poll = peer->maxpoll;
2918 sys_stratum = min(peer->stratum + 1, STRATUM_UNSPEC);
2919 if ( peer->stratum == STRATUM_REFCLOCK
2920 || peer->stratum == STRATUM_UNSPEC)
2921 sys_refid = peer->refid;
2923 sys_refid = addr2refid(&peer->srcadr);
2927 * E = p.epsilon_r + p.epsilon + p.psi + PHI*(s.t - p.t) + |THETA|
2936 * what they mean. When did peer->update happen? Has anything
2942 dtemp = peer->rootdisp
2943 + peer->disp
2945 + clock_phi * (current_time - peer->update)
2955 sys_rootdelay = peer->delay + peer->rootdelay;
2961 prev_time = current_time + 64 + (rand() & 0x3f); /* 64-127 s */
2963 sys_reftime = peer->dst;
2966 current_time, peer->epoch, peer->associd));
2977 case -1:
2981 * For Solaris enter the maintenance mode.
3000 exit (-1);
3074 * poll_update - update peer poll interval
3095 hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
3103 if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
3106 peer->hpoll = hpoll;
3125 utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
3126 (1 << peer->minpoll), ntp_minpkt);
3131 limit -= (limit >> 2);
3132 limit += peer->outdate;
3136 if (peer->burst > 0) {
3137 if (peer->nextdate > current_time)
3140 else if (peer->flags & FLAG_REFCLOCK)
3141 peer->nextdate = current_time + RESP_DELAY;
3144 peer->nextdate = utemp;
3151 } else if (peer->cmmd != NULL) {
3152 if (peer->nextdate > current_time) {
3153 if (peer->nextdate + ntp_minpkt != utemp)
3154 peer->nextdate = utemp;
3156 peer->nextdate = utemp;
3169 if (peer->retry > 0)
3170 hpoll = peer->minpoll;
3172 hpoll = min(peer->ppoll, peer->hpoll);
3174 if (peer->flags & FLAG_REFCLOCK)
3180 next += peer->outdate;
3197 next -= sub;
3205 peer->nextdate = next;
3207 peer->nextdate = utemp;
3208 if (peer->throttle > (1 << peer->minpoll))
3209 peer->nextdate += ntp_minpkt;
3213 if (peer->nextdate > limit) {
3215 limit, peer->nextdate));
3216 peer->nextdate = limit;
3219 current_time, ntoa(&peer->srcadr), peer->hpoll,
3220 peer->burst, peer->retry, peer->throttle,
3221 utemp - current_time, peer->nextdate -
3227 * peer_clear - clear peer filter registers. See Section 3.4.8 of the
3238 l_fp bxmt = peer->bxmt; /* bcast clients retain this! */
3251 if (peer->iffval != NULL)
3252 BN_free(peer->iffval);
3253 value_free(&peer->cookval);
3254 value_free(&peer->recval);
3255 value_free(&peer->encrypt);
3256 value_free(&peer->sndval);
3257 if (peer->cmmd != NULL)
3258 free(peer->cmmd);
3259 if (peer->subject != NULL)
3260 free(peer->subject);
3261 if (peer->issuer != NULL)
3262 free(peer->issuer);
3269 peer->ppoll = peer->maxpoll;
3270 peer->hpoll = peer->minpoll;
3271 peer->disp = MAXDISPERSE;
3272 peer->flash = peer_unfit(peer);
3273 peer->jitter = LOGTOD(sys_precision);
3276 if (peer->hmode == MODE_BCLIENT)
3277 peer->bxmt = bxmt;
3280 * If interleave mode, initialize the alternate origin switch.
3282 if (peer->flags & FLAG_XLEAVE)
3283 peer->flip = 1;
3285 peer->filter_order[u] = u;
3286 peer->filter_disp[u] = MAXDISPERSE;
3289 if (!(peer->flags & FLAG_REFCLOCK)) {
3291 peer->leap = LEAP_NOTINSYNC;
3292 peer->stratum = STRATUM_UNSPEC;
3293 memcpy(&peer->refid, ident, 4);
3297 peer->procptr->codeproc = 0;
3298 peer->procptr->coderecv = 0;
3304 * the polls at one-second intervals. Unconfigured associations'
3306 * rate limiting. Other post-startup new or cleared associations
3310 peer->nextdate = peer->update = peer->outdate = current_time;
3312 peer->nextdate += peer_associations;
3313 } else if (!(FLAG_CONFIG & peer->flags)) {
3314 peer->nextdate += ntp_minpkt + 1;
3316 if (peer->nextdate < earliest) {
3317 peer->nextdate = earliest;
3319 earliest = peer->nextdate + 1;
3321 peer->nextdate += ntp_random() % (1 << peer->minpoll);
3324 peer->refresh = current_time + (1 << NTP_REFRESH);
3327 current_time, peer->nextdate, peer->associd,
3333 * clock_filter - add incoming clock sample to filter register and run
3352 * of arrival. The offset and delay are determined by the on-
3359 j = peer->filter_nextpt;
3360 peer->filter_offset[j] = sample_offset;
3361 peer->filter_delay[j] = sample_delay;
3362 peer->filter_disp[j] = sample_disp;
3363 peer->filter_epoch[j] = current_time;
3365 peer->filter_nextpt = (u_char)j;
3378 dtemp = clock_phi * (current_time - peer->update);
3379 peer->update = current_time;
3380 for (i = NTP_SHIFT - 1; i >= 0; i--) {
3382 peer->filter_disp[j] += dtemp;
3383 if (peer->filter_disp[j] >= MAXDISPERSE) {
3384 peer->filter_disp[j] = MAXDISPERSE;
3386 } else if (peer->update - peer->filter_epoch[j] >
3388 dst[i] = peer->filter_delay[j] +
3389 peer->filter_disp[j];
3391 dst[i] = peer->filter_delay[j];
3426 peer->filter_order[i] = ord[i];
3439 peer->disp = peer->jitter = 0;
3441 for (i = NTP_SHIFT - 1; i >= 0; i--) {
3443 peer->disp = NTP_FWEIGHT * ( peer->disp
3444 + peer->filter_disp[j]);
3446 peer->jitter += DIFF(peer->filter_offset[j],
3447 peer->filter_offset[k]);
3461 etemp = fabs(peer->offset - peer->filter_offset[k]);
3462 peer->offset = peer->filter_offset[k];
3463 peer->delay = peer->filter_delay[k];
3465 peer->jitter /= m - 1;
3467 peer->jitter = max(SQRT(peer->jitter), LOGTOD(sys_precision));
3476 if ( peer->disp < sys_maxdist
3477 && peer->filter_disp[k] < sys_maxdist
3478 && etemp > CLOCK_SGATE * peer->jitter
3479 && peer->filter_epoch[k] - peer->epoch
3480 < 2. * ULOGTOD(peer->hpoll)) {
3492 if (peer->filter_epoch[k] <= peer->epoch) {
3494 current_time - peer->filter_epoch[k]));
3497 peer->epoch = peer->filter_epoch[k];
3504 record_peer_stats(&peer->srcadr, ctlpeerstatus(peer), peer->offset,
3505 peer->delay, peer->disp, peer->jitter);
3507 (u_short)m, peer->offset, peer->delay, peer->disp,
3508 peer->jitter));
3509 if (0 == peer->burst || LEAP_NOTINSYNC == sys_leap) {
3516 * clock_select - find the pick-of-the-litter clock
3573 for (peer = peer_list; peer != NULL; peer = peer->p_link)
3594 for (peer = peer_list; peer != NULL; peer = peer->p_link) {
3595 peer->new_status = CTL_PST_SEL_REJECT;
3615 if (!L_ISZERO(&peer->reftime) && d < lastresort_dist) {
3630 * orphan mode in timer().
3632 if (peer->stratum == sys_orphan) {
3636 if (peer->dstadr != NULL)
3637 localmet = ntohl(peer->dstadr->addr_refid);
3640 peermet = ntohl(addr2refid(&peer->srcadr));
3659 if (peer->stratum > sys_orphan) {
3667 if (!(peer->flags & FLAG_PREFER)) {
3668 switch (peer->refclktype) {
3689 peer->new_status = CTL_PST_SEL_SANE;
3692 peers[nlist].error = peer->jitter;
3700 e = peer->offset;
3701 endpoint[nl2].type = -1; /* lower end */
3702 endpoint[nl2].val = e - f;
3758 high = -1e9;
3768 n -= endpoint[indx[i]].type;
3769 if (n >= nlist - allow)
3773 for (j = nl2 - 1; j >= 0; j--) {
3776 if (n >= nlist - allow)
3808 || peer->offset + h < low
3809 || peer->offset - h > high
3810 ) && !(peer->flags & FLAG_TRUE))
3819 if (peer->flags & FLAG_PPS) {
3822 if (!(peer->flags & FLAG_TSTAMP_PPS))
3864 peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
3866 stoa(&peers[i].peer->srcadr), peers[i].synch));
3879 e = -1e9;
3889 f += DIFF(peers[j].peer->offset,
3890 peers[i].peer->offset);
3891 peers[i].seljit = SQRT(f / (nlist - 1));
3902 || ((FLAG_TRUE | FLAG_PREFER) & peers[k].peer->flags))
3906 ntoa(&peers[k].peer->srcadr), g, d));
3908 peers[k].peer->new_status = CTL_PST_SEL_EXCESS;
3910 peers[j - 1] = peers[j];
3911 nlist--;
3944 peer->unreach = 0;
3945 peer->new_status = CTL_PST_SEL_SYNCCAND;
3947 if (peer->leap == LEAP_ADDSECOND) {
3948 if (peer->flags & FLAG_REFCLOCK)
3953 if (peer->leap == LEAP_DELSECOND) {
3954 if (peer->flags & FLAG_REFCLOCK)
3959 if (peer->flags & FLAG_PREFER)
3962 peer->stratum * sys_mindisp;
3982 } else if ((x = fabs(typesystem->offset -
3983 osys_peer->offset)) < sys_mindisp) {
4007 typesystem->new_status = CTL_PST_SEL_SYSPEER;
4012 typesystem->new_status = CTL_PST_SEL_SYSPEER;
4013 sys_offset = typesystem->offset;
4014 sys_jitter = typesystem->jitter;
4029 && ( typepps->refclktype != REFCLK_ATOM_PPS
4030 || ( typepps->refclktype == REFCLK_ATOM_PPS
4035 typesystem->new_status = CTL_PST_SEL_PPS;
4036 sys_offset = typesystem->offset;
4037 sys_jitter = typesystem->jitter;
4054 for (peer = peer_list; peer != NULL; peer = peer->p_link)
4055 peer->status = peer->new_status;
4063 if (typesystem->epoch <= sys_epoch)
4067 * We have found the alpha male. Wind the clock.
4071 for (peer = peer_list; peer != NULL; peer = peer->p_link)
4072 peer->status = peer->new_status;
4091 z += x * peers[i].peer->offset;
4092 w += x * DIFF(peers[i].peer->offset,
4093 peers[syspeer].peer->offset);
4101 * root_distance - compute synchronization distance from peer to root
4115 * delta is the round-trip delay
4128 dtemp = (peer->delay + peer->rootdelay) / 2
4129 + peer->disp
4130 + clock_phi * (current_time - peer->update)
4131 + peer->rootdisp
4132 + peer->jitter;
4147 * peer_xmit - send packet for persistent association.
4159 if (!peer->dstadr) { /* can't send */
4162 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, peer->version,
4163 peer->hmode);
4165 xpkt.ppoll = peer->hpoll;
4172 HTONL_FP(&peer->rec, &xpkt.org);
4173 HTONL_FP(&peer->dst, &xpkt.rec);
4190 !(peer->flags & FLAG_SKEY) &&
4192 peer->keyid == 0) {
4195 * Transmit a-priori timestamps
4198 if (peer->flip == 0) { /* basic mode */
4199 peer->aorg = xmt_tx;
4202 if (peer->hmode == MODE_BROADCAST) { /* bcst */
4204 if (peer->flip > 0)
4205 HTONL_FP(&peer->borg,
4208 HTONL_FP(&peer->aorg,
4211 if (peer->flip > 0)
4212 HTONL_FP(&peer->borg,
4215 HTONL_FP(&peer->aorg,
4219 peer->t21_bytes = sendlen;
4220 sendpkt(&peer->srcadr, peer->dstadr,
4221 sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4223 peer->sent++;
4224 peer->throttle += (1 << peer->minpoll) - 2;
4227 * Capture a-posteriori timestamps
4230 if (peer->flip != 0) { /* interleaved modes */
4231 if (peer->flip > 0)
4232 peer->aorg = xmt_ty;
4234 peer->borg = xmt_ty;
4235 peer->flip = -peer->flip;
4238 LFPTOD(&xmt_ty, peer->xleave);
4239 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d len %zu xmt 0x%x.%08x\n",
4240 current_time, latoa(peer->dstadr),
4241 stoa(&peer->srcadr), peer->hmode, sendlen,
4252 if (peer->flags & FLAG_SKEY) {
4258 * 4-octet length/code word followed by a 4-octet
4260 * data includes a 4-octet data length field followed by
4295 * has expired, so the pseudo-random sequence is
4299 if (peer->keynumber == 0)
4300 make_keylist(peer, peer->dstadr);
4302 peer->keynumber--;
4303 xkeyid = peer->keylist[peer->keynumber];
4309 peer->keyid = xkeyid;
4311 switch (peer->hmode) {
4314 * In broadcast server mode the autokey values are
4321 if (peer->flags & FLAG_ASSOC)
4323 CRYPTO_RESP, peer->associd, NULL);
4326 CRYPTO_RESP, peer->associd, NULL);
4346 if (!peer->crypto)
4348 peer->associd, hostval.ptr);
4349 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4351 peer->associd, peer->issuer);
4352 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4354 crypto_ident(peer), peer->associd,
4369 && peer->leap != LEAP_NOTINSYNC
4370 && !(peer->crypto & CRYPTO_FLAG_COOK))
4372 peer->associd, NULL);
4373 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4375 peer->associd, NULL);
4376 else if ( peer->flags & FLAG_ASSOC
4377 && peer->crypto & CRYPTO_FLAG_SIGN)
4379 CRYPTO_RESP, peer->assoc, NULL);
4389 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4391 peer->associd, hostval.ptr);
4392 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4394 peer->associd, NULL);
4398 * In client mode the parameter, certificate, identity,
4401 * mode the same exchanges are required, except that the
4413 if (!peer->crypto)
4415 peer->associd, hostval.ptr);
4416 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4418 peer->associd, peer->issuer);
4419 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4421 crypto_ident(peer), peer->associd,
4429 else if (!(peer->crypto & CRYPTO_FLAG_COOK))
4431 peer->associd, NULL);
4432 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4434 peer->assoc, NULL);
4444 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4446 peer->associd, hostval.ptr);
4447 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4449 peer->associd, NULL);
4459 if (peer->cmmd != NULL) {
4463 peer->cmmd->opcode |= htonl(temp32);
4465 sendlen, peer->cmmd, 0);
4466 free(peer->cmmd);
4467 peer->cmmd = NULL;
4475 if (exten->opcode != 0)
4486 session_key(&peer->dstadr->sin, &peer->srcadr,
4493 * Transmit a-priori timestamps
4496 if (peer->flip == 0) { /* basic mode */
4497 peer->aorg = xmt_tx;
4500 if (peer->hmode == MODE_BROADCAST) { /* bcst */
4502 if (peer->flip > 0)
4503 HTONL_FP(&peer->borg, &xpkt.org);
4505 HTONL_FP(&peer->aorg, &xpkt.org);
4507 if (peer->flip > 0)
4508 HTONL_FP(&peer->borg, &xpkt.xmt);
4510 HTONL_FP(&peer->aorg, &xpkt.xmt);
4513 xkeyid = peer->keyid;
4517 peer->flash |= TEST5; /* auth error */
4518 peer->badauth++;
4530 peer->t21_bytes = sendlen;
4531 sendpkt(&peer->srcadr, peer->dstadr,
4532 sys_ttl[(peer->ttl >= sys_ttlmax) ? sys_ttlmax : peer->ttl],
4534 peer->sent++;
4535 peer->throttle += (1 << peer->minpoll) - 2;
4538 * Capture a-posteriori timestamps
4541 if (peer->flip != 0) { /* interleaved modes */
4542 if (peer->flip > 0)
4543 peer->aorg = xmt_ty;
4545 peer->borg = xmt_ty;
4546 peer->flip = -peer->flip;
4549 LFPTOD(&xmt_ty, peer->xleave);
4551 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu index %d\n",
4552 current_time, latoa(peer->dstadr), stoa(&peer->srcadr),
4553 peer->hmode, xkeyid, sendlen, peer->keynumber));
4555 DPRINTF(1, ("peer_xmit: at %ld %s->%s mode %d keyid %08x len %zu\n",
4556 current_time, peer->dstadr ?
4557 ntoa(&peer->dstadr->sin) : "-",
4558 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
4587 * fast_xmit - Send packet for nonpersistent association. Note that
4593 int xmode, /* receive mode */ /* XXX: HMS: really? */
4614 rpkt = &rbufp->recv_pkt;
4621 if (rbufp->dstadr->flags & INT_MCASTOPEN) {
4622 rbufp->dstadr = findinterface(&rbufp->recv_srcadr);
4623 if (NULL == rbufp->dstadr ||
4624 ANY_INTERFACE_CHOOSE(&rbufp->recv_srcadr) /* wildcard */
4625 == rbufp->dstadr) {
4628 stoa(&rbufp->recv_srcadr)));
4635 * If this is a kiss-o'-death (KoD) packet, show leap
4636 * unsynchronized, stratum zero, reference ID the four-character
4644 PKT_VERSION(rpkt->li_vn_mode), xmode);
4646 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4647 xpkt.precision = rpkt->precision;
4649 xpkt.rootdelay = rpkt->rootdelay;
4650 xpkt.rootdisp = rpkt->rootdisp;
4651 xpkt.reftime = rpkt->reftime;
4652 xpkt.org = rpkt->xmt;
4653 xpkt.rec = rpkt->xmt;
4654 xpkt.xmt = rpkt->xmt;
4673 * - the packet receive time,
4674 * - the packet transmit time,
4675 * - and eventually to the reftime to make sure the
4679 PKT_VERSION(rpkt->li_vn_mode), xmode);
4682 xpkt.ppoll = max(rpkt->ppoll, ntp_minpoll);
4752 xpkt.org = rpkt->xmt;
4758 this_recv_time = rbufp->recv_time;
4763 HTONL_FP(&rbufp->recv_time, &xpkt.rec);
4791 if (rbufp->recv_length == sendlen) {
4792 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt,
4794 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d len %lu\n",
4795 current_time, stoa(&rbufp->dstadr->sin),
4796 stoa(&rbufp->recv_srcadr), xmode,
4807 * source-destination-key ID combination.
4815 * client request message, so the mode must be
4822 cookie = session_key(&rbufp->recv_srcadr,
4823 &rbufp->dstadr->sin, 0, sys_private, 0);
4824 if ((size_t)rbufp->recv_length > sendlen + MAX_MAC_LEN) {
4825 session_key(&rbufp->dstadr->sin,
4826 &rbufp->recv_srcadr, xkeyid, 0, 2);
4828 rpkt->exten[0] |= htonl(temp32);
4830 sendlen, (struct exten *)rpkt->exten,
4833 session_key(&rbufp->dstadr->sin,
4834 &rbufp->recv_srcadr, xkeyid, cookie, 2);
4844 sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, 0, &xpkt, sendlen);
4848 DPRINTF(1, ("fast_xmit: at %ld %s->%s mode %d keyid %08x len %lu\n",
4849 current_time, ntoa(&rbufp->dstadr->sin),
4850 ntoa(&rbufp->recv_srcadr), xmode, xkeyid,
4856 * pool_xmit - resolve hostname or send unicast solicitation for pool.
4874 if (NULL == pool->ai) {
4875 if (pool->addrs != NULL) {
4877 free(pool->addrs);
4878 pool->addrs = NULL;
4880 af = AF(&pool->srcadr);
4891 hints.ai_family = AF(&pool->srcadr);
4896 pool->hostname,
4901 (void *)(intptr_t)pool->associd);
4904 pool->hostname));
4908 pool->hostname);
4914 rmtadr = (sockaddr_u *)(void *)pool->ai->ai_addr;
4915 pool->ai = pool->ai->ai_next;
4922 } while (p != NULL && pool->ai != NULL);
4924 return; /* out of addresses, re-query DNS next poll */
4929 xpkt.li_vn_mode = PKT_LI_VN_MODE(sys_leap, pool->version,
4932 xpkt.ppoll = pool->hpoll;
4941 pool->aorg = xmt_tx;
4943 if (FLAG_LOOPNONCE & pool->flags) {
4952 pool->nonce = nonce;
4955 L_CLR(&pool->nonce);
4958 pool->sent++;
4959 pool->throttle += (1 << pool->minpoll) - 2;
4960 DPRINTF(1, ("pool_xmit: at %ld %s->%s pool\n",
4964 sys_ttl[(pool->ttl >= sys_ttlmax) ? sys_ttlmax : pool->ttl],
4972 * group_test - test if this is the same group
5036 pool->addrs = copy_addrinfo_list(res);
5037 pool->ai = pool->addrs;
5046 * key_expire - purge the key list
5055 if (peer->keylist != NULL) {
5056 for (i = 0; i <= peer->keynumber; i++)
5057 authtrust(peer->keylist[i], 0);
5058 free(peer->keylist);
5059 peer->keylist = NULL;
5061 value_free(&peer->sndval);
5062 peer->keynumber = 0;
5063 peer->flags &= ~FLAG_ASSOC;
5065 peer->associd));
5071 * local_refid(peer) - Check peer refid to avoid selecting peers
5074 * refid differently on different-endian systems. It now calculates
5075 * the refid the same on both, the same way it did on little-endian
5076 * in the past. On big-endian systems, ntpd also calculates a
5077 * byte-swapped version of each of its IPv6 local addresses' refids,
5080 * BE ntpd, and keeps the more-common LE old ntpd code detecting
5082 * the byte-swapping idea.
5091 if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
5092 unicast_ep = p->dstadr;
5094 unicast_ep = findinterface(&p->srcadr);
5097 && ( p->refid == unicast_ep->addr_refid
5099 || ( IS_IPV6(&unicast_ep->sin)
5100 && p->refid == unicast_ep->old_refid)
5131 if ( peer->leap == LEAP_NOTINSYNC
5132 || peer->stratum < sys_floor
5133 || peer->stratum >= sys_ceiling) {
5142 if ( !(peer->flags & FLAG_REFCLOCK)
5144 + clock_phi * ULOGTOD(peer->hpoll)) {
5154 if (peer->stratum > 1 && local_refid(peer)) {
5162 if (!peer->reach || (peer->flags & FLAG_NOSELECT)) {
5166 peer->flash &= ~PEER_TEST_MASK;
5167 peer->flash |= rval;
5175 #define MINSTEP 20e-9 /* minimum clock increment (s) */
5191 * systems with high-precision clocks, and the tick interval or step
5192 * size for lower-precision stepping clocks.
5206 * get_ostime() simulation of a low-precision system clock.
5234 l_fp ldiff; /* val - last */
5306 for (i = 0; tick <= 1; i--)
5308 if (tick - 1 > 1 - tick / 2)
5316 * init_proto - initialize the protocol module's data
5349 sys_ttlmax = (MAX_TTL - 1);
5356 * proto_config - configure the protocol module
5375 * enable and disable commands - arguments are Boolean.
5427 * tos command - arguments are double, sometimes cast to int
5482 orphwait -= sys_orphwait;
5533 * proto_clr_stats - clear protocol stat counters