Additional RAP error code for password changes sent to a BDC, from Devin
[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.29 2002/02/22 08:54:54 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 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <stdio.h>
35 #include <glib.h>
36 #include <string.h>
37 #include <epan/packet.h>
38 #include "xdlc.h"
39
40 /* ISDN/LAPD references:
41  *
42  * http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/isdn.htm
43  * http://www.ece.wpi.edu/courses/ee535/hwk11cd95/agrebe/agrebe.html
44  * http://www.acacia-net.com/Clarinet/Protocol/q9213o84.htm
45  */
46
47 static int proto_lapd = -1;
48 static int hf_lapd_address = -1;
49 static int hf_lapd_sapi = -1;
50 static int hf_lapd_cr = -1;
51 static int hf_lapd_ea1 = -1;
52 static int hf_lapd_tei = -1;
53 static int hf_lapd_ea2 = -1;
54 static int hf_lapd_control = -1;
55
56 static gint ett_lapd = -1;
57 static gint ett_lapd_address = -1;
58 static gint ett_lapd_control = -1;
59
60 static dissector_handle_t q931_handle;
61 static dissector_handle_t data_handle;
62
63 /*
64  * Bits in the address field.
65  */
66 #define LAPD_SAPI       0xfc00  /* Service Access Point Identifier */
67 #define LAPD_SAPI_SHIFT 10
68 #define LAPD_CR         0x0200  /* Command/Response bit */
69 #define LAPD_EA1        0x0100  /* First Address Extension bit */
70 #define LAPD_TEI        0x00fe  /* Terminal Endpoint Identifier */
71 #define LAPD_EA2        0x0001  /* Second Address Extension bit */
72
73 #define LAPD_SAPI_Q931          0       /* Q.931 call control procedure */
74 #define LAPD_SAPI_PM_Q931       1       /* Packet mode Q.931 call control procedure */
75 #define LAPD_SAPI_X25           16      /* X.25 Level 3 procedures */
76 #define LAPD_SAPI_L2            63      /* Layer 2 management procedures */
77
78 static const value_string lapd_sapi_vals[] = {
79         { LAPD_SAPI_Q931,       "Q.931 Call control procedure" },
80         { LAPD_SAPI_PM_Q931,    "Packet mode Q.931 Call control procedure" },
81         { LAPD_SAPI_X25,        "X.25 Level 3 procedures" },
82         { LAPD_SAPI_L2,         "Layer 2 management procedures" },
83         { 0,                    NULL }
84 };
85
86 static void
87 dissect_lapd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
88 {
89         proto_tree      *lapd_tree, *addr_tree;
90         proto_item      *lapd_ti, *addr_ti;
91         guint16         control;
92         int             lapd_header_len;
93         guint16         address, cr, sapi;
94         gboolean        is_response;
95         tvbuff_t        *next_tvb;
96
97         if (check_col(pinfo->cinfo, COL_PROTOCOL))
98                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPD");
99         if (check_col(pinfo->cinfo, COL_INFO))
100                 col_clear(pinfo->cinfo, COL_INFO);
101
102         address = tvb_get_ntohs(tvb, 0);
103         cr = address & LAPD_CR;
104         sapi = (address & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
105         lapd_header_len = 2;    /* address */
106
107         if (pinfo->pseudo_header->p2p.sent) {
108                 is_response = cr ? TRUE : FALSE;
109                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
110                         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "Network");
111                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
112                         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "User");
113         }
114         else {
115                 is_response = cr ? FALSE : TRUE;
116                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
117                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "User");
118                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
119                     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "Network");
120         }
121
122         if (tree) {
123                 lapd_ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, -1,
124                     FALSE);
125                 lapd_tree = proto_item_add_subtree(lapd_ti, ett_lapd);
126
127                 addr_ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb,
128                     0, 2, address);
129                 addr_tree = proto_item_add_subtree(addr_ti, ett_lapd_address);
130
131                 proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
132                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
133                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
134                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
135                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
136         }
137         else {
138                 lapd_ti = NULL;
139                 lapd_tree = NULL;
140         }
141
142         control = dissect_xdlc_control(tvb, 2, pinfo, lapd_tree, hf_lapd_control,
143             ett_lapd_control, is_response, TRUE);
144         lapd_header_len += XDLC_CONTROL_LEN(control, TRUE);
145
146         if (tree)
147                 proto_item_set_len(lapd_ti, lapd_header_len);
148
149         next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
150         if (XDLC_IS_INFORMATION(control)) {
151                 /* call next protocol */
152                 switch (sapi) {
153
154                 case LAPD_SAPI_Q931:
155                         call_dissector(q931_handle, next_tvb, pinfo, tree);
156                         break;
157
158                 default:
159                         call_dissector(data_handle,next_tvb, pinfo, tree);
160                         break;
161                 }
162         } else
163                 call_dissector(data_handle,next_tvb, pinfo, tree);
164 }
165
166 void
167 proto_register_lapd(void)
168 {
169     static hf_register_info hf[] = {
170         { &hf_lapd_address,
171           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0, 
172                 "Address", HFILL }},
173
174         { &hf_lapd_sapi,
175           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
176                 "Service Access Point Identifier", HFILL }},
177
178         { &hf_lapd_cr,
179           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
180                 "Command/Response bit", HFILL }},
181
182         { &hf_lapd_ea1,
183           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
184                 "First Address Extension bit", HFILL }},
185
186         { &hf_lapd_tei,
187           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
188                 "Terminal Endpoint Identifier", HFILL }},
189
190         { &hf_lapd_ea2,
191           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
192                 "Second Address Extension bit", HFILL }},
193
194         { &hf_lapd_control,
195           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
196                 "Control field", HFILL }},
197     };
198     static gint *ett[] = {
199         &ett_lapd,
200         &ett_lapd_address,
201         &ett_lapd_control,
202     };
203
204     proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
205                                          "LAPD", "lapd");
206     proto_register_field_array (proto_lapd, hf, array_length(hf));
207     proto_register_subtree_array(ett, array_length(ett));
208 }
209
210 void
211 proto_reg_handoff_lapd(void)
212 {
213         dissector_handle_t lapd_handle;
214
215         /*
216          * Get handle for the Q.931 dissector.
217          */
218         q931_handle = find_dissector("q931");
219         data_handle = find_dissector("data");
220
221         lapd_handle = create_dissector_handle(dissect_lapd, proto_lapd);
222         dissector_add("wtap_encap", WTAP_ENCAP_LAPD, lapd_handle);
223 }