1 /* packet-tsp.c
2  * Routines for Time Synchronization Protocol (TSP) packet dissection
3  *
4  * Uwe Girlich <Uwe.Girlich@philosys.de>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-quake.c
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
14 
15 #include "config.h"
16 
17 #include <epan/packet.h>
18 
19 /*
20  * For a full documentation of the Time Synchronization Protocol (TSP) see:
21  * http://docs.freebsd.org/44doc/smm/12.timed/paper.pdf
22  */
23 void proto_register_tsp(void);
24 void proto_reg_handoff_tsp(void);
25 
26 static int proto_tsp = -1;
27 static int hf_tsp_type = -1;
28 static int hf_tsp_vers = -1;
29 static int hf_tsp_seq = -1;
30 static int hf_tsp_hopcnt = -1;
31 static int hf_tsp_time_sec = -1;
32 static int hf_tsp_time_usec = -1;
33 static int hf_tsp_name = -1;
34 
35 static gint ett_tsp = -1;
36 
37 /* timed port from /etc/services */
38 #define UDP_PORT_TIMED	525
39 
40 
41 static const value_string names_tsp_type[] = {
42 #define TSP_ANY                 0       /* match any types */
43 	{	TSP_ANY, "any" },
44 #define TSP_ADJTIME             1       /* send adjtime */
45 	{	TSP_ADJTIME, "adjtime" },
46 #define TSP_ACK                 2       /* generic acknowledgement */
47 	{	TSP_ACK, "ack" },
48 #define TSP_MASTERREQ           3       /* ask for master's name */
49 	{	TSP_MASTERREQ, "masterreq" },
50 #define TSP_MASTERACK           4       /* acknowledge master request */
51 	{	TSP_MASTERACK, "masterack" },
52 #define TSP_SETTIME             5       /* send network time */
53 	{	TSP_SETTIME, "settime" },
54 #define TSP_MASTERUP            6       /* inform slaves that master is up */
55 	{	TSP_MASTERUP, "masterup" },
56 #define TSP_SLAVEUP             7       /* slave is up but not polled */
57 	{	TSP_SLAVEUP, "slaveup" },
58 #define TSP_ELECTION            8       /* advance candidature for master */
59 	{	TSP_ELECTION, "election" },
60 #define TSP_ACCEPT              9       /* support candidature of master */
61 	{	TSP_ACCEPT, "accept" },
62 #define TSP_REFUSE              10      /* reject candidature of master */
63 	{	TSP_REFUSE, "refuse" },
64 #define TSP_CONFLICT            11      /* two or more masters present */
65 	{	TSP_CONFLICT, "conflict" },
66 #define TSP_RESOLVE             12      /* masters' conflict resolution */
67 	{	TSP_RESOLVE, "resolve" },
68 #define TSP_QUIT                13      /* reject candidature if master is up */
69 	{	TSP_QUIT, "quit" },
70 #define TSP_DATE                14      /* reset the time (date command) */
71 	{	TSP_DATE, "date" },
72 #define TSP_DATEREQ             15      /* remote request to reset the time */
73 	{	TSP_DATEREQ, "datereq" },
74 #define TSP_DATEACK             16      /* acknowledge time setting  */
75 	{	TSP_DATEACK, "dateack" },
76 #define TSP_TRACEON             17      /* turn tracing on */
77 	{	TSP_TRACEON, "traceon" },
78 #define TSP_TRACEOFF            18      /* turn tracing off */
79 	{	TSP_TRACEOFF, "traceoff" },
80 #define TSP_MSITE               19      /* find out master's site */
81 	{	TSP_MSITE, "msite" },
82 #define TSP_MSITEREQ            20      /* remote master's site request */
83 	{	TSP_MSITEREQ, "msitereq" },
84 #define TSP_TEST                21      /* for testing election algo */
85 	{	TSP_TEST, "test" },
86 #define TSP_SETDATE             22      /* New from date command */
87 	{	TSP_SETDATE, "setdate" },
88 #define TSP_SETDATEREQ          23      /* New remote for above */
89 	{	TSP_SETDATEREQ, "setdatereq" },
90 #define TSP_LOOP                24      /* loop detection packet */
91 	{	TSP_LOOP, "loop" },
92 	{ 0, NULL }
93 };
94 
95 
96 static int
dissect_tsp(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)97 dissect_tsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
98 {
99 	proto_tree	*tsp_tree;
100 	proto_item	*tsp_item;
101 
102 	guint8		tsp_type;
103 
104 	col_set_str(pinfo->cinfo, COL_PROTOCOL, "TSP");
105 	col_clear(pinfo->cinfo, COL_INFO);
106 
107 	tsp_type = tvb_get_guint8(tvb, 0);
108 	col_add_str(pinfo->cinfo, COL_INFO,
109 		    val_to_str(tsp_type, names_tsp_type, "Unknown message type (%u)"));
110 
111 	tsp_item = proto_tree_add_item(tree, proto_tsp,
112 				tvb, 0, -1, ENC_NA);
113 	tsp_tree = proto_item_add_subtree(tsp_item, ett_tsp);
114 
115 	if (tsp_tree) {
116 		proto_tree_add_uint(tsp_tree, hf_tsp_type,
117 			tvb, 0, 1, tsp_type);
118 		proto_tree_add_item(tsp_tree, hf_tsp_vers,
119 			tvb, 1, 1, ENC_BIG_ENDIAN);
120 		proto_tree_add_item(tsp_tree, hf_tsp_seq,
121 			tvb, 2, 2, ENC_BIG_ENDIAN);
122 	}
123 
124 	switch (tsp_type) {
125 
126 	case TSP_LOOP:
127 		if (tsp_tree)
128 			proto_tree_add_item(tsp_tree, hf_tsp_hopcnt,
129 				tvb, 4, 1, ENC_BIG_ENDIAN);
130 		break;
131 
132 	case TSP_SETTIME:
133 	case TSP_ADJTIME:
134 	case TSP_SETDATE:
135 	case TSP_SETDATEREQ:
136 		if (tsp_tree) {
137 			proto_tree_add_item(tsp_tree, hf_tsp_time_sec,
138 				tvb, 4, 4, ENC_BIG_ENDIAN);
139 			proto_tree_add_item(tsp_tree, hf_tsp_time_usec,
140 				tvb, 8, 4, ENC_BIG_ENDIAN);
141 		}
142 		break;
143 	}
144 
145 	if (tsp_tree) {
146 		proto_tree_add_item(tsp_tree, hf_tsp_name, tvb, 12,
147 			-1, ENC_ASCII|ENC_NA);
148 	}
149 	return tvb_captured_length(tvb);
150 }
151 
152 
153 void
proto_reg_handoff_tsp(void)154 proto_reg_handoff_tsp(void)
155 {
156 	dissector_handle_t	tsp_handle;
157 
158 	tsp_handle = create_dissector_handle(dissect_tsp, proto_tsp);
159 	dissector_add_uint_with_preference("udp.port", UDP_PORT_TIMED, tsp_handle);
160 }
161 
162 
163 void
proto_register_tsp(void)164 proto_register_tsp(void)
165 {
166 	static hf_register_info hf[] = {
167 		{ &hf_tsp_type,
168 		  { "Type", "tsp.type",
169 		    FT_UINT8, BASE_DEC, VALS(names_tsp_type), 0x0,
170 		    "Packet Type", HFILL }},
171 		{ &hf_tsp_vers,
172 		  { "Version", "tsp.version",
173 		    FT_UINT8, BASE_DEC, NULL, 0x0,
174 		    "Protocol Version Number", HFILL }},
175 		{ &hf_tsp_seq,
176 		  { "Sequence", "tsp.sequence",
177 		    FT_UINT16, BASE_DEC, NULL, 0x0,
178 		    "Sequence Number", HFILL }},
179 		{ &hf_tsp_hopcnt,
180 		  { "Hop Count", "tsp.hopcnt",
181 		    FT_UINT8, BASE_DEC, NULL, 0x0,
182 		    NULL, HFILL }},
183 		{ &hf_tsp_time_sec,
184 		  { "Seconds", "tsp.sec",
185 		    FT_UINT32, BASE_DEC, NULL, 0x0,
186 		    NULL, HFILL }},
187 		{ &hf_tsp_time_usec,
188 		  { "Microseconds", "tsp.usec",
189 		    FT_UINT32, BASE_DEC, NULL, 0x0,
190 		    NULL, HFILL }},
191 		{ &hf_tsp_name,
192 		  { "Machine Name", "tsp.name",
193 		    FT_STRINGZ, BASE_NONE, NULL, 0x0,
194 		    "Sender Machine Name", HFILL }}
195 	};
196 	static gint *ett[] = {
197 		&ett_tsp
198 	};
199 
200 	proto_tsp = proto_register_protocol("Time Synchronization Protocol",
201 					    "TSP", "tsp");
202 	proto_register_field_array(proto_tsp, hf, array_length(hf));
203 	proto_register_subtree_array(ett, array_length(ett));
204 }
205 
206 /*
207  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
208  *
209  * Local variables:
210  * c-basic-offset: 8
211  * tab-width: 8
212  * indent-tabs-mode: t
213  * End:
214  *
215  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
216  * :indentSize=8:tabSize=8:noTabs=false:
217  */
218