Lines Matching +full:lf +full:- +full:buffer +full:- +full:low +full:- +full:power

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