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