Correctly dissect LSA security descriptors, at least as they appear
[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.11 2002/03/04 22:39:22 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_sls = -1;
56
57 /* Initialize the subtree pointers */
58 static gint ett_mtp3 = -1;
59 static gint ett_mtp3_sio = -1;
60 static gint ett_mtp3_label = -1;
61 static gint ett_mtp3_label_dpc = -1;
62 static gint ett_mtp3_label_opc = -1;
63
64 static dissector_table_t mtp3_sio_dissector_table;
65
66 #include <packet-mtp3.h>
67 Standard_Type mtp3_standard = ITU_STANDARD;
68
69 #define SIO_LENGTH                1
70
71 #define ITU_ROUTING_LABEL_LENGTH  4
72 #define ITU_HEADER_LENGTH         (SIO_LENGTH + ITU_ROUTING_LABEL_LENGTH)
73 #define ITU_SLS_LENGTH            1
74
75 #define SIO_OFFSET                0
76 #define ITU_SLS_OFFSET            (SIO_OFFSET + ITU_HEADER_LENGTH - ITU_SLS_LENGTH)
77 #define ITU_ROUTING_LABEL_OFFSET  (SIO_OFFSET + SIO_LENGTH)
78 #define ITU_MTP_PAYLOAD_OFFSET    (SIO_OFFSET + ITU_HEADER_LENGTH)
79
80 #define ANSI_SLS_LENGTH           1
81 #define ANSI_ROUTING_LABEL_LENGTH (ANSI_PC_LENGTH + ANSI_PC_LENGTH + ANSI_SLS_LENGTH)
82 #define ANSI_HEADER_LENGTH        (SIO_LENGTH + ANSI_ROUTING_LABEL_LENGTH)
83
84 #define ANSI_ROUTING_LABEL_OFFSET (SIO_OFFSET + SIO_LENGTH)
85 #define ANSI_DPC_OFFSET           ANSI_ROUTING_LABEL_OFFSET
86 #define ANSI_OPC_OFFSET           (ANSI_DPC_OFFSET + ANSI_PC_LENGTH)
87 #define ANSI_SLS_OFFSET           (ANSI_OPC_OFFSET + ANSI_PC_LENGTH)
88 #define ANSI_MTP_PAYLOAD_OFFSET   (SIO_OFFSET + ANSI_HEADER_LENGTH)
89
90 #define SERVICE_INDICATOR_MASK         0x0F
91 #define SPARE_MASK                     0x30
92 #define ANSI_PRIORITY_MASK             SPARE_MASK
93 #define NETWORK_INDICATOR_MASK         0xC0
94 #define ITU_DPC_MASK                   0x00003FFF
95 #define ITU_OPC_MASK                   0x0FFFC000
96 #define ITU_SLS_MASK                   0xF0000000
97
98 #define ANSI_NETWORK_MASK              0x0000FF
99 #define ANSI_CLUSTER_MASK              0x00FF00
100 #define ANSI_MEMBER_MASK               0xFF0000
101 #define ANSI_5BIT_SLS_MASK             0x1F
102 /* TODO: eventually add support for ANSI 8-bit SLS */
103 #define ANSI_8BIT_SLS_MASK             0xFF
104
105 static const value_string service_indicator_code_vals[] = {
106         { 0x0,  "Signalling Network Management Message (SNM)" },
107         { 0x1,  "Maintenance Regular Message (MTN)" },
108         { 0x2,  "Maintenance Special Message (MTNS)" },
109         { 0x3,  "SCCP" },
110         { 0x4,  "TUP" },
111         { 0x5,  "ISUP" },
112         { 0x6,  "DUP (call and circuit related messages)" },
113         { 0x7,  "DUP (facility registration and cancellation message)" },
114         { 0x8,  "MTP testing user part" },
115         { 0x9,  "Spare" },
116         { 0xa,  "Spare" },
117         { 0xb,  "Spare" },
118         { 0xc,  "Spare" },
119         { 0xd,  "Spare" },
120         { 0xe,  "Spare" },
121         { 0xf,  "Spare" },
122         { 0,    NULL }
123 };
124
125 static const value_string network_indicator_vals[] = {
126         { 0x0,  "International network" },
127         { 0x1,  "Spare (for international use only)" },
128         { 0x2,  "National network" },
129         { 0x3,  "Reserved for national use" },
130         { 0,    NULL }
131 };
132
133 static dissector_handle_t data_handle;
134
135 static void
136 dissect_mtp3_sio(tvbuff_t *tvb, proto_tree *mtp3_tree)
137 {
138   guint8 sio;
139   proto_item *sio_item;
140   proto_tree *sio_tree;
141
142   sio_item = proto_tree_add_text(mtp3_tree, tvb, SIO_OFFSET, SIO_LENGTH, "Service information octet");
143   sio_tree = proto_item_add_subtree(sio_item, ett_mtp3_sio);
144
145   sio = tvb_get_guint8(tvb, SIO_OFFSET);
146   proto_tree_add_uint(sio_tree, hf_mtp3_network_indicator, tvb, SIO_OFFSET, SIO_LENGTH, sio);
147   
148   switch(mtp3_standard){
149   case ANSI_STANDARD:
150     proto_tree_add_uint(sio_tree, hf_mtp3_ansi_priority, tvb, SIO_OFFSET, SIO_LENGTH, sio);
151     break;
152   case ITU_STANDARD:
153     proto_tree_add_uint(sio_tree, hf_mtp3_itu_spare, tvb, SIO_OFFSET, SIO_LENGTH, sio);
154     break;
155   }
156   
157   proto_tree_add_uint(sio_tree, hf_mtp3_service_indicator, tvb, SIO_OFFSET, SIO_LENGTH, sio);
158 }
159
160 static void
161 dissect_mtp3_routing_label(tvbuff_t *tvb, proto_tree *mtp3_tree)
162 {
163   guint32 label, dpc, opc;
164   guint8 sls;
165   proto_item *label_item, *label_dpc_item, *label_opc_item;
166   proto_tree *label_tree, *label_dpc_tree, *label_opc_tree;
167
168   switch (mtp3_standard) {
169   case ITU_STANDARD:
170     label_item = proto_tree_add_text(mtp3_tree, tvb, ITU_ROUTING_LABEL_OFFSET, ITU_ROUTING_LABEL_LENGTH, "Routing label");
171     label_tree = proto_item_add_subtree(label_item, ett_mtp3_label);
172
173     label = tvb_get_letohl(tvb, ITU_ROUTING_LABEL_OFFSET);
174     sls   = tvb_get_guint8(tvb, ITU_ROUTING_LABEL_OFFSET);
175
176     proto_tree_add_uint(label_tree, hf_mtp3_itu_dpc, tvb, ITU_ROUTING_LABEL_OFFSET, ITU_ROUTING_LABEL_LENGTH, label);
177     proto_tree_add_uint(label_tree, hf_mtp3_itu_opc, tvb, ITU_ROUTING_LABEL_OFFSET, ITU_ROUTING_LABEL_LENGTH, label);
178     proto_tree_add_uint(label_tree, hf_mtp3_itu_sls, tvb, ITU_SLS_OFFSET, ITU_SLS_LENGTH, sls);
179     break;
180
181   case ANSI_STANDARD:
182     /* this could be 5 or 8 bits */
183     sls = tvb_get_guint8(tvb, ANSI_SLS_OFFSET);
184
185     /* Create the Routing Label Tree */
186     label_item = proto_tree_add_text(mtp3_tree, tvb, ANSI_ROUTING_LABEL_OFFSET, ANSI_ROUTING_LABEL_LENGTH, "Routing label");
187     label_tree = proto_item_add_subtree(label_item, ett_mtp3_label);
188
189     /* create the DPC tree */
190     dpc = tvb_get_ntoh24(tvb, ANSI_DPC_OFFSET);
191     label_dpc_item = proto_tree_add_text(label_tree, tvb, ANSI_DPC_OFFSET, ANSI_PC_LENGTH,
192                                          "DPC (%d-%d-%d)", (dpc & ANSI_NETWORK_MASK), ((dpc & ANSI_CLUSTER_MASK) >> 8), ((dpc & ANSI_MEMBER_MASK) >> 16));
193
194     label_dpc_tree = proto_item_add_subtree(label_dpc_item, ett_mtp3_label_dpc);
195
196     proto_tree_add_uint(label_dpc_tree, hf_mtp3_dpc_member, tvb, ANSI_DPC_OFFSET, ANSI_PC_LENGTH, dpc);
197     proto_tree_add_uint(label_dpc_tree, hf_mtp3_dpc_cluster,tvb, ANSI_DPC_OFFSET, ANSI_PC_LENGTH, dpc);
198     proto_tree_add_uint(label_dpc_tree, hf_mtp3_dpc_network,tvb, ANSI_DPC_OFFSET, ANSI_PC_LENGTH, dpc);
199
200     /* create the OPC tree */
201     opc = tvb_get_ntoh24(tvb, ANSI_OPC_OFFSET);
202
203     label_opc_item = proto_tree_add_text(label_tree, tvb, ANSI_OPC_OFFSET, ANSI_PC_LENGTH,
204                                          "OPC (%d-%d-%d)", (opc & ANSI_NETWORK_MASK), ((opc & ANSI_CLUSTER_MASK) >> 8), ((opc & ANSI_MEMBER_MASK) >> 16));
205     label_opc_tree = proto_item_add_subtree(label_opc_item, ett_mtp3_label_opc);
206
207     proto_tree_add_uint(label_opc_tree, hf_mtp3_opc_member, tvb, ANSI_OPC_OFFSET, ANSI_PC_LENGTH, opc);
208     proto_tree_add_uint(label_opc_tree, hf_mtp3_opc_cluster, tvb, ANSI_OPC_OFFSET, ANSI_PC_LENGTH, opc);
209     proto_tree_add_uint(label_opc_tree, hf_mtp3_opc_network,tvb, ANSI_OPC_OFFSET, ANSI_PC_LENGTH, opc);
210     /* SLS */
211     /* TODO: separate 5-bit and 8-bit SLS */
212     proto_tree_add_item(label_tree, hf_mtp3_ansi_sls, tvb, ANSI_SLS_OFFSET, ANSI_SLS_LENGTH, sls);
213     /*    proto_tree_add_uint(label_tree, hf_mtp3_ansi_sls, tvb,ANSI_SLS_OFFSET, ANSI_SLS_LENGTH, sls);*/
214     break;
215   }
216 }
217
218 static void
219 dissect_mtp3_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
220 {
221   guint8 sio;
222   guint8 service_indicator;
223   tvbuff_t *payload_tvb = NULL;
224
225   sio               = tvb_get_guint8(tvb, SIO_OFFSET);
226   service_indicator = sio & SERVICE_INDICATOR_MASK;
227
228   switch (mtp3_standard) {
229   case ITU_STANDARD:
230     payload_tvb = tvb_new_subset(tvb, ITU_MTP_PAYLOAD_OFFSET, -1, -1);
231     break;
232   case ANSI_STANDARD:
233     payload_tvb = tvb_new_subset(tvb, ANSI_MTP_PAYLOAD_OFFSET, -1, -1);
234     break;
235   }
236
237   if (!dissector_try_port(mtp3_sio_dissector_table, service_indicator, payload_tvb, pinfo, tree)) {
238     call_dissector(data_handle, payload_tvb, pinfo, tree);
239     if (check_col(pinfo->cinfo, COL_INFO))
240       col_append_str(pinfo->cinfo, COL_INFO, "DATA ");
241   }
242 }
243
244 /* Code to actually dissect the packets */
245 static void
246 dissect_mtp3(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
247 {
248
249   /* Set up structures needed to add the protocol subtree and manage it */
250   proto_item *mtp3_item = NULL;
251   proto_tree *mtp3_tree = NULL;
252
253   /* Make entries in Protocol column and Info column on summary display */
254   if (check_col(pinfo->cinfo, COL_PROTOCOL))
255     col_set_str(pinfo->cinfo, COL_PROTOCOL, "MTP3");
256
257   if (tree) {
258     /* create display subtree for the protocol */
259     switch (mtp3_standard) {
260     case ITU_STANDARD:
261       mtp3_item = proto_tree_add_item(tree, proto_mtp3, tvb, 0, ITU_HEADER_LENGTH, FALSE);
262       break;
263     case ANSI_STANDARD:
264       mtp3_item = proto_tree_add_item(tree, proto_mtp3, tvb, 0, ANSI_HEADER_LENGTH, FALSE);
265       break;
266     }
267     mtp3_tree = proto_item_add_subtree(mtp3_item, ett_mtp3);
268
269     /* dissect the packet */
270     dissect_mtp3_sio(tvb, mtp3_tree);
271     dissect_mtp3_routing_label(tvb, mtp3_tree);
272   }
273
274   /* Need to dissect payload even if !tree so can call sub-dissectors */
275   dissect_mtp3_payload(tvb, pinfo, tree);
276 }
277
278 void
279 proto_register_mtp3(void)
280 {
281
282   /* Setup list of header fields  See Section 1.6.1 for details*/
283   static hf_register_info hf[] = {
284     { &hf_mtp3_service_indicator,
285       { "Service indicator", "mtp3.service_indicator",
286         FT_UINT8, BASE_HEX, VALS(service_indicator_code_vals), SERVICE_INDICATOR_MASK,
287               "", HFILL }},
288     { &hf_mtp3_network_indicator,
289       { "Network indicator", "mtp3.network_indicator",
290               FT_UINT8, BASE_HEX, VALS(network_indicator_vals), NETWORK_INDICATOR_MASK,
291               "", HFILL }},
292     { &hf_mtp3_itu_spare,
293       { "Spare", "mtp3.spare",
294               FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
295               "", HFILL }},
296     { &hf_mtp3_ansi_priority,
297       { "Priority", "mtp3.priority",
298               FT_UINT8, BASE_HEX, NULL, ANSI_PRIORITY_MASK,
299               "", HFILL }},
300     { &hf_mtp3_itu_opc,
301       { "OPC", "mtp3.opc",
302               FT_UINT32, BASE_DEC, NULL, ITU_OPC_MASK,
303               "", HFILL }},
304     { &hf_mtp3_opc_network,
305      { "OPC Network", "mtp3.opc.network",
306              FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
307              "", HFILL }},
308     { &hf_mtp3_opc_cluster,
309       { "OPC Cluster", "mtp3.opc.cluster",
310               FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
311               "", HFILL }},
312     { &hf_mtp3_opc_member,
313       { "OPC Member", "mtp3.opc.member",
314               FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
315               "", HFILL }},
316     { &hf_mtp3_itu_dpc,
317       { "DPC", "mtp3.dpc",
318               FT_UINT32, BASE_DEC, NULL, ITU_DPC_MASK,
319               "", HFILL }},
320     { &hf_mtp3_dpc_network,
321       { "DPC Network", "mtp3.dpc.network",
322               FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
323               "", HFILL }},
324     { &hf_mtp3_dpc_cluster,
325       { "DPC Cluster", "mtp3.dpc.cluster",
326               FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
327               "", HFILL }},
328     { &hf_mtp3_dpc_member,
329       { "DPC Member", "mtp3.dpc.member",
330               FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
331               "", HFILL }},
332     { &hf_mtp3_itu_sls,
333       { "Signalling Link Selector", "mtp3.sls",
334               FT_UINT32, BASE_DEC, NULL, ITU_SLS_MASK,
335               "", HFILL }},
336     { &hf_mtp3_ansi_sls,
337       { "Signalling Link Selector", "mtp3.sls",
338               FT_UINT8, BASE_DEC, NULL, ANSI_5BIT_SLS_MASK,
339               "", HFILL }}
340   };
341
342   /* Setup protocol subtree array */
343   static gint *ett[] = {
344     &ett_mtp3,
345     &ett_mtp3_sio,
346     &ett_mtp3_label,
347     &ett_mtp3_label_dpc,
348     &ett_mtp3_label_opc
349   };
350
351   static enum_val_t mtp3_options[] = {
352     { "ITU",  ITU_STANDARD },
353     { "ANSI", ANSI_STANDARD },
354     { NULL, 0 }
355   };
356
357   /* Register the protocol name and description */
358   proto_mtp3 = proto_register_protocol("Message Transfer Part Level 3", "MTP3", "mtp3");
359   register_dissector("mtp3", dissect_mtp3, proto_mtp3);
360
361   /* Required function calls to register the header fields and subtrees used */
362   proto_register_field_array(proto_mtp3, hf, array_length(hf));
363   proto_register_subtree_array(ett, array_length(ett));
364
365   mtp3_sio_dissector_table = register_dissector_table("mtp3.service_indicator", "MTP3 Service indicator", FT_UINT8, BASE_HEX);
366   mtp3_module = prefs_register_protocol(proto_mtp3, NULL);
367   prefs_register_enum_preference(mtp3_module, "mtp3_standard", "MTP3 standard", "MTP3 standard",  (gint *)&mtp3_standard, mtp3_options, FALSE);
368 }
369
370 void
371 proto_reg_handoff_mtp3(void)
372 {
373   data_handle = find_dissector("data");
374 }