X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/6191f36146f5d286304e9b6e893477fe509d83ab..6e12d87355718f309ce9f33183ff8235b9c5eb0d:/print-ntp.c diff --git a/print-ntp.c b/print-ntp.c index 4960dc82..21bb7374 100644 --- a/print-ntp.c +++ b/print-ntp.c @@ -23,11 +23,6 @@ * loosely based on print-bootp.c */ -#ifndef lint -static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.42 2005-05-06 07:56:53 guy Exp $ (LBL)"; -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -43,16 +38,141 @@ static const char rcsid[] _U_ = #include "interface.h" #include "addrtoname.h" #include "extract.h" + +/* + * Based on ntp.h from the U of MD implementation + * This file is based on Version 2 of the NTP spec (RFC1119). + */ + +/* + * Definitions for the masses + */ +#define JAN_1970 2208988800U /* 1970 - 1900 in seconds */ + +/* + * Structure definitions for NTP fixed point values + * + * 0 1 2 3 + * 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Integer Part | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Fraction Part | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * 0 1 2 3 + * 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Integer Part | Fraction Part | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +*/ +struct l_fixedpt { + u_int32_t int_part; + u_int32_t fraction; +}; + +struct s_fixedpt { + u_int16_t int_part; + u_int16_t fraction; +}; + +/* rfc2030 + * 1 2 3 + * 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |LI | VN |Mode | Stratum | Poll | Precision | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Root Delay | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Root Dispersion | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Reference Identifier | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | | + * | Reference Timestamp (64) | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | | + * | Originate Timestamp (64) | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | | + * | Receive Timestamp (64) | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | | + * | Transmit Timestamp (64) | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Key Identifier (optional) (32) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | | + * | | + * | Message Digest (optional) (128) | + * | | + * | | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + +struct ntpdata { + u_char status; /* status of local clock and leap info */ + u_char stratum; /* Stratum level */ + u_char ppoll; /* poll value */ + int precision:8; + struct s_fixedpt root_delay; + struct s_fixedpt root_dispersion; + u_int32_t refid; + struct l_fixedpt ref_timestamp; + struct l_fixedpt org_timestamp; + struct l_fixedpt rec_timestamp; + struct l_fixedpt xmt_timestamp; + u_int32_t key_id; + u_int8_t message_digest[16]; +}; +/* + * Leap Second Codes (high order two bits) + */ +#define NO_WARNING 0x00 /* no warning */ +#define PLUS_SEC 0x40 /* add a second (61 seconds) */ +#define MINUS_SEC 0x80 /* minus a second (59 seconds) */ +#define ALARM 0xc0 /* alarm condition (clock unsynchronized) */ + +/* + * Clock Status Bits that Encode Version + */ +#define NTPVERSION_1 0x08 +#define VERSIONMASK 0x38 +#define LEAPMASK 0xc0 #ifdef MODEMASK #undef MODEMASK /* Solaris sucks */ #endif -#include "ntp.h" +#define MODEMASK 0x07 + +/* + * Code values + */ +#define MODE_UNSPEC 0 /* unspecified */ +#define MODE_SYM_ACT 1 /* symmetric active */ +#define MODE_SYM_PAS 2 /* symmetric passive */ +#define MODE_CLIENT 3 /* client */ +#define MODE_SERVER 4 /* server */ +#define MODE_BROADCAST 5 /* broadcast */ +#define MODE_RES1 6 /* reserved */ +#define MODE_RES2 7 /* reserved */ + +/* + * Stratum Definitions + */ +#define UNSPECIFIED 0 +#define PRIM_REF 1 /* radio clock */ +#define INFO_QUERY 62 /* **** THIS implementation dependent **** */ +#define INFO_REPLY 63 /* **** THIS implementation dependent **** */ static void p_sfix(const struct s_fixedpt *); static void p_ntp_time(const struct l_fixedpt *); static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *); -static struct tok ntp_mode_values[] = { +static const struct tok ntp_mode_values[] = { { MODE_UNSPEC, "unspecified" }, { MODE_SYM_ACT, "symmetric active" }, { MODE_SYM_PAS, "symmetric passive" }, @@ -64,7 +184,7 @@ static struct tok ntp_mode_values[] = { { 0, NULL } }; -static struct tok ntp_leapind_values[] = { +static const struct tok ntp_leapind_values[] = { { NO_WARNING, "" }, { PLUS_SEC, "+1s" }, { MINUS_SEC, "-1s" }, @@ -72,6 +192,12 @@ static struct tok ntp_leapind_values[] = { { 0, NULL } }; +static const struct tok ntp_stratum_values[] = { + { UNSPECIFIED, "unspecified" }, + { PRIM_REF, "primary reference" }, + { 0, NULL } +}; + /* * Print ntp requests */ @@ -95,10 +221,10 @@ ntp_print(register const u_char *cp, u_int length) length); return; } - + printf (", length %u\n\t%s", length, - tok2str(ntp_mode_values, "Unknown mode", mode)); + tok2str(ntp_mode_values, "Unknown mode", mode)); leapind = bp->status & LEAPMASK; printf (", Leap indicator: %s (%u)", @@ -106,10 +232,12 @@ ntp_print(register const u_char *cp, u_int length) leapind); TCHECK(bp->stratum); - printf(", Stratum %u", bp->stratum); + printf(", Stratum %u (%s)", + bp->stratum, + tok2str(ntp_stratum_values, (bp->stratum >=2 && bp->stratum<=15) ? "secondary reference" : "reserved", bp->stratum)); TCHECK(bp->ppoll); - printf(", poll %us", bp->ppoll); + printf(", poll %u (%us)", bp->ppoll, 1 << bp->ppoll); /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */ TCHECK2(bp->root_delay, 0); @@ -174,8 +302,19 @@ ntp_print(register const u_char *cp, u_int length) fputs("\n\t Originator - Transmit Timestamp: ", stdout); p_ntp_delta(&(bp->org_timestamp), &(bp->xmt_timestamp)); - /* FIXME key-id, authentication */ - + if ( (sizeof(struct ntpdata) - length) == 16) { /* Optional: key-id */ + TCHECK(bp->key_id); + printf("\n\tKey id: %u", bp->key_id); + } else if ( (sizeof(struct ntpdata) - length) == 0) { /* Optional: key-id + authentication */ + TCHECK(bp->key_id); + printf("\n\tKey id: %u", bp->key_id); + TCHECK2(bp->message_digest, sizeof (bp->message_digest)); + printf("\n\tAuthentication: %08x%08x%08x%08x", + EXTRACT_32BITS(bp->message_digest), + EXTRACT_32BITS(bp->message_digest + 4), + EXTRACT_32BITS(bp->message_digest + 8), + EXTRACT_32BITS(bp->message_digest + 12)); + } return; trunc: