In the hex dump, generate the offset at the beginning of each line in
[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-04.txt
5  *
6  * Copyright 2001, 2002, 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.10 2002/06/20 20:58:07 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
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <epan/packet.h>
38
39 #define SCTP_PORT_M2PA             3565
40 #define M2PA_PAYLOAD_PROTOCOL_ID   5
41
42 #define VERSION_LENGTH         1
43 #define SPARE_LENGTH           1
44 #define CLASS_LENGTH           1
45 #define TYPE_LENGTH            1
46 #define LENGTH_LENGTH          4
47 #define BSN_LENGTH             2
48 #define FSN_LENGTH             2
49
50 #define HEADER_LENGTH          (VERSION_LENGTH + SPARE_LENGTH + \
51                                 CLASS_LENGTH + TYPE_LENGTH + LENGTH_LENGTH + \
52                                 BSN_LENGTH + FSN_LENGTH)
53
54 #define VERSION_OFFSET         0
55 #define SPARE_OFFSET           (VERSION_OFFSET + VERSION_LENGTH)
56 #define CLASS_OFFSET           (SPARE_OFFSET + SPARE_LENGTH)
57 #define TYPE_OFFSET            (CLASS_OFFSET + CLASS_LENGTH)
58 #define LENGTH_OFFSET          (TYPE_OFFSET + TYPE_LENGTH)
59 #define BSN_OFFSET             (LENGTH_OFFSET + LENGTH_LENGTH)
60 #define FSN_OFFSET             (BSN_OFFSET + BSN_LENGTH)
61 #define HEADER_OFFSET          VERSION_OFFSET
62 #define MESSAGE_DATA_OFFSET    (HEADER_OFFSET + HEADER_LENGTH)
63
64 #define PROTOCOL_VERSION_RELEASE_1        1
65 static const value_string m2pa_protocol_version_values[] = {
66   { PROTOCOL_VERSION_RELEASE_1,  "Release 1" },
67   { 0,                           NULL } };
68
69
70 #define MESSAGE_CLASS_M2PA                0xb
71
72 static const value_string m2pa_message_class_values[] = {
73   { MESSAGE_CLASS_M2PA,          "M2PA" },
74   { 0,                           NULL } };
75
76
77 #define MESSAGE_TYPE_USER_DATA            0x1
78 #define MESSAGE_TYPE_LINK_STATUS          0x2
79
80 static const value_string m2pa_message_type_values[] = {
81   { MESSAGE_TYPE_USER_DATA,     "User Data" },
82   { MESSAGE_TYPE_LINK_STATUS,   "Link Status" },
83   { 0,                           NULL } };
84
85
86 /* parts of User Data message */
87 #define LI_OFFSET             0
88 #define LI_LENGTH             1
89 #define MTP3_OFFSET           (LI_OFFSET + LI_LENGTH)
90
91 /* LI is only used for (ITU national) priority in M2PA */
92 #define LI_SPARE_MASK              0x3f
93 #define LI_PRIORITY_MASK           0xc0
94
95
96 /* parts of Link Status message */
97 #define STATUS_LENGTH    4
98 #define STATUS_OFFSET    0
99 #define FILLER_OFFSET    (STATUS_OFFSET + STATUS_LENGTH)
100
101 #define STATUS_ALIGNMENT              1
102 #define STATUS_PROVING_NORMAL         2
103 #define STATUS_PROVING_EMERGENCY      3
104 #define STATUS_READY                  4
105 #define STATUS_PROCESSOR_OUTAGE       5
106 #define STATUS_PROCESSOR_OUTAGE_ENDED 6
107 #define STATUS_BUSY                   7
108 #define STATUS_BUSY_ENDED             8
109 #define STATUS_OUT_OF_SERVICE         9
110 #define STATUS_IN_SERVICE            10
111
112 static const value_string m2pa_link_status_values[] = {
113   { STATUS_ALIGNMENT,                   "Alignment" },
114   { STATUS_PROVING_NORMAL,              "Proving Normal" },
115   { STATUS_PROVING_EMERGENCY,           "Proving Emergency" },
116   { STATUS_READY,                       "Ready" },    
117   { STATUS_PROCESSOR_OUTAGE,            "Processor Outage" },
118   { STATUS_PROCESSOR_OUTAGE_ENDED,      "Processor Outage Ended" },
119   { STATUS_BUSY,                        "Busy" },
120   { STATUS_BUSY_ENDED,                  "Busy Ended" },
121   { STATUS_OUT_OF_SERVICE,              "Out of Service" },
122   { STATUS_IN_SERVICE,                  "In Service" },
123   { 0,                                  NULL } };
124
125
126 /* Initialize the protocol and registered fields */
127 static int proto_m2pa = -1;
128 static int hf_m2pa_version = -1;
129 static int hf_m2pa_spare = -1;
130 static int hf_m2pa_type = -1;
131 static int hf_m2pa_class = -1;
132 static int hf_m2pa_length = -1;
133 static int hf_m2pa_bsn = -1;
134 static int hf_m2pa_fsn = -1;
135 static int hf_m2pa_status = -1;
136 static int hf_m2pa_li_spare = -1;
137 static int hf_m2pa_li_prio = -1;
138 static int hf_m2pa_filler = -1;
139 static int hf_m2pa_unknown_data = -1;
140 /* Initialize the subtree pointers */
141 static gint ett_m2pa = -1;
142 static gint ett_m2pa_li = -1;
143
144 static dissector_handle_t mtp3_handle;
145
146 static void
147 dissect_m2pa_user_data_message(tvbuff_t *message_data_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
148 {
149   proto_item *m2pa_li_item;
150   proto_tree *m2pa_li_tree;
151   tvbuff_t *payload_tvb;
152   guint8 li;
153   guint32 payload_length;
154
155   payload_length = tvb_length(message_data_tvb) - LI_LENGTH;
156
157   if (m2pa_tree) {
158     li = tvb_get_guint8(message_data_tvb, LI_OFFSET);
159     m2pa_li_item = proto_tree_add_text(m2pa_tree, message_data_tvb, LI_OFFSET, LI_LENGTH, "Length Indicator");
160     m2pa_li_tree = proto_item_add_subtree(m2pa_li_item, ett_m2pa_li);
161     proto_tree_add_uint(m2pa_li_tree, hf_m2pa_li_prio,  message_data_tvb, LI_OFFSET, LI_LENGTH, li);
162     proto_tree_add_uint(m2pa_li_tree, hf_m2pa_li_spare, message_data_tvb, LI_OFFSET, LI_LENGTH, li);
163
164     /* Re-adjust length of M2PA item since it will be dissected as MTP3 */
165     proto_item_set_len(m2pa_item, HEADER_LENGTH + LI_LENGTH);
166   };
167
168   payload_tvb = tvb_new_subset(message_data_tvb, MTP3_OFFSET, payload_length, payload_length);
169   call_dissector(mtp3_handle, payload_tvb, pinfo, tree);
170 }
171
172 static void
173 dissect_m2pa_link_status_message(tvbuff_t *message_data_tvb, proto_tree *m2pa_tree)
174 {
175   guint32 status;
176   guint16 filler_length;
177   
178   if (m2pa_tree) {
179     status        = tvb_get_ntohl (message_data_tvb, STATUS_OFFSET);
180     filler_length = tvb_length(message_data_tvb) - STATUS_LENGTH;
181     proto_tree_add_uint(m2pa_tree, hf_m2pa_status, message_data_tvb, STATUS_OFFSET, STATUS_LENGTH, status);
182     if (filler_length > 0)
183       proto_tree_add_bytes(m2pa_tree, hf_m2pa_filler, message_data_tvb, FILLER_OFFSET, filler_length,
184                            tvb_get_ptr(message_data_tvb, FILLER_OFFSET, filler_length));
185   };
186
187 }
188
189 static void
190 dissect_m2pa_unknown_message(tvbuff_t *message_data_tvb, proto_tree *m2pa_tree)
191 {
192   guint32 message_data_length;
193   
194   if (m2pa_tree) {
195     message_data_length = tvb_length(message_data_tvb);
196     proto_tree_add_bytes(m2pa_tree, hf_m2pa_unknown_data, message_data_tvb, 0, message_data_length,
197                          tvb_get_ptr(message_data_tvb, 0, message_data_length));
198   }
199 }
200
201 static void
202 dissect_m2pa_header(tvbuff_t *header_tvb, packet_info *pinfo, proto_tree *m2pa_tree)
203 {
204   guint8  version, spare, class, type;
205   guint16 bsn, fsn;
206   guint32 length;
207
208   version        = tvb_get_guint8(header_tvb, VERSION_OFFSET);
209   spare          = tvb_get_guint8(header_tvb, SPARE_OFFSET);
210   class          = tvb_get_guint8(header_tvb, CLASS_OFFSET);
211   type           = tvb_get_guint8(header_tvb, TYPE_OFFSET);
212   length         = tvb_get_ntohl(header_tvb,  LENGTH_OFFSET);
213   bsn            = tvb_get_ntohs(header_tvb,  BSN_OFFSET);
214   fsn            = tvb_get_ntohs(header_tvb,  FSN_OFFSET);
215
216   if (check_col(pinfo->cinfo, COL_INFO)) {
217     col_append_str(pinfo->cinfo, COL_INFO, val_to_str(type, m2pa_message_type_values, "Invalid"));
218     col_append_str(pinfo->cinfo, COL_INFO, " ");
219   };
220
221   if (m2pa_tree) {
222     proto_tree_add_uint(m2pa_tree, hf_m2pa_version, header_tvb, VERSION_OFFSET, VERSION_LENGTH, version);
223     proto_tree_add_uint(m2pa_tree, hf_m2pa_spare, header_tvb, SPARE_OFFSET, SPARE_LENGTH, spare);
224     proto_tree_add_uint(m2pa_tree, hf_m2pa_type, header_tvb, TYPE_OFFSET, TYPE_LENGTH, type);
225     proto_tree_add_uint(m2pa_tree, hf_m2pa_class, header_tvb, CLASS_OFFSET, CLASS_LENGTH, class);
226     proto_tree_add_uint(m2pa_tree, hf_m2pa_length, header_tvb, LENGTH_OFFSET, LENGTH_LENGTH, length);
227     proto_tree_add_uint(m2pa_tree, hf_m2pa_bsn, header_tvb, BSN_OFFSET, BSN_LENGTH, bsn);
228     proto_tree_add_uint(m2pa_tree, hf_m2pa_fsn, header_tvb, FSN_OFFSET, FSN_LENGTH, fsn);
229   };
230 }
231
232 static void
233 dissect_m2pa_message_data(tvbuff_t *message_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
234 {
235   guint32 message_data_length;
236   guint8 type;
237   tvbuff_t *message_data_tvb;
238
239   message_data_length = tvb_get_ntohl(message_tvb,  LENGTH_OFFSET) - HEADER_LENGTH;
240   message_data_tvb    = tvb_new_subset(message_tvb, MESSAGE_DATA_OFFSET, message_data_length, message_data_length);
241   type                = tvb_get_guint8(message_tvb, TYPE_OFFSET);
242
243   switch(type) {
244   case MESSAGE_TYPE_USER_DATA:
245     dissect_m2pa_user_data_message(message_data_tvb, pinfo, m2pa_item, m2pa_tree, tree);
246     break;
247
248   case MESSAGE_TYPE_LINK_STATUS:
249     dissect_m2pa_link_status_message(message_data_tvb, m2pa_tree);
250     break;
251
252   default:
253     dissect_m2pa_unknown_message(message_data_tvb, m2pa_tree);
254   }
255
256 }
257 static void
258 dissect_m2pa_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_item *m2pa_item, proto_tree *m2pa_tree, proto_tree *tree)
259 {
260   tvbuff_t *header_tvb;
261
262   header_tvb = tvb_new_subset(message_tvb, HEADER_OFFSET, HEADER_LENGTH, HEADER_LENGTH);
263   dissect_m2pa_header(header_tvb, pinfo, m2pa_tree);
264   dissect_m2pa_message_data(message_tvb, pinfo, m2pa_item, m2pa_tree, tree);
265
266 }
267
268 static void
269 dissect_m2pa(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
270 {
271   proto_item *m2pa_item;
272   proto_tree *m2pa_tree;
273
274   /* make entry in the Protocol column on summary display */
275   if (check_col(pinfo->cinfo, COL_PROTOCOL))
276     col_set_str(pinfo->cinfo, COL_PROTOCOL, "M2PA");
277
278   /* In the interest of speed, if "tree" is NULL, don't do any work not
279      necessary to generate protocol tree items. */
280   if (tree) {
281     /* create the m2pa protocol tree */
282     m2pa_item = proto_tree_add_item(tree, proto_m2pa, tvb, 0, -1, FALSE);
283     m2pa_tree = proto_item_add_subtree(m2pa_item, ett_m2pa);
284   } else {
285     m2pa_item = NULL;
286     m2pa_tree = NULL;
287   };
288
289   /* dissect the message */
290   dissect_m2pa_message(tvb, pinfo, m2pa_item, m2pa_tree, tree);
291 }
292
293 /* Register the protocol with Ethereal */
294 void
295 proto_register_m2pa(void)
296 {
297
298   /* Setup list of header fields */
299   static hf_register_info hf[] = {
300     { &hf_m2pa_version,
301       { "Version", "m2pa.version",
302               FT_UINT8, BASE_DEC, VALS(m2pa_protocol_version_values), 0x0,
303               "", HFILL}
304     },
305     { &hf_m2pa_spare,
306       { "Spare", "m2pa.spare",
307               FT_UINT8, BASE_HEX, NULL, 0x0,
308               "", HFILL}
309     },
310     { &hf_m2pa_type,
311       { "Message Type", "m2pa.type",
312               FT_UINT8, BASE_DEC, VALS(m2pa_message_type_values), 0x0,
313               "", HFILL}
314     },
315     { &hf_m2pa_class,
316       { "Message Class", "m2pa.class",
317               FT_UINT8, BASE_DEC, VALS(m2pa_message_class_values), 0x0,
318               "", HFILL}
319     },
320     { &hf_m2pa_length,
321       { "Message length", "m2pa.length",
322               FT_UINT32, BASE_DEC, NULL, 0x0,
323               "", HFILL}
324     },
325     { &hf_m2pa_bsn,
326       { "BSN", "m2pa.bsn",
327               FT_UINT16, BASE_DEC, NULL, 0x0,
328               "", HFILL}
329     },
330     { &hf_m2pa_fsn,
331       { "FSN", "m2pa.fsn",
332               FT_UINT16, BASE_DEC, NULL, 0x0,
333               "", HFILL}
334     },
335     { &hf_m2pa_li_spare,
336       { "Spare", "m2pa.li_spare",
337               FT_UINT8, BASE_HEX, NULL, LI_SPARE_MASK,
338         "", HFILL}
339     },
340     { &hf_m2pa_li_prio,
341       { "Priority", "m2pa.li_priority",
342               FT_UINT8, BASE_HEX, NULL, LI_PRIORITY_MASK,
343               "", HFILL}
344     },
345     { &hf_m2pa_status,
346       { "Link Status Status", "m2pa.status",
347               FT_UINT32, BASE_DEC, VALS(m2pa_link_status_values), 0x0,
348               "", HFILL}
349     },
350     { &hf_m2pa_filler,
351       { "Filler", "m2pa.filler",
352                FT_BYTES, BASE_NONE, NULL, 0x0,          
353                "", HFILL }
354     },
355     { &hf_m2pa_unknown_data,
356       { "Unknown Data", "m2pa.unknown_data",
357                FT_BYTES, BASE_NONE, NULL, 0x0,          
358                "", HFILL }
359     }
360   };
361
362   /* Setup protocol subtree array */
363   static gint *ett[] = {
364     &ett_m2pa,
365     &ett_m2pa_li
366   };
367
368   /* Register the protocol name and description */
369   proto_m2pa = proto_register_protocol("MTP2 Peer Adaptation Layer", "M2PA", "m2pa");
370
371   /* Required function calls to register the header fields and subtrees used */
372   proto_register_field_array(proto_m2pa, hf, array_length(hf));
373   proto_register_subtree_array(ett, array_length(ett));
374
375 };
376
377 void
378 proto_reg_handoff_m2pa(void)
379 {
380   dissector_handle_t m2pa_handle;
381
382   /*
383    *  Get a handle for the MTP3 dissector.
384    */
385   mtp3_handle = find_dissector("mtp3");
386
387   m2pa_handle = create_dissector_handle(dissect_m2pa, proto_m2pa);
388   dissector_add("sctp.ppi",  M2PA_PAYLOAD_PROTOCOL_ID, m2pa_handle);
389   dissector_add("sctp.port", SCTP_PORT_M2PA, m2pa_handle);
390 }