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