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