Bugfix.
[metze/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.16 2002/09/20 09:17:38 sahlberg 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 <epan/resolv.h>
33 #include "packet-chdlc.h"
34 #include "packet-ip.h"
35
36 /*
37  * See section 4.3.1 of RFC 1547, and
38  *
39  *      http://www.nethelp.no/net/cisco-hdlc.txt
40  */
41
42 static int proto_chdlc = -1;
43 static int hf_chdlc_addr = -1;
44 static int hf_chdlc_proto = -1;
45
46 static gint ett_chdlc = -1;
47
48 static int proto_slarp = -1;
49 static int hf_slarp_ptype = -1;
50 static int hf_slarp_address = -1;
51 static int hf_slarp_mysequence = -1;
52 static int hf_slarp_yoursequence = -1;
53
54 static gint ett_slarp = -1;
55
56 static dissector_handle_t data_handle;
57
58 /*
59  * Protocol types for the Cisco HDLC format.
60  *
61  * As per the above, according to RFC 1547, these are "standard 16 bit
62  * Ethernet protocol type code[s]", but 0x8035 is Reverse ARP, and
63  * that is (at least according to the Linux ISDN code) not the
64  * same as Cisco SLARP.
65  *
66  * In addition, 0x2000 is apparently the Cisco Discovery Protocol, but
67  * on Ethernet those are encapsulated inside SNAP with an OUI of
68  * OUI_CISCO, not OUI_ENCAP_ETHER.
69  *
70  * Perhaps we should set up a protocol table for those protocols
71  * that differ between Ethernet and Cisco HDLC, and have the PPP
72  * code first try that table and, if it finds nothing in that
73  * table, call "ethertype()".  (Unfortunately, that means that -
74  * assuming we had a Cisco SLARP dissector - said dissector were
75  * disabled, SLARP packets would be dissected as Reverse ARP
76  * packets, not as data.)
77  */
78 #define CISCO_SLARP     0x8035  /* Cisco SLARP protocol */
79
80 static dissector_table_t subdissector_table;
81
82 static const value_string chdlc_address_vals[] = {
83         {CHDLC_ADDR_UNICAST,   "Unicast"},
84         {CHDLC_ADDR_MULTICAST, "Multicast"},
85         {0,                    NULL}
86 };
87
88 const value_string chdlc_vals[] = {
89         {0x2000,              "Cisco Discovery Protocol"},
90         {ETHERTYPE_IP,        "IP"},
91         {CISCO_SLARP,         "SLARP"},
92         {ETHERTYPE_DEC_LB,    "DEC LanBridge"},
93         {ETHERTYPE_ATALK,     "Appletalk"},
94         {ETHERTYPE_AARP,      "AARP"},
95         {ETHERTYPE_IPX,       "Netware IPX/SPX"},
96         {ETHERTYPE_ETHBRIDGE, "Transparent Ethernet bridging" },
97         {ETHERTYPE_OSI,       "OSI" },
98         {0,                   NULL}
99 };
100
101 void
102 capture_chdlc( const guchar *pd, int offset, int len, packet_counts *ld ) {
103   if (!BYTES_ARE_IN_FRAME(offset, len, 2)) {
104     ld->other++;
105     return;
106   }
107   switch (pntohs(&pd[offset + 2])) {
108     case ETHERTYPE_IP:
109       capture_ip(pd, offset + 4, len, ld);
110       break;
111     default:
112       ld->other++;
113       break;
114   }
115 }
116
117 void
118 chdlctype(guint16 chdlctype, tvbuff_t *tvb, int offset_after_chdlctype,
119           packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
120           int chdlctype_id)
121 {
122   tvbuff_t   *next_tvb;
123
124   if (tree) {
125     proto_tree_add_uint(fh_tree, chdlctype_id, tvb,
126                         offset_after_chdlctype - 2, 2, chdlctype);
127   }
128
129   next_tvb = tvb_new_subset(tvb, offset_after_chdlctype, -1, -1);
130
131   /* do lookup with the subdissector table */
132   if (!dissector_try_port(subdissector_table, chdlctype, next_tvb, pinfo, tree)) {
133     if (check_col(pinfo->cinfo, COL_PROTOCOL))
134       col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "0x%04x", chdlctype);
135     call_dissector(data_handle,next_tvb, pinfo, tree);
136   }
137 }
138
139 static void
140 dissect_chdlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
141 {
142   proto_item *ti;
143   proto_tree *fh_tree = NULL;
144   guint8     addr;
145   guint16    proto;
146
147   if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
148     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A");
149   if (check_col(pinfo->cinfo, COL_RES_DL_DST))
150     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A");
151   if (check_col(pinfo->cinfo, COL_PROTOCOL))
152     col_set_str(pinfo->cinfo, COL_PROTOCOL, "CHDLC");
153   if (check_col(pinfo->cinfo, COL_INFO))
154     col_clear(pinfo->cinfo, COL_INFO);
155
156   addr = tvb_get_guint8(tvb, 0);
157   proto = tvb_get_ntohs(tvb, 2);
158
159   if (tree) {
160     ti = proto_tree_add_item(tree, proto_chdlc, tvb, 0, 4, FALSE);
161     fh_tree = proto_item_add_subtree(ti, ett_chdlc);
162
163     proto_tree_add_uint(fh_tree, hf_chdlc_addr, tvb, 0, 1, addr);
164   }
165
166   chdlctype(proto, tvb, 4, pinfo, tree, fh_tree, hf_chdlc_proto);
167 }
168
169 void
170 proto_register_chdlc(void)
171 {
172   static hf_register_info hf[] = {
173     { &hf_chdlc_addr,
174       { "Address", "chdlc.address", FT_UINT8, BASE_HEX,
175         VALS(chdlc_address_vals), 0x0, "", HFILL }},
176     { &hf_chdlc_proto,
177       { "Protocol", "chdlc.protocol", FT_UINT16, BASE_HEX,
178         VALS(chdlc_vals), 0x0, "", HFILL }},
179   };
180   static gint *ett[] = {
181     &ett_chdlc,
182   };
183
184   proto_chdlc = proto_register_protocol("Cisco HDLC", "CHDLC", "chdlc");
185   proto_register_field_array(proto_chdlc, hf, array_length(hf));
186   proto_register_subtree_array(ett, array_length(ett));
187
188 /* subdissector code */
189   subdissector_table = register_dissector_table("chdlctype",
190         "Cisco HDLC frame type", FT_UINT16, BASE_HEX);
191
192   register_dissector("chdlc", dissect_chdlc, proto_chdlc);
193 }
194
195 void
196 proto_reg_handoff_chdlc(void)
197 {
198   dissector_handle_t chdlc_handle;
199
200   data_handle = find_dissector("data");
201   chdlc_handle = find_dissector("chdlc");
202   dissector_add("wtap_encap", WTAP_ENCAP_CHDLC, chdlc_handle);
203 }
204
205 #define SLARP_REQUEST   0
206 #define SLARP_REPLY     1
207 #define SLARP_LINECHECK 2
208
209 static const value_string slarp_ptype_vals[] = {
210         {SLARP_REQUEST,   "Request"},
211         {SLARP_REPLY,     "Reply"},
212         {SLARP_LINECHECK, "Line keepalive"},
213         {0,               NULL}
214 };
215
216 static void
217 dissect_slarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
218 {
219   proto_item *ti;
220   proto_tree *slarp_tree = NULL;
221   guint32 code;
222   guint32 address;
223   guint32 mysequence;
224   guint32 yoursequence;
225
226   if (check_col(pinfo->cinfo, COL_PROTOCOL))
227     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SLARP");
228   if (check_col(pinfo->cinfo, COL_INFO))
229     col_clear(pinfo->cinfo, COL_INFO);
230
231   code = tvb_get_ntohl(tvb, 0);
232
233   if (tree) {
234     ti = proto_tree_add_item(tree, proto_slarp, tvb, 0, 14, FALSE);
235     slarp_tree = proto_item_add_subtree(ti, ett_slarp);
236   }
237
238   switch (code) {
239
240   case SLARP_REQUEST:
241   case SLARP_REPLY:
242     if (check_col(pinfo->cinfo, COL_INFO)) {
243       tvb_memcpy(tvb, (guint8 *)&address, 4, 4);
244       col_add_fstr(pinfo->cinfo, COL_INFO, "%s, from %s, mask %s",
245         match_strval(code, slarp_ptype_vals),
246         get_hostname(address),
247         ip_to_str(tvb_get_ptr(tvb, 8, 4)));
248     }
249     if (tree) {
250       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
251       proto_tree_add_item(slarp_tree, hf_slarp_address, tvb, 4, 4, FALSE);
252       proto_tree_add_text(slarp_tree, tvb, 8, 4,
253                           "Netmask: %s", ip_to_str(tvb_get_ptr(tvb, 8, 4)));
254     }
255     break;
256
257   case SLARP_LINECHECK:
258     mysequence = tvb_get_ntohl(tvb, 4);
259     yoursequence = tvb_get_ntohl(tvb, 8);
260     if (check_col(pinfo->cinfo, COL_INFO)) {
261       col_add_fstr(pinfo->cinfo, COL_INFO,
262         "%s, outgoing sequence %u, returned sequence %u",
263         match_strval(code, slarp_ptype_vals),
264         mysequence, yoursequence);
265     }
266     if (tree) {
267       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
268       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 4, 4,
269                           mysequence);
270       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 8, 4,
271                           yoursequence);
272     }
273     break;
274
275   default:
276     if (check_col(pinfo->cinfo, COL_INFO))
277       col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown packet type 0x%08X", code);
278     if (tree) {
279       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
280       call_dissector(data_handle, tvb_new_subset(tvb, 4, -1, -1), pinfo,
281                      slarp_tree);
282     }
283     break;
284   }
285 }
286
287 void
288 proto_register_slarp(void)
289 {
290   static hf_register_info hf[] = {
291     { &hf_slarp_ptype,
292       { "Packet type", "slarp.ptype", FT_UINT32, BASE_DEC,
293         VALS(slarp_ptype_vals), 0x0, "", HFILL }},
294     { &hf_slarp_address,
295       { "Address", "slarp.address", FT_IPv4, BASE_NONE,
296         NULL, 0x0, "", HFILL }},
297     /* XXX - need an FT_ for netmasks, which is like FT_IPV4 but doesn't
298        get translated to a host name. */
299     { &hf_slarp_mysequence,
300       { "Outgoing sequence number", "slarp.mysequence", FT_UINT32, BASE_DEC,
301         NULL, 0x0, "", HFILL }},
302     { &hf_slarp_yoursequence,
303       { "Returned sequence number", "slarp.yoursequence", FT_UINT32, BASE_DEC,
304         NULL, 0x0, "", HFILL }},
305   };
306   static gint *ett[] = {
307     &ett_chdlc,
308     &ett_slarp,
309   };
310
311   proto_slarp = proto_register_protocol("Cisco SLARP", "SLARP", "slarp");
312   proto_register_field_array(proto_slarp, hf, array_length(hf));
313   proto_register_subtree_array(ett, array_length(ett));
314 }
315
316 void
317 proto_reg_handoff_slarp(void)
318 {
319   dissector_handle_t slarp_handle;
320
321   slarp_handle = create_dissector_handle(dissect_slarp, proto_slarp);
322   dissector_add("chdlctype", CISCO_SLARP, slarp_handle);
323 }