]> The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
add a convenience symlink for README
[tcpdump] / print-ntp.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 *
21 * Format and print ntp packets.
22 * By Jeffrey Mogul/DECWRL
23 * loosely based on print-bootp.c
24 */
25
26 #ifndef lint
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)";
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <tcpdump-stdinc.h>
36
37 #include <stdio.h>
38 #include <string.h>
39 #ifdef HAVE_STRFTIME
40 #include <time.h>
41 #endif
42
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "extract.h"
46
47 /*
48 * Based on ntp.h from the U of MD implementation
49 * This file is based on Version 2 of the NTP spec (RFC1119).
50 */
51
52 /*
53 * Definitions for the masses
54 */
55 #define JAN_1970 2208988800U /* 1970 - 1900 in seconds */
56
57 /*
58 * Structure definitions for NTP fixed point values
59 *
60 * 0 1 2 3
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * | Integer Part |
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * | Fraction Part |
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 *
68 * 0 1 2 3
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 */
74 struct l_fixedpt {
75 u_int32_t int_part;
76 u_int32_t fraction;
77 };
78
79 struct s_fixedpt {
80 u_int16_t int_part;
81 u_int16_t fraction;
82 };
83
84 /* rfc2030
85 * 1 2 3
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * | Root Delay |
91 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 * | Root Dispersion |
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * | Reference Identifier |
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 * | |
97 * | Reference Timestamp (64) |
98 * | |
99 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 * | |
101 * | Originate Timestamp (64) |
102 * | |
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 * | |
105 * | Receive Timestamp (64) |
106 * | |
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 * | |
109 * | Transmit Timestamp (64) |
110 * | |
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 * | Key Identifier (optional) (32) |
113 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 * | |
115 * | |
116 * | Message Digest (optional) (128) |
117 * | |
118 * | |
119 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 */
121
122 struct ntpdata {
123 u_char status; /* status of local clock and leap info */
124 u_char stratum; /* Stratum level */
125 u_char ppoll; /* poll value */
126 int precision:8;
127 struct s_fixedpt root_delay;
128 struct s_fixedpt root_dispersion;
129 u_int32_t refid;
130 struct l_fixedpt ref_timestamp;
131 struct l_fixedpt org_timestamp;
132 struct l_fixedpt rec_timestamp;
133 struct l_fixedpt xmt_timestamp;
134 u_int32_t key_id;
135 u_int8_t message_digest[16];
136 };
137 /*
138 * Leap Second Codes (high order two bits)
139 */
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) */
144
145 /*
146 * Clock Status Bits that Encode Version
147 */
148 #define NTPVERSION_1 0x08
149 #define VERSIONMASK 0x38
150 #define LEAPMASK 0xc0
151 #ifdef MODEMASK
152 #undef MODEMASK /* Solaris sucks */
153 #endif
154 #define MODEMASK 0x07
155
156 /*
157 * Code values
158 */
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 */
167
168 /*
169 * Stratum Definitions
170 */
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 **** */
175
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 *);
179
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" },
189 { 0, NULL }
190 };
191
192 static const struct tok ntp_leapind_values[] = {
193 { NO_WARNING, "" },
194 { PLUS_SEC, "+1s" },
195 { MINUS_SEC, "-1s" },
196 { ALARM, "clock unsynchronized" },
197 { 0, NULL }
198 };
199
200 static const struct tok ntp_stratum_values[] = {
201 { UNSPECIFIED, "unspecified" },
202 { PRIM_REF, "primary reference" },
203 { 0, NULL }
204 };
205
206 /*
207 * Print ntp requests
208 */
209 void
210 ntp_print(register const u_char *cp, u_int length)
211 {
212 register const struct ntpdata *bp;
213 int mode, version, leapind;
214
215 bp = (struct ntpdata *)cp;
216
217 TCHECK(bp->status);
218
219 version = (int)(bp->status & VERSIONMASK) >> 3;
220 printf("NTPv%d", version);
221
222 mode = bp->status & MODEMASK;
223 if (!vflag) {
224 printf (", %s, length %u",
225 tok2str(ntp_mode_values, "Unknown mode", mode),
226 length);
227 return;
228 }
229
230 printf (", length %u\n\t%s",
231 length,
232 tok2str(ntp_mode_values, "Unknown mode", mode));
233
234 leapind = bp->status & LEAPMASK;
235 printf (", Leap indicator: %s (%u)",
236 tok2str(ntp_leapind_values, "Unknown", leapind),
237 leapind);
238
239 TCHECK(bp->stratum);
240 printf(", Stratum %u (%s)",
241 bp->stratum,
242 tok2str(ntp_stratum_values, (bp->stratum >=2 && bp->stratum<=15) ? "secondary reference" : "reserved", bp->stratum));
243
244 TCHECK(bp->ppoll);
245 printf(", poll %u (%us)", bp->ppoll, 1 << bp->ppoll);
246
247 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
248 TCHECK2(bp->root_delay, 0);
249 printf(", precision %d", bp->precision);
250
251 TCHECK(bp->root_delay);
252 fputs("\n\tRoot Delay: ", stdout);
253 p_sfix(&bp->root_delay);
254
255 TCHECK(bp->root_dispersion);
256 fputs(", Root dispersion: ", stdout);
257 p_sfix(&bp->root_dispersion);
258
259 TCHECK(bp->refid);
260 fputs(", Reference-ID: ", stdout);
261 /* Interpretation depends on stratum */
262 switch (bp->stratum) {
263
264 case UNSPECIFIED:
265 printf("(unspec)");
266 break;
267
268 case PRIM_REF:
269 if (fn_printn((u_char *)&(bp->refid), 4, snapend))
270 goto trunc;
271 break;
272
273 case INFO_QUERY:
274 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
275 /* this doesn't have more content */
276 return;
277
278 case INFO_REPLY:
279 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
280 /* this is too complex to be worth printing */
281 return;
282
283 default:
284 printf("%s", ipaddr_string(&(bp->refid)));
285 break;
286 }
287
288 TCHECK(bp->ref_timestamp);
289 fputs("\n\t Reference Timestamp: ", stdout);
290 p_ntp_time(&(bp->ref_timestamp));
291
292 TCHECK(bp->org_timestamp);
293 fputs("\n\t Originator Timestamp: ", stdout);
294 p_ntp_time(&(bp->org_timestamp));
295
296 TCHECK(bp->rec_timestamp);
297 fputs("\n\t Receive Timestamp: ", stdout);
298 p_ntp_time(&(bp->rec_timestamp));
299
300 TCHECK(bp->xmt_timestamp);
301 fputs("\n\t Transmit Timestamp: ", stdout);
302 p_ntp_time(&(bp->xmt_timestamp));
303
304 fputs("\n\t Originator - Receive Timestamp: ", stdout);
305 p_ntp_delta(&(bp->org_timestamp), &(bp->rec_timestamp));
306
307 fputs("\n\t Originator - Transmit Timestamp: ", stdout);
308 p_ntp_delta(&(bp->org_timestamp), &(bp->xmt_timestamp));
309
310 if ( (sizeof(struct ntpdata) - length) == 16) { /* Optional: key-id */
311 TCHECK(bp->key_id);
312 printf("\n\tKey id: %u", bp->key_id);
313 } else if ( (sizeof(struct ntpdata) - length) == 0) { /* Optional: key-id + authentication */
314 TCHECK(bp->key_id);
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));
322 }
323 return;
324
325 trunc:
326 fputs(" [|ntp]", stdout);
327 }
328
329 static void
330 p_sfix(register const struct s_fixedpt *sfp)
331 {
332 register int i;
333 register int f;
334 register float ff;
335
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);
341 }
342
343 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
344
345 static void
346 p_ntp_time(register const struct l_fixedpt *lfp)
347 {
348 register int32_t i;
349 register u_int32_t uf;
350 register u_int32_t f;
351 register float ff;
352
353 i = EXTRACT_32BITS(&lfp->int_part);
354 uf = EXTRACT_32BITS(&lfp->fraction);
355 ff = uf;
356 if (ff < 0.0) /* some compilers are buggy */
357 ff += FMAXINT;
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);
361
362 #ifdef HAVE_STRFTIME
363 /*
364 * print the time in human-readable format.
365 */
366 if (i) {
367 time_t seconds = i - JAN_1970;
368 struct tm *tm;
369 char time_buf[128];
370
371 tm = localtime(&seconds);
372 strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
373 printf (" (%s)", time_buf);
374 }
375 #endif
376 }
377
378 /* Prints time difference between *lfp and *olfp */
379 static void
380 p_ntp_delta(register const struct l_fixedpt *olfp,
381 register const struct l_fixedpt *lfp)
382 {
383 register int32_t i;
384 register u_int32_t u, uf;
385 register u_int32_t ou, ouf;
386 register u_int32_t f;
387 register float ff;
388 int signbit;
389
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) {
395 p_ntp_time(lfp);
396 return;
397 }
398
399 i = u - ou;
400
401 if (i > 0) { /* new is definitely greater than old */
402 signbit = 0;
403 f = uf - ouf;
404 if (ouf > uf) /* must borrow from high-order bits */
405 i -= 1;
406 } else if (i < 0) { /* new is definitely less than old */
407 signbit = 1;
408 f = ouf - uf;
409 if (uf > ouf) /* must carry into the high-order bits */
410 i += 1;
411 i = -i;
412 } else { /* int_part is zero */
413 if (uf > ouf) {
414 signbit = 0;
415 f = uf - ouf;
416 } else {
417 signbit = 1;
418 f = ouf - uf;
419 }
420 }
421
422 ff = f;
423 if (ff < 0.0) /* some compilers are buggy */
424 ff += FMAXINT;
425 ff = ff / FMAXINT; /* shift radix point by 32 bits */
426 f = ff * 1000000000.0; /* treat fraction as parts per billion */
427 if (signbit)
428 putchar('-');
429 else
430 putchar('+');
431 printf("%d.%09d", i, f);
432 }
433