Removed check_col() and the occasional tree.
[metze/wireshark/wip.git] / epan / dissectors / packet-lmi.c
1 /* packet-lmi.c
2  * Routines for Frame Relay Local Management Interface (LMI) disassembly
3  * Copyright 2001, Jeffrey C. Foster <jfoste@woodward.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  * ToDo:
26  *
27  * References:
28  *
29  * http://www.techfest.com/networking/wan/frrel.htm
30  * http://www.frforum.com/5000/Approved/FRF.1.2/frf1_2.pdf
31  * http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/frame.htm#xtocid18
32  * http://www.net.aapt.com.au/techref/lmimess.htm
33  * http://www.raleigh.ibm.com:80/cgi-bin/bookmgr/BOOKS/EZ305800/1.2.4.4
34  */
35
36
37 #include "config.h"
38
39 #include <glib.h>
40 #include <epan/packet.h>
41 #include <epan/nlpid.h>
42
43 static int proto_lmi = -1;
44 static int hf_lmi_call_ref = -1;
45 static int hf_lmi_msg_type = -1;
46 static int hf_lmi_inf_ele = -1;
47 static int hf_lmi_inf_len = -1;
48
49 static int hf_lmi_rcd_type = -1;
50 static int hf_lmi_send_seq = -1;
51 static int hf_lmi_recv_seq = -1;
52 static int hf_lmi_dlci_high = -1;
53 static int hf_lmi_dlci_low = -1;
54 static int hf_lmi_new = -1;
55 static int hf_lmi_act = -1;
56
57 static gint ett_lmi = -1;
58 static gint ett_lmi_ele = -1;
59
60 #ifdef _OLD_
61 /*
62  * Bits in the address field.
63  */
64 #define LMI_CMD         0xf000  /* LMI Command */
65 #define LMI_SEQ         0x0fff  /* LMI Sequence number */
66
67 #endif
68
69 static const value_string msg_type_str[] = {
70         {0x75, "Status Enquiry"},
71         {0x7D, "Status"},
72         { 0,       NULL }
73         };
74
75 static const value_string element_type_str[] = {
76
77 /*** These are the ANSI values ***/
78         {0x01, "Report"},
79         {0x03, "Keep Alive"},
80         {0x07, "PVC Status"},
81
82 /*** These are the ITU values ***/
83         {0x51, "Report"},
84         {0x53, "Keep Alive"},
85         {0x07, "PVC Status"},
86
87         { 0,       NULL }
88         };
89
90 static const value_string record_type_str[] = {
91         {0x00, "Full Status"},
92         {0x01, "Link Integrity Verification Only"},
93         {0x02, "Single PVC"},
94         { 0,       NULL }
95         };
96
97 static const value_string pvc_status_new_str[] = {
98         {0x00, "PVC already present"},
99         {0x01, "PVC is new"},
100         { 0,       NULL }
101         };
102
103 static const value_string pvc_status_act_str[] = {
104         {0x00, "PVC is Inactive"},
105         {0x01, "PVC is Active"},
106         { 0,       NULL }
107         };
108
109 static void
110 dissect_lmi_report_type(tvbuff_t *tvb, int offset, proto_tree *tree)
111 {
112         proto_tree_add_uint(tree, hf_lmi_rcd_type, tvb, offset, 1, tvb_get_guint8( tvb, offset));
113 }
114
115 static void
116 dissect_lmi_link_int(tvbuff_t *tvb, int offset, proto_tree *tree)
117 {
118         proto_tree_add_uint(tree, hf_lmi_send_seq, tvb, offset, 1, tvb_get_guint8( tvb, offset));
119         ++offset;
120         proto_tree_add_uint(tree, hf_lmi_recv_seq, tvb, offset, 1, tvb_get_guint8( tvb, offset));
121
122 }
123
124 static void
125 dissect_lmi_pvc_status(tvbuff_t *tvb, int offset, proto_tree *tree)
126 {
127         proto_tree_add_uint(tree, hf_lmi_dlci_high, tvb, offset, 1, tvb_get_guint8( tvb, offset));
128         ++offset;
129         proto_tree_add_uint(tree, hf_lmi_dlci_low, tvb, offset, 1, tvb_get_guint8( tvb, offset));
130         ++offset;
131         proto_tree_add_uint(tree, hf_lmi_new, tvb, offset, 1, tvb_get_guint8( tvb, offset));
132         proto_tree_add_uint(tree, hf_lmi_act, tvb, offset, 1, tvb_get_guint8( tvb, offset));
133 }
134
135 static void
136 dissect_lmi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
137 {
138         proto_tree      *lmi_tree = NULL, *lmi_subtree;
139         proto_item      *ti;
140         int             offset = 2, len;
141         guint8          msg_type;
142         guint8          ele_id;
143
144         col_set_str(pinfo->cinfo, COL_PROTOCOL, "LMI");
145         col_clear(pinfo->cinfo, COL_INFO);
146
147         if (tree) {
148                 ti = proto_tree_add_item(tree, proto_lmi, tvb, 0, 3, ENC_NA);
149                 lmi_tree = proto_item_add_subtree(ti, ett_lmi_ele);
150
151                 proto_tree_add_item(lmi_tree, hf_lmi_call_ref, tvb, 0, 1, ENC_BIG_ENDIAN);
152         }
153         msg_type = tvb_get_guint8( tvb, 1);
154         col_add_str(pinfo->cinfo, COL_INFO,
155                     val_to_str(msg_type, msg_type_str, "Unknown message type (0x%02x)"));
156
157         if (tree) {
158                 proto_tree_add_uint(lmi_tree, hf_lmi_msg_type, tvb, 1, 1, msg_type);
159
160         /* Display the LMI elements */
161                 while (tvb_reported_length_remaining(tvb, offset) > 0) {
162                         ele_id = tvb_get_guint8( tvb, offset);
163                         len =  tvb_get_guint8( tvb, offset + 1);
164
165                         ti = proto_tree_add_text(lmi_tree, tvb, offset, len + 2,
166                                 "Information Element: %s",
167                                 val_to_str(ele_id, element_type_str, "Unknown (%u)"));
168
169                         lmi_subtree = proto_item_add_subtree(ti, ett_lmi_ele);
170
171                         proto_tree_add_uint(lmi_subtree, hf_lmi_inf_ele, tvb, offset, 1,
172                                 ele_id);
173                         ++offset;
174                         proto_tree_add_uint(lmi_subtree, hf_lmi_inf_len, tvb, offset, 1, len);
175                         ++offset;
176                         if (( ele_id == 1) || (ele_id == 51))
177                                 dissect_lmi_report_type( tvb, offset, lmi_subtree);
178                         else if (( ele_id == 3) || (ele_id == 53))
179                                 dissect_lmi_link_int( tvb, offset, lmi_subtree);
180                         else if (( ele_id == 7) || (ele_id == 57))
181                                 dissect_lmi_pvc_status( tvb, offset, lmi_subtree);
182                         offset += len;
183                 }
184         }
185         else {
186                 lmi_tree = NULL;
187         }
188 }
189
190
191 void
192 proto_register_lmi(void)
193 {
194     static hf_register_info hf[] = {
195         { &hf_lmi_call_ref,
196           { "Call reference", "lmi.cmd", FT_UINT8, BASE_HEX, NULL, 0,
197                 NULL, HFILL }},
198
199         { &hf_lmi_msg_type,
200           { "Message Type", "lmi.msg_type", FT_UINT8, BASE_HEX, VALS(msg_type_str), 0,
201                 NULL, HFILL }},
202
203         { &hf_lmi_inf_ele,
204           { "Type", "lmi.inf_ele_type", FT_UINT8, BASE_DEC, VALS(element_type_str), 0,
205                 "Information Element Type", HFILL }},
206         { &hf_lmi_inf_len,
207           { "Length", "lmi.inf_ele_len", FT_UINT8, BASE_DEC, NULL, 0,
208                 "Information Element Length", HFILL }},
209
210         { &hf_lmi_rcd_type,
211           { "Record Type", "lmi.ele_rcd_type", FT_UINT8, BASE_DEC, VALS(record_type_str), 0,
212                 NULL, HFILL }},
213         { &hf_lmi_send_seq,
214           { "Send Seq", "lmi.send_seq", FT_UINT8, BASE_DEC, NULL, 0,
215                 "Send Sequence", HFILL }},
216         { &hf_lmi_recv_seq,
217           { "Recv Seq", "lmi.recv_seq", FT_UINT8, BASE_DEC, NULL, 0,
218                 "Receive Sequence", HFILL }},
219         { &hf_lmi_dlci_high,
220           { "DLCI High", "lmi.dlci_hi", FT_UINT8, BASE_DEC, NULL, 0x3f,
221                 "DLCI High bits", HFILL }},
222         { &hf_lmi_dlci_low,
223           { "DLCI Low", "lmi.dlci_low", FT_UINT8, BASE_DEC, NULL, 0x78,
224                 "DLCI Low bits", HFILL }},
225         { &hf_lmi_new,
226           { "DLCI New", "lmi.dlci_new", FT_UINT8, BASE_DEC, VALS(pvc_status_new_str), 0x08,
227                 "DLCI New Flag", HFILL }},
228         { &hf_lmi_act,
229           { "DLCI Active","lmi.dlci_act", FT_UINT8, BASE_DEC, VALS(pvc_status_act_str), 0x02,
230                 "DLCI Active Flag", HFILL }},
231     };
232     static gint *ett[] = {
233         &ett_lmi,
234         &ett_lmi_ele,
235     };
236     proto_lmi = proto_register_protocol ("Local Management Interface", "LMI", "lmi");
237     proto_register_field_array (proto_lmi, hf, array_length(hf));
238     proto_register_subtree_array(ett, array_length(ett));
239
240 }
241
242 void
243 proto_reg_handoff_lmi(void)
244 {
245         dissector_handle_t lmi_handle;
246
247         lmi_handle = create_dissector_handle(dissect_lmi, proto_lmi);
248         dissector_add_uint("fr.ietf", NLPID_LMI, lmi_handle);
249 }