blob: 3fb482668508c117991dacdb0d5e9b9ff4b68479 [file] [log] [blame]
Wade Guthrie0d438532012-05-18 14:18:50 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This code is derived from the 'iw' source code. The copyright and license
6// of that code is as follows:
7//
8// Copyright (c) 2007, 2008 Johannes Berg
9// Copyright (c) 2007 Andy Lutomirski
10// Copyright (c) 2007 Mike Kershaw
11// Copyright (c) 2008-2009 Luis R. Rodriguez
12//
13// Permission to use, copy, modify, and/or distribute this software for any
14// purpose with or without fee is hereby granted, provided that the above
15// copyright notice and this permission notice appear in all copies.
16//
17// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
repo syncdc085c82012-12-28 08:54:41 -080025#include "shill/nl80211_message.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070026
27#include <ctype.h>
28#include <endian.h>
29#include <errno.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070030#include <linux/nl80211.h>
31#include <net/if.h>
Wade Guthrie8343f7f2012-12-04 13:52:32 -080032#include <netinet/in.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070033#include <netlink/attr.h>
34#include <netlink/genl/ctrl.h>
35#include <netlink/genl/family.h>
36#include <netlink/genl/genl.h>
37#include <netlink/msg.h>
38#include <netlink/netlink.h>
39
40#include <iomanip>
41#include <string>
42
43#include <base/format_macros.h>
44#include <base/stl_util.h>
45#include <base/stringprintf.h>
46
repo sync90ee0fa2012-12-18 10:08:08 -080047#include "shill/attribute_list.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070048#include "shill/ieee80211.h"
49#include "shill/logging.h"
repo syncdc085c82012-12-28 08:54:41 -080050#include "shill/netlink_socket.h"
repo sync1538d442012-12-20 15:24:35 -080051#include "shill/nl80211_attribute.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070052#include "shill/scope_logger.h"
53
54using base::LazyInstance;
55using base::StringAppendF;
56using base::StringPrintf;
57using std::map;
58using std::string;
59using std::vector;
60
61namespace shill {
62
63namespace {
repo syncdc085c82012-12-28 08:54:41 -080064LazyInstance<Nl80211MessageDataCollector> g_datacollector =
Wade Guthrie0d438532012-05-18 14:18:50 -070065 LAZY_INSTANCE_INITIALIZER;
66} // namespace
67
repo syncdc085c82012-12-28 08:54:41 -080068const char Nl80211Message::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX";
Wade Guthrie0d438532012-05-18 14:18:50 -070069
70const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
71const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
72
repo syncdc085c82012-12-28 08:54:41 -080073const uint32_t Nl80211Message::kIllegalMessage = 0;
74const unsigned int Nl80211Message::kEthernetAddressBytes = 6;
75map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
76map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
Wade Guthrie0d438532012-05-18 14:18:50 -070077
78// The nl messages look like this:
79//
80// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
81// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
82// +-- gnhl nlmsg_tail(hdr) --+
83// | nlmsg_next(hdr) --+
84// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
85// -----+-----+-+----------------------------------------------+-++----
86// ... | | | payload | ||
87// | | +------+-+--------+-+--------------------------+ ||
88// | nl | | | | | | attribs | ||
89// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
90// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
91// | |d| hdr |d| |d| attr |a| load |a| ... |d||
92// | | | | | | | |d| |d| | ||
93// -----+-----+-+----------------------------------------------+-++----
94// ^ ^ ^ ^
95// | | | XXXXXXX <-- nla_len(nlattr)
96// | | | +-- nla_data(nlattr)
97// | | X-nla_total_size-X
98// | | XXXXX-nlmsg_attrlen-XXXXXX
99// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
100// +-- msg = nlmsg_hdr(raw_message)
101
102//
repo syncdc085c82012-12-28 08:54:41 -0800103// Nl80211Message
Wade Guthrie0d438532012-05-18 14:18:50 -0700104//
105
repo syncdc085c82012-12-28 08:54:41 -0800106// TODO(wdg): do the nlattr parsing inside here.
107bool Nl80211Message::Init(nlattr *tb[NL80211_ATTR_MAX + 1],
108 nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700109 if (!tb) {
110 LOG(ERROR) << "Null |tb| parameter";
111 return false;
112 }
113
114 message_ = msg;
115
repo syncdc085c82012-12-28 08:54:41 -0800116 SLOG(WiFi, 6) << "NL Message " << sequence_number() << " <===";
Wade Guthrie0d438532012-05-18 14:18:50 -0700117
118 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
119 if (tb[i]) {
repo sync12cca802012-12-19 17:34:22 -0800120 attributes_.CreateAndInitFromNlAttr(static_cast<nl80211_attrs>(i), tb[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700121 }
122 }
123
124 // Convert integer values provided by libnl (for example, from the
125 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
126 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700127 if (!reason_code_string_) {
128 reason_code_string_ = new map<uint16_t, string>;
129 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
130 "Unspecified reason";
131 (*reason_code_string_)[
132 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700133 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700134 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
135 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
136 "ESS";
137 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
138 "Disassociated due to inactivity";
139 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
140 "Disassociated because AP is unable to handle all currently associated "
141 "STAs";
142 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
143 "Class 2 frame received from nonauthenticated STA";
144 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
145 "Class 3 frame received from nonassociated STA";
146 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
147 "Disassociated because sending STA is leaving (or has left) BSS";
148 (*reason_code_string_)[
149 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
150 "STA requesting (re)association is not authenticated with responding "
151 "STA";
152 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
153 "Disassociated because the information in the Power Capability "
154 "element is unacceptable";
155 (*reason_code_string_)[
156 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
157 "Disassociated because the information in the Supported Channels "
158 "element is unacceptable";
159 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
160 "Invalid information element, i.e., an information element defined in "
161 "this standard for which the content does not meet the specifications "
162 "in Clause 7";
163 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
164 "Message integrity code (MIC) failure";
165 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
166 "4-Way Handshake timeout";
167 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
168 "Group Key Handshake timeout";
169 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
170 "Information element in 4-Way Handshake different from "
171 "(Re)Association Request/Probe Response/Beacon frame";
172 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
173 "Invalid group cipher";
174 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
175 "Invalid pairwise cipher";
176 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
177 "Invalid AKMP";
178 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
179 "Unsupported RSN information element version";
180 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
181 "Invalid RSN information element capabilities";
182 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
183 "IEEE 802.1X authentication failed";
184 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
185 "Cipher suite rejected because of the security policy";
186 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
187 "Disassociated for unspecified, QoS-related reason";
188 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
189 "Disassociated because QoS AP lacks sufficient bandwidth for this "
190 "QoS STA";
191 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
192 "Disassociated because excessive number of frames need to be "
193 "acknowledged, but are not acknowledged due to AP transmissions "
194 "and/or poor channel conditions";
195 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
196 "Disassociated because STA is transmitting outside the limits of its "
197 "TXOPs";
198 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
199 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
200 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
201 "Requested from peer STA as it does not want to use the mechanism";
202 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
203 "Requested from peer STA as the STA received frames using the "
204 "mechanism for which a setup is required";
205 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
206 "Requested from peer STA due to timeout";
207 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
208 "Peer STA does not support the requested cipher suite";
209 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
210 }
211
212 if (!status_code_string_) {
213 status_code_string_ = new map<uint16_t, string>;
214 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
215 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
216 "Unspecified failure";
217 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700218 "Cannot support all requested capabilities in the capability "
219 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700220 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700221 "Reassociation denied due to inability to confirm that association "
222 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700223 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700224 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700225 (*status_code_string_)[
226 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700227 "Responding station does not support the specified authentication "
228 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700229 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700230 "Received an authentication frame with authentication transaction "
231 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700232 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700233 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700234 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700235 "Authentication rejected due to timeout waiting for next frame in "
236 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700237 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
238 "Association denied because AP is unable to handle additional "
239 "associated STA";
240 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700241 "Association denied due to requesting station not supporting all of "
242 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700243 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700244 "Association denied due to requesting station not supporting the "
245 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700246 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700247 "Association denied due to requesting station not supporting the PBCC "
248 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700249 (*status_code_string_)[
250 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700251 "Association denied due to requesting station not supporting the "
252 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700253 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700254 "Association request rejected because Spectrum Management capability "
255 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700256 (*status_code_string_)[
257 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700258 "Association request rejected because the information in the Power "
259 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700260 (*status_code_string_)[
261 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700262 "Association request rejected because the information in the "
263 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700264 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700265 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700266 "supporting the Short Slot Time option";
267 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
268 "Association request rejected due to requesting station not "
269 "supporting the DSSS-OFDM option";
270 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700271 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700272 (*status_code_string_)[
273 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700274 "Association denied due to QAP having insufficient bandwidth to handle "
275 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700276 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700277 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700278 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
279 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700280 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700281 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
282 "The request has been declined";
283 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700284 "The request has not been successful as one or more parameters have "
285 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700286 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
287 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700288 "However, a suggested Tspec is provided so that the initiating QSTA "
289 "may attempt to send another TS with the suggested changes to the "
290 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700291 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700292 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700293 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
294 "Invalid Group Cipher";
295 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
296 "Invalid Pairwise Cipher";
297 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
298 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
299 "Unsupported RSN Information Element version";
300 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
301 "Invalid RSN Information Element Capabilities";
302 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700303 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700304 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
305 "The TS has not been created. However, the HC may be capable of "
306 "creating a TS, in response to a request, after the time indicated in "
307 "the TS Delay element";
308 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700309 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700310 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
311 "Destination STA is not present within this BSS";
312 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
313 "The destination STA is not a QoS STA";
314 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700315 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700316 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700317 }
318
319 return true;
320}
321
Wade Guthrie0d438532012-05-18 14:18:50 -0700322// Helper function to provide a string for a MAC address.
repo syncdc085c82012-12-28 08:54:41 -0800323bool Nl80211Message::GetMacAttributeString(nl80211_attrs id,
324 string *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700325 if (!value) {
326 LOG(ERROR) << "Null |value| parameter";
327 return false;
328 }
329
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800330 ByteString data;
repo sync12cca802012-12-19 17:34:22 -0800331 if (!attributes().GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700332 value->assign(kBogusMacAddress);
333 return false;
334 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800335 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700336
337 return true;
338}
339
340// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
repo syncdc085c82012-12-28 08:54:41 -0800341bool Nl80211Message::GetScanFrequenciesAttribute(
repo sync12cca802012-12-19 17:34:22 -0800342 nl80211_attrs id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700343 if (!value) {
344 LOG(ERROR) << "Null |value| parameter";
345 return false;
346 }
347
348 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800349 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800350 if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800351 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700352
repo syncd316eb72012-12-10 15:48:47 -0800353 nlattr *nst = NULL;
354 // |nla_for_each_attr| requires a non-const parameter even though it
355 // doesn't change the data.
356 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
357 int rem_nst;
358 int len = rawdata.GetLength();
359
360 nla_for_each_attr(nst, attr_data, len, rem_nst) {
361 value->push_back(Nl80211Attribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700362 }
repo syncd316eb72012-12-10 15:48:47 -0800363 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700364}
365
366// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800367bool Nl80211Message::GetScanSsidsAttribute(
368 nl80211_attrs id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700369 if (!value) {
370 LOG(ERROR) << "Null |value| parameter";
371 return false;
372 }
373
repo sync90ee0fa2012-12-18 10:08:08 -0800374 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800375 if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty())
repo sync90ee0fa2012-12-18 10:08:08 -0800376 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700377
repo sync90ee0fa2012-12-18 10:08:08 -0800378 nlattr *nst = NULL;
379 // |nla_for_each_attr| requires a non-const parameter even though it
380 // doesn't change the data.
381 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
382 int rem_nst;
383 int len = rawdata.GetLength();
384
385 nla_for_each_attr(nst, data, len, rem_nst) {
386 value->push_back(StringFromSsid(nla_len(nst),
387 reinterpret_cast<const uint8_t *>(
388 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700389 }
repo sync90ee0fa2012-12-18 10:08:08 -0800390 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700391}
392
Wade Guthrie0d438532012-05-18 14:18:50 -0700393// Protected members.
394
repo syncdc085c82012-12-28 08:54:41 -0800395string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700396 char ifname[IF_NAMESIZE] = "";
397 uint32_t ifindex = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800398 bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
399 &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700400
401 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800402 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
403 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700404
405 string output;
406 if (ifindex_exists && wifi_exists) {
407 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
408 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
409 wifi);
410 } else if (ifindex_exists) {
411 StringAppendF(&output, "%s: ",
412 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
413 } else if (wifi_exists) {
414 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
415 }
416
417 return output;
418}
419
repo syncdc085c82012-12-28 08:54:41 -0800420string Nl80211Message::StringFromFrame(nl80211_attrs attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700421 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800422 ByteString frame_data;
repo sync90ee0fa2012-12-18 10:08:08 -0800423 if (attributes().GetRawAttributeValue(attr_name,
424 &frame_data) && !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800425 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700426 frame.ToString(&output);
427 } else {
428 output.append(" [no frame]");
429 }
430
431 return output;
432}
433
434// static
repo syncdc085c82012-12-28 08:54:41 -0800435string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700436 switch (key_type) {
437 case NL80211_KEYTYPE_GROUP:
438 return "Group";
439 case NL80211_KEYTYPE_PAIRWISE:
440 return "Pairwise";
441 case NL80211_KEYTYPE_PEERKEY:
442 return "PeerKey";
443 default:
444 return "<Unknown Key Type>";
445 }
446}
447
448// static
repo syncdc085c82012-12-28 08:54:41 -0800449string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700450 string output;
451
452 if (!arg) {
453 output = kBogusMacAddress;
454 LOG(ERROR) << "|arg| parameter is NULL.";
455 } else {
456 StringAppendF(&output, "%02x", arg[0]);
457
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800458 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700459 StringAppendF(&output, ":%02x", arg[i]);
460 }
461 }
462 return output;
463}
464
465// static
repo syncdc085c82012-12-28 08:54:41 -0800466string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700467 switch (initiator) {
468 case NL80211_REGDOM_SET_BY_CORE:
469 return "the wireless core upon initialization";
470 case NL80211_REGDOM_SET_BY_USER:
471 return "a user";
472 case NL80211_REGDOM_SET_BY_DRIVER:
473 return "a driver";
474 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
475 return "a country IE";
476 default:
477 return "<Unknown Reg Initiator>";
478 }
479}
480
481// static
repo syncdc085c82012-12-28 08:54:41 -0800482string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700483 const uint8_t *data) {
484 string output;
485 if (!data) {
486 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
487 LOG(ERROR) << "|data| parameter is NULL.";
488 return output;
489 }
490
491 for (int i = 0; i < len; ++i) {
492 if (data[i] == ' ')
493 output.append(" ");
494 else if (isprint(data[i]))
495 StringAppendF(&output, "%c", static_cast<char>(data[i]));
496 else
497 StringAppendF(&output, "\\x%2x", data[i]);
498 }
499
500 return output;
501}
502
503// static
repo syncdc085c82012-12-28 08:54:41 -0800504string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700505 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700506 match = reason_code_string_->find(status);
507 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700508 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700509 if (status < IEEE_80211::kReasonCodeMax) {
510 StringAppendF(&output, "<Reserved Reason:%u>", status);
511 } else {
512 StringAppendF(&output, "<Unknown Reason:%u>", status);
513 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700514 return output;
515 }
516 return match->second;
517}
518
Wade Guthried4977f22012-08-22 12:37:54 -0700519// static
repo syncdc085c82012-12-28 08:54:41 -0800520string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700521 map<uint16_t, string>::const_iterator match;
522 match = status_code_string_->find(status);
523 if (match == status_code_string_->end()) {
524 string output;
525 if (status < IEEE_80211::kStatusCodeMax) {
526 StringAppendF(&output, "<Reserved Status:%u>", status);
527 } else {
528 StringAppendF(&output, "<Unknown Status:%u>", status);
529 }
530 return output;
531 }
532 return match->second;
533}
534
repo syncdc085c82012-12-28 08:54:41 -0800535string Nl80211Message::GenericToString() const {
repo sync1538d442012-12-20 15:24:35 -0800536 string output;
repo syncdc085c82012-12-28 08:54:41 -0800537 StringAppendF(&output, "Message %s (%d)\n",
repo sync1538d442012-12-20 15:24:35 -0800538 message_type_string(), message_type());
539 StringAppendF(&output, "%s", attributes_.ToString().c_str());
540 return output;
541}
542
repo syncdc085c82012-12-28 08:54:41 -0800543ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const {
544 // Build netlink header.
545 nlmsghdr header;
546 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
547 header.nlmsg_len = nlmsghdr_with_pad;
548 header.nlmsg_type = nlmsg_type;
549 header.nlmsg_flags = NLM_F_REQUEST;
550 header.nlmsg_seq = sequence_number();
551 header.nlmsg_pid = getpid();
552
553 // Build genl message header.
554 genlmsghdr genl_header;
555 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
556 header.nlmsg_len += genlmsghdr_with_pad;
557 genl_header.cmd = message_type();
558 genl_header.version = 1;
559 genl_header.reserved = 0;
560
561 // Assemble attributes (padding is included by AttributeList::Encode).
562 ByteString attributes = attributes_.Encode();
563 header.nlmsg_len += attributes.GetLength();
564
565 // Now that we know the total message size, build the output ByteString.
566 ByteString result;
567
568 // Netlink header + pad.
569 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
570 sizeof(header)));
571 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
572
573 // Genl message header + pad.
574 result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header),
575 sizeof(genl_header)));
576 result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill.
577
578 // Attributes including pad.
579 result.Append(attributes);
580
581 return result;
582}
583
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800584Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700585 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800586 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700587 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800588 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
589 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700590
591 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800592 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700593 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800594 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
595 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700596 frame_type_ = frame->frame_control & kFrameTypeMask;
597
598 switch (frame_type_) {
599 case kAssocResponseFrameType:
600 case kReassocResponseFrameType:
601 status_ = le16toh(frame->u.associate_response.status_code);
602 break;
603
604 case kAuthFrameType:
605 status_ = le16toh(frame->u.authentiate_message.status_code);
606 break;
607
608 case kDisassocFrameType:
609 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700610 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700611 break;
612
613 default:
614 break;
615 }
616 }
617}
618
Wade Guthried4977f22012-08-22 12:37:54 -0700619bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700620 if (!output) {
621 LOG(ERROR) << "NULL |output|";
622 return false;
623 }
624
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800625 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700626 output->append(" [no frame]");
627 return true;
628 }
629
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800630 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700631 output->append(" [invalid frame: ");
632 } else {
633 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
634
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800635 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700636 case kAssocResponseFrameType:
637 StringAppendF(output, "; AssocResponse status: %u: %s",
638 status_,
repo syncdc085c82012-12-28 08:54:41 -0800639 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700640 break;
641 case kReassocResponseFrameType:
642 StringAppendF(output, "; ReassocResponse status: %u: %s",
643 status_,
repo syncdc085c82012-12-28 08:54:41 -0800644 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700645 break;
646 case kAuthFrameType:
647 StringAppendF(output, "; Auth status: %u: %s",
648 status_,
repo syncdc085c82012-12-28 08:54:41 -0800649 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700650 break;
651
652 case kDisassocFrameType:
653 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700654 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800655 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700656 break;
657 case kDeauthFrameType:
658 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700659 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800660 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700661 break;
662
663 default:
664 break;
665 }
666 output->append(" [frame: ");
667 }
668
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800669 const unsigned char *frame = frame_.GetConstData();
670 for (size_t i = 0; i < frame_.GetLength(); ++i) {
671 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700672 }
673 output->append("]");
674
675 return true;
676}
677
Wade Guthried4977f22012-08-22 12:37:54 -0700678bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800679 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700680}
681
Wade Guthried4977f22012-08-22 12:37:54 -0700682
Wade Guthrie0d438532012-05-18 14:18:50 -0700683//
repo syncdc085c82012-12-28 08:54:41 -0800684// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700685//
686
687const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
688const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
689
690string AssociateMessage::ToString() const {
691 string output(GetHeaderString());
692 output.append("assoc");
repo sync90ee0fa2012-12-18 10:08:08 -0800693 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL))
Wade Guthrie0d438532012-05-18 14:18:50 -0700694 output.append(StringFromFrame(NL80211_ATTR_FRAME));
repo sync90ee0fa2012-12-18 10:08:08 -0800695 else if (attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT))
Wade Guthrie0d438532012-05-18 14:18:50 -0700696 output.append(": timed out");
697 else
698 output.append(": unknown event");
699 return output;
700}
701
702const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
703const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
704
705string AuthenticateMessage::ToString() const {
706 string output(GetHeaderString());
707 output.append("auth");
repo sync90ee0fa2012-12-18 10:08:08 -0800708 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700709 output.append(StringFromFrame(NL80211_ATTR_FRAME));
710 } else {
repo sync90ee0fa2012-12-18 10:08:08 -0800711 output.append(attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT) ?
Wade Guthrie0d438532012-05-18 14:18:50 -0700712 ": timed out" : ": unknown event");
713 }
714 return output;
715}
716
717const uint8_t CancelRemainOnChannelMessage::kCommand =
718 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
719const char CancelRemainOnChannelMessage::kCommandString[] =
720 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
721
722string CancelRemainOnChannelMessage::ToString() const {
723 string output(GetHeaderString());
724 uint32_t freq;
725 uint64_t cookie;
726 StringAppendF(&output,
727 "done with remain on freq %" PRIu32 " (cookie %" PRIx64 ")",
repo sync90ee0fa2012-12-18 10:08:08 -0800728 (attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ,
729 &freq) ? 0 : freq),
730 (attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE,
731 &cookie) ? 0 : cookie));
Wade Guthrie0d438532012-05-18 14:18:50 -0700732 return output;
733}
734
735const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
736const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
737
738string ConnectMessage::ToString() const {
739 string output(GetHeaderString());
740
741 uint16_t status = UINT16_MAX;
742
repo sync90ee0fa2012-12-18 10:08:08 -0800743 if (!attributes().GetU16AttributeValue(NL80211_ATTR_STATUS_CODE, &status)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700744 output.append("unknown connect status");
745 } else if (status == 0) {
746 output.append("connected");
747 } else {
748 output.append("failed to connect");
749 }
750
repo sync90ee0fa2012-12-18 10:08:08 -0800751 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700752 string mac;
753 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
754 StringAppendF(&output, " to %s", mac.c_str());
755 }
756 if (status)
757 StringAppendF(&output, ", status: %u: %s", status,
758 StringFromStatus(status).c_str());
759 return output;
760}
761
762const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
763const char DeauthenticateMessage::kCommandString[] =
764 "NL80211_CMD_DEAUTHENTICATE";
765
766string DeauthenticateMessage::ToString() const {
767 string output(GetHeaderString());
768 StringAppendF(&output, "deauth%s",
769 StringFromFrame(NL80211_ATTR_FRAME).c_str());
770 return output;
771}
772
773const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
774const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
775
776string DeleteStationMessage::ToString() const {
777 string mac;
778 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
779 string output(GetHeaderString());
780 StringAppendF(&output, "del station %s", mac.c_str());
781 return output;
782}
783
784const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
785const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
786
787string DisassociateMessage::ToString() const {
788 string output(GetHeaderString());
789 StringAppendF(&output, "disassoc%s",
790 StringFromFrame(NL80211_ATTR_FRAME).c_str());
791 return output;
792}
793
794const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
795const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
796
797string DisconnectMessage::ToString() const {
798 string output(GetHeaderString());
799 StringAppendF(&output, "disconnected %s",
repo sync90ee0fa2012-12-18 10:08:08 -0800800 ((attributes().IsFlagAttributeTrue(
801 NL80211_ATTR_DISCONNECTED_BY_AP)) ?
802 "(by AP)" : "(local request)"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700803
804 uint16_t reason = UINT16_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800805 if (attributes().GetU16AttributeValue(NL80211_ATTR_REASON_CODE, &reason)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700806 StringAppendF(&output, " reason: %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700807 reason, StringFromReason(reason).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700808 }
809 return output;
810}
811
812const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
813const char FrameTxStatusMessage::kCommandString[] =
814 "NL80211_CMD_FRAME_TX_STATUS";
815
816string FrameTxStatusMessage::ToString() const {
817 string output(GetHeaderString());
818 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800819 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -0700820
repo sync90ee0fa2012-12-18 10:08:08 -0800821 StringAppendF(&output, "mgmt TX status (cookie %" PRIx64 "): %s", cookie,
822 (attributes().IsFlagAttributeTrue(NL80211_ATTR_ACK) ?
823 "acked" : "no ack"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700824 return output;
825}
826
827const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
828const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
829
830string JoinIbssMessage::ToString() const {
831 string mac;
832 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
833 string output(GetHeaderString());
834 StringAppendF(&output, "IBSS %s joined", mac.c_str());
835 return output;
836}
837
838const uint8_t MichaelMicFailureMessage::kCommand =
839 NL80211_CMD_MICHAEL_MIC_FAILURE;
840const char MichaelMicFailureMessage::kCommandString[] =
841 "NL80211_CMD_MICHAEL_MIC_FAILURE";
842
843string MichaelMicFailureMessage::ToString() const {
844 string output(GetHeaderString());
845
846 output.append("Michael MIC failure event:");
847
repo sync90ee0fa2012-12-18 10:08:08 -0800848 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700849 string mac;
850 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
851 StringAppendF(&output, " source MAC address %s", mac.c_str());
852 }
853
repo sync90ee0fa2012-12-18 10:08:08 -0800854 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800855 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800856 if (attributes().GetRawAttributeValue(NL80211_ATTR_KEY_SEQ,
857 &rawdata) &&
repo syncdc085c82012-12-28 08:54:41 -0800858 rawdata.GetLength() == Nl80211Message::kEthernetAddressBytes) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800859 const unsigned char *seq = rawdata.GetConstData();
Wade Guthrie0d438532012-05-18 14:18:50 -0700860 StringAppendF(&output, " seq=%02x%02x%02x%02x%02x%02x",
861 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
862 }
863 }
864 uint32_t key_type_val = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800865 if (attributes().GetU32AttributeValue(NL80211_ATTR_KEY_TYPE, &key_type_val)) {
repo sync12cca802012-12-19 17:34:22 -0800866 nl80211_key_type key_type = static_cast<nl80211_key_type >(key_type_val);
Wade Guthrie0d438532012-05-18 14:18:50 -0700867 StringAppendF(&output, " Key Type %s", StringFromKeyType(key_type).c_str());
868 }
869
870 uint8_t key_index = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800871 if (attributes().GetU8AttributeValue(NL80211_ATTR_KEY_IDX, &key_index)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700872 StringAppendF(&output, " Key Id %u", key_index);
873 }
874
875 return output;
876}
877
878const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
879const char NewScanResultsMessage::kCommandString[] =
880 "NL80211_CMD_NEW_SCAN_RESULTS";
881
882string NewScanResultsMessage::ToString() const {
883 string output(GetHeaderString());
884 output.append("scan finished");
885
886 {
887 output.append("; frequencies: ");
888 vector<uint32_t> list;
889 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
890 string str;
891 for (vector<uint32_t>::const_iterator i = list.begin();
892 i != list.end(); ++i) {
893 StringAppendF(&str, " %" PRIu32 ", ", *i);
894 }
895 output.append(str);
896 }
897 }
898
899 {
900 output.append("; SSIDs: ");
901 vector<string> list;
902 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
903 string str;
904 for (vector<string>::const_iterator i = list.begin();
905 i != list.end(); ++i) {
906 StringAppendF(&str, "\"%s\", ", i->c_str());
907 }
908 output.append(str);
909 }
910 }
911
912 return output;
913}
914
915const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
916const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
917
918string NewStationMessage::ToString() const {
919 string mac;
920 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
921 string output(GetHeaderString());
922 StringAppendF(&output, "new station %s", mac.c_str());
923
924 return output;
925}
926
927const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
928const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
929
930string NewWifiMessage::ToString() const {
931 string output(GetHeaderString());
932 string wifi_name = "None";
repo sync90ee0fa2012-12-18 10:08:08 -0800933 attributes().GetStringAttributeValue(NL80211_ATTR_WIPHY_NAME, &wifi_name);
Wade Guthrie0d438532012-05-18 14:18:50 -0700934 StringAppendF(&output, "renamed to %s", wifi_name.c_str());
935 return output;
936}
937
938const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
939const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
940
941string NotifyCqmMessage::ToString() const {
repo sync90ee0fa2012-12-18 10:08:08 -0800942 // TODO(wdg): use attributes().GetNestedAttributeValue()...
repo sync12cca802012-12-19 17:34:22 -0800943 static const nla_policy kCqmValidationPolicy[NL80211_ATTR_CQM_MAX + 1] = {
Wade Guthrie0d438532012-05-18 14:18:50 -0700944 { NLA_U32, 0, 0 }, // Who Knows?
945 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
946 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
947 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
948 };
949
950 string output(GetHeaderString());
951 output.append("connection quality monitor event: ");
952
repo sync90ee0fa2012-12-18 10:08:08 -0800953 const Nl80211RawAttribute *attribute =
954 attributes().GetRawAttribute(NL80211_ATTR_CQM);
repo syncd316eb72012-12-10 15:48:47 -0800955 if (!attribute) {
956 output.append("missing data!");
957 return output;
958 }
959
960 const nlattr *const_data = attribute->data();
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800961 // Note that |nla_parse_nested| doesn't change |const_data| but doesn't
962 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -0700963 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
964
965 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
repo sync12cca802012-12-19 17:34:22 -0800966 if (!cqm_attr ||
967 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
968 const_cast<nla_policy *>(kCqmValidationPolicy))) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700969 output.append("missing data!");
970 return output;
971 }
972 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
repo sync12cca802012-12-19 17:34:22 -0800973 nl80211_cqm_rssi_threshold_event rssi_event =
974 static_cast<nl80211_cqm_rssi_threshold_event>(
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800975 Nl80211Attribute::NlaGetU32(
976 cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -0700977 if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
978 output.append("RSSI went above threshold");
979 else
980 output.append("RSSI went below threshold");
981 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
repo sync90ee0fa2012-12-18 10:08:08 -0800982 attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700983 string mac;
984 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
985 StringAppendF(&output, "peer %s didn't ACK %" PRIu32 " packets",
986 mac.c_str(),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800987 Nl80211Attribute::NlaGetU32(
988 cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -0700989 } else {
990 output.append("unknown event");
991 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700992 return output;
993}
994
995const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
996const char PmksaCandidateMessage::kCommandString[] =
997 "NL80211_ATTR_PMKSA_CANDIDATE";
998
999string PmksaCandidateMessage::ToString() const {
1000 string output(GetHeaderString());
1001 output.append("PMKSA candidate found");
1002 return output;
1003}
1004
1005const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
1006const char RegBeaconHintMessage::kCommandString[] =
1007 "NL80211_CMD_REG_BEACON_HINT";
1008
1009string RegBeaconHintMessage::ToString() const {
1010 string output(GetHeaderString());
1011 uint32_t wiphy_idx = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001012 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, &wiphy_idx);
Wade Guthrie0d438532012-05-18 14:18:50 -07001013
repo sync90ee0fa2012-12-18 10:08:08 -08001014 const Nl80211RawAttribute *freq_before =
1015 attributes().GetRawAttribute(NL80211_ATTR_FREQ_BEFORE);
repo syncd316eb72012-12-10 15:48:47 -08001016 if (!freq_before)
1017 return "";
1018 const nlattr *const_before = freq_before->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001019 ieee80211_beacon_channel chan_before_beacon;
1020 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
1021 if (ParseBeaconHintChan(const_before, &chan_before_beacon))
1022 return "";
1023
repo sync90ee0fa2012-12-18 10:08:08 -08001024 const Nl80211RawAttribute *freq_after =
1025 attributes().GetRawAttribute(NL80211_ATTR_FREQ_AFTER);
repo syncd316eb72012-12-10 15:48:47 -08001026 if (!freq_after)
1027 return "";
1028
1029 const nlattr *const_after = freq_after->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001030 ieee80211_beacon_channel chan_after_beacon;
1031 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
1032 if (ParseBeaconHintChan(const_after, &chan_after_beacon))
1033 return "";
1034
1035 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
1036 return "";
1037
1038 /* A beacon hint is sent _only_ if something _did_ change */
1039 output.append("beacon hint:");
1040 StringAppendF(&output, "phy%" PRIu32 " %u MHz [%d]:",
1041 wiphy_idx, chan_before_beacon.center_freq,
1042 ChannelFromIeee80211Frequency(chan_before_beacon.center_freq));
1043
1044 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
1045 output.append("\to active scanning enabled");
1046 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
1047 output.append("\to beaconing enabled");
1048 return output;
1049}
1050
1051int RegBeaconHintMessage::ParseBeaconHintChan(const nlattr *tb,
1052 ieee80211_beacon_channel *chan)
1053 const {
1054 static const nla_policy kBeaconFreqPolicy[
1055 NL80211_FREQUENCY_ATTR_MAX + 1] = {
1056 {0, 0, 0},
1057 { NLA_U32, 0, 0 }, // [NL80211_FREQUENCY_ATTR_FREQ]
1058 {0, 0, 0},
1059 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]
1060 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_NO_IBSS]
1061 };
1062
1063 if (!tb) {
1064 LOG(ERROR) << "|tb| parameter is NULL.";
1065 return -EINVAL;
1066 }
1067
1068 if (!chan) {
1069 LOG(ERROR) << "|chan| parameter is NULL.";
1070 return -EINVAL;
1071 }
1072
1073 nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1074
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001075 // Note that |nla_parse_nested| doesn't change its parameters but doesn't
1076 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001077 if (nla_parse_nested(tb_freq,
1078 NL80211_FREQUENCY_ATTR_MAX,
1079 const_cast<nlattr *>(tb),
1080 const_cast<nla_policy *>(kBeaconFreqPolicy)))
1081 return -EINVAL;
1082
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001083 chan->center_freq = Nl80211Attribute::NlaGetU32(
1084 tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
Wade Guthrie0d438532012-05-18 14:18:50 -07001085
1086 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1087 chan->passive_scan = true;
1088 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1089 chan->no_ibss = true;
1090
1091 return 0;
1092}
1093
1094int RegBeaconHintMessage::ChannelFromIeee80211Frequency(int freq) {
1095 // TODO(wdg): get rid of these magic numbers.
1096 if (freq == 2484)
1097 return 14;
1098
1099 if (freq < 2484)
1100 return (freq - 2407) / 5;
1101
1102 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
1103 return freq/5 - 1000;
1104}
1105
1106const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
1107const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
1108
1109string RegChangeMessage::ToString() const {
1110 string output(GetHeaderString());
1111 output.append("regulatory domain change: ");
1112
1113 uint8_t reg_type = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001114 attributes().GetU8AttributeValue(NL80211_ATTR_REG_TYPE, &reg_type);
Wade Guthrie0d438532012-05-18 14:18:50 -07001115
1116 uint32_t initiator = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001117 attributes().GetU32AttributeValue(NL80211_ATTR_REG_INITIATOR, &initiator);
Wade Guthrie0d438532012-05-18 14:18:50 -07001118
1119 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001120 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
1121 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -07001122
1123 string alpha2 = "<None>";
repo sync90ee0fa2012-12-18 10:08:08 -08001124 attributes().GetStringAttributeValue(NL80211_ATTR_REG_ALPHA2, &alpha2);
Wade Guthrie0d438532012-05-18 14:18:50 -07001125
1126 switch (reg_type) {
1127 case NL80211_REGDOM_TYPE_COUNTRY:
1128 StringAppendF(&output, "set to %s by %s request",
1129 alpha2.c_str(), StringFromRegInitiator(initiator).c_str());
1130 if (wifi_exists)
1131 StringAppendF(&output, " on phy%" PRIu32, wifi);
1132 break;
1133
1134 case NL80211_REGDOM_TYPE_WORLD:
1135 StringAppendF(&output, "set to world roaming by %s request",
1136 StringFromRegInitiator(initiator).c_str());
1137 break;
1138
1139 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
1140 StringAppendF(&output,
1141 "custom world roaming rules in place on phy%" PRIu32
1142 " by %s request",
1143 wifi, StringFromRegInitiator(initiator).c_str());
1144 break;
1145
1146 case NL80211_REGDOM_TYPE_INTERSECTION:
1147 StringAppendF(&output, "intersection used due to a request made by %s",
1148 StringFromRegInitiator(initiator).c_str());
1149 if (wifi_exists)
1150 StringAppendF(&output, " on phy%" PRIu32, wifi);
1151 break;
1152
1153 default:
1154 output.append("unknown source");
1155 break;
1156 }
1157 return output;
1158}
1159
1160const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
1161const char RemainOnChannelMessage::kCommandString[] =
1162 "NL80211_CMD_REMAIN_ON_CHANNEL";
1163
1164string RemainOnChannelMessage::ToString() const {
1165 string output(GetHeaderString());
1166
1167 uint32_t wifi_freq = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001168 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ, &wifi_freq);
Wade Guthrie0d438532012-05-18 14:18:50 -07001169
1170 uint32_t duration = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001171 attributes().GetU32AttributeValue(NL80211_ATTR_DURATION, &duration);
Wade Guthrie0d438532012-05-18 14:18:50 -07001172
1173 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001174 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -07001175
1176 StringAppendF(&output, "remain on freq %" PRIu32 " (%" PRIu32 "ms, cookie %"
1177 PRIx64 ")",
1178 wifi_freq, duration, cookie);
1179 return output;
1180}
1181
1182const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
1183const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
1184
1185string RoamMessage::ToString() const {
1186 string output(GetHeaderString());
1187 output.append("roamed");
1188
repo sync90ee0fa2012-12-18 10:08:08 -08001189 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001190 string mac;
1191 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1192 StringAppendF(&output, " to %s", mac.c_str());
1193 }
1194 return output;
1195}
1196
1197const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
1198const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
1199
1200string ScanAbortedMessage::ToString() const {
1201 string output(GetHeaderString());
1202 output.append("scan aborted");
1203
1204 {
1205 output.append("; frequencies: ");
1206 vector<uint32_t> list;
1207 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1208 string str;
1209 for (vector<uint32_t>::const_iterator i = list.begin();
1210 i != list.end(); ++i) {
1211 StringAppendF(&str, " %" PRIu32 ", ", *i);
1212 }
1213 output.append(str);
1214 }
1215 }
1216
1217 {
1218 output.append("; SSIDs: ");
1219 vector<string> list;
1220 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1221 string str;
1222 for (vector<string>::const_iterator i = list.begin();
1223 i != list.end(); ++i) {
1224 StringAppendF(&str, "\"%s\", ", i->c_str());
1225 }
1226 output.append(str);
1227 }
1228 }
1229
1230 return output;
1231}
1232
1233const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
1234const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
1235
1236string TriggerScanMessage::ToString() const {
1237 string output(GetHeaderString());
1238 output.append("scan started");
1239
1240 {
1241 output.append("; frequencies: ");
1242 vector<uint32_t> list;
1243 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1244 string str;
1245 for (vector<uint32_t>::const_iterator i = list.begin();
1246 i != list.end(); ++i) {
1247 StringAppendF(&str, "%" PRIu32 ", ", *i);
1248 }
1249 output.append(str);
1250 }
1251 }
1252
1253 {
1254 output.append("; SSIDs: ");
1255 vector<string> list;
1256 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1257 string str;
1258 for (vector<string>::const_iterator i = list.begin();
1259 i != list.end(); ++i) {
1260 StringAppendF(&str, "\"%s\", ", i->c_str());
1261 }
1262 output.append(str);
1263 }
1264 }
1265
1266 return output;
1267}
1268
1269const uint8_t UnknownMessage::kCommand = 0xff;
1270const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
1271
1272string UnknownMessage::ToString() const {
1273 string output(GetHeaderString());
1274 StringAppendF(&output, "unknown event %u", command_);
1275 return output;
1276}
1277
1278const uint8_t UnprotDeauthenticateMessage::kCommand =
1279 NL80211_CMD_UNPROT_DEAUTHENTICATE;
1280const char UnprotDeauthenticateMessage::kCommandString[] =
1281 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
1282
1283string UnprotDeauthenticateMessage::ToString() const {
1284 string output(GetHeaderString());
1285 StringAppendF(&output, "unprotected deauth %s",
1286 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1287 return output;
1288}
1289
1290const uint8_t UnprotDisassociateMessage::kCommand =
1291 NL80211_CMD_UNPROT_DISASSOCIATE;
1292const char UnprotDisassociateMessage::kCommandString[] =
1293 "NL80211_CMD_UNPROT_DISASSOCIATE";
1294
1295string UnprotDisassociateMessage::ToString() const {
1296 string output(GetHeaderString());
1297 StringAppendF(&output, "unprotected disassoc %s",
1298 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1299 return output;
1300}
1301
1302//
1303// Factory class.
1304//
1305
repo syncdc085c82012-12-28 08:54:41 -08001306Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001307 if (!msg) {
1308 LOG(ERROR) << "NULL |msg| parameter";
1309 return NULL;
1310 }
1311
repo syncdc085c82012-12-28 08:54:41 -08001312 scoped_ptr<Nl80211Message> message;
1313
Wade Guthrie0d438532012-05-18 14:18:50 -07001314 genlmsghdr *gnlh =
1315 reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
1316
1317 if (!gnlh) {
1318 LOG(ERROR) << "NULL gnlh";
1319 return NULL;
1320 }
1321
1322 switch (gnlh->cmd) {
1323 case AssociateMessage::kCommand:
1324 message.reset(new AssociateMessage()); break;
1325 case AuthenticateMessage::kCommand:
1326 message.reset(new AuthenticateMessage()); break;
1327 case CancelRemainOnChannelMessage::kCommand:
1328 message.reset(new CancelRemainOnChannelMessage()); break;
1329 case ConnectMessage::kCommand:
1330 message.reset(new ConnectMessage()); break;
1331 case DeauthenticateMessage::kCommand:
1332 message.reset(new DeauthenticateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001333 case DeleteStationMessage::kCommand:
1334 message.reset(new DeleteStationMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001335 case DisassociateMessage::kCommand:
1336 message.reset(new DisassociateMessage()); break;
1337 case DisconnectMessage::kCommand:
1338 message.reset(new DisconnectMessage()); break;
1339 case FrameTxStatusMessage::kCommand:
1340 message.reset(new FrameTxStatusMessage()); break;
1341 case JoinIbssMessage::kCommand:
1342 message.reset(new JoinIbssMessage()); break;
1343 case MichaelMicFailureMessage::kCommand:
1344 message.reset(new MichaelMicFailureMessage()); break;
1345 case NewScanResultsMessage::kCommand:
1346 message.reset(new NewScanResultsMessage()); break;
1347 case NewStationMessage::kCommand:
1348 message.reset(new NewStationMessage()); break;
1349 case NewWifiMessage::kCommand:
1350 message.reset(new NewWifiMessage()); break;
1351 case NotifyCqmMessage::kCommand:
1352 message.reset(new NotifyCqmMessage()); break;
1353 case PmksaCandidateMessage::kCommand:
1354 message.reset(new PmksaCandidateMessage()); break;
1355 case RegBeaconHintMessage::kCommand:
1356 message.reset(new RegBeaconHintMessage()); break;
1357 case RegChangeMessage::kCommand:
1358 message.reset(new RegChangeMessage()); break;
1359 case RemainOnChannelMessage::kCommand:
1360 message.reset(new RemainOnChannelMessage()); break;
1361 case RoamMessage::kCommand:
1362 message.reset(new RoamMessage()); break;
1363 case ScanAbortedMessage::kCommand:
1364 message.reset(new ScanAbortedMessage()); break;
1365 case TriggerScanMessage::kCommand:
1366 message.reset(new TriggerScanMessage()); break;
1367 case UnprotDeauthenticateMessage::kCommand:
1368 message.reset(new UnprotDeauthenticateMessage()); break;
1369 case UnprotDisassociateMessage::kCommand:
1370 message.reset(new UnprotDisassociateMessage()); break;
1371
1372 default:
1373 message.reset(new UnknownMessage(gnlh->cmd)); break;
1374 break;
1375 }
1376
1377 nlattr *tb[NL80211_ATTR_MAX + 1];
1378
1379 // Parse the attributes from the nl message payload (which starts at the
1380 // header) into the 'tb' array.
1381 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001382 genlmsg_attrlen(gnlh, 0), NULL);
Wade Guthrie0d438532012-05-18 14:18:50 -07001383
1384 if (!message->Init(tb, msg)) {
1385 LOG(ERROR) << "Message did not initialize properly";
1386 return NULL;
1387 }
1388
Wade Guthrie0d438532012-05-18 14:18:50 -07001389 return message.release();
1390}
1391
repo syncdc085c82012-12-28 08:54:41 -08001392Nl80211MessageDataCollector *
1393 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001394 return g_datacollector.Pointer();
1395}
1396
repo syncdc085c82012-12-28 08:54:41 -08001397Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001398 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
1399 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
1400 need_to_print[NL80211_CMD_DEL_STATION] = true;
1401 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
1402 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
1403 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
1404 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
1405 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
1406 need_to_print[NL80211_CMD_REG_CHANGE] = true;
1407 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
1408 need_to_print[NL80211_CMD_ROAM] = true;
1409 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
1410 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
1411 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
1412}
1413
repo syncdc085c82012-12-28 08:54:41 -08001414void Nl80211MessageDataCollector::CollectDebugData(
1415 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001416 if (!msg) {
1417 LOG(ERROR) << "NULL |msg| parameter";
1418 return;
1419 }
1420
1421 bool doit = false;
1422
1423 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -08001424 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -07001425 if (node != need_to_print.end())
1426 doit = node->second;
1427
1428 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001429 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001430 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001431 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001432
Christopher Wileyefd521f2012-11-07 17:32:46 -08001433 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001434
1435 size_t bytes = nlmsg_total_size(payload_bytes);
1436 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001437 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001438 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001439 << std::hex << std::setfill('0') << std::setw(2)
1440 << + rawdata[i] << ",";
1441 }
Wade Guthried6153612012-08-23 11:36:14 -07001442 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001443 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001444 }
1445}
1446
1447} // namespace shill.