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 * Format and print ntp packets.
22 * By Jeffrey Mogul/DECWRL
23 * loosely based on print-bootp.c
27 static const char rcsid
[] _U_
=
28 "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.43 2007-11-30 13:45:10 hannes Exp $ (LBL)";
35 #include <tcpdump-stdinc.h>
43 #include "interface.h"
44 #include "addrtoname.h"
48 * Based on ntp.h from the U of MD implementation
49 * This file is based on Version 2 of the NTP spec (RFC1119).
53 * Definitions for the masses
55 #define JAN_1970 2208988800U /* 1970 - 1900 in seconds */
58 * Structure definitions for NTP fixed point values
61 * 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
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * 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
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * | Integer Part | Fraction Part |
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 * 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
87 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 * |LI | VN |Mode | Stratum | Poll | Precision |
89 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * | Reference Identifier |
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 * | Reference Timestamp (64) |
99 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 * | Originate Timestamp (64) |
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Receive Timestamp (64) |
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * | Transmit Timestamp (64) |
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 * | Key Identifier (optional) (32) |
113 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 * | Message Digest (optional) (128) |
119 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 u_char status
; /* status of local clock and leap info */
124 u_char stratum
; /* Stratum level */
125 u_char ppoll
; /* 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 u_int8_t message_digest
[16];
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 LEAPMASK 0xc0
152 #undef MODEMASK /* Solaris sucks */
154 #define MODEMASK 0x07
159 #define MODE_UNSPEC 0 /* unspecified */
160 #define MODE_SYM_ACT 1 /* symmetric active */
161 #define MODE_SYM_PAS 2 /* symmetric passive */
162 #define MODE_CLIENT 3 /* client */
163 #define MODE_SERVER 4 /* server */
164 #define MODE_BROADCAST 5 /* broadcast */
165 #define MODE_RES1 6 /* reserved */
166 #define MODE_RES2 7 /* reserved */
169 * Stratum Definitions
171 #define UNSPECIFIED 0
172 #define PRIM_REF 1 /* radio clock */
173 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */
174 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */
176 static void p_sfix(const struct s_fixedpt
*);
177 static void p_ntp_time(const struct l_fixedpt
*);
178 static void p_ntp_delta(const struct l_fixedpt
*, const struct l_fixedpt
*);
180 static const struct tok ntp_mode_values
[] = {
181 { MODE_UNSPEC
, "unspecified" },
182 { MODE_SYM_ACT
, "symmetric active" },
183 { MODE_SYM_PAS
, "symmetric passive" },
184 { MODE_CLIENT
, "Client" },
185 { MODE_SERVER
, "Server" },
186 { MODE_BROADCAST
, "Broadcast" },
187 { MODE_RES1
, "Reserved" },
188 { MODE_RES2
, "Reserved" },
192 static const struct tok ntp_leapind_values
[] = {
195 { MINUS_SEC
, "-1s" },
196 { ALARM
, "clock unsynchronized" },
200 static const struct tok ntp_stratum_values
[] = {
201 { UNSPECIFIED
, "unspecified" },
202 { PRIM_REF
, "primary reference" },
210 ntp_print(register const u_char
*cp
, u_int length
)
212 register const struct ntpdata
*bp
;
213 int mode
, version
, leapind
;
215 bp
= (struct ntpdata
*)cp
;
219 version
= (int)(bp
->status
& VERSIONMASK
) >> 3;
220 printf("NTPv%d", version
);
222 mode
= bp
->status
& MODEMASK
;
224 printf (", %s, length %u",
225 tok2str(ntp_mode_values
, "Unknown mode", mode
),
230 printf (", length %u\n\t%s",
232 tok2str(ntp_mode_values
, "Unknown mode", mode
));
234 leapind
= bp
->status
& LEAPMASK
;
235 printf (", Leap indicator: %s (%u)",
236 tok2str(ntp_leapind_values
, "Unknown", leapind
),
240 printf(", Stratum %u (%s)",
242 tok2str(ntp_stratum_values
, (bp
->stratum
>=2 && bp
->stratum
<=15) ? "secondary reference" : "reserved", bp
->stratum
));
245 printf(", poll %u (%us)", bp
->ppoll
, 1 << bp
->ppoll
);
247 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
248 TCHECK2(bp
->root_delay
, 0);
249 printf(", precision %d", bp
->precision
);
251 TCHECK(bp
->root_delay
);
252 fputs("\n\tRoot Delay: ", stdout
);
253 p_sfix(&bp
->root_delay
);
255 TCHECK(bp
->root_dispersion
);
256 fputs(", Root dispersion: ", stdout
);
257 p_sfix(&bp
->root_dispersion
);
260 fputs(", Reference-ID: ", stdout
);
261 /* Interpretation depends on stratum */
262 switch (bp
->stratum
) {
269 if (fn_printn((u_char
*)&(bp
->refid
), 4, snapend
))
274 printf("%s INFO_QUERY", ipaddr_string(&(bp
->refid
)));
275 /* this doesn't have more content */
279 printf("%s INFO_REPLY", ipaddr_string(&(bp
->refid
)));
280 /* this is too complex to be worth printing */
284 printf("%s", ipaddr_string(&(bp
->refid
)));
288 TCHECK(bp
->ref_timestamp
);
289 fputs("\n\t Reference Timestamp: ", stdout
);
290 p_ntp_time(&(bp
->ref_timestamp
));
292 TCHECK(bp
->org_timestamp
);
293 fputs("\n\t Originator Timestamp: ", stdout
);
294 p_ntp_time(&(bp
->org_timestamp
));
296 TCHECK(bp
->rec_timestamp
);
297 fputs("\n\t Receive Timestamp: ", stdout
);
298 p_ntp_time(&(bp
->rec_timestamp
));
300 TCHECK(bp
->xmt_timestamp
);
301 fputs("\n\t Transmit Timestamp: ", stdout
);
302 p_ntp_time(&(bp
->xmt_timestamp
));
304 fputs("\n\t Originator - Receive Timestamp: ", stdout
);
305 p_ntp_delta(&(bp
->org_timestamp
), &(bp
->rec_timestamp
));
307 fputs("\n\t Originator - Transmit Timestamp: ", stdout
);
308 p_ntp_delta(&(bp
->org_timestamp
), &(bp
->xmt_timestamp
));
310 if ( (sizeof(struct ntpdata
) - length
) == 16) { /* Optional: key-id */
312 printf("\n\tKey id: %u", bp
->key_id
);
313 } else if ( (sizeof(struct ntpdata
) - length
) == 0) { /* Optional: key-id + authentication */
315 printf("\n\tKey id: %u", bp
->key_id
);
316 TCHECK2(bp
->message_digest
, sizeof (bp
->message_digest
));
317 printf("\n\tAuthentication: %08x%08x%08x%08x",
318 EXTRACT_32BITS(bp
->message_digest
),
319 EXTRACT_32BITS(bp
->message_digest
+ 4),
320 EXTRACT_32BITS(bp
->message_digest
+ 8),
321 EXTRACT_32BITS(bp
->message_digest
+ 12));
326 fputs(" [|ntp]", stdout
);
330 p_sfix(register const struct s_fixedpt
*sfp
)
336 i
= EXTRACT_16BITS(&sfp
->int_part
);
337 f
= EXTRACT_16BITS(&sfp
->fraction
);
338 ff
= f
/ 65536.0; /* shift radix point by 16 bits */
339 f
= ff
* 1000000.0; /* Treat fraction as parts per million */
340 printf("%d.%06d", i
, f
);
343 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
346 p_ntp_time(register const struct l_fixedpt
*lfp
)
349 register u_int32_t uf
;
350 register u_int32_t f
;
353 i
= EXTRACT_32BITS(&lfp
->int_part
);
354 uf
= EXTRACT_32BITS(&lfp
->fraction
);
356 if (ff
< 0.0) /* some compilers are buggy */
358 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
359 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
360 printf("%u.%09d", i
, f
);
364 * print the time in human-readable format.
367 time_t seconds
= i
- JAN_1970
;
371 tm
= localtime(&seconds
);
372 strftime(time_buf
, sizeof (time_buf
), "%Y/%m/%d %H:%M:%S", tm
);
373 printf (" (%s)", time_buf
);
378 /* Prints time difference between *lfp and *olfp */
380 p_ntp_delta(register const struct l_fixedpt
*olfp
,
381 register const struct l_fixedpt
*lfp
)
384 register u_int32_t u
, uf
;
385 register u_int32_t ou
, ouf
;
386 register u_int32_t f
;
390 u
= EXTRACT_32BITS(&lfp
->int_part
);
391 ou
= EXTRACT_32BITS(&olfp
->int_part
);
392 uf
= EXTRACT_32BITS(&lfp
->fraction
);
393 ouf
= EXTRACT_32BITS(&olfp
->fraction
);
394 if (ou
== 0 && ouf
== 0) {
401 if (i
> 0) { /* new is definitely greater than old */
404 if (ouf
> uf
) /* must borrow from high-order bits */
406 } else if (i
< 0) { /* new is definitely less than old */
409 if (uf
> ouf
) /* must carry into the high-order bits */
412 } else { /* int_part is zero */
423 if (ff
< 0.0) /* some compilers are buggy */
425 ff
= ff
/ FMAXINT
; /* shift radix point by 32 bits */
426 f
= ff
* 1000000000.0; /* treat fraction as parts per billion */
431 printf("%d.%09d", i
, f
);