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