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