]> The Tcpdump Group git mirrors - tcpdump/blob - print-ntp.c
NTP: Print extension fields
[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 /* RFC 5905 updated by RFC 7822
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 * | |
82 * . .
83 * . Optional Extensions (variable) .
84 * . .
85 * | |
86 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 */
88
89 /* Length of the NTP data message with the mandatory fields ("the header")
90 * and without any optional fields (extension, Key Identifier,
91 * Message Digest).
92 */
93 #define NTP_TIMEMSG_MINLEN 48U
94
95 struct ntp_time_data {
96 nd_uint8_t status; /* status of local clock and leap info */
97 nd_uint8_t stratum; /* Stratum level */
98 nd_int8_t ppoll; /* poll value */
99 nd_int8_t precision;
100 struct s_fixedpt root_delay;
101 struct s_fixedpt root_dispersion;
102 nd_uint32_t refid;
103 struct l_fixedpt ref_timestamp;
104 struct l_fixedpt org_timestamp;
105 struct l_fixedpt rec_timestamp;
106 struct l_fixedpt xmt_timestamp;
107 /* extension fields and/or MAC follow */
108 };
109
110 struct ntp_extension_field {
111 nd_uint16_t type;
112 nd_uint16_t length;
113 /* body follows */
114 };
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 static u_int p_ext_fields(netdissect_options *, const u_char *, u_int length);
162
163 static const struct tok ntp_mode_values[] = {
164 { MODE_UNSPEC, "unspecified" },
165 { MODE_SYM_ACT, "symmetric active" },
166 { MODE_SYM_PAS, "symmetric passive" },
167 { MODE_CLIENT, "Client" },
168 { MODE_SERVER, "Server" },
169 { MODE_BROADCAST, "Broadcast" },
170 { MODE_CONTROL, "Control Message" },
171 { MODE_RES2, "Reserved" },
172 { 0, NULL }
173 };
174
175 static const struct tok ntp_leapind_values[] = {
176 { NO_WARNING, "" },
177 { PLUS_SEC, "+1s" },
178 { MINUS_SEC, "-1s" },
179 { ALARM, "clock unsynchronized" },
180 { 0, NULL }
181 };
182
183 static const struct tok ntp_stratum_values[] = {
184 { UNSPECIFIED, "unspecified" },
185 { PRIM_REF, "primary reference" },
186 { 0, NULL }
187 };
188
189 static const struct tok ntp_ef_types[] = {
190 { 0x0104, "Unique Identifier" },
191 { 0x0204, "NTS Cookie" },
192 { 0x0304, "NTS Cookie Placeholder" },
193 { 0x0404, "NTS Authenticator and Encrypted Extension Fields" },
194 { 0x2005, "Checksum Complement" },
195 { 0, NULL }
196 };
197
198 /* draft-ietf-ntp-mode-6-cmds-02
199 * 0 1 2 3
200 * 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
201 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
202 * |LI | VN |Mode |R|E|M| OpCode | Sequence Number |
203 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204 * | Status | Association ID |
205 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206 * | Offset | Count |
207 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208 * | |
209 * / Data (up to 468 bytes) /
210 * | |
211 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212 * | Padding (optional) |
213 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214 * | |
215 * / Authenticator (optional, 96 bytes) /
216 * | |
217 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218 *
219 * Figure 1: NTP Control Message Header
220 */
221
222 /* Length of the NTP control message with the mandatory fields ("the header")
223 * and without any optional fields (Data, Padding, Authenticator).
224 */
225 #define NTP_CTRLMSG_MINLEN 12U
226
227 struct ntp_control_data {
228 nd_uint8_t magic; /* LI, VN, Mode */
229 nd_uint8_t control; /* R, E, M, OpCode */
230 nd_uint16_t sequence; /* Sequence Number */
231 nd_uint16_t status; /* Status */
232 nd_uint16_t assoc; /* Association ID */
233 nd_uint16_t offset; /* Offset */
234 nd_uint16_t count; /* Count */
235 nd_uint8_t data[564]; /* Data, [Padding, [Authenticator]] */
236 };
237
238 /*
239 * Print NTP time requests and responses
240 */
241 static void
242 ntp_time_print(netdissect_options *ndo,
243 const struct ntp_time_data *bp, u_int length, u_int version)
244 {
245 const u_char *mac;
246 uint8_t stratum;
247 u_int efs_len;
248
249 if (length < NTP_TIMEMSG_MINLEN)
250 goto invalid;
251
252 stratum = GET_U_1(bp->stratum);
253 ND_PRINT(", Stratum %u (%s)",
254 stratum,
255 tok2str(ntp_stratum_values, (stratum >=2 && stratum<=15) ? "secondary reference" : "reserved", stratum));
256
257 ND_PRINT(", poll %d", GET_S_1(bp->ppoll));
258 p_poll(ndo, GET_S_1(bp->ppoll));
259
260 ND_PRINT(", precision %d", GET_S_1(bp->precision));
261
262 ND_PRINT("\n\tRoot Delay: ");
263 p_sfix(ndo, &bp->root_delay);
264
265 ND_PRINT(", Root dispersion: ");
266 p_sfix(ndo, &bp->root_dispersion);
267
268 ND_PRINT(", Reference-ID: ");
269 /* Interpretation depends on stratum */
270 switch (stratum) {
271
272 case UNSPECIFIED:
273 ND_PRINT("(unspec)");
274 ND_TCHECK_4(bp->refid);
275 break;
276
277 case PRIM_REF:
278 nd_printjn(ndo, (const u_char *)&(bp->refid), 4);
279 break;
280
281 case INFO_QUERY:
282 ND_PRINT("%s INFO_QUERY", GET_IPADDR_STRING(bp->refid));
283 /* this doesn't have more content */
284 return;
285
286 case INFO_REPLY:
287 ND_PRINT("%s INFO_REPLY", GET_IPADDR_STRING(bp->refid));
288 /* this is too complex to be worth printing */
289 return;
290
291 default:
292 /* In NTPv4 (RFC 5905) refid is an IPv4 address or first 32 bits of
293 MD5 sum of IPv6 address */
294 ND_PRINT("0x%08x", GET_BE_U_4(bp->refid));
295 break;
296 }
297
298 ND_PRINT("\n\t Reference Timestamp: ");
299 p_ntp_time(ndo, &(bp->ref_timestamp));
300
301 ND_PRINT("\n\t Originator Timestamp: ");
302 p_ntp_time(ndo, &(bp->org_timestamp));
303
304 ND_PRINT("\n\t Receive Timestamp: ");
305 p_ntp_time(ndo, &(bp->rec_timestamp));
306
307 ND_PRINT("\n\t Transmit Timestamp: ");
308 p_ntp_time(ndo, &(bp->xmt_timestamp));
309
310 ND_PRINT("\n\t Originator - Receive Timestamp: ");
311 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->rec_timestamp));
312
313 ND_PRINT("\n\t Originator - Transmit Timestamp: ");
314 p_ntp_delta(ndo, &(bp->org_timestamp), &(bp->xmt_timestamp));
315
316 if (version == 4)
317 efs_len = p_ext_fields(ndo, (const u_char *)bp + NTP_TIMEMSG_MINLEN, length - NTP_TIMEMSG_MINLEN);
318 else
319 efs_len = 0;
320
321 mac = (const u_char *)bp + NTP_TIMEMSG_MINLEN + efs_len;
322
323 if (length == NTP_TIMEMSG_MINLEN + efs_len + 4) { /* Optional: key-id (crypto-NAK) */
324 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(mac));
325 } else if (length == NTP_TIMEMSG_MINLEN + efs_len + 4 + 16) { /* Optional: key-id + 128-bit digest */
326 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(mac));
327 ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x",
328 GET_BE_U_4(mac + 4),
329 GET_BE_U_4(mac + 8),
330 GET_BE_U_4(mac + 12),
331 GET_BE_U_4(mac + 16));
332 } else if (length == NTP_TIMEMSG_MINLEN + efs_len + 4 + 20) { /* Optional: key-id + 160-bit digest */
333 ND_PRINT("\n\tKey id: %u", GET_BE_U_4(mac));
334 ND_PRINT("\n\tAuthentication: %08x%08x%08x%08x%08x",
335 GET_BE_U_4(mac + 4),
336 GET_BE_U_4(mac + 8),
337 GET_BE_U_4(mac + 12),
338 GET_BE_U_4(mac + 16),
339 GET_BE_U_4(mac + 20));
340 } else if (length > NTP_TIMEMSG_MINLEN + efs_len) {
341 ND_PRINT("\n\t(%u more bytes after the header and extension fields)",
342 length - NTP_TIMEMSG_MINLEN - efs_len);
343 }
344 return;
345
346 invalid:
347 nd_print_invalid(ndo);
348 ND_TCHECK_LEN(bp, length);
349 }
350
351 /*
352 * Print NTP control message requests and responses
353 */
354 static void
355 ntp_control_print(netdissect_options *ndo,
356 const struct ntp_control_data *cd, u_int length)
357 {
358 uint8_t control, R, E, M, opcode;
359 uint16_t sequence, status, assoc, offset, count;
360
361 if (length < NTP_CTRLMSG_MINLEN)
362 goto invalid;
363
364 control = GET_U_1(cd->control);
365 R = (control & 0x80) != 0;
366 E = (control & 0x40) != 0;
367 M = (control & 0x20) != 0;
368 opcode = control & 0x1f;
369 ND_PRINT(", %s, %s, %s, OpCode=%u\n",
370 R ? "Response" : "Request", E ? "Error" : "OK",
371 M ? "More" : "Last", opcode);
372
373 sequence = GET_BE_U_2(cd->sequence);
374 ND_PRINT("\tSequence=%hu", sequence);
375
376 status = GET_BE_U_2(cd->status);
377 ND_PRINT(", Status=%#hx", status);
378
379 assoc = GET_BE_U_2(cd->assoc);
380 ND_PRINT(", Assoc.=%hu", assoc);
381
382 offset = GET_BE_U_2(cd->offset);
383 ND_PRINT(", Offset=%hu", offset);
384
385 count = GET_BE_U_2(cd->count);
386 ND_PRINT(", Count=%hu", count);
387
388 if (NTP_CTRLMSG_MINLEN + count > length)
389 goto invalid;
390 if (count != 0) {
391 ND_TCHECK_LEN(cd->data, count);
392 ND_PRINT("\n\tTO-BE-DONE: data not interpreted");
393 }
394 return;
395
396 invalid:
397 nd_print_invalid(ndo);
398 ND_TCHECK_LEN(cd, length);
399 }
400
401 union ntpdata {
402 struct ntp_time_data td;
403 struct ntp_control_data cd;
404 };
405
406 /*
407 * Print NTP requests, handling the common VN, LI, and Mode
408 */
409 void
410 ntp_print(netdissect_options *ndo,
411 const u_char *cp, u_int length)
412 {
413 const union ntpdata *bp = (const union ntpdata *)cp;
414 u_int mode, version, leapind;
415 uint8_t status;
416
417 ndo->ndo_protocol = "ntp";
418 status = GET_U_1(bp->td.status);
419
420 version = (status & VERSIONMASK) >> VERSIONSHIFT;
421 ND_PRINT("NTPv%u", version);
422
423 mode = (status & MODEMASK) >> MODESHIFT;
424 if (!ndo->ndo_vflag) {
425 ND_PRINT(", %s, length %u",
426 tok2str(ntp_mode_values, "Unknown mode", mode),
427 length);
428 return;
429 }
430
431 ND_PRINT(", %s, length %u\n",
432 tok2str(ntp_mode_values, "Unknown mode", mode), length);
433
434 /* leapind = (status & LEAPMASK) >> LEAPSHIFT; */
435 leapind = (status & LEAPMASK);
436 ND_PRINT("\tLeap indicator: %s (%u)",
437 tok2str(ntp_leapind_values, "Unknown", leapind),
438 leapind);
439
440 switch (mode) {
441
442 case MODE_UNSPEC:
443 case MODE_SYM_ACT:
444 case MODE_SYM_PAS:
445 case MODE_CLIENT:
446 case MODE_SERVER:
447 case MODE_BROADCAST:
448 ntp_time_print(ndo, &bp->td, length, version);
449 break;
450
451 case MODE_CONTROL:
452 ntp_control_print(ndo, &bp->cd, length);
453 break;
454
455 default:
456 break; /* XXX: not implemented! */
457 }
458 }
459
460 static void
461 p_sfix(netdissect_options *ndo,
462 const struct s_fixedpt *sfp)
463 {
464 int i;
465 int f;
466 double ff;
467
468 i = GET_BE_U_2(sfp->int_part);
469 f = GET_BE_U_2(sfp->fraction);
470 ff = f / 65536.0; /* shift radix point by 16 bits */
471 f = (int)(ff * 1000000.0); /* Treat fraction as parts per million */
472 ND_PRINT("%d.%06d", i, f);
473 }
474
475 /* Prints time difference between *lfp and *olfp */
476 static void
477 p_ntp_delta(netdissect_options *ndo,
478 const struct l_fixedpt *olfp,
479 const struct l_fixedpt *lfp)
480 {
481 uint32_t u, uf;
482 uint32_t ou, ouf;
483 uint32_t i;
484 uint32_t f;
485 double ff;
486 int signbit;
487
488 u = GET_BE_U_4(lfp->int_part);
489 ou = GET_BE_U_4(olfp->int_part);
490 uf = GET_BE_U_4(lfp->fraction);
491 ouf = GET_BE_U_4(olfp->fraction);
492 if (ou == 0 && ouf == 0) {
493 p_ntp_time(ndo, lfp);
494 return;
495 }
496
497 if (u > ou) { /* new is definitely greater than old */
498 signbit = 0;
499 i = u - ou;
500 f = uf - ouf;
501 if (ouf > uf) /* must borrow from high-order bits */
502 i -= 1;
503 } else if (u < ou) { /* new is definitely less than old */
504 signbit = 1;
505 i = ou - u;
506 f = ouf - uf;
507 if (uf > ouf) /* must borrow from the high-order bits */
508 i -= 1;
509 } else { /* int_part is zero */
510 i = 0;
511 if (uf > ouf) {
512 signbit = 0;
513 f = uf - ouf;
514 } else {
515 signbit = 1;
516 f = ouf - uf;
517 }
518 }
519
520 ff = f;
521 if (ff < 0.0) /* some compilers are buggy */
522 ff += FMAXINT;
523 ff = ff / FMAXINT; /* shift radix point by 32 bits */
524 f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */
525 ND_PRINT("%s%u.%09u", signbit ? "-" : "+", i, f);
526 }
527
528 /* Prints polling interval in log2 as seconds or fraction of second */
529 static void
530 p_poll(netdissect_options *ndo,
531 const int poll_interval)
532 {
533 if (poll_interval <= -32 || poll_interval >= 32)
534 return;
535
536 if (poll_interval >= 0)
537 ND_PRINT(" (%us)", 1U << poll_interval);
538 else
539 ND_PRINT(" (1/%us)", 1U << -poll_interval);
540 }
541
542 /* Prints an NTPv4 extension field */
543 static void
544 p_ntp_ef(netdissect_options *ndo, u_int type, u_int length, const u_char *ef_body)
545 {
546 ND_PRINT("\n\t %s", tok2str(ntp_ef_types, "Unknown type", type));
547 ND_PRINT(" (0x%04x), length %u", type, length);
548
549 if (ndo->ndo_vflag > 2)
550 hex_print(ndo, "\n\t ", ef_body, length - 4);
551 }
552
553 /* Prints list of extension fields per RFC 7822 */
554 static u_int
555 p_ext_fields(netdissect_options *ndo, const u_char *cp, u_int length)
556 {
557 const struct ntp_extension_field *ef;
558 u_int ef_type, ef_len, efs_len;
559 int first_ef;
560
561 first_ef = 1;
562 efs_len = 0;
563
564 /* RFC 7822 requires the last EF in the packet to have at least
565 28 octets to avoid ambiguity with MACs */
566 while (length - efs_len >= 28) {
567 ef = (const struct ntp_extension_field *)(cp + efs_len);
568 ef_type = GET_BE_U_2(ef->type);
569 ef_len = GET_BE_U_2(ef->length);
570
571 if (efs_len + ef_len > length || ef_len < 4 || ef_len % 4 != 0) {
572 nd_print_invalid(ndo);
573 break;
574 }
575
576 if (first_ef) {
577 ND_PRINT("\n\tExtension fields:");
578 first_ef = 0;
579 }
580
581 p_ntp_ef(ndo, ef_type, ef_len, (const u_char *)(ef + 1));
582 efs_len += ef_len;
583 }
584
585 return efs_len;
586 }