xref: /openbsd/usr.sbin/ntpd/ntp.h (revision 09467b48)
1 /*	$OpenBSD: ntp.h,v 1.14 2020/01/30 15:55:41 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 
47 struct s_fixedpt {
48 	u_int16_t int_parts;
49 	u_int16_t fractions;
50 };
51 
52 /* RFC Section 4
53  *
54  *    0			  1		      2			  3
55  *    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
56  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57  *   |LI | VN  | Mode|	  Stratum    |	    Poll     |	 Precision   |
58  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59  *   |			   Synchronizing Distance		     |
60  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61  *   |			  Synchronizing Dispersion		     |
62  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63  *   |			Reference Clock Identifier		     |
64  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65  *   |								     |
66  *   |		       Reference Timestamp (64 bits)		     |
67  *   |								     |
68  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69  *   |								     |
70  *   |		       Originate Timestamp (64 bits)		     |
71  *   |								     |
72  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73  *   |								     |
74  *   |			Receive Timestamp (64 bits)		     |
75  *   |								     |
76  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77  *   |								     |
78  *   |			Transmit Timestamp (64 bits)		     |
79  *   |								     |
80  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81  *   |                 Key Identifier (optional) (32)                |
82  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83  *   |                                                               |
84  *   |                                                               |
85  *   |                 Message Digest (optional) (128)               |
86  *   |                                                               |
87  *   |                                                               |
88  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89  *
90  */
91 
92 #define	NTP_DIGESTSIZE		16
93 #define	NTP_MSGSIZE_NOAUTH	48
94 #define	NTP_MSGSIZE		(NTP_MSGSIZE_NOAUTH + 4 + NTP_DIGESTSIZE)
95 
96 struct ntp_msg {
97 	u_int8_t status;	/* status of local clock and leap info */
98 	u_int8_t stratum;	/* Stratum level */
99 	u_int8_t ppoll;		/* poll value */
100 	int8_t precision;
101 	struct s_fixedpt rootdelay;
102 	struct s_fixedpt dispersion;
103 	u_int32_t refid;
104 	struct l_fixedpt reftime;
105 	struct l_fixedpt orgtime;
106 	struct l_fixedpt rectime;
107 	struct l_fixedpt xmttime;
108 } __packed;
109 
110 struct ntp_query {
111 	int			fd;
112 	struct ntp_msg		msg;
113 	double			xmttime;
114 };
115 
116 /*
117  *	Leap Second Codes (high order two bits)
118  */
119 #define	LI_NOWARNING	(0 << 6)	/* no warning */
120 #define	LI_PLUSSEC	(1 << 6)	/* add a second (61 seconds) */
121 #define	LI_MINUSSEC	(2 << 6)	/* minus a second (59 seconds) */
122 #define	LI_ALARM	(3 << 6)	/* alarm condition */
123 
124 /*
125  *	Status Masks
126  */
127 #define	MODEMASK	(7 << 0)
128 #define	VERSIONMASK	(7 << 3)
129 #define LIMASK		(3 << 6)
130 
131 /*
132  *	Mode values
133  */
134 #define	MODE_RES0	0	/* reserved */
135 #define	MODE_SYM_ACT	1	/* symmetric active */
136 #define	MODE_SYM_PAS	2	/* symmetric passive */
137 #define	MODE_CLIENT	3	/* client */
138 #define	MODE_SERVER	4	/* server */
139 #define	MODE_BROADCAST	5	/* broadcast */
140 #define	MODE_RES1	6	/* reserved for NTP control message */
141 #define	MODE_RES2	7	/* reserved for private use */
142 
143 #define	JAN_1970	2208988800UL	/* 1970 - 1900 in seconds */
144 
145 /*
146  * The era we're in if we have no reason to assume otherwise.
147  * If lfp_to_d() sees an offset <= INT32_MAX the era is is assumed to be
148  * NTP_ERA + 1.
149  * Once the actual year is well into era 1, (after 2036) define NTP_ERA to 1
150  * and adapt (remove) the test in lfp_to_d().
151  * Once more than half of era 1 has elapsed (after 2104), re-inroduce the test
152  * to move to era 2 if offset <= INT32_MAX, repeat for each half era.
153  */
154 #define NTP_ERA		0
155 
156 #define SECS_IN_ERA	(UINT32_MAX + 1ULL)
157 
158 #define	NTP_VERSION	4
159 #define	NTP_MAXSTRATUM	15
160 
161 #endif	/* _NTP_H_ */
162