xref: /netbsd-src/external/bsd/tcpdump/dist/print-timed.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*
2  * Copyright (c) 2000 Ben Smithurst <ben@scientia.demon.co.uk>
3  * 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 
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-timed.c,v 1.8 2023/08/17 20:19:40 christos Exp $");
25 #endif
26 
27 /* \summary: Berkeley UNIX Time Synchronization Protocol */
28 
29 /* specification: https://docs.freebsd.org/44doc/smm/12.timed/paper.pdf */
30 
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 
35 #include "netdissect-stdinc.h"
36 
37 #include "netdissect.h"
38 #include "extract.h"
39 
40 struct tsp_timeval {
41 	nd_int32_t	tv_sec;
42 	nd_int32_t	tv_usec;
43 };
44 
45 struct tsp {
46 	nd_uint8_t	tsp_type;
47 	nd_uint8_t	tsp_vers;
48 	nd_uint16_t	tsp_seq;
49 	union {
50 		struct tsp_timeval tspu_time;
51 		nd_int8_t tspu_hopcnt;
52 	} tsp_u;
53 	nd_byte		tsp_name[256];	/* null-terminated string up to 256 */
54 };
55 
56 #define	tsp_time	tsp_u.tspu_time
57 #define	tsp_hopcnt	tsp_u.tspu_hopcnt
58 
59 /*
60  * Command types.
61  */
62 #define	TSP_ANY			0	/* match any types */
63 #define	TSP_ADJTIME		1	/* send adjtime */
64 #define	TSP_ACK			2	/* generic acknowledgement */
65 #define	TSP_MASTERREQ		3	/* ask for master's name */
66 #define	TSP_MASTERACK		4	/* acknowledge master request */
67 #define	TSP_SETTIME		5	/* send network time */
68 #define	TSP_MASTERUP		6	/* inform slaves that master is up */
69 #define	TSP_SLAVEUP		7	/* slave is up but not polled */
70 #define	TSP_ELECTION		8	/* advance candidature for master */
71 #define	TSP_ACCEPT		9	/* support candidature of master */
72 #define	TSP_REFUSE		10	/* reject candidature of master */
73 #define	TSP_CONFLICT		11	/* two or more masters present */
74 #define	TSP_RESOLVE		12	/* masters' conflict resolution */
75 #define	TSP_QUIT		13	/* reject candidature if master is up */
76 #define	TSP_DATE		14	/* reset the time (date command) */
77 #define	TSP_DATEREQ		15	/* remote request to reset the time */
78 #define	TSP_DATEACK		16	/* acknowledge time setting  */
79 #define	TSP_TRACEON		17	/* turn tracing on */
80 #define	TSP_TRACEOFF		18	/* turn tracing off */
81 #define	TSP_MSITE		19	/* find out master's site */
82 #define	TSP_MSITEREQ		20	/* remote master's site request */
83 #define	TSP_TEST		21	/* for testing election algo */
84 #define	TSP_SETDATE		22	/* New from date command */
85 #define	TSP_SETDATEREQ		23	/* New remote for above */
86 #define	TSP_LOOP		24	/* loop detection packet */
87 static const struct tok tsptype_str[] = {
88 	{ TSP_ANY,        "TSP_ANY"        },
89 	{ TSP_ADJTIME,    "TSP_ADJTIME"    },
90 	{ TSP_ACK,        "TSP_ACK"        },
91 	{ TSP_MASTERREQ,  "TSP_MASTERREQ"  },
92 	{ TSP_MASTERACK,  "TSP_MASTERACK"  },
93 	{ TSP_SETTIME,    "TSP_SETTIME"    },
94 	{ TSP_MASTERUP,   "TSP_MASTERUP"   },
95 	{ TSP_SLAVEUP,    "TSP_SLAVEUP"    },
96 	{ TSP_ELECTION,   "TSP_ELECTION"   },
97 	{ TSP_ACCEPT,     "TSP_ACCEPT"     },
98 	{ TSP_REFUSE,     "TSP_REFUSE"     },
99 	{ TSP_CONFLICT,   "TSP_CONFLICT"   },
100 	{ TSP_RESOLVE,    "TSP_RESOLVE"    },
101 	{ TSP_QUIT,       "TSP_QUIT"       },
102 	{ TSP_DATE,       "TSP_DATE"       },
103 	{ TSP_DATEREQ,    "TSP_DATEREQ"    },
104 	{ TSP_DATEACK,    "TSP_DATEACK"    },
105 	{ TSP_TRACEON,    "TSP_TRACEON"    },
106 	{ TSP_TRACEOFF,   "TSP_TRACEOFF"   },
107 	{ TSP_MSITE,      "TSP_MSITE"      },
108 	{ TSP_MSITEREQ,   "TSP_MSITEREQ"   },
109 	{ TSP_TEST,       "TSP_TEST"       },
110 	{ TSP_SETDATE,    "TSP_SETDATE"    },
111 	{ TSP_SETDATEREQ, "TSP_SETDATEREQ" },
112 	{ TSP_LOOP,       "TSP_LOOP"       },
113 	{ 0, NULL }
114 };
115 
116 UNALIGNED_OK
117 void
118 timed_print(netdissect_options *ndo,
119             const u_char *bp)
120 {
121 	const struct tsp *tsp = (const struct tsp *)bp;
122 	uint8_t tsp_type;
123 	int sec, usec;
124 
125 	ndo->ndo_protocol = "timed";
126 	tsp_type = GET_U_1(tsp->tsp_type);
127 	ND_PRINT("%s", tok2str(tsptype_str, "(tsp_type %#x)", tsp_type));
128 
129 	ND_PRINT(" vers %u", GET_U_1(tsp->tsp_vers));
130 
131 	ND_PRINT(" seq %u", GET_BE_U_2(tsp->tsp_seq));
132 
133 	switch (tsp_type) {
134 	case TSP_LOOP:
135 		ND_PRINT(" hopcnt %u", GET_U_1(tsp->tsp_hopcnt));
136 		break;
137 	case TSP_SETTIME:
138 	case TSP_ADJTIME:
139 	case TSP_SETDATE:
140 	case TSP_SETDATEREQ:
141 		sec = GET_BE_S_4(tsp->tsp_time.tv_sec);
142 		usec = GET_BE_S_4(tsp->tsp_time.tv_usec);
143 		/* XXX The comparison below is always false? */
144 		if (usec < 0)
145 			/* invalid, skip the rest of the packet */
146 			return;
147 		ND_PRINT(" time ");
148 		if (sec < 0 && usec != 0) {
149 			sec++;
150 			if (sec == 0)
151 				ND_PRINT("-");
152 			usec = 1000000 - usec;
153 		}
154 		ND_PRINT("%d.%06d", sec, usec);
155 		break;
156 	}
157 	ND_PRINT(" name ");
158 	nd_printjnp(ndo, tsp->tsp_name, sizeof(tsp->tsp_name));
159 }
160