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