12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005 |
- #include "libbb.h"
- #include <math.h>
- #include <netinet/ip.h> /* For IPTOS_DSCP_AF21 definition */
- #include <sys/timex.h>
- #ifndef IPTOS_DSCP_AF21
- # define IPTOS_DSCP_AF21 0x48
- #endif
- #define MAX_VERBOSE 3
- #define INITIAL_SAMPLES 4
- #define MIN_FREQHOLD 12
- #define BAD_DELAY_GROWTH 4
- #define RETRY_INTERVAL 32
- #define NOREPLY_INTERVAL 512
- #define RESPONSE_INTERVAL 16
- #define HOSTNAME_INTERVAL 4
- #define DNS_ERRORS_CAP 0x3f
- #define STEP_THRESHOLD 1
- #define SLEW_THRESHOLD 0.125
- #define BIGOFF STEP_THRESHOLD
- #define BIGOFF_INTERVAL (1 << 7)
- #define FREQ_TOLERANCE 0.000015
- #define BURSTPOLL 0
- #define MINPOLL 5
- #define BIGPOLL 9
- #define MAXPOLL 12
- #define MINDISP 0.01
- #define MAXDISP 16
- #define MAXSTRAT 16
- #define MAXDIST 1
- #define MIN_SELECTED 1
- #define MIN_CLUSTERED 3
- #define MAXDRIFT 0.000500
- #define POLLADJ_LIMIT 40
- #define POLLADJ_GATE 4
- #define TIMECONST_HACK_GATE 2
- #define ALLAN 512
- #define PLL 65536
- #define FLL (MAXPOLL + 1)
- #define AVG 4
- #define MAX_KEY_NUMBER 65535
- #define KEYID_SIZE sizeof(uint32_t)
- enum {
- NTP_VERSION = 4,
- NTP_MAXSTRATUM = 15,
- NTP_MD5_DIGESTSIZE = 16,
- NTP_MSGSIZE_NOAUTH = 48,
- NTP_MSGSIZE_MD5_AUTH = NTP_MSGSIZE_NOAUTH + KEYID_SIZE + NTP_MD5_DIGESTSIZE,
- NTP_SHA1_DIGESTSIZE = 20,
- NTP_MSGSIZE_SHA1_AUTH = NTP_MSGSIZE_NOAUTH + KEYID_SIZE + NTP_SHA1_DIGESTSIZE,
-
- MODE_MASK = (7 << 0),
- VERSION_MASK = (7 << 3),
- VERSION_SHIFT = 3,
- LI_MASK = (3 << 6),
-
- LI_NOWARNING = (0 << 6),
- LI_PLUSSEC = (1 << 6),
- LI_MINUSSEC = (2 << 6),
- LI_ALARM = (3 << 6),
-
- MODE_RES0 = 0,
- MODE_SYM_ACT = 1,
- MODE_SYM_PAS = 2,
- MODE_CLIENT = 3,
- MODE_SERVER = 4,
- MODE_BROADCAST = 5,
- MODE_RES1 = 6,
- MODE_RES2 = 7,
- };
- #define OFFSET_1900_1970 2208988800UL
- #define NUM_DATAPOINTS 8
- typedef struct {
- uint32_t int_partl;
- uint32_t fractionl;
- } l_fixedpt_t;
- typedef struct {
- uint16_t int_parts;
- uint16_t fractions;
- } s_fixedpt_t;
- typedef struct {
- uint8_t m_status;
- uint8_t m_stratum;
- uint8_t m_ppoll;
- int8_t m_precision_exp;
- s_fixedpt_t m_rootdelay;
- s_fixedpt_t m_rootdisp;
- uint32_t m_refid;
- l_fixedpt_t m_reftime;
- l_fixedpt_t m_orgtime;
- l_fixedpt_t m_rectime;
- l_fixedpt_t m_xmttime;
- uint32_t m_keyid;
- uint8_t m_digest[ENABLE_FEATURE_NTP_AUTH ? NTP_SHA1_DIGESTSIZE : NTP_MD5_DIGESTSIZE];
- } msg_t;
- typedef struct {
- double d_offset;
- double d_recv_time;
- double d_dispersion;
- } datapoint_t;
- #if ENABLE_FEATURE_NTP_AUTH
- enum {
- HASH_MD5,
- HASH_SHA1,
- };
- typedef struct {
- unsigned id;
- smalluint type;
- smalluint msg_size;
- smalluint key_length;
- char key[0];
- } key_entry_t;
- #endif
- typedef struct {
- len_and_sockaddr *p_lsa;
- char *p_dotted;
- #if ENABLE_FEATURE_NTP_AUTH
- key_entry_t *key_entry;
- #endif
- int p_fd;
- int datapoint_idx;
- uint32_t lastpkt_refid;
- uint8_t lastpkt_status;
- uint8_t lastpkt_stratum;
- uint8_t reachable_bits;
- uint8_t dns_errors;
-
- double next_action_time;
- double p_xmttime;
- double p_raw_delay;
-
-
- double lastpkt_recv_time;
- double lastpkt_delay;
- double lastpkt_rootdelay;
- double lastpkt_rootdisp;
-
- double filter_offset;
- double filter_dispersion;
- double filter_jitter;
- datapoint_t filter_datapoint[NUM_DATAPOINTS];
-
- msg_t p_xmt_msg;
- char p_hostname[1];
- } peer_t;
- #define USING_KERNEL_PLL_LOOP 1
- #define USING_INITIAL_FREQ_ESTIMATION 0
- enum {
- OPT_n = (1 << 0),
- OPT_q = (1 << 1),
- OPT_N = (1 << 2),
- OPT_x = (1 << 3),
- OPT_k = (1 << 4) * ENABLE_FEATURE_NTP_AUTH,
-
-
- OPT_w = (1 << (4+ENABLE_FEATURE_NTP_AUTH)),
- OPT_p = (1 << (5+ENABLE_FEATURE_NTP_AUTH)),
- OPT_S = (1 << (6+ENABLE_FEATURE_NTP_AUTH)),
- OPT_l = (1 << (7+ENABLE_FEATURE_NTP_AUTH)) * ENABLE_FEATURE_NTPD_SERVER,
- OPT_I = (1 << (8+ENABLE_FEATURE_NTP_AUTH)) * ENABLE_FEATURE_NTPD_SERVER,
-
- OPT_qq = (1 << 31),
- };
- struct globals {
- double cur_time;
-
- double rootdelay;
-
- double reftime;
-
- double rootdisp;
- double last_script_run;
- char *script_name;
- llist_t *ntp_peers;
- #if ENABLE_FEATURE_NTPD_SERVER
- int listen_fd;
- char *if_name;
- # define G_listen_fd (G.listen_fd)
- #else
- # define G_listen_fd (-1)
- #endif
- unsigned verbose;
- unsigned peer_cnt;
-
- uint32_t refid;
- uint8_t ntp_status;
-
- #define G_precision_exp -9
-
- #define G_precision_sec 0.002
- uint8_t stratum;
- #define STATE_NSET 0
- #define STATE_SYNC 4
- uint8_t discipline_state;
- uint8_t poll_exp;
- int polladj_count;
- int FREQHOLD_cnt;
- long kernel_freq_drift;
- peer_t *last_update_peer;
- double last_update_offset;
- double last_update_recv_time;
- double discipline_jitter;
-
- unsigned offset_to_jitter_ratio;
-
-
- #if !USING_KERNEL_PLL_LOOP
- double discipline_freq_drift;
-
- double discipline_wander;
- #endif
- };
- #define G (*ptr_to_globals)
- #define VERB1 if (MAX_VERBOSE && G.verbose)
- #define VERB2 if (MAX_VERBOSE >= 2 && G.verbose >= 2)
- #define VERB3 if (MAX_VERBOSE >= 3 && G.verbose >= 3)
- #define VERB4 if (MAX_VERBOSE >= 4 && G.verbose >= 4)
- #define VERB5 if (MAX_VERBOSE >= 5 && G.verbose >= 5)
- #define VERB6 if (MAX_VERBOSE >= 6 && G.verbose >= 6)
- static double LOG2D(int a)
- {
- if (a < 0)
- return 1.0 / (1UL << -a);
- return 1UL << a;
- }
- static ALWAYS_INLINE double SQUARE(double x)
- {
- return x * x;
- }
- static ALWAYS_INLINE double MAXD(double a, double b)
- {
- if (a > b)
- return a;
- return b;
- }
- static ALWAYS_INLINE double MIND(double a, double b)
- {
- if (a < b)
- return a;
- return b;
- }
- static NOINLINE double my_SQRT(double X)
- {
- union {
- float f;
- int32_t i;
- } v;
- double invsqrt;
- double Xhalf = X * 0.5;
-
- v.f = X;
-
- v.i = 0x5f375a86 - (v.i >> 1);
- invsqrt = v.f;
-
- invsqrt = invsqrt * (1.5 - Xhalf * invsqrt * invsqrt);
-
-
- return X * invsqrt;
- }
- static ALWAYS_INLINE double SQRT(double X)
- {
-
- if (sizeof(float) != 4)
- return sqrt(X);
-
- return my_SQRT(X);
- }
- static double
- gettime1900d(void)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- G.cur_time = tv.tv_sec + (1.0e-6 * tv.tv_usec) + OFFSET_1900_1970;
- return G.cur_time;
- }
- static void
- d_to_tv(double d, struct timeval *tv)
- {
- tv->tv_sec = (long)d;
- tv->tv_usec = (d - tv->tv_sec) * 1000000;
- }
- static double
- lfp_to_d(l_fixedpt_t lfp)
- {
- double ret;
- lfp.int_partl = ntohl(lfp.int_partl);
- lfp.fractionl = ntohl(lfp.fractionl);
- ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX);
- return ret;
- }
- static double
- sfp_to_d(s_fixedpt_t sfp)
- {
- double ret;
- sfp.int_parts = ntohs(sfp.int_parts);
- sfp.fractions = ntohs(sfp.fractions);
- ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX);
- return ret;
- }
- #if ENABLE_FEATURE_NTPD_SERVER
- static l_fixedpt_t
- d_to_lfp(double d)
- {
- l_fixedpt_t lfp;
- lfp.int_partl = (uint32_t)d;
- lfp.fractionl = (uint32_t)((d - lfp.int_partl) * UINT_MAX);
- lfp.int_partl = htonl(lfp.int_partl);
- lfp.fractionl = htonl(lfp.fractionl);
- return lfp;
- }
- static s_fixedpt_t
- d_to_sfp(double d)
- {
- s_fixedpt_t sfp;
- sfp.int_parts = (uint16_t)d;
- sfp.fractions = (uint16_t)((d - sfp.int_parts) * USHRT_MAX);
- sfp.int_parts = htons(sfp.int_parts);
- sfp.fractions = htons(sfp.fractions);
- return sfp;
- }
- #endif
- static double
- dispersion(const datapoint_t *dp)
- {
- return dp->d_dispersion + FREQ_TOLERANCE * (G.cur_time - dp->d_recv_time);
- }
- static double
- root_distance(peer_t *p)
- {
-
- return MAXD(MINDISP, p->lastpkt_rootdelay + p->lastpkt_delay) / 2
- + p->lastpkt_rootdisp
- + p->filter_dispersion
- + FREQ_TOLERANCE * (G.cur_time - p->lastpkt_recv_time)
- + p->filter_jitter;
- }
- static void
- set_next(peer_t *p, unsigned t)
- {
- p->next_action_time = G.cur_time + t;
- }
- static void
- filter_datapoints(peer_t *p)
- {
- int i, idx;
- double sum, wavg;
- datapoint_t *fdp;
- #if 0
- int got_newest;
- double minoff, maxoff, w;
- double x = x;
- double oldest_off = oldest_off;
- double oldest_age = oldest_age;
- double newest_off = newest_off;
- double newest_age = newest_age;
- fdp = p->filter_datapoint;
- minoff = maxoff = fdp[0].d_offset;
- for (i = 1; i < NUM_DATAPOINTS; i++) {
- if (minoff > fdp[i].d_offset)
- minoff = fdp[i].d_offset;
- if (maxoff < fdp[i].d_offset)
- maxoff = fdp[i].d_offset;
- }
- idx = p->datapoint_idx;
-
- wavg = 0;
- w = 0.5;
-
- got_newest = 0;
- sum = 0;
- for (i = 0; i < NUM_DATAPOINTS; i++) {
- VERB5 {
- bb_error_msg("datapoint[%d]: off:%f disp:%f(%f) age:%f%s",
- i,
- fdp[idx].d_offset,
- fdp[idx].d_dispersion, dispersion(&fdp[idx]),
- G.cur_time - fdp[idx].d_recv_time,
- (minoff == fdp[idx].d_offset || maxoff == fdp[idx].d_offset)
- ? " (outlier by offset)" : ""
- );
- }
- sum += dispersion(&fdp[idx]) / (2 << i);
- if (minoff == fdp[idx].d_offset) {
- minoff -= 1;
- } else
- if (maxoff == fdp[idx].d_offset) {
- maxoff += 1;
- } else {
- oldest_off = fdp[idx].d_offset;
- oldest_age = G.cur_time - fdp[idx].d_recv_time;
- if (!got_newest) {
- got_newest = 1;
- newest_off = oldest_off;
- newest_age = oldest_age;
- }
- x = oldest_off * w;
- wavg += x;
- w /= 2;
- }
- idx = (idx - 1) & (NUM_DATAPOINTS - 1);
- }
- p->filter_dispersion = sum;
- wavg += x;
-
- x = oldest_age - newest_age;
- if (x != 0) {
- x = newest_age / x;
- if (x < 1) {
- x = (newest_off - oldest_off) * x;
- wavg += x;
- }
- }
- p->filter_offset = wavg;
- #else
- fdp = p->filter_datapoint;
- idx = p->datapoint_idx;
-
- p->filter_offset = fdp[idx].d_offset;
-
- wavg = 0;
- sum = 0;
- for (i = 0; i < NUM_DATAPOINTS; i++) {
- sum += dispersion(&fdp[idx]) / (2 << i);
- wavg += fdp[idx].d_offset;
- idx = (idx - 1) & (NUM_DATAPOINTS - 1);
- }
- wavg /= NUM_DATAPOINTS;
- p->filter_dispersion = sum;
- #endif
-
- sum = 0;
- for (i = 0; i < NUM_DATAPOINTS; i++) {
- sum += SQUARE(wavg - fdp[i].d_offset);
- }
- sum = SQRT(sum / NUM_DATAPOINTS);
- p->filter_jitter = sum > G_precision_sec ? sum : G_precision_sec;
- VERB4 bb_error_msg("filter offset:%+f disp:%f jitter:%f",
- p->filter_offset,
- p->filter_dispersion,
- p->filter_jitter);
- }
- static void
- reset_peer_stats(peer_t *p, double offset)
- {
- int i;
- bool small_ofs = fabs(offset) < STEP_THRESHOLD;
-
- for (i = 0; i < NUM_DATAPOINTS; i++) {
- if (small_ofs) {
- p->filter_datapoint[i].d_recv_time += offset;
- if (p->filter_datapoint[i].d_offset != 0) {
- p->filter_datapoint[i].d_offset -= offset;
-
-
-
-
- }
- } else {
- p->filter_datapoint[i].d_recv_time = G.cur_time;
- p->filter_datapoint[i].d_offset = 0;
-
- }
- }
- if (small_ofs) {
- p->lastpkt_recv_time += offset;
- } else {
-
- p->lastpkt_recv_time = G.cur_time;
- }
- filter_datapoints(p);
- VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
- }
- static len_and_sockaddr*
- resolve_peer_hostname(peer_t *p)
- {
- len_and_sockaddr *lsa = host2sockaddr(p->p_hostname, 123);
- if (lsa) {
- free(p->p_lsa);
- free(p->p_dotted);
- p->p_lsa = lsa;
- p->p_dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
- VERB1 if (strcmp(p->p_hostname, p->p_dotted) != 0)
- bb_error_msg("'%s' is %s", p->p_hostname, p->p_dotted);
- p->dns_errors = 0;
- return lsa;
- }
- p->dns_errors = ((p->dns_errors << 1) | 1) & DNS_ERRORS_CAP;
- return lsa;
- }
- #if !ENABLE_FEATURE_NTP_AUTH
- #define add_peers(s, key_entry) \
- add_peers(s)
- #endif
- static void
- add_peers(const char *s, key_entry_t *key_entry)
- {
- llist_t *item;
- peer_t *p;
- p = xzalloc(sizeof(*p) + strlen(s));
- strcpy(p->p_hostname, s);
- p->p_fd = -1;
- p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3);
- p->next_action_time = G.cur_time;
- reset_peer_stats(p, STEP_THRESHOLD);
-
- if (resolve_peer_hostname(p)) {
- for (item = G.ntp_peers; item != NULL; item = item->link) {
- peer_t *pp = (peer_t *) item->data;
- if (pp->p_dotted && strcmp(p->p_dotted, pp->p_dotted) == 0) {
- bb_error_msg("duplicate peer %s (%s)", s, p->p_dotted);
- free(p->p_lsa);
- free(p->p_dotted);
- free(p);
- return;
- }
- }
- }
- IF_FEATURE_NTP_AUTH(p->key_entry = key_entry;)
- llist_add_to(&G.ntp_peers, p);
- G.peer_cnt++;
- }
- static int
- do_sendto(int fd,
- const struct sockaddr *from, const struct sockaddr *to, socklen_t addrlen,
- msg_t *msg, ssize_t len)
- {
- ssize_t ret;
- errno = 0;
- if (!from) {
- ret = sendto(fd, msg, len, MSG_DONTWAIT, to, addrlen);
- } else {
- ret = send_to_from(fd, msg, len, MSG_DONTWAIT, to, from, addrlen);
- }
- if (ret != len) {
- bb_perror_msg("send failed");
- return -1;
- }
- return 0;
- }
- #if ENABLE_FEATURE_NTP_AUTH
- static void
- hash(key_entry_t *key_entry, const msg_t *msg, uint8_t *output)
- {
- union {
- md5_ctx_t m;
- sha1_ctx_t s;
- } ctx;
- unsigned hash_size = sizeof(*msg) - sizeof(msg->m_keyid) - sizeof(msg->m_digest);
- switch (key_entry->type) {
- case HASH_MD5:
- md5_begin(&ctx.m);
- md5_hash(&ctx.m, key_entry->key, key_entry->key_length);
- md5_hash(&ctx.m, msg, hash_size);
- md5_end(&ctx.m, output);
- break;
- default:
- sha1_begin(&ctx.s);
- sha1_hash(&ctx.s, key_entry->key, key_entry->key_length);
- sha1_hash(&ctx.s, msg, hash_size);
- sha1_end(&ctx.s, output);
- break;
- }
- }
- static void
- hash_peer(peer_t *p)
- {
- p->p_xmt_msg.m_keyid = htonl(p->key_entry->id);
- hash(p->key_entry, &p->p_xmt_msg, p->p_xmt_msg.m_digest);
- }
- static int
- hashes_differ(peer_t *p, const msg_t *msg)
- {
- uint8_t digest[NTP_SHA1_DIGESTSIZE];
- hash(p->key_entry, msg, digest);
- return memcmp(digest, msg->m_digest, p->key_entry->msg_size - NTP_MSGSIZE_NOAUTH - KEYID_SIZE);
- }
- #endif
- static void
- send_query_to_peer(peer_t *p)
- {
- if (!p->p_lsa)
- return;
-
- #define PROBE_LOCAL_ADDR
- if (p->p_fd == -1) {
- int fd, family;
- len_and_sockaddr *local_lsa;
- family = p->p_lsa->u.sa.sa_family;
- p->p_fd = fd = xsocket_type(&local_lsa, family, SOCK_DGRAM);
-
- PROBE_LOCAL_ADDR
- xbind(fd, &local_lsa->u.sa, local_lsa->len);
- PROBE_LOCAL_ADDR
- #if ENABLE_FEATURE_IPV6
- if (family == AF_INET)
- #endif
- setsockopt_int(fd, IPPROTO_IP, IP_TOS, IPTOS_DSCP_AF21);
- free(local_lsa);
- }
-
- VERB1 bb_error_msg("sending query to %s", p->p_dotted);
-
- p->p_xmt_msg.m_xmttime.int_partl = rand();
- p->p_xmt_msg.m_xmttime.fractionl = rand();
- p->p_xmttime = gettime1900d();
-
- p->reachable_bits <<= 1;
- #if ENABLE_FEATURE_NTP_AUTH
- if (p->key_entry)
- hash_peer(p);
- if (do_sendto(p->p_fd, NULL, &p->p_lsa->u.sa, p->p_lsa->len,
- &p->p_xmt_msg, !p->key_entry ? NTP_MSGSIZE_NOAUTH : p->key_entry->msg_size) == -1
- )
- #else
- if (do_sendto(p->p_fd, NULL, &p->p_lsa->u.sa, p->p_lsa->len,
- &p->p_xmt_msg, NTP_MSGSIZE_NOAUTH) == -1
- )
- #endif
- {
- close(p->p_fd);
- p->p_fd = -1;
-
- set_next(p, RETRY_INTERVAL);
- return;
- }
- set_next(p, RESPONSE_INTERVAL);
- }
- static void run_script(const char *action, double offset)
- {
- char *argv[3];
- char *env1, *env2, *env3, *env4;
- G.last_script_run = G.cur_time;
- if (!G.script_name)
- return;
- argv[0] = (char*) G.script_name;
- argv[1] = (char*) action;
- argv[2] = NULL;
- VERB1 bb_error_msg("executing '%s %s'", G.script_name, action);
- env1 = xasprintf("%s=%u", "stratum", G.stratum);
- putenv(env1);
- env2 = xasprintf("%s=%ld", "freq_drift_ppm", G.kernel_freq_drift);
- putenv(env2);
- env3 = xasprintf("%s=%u", "poll_interval", 1 << G.poll_exp);
- putenv(env3);
- env4 = xasprintf("%s=%f", "offset", offset);
- putenv(env4);
-
-
-
- spawn(argv);
- unsetenv("stratum");
- unsetenv("freq_drift_ppm");
- unsetenv("poll_interval");
- unsetenv("offset");
- free(env1);
- free(env2);
- free(env3);
- free(env4);
- }
- static NOINLINE void
- step_time(double offset)
- {
- llist_t *item;
- double dtime;
- struct timeval tvc, tvn;
- char buf[sizeof("yyyy-mm-dd hh:mm:ss") + 4];
- time_t tval;
- gettimeofday(&tvc, NULL);
- dtime = tvc.tv_sec + (1.0e-6 * tvc.tv_usec) + offset;
- d_to_tv(dtime, &tvn);
- if (settimeofday(&tvn, NULL) == -1)
- bb_perror_msg_and_die("settimeofday");
- VERB2 {
- tval = tvc.tv_sec;
- strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
- bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec);
- }
- tval = tvn.tv_sec;
- strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
- bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset);
-
-
-
- G.cur_time += offset;
- G.last_update_recv_time += offset;
- G.last_script_run += offset;
-
- for (item = G.ntp_peers; item != NULL; item = item->link) {
- peer_t *pp = (peer_t *) item->data;
- reset_peer_stats(pp, offset);
-
-
- pp->next_action_time += offset;
- if (pp->p_fd >= 0) {
-
- close(pp->p_fd);
- pp->p_fd = -1;
- set_next(pp, RETRY_INTERVAL);
- }
- }
- }
- static void clamp_pollexp_and_set_MAXSTRAT(void)
- {
- if (G.poll_exp < MINPOLL)
- G.poll_exp = MINPOLL;
- if (G.poll_exp > BIGPOLL)
- G.poll_exp = BIGPOLL;
- G.polladj_count = 0;
- G.stratum = MAXSTRAT;
- }
- typedef struct {
- peer_t *p;
- int type;
- double edge;
- double opt_rd;
- } point_t;
- static int
- compare_point_edge(const void *aa, const void *bb)
- {
- const point_t *a = aa;
- const point_t *b = bb;
- if (a->edge < b->edge) {
- return -1;
- }
- return (a->edge > b->edge);
- }
- typedef struct {
- peer_t *p;
- double metric;
- } survivor_t;
- static int
- compare_survivor_metric(const void *aa, const void *bb)
- {
- const survivor_t *a = aa;
- const survivor_t *b = bb;
- if (a->metric < b->metric) {
- return -1;
- }
- return (a->metric > b->metric);
- }
- static int
- fit(peer_t *p, double rd)
- {
- if ((p->reachable_bits & (p->reachable_bits-1)) == 0) {
-
- VERB4 bb_error_msg("peer %s unfit for selection: "
- "unreachable", p->p_dotted);
- return 0;
- }
- #if 0
- if ((p->lastpkt_status & LI_ALARM) == LI_ALARM
- || p->lastpkt_stratum >= MAXSTRAT
- ) {
- VERB4 bb_error_msg("peer %s unfit for selection: "
- "bad status/stratum", p->p_dotted);
- return 0;
- }
- #endif
-
- if (rd > MAXDIST + FREQ_TOLERANCE * (1 << G.poll_exp)) {
- VERB3 bb_error_msg("peer %s unfit for selection: "
- "root distance %f too high, jitter:%f",
- p->p_dotted, rd, p->filter_jitter
- );
- return 0;
- }
- return 1;
- }
- static peer_t*
- select_and_cluster(void)
- {
- peer_t *p;
- llist_t *item;
- int i, j;
- int size = 3 * G.peer_cnt;
-
- point_t point[size];
- unsigned num_points, num_candidates;
- double low, high;
- unsigned num_falsetickers;
-
- survivor_t survivor[size];
- unsigned num_survivors;
-
- num_points = 0;
- item = G.ntp_peers;
- while (item != NULL) {
- double rd, offset;
- p = (peer_t *) item->data;
- rd = root_distance(p);
- offset = p->filter_offset;
- if (!fit(p, rd)) {
- item = item->link;
- continue;
- }
- VERB5 bb_error_msg("interval: [%f %f %f] %s",
- offset - rd,
- offset,
- offset + rd,
- p->p_dotted
- );
- point[num_points].p = p;
- point[num_points].type = -1;
- point[num_points].edge = offset - rd;
- point[num_points].opt_rd = rd;
- num_points++;
- point[num_points].p = p;
- point[num_points].type = 0;
- point[num_points].edge = offset;
- point[num_points].opt_rd = rd;
- num_points++;
- point[num_points].p = p;
- point[num_points].type = 1;
- point[num_points].edge = offset + rd;
- point[num_points].opt_rd = rd;
- num_points++;
- item = item->link;
- }
- num_candidates = num_points / 3;
- if (num_candidates == 0) {
- VERB3 bb_error_msg("no valid datapoints%s", ", no peer selected");
- return NULL;
- }
- qsort(point, num_points, sizeof(point[0]), compare_point_edge);
-
- num_falsetickers = 0;
- while (1) {
- int c;
- unsigned num_midpoints = 0;
- low = 1 << 9;
- high = - (1 << 9);
- c = 0;
- for (i = 0; i < num_points; i++) {
-
- c -= point[i].type;
- if (c >= num_candidates - num_falsetickers) {
-
- low = point[i].edge;
- break;
- }
- if (point[i].type == 0)
- num_midpoints++;
- }
- c = 0;
- for (i = num_points-1; i >= 0; i--) {
- c += point[i].type;
- if (c >= num_candidates - num_falsetickers) {
- high = point[i].edge;
- break;
- }
- if (point[i].type == 0)
- num_midpoints++;
- }
-
- if (num_midpoints <= num_falsetickers && low < high)
- break;
- num_falsetickers++;
- if (num_falsetickers * 2 >= num_candidates) {
- VERB3 bb_error_msg("falsetickers:%d, candidates:%d%s",
- num_falsetickers, num_candidates,
- ", no peer selected");
- return NULL;
- }
- }
- VERB4 bb_error_msg("selected interval: [%f, %f]; candidates:%d falsetickers:%d",
- low, high, num_candidates, num_falsetickers);
-
-
- num_survivors = 0;
- for (i = 0; i < num_points; i++) {
- if (point[i].edge < low || point[i].edge > high)
- continue;
- p = point[i].p;
- survivor[num_survivors].p = p;
-
- survivor[num_survivors].metric = MAXDIST * p->lastpkt_stratum + point[i].opt_rd;
- VERB5 bb_error_msg("survivor[%d] metric:%f peer:%s",
- num_survivors, survivor[num_survivors].metric, p->p_dotted);
- num_survivors++;
- }
-
- if (num_survivors < MIN_SELECTED) {
- VERB3 bb_error_msg("survivors:%d%s",
- num_survivors,
- ", no peer selected");
- return NULL;
- }
- qsort(survivor, num_survivors, sizeof(survivor[0]), compare_survivor_metric);
-
- while (1) {
- unsigned max_idx = max_idx;
- double max_selection_jitter = max_selection_jitter;
- double min_jitter = min_jitter;
- if (num_survivors <= MIN_CLUSTERED) {
- VERB4 bb_error_msg("num_survivors %d <= %d, not discarding more",
- num_survivors, MIN_CLUSTERED);
- break;
- }
-
- for (i = 0; i < num_survivors; i++) {
- double selection_jitter_sq;
- p = survivor[i].p;
- if (i == 0 || p->filter_jitter < min_jitter)
- min_jitter = p->filter_jitter;
- selection_jitter_sq = 0;
- for (j = 0; j < num_survivors; j++) {
- peer_t *q = survivor[j].p;
- selection_jitter_sq += SQUARE(p->filter_offset - q->filter_offset);
- }
- if (i == 0 || selection_jitter_sq > max_selection_jitter) {
- max_selection_jitter = selection_jitter_sq;
- max_idx = i;
- }
- VERB6 bb_error_msg("survivor %d selection_jitter^2:%f",
- i, selection_jitter_sq);
- }
- max_selection_jitter = SQRT(max_selection_jitter / num_survivors);
- VERB5 bb_error_msg("max_selection_jitter (at %d):%f min_jitter:%f",
- max_idx, max_selection_jitter, min_jitter);
-
- if (max_selection_jitter < min_jitter) {
- VERB4 bb_error_msg("max_selection_jitter:%f < min_jitter:%f, num_survivors:%d, not discarding more",
- max_selection_jitter, min_jitter, num_survivors);
- break;
- }
-
- VERB6 bb_error_msg("dropping survivor %d", max_idx);
- num_survivors--;
- while (max_idx < num_survivors) {
- survivor[max_idx] = survivor[max_idx + 1];
- max_idx++;
- }
- }
- if (0) {
-
- double x, y, z, w;
- y = z = w = 0;
- for (i = 0; i < num_survivors; i++) {
- p = survivor[i].p;
- x = root_distance(p);
- y += 1 / x;
- z += p->filter_offset / x;
- w += SQUARE(p->filter_offset - survivor[0].p->filter_offset) / x;
- }
-
-
- }
-
- p = survivor[0].p;
- if (G.last_update_peer
- && G.last_update_peer->lastpkt_stratum <= p->lastpkt_stratum
- ) {
-
- for (i = 1; i < num_survivors; i++) {
- if (G.last_update_peer == survivor[i].p) {
- VERB5 bb_error_msg("keeping old synced peer");
- p = G.last_update_peer;
- goto keep_old;
- }
- }
- }
- G.last_update_peer = p;
- keep_old:
- VERB4 bb_error_msg("selected peer %s filter_offset:%+f age:%f",
- p->p_dotted,
- p->filter_offset,
- G.cur_time - p->lastpkt_recv_time
- );
- return p;
- }
- static void
- set_new_values(int disc_state, double offset, double recv_time)
- {
-
- VERB4 bb_error_msg("disc_state=%d last update offset=%f recv_time=%f",
- disc_state, offset, recv_time);
- G.discipline_state = disc_state;
- G.last_update_offset = offset;
- G.last_update_recv_time = recv_time;
- }
- static NOINLINE int
- update_local_clock(peer_t *p)
- {
- int rc;
- struct timex tmx;
-
- double offset = p->filter_offset;
- double recv_time = p->lastpkt_recv_time;
- double abs_offset;
- #if !USING_KERNEL_PLL_LOOP
- double freq_drift;
- #endif
- #if !USING_KERNEL_PLL_LOOP || USING_INITIAL_FREQ_ESTIMATION
- double since_last_update;
- #endif
- double etemp, dtemp;
- abs_offset = fabs(offset);
- #if 0
-
-
- if (abs_offset > PANIC_THRESHOLD) {
- bb_error_msg_and_die("offset %f far too big, exiting", offset);
- }
- #endif
-
- if (recv_time <= G.last_update_recv_time) {
- VERB3 bb_error_msg("update from %s: same or older datapoint, not using it",
- p->p_dotted);
- return 0;
- }
-
- #if !USING_KERNEL_PLL_LOOP || USING_INITIAL_FREQ_ESTIMATION
- since_last_update = recv_time - G.reftime;
- #endif
- #if !USING_KERNEL_PLL_LOOP
- freq_drift = 0;
- #endif
- #if USING_INITIAL_FREQ_ESTIMATION
- if (G.discipline_state == STATE_FREQ) {
-
- if (since_last_update < WATCH_THRESHOLD) {
- VERB4 bb_error_msg("measuring drift, datapoint ignored, %f sec remains",
- WATCH_THRESHOLD - since_last_update);
- return 0;
- }
- # if !USING_KERNEL_PLL_LOOP
- freq_drift = (offset - G.last_update_offset) / since_last_update;
- # endif
- }
- #endif
-
- if (abs_offset > STEP_THRESHOLD) {
- #if 0
- double remains;
- switch (G.discipline_state) {
- case STATE_SYNC:
-
- VERB3 bb_error_msg("update from %s: offset:%+f, spike%s",
- p->p_dotted, offset,
- "");
- G.discipline_state = STATE_SPIK;
- return -1;
- case STATE_SPIK:
-
- remains = WATCH_THRESHOLD - since_last_update;
- if (remains > 0) {
- VERB3 bb_error_msg("update from %s: offset:%+f, spike%s",
- p->p_dotted, offset,
- ", datapoint ignored");
- return -1;
- }
-
- }
- #endif
-
- VERB4 bb_error_msg("stepping time by %+f; poll_exp=MINPOLL", offset);
- step_time(offset);
- if (option_mask32 & OPT_q) {
-
- exit(0);
- }
- clamp_pollexp_and_set_MAXSTRAT();
- run_script("step", offset);
- recv_time += offset;
- #if USING_INITIAL_FREQ_ESTIMATION
- if (G.discipline_state == STATE_NSET) {
- set_new_values(STATE_FREQ, 0, recv_time);
- return 1;
- }
- #endif
- abs_offset = offset = 0;
- set_new_values(STATE_SYNC, offset, recv_time);
- } else {
-
- G.offset_to_jitter_ratio = abs_offset / G.discipline_jitter;
-
- etemp = SQUARE(G.discipline_jitter);
- dtemp = SQUARE(offset - G.last_update_offset);
- G.discipline_jitter = SQRT(etemp + (dtemp - etemp) / AVG);
- if (G.discipline_jitter < G_precision_sec)
- G.discipline_jitter = G_precision_sec;
- switch (G.discipline_state) {
- case STATE_NSET:
- if (option_mask32 & OPT_q) {
-
- exit(0);
- }
- #if USING_INITIAL_FREQ_ESTIMATION
-
- set_new_values(STATE_FREQ, offset, recv_time);
- #else
- set_new_values(STATE_SYNC, offset, recv_time);
- #endif
- VERB4 bb_error_msg("transitioning to FREQ, datapoint ignored");
- return 0;
- #if 0
- case STATE_FSET:
-
- set_new_values(STATE_SYNC, offset, recv_time);
-
- break;
- #endif
- #if USING_INITIAL_FREQ_ESTIMATION
- case STATE_FREQ:
-
- set_new_values(STATE_SYNC, offset, recv_time);
- break;
- #endif
- default:
- #if !USING_KERNEL_PLL_LOOP
-
- if ((1 << G.poll_exp) > ALLAN / 2) {
- etemp = FLL - G.poll_exp;
- if (etemp < AVG)
- etemp = AVG;
- freq_drift += (offset - G.last_update_offset) / (MAXD(since_last_update, ALLAN) * etemp);
- }
-
- etemp = MIND(since_last_update, (1 << G.poll_exp));
- dtemp = (4 * PLL) << G.poll_exp;
- freq_drift += offset * etemp / SQUARE(dtemp);
- #endif
- set_new_values(STATE_SYNC, offset, recv_time);
- break;
- }
- if (G.stratum != p->lastpkt_stratum + 1) {
- G.stratum = p->lastpkt_stratum + 1;
- run_script("stratum", offset);
- }
- }
- G.reftime = G.cur_time;
- G.ntp_status = p->lastpkt_status;
- G.refid = p->lastpkt_refid;
- G.rootdelay = p->lastpkt_rootdelay + p->lastpkt_delay;
- dtemp = p->filter_jitter;
- dtemp += MAXD(p->filter_dispersion + FREQ_TOLERANCE * (G.cur_time - p->lastpkt_recv_time) + abs_offset, MINDISP);
- G.rootdisp = p->lastpkt_rootdisp + dtemp;
- VERB4 bb_error_msg("updating leap/refid/reftime/rootdisp from peer %s", p->p_dotted);
-
- #if !USING_KERNEL_PLL_LOOP
-
- dtemp = G.discipline_freq_drift + freq_drift;
- G.discipline_freq_drift = MAXD(MIND(MAXDRIFT, dtemp), -MAXDRIFT);
- etemp = SQUARE(G.discipline_wander);
- dtemp = SQUARE(dtemp);
- G.discipline_wander = SQRT(etemp + (dtemp - etemp) / AVG);
- VERB4 bb_error_msg("discipline freq_drift=%.9f(int:%ld corr:%e) wander=%f",
- G.discipline_freq_drift,
- (long)(G.discipline_freq_drift * 65536e6),
- freq_drift,
- G.discipline_wander);
- #endif
- VERB4 {
- memset(&tmx, 0, sizeof(tmx));
- if (adjtimex(&tmx) < 0)
- bb_perror_msg_and_die("adjtimex");
- bb_error_msg("p adjtimex freq:%ld offset:%+ld status:0x%x tc:%ld",
- tmx.freq, tmx.offset, tmx.status, tmx.constant);
- }
- memset(&tmx, 0, sizeof(tmx));
- #if 0
- tmx.modes = ADJ_FREQUENCY | ADJ_OFFSET;
-
- tmx.freq = G.discipline_freq_drift * 65536e6;
- #endif
- tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;
- tmx.offset = (long)(offset * 1000000);
- if (SLEW_THRESHOLD < STEP_THRESHOLD) {
- if (tmx.offset > (long)(SLEW_THRESHOLD * 1000000)) {
- tmx.offset = (long)(SLEW_THRESHOLD * 1000000);
- }
- if (tmx.offset < -(long)(SLEW_THRESHOLD * 1000000)) {
- tmx.offset = -(long)(SLEW_THRESHOLD * 1000000);
- }
- }
- tmx.status = STA_PLL;
- if (G.FREQHOLD_cnt != 0) {
-
- if (G.FREQHOLD_cnt < 0) {
-
-
- G.FREQHOLD_cnt = 1 + MIN_FREQHOLD + ((unsigned)(abs(tmx.offset)) >> 16);
- }
- G.FREQHOLD_cnt--;
- tmx.status |= STA_FREQHOLD;
- }
- if (G.ntp_status & LI_PLUSSEC)
- tmx.status |= STA_INS;
- if (G.ntp_status & LI_MINUSSEC)
- tmx.status |= STA_DEL;
- tmx.constant = (int)G.poll_exp - 4;
-
- if (G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
- tmx.constant--;
- if (tmx.constant < 0)
- tmx.constant = 0;
-
-
- rc = adjtimex(&tmx);
- if (rc < 0)
- bb_perror_msg_and_die("adjtimex");
-
- VERB4 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x",
- rc, tmx.freq, tmx.offset, tmx.status);
- G.kernel_freq_drift = tmx.freq / 65536;
- VERB2 bb_error_msg("update from:%s offset:%+f delay:%f jitter:%f clock drift:%+.3fppm tc:%d",
- p->p_dotted,
- offset,
- p->p_raw_delay,
- G.discipline_jitter,
- (double)tmx.freq / 65536,
- (int)tmx.constant
- );
- return 1;
- }
- static unsigned
- poll_interval(int upper_bound)
- {
- unsigned interval, r, mask;
- interval = 1 << G.poll_exp;
- if (interval > upper_bound)
- interval = upper_bound;
- mask = ((interval-1) >> 4) | 1;
- r = rand();
- interval += r & mask;
- VERB4 bb_error_msg("chose poll interval:%u (poll_exp:%d)", interval, G.poll_exp);
- return interval;
- }
- static void
- adjust_poll(int count)
- {
- G.polladj_count += count;
- if (G.polladj_count > POLLADJ_LIMIT) {
- G.polladj_count = 0;
- if (G.poll_exp < MAXPOLL) {
- G.poll_exp++;
- VERB4 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d",
- G.discipline_jitter, G.poll_exp);
- }
- } else if (G.polladj_count < -POLLADJ_LIMIT || (count < 0 && G.poll_exp > BIGPOLL)) {
- G.polladj_count = 0;
- if (G.poll_exp > MINPOLL) {
- llist_t *item;
- G.poll_exp--;
-
- for (item = G.ntp_peers; item != NULL; item = item->link) {
- peer_t *pp = (peer_t *) item->data;
- if (pp->p_fd < 0)
- pp->next_action_time -= (1 << G.poll_exp);
- }
- VERB4 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d",
- G.discipline_jitter, G.poll_exp);
- }
- } else {
- VERB4 bb_error_msg("polladj: count:%d", G.polladj_count);
- }
- }
- static NOINLINE void
- recv_and_process_peer_pkt(peer_t *p)
- {
- int rc;
- ssize_t size;
- msg_t msg;
- double T1, T2, T3, T4;
- double offset;
- double prev_delay, delay;
- unsigned interval;
- datapoint_t *datapoint;
- peer_t *q;
- offset = 0;
-
- recv_again:
- size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT);
- if (size < 0) {
- if (errno == EINTR)
-
- goto recv_again;
- if (errno == EAGAIN)
-
- return;
-
- bb_perror_msg_and_die("recv(%s) error", p->p_dotted);
- }
- #if ENABLE_FEATURE_NTP_AUTH
- if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH && size != NTP_MSGSIZE_SHA1_AUTH) {
- bb_error_msg("malformed packet received from %s", p->p_dotted);
- return;
- }
- if (p->key_entry && hashes_differ(p, &msg)) {
- bb_error_msg("invalid cryptographic hash received from %s", p->p_dotted);
- return;
- }
- #else
- if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH) {
- bb_error_msg("malformed packet received from %s", p->p_dotted);
- return;
- }
- #endif
- if (msg.m_orgtime.int_partl != p->p_xmt_msg.m_xmttime.int_partl
- || msg.m_orgtime.fractionl != p->p_xmt_msg.m_xmttime.fractionl
- ) {
-
- return;
- }
-
- close(p->p_fd);
- p->p_fd = -1;
- if ((msg.m_status & LI_ALARM) == LI_ALARM
- || msg.m_stratum == 0
- || msg.m_stratum > NTP_MAXSTRATUM
- ) {
- bb_error_msg("reply from %s: peer is unsynced", p->p_dotted);
-
- if (G.poll_exp < BIGPOLL)
- goto increase_interval;
- goto pick_normal_interval;
- }
-
- T1 = p->p_xmttime;
- T2 = lfp_to_d(msg.m_rectime);
- T3 = lfp_to_d(msg.m_xmttime);
- T4 = G.cur_time;
- delay = (T4 - T1) - (T3 - T2);
-
- prev_delay = p->p_raw_delay;
- p->p_raw_delay = (delay < 0 ? 0.0 : delay);
- if (p->reachable_bits
- && delay > prev_delay * BAD_DELAY_GROWTH
- && delay > 1.0 / (8 * 1024)
- ) {
- bb_error_msg("reply from %s: delay %f is too high, ignoring", p->p_dotted, delay);
- goto pick_normal_interval;
- }
-
- if (delay < G_precision_sec)
- delay = G_precision_sec;
- p->lastpkt_delay = delay;
- p->lastpkt_recv_time = T4;
- VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
- p->lastpkt_status = msg.m_status;
- p->lastpkt_stratum = msg.m_stratum;
- p->lastpkt_rootdelay = sfp_to_d(msg.m_rootdelay);
- p->lastpkt_rootdisp = sfp_to_d(msg.m_rootdisp);
- p->lastpkt_refid = msg.m_refid;
- p->datapoint_idx = p->reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0;
- datapoint = &p->filter_datapoint[p->datapoint_idx];
- datapoint->d_recv_time = T4;
- datapoint->d_offset = offset = ((T2 - T1) + (T3 - T4)) / 2;
- datapoint->d_dispersion = LOG2D(msg.m_precision_exp) + G_precision_sec;
- if (!p->reachable_bits) {
-
- int i;
- for (i = 0; i < NUM_DATAPOINTS; i++) {
- p->filter_datapoint[i].d_offset = offset;
- }
- }
- p->reachable_bits |= 1;
- if ((MAX_VERBOSE && G.verbose) || (option_mask32 & OPT_w)) {
- bb_error_msg("reply from %s: offset:%+f delay:%f status:0x%02x strat:%d refid:0x%08x rootdelay:%f reach:0x%02x",
- p->p_dotted,
- offset,
- p->p_raw_delay,
- p->lastpkt_status,
- p->lastpkt_stratum,
- p->lastpkt_refid,
- p->lastpkt_rootdelay,
- p->reachable_bits
-
- );
- }
-
- filter_datapoints(p);
- q = select_and_cluster();
- rc = 0;
- if (q) {
- if (!(option_mask32 & OPT_w)) {
- rc = update_local_clock(q);
- #if 0
-
- if (fabs(q->filter_offset) >= POLLDOWN_OFFSET) {
- VERB4 bb_error_msg("offset:%+f > POLLDOWN_OFFSET", q->filter_offset);
- adjust_poll(-POLLADJ_LIMIT * 3);
- rc = 0;
- }
- #endif
- }
- } else {
-
- if (G.poll_exp < BIGPOLL)
- goto increase_interval;
- }
- if (rc != 0) {
-
- if (rc > 0 && G.offset_to_jitter_ratio <= POLLADJ_GATE) {
-
- increase_interval:
- adjust_poll(MINPOLL);
- } else {
- VERB3 if (rc > 0)
- bb_error_msg("want smaller interval: offset/jitter = %u",
- G.offset_to_jitter_ratio);
- adjust_poll(-G.poll_exp * 2);
- }
- }
-
- pick_normal_interval:
- interval = poll_interval(INT_MAX);
- if (fabs(offset) >= BIGOFF && interval > BIGOFF_INTERVAL) {
-
- interval = BIGOFF_INTERVAL;
- }
- set_next(p, interval);
- }
- #if ENABLE_FEATURE_NTPD_SERVER
- static NOINLINE void
- recv_and_process_client_pkt(void )
- {
- ssize_t size;
-
- len_and_sockaddr *to;
- struct sockaddr *from;
- msg_t msg;
- uint8_t query_status;
- l_fixedpt_t query_xmttime;
- to = get_sock_lsa(G_listen_fd);
- from = xzalloc(to->len);
- size = recv_from_to(G_listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len);
- #if ENABLE_FEATURE_NTP_AUTH
- if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH && size != NTP_MSGSIZE_SHA1_AUTH)
- #else
- if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE_MD5_AUTH)
- #endif
- {
- char *addr;
- if (size < 0) {
- if (errno == EAGAIN)
- goto bail;
- bb_perror_msg_and_die("recv");
- }
- addr = xmalloc_sockaddr2dotted_noport(from);
- bb_error_msg("malformed packet received from %s: size %u", addr, (int)size);
- free(addr);
- goto bail;
- }
-
- if ((msg.m_status & MODE_MASK) != MODE_CLIENT
- && (msg.m_status & MODE_MASK) != MODE_SYM_ACT
- ) {
- goto bail;
- }
- query_status = msg.m_status;
- query_xmttime = msg.m_xmttime;
-
- memset(&msg, 0, sizeof(msg));
- msg.m_status = G.stratum < MAXSTRAT ? (G.ntp_status & LI_MASK) : LI_ALARM;
- msg.m_status |= (query_status & VERSION_MASK);
- msg.m_status |= ((query_status & MODE_MASK) == MODE_CLIENT) ?
- MODE_SERVER : MODE_SYM_PAS;
- msg.m_stratum = G.stratum;
- msg.m_ppoll = G.poll_exp;
- msg.m_precision_exp = G_precision_exp;
-
- msg.m_rectime = d_to_lfp(G.cur_time);
- msg.m_xmttime = d_to_lfp(gettime1900d());
- if (G.peer_cnt == 0) {
-
- G.reftime = G.cur_time;
- }
- msg.m_reftime = d_to_lfp(G.reftime);
- msg.m_orgtime = query_xmttime;
- msg.m_rootdelay = d_to_sfp(G.rootdelay);
- msg.m_rootdisp = d_to_sfp(G.rootdisp);
-
- msg.m_refid = G.refid;
-
- do_sendto(G_listen_fd,
- &to->u.sa, from, to->len,
- &msg, size);
- bail:
- free(to);
- free(from);
- }
- #endif
- #if ENABLE_FEATURE_NTP_AUTH
- static key_entry_t *
- find_key_entry(llist_t *key_entries, unsigned id)
- {
- while (key_entries) {
- key_entry_t *cur = (key_entry_t*) key_entries->data;
- if (cur->id == id)
- return cur;
- key_entries = key_entries->link;
- }
- bb_error_msg_and_die("key %u is not defined", id);
- }
- #endif
- static NOINLINE void ntp_init(char **argv)
- {
- unsigned opts;
- llist_t *peers;
- #if ENABLE_FEATURE_NTP_AUTH
- llist_t *key_entries;
- char *key_file_path;
- #endif
- srand(getpid());
- if (getuid())
- bb_error_msg_and_die(bb_msg_you_must_be_root);
-
- G.discipline_jitter = G_precision_sec;
- G.stratum = MAXSTRAT;
- if (BURSTPOLL != 0)
- G.poll_exp = BURSTPOLL;
- G.last_script_run = G.reftime = G.last_update_recv_time = gettime1900d();
- G.FREQHOLD_cnt = -1;
-
- peers = NULL;
- IF_FEATURE_NTP_AUTH(key_entries = NULL;)
- opts = getopt32(argv, "^"
- "nqNx"
- IF_FEATURE_NTP_AUTH("k:")
- "wp:*S:"IF_FEATURE_NTPD_SERVER("l")
- IF_FEATURE_NTPD_SERVER("I:")
- "d"
- "46aAbgL"
- "\0"
- "dd:wn"
- IF_FEATURE_NTPD_SERVER(":Il")
- IF_FEATURE_NTP_AUTH(, &key_file_path)
- , &peers, &G.script_name
- IF_FEATURE_NTPD_SERVER(, &G.if_name)
- , &G.verbose
- );
- #if ENABLE_FEATURE_NTPD_SERVER
- G_listen_fd = -1;
- if (opts & OPT_l) {
- G_listen_fd = create_and_bind_dgram_or_die(NULL, 123);
- if (G.if_name) {
- if (setsockopt_bindtodevice(G_listen_fd, G.if_name))
- xfunc_die();
- }
- socket_want_pktinfo(G_listen_fd);
- setsockopt_int(G_listen_fd, IPPROTO_IP, IP_TOS, IPTOS_DSCP_AF21);
- }
- #endif
-
- if (opts & OPT_N)
- setpriority(PRIO_PROCESS, 0, -15);
- if (!(opts & OPT_n)) {
- bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
- logmode = LOGMODE_NONE;
- }
- #if ENABLE_FEATURE_NTP_AUTH
- if (opts & OPT_k) {
- char *tokens[4];
- parser_t *parser;
- parser = config_open(key_file_path);
- while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL | PARSE_MIN_DIE) == 3) {
- key_entry_t *key_entry;
- char buffer[40];
- smalluint hash_type;
- smalluint msg_size;
- smalluint key_length;
- char *key;
- if ((tokens[1][0] | 0x20) == 'm')
-
- hash_type = HASH_MD5;
- else
- if (strncasecmp(tokens[1], "sha", 3) == 0)
-
- hash_type = HASH_SHA1;
- else
- bb_error_msg_and_die("only MD5 and SHA1 keys supported");
- key_length = strnlen(tokens[2], sizeof(buffer)+1);
- if (key_length >= sizeof(buffer)+1) {
- err:
- bb_error_msg_and_die("malformed key at line %u", parser->lineno);
- }
- if (hash_type == HASH_MD5) {
- key = tokens[2];
- msg_size = NTP_MSGSIZE_MD5_AUTH;
- } else
- if (!(key_length & 1)) {
- key_length >>= 1;
- if (!hex2bin(buffer, tokens[2], key_length))
- goto err;
- key = buffer;
- msg_size = NTP_MSGSIZE_SHA1_AUTH;
- } else {
- goto err;
- }
- key_entry = xzalloc(sizeof(*key_entry) + key_length);
- key_entry->type = hash_type;
- key_entry->msg_size = msg_size;
- key_entry->key_length = key_length;
- memcpy(key_entry->key, key, key_length);
- key_entry->id = xatou_range(tokens[0], 1, MAX_KEY_NUMBER);
- llist_add_to(&key_entries, key_entry);
- }
- config_close(parser);
- }
- #endif
- if (peers) {
- #if ENABLE_FEATURE_NTP_AUTH
- while (peers) {
- char *peer = llist_pop(&peers);
- key_entry_t *key_entry = NULL;
- if (strncmp(peer, "keyno:", 6) == 0) {
- char *end;
- int key_id;
- peer += 6;
- end = strchr(peer, ':');
- if (!end) bb_show_usage();
- *end = '\0';
- key_id = xatou_range(peer, 1, MAX_KEY_NUMBER);
- *end = ':';
- key_entry = find_key_entry(key_entries, key_id);
- peer = end + 1;
- }
- add_peers(peer, key_entry);
- }
- #else
- while (peers)
- add_peers(llist_pop(&peers), NULL);
- #endif
- }
- #if ENABLE_FEATURE_NTPD_CONF
- else {
- parser_t *parser;
- char *token[3 + 2*ENABLE_FEATURE_NTP_AUTH];
- parser = config_open("/etc/ntp.conf");
- while (config_read(parser, token, 3 + 2*ENABLE_FEATURE_NTP_AUTH, 1, "# \t", PARSE_NORMAL)) {
- if (strcmp(token[0], "server") == 0 && token[1]) {
- # if ENABLE_FEATURE_NTP_AUTH
- key_entry_t *key_entry = NULL;
- if (token[2] && token[3] && strcmp(token[2], "key") == 0) {
- unsigned key_id = xatou_range(token[3], 1, MAX_KEY_NUMBER);
- key_entry = find_key_entry(key_entries, key_id);
- }
- add_peers(token[1], key_entry);
- # else
- add_peers(token[1], NULL);
- # endif
- continue;
- }
- bb_error_msg("skipping %s:%u: unimplemented command '%s'",
- "/etc/ntp.conf", parser->lineno, token[0]
- );
- }
- config_close(parser);
- }
- #endif
- if (G.peer_cnt == 0) {
- if (!(opts & OPT_l))
- bb_show_usage();
-
- G.stratum = 1;
- }
-
- if (opts & OPT_q) {
- option_mask32 |= OPT_qq;
- alarm(10);
- }
- bb_signals(0
- | (1 << SIGTERM)
- | (1 << SIGINT)
- | (1 << SIGALRM)
- , record_signo
- );
- bb_signals(0
- | (1 << SIGPIPE)
- | (1 << SIGCHLD)
- , SIG_IGN
- );
- }
- int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int ntpd_main(int argc UNUSED_PARAM, char **argv)
- {
- #undef G
- struct globals G;
- struct pollfd *pfd;
- peer_t **idx2peer;
- unsigned cnt;
- memset(&G, 0, sizeof(G));
- SET_PTR_TO_GLOBALS(&G);
- ntp_init(argv);
-
- cnt = G.peer_cnt + ENABLE_FEATURE_NTPD_SERVER;
- idx2peer = xzalloc(sizeof(idx2peer[0]) * cnt);
- pfd = xzalloc(sizeof(pfd[0]) * cnt);
-
- cnt = G.peer_cnt * (INITIAL_SAMPLES + 1);
- write_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid");
- while (!bb_got_signal) {
- llist_t *item;
- unsigned i, j;
- int nfds, timeout;
- double nextaction;
-
- nextaction = G.last_script_run + (11*60);
- if (nextaction < G.cur_time + 1)
- nextaction = G.cur_time + 1;
- i = 0;
- #if ENABLE_FEATURE_NTPD_SERVER
- if (G_listen_fd != -1) {
- pfd[0].fd = G_listen_fd;
- pfd[0].events = POLLIN;
- i++;
- }
- #endif
-
- for (item = G.ntp_peers; item != NULL; item = item->link) {
- peer_t *p = (peer_t *) item->data;
- if (p->next_action_time <= G.cur_time) {
- if (p->p_fd == -1) {
-
- if (--cnt == 0) {
- VERB4 bb_error_msg("disabling burst mode");
- G.polladj_count = 0;
- G.poll_exp = MINPOLL;
- }
- send_query_to_peer(p);
- } else {
-
- close(p->p_fd);
- p->p_fd = -1;
-
- if (G.poll_exp < BIGPOLL)
- adjust_poll(MINPOLL);
- timeout = poll_interval(NOREPLY_INTERVAL);
- bb_error_msg("timed out waiting for %s, reach 0x%02x, next query in %us",
- p->p_dotted, p->reachable_bits, timeout);
-
- if (p->reachable_bits == 0)
- resolve_peer_hostname(p);
- set_next(p, timeout);
- }
- }
- if (p->next_action_time < nextaction)
- nextaction = p->next_action_time;
- if (p->p_fd >= 0) {
-
- pfd[i].fd = p->p_fd;
- pfd[i].events = POLLIN;
- idx2peer[i] = p;
- i++;
- }
- }
- timeout = nextaction - G.cur_time;
- if (timeout < 0)
- timeout = 0;
- timeout++;
-
- VERB2 {
- if (i > (ENABLE_FEATURE_NTPD_SERVER && G_listen_fd != -1)) {
-
- nfds = poll(pfd, i, 1000);
- if (nfds != 0)
- goto did_poll;
- if (--timeout <= 0)
- goto did_poll;
- }
- bb_error_msg("poll:%us sockets:%u interval:%us", timeout, i, 1 << G.poll_exp);
- }
- nfds = poll(pfd, i, timeout * 1000);
- did_poll:
- gettime1900d();
- if (nfds <= 0) {
- double ct;
- int dns_error;
- if (bb_got_signal)
- break;
- if (G.cur_time - G.last_script_run > 11*60) {
-
- run_script("periodic", G.last_update_offset);
- gettime1900d();
- }
-
- dns_error = 0;
- ct = G.cur_time;
- for (item = G.ntp_peers; item != NULL; item = item->link) {
- peer_t *p = (peer_t *) item->data;
- if (p->next_action_time <= ct && !p->p_lsa) {
-
- dns_error |= (!resolve_peer_hostname(p));
- }
- }
- if (!dns_error)
- goto check_unsync;
-
- gettime1900d();
- for (item = G.ntp_peers; item != NULL; item = item->link) {
- peer_t *p = (peer_t *) item->data;
- if (p->next_action_time <= ct && !p->p_lsa) {
- set_next(p, HOSTNAME_INTERVAL * p->dns_errors);
- }
- }
- goto check_unsync;
- }
-
- j = 0;
- #if ENABLE_FEATURE_NTPD_SERVER
- if (G.listen_fd != -1) {
- if (pfd[0].revents ) {
- nfds--;
- recv_and_process_client_pkt();
- gettime1900d();
- }
- j = 1;
- }
- #endif
- for (; nfds != 0 && j < i; j++) {
- if (pfd[j].revents ) {
-
- if (option_mask32 & OPT_qq) {
- option_mask32 &= ~OPT_qq;
- alarm(50);
- }
- nfds--;
- recv_and_process_peer_pkt(idx2peer[j]);
- gettime1900d();
- }
- }
- check_unsync:
- if (G.ntp_peers && G.stratum != MAXSTRAT) {
- for (item = G.ntp_peers; item != NULL; item = item->link) {
- peer_t *p = (peer_t *) item->data;
- if (p->reachable_bits)
- goto have_reachable_peer;
- }
-
- clamp_pollexp_and_set_MAXSTRAT();
- run_script("unsync", 0.0);
- have_reachable_peer: ;
- }
- }
- remove_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid");
- kill_myself_with_sig(bb_got_signal);
- }
- #if 0
- static double
- direct_freq(double fp_offset)
- {
- #ifdef KERNEL_PLL
-
- if (pll_control && kern_enable) {
- memset(&ntv, 0, sizeof(ntv));
- ntp_adjtime(&ntv);
- #ifdef STA_NANO
- clock_offset = ntv.offset / 1e9;
- #else
- clock_offset = ntv.offset / 1e6;
- #endif
- drift_comp = FREQTOD(ntv.freq);
- }
- #endif
- set_freq((fp_offset - clock_offset) / (current_time - clock_epoch) + drift_comp);
- wander_resid = 0;
- return drift_comp;
- }
- static void
- set_freq(double freq)
- {
- char tbuf[80];
- drift_comp = freq;
- #ifdef KERNEL_PLL
-
- if (pll_control && kern_enable) {
- memset(&ntv, 0, sizeof(ntv));
- ntv.modes = MOD_FREQUENCY;
- ntv.freq = DTOFREQ(drift_comp);
- ntp_adjtime(&ntv);
- snprintf(tbuf, sizeof(tbuf), "kernel %.3f PPM", drift_comp * 1e6);
- report_event(EVNT_FSET, NULL, tbuf);
- } else {
- snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6);
- report_event(EVNT_FSET, NULL, tbuf);
- }
- #else
- snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6);
- report_event(EVNT_FSET, NULL, tbuf);
- #endif
- }
- ...
- ...
- ...
- #ifdef KERNEL_PLL
-
- if (pll_control && kern_enable) {
- #define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | MOD_STATUS | MOD_TIMECONST)
-
- memset(&ntv, 0, sizeof(ntv));
- if (ext_enable) {
- ntv.modes = MOD_STATUS;
- } else {
- #ifdef STA_NANO
- ntv.modes = MOD_BITS | MOD_NANO;
- #else
- ntv.modes = MOD_BITS;
- #endif
- if (clock_offset < 0)
- dtemp = -.5;
- else
- dtemp = .5;
- #ifdef STA_NANO
- ntv.offset = (int32)(clock_offset * 1e9 + dtemp);
- ntv.constant = sys_poll;
- #else
- ntv.offset = (int32)(clock_offset * 1e6 + dtemp);
- ntv.constant = sys_poll - 4;
- #endif
- ntv.esterror = (u_int32)(clock_jitter * 1e6);
- ntv.maxerror = (u_int32)((sys_rootdelay / 2 + sys_rootdisp) * 1e6);
- ntv.status = STA_PLL;
-
- if (pps_enable) {
- if (!(pll_status & STA_PPSTIME))
- report_event(EVNT_KERN,
- NULL, "PPS enabled");
- ntv.status |= STA_PPSTIME | STA_PPSFREQ;
- } else {
- if (pll_status & STA_PPSTIME)
- report_event(EVNT_KERN,
- NULL, "PPS disabled");
- ntv.status &= ~(STA_PPSTIME | STA_PPSFREQ);
- }
- if (sys_leap == LEAP_ADDSECOND)
- ntv.status |= STA_INS;
- else if (sys_leap == LEAP_DELSECOND)
- ntv.status |= STA_DEL;
- }
-
- if (ntp_adjtime(&ntv) == TIME_ERROR) {
- if (!(ntv.status & STA_PPSSIGNAL))
- report_event(EVNT_KERN, NULL,
- "PPS no signal");
- }
- pll_status = ntv.status;
- #ifdef STA_NANO
- clock_offset = ntv.offset / 1e9;
- #else
- clock_offset = ntv.offset / 1e6;
- #endif
- clock_frequency = FREQTOD(ntv.freq);
-
- if (ntv.status & STA_PPSTIME) {
- #ifdef STA_NANO
- clock_jitter = ntv.jitter / 1e9;
- #else
- clock_jitter = ntv.jitter / 1e6;
- #endif
- }
- #if defined(STA_NANO) && NTP_API == 4
-
- if (loop_tai != sys_tai) {
- loop_tai = sys_tai;
- ntv.modes = MOD_TAI;
- ntv.constant = sys_tai;
- ntp_adjtime(&ntv);
- }
- #endif
- }
- #endif
- #endif
|