#include <string.h> and/or #include <stdio.h> not needed.
[obnox/wireshark/wip.git] / epan / dissectors / packet-pw-hdlc.c
1 /* packet-pw-hdlc.c
2  * Routines for HDLC PW dissection as per RFC4618.
3  * Copyright 2009, Dmitry Trebich, Artem Tamazov <artem.tamazov@tellabs.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
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  * History:
26  * ---------------------------------
27  * 02.03.2009 Initial implementation, supports:
28  * - HDLC mode (rfc4618 5.1), no CW, payload is PPP (PPP in HDLC-like Framing (rfc1662)).
29  * - FR port mode (rfc4618 5.2), no CW.       
30  *
31  * [informative: Not supported yet:
32  * - All kinds of HDLC PW with CW.
33  * - PPP mode (rfc4618 5.3).
34  * - For HDLC mode, decoding payloads other than PPP.]
35  */
36
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #include <stdlib.h>
42 #include <glib.h>
43 #include <epan/packet.h>
44
45 #include "packet-mpls.h"
46
47 static dissector_handle_t ppp_handle;
48 static dissector_handle_t fr_handle;
49
50 static gint proto_pw_hdlc_nocw_fr = -1;
51 static gint proto_pw_hdlc_nocw_hdlc_ppp = -1;
52
53 static gint ett_pw_hdlc = -1;
54
55 static int hf_pw_hdlc = -1;
56 static int hf_pw_hdlc_address_field = -1;
57 static int hf_pw_hdlc_address = -1;
58 static int hf_pw_hdlc_cr_bit = -1;
59 static int hf_pw_hdlc_control_field = -1;
60 static int hf_pw_hdlc_pf_bit = -1;
61
62 static void dissect_pw_hdlc_nocw_fr( tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree )
63 {
64         call_dissector( fr_handle, tvb, pinfo, tree );
65 }
66
67
68 static void dissect_pw_hdlc_nocw_hdlc_ppp( tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree )
69 {
70         if (tvb_reported_length_remaining(tvb, 0) < 2)
71         {
72                 if (tree)
73                 {
74                         proto_tree_add_text(tree, tvb, 0, -1, "Error processing message");
75                 }
76                 return;
77         }
78
79         if (tree)
80         {
81                 proto_tree  *tr;
82                 proto_item  *item;
83                 proto_item  *item_address;
84                 proto_item  *item_control;
85                 guint8      addr;
86                 guint8      control;
87
88                 addr    = tvb_get_guint8(tvb, 0);
89                 control = tvb_get_guint8(tvb, 1);
90
91                 item = proto_tree_add_item( tree, proto_pw_hdlc_nocw_hdlc_ppp, tvb, 0, 2, FALSE );
92
93                 tr = proto_item_add_subtree( item, ett_pw_hdlc );
94
95                 item_address = proto_tree_add_uint( tr, hf_pw_hdlc_address_field, tvb, 0, 1, addr );
96                 item_control = proto_tree_add_uint_format( tr, hf_pw_hdlc_control_field, tvb, 1, 1, control, "Control field: 0x%x", control );
97
98                 tr = proto_item_add_subtree( item_address, ett_pw_hdlc );
99
100                 if ( 0x3F == (( addr & 0xFC ) >> 2 ))
101                         proto_tree_add_uint_format( tr, hf_pw_hdlc_address, tvb, 0, 1, 0xFC, "Address: 0x%x (All stations)", 0x3F );
102                 else
103                         proto_tree_add_uint( tr, hf_pw_hdlc_address, tvb, 0, 1, ( addr & 0xFC ) >> 2 );
104
105                 proto_tree_add_uint( tr, hf_pw_hdlc_cr_bit, tvb, 0, 1, ( addr & 2 ) >> 1 );
106
107                 tr = proto_item_add_subtree( item_control, ett_pw_hdlc );
108
109                 if ( control & 1 )
110                 {
111                         if ( control & 2 )
112                         {
113                                 guint8 modifier2;
114                                 guint8 modifier3;
115
116                                 proto_tree_add_text( tr, tvb, 1, 1, "U frame" );
117
118                                 proto_tree_add_uint( tr, hf_pw_hdlc_pf_bit, tvb, 1, 1, ( control & 0x10 ) >> 4 );
119
120                                 modifier2 = (( control & 0xC ) >> 2 );
121                                 modifier3 = (( control & 0xE0 ) >> 5 );
122
123                                 /**/ if ( modifier2 == 0 && modifier3 == 0 )
124                                         proto_tree_add_text( tr, tvb, 1, 1,
125                                                 "Modifier: UI - Unnumbered information" );
126                                 else if ( modifier2 == 0 && modifier3 == 1 )
127                                         proto_tree_add_text( tr, tvb, 1, 1,
128                                                 "Modifier: UP - Unnumbered poll" );
129                                 else if ( modifier2 == 0 && modifier3 == 2 )
130                                         proto_tree_add_text( tr, tvb, 1, 1,
131                                                 "Modifier: DISC/RD - Disconnect/Request disconnect" );
132                                 else if ( modifier2 == 0 && modifier3 == 3 )
133                                         proto_tree_add_text( tr, tvb, 1, 1,
134                                                 "Modifier: UA - Unnumbered acknowledgment" );
135                                 else if ( modifier2 == 0 && modifier3 == 4 )
136                                         proto_tree_add_text( tr, tvb, 1, 1,
137                                                 "Modifier: SNRM - Set normal response mode" );
138                                 else if ( modifier2 == 0 && modifier3 == 7 )
139                                         proto_tree_add_text( tr, tvb, 1, 1,
140                                                 "Modifier: TEST - Test" );
141                                 else if ( modifier2 == 1 && modifier3 == 0 )
142                                         proto_tree_add_text( tr, tvb, 1, 1,
143                                                 "Modifier: SIM/RIM"
144                                                 " - Set initialization mode/Request initialization mode" );
145                                 else if ( modifier2 == 1 && modifier3 == 4 )
146                                         proto_tree_add_text( tr, tvb, 1, 1,
147                                                 "Modifier: FRMR - Frame reject" );
148                                 else if ( modifier2 == 3 && modifier3 == 0 )
149                                         proto_tree_add_text( tr, tvb, 1, 1,
150                                                 "Modifier: SARM/DM"
151                                                 " - Set asynchronous response mode/Disconnect mode" );
152                                 else if ( modifier2 == 3 && modifier3 == 1 )
153                                         proto_tree_add_text( tr, tvb, 1, 1,
154                                                 "Modifier: SABM - Set asynchronous balanced mode" );
155                                 else if ( modifier2 == 3 && modifier3 == 2 )
156                                         proto_tree_add_text( tr, tvb, 1, 1,
157                                                 "Modifier: SARME - Set asynchronous response extended mode" );
158                                 else if ( modifier2 == 3 && modifier3 == 3 )
159                                         proto_tree_add_text( tr, tvb, 1, 1,
160                                                 "Modifier: SABME - Set asynchronous balanced extended mode" );
161                                 else if ( modifier2 == 3 && modifier3 == 4 )
162                                         proto_tree_add_text( tr, tvb, 1, 1,
163                                                 "Modifier: RSET - Reset" );
164                                 else if ( modifier2 == 3 && modifier3 == 5 )
165                                         proto_tree_add_text( tr, tvb, 1, 1,
166                                                 "Modifier: XID - Exchange identification" );
167                                 else if ( modifier2 == 3 && modifier3 == 6 )
168                                         proto_tree_add_text( tr, tvb, 1, 1,
169                                                 "Modifier: SNRME - Set normal response extended mode" );
170                         }
171                         else
172                         {
173                                 proto_tree_add_text( tr, tvb, 1, 1, "S frame" );
174                         }
175                 }
176                 else
177                 {
178                         proto_tree_add_text( tr, tvb, 1, 1, "I frame" );
179                 }
180         }
181         call_dissector( ppp_handle, tvb_new_subset_remaining(tvb, 2), pinfo, tree );
182 }
183
184 void proto_register_pw_hdlc(void)
185 {
186         static hf_register_info hf[] = {
187                 {
188                         &hf_pw_hdlc,
189                         {
190                                 "PW HDLC", "pw_hdlc", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL
191                         }
192                 },
193                 {
194                         &hf_pw_hdlc_address_field,
195                         {
196                                 "Address field", "pw_hdlc.address_field",
197                                 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
198                         }
199                 },
200                 {
201                         &hf_pw_hdlc_address,
202                         {
203                                 "Address", "pw_hdlc.address",
204                                 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
205                         }
206                 },
207                 {
208                         &hf_pw_hdlc_cr_bit,
209                         {
210                                 "C/R bit", "pw_hdlc.cr_bit",
211                                 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
212                         }
213                 },
214                 {
215                         &hf_pw_hdlc_control_field,
216                         {
217                                 "Control field", "pw_hdlc.control_field",
218                                 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
219                         }
220                 },
221                 {
222                         &hf_pw_hdlc_pf_bit,
223                         {
224                                 "Poll/Final bit", "pw_hdlc.pf_bit",
225                                 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
226                         }
227                 }
228         };
229
230         static gint *ett[] = {
231                 &ett_pw_hdlc
232         };
233
234         proto_pw_hdlc_nocw_fr = proto_register_protocol("HDLC PW, FR port mode (no CW)", /*not displayed*/
235                                                         "HDLC PW, FR port mode (no CW)",
236                                                         "pw_hdlc_nocw_fr" );
237         proto_pw_hdlc_nocw_hdlc_ppp = proto_register_protocol("HDLC-like framing for PPP",
238                                                               "HDLC PW with PPP payload (no CW)",
239                                                               "pw_hdlc_nocw_hdlc_ppp" );
240
241         proto_register_field_array(proto_pw_hdlc_nocw_fr, hf, array_length(hf));
242         proto_register_subtree_array(ett, array_length(ett));
243
244         register_dissector("pw_hdlc_nocw_fr", dissect_pw_hdlc_nocw_fr, proto_pw_hdlc_nocw_fr );
245         register_dissector("pw_hdlc_nocw_hdlc_ppp", dissect_pw_hdlc_nocw_hdlc_ppp, proto_pw_hdlc_nocw_hdlc_ppp );
246 }
247
248 void proto_reg_handoff_pw_hdlc(void)
249 {
250         dissector_handle_t handle;
251
252         handle = find_dissector("pw_hdlc_nocw_fr");
253         dissector_add( "mpls.label", LABEL_INVALID, handle );
254
255         handle = find_dissector("pw_hdlc_nocw_hdlc_ppp");
256         dissector_add( "mpls.label", LABEL_INVALID, handle );
257
258         ppp_handle = find_dissector( "ppp" );
259         fr_handle = find_dissector( "fr" );
260 }