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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 u_char status
; /* status of local clock and leap info */
118 u_char stratum
; /* Stratum level */
119 int ppoll
:8; /* poll value */
121 struct s_fixedpt root_delay
;
122 struct s_fixedpt root_dispersion
;
124 struct l_fixedpt ref_timestamp
;
125 struct l_fixedpt org_timestamp
;
126 struct l_fixedpt rec_timestamp
;
127 struct l_fixedpt xmt_timestamp
;
129 uint8_t message_digest
[16];
132 * Leap Second Codes (high order two bits)
134 #define NO_WARNING 0x00 /* no warning */
135 #define PLUS_SEC 0x40 /* add a second (61 seconds) */
136 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */
137 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */
140 * Clock Status Bits that Encode Version
142 #define NTPVERSION_1 0x08
143 #define VERSIONMASK 0x38
144 #define VERSIONSHIFT 3
145 #define LEAPMASK 0xc0
147 #undef MODEMASK /* Solaris sucks */
149 #define MODEMASK 0x07
154 #define MODE_UNSPEC 0 /* unspecified */
155 #define MODE_SYM_ACT 1 /* symmetric active */
156 #define MODE_SYM_PAS 2 /* symmetric passive */
157 #define MODE_CLIENT 3 /* client */
158 #define MODE_SERVER 4 /* server */
159 #define MODE_BROADCAST 5 /* broadcast */
160 #define MODE_RES1 6 /* reserved */
161 #define MODE_RES2 7 /* reserved */
164 * Stratum Definitions
166 #define UNSPECIFIED 0
167 #define PRIM_REF 1 /* radio clock */
168 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */
169 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */
171 static void p_sfix(netdissect_options
*ndo
, const struct s_fixedpt
*);
172 static void p_ntp_time(netdissect_options
*, const struct l_fixedpt
*);
173 static void p_ntp_delta(netdissect_options
*, const struct l_fixedpt
*, const struct l_fixedpt
*);
174 static void p_poll(netdissect_options
*, register const int);
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" },
188 static const struct tok ntp_leapind_values
[] = {
191 { MINUS_SEC
, "-1s" },
192 { ALARM
, "clock unsynchronized" },
196 static const struct tok ntp_stratum_values
[] = {
197 { UNSPECIFIED
, "unspecified" },
198 { PRIM_REF
, "primary reference" },
206 ntp_print(netdissect_options
*ndo
,
207 register const u_char
*cp
, u_int length
)
209 register const struct ntpdata
*bp
;
210 int mode
, version
, leapind
;
212 bp
= (const struct ntpdata
*)cp
;
214 ND_TCHECK(bp
->status
);
216 version
= (int)(bp
->status
& VERSIONMASK
) >> VERSIONSHIFT
;
217 ND_PRINT((ndo
, "NTPv%d", version
));
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
),
227 ND_PRINT((ndo
, ", length %u\n\t%s",
229 tok2str(ntp_mode_values
, "Unknown mode", mode
)));
231 leapind
= bp
->status
& LEAPMASK
;
232 ND_PRINT((ndo
, ", Leap indicator: %s (%u)",
233 tok2str(ntp_leapind_values
, "Unknown", leapind
),
236 ND_TCHECK(bp
->stratum
);
237 ND_PRINT((ndo
, ", Stratum %u (%s)",
239 tok2str(ntp_stratum_values
, (bp
->stratum
>=2 && bp
->stratum
<=15) ? "secondary reference" : "reserved", bp
->stratum
)));
241 /* Can't ND_TCHECK bp->ppoll bitfield so bp->stratum + 2 instead */
242 ND_TCHECK2(bp
->stratum
, 2);
243 ND_PRINT((ndo
, ", poll %d", bp
->ppoll
));
244 p_poll(ndo
, bp
->ppoll
);
246 /* Can't ND_TCHECK bp->precision bitfield so bp->distance + 0 instead */
247 ND_TCHECK2(bp
->root_delay
, 0);
248 ND_PRINT((ndo
, ", precision %d", bp
->precision
));
250 ND_TCHECK(bp
->root_delay
);
251 ND_PRINT((ndo
, "\n\tRoot Delay: "));
252 p_sfix(ndo
, &bp
->root_delay
);
254 ND_TCHECK(bp
->root_dispersion
);
255 ND_PRINT((ndo
, ", Root dispersion: "));
256 p_sfix(ndo
, &bp
->root_dispersion
);
258 ND_TCHECK(bp
->refid
);
259 ND_PRINT((ndo
, ", Reference-ID: "));
260 /* Interpretation depends on stratum */
261 switch (bp
->stratum
) {
264 ND_PRINT((ndo
, "(unspec)"));
268 if (fn_printn(ndo
, (const u_char
*)&(bp
->refid
), 4, ndo
->ndo_snapend
))
273 ND_PRINT((ndo
, "%s INFO_QUERY", ipaddr_string(ndo
, &(bp
->refid
))));
274 /* this doesn't have more content */
278 ND_PRINT((ndo
, "%s INFO_REPLY", ipaddr_string(ndo
, &(bp
->refid
))));
279 /* this is too complex to be worth printing */
283 /* In NTPv4 (RFC 5905) refid is an IPv4 address or first 32 bits of
284 MD5 sum of IPv6 address */
285 ND_PRINT((ndo
, "0x%08x", EXTRACT_32BITS(&bp
->refid
)));
289 ND_TCHECK(bp
->ref_timestamp
);
290 ND_PRINT((ndo
, "\n\t Reference Timestamp: "));
291 p_ntp_time(ndo
, &(bp
->ref_timestamp
));
293 ND_TCHECK(bp
->org_timestamp
);
294 ND_PRINT((ndo
, "\n\t Originator Timestamp: "));
295 p_ntp_time(ndo
, &(bp
->org_timestamp
));
297 ND_TCHECK(bp
->rec_timestamp
);
298 ND_PRINT((ndo
, "\n\t Receive Timestamp: "));
299 p_ntp_time(ndo
, &(bp
->rec_timestamp
));
301 ND_TCHECK(bp
->xmt_timestamp
);
302 ND_PRINT((ndo
, "\n\t Transmit Timestamp: "));
303 p_ntp_time(ndo
, &(bp
->xmt_timestamp
));
305 ND_PRINT((ndo
, "\n\t Originator - Receive Timestamp: "));
306 p_ntp_delta(ndo
, &(bp
->org_timestamp
), &(bp
->rec_timestamp
));
308 ND_PRINT((ndo
, "\n\t Originator - Transmit Timestamp: "));
309 p_ntp_delta(ndo
, &(bp
->org_timestamp
), &(bp
->xmt_timestamp
));
311 if ( (sizeof(struct ntpdata
) - length
) == 16) { /* Optional: key-id */
312 ND_TCHECK(bp
->key_id
);
313 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_32BITS(&bp
->key_id
)));
314 } else if ( (sizeof(struct ntpdata
) - length
) == 0) { /* Optional: key-id + authentication */
315 ND_TCHECK(bp
->key_id
);
316 ND_PRINT((ndo
, "\n\tKey id: %u", EXTRACT_32BITS(&bp
->key_id
)));
317 ND_TCHECK2(bp
->message_digest
, sizeof (bp
->message_digest
));
318 ND_PRINT((ndo
, "\n\tAuthentication: %08x%08x%08x%08x",
319 EXTRACT_32BITS(bp
->message_digest
),
320 EXTRACT_32BITS(bp
->message_digest
+ 4),
321 EXTRACT_32BITS(bp
->message_digest
+ 8),
322 EXTRACT_32BITS(bp
->message_digest
+ 12)));
327 ND_PRINT((ndo
, " [|ntp]"));
331 p_sfix(netdissect_options
*ndo
,
332 register const struct s_fixedpt
*sfp
)
338 i
= EXTRACT_16BITS(&sfp
->int_part
);
339 f
= EXTRACT_16BITS(&sfp
->fraction
);
340 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
341 f
= (int)(ff
* 1000000.0); /* Treat fraction as parts per million */
342 ND_PRINT((ndo
, "%d.%06d", i
, f
));
345 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
348 p_ntp_time(netdissect_options
*ndo
,
349 register const struct l_fixedpt
*lfp
)
352 register uint32_t uf
;
356 i
= EXTRACT_32BITS(&lfp
->int_part
);
357 uf
= EXTRACT_32BITS(&lfp
->fraction
);
359 if (ff
< 0.0) /* some compilers are buggy */
361 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
362 f
= (uint32_t)(ff
* 1000000000.0); /* treat fraction as parts per billion */
363 ND_PRINT((ndo
, "%u.%09d", i
, f
));
367 * print the time in human-readable format.
370 time_t seconds
= i
- JAN_1970
;
374 tm
= localtime(&seconds
);
375 strftime(time_buf
, sizeof (time_buf
), "%Y/%m/%d %H:%M:%S", tm
);
376 ND_PRINT((ndo
, " (%s)", time_buf
));
381 /* Prints time difference between *lfp and *olfp */
383 p_ntp_delta(netdissect_options
*ndo
,
384 register const struct l_fixedpt
*olfp
,
385 register const struct l_fixedpt
*lfp
)
388 register uint32_t u
, uf
;
389 register uint32_t ou
, ouf
;
394 u
= EXTRACT_32BITS(&lfp
->int_part
);
395 ou
= EXTRACT_32BITS(&olfp
->int_part
);
396 uf
= EXTRACT_32BITS(&lfp
->fraction
);
397 ouf
= EXTRACT_32BITS(&olfp
->fraction
);
398 if (ou
== 0 && ouf
== 0) {
399 p_ntp_time(ndo
, lfp
);
405 if (i
> 0) { /* new is definitely greater than old */
408 if (ouf
> uf
) /* must borrow from high-order bits */
410 } else if (i
< 0) { /* new is definitely less than old */
413 if (uf
> ouf
) /* must carry into the high-order bits */
416 } else { /* int_part is zero */
427 if (ff
< 0.0) /* some compilers are buggy */
429 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
430 f
= (uint32_t)(ff
* 1000000000.0); /* treat fraction as parts per billion */
431 ND_PRINT((ndo
, "%s%d.%09d", signbit
? "-" : "+", i
, f
));
434 /* Prints polling interval in log2 as seconds or fraction of second */
436 p_poll(netdissect_options
*ndo
,
437 register const int poll
)
439 if (poll
<= -32 || poll
>= 32)
443 ND_PRINT((ndo
, " (%us)", 1U << poll
));
445 ND_PRINT((ndo
, " (1/%us)", 1U << -poll
));