1 /* packet-ax25-nol3.c
2  *
3  * Routines for Amateur Packet Radio protocol dissection
4  * Copyright 2007,2008,2009,2010,2012 R.W. Stearn <richard@rns-stearn.demon.co.uk>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 /*
14  * This dissector is for the "No Layer 3 protocol" PID of the AX.25 Amateur
15  * Packet-Radio Link-Layer Protocol, Version 2.0, October 1984
16  *
17  * At the time of writing the specification could be found here:
18  *   http://www.tapr.org/pub_ax25.html
19  *
20  * Information on the "protocols" recognised by this dissector are drawn from:
21  *  DX cluster:
22  *    A network capture kindly donated by Luca Melette.
23  *  APRS:
24  *    http://www.aprs.org/
25  *
26  * Inspiration on how to build the dissector drawn from
27  *   packet-sdlc.c
28  *   packet-x25.c
29  *   packet-lapb.c
30  *   paket-gprs-llc.c
31  *   xdlc.c
32  * with the base file built from README.developers.
33  */
34 
35 #include "config.h"
36 
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39 #include <epan/ax25_pids.h>
40 
41 #define STRLEN	80
42 
43 void proto_register_ax25_nol3(void);
44 void proto_reg_handoff_ax25_nol3(void);
45 
46 /* Dissector handles - all the possibles are listed */
47 static dissector_handle_t aprs_handle;
48 
49 /* Initialize the protocol and registered fields */
50 static int proto_ax25_nol3		= -1;
51 static int proto_dx			= -1;
52 
53 static int hf_dx_report			= -1;
54 
55 /* static int hf_text			= -1; */
56 
57 /* Global preferences */
58 static gboolean gPREF_APRS     = FALSE;
59 static gboolean gPREF_DX       = FALSE;
60 
61 /* Initialize the subtree pointers */
62 static gint ett_ax25_nol3 = -1;
63 
64 static gint ett_dx		= -1;
65 
66 
67 /* Code to actually dissect the packets */
68 static int
69 dissect_dx(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data _U_)
70 {
71 	proto_item *ti;
72 	proto_tree *dx_tree;
73 
74 	int data_len;
75 	int offset;
76 
77 	offset   = 0;
78 	data_len = tvb_reported_length_remaining( tvb, offset );
79 
80 	col_set_str( pinfo->cinfo, COL_PROTOCOL, "DX" );
81 
82 	col_add_fstr( pinfo->cinfo, COL_INFO, "%s", tvb_format_text( pinfo->pool, tvb, offset, 15 ) );
83 
84 	if ( parent_tree )
85 		{
86 		/* create display subtree for the protocol */
87 		ti = proto_tree_add_protocol_format( parent_tree, proto_dx, tvb, 0, -1,
88 		    "DX (%s)", tvb_format_text( pinfo->pool, tvb, offset, 15 ) );
89 		dx_tree = proto_item_add_subtree( ti, ett_dx );
90 		offset = 0;
91 
92 		proto_tree_add_item( dx_tree, hf_dx_report, tvb, offset, data_len, ENC_ASCII|ENC_NA );
93 	}
94 
95 	return tvb_captured_length(tvb);
96 }
97 
98 static gboolean
99 isaprs( guint8 dti )
100 {
101 	gboolean b = FALSE;
102 
103 	switch ( dti )
104 		{
105 		case 0x1c	:
106 		case 0x1d	:
107 		case '!'	:
108 		case '#'	:
109 		case '$'	:
110 		case '%'	:
111 		case '&'	:
112 		case ')'	:
113 		case '*'	:
114 		case '+'	:
115 		case ','	:
116 		case '.'	:
117 		case '/'	:
118 		case ':'	:
119 		case ';'	:
120 		case '<'	:
121 		case '='	:
122 		case '>'	:
123 		case '?'	:
124 		case '@'	:
125 		case 'T'	:
126 		case '['	:
127 		case '\''	:
128 		case '_'	:
129 		case '`'	:
130 		case '{'	:
131 		case '}'	: b = TRUE; break;
132 		default		: break;
133 		}
134 	return b;
135 }
136 
137 static int
138 dissect_ax25_nol3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, void* data _U_ )
139 {
140 	proto_item *ti;
141 	proto_tree *ax25_nol3_tree;
142 	char       *info_buffer;
143 	int         offset;
144 	tvbuff_t   *next_tvb = NULL;
145 	guint8      dti      = 0;
146 	gboolean    dissected;
147 
148 	info_buffer = (char *)wmem_alloc( pinfo->pool, STRLEN );
149 	info_buffer[0] = '\0';
150 
151 	col_set_str( pinfo->cinfo, COL_PROTOCOL, "AX.25-NoL3");
152 
153 	col_clear( pinfo->cinfo, COL_INFO);
154 
155 	offset = 0;
156 	g_snprintf( info_buffer, STRLEN, "Text" );
157 
158 	if ( gPREF_APRS )
159 		{
160 		dti = tvb_get_guint8( tvb, offset );
161 		if ( isaprs( dti ) )
162 			g_snprintf( info_buffer, STRLEN, "APRS" );
163 		}
164 	if ( gPREF_DX )
165 		{
166 		if ( tvb_get_guint8( tvb, offset ) == 'D' && tvb_get_guint8( tvb, offset + 1 ) == 'X' )
167 		g_snprintf( info_buffer, STRLEN, "DX cluster" );
168 		}
169 
170 	col_add_str( pinfo->cinfo, COL_INFO, info_buffer );
171 
172 	/* Call sub-dissectors here */
173 
174 	/* create display subtree for the protocol */
175 	ti = proto_tree_add_protocol_format( parent_tree,
176 						proto_ax25_nol3,
177 						tvb,
178 						0,
179 						-1,
180 						"AX.25 No Layer 3 - (%s)", info_buffer );
181 	ax25_nol3_tree = proto_item_add_subtree( ti, ett_ax25_nol3 );
182 
183 	next_tvb = tvb_new_subset_remaining(tvb, offset);
184 	dissected = FALSE;
185 	if ( gPREF_APRS )
186 		{
187 		if ( isaprs( dti ) )
188 			{
189 			dissected = TRUE;
190 			call_dissector( aprs_handle , next_tvb, pinfo, ax25_nol3_tree );
191 			}
192 		}
193 	if ( gPREF_DX )
194 		{
195 		if ( tvb_get_guint8( tvb, offset ) == 'D' && tvb_get_guint8( tvb, offset + 1 ) == 'X' )
196 			{
197 			dissected = TRUE;
198 			dissect_dx( next_tvb, pinfo, ax25_nol3_tree, NULL );
199 			}
200 		}
201 	if ( ! dissected )
202 		call_data_dissector(next_tvb, pinfo, ax25_nol3_tree );
203 
204 	return tvb_captured_length(tvb);
205 }
206 
207 void
208 proto_register_ax25_nol3(void)
209 {
210 	module_t *ax25_nol3_module;
211 
212 	/* Setup list of header fields */
213 #if 0 /* not used ? */
214 	static hf_register_info hf[] = {
215 		{ &hf_text,
216 			{ "Text",			"ax25_nol3.text",
217 			FT_STRING, BASE_NONE, NULL, 0x0,
218 			NULL, HFILL }
219 		},
220 	};
221 #endif
222 
223 	static hf_register_info hf_dx[] = {
224 		{ &hf_dx_report,
225 			{ "DX",				"ax25_nol3.dx",
226 			FT_STRING, BASE_NONE, NULL, 0x0,
227 			"DX cluster", HFILL }
228 		},
229 	};
230 
231 	/* Setup protocol subtree array */
232 	static gint *ett[] = {
233 		&ett_ax25_nol3,
234 		&ett_dx,
235 	};
236 
237 	/* Register the protocol name and description */
238 	proto_ax25_nol3 = proto_register_protocol("AX.25 no Layer 3", "AX.25 no L3", "ax25_nol3");
239 
240 	/* Required function calls to register the header fields and subtrees used */
241 	/* proto_register_field_array( proto_ax25_nol3, hf, array_length(hf ) ); */
242 	proto_register_subtree_array( ett, array_length( ett ) );
243 
244 	/* Register preferences module */
245 	ax25_nol3_module = prefs_register_protocol( proto_ax25_nol3, NULL);
246 
247 	/* Register any preference */
248 	prefs_register_bool_preference(ax25_nol3_module, "showaprs",
249 	     "Decode the APRS info field",
250 	     "Enable decoding of the payload as APRS.",
251 	     &gPREF_APRS );
252 
253 	prefs_register_bool_preference(ax25_nol3_module, "showcluster",
254 	     "Decode DX cluster info field",
255 	     "Enable decoding of the payload as DX cluster info.",
256 	     &gPREF_DX );
257 
258 	/* Register the sub-protocol name and description */
259 	proto_dx = proto_register_protocol("DX cluster", "DX", "dx");
260 
261 	/* Register the dissector */
262 	register_dissector( "dx", dissect_dx, proto_dx);
263 
264 	/* Register the header fields */
265 	proto_register_field_array( proto_dx, hf_dx, array_length( hf_dx ) );
266 
267 	/* Register the subtrees used */
268 	/* proto_register_subtree_array( ett_dx, array_length( ett_dx ) ); */
269 }
270 
271 void
272 proto_reg_handoff_ax25_nol3(void)
273 {
274 	dissector_add_uint( "ax25.pid", AX25_P_NO_L3, create_dissector_handle( dissect_ax25_nol3, proto_ax25_nol3 ) );
275 
276 	/*
277 	 */
278 	aprs_handle     = find_dissector_add_dependency( "aprs", proto_ax25_nol3 );
279 }
280 
281 /*
282  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
283  *
284  * Local variables:
285  * c-basic-offset: 8
286  * tab-width: 8
287  * indent-tabs-mode: t
288  * End:
289  *
290  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
291  * :indentSize=8:tabSize=8:noTabs=false:
292  */
293