2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * By Jeffrey Mogul/DECWRL
22 * loosely based on print-bootp.c
25 /* \summary: Network Time Protocol (NTP) printer */
31 #include <netdissect-stdinc.h>
37 #include "netdissect.h"
38 #include "addrtoname.h"
42 * Based on ntp.h from the U of MD implementation
43 * This file is based on Version 2 of the NTP spec (RFC1119).
47 * Definitions for the masses
49 #define JAN_1970 2208988800U /* 1970 - 1900 in seconds */
52 * Structure definitions for NTP fixed point values
55 * 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
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * 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
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * | Integer Part | Fraction Part |
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 * 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
81 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82 * |LI | VN |Mode | Stratum | Poll | Precision |
83 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 * | Reference Identifier |
89 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | Reference Timestamp (64) |
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 * | Originate Timestamp (64) |
97 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 * | Receive Timestamp (64) |
101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 * | Transmit Timestamp (64) |
105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 * | Key Identifier (optional) (32) |
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 * | Message Digest (optional) (128) |
113 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 /* Length of the NTP message with the mandatory fields ("the header")
117 * and without any optional fields (extension, Key Identifier,
120 #define NTP_MSG_MINLEN 48
123 u_char status
; /* status of local clock and leap info */
124 u_char stratum
; /* Stratum level */
125 int ppoll
:8; /* poll value */
127 struct s_fixedpt root_delay
;
128 struct s_fixedpt root_dispersion
;
130 struct l_fixedpt ref_timestamp
;
131 struct l_fixedpt org_timestamp
;
132 struct l_fixedpt rec_timestamp
;
133 struct l_fixedpt xmt_timestamp
;
135 uint8_t message_digest
[20];
138 * Leap Second Codes (high order two bits)
140 #define NO_WARNING 0x00 /* no warning */
141 #define PLUS_SEC 0x40 /* add a second (61 seconds) */
142 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */
143 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */
146 * Clock Status Bits that Encode Version
148 #define NTPVERSION_1 0x08
149 #define VERSIONMASK 0x38
150 #define VERSIONSHIFT 3
151 #define LEAPMASK 0xc0
154 #undef MODEMASK /* Solaris sucks */
156 #define MODEMASK 0x07
162 #define MODE_UNSPEC 0 /* unspecified */
163 #define MODE_SYM_ACT 1 /* symmetric active */
164 #define MODE_SYM_PAS 2 /* symmetric passive */
165 #define MODE_CLIENT 3 /* client */
166 #define MODE_SERVER 4 /* server */
167 #define MODE_BROADCAST 5 /* broadcast */
168 #define MODE_CONTROL 6 /* control message */
169 #define MODE_RES2 7 /* reserved */
172 * Stratum Definitions
174 #define UNSPECIFIED 0
175 #define PRIM_REF 1 /* radio clock */
176 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */
177 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */
179 static void p_sfix(netdissect_options
*ndo
, const struct s_fixedpt
*);
180 static void p_ntp_time(netdissect_options
*, const struct l_fixedpt
*);
181 static void p_ntp_delta(netdissect_options
*, const struct l_fixedpt
*, const struct l_fixedpt
*);
182 static void p_poll(netdissect_options
*, register const int);
184 static const struct tok ntp_mode_values
[] = {
185 { MODE_UNSPEC
, "unspecified" },
186 { MODE_SYM_ACT
, "symmetric active" },
187 { MODE_SYM_PAS
, "symmetric passive" },
188 { MODE_CLIENT
, "Client" },
189 { MODE_SERVER
, "Server" },
190 { MODE_BROADCAST
, "Broadcast" },
191 { MODE_CONTROL
, "Control Message" },
192 { MODE_RES2
, "Reserved" },
196 static const struct tok ntp_leapind_values
[] = {
199 { MINUS_SEC
, "-1s" },
200 { ALARM
, "clock unsynchronized" },
204 static const struct tok ntp_stratum_values
[] = {
205 { UNSPECIFIED
, "unspecified" },
206 { PRIM_REF
, "primary reference" },
214 ntp_print(netdissect_options
*ndo
,
215 register const u_char
*cp
, u_int length
)
217 register const struct ntpdata
*bp
;
218 int mode
, version
, leapind
;
220 if (length
< NTP_MSG_MINLEN
) {
221 ND_PRINT((ndo
, "NTP, length %u", length
));
225 bp
= (const struct ntpdata
*)cp
;
227 ND_TCHECK(bp
->status
);
229 version
= (int)(bp
->status
& VERSIONMASK
) >> VERSIONSHIFT
;
230 ND_PRINT((ndo
, "NTPv%d", version
));
232 mode
= bp
->status
& MODEMASK
;
233 if (!ndo
->ndo_vflag
) {
234 ND_PRINT((ndo
, ", %s, length %u",
235 tok2str(ntp_mode_values
, "Unknown mode", mode
),
240 ND_PRINT((ndo
, ", length %u\n\t%s",
242 tok2str(ntp_mode_values
, "Unknown mode", mode
)));
244 leapind
= bp
->status
& LEAPMASK
;
245 ND_PRINT((ndo
, ", Leap indicator: %s (%u)",
246 tok2str(ntp_leapind_values
, "Unknown", leapind
),
249 ND_TCHECK(bp
->stratum
);
250 ND_PRINT((ndo
, ", Stratum %u (%s)",
252 tok2str(ntp_stratum_values
, (bp
->stratum
>=2 && bp
->stratum
<=15) ? "secondary reference" : "reserved", bp
->stratum
)));
254 /* Can't ND_TCHECK bp->ppoll bitfield so bp->stratum + 2 instead */
255 ND_TCHECK2(bp
->stratum
, 2);
256 ND_PRINT((ndo
, ", poll %d", bp
->ppoll
));
257 p_poll(ndo
, bp
->ppoll
);
259 /* Can't ND_TCHECK bp->precision bitfield so bp->distance + 0 instead */
260 ND_TCHECK2(bp
->root_delay
, 0);
261 ND_PRINT((ndo
, ", precision %d", bp
->precision
));
263 ND_TCHECK(bp
->root_delay
);
264 ND_PRINT((ndo
, "\n\tRoot Delay: "));
265 p_sfix(ndo
, &bp
->root_delay
);
267 ND_TCHECK(bp
->root_dispersion
);
268 ND_PRINT((ndo
, ", Root dispersion: "));
269 p_sfix(ndo
, &bp
->root_dispersion
);
271 ND_TCHECK(bp
->refid
);
272 ND_PRINT((ndo
, ", Reference-ID: "));
273 /* Interpretation depends on stratum */
274 switch (bp
->stratum
) {
277 ND_PRINT((ndo
, "(unspec)"));
281 if (fn_printn(ndo
, (const u_char
*)&(bp
->refid
), 4, ndo
->ndo_snapend
))
286 ND_PRINT((ndo
, "%s INFO_QUERY", ipaddr_string(ndo
, &(bp
->refid
))));
287 /* this doesn't have more content */
291 ND_PRINT((ndo
, "%s INFO_REPLY", ipaddr_string(ndo
, &(bp
->refid
))));
292 /* this is too complex to be worth printing */
296 /* In NTPv4 (RFC 5905) refid is an IPv4 address or first 32 bits of
297 MD5 sum of IPv6 address */
298 ND_PRINT((ndo
, "0x%08x", EXTRACT_32BITS(&bp
->refid
)));
302 ND_TCHECK(bp
->ref_timestamp
);
303 ND_PRINT((ndo
, "\n\t Reference Timestamp: "));
304 p_ntp_time(ndo
, &(bp
->ref_timestamp
));
306 ND_TCHECK(bp
->org_timestamp
);
307 ND_PRINT((ndo
, "\n\t Originator Timestamp: "));
308 p_ntp_time(ndo
, &(bp
->org_timestamp
));
310 ND_TCHECK(bp
->rec_timestamp
);
311 ND_PRINT((ndo
, "\n\t Receive Timestamp: "));
312 p_ntp_time(ndo
, &(bp
->rec_timestamp
));
314 ND_TCHECK(bp
->xmt_timestamp
);
315 ND_PRINT((ndo
, "\n\t Transmit Timestamp: "));
316 p_ntp_time(ndo
, &(bp
->xmt_timestamp
));
318 ND_PRINT((ndo
, "\n\t Originator - Receive Timestamp: "));
319 p_ntp_delta(ndo
, &(bp
->org_timestamp
), &(bp
->rec_timestamp
));
321 ND_PRINT((ndo
, "\n\t Originator - Transmit Timestamp: "));
322 p_ntp_delta(ndo
, &(bp
->org_timestamp
), &(bp
->xmt_timestamp
));
324 /* FIXME: this code is not aware of any extension fields */
325 if (length
== NTP_MSG_MINLEN
+ 4) { /* Optional: key-id (crypto-NAK) */
326 ND_TCHECK(bp
->key_id
);
327 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_32BITS(&bp
->key_id
)));
328 } else if (length
== NTP_MSG_MINLEN
+ 4 + 16) { /* Optional: key-id + 128-bit digest */
329 ND_TCHECK(bp
->key_id
);
330 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_32BITS(&bp
->key_id
)));
331 ND_TCHECK2(bp
->message_digest
, 16);
332 ND_PRINT((ndo
, "\n\tAuthentication: %08x%08x%08x%08x",
333 EXTRACT_32BITS(bp
->message_digest
),
334 EXTRACT_32BITS(bp
->message_digest
+ 4),
335 EXTRACT_32BITS(bp
->message_digest
+ 8),
336 EXTRACT_32BITS(bp
->message_digest
+ 12)));
337 } else if (length
== NTP_MSG_MINLEN
+ 4 + 20) { /* Optional: key-id + 160-bit digest */
338 ND_TCHECK(bp
->key_id
);
339 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_32BITS(&bp
->key_id
)));
340 ND_TCHECK2(bp
->message_digest
, 20);
341 ND_PRINT((ndo
, "\n\tAuthentication: %08x%08x%08x%08x%08x",
342 EXTRACT_32BITS(bp
->message_digest
),
343 EXTRACT_32BITS(bp
->message_digest
+ 4),
344 EXTRACT_32BITS(bp
->message_digest
+ 8),
345 EXTRACT_32BITS(bp
->message_digest
+ 12),
346 EXTRACT_32BITS(bp
->message_digest
+ 16)));
347 } else if (length
> NTP_MSG_MINLEN
) {
348 ND_PRINT((ndo
, "\n\t(%u more bytes after the header)", length
- NTP_MSG_MINLEN
));
353 ND_PRINT((ndo
, " %s", istr
));
354 ND_TCHECK2(*cp
, length
);
358 ND_PRINT((ndo
, " [|ntp]"));
362 p_sfix(netdissect_options
*ndo
,
363 register const struct s_fixedpt
*sfp
)
369 i
= EXTRACT_16BITS(&sfp
->int_part
);
370 f
= EXTRACT_16BITS(&sfp
->fraction
);
371 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
372 f
= (int)(ff
* 1000000.0); /* Treat fraction as parts per million */
373 ND_PRINT((ndo
, "%d.%06d", i
, f
));
376 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
379 p_ntp_time(netdissect_options
*ndo
,
380 register const struct l_fixedpt
*lfp
)
383 register uint32_t uf
;
387 i
= EXTRACT_32BITS(&lfp
->int_part
);
388 uf
= EXTRACT_32BITS(&lfp
->fraction
);
390 if (ff
< 0.0) /* some compilers are buggy */
392 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
393 f
= (uint32_t)(ff
* 1000000000.0); /* treat fraction as parts per billion */
394 ND_PRINT((ndo
, "%u.%09d", i
, f
));
398 * print the UTC time in human-readable format.
401 time_t seconds
= i
- JAN_1970
;
405 tm
= gmtime(&seconds
);
406 /* use ISO 8601 (RFC3339) format */
407 strftime(time_buf
, sizeof (time_buf
), "%Y-%m-%dT%H:%M:%S", tm
);
408 ND_PRINT((ndo
, " (%s)", time_buf
));
413 /* Prints time difference between *lfp and *olfp */
415 p_ntp_delta(netdissect_options
*ndo
,
416 register const struct l_fixedpt
*olfp
,
417 register const struct l_fixedpt
*lfp
)
420 register uint32_t u
, uf
;
421 register uint32_t ou
, ouf
;
426 u
= EXTRACT_32BITS(&lfp
->int_part
);
427 ou
= EXTRACT_32BITS(&olfp
->int_part
);
428 uf
= EXTRACT_32BITS(&lfp
->fraction
);
429 ouf
= EXTRACT_32BITS(&olfp
->fraction
);
430 if (ou
== 0 && ouf
== 0) {
431 p_ntp_time(ndo
, lfp
);
437 if (i
> 0) { /* new is definitely greater than old */
440 if (ouf
> uf
) /* must borrow from high-order bits */
442 } else if (i
< 0) { /* new is definitely less than old */
445 if (uf
> ouf
) /* must carry into the high-order bits */
448 } else { /* int_part is zero */
459 if (ff
< 0.0) /* some compilers are buggy */
461 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
462 f
= (uint32_t)(ff
* 1000000000.0); /* treat fraction as parts per billion */
463 ND_PRINT((ndo
, "%s%d.%09d", signbit
? "-" : "+", i
, f
));
466 /* Prints polling interval in log2 as seconds or fraction of second */
468 p_poll(netdissect_options
*ndo
,
469 register const int poll
)
471 if (poll
<= -32 || poll
>= 32)
475 ND_PRINT((ndo
, " (%us)", 1U << poll
));
477 ND_PRINT((ndo
, " (1/%us)", 1U << -poll
));