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