1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/* packet-pdu_transport.c
* PDU Transport dissector for FDN and others.
* By <lars.voelker@technica-engineering.de>
* Copyright 2020-2023 Dr. Lars Voelker
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/*
* This is a dissector for PDUs transported over UDP or TCP.
* The transported PDUs are typically CAN, FlexRay, LIN, or other protocols.
*
* The format is as follows:
* uint32 ID
* uint32 length
* uint8[length] data
* (restart with ID, if more data exists)
*
* One known implementation of this protocol is the AUTOSAR Socket Adaptor
* with Header Option turned on.
* See AUTOSAR "Specification of Socket Adaptor" (SWS), Section 7.3 PDU Header option:
* https://www.autosar.org/fileadmin/user_upload/standards/classic/20-11/AUTOSAR_SWS_SocketAdaptor.pdf
*/
#include <config.h>
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/uat.h>
#include "packet-tcp.h"
#include <epan/reassemble.h>
#include "packet-udp.h"
#include "packet-pdu-transport.h"
#include <epan/decode_as.h>
#include <epan/proto_data.h>
void proto_register_pdu_transport(void);
void proto_reg_handoff_pdu_transport(void);
static int proto_pdu_transport = -1;
static dissector_handle_t pdu_transport_handle_udp = NULL;
static dissector_handle_t pdu_transport_handle_tcp = NULL;
static dissector_table_t subdissector_table = NULL;
#define PDU_TRANSPORT_NAME "PDU Transport"
#define PDU_TRANSPORT_HDR_LEN 8
/* header field */
static int hf_pdu_transport_id = -1;
static int hf_pdu_transport_length = -1;
static int hf_pdu_transport_payload = -1;
/* protocol tree items */
static gint ett_pdu_transport = -1;
/* expert info items */
static expert_field ef_pdu_transport_message_truncated = EI_INIT;
/********* UATs *********/
typedef struct _generic_one_id_string {
guint id;
gchar *name;
} generic_one_id_string_t;
static void
pdu_transport_free_key(gpointer key) {
wmem_free(wmem_epan_scope(), key);
}
static void
simple_free(gpointer data) {
/* we need to free because of the g_strdup in post_update*/
g_free(data);
}
/* ID -> Name */
static void *
copy_generic_one_id_string_cb(void *n, const void *o, size_t size _U_) {
generic_one_id_string_t *new_rec = (generic_one_id_string_t *)n;
const generic_one_id_string_t *old_rec = (const generic_one_id_string_t *)o;
new_rec->name = g_strdup(old_rec->name);
new_rec->id = old_rec->id;
return new_rec;
}
static bool
update_generic_one_identifier_32bit(void *r, char **err) {
generic_one_id_string_t *rec = (generic_one_id_string_t *)r;
if (rec->name == NULL || rec->name[0] == 0) {
*err = g_strdup("Name cannot be empty");
return FALSE;
}
return TRUE;
}
static void
free_generic_one_id_string_cb(void *r) {
generic_one_id_string_t *rec = (generic_one_id_string_t *)r;
/* freeing result of g_strdup */
g_free(rec->name);
rec->name = NULL;
}
static void
post_update_one_id_string_template_cb(generic_one_id_string_t *data, guint data_num, GHashTable *ht) {
guint i;
int *key = NULL;
for (i = 0; i < data_num; i++) {
key = wmem_new(wmem_epan_scope(), int);
*key = data[i].id;
g_hash_table_insert(ht, key, g_strdup(data[i].name));
}
}
static char *
ht_lookup_name(GHashTable *ht, unsigned int identifier) {
char *tmp = NULL;
unsigned int *id = NULL;
if (ht == NULL) {
return NULL;
}
id = wmem_new(wmem_epan_scope(), unsigned int);
*id = (unsigned int)identifier;
tmp = (char *)g_hash_table_lookup(ht, id);
wmem_free(wmem_epan_scope(), id);
return tmp;
}
/*** UAT pdu_transport_CM_IDs ***/
#define DATAFILE_PDU_IDS "PDU_Transport_identifiers"
static GHashTable *data_pdu_transport_pdus = NULL;
static generic_one_id_string_t *pdu_transport_pdus = NULL;
static guint pdu_transport_pdus_num = 0;
UAT_HEX_CB_DEF(pdu_transport_pdus, id, generic_one_id_string_t)
UAT_CSTRING_CB_DEF(pdu_transport_pdus, name, generic_one_id_string_t)
static void
post_update_pdu_transport_pdus_cb(void) {
/* destroy old hash table, if it exists */
if (data_pdu_transport_pdus) {
g_hash_table_destroy(data_pdu_transport_pdus);
data_pdu_transport_pdus = NULL;
}
/* create new hash table */
data_pdu_transport_pdus = g_hash_table_new_full(g_int_hash, g_int_equal, &pdu_transport_free_key, &simple_free);
post_update_one_id_string_template_cb(pdu_transport_pdus, pdu_transport_pdus_num, data_pdu_transport_pdus);
}
static int
dissect_pdu_transport(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
proto_item *ti_top = NULL;
proto_item *ti = NULL;
proto_tree *pdu_transport_tree = NULL;
guint offset = 0;
tvbuff_t *subtvb = NULL;
gint tmp = 0;
guint32 length = 0;
guint32 pdu_id = 0;
const gchar *descr;
if (p_get_proto_data(pinfo->pool, pinfo, proto_pdu_transport, pinfo->curr_layer_num) != NULL) {
col_append_str(pinfo->cinfo, COL_INFO, ", ");
col_set_fence(pinfo->cinfo, COL_INFO);
}
col_set_str(pinfo->cinfo, COL_INFO, "PDU");
col_set_str(pinfo->cinfo, COL_PROTOCOL, PDU_TRANSPORT_NAME);
ti_top = proto_tree_add_item(tree, proto_pdu_transport, tvb, 0, -1, ENC_NA);
pdu_transport_tree = proto_item_add_subtree(ti_top, ett_pdu_transport);
if (tvb_captured_length_remaining(tvb, offset) < 8) {
expert_add_info(pinfo, ti_top, &ef_pdu_transport_message_truncated);
}
/* taken from packet-ip.c
* if pdu_transport is not referenced from any filters we don't need to worry about
* generating any tree items. We must do this after we created the actual
* protocol above so that proto hier stat still works though.
* XXX: Note that because of the following optimization expert items must
* not be generated inside of an 'if (tree) ...'
* so that Analyze ! Expert ... will work.
*/
if (!proto_field_is_referenced(tree, proto_pdu_transport)) {
pdu_transport_tree = NULL;
}
ti = proto_tree_add_item_ret_uint(pdu_transport_tree, hf_pdu_transport_id, tvb, offset, 4, ENC_BIG_ENDIAN, &pdu_id);
offset += 4;
proto_tree_add_item_ret_uint(pdu_transport_tree, hf_pdu_transport_length, tvb, offset, 4, ENC_BIG_ENDIAN, &length);
offset += 4;
descr = ht_lookup_name(data_pdu_transport_pdus, pdu_id);
if (descr != NULL) {
proto_item_append_text(ti_top, ", ID 0x%x (%s), Length: %d", pdu_id, descr, length);
proto_item_append_text(ti, " (%s)", descr);
col_append_fstr(pinfo->cinfo, COL_INFO, " (ID: 0x%x, %s)", pdu_id, descr);
} else {
proto_item_append_text(ti_top, ", ID 0x%x, Length: %d", pdu_id, length);
col_append_fstr(pinfo->cinfo, COL_INFO, " (ID: 0x%x)", pdu_id);
}
p_add_proto_data(pinfo->pool, pinfo, proto_pdu_transport, pinfo->curr_layer_num, GUINT_TO_POINTER(pdu_id));
tmp = tvb_captured_length_remaining(tvb, offset);
if ((gint)length <= tmp) {
proto_tree_add_item(pdu_transport_tree, hf_pdu_transport_payload, tvb, offset, length, ENC_NA);
subtvb = tvb_new_subset_length_caplen(tvb, offset, length, length);
} else {
proto_tree_add_item(pdu_transport_tree, hf_pdu_transport_payload, tvb, offset, tmp, ENC_NA);
subtvb = tvb_new_subset_length_caplen(tvb, offset, tmp, length);
expert_add_info(pinfo, ti_top, &ef_pdu_transport_message_truncated);
}
if (subtvb != NULL) {
pdu_transport_info_t pdu_t_info;
pdu_t_info.id = pdu_id;
dissector_try_uint_new(subdissector_table, pdu_id, subtvb, pinfo, tree, FALSE, (void *)(&pdu_t_info));
}
offset += (gint)length;
col_set_fence(pinfo->cinfo, COL_INFO);
return offset;
}
static guint
get_pdu_transport_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_) {
return PDU_TRANSPORT_HDR_LEN + (guint)tvb_get_ntohl(tvb, offset + 4);
}
static int
dissect_pdu_transport_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data) {
tcp_dissect_pdus(tvb, pinfo, tree, TRUE, PDU_TRANSPORT_HDR_LEN, get_pdu_transport_message_len, dissect_pdu_transport, data);
return tvb_reported_length(tvb);
}
static int
dissect_pdu_transport_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data) {
return udp_dissect_pdus(tvb, pinfo, tree, PDU_TRANSPORT_HDR_LEN, NULL, get_pdu_transport_message_len, dissect_pdu_transport, data);
}
static void
pdu_transport_id_prompt(packet_info *pinfo, gchar *result) {
snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "PDU Transport ID 0x%08x as",
GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, proto_pdu_transport, pinfo->curr_layer_num)));
}
static gpointer
pdu_transport_id_value(packet_info *pinfo) {
/* Limitation: This only returns the last proto_data, since udp_dissect_pdus gives us the same layer for all. */
return p_get_proto_data(pinfo->pool, pinfo, proto_pdu_transport, pinfo->curr_layer_num);
}
void
proto_register_pdu_transport(void) {
module_t *pdu_transport_module = NULL;
expert_module_t *expert_module_pdu_transport = NULL;
uat_t *pdu_transport_pduid_uat = NULL;
static hf_register_info hf[] = {
{ &hf_pdu_transport_id,
{ "ID", "pdu_transport.id", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
{ &hf_pdu_transport_length,
{ "Length", "pdu_transport.length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
{ &hf_pdu_transport_payload,
{ "Payload", "pdu_transport.payload", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
};
static gint *ett[] = {
&ett_pdu_transport,
};
static ei_register_info ei[] = {
{ &ef_pdu_transport_message_truncated,{ "pdu_transport.message_truncated",
PI_MALFORMED, PI_ERROR, "PDU Transport Truncated message!", EXPFILL } },
};
/* Decode As handling */
static build_valid_func pdu_transport_da_build_value[1] = {pdu_transport_id_value};
static decode_as_value_t pdu_transport_da_values = {pdu_transport_id_prompt, 1, pdu_transport_da_build_value};
static decode_as_t pdu_transport_da = { "pdu_transport", "pdu_transport.id", 1, 0, &pdu_transport_da_values,
NULL, NULL, decode_as_default_populate_list,
decode_as_default_reset, decode_as_default_change, NULL };
proto_pdu_transport = proto_register_protocol("PDU Transport Protocol", PDU_TRANSPORT_NAME, "pdu_transport");
proto_register_field_array(proto_pdu_transport, hf, array_length(hf));
proto_register_subtree_array(ett, array_length(ett));
pdu_transport_module = prefs_register_protocol(proto_pdu_transport, NULL);
expert_module_pdu_transport = expert_register_protocol(proto_pdu_transport);
expert_register_field_array(expert_module_pdu_transport, ei, array_length(ei));
static uat_field_t pdu_transport_cm_id_uat_fields[] = {
UAT_FLD_HEX(pdu_transport_pdus, id, "ID", "ID (hex uint32)"),
UAT_FLD_CSTRING(pdu_transport_pdus, name, "Name", "Name of the PDU (string)"),
UAT_END_FIELDS
};
pdu_transport_pduid_uat = uat_new("pdu_transport Capture Modules",
sizeof(generic_one_id_string_t), /* record size */
DATAFILE_PDU_IDS, /* filename */
TRUE, /* from profile */
(void**)&pdu_transport_pdus, /* data_ptr */
&pdu_transport_pdus_num, /* numitems_ptr */
UAT_AFFECTS_DISSECTION, /* but not fields */
NULL, /* help */
copy_generic_one_id_string_cb, /* copy callback */
update_generic_one_identifier_32bit, /* update callback */
free_generic_one_id_string_cb, /* free callback */
post_update_pdu_transport_pdus_cb, /* post update callback */
NULL, /* reset callback */
pdu_transport_cm_id_uat_fields /* UAT field definitions */
);
prefs_register_uat_preference(pdu_transport_module, "_udf_pdu_transport_pdus", "PDUs",
"A table to define names and IDs of PDUs", pdu_transport_pduid_uat);
subdissector_table = register_dissector_table("pdu_transport.id", "PDU Transport ID", proto_pdu_transport, FT_UINT32, BASE_HEX);
register_decode_as(&pdu_transport_da);
}
void
proto_reg_handoff_pdu_transport(void) {
pdu_transport_handle_udp = register_dissector("pdu_transport_over_udp", dissect_pdu_transport_udp, proto_pdu_transport);
pdu_transport_handle_tcp = register_dissector("pdu_transport_over_tcp", dissect_pdu_transport_tcp, proto_pdu_transport);
dissector_add_uint_range_with_preference("udp.port", "", pdu_transport_handle_udp);
dissector_add_uint_range_with_preference("tcp.port", "", pdu_transport_handle_tcp);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|