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