Added tap functionality to UDP
[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.34 2003/01/07 01:08:27 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
52 static gint ett_lapd = -1;
53 static gint ett_lapd_address = -1;
54 static gint ett_lapd_control = -1;
55
56 static dissector_handle_t q931_handle;
57 static dissector_handle_t data_handle;
58
59 /*
60  * Bits in the address field.
61  */
62 #define LAPD_SAPI       0xfc00  /* Service Access Point Identifier */
63 #define LAPD_SAPI_SHIFT 10
64 #define LAPD_CR         0x0200  /* Command/Response bit */
65 #define LAPD_EA1        0x0100  /* First Address Extension bit */
66 #define LAPD_TEI        0x00fe  /* Terminal Endpoint Identifier */
67 #define LAPD_EA2        0x0001  /* Second Address Extension bit */
68
69 #define LAPD_SAPI_Q931          0       /* Q.931 call control procedure */
70 #define LAPD_SAPI_PM_Q931       1       /* Packet mode Q.931 call control procedure */
71 #define LAPD_SAPI_X25           16      /* X.25 Level 3 procedures */
72 #define LAPD_SAPI_L2            63      /* Layer 2 management procedures */
73
74 static const value_string lapd_sapi_vals[] = {
75         { LAPD_SAPI_Q931,       "Q.931 Call control procedure" },
76         { LAPD_SAPI_PM_Q931,    "Packet mode Q.931 Call control procedure" },
77         { LAPD_SAPI_X25,        "X.25 Level 3 procedures" },
78         { LAPD_SAPI_L2,         "Layer 2 management procedures" },
79         { 0,                    NULL }
80 };
81
82 static void
83 dissect_lapd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
84 {
85         proto_tree      *lapd_tree, *addr_tree;
86         proto_item      *lapd_ti, *addr_ti;
87         guint16         control;
88         int             lapd_header_len;
89         guint16         address, cr, sapi;
90         gboolean        is_response;
91         tvbuff_t        *next_tvb;
92
93         if (check_col(pinfo->cinfo, COL_PROTOCOL))
94                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPD");
95         if (check_col(pinfo->cinfo, COL_INFO))
96                 col_clear(pinfo->cinfo, COL_INFO);
97
98         address = tvb_get_ntohs(tvb, 0);
99         cr = address & LAPD_CR;
100         sapi = (address & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
101         lapd_header_len = 2;    /* address */
102
103         if (pinfo->p2p_dir == P2P_DIR_SENT) {
104                 is_response = cr ? TRUE : FALSE;
105                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
106                         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "Network");
107                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
108                         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "User");
109         }
110         else {
111                 /* XXX - what if the direction is unknown? */
112                 is_response = cr ? FALSE : TRUE;
113                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
114                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "User");
115                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
116                     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "Network");
117         }
118
119         if (tree) {
120                 lapd_ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, -1,
121                     FALSE);
122                 lapd_tree = proto_item_add_subtree(lapd_ti, ett_lapd);
123
124                 addr_ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb,
125                     0, 2, address);
126                 addr_tree = proto_item_add_subtree(addr_ti, ett_lapd_address);
127
128                 proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
129                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
130                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
131                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
132                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
133         }
134         else {
135                 lapd_ti = NULL;
136                 lapd_tree = NULL;
137         }
138
139         control = dissect_xdlc_control(tvb, 2, pinfo, lapd_tree, hf_lapd_control,
140             ett_lapd_control, is_response, TRUE);
141         lapd_header_len += XDLC_CONTROL_LEN(control, TRUE);
142
143         if (tree)
144                 proto_item_set_len(lapd_ti, lapd_header_len);
145
146         next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
147         if (XDLC_IS_INFORMATION(control)) {
148                 /* call next protocol */
149                 switch (sapi) {
150
151                 case LAPD_SAPI_Q931:
152                         call_dissector(q931_handle, next_tvb, pinfo, tree);
153                         break;
154
155                 default:
156                         /*
157                          * XXX - handle some of the others, such as
158                          * LAPD_SAPI_L2.
159                          */
160                         call_dissector(data_handle, next_tvb, pinfo, tree);
161                         break;
162                 }
163         } else
164                 call_dissector(data_handle,next_tvb, pinfo, tree);
165 }
166
167 void
168 proto_register_lapd(void)
169 {
170     static hf_register_info hf[] = {
171         { &hf_lapd_address,
172           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0,
173                 "Address", HFILL }},
174
175         { &hf_lapd_sapi,
176           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
177                 "Service Access Point Identifier", HFILL }},
178
179         { &hf_lapd_cr,
180           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
181                 "Command/Response bit", HFILL }},
182
183         { &hf_lapd_ea1,
184           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
185                 "First Address Extension bit", HFILL }},
186
187         { &hf_lapd_tei,
188           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
189                 "Terminal Endpoint Identifier", HFILL }},
190
191         { &hf_lapd_ea2,
192           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
193                 "Second Address Extension bit", HFILL }},
194
195         { &hf_lapd_control,
196           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
197                 "Control field", HFILL }},
198     };
199     static gint *ett[] = {
200         &ett_lapd,
201         &ett_lapd_address,
202         &ett_lapd_control,
203     };
204
205     proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
206                                          "LAPD", "lapd");
207     proto_register_field_array (proto_lapd, hf, array_length(hf));
208     proto_register_subtree_array(ett, array_length(ett));
209
210     register_dissector("lapd", dissect_lapd, proto_lapd);
211 }
212
213 void
214 proto_reg_handoff_lapd(void)
215 {
216         /*
217          * Get handle for the Q.931 dissector.
218          */
219         q931_handle = find_dissector("q931");
220         data_handle = find_dissector("data");
221 }