Initial support for Network Monitor 802.11 radio header.
[obnox/wireshark/wip.git] / epan / dissectors / packet-netmon-802_11.c
1 /*
2  *  packet-netmon-802_11.c
3  *      Decode packets with a Network Monitor 802.11 radio header
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from README.developer
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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <glib.h>
33 #include <string.h>
34
35 #include <epan/packet.h>
36
37 /* protocol */
38 static int proto_netmon_802_11 = -1;
39
40 #define MIN_HEADER_LEN  32
41
42 /* op_mode */
43 #define OP_MODE_STA     0x00000001      /* station mode */
44 #define OP_MODE_AP      0x00000002      /* AP mode */
45 #define OP_MODE_STA_EXT 0x00000004      /* extensible station mode */
46 #define OP_MODE_MON     0x80000000      /* monitor mode */
47
48 /* phy_type */
49 #define PHY_TYPE_11A    4
50 #define PHY_TYPE_11B    5
51 #define PHY_TYPE_11G    6
52 #define PHY_TYPE_11N    7
53
54 static int hf_netmon_802_11_version = -1;
55 static int hf_netmon_802_11_length = -1;
56 static int hf_netmon_802_11_op_mode = -1;
57 static int hf_netmon_802_11_op_mode_sta = -1;
58 static int hf_netmon_802_11_op_mode_ap = -1;
59 static int hf_netmon_802_11_op_mode_sta_ext = -1;
60 static int hf_netmon_802_11_op_mode_mon = -1;
61 static int hf_netmon_802_11_flags = -1;
62 static int hf_netmon_802_11_phy_type = -1;
63 static int hf_netmon_802_11_channel = -1;
64 static int hf_netmon_802_11_frequency = -1;
65 static int hf_netmon_802_11_rssi = -1;
66 static int hf_netmon_802_11_datarate = -1;
67 static int hf_netmon_802_11_timestamp = -1;
68
69 static gint ett_netmon_802_11 = -1;
70 static gint ett_netmon_802_11_op_mode = -1;
71
72 static dissector_handle_t ieee80211_handle;
73
74 static void
75 dissect_netmon_802_11(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
76 {
77   proto_tree *wlan_tree, *opmode_tree;
78   proto_item *ti;
79   tvbuff_t *next_tvb;
80   int offset;
81   guint8 version;
82   guint16 length;
83   guint32 flags;
84   guint32 channel;
85   gint32 rssi;
86   guint8 rate;
87
88   col_set_str(pinfo->cinfo, COL_PROTOCOL, "WLAN");
89   col_clear(pinfo->cinfo, COL_INFO);
90   offset = 0;
91
92   version = tvb_get_guint8(tvb, offset);
93   length = tvb_get_letohs(tvb, offset+1);
94   col_add_fstr(pinfo->cinfo, COL_INFO, "NetMon WLAN Capture v%u, Length %u",
95                version, length);
96   if (version != 2) {
97     /* XXX - complain */
98     goto skip;
99   }
100   if (length < MIN_HEADER_LEN) {
101     /* XXX - complain */
102     goto skip;
103   }
104
105   /* Dissect the packet */
106   if (tree) {
107     ti = proto_tree_add_item(tree, proto_netmon_802_11, tvb, 0, length,
108                              ENC_NA);
109     wlan_tree = proto_item_add_subtree(ti, ett_netmon_802_11);
110     proto_tree_add_item(wlan_tree, hf_netmon_802_11_version, tvb, offset, 1,
111                         ENC_LITTLE_ENDIAN);
112     offset += 1;
113     proto_tree_add_item(wlan_tree, hf_netmon_802_11_length, tvb, offset, 2,
114                         ENC_LITTLE_ENDIAN);
115     offset += 2;
116     ti = proto_tree_add_item(wlan_tree, hf_netmon_802_11_op_mode, tvb, offset, 4, ENC_LITTLE_ENDIAN);
117     opmode_tree = proto_item_add_subtree(ti, ett_netmon_802_11_op_mode);
118     proto_tree_add_item(opmode_tree, hf_netmon_802_11_op_mode_sta, tvb, offset,
119                         4, ENC_LITTLE_ENDIAN);
120     proto_tree_add_item(opmode_tree, hf_netmon_802_11_op_mode_ap, tvb, offset,
121                         4, ENC_LITTLE_ENDIAN);
122     proto_tree_add_item(opmode_tree, hf_netmon_802_11_op_mode_sta_ext, tvb,
123                         offset, 4, ENC_LITTLE_ENDIAN);
124     proto_tree_add_item(opmode_tree, hf_netmon_802_11_op_mode_mon, tvb, offset,
125                         4, ENC_LITTLE_ENDIAN);
126     offset += 4;
127     flags = tvb_get_letohl(tvb, offset);
128     offset += 4;
129     if (flags != 0xffffffff) {
130       proto_tree_add_item(wlan_tree, hf_netmon_802_11_phy_type, tvb, offset, 4,
131                           ENC_LITTLE_ENDIAN);
132       offset += 4;
133       channel = tvb_get_letohl(tvb, offset);
134       if (channel < 1000) {
135         proto_tree_add_uint(wlan_tree, hf_netmon_802_11_channel,
136                             tvb, offset, 4, channel);
137       } else {
138         proto_tree_add_uint_format_value(wlan_tree, hf_netmon_802_11_frequency,
139                                          tvb, offset, 4, channel,
140                                          "%u Mhz", channel);
141       }
142       offset += 4;
143       rssi = tvb_get_letohl(tvb, offset);
144       proto_tree_add_int_format_value(wlan_tree, hf_netmon_802_11_rssi,
145                                       tvb, offset, 4, rssi,
146                                       "%d dBm", rssi);
147       offset += 4;
148       rate = tvb_get_guint8(tvb, offset);
149       if (rate == 0) {
150         proto_tree_add_uint_format_value(wlan_tree, hf_netmon_802_11_datarate,
151                                          tvb, offset, 1, rate,
152                                          "Unknown");
153       } else {
154         proto_tree_add_uint_format_value(wlan_tree, hf_netmon_802_11_datarate,
155                                          tvb, offset, 1, rate,
156                                          "%f Mb/s", rate*.5);
157       }
158       offset += 1;
159     } else
160       offset += 13;
161     proto_tree_add_item(wlan_tree, hf_netmon_802_11_timestamp, tvb, offset, 8,
162                         ENC_LITTLE_ENDIAN);
163     offset += 8;
164   }
165
166 skip:
167   offset = length;
168
169   /* dissect the 802.11 header next */
170   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
171   call_dissector(ieee80211_handle, next_tvb, pinfo, tree);
172 }
173
174 void
175 proto_register_netmon_802_11(void)
176 {
177   static const value_string phy_type[] = {
178     { PHY_TYPE_11A, "802.11a" },
179     { PHY_TYPE_11B, "802.11b" },
180     { PHY_TYPE_11G, "802.11g" },
181     { PHY_TYPE_11N, "802.11n" },
182     { 0, NULL },
183   };
184
185   static hf_register_info hf[] = {
186     { &hf_netmon_802_11_version, { "Header revision", "netmon_802_11.version", FT_UINT8,
187                           BASE_DEC, NULL, 0x0, "", HFILL } },
188     { &hf_netmon_802_11_length, { "Header length", "netmon_802_11.length", FT_UINT16,
189                          BASE_DEC, NULL, 0x0, "", HFILL } },
190     { &hf_netmon_802_11_op_mode, { "Operation mode", "netmon_802_11.op_mode", FT_UINT32,
191                           BASE_HEX, NULL, 0x0, "", HFILL } },
192     { &hf_netmon_802_11_op_mode_sta, { "Station mode", "netmon_802_11.op_mode.sta", FT_UINT32,
193                           BASE_HEX, NULL, OP_MODE_STA, "", HFILL } },
194     { &hf_netmon_802_11_op_mode_ap, { "AP mode", "netmon_802_11.op_mode.ap", FT_UINT32,
195                           BASE_HEX, NULL, OP_MODE_AP, "", HFILL } },
196     { &hf_netmon_802_11_op_mode_sta_ext, { "Extensible station mode", "netmon_802_11.op_mode.sta_ext", FT_UINT32,
197                           BASE_HEX, NULL, OP_MODE_STA_EXT, "", HFILL } },
198     { &hf_netmon_802_11_op_mode_mon, { "Monitor mode", "netmon_802_11.op_mode.on", FT_UINT32,
199                           BASE_HEX, NULL, OP_MODE_MON, "", HFILL } },
200     { &hf_netmon_802_11_flags, { "Flags", "netmon_802_11.flags", FT_UINT32,
201                           BASE_HEX, NULL, 0x0, "", HFILL } },
202     { &hf_netmon_802_11_phy_type, { "PHY type", "netmon_802_11.phy_type", FT_UINT32,
203                           BASE_DEC, VALS(phy_type), 0x0, "", HFILL } },
204     { &hf_netmon_802_11_channel, { "Channel", "netmon_802_11.channel", FT_UINT32,
205                           BASE_DEC, NULL, 0x0, "", HFILL } },
206     { &hf_netmon_802_11_frequency, { "Center frequency", "netmon_802_11.frequency", FT_UINT32,
207                           BASE_DEC, NULL, 0x0, "", HFILL } },
208     { &hf_netmon_802_11_rssi, { "RSSI", "netmon_802_11.rssi", FT_INT32,
209                           BASE_DEC, NULL, 0x0, "", HFILL } },
210     { &hf_netmon_802_11_datarate, { "Data rate", "netmon_802_11.datarate", FT_UINT32,
211                           BASE_DEC, NULL, 0x0, "", HFILL } },
212     /*
213      * XXX - is this host, or MAC, time stamp?
214      * It might be a FILETIME.
215      */
216     { &hf_netmon_802_11_timestamp, { "Timestamp", "netmon_802_11.timestamp", FT_UINT64,
217                           BASE_DEC, NULL, 0x0, "", HFILL } },
218   };
219   static gint *ett[] = {
220     &ett_netmon_802_11,
221     &ett_netmon_802_11_op_mode
222   };
223
224   proto_netmon_802_11 = proto_register_protocol("NetMon 802.11 capture header",
225                                                 "NetMon 802.11",
226                                                 "netmon_802_11");
227   proto_register_field_array(proto_netmon_802_11, hf, array_length(hf));
228   proto_register_subtree_array(ett, array_length(ett));
229 }
230
231 void
232 proto_reg_handoff_netmon_802_11(void)
233 {
234   dissector_handle_t netmon_802_11_handle;
235
236   /* handle for 802.11 dissector */
237   ieee80211_handle = find_dissector("wlan");
238   netmon_802_11_handle = create_dissector_handle(dissect_netmon_802_11,
239                                                  proto_netmon_802_11);
240   dissector_add("wtap_encap", WTAP_ENCAP_IEEE802_11_NETMON_RADIO, netmon_802_11_handle);
241 }