Include <string.h> to get "strcmp()" declared.
[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.2 2003/08/05 19:09:27 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   };
121
122   static const value_string encoding_type[] = {
123     { 0, "Unknown" },
124     { 1, "CCK" },
125     { 2, "PBCC" },
126     { 3, "OFDM" },
127   };
128
129   static const value_string ssi_type[] = {
130     { 0, "None" },
131     { 1, "Normalized RSSI" },
132     { 2, "dBm" },
133     { 3, "Raw RSSI" },
134   };
135
136   static const value_string preamble_type[] = {
137     { 0, "Unknown" },
138     { 1, "Short" },
139     { 2, "Long" },
140   };
141
142   static hf_register_info hf[] = {
143     { &hf_wlan_version, { "Header revision", "wlancap.version", FT_UINT32, 
144                           BASE_DEC, NULL, 0x0, "", HFILL } },
145     { &hf_wlan_length, { "Header length", "wlancap.length", FT_UINT32, 
146                          BASE_DEC, NULL, 0x0, "", HFILL } },
147     { &hf_wlan_mactime, { "MAC timestamp", "wlancap.mactime", FT_UINT64, 
148                           BASE_DEC, NULL, 0x0, "", HFILL } },
149     { &hf_wlan_hosttime, { "Host timestamp", "wlancap.hosttime", FT_UINT64, 
150                            BASE_DEC, NULL, 0x0, "", HFILL } },
151     { &hf_wlan_phytype, { "PHY type", "wlancap.phytype", FT_UINT32, BASE_DEC,
152                           VALS(phy_type), 0x0, "", HFILL } },
153     { &hf_wlan_channel, { "Channel", "wlancap.channel", FT_UINT32, BASE_DEC,
154                           NULL, 0x0, "", HFILL } },
155     { &hf_wlan_datarate, { "Data rate", "wlancap.datarate", FT_UINT32, 
156                            BASE_DEC, NULL, 0x0, "", HFILL } },
157     { &hf_wlan_antenna, { "Antenna", "wlancap.antenna", FT_UINT32, BASE_DEC,
158                           NULL, 0x0, "", HFILL } },
159     { &hf_wlan_priority, { "Priority", "wlancap.priority", FT_UINT32, BASE_DEC,
160                            NULL, 0x0, "", HFILL } },
161     { &hf_wlan_ssi_type, { "SSI Type", "wlancap.ssi_type", FT_UINT32, BASE_DEC,
162                            VALS(ssi_type), 0x0, "", HFILL } },
163     { &hf_wlan_ssi_signal, { "SSI Signal", "wlancap.ssi_signal", FT_INT32, 
164                              BASE_DEC, NULL, 0x0, "", HFILL } },
165     { &hf_wlan_ssi_noise, { "SSI Noise", "wlancap.ssi_noise", FT_INT32, 
166                             BASE_DEC, NULL, 0x0, "", HFILL } },
167     { &hf_wlan_preamble, { "Preamble", "wlancap.preamble", FT_UINT32, 
168                            BASE_DEC, VALS(preamble_type), 0x0, "", HFILL } },
169     { &hf_wlan_encoding, { "Encoding Type", "wlancap.encoding", FT_UINT32, 
170                            BASE_DEC, VALS(encoding_type), 0x0, "", HFILL } },
171   };
172   static gint *ett[] = {
173     &ett_wlan
174   };
175
176   proto_wlancap = proto_register_protocol("AVS WLAN Capture header", "AVS WLANCAP", "wlancap");
177   proto_register_field_array(proto_wlancap, hf, array_length(hf));
178   proto_register_subtree_array(ett, array_length(ett));
179   register_dissector("wlancap", dissect_wlancap, proto_wlancap);
180
181 }
182
183 static void
184 dissect_wlancap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
185 {
186     proto_tree *wlan_tree;
187     proto_item *ti;
188     tvbuff_t *next_tvb;
189     int offset;
190     guint32 version;
191     guint32 length;
192
193     if(check_col(pinfo->cinfo, COL_PROTOCOL))
194         col_set_str(pinfo->cinfo, COL_PROTOCOL, "WLAN");
195     if(check_col(pinfo->cinfo, COL_INFO))
196         col_clear(pinfo->cinfo, COL_INFO);
197     offset = 0;
198
199     version = tvb_get_ntohl(tvb, offset) - WLANCAP_MAGIC_COOKIE_BASE;
200     length = tvb_get_ntohl(tvb, offset+4);
201
202     if(check_col(pinfo->cinfo, COL_INFO))
203         col_add_fstr(pinfo->cinfo, COL_INFO, "AVS WLAN Capture v%x, Length %d",version, length);
204
205     /* Dissect the packet */
206     if (tree) {
207       ti = proto_tree_add_protocol_format(tree, proto_wlancap,
208             tvb, 0, length, "AVS WLAN Monitoring Header");
209       wlan_tree = proto_item_add_subtree(ti, ett_wlan);
210       proto_tree_add_uint(wlan_tree, hf_wlan_version, tvb, offset,
211                           4, tvb_get_ntohl(tvb, offset) - WLANCAP_MAGIC_COOKIE_BASE);
212       offset+=4;
213       proto_tree_add_uint(wlan_tree, hf_wlan_length, tvb, offset,
214                           4, tvb_get_ntohl(tvb, offset));
215       offset+=4;
216       proto_tree_add_item(wlan_tree, hf_wlan_mactime, tvb, offset,
217                           8, FALSE);
218       offset+=8;
219       proto_tree_add_item(wlan_tree, hf_wlan_hosttime, tvb, offset,
220                           8, FALSE);
221       offset+=8;
222
223       proto_tree_add_uint(wlan_tree, hf_wlan_phytype, tvb, offset,
224                           4, tvb_get_ntohl(tvb, offset));
225       offset+=4;
226       /* XXX cook channel (fh uses different numbers) */
227       proto_tree_add_uint(wlan_tree, hf_wlan_channel, tvb, offset,
228                           4, tvb_get_ntohl(tvb, offset));
229       offset+=4;
230
231       proto_tree_add_uint_format(wlan_tree, hf_wlan_datarate, tvb, offset, 
232                                  4, tvb_get_ntohl(tvb, offset) * 100, 
233                                  "Datarate: %d kbps", 
234                                  tvb_get_ntohl(tvb, offset) * 100);
235       offset+=4;
236       proto_tree_add_uint(wlan_tree, hf_wlan_antenna, tvb, offset,
237                           4, tvb_get_ntohl(tvb, offset));
238       offset+=4;
239       proto_tree_add_uint(wlan_tree, hf_wlan_priority, tvb, offset,
240                           4, tvb_get_ntohl(tvb, offset));
241       offset+=4;
242       proto_tree_add_uint(wlan_tree, hf_wlan_ssi_type, tvb, offset,
243                           4, tvb_get_ntohl(tvb, offset));
244       offset+=4;
245       /* XXX cook ssi_signal (Based on type; ie format) */
246       proto_tree_add_int(wlan_tree, hf_wlan_ssi_signal, tvb, offset,
247                          4, tvb_get_ntohl(tvb, offset));
248       offset+=4;
249       /* XXX cook ssi_noise (Based on type; ie format) */
250       proto_tree_add_int(wlan_tree, hf_wlan_ssi_noise, tvb, offset,
251                           4, tvb_get_ntohl(tvb, offset));
252       offset+=4;
253       proto_tree_add_uint(wlan_tree, hf_wlan_preamble, tvb, offset,
254                           4, tvb_get_ntohl(tvb, offset));
255       offset+=4;
256       proto_tree_add_uint(wlan_tree, hf_wlan_encoding, tvb, offset,
257                           4, tvb_get_ntohl(tvb, offset));
258       offset+=4;
259     }
260
261     if (offset == 0)
262       offset = length;
263
264     /* dissect the 802.11 header next */
265     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
266     call_dissector(ieee80211_handle, next_tvb, pinfo, tree);
267 }
268
269 void
270 proto_reg_handoff_wlancap(void)
271 {
272     dissector_handle_t wlancap_handle;
273
274     /* handle for 802.11 dissector */
275     ieee80211_handle = find_dissector("wlan");
276
277     wlancap_handle = create_dissector_handle(dissect_wlancap, proto_wlancap);
278
279     dissector_add("wtap_encap", WTAP_ENCAP_WLAN_HEADER, wlancap_handle);
280 }