xref: /openbsd/usr.sbin/ntpd/ntp.h (revision c338e3e2)
1 /*	$OpenBSD: ntp.h,v 1.15 2023/11/15 15:52:09 otto Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
5  * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _NTP_H_
21 #define _NTP_H_
22 
23 /* Style borrowed from NTP ref/tcpdump and updated for SNTPv4 (RFC2030). */
24 
25 /*
26  * RFC Section 3
27  *
28  *    0			  1		      2			  3
29  *    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
30  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31  *   |			       Integer Part			     |
32  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33  *   |			       Fraction Part			     |
34  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35  *
36  *    0			  1		      2			  3
37  *    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
38  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  *   |		  Integer Part	     |	   Fraction Part	     |
40  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 */
42 struct l_fixedpt {
43 	u_int32_t int_partl;
44 	u_int32_t fractionl;
45 };
46 #define L_DENOMINATOR	(UINT32_MAX + 1ULL)
47 
48 struct s_fixedpt {
49 	u_int16_t int_parts;
50 	u_int16_t fractions;
51 };
52 #define S_DENOMINATOR	(UINT16_MAX + 1)
53 
54 /* RFC Section 4
55  *
56  *    0			  1		      2			  3
57  *    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
58  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59  *   |LI | VN  | Mode|	  Stratum    |	    Poll     |	 Precision   |
60  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61  *   |			   Synchronizing Distance		     |
62  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63  *   |			  Synchronizing Dispersion		     |
64  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65  *   |			Reference Clock Identifier		     |
66  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67  *   |								     |
68  *   |		       Reference Timestamp (64 bits)		     |
69  *   |								     |
70  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71  *   |								     |
72  *   |		       Originate Timestamp (64 bits)		     |
73  *   |								     |
74  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75  *   |								     |
76  *   |			Receive Timestamp (64 bits)		     |
77  *   |								     |
78  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79  *   |								     |
80  *   |			Transmit Timestamp (64 bits)		     |
81  *   |								     |
82  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83  *   |                 Key Identifier (optional) (32)                |
84  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85  *   |                                                               |
86  *   |                                                               |
87  *   |                 Message Digest (optional) (128)               |
88  *   |                                                               |
89  *   |                                                               |
90  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91  *
92  */
93 
94 #define	NTP_DIGESTSIZE		16
95 #define	NTP_MSGSIZE_NOAUTH	48
96 #define	NTP_MSGSIZE		(NTP_MSGSIZE_NOAUTH + 4 + NTP_DIGESTSIZE)
97 
98 struct ntp_msg {
99 	u_int8_t status;	/* status of local clock and leap info */
100 	u_int8_t stratum;	/* Stratum level */
101 	u_int8_t ppoll;		/* poll value */
102 	int8_t precision;
103 	struct s_fixedpt rootdelay;
104 	struct s_fixedpt dispersion;
105 	u_int32_t refid;
106 	struct l_fixedpt reftime;
107 	struct l_fixedpt orgtime;
108 	struct l_fixedpt rectime;
109 	struct l_fixedpt xmttime;
110 } __packed;
111 
112 struct ntp_query {
113 	int			fd;
114 	struct ntp_msg		msg;
115 	double			xmttime;
116 };
117 
118 /*
119  *	Leap Second Codes (high order two bits)
120  */
121 #define	LI_NOWARNING	(0 << 6)	/* no warning */
122 #define	LI_PLUSSEC	(1 << 6)	/* add a second (61 seconds) */
123 #define	LI_MINUSSEC	(2 << 6)	/* minus a second (59 seconds) */
124 #define	LI_ALARM	(3 << 6)	/* alarm condition */
125 
126 /*
127  *	Status Masks
128  */
129 #define	MODEMASK	(7 << 0)
130 #define	VERSIONMASK	(7 << 3)
131 #define LIMASK		(3 << 6)
132 
133 /*
134  *	Mode values
135  */
136 #define	MODE_RES0	0	/* reserved */
137 #define	MODE_SYM_ACT	1	/* symmetric active */
138 #define	MODE_SYM_PAS	2	/* symmetric passive */
139 #define	MODE_CLIENT	3	/* client */
140 #define	MODE_SERVER	4	/* server */
141 #define	MODE_BROADCAST	5	/* broadcast */
142 #define	MODE_RES1	6	/* reserved for NTP control message */
143 #define	MODE_RES2	7	/* reserved for private use */
144 
145 #define	JAN_1970	2208988800UL	/* 1970 - 1900 in seconds */
146 
147 /*
148  * The era we're in if we have no reason to assume otherwise.
149  * If lfp_to_d() sees an offset <= INT32_MAX the era is is assumed to be
150  * NTP_ERA + 1.
151  * Once the actual year is well into era 1, (after 2036) define NTP_ERA to 1
152  * and adapt (remove) the test in lfp_to_d().
153  * Once more than half of era 1 has elapsed (after 2104), re-inroduce the test
154  * to move to era 2 if offset <= INT32_MAX, repeat for each half era.
155  */
156 #define NTP_ERA		0
157 
158 #define SECS_IN_ERA	(UINT32_MAX + 1ULL)
159 
160 #define	NTP_VERSION	4
161 #define	NTP_MAXSTRATUM	15
162 
163 #endif	/* _NTP_H_ */
164