Don't capitalize "Filter" in "Display Filter" in the "Find" dialog box,
[obnox/wireshark/wip.git] / packet-wlancap.c
1 /*
2  *  packet-wlancap.c
3  *      Decode packets with a AVS-WLAN header
4  *
5  *  AVS linux-wlan-based products use a new sniff header to replace the 
6  *  old prism2-specific one dissected in packet-prism2.c.  This one has
7  *  additional fields, is designed to be non-hardware-specific, and more 
8  *  importantly, version and length fields so it can be extended later 
9  *  without breaking anything.
10  * 
11  * By Solomon Peachy
12  *
13  * $Id: packet-wlancap.c,v 1.4 2004/01/29 10:58:28 guy Exp $
14  *
15  * Ethereal - Network traffic analyzer
16  * By Gerald Combs <gerald@ethereal.com>
17  * Copyright 1998 Gerald Combs
18  *
19  * Copied from README.developer
20  *
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation; either version 2
24  * of the License, or (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <glib.h>
41 #include <string.h>
42
43 #include <epan/packet.h>
44 #include "packet-ieee80211.h"
45 #include "packet-wlancap.h"
46
47 #define SHORT_STR 256
48
49 /* protocol */
50 static int proto_wlancap = -1;
51
52 /* header attached during wlan monitor mode */
53 struct wlan_header_v1 {
54   guint32 version;
55   guint32 length;
56   guint64 mactime;
57   guint64 hosttime;
58   guint32 phytype;
59   guint32 channel;
60   guint32 datarate;
61   guint32 antenna;
62   guint32 priority;
63   guint32 ssi_type;
64   gint32 ssi_signal;
65   gint32 ssi_noise;
66   gint32 preamble;
67   gint32 encoding;
68 };
69
70 static int hf_wlan_version = -1;
71 static int hf_wlan_length = -1;
72 static int hf_wlan_mactime = -1;
73 static int hf_wlan_hosttime = -1;
74 static int hf_wlan_phytype = -1;
75 static int hf_wlan_channel = -1;
76 static int hf_wlan_datarate = -1;
77 static int hf_wlan_antenna = -1;
78 static int hf_wlan_priority = -1;
79 static int hf_wlan_ssi_type = -1;
80 static int hf_wlan_ssi_signal = -1;
81 static int hf_wlan_ssi_noise = -1;
82 static int hf_wlan_preamble = -1;
83 static int hf_wlan_encoding = -1;
84
85 static gint ett_wlan = -1;
86
87 static dissector_handle_t ieee80211_handle;
88
89 static void
90 dissect_wlancap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
91
92 void
93 capture_wlancap(const guchar *pd, int offset, int len, packet_counts *ld)
94 {
95     /* XXX eventually add in a version test. */
96     if(!BYTES_ARE_IN_FRAME(offset, len, (int)sizeof(struct wlan_header_v1))) {
97         ld->other ++;
98         return;
99     }
100     offset += sizeof(struct wlan_header_v1);
101
102     /* 802.11 header follows */
103     capture_ieee80211(pd, offset, len, ld);
104 }
105
106 void
107 proto_register_wlancap(void)
108 {
109
110   static const value_string phy_type[] = {
111     { 0, "Unknown" },
112     { 1, "FHSS 802.11 '97" },
113     { 2, "DSSS 802.11 '97" }, 
114     { 3, "IR Baseband" },
115     { 4, "DSSS 802.11b" },
116     { 5, "PBCC 802.11b" }, 
117     { 6, "OFDM 802.11g" },
118     { 7, "PBCC 802.11g" },
119     { 8, "OFDM 802.11a" },
120     { 0, NULL },
121   };
122
123   static const value_string encoding_type[] = {
124     { 0, "Unknown" },
125     { 1, "CCK" },
126     { 2, "PBCC" },
127     { 3, "OFDM" },
128     { 0, NULL },
129   };
130
131   static const value_string ssi_type[] = {
132     { 0, "None" },
133     { 1, "Normalized RSSI" },
134     { 2, "dBm" },
135     { 3, "Raw RSSI" },
136     { 0, NULL },
137   };
138
139   static const value_string preamble_type[] = {
140     { 0, "Unknown" },
141     { 1, "Short" },
142     { 2, "Long" },
143     { 0, NULL },
144   };
145
146   static hf_register_info hf[] = {
147     { &hf_wlan_version, { "Header revision", "wlancap.version", FT_UINT32, 
148                           BASE_DEC, NULL, 0x0, "", HFILL } },
149     { &hf_wlan_length, { "Header length", "wlancap.length", FT_UINT32, 
150                          BASE_DEC, NULL, 0x0, "", HFILL } },
151     { &hf_wlan_mactime, { "MAC timestamp", "wlancap.mactime", FT_UINT64, 
152                           BASE_DEC, NULL, 0x0, "", HFILL } },
153     { &hf_wlan_hosttime, { "Host timestamp", "wlancap.hosttime", FT_UINT64, 
154                            BASE_DEC, NULL, 0x0, "", HFILL } },
155     { &hf_wlan_phytype, { "PHY type", "wlancap.phytype", FT_UINT32, BASE_DEC,
156                           VALS(phy_type), 0x0, "", HFILL } },
157     { &hf_wlan_channel, { "Channel", "wlancap.channel", FT_UINT32, BASE_DEC,
158                           NULL, 0x0, "", HFILL } },
159     { &hf_wlan_datarate, { "Data rate", "wlancap.datarate", FT_UINT32, 
160                            BASE_DEC, NULL, 0x0, "", HFILL } },
161     { &hf_wlan_antenna, { "Antenna", "wlancap.antenna", FT_UINT32, BASE_DEC,
162                           NULL, 0x0, "", HFILL } },
163     { &hf_wlan_priority, { "Priority", "wlancap.priority", FT_UINT32, BASE_DEC,
164                            NULL, 0x0, "", HFILL } },
165     { &hf_wlan_ssi_type, { "SSI Type", "wlancap.ssi_type", FT_UINT32, BASE_DEC,
166                            VALS(ssi_type), 0x0, "", HFILL } },
167     { &hf_wlan_ssi_signal, { "SSI Signal", "wlancap.ssi_signal", FT_INT32, 
168                              BASE_DEC, NULL, 0x0, "", HFILL } },
169     { &hf_wlan_ssi_noise, { "SSI Noise", "wlancap.ssi_noise", FT_INT32, 
170                             BASE_DEC, NULL, 0x0, "", HFILL } },
171     { &hf_wlan_preamble, { "Preamble", "wlancap.preamble", FT_UINT32, 
172                            BASE_DEC, VALS(preamble_type), 0x0, "", HFILL } },
173     { &hf_wlan_encoding, { "Encoding Type", "wlancap.encoding", FT_UINT32, 
174                            BASE_DEC, VALS(encoding_type), 0x0, "", HFILL } },
175   };
176   static gint *ett[] = {
177     &ett_wlan
178   };
179
180   proto_wlancap = proto_register_protocol("AVS WLAN Capture header", "AVS WLANCAP", "wlancap");
181   proto_register_field_array(proto_wlancap, hf, array_length(hf));
182   proto_register_subtree_array(ett, array_length(ett));
183   register_dissector("wlancap", dissect_wlancap, proto_wlancap);
184
185 }
186
187 static void
188 dissect_wlancap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
189 {
190     proto_tree *wlan_tree;
191     proto_item *ti;
192     tvbuff_t *next_tvb;
193     int offset;
194     guint32 version;
195     guint32 length;
196
197     if(check_col(pinfo->cinfo, COL_PROTOCOL))
198         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WLAN");
199     if(check_col(pinfo->cinfo, COL_INFO))
200         col_clear(pinfo->cinfo, COL_INFO);
201     offset = 0;
202
203     version = tvb_get_ntohl(tvb, offset) - WLANCAP_MAGIC_COOKIE_BASE;
204     length = tvb_get_ntohl(tvb, offset+4);
205
206     if(check_col(pinfo->cinfo, COL_INFO))
207         col_add_fstr(pinfo->cinfo, COL_INFO, "AVS WLAN Capture v%x, Length %d",version, length);
208
209     /* Dissect the packet */
210     if (tree) {
211       ti = proto_tree_add_protocol_format(tree, proto_wlancap,
212             tvb, 0, length, "AVS WLAN Monitoring Header");
213       wlan_tree = proto_item_add_subtree(ti, ett_wlan);
214       proto_tree_add_uint(wlan_tree, hf_wlan_version, tvb, offset,
215                           4, tvb_get_ntohl(tvb, offset) - WLANCAP_MAGIC_COOKIE_BASE);
216       offset+=4;
217       proto_tree_add_uint(wlan_tree, hf_wlan_length, tvb, offset,
218                           4, tvb_get_ntohl(tvb, offset));
219       offset+=4;
220       proto_tree_add_item(wlan_tree, hf_wlan_mactime, tvb, offset,
221                           8, FALSE);
222       offset+=8;
223       proto_tree_add_item(wlan_tree, hf_wlan_hosttime, tvb, offset,
224                           8, FALSE);
225       offset+=8;
226
227       proto_tree_add_uint(wlan_tree, hf_wlan_phytype, tvb, offset,
228                           4, tvb_get_ntohl(tvb, offset));
229       offset+=4;
230       /* XXX cook channel (fh uses different numbers) */
231       proto_tree_add_uint(wlan_tree, hf_wlan_channel, tvb, offset,
232                           4, tvb_get_ntohl(tvb, offset));
233       offset+=4;
234
235       proto_tree_add_uint_format(wlan_tree, hf_wlan_datarate, tvb, offset, 
236                                  4, tvb_get_ntohl(tvb, offset) * 100, 
237                                  "Datarate: %d kbps", 
238                                  tvb_get_ntohl(tvb, offset) * 100);
239       offset+=4;
240       proto_tree_add_uint(wlan_tree, hf_wlan_antenna, tvb, offset,
241                           4, tvb_get_ntohl(tvb, offset));
242       offset+=4;
243       proto_tree_add_uint(wlan_tree, hf_wlan_priority, tvb, offset,
244                           4, tvb_get_ntohl(tvb, offset));
245       offset+=4;
246       proto_tree_add_uint(wlan_tree, hf_wlan_ssi_type, tvb, offset,
247                           4, tvb_get_ntohl(tvb, offset));
248       offset+=4;
249       /* XXX cook ssi_signal (Based on type; ie format) */
250       proto_tree_add_int(wlan_tree, hf_wlan_ssi_signal, tvb, offset,
251                          4, tvb_get_ntohl(tvb, offset));
252       offset+=4;
253       /* XXX cook ssi_noise (Based on type; ie format) */
254       proto_tree_add_int(wlan_tree, hf_wlan_ssi_noise, tvb, offset,
255                           4, tvb_get_ntohl(tvb, offset));
256       offset+=4;
257       proto_tree_add_uint(wlan_tree, hf_wlan_preamble, tvb, offset,
258                           4, tvb_get_ntohl(tvb, offset));
259       offset+=4;
260       proto_tree_add_uint(wlan_tree, hf_wlan_encoding, tvb, offset,
261                           4, tvb_get_ntohl(tvb, offset));
262       offset+=4;
263     }
264
265     if (offset == 0)
266       offset = length;
267
268     /* dissect the 802.11 header next */
269     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
270     call_dissector(ieee80211_handle, next_tvb, pinfo, tree);
271 }
272
273 void
274 proto_reg_handoff_wlancap(void)
275 {
276     dissector_handle_t wlancap_handle;
277
278     /* handle for 802.11 dissector */
279     ieee80211_handle = find_dissector("wlan");
280
281     wlancap_handle = create_dissector_handle(dissect_wlancap, proto_wlancap);
282
283     dissector_add("wtap_encap", WTAP_ENCAP_IEEE_802_11_WLAN_AVS, wlancap_handle);
284 }