Include packet-wlancap.h into the tarball
[obnox/wireshark/wip.git] / packet-mtp3.c
1 /* packet-mtp3.c
2  * Routines for Message Transfer Part Level 3 dissection
3  * Copyright 2001, Michael Tuexen <Michael.Tuexen@icn.siemens.de>
4  * Updated for ANSI support by Jeff Morriss <jeff.morriss[AT]ulticom.com>
5  *
6  * $Id: packet-mtp3.c,v 1.12 2002/06/20 20:40:36 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * Copied from README.developer
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <glib.h>
34
35 #include <epan/packet.h>
36 #include "prefs.h"
37
38 /* Initialize the protocol and registered fields */
39 static int proto_mtp3  = -1;
40 static module_t *mtp3_module;
41
42 static int hf_mtp3_service_indicator = -1;
43 static int hf_mtp3_network_indicator = -1;
44 static int hf_mtp3_itu_spare = -1;
45 static int hf_mtp3_ansi_priority = -1;
46 static int hf_mtp3_itu_opc = -1;
47 static int hf_mtp3_opc_network = -1;
48 static int hf_mtp3_opc_cluster = -1;
49 static int hf_mtp3_opc_member = -1;
50 static int hf_mtp3_itu_dpc = -1;
51 static int hf_mtp3_dpc_network = -1;
52 static int hf_mtp3_dpc_cluster = -1;
53 static int hf_mtp3_dpc_member = -1;
54 static int hf_mtp3_itu_sls = -1;
55 static int hf_mtp3_ansi_5_bit_sls = -1;
56 static int hf_mtp3_ansi_8_bit_sls = -1;
57
58 /* Initialize the subtree pointers */
59 static gint ett_mtp3 = -1;
60 static gint ett_mtp3_sio = -1;
61 static gint ett_mtp3_label = -1;
62 static gint ett_mtp3_label_dpc = -1;
63 static gint ett_mtp3_label_opc = -1;
64
65 static dissector_table_t mtp3_sio_dissector_table;
66
67 #include <packet-mtp3.h>
68 Standard_Type mtp3_standard = ITU_STANDARD;
69
70 gboolean mtp3_use_ansi_5_bit_sls = FALSE;
71
72 #define SIO_LENGTH                1
73
74 #define ITU_ROUTING_LABEL_LENGTH  4
75 #define ITU_HEADER_LENGTH         (SIO_LENGTH + ITU_ROUTING_LABEL_LENGTH)
76 #define ITU_SLS_LENGTH            1
77
78 #define SIO_OFFSET                0
79 #define ITU_SLS_OFFSET            (SIO_OFFSET + ITU_HEADER_LENGTH - ITU_SLS_LENGTH)
80 #define ITU_ROUTING_LABEL_OFFSET  (SIO_OFFSET + SIO_LENGTH)
81 #define ITU_MTP_PAYLOAD_OFFSET    (SIO_OFFSET + ITU_HEADER_LENGTH)
82
83 #define ANSI_SLS_LENGTH           1
84 #define ANSI_ROUTING_LABEL_LENGTH (ANSI_PC_LENGTH + ANSI_PC_LENGTH + ANSI_SLS_LENGTH)
85 #define ANSI_HEADER_LENGTH        (SIO_LENGTH + ANSI_ROUTING_LABEL_LENGTH)
86
87 #define ANSI_ROUTING_LABEL_OFFSET (SIO_OFFSET + SIO_LENGTH)
88 #define ANSI_DPC_OFFSET           ANSI_ROUTING_LABEL_OFFSET
89 #define ANSI_OPC_OFFSET           (ANSI_DPC_OFFSET + ANSI_PC_LENGTH)
90 #define ANSI_SLS_OFFSET           (ANSI_OPC_OFFSET + ANSI_PC_LENGTH)
91 #define ANSI_MTP_PAYLOAD_OFFSET   (SIO_OFFSET + ANSI_HEADER_LENGTH)
92
93 #define SERVICE_INDICATOR_MASK         0x0F
94 #define SPARE_MASK                     0x30
95 #define ANSI_PRIORITY_MASK             SPARE_MASK
96 #define NETWORK_INDICATOR_MASK         0xC0
97 #define ITU_DPC_MASK                   0x00003FFF
98 #define ITU_OPC_MASK                   0x0FFFC000
99 #define ITU_SLS_MASK                   0xF0000000
100
101 #define ANSI_NETWORK_MASK              0x0000FF
102 #define ANSI_CLUSTER_MASK              0x00FF00
103 #define ANSI_MEMBER_MASK               0xFF0000
104 #define ANSI_5BIT_SLS_MASK             0x1F
105 #define ANSI_8BIT_SLS_MASK             0xFF
106
107 static const value_string service_indicator_code_vals[] = {
108         { 0x0,  "Signalling Network Management Message (SNM)" },
109         { 0x1,  "Maintenance Regular Message (MTN)" },
110         { 0x2,  "Maintenance Special Message (MTNS)" },
111         { 0x3,  "SCCP" },
112         { 0x4,  "TUP" },
113         { 0x5,  "ISUP" },
114         { 0x6,  "DUP (call and circuit related messages)" },
115         { 0x7,  "DUP (facility registration and cancellation message)" },
116         { 0x8,  "MTP testing user part" },
117         { 0x9,  "Spare" },
118         { 0xa,  "Spare" },
119         { 0xb,  "Spare" },
120         { 0xc,  "Spare" },
121         { 0xd,  "Spare" },
122         { 0xe,  "Spare" },
123         { 0xf,  "Spare" },
124         { 0,    NULL }
125 };
126
127 static const value_string network_indicator_vals[] = {
128         { 0x0,  "International network" },
129         { 0x1,  "Spare (for international use only)" },
130         { 0x2,  "National network" },
131         { 0x3,  "Reserved for national use" },
132         { 0,    NULL }
133 };
134
135 static dissector_handle_t data_handle;
136
137 static void
138 dissect_mtp3_sio(tvbuff_t *tvb, proto_tree *mtp3_tree)
139 {
140   guint8 sio;
141   proto_item *sio_item;
142   proto_tree *sio_tree;
143
144   sio_item = proto_tree_add_text(mtp3_tree, tvb, SIO_OFFSET, SIO_LENGTH,
145                                  "Service information octet");
146   sio_tree = proto_item_add_subtree(sio_item, ett_mtp3_sio);
147
148   sio = tvb_get_guint8(tvb, SIO_OFFSET);
149   proto_tree_add_uint(sio_tree, hf_mtp3_network_indicator, tvb, SIO_OFFSET,
150                       SIO_LENGTH, sio);
151
152   switch(mtp3_standard){
153   case ANSI_STANDARD:
154     proto_tree_add_uint(sio_tree, hf_mtp3_ansi_priority, tvb, SIO_OFFSET,
155                         SIO_LENGTH, sio);
156     break;
157   case ITU_STANDARD:
158     proto_tree_add_uint(sio_tree, hf_mtp3_itu_spare, tvb, SIO_OFFSET,
159                         SIO_LENGTH, sio);
160     break;
161   }
162
163   proto_tree_add_uint(sio_tree, hf_mtp3_service_indicator, tvb, SIO_OFFSET,
164                       SIO_LENGTH, sio);
165 }
166
167 static void
168 dissect_mtp3_routing_label(tvbuff_t *tvb, proto_tree *mtp3_tree)
169 {
170   guint32 label, dpc, opc;
171   guint8 sls;
172   proto_item *label_item, *label_dpc_item, *label_opc_item;
173   proto_tree *label_tree, *label_dpc_tree, *label_opc_tree;
174
175   switch (mtp3_standard) {
176   case ITU_STANDARD:
177     label_item = proto_tree_add_text(mtp3_tree, tvb, ITU_ROUTING_LABEL_OFFSET,
178                                      ITU_ROUTING_LABEL_LENGTH, "Routing label");
179     label_tree = proto_item_add_subtree(label_item, ett_mtp3_label);
180
181     label = tvb_get_letohl(tvb, ITU_ROUTING_LABEL_OFFSET);
182     sls   = tvb_get_guint8(tvb, ITU_ROUTING_LABEL_OFFSET);
183
184     proto_tree_add_uint(label_tree, hf_mtp3_itu_dpc, tvb,
185                         ITU_ROUTING_LABEL_OFFSET, ITU_ROUTING_LABEL_LENGTH,
186                         label);
187     proto_tree_add_uint(label_tree, hf_mtp3_itu_opc, tvb,
188                         ITU_ROUTING_LABEL_OFFSET, ITU_ROUTING_LABEL_LENGTH,
189                         label);
190     proto_tree_add_uint(label_tree, hf_mtp3_itu_sls, tvb, ITU_SLS_OFFSET,
191                         ITU_SLS_LENGTH, sls);
192     break;
193
194   case ANSI_STANDARD:
195     /* this could be 5 or 8 bits */
196     sls = tvb_get_guint8(tvb, ANSI_SLS_OFFSET);
197
198     /* Create the Routing Label Tree */
199     label_item = proto_tree_add_text(mtp3_tree, tvb, ANSI_ROUTING_LABEL_OFFSET,
200                                      ANSI_ROUTING_LABEL_LENGTH,
201                                      "Routing label");
202     label_tree = proto_item_add_subtree(label_item, ett_mtp3_label);
203
204     /* create the DPC tree */
205     dpc = tvb_get_ntoh24(tvb, ANSI_DPC_OFFSET);
206     label_dpc_item = proto_tree_add_text(label_tree, tvb, ANSI_DPC_OFFSET,
207                                          ANSI_PC_LENGTH, "DPC (%d-%d-%d)",
208                                          (dpc & ANSI_NETWORK_MASK),
209                                          ((dpc & ANSI_CLUSTER_MASK) >> 8),
210                                          ((dpc & ANSI_MEMBER_MASK) >> 16));
211
212     label_dpc_tree = proto_item_add_subtree(label_dpc_item, ett_mtp3_label_dpc);
213
214     proto_tree_add_uint(label_dpc_tree, hf_mtp3_dpc_member, tvb,
215                         ANSI_DPC_OFFSET, ANSI_PC_LENGTH, dpc);
216     proto_tree_add_uint(label_dpc_tree, hf_mtp3_dpc_cluster,tvb,
217                         ANSI_DPC_OFFSET, ANSI_PC_LENGTH, dpc);
218     proto_tree_add_uint(label_dpc_tree, hf_mtp3_dpc_network,tvb,
219                         ANSI_DPC_OFFSET, ANSI_PC_LENGTH, dpc);
220
221     /* create the OPC tree */
222     opc = tvb_get_ntoh24(tvb, ANSI_OPC_OFFSET);
223
224     label_opc_item = proto_tree_add_text(label_tree, tvb, ANSI_OPC_OFFSET,
225                                          ANSI_PC_LENGTH, "OPC (%d-%d-%d)",
226                                          (opc & ANSI_NETWORK_MASK),
227                                          ((opc & ANSI_CLUSTER_MASK) >> 8),
228                                          ((opc & ANSI_MEMBER_MASK) >> 16));
229     label_opc_tree = proto_item_add_subtree(label_opc_item, ett_mtp3_label_opc);
230
231     proto_tree_add_uint(label_opc_tree, hf_mtp3_opc_member, tvb,
232                         ANSI_OPC_OFFSET, ANSI_PC_LENGTH, opc);
233     proto_tree_add_uint(label_opc_tree, hf_mtp3_opc_cluster, tvb,
234                         ANSI_OPC_OFFSET, ANSI_PC_LENGTH, opc);
235     proto_tree_add_uint(label_opc_tree, hf_mtp3_opc_network,tvb,
236                         ANSI_OPC_OFFSET, ANSI_PC_LENGTH, opc);
237
238     /* SLS */
239     if (mtp3_use_ansi_5_bit_sls)
240       proto_tree_add_item(label_tree, hf_mtp3_ansi_5_bit_sls, tvb,
241                           ANSI_SLS_OFFSET, ANSI_SLS_LENGTH, sls);
242     else
243       proto_tree_add_item(label_tree, hf_mtp3_ansi_8_bit_sls, tvb,
244                           ANSI_SLS_OFFSET, ANSI_SLS_LENGTH, sls);
245     break;
246   }
247 }
248
249 static void
250 dissect_mtp3_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
251 {
252   guint8 sio;
253   guint8 service_indicator;
254   tvbuff_t *payload_tvb = NULL;
255
256   sio               = tvb_get_guint8(tvb, SIO_OFFSET);
257   service_indicator = sio & SERVICE_INDICATOR_MASK;
258
259   switch (mtp3_standard) {
260   case ITU_STANDARD:
261     payload_tvb = tvb_new_subset(tvb, ITU_MTP_PAYLOAD_OFFSET, -1, -1);
262     break;
263   case ANSI_STANDARD:
264     payload_tvb = tvb_new_subset(tvb, ANSI_MTP_PAYLOAD_OFFSET, -1, -1);
265     break;
266   }
267
268   if (!dissector_try_port(mtp3_sio_dissector_table, service_indicator,
269                           payload_tvb, pinfo, tree)) {
270     call_dissector(data_handle, payload_tvb, pinfo, tree);
271     if (check_col(pinfo->cinfo, COL_INFO))
272       col_append_str(pinfo->cinfo, COL_INFO, "DATA ");
273   }
274 }
275
276 /* Code to actually dissect the packets */
277 static void
278 dissect_mtp3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
279 {
280
281   /* Set up structures needed to add the protocol subtree and manage it */
282   proto_item *mtp3_item = NULL;
283   proto_tree *mtp3_tree = NULL;
284
285   /* Make entries in Protocol column and Info column on summary display */
286   if (check_col(pinfo->cinfo, COL_PROTOCOL))
287     col_set_str(pinfo->cinfo, COL_PROTOCOL, "MTP3");
288
289   if (tree) {
290     /* create display subtree for the protocol */
291     switch (mtp3_standard) {
292     case ITU_STANDARD:
293       mtp3_item = proto_tree_add_item(tree, proto_mtp3, tvb, 0,
294                                       ITU_HEADER_LENGTH, FALSE);
295       break;
296     case ANSI_STANDARD:
297       mtp3_item = proto_tree_add_item(tree, proto_mtp3, tvb, 0,
298                                       ANSI_HEADER_LENGTH, FALSE);
299       break;
300     }
301     mtp3_tree = proto_item_add_subtree(mtp3_item, ett_mtp3);
302
303     /* dissect the packet */
304     dissect_mtp3_sio(tvb, mtp3_tree);
305     dissect_mtp3_routing_label(tvb, mtp3_tree);
306   }
307
308   /* Need to dissect payload even if !tree so can call sub-dissectors */
309   dissect_mtp3_payload(tvb, pinfo, tree);
310 }
311
312 void
313 proto_register_mtp3(void)
314 {
315
316   /* Setup list of header fields  See Section 1.6.1 for details*/
317   static hf_register_info hf[] = {
318     { &hf_mtp3_service_indicator,
319       { "Service indicator", "mtp3.service_indicator",
320         FT_UINT8, BASE_HEX, VALS(service_indicator_code_vals), SERVICE_INDICATOR_MASK,
321               "", HFILL }},
322     { &hf_mtp3_network_indicator,
323       { "Network indicator", "mtp3.network_indicator",
324               FT_UINT8, BASE_HEX, VALS(network_indicator_vals), NETWORK_INDICATOR_MASK,
325               "", HFILL }},
326     { &hf_mtp3_itu_spare,
327       { "Spare", "mtp3.spare",
328               FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
329               "", HFILL }},
330     { &hf_mtp3_ansi_priority,
331       { "Priority", "mtp3.priority",
332               FT_UINT8, BASE_HEX, NULL, ANSI_PRIORITY_MASK,
333               "", HFILL }},
334     { &hf_mtp3_itu_opc,
335       { "OPC", "mtp3.opc",
336               FT_UINT32, BASE_DEC, NULL, ITU_OPC_MASK,
337               "", HFILL }},
338     { &hf_mtp3_opc_network,
339      { "OPC Network", "mtp3.opc.network",
340              FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
341              "", HFILL }},
342     { &hf_mtp3_opc_cluster,
343       { "OPC Cluster", "mtp3.opc.cluster",
344               FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
345               "", HFILL }},
346     { &hf_mtp3_opc_member,
347       { "OPC Member", "mtp3.opc.member",
348               FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
349               "", HFILL }},
350     { &hf_mtp3_itu_dpc,
351       { "DPC", "mtp3.dpc",
352               FT_UINT32, BASE_DEC, NULL, ITU_DPC_MASK,
353               "", HFILL }},
354     { &hf_mtp3_dpc_network,
355       { "DPC Network", "mtp3.dpc.network",
356               FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
357               "", HFILL }},
358     { &hf_mtp3_dpc_cluster,
359       { "DPC Cluster", "mtp3.dpc.cluster",
360               FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
361               "", HFILL }},
362     { &hf_mtp3_dpc_member,
363       { "DPC Member", "mtp3.dpc.member",
364               FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
365               "", HFILL }},
366     { &hf_mtp3_itu_sls,
367       { "Signalling Link Selector", "mtp3.sls",
368               FT_UINT32, BASE_DEC, NULL, ITU_SLS_MASK,
369               "", HFILL }},
370     { &hf_mtp3_ansi_5_bit_sls,
371       { "Signalling Link Selector", "mtp3.sls",
372               FT_UINT8, BASE_DEC, NULL, ANSI_5BIT_SLS_MASK,
373               "", HFILL }},
374     { &hf_mtp3_ansi_8_bit_sls,
375       { "Signalling Link Selector", "mtp3.sls",
376               FT_UINT8, BASE_DEC, NULL, ANSI_8BIT_SLS_MASK,
377               "", HFILL }}
378   };
379
380   /* Setup protocol subtree array */
381   static gint *ett[] = {
382     &ett_mtp3,
383     &ett_mtp3_sio,
384     &ett_mtp3_label,
385     &ett_mtp3_label_dpc,
386     &ett_mtp3_label_opc
387   };
388
389   static enum_val_t mtp3_options[] = {
390     { "ITU",  ITU_STANDARD },
391     { "ANSI", ANSI_STANDARD },
392     { NULL, 0 }
393   };
394
395   /* Register the protocol name and description */
396   proto_mtp3 = proto_register_protocol("Message Transfer Part Level 3",
397                                        "MTP3", "mtp3");
398   register_dissector("mtp3", dissect_mtp3, proto_mtp3);
399
400   /* Required function calls to register the header fields and subtrees used */
401   proto_register_field_array(proto_mtp3, hf, array_length(hf));
402   proto_register_subtree_array(ett, array_length(ett));
403
404   mtp3_sio_dissector_table = register_dissector_table("mtp3.service_indicator",
405                                                       "MTP3 Service indicator",
406                                                       FT_UINT8, BASE_HEX);
407
408   mtp3_module = prefs_register_protocol(proto_mtp3, NULL);
409
410   /* Version 1.5 to 1.11 of this module used "mtp3_standard" */
411   prefs_register_obsolete_preference(mtp3_module, "mtp3_standard");
412
413   prefs_register_enum_preference(mtp3_module, "standard", "MTP3 standard",
414                                  "The SS7 standard used in MTP3 packets",
415                                  (gint *)&mtp3_standard, mtp3_options, FALSE);
416
417   prefs_register_bool_preference(mtp3_module, "ansi_5_bit_sls",
418                                  "Use 5-bit SLS (ANSI only)",
419                                  "Use 5-bit (instead of 8-bit) SLS in ANSI MTP3 packets",
420                                  &mtp3_use_ansi_5_bit_sls);
421 }
422
423 void
424 proto_reg_handoff_mtp3(void)
425 {
426   data_handle = find_dissector("data");
427 }