Support for draft-rosen-vpn-ospf-bgp-mpls and for additional BGP
[obnox/wireshark/wip.git] / packet-lapd.c
1 /* packet-lapd.c
2  * Routines for LAPD frame disassembly
3  * Gilbert Ramirez <gram@xiexie.org>
4  *
5  * $Id: packet-lapd.c,v 1.23 2001/06/18 02:17:48 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <stdio.h>
36 #include <glib.h>
37 #include <string.h>
38 #include "packet.h"
39 #include "xdlc.h"
40
41 /* ISDN/LAPD references:
42  *
43  * http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/isdn.htm
44  * http://www.ece.wpi.edu/courses/ee535/hwk11cd95/agrebe/agrebe.html
45  * http://www.acacia-net.com/Clarinet/Protocol/q9213o84.htm
46  */
47
48 static int proto_lapd = -1;
49 static int hf_lapd_address = -1;
50 static int hf_lapd_sapi = -1;
51 static int hf_lapd_cr = -1;
52 static int hf_lapd_ea1 = -1;
53 static int hf_lapd_tei = -1;
54 static int hf_lapd_ea2 = -1;
55 static int hf_lapd_control = -1;
56
57 static gint ett_lapd = -1;
58 static gint ett_lapd_address = -1;
59 static gint ett_lapd_control = -1;
60
61 static dissector_handle_t q931_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      *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->fd, COL_PROTOCOL))
98                 col_set_str(pinfo->fd, COL_PROTOCOL, "LAPD");
99         if (check_col(pinfo->fd, COL_INFO))
100                 col_clear(pinfo->fd, 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->fd, COL_RES_DL_DST))
110                         col_set_str(pinfo->fd, COL_RES_DL_DST, "Network");
111                 if(check_col(pinfo->fd, COL_RES_DL_SRC))
112                         col_set_str(pinfo->fd, COL_RES_DL_SRC, "User");
113         }
114         else {
115                 is_response = cr ? FALSE : TRUE;
116                 if(check_col(pinfo->fd, COL_RES_DL_DST))
117                     col_set_str(pinfo->fd, COL_RES_DL_DST, "User");
118                 if(check_col(pinfo->fd, COL_RES_DL_SRC))
119                     col_set_str(pinfo->fd, COL_RES_DL_SRC, "Network");
120         }
121
122         if (tree) {
123                 ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, 3, FALSE);
124                 lapd_tree = proto_item_add_subtree(ti, ett_lapd);
125
126                 ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb, 0, 2, address);
127                 addr_tree = proto_item_add_subtree(ti, ett_lapd_address);
128
129                 proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
130                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
131                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
132                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
133                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
134         }
135         else {
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         next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
144         if (XDLC_IS_INFORMATION(control)) {
145                 /* call next protocol */
146                 switch (sapi) {
147
148                 case LAPD_SAPI_Q931:
149                         call_dissector(q931_handle, next_tvb, pinfo, tree);
150                         break;
151
152                 default:
153                         dissect_data(next_tvb, 0, pinfo, tree);
154                         break;
155                 }
156         } else
157                 dissect_data(next_tvb, 0, pinfo, tree);
158 }
159
160 void
161 proto_register_lapd(void)
162 {
163     static hf_register_info hf[] = {
164         { &hf_lapd_address,
165           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0, 
166                 "Address", HFILL }},
167
168         { &hf_lapd_sapi,
169           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
170                 "Service Access Point Identifier", HFILL }},
171
172         { &hf_lapd_cr,
173           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
174                 "Command/Response bit", HFILL }},
175
176         { &hf_lapd_ea1,
177           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
178                 "First Address Extension bit", HFILL }},
179
180         { &hf_lapd_tei,
181           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
182                 "Terminal Endpoint Identifier", HFILL }},
183
184         { &hf_lapd_ea2,
185           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
186                 "Second Address Extension bit", HFILL }},
187
188         { &hf_lapd_control,
189           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
190                 "Control field", HFILL }},
191     };
192     static gint *ett[] = {
193         &ett_lapd,
194         &ett_lapd_address,
195         &ett_lapd_control,
196     };
197
198     proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
199                                          "LAPD", "lapd");
200     proto_register_field_array (proto_lapd, hf, array_length(hf));
201     proto_register_subtree_array(ett, array_length(ett));
202 }
203
204 void
205 proto_reg_handoff_lapd(void)
206 {
207         /*
208          * Get handle for the Q.931 dissector.
209          */
210         q931_handle = find_dissector("q931");
211
212         dissector_add("wtap_encap", WTAP_ENCAP_LAPD, dissect_lapd, proto_lapd);
213 }