46faf8edb91f7d64153f1975c6f29b8cca0fc3a9
[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.4 2001/12/10 00:25:30 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_bytes(m2pa_tree, hf_m2pa_proving_data, 
201                          message_data_tvb, 0, message_data_length,
202                          tvb_get_ptr(message_data_tvb, 0, message_data_length));
203   };
204 }
205
206 static void
207 dissect_m2pa_unknown_message(tvbuff_t *message_data_tvb, proto_tree *m2pa_tree)
208 {
209   guint32 message_data_length;
210   
211   if (m2pa_tree) {
212     message_data_length = tvb_length(message_data_tvb);
213     proto_tree_add_bytes(m2pa_tree, hf_m2pa_unknown_data, 
214                          message_data_tvb, 0, message_data_length,
215                          tvb_get_ptr(message_data_tvb, 0, message_data_length));
216   };
217 }
218
219 static void
220 dissect_m2pa_common_header(tvbuff_t *common_header_tvb, packet_info *pinfo, proto_tree *m2pa_tree)
221 {
222   guint8  version, spare, class, type;
223   guint32 length;
224
225   version        = tvb_get_guint8(common_header_tvb, VERSION_OFFSET);
226   spare          = tvb_get_guint8(common_header_tvb, SPARE_OFFSET);
227   class          = tvb_get_guint8(common_header_tvb, CLASS_OFFSET);
228   type           = tvb_get_guint8(common_header_tvb, TYPE_OFFSET);
229   length = tvb_get_ntohl(common_header_tvb,  LENGTH_OFFSET);
230   
231   if (check_col(pinfo->cinfo, COL_INFO)) {
232     col_append_str(pinfo->cinfo, COL_INFO, val_to_str(type, m2pa_message_type_values, "Invalid"));
233     col_append_str(pinfo->cinfo, COL_INFO, " ");
234   };
235
236   if (m2pa_tree) {
237     proto_tree_add_uint(m2pa_tree, hf_m2pa_version, common_header_tvb, VERSION_OFFSET, VERSION_LENGTH, version);
238     proto_tree_add_uint(m2pa_tree, hf_m2pa_spare, common_header_tvb, SPARE_OFFSET, SPARE_LENGTH, spare);
239     proto_tree_add_uint(m2pa_tree, hf_m2pa_type, common_header_tvb, TYPE_OFFSET, TYPE_LENGTH, type);
240     proto_tree_add_uint(m2pa_tree, hf_m2pa_class, common_header_tvb, CLASS_OFFSET, CLASS_LENGTH, class);
241     proto_tree_add_uint(m2pa_tree, hf_m2pa_length, common_header_tvb, LENGTH_OFFSET, LENGTH_LENGTH, length);
242   };
243 }
244
245 static void
246 dissect_m2pa_message_data(tvbuff_t *message_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
247 {
248   guint32 message_data_length;
249   guint8 type;
250   tvbuff_t *message_data_tvb;
251
252   message_data_length = tvb_get_ntohl(message_tvb,  LENGTH_OFFSET) - COMMON_HEADER_LENGTH;
253   message_data_tvb    = tvb_new_subset(message_tvb, MESSAGE_DATA_OFFSET, message_data_length, message_data_length);
254   type                = tvb_get_guint8(message_tvb, TYPE_OFFSET);
255
256   switch(type) {
257   case MESSAGE_TYPE_USER_DATA:
258     dissect_m2pa_user_data_message(message_data_tvb, pinfo, m2pa_item, m2pa_tree, tree);
259     break;
260
261   case MESSAGE_TYPE_LINK_STATUS:
262     dissect_m2pa_link_status_message(message_data_tvb, m2pa_tree);
263     break;
264
265   case MESSAGE_TYPE_PROVING_DATA:
266     dissect_m2pa_proving_data_message(message_data_tvb, m2pa_tree);
267     break;
268     
269   default:
270     dissect_m2pa_unknown_message(message_data_tvb, m2pa_tree);
271   }
272
273 }
274 static void
275 dissect_m2pa_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
276 {
277   tvbuff_t *common_header_tvb;
278
279   common_header_tvb = tvb_new_subset(message_tvb, COMMON_HEADER_OFFSET, COMMON_HEADER_LENGTH, COMMON_HEADER_LENGTH);
280   dissect_m2pa_common_header(common_header_tvb, pinfo, m2pa_tree);
281   dissect_m2pa_message_data(message_tvb, pinfo, m2pa_item, m2pa_tree, tree);
282
283 }
284
285 static void
286 dissect_m2pa(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
287 {
288   proto_item *m2pa_item;
289   proto_tree *m2pa_tree;
290
291   /* make entry in the Protocol column on summary display */
292   if (check_col(pinfo->cinfo, COL_PROTOCOL))
293     col_set_str(pinfo->cinfo, COL_PROTOCOL, "M2PA");
294
295   /* In the interest of speed, if "tree" is NULL, don't do any work not
296      necessary to generate protocol tree items. */
297   if (tree) {
298     /* create the m2pa protocol tree */
299     m2pa_item = proto_tree_add_item(tree, proto_m2pa, tvb, 0, tvb_length(tvb), FALSE);
300     m2pa_tree = proto_item_add_subtree(m2pa_item, ett_m2pa);
301   } else {
302     m2pa_item = NULL;
303     m2pa_tree = NULL;
304   };
305
306   /* dissect the message */
307   dissect_m2pa_message(tvb, pinfo, m2pa_item, m2pa_tree, tree);
308 }
309
310 /* Register the protocol with Ethereal */
311 void
312 proto_register_m2pa(void)
313 {
314
315   /* Setup list of header fields */
316   static hf_register_info hf[] = {
317     { &hf_m2pa_version,
318       { "Version", "m2pa.version",
319               FT_UINT8, BASE_DEC, VALS(m2pa_protocol_version_values), 0x0,
320               "", HFILL}
321     },
322     { &hf_m2pa_spare,
323       { "Spare", "m2pa.spare",
324               FT_UINT8, BASE_HEX, NULL, 0x0,
325               "", HFILL}
326     },
327     { &hf_m2pa_type,
328       { "Message Type", "m2pa.type",
329               FT_UINT8, BASE_DEC, VALS(m2pa_message_type_values), 0x0,
330               "", HFILL}
331     },
332     { &hf_m2pa_class,
333       { "Message Class", "m2pa.class",
334               FT_UINT8, BASE_DEC, VALS(m2pa_message_class_values), 0x0,
335               "", HFILL}
336     },
337     { &hf_m2pa_length,
338       { "Message length", "m2pa.length",
339               FT_UINT32, BASE_DEC, NULL, 0x0,
340               "", HFILL}
341     },
342     { &hf_m2pa_li_spare,
343       { "Spare", "m2pa.li_spare",
344               FT_UINT8, BASE_HEX, NULL, LI_SPARE_MASK,
345         "", HFILL}
346     },
347     { &hf_m2pa_li_prio,
348       { "Priority", "m2pa.li_priority",
349               FT_UINT8, BASE_HEX, NULL, LI_PRIORITY_MASK,
350               "", HFILL}
351     },
352     { &hf_m2pa_status,
353       { "Link Status Status", "m2pa.status",
354               FT_UINT32, BASE_DEC, VALS(m2pa_link_status_values), 0x0,
355               "", HFILL}
356     },
357     { &hf_m2pa_proving_data,
358       { "Proving Data", "m2pa.proving_data",
359                FT_BYTES, BASE_NONE, NULL, 0x0,          
360                "", HFILL }
361     },
362     { &hf_m2pa_unknown_data,
363       { "Unknown Data", "m2pa.unknown_data",
364                FT_BYTES, BASE_NONE, NULL, 0x0,          
365                "", HFILL }
366     }
367   };
368
369   /* Setup protocol subtree array */
370   static gint *ett[] = {
371     &ett_m2pa,
372     &ett_m2pa_li
373   };
374
375   /* Register the protocol name and description */
376   proto_m2pa = proto_register_protocol("MTP2 Peer Adaptation Layer", "M2PA", "m2pa");
377
378   /* Required function calls to register the header fields and subtrees used */
379   proto_register_field_array(proto_m2pa, hf, array_length(hf));
380   proto_register_subtree_array(ett, array_length(ett));
381
382 };
383
384 void
385 proto_reg_handoff_m2pa(void)
386 {
387   dissector_handle_t m2pa_handle;
388
389   /*
390    *  Get a handle for the MTP3 dissector.
391    */
392   mtp3_handle = find_dissector("mtp3");
393
394   m2pa_handle = create_dissector_handle(dissect_m2pa, proto_m2pa);
395   dissector_add("sctp.ppi",  M2PA_PAYLOAD_PROTOCOL_ID, m2pa_handle);
396   dissector_add("sctp.port", SCTP_PORT_M2PA, m2pa_handle);
397 }