]> The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
Fix spaces before tabs in indentation
[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 /*
28 * specification:
29 *
30 * RFC 1119 - NTPv2
31 * RFC 1305 - NTPv3
32 * RFC 5905 - NTPv4
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "netdissect-stdinc.h"
40
41 #define ND_LONGJMP_FROM_TCHECK
42 #include "netdissect.h"
43 #include "addrtoname.h"
44 #include "extract.h"
45
46 #include "ntp.h"
47
48 /*
49 * Based on ntp.h from the U of MD implementation
50 * This file is based on Version 2 of the NTP spec (RFC1119).
51 */
52
53 /* rfc2030
54 * 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 * |LI | VN |Mode | Stratum | Poll | Precision |
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 * | Root Delay |
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * | Root Dispersion |
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * | Reference Identifier |
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * | |
66 * | Reference Timestamp (64) |
67 * | |
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | |
70 * | Originate Timestamp (64) |
71 * | |
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * | |
74 * | Receive Timestamp (64) |
75 * | |
76 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77 * | |
78 * | Transmit Timestamp (64) |
79 * | |
80 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 * | Key Identifier (optional) (32) |
82 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 * | |
84 * | |
85 * | Message Digest (optional) (128) |
86 * | |
87 * | |
88 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 */
90
91 /* Length of the NTP data message with the mandatory fields ("the header")
92 * and without any optional fields (extension, Key Identifier,
93 * Message Digest).
94 */
95 #define NTP_TIMEMSG_MINLEN 48U
96
97 struct ntp_time_data {
98 nd_uint8_t status; /* status of local clock and leap info */
99 nd_uint8_t stratum; /* Stratum level */
100 nd_int8_t ppoll; /* poll value */
101 nd_int8_t precision;
102 struct s_fixedpt root_delay;
103 struct s_fixedpt root_dispersion;
104 nd_uint32_t refid;
105 struct l_fixedpt ref_timestamp;
106 struct l_fixedpt org_timestamp;
107 struct l_fixedpt rec_timestamp;
108 struct l_fixedpt xmt_timestamp;
109 nd_uint32_t key_id;
110 nd_uint8_t message_digest[20];
111 };
112 /*
113 * Leap Second Codes (high order two bits)
114 */
115 #define NO_WARNING 0x00 /* no warning */
116 #define PLUS_SEC 0x40 /* add a second (61 seconds) */
117 #define MINUS_SEC 0x80 /* minus a second (59 seconds) */
118 #define ALARM 0xc0 /* alarm condition (clock unsynchronized) */
119
120 /*
121 * Clock Status Bits that Encode Version
122 */
123 #define NTPVERSION_1 0x08
124 #define VERSIONMASK 0x38
125 #define VERSIONSHIFT 3
126 #define LEAPMASK 0xc0
127 #define LEAPSHIFT 6
128 #ifdef MODEMASK
129 #undef MODEMASK /* Solaris sucks */
130 #endif
131 #define MODEMASK 0x07
132 #define MODESHIFT 0
133
134 /*
135 * Code values
136 */
137 #define MODE_UNSPEC 0 /* unspecified */
138 #define MODE_SYM_ACT 1 /* symmetric active */
139 #define MODE_SYM_PAS 2 /* symmetric passive */
140 #define MODE_CLIENT 3 /* client */
141 #define MODE_SERVER 4 /* server */
142 #define MODE_BROADCAST 5 /* broadcast */
143 #define MODE_CONTROL 6 /* control message */
144 #define MODE_RES2 7 /* reserved */
145
146 /*
147 * Stratum Definitions
148 */
149 #define UNSPECIFIED 0
150 #define PRIM_REF 1 /* radio clock */
151 #define INFO_QUERY 62 /* **** THIS implementation dependent **** */
152 #define INFO_REPLY 63 /* **** THIS implementation dependent **** */
153
154 static void p_sfix(netdissect_options *ndo, const struct s_fixedpt *);
155 static void p_ntp_delta(netdissect_options *, const struct l_fixedpt *, const struct l_fixedpt *);
156 static void p_poll(netdissect_options *, const int);
157
158 static const struct tok ntp_mode_values[] = {
159 { MODE_UNSPEC, "unspecified" },
160 { MODE_SYM_ACT, "symmetric active" },
161 { MODE_SYM_PAS, "symmetric passive" },
162 { MODE_CLIENT, "Client" },
163 { MODE_SERVER, "Server" },
164 { MODE_BROADCAST, "Broadcast" },
165 { MODE_CONTROL, "Control Message" },
166 { MODE_RES2, "Reserved" },
167 { 0, NULL }
168 };
169
170 static const struct tok ntp_leapind_values[] = {
171 { NO_WARNING, "" },
172 { PLUS_SEC, "+1s" },
173 { MINUS_SEC, "-1s" },
174 { ALARM, "clock unsynchronized" },
175 { 0, NULL }
176 };
177
178 static const struct tok ntp_stratum_values[] = {
179 { UNSPECIFIED, "unspecified" },
180 { PRIM_REF, "primary reference" },
181 { 0, NULL }
182 };
183
184 /* draft-ietf-ntp-mode-6-cmds-02
185 * 0 1 2 3
186 * 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
187 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
188 * |LI | VN |Mode |R|E|M| OpCode | Sequence Number |
189 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
190 * | Status | Association ID |
191 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
192 * | Offset | Count |
193 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
194 * | |
195 * / Data (up to 468 bytes) /
196 * | |
197 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
198 * | Padding (optional) |
199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
200 * | |
201 * / Authenticator (optional, 96 bytes) /
202 * | |
203 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204 *
205 * Figure 1: NTP Control Message Header
206 */
207
208 /* Length of the NTP control message with the mandatory fields ("the header")
209 * and without any optional fields (Data, Padding, Authenticator).
210 */
211 #define NTP_CTRLMSG_MINLEN 12U
212
213 struct ntp_control_data {
214 nd_uint8_t magic; /* LI, VN, Mode */
215 nd_uint8_t control; /* R, E, M, OpCode */
216 nd_uint16_t sequence; /* Sequence Number */
217 nd_uint16_t status; /* Status */
218 nd_uint16_t assoc; /* Association ID */
219 nd_uint16_t offset; /* Offset */
220 nd_uint16_t count; /* Count */
221 nd_uint8_t data[564]; /* Data, [Padding, [Authenticator]] */
222 };
223
224 /*
225 * Print NTP time requests and responses
226 */
227 static void
228 ntp_time_print(netdissect_options *ndo,
229 const struct ntp_time_data *bp, u_int length)
230 {
231 uint8_t stratum;
232
233 if (length < NTP_TIMEMSG_MINLEN)
234 goto invalid;
235
236 stratum = GET_U_1(bp->stratum);
237 ND_PRINT(", Stratum %u (%s)",
238 stratum,
239 tok2str(ntp_stratum_values, (stratum >=2 && stratum<=15) ? "secondary reference" : "reserved", stratum));
240
241 ND_PRINT(", poll %d", GET_S_1(bp->ppoll));
242 p_poll(ndo, GET_S_1(bp->ppoll));
243
244 ND_PRINT(", precision %d", GET_S_1(bp->precision));
245
246 ND_PRINT("\n\tRoot Delay: ");
247 p_sfix(ndo, &bp->root_delay);
248
249 ND_PRINT(", Root dispersion: ");
250 p_sfix(ndo, &bp->root_dispersion);
251
252 ND_PRINT(", Reference-ID: ");
253 /* Interpretation depends on stratum */
254 switch (stratum) {
255
256 case UNSPECIFIED:
257 ND_PRINT("(unspec)");
258 ND_TCHECK_4(bp->refid);
259 break;
260
261 case PRIM_REF:
262 nd_printjn(ndo, (const u_char *)&(bp->refid), 4);
263 break;
264
265 case INFO_QUERY:
266 ND_PRINT("%s INFO_QUERY", GET_IPADDR_STRING(bp->refid));
267 /* this doesn't have more content */
268 return;
269
270 case INFO_REPLY:
271 ND_PRINT("%s INFO_REPLY", GET_IPADDR_STRING(bp->refid));
272 /* this is too complex to be worth printing */
273 return;
274
275 default:
276 /* In NTPv4 (RFC 5905) refid is an IPv4 address or first 32 bits of
277 MD5 sum of IPv6 address */
278 ND_PRINT("0x%08x", GET_BE_U_4(bp->refid));
279 break;
280 }
281
282 ND_PRINT("\n\t Reference Timestamp: ");
283 p_ntp_time(ndo, &(bp->ref_timestamp));
284
285 ND_PRINT("\n\t Originator Timestamp: ");
286 p_ntp_time(ndo, &(bp->org_timestamp));
287
288 ND_PRINT("\n\t Receive Timestamp: ");
289 p_ntp_time(ndo, &(bp->rec_timestamp));
290
291 ND_PRINT("\n\t Transmit Timestamp: ");
292 p_ntp_time(ndo, &(bp->xmt_timestamp));
293
294 ND_PRINT("\n\t Originator - Receive Timestamp: ");
295 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->rec_timestamp));
296
297 ND_PRINT("\n\t Originator - Transmit Timestamp: ");
298 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->xmt_timestamp));
299
300 /* FIXME: this code is not aware of any extension fields */
301 if (length == NTP_TIMEMSG_MINLEN + 4) { /* Optional: key-id (crypto-NAK) */
302 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
303 } else if (length == NTP_TIMEMSG_MINLEN + 4 + 16) { /* Optional: key-id + 128-bit digest */
304 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
305 ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x",
306 GET_BE_U_4(bp->message_digest),
307 GET_BE_U_4(bp->message_digest + 4),
308 GET_BE_U_4(bp->message_digest + 8),
309 GET_BE_U_4(bp->message_digest + 12));
310 } else if (length == NTP_TIMEMSG_MINLEN + 4 + 20) { /* Optional: key-id + 160-bit digest */
311 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(bp->key_id));
312 ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x%08x",
313 GET_BE_U_4(bp->message_digest),
314 GET_BE_U_4(bp->message_digest + 4),
315 GET_BE_U_4(bp->message_digest + 8),
316 GET_BE_U_4(bp->message_digest + 12),
317 GET_BE_U_4(bp->message_digest + 16));
318 } else if (length > NTP_TIMEMSG_MINLEN) {
319 ND_PRINT("\n\t(%u more bytes after the header)", length - NTP_TIMEMSG_MINLEN);
320 }
321 return;
322
323 invalid:
324 nd_print_invalid(ndo);
325 ND_TCHECK_LEN(bp, length);
326 }
327
328 /*
329 * Print NTP control message requests and responses
330 */
331 static void
332 ntp_control_print(netdissect_options *ndo,
333 const struct ntp_control_data *cd, u_int length)
334 {
335 uint8_t control, R, E, M, opcode;
336 uint16_t sequence, status, assoc, offset, count;
337
338 if (length < NTP_CTRLMSG_MINLEN)
339 goto invalid;
340
341 control = GET_U_1(cd->control);
342 R = (control & 0x80) != 0;
343 E = (control & 0x40) != 0;
344 M = (control & 0x20) != 0;
345 opcode = control & 0x1f;
346 ND_PRINT(", %s, %s, %s, OpCode=%u\n",
347 R ? "Response" : "Request", E ? "Error" : "OK",
348 M ? "More" : "Last", opcode);
349
350 sequence = GET_BE_U_2(cd->sequence);
351 ND_PRINT("\tSequence=%hu", sequence);
352
353 status = GET_BE_U_2(cd->status);
354 ND_PRINT(", Status=%#hx", status);
355
356 assoc = GET_BE_U_2(cd->assoc);
357 ND_PRINT(", Assoc.=%hu", assoc);
358
359 offset = GET_BE_U_2(cd->offset);
360 ND_PRINT(", Offset=%hu", offset);
361
362 count = GET_BE_U_2(cd->count);
363 ND_PRINT(", Count=%hu", count);
364
365 if (NTP_CTRLMSG_MINLEN + count > length)
366 goto invalid;
367 if (count != 0) {
368 ND_TCHECK_LEN(cd->data, count);
369 ND_PRINT("\n\tTO-BE-DONE: data not interpreted");
370 }
371 return;
372
373 invalid:
374 nd_print_invalid(ndo);
375 ND_TCHECK_LEN(cd, length);
376 }
377
378 union ntpdata {
379 struct ntp_time_data td;
380 struct ntp_control_data cd;
381 };
382
383 /*
384 * Print NTP requests, handling the common VN, LI, and Mode
385 */
386 void
387 ntp_print(netdissect_options *ndo,
388 const u_char *cp, u_int length)
389 {
390 const union ntpdata *bp = (const union ntpdata *)cp;
391 u_int mode, version, leapind;
392 uint8_t status;
393
394 ndo->ndo_protocol = "ntp";
395 status = GET_U_1(bp->td.status);
396
397 version = (status & VERSIONMASK) >> VERSIONSHIFT;
398 ND_PRINT("NTPv%u", version);
399
400 mode = (status & MODEMASK) >> MODESHIFT;
401 if (!ndo->ndo_vflag) {
402 ND_PRINT(", %s, length %u",
403 tok2str(ntp_mode_values, "Unknown mode", mode),
404 length);
405 return;
406 }
407
408 ND_PRINT(", %s, length %u\n",
409 tok2str(ntp_mode_values, "Unknown mode", mode), length);
410
411 /* leapind = (status & LEAPMASK) >> LEAPSHIFT; */
412 leapind = (status & LEAPMASK);
413 ND_PRINT("\tLeap indicator: %s (%u)",
414 tok2str(ntp_leapind_values, "Unknown", leapind),
415 leapind);
416
417 switch (mode) {
418
419 case MODE_UNSPEC:
420 case MODE_SYM_ACT:
421 case MODE_SYM_PAS:
422 case MODE_CLIENT:
423 case MODE_SERVER:
424 case MODE_BROADCAST:
425 ntp_time_print(ndo, &bp->td, length);
426 break;
427
428 case MODE_CONTROL:
429 ntp_control_print(ndo, &bp->cd, length);
430 break;
431
432 default:
433 break; /* XXX: not implemented! */
434 }
435 }
436
437 static void
438 p_sfix(netdissect_options *ndo,
439 const struct s_fixedpt *sfp)
440 {
441 int i;
442 int f;
443 double ff;
444
445 i = GET_BE_U_2(sfp->int_part);
446 f = GET_BE_U_2(sfp->fraction);
447 ff = f / 65536.0; /* shift radix point by 16 bits */
448 f = (int)(ff * 1000000.0); /* Treat fraction as parts per million */
449 ND_PRINT("%d.%06d", i, f);
450 }
451
452 /* Prints time difference between *lfp and *olfp */
453 static void
454 p_ntp_delta(netdissect_options *ndo,
455 const struct l_fixedpt *olfp,
456 const struct l_fixedpt *lfp)
457 {
458 uint32_t u, uf;
459 uint32_t ou, ouf;
460 uint32_t i;
461 uint32_t f;
462 double ff;
463 int signbit;
464
465 u = GET_BE_U_4(lfp->int_part);
466 ou = GET_BE_U_4(olfp->int_part);
467 uf = GET_BE_U_4(lfp->fraction);
468 ouf = GET_BE_U_4(olfp->fraction);
469 if (ou == 0 && ouf == 0) {
470 p_ntp_time(ndo, lfp);
471 return;
472 }
473
474 if (u > ou) { /* new is definitely greater than old */
475 signbit = 0;
476 i = u - ou;
477 f = uf - ouf;
478 if (ouf > uf) /* must borrow from high-order bits */
479 i -= 1;
480 } else if (u < ou) { /* new is definitely less than old */
481 signbit = 1;
482 i = ou - u;
483 f = ouf - uf;
484 if (uf > ouf) /* must borrow from the high-order bits */
485 i -= 1;
486 } else { /* int_part is zero */
487 i = 0;
488 if (uf > ouf) {
489 signbit = 0;
490 f = uf - ouf;
491 } else {
492 signbit = 1;
493 f = ouf - uf;
494 }
495 }
496
497 ff = f;
498 if (ff < 0.0) /* some compilers are buggy */
499 ff += FMAXINT;
500 ff = ff / FMAXINT; /* shift radix point by 32 bits */
501 f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */
502 ND_PRINT("%s%u.%09u", signbit ? "-" : "+", i, f);
503 }
504
505 /* Prints polling interval in log2 as seconds or fraction of second */
506 static void
507 p_poll(netdissect_options *ndo,
508 const int poll_interval)
509 {
510 if (poll_interval <= -32 || poll_interval >= 32)
511 return;
512
513 if (poll_interval >= 0)
514 ND_PRINT(" (%us)", 1U << poll_interval);
515 else
516 ND_PRINT(" (1/%us)", 1U << -poll_interval);
517 }
518