For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-cisco-wids.c
1 /* packet-cwids.c
2  * Routines for dissecting wireless ids packets sent from a Cisco
3  * access point to the WLSE (or whatever)
4  *
5  * $Id$
6  *
7  * Copyright 2006 Joerg Mayer (see AUTHORS file)
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 /* With current IOS, you can use Cisco wireless Bridges/APs as
29  * wireless sniffers and configure them to send the data to some
30  * central IDS:
31  * interface dot11Radio 0
32  *   station-role scanner
33  *   monitor frames endpoint ip address 172.22.1.1 port 8999 truncate 2312
34  * These frames are raw, i.e. they don't have a pcap header.
35  * Running wireshark at the receiving end will provide those.
36  */
37
38 /* 2do:
39  *      - Find out more about the contents of the capture header
40  *      - Protect the address fields etc (all columns?)
41  *      - Create subelements and put each header and packet into it
42  *      - fuzz-test the dissector
43  *      - Find some heuristic to detect the packet automagically and
44  *        convert dissector into a heuristic dissector
45  *      - Is the TRY/CATCH stuff OK?
46  */
47
48 #ifdef HAVE_CONFIG_H
49 # include "config.h"
50 #endif
51
52 #include <glib.h>
53 #include <epan/packet.h>
54 #include <epan/etypes.h>
55 #include <epan/expert.h>
56 #include <epan/prefs.h>
57
58 static guint global_udp_port = 0;
59
60 static int proto_cwids = -1;
61 static int hf_cwids_version = -1;
62 static int hf_cwids_unknown1 = -1;
63 static int hf_cwids_channel = -1;
64 static int hf_cwids_unknown2 = -1;
65 static int hf_cwids_reallength = -1;
66 static int hf_cwids_capturelen = -1;
67 static int hf_cwids_unknown3 = -1;
68
69 static gint ett_cwids = -1;
70
71 static dissector_handle_t ieee80211_handle;
72
73 static void
74 dissect_cwids(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
75 {
76         tvbuff_t *wlan_tvb;
77         proto_tree *ti, *cwids_tree;
78         volatile int offset = 0;
79         guint16 capturelen;
80         void *pd_save;
81
82         col_set_str(pinfo->cinfo, COL_PROTOCOL, "CWIDS");
83         col_set_str(pinfo->cinfo, COL_INFO, "Cwids: ");
84         /* FIXME: col_set_fence(pinfo->cinfo, all-cols, only addr-cols?); */
85
86         cwids_tree = NULL;
87
88         while(tvb_length_remaining(tvb, offset) > 0) {
89                 ti = proto_tree_add_item(tree, proto_cwids, tvb, offset, 28, ENC_NA);
90                 cwids_tree = proto_item_add_subtree(ti, ett_cwids);
91
92                 proto_tree_add_item(cwids_tree, hf_cwids_version, tvb, offset, 2, ENC_BIG_ENDIAN);
93                 offset += 2;
94                 proto_tree_add_item(cwids_tree, hf_cwids_unknown1, tvb, offset, 7, ENC_NA);
95                 offset += 7;
96                 proto_tree_add_item(cwids_tree, hf_cwids_channel, tvb, offset, 1, ENC_BIG_ENDIAN);
97                 offset += 1;
98                 proto_tree_add_item(cwids_tree, hf_cwids_unknown2, tvb, offset, 6, ENC_NA);
99                 offset += 6;
100                 proto_tree_add_item(cwids_tree, hf_cwids_reallength, tvb, offset, 2, ENC_BIG_ENDIAN);
101                 offset += 2;
102                 capturelen = tvb_get_ntohs(tvb, offset);
103                 proto_tree_add_item(cwids_tree, hf_cwids_capturelen, tvb, offset, 2, ENC_BIG_ENDIAN);
104                 offset += 2;
105                 proto_tree_add_item(cwids_tree, hf_cwids_unknown3, tvb, offset, 8, ENC_NA);
106                 offset += 8;
107
108                 wlan_tvb = tvb_new_subset(tvb, offset, capturelen, capturelen);
109                 /* Continue after ieee80211 dissection errors */
110                 pd_save = pinfo->private_data;
111                 TRY {
112                         call_dissector(ieee80211_handle, wlan_tvb, pinfo, tree);
113                 } CATCH2(BoundsError, ReportedBoundsError) {
114
115                         expert_add_info_format(pinfo, NULL,
116                                 PI_MALFORMED, PI_ERROR,
117                                 "Malformed or short IEEE80211 subpacket");
118
119                         /*  Restore the private_data structure in case one of the
120                          *  called dissectors modified it (and, due to the exception,
121                          *  was unable to restore it).
122                          */
123                         pinfo->private_data = pd_save;
124
125                         col_append_str(pinfo->cinfo, COL_INFO,
126                                 " [Malformed or short IEEE80211 subpacket] " );
127                         col_set_fence(pinfo->cinfo, COL_INFO);
128 #if 0
129         wlan_tvb = tvb_new_subset(tvb, offset, capturelen, capturelen);
130                         /* FIXME: Why does this throw an exception? */
131                         proto_tree_add_text(cwids_tree, wlan_tvb, offset, capturelen,
132                                 "[Malformed or short IEEE80211 subpacket]");
133 #else
134                         tvb_new_subset(tvb, offset, capturelen, capturelen);
135 #endif
136         ;
137                 } ENDTRY;
138
139                 offset += capturelen;
140         }
141 }
142
143 void proto_register_cwids(void);
144 void proto_reg_handoff_cwids(void);
145
146 void
147 proto_register_cwids(void)
148 {
149         static hf_register_info hf[] = {
150                 { &hf_cwids_version,
151                 { "Capture Version", "cwids.version", FT_UINT16, BASE_DEC, NULL,
152                         0x0, "Version or format of record", HFILL }},
153
154                 { &hf_cwids_unknown1,
155                 { "Unknown1", "cwids.unknown1", FT_BYTES, BASE_NONE, NULL,
156                         0x0, "1st Unknown block - timestamp?", HFILL }},
157
158                 { &hf_cwids_channel,
159                 { "Channel", "cwids.channel", FT_UINT8, BASE_DEC, NULL,
160                         0x0, "Channel for this capture", HFILL }},
161
162                 { &hf_cwids_unknown2,
163                 { "Unknown2", "cwids.unknown2", FT_BYTES, BASE_NONE, NULL,
164                         0x0, "2nd Unknown block", HFILL }},
165
166                 { &hf_cwids_reallength,
167                 { "Original length", "cwids.reallen", FT_UINT16, BASE_DEC, NULL,
168                         0x0, "Original num bytes in frame", HFILL }},
169
170                 { &hf_cwids_capturelen,
171                 { "Capture length", "cwids.caplen", FT_UINT16, BASE_DEC, NULL,
172                         0x0, "Captured bytes in record", HFILL }},
173
174                 { &hf_cwids_unknown3,
175                 { "Unknown3", "cwids.unknown3", FT_BYTES, BASE_NONE, NULL,
176                         0x0, "3rd Unknown block", HFILL }},
177
178         };
179         static gint *ett[] = {
180                 &ett_cwids,
181         };
182
183         module_t *cwids_module;
184
185         proto_cwids = proto_register_protocol("Cisco Wireless IDS Captures", "CWIDS", "cwids");
186         proto_register_field_array(proto_cwids, hf, array_length(hf));
187         proto_register_subtree_array(ett, array_length(ett));
188
189         cwids_module = prefs_register_protocol(proto_cwids, proto_reg_handoff_cwids);
190         prefs_register_uint_preference(cwids_module, "udp.port",
191                 "CWIDS port",
192                 "Set the destination UDP port Cisco wireless IDS messages",
193                 10, &global_udp_port);
194
195 }
196
197 void
198 proto_reg_handoff_cwids(void)
199 {
200         static dissector_handle_t cwids_handle;
201         static guint saved_udp_port;
202         static gboolean initialized = FALSE;
203
204         if (!initialized) {
205                 cwids_handle = create_dissector_handle(dissect_cwids, proto_cwids);
206                 dissector_add_handle("udp.port", cwids_handle);
207                 ieee80211_handle = find_dissector("wlan");
208                 initialized = TRUE;
209         } else {
210                 if (saved_udp_port != 0) {
211                         dissector_delete_uint("udp.port", saved_udp_port, cwids_handle);
212                 }
213         }
214         if (global_udp_port != 0) {
215                 dissector_add_uint("udp.port", global_udp_port, cwids_handle);
216         }
217         saved_udp_port = global_udp_port;
218 }
219