xref: /freebsd/contrib/tcpdump/print-timed.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1685295f4SBill Fenner /*
2685295f4SBill Fenner  * Copyright (c) 2000 Ben Smithurst <ben@scientia.demon.co.uk>
3685295f4SBill Fenner  * All rights reserved.
4685295f4SBill Fenner  *
5685295f4SBill Fenner  * Redistribution and use in source and binary forms, with or without
6685295f4SBill Fenner  * modification, are permitted provided that: (1) source code distributions
7685295f4SBill Fenner  * retain the above copyright notice and this paragraph in its entirety, (2)
8685295f4SBill Fenner  * distributions including binary code include the above copyright notice and
9685295f4SBill Fenner  * this paragraph in its entirety in the documentation or other materials
10685295f4SBill Fenner  * provided with the distribution, and (3) all advertising materials mentioning
11685295f4SBill Fenner  * features or use of this software display the following acknowledgement:
12685295f4SBill Fenner  * ``This product includes software developed by the University of California,
13685295f4SBill Fenner  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14685295f4SBill Fenner  * the University nor the names of its contributors may be used to endorse
15685295f4SBill Fenner  * or promote products derived from this software without specific prior
16685295f4SBill Fenner  * written permission.
17685295f4SBill Fenner  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18685295f4SBill Fenner  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19685295f4SBill Fenner  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20685295f4SBill Fenner  */
21685295f4SBill Fenner 
22*ee67461eSJoseph Mingrone /* \summary: Berkeley UNIX Time Synchronization Protocol */
23*ee67461eSJoseph Mingrone 
24*ee67461eSJoseph Mingrone /* specification: https://docs.freebsd.org/44doc/smm/12.timed/paper.pdf */
253340d773SGleb Smirnoff 
26*ee67461eSJoseph Mingrone #include <config.h>
27685295f4SBill Fenner 
28*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
295b0fe478SBruce M Simpson 
303340d773SGleb Smirnoff #include "netdissect.h"
315b0fe478SBruce M Simpson #include "extract.h"
32685295f4SBill Fenner 
333c602fabSXin LI struct tsp_timeval {
34*ee67461eSJoseph Mingrone 	nd_int32_t	tv_sec;
35*ee67461eSJoseph Mingrone 	nd_int32_t	tv_usec;
363c602fabSXin LI };
373c602fabSXin LI 
383c602fabSXin LI struct tsp {
39*ee67461eSJoseph Mingrone 	nd_uint8_t	tsp_type;
40*ee67461eSJoseph Mingrone 	nd_uint8_t	tsp_vers;
41*ee67461eSJoseph Mingrone 	nd_uint16_t	tsp_seq;
423c602fabSXin LI 	union {
433c602fabSXin LI 		struct tsp_timeval tspu_time;
44*ee67461eSJoseph Mingrone 		nd_int8_t tspu_hopcnt;
453c602fabSXin LI 	} tsp_u;
46*ee67461eSJoseph Mingrone 	nd_byte		tsp_name[256];	/* null-terminated string up to 256 */
473c602fabSXin LI };
483c602fabSXin LI 
493c602fabSXin LI #define	tsp_time	tsp_u.tspu_time
503c602fabSXin LI #define	tsp_hopcnt	tsp_u.tspu_hopcnt
513c602fabSXin LI 
523c602fabSXin LI /*
533c602fabSXin LI  * Command types.
543c602fabSXin LI  */
553c602fabSXin LI #define	TSP_ANY			0	/* match any types */
563c602fabSXin LI #define	TSP_ADJTIME		1	/* send adjtime */
573c602fabSXin LI #define	TSP_ACK			2	/* generic acknowledgement */
583c602fabSXin LI #define	TSP_MASTERREQ		3	/* ask for master's name */
593c602fabSXin LI #define	TSP_MASTERACK		4	/* acknowledge master request */
603c602fabSXin LI #define	TSP_SETTIME		5	/* send network time */
613c602fabSXin LI #define	TSP_MASTERUP		6	/* inform slaves that master is up */
623c602fabSXin LI #define	TSP_SLAVEUP		7	/* slave is up but not polled */
633c602fabSXin LI #define	TSP_ELECTION		8	/* advance candidature for master */
643c602fabSXin LI #define	TSP_ACCEPT		9	/* support candidature of master */
653c602fabSXin LI #define	TSP_REFUSE		10	/* reject candidature of master */
663c602fabSXin LI #define	TSP_CONFLICT		11	/* two or more masters present */
673c602fabSXin LI #define	TSP_RESOLVE		12	/* masters' conflict resolution */
683c602fabSXin LI #define	TSP_QUIT		13	/* reject candidature if master is up */
693c602fabSXin LI #define	TSP_DATE		14	/* reset the time (date command) */
703c602fabSXin LI #define	TSP_DATEREQ		15	/* remote request to reset the time */
713c602fabSXin LI #define	TSP_DATEACK		16	/* acknowledge time setting  */
723c602fabSXin LI #define	TSP_TRACEON		17	/* turn tracing on */
733c602fabSXin LI #define	TSP_TRACEOFF		18	/* turn tracing off */
743c602fabSXin LI #define	TSP_MSITE		19	/* find out master's site */
753c602fabSXin LI #define	TSP_MSITEREQ		20	/* remote master's site request */
763c602fabSXin LI #define	TSP_TEST		21	/* for testing election algo */
773c602fabSXin LI #define	TSP_SETDATE		22	/* New from date command */
783c602fabSXin LI #define	TSP_SETDATEREQ		23	/* New remote for above */
793c602fabSXin LI #define	TSP_LOOP		24	/* loop detection packet */
80*ee67461eSJoseph Mingrone static const struct tok tsptype_str[] = {
81*ee67461eSJoseph Mingrone 	{ TSP_ANY,        "TSP_ANY"        },
82*ee67461eSJoseph Mingrone 	{ TSP_ADJTIME,    "TSP_ADJTIME"    },
83*ee67461eSJoseph Mingrone 	{ TSP_ACK,        "TSP_ACK"        },
84*ee67461eSJoseph Mingrone 	{ TSP_MASTERREQ,  "TSP_MASTERREQ"  },
85*ee67461eSJoseph Mingrone 	{ TSP_MASTERACK,  "TSP_MASTERACK"  },
86*ee67461eSJoseph Mingrone 	{ TSP_SETTIME,    "TSP_SETTIME"    },
87*ee67461eSJoseph Mingrone 	{ TSP_MASTERUP,   "TSP_MASTERUP"   },
88*ee67461eSJoseph Mingrone 	{ TSP_SLAVEUP,    "TSP_SLAVEUP"    },
89*ee67461eSJoseph Mingrone 	{ TSP_ELECTION,   "TSP_ELECTION"   },
90*ee67461eSJoseph Mingrone 	{ TSP_ACCEPT,     "TSP_ACCEPT"     },
91*ee67461eSJoseph Mingrone 	{ TSP_REFUSE,     "TSP_REFUSE"     },
92*ee67461eSJoseph Mingrone 	{ TSP_CONFLICT,   "TSP_CONFLICT"   },
93*ee67461eSJoseph Mingrone 	{ TSP_RESOLVE,    "TSP_RESOLVE"    },
94*ee67461eSJoseph Mingrone 	{ TSP_QUIT,       "TSP_QUIT"       },
95*ee67461eSJoseph Mingrone 	{ TSP_DATE,       "TSP_DATE"       },
96*ee67461eSJoseph Mingrone 	{ TSP_DATEREQ,    "TSP_DATEREQ"    },
97*ee67461eSJoseph Mingrone 	{ TSP_DATEACK,    "TSP_DATEACK"    },
98*ee67461eSJoseph Mingrone 	{ TSP_TRACEON,    "TSP_TRACEON"    },
99*ee67461eSJoseph Mingrone 	{ TSP_TRACEOFF,   "TSP_TRACEOFF"   },
100*ee67461eSJoseph Mingrone 	{ TSP_MSITE,      "TSP_MSITE"      },
101*ee67461eSJoseph Mingrone 	{ TSP_MSITEREQ,   "TSP_MSITEREQ"   },
102*ee67461eSJoseph Mingrone 	{ TSP_TEST,       "TSP_TEST"       },
103*ee67461eSJoseph Mingrone 	{ TSP_SETDATE,    "TSP_SETDATE"    },
104*ee67461eSJoseph Mingrone 	{ TSP_SETDATEREQ, "TSP_SETDATEREQ" },
105*ee67461eSJoseph Mingrone 	{ TSP_LOOP,       "TSP_LOOP"       },
106*ee67461eSJoseph Mingrone 	{ 0, NULL }
107*ee67461eSJoseph Mingrone };
108685295f4SBill Fenner 
109685295f4SBill Fenner void
timed_print(netdissect_options * ndo,const u_char * bp)1103c602fabSXin LI timed_print(netdissect_options *ndo,
111*ee67461eSJoseph Mingrone             const u_char *bp)
112685295f4SBill Fenner {
1133340d773SGleb Smirnoff 	const struct tsp *tsp = (const struct tsp *)bp;
114*ee67461eSJoseph Mingrone 	uint8_t tsp_type;
115*ee67461eSJoseph Mingrone 	int sec, usec;
116685295f4SBill Fenner 
117*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "timed";
118*ee67461eSJoseph Mingrone 	tsp_type = GET_U_1(tsp->tsp_type);
119*ee67461eSJoseph Mingrone 	ND_PRINT("%s", tok2str(tsptype_str, "(tsp_type %#x)", tsp_type));
120685295f4SBill Fenner 
121*ee67461eSJoseph Mingrone 	ND_PRINT(" vers %u", GET_U_1(tsp->tsp_vers));
122685295f4SBill Fenner 
123*ee67461eSJoseph Mingrone 	ND_PRINT(" seq %u", GET_BE_U_2(tsp->tsp_seq));
124685295f4SBill Fenner 
125*ee67461eSJoseph Mingrone 	switch (tsp_type) {
1263c602fabSXin LI 	case TSP_LOOP:
127*ee67461eSJoseph Mingrone 		ND_PRINT(" hopcnt %u", GET_U_1(tsp->tsp_hopcnt));
1283c602fabSXin LI 		break;
1293c602fabSXin LI 	case TSP_SETTIME:
1303c602fabSXin LI 	case TSP_ADJTIME:
1313c602fabSXin LI 	case TSP_SETDATE:
1323c602fabSXin LI 	case TSP_SETDATEREQ:
133*ee67461eSJoseph Mingrone 		sec = GET_BE_S_4(tsp->tsp_time.tv_sec);
134*ee67461eSJoseph Mingrone 		usec = GET_BE_S_4(tsp->tsp_time.tv_usec);
1353c602fabSXin LI 		/* XXX The comparison below is always false? */
136685295f4SBill Fenner 		if (usec < 0)
1373340d773SGleb Smirnoff 			/* invalid, skip the rest of the packet */
138685295f4SBill Fenner 			return;
139*ee67461eSJoseph Mingrone 		ND_PRINT(" time ");
140685295f4SBill Fenner 		if (sec < 0 && usec != 0) {
141685295f4SBill Fenner 			sec++;
142685295f4SBill Fenner 			if (sec == 0)
143*ee67461eSJoseph Mingrone 				ND_PRINT("-");
144685295f4SBill Fenner 			usec = 1000000 - usec;
145685295f4SBill Fenner 		}
146*ee67461eSJoseph Mingrone 		ND_PRINT("%d.%06d", sec, usec);
1473c602fabSXin LI 		break;
148685295f4SBill Fenner 	}
149*ee67461eSJoseph Mingrone 	ND_PRINT(" name ");
150*ee67461eSJoseph Mingrone 	nd_printjnp(ndo, tsp->tsp_name, sizeof(tsp->tsp_name));
151685295f4SBill Fenner }
152