summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_ntp.c
blob: 4f6fae3986d285076a1dd890ee0a0b3fd87daeb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <fcntl.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <tzfile.h>
#include "snoop.h"
#include "ntp.h"

/*
 * In verbose mode, how many octets of the control-mode data payload
 * are displayed per line of output.  The value 64 fits well on an
 * 80-column screen and, as a power of 2, is easily correlated to
 * hexadecimal output.
 */
#define	OCTETS_PER_LINE	64

extern char *dlc_header;

static	char	*show_leap(int);
static	char	*show_mode(int);
static	char	*show_ref(int, ulong_t);
static	char	*show_time(struct l_fixedpt);
static	double	s_fixed_to_double(struct s_fixedpt *);
static	char	*iso_date_time(time_t);
static	char	*show_operation(int);

int
interpret_ntp(int flags, struct ntpdata *ntp_pkt, int fraglen)
{
	unsigned int	i, j, macbytes;
	unsigned int	proto_version;
	unsigned int	datalen;
	unsigned int	linelen = OCTETS_PER_LINE;
	unsigned int	sofar = 0;

	char	*datap;
	char	hbuf[2 * MAC_OCTETS_MAX + 1];
	static	char *hexstr = "0123456789ABCDEF";

	union	ntp_pkt_buf {
		struct	ntpdata ntp_msg;
		union ntpc_buf {
			struct	ntp_control chdr;
			uchar_t	data2[NTPC_DATA_MAXLEN - 1];
		} ntpc_msg;
		union ntpp_buf {
			struct	ntp_private phdr;
			uchar_t	data2[1];
		} ntpp_msg;
	} fragbuf;

	struct	ntpdata		*ntp = &fragbuf.ntp_msg;
	struct	ntp_control	*ntpc = (struct ntp_control *)&fragbuf.ntpc_msg;
	struct	ntp_private	*ntpp = (struct ntp_private *)&fragbuf.ntpp_msg;

	/*
	 * Copying packet contents into a local buffer avoids
	 * problems of interpretation if the packet is truncated.
	 */
	(void) memcpy(&fragbuf, ntp_pkt, MIN(sizeof (fragbuf), fraglen));

	if (flags & F_SUM) {
		switch (ntp->li_vn_mode & NTPMODEMASK) {
		case MODE_SYM_ACT:
		case MODE_SYM_PAS:
		case MODE_CLIENT:
		case MODE_SERVER:
		case MODE_BROADCAST:
		    (void) sprintf(get_sum_line(),
			"NTP  %s [st=%hd] (%s)",
			show_mode(ntp->li_vn_mode & NTPMODEMASK),
			ntp->stratum,
			show_time(ntp->xmt));
		    break;
		case MODE_CONTROL:
		    (void) sprintf(get_sum_line(),
			"NTP  %s "
			"(Flags/op=0x%02x Seq=%hu Status=0x%04hx Assoc=%hu)",
			show_mode(ntpc->li_vn_mode & NTPMODEMASK),
			ntpc->r_m_e_op,
			ntohs(ntpc->sequence),
			ntohs(ntpc->status),
			ntohs(ntpc->associd));
		    break;
		default:
		    (void) sprintf(get_sum_line(),
			"NTP  %s",
			show_mode(ntpp->rm_vn_mode & NTPMODEMASK));
		    break;
		}
	}

	proto_version = (ntp->li_vn_mode & VERSIONMASK) >> 3;

	if (flags & F_DTAIL) {
		show_header("NTP:  ", "Network Time Protocol", fraglen);
		show_space();
		switch (ntp->li_vn_mode & NTPMODEMASK) {
		case MODE_SYM_ACT:
		case MODE_SYM_PAS:
		case MODE_CLIENT:
		case MODE_SERVER:
		case MODE_BROADCAST:
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->li_vn_mode -
			dlc_header, 1),
			"Leap    = 0x%x (%s)",
			(int)(ntp->li_vn_mode & LEAPMASK) >> 6,
			show_leap(ntp->li_vn_mode & LEAPMASK));
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->li_vn_mode -
			dlc_header, 1),
			"Version = %lu", proto_version);
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->li_vn_mode -
			dlc_header, 1),
			"Mode    = %hu (%s)",
			ntp->li_vn_mode & NTPMODEMASK,
			show_mode(ntp->li_vn_mode & NTPMODEMASK));
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->stratum -
			dlc_header, 1),
			"Stratum = %d (%s)",
			ntp->stratum,
			ntp->stratum == 0 ? "unspecified" :
			ntp->stratum == 1 ? "primary reference" :
			"secondary reference");
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->ppoll -
			dlc_header, 1),	"Poll    = %hu", ntp->ppoll);
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->precision -
			dlc_header, 1),
			"Precision = %d seconds",
			ntp->precision);
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntp->distance.int_part -
			dlc_header, 1),
			"Synchronizing distance   = 0x%04x.%04x  (%f)",
			ntohs(ntp->distance.int_part),
			ntohs(ntp->distance.fraction),
			s_fixed_to_double(&ntp->distance));
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntp->dispersion.int_part -
			dlc_header, 1),
			"Synchronizing dispersion = 0x%04x.%04x  (%f)",
			ntohs(ntp->dispersion.int_part),
			ntohs(ntp->dispersion.fraction),
			s_fixed_to_double(&ntp->dispersion));
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->refid -
			dlc_header, 1), "Reference clock = %s",
			show_ref(ntp->stratum, ntp->refid));

		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntp->reftime.int_part - dlc_header,
			1), "Reference time = 0x%08lx.%08lx (%s)",
			ntohl(ntp->reftime.int_part),
			ntohl(ntp->reftime.fraction),
			show_time(ntp->reftime));

		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntp->org.int_part - dlc_header, 1),
			"Originate time = 0x%08lx.%08lx (%s)",
			ntohl(ntp->org.int_part),
			ntohl(ntp->org.fraction),
			show_time(ntp->org));

		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntp->rec.int_part - dlc_header, 1),
			"Receive   time = 0x%08lx.%08lx (%s)",
			ntohl(ntp->rec.int_part),
			ntohl(ntp->rec.fraction),
			show_time(ntp->rec));

		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntp->xmt.int_part - dlc_header, 1),
			"Transmit  time = 0x%08lx.%08lx (%s)",
			ntohl(ntp->xmt.int_part),
			ntohl(ntp->xmt.fraction),
			show_time(ntp->xmt));

		    if (proto_version > 3 ||
			fraglen < (LEN_PKT_NOMAC + MAC_OCTETS_MIN)) {
				/*
				 * A newer protocol version we can't parse,
				 * or v3 packet with no valid authentication.
				 */
				break;
		    }
		    (void) sprintf(get_line((char *)ntp->keyid -
			dlc_header, 1),
			"Key ID  = %8lu", ntohl(ntp->keyid));

		    macbytes = fraglen - (LEN_PKT_NOMAC + sizeof (uint32_t));

		    for (i = 0, j = 0; i < macbytes; i++) {
			    hbuf[j++] = hexstr[ntp->mac[i] >> 4 & 0x0f];
			    hbuf[j++] = hexstr[ntp->mac[i] & 0x0f];
		    }
		    hbuf[j] = '\0';
		    (void) sprintf(get_line((char *)ntp->mac -
			dlc_header, 1),
			"Authentication code = %s", hbuf);
		    break;

		case MODE_CONTROL:
		    /* NTP Control Message, mode 6 */

		    (void) sprintf(get_line((char *)(uintptr_t)ntp->li_vn_mode -
			dlc_header, 1),
			"Leap    = 0x%x (%s)",
			(int)(ntp->li_vn_mode & LEAPMASK) >> 6,
			show_leap(ntp->li_vn_mode & LEAPMASK));
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->li_vn_mode -
			dlc_header, 1),
			"Version = %lu", proto_version);
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->li_vn_mode -
			dlc_header, 1),
			"Mode    = %hu (%s)",
			ntp->li_vn_mode & NTPMODEMASK,
			show_mode(ntp->li_vn_mode & NTPMODEMASK));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->r_m_e_op -
			dlc_header, 1),
			"Flags and operation code = 0x%02x",
			ntpc->r_m_e_op);
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->r_m_e_op -
			dlc_header, 1),
			"      %s",
			getflag(ntpc->r_m_e_op, CTL_RESPONSE, "response",
			"request"));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->r_m_e_op -
			dlc_header, 1),
			"      %s",
			getflag(ntpc->r_m_e_op, CTL_ERROR, "error",
			"success"));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->r_m_e_op -
			dlc_header, 1),
			"      %s",
			getflag(ntpc->r_m_e_op, CTL_MORE, "more",
			"no more"));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->r_m_e_op -
			dlc_header, 1),
			"      ...x xxxx = %hd (%s)",
			ntpc->r_m_e_op & CTL_OP_MASK,
			show_operation(ntpc->r_m_e_op & CTL_OP_MASK));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->sequence -
			dlc_header, 1),
			"Sequence = %hu",
			ntohs(ntpc->sequence));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->status -
			dlc_header, 1),
			"Status = 0x%04hx",
			ntohs(ntpc->status));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->associd -
			dlc_header, 1),
			"Assoc ID = %hu",
			ntohs(ntpc->associd));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->offset -
			dlc_header, 1),
			"Data offset = %hu",
			ntohs(ntpc->offset));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpc->count -
			dlc_header, 1),
			"Data bytes = %hu",
			ntohs(ntpc->count));
		    datalen = ntohs(ntpc->count);
		    if (datalen == 0) {
			    break;
		    } else if (datalen > NTPC_DATA_MAXLEN) {
			    datalen = NTPC_DATA_MAXLEN;
		    }
		    show_space();
		    datap = (char *)ntpc->data;
		    do {
			    (void) sprintf(get_line(datap -
				dlc_header, 1),
				"\"%s\"",
				show_string(datap, linelen, datalen));
			    sofar += linelen;
			    datap += linelen;
			    if ((sofar + linelen) > datalen) {
				    linelen = datalen - sofar;
			    }
		    } while (sofar < datalen);
		    show_trailer();
		    break;

		case MODE_PRIVATE:
		    /* NTP Private Message, mode 7 */

		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->rm_vn_mode - dlc_header, 1),
			"Version = %hu", INFO_VERSION(ntpp->rm_vn_mode));
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->rm_vn_mode - dlc_header, 1),
			"Mode    = %hu (%s)", INFO_MODE(ntpp->rm_vn_mode),
			show_mode(INFO_MODE(ntpp->rm_vn_mode)));
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->rm_vn_mode - dlc_header, 1),
			"Flags = 0x%02hx", ntpp->rm_vn_mode);
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->rm_vn_mode - dlc_header, 1),
			"      %s",
			getflag(ntpp->rm_vn_mode, RESP_BIT, "response",
			"request"));
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->rm_vn_mode - dlc_header, 1),
			"      %s",
			getflag(ntpp->rm_vn_mode, MORE_BIT, "more", "no more"));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpp->auth_seq -
			dlc_header, 1),
			"Authentication and sequence = 0x%02x", ntpp->auth_seq);
		    (void) sprintf(get_line((char *)(uintptr_t)ntpp->auth_seq -
			dlc_header, 1),
			"      %s",
			getflag(ntpp->auth_seq, AUTH_BIT, "authenticated",
			"unauthenticated"));
		    (void) sprintf(get_line((char *)(uintptr_t)ntpp->auth_seq -
			dlc_header, 1),
			"      .xxx xxxx = %hu (sequence number)",
			INFO_SEQ(ntpp->auth_seq));
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->implementation - dlc_header,
			1), "Implementation = %hu", ntpp->implementation);
		    (void) sprintf(get_line((char *)(uintptr_t)ntpp->request -
			dlc_header, 1), "Request = %hu", ntpp->request);
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->err_nitems - dlc_header, 1),
			"Error = %hu", INFO_ERR(ntpp->err_nitems));
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->err_nitems - dlc_header, 1),
			"Items = %hu", INFO_NITEMS(ntpp->err_nitems));
		    (void) sprintf(get_line(
			(char *)(uintptr_t)ntpp->mbz_itemsize - dlc_header, 1),
			"Item size = %hu", INFO_ITEMSIZE(ntpp->mbz_itemsize));
		    break;

		default:
		    /* Unknown mode */
		    (void) sprintf(get_line((char *)(uintptr_t)ntp->li_vn_mode -
			dlc_header, 1),	"Mode    = %hu (%s)",
			ntp->li_vn_mode & NTPMODEMASK,
			show_mode(ntp->li_vn_mode & NTPMODEMASK));
		    break;
		}
	}

	return (fraglen);
}

char *
show_leap(int leap)
{
	switch (leap) {
	case NO_WARNING: return ("OK");
	case PLUS_SEC:	return ("add a second (61 seconds)");
	case MINUS_SEC: return ("minus a second (59 seconds)");
	case ALARM:	return ("alarm condition (clock unsynchronized)");
	default:	return ("unknown");
	}
}

char *
show_mode(int mode)
{
	switch (mode) {
	case MODE_UNSPEC:	return ("unspecified");
	case MODE_SYM_ACT:	return ("symmetric active");
	case MODE_SYM_PAS:	return ("symmetric passive");
	case MODE_CLIENT:	return ("client");
	case MODE_SERVER:	return ("server");
	case MODE_BROADCAST:	return ("broadcast");
	case MODE_CONTROL:	return ("control");
	case MODE_PRIVATE:	return ("private");
	default:		return ("unknown");
	}
}

char *
show_ref(int mode, ulong_t refid)
{
	static char buff[MAXHOSTNAMELEN + 32];
	struct in_addr host;
	extern char *inet_ntoa();

	switch (mode) {
	case 0:
	case 1:
		(void) strncpy(buff, (char *)&refid, 4);
		buff[4] = '\0';
		break;

	default:
		host.s_addr = refid;
		(void) sprintf(buff, "%s (%s)",
		    inet_ntoa(host),
		    addrtoname(AF_INET, &host));
		break;
	}

	return (buff);
}

/*
 *  Here we have to worry about the high order bit being signed
 */
double
s_fixed_to_double(struct s_fixedpt *t)
{
	double a;

	if (ntohs(t->int_part) & 0x8000) {
		a = ntohs((int)(~t->fraction) & 0xFFFF);
		a = a / 65536.0;	/* shift dec point over by 16 bits */
		a +=  ntohs((int)(~t->int_part) & 0xFFFF);
		a = -a;
	} else {
		a = ntohs(t->fraction);
		a = a / 65536.0;	/* shift dec point over by 16 bits */
		a += ntohs(t->int_part);
	}
	return (a);
}

/*
 * Consistent with RFC-3339, ISO 8601.
 */
char *
iso_date_time(time_t input_time)
{
	struct tm	*time_parts;
	static char	tbuf[sizeof ("yyyy-mm-dd hh:mm:ss")];

	time_parts = localtime(&input_time);
	(void) strftime(tbuf, sizeof (tbuf), "%Y-%m-%d %H:%M:%S", time_parts);
	return (tbuf);
}

/*
 * The base of NTP timestamps is 1900-01-01 00:00:00.00000
 */
char *
show_time(struct l_fixedpt pkt_time)
{
	struct l_fixedpt net_time;
	unsigned long	fracsec;
	static char	buff[32];

	if (pkt_time.int_part == 0) {
		buff[0] = '\0';
		return (buff);
	}

	net_time.int_part = ntohl(pkt_time.int_part) - JAN_1970;
	net_time.fraction = ntohl(pkt_time.fraction);

	fracsec = net_time.fraction / 42949;	/* fract / (2**32/10**6) */

	(void) strlcpy(buff, iso_date_time(net_time.int_part), sizeof (buff));
	(void) snprintf(buff, sizeof (buff), "%s.%05lu", buff, fracsec);

	return (buff);
}

char *
show_operation(int op)
{
	switch (op) {
	case CTL_OP_UNSPEC:	return ("unspecified");
	case CTL_OP_READSTAT:	return ("read stats");
	case CTL_OP_READVAR:	return ("read var");
	case CTL_OP_WRITEVAR:	return ("write var");
	case CTL_OP_READCLOCK:	return ("read clock");
	case CTL_OP_WRITECLOCK: return ("write clock");
	case CTL_OP_SETTRAP:	return ("set trap");
	case CTL_OP_ASYNCMSG:	return ("async msg");
	case CTL_OP_UNSETTRAP:	return ("unset trap");
	default:		return ("unknown");
	}
}