Standardize License information
[openafs.git] / src / ntp / ntp.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Revision 2.3  90/09/20  13:14:18
12  * Open aperture for AIX3.1 to 512 msec, since settimeofday doesn't get
13  *   it very close.
14  * On both AIX systems use "signed char" for precision.
15  * 
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.
22  * 
23  * Revision 2.1  90/08/07  19:22:47
24  * Start with clean version to sync test and dev trees.
25  * 
26  * Revision 1.11  90/05/24  16:18:08
27  * Changed CLOCK_PHASE for AIX from 8 to 5.
28  * 
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
31  *   too well.
32  * 
33  * Revision 1.9  89/05/24  12:25:13
34  * Latest May 18, Version 4.3 release from UMD.
35  * 
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.
38  * 
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.
42  * 
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 (!!)
47  * 
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.
51  * 
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'
55  * per the new spec.
56  * 
57  * Revision 3.4.1.2  89/03/22  18:28:18
58  * patch3: Use new RCS headers.
59  * 
60  * Revision 3.4.1.1  89/03/20  00:02:53
61  * 1
62  * 
63  * Revision 3.4  89/03/17  18:37:00
64  * Latest test release.
65  * 
66  * Revision 3.3.1.1  89/03/17  18:23:49
67  * Change CLOCK_FACTOR to be a power of 2.
68  * 
69  * Revision 3.3  89/03/15  14:19:36
70  * New baseline for next release.
71  * 
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
76  * of status data.
77  * 
78  * Revision 3.2.1.1  89/03/09  17:11:24
79  * patch1: Updated constants, which were previously in incorrect units.
80  * 
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
85  * packets.
86  * 
87  * Revision 3.1.1.1  89/02/15  08:54:42
88  * *** empty log message ***
89  * 
90  *
91  * Revision 3.1  89/01/30  14:43:07
92  * Second UNIX NTP test release.
93  * 
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.
97  * 
98  */
99
100 #include <afs/param.h>                  /* include this, just to make sure */
101
102 #ifndef FD_SET
103 #define NFDBITS         32
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)))
109 #endif
110
111 #ifndef NBBY
112 #define NBBY    8       /* number of bits per byte */
113 #endif
114
115 #define MAXNETIF        10
116
117 struct intf {
118         int fd;
119         char *name;
120         struct sockaddr_in sin;
121         struct sockaddr_in bcast;
122         struct sockaddr_in mask;
123         int uses;
124         int if_flags;
125 };
126 extern struct intf addrs[];
127 extern int nintf;
128
129 /*
130  *  Definitions for the masses
131  */
132 #define JAN_1970        2208988800      /* 1970 - 1900 in seconds */
133
134 /*
135  *  Daemon specific (ntpd.c)
136  */
137 #define SHIFT_MASK      0xff    /* number of intervals to wait */
138
139 #ifndef WAYTOOBIG
140 #define WAYTOOBIG       1000.0  /* Too many seconds to correct, something is
141                                  * really wrong */
142 #endif
143
144 #ifndef XTAL
145 #define XTAL    1       /* crystal controlled clock by default */
146 #endif
147
148 #ifndef NTPINITFILE
149 #define NTPINITFILE     "/etc/ntp.conf"
150 #endif
151 #ifndef NTPDRIFTCOMP
152 #define NTPDRIFTCOMP    "/etc/ntp.drift"
153 #endif
154
155 struct list {
156         struct ntp_peer *head;
157         struct ntp_peer *tail;
158         int members;
159 };
160
161 #define STRMCMP(a, cond, b) \
162         (((a) == UNSPECIFIED ? NTP_INFIN+1 : a) cond \
163          ((b) == UNSPECIFIED ? NTP_INFIN+1 : (b)))
164
165
166 /*
167  *  Definitions outlined in the NTP spec
168  */
169 #define NTP_VERSION     1
170 #define NTP_PORT        123     /* for ref only (see /etc/services) */
171 #define NTP_INFIN       15
172 #define NTP_MAXAGE      86400
173 #define NTP_MAXSKW      0.01    /* seconds */
174 #define NTP_MINDIST     0.02    /* seconds */
175 #ifdef  REFCLOCK
176 #define NTP_REFMAXSKW   0.001   /* seconds (for REFCLOCKs) */
177 #define NTP_REFMINDIST  0.001   /* seconds (for REFCLOCKs) */
178 #endif
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
186                                    clock selection */
187 #define NTP_SELECT      0.75    /* weight used to compute dispersion */
188
189 #define PEER_MAXDISP    64.0    /* Maximum dispersion  */
190 #define PEER_THRESHOLD  0.5     /* dispersion threshold */
191 #define PEER_FILTER     0.5     /* filter weight */
192
193 #if     XTAL == 0
194 #define PEER_SHIFT      4
195 #define NTP_WINDOW_SHIFT_MASK 0x0f
196 #else
197 #define PEER_SHIFT      8
198 #define NTP_WINDOW_SHIFT_MASK 0xff
199 #endif
200
201
202 /*
203  *  5.1 Uniform Phase Adjustments
204  *  Clock parameters
205  */
206 #define CLOCK_UPDATE    8       /* update interval (1<<CLOCK_UPDATE secs) */
207 #if     XTAL
208 #define CLOCK_ADJ       2       /* adjustment interval (1<<CLOCK_ADJ secs) */
209
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) */
213
214 #else
215
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) */
219
220 #else
221
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) */
225
226 #else
227
228 #define CLOCK_PHASE     8       /* phase shift */
229 #define CLOCK_MAX       0.128   /* maximum aperture (milliseconds) */
230 #endif
231 #endif
232 #endif
233
234 #else /*!XTAL*/
235 #define CLOCK_ADJ       0
236 #define CLOCK_PHASE     6       /* phase shift */
237 #define CLOCK_MAX       0.512   /* maximum aperture (milliseconds) */
238 #endif
239 #define CLOCK_FREQ      10      /* frequency shift */
240 #define CLOCK_TRACK     8
241 #define CLOCK_COMP      4
242 #define CLOCK_FACTOR    18
243 \f
244 /*
245  * Structure definitions for NTP fixed point values
246  *
247  *    0                   1                   2                   3
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  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
250  *   |                         Integer Part                          |
251  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
252  *   |                         Fraction Part                         |
253  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
254  *
255  *    0                   1                   2                   3
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  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
260 */
261 struct l_fixedpt {
262         afs_uint32 int_part;
263         afs_uint32 fraction;
264 };
265
266 struct s_fixedpt {
267         u_short int_part;
268         u_short fraction;
269 };
270
271 /* sign extension problem */
272 #if defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
273 #define s_char(v) char v
274 #else
275 #if defined(AFS_HPUX_ENV) || defined(AFS_AIX_ENV)
276 #define s_char(v) signed char v
277 #else
278 #define s_char(v) int v:8
279 #endif
280 #endif
281
282 /*  =================  Table 3.3. Packet Variables   ================= */
283 /*
284  *    0                   1                   2                   3
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  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
295  *   |                                                               |
296  *   |                 Reference Timestamp (64 bits)                 |
297  *   |                                                               |
298  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
299  *   |                                                               |
300  *   |                 Originate Timestamp (64 bits)                 |
301  *   |                                                               |
302  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
303  *   |                                                               |
304  *   |                  Receive Timestamp (64 bits)                  |
305  *   |                                                               |
306  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
307  *   |                                                               |
308  *   |                  Transmit Timestamp (64 bits)                 |
309  *   |                                                               |
310  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 */
312 struct ntpdata {
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 */
317
318         struct s_fixedpt distance;
319         struct s_fixedpt dispersion;
320         afs_uint32 refid;
321         struct l_fixedpt reftime;
322         struct l_fixedpt org;
323         struct l_fixedpt rec;
324         struct l_fixedpt xmt;
325 };
326 /*
327  *      Leap Second Codes (high order two bits)
328  */
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) */
333
334 #ifdef MODEMASK
335 #undef MODEMASK
336 #endif
337
338 /*
339  *      Clock Status Bits that Encode Version
340  */
341 #define NTPVERSION_1    0x08
342 #define VERSIONMASK     0x38
343 #define LEAPMASK        0xc0
344 #define MODEMASK        0x07
345
346 /*
347  *      Code values
348  */
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 */
357
358 /*
359  *      Stratum Definitions
360  */
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 **** */
365
366
367 /* =================  table 3.2 Peer Variables  ================= */
368 struct ntp_peer {
369         struct ntp_peer *next, *prev;
370         struct sockaddr_in src;         /* both peer.srcadr and 
371                                            peer.srcport */
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 */
381
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 */
401         /*
402          * first order offsets
403          */
404         struct  filter {
405                 short samples;          /* <<local>> */
406                 double offset[PEER_SHIFT];
407                 double delay[PEER_SHIFT];
408         } filter;                       /* filter, clear */
409
410         double  estdelay;               /* filter */
411         double  estoffset;              /* filter */
412         double  estdisp;                /* filter */
413
414         afs_uint32      pkt_sent;               /* <<local>> */
415         afs_uint32      pkt_rcvd;               /* <<local>> */
416         afs_uint32      pkt_dropped;            /* <<local>> */
417 };
418
419 /* ================= table 3.1:  System Variables ================= */
420
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 */
433 };
434 \f
435 #define NTPDC_VERSION   2
436
437 /*
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
440  *  NTP specification.
441  */
442 struct clockinfo {
443         afs_uint32 net_address;
444         afs_uint32 my_address;
445         u_short port;
446         u_short flags;
447         afs_uint32 pkt_sent;
448         afs_uint32 pkt_rcvd;
449         afs_uint32 pkt_dropped;
450         afs_uint32 timer;
451         u_char leap;
452         u_char stratum;
453         u_char ppoll;
454         s_char(precision);
455
456         u_char hpoll;
457         u_char filler1;
458         u_short reach;
459
460         afs_int32       estdisp;                        /* scaled by 1000 */
461         afs_int32       estdelay;                       /* in milliseconds */
462         afs_int32       estoffset;                      /* in milliseconds */
463         afs_uint32 refid;
464         struct l_fixedpt reftime;
465         struct info_filter {
466                 short index;
467                 short filler;
468                 afs_int32 offset[PEER_SHIFT];   /* in milliseconds */
469                 afs_int32 delay[PEER_SHIFT];    /* in milliseconds */
470         } info_filter;
471 };
472
473 struct ntpinfo {
474         u_char version;
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 */
478
479         u_char npkts;           /* total number of packets */
480         u_char peers;
481         u_char fill3;
482         u_char fill4;
483 };
484
485