xref: /openbsd/usr.sbin/tcpdump/print-ntp.c (revision f6aab3d8)
1 /*	$OpenBSD: print-ntp.c,v 1.20 2021/12/01 18:28:46 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * Format and print ntp packets.
24  *	By Jeffrey Mogul/DECWRL
25  *	loosely based on print-bootp.c
26  */
27 
28 #include <sys/time.h>
29 #include <sys/socket.h>
30 
31 #include <net/if.h>
32 
33 #include <netinet/in.h>
34 #include <netinet/if_ether.h>
35 
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "interface.h"
41 #include "addrtoname.h"
42 #ifdef MODEMASK
43 #undef MODEMASK					/* Solaris sucks */
44 #endif
45 #include "ntp.h"
46 
47 static void p_sfix(const struct s_fixedpt *);
48 static void p_ntp_time(const struct l_fixedpt *);
49 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
50 
51 /*
52  * Print ntp requests
53  */
54 void
55 ntp_print(const u_char *cp, u_int length)
56 {
57 	const struct ntpdata *bp;
58 	int mode, version, leapind;
59 
60 	bp = (struct ntpdata *)cp;
61 	/* Note funny sized packets */
62 	if (length != sizeof(struct ntpdata))
63 		printf(" [len=%d]", length);
64 
65 	TCHECK(bp->status);
66 
67 	version = (int)(bp->status & VERSIONMASK) >> 3;
68 	printf("v%d", version);
69 
70 	leapind = bp->status & LEAPMASK;
71 	switch (leapind) {
72 
73 	case NO_WARNING:
74 		break;
75 
76 	case ALARM:
77 		printf(" alarm");
78 		break;
79 
80 	case PLUS_SEC:
81 		printf(" +1s");
82 		break;
83 
84 	case MINUS_SEC:
85 		printf(" -1s");
86 		break;
87 	}
88 
89 	mode = bp->status & MODEMASK;
90 	switch (mode) {
91 
92 	case MODE_UNSPEC:	/* unspecified */
93 		printf(" unspec");
94 		break;
95 
96 	case MODE_SYM_ACT:	/* symmetric active */
97 		printf(" sym_act");
98 		break;
99 
100 	case MODE_SYM_PAS:	/* symmetric passive */
101 		printf(" sym_pas");
102 		break;
103 
104 	case MODE_CLIENT:	/* client */
105 		printf(" client");
106 		break;
107 
108 	case MODE_SERVER:	/* server */
109 		printf(" server");
110 		break;
111 
112 	case MODE_BROADCAST:	/* broadcast */
113 		printf(" bcast");
114 		break;
115 
116 	case MODE_RES1:		/* reserved */
117 		printf(" res1");
118 		break;
119 
120 	case MODE_RES2:		/* reserved */
121 		printf(" res2");
122 		break;
123 
124 	}
125 
126 	TCHECK(bp->stratum);
127 	printf(" strat %d", bp->stratum);
128 
129 	TCHECK(bp->ppoll);
130 	printf(" poll %d", bp->ppoll);
131 
132 	/* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
133 	TCHECK2(bp->distance, 0);
134 	printf(" prec %d", bp->precision);
135 
136 	if (!vflag)
137 		return;
138 
139 	TCHECK(bp->distance);
140 	printf(" dist ");
141 	p_sfix(&bp->distance);
142 
143 	TCHECK(bp->dispersion);
144 	printf(" disp ");
145 	p_sfix(&bp->dispersion);
146 
147 	TCHECK(bp->refid);
148 	printf(" ref ");
149 	/* Interpretation depends on stratum */
150 	switch (bp->stratum) {
151 
152 	case UNSPECIFIED:
153 		printf("(unspec)");
154 		break;
155 
156 	case PRIM_REF:
157 		fn_printn((u_char *)&bp->refid, sizeof(bp->refid), NULL);
158 		break;
159 
160 	case INFO_QUERY:
161 		printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
162 		/* this doesn't have more content */
163 		return;
164 
165 	case INFO_REPLY:
166 		printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
167 		/* this is too complex to be worth printing */
168 		return;
169 
170 	default:
171 		printf("%s", ipaddr_string(&(bp->refid)));
172 		break;
173 	}
174 
175 	TCHECK(bp->reftime);
176 	putchar('@');
177 	p_ntp_time(&(bp->reftime));
178 
179 	TCHECK(bp->org);
180 	printf(" orig ");
181 	p_ntp_time(&(bp->org));
182 
183 	TCHECK(bp->rec);
184 	printf(" rec ");
185 	p_ntp_delta(&(bp->org), &(bp->rec));
186 
187 	TCHECK(bp->xmt);
188 	printf(" xmt ");
189 	p_ntp_delta(&(bp->org), &(bp->xmt));
190 
191 	return;
192 
193 trunc:
194 	printf(" [|ntp]");
195 }
196 
197 static void
198 p_sfix(const struct s_fixedpt *sfp)
199 {
200 	int i;
201 	int f;
202 	float ff;
203 
204 	i = ntohs(sfp->int_part);
205 	f = ntohs(sfp->fraction);
206 	ff = f / 65536.0;	/* shift radix point by 16 bits */
207 	f = ff * 1000000.0;	/* Treat fraction as parts per million */
208 	printf("%d.%06d", i, f);
209 }
210 
211 #define	FMAXINT	(4294967296.0)	/* floating point rep. of MAXINT */
212 
213 static void
214 p_ntp_time(const struct l_fixedpt *lfp)
215 {
216 	int32_t i;
217 	u_int32_t uf;
218 	u_int32_t f;
219 	float ff;
220 
221 	i = ntohl(lfp->int_part);
222 	uf = ntohl(lfp->fraction);
223 	ff = uf;
224 	if (ff < 0.0)		/* some compilers are buggy */
225 		ff += FMAXINT;
226 	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
227 	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
228 	printf("%u.%09d", i, f);
229 }
230 
231 /* Prints time difference between *lfp and *olfp */
232 static void
233 p_ntp_delta(const struct l_fixedpt *olfp, const struct l_fixedpt *lfp)
234 {
235 	int32_t i;
236 	u_int32_t uf;
237 	u_int32_t ouf;
238 	u_int32_t f;
239 	float ff;
240 	int signbit;
241 
242 	i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
243 
244 	uf = ntohl(lfp->fraction);
245 	ouf = ntohl(olfp->fraction);
246 
247 	if (i > 0) {		/* new is definitely greater than old */
248 		signbit = 0;
249 		f = uf - ouf;
250 		if (ouf > uf)	/* must borrow from high-order bits */
251 			i -= 1;
252 	} else if (i < 0) {	/* new is definitely less than old */
253 		signbit = 1;
254 		f = ouf - uf;
255 		if (uf > ouf)	/* must carry into the high-order bits */
256 			i += 1;
257 		i = -i;
258 	} else {		/* int_part is zero */
259 		if (uf > ouf) {
260 			signbit = 0;
261 			f = uf - ouf;
262 		} else {
263 			signbit = 1;
264 			f = ouf - uf;
265 		}
266 	}
267 
268 	ff = f;
269 	if (ff < 0.0)		/* some compilers are buggy */
270 		ff += FMAXINT;
271 	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
272 	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
273 	if (signbit)
274 		putchar('-');
275 	else
276 		putchar('+');
277 	printf("%d.%09d", i, f);
278 }
279