Update a URL.
[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.21 2003/10/25 07:17:26 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   next_tvb = tvb_new_subset(tvb, offset_after_chdlctype, -1, -1);
136
137   /* do lookup with the subdissector table */
138   if (!dissector_try_port(subdissector_table, chdlctype, next_tvb, pinfo, tree)) {
139     if (check_col(pinfo->cinfo, COL_PROTOCOL))
140       col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "0x%04x", chdlctype);
141     call_dissector(data_handle,next_tvb, pinfo, tree);
142   }
143 }
144
145 static gint chdlc_fcs_decode = 0; /* 0 = No FCS, 1 = 16 bit FCS, 2 = 32 bit FCS */
146
147 static void
148 dissect_chdlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
149 {
150   proto_item *ti;
151   proto_tree *fh_tree = NULL;
152   guint8     addr;
153   guint16    proto;
154
155   if (check_col(pinfo->cinfo, COL_PROTOCOL))
156     col_set_str(pinfo->cinfo, COL_PROTOCOL, "CHDLC");
157   if (check_col(pinfo->cinfo, COL_INFO))
158     col_clear(pinfo->cinfo, COL_INFO);
159
160   switch (pinfo->p2p_dir) {
161
162   case P2P_DIR_SENT:
163     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
164       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
165     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
166       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
167     break;
168
169   case P2P_DIR_RECV:
170     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
171       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
172     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
173       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
174     break;
175
176   default:
177     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
178       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A");
179     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
180       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A");
181     break;
182   }
183
184   addr = tvb_get_guint8(tvb, 0);
185   proto = tvb_get_ntohs(tvb, 2);
186
187   if (tree) {
188     ti = proto_tree_add_item(tree, proto_chdlc, tvb, 0, 4, FALSE);
189     fh_tree = proto_item_add_subtree(ti, ett_chdlc);
190
191     proto_tree_add_uint(fh_tree, hf_chdlc_addr, tvb, 0, 1, addr);
192   }
193
194   decode_fcs(tvb, fh_tree, chdlc_fcs_decode, 2);
195
196   chdlctype(proto, tvb, 4, pinfo, tree, fh_tree, hf_chdlc_proto);
197 }
198
199 void
200 proto_register_chdlc(void)
201 {
202   static hf_register_info hf[] = {
203     { &hf_chdlc_addr,
204       { "Address", "chdlc.address", FT_UINT8, BASE_HEX,
205         VALS(chdlc_address_vals), 0x0, "", HFILL }},
206     { &hf_chdlc_proto,
207       { "Protocol", "chdlc.protocol", FT_UINT16, BASE_HEX,
208         VALS(chdlc_vals), 0x0, "", HFILL }},
209   };
210   static gint *ett[] = {
211     &ett_chdlc,
212   };
213
214   module_t *chdlc_module;
215
216   proto_chdlc = proto_register_protocol("Cisco HDLC", "CHDLC", "chdlc");
217   proto_register_field_array(proto_chdlc, hf, array_length(hf));
218   proto_register_subtree_array(ett, array_length(ett));
219
220 /* subdissector code */
221   subdissector_table = register_dissector_table("chdlctype",
222         "Cisco HDLC frame type", FT_UINT16, BASE_HEX);
223
224   register_dissector("chdlc", dissect_chdlc, proto_chdlc);
225
226   /* Register the preferences for the chdlc protocol */
227   chdlc_module = prefs_register_protocol(proto_chdlc, NULL);
228
229   prefs_register_enum_preference(chdlc_module,
230         "fcs_type",
231         "CHDLC Frame Checksum Type",
232         "The type of CHDLC frame checksum (none, 16-bit, 32-bit)",
233         &chdlc_fcs_decode,
234         fcs_options, FALSE);
235
236 }
237
238 void
239 proto_reg_handoff_chdlc(void)
240 {
241   dissector_handle_t chdlc_handle;
242
243   data_handle = find_dissector("data");
244   chdlc_handle = find_dissector("chdlc");
245   dissector_add("wtap_encap", WTAP_ENCAP_CHDLC, chdlc_handle);
246   dissector_add("wtap_encap", WTAP_ENCAP_CHDLC_WITH_PHDR, chdlc_handle);
247 }
248
249 #define SLARP_REQUEST   0
250 #define SLARP_REPLY     1
251 #define SLARP_LINECHECK 2
252
253 static const value_string slarp_ptype_vals[] = {
254         {SLARP_REQUEST,   "Request"},
255         {SLARP_REPLY,     "Reply"},
256         {SLARP_LINECHECK, "Line keepalive"},
257         {0,               NULL}
258 };
259
260 static void
261 dissect_slarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
262 {
263   proto_item *ti;
264   proto_tree *slarp_tree = NULL;
265   guint32 code;
266   guint32 address;
267   guint32 mysequence;
268   guint32 yoursequence;
269
270   if (check_col(pinfo->cinfo, COL_PROTOCOL))
271     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SLARP");
272   if (check_col(pinfo->cinfo, COL_INFO))
273     col_clear(pinfo->cinfo, COL_INFO);
274
275   code = tvb_get_ntohl(tvb, 0);
276
277   if (tree) {
278     ti = proto_tree_add_item(tree, proto_slarp, tvb, 0, 14, FALSE);
279     slarp_tree = proto_item_add_subtree(ti, ett_slarp);
280   }
281
282   switch (code) {
283
284   case SLARP_REQUEST:
285   case SLARP_REPLY:
286     if (check_col(pinfo->cinfo, COL_INFO)) {
287       tvb_memcpy(tvb, (guint8 *)&address, 4, 4);
288       col_add_fstr(pinfo->cinfo, COL_INFO, "%s, from %s, mask %s",
289         match_strval(code, slarp_ptype_vals),
290         get_hostname(address),
291         ip_to_str(tvb_get_ptr(tvb, 8, 4)));
292     }
293     if (tree) {
294       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
295       proto_tree_add_item(slarp_tree, hf_slarp_address, tvb, 4, 4, FALSE);
296       proto_tree_add_text(slarp_tree, tvb, 8, 4,
297                           "Netmask: %s", ip_to_str(tvb_get_ptr(tvb, 8, 4)));
298     }
299     break;
300
301   case SLARP_LINECHECK:
302     mysequence = tvb_get_ntohl(tvb, 4);
303     yoursequence = tvb_get_ntohl(tvb, 8);
304     if (check_col(pinfo->cinfo, COL_INFO)) {
305       col_add_fstr(pinfo->cinfo, COL_INFO,
306         "%s, outgoing sequence %u, returned sequence %u",
307         match_strval(code, slarp_ptype_vals),
308         mysequence, yoursequence);
309     }
310     if (tree) {
311       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
312       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 4, 4,
313                           mysequence);
314       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 8, 4,
315                           yoursequence);
316     }
317     break;
318
319   default:
320     if (check_col(pinfo->cinfo, COL_INFO))
321       col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown packet type 0x%08X", code);
322     if (tree) {
323       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
324       call_dissector(data_handle, tvb_new_subset(tvb, 4, -1, -1), pinfo,
325                      slarp_tree);
326     }
327     break;
328   }
329 }
330
331 void
332 proto_register_slarp(void)
333 {
334   static hf_register_info hf[] = {
335     { &hf_slarp_ptype,
336       { "Packet type", "slarp.ptype", FT_UINT32, BASE_DEC,
337         VALS(slarp_ptype_vals), 0x0, "", HFILL }},
338     { &hf_slarp_address,
339       { "Address", "slarp.address", FT_IPv4, BASE_NONE,
340         NULL, 0x0, "", HFILL }},
341     /* XXX - need an FT_ for netmasks, which is like FT_IPV4 but doesn't
342        get translated to a host name. */
343     { &hf_slarp_mysequence,
344       { "Outgoing sequence number", "slarp.mysequence", FT_UINT32, BASE_DEC,
345         NULL, 0x0, "", HFILL }},
346     { &hf_slarp_yoursequence,
347       { "Returned sequence number", "slarp.yoursequence", FT_UINT32, BASE_DEC,
348         NULL, 0x0, "", HFILL }},
349   };
350   static gint *ett[] = {
351     &ett_chdlc,
352     &ett_slarp,
353   };
354
355   proto_slarp = proto_register_protocol("Cisco SLARP", "SLARP", "slarp");
356   proto_register_field_array(proto_slarp, hf, array_length(hf));
357   proto_register_subtree_array(ett, array_length(ett));
358 }
359
360 void
361 proto_reg_handoff_slarp(void)
362 {
363   dissector_handle_t slarp_handle;
364
365   slarp_handle = create_dissector_handle(dissect_slarp, proto_slarp);
366   dissector_add("chdlctype", CISCO_SLARP, slarp_handle);
367 }