2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Revision 2.3 90/09/20 13:14:18
12 * Open aperture for AIX3.1 to 512 msec, since settimeofday doesn't get
14 * On both AIX systems use "signed char" for precision.
16 * Revision 2.2 90/09/19 16:39:28
17 * Include <afs/param.h> so sys_name specific conditionals work.
18 * On hp set clock phase to 5.
19 * On sun set clock max to .5 sec.
20 * Define a type called s_char which is really a signed character. Use this
21 * in both packet structures.
23 * Revision 2.1 90/08/07 19:22:47
24 * Start with clean version to sync test and dev trees.
26 * Revision 1.11 90/05/24 16:18:08
27 * Changed CLOCK_PHASE for AIX from 8 to 5.
29 * Revision 1.10 90/05/21 13:50:30
30 * Set CLOCK_MAX to 1.167 (one second plus one tick) for AIX, since it doesn't do
33 * Revision 1.9 89/05/24 12:25:13
34 * Latest May 18, Version 4.3 release from UMD.
36 * Revision 3.4.1.7 89/05/18 18:22:14
37 * A few extra diddles in ntp.h for the reference clock feature.
39 * Revision 3.4.1.6 89/05/03 15:11:06
40 * Specify default file for drift value and more peer flag definitions to
41 * reflect various stages of clock selection critera.
43 * Revision 3.4.1.5 89/04/10 15:55:42
44 * Provide default value for number of bits/byte if not defined. Compute the
45 * Window shift mask inside of conditional code on XTAL so we get the correct
46 * value if configured without a crystal controled clock (!!)
48 * Revision 3.4.1.4 89/03/31 16:34:50
49 * Add bit in flags which allow a peer to be synced to. Changed a char to a bit
50 * field so that it is always signed.
52 * Revision 3.4.1.3 89/03/29 12:26:18
53 * Removed some unused #defines. Replaced MAXSTRATUM with NTP_INFIN per new
54 * spec. The variable 'mode' in the peer structure has been renamed 'hmode'
57 * Revision 3.4.1.2 89/03/22 18:28:18
58 * patch3: Use new RCS headers.
60 * Revision 3.4.1.1 89/03/20 00:02:53
63 * Revision 3.4 89/03/17 18:37:00
64 * Latest test release.
66 * Revision 3.3.1.1 89/03/17 18:23:49
67 * Change CLOCK_FACTOR to be a power of 2.
69 * Revision 3.3 89/03/15 14:19:36
70 * New baseline for next release.
72 * Revision 3.2.1.2 89/03/15 13:46:52
73 * The version number for that particular flavor of ntpd <--> ntpdc interaction
74 * is now defined by NTPDC_VERSION. The packet format for the ntpdc program
75 * has changed slightly to improve robustness when dealing with multiple packets
78 * Revision 3.2.1.1 89/03/09 17:11:24
79 * patch1: Updated constants, which were previously in incorrect units.
81 * Revision 3.2 89/03/07 18:21:45
82 * New version of UNIX NTP daemon and software based on the 6 March 1989
83 * draft of the new NTP protocol specification. This version doesn't
84 * implement authentication, and accepts and send only NTP Version 1
87 * Revision 3.1.1.1 89/02/15 08:54:42
88 * *** empty log message ***
91 * Revision 3.1 89/01/30 14:43:07
92 * Second UNIX NTP test release.
94 * Revision 3.0 88/12/12 16:01:07
95 * Test release of new UNIX NTP software. This version should conform to the
96 * revised NTP protocol specification.
100 #include <afs/param.h> /* include this, just to make sure */
104 #define FD_SETSIZE 32
105 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
106 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
107 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
108 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
112 #define NBBY 8 /* number of bits per byte */
120 struct sockaddr_in sin;
121 struct sockaddr_in bcast;
122 struct sockaddr_in mask;
126 extern struct intf addrs[];
130 * Definitions for the masses
132 #define JAN_1970 2208988800 /* 1970 - 1900 in seconds */
135 * Daemon specific (ntpd.c)
137 #define SHIFT_MASK 0xff /* number of intervals to wait */
140 #define WAYTOOBIG 1000.0 /* Too many seconds to correct, something is
145 #define XTAL 1 /* crystal controlled clock by default */
149 #define NTPINITFILE "/etc/ntp.conf"
152 #define NTPDRIFTCOMP "/etc/ntp.drift"
156 struct ntp_peer *head;
157 struct ntp_peer *tail;
161 #define STRMCMP(a, cond, b) \
162 (((a) == UNSPECIFIED ? NTP_INFIN+1 : a) cond \
163 ((b) == UNSPECIFIED ? NTP_INFIN+1 : (b)))
167 * Definitions outlined in the NTP spec
169 #define NTP_VERSION 1
170 #define NTP_PORT 123 /* for ref only (see /etc/services) */
172 #define NTP_MAXAGE 86400
173 #define NTP_MAXSKW 0.01 /* seconds */
174 #define NTP_MINDIST 0.02 /* seconds */
176 #define NTP_REFMAXSKW 0.001 /* seconds (for REFCLOCKs) */
177 #define NTP_REFMINDIST 0.001 /* seconds (for REFCLOCKs) */
179 #define NTP_MINPOLL 6 /* (64) seconds between messages */
180 #define NTP_MAXPOLL 10 /* (1024) secs to poll */
181 #define NTP_WINDOW 8 /* size of shift register */
182 #define NTP_MAXWGT 8 /* maximum allowable dispersion */
183 #define NTP_MAXLIST 5 /* max size of selection list */
184 #define NTP_MAXSTRA 2 /* max number of strata in selection list */
185 #define X_NTP_CANDIDATES 64 /* number of peers to consider when doing
187 #define NTP_SELECT 0.75 /* weight used to compute dispersion */
189 #define PEER_MAXDISP 64.0 /* Maximum dispersion */
190 #define PEER_THRESHOLD 0.5 /* dispersion threshold */
191 #define PEER_FILTER 0.5 /* filter weight */
195 #define NTP_WINDOW_SHIFT_MASK 0x0f
198 #define NTP_WINDOW_SHIFT_MASK 0xff
203 * 5.1 Uniform Phase Adjustments
206 #define CLOCK_UPDATE 8 /* update interval (1<<CLOCK_UPDATE secs) */
208 #define CLOCK_ADJ 2 /* adjustment interval (1<<CLOCK_ADJ secs) */
210 #if defined (hpux) /* must use settimeofday instead of adjtime */
211 #define CLOCK_PHASE 5 /* send bigger chunks */
212 #define CLOCK_MAX 0.128 /* maximum aperture (milliseconds) */
216 #if defined (AFS_SUN_ENV) /* these guys have such terrible clocks... */
217 #define CLOCK_PHASE 8 /* phase shift */
218 #define CLOCK_MAX 0.512 /* maximum aperture (milliseconds) */
222 #if defined (AFS_AIX32_ENV) /* there is a bug in adjtime */
223 #define CLOCK_PHASE 8 /* phase shift */
224 #define CLOCK_MAX 0.512 /* maximum aperture (milliseconds) */
228 #define CLOCK_PHASE 8 /* phase shift */
229 #define CLOCK_MAX 0.128 /* maximum aperture (milliseconds) */
236 #define CLOCK_PHASE 6 /* phase shift */
237 #define CLOCK_MAX 0.512 /* maximum aperture (milliseconds) */
239 #define CLOCK_FREQ 10 /* frequency shift */
240 #define CLOCK_TRACK 8
242 #define CLOCK_FACTOR 18
245 * Structure definitions for NTP fixed point values
248 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
249 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
251 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
253 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
256 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
257 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
258 * | Integer Part | Fraction Part |
259 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
271 /* sign extension problem */
272 #if defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
273 #define s_char(v) char v
275 #if defined(AFS_HPUX_ENV) || defined(AFS_AIX_ENV)
276 #define s_char(v) signed char v
278 #define s_char(v) int v:8
282 /* ================= Table 3.3. Packet Variables ================= */
285 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
286 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
287 * |LI | VN | Mode| Stratum | Poll | Precision |
288 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
289 * | Synchronizing Distance |
290 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
291 * | Synchronizing Dispersion |
292 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
293 * | Reference Clock Identifier |
294 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
296 * | Reference Timestamp (64 bits) |
298 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
300 * | Originate Timestamp (64 bits) |
302 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
304 * | Receive Timestamp (64 bits) |
306 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
308 * | Transmit Timestamp (64 bits) |
310 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
313 u_char status; /* status of local clock and leap info */
314 u_char stratum; /* Stratum level */
315 u_char ppoll; /* poll value */
316 s_char(precision); /* -6..-31 */
318 struct s_fixedpt distance;
319 struct s_fixedpt dispersion;
321 struct l_fixedpt reftime;
322 struct l_fixedpt org;
323 struct l_fixedpt rec;
324 struct l_fixedpt xmt;
327 * Leap Second Codes (high order two bits)
329 #define NO_WARNING 0x00 /* no warning */
330 #define PLUS_SEC 0x40 /* add a second (61 seconds) */
331 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */
332 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */
339 * Clock Status Bits that Encode Version
341 #define NTPVERSION_1 0x08
342 #define VERSIONMASK 0x38
343 #define LEAPMASK 0xc0
344 #define MODEMASK 0x07
349 #define MODE_UNSPEC 0 /* unspecified */
350 #define MODE_SYM_ACT 1 /* symmetric active */
351 #define MODE_SYM_PAS 2 /* symmetric passive */
352 #define MODE_CLIENT 3 /* client */
353 #define MODE_SERVER 4 /* server */
354 #define MODE_BROADCAST 5 /* broadcast */
355 #define MODE_RES1 6 /* reserved */
356 #define MODE_RES2 7 /* reserved */
359 * Stratum Definitions
361 #define UNSPECIFIED 0
362 #define PRIM_REF 1 /* radio clock */
363 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */
364 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */
367 /* ================= table 3.2 Peer Variables ================= */
369 struct ntp_peer *next, *prev;
370 struct sockaddr_in src; /* both peer.srcadr and
372 int flags; /* local flags */
373 #define PEER_FL_CONFIG 1
374 #define PEER_FL_AUTHENABLE 2
375 #define PEER_FL_SANE 0x0100 /* sane peer */
376 #define PEER_FL_CANDIDATE 0x0200 /* candidate peer */
377 #define PEER_FL_SYNC 0x1000 /* peer can bet sync'd to */
378 #define PEER_FL_BCAST 0x2000 /* broadcast peer */
379 #define PEER_FL_REFCLOCK 0x4000 /* peer is a local reference clock */
380 #define PEER_FL_SELECTED 0x8000 /* actually used by query routine */
382 int sock; /* index into sockets to derive
383 peer.dstadr and peer.dstport */
384 u_char leap; /* receive */
385 u_char hmode; /* receive */
386 u_char stratum; /* receive */
387 u_char ppoll; /* receive */
388 u_char hpoll; /* poll update */
389 short precision; /* receive */
390 struct s_fixedpt distance; /* receive */
391 struct s_fixedpt dispersion; /* receive */
392 afs_uint32 refid; /* receive */
393 struct l_fixedpt reftime; /* receive */
394 struct l_fixedpt org; /* receive, clear */
395 struct l_fixedpt rec; /* receive, clear */
396 struct l_fixedpt xmt; /* transmit, clear */
397 afs_uint32 reach; /* receive, transmit, clear */
398 afs_uint32 valid; /* packet, transmit, clear */
399 afs_uint32 timer; /* receive, transmit, poll update */
400 afs_int32 stopwatch; /* <<local>> for timing */
402 * first order offsets
405 short samples; /* <<local>> */
406 double offset[PEER_SHIFT];
407 double delay[PEER_SHIFT];
408 } filter; /* filter, clear */
410 double estdelay; /* filter */
411 double estoffset; /* filter */
412 double estdisp; /* filter */
414 afs_uint32 pkt_sent; /* <<local>> */
415 afs_uint32 pkt_rcvd; /* <<local>> */
416 afs_uint32 pkt_dropped; /* <<local>> */
419 /* ================= table 3.1: System Variables ================= */
421 struct sysdata { /* procedure */
422 u_char leap; /* clock update */
423 u_char stratum; /* clock update */
424 short precision; /* system */
425 struct s_fixedpt distance; /* clock update */
426 struct s_fixedpt dispersion; /* clock update */
427 afs_uint32 refid; /* clock update */
428 struct l_fixedpt reftime; /* clock update */
429 int hold; /* clock update */
430 struct ntp_peer *peer; /* selection */
431 int maxpeers; /* <<local>> */
432 u_char filler; /* put here for %&*%$$ SUNs */
435 #define NTPDC_VERSION 2
438 * These structures are used to pass information to the ntpdc (control)
439 * program. They are unique to this implementation and not part of the
443 afs_uint32 net_address;
444 afs_uint32 my_address;
449 afs_uint32 pkt_dropped;
460 afs_int32 estdisp; /* scaled by 1000 */
461 afs_int32 estdelay; /* in milliseconds */
462 afs_int32 estoffset; /* in milliseconds */
464 struct l_fixedpt reftime;
468 afs_int32 offset[PEER_SHIFT]; /* in milliseconds */
469 afs_int32 delay[PEER_SHIFT]; /* in milliseconds */
475 u_char type; /* request type (stratum in ntp packets) */
476 u_char count; /* number of entries in this packet */
477 u_char seq; /* sequence number of this packet */
479 u_char npkts; /* total number of packets */