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 * Format and print ntp packets. 22 * By Jeffrey Mogul/DECWRL 23 * loosely based on print-bootp.c 24 */ 25 26 #ifndef lint 27 static const char rcsid[] _U_ = 28 "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.43 2007-11-30 13:45:10 hannes Exp $ (LBL)"; 29 #endif 30 31 #ifdef HAVE_CONFIG_H 32 #include "config.h" 33 #endif 34 35 #include <tcpdump-stdinc.h> 36 37 #include <stdio.h> 38 #include <string.h> 39 #ifdef HAVE_STRFTIME 40 #include <time.h> 41 #endif 42 43 #include "interface.h" 44 #include "addrtoname.h" 45 #include "extract.h" 46 #ifdef MODEMASK 47 #undef MODEMASK /* Solaris sucks */ 48 #endif 49 #include "ntp.h" 50 51 static void p_sfix(const struct s_fixedpt *); 52 static void p_ntp_time(const struct l_fixedpt *); 53 static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *); 54 55 static const struct tok ntp_mode_values[] = { 56 { MODE_UNSPEC, "unspecified" }, 57 { MODE_SYM_ACT, "symmetric active" }, 58 { MODE_SYM_PAS, "symmetric passive" }, 59 { MODE_CLIENT, "Client" }, 60 { MODE_SERVER, "Server" }, 61 { MODE_BROADCAST, "Broadcast" }, 62 { MODE_RES1, "Reserved" }, 63 { MODE_RES2, "Reserved" }, 64 { 0, NULL } 65 }; 66 67 static const struct tok ntp_leapind_values[] = { 68 { NO_WARNING, "" }, 69 { PLUS_SEC, "+1s" }, 70 { MINUS_SEC, "-1s" }, 71 { ALARM, "clock unsynchronized" }, 72 { 0, NULL } 73 }; 74 75 static const struct tok ntp_stratum_values[] = { 76 { UNSPECIFIED, "unspecified" }, 77 { PRIM_REF, "primary reference" }, 78 { 0, NULL } 79 }; 80 81 /* 82 * Print ntp requests 83 */ 84 void 85 ntp_print(register const u_char *cp, u_int length) 86 { 87 register const struct ntpdata *bp; 88 int mode, version, leapind; 89 90 bp = (struct ntpdata *)cp; 91 92 TCHECK(bp->status); 93 94 version = (int)(bp->status & VERSIONMASK) >> 3; 95 printf("NTPv%d", version); 96 97 mode = bp->status & MODEMASK; 98 if (!vflag) { 99 printf (", %s, length %u", 100 tok2str(ntp_mode_values, "Unknown mode", mode), 101 length); 102 return; 103 } 104 105 printf (", length %u\n\t%s", 106 length, 107 tok2str(ntp_mode_values, "Unknown mode", mode)); 108 109 leapind = bp->status & LEAPMASK; 110 printf (", Leap indicator: %s (%u)", 111 tok2str(ntp_leapind_values, "Unknown", leapind), 112 leapind); 113 114 TCHECK(bp->stratum); 115 printf(", Stratum %u (%s)", 116 bp->stratum, 117 tok2str(ntp_stratum_values, (bp->stratum >=2 && bp->stratum<=15) ? "secondary reference" : "reserved", bp->stratum)); 118 119 TCHECK(bp->ppoll); 120 printf(", poll %u (%us)", bp->ppoll, 1 << bp->ppoll); 121 122 /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */ 123 TCHECK2(bp->root_delay, 0); 124 printf(", precision %d", bp->precision); 125 126 TCHECK(bp->root_delay); 127 fputs("\n\tRoot Delay: ", stdout); 128 p_sfix(&bp->root_delay); 129 130 TCHECK(bp->root_dispersion); 131 fputs(", Root dispersion: ", stdout); 132 p_sfix(&bp->root_dispersion); 133 134 TCHECK(bp->refid); 135 fputs(", Reference-ID: ", stdout); 136 /* Interpretation depends on stratum */ 137 switch (bp->stratum) { 138 139 case UNSPECIFIED: 140 printf("(unspec)"); 141 break; 142 143 case PRIM_REF: 144 if (fn_printn((u_char *)&(bp->refid), 4, snapend)) 145 goto trunc; 146 break; 147 148 case INFO_QUERY: 149 printf("%s INFO_QUERY", ipaddr_string(&(bp->refid))); 150 /* this doesn't have more content */ 151 return; 152 153 case INFO_REPLY: 154 printf("%s INFO_REPLY", ipaddr_string(&(bp->refid))); 155 /* this is too complex to be worth printing */ 156 return; 157 158 default: 159 printf("%s", ipaddr_string(&(bp->refid))); 160 break; 161 } 162 163 TCHECK(bp->ref_timestamp); 164 fputs("\n\t Reference Timestamp: ", stdout); 165 p_ntp_time(&(bp->ref_timestamp)); 166 167 TCHECK(bp->org_timestamp); 168 fputs("\n\t Originator Timestamp: ", stdout); 169 p_ntp_time(&(bp->org_timestamp)); 170 171 TCHECK(bp->rec_timestamp); 172 fputs("\n\t Receive Timestamp: ", stdout); 173 p_ntp_time(&(bp->rec_timestamp)); 174 175 TCHECK(bp->xmt_timestamp); 176 fputs("\n\t Transmit Timestamp: ", stdout); 177 p_ntp_time(&(bp->xmt_timestamp)); 178 179 fputs("\n\t Originator - Receive Timestamp: ", stdout); 180 p_ntp_delta(&(bp->org_timestamp), &(bp->rec_timestamp)); 181 182 fputs("\n\t Originator - Transmit Timestamp: ", stdout); 183 p_ntp_delta(&(bp->org_timestamp), &(bp->xmt_timestamp)); 184 185 if ( (sizeof(struct ntpdata) - length) == 16) { /* Optional: key-id */ 186 TCHECK(bp->key_id); 187 printf("\n\tKey id: %u", bp->key_id); 188 } else if ( (sizeof(struct ntpdata) - length) == 0) { /* Optional: key-id + authentication */ 189 TCHECK(bp->key_id); 190 printf("\n\tKey id: %u", bp->key_id); 191 TCHECK2(bp->message_digest, sizeof (bp->message_digest)); 192 printf("\n\tAuthentication: %08x%08x%08x%08x", 193 EXTRACT_32BITS(bp->message_digest), 194 EXTRACT_32BITS(bp->message_digest + 4), 195 EXTRACT_32BITS(bp->message_digest + 8), 196 EXTRACT_32BITS(bp->message_digest + 12)); 197 } 198 return; 199 200 trunc: 201 fputs(" [|ntp]", stdout); 202 } 203 204 static void 205 p_sfix(register const struct s_fixedpt *sfp) 206 { 207 register int i; 208 register int f; 209 register float ff; 210 211 i = EXTRACT_16BITS(&sfp->int_part); 212 f = EXTRACT_16BITS(&sfp->fraction); 213 ff = f / 65536.0; /* shift radix point by 16 bits */ 214 f = ff * 1000000.0; /* Treat fraction as parts per million */ 215 printf("%d.%06d", i, f); 216 } 217 218 #define FMAXINT (4294967296.0) /* floating point rep. of MAXINT */ 219 220 static void 221 p_ntp_time(register const struct l_fixedpt *lfp) 222 { 223 register int32_t i; 224 register u_int32_t uf; 225 register u_int32_t f; 226 register float ff; 227 228 i = EXTRACT_32BITS(&lfp->int_part); 229 uf = EXTRACT_32BITS(&lfp->fraction); 230 ff = uf; 231 if (ff < 0.0) /* some compilers are buggy */ 232 ff += FMAXINT; 233 ff = ff / FMAXINT; /* shift radix point by 32 bits */ 234 f = ff * 1000000000.0; /* treat fraction as parts per billion */ 235 printf("%u.%09d", i, f); 236 237 #ifdef HAVE_STRFTIME 238 /* 239 * print the time in human-readable format. 240 */ 241 if (i) { 242 time_t seconds = i - JAN_1970; 243 struct tm *tm; 244 char time_buf[128]; 245 246 tm = localtime(&seconds); 247 strftime(time_buf, sizeof (time_buf), "%Y/%m/%d %H:%M:%S", tm); 248 printf (" (%s)", time_buf); 249 } 250 #endif 251 } 252 253 /* Prints time difference between *lfp and *olfp */ 254 static void 255 p_ntp_delta(register const struct l_fixedpt *olfp, 256 register const struct l_fixedpt *lfp) 257 { 258 register int32_t i; 259 register u_int32_t u, uf; 260 register u_int32_t ou, ouf; 261 register u_int32_t f; 262 register float ff; 263 int signbit; 264 265 u = EXTRACT_32BITS(&lfp->int_part); 266 ou = EXTRACT_32BITS(&olfp->int_part); 267 uf = EXTRACT_32BITS(&lfp->fraction); 268 ouf = EXTRACT_32BITS(&olfp->fraction); 269 if (ou == 0 && ouf == 0) { 270 p_ntp_time(lfp); 271 return; 272 } 273 274 i = u - ou; 275 276 if (i > 0) { /* new is definitely greater than old */ 277 signbit = 0; 278 f = uf - ouf; 279 if (ouf > uf) /* must borrow from high-order bits */ 280 i -= 1; 281 } else if (i < 0) { /* new is definitely less than old */ 282 signbit = 1; 283 f = ouf - uf; 284 if (uf > ouf) /* must carry into the high-order bits */ 285 i += 1; 286 i = -i; 287 } else { /* int_part is zero */ 288 if (uf > ouf) { 289 signbit = 0; 290 f = uf - ouf; 291 } else { 292 signbit = 1; 293 f = ouf - uf; 294 } 295 } 296 297 ff = f; 298 if (ff < 0.0) /* some compilers are buggy */ 299 ff += FMAXINT; 300 ff = ff / FMAXINT; /* shift radix point by 32 bits */ 301 f = ff * 1000000000.0; /* treat fraction as parts per billion */ 302 if (signbit) 303 putchar('-'); 304 else 305 putchar('+'); 306 printf("%d.%09d", i, f); 307 } 308 309