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 */
39 #include <netdissect-stdinc.h>
45 #include "netdissect.h"
46 #include "addrtoname.h"
49 static const char tstr
[] = " [|ntp]";
52 * Based on ntp.h from the U of MD implementation
53 * This file is based on Version 2 of the NTP spec (RFC1119).
57 * Definitions for the masses
59 #define JAN_1970 INT64_T_CONSTANT(2208988800) /* 1970 - 1900 in seconds */
62 * Structure definitions for NTP fixed point values
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * 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
74 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75 * | Integer Part | Fraction Part |
76 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * 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
91 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 * |LI | VN |Mode | Stratum | Poll | Precision |
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 * | Reference Identifier |
99 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 * | Reference Timestamp (64) |
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Originate Timestamp (64) |
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * | Receive Timestamp (64) |
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 * | Transmit Timestamp (64) |
115 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 * | Key Identifier (optional) (32) |
117 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 * | Message Digest (optional) (128) |
123 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 /* Length of the NTP data message with the mandatory fields ("the header")
127 * and without any optional fields (extension, Key Identifier,
130 #define NTP_TIMEMSG_MINLEN 48U
132 struct ntp_time_data
{
133 nd_uint8_t status
; /* status of local clock and leap info */
134 nd_uint8_t stratum
; /* Stratum level */
135 nd_int8_t ppoll
; /* poll value */
137 struct s_fixedpt root_delay
;
138 struct s_fixedpt root_dispersion
;
140 struct l_fixedpt ref_timestamp
;
141 struct l_fixedpt org_timestamp
;
142 struct l_fixedpt rec_timestamp
;
143 struct l_fixedpt xmt_timestamp
;
145 nd_uint8_t message_digest
[20];
148 * Leap Second Codes (high order two bits)
150 #define NO_WARNING 0x00 /* no warning */
151 #define PLUS_SEC 0x40 /* add a second (61 seconds) */
152 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */
153 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */
156 * Clock Status Bits that Encode Version
158 #define NTPVERSION_1 0x08
159 #define VERSIONMASK 0x38
160 #define VERSIONSHIFT 3
161 #define LEAPMASK 0xc0
164 #undef MODEMASK /* Solaris sucks */
166 #define MODEMASK 0x07
172 #define MODE_UNSPEC 0 /* unspecified */
173 #define MODE_SYM_ACT 1 /* symmetric active */
174 #define MODE_SYM_PAS 2 /* symmetric passive */
175 #define MODE_CLIENT 3 /* client */
176 #define MODE_SERVER 4 /* server */
177 #define MODE_BROADCAST 5 /* broadcast */
178 #define MODE_CONTROL 6 /* control message */
179 #define MODE_RES2 7 /* reserved */
182 * Stratum Definitions
184 #define UNSPECIFIED 0
185 #define PRIM_REF 1 /* radio clock */
186 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */
187 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */
189 static void p_sfix(netdissect_options
*ndo
, const struct s_fixedpt
*);
190 static void p_ntp_time(netdissect_options
*, const struct l_fixedpt
*);
191 static void p_ntp_delta(netdissect_options
*, const struct l_fixedpt
*, const struct l_fixedpt
*);
192 static void p_poll(netdissect_options
*, register const int);
194 static const struct tok ntp_mode_values
[] = {
195 { MODE_UNSPEC
, "unspecified" },
196 { MODE_SYM_ACT
, "symmetric active" },
197 { MODE_SYM_PAS
, "symmetric passive" },
198 { MODE_CLIENT
, "Client" },
199 { MODE_SERVER
, "Server" },
200 { MODE_BROADCAST
, "Broadcast" },
201 { MODE_CONTROL
, "Control Message" },
202 { MODE_RES2
, "Reserved" },
206 static const struct tok ntp_leapind_values
[] = {
209 { MINUS_SEC
, "-1s" },
210 { ALARM
, "clock unsynchronized" },
214 static const struct tok ntp_stratum_values
[] = {
215 { UNSPECIFIED
, "unspecified" },
216 { PRIM_REF
, "primary reference" },
220 /* draft-ietf-ntp-mode-6-cmds-02
222 * 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
223 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
224 * |LI | VN |Mode |R|E|M| OpCode | Sequence Number |
225 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
226 * | Status | Association ID |
227 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231 * / Data (up to 468 bytes) /
233 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
234 * | Padding (optional) |
235 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237 * / Authenticator (optional, 96 bytes) /
239 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
241 * Figure 1: NTP Control Message Header
244 /* Length of the NTP control message with the mandatory fields ("the header")
245 * and without any optional fields (Data, Padding, Authenticator).
247 #define NTP_CTRLMSG_MINLEN 12U
249 struct ntp_control_data
{
250 nd_uint8_t magic
; /* LI, VN, Mode */
251 nd_uint8_t control
; /* R, E, M, OpCode */
252 nd_uint16_t sequence
; /* Sequence Number */
253 nd_uint16_t status
; /* Status */
254 nd_uint16_t assoc
; /* Association ID */
255 nd_uint16_t offset
; /* Offset */
256 nd_uint16_t count
; /* Count */
257 nd_uint8_t data
[564]; /* Data, [Padding, [Authenticator]] */
261 * Print NTP time requests and responses
264 ntp_time_print(netdissect_options
*ndo
,
265 register const struct ntp_time_data
*bp
, u_int length
)
269 if (length
< NTP_TIMEMSG_MINLEN
)
272 ND_TCHECK_1(bp
->stratum
);
273 stratum
= EXTRACT_U_1(bp
->stratum
);
274 ND_PRINT((ndo
, ", Stratum %u (%s)",
276 tok2str(ntp_stratum_values
, (stratum
>=2 && stratum
<=15) ? "secondary reference" : "reserved", stratum
)));
278 ND_TCHECK_1(bp
->ppoll
);
279 ND_PRINT((ndo
, ", poll %d", EXTRACT_S_1(bp
->ppoll
)));
280 p_poll(ndo
, EXTRACT_U_1(bp
->ppoll
));
282 ND_TCHECK_1(bp
->precision
);
283 ND_PRINT((ndo
, ", precision %d", EXTRACT_S_1(bp
->precision
)));
285 ND_TCHECK(bp
->root_delay
);
286 ND_PRINT((ndo
, "\n\tRoot Delay: "));
287 p_sfix(ndo
, &bp
->root_delay
);
289 ND_TCHECK(bp
->root_dispersion
);
290 ND_PRINT((ndo
, ", Root dispersion: "));
291 p_sfix(ndo
, &bp
->root_dispersion
);
293 ND_TCHECK(bp
->refid
);
294 ND_PRINT((ndo
, ", Reference-ID: "));
295 /* Interpretation depends on stratum */
299 ND_PRINT((ndo
, "(unspec)"));
303 if (fn_printn(ndo
, (const u_char
*)&(bp
->refid
), 4, ndo
->ndo_snapend
))
308 ND_PRINT((ndo
, "%s INFO_QUERY", ipaddr_string(ndo
, &(bp
->refid
))));
309 /* this doesn't have more content */
313 ND_PRINT((ndo
, "%s INFO_REPLY", ipaddr_string(ndo
, &(bp
->refid
))));
314 /* this is too complex to be worth printing */
318 /* In NTPv4 (RFC 5905) refid is an IPv4 address or first 32 bits of
319 MD5 sum of IPv6 address */
320 ND_PRINT((ndo
, "0x%08x", EXTRACT_BE_U_4(&bp
->refid
)));
324 ND_TCHECK(bp
->ref_timestamp
);
325 ND_PRINT((ndo
, "\n\t Reference Timestamp: "));
326 p_ntp_time(ndo
, &(bp
->ref_timestamp
));
328 ND_TCHECK(bp
->org_timestamp
);
329 ND_PRINT((ndo
, "\n\t Originator Timestamp: "));
330 p_ntp_time(ndo
, &(bp
->org_timestamp
));
332 ND_TCHECK(bp
->rec_timestamp
);
333 ND_PRINT((ndo
, "\n\t Receive Timestamp: "));
334 p_ntp_time(ndo
, &(bp
->rec_timestamp
));
336 ND_TCHECK(bp
->xmt_timestamp
);
337 ND_PRINT((ndo
, "\n\t Transmit Timestamp: "));
338 p_ntp_time(ndo
, &(bp
->xmt_timestamp
));
340 ND_PRINT((ndo
, "\n\t Originator - Receive Timestamp: "));
341 p_ntp_delta(ndo
, &(bp
->org_timestamp
), &(bp
->rec_timestamp
));
343 ND_PRINT((ndo
, "\n\t Originator - Transmit Timestamp: "));
344 p_ntp_delta(ndo
, &(bp
->org_timestamp
), &(bp
->xmt_timestamp
));
346 /* FIXME: this code is not aware of any extension fields */
347 if (length
== NTP_TIMEMSG_MINLEN
+ 4) { /* Optional: key-id (crypto-NAK) */
348 ND_TCHECK_4(bp
->key_id
);
349 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_BE_U_4(bp
->key_id
)));
350 } else if (length
== NTP_TIMEMSG_MINLEN
+ 4 + 16) { /* Optional: key-id + 128-bit digest */
351 ND_TCHECK_4(bp
->key_id
);
352 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_BE_U_4(bp
->key_id
)));
353 ND_TCHECK2(bp
->message_digest
, 16);
354 ND_PRINT((ndo
, "\n\tAuthentication: %08x%08x%08x%08x",
355 EXTRACT_BE_U_4(bp
->message_digest
),
356 EXTRACT_BE_U_4(bp
->message_digest
+ 4),
357 EXTRACT_BE_U_4(bp
->message_digest
+ 8),
358 EXTRACT_BE_U_4(bp
->message_digest
+ 12)));
359 } else if (length
== NTP_TIMEMSG_MINLEN
+ 4 + 20) { /* Optional: key-id + 160-bit digest */
360 ND_TCHECK_4(bp
->key_id
);
361 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_BE_U_4(bp
->key_id
)));
362 ND_TCHECK2(bp
->message_digest
, 20);
363 ND_PRINT((ndo
, "\n\tAuthentication: %08x%08x%08x%08x%08x",
364 EXTRACT_BE_U_4(bp
->message_digest
),
365 EXTRACT_BE_U_4(bp
->message_digest
+ 4),
366 EXTRACT_BE_U_4(bp
->message_digest
+ 8),
367 EXTRACT_BE_U_4(bp
->message_digest
+ 12),
368 EXTRACT_BE_U_4(bp
->message_digest
+ 16)));
369 } else if (length
> NTP_TIMEMSG_MINLEN
) {
370 ND_PRINT((ndo
, "\n\t(%u more bytes after the header)", length
- NTP_TIMEMSG_MINLEN
));
375 ND_PRINT((ndo
, " %s", istr
));
376 ND_TCHECK_LEN(bp
, length
);
380 ND_PRINT((ndo
, " %s", tstr
));
384 * Print NTP control message requests and responses
387 ntp_control_print(netdissect_options
*ndo
,
388 register const struct ntp_control_data
*cd
, u_int length
)
390 uint8_t control
, R
, E
, M
, opcode
;
391 uint16_t sequence
, status
, assoc
, offset
, count
;
393 if (length
< NTP_CTRLMSG_MINLEN
)
396 ND_TCHECK_1(cd
->control
);
397 control
= EXTRACT_U_1(cd
->control
);
398 R
= (control
& 0x80) != 0;
399 E
= (control
& 0x40) != 0;
400 M
= (control
& 0x20) != 0;
401 opcode
= control
& 0x1f;
402 ND_PRINT((ndo
, ", %s, %s, %s, OpCode=%u\n",
403 R
? "Response" : "Request", E
? "Error" : "OK",
404 M
? "More" : "Last", opcode
));
406 ND_TCHECK_2(cd
->sequence
);
407 sequence
= EXTRACT_BE_U_2(cd
->sequence
);
408 ND_PRINT((ndo
, "\tSequence=%hu", sequence
));
410 ND_TCHECK_2(cd
->status
);
411 status
= EXTRACT_BE_U_2(cd
->status
);
412 ND_PRINT((ndo
, ", Status=%#hx", status
));
414 ND_TCHECK_2(cd
->assoc
);
415 assoc
= EXTRACT_BE_U_2(cd
->assoc
);
416 ND_PRINT((ndo
, ", Assoc.=%hu", assoc
));
418 ND_TCHECK_2(cd
->offset
);
419 offset
= EXTRACT_BE_U_2(cd
->offset
);
420 ND_PRINT((ndo
, ", Offset=%hu", offset
));
422 ND_TCHECK_2(cd
->count
);
423 count
= EXTRACT_BE_U_2(cd
->count
);
424 ND_PRINT((ndo
, ", Count=%hu", count
));
426 if (NTP_CTRLMSG_MINLEN
+ count
> length
)
429 ND_TCHECK2(cd
->data
, count
);
430 ND_PRINT((ndo
, "\n\tTO-BE-DONE: data not interpreted"));
435 ND_PRINT((ndo
, " %s", istr
));
436 ND_TCHECK_LEN(cd
, length
);
440 ND_PRINT((ndo
, " %s", tstr
));
444 struct ntp_time_data td
;
445 struct ntp_control_data cd
;
449 * Print NTP requests, handling the common VN, LI, and Mode
452 ntp_print(netdissect_options
*ndo
,
453 register const u_char
*cp
, u_int length
)
455 register const union ntpdata
*bp
= (const union ntpdata
*)cp
;
456 int mode
, version
, leapind
;
459 ND_TCHECK(bp
->td
.status
);
460 status
= EXTRACT_U_1(bp
->td
.status
);
462 version
= (status
& VERSIONMASK
) >> VERSIONSHIFT
;
463 ND_PRINT((ndo
, "NTPv%d", version
));
465 mode
= (status
& MODEMASK
) >> MODESHIFT
;
466 if (!ndo
->ndo_vflag
) {
467 ND_PRINT((ndo
, ", %s, length %u",
468 tok2str(ntp_mode_values
, "Unknown mode", mode
),
473 ND_PRINT((ndo
, ", %s, length %u\n",
474 tok2str(ntp_mode_values
, "Unknown mode", mode
), length
));
476 /* leapind = (status & LEAPMASK) >> LEAPSHIFT; */
477 leapind
= (status
& LEAPMASK
);
478 ND_PRINT((ndo
, "\tLeap indicator: %s (%u)",
479 tok2str(ntp_leapind_values
, "Unknown", leapind
),
482 if (mode
>= MODE_UNSPEC
&& mode
<= MODE_BROADCAST
)
483 ntp_time_print(ndo
, &bp
->td
, length
);
484 else if (mode
== MODE_CONTROL
)
485 ntp_control_print(ndo
, &bp
->cd
, length
);
487 {;} /* XXX: not implemented! */
491 ND_PRINT((ndo
, " %s", tstr
));
495 p_sfix(netdissect_options
*ndo
,
496 register const struct s_fixedpt
*sfp
)
502 i
= EXTRACT_BE_U_2(&sfp
->int_part
);
503 f
= EXTRACT_BE_U_2(&sfp
->fraction
);
504 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
505 f
= (int)(ff
* 1000000.0); /* Treat fraction as parts per million */
506 ND_PRINT((ndo
, "%d.%06d", i
, f
));
509 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
512 p_ntp_time(netdissect_options
*ndo
,
513 register const struct l_fixedpt
*lfp
)
516 register uint32_t uf
;
520 i
= EXTRACT_BE_U_4(&lfp
->int_part
);
521 uf
= EXTRACT_BE_U_4(&lfp
->fraction
);
523 if (ff
< 0.0) /* some compilers are buggy */
525 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
526 f
= (uint32_t)(ff
* 1000000000.0); /* treat fraction as parts per billion */
527 ND_PRINT((ndo
, "%u.%09d", i
, f
));
531 * print the UTC time in human-readable format.
534 int64_t seconds_64bit
= (int64_t)i
- JAN_1970
;
539 seconds
= (time_t)seconds_64bit
;
540 if (seconds
!= seconds_64bit
) {
542 * It doesn't fit into a time_t, so we can't hand it
545 ND_PRINT((ndo
, " (unrepresentable)"));
547 tm
= gmtime(&seconds
);
550 * gmtime() can't handle it.
551 * (Yes, that might happen with some version of
552 * Microsoft's C library.)
554 ND_PRINT((ndo
, " (unrepresentable)"));
556 /* use ISO 8601 (RFC3339) format */
557 strftime(time_buf
, sizeof (time_buf
), "%Y-%m-%dT%H:%M:%S", tm
);
558 ND_PRINT((ndo
, " (%s)", time_buf
));
565 /* Prints time difference between *lfp and *olfp */
567 p_ntp_delta(netdissect_options
*ndo
,
568 register const struct l_fixedpt
*olfp
,
569 register const struct l_fixedpt
*lfp
)
572 register uint32_t u
, uf
;
573 register uint32_t ou
, ouf
;
578 u
= EXTRACT_BE_U_4(&lfp
->int_part
);
579 ou
= EXTRACT_BE_U_4(&olfp
->int_part
);
580 uf
= EXTRACT_BE_U_4(&lfp
->fraction
);
581 ouf
= EXTRACT_BE_U_4(&olfp
->fraction
);
582 if (ou
== 0 && ouf
== 0) {
583 p_ntp_time(ndo
, lfp
);
589 if (i
> 0) { /* new is definitely greater than old */
592 if (ouf
> uf
) /* must borrow from high-order bits */
594 } else if (i
< 0) { /* new is definitely less than old */
597 if (uf
> ouf
) /* must carry into the high-order bits */
600 } else { /* int_part is zero */
611 if (ff
< 0.0) /* some compilers are buggy */
613 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
614 f
= (uint32_t)(ff
* 1000000000.0); /* treat fraction as parts per billion */
615 ND_PRINT((ndo
, "%s%d.%09d", signbit
? "-" : "+", i
, f
));
618 /* Prints polling interval in log2 as seconds or fraction of second */
620 p_poll(netdissect_options
*ndo
,
621 register const int poll_interval
)
623 if (poll_interval
<= -32 || poll_interval
>= 32)
626 if (poll_interval
>= 0)
627 ND_PRINT((ndo
, " (%us)", 1U << poll_interval
));
629 ND_PRINT((ndo
, " (1/%us)", 1U << -poll_interval
));