aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-samsung-ipc.c
blob: b3856cd848bd3b34cb04ebab36495646d133847b (plain)
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
/* packet-isi.c
 * Dissector for Samsung's samsung-ipc protocol
 * Copyright 2022, Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/packet.h>

void proto_register_samsung_ipc(void);
void proto_reg_handoff_samsung_ipc(void);

#define SAMSUNG_IPC_HDLC_START 0x7f
#define SAMSUNG_IPC_HDLC_END 0x7e

#if 0 /* This is to avoid defined but not used errors */
static const value_string ipc_requests_types[] = {
	{0x01, "IPC_TYPE_EXEC"},
	{0x02, "IPC_TYPE_GET"},
	{0x03, "IPC_TYPE_SET"},
	{0x04, "IPC_TYPE_CFRM"},
	{0x05, "IPC_TYPE_EVENT"},
};

static const value_string ipc_responses_types[] = {
	{0x01, "IPC_TYPE_INDI"},
	{0x02, "IPC_TYPE_RESP"},
	{0x03, "IPC_TYPE_NOTI"},
};

static const value_string hf_samsung_ipc_groups[] = {
	{0x01, "PWR"},
	{0x02, "CALL"},
	{0x04, "SMS"},
	{0x05, "SEC"},
	{0x06, "PB"},
	{0x07, "DISP"},
	{0x08, "NET"},
	{0x09, "SND"},
	{0x0A, "MISC"},
	{0x0B, "SVC"},
	{0x0C, "SS"},
	{0x0D, "GPRS"},
	{0x0E, "SAT"},
	{0x0F, "CFG"},
	{0x10, "IMEI"},
	{0x11, "GPS"},
	{0x12, "SAP"},
	{0x42, "RFS"},
	{0x80, "GEN"},
};

#endif

static gint proto_samsung_ipc = -1;
static gint hf_samsung_ipc = -1; // TODO: check variable name with isi
static gint ett_samsung_ipc = -1;

static int dissect_samsung_ipc(tvbuff_t *tvb, packet_info *pinfo,
			       proto_tree *tree _U_, void *data _U_)
{
//	proto_tree *samsung_ipc_tree;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "samsung-ipc");
	col_clear(pinfo->cinfo, COL_INFO);

	proto_tree_add_item(tree, proto_samsung_ipc, tvb, 0, -1, ENC_NA);
//	samsung_ipc_tree = proto_item_add_subtree(samsung_ipc_tree, ett_samsung_ipc);

//	proto_tree_add_item(samsung_ipc_tree, hf_samsung_ipc_groups, tvb, 7, 1, ENC_NA);

	return tvb_captured_length(tvb);
}

static gboolean dissect_single_usb_samsung_ipc(tvbuff_t *tvb,
					       packet_info *pinfo,
					       proto_tree *tree,
					       void *data, guint offset,
					       guint *length)
{
	tvbuff_t *next_tvb;
	guint tlv_length;

	tlv_length = tvb_reported_length(tvb);

	/* The length field is at 4 and its size is 2 */
	if (tlv_length - offset < 5)
		return FALSE;

	if (tvb_get_guint8(tvb, offset) != SAMSUNG_IPC_HDLC_START)
		return FALSE;

	/* From HDLC_START to HDLC_END */
	*length = tvb_get_letohs(tvb, offset + 4);
	*length += 5;

	if (offset + *length > tlv_length)
		return FALSE;

	if (tvb_get_guint8(tvb, offset + *length - 1) != SAMSUNG_IPC_HDLC_END)
		return FALSE;

	/* TODO: get the IPC channel from the USB enpoint
	 * From the device tree:
	 * 0x01 -> FMT
	 * 0x41 -> RFS
	 * 0x2a -> RAW
	 * 0x2b -> RAW
	 */

	/* Strip off the HDLC_START and HDLC_END */
	next_tvb = tvb_new_subset_length(tvb, offset + 1, *length - 2);

	dissect_samsung_ipc(next_tvb, pinfo, tree, data);

	return TRUE;
}

static gboolean dissect_usb_samsung_ipc(tvbuff_t *tvb, packet_info *pinfo,
					proto_tree *tree, void *data)
{
	gboolean found;
	guint offset;
	guint usb_sipc_length;
	guint tlv_length;

	tlv_length = tvb_reported_length(tvb);

	found = dissect_single_usb_samsung_ipc(tvb, pinfo, tree, data,
					       0, &usb_sipc_length);

	/* [HDLC_START:1][?:1][?:1][?:1][len:2][data:len-2][HDLC_END:1] */
	if (!found || tlv_length == usb_sipc_length)
		return found;

	/* An USB packet might have more than one samsung-ipc packet
	 * inside
	 */
	offset = usb_sipc_length;
	while(true) {
		gboolean _found;

		_found = dissect_single_usb_samsung_ipc(tvb, pinfo, tree, data,
							offset,
							&usb_sipc_length);
		if (!_found || tlv_length == offset + usb_sipc_length)
			return found;

		offset += usb_sipc_length;
	}
}

void proto_register_samsung_ipc(void)
{
	static hf_register_info hf[] = {
		{ &hf_samsung_ipc,
		  {
			  "group",
			  "samsung-ipc.group",
			  FT_UINT8,
			  BASE_HEX, // BASE_DEC ?
			  NULL, // VALS(hf_samsung_ipc_groups),
			  0x0,
			  NULL,
			  HFILL
		  }
		},
	};
	static gint *ett[] = {
		&ett_samsung_ipc
	};

	proto_samsung_ipc = proto_register_protocol("Samsung IPC",
						    "samsung-ipc",
						    "samsung-ipc");

	proto_register_field_array(proto_samsung_ipc, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}

/* Handler registration */
void proto_reg_handoff_samsung_ipc(void)
{
	static gboolean initialized = FALSE;

	if (!initialized) {
		create_dissector_handle(dissect_samsung_ipc, proto_samsung_ipc);

		heur_dissector_add("usb.bulk",
				   dissect_usb_samsung_ipc,
				   "samsung-ipc bulk endpoint",
				   "usb_bulk_samsung_ipc",
				   proto_samsung_ipc,
				   HEURISTIC_DISABLE);
	}
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */