xref: /dflybsd-src/contrib/tcpdump/print-ntp.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
341c99275SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos  * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos  * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos  * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos  * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos  * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos  * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos  * or promote products derived from this software without specific prior
1641c99275SPeter Avalos  * written permission.
1741c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos  *
2141c99275SPeter Avalos  *	By Jeffrey Mogul/DECWRL
2241c99275SPeter Avalos  *	loosely based on print-bootp.c
2341c99275SPeter Avalos  */
2441c99275SPeter Avalos 
25411677aeSAaron LI /* \summary: Network Time Protocol (NTP) printer */
2641c99275SPeter Avalos 
27*ed775ee7SAntonio Huete Jimenez /*
28*ed775ee7SAntonio Huete Jimenez  * specification:
29*ed775ee7SAntonio Huete Jimenez  *
30*ed775ee7SAntonio Huete Jimenez  * RFC 1119 - NTPv2
31*ed775ee7SAntonio Huete Jimenez  * RFC 1305 - NTPv3
32*ed775ee7SAntonio Huete Jimenez  * RFC 5905 - NTPv4
33*ed775ee7SAntonio Huete Jimenez  */
34*ed775ee7SAntonio Huete Jimenez 
3541c99275SPeter Avalos #ifdef HAVE_CONFIG_H
36*ed775ee7SAntonio Huete Jimenez #include <config.h>
3741c99275SPeter Avalos #endif
3841c99275SPeter Avalos 
39*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
4041c99275SPeter Avalos 
4141c99275SPeter Avalos #ifdef HAVE_STRFTIME
4241c99275SPeter Avalos #include <time.h>
4341c99275SPeter Avalos #endif
4441c99275SPeter Avalos 
45411677aeSAaron LI #include "netdissect.h"
4641c99275SPeter Avalos #include "addrtoname.h"
4741c99275SPeter Avalos #include "extract.h"
48411677aeSAaron LI 
49*ed775ee7SAntonio Huete Jimenez #include "ntp.h"
50*ed775ee7SAntonio Huete Jimenez 
51411677aeSAaron LI /*
52411677aeSAaron LI  * Based on ntp.h from the U of MD implementation
53411677aeSAaron LI  *	This file is based on Version 2 of the NTP spec (RFC1119).
54411677aeSAaron LI  */
55411677aeSAaron LI 
56411677aeSAaron LI /* rfc2030
57411677aeSAaron LI  *                      1                   2                   3
58411677aeSAaron LI  *  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
59411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60411677aeSAaron LI  * |LI | VN  |Mode |    Stratum    |     Poll      |   Precision   |
61411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62411677aeSAaron LI  * |                          Root Delay                           |
63411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64411677aeSAaron LI  * |                       Root Dispersion                         |
65411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66411677aeSAaron LI  * |                     Reference Identifier                      |
67411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68411677aeSAaron LI  * |                                                               |
69411677aeSAaron LI  * |                   Reference Timestamp (64)                    |
70411677aeSAaron LI  * |                                                               |
71411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72411677aeSAaron LI  * |                                                               |
73411677aeSAaron LI  * |                   Originate Timestamp (64)                    |
74411677aeSAaron LI  * |                                                               |
75411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76411677aeSAaron LI  * |                                                               |
77411677aeSAaron LI  * |                    Receive Timestamp (64)                     |
78411677aeSAaron LI  * |                                                               |
79411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80411677aeSAaron LI  * |                                                               |
81411677aeSAaron LI  * |                    Transmit Timestamp (64)                    |
82411677aeSAaron LI  * |                                                               |
83411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84411677aeSAaron LI  * |                 Key Identifier (optional) (32)                |
85411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86411677aeSAaron LI  * |                                                               |
87411677aeSAaron LI  * |                                                               |
88411677aeSAaron LI  * |                 Message Digest (optional) (128)               |
89411677aeSAaron LI  * |                                                               |
90411677aeSAaron LI  * |                                                               |
91411677aeSAaron LI  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92411677aeSAaron LI  */
93411677aeSAaron LI 
94*ed775ee7SAntonio Huete Jimenez /* Length of the NTP data message with the mandatory fields ("the header")
95*ed775ee7SAntonio Huete Jimenez  * and without any optional fields (extension, Key Identifier,
96*ed775ee7SAntonio Huete Jimenez  * Message Digest).
97*ed775ee7SAntonio Huete Jimenez  */
98*ed775ee7SAntonio Huete Jimenez #define NTP_TIMEMSG_MINLEN 48U
99*ed775ee7SAntonio Huete Jimenez 
100*ed775ee7SAntonio Huete Jimenez struct ntp_time_data {
101*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t status;		/* status of local clock and leap info */
102*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t stratum;		/* Stratum level */
103*ed775ee7SAntonio Huete Jimenez 	nd_int8_t ppoll;		/* poll value */
104*ed775ee7SAntonio Huete Jimenez 	nd_int8_t precision;
105411677aeSAaron LI 	struct s_fixedpt root_delay;
106411677aeSAaron LI 	struct s_fixedpt root_dispersion;
107*ed775ee7SAntonio Huete Jimenez 	nd_uint32_t refid;
108411677aeSAaron LI 	struct l_fixedpt ref_timestamp;
109411677aeSAaron LI 	struct l_fixedpt org_timestamp;
110411677aeSAaron LI 	struct l_fixedpt rec_timestamp;
111411677aeSAaron LI 	struct l_fixedpt xmt_timestamp;
112*ed775ee7SAntonio Huete Jimenez 	nd_uint32_t key_id;
113*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t  message_digest[20];
114411677aeSAaron LI };
115411677aeSAaron LI /*
116411677aeSAaron LI  *	Leap Second Codes (high order two bits)
117411677aeSAaron LI  */
118411677aeSAaron LI #define	NO_WARNING	0x00	/* no warning */
119411677aeSAaron LI #define	PLUS_SEC	0x40	/* add a second (61 seconds) */
120411677aeSAaron LI #define	MINUS_SEC	0x80	/* minus a second (59 seconds) */
121411677aeSAaron LI #define	ALARM		0xc0	/* alarm condition (clock unsynchronized) */
122411677aeSAaron LI 
123411677aeSAaron LI /*
124411677aeSAaron LI  *	Clock Status Bits that Encode Version
125411677aeSAaron LI  */
126411677aeSAaron LI #define	NTPVERSION_1	0x08
127411677aeSAaron LI #define	VERSIONMASK	0x38
128*ed775ee7SAntonio Huete Jimenez #define	VERSIONSHIFT	3
129411677aeSAaron LI #define LEAPMASK	0xc0
130*ed775ee7SAntonio Huete Jimenez #define LEAPSHIFT	6
13141c99275SPeter Avalos #ifdef MODEMASK
13241c99275SPeter Avalos #undef MODEMASK					/* Solaris sucks */
13341c99275SPeter Avalos #endif
134411677aeSAaron LI #define	MODEMASK	0x07
135*ed775ee7SAntonio Huete Jimenez #define	MODESHIFT	0
13641c99275SPeter Avalos 
137411677aeSAaron LI /*
138411677aeSAaron LI  *	Code values
139411677aeSAaron LI  */
140411677aeSAaron LI #define	MODE_UNSPEC	0	/* unspecified */
141411677aeSAaron LI #define	MODE_SYM_ACT	1	/* symmetric active */
142411677aeSAaron LI #define	MODE_SYM_PAS	2	/* symmetric passive */
143411677aeSAaron LI #define	MODE_CLIENT	3	/* client */
144411677aeSAaron LI #define	MODE_SERVER	4	/* server */
145411677aeSAaron LI #define	MODE_BROADCAST	5	/* broadcast */
146*ed775ee7SAntonio Huete Jimenez #define	MODE_CONTROL	6	/* control message */
147411677aeSAaron LI #define	MODE_RES2	7	/* reserved */
14841c99275SPeter Avalos 
149411677aeSAaron LI /*
150411677aeSAaron LI  *	Stratum Definitions
151411677aeSAaron LI  */
152411677aeSAaron LI #define	UNSPECIFIED	0
153411677aeSAaron LI #define	PRIM_REF	1	/* radio clock */
154411677aeSAaron LI #define	INFO_QUERY	62	/* **** THIS implementation dependent **** */
155411677aeSAaron LI #define	INFO_REPLY	63	/* **** THIS implementation dependent **** */
156411677aeSAaron LI 
157411677aeSAaron LI static void p_sfix(netdissect_options *ndo, const struct s_fixedpt *);
158411677aeSAaron LI static void p_ntp_delta(netdissect_options *, const struct l_fixedpt *, const struct l_fixedpt *);
159*ed775ee7SAntonio Huete Jimenez static void p_poll(netdissect_options *, const int);
160411677aeSAaron LI 
161411677aeSAaron LI static const struct tok ntp_mode_values[] = {
16241c99275SPeter Avalos     { MODE_UNSPEC,    "unspecified" },
16341c99275SPeter Avalos     { MODE_SYM_ACT,   "symmetric active" },
16441c99275SPeter Avalos     { MODE_SYM_PAS,   "symmetric passive" },
16541c99275SPeter Avalos     { MODE_CLIENT,    "Client" },
16641c99275SPeter Avalos     { MODE_SERVER,    "Server" },
16741c99275SPeter Avalos     { MODE_BROADCAST, "Broadcast" },
168*ed775ee7SAntonio Huete Jimenez     { MODE_CONTROL,   "Control Message" },
16941c99275SPeter Avalos     { MODE_RES2,      "Reserved" },
17041c99275SPeter Avalos     { 0, NULL }
17141c99275SPeter Avalos };
17241c99275SPeter Avalos 
173411677aeSAaron LI static const struct tok ntp_leapind_values[] = {
17441c99275SPeter Avalos     { NO_WARNING,     "" },
17541c99275SPeter Avalos     { PLUS_SEC,       "+1s" },
17641c99275SPeter Avalos     { MINUS_SEC,      "-1s" },
17741c99275SPeter Avalos     { ALARM,          "clock unsynchronized" },
17841c99275SPeter Avalos     { 0, NULL }
17941c99275SPeter Avalos };
18041c99275SPeter Avalos 
181411677aeSAaron LI static const struct tok ntp_stratum_values[] = {
18227bfbee1SPeter Avalos 	{ UNSPECIFIED,	"unspecified" },
18327bfbee1SPeter Avalos 	{ PRIM_REF, 	"primary reference" },
18427bfbee1SPeter Avalos 	{ 0, NULL }
18527bfbee1SPeter Avalos };
18627bfbee1SPeter Avalos 
187*ed775ee7SAntonio Huete Jimenez /* draft-ietf-ntp-mode-6-cmds-02
188*ed775ee7SAntonio Huete Jimenez  *  0                   1                   2                   3
189*ed775ee7SAntonio Huete Jimenez  *  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
190*ed775ee7SAntonio Huete Jimenez  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
191*ed775ee7SAntonio Huete Jimenez  * |LI |  VN |Mode |R|E|M| OpCode  |       Sequence Number         |
192*ed775ee7SAntonio Huete Jimenez  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
193*ed775ee7SAntonio Huete Jimenez  * |            Status             |       Association ID          |
194*ed775ee7SAntonio Huete Jimenez  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
195*ed775ee7SAntonio Huete Jimenez  * |            Offset             |            Count              |
196*ed775ee7SAntonio Huete Jimenez  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
197*ed775ee7SAntonio Huete Jimenez  * |                                                               |
198*ed775ee7SAntonio Huete Jimenez  * /                    Data (up to 468 bytes)                     /
199*ed775ee7SAntonio Huete Jimenez  * |                                                               |
200*ed775ee7SAntonio Huete Jimenez  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201*ed775ee7SAntonio Huete Jimenez  * |                    Padding (optional)                         |
202*ed775ee7SAntonio Huete Jimenez  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203*ed775ee7SAntonio Huete Jimenez  * |                                                               |
204*ed775ee7SAntonio Huete Jimenez  * /              Authenticator (optional, 96 bytes)               /
205*ed775ee7SAntonio Huete Jimenez  * |                                                               |
206*ed775ee7SAntonio Huete Jimenez  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207*ed775ee7SAntonio Huete Jimenez  *
208*ed775ee7SAntonio Huete Jimenez  *               Figure 1: NTP Control Message Header
20941c99275SPeter Avalos  */
210*ed775ee7SAntonio Huete Jimenez 
211*ed775ee7SAntonio Huete Jimenez /* Length of the NTP control message with the mandatory fields ("the header")
212*ed775ee7SAntonio Huete Jimenez  * and without any optional fields (Data, Padding, Authenticator).
213*ed775ee7SAntonio Huete Jimenez  */
214*ed775ee7SAntonio Huete Jimenez #define NTP_CTRLMSG_MINLEN 12U
215*ed775ee7SAntonio Huete Jimenez 
216*ed775ee7SAntonio Huete Jimenez struct ntp_control_data {
217*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t	magic;		/* LI, VN, Mode */
218*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t	control;	/* R, E, M, OpCode */
219*ed775ee7SAntonio Huete Jimenez 	nd_uint16_t	sequence;	/* Sequence Number */
220*ed775ee7SAntonio Huete Jimenez 	nd_uint16_t	status;		/* Status */
221*ed775ee7SAntonio Huete Jimenez 	nd_uint16_t	assoc;		/* Association ID */
222*ed775ee7SAntonio Huete Jimenez 	nd_uint16_t	offset;		/* Offset */
223*ed775ee7SAntonio Huete Jimenez 	nd_uint16_t	count;		/* Count */
224*ed775ee7SAntonio Huete Jimenez 	nd_uint8_t	data[564];	/* Data, [Padding, [Authenticator]] */
225*ed775ee7SAntonio Huete Jimenez };
226*ed775ee7SAntonio Huete Jimenez 
227*ed775ee7SAntonio Huete Jimenez /*
228*ed775ee7SAntonio Huete Jimenez  * Print NTP time requests and responses
229*ed775ee7SAntonio Huete Jimenez  */
230*ed775ee7SAntonio Huete Jimenez static void
ntp_time_print(netdissect_options * ndo,const struct ntp_time_data * bp,u_int length)231*ed775ee7SAntonio Huete Jimenez ntp_time_print(netdissect_options *ndo,
232*ed775ee7SAntonio Huete Jimenez 	       const struct ntp_time_data *bp, u_int length)
23341c99275SPeter Avalos {
234*ed775ee7SAntonio Huete Jimenez 	uint8_t stratum;
23541c99275SPeter Avalos 
236*ed775ee7SAntonio Huete Jimenez 	if (length < NTP_TIMEMSG_MINLEN)
237*ed775ee7SAntonio Huete Jimenez 		goto invalid;
23841c99275SPeter Avalos 
239*ed775ee7SAntonio Huete Jimenez 	stratum = GET_U_1(bp->stratum);
240*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", Stratum %u (%s)",
241*ed775ee7SAntonio Huete Jimenez 		stratum,
242*ed775ee7SAntonio Huete Jimenez 		tok2str(ntp_stratum_values, (stratum >=2 && stratum<=15) ? "secondary reference" : "reserved", stratum));
24341c99275SPeter Avalos 
244*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", poll %d", GET_S_1(bp->ppoll));
245*ed775ee7SAntonio Huete Jimenez 	p_poll(ndo, GET_S_1(bp->ppoll));
24641c99275SPeter Avalos 
247*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", precision %d", GET_S_1(bp->precision));
24841c99275SPeter Avalos 
249*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(&bp->root_delay);
250*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\tRoot Delay: ");
251411677aeSAaron LI 	p_sfix(ndo, &bp->root_delay);
25241c99275SPeter Avalos 
253*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(&bp->root_dispersion);
254*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", Root dispersion: ");
255411677aeSAaron LI 	p_sfix(ndo, &bp->root_dispersion);
25641c99275SPeter Avalos 
257*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_4(bp->refid);
258*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", Reference-ID: ");
25941c99275SPeter Avalos 	/* Interpretation depends on stratum */
260*ed775ee7SAntonio Huete Jimenez 	switch (stratum) {
26141c99275SPeter Avalos 
26241c99275SPeter Avalos 	case UNSPECIFIED:
263*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("(unspec)");
26441c99275SPeter Avalos 		break;
26541c99275SPeter Avalos 
26641c99275SPeter Avalos 	case PRIM_REF:
267*ed775ee7SAntonio Huete Jimenez 		if (nd_printn(ndo, (const u_char *)&(bp->refid), 4, ndo->ndo_snapend))
26841c99275SPeter Avalos 			goto trunc;
26941c99275SPeter Avalos 		break;
27041c99275SPeter Avalos 
27141c99275SPeter Avalos 	case INFO_QUERY:
272*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s INFO_QUERY", GET_IPADDR_STRING(bp->refid));
27341c99275SPeter Avalos 		/* this doesn't have more content */
27441c99275SPeter Avalos 		return;
27541c99275SPeter Avalos 
27641c99275SPeter Avalos 	case INFO_REPLY:
277*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s INFO_REPLY", GET_IPADDR_STRING(bp->refid));
27841c99275SPeter Avalos 		/* this is too complex to be worth printing */
27941c99275SPeter Avalos 		return;
28041c99275SPeter Avalos 
28141c99275SPeter Avalos 	default:
282*ed775ee7SAntonio Huete Jimenez 		/* In NTPv4 (RFC 5905) refid is an IPv4 address or first 32 bits of
283*ed775ee7SAntonio Huete Jimenez 		   MD5 sum of IPv6 address */
284*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("0x%08x", GET_BE_U_4(bp->refid));
28541c99275SPeter Avalos 		break;
28641c99275SPeter Avalos 	}
28741c99275SPeter Avalos 
288*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(&bp->ref_timestamp);
289*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t  Reference Timestamp:  ");
290411677aeSAaron LI 	p_ntp_time(ndo, &(bp->ref_timestamp));
29141c99275SPeter Avalos 
292*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(&bp->org_timestamp);
293*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t  Originator Timestamp: ");
294411677aeSAaron LI 	p_ntp_time(ndo, &(bp->org_timestamp));
29541c99275SPeter Avalos 
296*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(&bp->rec_timestamp);
297*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t  Receive Timestamp:    ");
298411677aeSAaron LI 	p_ntp_time(ndo, &(bp->rec_timestamp));
29941c99275SPeter Avalos 
300*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_SIZE(&bp->xmt_timestamp);
301*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t  Transmit Timestamp:   ");
302411677aeSAaron LI 	p_ntp_time(ndo, &(bp->xmt_timestamp));
30341c99275SPeter Avalos 
304*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t    Originator - Receive Timestamp:  ");
305411677aeSAaron LI 	p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->rec_timestamp));
30641c99275SPeter Avalos 
307*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n\t    Originator - Transmit Timestamp: ");
308411677aeSAaron LI 	p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->xmt_timestamp));
30941c99275SPeter Avalos 
310*ed775ee7SAntonio Huete Jimenez 	/* FIXME: this code is not aware of any extension fields */
311*ed775ee7SAntonio Huete Jimenez 	if (length == NTP_TIMEMSG_MINLEN + 4) { 	/* Optional: key-id (crypto-NAK) */
312*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
313*ed775ee7SAntonio Huete Jimenez 	} else if (length == NTP_TIMEMSG_MINLEN + 4 + 16) { 	/* Optional: key-id + 128-bit digest */
314*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
315*ed775ee7SAntonio Huete Jimenez 		ND_TCHECK_LEN(bp->message_digest, 16);
316*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x",
317*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest),
318*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest + 4),
319*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest + 8),
320*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest + 12));
321*ed775ee7SAntonio Huete Jimenez 	} else if (length == NTP_TIMEMSG_MINLEN + 4 + 20) { 	/* Optional: key-id + 160-bit digest */
322*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
323*ed775ee7SAntonio Huete Jimenez 		ND_TCHECK_LEN(bp->message_digest, 20);
324*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x%08x",
325*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest),
326*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest + 4),
327*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest + 8),
328*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest + 12),
329*ed775ee7SAntonio Huete Jimenez 			 GET_BE_U_4(bp->message_digest + 16));
330*ed775ee7SAntonio Huete Jimenez 	} else if (length > NTP_TIMEMSG_MINLEN) {
331*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\t(%u more bytes after the header)", length - NTP_TIMEMSG_MINLEN);
33227bfbee1SPeter Avalos 	}
33341c99275SPeter Avalos 	return;
33441c99275SPeter Avalos 
335*ed775ee7SAntonio Huete Jimenez invalid:
336*ed775ee7SAntonio Huete Jimenez 	nd_print_invalid(ndo);
337*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(bp, length);
338*ed775ee7SAntonio Huete Jimenez 	return;
339*ed775ee7SAntonio Huete Jimenez 
34041c99275SPeter Avalos trunc:
341*ed775ee7SAntonio Huete Jimenez 	nd_print_trunc(ndo);
342*ed775ee7SAntonio Huete Jimenez }
343*ed775ee7SAntonio Huete Jimenez 
344*ed775ee7SAntonio Huete Jimenez /*
345*ed775ee7SAntonio Huete Jimenez  * Print NTP control message requests and responses
346*ed775ee7SAntonio Huete Jimenez  */
347*ed775ee7SAntonio Huete Jimenez static void
ntp_control_print(netdissect_options * ndo,const struct ntp_control_data * cd,u_int length)348*ed775ee7SAntonio Huete Jimenez ntp_control_print(netdissect_options *ndo,
349*ed775ee7SAntonio Huete Jimenez 		  const struct ntp_control_data *cd, u_int length)
350*ed775ee7SAntonio Huete Jimenez {
351*ed775ee7SAntonio Huete Jimenez 	uint8_t control, R, E, M, opcode;
352*ed775ee7SAntonio Huete Jimenez 	uint16_t sequence, status, assoc, offset, count;
353*ed775ee7SAntonio Huete Jimenez 
354*ed775ee7SAntonio Huete Jimenez 	if (length < NTP_CTRLMSG_MINLEN)
355*ed775ee7SAntonio Huete Jimenez 		goto invalid;
356*ed775ee7SAntonio Huete Jimenez 
357*ed775ee7SAntonio Huete Jimenez 	control = GET_U_1(cd->control);
358*ed775ee7SAntonio Huete Jimenez 	R = (control & 0x80) != 0;
359*ed775ee7SAntonio Huete Jimenez 	E = (control & 0x40) != 0;
360*ed775ee7SAntonio Huete Jimenez 	M = (control & 0x20) != 0;
361*ed775ee7SAntonio Huete Jimenez 	opcode = control & 0x1f;
362*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", %s, %s, %s, OpCode=%u\n",
363*ed775ee7SAntonio Huete Jimenez 		  R ? "Response" : "Request", E ? "Error" : "OK",
364*ed775ee7SAntonio Huete Jimenez 		  M ? "More" : "Last", opcode);
365*ed775ee7SAntonio Huete Jimenez 
366*ed775ee7SAntonio Huete Jimenez 	sequence = GET_BE_U_2(cd->sequence);
367*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\tSequence=%hu", sequence);
368*ed775ee7SAntonio Huete Jimenez 
369*ed775ee7SAntonio Huete Jimenez 	status = GET_BE_U_2(cd->status);
370*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", Status=%#hx", status);
371*ed775ee7SAntonio Huete Jimenez 
372*ed775ee7SAntonio Huete Jimenez 	assoc = GET_BE_U_2(cd->assoc);
373*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", Assoc.=%hu", assoc);
374*ed775ee7SAntonio Huete Jimenez 
375*ed775ee7SAntonio Huete Jimenez 	offset = GET_BE_U_2(cd->offset);
376*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", Offset=%hu", offset);
377*ed775ee7SAntonio Huete Jimenez 
378*ed775ee7SAntonio Huete Jimenez 	count = GET_BE_U_2(cd->count);
379*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", Count=%hu", count);
380*ed775ee7SAntonio Huete Jimenez 
381*ed775ee7SAntonio Huete Jimenez 	if (NTP_CTRLMSG_MINLEN + count > length)
382*ed775ee7SAntonio Huete Jimenez 		goto invalid;
383*ed775ee7SAntonio Huete Jimenez 	if (count != 0) {
384*ed775ee7SAntonio Huete Jimenez 		ND_TCHECK_LEN(cd->data, count);
385*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("\n\tTO-BE-DONE: data not interpreted");
386*ed775ee7SAntonio Huete Jimenez 	}
387*ed775ee7SAntonio Huete Jimenez 	return;
388*ed775ee7SAntonio Huete Jimenez 
389*ed775ee7SAntonio Huete Jimenez invalid:
390*ed775ee7SAntonio Huete Jimenez 	nd_print_invalid(ndo);
391*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(cd, length);
392*ed775ee7SAntonio Huete Jimenez 	return;
393*ed775ee7SAntonio Huete Jimenez 
394*ed775ee7SAntonio Huete Jimenez trunc:
395*ed775ee7SAntonio Huete Jimenez 	nd_print_trunc(ndo);
396*ed775ee7SAntonio Huete Jimenez }
397*ed775ee7SAntonio Huete Jimenez 
398*ed775ee7SAntonio Huete Jimenez union ntpdata {
399*ed775ee7SAntonio Huete Jimenez 	struct ntp_time_data	td;
400*ed775ee7SAntonio Huete Jimenez 	struct ntp_control_data	cd;
401*ed775ee7SAntonio Huete Jimenez };
402*ed775ee7SAntonio Huete Jimenez 
403*ed775ee7SAntonio Huete Jimenez /*
404*ed775ee7SAntonio Huete Jimenez  * Print NTP requests, handling the common VN, LI, and Mode
405*ed775ee7SAntonio Huete Jimenez  */
406*ed775ee7SAntonio Huete Jimenez void
ntp_print(netdissect_options * ndo,const u_char * cp,u_int length)407*ed775ee7SAntonio Huete Jimenez ntp_print(netdissect_options *ndo,
408*ed775ee7SAntonio Huete Jimenez 	  const u_char *cp, u_int length)
409*ed775ee7SAntonio Huete Jimenez {
410*ed775ee7SAntonio Huete Jimenez 	const union ntpdata *bp = (const union ntpdata *)cp;
411*ed775ee7SAntonio Huete Jimenez 	u_int mode, version, leapind;
412*ed775ee7SAntonio Huete Jimenez 	uint8_t status;
413*ed775ee7SAntonio Huete Jimenez 
414*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "ntp";
415*ed775ee7SAntonio Huete Jimenez 	status = GET_U_1(bp->td.status);
416*ed775ee7SAntonio Huete Jimenez 
417*ed775ee7SAntonio Huete Jimenez 	version = (status & VERSIONMASK) >> VERSIONSHIFT;
418*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("NTPv%u", version);
419*ed775ee7SAntonio Huete Jimenez 
420*ed775ee7SAntonio Huete Jimenez 	mode = (status & MODEMASK) >> MODESHIFT;
421*ed775ee7SAntonio Huete Jimenez 	if (!ndo->ndo_vflag) {
422*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", %s, length %u",
423*ed775ee7SAntonio Huete Jimenez 			 tok2str(ntp_mode_values, "Unknown mode", mode),
424*ed775ee7SAntonio Huete Jimenez 			 length);
425*ed775ee7SAntonio Huete Jimenez 		return;
426*ed775ee7SAntonio Huete Jimenez 	}
427*ed775ee7SAntonio Huete Jimenez 
428*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", %s, length %u\n",
429*ed775ee7SAntonio Huete Jimenez 		  tok2str(ntp_mode_values, "Unknown mode", mode), length);
430*ed775ee7SAntonio Huete Jimenez 
431*ed775ee7SAntonio Huete Jimenez 	/* leapind = (status & LEAPMASK) >> LEAPSHIFT; */
432*ed775ee7SAntonio Huete Jimenez 	leapind = (status & LEAPMASK);
433*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\tLeap indicator: %s (%u)",
434*ed775ee7SAntonio Huete Jimenez 		 tok2str(ntp_leapind_values, "Unknown", leapind),
435*ed775ee7SAntonio Huete Jimenez 		 leapind);
436*ed775ee7SAntonio Huete Jimenez 
437*ed775ee7SAntonio Huete Jimenez 	switch (mode) {
438*ed775ee7SAntonio Huete Jimenez 
439*ed775ee7SAntonio Huete Jimenez 	case MODE_UNSPEC:
440*ed775ee7SAntonio Huete Jimenez 	case MODE_SYM_ACT:
441*ed775ee7SAntonio Huete Jimenez 	case MODE_SYM_PAS:
442*ed775ee7SAntonio Huete Jimenez 	case MODE_CLIENT:
443*ed775ee7SAntonio Huete Jimenez 	case MODE_SERVER:
444*ed775ee7SAntonio Huete Jimenez 	case MODE_BROADCAST:
445*ed775ee7SAntonio Huete Jimenez 		ntp_time_print(ndo, &bp->td, length);
446*ed775ee7SAntonio Huete Jimenez 		break;
447*ed775ee7SAntonio Huete Jimenez 
448*ed775ee7SAntonio Huete Jimenez 	case MODE_CONTROL:
449*ed775ee7SAntonio Huete Jimenez 		ntp_control_print(ndo, &bp->cd, length);
450*ed775ee7SAntonio Huete Jimenez 		break;
451*ed775ee7SAntonio Huete Jimenez 
452*ed775ee7SAntonio Huete Jimenez 	default:
453*ed775ee7SAntonio Huete Jimenez 		break;			/* XXX: not implemented! */
454*ed775ee7SAntonio Huete Jimenez 	}
45541c99275SPeter Avalos }
45641c99275SPeter Avalos 
45741c99275SPeter Avalos static void
p_sfix(netdissect_options * ndo,const struct s_fixedpt * sfp)458411677aeSAaron LI p_sfix(netdissect_options *ndo,
459*ed775ee7SAntonio Huete Jimenez        const struct s_fixedpt *sfp)
46041c99275SPeter Avalos {
461*ed775ee7SAntonio Huete Jimenez 	int i;
462*ed775ee7SAntonio Huete Jimenez 	int f;
463*ed775ee7SAntonio Huete Jimenez 	double ff;
46441c99275SPeter Avalos 
465*ed775ee7SAntonio Huete Jimenez 	i = GET_BE_U_2(sfp->int_part);
466*ed775ee7SAntonio Huete Jimenez 	f = GET_BE_U_2(sfp->fraction);
46741c99275SPeter Avalos 	ff = f / 65536.0;		/* shift radix point by 16 bits */
468411677aeSAaron LI 	f = (int)(ff * 1000000.0);	/* Treat fraction as parts per million */
469*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("%d.%06d", i, f);
47041c99275SPeter Avalos }
47141c99275SPeter Avalos 
47241c99275SPeter Avalos /* Prints time difference between *lfp and *olfp */
47341c99275SPeter Avalos static void
p_ntp_delta(netdissect_options * ndo,const struct l_fixedpt * olfp,const struct l_fixedpt * lfp)474411677aeSAaron LI p_ntp_delta(netdissect_options *ndo,
475*ed775ee7SAntonio Huete Jimenez 	    const struct l_fixedpt *olfp,
476*ed775ee7SAntonio Huete Jimenez 	    const struct l_fixedpt *lfp)
47741c99275SPeter Avalos {
478*ed775ee7SAntonio Huete Jimenez 	uint32_t u, uf;
479*ed775ee7SAntonio Huete Jimenez 	uint32_t ou, ouf;
480*ed775ee7SAntonio Huete Jimenez 	uint32_t i;
481*ed775ee7SAntonio Huete Jimenez 	uint32_t f;
482*ed775ee7SAntonio Huete Jimenez 	double ff;
48341c99275SPeter Avalos 	int signbit;
48441c99275SPeter Avalos 
485*ed775ee7SAntonio Huete Jimenez 	u = GET_BE_U_4(lfp->int_part);
486*ed775ee7SAntonio Huete Jimenez 	ou = GET_BE_U_4(olfp->int_part);
487*ed775ee7SAntonio Huete Jimenez 	uf = GET_BE_U_4(lfp->fraction);
488*ed775ee7SAntonio Huete Jimenez 	ouf = GET_BE_U_4(olfp->fraction);
48941c99275SPeter Avalos 	if (ou == 0 && ouf == 0) {
490411677aeSAaron LI 		p_ntp_time(ndo, lfp);
49141c99275SPeter Avalos 		return;
49241c99275SPeter Avalos 	}
49341c99275SPeter Avalos 
494*ed775ee7SAntonio Huete Jimenez 	if (u > ou) {		/* new is definitely greater than old */
49541c99275SPeter Avalos 		signbit = 0;
496*ed775ee7SAntonio Huete Jimenez 		i = u - ou;
49741c99275SPeter Avalos 		f = uf - ouf;
49841c99275SPeter Avalos 		if (ouf > uf)	/* must borrow from high-order bits */
49941c99275SPeter Avalos 			i -= 1;
500*ed775ee7SAntonio Huete Jimenez 	} else if (u < ou) {	/* new is definitely less than old */
50141c99275SPeter Avalos 		signbit = 1;
502*ed775ee7SAntonio Huete Jimenez 		i = ou - u;
50341c99275SPeter Avalos 		f = ouf - uf;
504*ed775ee7SAntonio Huete Jimenez 		if (uf > ouf)	/* must borrow from the high-order bits */
505*ed775ee7SAntonio Huete Jimenez 			i -= 1;
50641c99275SPeter Avalos 	} else {		/* int_part is zero */
507*ed775ee7SAntonio Huete Jimenez 		i = 0;
50841c99275SPeter Avalos 		if (uf > ouf) {
50941c99275SPeter Avalos 			signbit = 0;
51041c99275SPeter Avalos 			f = uf - ouf;
51141c99275SPeter Avalos 		} else {
51241c99275SPeter Avalos 			signbit = 1;
51341c99275SPeter Avalos 			f = ouf - uf;
51441c99275SPeter Avalos 		}
51541c99275SPeter Avalos 	}
51641c99275SPeter Avalos 
51741c99275SPeter Avalos 	ff = f;
51841c99275SPeter Avalos 	if (ff < 0.0)		/* some compilers are buggy */
51941c99275SPeter Avalos 		ff += FMAXINT;
52041c99275SPeter Avalos 	ff = ff / FMAXINT;			/* shift radix point by 32 bits */
521411677aeSAaron LI 	f = (uint32_t)(ff * 1000000000.0);	/* treat fraction as parts per billion */
522*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("%s%u.%09u", signbit ? "-" : "+", i, f);
523*ed775ee7SAntonio Huete Jimenez }
524*ed775ee7SAntonio Huete Jimenez 
525*ed775ee7SAntonio Huete Jimenez /* Prints polling interval in log2 as seconds or fraction of second */
526*ed775ee7SAntonio Huete Jimenez static void
p_poll(netdissect_options * ndo,const int poll_interval)527*ed775ee7SAntonio Huete Jimenez p_poll(netdissect_options *ndo,
528*ed775ee7SAntonio Huete Jimenez        const int poll_interval)
529*ed775ee7SAntonio Huete Jimenez {
530*ed775ee7SAntonio Huete Jimenez 	if (poll_interval <= -32 || poll_interval >= 32)
531*ed775ee7SAntonio Huete Jimenez 		return;
532*ed775ee7SAntonio Huete Jimenez 
533*ed775ee7SAntonio Huete Jimenez 	if (poll_interval >= 0)
534*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" (%us)", 1U << poll_interval);
535*ed775ee7SAntonio Huete Jimenez 	else
536*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" (1/%us)", 1U << -poll_interval);
53741c99275SPeter Avalos }
53841c99275SPeter Avalos 
539