replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.git] / epan / dissectors / packet-smp.c
1 /* packet-smp.c
2  * Routines for Session Multiplex Protocol (SMP) dissection
3  * January 2017 Uli Heilmeier with the help of Michael Mann
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 /*
13  * References:
14  * https://web.archive.org/web/20171009015219/https://msdn.microsoft.com/en-us/library/cc219643.aspx
15  * https://web.archive.org/web/20171223021159/https://msdn.microsoft.com/en-us/library/dd304523.aspx
16  * https://web.archive.org/web/20171005092351/https://docs.microsoft.com/en-us/sql/relational-databases/native-client/features/using-multiple-active-result-sets-mars
17  *
18  *     0                   1                   2                   3
19  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
20  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21  *    |       SMID    |     FLAGS     |               SID             |
22  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23  *    |                           LENGTH                              |
24  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25  *    |                           SEQNUM                              |
26  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27  *    |                            WNDW                               |
28  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29  *    |                                                               |
30  *    /                         DATA (variable)                       /
31  *    |                                                               |
32  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33  */
34
35 #include <config.h>
36 #include <epan/packet.h>
37 #include <epan/prefs.h>
38 #include <epan/decode_as.h>
39
40 #include "packet-tcp.h"
41
42 void proto_reg_handoff_smp(void);
43 void proto_register_smp(void);
44
45 static int proto_smp = -1;
46
47 static int hf_smp_smid = -1;
48 static int hf_smp_flags = -1;
49 static int hf_smp_flags_syn = -1;
50 static int hf_smp_flags_ack = -1;
51 static int hf_smp_flags_fin = -1;
52 static int hf_smp_flags_data = -1;
53 static int hf_smp_sid = -1;
54 static int hf_smp_length = -1;
55 static int hf_smp_seqnum = -1;
56 static int hf_smp_wndw = -1;
57 static int hf_smp_data = -1;
58
59 static gint ett_smp = -1;
60 static gint ett_smp_flags = -1;
61
62 #define SMP_FLAGS_SYN  0x01
63 #define SMP_FLAGS_ACK  0x02
64 #define SMP_FLAGS_FIN  0x04
65 #define SMP_FLAGS_DATA 0x08
66
67 #define SMP_MIN_LENGTH 16
68
69 static dissector_handle_t tds_handle;
70 static dissector_table_t smp_payload_table;
71
72 static gboolean reassemble_smp = TRUE;
73
74 static void smp_prompt(packet_info *pinfo _U_, gchar* result)
75 {
76     g_snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Payload as");
77 }
78
79 /* Code to actually dissect the packets */
80 static int
81 dissect_smp_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean tds_payload)
82 {
83     guint offset = 0;
84     guint remaining_bytes;
85     proto_item *ti;
86     proto_tree *smp_tree;
87     guint32 flags, sid, smp_length;
88     tvbuff_t* next_tvb;
89     int parsed_bytes;
90     static const int *flag_fields[] = {
91         &hf_smp_flags_syn,
92         &hf_smp_flags_ack,
93         &hf_smp_flags_fin,
94         &hf_smp_flags_data,
95         NULL
96     };
97
98     if (tvb_reported_length(tvb) < SMP_MIN_LENGTH)
99         return 0;
100
101     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMP");
102     col_clear(pinfo->cinfo, COL_INFO);
103
104     ti = proto_tree_add_item(tree, proto_smp, tvb, 0, -1, ENC_NA);
105     smp_tree = proto_item_add_subtree(ti, ett_smp);
106
107     proto_tree_add_item(smp_tree, hf_smp_smid, tvb, offset, 1, ENC_NA);
108     offset+=1;
109
110     proto_tree_add_bitmask(smp_tree, tvb, offset, hf_smp_flags, ett_smp_flags, flag_fields, ENC_NA);
111     flags = tvb_get_guint8(tvb, offset);
112     offset += 1;
113
114     proto_tree_add_item_ret_uint(smp_tree, hf_smp_sid, tvb, offset, 2, ENC_LITTLE_ENDIAN, &sid);
115     col_append_fstr(pinfo->cinfo, COL_INFO, "SID: %u", sid);
116     offset += 2;
117
118     if (flags & SMP_FLAGS_SYN)
119         col_append_str(pinfo->cinfo, COL_INFO, ", Syn");
120     if (flags & SMP_FLAGS_ACK)
121         col_append_str(pinfo->cinfo, COL_INFO, ", Ack");
122     if (flags & SMP_FLAGS_FIN)
123         col_append_str(pinfo->cinfo, COL_INFO, ", Fin");
124     if (flags & SMP_FLAGS_DATA)
125         col_append_str(pinfo->cinfo, COL_INFO, ", Data");
126
127     proto_tree_add_item_ret_uint(smp_tree, hf_smp_length, tvb, offset, 4, ENC_LITTLE_ENDIAN, &smp_length);
128     offset += 4;
129
130     proto_tree_add_item(smp_tree, hf_smp_seqnum, tvb, offset, 4, ENC_LITTLE_ENDIAN);
131     offset += 4;
132
133     proto_tree_add_item(smp_tree, hf_smp_wndw, tvb, offset, 4, ENC_LITTLE_ENDIAN);
134     offset += 4;
135
136     if ((flags & SMP_FLAGS_DATA) && (tvb_reported_length(tvb) > SMP_MIN_LENGTH)) {
137
138         next_tvb = tvb_new_subset_remaining(tvb, offset);
139         if (tds_payload) {
140             parsed_bytes = call_dissector(tds_handle, next_tvb, pinfo, tree);
141         } else {
142             parsed_bytes = dissector_try_payload(smp_payload_table, next_tvb, pinfo, tree);
143         }
144         if (parsed_bytes <= 0)
145         {
146             remaining_bytes = tvb_reported_length_remaining(tvb, offset);
147             if ( remaining_bytes < (smp_length - SMP_MIN_LENGTH)) {
148                 // Fragmented
149                 proto_tree_add_item(smp_tree, hf_smp_data, tvb, offset, remaining_bytes, ENC_NA);
150                 offset += remaining_bytes;
151             }
152             else {
153                 proto_tree_add_item(smp_tree, hf_smp_data, tvb, offset, smp_length - SMP_MIN_LENGTH, ENC_NA);
154                 offset += (smp_length - SMP_MIN_LENGTH);
155             }
156         }
157         else {
158             offset += parsed_bytes;
159         }
160     }
161
162     return offset;
163 }
164
165 static guint get_smp_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_)
166 {
167     return tvb_get_letohl(tvb, offset + 4);
168 }
169
170 static int
171 dissect_smp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
172 {
173     return dissect_smp_common(tvb, pinfo, tree, FALSE);
174 }
175
176 static int dissect_smp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
177 {
178     if ((tvb_reported_length(tvb) > 0) && (tvb_get_guint8(tvb, 0) == 0x53)) {
179         tcp_dissect_pdus(tvb, pinfo, tree, reassemble_smp, SMP_MIN_LENGTH,
180                      get_smp_pdu_len, dissect_smp_pdu, data);
181
182         return tvb_captured_length(tvb);
183     }
184
185     return 0;
186 }
187
188 static int
189 dissect_smp_tds(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
190 {
191     return dissect_smp_common(tvb, pinfo, tree, TRUE);
192 }
193
194 void
195 proto_register_smp(void)
196 {
197     static hf_register_info hf[] = {
198         { &hf_smp_smid,
199           { "Smid", "smp.smid", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
200         { &hf_smp_flags,
201           { "Flags", "smp.flags", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL } },
202         { &hf_smp_flags_syn,
203           { "Syn", "smp.flags.syn", FT_BOOLEAN, 8, TFS(&tfs_set_notset), SMP_FLAGS_SYN, NULL, HFILL }},
204         { &hf_smp_flags_ack,
205           { "Ack", "smp.flags.ack", FT_BOOLEAN, 8, TFS(&tfs_set_notset), SMP_FLAGS_ACK, NULL, HFILL }},
206         { &hf_smp_flags_fin,
207           { "Fin", "smp.flags.fin", FT_BOOLEAN, 8, TFS(&tfs_set_notset), SMP_FLAGS_FIN, NULL, HFILL }},
208         { &hf_smp_flags_data,
209           { "Data", "smp.flags.data", FT_BOOLEAN, 8, TFS(&tfs_set_notset), SMP_FLAGS_DATA, NULL, HFILL }},
210         { &hf_smp_sid,
211           { "SID", "smp.sid", FT_UINT16, BASE_DEC, NULL, 0, "Session ID", HFILL }},
212         { &hf_smp_length,
213           { "Length", "smp.length", FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL }},
214         { &hf_smp_seqnum,
215           { "SeqNum", "smp.seqnum", FT_UINT32, BASE_HEX, NULL, 0, "Sequence Number", HFILL }},
216         { &hf_smp_wndw,
217           { "Wndw", "smp.wndw", FT_UINT32, BASE_HEX, NULL, 0, "Window Size", HFILL }},
218         { &hf_smp_data,
219           { "Data", "smp.data", FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL }},
220     };
221
222     static gint *ett[] = {
223         &ett_smp,
224         &ett_smp_flags,
225     };
226
227     module_t *smp_module;
228
229     proto_smp = proto_register_protocol("Session Multiplex Protocol", "SMP", "smp");
230
231     proto_register_field_array(proto_smp, hf, array_length(hf));
232     proto_register_subtree_array(ett, array_length(ett));
233     register_dissector("smp_tds", dissect_smp_tds, proto_smp);
234
235     smp_payload_table = register_decode_as_next_proto(proto_smp, "SMP Payload", "smp.payload", "SMP Payload", (build_label_func*)&smp_prompt);
236
237     smp_module = prefs_register_protocol(proto_smp, NULL);
238     prefs_register_bool_preference(smp_module, "desegment",
239           "Reassemble SMP messages spanning multiple TCP segments",
240           "Whether the SMP dissector should reassemble messages spanning multiple TCP segments."
241           " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
242           &reassemble_smp);
243 }
244
245 void
246 proto_reg_handoff_smp(void)
247 {
248     dissector_handle_t smp_handle;
249     smp_handle = create_dissector_handle(dissect_smp, proto_smp);
250     dissector_add_for_decode_as_with_preference("tcp.port", smp_handle);
251
252     tds_handle = find_dissector_add_dependency("tds", proto_smp);
253 }
254
255 /*
256  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
257  *
258  * Local variables:
259  * c-basic-offset: 4
260  * tab-width: 8
261  * indent-tabs-mode: nil
262  * End:
263  *
264  * vi: set shiftwidth=4 tabstop=8 expandtab:
265  * :indentSize=4:tabSize=8:noTabs=true:
266  */