Move dissectors to epan/dissectors directory.
[obnox/wireshark/wip.git] / epan / dissectors / packet-lapd.c
1 /* packet-lapd.c
2  * Routines for LAPD frame disassembly
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <glib.h>
32 #include <string.h>
33 #include <epan/packet.h>
34 #include "xdlc.h"
35
36 #include "lapd_sapi.h"
37
38 /* ISDN/LAPD references:
39  *
40  * http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/isdn.htm
41  * http://www.ece.wpi.edu/courses/ee535/hwk11cd95/agrebe/agrebe.html
42  * http://www.acacia-net.com/Clarinet/Protocol/q9213o84.htm
43  */
44
45 static int proto_lapd = -1;
46 static int hf_lapd_address = -1;
47 static int hf_lapd_sapi = -1;
48 static int hf_lapd_cr = -1;
49 static int hf_lapd_ea1 = -1;
50 static int hf_lapd_tei = -1;
51 static int hf_lapd_ea2 = -1;
52 static int hf_lapd_control = -1;
53 static int hf_lapd_n_r = -1;
54 static int hf_lapd_n_s = -1;
55 static int hf_lapd_p = -1;
56 static int hf_lapd_p_ext = -1;
57 static int hf_lapd_f = -1;
58 static int hf_lapd_f_ext = -1;
59 static int hf_lapd_s_ftype = -1;
60 static int hf_lapd_u_modifier_cmd = -1;
61 static int hf_lapd_u_modifier_resp = -1;
62 static int hf_lapd_ftype_i = -1;
63 static int hf_lapd_ftype_s_u = -1;
64 static int hf_lapd_ftype_s_u_ext = -1;
65
66 static gint ett_lapd = -1;
67 static gint ett_lapd_address = -1;
68 static gint ett_lapd_control = -1;
69
70 static dissector_table_t lapd_sapi_dissector_table;
71
72 static dissector_handle_t data_handle;
73 static dissector_handle_t tei_handle;
74
75 /*
76  * Bits in the address field.
77  */
78 #define LAPD_SAPI       0xfc00  /* Service Access Point Identifier */
79 #define LAPD_SAPI_SHIFT 10
80 #define LAPD_CR         0x0200  /* Command/Response bit */
81 #define LAPD_EA1        0x0100  /* First Address Extension bit */
82 #define LAPD_TEI        0x00fe  /* Terminal Endpoint Identifier */
83 #define LAPD_EA2        0x0001  /* Second Address Extension bit */
84
85 static const value_string lapd_sapi_vals[] = {
86         { LAPD_SAPI_Q931,       "Q.931 Call control procedure" },
87         { LAPD_SAPI_PM_Q931,    "Packet mode Q.931 Call control procedure" },
88         { LAPD_SAPI_X25,        "X.25 Level 3 procedures" },
89         { LAPD_SAPI_L2,         "Layer 2 management procedures" },
90         { 0,                    NULL }
91 };
92
93 /* Used only for U frames */
94 static const xdlc_cf_items lapd_cf_items = {
95         NULL,
96         NULL,
97         &hf_lapd_p,
98         &hf_lapd_f,
99         NULL,
100         &hf_lapd_u_modifier_cmd,
101         &hf_lapd_u_modifier_resp,
102         NULL,
103         &hf_lapd_ftype_s_u
104 };
105
106 /* Used only for I and S frames */
107 static const xdlc_cf_items lapd_cf_items_ext = {
108         &hf_lapd_n_r,
109         &hf_lapd_n_s,
110         &hf_lapd_p_ext,
111         &hf_lapd_f_ext,
112         &hf_lapd_s_ftype,
113         NULL,
114         NULL,
115         &hf_lapd_ftype_i,
116         &hf_lapd_ftype_s_u_ext
117 };
118
119 static void
120 dissect_lapd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
121 {
122         proto_tree      *lapd_tree, *addr_tree;
123         proto_item      *lapd_ti, *addr_ti;
124         guint16         control;
125         int             lapd_header_len;
126         guint16         address, cr, sapi;
127         gboolean        is_response;
128         tvbuff_t        *next_tvb;
129
130         if (check_col(pinfo->cinfo, COL_PROTOCOL))
131                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPD");
132         if (check_col(pinfo->cinfo, COL_INFO))
133                 col_clear(pinfo->cinfo, COL_INFO);
134
135         address = tvb_get_ntohs(tvb, 0);
136         cr = address & LAPD_CR;
137         sapi = (address & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
138         lapd_header_len = 2;    /* address */
139
140         if (pinfo->p2p_dir == P2P_DIR_SENT) {
141                 is_response = cr ? TRUE : FALSE;
142                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
143                         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "Network");
144                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
145                         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "User");
146         }
147         else {
148                 /* XXX - what if the direction is unknown? */
149                 is_response = cr ? FALSE : TRUE;
150                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
151                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "User");
152                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
153                     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "Network");
154         }
155
156         if (tree) {
157                 lapd_ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, -1,
158                     FALSE);
159                 lapd_tree = proto_item_add_subtree(lapd_ti, ett_lapd);
160
161                 addr_ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb,
162                     0, 2, address);
163                 addr_tree = proto_item_add_subtree(addr_ti, ett_lapd_address);
164
165                 proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
166                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
167                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
168                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
169                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
170         }
171         else {
172                 lapd_ti = NULL;
173                 lapd_tree = NULL;
174         }
175
176         control = dissect_xdlc_control(tvb, 2, pinfo, lapd_tree, hf_lapd_control,
177             ett_lapd_control, &lapd_cf_items, &lapd_cf_items_ext, NULL, NULL,
178             is_response, TRUE, FALSE);
179         lapd_header_len += XDLC_CONTROL_LEN(control, TRUE);
180
181         if (tree)
182                 proto_item_set_len(lapd_ti, lapd_header_len);
183
184         next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
185         if (XDLC_IS_INFORMATION(control)) {
186                 /* call next protocol */
187                 if (!dissector_try_port(lapd_sapi_dissector_table, sapi,
188                     next_tvb, pinfo, tree))
189                         call_dissector(data_handle,next_tvb, pinfo, tree);
190         } else
191                 call_dissector(data_handle,next_tvb, pinfo, tree);
192 }
193
194 void
195 proto_register_lapd(void)
196 {
197     static hf_register_info hf[] = {
198         { &hf_lapd_address,
199           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0,
200                 "Address", HFILL }},
201
202         { &hf_lapd_sapi,
203           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
204                 "Service Access Point Identifier", HFILL }},
205
206         { &hf_lapd_cr,
207           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
208                 "Command/Response bit", HFILL }},
209
210         { &hf_lapd_ea1,
211           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
212                 "First Address Extension bit", HFILL }},
213
214         { &hf_lapd_tei,
215           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
216                 "Terminal Endpoint Identifier", HFILL }},
217
218         { &hf_lapd_ea2,
219           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
220                 "Second Address Extension bit", HFILL }},
221
222         { &hf_lapd_control,
223           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
224                 "Control field", HFILL }},
225
226         { &hf_lapd_n_r,
227             { "N(R)", "lapd.control.n_r", FT_UINT16, BASE_DEC,
228                 NULL, XDLC_N_R_EXT_MASK, "", HFILL }},
229
230         { &hf_lapd_n_s,
231             { "N(S)", "lapd.control.n_s", FT_UINT16, BASE_DEC,
232                 NULL, XDLC_N_S_EXT_MASK, "", HFILL }},
233
234         { &hf_lapd_p,
235             { "Poll", "lapd.control.p", FT_BOOLEAN, 8,
236                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
237
238         { &hf_lapd_p_ext,
239             { "Poll", "lapd.control.p", FT_BOOLEAN, 16,
240                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
241
242         { &hf_lapd_f,
243             { "Final", "lapd.control.f", FT_BOOLEAN, 8,
244                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
245
246         { &hf_lapd_f_ext,
247             { "Final", "lapd.control.f", FT_BOOLEAN, 16,
248                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
249
250         { &hf_lapd_s_ftype,
251             { "Supervisory frame type", "lapd.control.s_ftype", FT_UINT16, BASE_HEX,
252                 VALS(stype_vals), XDLC_S_FTYPE_MASK, "", HFILL }},
253
254         { &hf_lapd_u_modifier_cmd,
255             { "Command", "lapd.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
256                 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, "", HFILL }},
257
258         { &hf_lapd_u_modifier_resp,
259             { "Response", "lapd.control.u_modifier_resp", FT_UINT8, BASE_HEX,
260                 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, "", HFILL }},
261
262         { &hf_lapd_ftype_i,
263             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
264                 VALS(ftype_vals), XDLC_I_MASK, "", HFILL }},
265
266         { &hf_lapd_ftype_s_u,
267             { "Frame type", "lapd.control.ftype", FT_UINT8, BASE_HEX,
268                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
269
270         { &hf_lapd_ftype_s_u_ext,
271             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
272                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
273     };
274     static gint *ett[] = {
275         &ett_lapd,
276         &ett_lapd_address,
277         &ett_lapd_control,
278     };
279
280     proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
281                                          "LAPD", "lapd");
282     proto_register_field_array (proto_lapd, hf, array_length(hf));
283     proto_register_subtree_array(ett, array_length(ett));
284
285     register_dissector("lapd", dissect_lapd, proto_lapd);
286
287     lapd_sapi_dissector_table = register_dissector_table("lapd.sapi",
288             "LAPD SAPI", FT_UINT16, BASE_DEC);
289 }
290
291 void
292 proto_reg_handoff_lapd(void)
293 {
294         data_handle = find_dissector("data");
295         tei_handle = find_dissector("tei");
296 }