xref: /netbsd-src/external/bsd/tcpdump/dist/print-timed.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
10f74e101Schristos /*
20f74e101Schristos  * Copyright (c) 2000 Ben Smithurst <ben@scientia.demon.co.uk>
30f74e101Schristos  * All rights reserved.
40f74e101Schristos  *
50f74e101Schristos  * Redistribution and use in source and binary forms, with or without
60f74e101Schristos  * modification, are permitted provided that: (1) source code distributions
70f74e101Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
80f74e101Schristos  * distributions including binary code include the above copyright notice and
90f74e101Schristos  * this paragraph in its entirety in the documentation or other materials
100f74e101Schristos  * provided with the distribution, and (3) all advertising materials mentioning
110f74e101Schristos  * features or use of this software display the following acknowledgement:
120f74e101Schristos  * ``This product includes software developed by the University of California,
130f74e101Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
140f74e101Schristos  * the University nor the names of its contributors may be used to endorse
150f74e101Schristos  * or promote products derived from this software without specific prior
160f74e101Schristos  * written permission.
170f74e101Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
180f74e101Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
190f74e101Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
200f74e101Schristos  */
210f74e101Schristos 
2211b3aaa1Schristos #include <sys/cdefs.h>
230f74e101Schristos #ifndef lint
24*26ba0b50Schristos __RCSID("$NetBSD: print-timed.c,v 1.9 2024/09/02 16:15:33 christos Exp $");
250f74e101Schristos #endif
260f74e101Schristos 
27c74ad251Schristos /* \summary: Berkeley UNIX Time Synchronization Protocol */
28c74ad251Schristos 
29c74ad251Schristos /* specification: https://docs.freebsd.org/44doc/smm/12.timed/paper.pdf */
30dc860a36Sspz 
31c74ad251Schristos #include <config.h>
320f74e101Schristos 
33c74ad251Schristos #include "netdissect-stdinc.h"
340f74e101Schristos 
35fdccd7e4Schristos #include "netdissect.h"
360f74e101Schristos #include "extract.h"
370f74e101Schristos 
38b3a00663Schristos struct tsp_timeval {
39c74ad251Schristos 	nd_int32_t	tv_sec;
40c74ad251Schristos 	nd_int32_t	tv_usec;
41b3a00663Schristos };
42b3a00663Schristos 
43b3a00663Schristos struct tsp {
44c74ad251Schristos 	nd_uint8_t	tsp_type;
45c74ad251Schristos 	nd_uint8_t	tsp_vers;
46c74ad251Schristos 	nd_uint16_t	tsp_seq;
47b3a00663Schristos 	union {
48b3a00663Schristos 		struct tsp_timeval tspu_time;
49c74ad251Schristos 		nd_int8_t tspu_hopcnt;
50b3a00663Schristos 	} tsp_u;
51c74ad251Schristos 	nd_byte		tsp_name[256];	/* null-terminated string up to 256 */
52b3a00663Schristos };
53b3a00663Schristos 
54b3a00663Schristos #define	tsp_time	tsp_u.tspu_time
55b3a00663Schristos #define	tsp_hopcnt	tsp_u.tspu_hopcnt
56b3a00663Schristos 
57b3a00663Schristos /*
58b3a00663Schristos  * Command types.
59b3a00663Schristos  */
60b3a00663Schristos #define	TSP_ANY			0	/* match any types */
61b3a00663Schristos #define	TSP_ADJTIME		1	/* send adjtime */
62b3a00663Schristos #define	TSP_ACK			2	/* generic acknowledgement */
63b3a00663Schristos #define	TSP_MASTERREQ		3	/* ask for master's name */
64b3a00663Schristos #define	TSP_MASTERACK		4	/* acknowledge master request */
65b3a00663Schristos #define	TSP_SETTIME		5	/* send network time */
66b3a00663Schristos #define	TSP_MASTERUP		6	/* inform slaves that master is up */
67b3a00663Schristos #define	TSP_SLAVEUP		7	/* slave is up but not polled */
68b3a00663Schristos #define	TSP_ELECTION		8	/* advance candidature for master */
69b3a00663Schristos #define	TSP_ACCEPT		9	/* support candidature of master */
70b3a00663Schristos #define	TSP_REFUSE		10	/* reject candidature of master */
71b3a00663Schristos #define	TSP_CONFLICT		11	/* two or more masters present */
72b3a00663Schristos #define	TSP_RESOLVE		12	/* masters' conflict resolution */
73b3a00663Schristos #define	TSP_QUIT		13	/* reject candidature if master is up */
74b3a00663Schristos #define	TSP_DATE		14	/* reset the time (date command) */
75b3a00663Schristos #define	TSP_DATEREQ		15	/* remote request to reset the time */
76b3a00663Schristos #define	TSP_DATEACK		16	/* acknowledge time setting  */
77b3a00663Schristos #define	TSP_TRACEON		17	/* turn tracing on */
78b3a00663Schristos #define	TSP_TRACEOFF		18	/* turn tracing off */
79b3a00663Schristos #define	TSP_MSITE		19	/* find out master's site */
80b3a00663Schristos #define	TSP_MSITEREQ		20	/* remote master's site request */
81b3a00663Schristos #define	TSP_TEST		21	/* for testing election algo */
82b3a00663Schristos #define	TSP_SETDATE		22	/* New from date command */
83b3a00663Schristos #define	TSP_SETDATEREQ		23	/* New remote for above */
84b3a00663Schristos #define	TSP_LOOP		24	/* loop detection packet */
85c74ad251Schristos static const struct tok tsptype_str[] = {
86c74ad251Schristos 	{ TSP_ANY,        "TSP_ANY"        },
87c74ad251Schristos 	{ TSP_ADJTIME,    "TSP_ADJTIME"    },
88c74ad251Schristos 	{ TSP_ACK,        "TSP_ACK"        },
89c74ad251Schristos 	{ TSP_MASTERREQ,  "TSP_MASTERREQ"  },
90c74ad251Schristos 	{ TSP_MASTERACK,  "TSP_MASTERACK"  },
91c74ad251Schristos 	{ TSP_SETTIME,    "TSP_SETTIME"    },
92c74ad251Schristos 	{ TSP_MASTERUP,   "TSP_MASTERUP"   },
93c74ad251Schristos 	{ TSP_SLAVEUP,    "TSP_SLAVEUP"    },
94c74ad251Schristos 	{ TSP_ELECTION,   "TSP_ELECTION"   },
95c74ad251Schristos 	{ TSP_ACCEPT,     "TSP_ACCEPT"     },
96c74ad251Schristos 	{ TSP_REFUSE,     "TSP_REFUSE"     },
97c74ad251Schristos 	{ TSP_CONFLICT,   "TSP_CONFLICT"   },
98c74ad251Schristos 	{ TSP_RESOLVE,    "TSP_RESOLVE"    },
99c74ad251Schristos 	{ TSP_QUIT,       "TSP_QUIT"       },
100c74ad251Schristos 	{ TSP_DATE,       "TSP_DATE"       },
101c74ad251Schristos 	{ TSP_DATEREQ,    "TSP_DATEREQ"    },
102c74ad251Schristos 	{ TSP_DATEACK,    "TSP_DATEACK"    },
103c74ad251Schristos 	{ TSP_TRACEON,    "TSP_TRACEON"    },
104c74ad251Schristos 	{ TSP_TRACEOFF,   "TSP_TRACEOFF"   },
105c74ad251Schristos 	{ TSP_MSITE,      "TSP_MSITE"      },
106c74ad251Schristos 	{ TSP_MSITEREQ,   "TSP_MSITEREQ"   },
107c74ad251Schristos 	{ TSP_TEST,       "TSP_TEST"       },
108c74ad251Schristos 	{ TSP_SETDATE,    "TSP_SETDATE"    },
109c74ad251Schristos 	{ TSP_SETDATEREQ, "TSP_SETDATEREQ" },
110c74ad251Schristos 	{ TSP_LOOP,       "TSP_LOOP"       },
111c74ad251Schristos 	{ 0, NULL }
112c74ad251Schristos };
1130f74e101Schristos 
114a8e08e94Skamil UNALIGNED_OK
1150f74e101Schristos void
116b3a00663Schristos timed_print(netdissect_options *ndo,
117c74ad251Schristos             const u_char *bp)
1180f74e101Schristos {
119fdccd7e4Schristos 	const struct tsp *tsp = (const struct tsp *)bp;
120c74ad251Schristos 	uint8_t tsp_type;
121c74ad251Schristos 	int sec, usec;
1220f74e101Schristos 
123c74ad251Schristos 	ndo->ndo_protocol = "timed";
124c74ad251Schristos 	tsp_type = GET_U_1(tsp->tsp_type);
125c74ad251Schristos 	ND_PRINT("%s", tok2str(tsptype_str, "(tsp_type %#x)", tsp_type));
1260f74e101Schristos 
127c74ad251Schristos 	ND_PRINT(" vers %u", GET_U_1(tsp->tsp_vers));
1280f74e101Schristos 
129c74ad251Schristos 	ND_PRINT(" seq %u", GET_BE_U_2(tsp->tsp_seq));
1300f74e101Schristos 
131c74ad251Schristos 	switch (tsp_type) {
132b3a00663Schristos 	case TSP_LOOP:
133c74ad251Schristos 		ND_PRINT(" hopcnt %u", GET_U_1(tsp->tsp_hopcnt));
134b3a00663Schristos 		break;
135b3a00663Schristos 	case TSP_SETTIME:
136b3a00663Schristos 	case TSP_ADJTIME:
137b3a00663Schristos 	case TSP_SETDATE:
138b3a00663Schristos 	case TSP_SETDATEREQ:
139c74ad251Schristos 		sec = GET_BE_S_4(tsp->tsp_time.tv_sec);
140c74ad251Schristos 		usec = GET_BE_S_4(tsp->tsp_time.tv_usec);
141b3a00663Schristos 		/* XXX The comparison below is always false? */
1420f74e101Schristos 		if (usec < 0)
143fdccd7e4Schristos 			/* invalid, skip the rest of the packet */
1440f74e101Schristos 			return;
145c74ad251Schristos 		ND_PRINT(" time ");
1460f74e101Schristos 		if (sec < 0 && usec != 0) {
1470f74e101Schristos 			sec++;
1480f74e101Schristos 			if (sec == 0)
149c74ad251Schristos 				ND_PRINT("-");
1500f74e101Schristos 			usec = 1000000 - usec;
1510f74e101Schristos 		}
152c74ad251Schristos 		ND_PRINT("%d.%06d", sec, usec);
153b3a00663Schristos 		break;
1540f74e101Schristos 	}
155c74ad251Schristos 	ND_PRINT(" name ");
156c74ad251Schristos 	nd_printjnp(ndo, tsp->tsp_name, sizeof(tsp->tsp_name));
1570f74e101Schristos }
158