Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/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  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * History:
24  * ---------------------------------
25  * 02.03.2009 Initial implementation, supports:
26  * - HDLC mode (rfc4618 5.1), no CW, payload is PPP (PPP in HDLC-like Framing (rfc1662)).
27  * - FR port mode (rfc4618 5.2), no CW.
28  *
29  * [informative: Not supported yet:
30  * - All kinds of HDLC PW with CW.
31  * - PPP mode (rfc4618 5.3).
32  * - For HDLC mode, decoding payloads other than PPP.]
33  */
34
35 #include "config.h"
36
37 #include <glib.h>
38
39 #include <epan/packet.h>
40
41 #include "packet-mpls.h"
42
43 void proto_register_pw_hdlc(void);
44 void proto_reg_handoff_pw_hdlc(void);
45
46 static dissector_handle_t ppp_handle;
47 static dissector_handle_t fr_handle;
48
49 static gint proto_pw_hdlc_nocw_fr = -1;
50 static gint proto_pw_hdlc_nocw_hdlc_ppp = -1;
51
52 static gint ett_pw_hdlc = -1;
53
54 /* static int hf_pw_hdlc = -1; */
55 static int hf_pw_hdlc_address_field = -1;
56 static int hf_pw_hdlc_address = -1;
57 static int hf_pw_hdlc_cr_bit = -1;
58 static int hf_pw_hdlc_control_field = -1;
59 static int hf_pw_hdlc_pf_bit = -1;
60 static int hf_pw_hdlc_modifier = -1;
61
62 static const value_string pw_hdlc_modifier_vals[] = {
63         {0x00, "UI - Unnumbered information" },
64         {0x08, "UP - Unnumbered poll" },
65         {0x10, "DISC/RD - Disconnect/Request disconnect" },
66         {0x18, "UA - Unnumbered acknowledgment" },
67         {0x20, "SNRM - Set normal response mode" },
68         {0x38, "TEST - Test" },
69         {0x01, "SIM/RIM - Set initialization mode/Request initialization mode" },
70         {0x21, "FRMR - Frame reject" },
71         {0x03, "SARM/DM - Set asynchronous response mode/Disconnect mode" },
72         {0x0B, "SABM - Set asynchronous balanced mode" },
73         {0x13, "SARME - Set asynchronous response extended mode" },
74         {0x1B, "SABME - Set asynchronous balanced extended mode" },
75         {0x23, "RSET - Reset" },
76         {0x2B, "XID - Exchange identification" },
77         {0x33, "SNRME - Set normal response extended mode" },
78         {0, NULL }
79 };
80
81 static void dissect_pw_hdlc_nocw_fr( tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree )
82 {
83         call_dissector( fr_handle, tvb, pinfo, tree );
84 }
85
86
87 static void dissect_pw_hdlc_nocw_hdlc_ppp( tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree )
88 {
89         if (tvb_reported_length_remaining(tvb, 0) < 2)
90         {
91                 proto_tree_add_text(tree, tvb, 0, -1, "Error processing message");
92                 return;
93         }
94
95         if (tree)
96         {
97                 proto_tree  *tr;
98                 proto_item  *item;
99                 proto_item  *item_address;
100                 proto_item  *item_control;
101                 guint8      addr;
102                 guint8      control;
103
104                 addr    = tvb_get_guint8(tvb, 0);
105                 control = tvb_get_guint8(tvb, 1);
106
107                 item = proto_tree_add_item( tree, proto_pw_hdlc_nocw_hdlc_ppp, tvb, 0, 2, ENC_NA );
108
109                 tr = proto_item_add_subtree( item, ett_pw_hdlc );
110
111                 item_address = proto_tree_add_item( tr, hf_pw_hdlc_address_field, tvb, 0, 1, ENC_NA );
112                 item_control = proto_tree_add_item( tr, hf_pw_hdlc_control_field, tvb, 1, 1, ENC_NA );
113
114                 tr = proto_item_add_subtree( item_address, ett_pw_hdlc );
115
116                 if ( 0x3F == (( addr & 0xFC ) >> 2 ))
117                         proto_tree_add_uint_format_value( tr, hf_pw_hdlc_address, tvb, 0, 1, 0xFC, "0x%x (All stations)", 0x3F );
118                 else
119                         proto_tree_add_uint( tr, hf_pw_hdlc_address, tvb, 0, 1, ( addr & 0xFC ) >> 2 );
120
121                 proto_tree_add_uint( tr, hf_pw_hdlc_cr_bit, tvb, 0, 1, ( addr & 2 ) >> 1 );
122
123                 tr = proto_item_add_subtree( item_control, ett_pw_hdlc );
124
125                 if ( control & 1 )
126                 {
127                         if ( control & 2 )
128                         {
129                                 proto_tree_add_text( tr, tvb, 1, 1, "U frame" );
130
131                                 proto_tree_add_uint( tr, hf_pw_hdlc_pf_bit, tvb, 1, 1, ( control & 0x10 ) >> 4 );
132                                 proto_tree_add_uint( tr, hf_pw_hdlc_modifier, tvb, 1, 1, (control & 0xEC) >> 2);
133                         }
134                         else
135                         {
136                                 proto_tree_add_text( tr, tvb, 1, 1, "S frame" );
137                         }
138                 }
139                 else
140                 {
141                         proto_tree_add_text( tr, tvb, 1, 1, "I frame" );
142                 }
143         }
144         call_dissector( ppp_handle, tvb_new_subset_remaining(tvb, 2), pinfo, tree );
145 }
146
147 void proto_register_pw_hdlc(void)
148 {
149         static hf_register_info hf[] = {
150 #if 0
151                 {
152                         &hf_pw_hdlc,
153                         {
154                                 "PW HDLC", "pw_hdlc", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL
155                         }
156                 },
157 #endif
158                 {
159                         &hf_pw_hdlc_address_field,
160                         {
161                                 "Address field", "pw_hdlc.address_field",
162                                 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
163                         }
164                 },
165                 {
166                         &hf_pw_hdlc_address,
167                         {
168                                 "Address", "pw_hdlc.address",
169                                 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
170                         }
171                 },
172                 {
173                         &hf_pw_hdlc_cr_bit,
174                         {
175                                 "C/R bit", "pw_hdlc.cr_bit",
176                                 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
177                         }
178                 },
179                 {
180                         &hf_pw_hdlc_control_field,
181                         {
182                                 "Control field", "pw_hdlc.control_field",
183                                 FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
184                         }
185                 },
186                 {
187                         &hf_pw_hdlc_pf_bit,
188                         {
189                                 "Poll/Final bit", "pw_hdlc.pf_bit",
190                                 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
191                         }
192                 },
193                 {
194                         &hf_pw_hdlc_modifier,
195                         {
196                                 "Modifier", "pw_hdlc.modifier",
197                                 FT_UINT8, BASE_HEX, VALS(pw_hdlc_modifier_vals), 0x0, NULL, HFILL
198                         }
199                 }
200         };
201
202         static gint *ett[] = {
203                 &ett_pw_hdlc
204         };
205
206         proto_pw_hdlc_nocw_fr = proto_register_protocol("HDLC PW, FR port mode (no CW)", /*not displayed*/
207                                                         "HDLC PW, FR port mode (no CW)",
208                                                         "pw_hdlc_nocw_fr" );
209         proto_pw_hdlc_nocw_hdlc_ppp = proto_register_protocol("HDLC-like framing for PPP",
210                                                               "HDLC PW with PPP payload (no CW)",
211                                                               "pw_hdlc_nocw_hdlc_ppp" );
212
213         proto_register_field_array(proto_pw_hdlc_nocw_fr, hf, array_length(hf));
214         proto_register_subtree_array(ett, array_length(ett));
215
216         register_dissector("pw_hdlc_nocw_fr", dissect_pw_hdlc_nocw_fr, proto_pw_hdlc_nocw_fr );
217         register_dissector("pw_hdlc_nocw_hdlc_ppp", dissect_pw_hdlc_nocw_hdlc_ppp, proto_pw_hdlc_nocw_hdlc_ppp );
218 }
219
220 void proto_reg_handoff_pw_hdlc(void)
221 {
222         dissector_handle_t handle;
223
224         handle = find_dissector("pw_hdlc_nocw_fr");
225         dissector_add_uint( "mpls.label", MPLS_LABEL_INVALID, handle );
226
227         handle = find_dissector("pw_hdlc_nocw_hdlc_ppp");
228         dissector_add_uint( "mpls.label", MPLS_LABEL_INVALID, handle );
229
230         ppp_handle = find_dissector( "ppp" );
231         fr_handle = find_dissector( "fr" );
232 }