1 /**	$MirOS: src/usr.sbin/ntpd/ntpd.h,v 1.24 2014/03/13 05:48:25 tg Exp $ */
2 /*	$OpenBSD: ntpd.h,v 1.70 2006/06/04 18:58:13 otto Exp $ */
3 
4 /*
5  * Copyright © 2007, 2008, 2009, 2011, 2013, 2014
6  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
7  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
18  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #include <sys/types.h>
23 #include <sys/uio.h>
24 #include <sys/socket.h>
25 #include <sys/queue.h>
26 #include <sys/time.h>
27 #include <netinet/in.h>
28 #include <netinet/in_systm.h>
29 #include <netinet/ip.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 #include <stdarg.h>
33 #include <stdbool.h>
34 
35 #include "ntp.h"
36 
37 #define	NTPD_USER	"_ntp"
38 #define	CONFFILE	"/etc/ntpd.conf"
39 
40 #define	READ_BUF_SIZE		4096
41 
42 #define	NTPD_OPT_VERBOSE	0x0001
43 #define	NTPD_OPT_VERBOSE2	0x0002
44 
45 #define	INTERVAL_QUERY_NORMAL		30	/* sync to peers every n secs */
46 #define	INTERVAL_QUERY_PATHETIC		60
47 #define	INTERVAL_QUERY_AGRESSIVE	5
48 
49 #define	TRUSTLEVEL_BADPEER		6
50 #define	TRUSTLEVEL_PATHETIC		2
51 #define	TRUSTLEVEL_AGRESSIVE		8
52 #define	TRUSTLEVEL_MAX			10
53 #define	TRUSTLEVEL_RESET		(TRUSTLEVEL_AGRESSIVE - 1)
54 
55 #define	MAX_SERVERS_DNS			8
56 
57 #define	QSCALE_OFF_MIN			0.05
58 #define	QSCALE_OFF_MAX			0.50
59 
60 #define	QUERYTIME_MAX		15	/* single query might take n secs max */
61 #define	OFFSET_ARRAY_SIZE	4	/* min. 3, recommended 6, max. 8 */
62 #define	SETTIME_MIN_OFFSET	180	/* min offset for settime at start */
63 #define	SETTIME_TIMEOUT		15	/* max seconds to wait with -s */
64 #define	LOG_NEGLIGEE		125	/* negligible drift to not log (ms) */
65 
66 /**
67  * While various sources state that the delay can never be negative,
68  * here is an example where it actually can be, with good reason:
69  *
70  * Assume an NTP server with a low- to medium precision clock (for
71  * purposes of this example, we assume 10 ms), and an NTP client
72  * with a high-precision clock. Assume they sit on a LAN which has
73  * a transaction time of 1 ms. They are 100 ms apart initially. We
74  * can observe the following sample values over two requests with
75  * no adjustment applied in between:
76  *
77  *		Request 1	Request 2	Comment
78  * T1		1000		2000
79  * T2		1101		2101		In Req2, a tick increasement
80  * T3		1101		2111		happens between T2 and T3
81  * T4		1002		2002
82  *
83  * This leaves us with the following results:
84  * Req1:	d =  2		t = 100		v =  2
85  * Req2:	d = -8		t = 105		v = -8
86  *
87  * According to Mark Martinec, the true delay can only be observed
88  * by averaging delays, including negative ones. However, if one end
89  * is a VM or another low-resolution device, and the other end has
90  * a HPET or similar, chances are that most replies have negative
91  * delays. For this reason, MirBSD OpenNTPD 2009-12-24 ignores delays
92  * below DELAY_NEGLIGEE which defaults to 10 ms (keep it over 1 ms to
93  * accomodate systems without sub-millisecond precision), as most sy-
94  * stems use 100 Hz ticks; set to 0 to revert to the previous behaviour.
95  */
96 #define DELAY_NEGLIGEE	(-0.010)	/* minimum xfer delay accepted */
97 
98 
99 enum client_state {
100 	STATE_NONE,
101 	STATE_DNS_INPROGRESS,
102 	STATE_DNS_TEMPFAIL,
103 	STATE_DNS_DONE,
104 	STATE_QUERY_SENT,
105 	STATE_REPLY_RECEIVED
106 };
107 
108 struct listen_addr {
109 	TAILQ_ENTRY(listen_addr)	 entry;
110 	struct sockaddr_storage		 sa;
111 	int				 fd;
112 };
113 
114 struct ntp_addr {
115 	struct ntp_addr		*next;
116 	struct sockaddr_storage	 ss;
117 };
118 
119 struct ntp_addr_wrap {
120 	char			*name;
121 	struct ntp_addr		*a;
122 	u_int8_t		 pool;
123 };
124 
125 struct ntp_status {
126 	double		rootdelay;
127 	double		rootdispersion;
128 	double		reftime;
129 	u_int32_t	refid;
130 	u_int32_t	refid4;
131 	u_int8_t	synced;
132 	u_int8_t	leap;
133 	int8_t		precision;
134 	u_int8_t	poll;
135 	u_int8_t	stratum;
136 };
137 
138 struct ntp_offset {
139 	struct ntp_status	status;
140 	double			offset;
141 	double			delay;
142 	double			error;
143 	time_t			rcvd;
144 	int			good;
145 };
146 
147 struct ntp_peer {
148 	TAILQ_ENTRY(ntp_peer)		 entry;
149 	struct ntp_addr_wrap		 addr_head;
150 	struct ntp_addr			*addr;
151 	struct ntp_query		*query;
152 	struct ntp_offset		 reply[OFFSET_ARRAY_SIZE];
153 	struct ntp_offset		 update;
154 	time_t				 next;
155 	time_t				 deadline;
156 	int				 lasterror;
157 	enum client_state		 state;
158 	u_int32_t			 id;
159 	u_int8_t			 shift;
160 	u_int8_t			 trustlevel;
161 	u_int8_t			 stratum_offset;
162 };
163 
164 struct ntpd_conf {
165 	TAILQ_HEAD(listen_addrs, listen_addr)	listen_addrs;
166 	TAILQ_HEAD(ntp_peers, ntp_peer)		ntp_peers;
167 	struct ntp_status			status;
168 	u_int32_t				scale;
169 	u_int8_t				listen_all;
170 	u_int8_t				settime;
171 	u_int8_t				debug;
172 	u_int8_t				trace;
173 };
174 
175 struct buf {
176 	TAILQ_ENTRY(buf)	 entry;
177 	u_char			*buf;
178 	size_t			 size;
179 	size_t			 wpos;
180 	size_t			 rpos;
181 };
182 
183 struct msgbuf {
184 	TAILQ_HEAD(, buf)	 bufs;
185 	u_int32_t		 queued;
186 	int			 fd;
187 };
188 
189 struct buf_read {
190 	size_t			 wpos;
191 	u_char			 buf[READ_BUF_SIZE];
192 	u_char			*rptr;
193 };
194 
195 /* ipc messages */
196 
197 #define	IMSG_HEADER_SIZE	sizeof(struct imsg_hdr)
198 #define	MAX_IMSGSIZE		8192
199 
200 struct imsgbuf {
201 	int			fd;
202 	pid_t			pid;
203 	struct buf_read		r;
204 	struct msgbuf		w;
205 };
206 
207 enum imsg_type {
208 	IMSG_NONE,
209 	IMSG_RESET,
210 	IMSG_ADJTIME,
211 	IMSG_SETTIME,
212 	IMSG_HOST_DNS
213 };
214 
215 struct imsg_hdr {
216 	enum imsg_type	type;
217 	u_int32_t	peerid;
218 	pid_t		pid;
219 	u_int16_t	len;
220 };
221 
222 struct imsg {
223 	struct imsg_hdr	 hdr;
224 	void		*data;
225 };
226 
227 /* prototypes */
228 /* log.c */
229 void		 log_init(int);
230 void		 vlog(int, const char *, va_list)
231     __attribute__((__format__(__syslog__, 2, 0)))
232     __attribute__((__format__(__printf__, 2, 0)))
233     __attribute__((__nonnull__(2)));
234 void		 log_warn(const char *, ...)
235     __attribute__((__format__(__syslog__, 1, 2)))
236     __attribute__((__format__(__printf__, 1, 2)))
237     __attribute__((__nonnull__(1)));
238 void		 log_warnx(const char *, ...)
239     __attribute__((__format__(__syslog__, 1, 2)))
240     __attribute__((__format__(__printf__, 1, 2)))
241     __attribute__((__nonnull__(1)));
242 void		 log_info(const char *, ...)
243     __attribute__((__format__(__syslog__, 1, 2)))
244     __attribute__((__format__(__printf__, 1, 2)))
245     __attribute__((__nonnull__(1)));
246 void		 log_debug(const char *, ...)
247     __attribute__((__format__(__syslog__, 1, 2)))
248     __attribute__((__format__(__printf__, 1, 2)))
249     __attribute__((__nonnull__(1)));
250 __dead void	 fatal(const char *);
251 __dead void	 fatalx(const char *);
252 
253 /* buffer.c */
254 struct buf	*buf_open(size_t);
255 int		 buf_add(struct buf *, void *, size_t);
256 int		 buf_close(struct msgbuf *, struct buf *);
257 void		 buf_free(struct buf *);
258 void		 msgbuf_init(struct msgbuf *);
259 void		 msgbuf_clear(struct msgbuf *);
260 int		 msgbuf_write(struct msgbuf *);
261 
262 /* imsg.c */
263 void	 imsg_init(struct imsgbuf *, int);
264 int	 imsg_read(struct imsgbuf *);
265 int	 imsg_get(struct imsgbuf *, struct imsg *);
266 int	 imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
267 	    void *, u_int16_t);
268 struct buf	*imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
269 		    u_int16_t);
270 int	 imsg_add(struct buf *, void *, u_int16_t);
271 int	 imsg_close(struct imsgbuf *, struct buf *);
272 void	 imsg_free(struct imsg *);
273 
274 /* ntp.c */
275 pid_t	 ntp_main(int[2], struct ntpd_conf *);
276 int	 priv_adjtime(void);
277 void	 priv_settime(double);
278 void	 priv_host_dns(char *, u_int32_t);
279 void	 chpeertrust(struct ntp_peer *, bool);
280 
281 /* parse.y */
282 int	 parse_config(const char *, struct ntpd_conf *);
283 
284 /* config.c */
285 int		 host(const char *, struct ntp_addr **);
286 int		 host_dns(const char *, struct ntp_addr **);
287 struct ntp_peer	*new_peer(void);
288 
289 /* ntp_msg.c */
290 int	ntp_getmsg(struct sockaddr *, char *, ssize_t, struct ntp_msg *);
291 int	ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *, ssize_t, int);
292 
293 /* server.c */
294 int	setup_listeners(struct servent *, struct ntpd_conf *, u_int *);
295 int	ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
296 int	server_dispatch(int, struct ntpd_conf *);
297 
298 /* client.c */
299 int	client_peer_init(struct ntp_peer *);
300 int	client_addr_init(struct ntp_peer *);
301 int	client_nextaddr(struct ntp_peer *);
302 int	client_query(struct ntp_peer *);
303 int	client_dispatch(struct ntp_peer *, u_int8_t, uint8_t);
304 void	client_log_error(struct ntp_peer *, const char *, int);
305 void	update_scale(double);
306 time_t	scale_interval(time_t);
307 time_t	error_interval(void);
308 void	set_next(struct ntp_peer *, time_t);
309 
310 /* util.c */
311 void			d_to_tv(double, struct timeval *);
312 double			lfp_to_d(struct l_fixedpt);
313 struct l_fixedpt	d_to_lfp(double);
314 double			sfp_to_d(struct s_fixedpt);
315 struct s_fixedpt	d_to_sfp(double);
316 
317 /* ../rdate/cutil.c */
318 const char *log_sockaddr(struct sockaddr *);
319 double gettime(void);
320