change a whole bunch of ethereal into wireshark
[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  * 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., 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 <epan/xdlc.h>
35
36 #include <epan/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_TEI_SHIFT  1
84 #define LAPD_EA2        0x0001  /* Second Address Extension bit */
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, tei;
128         gboolean        is_response = 0;
129         tvbuff_t        *next_tvb;
130         char            *srcname = "?";
131         char            *dstname = "?";
132
133         if (check_col(pinfo->cinfo, COL_PROTOCOL))
134                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPD");
135         if (check_col(pinfo->cinfo, COL_INFO))
136                 col_clear(pinfo->cinfo, COL_INFO);
137
138         address = tvb_get_ntohs(tvb, 0);
139         cr = address & LAPD_CR;
140         tei = (address & LAPD_TEI) >> LAPD_TEI_SHIFT;
141         sapi = (address & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
142         lapd_header_len = 2;    /* address */
143
144         if (check_col(pinfo->cinfo, COL_TEI))
145                 col_add_fstr(pinfo->cinfo, COL_TEI, "%u", tei);
146
147         if (pinfo->fd->lnk_t == WTAP_ENCAP_LINUX_LAPD) {
148                 /* frame is captured via libpcap */
149                 if (pinfo->pseudo_header->lapd.pkttype == 4 /*PACKET_OUTGOING*/) {
150                         if (pinfo->pseudo_header->lapd.we_network) {
151                                 is_response = cr ? FALSE : TRUE;
152                                 srcname = "Local Network";
153                                 dstname = "Remote User";
154                         } else {
155                                 srcname = "Local User";
156                                 dstname = "Remote Network";
157                         }
158                 }
159                 else if (pinfo->pseudo_header->lapd.pkttype == 3 /*PACKET_OTHERHOST*/) {
160                         // We must be a TE, sniffing what other TE transmit
161
162                         is_response = cr ? TRUE : FALSE;
163                         srcname = "Remote User";
164                         dstname = "Remote Network";
165                 }
166                 else {
167                         // The frame is incoming
168                         if (pinfo->pseudo_header->lapd.we_network) {
169                                 is_response = cr ? TRUE : FALSE;
170                                 srcname = "Remote User";
171                                 dstname = "Local Network";
172                         } else {
173                                 is_response = cr ? FALSE : TRUE;
174                                 srcname = "Remote Network";
175                                 dstname = "Local User";
176                         }
177                 }
178         }
179         else if (pinfo->p2p_dir == P2P_DIR_SENT) {
180                 is_response = cr ? FALSE : TRUE;
181                 srcname = "Network";
182                 dstname = "User";
183         }
184         else if (pinfo->p2p_dir == P2P_DIR_RECV) {
185                 is_response = cr ? TRUE : FALSE;
186                 srcname = "User";
187                 dstname = "Network";
188         }
189
190         if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
191             col_set_str(pinfo->cinfo, COL_RES_DL_SRC, srcname);
192         if(check_col(pinfo->cinfo, COL_RES_DL_DST))
193             col_set_str(pinfo->cinfo, COL_RES_DL_DST, dstname);
194
195         if (tree) {
196                 lapd_ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, -1,
197                     FALSE);
198                 lapd_tree = proto_item_add_subtree(lapd_ti, ett_lapd);
199
200                 addr_ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb,
201                     0, 2, address);
202                 addr_tree = proto_item_add_subtree(addr_ti, ett_lapd_address);
203
204                 proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
205                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
206                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
207                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
208                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
209         }
210         else {
211                 lapd_ti = NULL;
212                 lapd_tree = NULL;
213         }
214
215         control = dissect_xdlc_control(tvb, 2, pinfo, lapd_tree, hf_lapd_control,
216             ett_lapd_control, &lapd_cf_items, &lapd_cf_items_ext, NULL, NULL,
217             is_response, TRUE, FALSE);
218         lapd_header_len += XDLC_CONTROL_LEN(control, TRUE);
219
220         if (tree)
221                 proto_item_set_len(lapd_ti, lapd_header_len);
222
223         next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
224         if (XDLC_IS_INFORMATION(control)) {
225                 /* call next protocol */
226                 if (!dissector_try_port(lapd_sapi_dissector_table, sapi,
227                     next_tvb, pinfo, tree))
228                         call_dissector(data_handle,next_tvb, pinfo, tree);
229         } else
230                 call_dissector(data_handle,next_tvb, pinfo, tree);
231 }
232
233 void
234 proto_register_lapd(void)
235 {
236     static hf_register_info hf[] = {
237         { &hf_lapd_address,
238           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0,
239                 "Address", HFILL }},
240
241         { &hf_lapd_sapi,
242           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
243                 "Service Access Point Identifier", HFILL }},
244
245         { &hf_lapd_cr,
246           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
247                 "Command/Response bit", HFILL }},
248
249         { &hf_lapd_ea1,
250           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
251                 "First Address Extension bit", HFILL }},
252
253         { &hf_lapd_tei,
254           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
255                 "Terminal Endpoint Identifier", HFILL }},
256
257         { &hf_lapd_ea2,
258           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
259                 "Second Address Extension bit", HFILL }},
260
261         { &hf_lapd_control,
262           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
263                 "Control field", HFILL }},
264
265         { &hf_lapd_n_r,
266             { "N(R)", "lapd.control.n_r", FT_UINT16, BASE_DEC,
267                 NULL, XDLC_N_R_EXT_MASK, "", HFILL }},
268
269         { &hf_lapd_n_s,
270             { "N(S)", "lapd.control.n_s", FT_UINT16, BASE_DEC,
271                 NULL, XDLC_N_S_EXT_MASK, "", HFILL }},
272
273         { &hf_lapd_p,
274             { "Poll", "lapd.control.p", FT_BOOLEAN, 8,
275                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
276
277         { &hf_lapd_p_ext,
278             { "Poll", "lapd.control.p", FT_BOOLEAN, 16,
279                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
280
281         { &hf_lapd_f,
282             { "Final", "lapd.control.f", FT_BOOLEAN, 8,
283                 TFS(&flags_set_truth), XDLC_P_F, "", HFILL }},
284
285         { &hf_lapd_f_ext,
286             { "Final", "lapd.control.f", FT_BOOLEAN, 16,
287                 TFS(&flags_set_truth), XDLC_P_F_EXT, "", HFILL }},
288
289         { &hf_lapd_s_ftype,
290             { "Supervisory frame type", "lapd.control.s_ftype", FT_UINT16, BASE_HEX,
291                 VALS(stype_vals), XDLC_S_FTYPE_MASK, "", HFILL }},
292
293         { &hf_lapd_u_modifier_cmd,
294             { "Command", "lapd.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
295                 VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, "", HFILL }},
296
297         { &hf_lapd_u_modifier_resp,
298             { "Response", "lapd.control.u_modifier_resp", FT_UINT8, BASE_HEX,
299                 VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, "", HFILL }},
300
301         { &hf_lapd_ftype_i,
302             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
303                 VALS(ftype_vals), XDLC_I_MASK, "", HFILL }},
304
305         { &hf_lapd_ftype_s_u,
306             { "Frame type", "lapd.control.ftype", FT_UINT8, BASE_HEX,
307                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
308
309         { &hf_lapd_ftype_s_u_ext,
310             { "Frame type", "lapd.control.ftype", FT_UINT16, BASE_HEX,
311                 VALS(ftype_vals), XDLC_S_U_MASK, "", HFILL }},
312     };
313     static gint *ett[] = {
314         &ett_lapd,
315         &ett_lapd_address,
316         &ett_lapd_control,
317     };
318
319     proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
320                                          "LAPD", "lapd");
321     proto_register_field_array (proto_lapd, hf, array_length(hf));
322     proto_register_subtree_array(ett, array_length(ett));
323
324     register_dissector("lapd", dissect_lapd, proto_lapd);
325
326     lapd_sapi_dissector_table = register_dissector_table("lapd.sapi",
327             "LAPD SAPI", FT_UINT16, BASE_DEC);
328 }
329
330 void
331 proto_reg_handoff_lapd(void)
332 {
333         dissector_handle_t lapd_handle;
334
335         data_handle = find_dissector("data");
336         tei_handle = find_dissector("tei");
337
338
339         lapd_handle = create_dissector_handle(dissect_lapd, proto_lapd);
340         dissector_add("wtap_encap", WTAP_ENCAP_LINUX_LAPD, lapd_handle);
341 }