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