xref: /titanic_52/usr/src/cmd/cmd-inet/usr.sbin/snoop/ntp.h (revision 6185db853e024a486ff8837e6784dd290d866112)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * University Copyright- Copyright (c) 1982, 1986, 1988
29  * The Regents of the University of California
30  * All Rights Reserved
31  *
32  * University Acknowledgment- Portions of this document are derived from
33  * software developed by the University of California, Berkeley, and its
34  * contributors.
35  */
36 
37 #ifndef	_NTP_H
38 #define	_NTP_H
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* $Source: /usr/users/louie/ntp/RCS/ntp.h,v $  */
47 /* $Revision: 3.4.1.5 $ $Date: 89/04/10 15:55:42 $ */
48 
49 /*
50  *  $Log:	ntp.h,v $
51  * Revision 3.4.1.5  89/04/10  15:55:42  louie
52  * Provide default value for number of bits/byte if not defined.  Compute the
53  * Window shift mask inside of conditional code on XTAL so we get the correct
54  * value if configured without a crystal controled clock (!!)
55  *
56  * Revision 3.4.1.4  89/03/31  16:34:50  louie
57  * Add bit in flags which allow a peer to be synced to.  Changed a char to a bit
58  * field so that it is always signed.
59  *
60  * Revision 3.4.1.3  89/03/29  12:26:18  louie
61  * Removed some unused #defines.  Replaced MAXSTRATUM with NTP_INFIN per new
62  * spec.  The variable 'mode' in the peer structure has been renamed 'hmode'
63  * per the new spec.
64  *
65  * Revision 3.4.1.2  89/03/22  18:28:18  louie
66  * patch3: Use new RCS headers.
67  *
68  * Revision 3.4.1.1  89/03/20  00:02:53  louie
69  * 1
70  *
71  * Revision 3.4  89/03/17  18:37:00  louie
72  * Latest test release.
73  *
74  * Revision 3.3.1.1  89/03/17  18:23:49  louie
75  * Change CLOCK_FACTOR to be a power of 2.
76  *
77  * Revision 3.3  89/03/15  14:19:36  louie
78  * New baseline for next release.
79  *
80  * Revision 3.2.1.2  89/03/15  13:46:52  louie
81  * The version number for that particular flavor of ntpd <--> ntpdc interaction
82  * is now defined by NTPDC_VERSION.  The packet format for the ntpdc program
83  * has changed slightly to improve robustness when dealing with multiple packets
84  * of status data.
85  *
86  * Revision 3.2.1.1  89/03/09  17:11:24  louie
87  * patch1: Updated constants, which were previously in incorrect units.
88  *
89  * Revision 3.2  89/03/07  18:21:45  louie
90  * New version of UNIX NTP daemon and software based on the 6 March 1989
91  * draft of the new NTP protocol specification.  This version doesn't
92  * implement authentication, and accepts and send only NTP Version 1
93  * packets.
94  *
95  * Revision 3.1.1.1  89/02/15  08:54:42  louie
96  * *** empty log message ***
97  *
98  *
99  * Revision 3.1  89/01/30  14:43:07  louie
100  * Second UNIX NTP test release.
101  *
102  * Revision 3.0  88/12/12  16:01:07  louie
103  * Test release of new UNIX NTP software.  This version should conform to the
104  * revised NTP protocol specification.
105  *
106  */
107 
108 #ifndef FD_SET
109 #define	NFDBITS		32
110 #define	FD_SETSIZE	32
111 #define	FD_SET(n, p)	((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
112 #define	FD_CLR(n, p)	((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
113 #define	FD_ISSET(n, p)	((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
114 #define	FD_ZERO(p)	bzero((char *)(p), sizeof (*(p)))
115 #endif
116 
117 #ifndef	NBBY
118 #define	NBBY	8	/* number of bits per byte */
119 #endif
120 
121 #define	MAXNETIF	10
122 
123 struct intf {
124 	int fd;
125 	char *name;
126 	struct sockaddr_in sin;
127 	struct sockaddr_in bcast;
128 	struct sockaddr_in mask;
129 	int uses;
130 	int if_flags;
131 };
132 extern struct intf addrs[];
133 extern int nintf;
134 
135 /*
136  *  Definitions for the masses
137  */
138 #define	JAN_1970	2208988800U	/* 1970 - 1900 in seconds */
139 
140 /*
141  *  Daemon specific (ntpd.c)
142  */
143 #define	SHIFT_MASK	0xff	/* number of intervals to wait */
144 
145 #ifndef	WAYTOOBIG
146 #define	WAYTOOBIG	1000.0	/* Too many seconds to correct, something is */
147 				/* really wrong */
148 #endif
149 
150 #ifndef	XTAL
151 #define	XTAL	1	/* crystal controlled clock by default */
152 #endif
153 
154 #ifndef	NTPINITFILE
155 #define	NTPINITFILE	"/etc/ntp.conf"
156 #endif
157 
158 struct list {
159 	struct ntp_peer *head;
160 	struct ntp_peer *tail;
161 	int members;
162 };
163 
164 #define	STRMCMP(a, cond, b) \
165 	(((a) == UNSPECIFIED ? NTP_INFIN+1 : a) cond \
166 		((b) == UNSPECIFIED ? NTP_INFIN+1 : (b)))
167 
168 
169 /*
170  *  Definitions outlined in the NTP spec
171  */
172 #define	NTP_VERSION	1
173 #define	NTP_PORT	123	/* for ref only (see /etc/services) */
174 #define	NTP_INFIN	15
175 #define	NTP_MAXAGE	86400
176 #define	NTP_MAXSKW	0.01	/* seconds */
177 #define	NTP_MINDIST	0.02	/* seconds */
178 #define	NTP_MINPOLL	6	/* (64) seconds between messages */
179 #define	NTP_MAXPOLL	10	/* (1024) secs to poll */
180 #define	NTP_WINDOW	8	/* size of shift register */
181 #define	NTP_MAXWGT	8	/* maximum allowable dispersion */
182 #define	NTP_MAXLIST	5	/* max size of selection list */
183 #define	NTP_MAXSTRA	2	/* max number of strata in selection list */
184 #define	X_NTP_CANDIDATES 64	/* number of peers to consider when doing */
185 				/*	clock selection */
186 #define	NTP_SELECT	0.75	/* weight used to compute dispersion */
187 
188 #define	PEER_MAXDISP	64.0	/* Maximum dispersion  */
189 #define	PEER_THRESHOLD	0.5	/* dispersion threshold */
190 #define	PEER_FILTER	0.5	/* filter weight */
191 
192 #if	XTAL == 0
193 #define	PEER_SHIFT	4
194 #define	NTP_WINDOW_SHIFT_MASK 0x0f
195 #else
196 #define	PEER_SHIFT	8
197 #define	NTP_WINDOW_SHIFT_MASK 0xff
198 #endif
199 
200 
201 /*
202  *  5.1 Uniform Phase Adjustments
203  *  Clock parameters
204  */
205 #define	CLOCK_UPDATE	8	/* update interval (1<<CLOCK_UPDATE secs) */
206 #if	XTAL
207 #define	CLOCK_ADJ	2	/* adjustment interval (1<<CLOCK_ADJ secs) */
208 #define	CLOCK_PHASE	8	/* phase shift */
209 #define	CLOCK_MAX	0.128	/* maximum aperture (milliseconds) */
210 #else
211 #define	CLOCK_ADJ	0
212 #define	CLOCK_PHASE	6	/* phase shift */
213 #define	CLOCK_MAX	0.512	/* maximum aperture (milliseconds) */
214 #endif
215 #define	CLOCK_FREQ	10	/* frequency shift */
216 #define	CLOCK_TRACK	8
217 #define	CLOCK_COMP	4
218 #define	CLOCK_FACTOR	18
219 
220 /*
221  * Structure definitions for NTP fixed point values
222  *
223  *    0			  1		      2			  3
224  *    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
225  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
226  *   |			       Integer Part			     |
227  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
228  *   |			       Fraction Part			     |
229  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
230  *
231  *
232  *    0			  1		      2			  3
233  *    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
234  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235  *   |		  Integer Part	     |	   Fraction Part	     |
236  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237  */
238 struct l_fixedpt {
239 	ulong_t int_part;
240 	ulong_t fraction;
241 };
242 
243 struct s_fixedpt {
244 	ushort_t int_part;
245 	ushort_t fraction;
246 };
247 
248 /*
249  *  =================  Table 3.3. Packet Variables   =================
250  *    0			  1		      2			  3
251  *    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
252  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
253  *   |LI | VN  | Mode|	  Stratum    |	    Poll     |	 Precision   |
254  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
255  *   |			   Synchronizing Distance		     |
256  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
257  *   |			  Synchronizing Dispersion		     |
258  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
259  *   |			Reference Clock Identifier		     |
260  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
261  *   |								     |
262  *   |		       Reference Timestamp (64 bits)		     |
263  *   |								     |
264  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
265  *   |								     |
266  *   |		       Originate Timestamp (64 bits)		     |
267  *   |								     |
268  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
269  *   |								     |
270  *   |			Receive Timestamp (64 bits)		     |
271  *   |								     |
272  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
273  *   |								     |
274  *   |			Transmit Timestamp (64 bits)		     |
275  *   |								     |
276  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
277  *   |		     Encryption Keyid (32 bits, when A bit set)	     |
278  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
279  *   |								     |
280  *   |		Message Authentication Code/MAC (when A bit set)     |
281  *   |								     |
282  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
283  */
284 
285 #define	MAC_OCTETS_DES	8
286 #define	MAC_OCTETS_MD5	16
287 #define	MAC_OCTETS_MIN	MAC_OCTETS_DES
288 #define	MAC_OCTETS_MAX	MAC_OCTETS_MD5
289 #define	AUTH_OCTETS_V3	(MAC_OCTETS_MAX + sizeof (uint32_t))
290 
291 struct ntpdata {
292 	uchar_t li_vn_mode;	/* contains leap indicator, version and mode */
293 	uchar_t stratum; 	/* Stratum level */
294 	uchar_t ppoll;		/* poll value */
295 	int precision:8;
296 	struct s_fixedpt distance;
297 	struct s_fixedpt dispersion;
298 	ulong_t refid;
299 	struct l_fixedpt reftime;
300 	struct l_fixedpt org;
301 	struct l_fixedpt rec;
302 	struct l_fixedpt xmt;
303 	uint32_t keyid;
304 	uchar_t mac[MAC_OCTETS_MAX];
305 };
306 
307 #define	LEN_PKT_NOMAC	(sizeof (struct ntpdata) - AUTH_OCTETS_V3)
308 
309 /*
310  *	Leap Second Codes (high order two bits)
311  */
312 #define	NO_WARNING	0x00	/* no warning */
313 #define	PLUS_SEC	0x40	/* add a second (61 seconds) */
314 #define	MINUS_SEC	0x80	/* minus a second (59 seconds) */
315 #define	ALARM		0xc0	/* alarm condition (clock unsynchronized) */
316 
317 /*
318  *	Clock Status Bits that Encode Version
319  */
320 #define	NTPVERSION_1	0x08
321 #define	VERSIONMASK	0x38
322 #define	LEAPMASK	0xc0
323 #define	NTPMODEMASK	0x07
324 
325 /*
326  *	Code values
327  */
328 #define	MODE_UNSPEC	0	/* unspecified */
329 #define	MODE_SYM_ACT	1	/* symmetric active */
330 #define	MODE_SYM_PAS	2	/* symmetric passive */
331 #define	MODE_CLIENT	3	/* client */
332 #define	MODE_SERVER	4	/* server */
333 #define	MODE_BROADCAST	5	/* broadcast */
334 #define	MODE_CONTROL	6	/* control */
335 #define	MODE_PRIVATE	7	/* private */
336 
337 /*
338  *	Stratum Definitions
339  */
340 #define	UNSPECIFIED	0
341 #define	PRIM_REF	1	/* radio clock */
342 #define	INFO_QUERY	62	/* **** THIS implementation dependent **** */
343 #define	INFO_REPLY	63	/* **** THIS implementation dependent **** */
344 
345 
346 /* =================  table 3.2 Peer Variables	================= */
347 struct ntp_peer {
348 	struct ntp_peer *next, *prev;
349 	struct sockaddr_in src;		/* both peer.srcadr and peer.srcport */
350 	int	flags;			/* local flags */
351 #define	PEER_FL_CONFIG		1
352 #define	PEER_FL_AUTHENABLE	2
353 #define	PEER_FL_SYNC		0x1000	/* peer can bet sync'd to */
354 #define	PEER_FL_BCAST		0x2000	/* broadcast peer */
355 #define	PEER_FL_SELECTED	0x8000	/* actually used by query routine */
356 
357 	int	sock;			/* index into sockets to derive */
358 					/*   peer.dstadr and peer.dstport */
359 	uchar_t	leap;			/* receive */
360 	uchar_t	hmode;			/* receive */
361 	uchar_t	stratum;		/* receive */
362 	uchar_t	ppoll;			/* receive */
363 	uchar_t	hpoll;			/* poll update */
364 	short	precision;		/* receive */
365 	struct	s_fixedpt distance;	/* receive */
366 	struct	s_fixedpt dispersion;	/* receive */
367 	ulong_t	refid;			/* receive */
368 	struct	l_fixedpt reftime;	/* receive */
369 	struct	l_fixedpt org;		/* receive, clear */
370 	struct	l_fixedpt rec;		/* receive, clear */
371 	struct	l_fixedpt xmt;		/* transmit, clear */
372 	ulong_t	reach;			/* receive, transmit, clear */
373 	ulong_t	valid;			/* packet, transmit, clear */
374 	ulong_t	timer;			/* receive, transmit, poll update */
375 	long	stopwatch;		/* <<local>> for timing */
376 	/*
377 	 * first order offsets
378 	 */
379 	struct	filter {
380 		short samples;		/* <<local>> */
381 		double offset[PEER_SHIFT];
382 		double delay[PEER_SHIFT];
383 	} filter;			/* filter, clear */
384 
385 	double	estdelay;		/* filter */
386 	double	estoffset;		/* filter */
387 	double	estdisp;		/* filter */
388 
389 	ulong_t	pkt_sent;		/* <<local>> */
390 	ulong_t pkt_rcvd;		/* <<local>> */
391 	ulong_t	pkt_dropped;		/* <<local>> */
392 };
393 
394 /* ================= table 3.1:  System Variables ================= */
395 
396 struct sysdata {			/* procedure */
397 	uchar_t leap;			/* clock update */
398 	uchar_t stratum;		/* clock update */
399 	short precision;		/* system */
400 	struct s_fixedpt distance;	/* clock update */
401 	struct s_fixedpt dispersion;	/* clock update */
402 	ulong_t refid;			/* clock update */
403 	struct l_fixedpt reftime;	/* clock update */
404 	int hold;			/* clock update */
405 	struct ntp_peer *peer;		/* selection */
406 	int maxpeers;			/* <<local>> */
407 	uchar_t filler;			/* put here for %&*%$$ SUNs */
408 };
409 
410 #define	NTPDC_VERSION	2
411 
412 /*
413  *  These structures are used to pass information to the ntpdc (control)
414  *  program.  They are unique to this implementation and not part of the
415  *  NTP specification.
416  */
417 struct clockinfo {
418 	ulong_t net_address;
419 	ulong_t my_address;
420 	ushort_t port;
421 	ushort_t flags;
422 	ulong_t pkt_sent;
423 	ulong_t pkt_rcvd;
424 	ulong_t pkt_dropped;
425 	ulong_t timer;
426 	uchar_t leap;
427 	uchar_t stratum;
428 	uchar_t ppoll;
429 	int precision:8;
430 
431 	uchar_t hpoll;
432 	uchar_t filler1;
433 	ushort_t reach;
434 
435 	long	estdisp;			/* scaled by 1000 */
436 	long	estdelay;			/* in milliseconds */
437 	long	estoffset;			/* in milliseconds */
438 	ulong_t refid;
439 	struct l_fixedpt reftime;
440 	struct info_filter {
441 		short index;
442 		short filler;
443 		long offset[PEER_SHIFT];	/* in milliseconds */
444 		long delay[PEER_SHIFT];		/* in milliseconds */
445 	} info_filter;
446 };
447 
448 struct ntpinfo {
449 	uchar_t version;
450 	uchar_t type;		/* request type (stratum in ntp packets) */
451 	uchar_t count;		/* number of entries in this packet */
452 	uchar_t seq;		/* sequence number of this packet */
453 
454 	uchar_t npkts;		/* total number of packets */
455 	uchar_t peers;
456 	uchar_t fill3;
457 	uchar_t fill4;
458 };
459 
460 /*
461  * From usr/src/cmd/xntpd/include/ntp_control.h:
462  * Definition of a mode 6 packet.
463  */
464 struct ntp_control {
465 	uchar_t li_vn_mode;		/* leap, version, mode */
466 	uchar_t r_m_e_op;		/* response, more, error, opcode */
467 	ushort_t sequence;		/* sequence number of request */
468 	ushort_t status;		/* status word for association */
469 	ushort_t associd;		/* association ID */
470 	ushort_t offset;		/* offset of this batch of data */
471 	ushort_t count;			/* count of data in this packet */
472 	uchar_t data[1];		/* data + auth */
473 };
474 
475 #define	NTPC_DATA_MAXLEN	(480 + AUTH_OCTETS_V3)
476 
477 /*
478  * Decoding for the r_m_e_op field
479  */
480 #define	CTL_RESPONSE	0x80
481 #define	CTL_ERROR	0x40
482 #define	CTL_MORE	0x20
483 #define	CTL_OP_MASK	0x1f
484 
485 /*
486  * Opcodes
487  */
488 #define	CTL_OP_UNSPEC		0
489 #define	CTL_OP_READSTAT		1
490 #define	CTL_OP_READVAR		2
491 #define	CTL_OP_WRITEVAR		3
492 #define	CTL_OP_READCLOCK	4
493 #define	CTL_OP_WRITECLOCK	5
494 #define	CTL_OP_SETTRAP		6
495 #define	CTL_OP_ASYNCMSG		7
496 #define	CTL_OP_UNSETTRAP	31
497 
498 /*
499  * From usr/src/cmd/xntpd/include/ntp_request.h:
500  * A mode 7 packet is used exchanging data between an NTP server
501  * and a client for purposes other than time synchronization, e.g.
502  * monitoring, statistics gathering and configuration.  A mode 7
503  * packet has the following format:
504  */
505 
506 struct ntp_private {
507 	uchar_t rm_vn_mode;		/* response, more, version, mode */
508 	uchar_t auth_seq;		/* key, sequence number */
509 	uchar_t implementation;		/* implementation number */
510 	uchar_t request;		/* request number */
511 	ushort_t err_nitems;		/* error code/number of data items */
512 	ushort_t mbz_itemsize;		/* item size */
513 	char data[1];			/* data area */
514 };
515 
516 #define	RESP_BIT		0x80
517 #define	MORE_BIT		0x40
518 #define	INFO_VERSION(rm_vn_mode) ((uchar_t)(((rm_vn_mode)>>3) & 0x7))
519 #define	INFO_MODE(rm_vn_mode)	((rm_vn_mode) & 0x7)
520 
521 #define	AUTH_BIT		0x80
522 #define	INFO_SEQ(auth_seq)	((auth_seq) & 0x7f)
523 
524 #define	INFO_ERR(err_nitems)	((ushort_t)((ntohs(err_nitems) >> 12) & 0xf))
525 #define	INFO_NITEMS(err_nitems)	((ushort_t)(ntohs(err_nitems) & 0xfff))
526 
527 #define	INFO_ITEMSIZE(mbz_itemsize) (ntohs(mbz_itemsize) & 0xfff)
528 
529 #ifdef __cplusplus
530 }
531 #endif
532 
533 #endif	/* _NTP_H */
534