]> The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
Move the printer summaries from INSTALL.txt to each printer
[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 /* \summary: Network Time Protocol (NTP) printer */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <netdissect-stdinc.h>
33
34 #ifdef HAVE_STRFTIME
35 #include <time.h>
36 #endif
37
38 #include "netdissect.h"
39 #include "addrtoname.h"
40 #include "extract.h"
41
42 /*
43 * Based on ntp.h from the U of MD implementation
44 * This file is based on Version 2 of the NTP spec (RFC1119).
45 */
46
47 /*
48 * Definitions for the masses
49 */
50 #define JAN_1970 2208988800U /* 1970 - 1900 in seconds */
51
52 /*
53 * Structure definitions for NTP fixed point values
54 *
55 * 0 1 2 3
56 * 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
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Integer Part |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * | Fraction Part |
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 *
63 * 0 1 2 3
64 * 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
65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 * | Integer Part | Fraction Part |
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 */
69 struct l_fixedpt {
70 uint32_t int_part;
71 uint32_t fraction;
72 };
73
74 struct s_fixedpt {
75 uint16_t int_part;
76 uint16_t fraction;
77 };
78
79 /* rfc2030
80 * 1 2 3
81 * 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
82 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 * |LI | VN |Mode | Stratum | Poll | Precision |
84 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 * | Root Delay |
86 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 * | Root Dispersion |
88 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 * | Reference Identifier |
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | |
92 * | Reference Timestamp (64) |
93 * | |
94 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 * | |
96 * | Originate Timestamp (64) |
97 * | |
98 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 * | |
100 * | Receive Timestamp (64) |
101 * | |
102 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 * | |
104 * | Transmit Timestamp (64) |
105 * | |
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * | Key Identifier (optional) (32) |
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * | |
110 * | |
111 * | Message Digest (optional) (128) |
112 * | |
113 * | |
114 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 */
116
117 struct ntpdata {
118 u_char status; /* status of local clock and leap info */
119 u_char stratum; /* Stratum level */
120 u_char ppoll; /* poll value */
121 int precision:8;
122 struct s_fixedpt root_delay;
123 struct s_fixedpt root_dispersion;
124 uint32_t refid;
125 struct l_fixedpt ref_timestamp;
126 struct l_fixedpt org_timestamp;
127 struct l_fixedpt rec_timestamp;
128 struct l_fixedpt xmt_timestamp;
129 uint32_t key_id;
130 uint8_t message_digest[16];
131 };
132 /*
133 * Leap Second Codes (high order two bits)
134 */
135 #define NO_WARNING 0x00 /* no warning */
136 #define PLUS_SEC 0x40 /* add a second (61 seconds) */
137 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */
138 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */
139
140 /*
141 * Clock Status Bits that Encode Version
142 */
143 #define NTPVERSION_1 0x08
144 #define VERSIONMASK 0x38
145 #define LEAPMASK 0xc0
146 #ifdef MODEMASK
147 #undef MODEMASK /* Solaris sucks */
148 #endif
149 #define MODEMASK 0x07
150
151 /*
152 * Code values
153 */
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 */
162
163 /*
164 * Stratum Definitions
165 */
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 **** */
170
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
175 static const struct tok ntp_mode_values[] = {
176 { MODE_UNSPEC, "unspecified" },
177 { MODE_SYM_ACT, "symmetric active" },
178 { MODE_SYM_PAS, "symmetric passive" },
179 { MODE_CLIENT, "Client" },
180 { MODE_SERVER, "Server" },
181 { MODE_BROADCAST, "Broadcast" },
182 { MODE_RES1, "Reserved" },
183 { MODE_RES2, "Reserved" },
184 { 0, NULL }
185 };
186
187 static const struct tok ntp_leapind_values[] = {
188 { NO_WARNING, "" },
189 { PLUS_SEC, "+1s" },
190 { MINUS_SEC, "-1s" },
191 { ALARM, "clock unsynchronized" },
192 { 0, NULL }
193 };
194
195 static const struct tok ntp_stratum_values[] = {
196 { UNSPECIFIED, "unspecified" },
197 { PRIM_REF, "primary reference" },
198 { 0, NULL }
199 };
200
201 /*
202 * Print ntp requests
203 */
204 void
205 ntp_print(netdissect_options *ndo,
206 register const u_char *cp, u_int length)
207 {
208 register const struct ntpdata *bp;
209 int mode, version, leapind;
210
211 bp = (const struct ntpdata *)cp;
212
213 ND_TCHECK(bp->status);
214
215 version = (int)(bp->status & VERSIONMASK) >> 3;
216 ND_PRINT((ndo, "NTPv%d", version));
217
218 mode = bp->status & MODEMASK;
219 if (!ndo->ndo_vflag) {
220 ND_PRINT((ndo, ", %s, length %u",
221 tok2str(ntp_mode_values, "Unknown mode", mode),
222 length));
223 return;
224 }
225
226 ND_PRINT((ndo, ", length %u\n\t%s",
227 length,
228 tok2str(ntp_mode_values, "Unknown mode", mode)));
229
230 leapind = bp->status & LEAPMASK;
231 ND_PRINT((ndo, ", Leap indicator: %s (%u)",
232 tok2str(ntp_leapind_values, "Unknown", leapind),
233 leapind));
234
235 ND_TCHECK(bp->stratum);
236 ND_PRINT((ndo, ", Stratum %u (%s)",
237 bp->stratum,
238 tok2str(ntp_stratum_values, (bp->stratum >=2 && bp->stratum<=15) ? "secondary reference" : "reserved", bp->stratum)));
239
240 ND_TCHECK(bp->ppoll);
241 ND_PRINT((ndo, ", poll %u (%us)", bp->ppoll, 1 << bp->ppoll));
242
243 /* Can't ND_TCHECK bp->precision bitfield so bp->distance + 0 instead */
244 ND_TCHECK2(bp->root_delay, 0);
245 ND_PRINT((ndo, ", precision %d", bp->precision));
246
247 ND_TCHECK(bp->root_delay);
248 ND_PRINT((ndo, "\n\tRoot Delay: "));
249 p_sfix(ndo, &bp->root_delay);
250
251 ND_TCHECK(bp->root_dispersion);
252 ND_PRINT((ndo, ", Root dispersion: "));
253 p_sfix(ndo, &bp->root_dispersion);
254
255 ND_TCHECK(bp->refid);
256 ND_PRINT((ndo, ", Reference-ID: "));
257 /* Interpretation depends on stratum */
258 switch (bp->stratum) {
259
260 case UNSPECIFIED:
261 ND_PRINT((ndo, "(unspec)"));
262 break;
263
264 case PRIM_REF:
265 if (fn_printn(ndo, (const u_char *)&(bp->refid), 4, ndo->ndo_snapend))
266 goto trunc;
267 break;
268
269 case INFO_QUERY:
270 ND_PRINT((ndo, "%s INFO_QUERY", ipaddr_string(ndo, &(bp->refid))));
271 /* this doesn't have more content */
272 return;
273
274 case INFO_REPLY:
275 ND_PRINT((ndo, "%s INFO_REPLY", ipaddr_string(ndo, &(bp->refid))));
276 /* this is too complex to be worth printing */
277 return;
278
279 default:
280 ND_PRINT((ndo, "%s", ipaddr_string(ndo, &(bp->refid))));
281 break;
282 }
283
284 ND_TCHECK(bp->ref_timestamp);
285 ND_PRINT((ndo, "\n\t Reference Timestamp: "));
286 p_ntp_time(ndo, &(bp->ref_timestamp));
287
288 ND_TCHECK(bp->org_timestamp);
289 ND_PRINT((ndo, "\n\t Originator Timestamp: "));
290 p_ntp_time(ndo, &(bp->org_timestamp));
291
292 ND_TCHECK(bp->rec_timestamp);
293 ND_PRINT((ndo, "\n\t Receive Timestamp: "));
294 p_ntp_time(ndo, &(bp->rec_timestamp));
295
296 ND_TCHECK(bp->xmt_timestamp);
297 ND_PRINT((ndo, "\n\t Transmit Timestamp: "));
298 p_ntp_time(ndo, &(bp->xmt_timestamp));
299
300 ND_PRINT((ndo, "\n\t Originator - Receive Timestamp: "));
301 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->rec_timestamp));
302
303 ND_PRINT((ndo, "\n\t Originator - Transmit Timestamp: "));
304 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->xmt_timestamp));
305
306 if ( (sizeof(struct ntpdata) - length) == 16) { /* Optional: key-id */
307 ND_TCHECK(bp->key_id);
308 ND_PRINT((ndo, "\n\tKey id: %u", bp->key_id));
309 } else if ( (sizeof(struct ntpdata) - length) == 0) { /* Optional: key-id + authentication */
310 ND_TCHECK(bp->key_id);
311 ND_PRINT((ndo, "\n\tKey id: %u", bp->key_id));
312 ND_TCHECK2(bp->message_digest, sizeof (bp->message_digest));
313 ND_PRINT((ndo, "\n\tAuthentication: %08x%08x%08x%08x",
314 EXTRACT_32BITS(bp->message_digest),
315 EXTRACT_32BITS(bp->message_digest + 4),
316 EXTRACT_32BITS(bp->message_digest + 8),
317 EXTRACT_32BITS(bp->message_digest + 12)));
318 }
319 return;
320
321 trunc:
322 ND_PRINT((ndo, " [|ntp]"));
323 }
324
325 static void
326 p_sfix(netdissect_options *ndo,
327 register const struct s_fixedpt *sfp)
328 {
329 register int i;
330 register int f;
331 register double ff;
332
333 i = EXTRACT_16BITS(&sfp->int_part);
334 f = EXTRACT_16BITS(&sfp->fraction);
335 ff = f / 65536.0; /* shift radix point by 16 bits */
336 f = (int)(ff * 1000000.0); /* Treat fraction as parts per million */
337 ND_PRINT((ndo, "%d.%06d", i, f));
338 }
339
340 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */
341
342 static void
343 p_ntp_time(netdissect_options *ndo,
344 register const struct l_fixedpt *lfp)
345 {
346 register int32_t i;
347 register uint32_t uf;
348 register uint32_t f;
349 register double ff;
350
351 i = EXTRACT_32BITS(&lfp->int_part);
352 uf = EXTRACT_32BITS(&lfp->fraction);
353 ff = uf;
354 if (ff < 0.0) /* some compilers are buggy */
355 ff += FMAXINT;
356 ff = ff / FMAXINT; /* shift radix point by 32 bits */
357 f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */
358 ND_PRINT((ndo, "%u.%09d", i, f));
359
360 #ifdef HAVE_STRFTIME
361 /*
362 * print the time in human-readable format.
363 */
364 if (i) {
365 time_t seconds = i - JAN_1970;
366 struct tm *tm;
367 char time_buf[128];
368
369 tm = localtime(&seconds);
370 strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm);
371 ND_PRINT((ndo, " (%s)", time_buf));
372 }
373 #endif
374 }
375
376 /* Prints time difference between *lfp and *olfp */
377 static void
378 p_ntp_delta(netdissect_options *ndo,
379 register const struct l_fixedpt *olfp,
380 register const struct l_fixedpt *lfp)
381 {
382 register int32_t i;
383 register uint32_t u, uf;
384 register uint32_t ou, ouf;
385 register uint32_t f;
386 register double ff;
387 int signbit;
388
389 u = EXTRACT_32BITS(&lfp->int_part);
390 ou = EXTRACT_32BITS(&olfp->int_part);
391 uf = EXTRACT_32BITS(&lfp->fraction);
392 ouf = EXTRACT_32BITS(&olfp->fraction);
393 if (ou == 0 && ouf == 0) {
394 p_ntp_time(ndo, lfp);
395 return;
396 }
397
398 i = u - ou;
399
400 if (i > 0) { /* new is definitely greater than old */
401 signbit = 0;
402 f = uf - ouf;
403 if (ouf > uf) /* must borrow from high-order bits */
404 i -= 1;
405 } else if (i < 0) { /* new is definitely less than old */
406 signbit = 1;
407 f = ouf - uf;
408 if (uf > ouf) /* must carry into the high-order bits */
409 i += 1;
410 i = -i;
411 } else { /* int_part is zero */
412 if (uf > ouf) {
413 signbit = 0;
414 f = uf - ouf;
415 } else {
416 signbit = 1;
417 f = ouf - uf;
418 }
419 }
420
421 ff = f;
422 if (ff < 0.0) /* some compilers are buggy */
423 ff += FMAXINT;
424 ff = ff / FMAXINT; /* shift radix point by 32 bits */
425 f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */
426 ND_PRINT((ndo, "%s%d.%09d", signbit ? "-" : "+", i, f));
427 }
428