c792d4ba53ae53d60e366d5135d1a1869295acb6
[metze/wireshark/wip.git] / epan / dissectors / packet-aruba-erm.c
1 /* packet-aruba-erm.c
2  * Routines for the disassembly of Aruba encapsulated remote mirroring frames
3  * (Adapted from packet-hp-erm.c and packet-cisco-erspan.c)
4  *
5  * Copyright 2010  Alexis La Goutte <alexis.lagoutte at gmail dot com>
6  *
7  * ERM Radio-Format added by Hadriel Kaplan
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26  */
27
28 /*
29  * Format:
30  *  Use the Header of Record (Packet) Header
31  *
32  * typedef struct pcaprec_hdr_s {
33  *       guint32 ts_sec;          timestamp seconds
34  *       guint32 ts_usec;         timestamp microseconds
35  *       guint32 incl_len;        number of octets of packet saved in file
36  *       guint32 orig_len;        actual length of packet
37  * } pcaprec_hdr_t;
38  *
39  * Following with 802.11 header
40  */
41
42 /*
43  * Format:
44  *  The ERM Radio-Format has the above header, plus more, like this:
45  *
46  *  struct radio_pcap_hdr {
47  *   struct timeval ts;
48  *   __u32  capture_length;
49  *   __u32  frame_length;
50  *   __u16  rate_per_half_mhz;
51  *   __u8   channel;
52  *   __u8   signal_percent;
53  *  } __attribute__ ((packed));
54  *
55  * Following with 802.11 header
56  */
57
58 #include "config.h"
59
60 #include <glib.h>
61 #include <epan/packet.h>
62 #include <epan/prefs.h>
63
64 #define PROTO_SHORT_NAME "ARUBA_ERM"
65 #define PROTO_LONG_NAME  "ARUBA encapsulated remote mirroring"
66 #define PROTO_RADIO_SHORT_NAME "ARUBA_ERM_RADIO_FORMAT"
67 #define PROTO_RADIO_LONG_NAME  "ARUBA encapsulated remote mirroring - radio format"
68
69 void proto_register_aruba_erm(void);
70 void proto_reg_handoff_aruba_erm(void);
71 void proto_reg_handoff_aruba_erm_radio(void);
72
73 static range_t *global_aruba_erm_port_range;
74 static range_t *global_aruba_erm_radio_port_range;
75
76 static int  proto_aruba_erm       = -1;
77
78 static int  hf_aruba_erm_time             = -1;
79 static int  hf_aruba_erm_incl_len         = -1;
80 static int  hf_aruba_erm_orig_len         = -1;
81 static int  hf_aruba_erm_data_rate        = -1;
82 static int  hf_aruba_erm_channel          = -1;
83 static int  hf_aruba_erm_signal_strength  = -1;
84
85 static gint ett_aruba_erm = -1;
86
87 static dissector_handle_t aruba_erm_handle;
88 static dissector_handle_t aruba_erm_radio_handle;
89 static dissector_handle_t ieee80211_handle;
90
91 static void
92 dissect_aruba_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean is_radio)
93 {
94     proto_item *ti;
95     proto_tree *aruba_erm_tree;
96     tvbuff_t   *eth_tvb;
97     nstime_t ts;
98     int offset = 16;
99     guint16 data_rate;
100     guint8 signal_strength;
101
102     if (tree) {
103         ti = proto_tree_add_item(tree, proto_aruba_erm, tvb, 0, -1, ENC_NA);
104         aruba_erm_tree = proto_item_add_subtree(ti, ett_aruba_erm);
105
106         ts.secs = tvb_get_ntohl(tvb, 0);
107         ts.nsecs = tvb_get_ntohl(tvb,4)*1000;
108         proto_tree_add_time(aruba_erm_tree, hf_aruba_erm_time, tvb, 0, 8,&ts);
109         proto_tree_add_item(aruba_erm_tree, hf_aruba_erm_incl_len, tvb, 8, 4, ENC_BIG_ENDIAN);
110         proto_tree_add_item(aruba_erm_tree, hf_aruba_erm_orig_len, tvb, 12, 4, ENC_BIG_ENDIAN);
111
112         if (is_radio) {
113             data_rate = tvb_get_ntohs(tvb, 16);
114             proto_tree_add_uint_format(aruba_erm_tree, hf_aruba_erm_data_rate, tvb, 16, 2,
115                                          (guint32)data_rate,
116                                          "Data Rate: %u.%u Mb/s",
117                                          data_rate / 2,
118                                          data_rate & 1 ? 5 : 0);
119
120             proto_tree_add_item(aruba_erm_tree, hf_aruba_erm_channel, tvb, 18, 1, ENC_NA);
121
122             signal_strength = tvb_get_guint8(tvb, 19);
123             proto_tree_add_uint_format(aruba_erm_tree, hf_aruba_erm_signal_strength, tvb, 19, 1,
124                                          (guint32)signal_strength,
125                                          "Signal Strength: %u%%",
126                                          signal_strength);
127             offset += 4;
128         }
129     }
130
131     eth_tvb = tvb_new_subset_remaining(tvb, offset);
132     call_dissector(ieee80211_handle, eth_tvb, pinfo, tree);
133 }
134
135 static void
136 dissect_aruba_erm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
137 {
138     col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
139     col_set_str(pinfo->cinfo, COL_INFO, PROTO_SHORT_NAME ":");
140
141     dissect_aruba_common(tvb, pinfo, tree, FALSE);
142 }
143
144 static void
145 dissect_aruba_erm_radio(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
146 {
147     col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_RADIO_SHORT_NAME);
148     col_set_str(pinfo->cinfo, COL_INFO, PROTO_RADIO_SHORT_NAME ":");
149
150     dissect_aruba_common(tvb, pinfo, tree, TRUE);
151 }
152
153 void
154 proto_register_aruba_erm(void)
155 {
156
157     static hf_register_info hf[] = {
158
159         { &hf_aruba_erm_time,
160           { "Packet Capture Timestamp", "aruba_erm.time", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL,
161             0x00, NULL, HFILL }},
162         { &hf_aruba_erm_incl_len,
163           { "Packet Captured Length", "aruba_erm.incl_len", FT_UINT32, BASE_DEC, NULL,
164             0x00, NULL, HFILL }},
165         { &hf_aruba_erm_orig_len,
166           { "Packet Length", "aruba_erm.orig_len", FT_UINT32, BASE_DEC, NULL,
167             0x00, NULL, HFILL }},
168         { &hf_aruba_erm_data_rate,
169           { "Data Rate", "aruba_erm.data_rate", FT_UINT16, BASE_DEC, NULL,
170             0x00, "Data rate (1/2 Mb/s)", HFILL }},
171         { &hf_aruba_erm_channel,
172           { "Channel", "aruba_erm.channel", FT_UINT8, BASE_DEC, NULL,
173             0x00, "802.11 channel number that this frame was sent/received on", HFILL }},
174         { &hf_aruba_erm_signal_strength,
175           { "Signal Strength", "aruba_erm.signal_strength", FT_UINT8, BASE_DEC, NULL,
176             0x00, "Signal strength (Percentage)", HFILL }},
177     };
178
179     /* both formats share the same tree */
180     static gint *ett[] = {
181         &ett_aruba_erm,
182     };
183
184     module_t *aruba_erm_module;
185
186     proto_aruba_erm = proto_register_protocol(PROTO_LONG_NAME, PROTO_SHORT_NAME, "aruba_erm");
187
188     range_convert_str (&global_aruba_erm_port_range, "0", MAX_UDP_PORT);
189     range_convert_str (&global_aruba_erm_radio_port_range, "0", MAX_UDP_PORT);
190
191     aruba_erm_module = prefs_register_protocol(proto_aruba_erm, proto_reg_handoff_aruba_erm);
192
193     prefs_register_range_preference(aruba_erm_module, "udp.ports", "ARUBA_ERM UDP Port numbers",
194                                     "Set the UDP port numbers (typically the range 5555 to 5560) used for ARUBA"
195                                     " encapsulated remote mirroring frames;\n"
196                                     "0 (default) means that the ARUBA_ERM dissector is not active\n",
197                                     &global_aruba_erm_port_range, MAX_UDP_PORT);
198
199     prefs_register_range_preference(aruba_erm_module, "radio.udp.ports", "ARUBA_ERM_RADIO_FORMAT UDP Port numbers",
200                                     "Set the UDP port numbers (typically the range 5555 to 5560) used for ARUBA"
201                                     " encapsulated remote mirroring frames with radio format;\n"
202                                     "0 (default) means that the ARUBA_ERM_RADIO_FORMAT dissector is not active\n",
203                                     &global_aruba_erm_radio_port_range, MAX_UDP_PORT);
204
205     proto_register_field_array(proto_aruba_erm, hf, array_length(hf));
206     proto_register_subtree_array(ett, array_length(ett));
207 }
208
209 void
210 proto_reg_handoff_aruba_erm(void)
211 {
212     static range_t *aruba_erm_port_range;
213     static range_t *aruba_erm_radio_port_range;
214     static gboolean initialized = FALSE;
215
216     if (!initialized) {
217         ieee80211_handle = find_dissector("wlan");
218         aruba_erm_handle = create_dissector_handle(dissect_aruba_erm, proto_aruba_erm);
219         aruba_erm_radio_handle = create_dissector_handle(dissect_aruba_erm_radio, proto_aruba_erm);
220         initialized = TRUE;
221     } else {
222         dissector_delete_uint_range("udp.port", aruba_erm_port_range, aruba_erm_handle);
223         dissector_delete_uint_range("udp.port", aruba_erm_radio_port_range, aruba_erm_radio_handle);
224         g_free(aruba_erm_port_range);
225         g_free(aruba_erm_radio_port_range);
226     }
227
228     aruba_erm_port_range = range_copy(global_aruba_erm_port_range);
229     aruba_erm_radio_port_range = range_copy(global_aruba_erm_radio_port_range);
230
231     dissector_add_uint_range("udp.port", aruba_erm_port_range, aruba_erm_handle);
232     dissector_add_uint_range("udp.port", aruba_erm_radio_port_range, aruba_erm_radio_handle);
233 }
234
235 /*
236  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
237  *
238  * Local variables:
239  * c-basic-offset: 4
240  * tab-width: 8
241  * indent-tabs-mode: nil
242  * End:
243  *
244  * vi: set shiftwidth=4 tabstop=8 expandtab:
245  * :indentSize=4:tabSize=8:noTabs=true:
246  */