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