xref: /openbsd-src/usr.sbin/ntpd/ntpd.h (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /*	$OpenBSD: ntpd.h,v 1.144 2019/06/20 07:28:18 otto Exp $ */
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  * Copyright (c) 2012 Mike Miller <mmiller@mgm51.com>
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 #include <sys/types.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/queue.h>
24 #include <sys/time.h>
25 #include <netinet/in.h>
26 #include <netinet/ip.h>
27 #include <arpa/inet.h>
28 #include <netdb.h>
29 #include <pwd.h>
30 #include <stdarg.h>
31 #include <poll.h>
32 #include <imsg.h>
33 
34 #include "ntp.h"
35 
36 #define MAXIMUM(a, b)	((a) > (b) ? (a) : (b))
37 
38 #define	NTPD_USER	"_ntp"
39 #define	CONFFILE	"/etc/ntpd.conf"
40 #define DRIFTFILE	"/var/db/ntpd.drift"
41 #define	CTLSOCKET	"/var/run/ntpd.sock"
42 
43 #define	INTERVAL_QUERY_NORMAL		30	/* sync to peers every n secs */
44 #define	INTERVAL_QUERY_PATHETIC		60
45 #define	INTERVAL_QUERY_AGGRESSIVE	5
46 #define	INTERVAL_QUERY_ULTRA_VIOLENCE	1	/* used at startup for auto */
47 
48 #define	TRUSTLEVEL_BADPEER		6
49 #define	TRUSTLEVEL_PATHETIC		2
50 #define	TRUSTLEVEL_AGGRESSIVE		8
51 #define	TRUSTLEVEL_MAX			10
52 
53 #define	MAX_SERVERS_DNS			8
54 
55 #define	QSCALE_OFF_MIN			0.001
56 #define	QSCALE_OFF_MAX			0.050
57 
58 #define	QUERYTIME_MAX		15	/* single query might take n secs max */
59 #define	OFFSET_ARRAY_SIZE	8
60 #define	SENSOR_OFFSETS		6
61 #define	SETTIME_TIMEOUT		15	/* max seconds to wait with -s */
62 #define	LOG_NEGLIGIBLE_ADJTIME	32	/* negligible drift to not log (ms) */
63 #define	LOG_NEGLIGIBLE_ADJFREQ	0.05	/* negligible rate to not log (ppm) */
64 #define	FREQUENCY_SAMPLES	8	/* samples for est. of permanent drift */
65 #define	MAX_FREQUENCY_ADJUST	128e-5	/* max correction per iteration */
66 #define MAX_SEND_ERRORS		3	/* max send errors before reconnect */
67 #define	MAX_DISPLAY_WIDTH	80	/* max chars in ctl_show report line */
68 
69 #define FILTER_ADJFREQ		0x01	/* set after doing adjfreq */
70 #define AUTO_REPLIES    	4	/* # of ntp replies we want for auto */
71 #define AUTO_THRESHOLD		60	/* dont bother auto setting < this */
72 #define INTERVAL_AUIO_DNSFAIL	1	/* DNS tmpfail interval for auto */
73 #define TRIES_AUTO_DNSFAIL	4	/* DNS tmpfail quick retries */
74 
75 
76 #define	SENSOR_DATA_MAXAGE		(15*60)
77 #define	SENSOR_QUERY_INTERVAL		15
78 #define	SENSOR_QUERY_INTERVAL_SETTIME	(SETTIME_TIMEOUT/3)
79 #define	SENSOR_SCAN_INTERVAL		(1*60)
80 #define	SENSOR_DEFAULT_REFID		"HARD"
81 
82 #define CONSTRAINT_ERROR_MARGIN		(4)
83 #define CONSTRAINT_RETRY_INTERVAL	(15)
84 #define CONSTRAINT_SCAN_INTERVAL	(15*60)
85 #define CONSTRAINT_SCAN_TIMEOUT		(10)
86 #define CONSTRAINT_MARGIN		(2.0*60)
87 #define CONSTRAINT_PORT			"443"	/* HTTPS port */
88 #define	CONSTRAINT_MAXHEADERLENGTH	8192
89 #define CONSTRAINT_PASSFD		(STDERR_FILENO + 1)
90 
91 #define PARENT_SOCK_FILENO		CONSTRAINT_PASSFD
92 
93 #define NTP_PROC_NAME			"ntp_main"
94 #define NTPDNS_PROC_NAME		"ntp_dns"
95 #define CONSTRAINT_PROC_NAME		"constraint"
96 
97 enum client_state {
98 	STATE_NONE,
99 	STATE_DNS_INPROGRESS,
100 	STATE_DNS_TEMPFAIL,
101 	STATE_DNS_DONE,
102 	STATE_QUERY_SENT,
103 	STATE_REPLY_RECEIVED,
104 	STATE_TIMEOUT,
105 	STATE_INVALID
106 };
107 
108 struct listen_addr {
109 	TAILQ_ENTRY(listen_addr)	 entry;
110 	struct sockaddr_storage		 sa;
111 	int				 fd;
112 	int				 rtable;
113 };
114 
115 struct ntp_addr {
116 	struct ntp_addr		*next;
117 	struct sockaddr_storage	 ss;
118 	int			 notauth;
119 };
120 
121 struct ntp_addr_wrap {
122 	char			*name;
123 	char			*path;
124 	struct ntp_addr		*a;
125 	u_int8_t		 pool;
126 };
127 
128 struct ntp_addr_msg {
129 	struct ntp_addr		 a;
130 	size_t			 namelen;
131 	size_t			 pathlen;
132 };
133 
134 struct ntp_status {
135 	double		rootdelay;
136 	double		rootdispersion;
137 	double		reftime;
138 	u_int32_t	refid;
139 	u_int32_t	send_refid;
140 	u_int8_t	synced;
141 	u_int8_t	leap;
142 	int8_t		precision;
143 	u_int8_t	poll;
144 	u_int8_t	stratum;
145 };
146 
147 struct ntp_offset {
148 	struct ntp_status	status;
149 	double			offset;
150 	double			delay;
151 	double			error;
152 	time_t			rcvd;
153 	u_int8_t		good;
154 };
155 
156 struct ntp_peer {
157 	TAILQ_ENTRY(ntp_peer)		 entry;
158 	struct ntp_addr_wrap		 addr_head;
159 	struct ntp_addr			*addr;
160 	struct ntp_query		*query;
161 	struct ntp_offset		 reply[OFFSET_ARRAY_SIZE];
162 	struct ntp_offset		 update;
163 	struct sockaddr_in		 query_addr4;
164 	struct sockaddr_in6		 query_addr6;
165 	enum client_state		 state;
166 	time_t				 next;
167 	time_t				 deadline;
168 	time_t				 poll;
169 	u_int32_t			 id;
170 	u_int8_t			 shift;
171 	u_int8_t			 trustlevel;
172 	u_int8_t			 weight;
173 	int				 lasterror;
174 	int				 senderrors;
175 };
176 
177 struct ntp_sensor {
178 	TAILQ_ENTRY(ntp_sensor)		 entry;
179 	struct ntp_offset		 offsets[SENSOR_OFFSETS];
180 	struct ntp_offset		 update;
181 	time_t				 next;
182 	time_t				 last;
183 	char				*device;
184 	u_int32_t			 refid;
185 	int				 sensordevid;
186 	int				 correction;
187 	u_int8_t			 stratum;
188 	u_int8_t			 weight;
189 	u_int8_t			 shift;
190 };
191 
192 struct constraint {
193 	TAILQ_ENTRY(constraint)		 entry;
194 	struct ntp_addr_wrap		 addr_head;
195 	struct ntp_addr			*addr;
196 	int				 senderrors;
197 	enum client_state		 state;
198 	u_int32_t			 id;
199 	int				 fd;
200 	pid_t				 pid;
201 	struct imsgbuf			 ibuf;
202 	time_t				 last;
203 	time_t				 constraint;
204 	int				 dnstries;
205 };
206 
207 struct ntp_conf_sensor {
208 	TAILQ_ENTRY(ntp_conf_sensor)		 entry;
209 	char					*device;
210 	char					*refstr;
211 	int					 correction;
212 	u_int8_t				 stratum;
213 	u_int8_t				 weight;
214 };
215 
216 struct ntp_freq {
217 	double				overall_offset;
218 	double				x, y;
219 	double				xx, xy;
220 	int				samples;
221 	u_int				num;
222 };
223 
224 struct ntpd_conf {
225 	TAILQ_HEAD(listen_addrs, listen_addr)		listen_addrs;
226 	TAILQ_HEAD(ntp_peers, ntp_peer)			ntp_peers;
227 	TAILQ_HEAD(ntp_sensors, ntp_sensor)		ntp_sensors;
228 	TAILQ_HEAD(ntp_conf_sensors, ntp_conf_sensor)	ntp_conf_sensors;
229 	TAILQ_HEAD(constraints, constraint)		constraints;
230 	struct ntp_status				status;
231 	struct ntp_freq					freq;
232 	struct sockaddr_in				query_addr4;
233 	struct sockaddr_in6				query_addr6;
234 	u_int32_t					scale;
235 	int				        	debug;
236 	int				        	verbose;
237 	u_int8_t					listen_all;
238 	u_int8_t					settime;
239 	u_int8_t					automatic;
240 	u_int8_t					noaction;
241 	u_int8_t					filters;
242 	time_t						constraint_last;
243 	time_t						constraint_median;
244 	u_int						constraint_errors;
245 	u_int8_t					*ca;
246 	size_t						ca_len;
247 	int						tmpfail;
248 };
249 
250 struct ctl_show_status {
251 	u_int		 peercnt;
252 	u_int		 sensorcnt;
253 	u_int		 valid_peers;
254 	u_int		 valid_sensors;
255 	u_int8_t	 synced;
256 	u_int8_t	 stratum;
257 	double		 clock_offset;
258 	time_t		 constraint_median;
259 	time_t		 constraint_last;
260 	u_int		 constraint_errors;
261 };
262 
263 struct ctl_show_peer {
264 	char		 peer_desc[MAX_DISPLAY_WIDTH];
265 	u_int8_t	 syncedto;
266 	u_int8_t	 weight;
267 	u_int8_t	 trustlevel;
268 	u_int8_t	 stratum;
269 	time_t		 next;
270 	time_t		 poll;
271 	double		 offset;
272 	double		 delay;
273 	double		 jitter;
274 };
275 
276 struct ctl_show_sensor {
277 	char		 sensor_desc[MAX_DISPLAY_WIDTH];
278 	u_int8_t	 syncedto;
279 	u_int8_t	 weight;
280 	u_int8_t	 good;
281 	u_int8_t	 stratum;
282 	time_t		 next;
283 	time_t		 poll;
284 	double		 offset;
285 	double		 correction;
286 };
287 
288 struct ctl_conn {
289 	TAILQ_ENTRY(ctl_conn)	entry;
290 	struct imsgbuf		ibuf;
291 };
292 
293 TAILQ_HEAD(ctl_conns, ctl_conn)	;
294 
295 enum imsg_type {
296 	IMSG_NONE,
297 	IMSG_ADJTIME,
298 	IMSG_ADJFREQ,
299 	IMSG_SETTIME,
300 	IMSG_HOST_DNS,
301 	IMSG_CONSTRAINT_DNS,
302 	IMSG_CONSTRAINT_QUERY,
303 	IMSG_CONSTRAINT_RESULT,
304 	IMSG_CONSTRAINT_CLOSE,
305 	IMSG_CONSTRAINT_KILL,
306 	IMSG_CTL_SHOW_STATUS,
307 	IMSG_CTL_SHOW_PEERS,
308 	IMSG_CTL_SHOW_PEERS_END,
309 	IMSG_CTL_SHOW_SENSORS,
310 	IMSG_CTL_SHOW_SENSORS_END,
311 	IMSG_CTL_SHOW_ALL,
312 	IMSG_CTL_SHOW_ALL_END,
313 	IMSG_SYNCED,
314 	IMSG_UNSYNCED,
315 	IMSG_PROBE_ROOT
316 };
317 
318 enum ctl_actions {
319 	CTL_SHOW_STATUS,
320 	CTL_SHOW_PEERS,
321 	CTL_SHOW_SENSORS,
322 	CTL_SHOW_ALL
323 };
324 
325 /* prototypes */
326 
327 /* ntp.c */
328 void	 ntp_main(struct ntpd_conf *, struct passwd *, int, char **);
329 void	 peer_addr_head_clear(struct ntp_peer *);
330 int	 priv_adjtime(void);
331 void	 priv_settime(double, char *);
332 void	 priv_dns(int, char *, u_int32_t);
333 int	 offset_compare(const void *, const void *);
334 void	 update_scale(double);
335 time_t	 scale_interval(time_t);
336 time_t	 error_interval(void);
337 extern struct ntpd_conf *conf;
338 extern struct ctl_conns  ctl_conns;
339 
340 /* parse.y */
341 int	 parse_config(const char *, struct ntpd_conf *);
342 
343 /* config.c */
344 void			 host(const char *, struct ntp_addr **);
345 int			 host_dns(const char *, int, struct ntp_addr **);
346 void			 host_dns_free(struct ntp_addr *);
347 struct ntp_peer		*new_peer(void);
348 struct ntp_conf_sensor	*new_sensor(char *);
349 struct constraint	*new_constraint(void);
350 
351 /* ntp_msg.c */
352 int	ntp_getmsg(struct sockaddr *, char *, ssize_t, struct ntp_msg *);
353 int	ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *);
354 
355 /* server.c */
356 int	setup_listeners(struct servent *, struct ntpd_conf *, u_int *);
357 int	ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
358 int	server_dispatch(int, struct ntpd_conf *);
359 
360 /* client.c */
361 int	client_peer_init(struct ntp_peer *);
362 int	client_addr_init(struct ntp_peer *);
363 int	client_nextaddr(struct ntp_peer *);
364 int	client_query(struct ntp_peer *);
365 int	client_dispatch(struct ntp_peer *, u_int8_t, u_int8_t);
366 void	client_log_error(struct ntp_peer *, const char *, int);
367 void	set_next(struct ntp_peer *, time_t);
368 
369 /* constraint.c */
370 void	 constraint_add(struct constraint *);
371 void	 constraint_remove(struct constraint *);
372 void	 constraint_purge(void);
373 int	 constraint_init(struct constraint *);
374 int	 constraint_query(struct constraint *);
375 int	 constraint_check(double);
376 void	 constraint_msg_dns(u_int32_t, u_int8_t *, size_t);
377 void	 constraint_msg_result(u_int32_t, u_int8_t *, size_t);
378 void	 constraint_msg_close(u_int32_t, u_int8_t *, size_t);
379 void	 priv_constraint_msg(u_int32_t, u_int8_t *, size_t, int, char **);
380 void	 priv_constraint_child(const char *, uid_t, gid_t);
381 void	 priv_constraint_kill(u_int32_t);
382 int	 priv_constraint_dispatch(struct pollfd *);
383 void	 priv_constraint_check_child(pid_t, int);
384 char	*get_string(u_int8_t *, size_t);
385 
386 /* util.c */
387 double			 gettime_corrected(void);
388 double			 gettime_from_timeval(struct timeval *);
389 double			 getoffset(void);
390 double			 gettime(void);
391 time_t			 getmonotime(void);
392 void			 d_to_tv(double, struct timeval *);
393 double			 lfp_to_d(struct l_fixedpt);
394 struct l_fixedpt	 d_to_lfp(double);
395 double			 sfp_to_d(struct s_fixedpt);
396 struct s_fixedpt	 d_to_sfp(double);
397 char			*print_rtable(int);
398 const char		*log_sockaddr(struct sockaddr *);
399 pid_t			 start_child(char *, int, int, char **);
400 int			 sanitize_argv(int *, char ***);
401 
402 /* sensors.c */
403 void			sensor_init(void);
404 int			sensor_scan(void);
405 void			sensor_query(struct ntp_sensor *);
406 
407 /* ntp_dns.c */
408 void			ntp_dns(struct ntpd_conf *, struct passwd *);
409 
410 /* control.c */
411 int			 control_check(char *);
412 int			 control_init(char *);
413 int			 control_listen(int);
414 void			 control_shutdown(int);
415 int			 control_accept(int);
416 struct ctl_conn		*control_connbyfd(int);
417 int			 control_close(int);
418 int			 control_dispatch_msg(struct pollfd *, u_int *);
419 void			 session_socket_nonblockmode(int);
420 void			 build_show_status(struct ctl_show_status *);
421 void			 build_show_peer(struct ctl_show_peer *,
422 			     struct ntp_peer *);
423 void			 build_show_sensor(struct ctl_show_sensor *,
424 			     struct ntp_sensor *);
425 
426 /* log.c */
427 void	log_init(int, int);
428 void	log_procinit(const char *);
429 void	log_setverbose(int);
430 int	log_getverbose(void);
431 void	log_warn(const char *, ...)
432 	    __attribute__((__format__ (printf, 1, 2)));
433 void	log_warnx(const char *, ...)
434 	    __attribute__((__format__ (printf, 1, 2)));
435 void	log_info(const char *, ...)
436 	    __attribute__((__format__ (printf, 1, 2)));
437 void	log_debug(const char *, ...)
438 	    __attribute__((__format__ (printf, 1, 2)));
439 void	logit(int, const char *, ...)
440 	    __attribute__((__format__ (printf, 2, 3)));
441 void	vlog(int, const char *, va_list)
442 	    __attribute__((__format__ (printf, 2, 0)));
443 __dead void fatal(const char *, ...)
444 	    __attribute__((__format__ (printf, 1, 2)));
445 __dead void fatalx(const char *, ...)
446 	    __attribute__((__format__ (printf, 1, 2)));
447