56ac3c2f2ac3be2bb80c214c9254a611e5f3ce26
[obnox/wireshark/wip.git] / packet-m2pa.c
1 /* packet-m2pa.c
2  * Routines for MTP2 Peer Adaptation Layer dissection
3  * It is hopefully (needs testing) compliant to
4  * http://www.ietf.org/internet-drafts/draft-ietf-sigtran-m2pa-03.txt
5  *
6  * Copyright 2001, Jeff Morriss <jeff.morriss[AT]ulticom.com>, 
7  * updated by Michael Tuexen <michael.tuexen[AT]icn.siemens.de>
8  *
9  * $Id: packet-m2pa.c,v 1.5 2002/01/20 22:12:26 guy Exp $
10  *
11  * Ethereal - Network traffic analyzer
12  * By Gerald Combs <gerald@ethereal.com>
13  * Copyright 1998 Gerald Combs
14  *
15  * Copied from packet-m3ua.c
16  * 
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  * 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * 
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38
39
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #ifdef HAVE_NETINET_IN_H
45 # include <netinet/in.h>
46 #endif
47
48 #include <string.h>
49 #include <glib.h>
50
51 #ifdef NEED_SNPRINTF_H
52 # include "snprintf.h"
53 #endif
54
55 #include "packet.h"
56 #include "prefs.h"
57
58 /* Warning:  Neither of these are standardized yet! */
59 #define SCTP_PORT_M2PA             2904
60 #define M2PA_PAYLOAD_PROTOCOL_ID   5
61
62 #define VERSION_LENGTH         1
63 #define SPARE_LENGTH           1
64 #define CLASS_LENGTH           1
65 #define TYPE_LENGTH            1
66 #define LENGTH_LENGTH          4
67 #define COMMON_HEADER_LENGTH   (VERSION_LENGTH + SPARE_LENGTH + \
68                                 CLASS_LENGTH + TYPE_LENGTH + LENGTH_LENGTH)
69
70 #define VERSION_OFFSET         0
71 #define SPARE_OFFSET           (VERSION_OFFSET + VERSION_LENGTH)
72 #define CLASS_OFFSET           (SPARE_OFFSET + SPARE_LENGTH)
73 #define TYPE_OFFSET            (CLASS_OFFSET + CLASS_LENGTH)
74 #define LENGTH_OFFSET          (TYPE_OFFSET + TYPE_LENGTH)
75 #define COMMON_HEADER_OFFSET   VERSION_OFFSET
76 #define MESSAGE_DATA_OFFSET    (COMMON_HEADER_OFFSET + COMMON_HEADER_LENGTH)
77
78 #define PROTOCOL_VERSION_RELEASE_1        1
79 static const value_string m2pa_protocol_version_values[] = {
80   { PROTOCOL_VERSION_RELEASE_1,  "Release 1" },
81   { 0,                           NULL } };
82
83
84 #define MESSAGE_CLASS_M2PA                0xb
85
86 static const value_string m2pa_message_class_values[] = {
87   { MESSAGE_CLASS_M2PA,          "M2PA" },
88   { 0,                           NULL } };
89
90
91 #define MESSAGE_TYPE_USER_DATA            0x1
92 #define MESSAGE_TYPE_LINK_STATUS          0x2
93 #define MESSAGE_TYPE_PROVING_DATA         0x3
94
95 static const value_string m2pa_message_type_values[] = {
96   { MESSAGE_TYPE_USER_DATA,     "User Data" },
97   { MESSAGE_TYPE_LINK_STATUS,   "Link Status" },
98   { MESSAGE_TYPE_PROVING_DATA,  "Proving Data" },
99   { 0,                           NULL } };
100
101
102 /* parts of User Data message */
103 #define LI_OFFSET             0
104 #define LI_LENGTH             1
105 #define MTP3_OFFSET           (LI_OFFSET + LI_LENGTH)
106
107 /* LI is only used for (ITU national) priority in M2PA */
108 #define LI_SPARE_MASK              0xfc
109 #define LI_PRIORITY_MASK           0x3
110
111
112 /* parts of Link Status message */
113 #define STATUS_LENGTH    4
114 #define STATUS_OFFSET    0
115
116 #define STATUS_ALIGNMENT              1
117 #define STATUS_PROVING_NORMAL         2
118 #define STATUS_PROVING_EMERGENCY      3
119 #define STATUS_READY                  4
120 #define STATUS_PROCESSOR_OUTAGE       5
121 #define STATUS_PROCESSOR_OUTAGE_ENDED 6
122 #define STATUS_BUSY                   7
123 #define STATUS_BUSY_ENDED             8
124
125 static const value_string m2pa_link_status_values[] = {
126   { STATUS_ALIGNMENT,                   "Alignment" },
127   { STATUS_PROVING_NORMAL,              "Proving Normal" },
128   { STATUS_PROVING_EMERGENCY,           "Proving Emergency" },
129   { STATUS_READY,                       "Ready" },    
130   { STATUS_PROCESSOR_OUTAGE,            "Processor Outage" },
131   { STATUS_PROCESSOR_OUTAGE_ENDED,      "Processor Outage Ended" },
132   { STATUS_BUSY,                        "Busy" },
133   { STATUS_BUSY_ENDED,                  "Busy Ended" },
134   { 0,                                  NULL } };
135
136
137 /* Initialize the protocol and registered fields */
138 static int proto_m2pa = -1;
139 static int hf_m2pa_version = -1;
140 static int hf_m2pa_spare = -1;
141 static int hf_m2pa_type = -1;
142 static int hf_m2pa_class = -1;
143 static int hf_m2pa_length = -1;
144 static int hf_m2pa_status = -1;
145 static int hf_m2pa_li_spare = -1;
146 static int hf_m2pa_li_prio = -1;
147 static int hf_m2pa_proving_data = -1;
148 static int hf_m2pa_unknown_data = -1;
149 /* Initialize the subtree pointers */
150 static gint ett_m2pa = -1;
151 static gint ett_m2pa_li = -1;
152
153 static dissector_handle_t mtp3_handle;
154
155 static void
156 dissect_m2pa_user_data_message(tvbuff_t *message_data_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
157 {
158   proto_item *m2pa_li_item;
159   proto_tree *m2pa_li_tree;
160   tvbuff_t *payload_tvb;
161   guint8 li;
162   guint32 payload_length;
163
164   payload_length = tvb_length(message_data_tvb) - LI_LENGTH;
165
166   if (m2pa_tree) {
167     li = tvb_get_guint8(message_data_tvb, LI_OFFSET);
168     m2pa_li_item = proto_tree_add_text(m2pa_tree, message_data_tvb, LI_OFFSET, LI_LENGTH, "Length Indicator");
169     m2pa_li_tree = proto_item_add_subtree(m2pa_li_item, ett_m2pa_li);
170     proto_tree_add_uint(m2pa_li_tree, hf_m2pa_li_spare, message_data_tvb, LI_OFFSET, LI_LENGTH, li);
171     proto_tree_add_uint(m2pa_li_tree, hf_m2pa_li_prio,  message_data_tvb, LI_OFFSET, LI_LENGTH, li);
172
173     /* Re-adjust length of M2PA item since it will be dissected as MTP3 */
174     proto_item_set_len(m2pa_item, COMMON_HEADER_LENGTH + LI_LENGTH);
175   };
176
177   payload_tvb = tvb_new_subset(message_data_tvb, MTP3_OFFSET, payload_length, payload_length);
178   call_dissector(mtp3_handle, payload_tvb, pinfo, tree);
179 }
180
181 static void
182 dissect_m2pa_link_status_message(tvbuff_t *message_data_tvb, proto_tree *m2pa_tree)
183 {
184   guint32 status;
185
186   if (m2pa_tree) {
187     status = tvb_get_ntohl (message_data_tvb, STATUS_OFFSET);
188     proto_tree_add_uint(m2pa_tree, hf_m2pa_status, message_data_tvb, STATUS_OFFSET, STATUS_LENGTH, status);
189   };
190
191 }
192
193 static void
194 dissect_m2pa_proving_data_message(tvbuff_t *message_data_tvb,  proto_tree *m2pa_tree)
195 {
196   guint32 message_data_length;
197   
198   if (m2pa_tree) {
199     message_data_length = tvb_length(message_data_tvb);
200     proto_tree_add_item(m2pa_tree, hf_m2pa_proving_data, 
201                          message_data_tvb, 0, message_data_length, FALSE);
202   };
203 }
204
205 static void
206 dissect_m2pa_unknown_message(tvbuff_t *message_data_tvb, proto_tree *m2pa_tree)
207 {
208   guint32 message_data_length;
209   
210   if (m2pa_tree) {
211     message_data_length = tvb_length(message_data_tvb);
212     proto_tree_add_item(m2pa_tree, hf_m2pa_unknown_data, 
213                          message_data_tvb, 0, message_data_length, FALSE);
214   };
215 }
216
217 static void
218 dissect_m2pa_common_header(tvbuff_t *common_header_tvb, packet_info *pinfo, proto_tree *m2pa_tree)
219 {
220   guint8  version, spare, class, type;
221   guint32 length;
222
223   version        = tvb_get_guint8(common_header_tvb, VERSION_OFFSET);
224   spare          = tvb_get_guint8(common_header_tvb, SPARE_OFFSET);
225   class          = tvb_get_guint8(common_header_tvb, CLASS_OFFSET);
226   type           = tvb_get_guint8(common_header_tvb, TYPE_OFFSET);
227   length = tvb_get_ntohl(common_header_tvb,  LENGTH_OFFSET);
228   
229   if (check_col(pinfo->cinfo, COL_INFO)) {
230     col_append_str(pinfo->cinfo, COL_INFO, val_to_str(type, m2pa_message_type_values, "Invalid"));
231     col_append_str(pinfo->cinfo, COL_INFO, " ");
232   };
233
234   if (m2pa_tree) {
235     proto_tree_add_uint(m2pa_tree, hf_m2pa_version, common_header_tvb, VERSION_OFFSET, VERSION_LENGTH, version);
236     proto_tree_add_uint(m2pa_tree, hf_m2pa_spare, common_header_tvb, SPARE_OFFSET, SPARE_LENGTH, spare);
237     proto_tree_add_uint(m2pa_tree, hf_m2pa_type, common_header_tvb, TYPE_OFFSET, TYPE_LENGTH, type);
238     proto_tree_add_uint(m2pa_tree, hf_m2pa_class, common_header_tvb, CLASS_OFFSET, CLASS_LENGTH, class);
239     proto_tree_add_uint(m2pa_tree, hf_m2pa_length, common_header_tvb, LENGTH_OFFSET, LENGTH_LENGTH, length);
240   };
241 }
242
243 static void
244 dissect_m2pa_message_data(tvbuff_t *message_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
245 {
246   guint32 message_data_length;
247   guint8 type;
248   tvbuff_t *message_data_tvb;
249
250   message_data_length = tvb_get_ntohl(message_tvb,  LENGTH_OFFSET) - COMMON_HEADER_LENGTH;
251   message_data_tvb    = tvb_new_subset(message_tvb, MESSAGE_DATA_OFFSET, message_data_length, message_data_length);
252   type                = tvb_get_guint8(message_tvb, TYPE_OFFSET);
253
254   switch(type) {
255   case MESSAGE_TYPE_USER_DATA:
256     dissect_m2pa_user_data_message(message_data_tvb, pinfo, m2pa_item, m2pa_tree, tree);
257     break;
258
259   case MESSAGE_TYPE_LINK_STATUS:
260     dissect_m2pa_link_status_message(message_data_tvb, m2pa_tree);
261     break;
262
263   case MESSAGE_TYPE_PROVING_DATA:
264     dissect_m2pa_proving_data_message(message_data_tvb, m2pa_tree);
265     break;
266     
267   default:
268     dissect_m2pa_unknown_message(message_data_tvb, m2pa_tree);
269   }
270
271 }
272 static void
273 dissect_m2pa_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
274 {
275   tvbuff_t *common_header_tvb;
276
277   common_header_tvb = tvb_new_subset(message_tvb, COMMON_HEADER_OFFSET, COMMON_HEADER_LENGTH, COMMON_HEADER_LENGTH);
278   dissect_m2pa_common_header(common_header_tvb, pinfo, m2pa_tree);
279   dissect_m2pa_message_data(message_tvb, pinfo, m2pa_item, m2pa_tree, tree);
280
281 }
282
283 static void
284 dissect_m2pa(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
285 {
286   proto_item *m2pa_item;
287   proto_tree *m2pa_tree;
288
289   /* make entry in the Protocol column on summary display */
290   if (check_col(pinfo->cinfo, COL_PROTOCOL))
291     col_set_str(pinfo->cinfo, COL_PROTOCOL, "M2PA");
292
293   /* In the interest of speed, if "tree" is NULL, don't do any work not
294      necessary to generate protocol tree items. */
295   if (tree) {
296     /* create the m2pa protocol tree */
297     m2pa_item = proto_tree_add_item(tree, proto_m2pa, tvb, 0, -1, FALSE);
298     m2pa_tree = proto_item_add_subtree(m2pa_item, ett_m2pa);
299   } else {
300     m2pa_item = NULL;
301     m2pa_tree = NULL;
302   };
303
304   /* dissect the message */
305   dissect_m2pa_message(tvb, pinfo, m2pa_item, m2pa_tree, tree);
306 }
307
308 /* Register the protocol with Ethereal */
309 void
310 proto_register_m2pa(void)
311 {
312
313   /* Setup list of header fields */
314   static hf_register_info hf[] = {
315     { &hf_m2pa_version,
316       { "Version", "m2pa.version",
317               FT_UINT8, BASE_DEC, VALS(m2pa_protocol_version_values), 0x0,
318               "", HFILL}
319     },
320     { &hf_m2pa_spare,
321       { "Spare", "m2pa.spare",
322               FT_UINT8, BASE_HEX, NULL, 0x0,
323               "", HFILL}
324     },
325     { &hf_m2pa_type,
326       { "Message Type", "m2pa.type",
327               FT_UINT8, BASE_DEC, VALS(m2pa_message_type_values), 0x0,
328               "", HFILL}
329     },
330     { &hf_m2pa_class,
331       { "Message Class", "m2pa.class",
332               FT_UINT8, BASE_DEC, VALS(m2pa_message_class_values), 0x0,
333               "", HFILL}
334     },
335     { &hf_m2pa_length,
336       { "Message length", "m2pa.length",
337               FT_UINT32, BASE_DEC, NULL, 0x0,
338               "", HFILL}
339     },
340     { &hf_m2pa_li_spare,
341       { "Spare", "m2pa.li_spare",
342               FT_UINT8, BASE_HEX, NULL, LI_SPARE_MASK,
343         "", HFILL}
344     },
345     { &hf_m2pa_li_prio,
346       { "Priority", "m2pa.li_priority",
347               FT_UINT8, BASE_HEX, NULL, LI_PRIORITY_MASK,
348               "", HFILL}
349     },
350     { &hf_m2pa_status,
351       { "Link Status Status", "m2pa.status",
352               FT_UINT32, BASE_DEC, VALS(m2pa_link_status_values), 0x0,
353               "", HFILL}
354     },
355     { &hf_m2pa_proving_data,
356       { "Proving Data", "m2pa.proving_data",
357                FT_BYTES, BASE_NONE, NULL, 0x0,          
358                "", HFILL }
359     },
360     { &hf_m2pa_unknown_data,
361       { "Unknown Data", "m2pa.unknown_data",
362                FT_BYTES, BASE_NONE, NULL, 0x0,          
363                "", HFILL }
364     }
365   };
366
367   /* Setup protocol subtree array */
368   static gint *ett[] = {
369     &ett_m2pa,
370     &ett_m2pa_li
371   };
372
373   /* Register the protocol name and description */
374   proto_m2pa = proto_register_protocol("MTP2 Peer Adaptation Layer", "M2PA", "m2pa");
375
376   /* Required function calls to register the header fields and subtrees used */
377   proto_register_field_array(proto_m2pa, hf, array_length(hf));
378   proto_register_subtree_array(ett, array_length(ett));
379
380 };
381
382 void
383 proto_reg_handoff_m2pa(void)
384 {
385   dissector_handle_t m2pa_handle;
386
387   /*
388    *  Get a handle for the MTP3 dissector.
389    */
390   mtp3_handle = find_dissector("mtp3");
391
392   m2pa_handle = create_dissector_handle(dissect_m2pa, proto_m2pa);
393   dissector_add("sctp.ppi",  M2PA_PAYLOAD_PROTOCOL_ID, m2pa_handle);
394   dissector_add("sctp.port", SCTP_PORT_M2PA, m2pa_handle);
395 }