xref: /netbsd-src/external/bsd/ntp/dist/libntp/statestr.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: statestr.c,v 1.8 2020/05/25 20:47:24 christos Exp $	*/
2 
3 /*
4  * pretty printing of status information
5  */
6 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9 #include <stdio.h>
10 #include "ntp_stdlib.h"
11 #include "ntp_fp.h"
12 #include "ntp.h"
13 #include "lib_strbuf.h"
14 #include "ntp_refclock.h"
15 #include "ntp_control.h"
16 #include "ntp_string.h"
17 #ifdef KERNEL_PLL
18 # include "ntp_syscall.h"
19 #endif
20 
21 
22 /*
23  * Structure for turning various constants into a readable string.
24  */
25 struct codestring {
26 	int code;
27 	const char * const string1;
28 	const char * const string0;
29 };
30 
31 /*
32  * Leap status (leap)
33  */
34 static const struct codestring leap_codes[] = {
35 	{ LEAP_NOWARNING,	"leap_none",	0 },
36 	{ LEAP_ADDSECOND,	"leap_add_sec",	0 },
37 	{ LEAP_DELSECOND,	"leap_del_sec",	0 },
38 	{ LEAP_NOTINSYNC,	"leap_alarm",	0 },
39 	{ -1,			"leap",		0 }
40 };
41 
42 /*
43  * Clock source status (sync)
44  */
45 static const struct codestring sync_codes[] = {
46 	{ CTL_SST_TS_UNSPEC,	"sync_unspec",		0 },
47 	{ CTL_SST_TS_ATOM,	"sync_pps",		0 },
48 	{ CTL_SST_TS_LF,	"sync_lf_radio",	0 },
49 	{ CTL_SST_TS_HF,	"sync_hf_radio",	0 },
50 	{ CTL_SST_TS_UHF,	"sync_uhf_radio",	0 },
51 	{ CTL_SST_TS_LOCAL,	"sync_local",		0 },
52 	{ CTL_SST_TS_NTP,	"sync_ntp",		0 },
53 	{ CTL_SST_TS_UDPTIME,	"sync_other",		0 },
54 	{ CTL_SST_TS_WRSTWTCH,	"sync_wristwatch",	0 },
55 	{ CTL_SST_TS_TELEPHONE,	"sync_telephone",	0 },
56 	{ -1,			"sync",			0 }
57 };
58 
59 /*
60  * Peer selection status (sel)
61  */
62 static const struct codestring select_codes[] = {
63 	{ CTL_PST_SEL_REJECT,	"sel_reject",		0 },
64 	{ CTL_PST_SEL_SANE,	"sel_falsetick",	0 },
65 	{ CTL_PST_SEL_CORRECT,	"sel_excess",		0 },
66 	{ CTL_PST_SEL_SELCAND,	"sel_outlier",		0 },
67 	{ CTL_PST_SEL_SYNCCAND,	"sel_candidate",	0 },
68 	{ CTL_PST_SEL_EXCESS,	"sel_backup",		0 },
69 	{ CTL_PST_SEL_SYSPEER,	"sel_sys.peer",		0 },
70 	{ CTL_PST_SEL_PPS,	"sel_pps.peer",		0 },
71 	{ -1,			"sel",			0 }
72 };
73 
74 /*
75  * Clock status (clk)
76  */
77 static const struct codestring clock_codes[] = {
78 	{ CTL_CLK_OKAY,		"clk_unspec",		0 },
79 	{ CTL_CLK_NOREPLY,	"clk_no_reply",		0 },
80 	{ CTL_CLK_BADFORMAT,	"clk_bad_format",	0 },
81 	{ CTL_CLK_FAULT,	"clk_fault",		0 },
82 	{ CTL_CLK_PROPAGATION,	"clk_bad_signal",	0 },
83 	{ CTL_CLK_BADDATE,	"clk_bad_date",		0 },
84 	{ CTL_CLK_BADTIME,	"clk_bad_time",		0 },
85 	{ -1,			"clk",			0 }
86 };
87 
88 
89 #ifdef FLASH_CODES_UNUSED
90 /*
91  * Flash bits -- see ntpq.c tstflags & tstflagnames
92  */
93 static const struct codestring flash_codes[] = {
94 	{ TEST1,		"pkt_dup",	0 },
95 	{ TEST2,		"pkt_bogus",	0 },
96 	{ TEST3,		"pkt_unsync",	0 },
97 	{ TEST4,		"pkt_denied",	0 },
98 	{ TEST5,		"pkt_auth",	0 },
99 	{ TEST6,		"pkt_stratum",	0 },
100 	{ TEST7,		"pkt_header",	0 },
101 	{ TEST8,		"pkt_autokey",	0 },
102 	{ TEST9,		"pkt_crypto",	0 },
103 	{ TEST10,		"peer_stratum",	0 },
104 	{ TEST11,		"peer_dist",	0 },
105 	{ TEST12,		"peer_loop",	0 },
106 	{ TEST13,		"peer_unreach",	0 },
107 	{ -1,			"flash",	0 }
108 };
109 #endif
110 
111 
112 /*
113  * System events (sys)
114  */
115 static const struct codestring sys_codes[] = {
116 	{ EVNT_UNSPEC,		"unspecified",			0 },
117 	{ EVNT_NSET,		"freq_not_set",			0 },
118 	{ EVNT_FSET,		"freq_set",			0 },
119 	{ EVNT_SPIK,		"spike_detect",			0 },
120 	{ EVNT_FREQ,		"freq_mode",			0 },
121 	{ EVNT_SYNC,		"clock_sync",			0 },
122 	{ EVNT_SYSRESTART,	"restart",			0 },
123 	{ EVNT_SYSFAULT,	"panic_stop",			0 },
124 	{ EVNT_NOPEER,		"no_sys_peer",			0 },
125 	{ EVNT_ARMED,		"leap_armed",			0 },
126 	{ EVNT_DISARMED,	"leap_disarmed",		0 },
127 	{ EVNT_LEAP,		"leap_event",			0 },
128 	{ EVNT_CLOCKRESET,	"clock_step",			0 },
129 	{ EVNT_KERN,		"kern",				0 },
130 	{ EVNT_TAI,		"TAI",				0 },
131 	{ EVNT_LEAPVAL,		"stale_leapsecond_values",	0 },
132 	{ -1,			"",				0 }
133 };
134 
135 /*
136  * Peer events (peer)
137  */
138 static const struct codestring peer_codes[] = {
139 	{ PEVNT_MOBIL & ~PEER_EVENT,	"mobilize",		0 },
140 	{ PEVNT_DEMOBIL & ~PEER_EVENT,	"demobilize",		0 },
141 	{ PEVNT_UNREACH & ~PEER_EVENT,	"unreachable",		0 },
142 	{ PEVNT_REACH & ~PEER_EVENT,	"reachable",		0 },
143 	{ PEVNT_RESTART & ~PEER_EVENT,	"restart",		0 },
144 	{ PEVNT_REPLY & ~PEER_EVENT,	"no_reply",		0 },
145 	{ PEVNT_RATE & ~PEER_EVENT,	"rate_exceeded",	0 },
146 	{ PEVNT_DENY & ~PEER_EVENT,	"access_denied",	0 },
147 	{ PEVNT_ARMED & ~PEER_EVENT,	"leap_armed",		0 },
148 	{ PEVNT_NEWPEER & ~PEER_EVENT,	"sys_peer",		0 },
149 	{ PEVNT_CLOCK & ~PEER_EVENT,	"clock_event",		0 },
150 	{ PEVNT_AUTH & ~PEER_EVENT,	"bad_auth",		0 },
151 	{ PEVNT_POPCORN & ~PEER_EVENT,	"popcorn",		0 },
152 	{ PEVNT_XLEAVE & ~PEER_EVENT,	"interleave_mode",	0 },
153 	{ PEVNT_XERR & ~PEER_EVENT,	"interleave_error",	0 },
154 	{ -1,				"",			0 }
155 };
156 
157 /*
158  * Peer status bits
159  */
160 static const struct codestring peer_st_bits[] = {
161 	{ CTL_PST_CONFIG,		"conf",		0 },
162 	{ CTL_PST_AUTHENABLE,		"authenb",	0 },
163 	{ CTL_PST_AUTHENTIC,		"auth",		0 },
164 	{ CTL_PST_REACH,		"reach",	0 },
165 	{ CTL_PST_BCAST,		"bcast",	0 },
166 	/* not used with getcode(), no terminating entry needed */
167 };
168 
169 /*
170  * Restriction match bits
171  */
172 static const struct codestring res_match_bits[] = {
173 	{ RESM_NTPONLY,			"ntpport",	0 },
174 	{ RESM_INTERFACE,		"interface",	0 },
175 	{ RESM_SOURCE,			"source",	0 },
176 	/* not used with getcode(), no terminating entry needed */
177 };
178 
179 /*
180  * Restriction access bits
181  */
182 static const struct codestring res_access_bits[] = {
183 	{ RES_IGNORE,			"ignore",	0 },
184 	{ RES_DONTSERVE,		"noserve",	"serve" },
185 	{ RES_DONTTRUST,		"notrust",	"trust" },
186 	{ RES_VERSION,			"version",	0 },
187 	{ RES_NOPEER,			"nopeer",	"peer" },
188 	{ RES_NOEPEER,			"noepeer",	"epeer" },
189 	{ RES_LIMITED,			"limited",	0 },
190 
191 	{ RES_NOQUERY,			"noquery",	"query" },
192 	{ RES_NOMODIFY,			"nomodify",	0 },
193 	{ RES_NOTRAP,			"notrap",	"trap" },
194 	{ RES_LPTRAP,			"lptrap",	0 },
195 
196 	{ RES_KOD,			"kod",		0 },
197 	{ RES_MSSNTP,			"mssntp",	0 },
198 	{ RES_FLAKE,			"flake",	0 },
199 	{ RES_NOMRULIST,		"nomrulist",	0 },
200 
201 	{ RES_SRVRSPFUZ,		"serverresponse fuzz",	0 },
202 
203 	/* not used with getcode(), no terminating entry needed */
204 };
205 
206 #ifdef AUTOKEY
207 /*
208  * Crypto events (cryp)
209  */
210 static const struct codestring crypto_codes[] = {
211 	{ XEVNT_OK & ~CRPT_EVENT,	"success",			0 },
212 	{ XEVNT_LEN & ~CRPT_EVENT,	"bad_field_format_or_length",	0 },
213 	{ XEVNT_TSP & ~CRPT_EVENT,	"bad_timestamp",		0 },
214 	{ XEVNT_FSP & ~CRPT_EVENT,	"bad_filestamp",		0 },
215 	{ XEVNT_PUB & ~CRPT_EVENT,	"bad_or_missing_public_key",	0 },
216 	{ XEVNT_MD & ~CRPT_EVENT,	"unsupported_digest_type",	0 },
217 	{ XEVNT_KEY & ~CRPT_EVENT,	"unsupported_identity_type",	0 },
218 	{ XEVNT_SGL & ~CRPT_EVENT,	"bad_signature_length",		0 },
219 	{ XEVNT_SIG & ~CRPT_EVENT,	"signature_not_verified",	0 },
220 	{ XEVNT_VFY & ~CRPT_EVENT,	"certificate_not_verified",	0 },
221 	{ XEVNT_PER & ~CRPT_EVENT,	"host_certificate_expired",	0 },
222 	{ XEVNT_CKY & ~CRPT_EVENT,	"bad_or_missing_cookie",	0 },
223 	{ XEVNT_DAT & ~CRPT_EVENT,	"bad_or_missing_leapseconds",	0 },
224 	{ XEVNT_CRT & ~CRPT_EVENT,	"bad_or_missing_certificate",	0 },
225 	{ XEVNT_ID & ~CRPT_EVENT,	"bad_or_missing_group key",	0 },
226 	{ XEVNT_ERR & ~CRPT_EVENT,	"protocol_error",		0 },
227 	{ -1,				"",				0 }
228 };
229 #endif	/* AUTOKEY */
230 
231 #ifdef KERNEL_PLL
232 /*
233  * kernel discipline status bits
234  */
235 static const struct codestring k_st_bits[] = {
236 # ifdef STA_PLL
237 	{ STA_PLL,			"pll",		0 },
238 # endif
239 # ifdef STA_PPSFREQ
240 	{ STA_PPSFREQ,			"ppsfreq",	0 },
241 # endif
242 # ifdef STA_PPSTIME
243 	{ STA_PPSTIME,			"ppstime",	0 },
244 # endif
245 # ifdef STA_FLL
246 	{ STA_FLL,			"fll",		0 },
247 # endif
248 # ifdef STA_INS
249 	{ STA_INS,			"ins",		0 },
250 # endif
251 # ifdef STA_DEL
252 	{ STA_DEL,			"del",		0 },
253 # endif
254 # ifdef STA_UNSYNC
255 	{ STA_UNSYNC,			"unsync",	0 },
256 # endif
257 # ifdef STA_FREQHOLD
258 	{ STA_FREQHOLD,			"freqhold",	0 },
259 # endif
260 # ifdef STA_PPSSIGNAL
261 	{ STA_PPSSIGNAL,		"ppssignal",	0 },
262 # endif
263 # ifdef STA_PPSJITTER
264 	{ STA_PPSJITTER,		"ppsjitter",	0 },
265 # endif
266 # ifdef STA_PPSWANDER
267 	{ STA_PPSWANDER,		"ppswander",	0 },
268 # endif
269 # ifdef STA_PPSERROR
270 	{ STA_PPSERROR,			"ppserror",	0 },
271 # endif
272 # ifdef STA_CLOCKERR
273 	{ STA_CLOCKERR,			"clockerr",	0 },
274 # endif
275 # ifdef STA_NANO
276 	{ STA_NANO,			"nano",		0 },
277 # endif
278 # ifdef STA_MODE
279 	{ STA_MODE,			"mode=fll",	0 },
280 # endif
281 # ifdef STA_CLK
282 	{ STA_CLK,			"src=B",	0 },
283 # endif
284 	/* not used with getcode(), no terminating entry needed */
285 };
286 #endif	/* KERNEL_PLL */
287 
288 /* Forwards */
289 static const char *	getcode(int, const struct codestring *);
290 static const char *	getevents(int);
291 static const char *	peer_st_flags(u_char pst);
292 
293 /*
294  * getcode - return string corresponding to code
295  */
296 static const char *
297 getcode(
298 	int				code,
299 	const struct codestring *	codetab
300 	)
301 {
302 	char *	buf;
303 
304 	while (codetab->code != -1) {
305 		if (codetab->code == code)
306 			return codetab->string1;
307 		codetab++;
308 	}
309 
310 	LIB_GETBUF(buf);
311 	snprintf(buf, LIB_BUFLENGTH, "%s_%d", codetab->string1, code);
312 
313 	return buf;
314 }
315 
316 /*
317  * getevents - return a descriptive string for the event count
318  */
319 static const char *
320 getevents(
321 	int cnt
322 	)
323 {
324 	char *	buf;
325 
326 	if (cnt == 0)
327 		return "no events";
328 
329 	LIB_GETBUF(buf);
330 	snprintf(buf, LIB_BUFLENGTH, "%d event%s", cnt,
331 		 (1 == cnt)
332 		     ? ""
333 		     : "s");
334 
335 	return buf;
336 }
337 
338 
339 /*
340  * decode_bitflags()
341  *
342  * returns a human-readable string with a keyword from tab for each bit
343  * set in bits, separating multiple entries with text of sep2.
344  */
345 static const char *
346 decode_bitflags(
347 	int				bits,
348 	const char *			sep2,
349 	const struct codestring *	tab,
350 	size_t				tab_ct
351 	)
352 {
353 	const char *	sep;
354 	char *		buf;
355 	char *		pch;
356 	char *		lim;
357 	size_t		b;
358 	int		rc;
359 	int		saved_errno;	/* for use in DPRINTF with %m */
360 
361 	saved_errno = errno;
362 	LIB_GETBUF(buf);
363 	pch = buf;
364 	lim = buf + LIB_BUFLENGTH;
365 	sep = "";
366 
367 	for (b = 0; b < tab_ct; b++) {
368 		const char * flagstr;
369 
370 		if (tab[b].code & bits) {
371 			flagstr = tab[b].string1;
372 		} else {
373 			flagstr = tab[b].string0;
374 		}
375 
376 		if (flagstr) {
377 			size_t avail = lim - pch;
378 			rc = snprintf(pch, avail, "%s%s", sep,
379 				      flagstr);
380 			if ((size_t)rc >= avail)
381 				goto toosmall;
382 			pch += rc;
383 			sep = sep2;
384 		}
385 	}
386 
387 	return buf;
388 
389     toosmall:
390 	snprintf(buf, LIB_BUFLENGTH,
391 		 "decode_bitflags(%s) can't decode 0x%x in %d bytes",
392 		 (tab == peer_st_bits)
393 		     ? "peer_st"
394 		     :
395 #ifdef KERNEL_PLL
396 		       (tab == k_st_bits)
397 			   ? "kern_st"
398 			   :
399 #endif
400 			     "",
401 		 bits, (int)LIB_BUFLENGTH);
402 	errno = saved_errno;
403 
404 	return buf;
405 }
406 
407 
408 static const char *
409 peer_st_flags(
410 	u_char pst
411 	)
412 {
413 	return decode_bitflags(pst, ", ", peer_st_bits,
414 			       COUNTOF(peer_st_bits));
415 }
416 
417 
418 const char *
419 res_match_flags(
420 	u_short mf
421 	)
422 {
423 	return decode_bitflags(mf, " ", res_match_bits,
424 			       COUNTOF(res_match_bits));
425 }
426 
427 
428 const char *
429 res_access_flags(
430 	u_int32 af
431 	)
432 {
433 	return decode_bitflags(af, " ", res_access_bits,
434 			       COUNTOF(res_access_bits));
435 }
436 
437 
438 #ifdef KERNEL_PLL
439 const char *
440 k_st_flags(
441 	u_int32 st
442 	)
443 {
444 	return decode_bitflags(st, " ", k_st_bits, COUNTOF(k_st_bits));
445 }
446 #endif	/* KERNEL_PLL */
447 
448 
449 /*
450  * statustoa - return a descriptive string for a peer status
451  */
452 char *
453 statustoa(
454 	int type,
455 	int st
456 	)
457 {
458 	char *	cb;
459 	char *	cc;
460 	u_char	pst;
461 
462 	LIB_GETBUF(cb);
463 
464 	switch (type) {
465 
466 	case TYPE_SYS:
467 		snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s, %s",
468 			 getcode(CTL_SYS_LI(st), leap_codes),
469 			 getcode(CTL_SYS_SOURCE(st), sync_codes),
470 			 getevents(CTL_SYS_NEVNT(st)),
471 			 getcode(CTL_SYS_EVENT(st), sys_codes));
472 		break;
473 
474 	case TYPE_PEER:
475 		pst = (u_char)CTL_PEER_STATVAL(st);
476 		snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s",
477 			 peer_st_flags(pst),
478 			 getcode(pst & 0x7, select_codes),
479 			 getevents(CTL_PEER_NEVNT(st)));
480 		if (CTL_PEER_EVENT(st) != EVNT_UNSPEC) {
481 			cc = cb + strlen(cb);
482 			snprintf(cc, LIB_BUFLENGTH - (cc - cb), ", %s",
483 				 getcode(CTL_PEER_EVENT(st),
484 					 peer_codes));
485 		}
486 		break;
487 
488 	case TYPE_CLOCK:
489 		snprintf(cb, LIB_BUFLENGTH, "%s, %s",
490 			 getevents(CTL_SYS_NEVNT(st)),
491 			 getcode((st) & 0xf, clock_codes));
492 		break;
493 	}
494 
495 	return cb;
496 }
497 
498 const char *
499 eventstr(
500 	int num
501 	)
502 {
503 	if (num & PEER_EVENT)
504 		return (getcode(num & ~PEER_EVENT, peer_codes));
505 #ifdef AUTOKEY
506 	else if (num & CRPT_EVENT)
507 		return (getcode(num & ~CRPT_EVENT, crypto_codes));
508 #endif	/* AUTOKEY */
509 	else
510 		return (getcode(num, sys_codes));
511 }
512 
513 const char *
514 ceventstr(
515 	int num
516 	)
517 {
518 	return getcode(num, clock_codes);
519 }
520