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