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