2 * Routines for NTP packet dissection
3 * Copyright 1999, Nathan Neulinger <nneul@umr.edu>
5 * $Id: packet-ntp.c,v 1.7 1999/12/21 17:21:22 gerald Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@unicom.net>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-tftp.c
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
47 #ifdef NEED_SNPRINTF_H
53 # include "snprintf.h"
58 #include "packet-ntp.h"
61 * Dissecting NTP packets version 3 and 4 (RFC2030, RFC1769, RFC1361,
64 * Those packets have simple structure:
66 * 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
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * |LI | VN |Mode | Stratum | Poll | Precision |
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 * | Reference Identifier |
75 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76 * | Reference Timestamp (64) |
78 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 * | Originate Timestamp (64) |
81 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82 * | Receive Timestamp (64) |
84 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 * | Transmit Timestamp (64) |
87 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 * | Key Identifier (optional) (32) |
89 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * | Message Digest (optional) (128) |
94 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 * NTP timestamps are represented as a 64-bit unsigned fixed-point number,
96 * in seconds relative to 0h on 1 January 1900. The integer part is in the
97 * first 32 bits and the fraction part in the last 32 bits.
100 /* Leap indicator, 2bit field is used to warn of a inserted/deleted
101 * second, or to alarm loosed synchronization.
103 static const value_string li_types[] = {
104 { NTP_LI_NONE, "no warning" },
105 { NTP_LI_61, "last minute has 61 seconds" },
106 { NTP_LI_59, "last minute has 59 seconds" },
107 { NTP_LI_ALARM, "alarm condition (clock not synchronized)" },
111 /* Version info, 3bit field informs about NTP version used in particular
112 * packet. According to rfc2030, version info could be only 3 or 4, but I
113 * have noticed packets with 1 or even 6 as version numbers. They are
114 * produced as a result of ntptrace command. Are those packets mailformed
115 * on purpose? I don't know yet, probably some browsing through ntp sources
116 * would help. My solution is to put them as reserved for now.
118 static const value_string ver_nums[] = {
119 { NTP_VN_R0, "reserved" },
120 { NTP_VN_R1, "reserved" },
121 { NTP_VN_R2, "reserved" },
122 { NTP_VN_3, "NTP Version 3" },
123 { NTP_VN_4, "NTP Version 4" },
124 { NTP_VN_R5, "reserved" },
125 { NTP_VN_R6, "reserved" },
126 { NTP_VN_R7, "reserved" },
130 /* Mode, 3bit field representing mode of comunication.
132 static const value_string mode_types[] = {
133 { NTP_MODE_RSV, "reserved" },
134 { NTP_MODE_SYMACT, "symmetric active" },
135 { NTP_MODE_SYMPAS, "symmetric passive" },
136 { NTP_MODE_CLIENT, "client" },
137 { NTP_MODE_SERVER, "server" },
138 { NTP_MODE_BCAST, "broadcast" },
139 { NTP_MODE_CTRL, "reserved for NTP control message"},
140 { NTP_MODE_PRIV, "reserved for private use" },
144 /* According to rfc, primary (stratum-0 and stratum-1) servers should set
145 * their Reference Clock ID (4bytes field) according to following table:
147 static const struct {
150 } primary_sources[] = {
151 { "LOCL", "uncalibrated local clock" },
152 { "PPS\0", "atomic clock or other pulse-per-second source" },
153 { "ACTS", "NIST dialup modem service" },
154 { "USNO", "USNO modem service" },
155 { "PTB\0", "PTB (Germany) modem service" },
156 { "TDF\0", "Allouis (France) Radio 164 kHz" },
157 { "DCF\0", "Mainflingen (Germany) Radio 77.5 kHz" },
158 { "MSF\0", "Rugby (UK) Radio 60 kHz" },
159 { "WWV\0", "Ft. Collins (US) Radio 2.5, 5, 10, 15, 20 MHz" },
160 { "WWVB", "Boulder (US) Radio 60 kHz" },
161 { "WWVH", "Kaui Hawaii (US) Radio 2.5, 5, 10, 15 MHz" },
162 { "CHU\0", "Ottawa (Canada) Radio 3330, 7335, 14670 kHz" },
163 { "LORC", "LORAN-C radionavigation system" },
164 { "OMEG", "OMEGA radionavigation system" },
165 { "GPS\0", "Global Positioning Service" },
166 { "GOES", "Geostationary Orbit Environment Satellite" },
167 { "DCN\0", "DCN routing protocol" },
168 { "NIST", "NIST public modem" },
169 { "TSP\0", "TSP time protocol" },
170 { "DTS\0", "Digital Time Service" },
171 { "ATOM", "Atomic clock (calibrated)" },
172 { "VLF\0", "VLF radio (OMEGA,, etc.)" },
176 static int proto_ntp = -1;
177 static int hf_ntp_flags = -1;
178 static int hf_ntp_flags_li = -1;
179 static int hf_ntp_flags_vn = -1;
180 static int hf_ntp_flags_mode = -1;
181 static int hf_ntp_stratum = -1;
182 static int hf_ntp_ppoll = -1;
183 static int hf_ntp_precision = -1;
184 static int hf_ntp_rootdelay = -1;
185 static int hf_ntp_rootdispersion = -1;
186 static int hf_ntp_refid = -1;
187 static int hf_ntp_reftime = -1;
188 static int hf_ntp_org = -1;
189 static int hf_ntp_rec = -1;
190 static int hf_ntp_xmt = -1;
191 static int hf_ntp_keyid = -1;
192 static int hf_ntp_mac = -1;
194 static gint ett_ntp = -1;
195 static gint ett_ntp_flags = -1;
197 /* ntp_fmt_ts - converts NTP timestamp to human readable string.
198 * tsdata - 64bit timestamp (IN)
199 * buff - string buffer for result (OUT)
200 * returns pointer to filled buffer.
203 ntp_fmt_ts(unsigned char * reftime, char* buff)
205 guint32 tempstmp, tempfrac;
210 tempstmp = pntohl(&reftime[0]);
211 tempfrac = pntohl(&reftime[4]);
212 if ((tempstmp == 0) && (tempfrac == 0)) {
213 strcpy (buff, "NULL");
216 temptime = tempstmp - (guint32) NTP_BASETIME;
217 bd = gmtime(&temptime);
218 fractime = bd->tm_sec + tempfrac / 4294967296.0;
219 snprintf(buff, NTP_TS_SIZE, "%04d-%02d-%02d %02d:%02d:%07.4f UTC",
220 bd->tm_year + 1900, bd->tm_mon, bd->tm_mday, bd->tm_hour,
221 bd->tm_min, fractime);
226 /* dissect_ntp - dissects NTP packet data
227 * pd - pointer to packet data (IN)
228 * offset - offset of NTP data in pd (IN)
230 * proto_tree - resolved protocol tree
233 dissect_ntp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
235 proto_tree *ntp_tree, *flags_tree;
237 struct ntp_packet *pkt;
238 gchar buff[NTP_TS_SIZE];
241 /* get at least a full packet structure */
242 if ( !BYTES_ARE_IN_FRAME(offset, 48) ) /* 48 without keyid or mac */
245 pkt = (struct ntp_packet *) &pd[offset];
247 if (check_col(fd, COL_PROTOCOL))
248 col_add_str(fd, COL_PROTOCOL, "NTP");
250 if (check_col(fd, COL_INFO))
251 col_add_str(fd, COL_INFO, "NTP");
254 /* Adding NTP item and subtree */
255 ti = proto_tree_add_item(tree, proto_ntp, offset, END_OF_FRAME, NULL);
256 ntp_tree = proto_item_add_subtree(ti, ett_ntp);
257 tf = proto_tree_add_item(ntp_tree, hf_ntp_flags, offset, 1, pkt->flags);
259 /* Adding flag subtree and items */
260 flags_tree = proto_item_add_subtree(tf, ett_ntp_flags);
261 proto_tree_add_item_format(flags_tree, hf_ntp_flags_li, offset, 1,
262 *pkt->flags & NTP_LI_MASK,
263 decode_enumerated_bitfield(*pkt->flags, NTP_LI_MASK,
264 sizeof(pkt->flags) * 8, li_types, "Leap Indicator: %s"));
265 proto_tree_add_item_format(flags_tree, hf_ntp_flags_vn, offset, 1,
266 *pkt->flags & NTP_VN_MASK,
267 decode_enumerated_bitfield(*pkt->flags, NTP_VN_MASK,
268 sizeof(pkt->flags) * 8, ver_nums, "Version number: %s"));
269 proto_tree_add_item_format(flags_tree, hf_ntp_flags_mode, offset, 1,
270 *pkt->flags & NTP_MODE_MASK,
271 decode_enumerated_bitfield(*pkt->flags, NTP_MODE_MASK,
272 sizeof(pkt->flags) * 8, mode_types, "Mode: %s"));
274 /* Stratum, 1byte field represents distance from primary source
276 if (*pkt->stratum == 0) {
277 strcpy (buff, "Peer Clock Stratum: unspecified or unavailable (%d)");
278 } else if (*pkt->stratum == 1) {
279 strcpy (buff, "Peer Clock Stratum: primary reference (%d)");
280 } else if ((*pkt->stratum >= 2) && (*pkt->stratum <= 15)) {
281 strcpy (buff, "Peer Clock Stratum: secondary reference (%d)");
283 strcpy (buff, "Peer Clock Stratum: reserved: %d");
285 proto_tree_add_item_format(ntp_tree, hf_ntp_stratum, offset+1, 1, pkt->stratum,
286 buff, (int) *pkt->stratum);
287 /* Poll interval, 1byte field indicating the maximum interval between
288 * successive messages, in seconds to the nearest power of two.
290 proto_tree_add_item_format(ntp_tree, hf_ntp_ppoll, offset+2, 1, pkt->ppoll,
291 (((*pkt->ppoll >= 4) && (*pkt->ppoll <= 16)) ?
292 "Peer Pooling Interval: %d (%d sec)" :
293 "Peer Pooling Interval: invalid (%d)"), (int) *pkt->ppoll,
295 /* Precision, 1byte field indicating the precision of the
296 * local clock, in seconds to the nearest power of two.
298 proto_tree_add_item_format(ntp_tree, hf_ntp_precision, offset+3, 1, pkt->precision,
299 "Peer Clock Precision: %8.6f sec", pow(2, *pkt->precision));
300 /* Root Delay is a 32-bit signed fixed-point number indicating the
301 * total roundtrip delay to the primary reference source, in seconds
302 * with fraction point between bits 15 and 16.
304 proto_tree_add_item_format(ntp_tree, hf_ntp_rootdelay, offset+4, 4, pkt->rootdelay,
305 "Root Delay: %9.4f sec",
306 ((gint32) pntohs(pkt->rootdelay)) +
307 pntohs(pkt->rootdelay + 2) / 65536.0);
308 /* Root Dispersion, 32-bit unsigned fixed-point number indicating
309 * the nominal error relative to the primary reference source, in
310 * seconds with fraction point between bits 15 and 16.
312 proto_tree_add_item_format(ntp_tree, hf_ntp_rootdispersion, offset+8, 4, pkt->rootdispersion,
313 "Clock Dispersion: %9.4f sec",
314 ((gint32) pntohs(pkt->rootdispersion)) +
315 pntohs(pkt->rootdispersion + 2) / 65536.0);
316 /* Now, there is a problem with secondary servers. Standards asks
317 * from stratum-2 - stratum-15 servers to set this to the low order
318 * 32 bits of the latest transmit timestamp of the reference source.
319 * But, all V3 and V4 servers set this to IP adress of their higher
320 * level server. My decision was to resolve this address.
322 if (*pkt->stratum <= 1) {
323 snprintf (buff, sizeof buff,
324 "Unindentified reference source '%.4s'",
326 for (i = 0; primary_sources[i].id; i++) {
327 if (memcmp (pkt->refid, primary_sources[i].id,
329 strcpy (buff, primary_sources[i].data);
334 strcpy (buff, get_hostname (pntohl(pkt->refid)));
335 proto_tree_add_item_format(ntp_tree, hf_ntp_refid, offset+12, 4, pkt->refid,
336 "Reference Clock ID: %s", buff);
337 /* Reference Timestamp: This is the time at which the local clock was
338 * last set or corrected.
340 proto_tree_add_item_format(ntp_tree, hf_ntp_reftime, offset+16, 8, pkt->reftime,
341 "Reference Clock Update Time: %s",
342 ntp_fmt_ts(pkt->reftime, buff));
343 /* Originate Timestamp: This is the time at which the request departed
344 * the client for the server.
346 proto_tree_add_item_format(ntp_tree, hf_ntp_org, offset+24, 8, pkt->org,
347 "Originate Time Stamp: %s",
348 ntp_fmt_ts(pkt->org, buff));
349 /* Receive Timestamp: This is the time at which the request arrived at
352 proto_tree_add_item_format(ntp_tree, hf_ntp_rec, offset+32, 8, pkt->rec,
353 "Receive Time Stamp: %s",
354 ntp_fmt_ts(pkt->rec, buff));
355 /* Transmit Timestamp: This is the time at which the reply departed the
356 * server for the client.
358 proto_tree_add_item_format(ntp_tree, hf_ntp_xmt, offset+40, 8, pkt->xmt,
359 "Transmit Time Stamp: %s",
360 ntp_fmt_ts(pkt->xmt, buff));
362 /* When the NTP authentication scheme is implemented, the Key Identifier
363 * and Message Digest fields contain the message authentication code
364 * (MAC) information defined in Appendix C of RFC-1305. Will print this as
367 if ( BYTES_ARE_IN_FRAME(offset, 50) )
368 proto_tree_add_item(ntp_tree, hf_ntp_keyid, offset+48, 4, pkt->keyid);
369 if ( BYTES_ARE_IN_FRAME(offset, 53) )
370 proto_tree_add_item(ntp_tree, hf_ntp_mac, offset+52, END_OF_FRAME, pkt->mac);
376 proto_register_ntp(void)
378 static hf_register_info hf[] = {
380 "Flags", "ntp.flags", FT_BYTES, BASE_HEX,
381 NULL, 0, "Flags (Leap/Version/Mode)" }},
382 { &hf_ntp_flags_li, {
383 "Leap Indicator", "ntp.flags.li", FT_UINT8, BASE_DEC,
384 VALS(li_types), 0, "Leap Indicator" }},
385 { &hf_ntp_flags_vn, {
386 "Version number", "ntp.flags.vn", FT_UINT8, BASE_DEC,
387 VALS(ver_nums), 0, "Version number" }},
388 { &hf_ntp_flags_mode, {
389 "Leap Indicator", "ntp.flags.mode", FT_UINT8, BASE_DEC,
390 VALS(mode_types), 0, "Leap Indicator" }},
392 "Peer Clock Stratum", "ntp.stratum", FT_BYTES, BASE_DEC,
393 NULL, 0, "Peer Clock Stratum" }},
395 "Peer Polling Interval", "ntp.ppoll", FT_BYTES, BASE_DEC,
396 NULL, 0, "Peer Polling Interval" }},
397 { &hf_ntp_precision, {
398 "Peer Clock Precision", "ntp.precision", FT_BYTES, BASE_DEC,
399 NULL, 0, "Peer Clock Precision" }},
400 { &hf_ntp_rootdelay, {
401 "Root Delay", "ntp.rootdelay", FT_BYTES, BASE_DEC,
402 NULL, 0, "Root Delay" }},
403 { &hf_ntp_rootdispersion, {
404 "Clock Dispersion", "ntp.rootdispersion", FT_BYTES, BASE_DEC,
405 NULL, 0, "Clock Dispersion" }},
407 "Reference Clock ID", "ntp.refid", FT_BYTES, BASE_NONE,
408 NULL, 0, "Reference Clock ID" }},
410 "Reference Clock Update Time", "ntp.reftime", FT_BYTES, BASE_NONE,
411 NULL, 0, "Reference Clock Update Time" }},
413 "Originate Time Stamp", "ntp.org", FT_BYTES, BASE_NONE,
414 NULL, 0, "Originate Time Stamp" }},
416 "Receive Time Stamp", "ntp.rec", FT_BYTES, BASE_NONE,
417 NULL, 0, "Receive Time Stamp" }},
419 "Transmit Time Stamp", "ntp.xmt", FT_BYTES, BASE_NONE,
420 NULL, 0, "Transmit Time Stamp" }},
422 "Key ID", "ntp.keyid", FT_BYTES, BASE_HEX,
423 NULL, 0, "Key ID" }},
425 "Message Authentication Code", "ntp.mac", FT_BYTES, BASE_HEX,
426 NULL, 0, "Message Authentication Code" }},
428 static gint *ett[] = {
433 proto_ntp = proto_register_protocol("Network Time Protocol", "ntp");
434 proto_register_field_array(proto_ntp, hf, array_length(hf));
435 proto_register_subtree_array(ett, array_length(ett));