xref: /openbsd-src/usr.sbin/tcpdump/print-ntp.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: print-ntp.c,v 1.14 2009/03/04 16:54:42 stevesk 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 #ifndef lint
29 static const char rcsid[] =
30     "@(#) $Id: print-ntp.c,v 1.14 2009/03/04 16:54:42 stevesk Exp $ (LBL)";
31 #endif
32 
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 
37 struct mbuf;
38 struct rtentry;
39 #include <net/if.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <string.h>
47 
48 #include "interface.h"
49 #include "addrtoname.h"
50 #ifdef MODEMASK
51 #undef MODEMASK					/* Solaris sucks */
52 #endif
53 #include "ntp.h"
54 
55 static void p_sfix(const struct s_fixedpt *);
56 static void p_ntp_time(const struct l_fixedpt *);
57 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
58 
59 /*
60  * Print ntp requests
61  */
62 void
63 ntp_print(register const u_char *cp, u_int length)
64 {
65 	register const struct ntpdata *bp;
66 	int mode, version, leapind;
67 
68 	bp = (struct ntpdata *)cp;
69 	/* Note funny sized packets */
70 	if (length != sizeof(struct ntpdata))
71 		(void)printf(" [len=%d]", length);
72 
73 	TCHECK(bp->status);
74 
75 	version = (int)(bp->status & VERSIONMASK) >> 3;
76 	printf(" v%d", version);
77 
78 	leapind = bp->status & LEAPMASK;
79 	switch (leapind) {
80 
81 	case NO_WARNING:
82 		break;
83 
84 	case ALARM:
85 		fputs(" alarm", stdout);
86 		break;
87 
88 	case PLUS_SEC:
89 		fputs(" +1s", stdout);
90 		break;
91 
92 	case MINUS_SEC:
93 		fputs(" -1s", stdout);
94 		break;
95 	}
96 
97 	mode = bp->status & MODEMASK;
98 	switch (mode) {
99 
100 	case MODE_UNSPEC:	/* unspecified */
101 		fputs(" unspec", stdout);
102 		break;
103 
104 	case MODE_SYM_ACT:	/* symmetric active */
105 		fputs(" sym_act", stdout);
106 		break;
107 
108 	case MODE_SYM_PAS:	/* symmetric passive */
109 		fputs(" sym_pas", stdout);
110 		break;
111 
112 	case MODE_CLIENT:	/* client */
113 		fputs(" client", stdout);
114 		break;
115 
116 	case MODE_SERVER:	/* server */
117 		fputs(" server", stdout);
118 		break;
119 
120 	case MODE_BROADCAST:	/* broadcast */
121 		fputs(" bcast", stdout);
122 		break;
123 
124 	case MODE_RES1:		/* reserved */
125 		fputs(" res1", stdout);
126 		break;
127 
128 	case MODE_RES2:		/* reserved */
129 		fputs(" res2", stdout);
130 		break;
131 
132 	}
133 
134 	TCHECK(bp->stratum);
135 	printf(" strat %d", bp->stratum);
136 
137 	TCHECK(bp->ppoll);
138 	printf(" poll %d", bp->ppoll);
139 
140 	/* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
141 	TCHECK2(bp->distance, 0);
142 	printf(" prec %d", bp->precision);
143 
144 	if (!vflag)
145 		return;
146 
147 	TCHECK(bp->distance);
148 	fputs(" dist ", stdout);
149 	p_sfix(&bp->distance);
150 
151 	TCHECK(bp->dispersion);
152 	fputs(" disp ", stdout);
153 	p_sfix(&bp->dispersion);
154 
155 	TCHECK(bp->refid);
156 	fputs(" ref ", stdout);
157 	/* Interpretation depends on stratum */
158 	switch (bp->stratum) {
159 
160 	case UNSPECIFIED:
161 		printf("(unspec)");
162 		break;
163 
164 	case PRIM_REF:
165 		fn_printn((u_char *)&bp->refid, sizeof(bp->refid), NULL);
166 		break;
167 
168 	case INFO_QUERY:
169 		printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
170 		/* this doesn't have more content */
171 		return;
172 
173 	case INFO_REPLY:
174 		printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
175 		/* this is too complex to be worth printing */
176 		return;
177 
178 	default:
179 		printf("%s", ipaddr_string(&(bp->refid)));
180 		break;
181 	}
182 
183 	TCHECK(bp->reftime);
184 	putchar('@');
185 	p_ntp_time(&(bp->reftime));
186 
187 	TCHECK(bp->org);
188 	fputs(" orig ", stdout);
189 	p_ntp_time(&(bp->org));
190 
191 	TCHECK(bp->rec);
192 	fputs(" rec ", stdout);
193 	p_ntp_delta(&(bp->org), &(bp->rec));
194 
195 	TCHECK(bp->xmt);
196 	fputs(" xmt ", stdout);
197 	p_ntp_delta(&(bp->org), &(bp->xmt));
198 
199 	return;
200 
201 trunc:
202 	fputs(" [|ntp]", stdout);
203 }
204 
205 static void
206 p_sfix(register const struct s_fixedpt *sfp)
207 {
208 	register int i;
209 	register int f;
210 	register float ff;
211 
212 	i = ntohs(sfp->int_part);
213 	f = ntohs(sfp->fraction);
214 	ff = f / 65536.0;	/* shift radix point by 16 bits */
215 	f = ff * 1000000.0;	/* Treat fraction as parts per million */
216 	printf("%d.%06d", i, f);
217 }
218 
219 #define	FMAXINT	(4294967296.0)	/* floating point rep. of MAXINT */
220 
221 static void
222 p_ntp_time(register const struct l_fixedpt *lfp)
223 {
224 	register int32_t i;
225 	register u_int32_t uf;
226 	register u_int32_t f;
227 	register float ff;
228 
229 	i = ntohl(lfp->int_part);
230 	uf = ntohl(lfp->fraction);
231 	ff = uf;
232 	if (ff < 0.0)		/* some compilers are buggy */
233 		ff += FMAXINT;
234 	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
235 	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
236 	printf("%u.%09d", i, f);
237 }
238 
239 /* Prints time difference between *lfp and *olfp */
240 static void
241 p_ntp_delta(register const struct l_fixedpt *olfp,
242 	    register const struct l_fixedpt *lfp)
243 {
244 	register int32_t i;
245 	register u_int32_t uf;
246 	register u_int32_t ouf;
247 	register u_int32_t f;
248 	register float ff;
249 	int signbit;
250 
251 	i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
252 
253 	uf = ntohl(lfp->fraction);
254 	ouf = ntohl(olfp->fraction);
255 
256 	if (i > 0) {		/* new is definitely greater than old */
257 		signbit = 0;
258 		f = uf - ouf;
259 		if (ouf > uf)	/* must borrow from high-order bits */
260 			i -= 1;
261 	} else if (i < 0) {	/* new is definitely less than old */
262 		signbit = 1;
263 		f = ouf - uf;
264 		if (uf > ouf)	/* must carry into the high-order bits */
265 			i += 1;
266 		i = -i;
267 	} else {		/* int_part is zero */
268 		if (uf > ouf) {
269 			signbit = 0;
270 			f = uf - ouf;
271 		} else {
272 			signbit = 1;
273 			f = ouf - uf;
274 		}
275 	}
276 
277 	ff = f;
278 	if (ff < 0.0)		/* some compilers are buggy */
279 		ff += FMAXINT;
280 	ff = ff / FMAXINT;	/* shift radix point by 32 bits */
281 	f = ff * 1000000000.0;	/* treat fraction as parts per billion */
282 	if (signbit)
283 		putchar('-');
284 	else
285 		putchar('+');
286 	printf("%d.%09d", i, f);
287 }
288