bugfix to a bug reported by Ian Schorr:
[obnox/wireshark/wip.git] / packet-chdlc.c
1 /* packet-chdlc.c
2  * Routines for Cisco HDLC packet disassembly
3  *
4  * $Id: packet-chdlc.c,v 1.22 2004/05/02 21:35:42 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30 #include <epan/packet.h>
31 #include "etypes.h"
32 #include "prefs.h"
33 #include "chdlctypes.h"
34 #include <epan/resolv.h>
35 #include "packet-chdlc.h"
36 #include "packet-ppp.h"
37 #include "packet-ip.h"
38
39 /*
40  * See section 4.3.1 of RFC 1547, and
41  *
42  *      http://www.nethelp.no/net/cisco-hdlc.txt
43  */
44
45 static int proto_chdlc = -1;
46 static int hf_chdlc_addr = -1;
47 static int hf_chdlc_proto = -1;
48
49 static gint ett_chdlc = -1;
50
51 static int proto_slarp = -1;
52 static int hf_slarp_ptype = -1;
53 static int hf_slarp_address = -1;
54 static int hf_slarp_mysequence = -1;
55 static int hf_slarp_yoursequence = -1;
56
57 static gint ett_slarp = -1;
58
59 static dissector_handle_t data_handle;
60
61 /*
62  * Protocol types for the Cisco HDLC format.
63  *
64  * As per the above, according to RFC 1547, these are "standard 16 bit
65  * Ethernet protocol type code[s]", but 0x8035 is Reverse ARP, and
66  * that is (at least according to the Linux ISDN code) not the
67  * same as Cisco SLARP.
68  *
69  * In addition, 0x2000 is apparently the Cisco Discovery Protocol, but
70  * on Ethernet those are encapsulated inside SNAP with an OUI of
71  * OUI_CISCO, not OUI_ENCAP_ETHER.
72  *
73  * Perhaps we should set up a protocol table for those protocols
74  * that differ between Ethernet and Cisco HDLC, and have the PPP
75  * code first try that table and, if it finds nothing in that
76  * table, call "ethertype()".  (Unfortunately, that means that -
77  * assuming we had a Cisco SLARP dissector - said dissector were
78  * disabled, SLARP packets would be dissected as Reverse ARP
79  * packets, not as data.)
80  */
81 #define CISCO_SLARP     0x8035  /* Cisco SLARP protocol */
82
83 static dissector_table_t subdissector_table;
84
85 static const value_string chdlc_address_vals[] = {
86         {CHDLC_ADDR_UNICAST,   "Unicast"},
87         {CHDLC_ADDR_MULTICAST, "Multicast"},
88         {0,                    NULL}
89 };
90
91 const value_string chdlc_vals[] = {
92         {0x2000,               "Cisco Discovery Protocol"},
93         {ETHERTYPE_IP,         "IP"},
94         {CISCO_SLARP,          "SLARP"},
95         {ETHERTYPE_DEC_LB,     "DEC LanBridge"},
96         {CHDLCTYPE_BPDU,       "Spanning Tree BPDU"},
97         {ETHERTYPE_ATALK,      "Appletalk"},
98         {ETHERTYPE_AARP,       "AARP"},
99         {ETHERTYPE_IPX,        "Netware IPX/SPX"},
100         {ETHERTYPE_ETHBRIDGE,  "Transparent Ethernet bridging" },
101         {CHDLCTYPE_OSI,        "OSI" },
102         {ETHERTYPE_MPLS,       "MPLS unicast"},
103         {ETHERTYPE_MPLS_MULTI, "MPLS multicast"},
104         {0,                     NULL}
105 };
106
107 void
108 capture_chdlc( const guchar *pd, int offset, int len, packet_counts *ld ) {
109   if (!BYTES_ARE_IN_FRAME(offset, len, 4)) {
110     ld->other++;
111     return;
112   }
113   switch (pntohs(&pd[offset + 2])) {
114     case ETHERTYPE_IP:
115       capture_ip(pd, offset + 4, len, ld);
116       break;
117     default:
118       ld->other++;
119       break;
120   }
121 }
122
123 void
124 chdlctype(guint16 chdlctype, tvbuff_t *tvb, int offset_after_chdlctype,
125           packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
126           int chdlctype_id)
127 {
128   tvbuff_t   *next_tvb;
129
130   if (tree) {
131     proto_tree_add_uint(fh_tree, chdlctype_id, tvb,
132                         offset_after_chdlctype - 2, 2, chdlctype);
133   }
134
135   if (chdlctype == CHDLCTYPE_OSI) {
136     /* There is a Padding Byte for CLNS protocols over Cisco HDLC */
137     proto_tree_add_text(fh_tree, tvb, offset_after_chdlctype, 1, "CLNS Padding: 0x%02x",
138         tvb_get_guint8(tvb, offset_after_chdlctype));
139     next_tvb = tvb_new_subset(tvb, offset_after_chdlctype + 1, -1, -1);
140   } else {
141     next_tvb = tvb_new_subset(tvb, offset_after_chdlctype, -1, -1);
142   }
143
144   /* do lookup with the subdissector table */
145   if (!dissector_try_port(subdissector_table, chdlctype, next_tvb, pinfo, tree)) {
146     if (check_col(pinfo->cinfo, COL_PROTOCOL))
147       col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "0x%04x", chdlctype);
148     call_dissector(data_handle,next_tvb, pinfo, tree);
149   }
150 }
151
152 static gint chdlc_fcs_decode = 0; /* 0 = No FCS, 1 = 16 bit FCS, 2 = 32 bit FCS */
153
154 static void
155 dissect_chdlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
156 {
157   proto_item *ti;
158   proto_tree *fh_tree = NULL;
159   guint8     addr;
160   guint16    proto;
161
162   if (check_col(pinfo->cinfo, COL_PROTOCOL))
163     col_set_str(pinfo->cinfo, COL_PROTOCOL, "CHDLC");
164   if (check_col(pinfo->cinfo, COL_INFO))
165     col_clear(pinfo->cinfo, COL_INFO);
166
167   switch (pinfo->p2p_dir) {
168
169   case P2P_DIR_SENT:
170     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
171       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
172     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
173       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
174     break;
175
176   case P2P_DIR_RECV:
177     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
178       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
179     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
180       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
181     break;
182
183   default:
184     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
185       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A");
186     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
187       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A");
188     break;
189   }
190
191   addr = tvb_get_guint8(tvb, 0);
192   proto = tvb_get_ntohs(tvb, 2);
193
194   if (tree) {
195     ti = proto_tree_add_item(tree, proto_chdlc, tvb, 0, 4, FALSE);
196     fh_tree = proto_item_add_subtree(ti, ett_chdlc);
197
198     proto_tree_add_uint(fh_tree, hf_chdlc_addr, tvb, 0, 1, addr);
199   }
200
201   decode_fcs(tvb, fh_tree, chdlc_fcs_decode, 2);
202
203   chdlctype(proto, tvb, 4, pinfo, tree, fh_tree, hf_chdlc_proto);
204 }
205
206 void
207 proto_register_chdlc(void)
208 {
209   static hf_register_info hf[] = {
210     { &hf_chdlc_addr,
211       { "Address", "chdlc.address", FT_UINT8, BASE_HEX,
212         VALS(chdlc_address_vals), 0x0, "", HFILL }},
213     { &hf_chdlc_proto,
214       { "Protocol", "chdlc.protocol", FT_UINT16, BASE_HEX,
215         VALS(chdlc_vals), 0x0, "", HFILL }},
216   };
217   static gint *ett[] = {
218     &ett_chdlc,
219   };
220
221   module_t *chdlc_module;
222
223   proto_chdlc = proto_register_protocol("Cisco HDLC", "CHDLC", "chdlc");
224   proto_register_field_array(proto_chdlc, hf, array_length(hf));
225   proto_register_subtree_array(ett, array_length(ett));
226
227 /* subdissector code */
228   subdissector_table = register_dissector_table("chdlctype",
229         "Cisco HDLC frame type", FT_UINT16, BASE_HEX);
230
231   register_dissector("chdlc", dissect_chdlc, proto_chdlc);
232
233   /* Register the preferences for the chdlc protocol */
234   chdlc_module = prefs_register_protocol(proto_chdlc, NULL);
235
236   prefs_register_enum_preference(chdlc_module,
237         "fcs_type",
238         "CHDLC Frame Checksum Type",
239         "The type of CHDLC frame checksum (none, 16-bit, 32-bit)",
240         &chdlc_fcs_decode,
241         fcs_options, FALSE);
242
243 }
244
245 void
246 proto_reg_handoff_chdlc(void)
247 {
248   dissector_handle_t chdlc_handle;
249
250   data_handle = find_dissector("data");
251   chdlc_handle = find_dissector("chdlc");
252   dissector_add("wtap_encap", WTAP_ENCAP_CHDLC, chdlc_handle);
253   dissector_add("wtap_encap", WTAP_ENCAP_CHDLC_WITH_PHDR, chdlc_handle);
254 }
255
256 #define SLARP_REQUEST   0
257 #define SLARP_REPLY     1
258 #define SLARP_LINECHECK 2
259
260 static const value_string slarp_ptype_vals[] = {
261         {SLARP_REQUEST,   "Request"},
262         {SLARP_REPLY,     "Reply"},
263         {SLARP_LINECHECK, "Line keepalive"},
264         {0,               NULL}
265 };
266
267 static void
268 dissect_slarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
269 {
270   proto_item *ti;
271   proto_tree *slarp_tree = NULL;
272   guint32 code;
273   guint32 address;
274   guint32 mysequence;
275   guint32 yoursequence;
276
277   if (check_col(pinfo->cinfo, COL_PROTOCOL))
278     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SLARP");
279   if (check_col(pinfo->cinfo, COL_INFO))
280     col_clear(pinfo->cinfo, COL_INFO);
281
282   code = tvb_get_ntohl(tvb, 0);
283
284   if (tree) {
285     ti = proto_tree_add_item(tree, proto_slarp, tvb, 0, 14, FALSE);
286     slarp_tree = proto_item_add_subtree(ti, ett_slarp);
287   }
288
289   switch (code) {
290
291   case SLARP_REQUEST:
292   case SLARP_REPLY:
293     if (check_col(pinfo->cinfo, COL_INFO)) {
294       tvb_memcpy(tvb, (guint8 *)&address, 4, 4);
295       col_add_fstr(pinfo->cinfo, COL_INFO, "%s, from %s, mask %s",
296         match_strval(code, slarp_ptype_vals),
297         get_hostname(address),
298         ip_to_str(tvb_get_ptr(tvb, 8, 4)));
299     }
300     if (tree) {
301       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
302       proto_tree_add_item(slarp_tree, hf_slarp_address, tvb, 4, 4, FALSE);
303       proto_tree_add_text(slarp_tree, tvb, 8, 4,
304                           "Netmask: %s", ip_to_str(tvb_get_ptr(tvb, 8, 4)));
305     }
306     break;
307
308   case SLARP_LINECHECK:
309     mysequence = tvb_get_ntohl(tvb, 4);
310     yoursequence = tvb_get_ntohl(tvb, 8);
311     if (check_col(pinfo->cinfo, COL_INFO)) {
312       col_add_fstr(pinfo->cinfo, COL_INFO,
313         "%s, outgoing sequence %u, returned sequence %u",
314         match_strval(code, slarp_ptype_vals),
315         mysequence, yoursequence);
316     }
317     if (tree) {
318       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
319       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 4, 4,
320                           mysequence);
321       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 8, 4,
322                           yoursequence);
323     }
324     break;
325
326   default:
327     if (check_col(pinfo->cinfo, COL_INFO))
328       col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown packet type 0x%08X", code);
329     if (tree) {
330       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
331       call_dissector(data_handle, tvb_new_subset(tvb, 4, -1, -1), pinfo,
332                      slarp_tree);
333     }
334     break;
335   }
336 }
337
338 void
339 proto_register_slarp(void)
340 {
341   static hf_register_info hf[] = {
342     { &hf_slarp_ptype,
343       { "Packet type", "slarp.ptype", FT_UINT32, BASE_DEC,
344         VALS(slarp_ptype_vals), 0x0, "", HFILL }},
345     { &hf_slarp_address,
346       { "Address", "slarp.address", FT_IPv4, BASE_NONE,
347         NULL, 0x0, "", HFILL }},
348     /* XXX - need an FT_ for netmasks, which is like FT_IPV4 but doesn't
349        get translated to a host name. */
350     { &hf_slarp_mysequence,
351       { "Outgoing sequence number", "slarp.mysequence", FT_UINT32, BASE_DEC,
352         NULL, 0x0, "", HFILL }},
353     { &hf_slarp_yoursequence,
354       { "Returned sequence number", "slarp.yoursequence", FT_UINT32, BASE_DEC,
355         NULL, 0x0, "", HFILL }},
356   };
357   static gint *ett[] = {
358     &ett_chdlc,
359     &ett_slarp,
360   };
361
362   proto_slarp = proto_register_protocol("Cisco SLARP", "SLARP", "slarp");
363   proto_register_field_array(proto_slarp, hf, array_length(hf));
364   proto_register_subtree_array(ett, array_length(ett));
365 }
366
367 void
368 proto_reg_handoff_slarp(void)
369 {
370   dissector_handle_t slarp_handle;
371
372   slarp_handle = create_dissector_handle(dissect_slarp, proto_slarp);
373   dissector_add("chdlctype", CISCO_SLARP, slarp_handle);
374 }