xref: /netbsd-src/external/bsd/tcpdump/dist/print-ntp.c (revision cc576e1d8e4f4078fd4e81238abca9fca216f6ec)
1 /*
2  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * Format and print ntp packets.
22  *	By Jeffrey Mogul/DECWRL
23  *	loosely based on print-bootp.c
24  */
25 
26 #include <sys/cdefs.h>
27 #ifndef lint
28 __RCSID("$NetBSD: print-ntp.c,v 1.6 2017/01/24 23:29:14 christos Exp $");
29 #endif
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <netdissect-stdinc.h>
36 
37 #ifdef HAVE_STRFTIME
38 #include <time.h>
39 #endif
40 
41 #include "netdissect.h"
42 #include "addrtoname.h"
43 #include "extract.h"
44 
45 /*
46  * Based on ntp.h from the U of MD implementation
47  *	This file is based on Version 2 of the NTP spec (RFC1119).
48  */
49 
50 /*
51  *  Definitions for the masses
52  */
53 #define	JAN_1970	2208988800U	/* 1970 - 1900 in seconds */
54 
55 /*
56  * Structure definitions for NTP fixed point values
57  *
58  *    0			  1		      2			  3
59  *    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
60  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61  *   |			       Integer Part			     |
62  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63  *   |			       Fraction Part			     |
64  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65  *
66  *    0			  1		      2			  3
67  *    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
68  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69  *   |		  Integer Part	     |	   Fraction Part	     |
70  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 */
72 struct l_fixedpt {
73 	uint32_t int_part;
74 	uint32_t fraction;
75 };
76 
77 struct s_fixedpt {
78 	uint16_t int_part;
79 	uint16_t fraction;
80 };
81 
82 /* rfc2030
83  *                      1                   2                   3
84  *  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
85  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86  * |LI | VN  |Mode |    Stratum    |     Poll      |   Precision   |
87  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88  * |                          Root Delay                           |
89  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90  * |                       Root Dispersion                         |
91  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92  * |                     Reference Identifier                      |
93  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94  * |                                                               |
95  * |                   Reference Timestamp (64)                    |
96  * |                                                               |
97  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98  * |                                                               |
99  * |                   Originate Timestamp (64)                    |
100  * |                                                               |
101  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102  * |                                                               |
103  * |                    Receive Timestamp (64)                     |
104  * |                                                               |
105  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106  * |                                                               |
107  * |                    Transmit Timestamp (64)                    |
108  * |                                                               |
109  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110  * |                 Key Identifier (optional) (32)                |
111  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112  * |                                                               |
113  * |                                                               |
114  * |                 Message Digest (optional) (128)               |
115  * |                                                               |
116  * |                                                               |
117  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118  */
119 
120 struct ntpdata {
121 	u_char status;		/* status of local clock and leap info */
122 	u_char stratum;		/* Stratum level */
123 	u_char ppoll;		/* poll value */
124 	int precision:8;
125 	struct s_fixedpt root_delay;
126 	struct s_fixedpt root_dispersion;
127 	uint32_t refid;
128 	struct l_fixedpt ref_timestamp;
129 	struct l_fixedpt org_timestamp;
130 	struct l_fixedpt rec_timestamp;
131 	struct l_fixedpt xmt_timestamp;
132         uint32_t key_id;
133         uint8_t  message_digest[16];
134 };
135 /*
136  *	Leap Second Codes (high order two bits)
137  */
138 #define	NO_WARNING	0x00	/* no warning */
139 #define	PLUS_SEC	0x40	/* add a second (61 seconds) */
140 #define	MINUS_SEC	0x80	/* minus a second (59 seconds) */
141 #define	ALARM		0xc0	/* alarm condition (clock unsynchronized) */
142 
143 /*
144  *	Clock Status Bits that Encode Version
145  */
146 #define	NTPVERSION_1	0x08
147 #define	VERSIONMASK	0x38
148 #define LEAPMASK	0xc0
149 #ifdef MODEMASK
150 #undef MODEMASK					/* Solaris sucks */
151 #endif
152 #define	MODEMASK	0x07
153 
154 /*
155  *	Code values
156  */
157 #define	MODE_UNSPEC	0	/* unspecified */
158 #define	MODE_SYM_ACT	1	/* symmetric active */
159 #define	MODE_SYM_PAS	2	/* symmetric passive */
160 #define	MODE_CLIENT	3	/* client */
161 #define	MODE_SERVER	4	/* server */
162 #define	MODE_BROADCAST	5	/* broadcast */
163 #define	MODE_RES1	6	/* reserved */
164 #define	MODE_RES2	7	/* reserved */
165 
166 /*
167  *	Stratum Definitions
168  */
169 #define	UNSPECIFIED	0
170 #define	PRIM_REF	1	/* radio clock */
171 #define	INFO_QUERY	62	/* **** THIS implementation dependent **** */
172 #define	INFO_REPLY	63	/* **** THIS implementation dependent **** */
173 
174 static void p_sfix(netdissect_options *ndo, const struct s_fixedpt *);
175 static void p_ntp_time(netdissect_options *, const struct l_fixedpt *);
176 static void p_ntp_delta(netdissect_options *, const struct l_fixedpt *, const struct l_fixedpt *);
177 
178 static const struct tok ntp_mode_values[] = {
179     { MODE_UNSPEC,    "unspecified" },
180     { MODE_SYM_ACT,   "symmetric active" },
181     { MODE_SYM_PAS,   "symmetric passive" },
182     { MODE_CLIENT,    "Client" },
183     { MODE_SERVER,    "Server" },
184     { MODE_BROADCAST, "Broadcast" },
185     { MODE_RES1,      "Reserved" },
186     { MODE_RES2,      "Reserved" },
187     { 0, NULL }
188 };
189 
190 static const struct tok ntp_leapind_values[] = {
191     { NO_WARNING,     "" },
192     { PLUS_SEC,       "+1s" },
193     { MINUS_SEC,      "-1s" },
194     { ALARM,          "clock unsynchronized" },
195     { 0, NULL }
196 };
197 
198 static const struct tok ntp_stratum_values[] = {
199 	{ UNSPECIFIED,	"unspecified" },
200 	{ PRIM_REF, 	"primary reference" },
201 	{ 0, NULL }
202 };
203 
204 /*
205  * Print ntp requests
206  */
207 void
208 ntp_print(netdissect_options *ndo,
209           register const u_char *cp, u_int length)
210 {
211 	register const struct ntpdata *bp;
212 	int mode, version, leapind;
213 
214 	bp = (const struct ntpdata *)cp;
215 
216 	ND_TCHECK(bp->status);
217 
218 	version = (int)(bp->status & VERSIONMASK) >> 3;
219 	ND_PRINT((ndo, "NTPv%d", version));
220 
221 	mode = bp->status & MODEMASK;
222 	if (!ndo->ndo_vflag) {
223 		ND_PRINT((ndo, ", %s, length %u",
224 		          tok2str(ntp_mode_values, "Unknown mode", mode),
225 		          length));
226 		return;
227 	}
228 
229 	ND_PRINT((ndo, ", length %u\n\t%s",
230 	          length,
231 	          tok2str(ntp_mode_values, "Unknown mode", mode)));
232 
233 	leapind = bp->status & LEAPMASK;
234 	ND_PRINT((ndo, ", Leap indicator: %s (%u)",
235 	          tok2str(ntp_leapind_values, "Unknown", leapind),
236 	          leapind));
237 
238 	ND_TCHECK(bp->stratum);
239 	ND_PRINT((ndo, ", Stratum %u (%s)",
240 		bp->stratum,
241 		tok2str(ntp_stratum_values, (bp->stratum >=2 && bp->stratum<=15) ? "secondary reference" : "reserved", bp->stratum)));
242 
243 	ND_TCHECK(bp->ppoll);
244 	ND_PRINT((ndo, ", poll %u (%us)", bp->ppoll, 1 << bp->ppoll));
245 
246 	/* Can't ND_TCHECK bp->precision bitfield so bp->distance + 0 instead */
247 	ND_TCHECK2(bp->root_delay, 0);
248 	ND_PRINT((ndo, ", precision %d", bp->precision));
249 
250 	ND_TCHECK(bp->root_delay);
251 	ND_PRINT((ndo, "\n\tRoot Delay: "));
252 	p_sfix(ndo, &bp->root_delay);
253 
254 	ND_TCHECK(bp->root_dispersion);
255 	ND_PRINT((ndo, ", Root dispersion: "));
256 	p_sfix(ndo, &bp->root_dispersion);
257 
258 	ND_TCHECK(bp->refid);
259 	ND_PRINT((ndo, ", Reference-ID: "));
260 	/* Interpretation depends on stratum */
261 	switch (bp->stratum) {
262 
263 	case UNSPECIFIED:
264 		ND_PRINT((ndo, "(unspec)"));
265 		break;
266 
267 	case PRIM_REF:
268 		if (fn_printn(ndo, (const u_char *)&(bp->refid), 4, ndo->ndo_snapend))
269 			goto trunc;
270 		break;
271 
272 	case INFO_QUERY:
273 		ND_PRINT((ndo, "%s INFO_QUERY", ipaddr_string(ndo, &(bp->refid))));
274 		/* this doesn't have more content */
275 		return;
276 
277 	case INFO_REPLY:
278 		ND_PRINT((ndo, "%s INFO_REPLY", ipaddr_string(ndo, &(bp->refid))));
279 		/* this is too complex to be worth printing */
280 		return;
281 
282 	default:
283 		ND_PRINT((ndo, "%s", ipaddr_string(ndo, &(bp->refid))));
284 		break;
285 	}
286 
287 	ND_TCHECK(bp->ref_timestamp);
288 	ND_PRINT((ndo, "\n\t  Reference Timestamp:  "));
289 	p_ntp_time(ndo, &(bp->ref_timestamp));
290 
291 	ND_TCHECK(bp->org_timestamp);
292 	ND_PRINT((ndo, "\n\t  Originator Timestamp: "));
293 	p_ntp_time(ndo, &(bp->org_timestamp));
294 
295 	ND_TCHECK(bp->rec_timestamp);
296 	ND_PRINT((ndo, "\n\t  Receive Timestamp:    "));
297 	p_ntp_time(ndo, &(bp->rec_timestamp));
298 
299 	ND_TCHECK(bp->xmt_timestamp);
300 	ND_PRINT((ndo, "\n\t  Transmit Timestamp:   "));
301 	p_ntp_time(ndo, &(bp->xmt_timestamp));
302 
303 	ND_PRINT((ndo, "\n\t    Originator - Receive Timestamp:  "));
304 	p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->rec_timestamp));
305 
306 	ND_PRINT((ndo, "\n\t    Originator - Transmit Timestamp: "));
307 	p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->xmt_timestamp));
308 
309 	if ( (sizeof(struct ntpdata) - length) == 16) { 	/* Optional: key-id */
310 		ND_TCHECK(bp->key_id);
311 		ND_PRINT((ndo, "\n\tKey id: %u", bp->key_id));
312 	} else if ( (sizeof(struct ntpdata) - length) == 0) { 	/* Optional: key-id + authentication */
313 		ND_TCHECK(bp->key_id);
314 		ND_PRINT((ndo, "\n\tKey id: %u", bp->key_id));
315 		ND_TCHECK2(bp->message_digest, sizeof (bp->message_digest));
316                 ND_PRINT((ndo, "\n\tAuthentication: %08x%08x%08x%08x",
317         		       EXTRACT_32BITS(bp->message_digest),
318 		               EXTRACT_32BITS(bp->message_digest + 4),
319 		               EXTRACT_32BITS(bp->message_digest + 8),
320 		               EXTRACT_32BITS(bp->message_digest + 12)));
321         }
322 	return;
323 
324 trunc:
325 	ND_PRINT((ndo, " [|ntp]"));
326 }
327 
328 static void
329 p_sfix(netdissect_options *ndo,
330        register const struct s_fixedpt *sfp)
331 {
332 	register int i;
333 	register int f;
334 	register double ff;
335 
336 	i = EXTRACT_16BITS(&sfp->int_part);
337 	f = EXTRACT_16BITS(&sfp->fraction);
338 	ff = f / 65536.0;		/* shift radix point by 16 bits */
339 	f = (int)(ff * 1000000.0);	/* Treat fraction as parts per million */
340 	ND_PRINT((ndo, "%d.%06d", i, f));
341 }
342 
343 #define	FMAXINT	(4294967296.0)	/* floating point rep. of MAXINT */
344 
345 static void
346 p_ntp_time(netdissect_options *ndo,
347            register const struct l_fixedpt *lfp)
348 {
349 	register int32_t i;
350 	register uint32_t uf;
351 	register uint32_t f;
352 	register double ff;
353 
354 	i = EXTRACT_32BITS(&lfp->int_part);
355 	uf = EXTRACT_32BITS(&lfp->fraction);
356 	ff = uf;
357 	if (ff < 0.0)		/* some compilers are buggy */
358 		ff += FMAXINT;
359 	ff = ff / FMAXINT;			/* shift radix point by 32 bits */
360 	f = (uint32_t)(ff * 1000000000.0);	/* treat fraction as parts per billion */
361 	ND_PRINT((ndo, "%u.%09d", i, f));
362 
363 #ifdef HAVE_STRFTIME
364 	/*
365 	 * print the time in human-readable format.
366 	 */
367 	if (i) {
368 	    time_t seconds = i - JAN_1970;
369 	    struct tm *tm;
370 	    char time_buf[128];
371 
372 	    tm = localtime(&seconds);
373 	    strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
374 	    ND_PRINT((ndo, " (%s)", time_buf));
375 	}
376 #endif
377 }
378 
379 /* Prints time difference between *lfp and *olfp */
380 static void
381 p_ntp_delta(netdissect_options *ndo,
382             register const struct l_fixedpt *olfp,
383             register const struct l_fixedpt *lfp)
384 {
385 	register int32_t i;
386 	register uint32_t u, uf;
387 	register uint32_t ou, ouf;
388 	register uint32_t f;
389 	register double ff;
390 	int signbit;
391 
392 	u = EXTRACT_32BITS(&lfp->int_part);
393 	ou = EXTRACT_32BITS(&olfp->int_part);
394 	uf = EXTRACT_32BITS(&lfp->fraction);
395 	ouf = EXTRACT_32BITS(&olfp->fraction);
396 	if (ou == 0 && ouf == 0) {
397 		p_ntp_time(ndo, lfp);
398 		return;
399 	}
400 
401 	i = u - ou;
402 
403 	if (i > 0) {		/* new is definitely greater than old */
404 		signbit = 0;
405 		f = uf - ouf;
406 		if (ouf > uf)	/* must borrow from high-order bits */
407 			i -= 1;
408 	} else if (i < 0) {	/* new is definitely less than old */
409 		signbit = 1;
410 		f = ouf - uf;
411 		if (uf > ouf)	/* must carry into the high-order bits */
412 			i += 1;
413 		i = -i;
414 	} else {		/* int_part is zero */
415 		if (uf > ouf) {
416 			signbit = 0;
417 			f = uf - ouf;
418 		} else {
419 			signbit = 1;
420 			f = ouf - uf;
421 		}
422 	}
423 
424 	ff = f;
425 	if (ff < 0.0)		/* some compilers are buggy */
426 		ff += FMAXINT;
427 	ff = ff / FMAXINT;			/* shift radix point by 32 bits */
428 	f = (uint32_t)(ff * 1000000000.0);	/* treat fraction as parts per billion */
429 	ND_PRINT((ndo, "%s%d.%09d", signbit ? "-" : "+", i, f));
430 }
431 
432