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