Lines Matching defs:peer
96 * peer_select groups statistics for a peer used by clock_select() and
100 struct peer * peer;
124 struct peer *sys_peer; /* current peer */
212 nak_code valid_NAK (struct peer *peer, struct recvbuf *rbufp, u_char hismode);
213 static double root_distance (struct peer *);
215 static void peer_xmit (struct peer *);
217 static void pool_xmit (struct peer *);
218 static void clock_update (struct peer *);
221 static int local_refid (struct peer *);
222 static int peer_unfit (struct peer *);
301 struct peer *peer,
311 const l_fp * myorg; /* selected peer origin */
351 * So the NAK is invalid if either there's no peer, or
354 if (!peer || (!peer->keyid && !(peer->flags & FLAG_SKEY))) {
362 if (FLAG_LOOPNONCE & peer->flags) {
363 myorg = &peer->nonce;
365 if (peer->flip > 0) {
366 myorg = &peer->borg;
368 myorg = &peer->aorg;
392 struct peer *peer /* peer structure pointer */
403 hpoll = peer->hpoll;
409 if (peer->outdate > peer->timelastrec && !peer->reach)
410 peer->ppoll = peer->maxpoll;
416 if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) {
417 peer->outdate = current_time;
418 poll_update(peer, hpoll, 0);
420 peer_xmit(peer);
433 * this code is not quite right -- peer->unreach counts polls
437 if (peer->cast_flags & MDF_ACAST) {
438 peer->outdate = current_time;
439 poll_update(peer, hpoll, 0);
440 if (peer->unreach > sys_beacon) {
441 peer->unreach = 0;
442 peer->ttl = 0;
443 peer_xmit(peer);
446 if (peer->ttl < sys_ttlmax)
447 peer->ttl++;
448 peer_xmit(peer);
450 peer->unreach++;
466 if (peer->cast_flags & MDF_POOL) {
467 peer->outdate = current_time;
468 poll_update(peer, hpoll, 0);
472 pool_xmit(peer);
477 if (MDF_PCLNT & peer->cast_flags) {
478 if ( (IS_IPV6(&peer->srcadr) && !nonlocal_v6_addr_up)
480 unpeer(peer);
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) {
505 * Here the peer is unreachable. If it was
509 clock_filter(peer, 0., 0., MAXDISPERSE);
511 peer_unfit(peer);
512 report_event(PEVNT_UNREACH, peer, NULL);
514 if ( (peer->flags & FLAG_IBURST)
515 && peer->retry == 0)
516 peer->retry = NTP_RETRY;
520 * Here the peer is reachable. Send a burst if
521 * enabled and the peer is fit. Reset unreach
527 if (!(peer->flags & FLAG_PREEMPT))
528 peer->unreach = 0;
529 if ( (peer->flags & FLAG_BURST)
530 && peer->retry == 0
531 && !peer_unfit(peer))
532 peer->retry = NTP_RETRY;
540 * and this peer has the minimum score of preemptibles,
543 if (peer->unreach >= NTP_UNREACH) {
546 if (!(peer->flags & (FLAG_CONFIG | FLAG_PREEMPT))) {
547 report_event(PEVNT_RESTART, peer, "timeout");
548 peer_clear(peer, "TIME");
549 unpeer(peer);
552 if ( (peer->flags & FLAG_PREEMPT)
554 && score_all(peer)) {
555 report_event(PEVNT_RESTART, peer, "timeout");
556 peer_clear(peer, "TIME");
557 unpeer(peer);
562 peer->burst--;
563 if (peer->burst == 0) {
583 if (peer->retry > 0)
584 peer->retry--;
589 poll_update(peer, hpoll, (peer->hmode == MODE_CLIENT));
590 if (peer->hmode != MODE_BCLIENT)
591 peer_xmit(peer);
632 register struct peer *peer; /* peer structure pointer */
651 struct peer *peer2; /* aux peer structure pointer */
1003 peer = findpeer(rbufp, hismode, &retcode);
1036 crypto_nak_test = valid_NAK(peer, rbufp, hismode);
1042 report_event(PEVNT_AUTH, peer, "Invalid_NAK");
1043 if (0 != peer) {
1044 peer->badNAK++;
1124 * the EXOR of the peer and host cookies.
1163 } else if (peer == NULL) {
1168 pkeyid = peer->pcookie;
1229 if (0 != peer && 0 != peer->keyid) {
1237 if (peer->keyid != skeyid) {
1239 peer->keyid, skeyid,
1294 * association is processed by the peer process for that
1470 DPRINTF(2, ("receive: AM_MANYCAST drop: No manycast peer\n"));
1500 peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1505 if (NULL == peer) {
1523 * association) and tinker with peer->nextdate delaying
1621 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1625 if (NULL == peer) {
1631 peer->delay = sys_bdelay;
1632 peer->bxmt = p_xmt;
1645 peer = newpeer(&rbufp->recv_srcadr, NULL, match_ep,
1650 if (NULL == peer) {
1655 peer->bxmt = p_xmt;
1658 crypto_recv(peer, rbufp);
1665 * symmetric active peer. First, deal with broken Windows clients.
1721 * ephemeral peer requests, and it didn't meet
1730 " with unknown peer %s mode %d/%s:%s keyid %08x len %d auth %d\n",
1747 * this allows an unsynchronized peer to synchronize to
1767 if ((peer = newpeer(&rbufp->recv_srcadr, NULL, rbufp->dstadr,
1789 if (group_test(groupname, peer->ident)) {
1802 (current_time - peer->timelastrec),
1803 peer->ppoll, (1 << peer->ppoll)
1815 if (pkt->ppoll != peer->ppoll) {
1818 peer->ppoll, pkt->ppoll);
1822 if ( pkt->ppoll < peer->minpoll
1823 || pkt->ppoll > peer->maxpoll) {
1826 peer->minpoll, peer->maxpoll);
1841 if (FLAG_BC_VOL & peer->flags)
1843 if ((current_time - peer->timereceived) < deadband) {
1846 (current_time - peer->timereceived),
1874 /* pkt->ppoll or peer->ppoll ? */
1881 if (L_ISZERO(&peer->bxmt)) {
1885 L_SUB(&tdiff, &peer->bxmt);
1888 && (current_time - peer->timereceived) < deadband)
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;
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++;
2000 hisstratum, ntoa(&peer->srcadr));
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++;
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++;
2051 ntoa(&peer->srcadr),
2052 peer->borg.l_ui, peer->borg.l_uf);
2081 DEBUG_INSIST(0 == (FLAG_LOOPNONCE & peer->flags));
2085 ntoa(&peer->srcadr), 1u << pkt->ppoll);
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),
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 */
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;
2147 report_event(PEVNT_XLEAVE, peer, NULL);
2151 hm_str, ntoa(&peer->srcadr));
2155 L_CLR(&peer->aorg);
2164 || L_ISZERO(&peer->dst)) {
2165 peer->flash |= TEST3; /* unsynch */
2172 } else if ( !L_ISZERO(&peer->dst)
2173 && !L_ISEQU(&p_org, &peer->dst)) {
2175 peer->bogusorg++;
2176 peer->flags |= FLAG_XBOGUS;
2177 peer->flash |= TEST2; /* bogus */
2191 report_event(PEVNT_AUTH, peer, "crypto_NAK");
2192 peer->flash |= TEST5; /* bad auth */
2193 peer->badauth++;
2194 if (peer->flags & FLAG_PREEMPT) {
2196 unpeer(peer);
2202 if (peer->crypto) {
2203 peer_clear(peer, "AUTH");
2215 * crypto-NAK. The peer should restart the protocol.
2217 } else if (!AUTH(peer->keyid || has_mac ||
2220 if (peer->flash & PKT_TEST_MASK) {
2223 ntoa(&peer->srcadr));
2227 report_event(PEVNT_AUTH, peer, "digest");
2228 peer->flash |= TEST5; /* bad auth */
2229 peer->badauth++;
2234 if (peer->flags & FLAG_PREEMPT) {
2236 unpeer(peer);
2240 else if (peer_clear_digest_early && peer->crypto) {
2241 peer_clear(peer, "AUTH");
2265 if (!(peer->flags & FLAG_XB)) {
2268 ntoa(&peer->srcadr));
2269 peer->flags |= FLAG_XB;
2270 peer->aorg = p_xmt;
2271 peer->borg = rbufp->recv_time;
2272 report_event(PEVNT_XLEAVE, peer, NULL);
2275 } else if (peer->flags & FLAG_XB) {
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;
2306 * Set the peer ppoll to the maximum of the packet ppoll and the
2307 * peer minpoll. If a kiss-o'-death, set the peer minpoll to
2330 peer->ppoll = max(peer->minpoll, pkt->ppoll);
2332 peer->selbroken++; /* Increment the KoD count */
2333 report_event(PEVNT_RATE, peer, NULL);
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);
2342 peer->selbroken++; /* Increment the KoD count */
2371 && !authistrustedip(skeyid, &peer->srcadr)) {
2372 report_event(PEVNT_AUTH, peer, "authIP");
2373 peer->badauth++;
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;
2424 * obtained from the broadcaster or symmetric peer. If no
2430 if (peer->flags & FLAG_SKEY) {
2435 ap = (struct autokey *)peer->recval.ptr;
2440 peer->flash |= TEST8;
2441 rval = crypto_recv(peer, rbufp);
2443 peer->unreach = 0;
2446 report_event(PEVNT_RESTART, peer,
2448 peer_clear(peer, "CRYP");
2449 peer->flash |= TEST9; /* bad crypt */
2450 if (peer->flags & FLAG_PREEMPT) {
2452 unpeer(peer);
2464 if (skeyid == peer->keyid)
2465 peer->flash &= ~TEST8;
2472 } else if (!(peer->flash & TEST8)) {
2473 peer->pkeyid = skeyid;
2489 if ( tkeyid == peer->pkeyid
2491 peer->flash &= ~TEST8;
2492 peer->pkeyid = skeyid;
2497 peer->crypto &=
2505 if (peer->flash & TEST8)
2506 report_event(PEVNT_AUTH, peer, "keylist");
2508 if (!(peer->crypto & CRYPTO_FLAG_PROV)) /* test 9 */
2509 peer->flash |= TEST8; /* bad autokey */
2516 if (current_time > peer->refresh) {
2517 report_event(PEVNT_RESTART, peer,
2519 peer_clear(peer, "TIME");
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));
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--;
2566 register struct peer *peer,
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++;
2622 * Capture the header values in the client/peer association..
2624 record_raw_stats(&peer->srcadr,
2625 peer->dstadr ? &peer->dstadr->sin : NULL,
2626 &p_org, &p_rec, &p_xmt, &peer->dst,
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;
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;
2656 * If the peer was previously unreachable, raise a trap. In any
2659 if (!peer->reach) {
2660 report_event(PEVNT_REACH, peer, NULL);
2661 peer->timereachable = current_time;
2663 peer->reach |= 1;
2694 * t1 = peer->aorg/borg, t2 = peer->rec, t3 = p_xmt,
2695 * t4 = peer->dst
2697 if (peer->flip != 0) {
2699 L_SUB(&ci, &peer->dst);
2702 if (peer->flip > 0)
2703 L_SUB(&ci, &peer->borg);
2705 L_SUB(&ci, &peer->aorg);
2710 mprintf_event(PEVNT_XERR, peer,
2718 } else if (peer->pmode == MODE_BROADCAST) {
2722 * t1 = peer->borg, t2 = p_org, t3 = p_org, t4 = aorg
2724 if (peer->flags & FLAG_XB) {
2726 L_SUB(&ci, &peer->aorg);
2729 L_SUB(&ci, &peer->borg);
2731 peer->aorg = p_xmt;
2732 peer->borg = peer->dst;
2735 if (FLAG_BC_VOL & peer->flags)
2737 mprintf_event(PEVNT_XERR, peer,
2743 peer->xleave = t34;
2747 * t3 = p_xmt, t4 = peer->dst
2751 L_SUB(&ci, &peer->dst);
2762 * [Bug 3031] Don't keep this peer when the delay
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)) {
2774 unpeer(peer);
2778 peer->nextdate = current_time + (1u << peer->ppoll) - 2u;
2779 p_del = peer->delay;
2786 * t1 = p_org, t2 = p_rec, t3 = p_xmt, t4 = peer->dst
2790 L_SUB(&ci, &peer->dst);
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;
2817 peer->r21 = ftemp;
2820 itemp = len - peer->t34_bytes;
2822 etemp = -t34 - peer->t34;
2826 peer->r34 = ftemp;
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) -
2861 p_del, peer->r21 / 1e3, peer->r34 / 1e3,
2869 clock_filter(peer, p_offset + peer->bias, p_del, p_disp);
2876 if ( (FLAG_BC_VOL & peer->flags)
2877 && MODE_CLIENT == peer->hmode
2878 && !(TEST11 & peer_unfit(peer))) { /* distance exceeded */
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;
2898 struct peer *peer /* peer structure pointer */
2911 sys_peer = peer;
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;
2917 poll_update(peer, sys_poll, 0);
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);
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;
2963 sys_reftime = peer->dst;
2966 current_time, peer->epoch, peer->associd));
2972 switch (local_clock(peer, sys_offset)) {
3074 * poll_update - update peer poll interval
3078 struct peer *peer, /* peer structure pointer */
3095 hpoll = max(min(peer->maxpoll, mpoll), peer->minpoll);
3103 if ((peer->flags & FLAG_SKEY) && hpoll != peer->hpoll)
3104 key_expire(peer);
3106 peer->hpoll = hpoll;
3125 utemp = current_time + max(peer->throttle - (NTP_SHIFT - 1) *
3126 (1 << peer->minpoll), ntp_minpkt);
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;
3162 * use host poll; otherwise, use the minimum of host and peer
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;
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
3232 struct peer *peer, /* peer structure */
3238 l_fp bxmt = peer->bxmt; /* bcast clients retain this! */
3246 * that, if the peer is purged, the cryptographic variables are
3250 key_expire(peer);
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);
3268 memset(CLEAR_TO_ZERO(peer), 0, LEN_CLEAR_TO_ZERO(peer));
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;
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;
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,
3338 struct peer *peer, /* peer structure pointer */
3355 * peer precision and the system precision as required by the
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;
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;
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)) {
3481 mprintf_event(PEVNT_POPCORN, peer, "%.9f s", etemp);
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) {
3518 * LOCKCLOCK: (1) If the local clock is the prefer peer, it will always
3519 * be enabled, even if declared falseticker, (2) only the prefer peer
3520 * can be selected as the system peer, (3) if the external source is
3527 struct peer *peer;
3538 struct peer *osys_peer;
3539 struct peer *sys_prefer = NULL; /* prefer peer */
3540 struct peer *typesystem = NULL;
3541 struct peer *typelastresort = NULL;
3542 struct peer *typeorphan = NULL;
3544 struct peer *typeacts = NULL;
3545 struct peer *typelocal = NULL;
3546 struct peer *typepps = NULL;
3557 * Initialize and create endpoint, index and peer lists big
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;
3598 * Leave the island immediately if the peer is
3601 if (peer_unfit(peer)) {
3606 * If we have never been synchronised, look for any peer
3610 * with this peer, sys_reftime gets set and so this
3614 d = root_distance(peer);
3615 if (!L_ISZERO(&peer->reftime) && d < lastresort_dist) {
3616 typelastresort = peer;
3622 * If this peer is an orphan parent, elect the
3632 if (peer->stratum == sys_orphan) {
3636 if (peer->dstadr != NULL)
3637 localmet = ntohl(peer->dstadr->addr_refid);
3640 peermet = ntohl(addr2refid(&peer->srcadr));
3642 typeorphan = peer;
3649 * If this peer could have the orphan parent
3659 if (peer->stratum > sys_orphan) {
3667 if (!(peer->flags & FLAG_PREFER)) {
3668 switch (peer->refclktype) {
3672 typelocal = peer;
3678 typeacts = peer;
3685 * If we get this far, the peer can stay on the
3689 peer->new_status = CTL_PST_SEL_SANE;
3690 f = root_distance(peer);
3691 peers[nlist].peer = peer;
3692 peers[nlist].error = peer->jitter;
3700 e = peer->offset;
3791 * who leave the island immediately. The TRUE peer is always a
3792 * truechimer. We must leave at least one peer to collect the
3805 peer = peers[i].peer;
3808 || peer->offset + h < low
3809 || peer->offset - h > high
3810 ) && !(peer->flags & FLAG_TRUE))
3819 if (peer->flags & FLAG_PPS) {
3821 typepps = peer;
3822 if (!(peer->flags & FLAG_TSTAMP_PPS))
3844 peers[0].peer = typeacts;
3847 peers[0].peer = typelocal;
3852 peers[0].peer = typeorphan;
3855 peers[0].peer = typelastresort;
3864 peers[i].peer->new_status = CTL_PST_SEL_SELCAND;
3866 stoa(&peers[i].peer->srcadr), peers[i].synch));
3872 * than sys_minclock survivors and the select jitter of the peer
3873 * with the worst metric is greater than the minimum peer
3875 * peer, who of course have the immunity idol.
3889 f += DIFF(peers[j].peer->offset,
3890 peers[i].peer->offset);
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;
3924 * Choose the system peer using a hybrid metric composed of the
3928 * reference clock and a network peer which has a refclock and
3933 * in selecting the system peer, using a weight of 1 second of
3943 peer = peers[i].peer;
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)
3960 sys_prefer = peer;
3962 peer->stratum * sys_mindisp;
3973 * system peer is not speer, stay with the current system peer
3979 typesystem = peers[speer].peer;
4002 * there is a prefer peer, use its offset and jitter.
4022 * 0.4 s, select the driver as the PPS peer and use its offset
4024 * if there is a prefer peer or there are no survivors and none
4045 * system peer. If so and this is an old update, keep the
4054 for (peer = peer_list; peer != NULL; peer = peer->p_link)
4055 peer->status = peer->new_status;
4071 for (peer = peer_list; peer != NULL; peer = peer->p_link)
4072 peer->status = peer->new_status;
4081 int syspeer /* index of sys.peer */
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
4105 struct peer *peer /* peer structure pointer */
4117 * epsilon is the peer dispersion
4128 dtemp = (peer->delay + peer->rootdelay) / 2
4129 + peer->disp
4130 + clock_phi * (current_time - peer->update)
4131 + peer->rootdisp
4132 + peer->jitter;
4151 struct peer *peer /* peer structure pointer */
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;
4170 /* Use sys_reftime for peer exchanges */
4172 HTONL_FP(&peer->rec, &xpkt.org);
4173 HTONL_FP(&peer->dst, &xpkt.rec);
4190 !(peer->flags & FLAG_SKEY) &&
4192 peer->keyid == 0) {
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;
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);
4240 current_time, latoa(peer->dstadr),
4241 stoa(&peer->srcadr), peer->hmode, sendlen,
4252 if (peer->flags & FLAG_SKEY) {
4299 if (peer->keynumber == 0)
4300 make_keylist(peer, peer->dstadr);
4302 peer->keynumber--;
4303 xkeyid = peer->keylist[peer->keynumber];
4307 key_expire(peer);
4309 peer->keyid = xkeyid;
4311 switch (peer->hmode) {
4321 if (peer->flags & FLAG_ASSOC)
4322 exten = crypto_args(peer, CRYPTO_AUTO |
4323 CRYPTO_RESP, peer->associd, NULL);
4325 exten = crypto_args(peer, CRYPTO_ASSOC |
4326 CRYPTO_RESP, peer->associd, NULL);
4333 * peer will not believe the other peer until the other
4334 * peer has synchronized, so the certificate exchange
4335 * might loop until then. If a peer finds a broken
4346 if (!peer->crypto)
4347 exten = crypto_args(peer, CRYPTO_ASSOC,
4348 peer->associd, hostval.ptr);
4349 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4350 exten = crypto_args(peer, CRYPTO_CERT,
4351 peer->associd, peer->issuer);
4352 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4353 exten = crypto_args(peer,
4354 crypto_ident(peer), peer->associd,
4359 * only when the this peer and the other peer
4360 * are synchronized. But, this peer needs the
4364 * some reason either peer finds a broken
4369 && peer->leap != LEAP_NOTINSYNC
4370 && !(peer->crypto & CRYPTO_FLAG_COOK))
4371 exten = crypto_args(peer, CRYPTO_COOK,
4372 peer->associd, NULL);
4373 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4374 exten = crypto_args(peer, CRYPTO_AUTO,
4375 peer->associd, NULL);
4376 else if ( peer->flags & FLAG_ASSOC
4377 && peer->crypto & CRYPTO_FLAG_SIGN)
4378 exten = crypto_args(peer, CRYPTO_AUTO |
4379 CRYPTO_RESP, peer->assoc, NULL);
4389 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4390 exten = crypto_args(peer, CRYPTO_SIGN,
4391 peer->associd, hostval.ptr);
4392 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4393 exten = crypto_args(peer, CRYPTO_LEAP,
4394 peer->associd, NULL);
4413 if (!peer->crypto)
4414 exten = crypto_args(peer, CRYPTO_ASSOC,
4415 peer->associd, hostval.ptr);
4416 else if (!(peer->crypto & CRYPTO_FLAG_CERT))
4417 exten = crypto_args(peer, CRYPTO_CERT,
4418 peer->associd, peer->issuer);
4419 else if (!(peer->crypto & CRYPTO_FLAG_VRFY))
4420 exten = crypto_args(peer,
4421 crypto_ident(peer), peer->associd,
4426 * we use the peer association ID with autokey
4429 else if (!(peer->crypto & CRYPTO_FLAG_COOK))
4430 exten = crypto_args(peer, CRYPTO_COOK,
4431 peer->associd, NULL);
4432 else if (!(peer->crypto & CRYPTO_FLAG_AUTO))
4433 exten = crypto_args(peer, CRYPTO_AUTO,
4434 peer->assoc, NULL);
4444 else if (!(peer->crypto & CRYPTO_FLAG_SIGN))
4445 exten = crypto_args(peer, CRYPTO_SIGN,
4446 peer->associd, hostval.ptr);
4447 else if (!(peer->crypto & CRYPTO_FLAG_LEAP))
4448 exten = crypto_args(peer, CRYPTO_LEAP,
4449 peer->associd, NULL);
4459 if (peer->cmmd != NULL) {
4463 peer->cmmd->opcode |= htonl(temp32);
4464 sendlen += crypto_xmit(peer, &xpkt, NULL,
4465 sendlen, peer->cmmd, 0);
4466 free(peer->cmmd);
4467 peer->cmmd = NULL;
4476 sendlen += crypto_xmit(peer, &xpkt,
4486 session_key(&peer->dstadr->sin, &peer->srcadr,
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;
4516 report_event(PEVNT_AUTH, peer, "no key");
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;
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);
4552 current_time, latoa(peer->dstadr), stoa(&peer->srcadr),
4553 peer->hmode, xkeyid, sendlen, peer->keynumber));
4556 current_time, peer->dstadr ?
4557 ntoa(&peer->dstadr->sin) : "-",
4558 ntoa(&peer->srcadr), peer->hmode, xkeyid, sendlen));
4609 * set the peer poll at the maximum of the receive peer poll and
4860 struct peer *pool /* pool solicitor association */
4870 struct peer * p;
5017 struct peer * pool; /* pool solicitor association */
5050 struct peer *peer /* peer structure pointer */
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
5086 struct peer * p
5111 * Determine if the peer is unfit for synchronization
5113 * A peer is unfit for synchronization if
5115 * > TEST11 root distance exceeded for remote peer
5121 struct peer *peer /* peer structure pointer */
5131 if ( peer->leap == LEAP_NOTINSYNC
5132 || peer->stratum < sys_floor
5133 || peer->stratum >= sys_ceiling) {
5138 * A distance error for a remote peer occurs if the root
5142 if ( !(peer->flags & FLAG_REFCLOCK)
5143 && root_distance(peer) >= sys_maxdist
5144 + clock_phi * ULOGTOD(peer->hpoll)) {
5149 * A loop error occurs if the remote peer is synchronized to the
5150 * local peer or if the remote peer is synchronized to the same
5151 * server as the local peer but only if the remote peer is
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;