77
77
double sys_mindisp = MINDISPERSE; /* minimum distance (s) */
78
78
double sys_maxdist = MAXDISTANCE; /* selection threshold */
79
79
double sys_jitter; /* system jitter */
80
u_long sys_epoch; /* last clock update time */
80
u_long sys_epoch; /* last clock update time */
81
81
static double sys_clockhop; /* clockhop threshold */
82
82
int leap_tai; /* TAI at next next leap */
83
83
u_long leap_sec; /* next scheduled leap from file */
309
310
u_int32 opcode = 0; /* extension field opcode */
310
311
sockaddr_u *dstadr_sin; /* active runway */
311
312
struct peer *peer2; /* aux peer structure pointer */
313
endpt * match_ep; /* newpeer() local address */
312
314
l_fp p_org; /* origin timestamp */
313
315
l_fp p_rec; /* receive timestamp */
314
316
l_fp p_xmt; /* transmit timestamp */
468
470
restrict_mask = ntp_monitor(rbufp, restrict_mask);
469
471
if (restrict_mask & RES_LIMITED) {
470
472
sys_limitrejected++;
471
if (!(restrict_mask & RES_KOD) || hismode ==
473
if (!(restrict_mask & RES_KOD) || MODE_BROADCAST ==
474
hismode || MODE_SERVER == hismode)
473
475
return; /* rate exceeded */
475
477
if (hismode == MODE_CLIENT)
849
851
#endif /* OPENSSL */
854
* Broadcasts received via a multicast address may
855
* arrive after a unicast volley has begun
856
* with the same remote address. newpeer() will not
857
* find duplicate associations on other local endpoints
858
* if a non-NULL endpoint is supplied. multicastclient
859
* ephemeral associations are unique across all local
862
if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
863
match_ep = rbufp->dstadr;
852
868
* Determine whether to execute the initial volley.
854
870
if (sys_bdelay != 0) {
867
883
* Do not execute the volley. Start out in
868
884
* broadcast client mode.
870
if ((peer = newpeer(&rbufp->recv_srcadr,
871
rbufp->dstadr, MODE_BCLIENT, hisversion,
872
pkt->ppoll, pkt->ppoll, 0, 0, 0,
886
peer = newpeer(&rbufp->recv_srcadr, match_ep,
887
MODE_BCLIENT, hisversion, pkt->ppoll,
888
pkt->ppoll, FLAG_PREEMPT, MDF_BCLNT, 0,
874
891
sys_restricted++;
875
892
return; /* ignore duplicate */
889
906
* packet, normally 6 (64 s) and that the poll interval
890
907
* is fixed at this value.
892
if ((peer = newpeer(&rbufp->recv_srcadr, rbufp->dstadr,
909
peer = newpeer(&rbufp->recv_srcadr, match_ep,
893
910
MODE_CLIENT, hisversion, pkt->ppoll, pkt->ppoll,
894
FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT, 0,
911
FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
896
914
sys_restricted++;
897
915
return; /* ignore duplicate */
1495
1513
* timestamp. This works for both basic and interleaved
1498
if (peer->cast_flags & MDF_BCLNT) {
1499
peer->cast_flags &= ~MDF_BCLNT;
1516
if (FLAG_BC_VOL & peer->flags) {
1517
peer->flags &= ~FLAG_BC_VOL;
1500
1518
peer->delay = (peer->offset - p_offset) * 2;
1502
1520
p_del = peer->delay;
1602
1620
* client mode when the client is fit and the autokey dance is
1605
if ((peer->cast_flags & MDF_BCLNT) && !(peer_unfit(peer) &
1623
if ((FLAG_BC_VOL & peer->flags) && MODE_CLIENT == peer->hmode &&
1624
!(TEST11 & peer_unfit(peer))) { /* distance exceeded */
1608
1626
if (peer->flags & FLAG_SKEY) {
1609
1627
if (!(~peer->crypto & CRYPTO_FLAG_ALL))
1649
1667
sys_refid = peer->refid;
1651
1669
sys_refid = addr2refid(&peer->srcadr);
1652
dtemp = sys_jitter + fabs(sys_offset) + peer->disp + clock_phi *
1670
dtemp = sys_jitter + fabs(sys_offset) + peer->disp +
1671
(peer->delay + peer->rootdelay) / 2 + clock_phi *
1653
1672
(current_time - peer->update);
1654
1673
sys_rootdisp = dtemp + peer->rootdisp;
1655
1674
sys_rootdelay = peer->delay + peer->rootdelay;
2017
2036
* A sample consists of the offset, delay, dispersion and epoch
2018
2037
* of arrival. The offset and delay are determined by the on-
2019
* wire protcol. The dispersion grows from the last outbound
2038
* wire protocol. The dispersion grows from the last outbound
2020
2039
* packet to the arrival of this one increased by the sum of the
2021
2040
* peer precision and the system precision as required by the
2022
2041
* error budget. First, shift the new arrival into the shift
2120
2139
* save the offset, delay and jitter. Note the jitter must not
2121
2140
* be less than the precision.
2126
2147
etemp = fabs(peer->offset - peer->filter_offset[k]);
2127
2148
peer->offset = peer->filter_offset[k];
2202
2223
double high, low;
2203
2224
double seljitter;
2204
2225
double synch[NTP_MAXASSOC], error[NTP_MAXASSOC];
2205
double orphdist = 1e10;
2226
double orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
2206
2227
struct peer *osys_peer = NULL;
2207
2228
struct peer *sys_prefer = NULL; /* prefer peer */
2208
2229
struct peer *typesystem = NULL;
2267
2288
for (n = 0; n < NTP_HASH_SIZE; n++) {
2268
2289
for (peer = peer_hash[n]; peer != NULL; peer =
2270
peer->flags &= ~FLAG_SYSPEER;
2271
peer->status = CTL_PST_SEL_REJECT;
2291
peer->new_status = CTL_PST_SEL_REJECT;
2274
2294
* Leave the island immediately if the peer is
2281
* If this is an orphan, choose the one with
2282
* the lowest metric defined as the IPv4 address
2283
* or the first 64 bits of the hashed IPv6 address.
2301
* If this peer is an orphan parent, elect the
2302
* one with the lowest metric defined as the
2303
* IPv4 address or the first 64 bits of the
2304
* hashed IPv6 address. To ensure convergence
2305
* on the same selected orphan, consider as
2306
* well that this system may have the lowest
2307
* metric and be the orphan parent. If this
2308
* system wins, sys_peer will be NULL to trigger
2309
* orphan mode in timer().
2285
2311
if (peer->stratum == sys_orphan) {
2288
ftemp = addr2refid(&peer->srcadr);
2289
if (ftemp < orphdist) {
2315
if (peer->dstadr != NULL)
2316
localmet = ntohl(peer->dstadr->addr_refid);
2318
localmet = U_INT32_MAX;
2319
peermet = ntohl(addr2refid(&peer->srcadr));
2320
if (peermet < localmet &&
2321
peermet < orphmet) {
2290
2322
typeorphan = peer;
2329
* If this peer could have the orphan parent
2330
* as a synchronization ancestor, exclude it
2331
* from selection to avoid forming a
2332
* synchronization loop within the orphan mesh,
2333
* triggering stratum climb to infinity
2334
* instability. Peers at stratum higher than
2335
* the orphan stratum could have the orphan
2336
* parent in ancestry so are excluded.
2337
* See http://bugs.ntp.org/2050
2339
if (peer->stratum > sys_orphan)
2295
2341
#ifdef REFCLOCK
2297
2343
* The following are special cases. We deal
2298
2344
* with them later.
2300
switch (peer->refclktype) {
2301
case REFCLK_LOCALCLOCK:
2302
if (typelocal == NULL &&
2303
!(peer->flags & FLAG_PREFER))
2346
if (!(peer->flags & FLAG_PREFER)) {
2347
switch (peer->refclktype) {
2348
case REFCLK_LOCALCLOCK:
2349
if (typelocal == NULL)
2308
if (typeacts == NULL &&
2309
!(peer->flags & FLAG_PREFER))
2354
if (typeacts == NULL)
2313
2359
#endif /* REFCLOCK */
2317
2363
* island, but does not yet have the immunity
2320
peer->status = CTL_PST_SEL_SANE;
2366
peer->new_status = CTL_PST_SEL_SANE;
2321
2367
peer_list[nlist++] = peer;
2387
2433
* Here, nlist is the number of candidates and allow is the
2388
2434
* number of falsetickers. Upon exit, the truechimers are the
2389
* susvivors with offsets not less than low and not greater than
2435
* survivors with offsets not less than low and not greater than
2390
2436
* high. There may be none of them.
2455
2501
#ifdef REFCLOCK
2457
* Elegible PPS peers must survive the intersection
2503
* Eligible PPS peers must survive the intersection
2458
2504
* algorithm. Use the first one found, but don't
2459
2505
* include any of them in the cluster population.
2520
2566
* Mark the candidates at this point as truechimers.
2522
2568
for (i = 0; i < nlist; i++) {
2523
peer_list[i]->status = CTL_PST_SEL_SELCAND;
2569
peer_list[i]->new_status = CTL_PST_SEL_SELCAND;
2526
2572
printf("select: survivor %s %f\n",
2574
2620
ntoa(&peer_list[k]->srcadr), g, d);
2576
2622
if (nlist > sys_maxclock)
2577
peer_list[k]->status = CTL_PST_SEL_EXCESS;
2623
peer_list[k]->new_status = CTL_PST_SEL_EXCESS;
2578
2624
for (j = k + 1; j < nlist; j++) {
2579
2625
peer_list[j - 1] = peer_list[j];
2580
2626
synch[j - 1] = synch[j];
2598
2644
for (i = 0; i < nlist; i++) {
2599
2645
peer = peer_list[i];
2600
2646
peer->unreach = 0;
2601
peer->status = CTL_PST_SEL_SYNCCAND;
2647
peer->new_status = CTL_PST_SEL_SYNCCAND;
2602
2648
sys_survivors++;
2603
2649
if (peer->leap == LEAP_ADDSECOND) {
2604
2650
if (peer->flags & FLAG_REFCLOCK)
2652
2698
if (typesystem != NULL) {
2653
2699
if (sys_prefer == NULL) {
2654
typesystem->status = CTL_PST_SEL_SYSPEER;
2700
typesystem->new_status = CTL_PST_SEL_SYSPEER;
2655
2701
clock_combine(peer_list, sys_survivors);
2656
sys_jitter = SQRT(SQUARE(typesystem->jitter) +
2657
SQUARE(sys_jitter) + SQUARE(seljitter));
2702
sys_jitter = SQRT(SQUARE(sys_jitter) +
2659
2705
typesystem = sys_prefer;
2660
2706
sys_clockhop = 0;
2661
typesystem->status = CTL_PST_SEL_SYSPEER;
2707
typesystem->new_status = CTL_PST_SEL_SYSPEER;
2662
2708
sys_offset = typesystem->offset;
2663
2709
sys_jitter = typesystem->jitter;
2682
2728
NULL || (typesystem == NULL && sys_minsane == 0))))) {
2683
2729
typesystem = typepps;
2684
2730
sys_clockhop = 0;
2685
typesystem->status = CTL_PST_SEL_PPS;
2731
typesystem->new_status = CTL_PST_SEL_PPS;
2686
2732
sys_offset = typesystem->offset;
2687
2733
sys_jitter = typesystem->jitter;
2702
2748
if (osys_peer != NULL)
2703
2749
report_event(EVNT_NOPEER, NULL, NULL);
2704
2750
sys_peer = NULL;
2751
for (n = 0; n < NTP_HASH_SIZE; n++)
2752
for (peer = peer_hash[n]; peer != NULL; peer =
2754
peer->status = peer->new_status;
2716
2766
* We have found the alpha male. Wind the clock.
2718
if (osys_peer != typesystem)
2768
if (osys_peer != typesystem)
2719
2769
report_event(PEVNT_NEWPEER, typesystem, NULL);
2720
typesystem->flags |= FLAG_SYSPEER;
2770
for (n = 0; n < NTP_HASH_SIZE; n++)
2771
for (peer = peer_hash[n]; peer != NULL; peer =
2773
peer->status = peer->new_status;
2721
2774
clock_update(typesystem);
3438
* local_refid(peer) - check peer refid to avoid selecting peers
3439
* currently synced to this ntpd.
3448
if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
3449
unicast_ep = p->dstadr;
3451
unicast_ep = findinterface(&p->srcadr);
3453
if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
3385
3461
* Determine if the peer is unfit for synchronization
3387
3463
* A peer is unfit for synchronization if
3421
3497
* server as the local peer but only if the remote peer is
3422
3498
* neither a reference clock nor an orphan.
3424
if (peer->stratum > 1 && peer->refid != htonl(LOOPBACKADR) &&
3425
(peer->refid == (peer->dstadr ? peer->dstadr->addr_refid :
3426
0) || peer->refid == sys_refid))
3500
if (peer->stratum > 1 && local_refid(peer))
3427
3501
rval |= TEST12; /* synchronization loop */