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#if 0
29static const char rcsid[] _U_ =
30    "@(#) Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.43 2007-11-30 13:45:10 hannes Exp (LBL)";
31#else
32__RCSID("$NetBSD: print-ntp.c,v 1.2 2010/12/05 05:11:30 christos Exp $");
33#endif
34#endif
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40#include <tcpdump-stdinc.h>
41
42#include <stdio.h>
43#include <string.h>
44#ifdef HAVE_STRFTIME
45#include <time.h>
46#endif
47
48#include "interface.h"
49#include "addrtoname.h"
50#include "extract.h"
51#ifdef MODEMASK
52#undef MODEMASK					/* Solaris sucks */
53#endif
54#include "ntp.h"
55
56static void p_sfix(const struct s_fixedpt *);
57static void p_ntp_time(const struct l_fixedpt *);
58static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
59
60static struct tok ntp_mode_values[] = {
61    { MODE_UNSPEC,    "unspecified" },
62    { MODE_SYM_ACT,   "symmetric active" },
63    { MODE_SYM_PAS,   "symmetric passive" },
64    { MODE_CLIENT,    "Client" },
65    { MODE_SERVER,    "Server" },
66    { MODE_BROADCAST, "Broadcast" },
67    { MODE_RES1,      "Reserved" },
68    { MODE_RES2,      "Reserved" },
69    { 0, NULL }
70};
71
72static struct tok ntp_leapind_values[] = {
73    { NO_WARNING,     "" },
74    { PLUS_SEC,       "+1s" },
75    { MINUS_SEC,      "-1s" },
76    { ALARM,          "clock unsynchronized" },
77    { 0, NULL }
78};
79
80static struct tok ntp_stratum_values[] = {
81	{ UNSPECIFIED,	"unspecified" },
82	{ PRIM_REF, 	"primary reference" },
83	{ 0, NULL }
84};
85
86/*
87 * Print ntp requests
88 */
89void
90ntp_print(register const u_char *cp, u_int length)
91{
92	register const struct ntpdata *bp;
93	int mode, version, leapind;
94
95	bp = (struct ntpdata *)cp;
96
97	TCHECK(bp->status);
98
99	version = (int)(bp->status & VERSIONMASK) >> 3;
100	printf("NTPv%d", version);
101
102	mode = bp->status & MODEMASK;
103        if (!vflag) {
104            printf (", %s, length %u",
105                    tok2str(ntp_mode_values, "Unknown mode", mode),
106                    length);
107            return;
108        }
109
110        printf (", length %u\n\t%s",
111                length,
112                tok2str(ntp_mode_values, "Unknown mode", mode));
113
114	leapind = bp->status & LEAPMASK;
115        printf (", Leap indicator: %s (%u)",
116                tok2str(ntp_leapind_values, "Unknown", leapind),
117                leapind);
118
119	TCHECK(bp->stratum);
120	printf(", Stratum %u (%s)",
121		bp->stratum,
122		tok2str(ntp_stratum_values, (bp->stratum >=2 && bp->stratum<=15) ? "secondary reference" : "reserved", bp->stratum));
123
124	TCHECK(bp->ppoll);
125	printf(", poll %us", bp->ppoll);
126
127	/* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
128	TCHECK2(bp->root_delay, 0);
129	printf(", precision %d", bp->precision);
130
131	TCHECK(bp->root_delay);
132	fputs("\n\tRoot Delay: ", stdout);
133	p_sfix(&bp->root_delay);
134
135	TCHECK(bp->root_dispersion);
136	fputs(", Root dispersion: ", stdout);
137	p_sfix(&bp->root_dispersion);
138
139	TCHECK(bp->refid);
140	fputs(", Reference-ID: ", stdout);
141	/* Interpretation depends on stratum */
142	switch (bp->stratum) {
143
144	case UNSPECIFIED:
145		printf("(unspec)");
146		break;
147
148	case PRIM_REF:
149		if (fn_printn((u_char *)&(bp->refid), 4, snapend))
150			goto trunc;
151		break;
152
153	case INFO_QUERY:
154		printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
155		/* this doesn't have more content */
156		return;
157
158	case INFO_REPLY:
159		printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
160		/* this is too complex to be worth printing */
161		return;
162
163	default:
164		printf("%s", ipaddr_string(&(bp->refid)));
165		break;
166	}
167
168	TCHECK(bp->ref_timestamp);
169	fputs("\n\t  Reference Timestamp:  ", stdout);
170	p_ntp_time(&(bp->ref_timestamp));
171
172	TCHECK(bp->org_timestamp);
173	fputs("\n\t  Originator Timestamp: ", stdout);
174	p_ntp_time(&(bp->org_timestamp));
175
176	TCHECK(bp->rec_timestamp);
177	fputs("\n\t  Receive Timestamp:    ", stdout);
178	p_ntp_time(&(bp->rec_timestamp));
179
180	TCHECK(bp->xmt_timestamp);
181	fputs("\n\t  Transmit Timestamp:   ", stdout);
182	p_ntp_time(&(bp->xmt_timestamp));
183
184	fputs("\n\t    Originator - Receive Timestamp:  ", stdout);
185	p_ntp_delta(&(bp->org_timestamp), &(bp->rec_timestamp));
186
187	fputs("\n\t    Originator - Transmit Timestamp: ", stdout);
188	p_ntp_delta(&(bp->org_timestamp), &(bp->xmt_timestamp));
189
190	if ( (sizeof(struct ntpdata) - length) == 16) { 	/* Optional: key-id */
191		TCHECK(bp->key_id);
192		printf("\n\tKey id: %u", bp->key_id);
193	} else if ( (sizeof(struct ntpdata) - length) == 0) { 	/* Optional: key-id + authentication */
194		TCHECK(bp->key_id);
195		printf("\n\tKey id: %u", bp->key_id);
196		TCHECK2(bp->message_digest, sizeof (bp->message_digest));
197                printf("\n\tAuthentication: %08x%08x%08x%08x",
198        		       EXTRACT_32BITS(bp->message_digest),
199		               EXTRACT_32BITS(bp->message_digest + 4),
200		               EXTRACT_32BITS(bp->message_digest + 8),
201		               EXTRACT_32BITS(bp->message_digest + 12));
202        }
203	return;
204
205trunc:
206	fputs(" [|ntp]", stdout);
207}
208
209static void
210p_sfix(register const struct s_fixedpt *sfp)
211{
212	register int i;
213	register int f;
214	register float ff;
215
216	i = EXTRACT_16BITS(&sfp->int_part);
217	f = EXTRACT_16BITS(&sfp->fraction);
218	ff = f / 65536.0;	/* shift radix point by 16 bits */
219	f = ff * 1000000.0;	/* Treat fraction as parts per million */
220	printf("%d.%06d", i, f);
221}
222
223#define	FMAXINT	(4294967296.0)	/* floating point rep. of MAXINT */
224
225static void
226p_ntp_time(register const struct l_fixedpt *lfp)
227{
228	register int32_t i;
229	register u_int32_t uf;
230	register u_int32_t f;
231	register float ff;
232
233	i = EXTRACT_32BITS(&lfp->int_part);
234	uf = EXTRACT_32BITS(&lfp->fraction);
235	ff = uf;
236	if (ff < 0.0)		/* some compilers are buggy */
237		ff += FMAXINT;
238	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
239	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
240	printf("%u.%09d", i, f);
241
242#ifdef HAVE_STRFTIME
243	/*
244	 * print the time in human-readable format.
245	 */
246	if (i) {
247	    time_t seconds = i - JAN_1970;
248	    struct tm *tm;
249	    char time_buf[128];
250
251	    tm = localtime(&seconds);
252	    strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
253	    printf (" (%s)", time_buf);
254	}
255#endif
256}
257
258/* Prints time difference between *lfp and *olfp */
259static void
260p_ntp_delta(register const struct l_fixedpt *olfp,
261	    register const struct l_fixedpt *lfp)
262{
263	register int32_t i;
264	register u_int32_t u, uf;
265	register u_int32_t ou, ouf;
266	register u_int32_t f;
267	register float ff;
268	int signbit;
269
270	u = EXTRACT_32BITS(&lfp->int_part);
271	ou = EXTRACT_32BITS(&olfp->int_part);
272	uf = EXTRACT_32BITS(&lfp->fraction);
273	ouf = EXTRACT_32BITS(&olfp->fraction);
274	if (ou == 0 && ouf == 0) {
275		p_ntp_time(lfp);
276		return;
277	}
278
279	i = u - ou;
280
281	if (i > 0) {		/* new is definitely greater than old */
282		signbit = 0;
283		f = uf - ouf;
284		if (ouf > uf)	/* must borrow from high-order bits */
285			i -= 1;
286	} else if (i < 0) {	/* new is definitely less than old */
287		signbit = 1;
288		f = ouf - uf;
289		if (uf > ouf)	/* must carry into the high-order bits */
290			i += 1;
291		i = -i;
292	} else {		/* int_part is zero */
293		if (uf > ouf) {
294			signbit = 0;
295			f = uf - ouf;
296		} else {
297			signbit = 1;
298			f = ouf - uf;
299		}
300	}
301
302	ff = f;
303	if (ff < 0.0)		/* some compilers are buggy */
304		ff += FMAXINT;
305	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
306	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
307	if (signbit)
308		putchar('-');
309	else
310		putchar('+');
311	printf("%d.%09d", i, f);
312}
313
314