From Kovarththanan Rajaratnam via bug 3548:
[metze/wireshark/wip.git] / epan / dissectors / packet-chdlc.c
1 /* packet-chdlc.c
2  * Routines for Cisco HDLC packet disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 <epan/etypes.h>
32 #include <epan/prefs.h>
33 #include <epan/chdlctypes.h>
34 #include <epan/nlpid.h>
35 #include <epan/addr_resolv.h>
36 #include "packet-chdlc.h"
37 #include "packet-ppp.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  * We thus have a separate dissector table for Cisco HDLC types.
75  * We could perhaps have that table hold only type values that
76  * wouldn't be in the Ethernet dissector table, and check that
77  * table first and the Ethernet dissector table if that fails.
78  */
79 #define CISCO_SLARP     0x8035  /* Cisco SLARP protocol */
80
81 static dissector_table_t subdissector_table;
82
83 static const value_string chdlc_address_vals[] = {
84         {CHDLC_ADDR_UNICAST,   "Unicast"},
85         {CHDLC_ADDR_MULTICAST, "Multicast"},
86         {0,                    NULL}
87 };
88
89 const value_string chdlc_vals[] = {
90         {0x2000,               "Cisco Discovery Protocol"},
91         {ETHERTYPE_IP,         "IP"},
92         {ETHERTYPE_IPv6,       "IPv6"},
93         {CISCO_SLARP,          "SLARP"},
94         {ETHERTYPE_DEC_LB,     "DEC LanBridge"},
95         {CHDLCTYPE_BPDU,       "Spanning Tree BPDU"},
96         {ETHERTYPE_ATALK,      "Appletalk"},
97         {ETHERTYPE_AARP,       "AARP"},
98         {ETHERTYPE_IPX,        "Netware IPX/SPX"},
99         {ETHERTYPE_ETHBRIDGE,  "Transparent Ethernet bridging" },
100         {CHDLCTYPE_OSI,        "OSI" },
101         {ETHERTYPE_MPLS,       "MPLS unicast"},
102         {ETHERTYPE_MPLS_MULTI, "MPLS multicast"},
103         {0,                     NULL}
104 };
105
106 void
107 capture_chdlc( const guchar *pd, int offset, int len, packet_counts *ld ) {
108   if (!BYTES_ARE_IN_FRAME(offset, len, 4)) {
109     ld->other++;
110     return;
111   }
112   switch (pntohs(&pd[offset + 2])) {
113     case ETHERTYPE_IP:
114       capture_ip(pd, offset + 4, len, ld);
115       break;
116     default:
117       ld->other++;
118       break;
119   }
120 }
121
122 void
123 chdlctype(guint16 chdlc_type, tvbuff_t *tvb, int offset_after_chdlctype,
124           packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
125           int chdlctype_id)
126 {
127   tvbuff_t   *next_tvb;
128   int padbyte = 0;
129
130   if (tree) {
131     proto_tree_add_uint(fh_tree, chdlctype_id, tvb,
132                         offset_after_chdlctype - 2, 2, chdlc_type);
133   }
134
135   padbyte = tvb_get_guint8(tvb, offset_after_chdlctype);
136   if (chdlc_type == CHDLCTYPE_OSI &&
137     !( padbyte == NLPID_ISO8473_CLNP || /* older Juniper SW does not send a padbyte */
138        padbyte == NLPID_ISO9542_ESIS ||
139        padbyte == NLPID_ISO10589_ISIS)) {
140     /* There is a Padding Byte for CLNS protocols over Cisco HDLC */
141     proto_tree_add_text(fh_tree, tvb, offset_after_chdlctype, 1, "CLNS Padding: 0x%02x",
142         padbyte);
143     next_tvb = tvb_new_subset(tvb, offset_after_chdlctype + 1, -1, -1);
144   } else {
145     next_tvb = tvb_new_subset(tvb, offset_after_chdlctype, -1, -1);
146   }
147
148   /* do lookup with the subdissector table */
149   if (!dissector_try_port(subdissector_table, chdlc_type, next_tvb, pinfo, tree)) {
150     if (check_col(pinfo->cinfo, COL_PROTOCOL))
151       col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "0x%04x", chdlc_type);
152     call_dissector(data_handle,next_tvb, pinfo, tree);
153   }
154 }
155
156 static gint chdlc_fcs_decode = 0; /* 0 = No FCS, 1 = 16 bit FCS, 2 = 32 bit FCS */
157
158 static void
159 dissect_chdlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
160 {
161   proto_item *ti;
162   proto_tree *fh_tree = NULL;
163   guint8     addr;
164   guint16    proto;
165
166   if (check_col(pinfo->cinfo, COL_PROTOCOL))
167     col_set_str(pinfo->cinfo, COL_PROTOCOL, "CHDLC");
168   if (check_col(pinfo->cinfo, COL_INFO))
169     col_clear(pinfo->cinfo, COL_INFO);
170
171   switch (pinfo->p2p_dir) {
172
173   case P2P_DIR_SENT:
174     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
175       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
176     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
177       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
178     break;
179
180   case P2P_DIR_RECV:
181     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
182       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
183     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
184       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
185     break;
186
187   default:
188     if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
189       col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "N/A");
190     if (check_col(pinfo->cinfo, COL_RES_DL_DST))
191       col_set_str(pinfo->cinfo, COL_RES_DL_DST, "N/A");
192     break;
193   }
194
195   addr = tvb_get_guint8(tvb, 0);
196   proto = tvb_get_ntohs(tvb, 2);
197
198   if (tree) {
199     ti = proto_tree_add_item(tree, proto_chdlc, tvb, 0, 4, FALSE);
200     fh_tree = proto_item_add_subtree(ti, ett_chdlc);
201
202     proto_tree_add_uint(fh_tree, hf_chdlc_addr, tvb, 0, 1, addr);
203   }
204
205   decode_fcs(tvb, fh_tree, chdlc_fcs_decode, 2);
206
207   chdlctype(proto, tvb, 4, pinfo, tree, fh_tree, hf_chdlc_proto);
208 }
209
210 void
211 proto_register_chdlc(void)
212 {
213   static hf_register_info hf[] = {
214     { &hf_chdlc_addr,
215       { "Address", "chdlc.address", FT_UINT8, BASE_HEX,
216         VALS(chdlc_address_vals), 0x0, "", HFILL }},
217     { &hf_chdlc_proto,
218       { "Protocol", "chdlc.protocol", FT_UINT16, BASE_HEX,
219         VALS(chdlc_vals), 0x0, "", HFILL }},
220   };
221   static gint *ett[] = {
222     &ett_chdlc,
223   };
224
225   module_t *chdlc_module;
226
227   proto_chdlc = proto_register_protocol("Cisco HDLC", "CHDLC", "chdlc");
228   proto_register_field_array(proto_chdlc, hf, array_length(hf));
229   proto_register_subtree_array(ett, array_length(ett));
230
231 /* subdissector code */
232   subdissector_table = register_dissector_table("chdlctype",
233         "Cisco HDLC frame type", FT_UINT16, BASE_HEX);
234
235   register_dissector("chdlc", dissect_chdlc, proto_chdlc);
236
237   /* Register the preferences for the chdlc protocol */
238   chdlc_module = prefs_register_protocol(proto_chdlc, NULL);
239
240   prefs_register_enum_preference(chdlc_module,
241         "fcs_type",
242         "CHDLC Frame Checksum Type",
243         "The type of CHDLC frame checksum (none, 16-bit, 32-bit)",
244         &chdlc_fcs_decode,
245         fcs_options, FALSE);
246
247 }
248
249 void
250 proto_reg_handoff_chdlc(void)
251 {
252   dissector_handle_t chdlc_handle;
253
254   data_handle = find_dissector("data");
255   chdlc_handle = find_dissector("chdlc");
256   dissector_add("wtap_encap", WTAP_ENCAP_CHDLC, chdlc_handle);
257   dissector_add("wtap_encap", WTAP_ENCAP_CHDLC_WITH_PHDR, chdlc_handle);
258 }
259
260 #define SLARP_REQUEST   0
261 #define SLARP_REPLY     1
262 #define SLARP_LINECHECK 2
263
264 static const value_string slarp_ptype_vals[] = {
265         {SLARP_REQUEST,   "Request"},
266         {SLARP_REPLY,     "Reply"},
267         {SLARP_LINECHECK, "Line keepalive"},
268         {0,               NULL}
269 };
270
271 static void
272 dissect_slarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
273 {
274   proto_item *ti;
275   proto_tree *slarp_tree = NULL;
276   guint32 code;
277   guint32 addr;
278   guint32 mysequence;
279   guint32 yoursequence;
280
281   if (check_col(pinfo->cinfo, COL_PROTOCOL))
282     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SLARP");
283   if (check_col(pinfo->cinfo, COL_INFO))
284     col_clear(pinfo->cinfo, COL_INFO);
285
286   code = tvb_get_ntohl(tvb, 0);
287
288   if (tree) {
289     ti = proto_tree_add_item(tree, proto_slarp, tvb, 0, 14, FALSE);
290     slarp_tree = proto_item_add_subtree(ti, ett_slarp);
291   }
292
293   switch (code) {
294
295   case SLARP_REQUEST:
296   case SLARP_REPLY:
297     if (check_col(pinfo->cinfo, COL_INFO)) {
298         addr = tvb_get_ipv4(tvb, 4);
299         col_add_fstr(pinfo->cinfo, COL_INFO, "%s, from %s, mask %s",
300                      val_to_str(code, slarp_ptype_vals, "Unknown (%d)"),
301                      get_hostname(addr),
302         ip_to_str(tvb_get_ptr(tvb, 8, 4)));
303     }
304     if (tree) {
305       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
306       proto_tree_add_item(slarp_tree, hf_slarp_address, tvb, 4, 4, FALSE);
307       proto_tree_add_text(slarp_tree, tvb, 8, 4,
308                           "Netmask: %s", ip_to_str(tvb_get_ptr(tvb, 8, 4)));
309     }
310     break;
311
312   case SLARP_LINECHECK:
313     mysequence = tvb_get_ntohl(tvb, 4);
314     yoursequence = tvb_get_ntohl(tvb, 8);
315     if (check_col(pinfo->cinfo, COL_INFO)) {
316         col_add_fstr(pinfo->cinfo, COL_INFO,
317                      "%s, outgoing sequence %u, returned sequence %u",
318                      val_to_str(code, slarp_ptype_vals, "Unknown (%d)"),
319                      mysequence, yoursequence);
320     }
321     if (tree) {
322       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
323       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 4, 4,
324                           mysequence);
325       proto_tree_add_uint(slarp_tree, hf_slarp_mysequence, tvb, 8, 4,
326                           yoursequence);
327     }
328     break;
329
330   default:
331     if (check_col(pinfo->cinfo, COL_INFO))
332       col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown packet type 0x%08X", code);
333     if (tree) {
334       proto_tree_add_uint(slarp_tree, hf_slarp_ptype, tvb, 0, 4, code);
335       call_dissector(data_handle, tvb_new_subset(tvb, 4, -1, -1), pinfo,
336                      slarp_tree);
337     }
338     break;
339   }
340 }
341
342 void
343 proto_register_slarp(void)
344 {
345   static hf_register_info hf[] = {
346     { &hf_slarp_ptype,
347       { "Packet type", "slarp.ptype", FT_UINT32, BASE_DEC,
348         VALS(slarp_ptype_vals), 0x0, NULL, HFILL }},
349     { &hf_slarp_address,
350       { "Address", "slarp.address", FT_IPv4, BASE_NONE,
351         NULL, 0x0, NULL, HFILL }},
352     /* XXX - need an FT_ for netmasks, which is like FT_IPV4 but doesn't
353        get translated to a host name. */
354     { &hf_slarp_mysequence,
355       { "Outgoing sequence number", "slarp.mysequence", FT_UINT32, BASE_DEC,
356         NULL, 0x0, NULL, HFILL }},
357     { &hf_slarp_yoursequence,
358       { "Returned sequence number", "slarp.yoursequence", FT_UINT32, BASE_DEC,
359         NULL, 0x0, NULL, HFILL }},
360   };
361   static gint *ett[] = {
362     &ett_chdlc,
363     &ett_slarp,
364   };
365
366   proto_slarp = proto_register_protocol("Cisco SLARP", "SLARP", "slarp");
367   proto_register_field_array(proto_slarp, hf, array_length(hf));
368   proto_register_subtree_array(ett, array_length(ett));
369 }
370
371 void
372 proto_reg_handoff_slarp(void)
373 {
374   dissector_handle_t slarp_handle;
375
376   slarp_handle = create_dissector_handle(dissect_slarp, proto_slarp);
377   dissector_add("chdlctype", CISCO_SLARP, slarp_handle);
378 }