blob: 9afaeaf4685d45428003b6b254ef216cddd0de57 [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
25#include "shill/user_bound_nlmessage.h"
26
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 sync1538d442012-12-20 15:24:35 -080050#include "shill/nl80211_attribute.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070051#include "shill/scope_logger.h"
52
53using base::LazyInstance;
54using base::StringAppendF;
55using base::StringPrintf;
56using std::map;
57using std::string;
58using std::vector;
59
60namespace shill {
61
62namespace {
63LazyInstance<UserBoundNlMessageDataCollector> g_datacollector =
64 LAZY_INSTANCE_INITIALIZER;
65} // namespace
66
Wade Guthrie0d438532012-05-18 14:18:50 -070067const char UserBoundNlMessage::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX";
68
69const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
70const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
71
72const uint32_t UserBoundNlMessage::kIllegalMessage = 0xFFFFFFFF;
Wade Guthrie8343f7f2012-12-04 13:52:32 -080073const unsigned int UserBoundNlMessage::kEthernetAddressBytes = 6;
Wade Guthried4977f22012-08-22 12:37:54 -070074map<uint16_t, string> *UserBoundNlMessage::reason_code_string_ = NULL;
75map<uint16_t, string> *UserBoundNlMessage::status_code_string_ = NULL;
Wade Guthrie0d438532012-05-18 14:18:50 -070076
77// The nl messages look like this:
78//
79// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
80// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
81// +-- gnhl nlmsg_tail(hdr) --+
82// | nlmsg_next(hdr) --+
83// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
84// -----+-----+-+----------------------------------------------+-++----
85// ... | | | payload | ||
86// | | +------+-+--------+-+--------------------------+ ||
87// | nl | | | | | | attribs | ||
88// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
89// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
90// | |d| hdr |d| |d| attr |a| load |a| ... |d||
91// | | | | | | | |d| |d| | ||
92// -----+-----+-+----------------------------------------------+-++----
93// ^ ^ ^ ^
94// | | | XXXXXXX <-- nla_len(nlattr)
95// | | | +-- nla_data(nlattr)
96// | | X-nla_total_size-X
97// | | XXXXX-nlmsg_attrlen-XXXXXX
98// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
99// +-- msg = nlmsg_hdr(raw_message)
100
101//
102// UserBoundNlMessage
103//
104
Wade Guthrie0d438532012-05-18 14:18:50 -0700105bool UserBoundNlMessage::Init(nlattr *tb[NL80211_ATTR_MAX + 1],
106 nlmsghdr *msg) {
107 if (!tb) {
108 LOG(ERROR) << "Null |tb| parameter";
109 return false;
110 }
111
112 message_ = msg;
113
114 SLOG(WiFi, 6) << "NL Message " << GetId() << " <===";
115
116 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
117 if (tb[i]) {
repo sync12cca802012-12-19 17:34:22 -0800118 attributes_.CreateAndInitFromNlAttr(static_cast<nl80211_attrs>(i), tb[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700119 }
120 }
121
122 // Convert integer values provided by libnl (for example, from the
123 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
124 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700125 if (!reason_code_string_) {
126 reason_code_string_ = new map<uint16_t, string>;
127 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
128 "Unspecified reason";
129 (*reason_code_string_)[
130 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700131 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700132 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
133 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
134 "ESS";
135 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
136 "Disassociated due to inactivity";
137 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
138 "Disassociated because AP is unable to handle all currently associated "
139 "STAs";
140 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
141 "Class 2 frame received from nonauthenticated STA";
142 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
143 "Class 3 frame received from nonassociated STA";
144 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
145 "Disassociated because sending STA is leaving (or has left) BSS";
146 (*reason_code_string_)[
147 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
148 "STA requesting (re)association is not authenticated with responding "
149 "STA";
150 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
151 "Disassociated because the information in the Power Capability "
152 "element is unacceptable";
153 (*reason_code_string_)[
154 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
155 "Disassociated because the information in the Supported Channels "
156 "element is unacceptable";
157 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
158 "Invalid information element, i.e., an information element defined in "
159 "this standard for which the content does not meet the specifications "
160 "in Clause 7";
161 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
162 "Message integrity code (MIC) failure";
163 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
164 "4-Way Handshake timeout";
165 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
166 "Group Key Handshake timeout";
167 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
168 "Information element in 4-Way Handshake different from "
169 "(Re)Association Request/Probe Response/Beacon frame";
170 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
171 "Invalid group cipher";
172 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
173 "Invalid pairwise cipher";
174 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
175 "Invalid AKMP";
176 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
177 "Unsupported RSN information element version";
178 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
179 "Invalid RSN information element capabilities";
180 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
181 "IEEE 802.1X authentication failed";
182 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
183 "Cipher suite rejected because of the security policy";
184 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
185 "Disassociated for unspecified, QoS-related reason";
186 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
187 "Disassociated because QoS AP lacks sufficient bandwidth for this "
188 "QoS STA";
189 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
190 "Disassociated because excessive number of frames need to be "
191 "acknowledged, but are not acknowledged due to AP transmissions "
192 "and/or poor channel conditions";
193 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
194 "Disassociated because STA is transmitting outside the limits of its "
195 "TXOPs";
196 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
197 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
198 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
199 "Requested from peer STA as it does not want to use the mechanism";
200 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
201 "Requested from peer STA as the STA received frames using the "
202 "mechanism for which a setup is required";
203 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
204 "Requested from peer STA due to timeout";
205 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
206 "Peer STA does not support the requested cipher suite";
207 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
208 }
209
210 if (!status_code_string_) {
211 status_code_string_ = new map<uint16_t, string>;
212 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
213 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
214 "Unspecified failure";
215 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700216 "Cannot support all requested capabilities in the capability "
217 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700218 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700219 "Reassociation denied due to inability to confirm that association "
220 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700221 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700222 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700223 (*status_code_string_)[
224 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700225 "Responding station does not support the specified authentication "
226 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700227 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700228 "Received an authentication frame with authentication transaction "
229 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700230 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700231 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700232 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700233 "Authentication rejected due to timeout waiting for next frame in "
234 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700235 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
236 "Association denied because AP is unable to handle additional "
237 "associated STA";
238 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700239 "Association denied due to requesting station not supporting all of "
240 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700241 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700242 "Association denied due to requesting station not supporting the "
243 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700244 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700245 "Association denied due to requesting station not supporting the PBCC "
246 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700247 (*status_code_string_)[
248 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700249 "Association denied due to requesting station not supporting the "
250 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700251 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700252 "Association request rejected because Spectrum Management capability "
253 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700254 (*status_code_string_)[
255 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700256 "Association request rejected because the information in the Power "
257 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700258 (*status_code_string_)[
259 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700260 "Association request rejected because the information in the "
261 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700262 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700263 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700264 "supporting the Short Slot Time option";
265 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
266 "Association request rejected due to requesting station not "
267 "supporting the DSSS-OFDM option";
268 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700269 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700270 (*status_code_string_)[
271 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700272 "Association denied due to QAP having insufficient bandwidth to handle "
273 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700274 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700275 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700276 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
277 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700278 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700279 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
280 "The request has been declined";
281 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700282 "The request has not been successful as one or more parameters have "
283 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700284 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
285 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700286 "However, a suggested Tspec is provided so that the initiating QSTA "
287 "may attempt to send another TS with the suggested changes to the "
288 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700289 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700290 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700291 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
292 "Invalid Group Cipher";
293 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
294 "Invalid Pairwise Cipher";
295 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
296 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
297 "Unsupported RSN Information Element version";
298 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
299 "Invalid RSN Information Element Capabilities";
300 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700301 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700302 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
303 "The TS has not been created. However, the HC may be capable of "
304 "creating a TS, in response to a request, after the time indicated in "
305 "the TS Delay element";
306 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700307 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700308 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
309 "Destination STA is not present within this BSS";
310 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
311 "The destination STA is not a QoS STA";
312 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700313 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700314 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700315 }
316
317 return true;
318}
319
Wade Guthrie0d438532012-05-18 14:18:50 -0700320uint32_t UserBoundNlMessage::GetId() const {
321 if (!message_) {
322 return kIllegalMessage;
323 }
324 return message_->nlmsg_seq;
325}
326
Wade Guthrie0d438532012-05-18 14:18:50 -0700327
328// Helper function to provide a string for a MAC address.
repo sync12cca802012-12-19 17:34:22 -0800329bool UserBoundNlMessage::GetMacAttributeString(nl80211_attrs id,
Wade Guthrie0d438532012-05-18 14:18:50 -0700330 string *value) const {
331 if (!value) {
332 LOG(ERROR) << "Null |value| parameter";
333 return false;
334 }
335
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800336 ByteString data;
repo sync12cca802012-12-19 17:34:22 -0800337 if (!attributes().GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700338 value->assign(kBogusMacAddress);
339 return false;
340 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800341 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700342
343 return true;
344}
345
346// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
347bool UserBoundNlMessage::GetScanFrequenciesAttribute(
repo sync12cca802012-12-19 17:34:22 -0800348 nl80211_attrs id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700349 if (!value) {
350 LOG(ERROR) << "Null |value| parameter";
351 return false;
352 }
353
354 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800355 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800356 if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800357 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700358
repo syncd316eb72012-12-10 15:48:47 -0800359 nlattr *nst = NULL;
360 // |nla_for_each_attr| requires a non-const parameter even though it
361 // doesn't change the data.
362 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
363 int rem_nst;
364 int len = rawdata.GetLength();
365
366 nla_for_each_attr(nst, attr_data, len, rem_nst) {
367 value->push_back(Nl80211Attribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700368 }
repo syncd316eb72012-12-10 15:48:47 -0800369 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700370}
371
372// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo sync12cca802012-12-19 17:34:22 -0800373bool UserBoundNlMessage::GetScanSsidsAttribute(nl80211_attrs id,
374 vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700375 if (!value) {
376 LOG(ERROR) << "Null |value| parameter";
377 return false;
378 }
379
repo sync90ee0fa2012-12-18 10:08:08 -0800380 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800381 if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty())
repo sync90ee0fa2012-12-18 10:08:08 -0800382 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700383
repo sync90ee0fa2012-12-18 10:08:08 -0800384 nlattr *nst = NULL;
385 // |nla_for_each_attr| requires a non-const parameter even though it
386 // doesn't change the data.
387 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
388 int rem_nst;
389 int len = rawdata.GetLength();
390
391 nla_for_each_attr(nst, data, len, rem_nst) {
392 value->push_back(StringFromSsid(nla_len(nst),
393 reinterpret_cast<const uint8_t *>(
394 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700395 }
repo sync90ee0fa2012-12-18 10:08:08 -0800396 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700397}
398
Wade Guthrie0d438532012-05-18 14:18:50 -0700399// Protected members.
400
Wade Guthrie0d438532012-05-18 14:18:50 -0700401string UserBoundNlMessage::GetHeaderString() const {
402 char ifname[IF_NAMESIZE] = "";
403 uint32_t ifindex = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800404 bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
405 &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700406
407 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800408 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
409 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700410
411 string output;
412 if (ifindex_exists && wifi_exists) {
413 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
414 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
415 wifi);
416 } else if (ifindex_exists) {
417 StringAppendF(&output, "%s: ",
418 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
419 } else if (wifi_exists) {
420 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
421 }
422
423 return output;
424}
425
repo sync12cca802012-12-19 17:34:22 -0800426string UserBoundNlMessage::StringFromFrame(nl80211_attrs attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700427 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800428 ByteString frame_data;
repo sync90ee0fa2012-12-18 10:08:08 -0800429 if (attributes().GetRawAttributeValue(attr_name,
430 &frame_data) && !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800431 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700432 frame.ToString(&output);
433 } else {
434 output.append(" [no frame]");
435 }
436
437 return output;
438}
439
440// static
441string UserBoundNlMessage::StringFromKeyType(nl80211_key_type key_type) {
442 switch (key_type) {
443 case NL80211_KEYTYPE_GROUP:
444 return "Group";
445 case NL80211_KEYTYPE_PAIRWISE:
446 return "Pairwise";
447 case NL80211_KEYTYPE_PEERKEY:
448 return "PeerKey";
449 default:
450 return "<Unknown Key Type>";
451 }
452}
453
454// static
455string UserBoundNlMessage::StringFromMacAddress(const uint8_t *arg) {
456 string output;
457
458 if (!arg) {
459 output = kBogusMacAddress;
460 LOG(ERROR) << "|arg| parameter is NULL.";
461 } else {
462 StringAppendF(&output, "%02x", arg[0]);
463
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800464 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700465 StringAppendF(&output, ":%02x", arg[i]);
466 }
467 }
468 return output;
469}
470
471// static
472string UserBoundNlMessage::StringFromRegInitiator(__u8 initiator) {
473 switch (initiator) {
474 case NL80211_REGDOM_SET_BY_CORE:
475 return "the wireless core upon initialization";
476 case NL80211_REGDOM_SET_BY_USER:
477 return "a user";
478 case NL80211_REGDOM_SET_BY_DRIVER:
479 return "a driver";
480 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
481 return "a country IE";
482 default:
483 return "<Unknown Reg Initiator>";
484 }
485}
486
487// static
488string UserBoundNlMessage::StringFromSsid(const uint8_t len,
489 const uint8_t *data) {
490 string output;
491 if (!data) {
492 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
493 LOG(ERROR) << "|data| parameter is NULL.";
494 return output;
495 }
496
497 for (int i = 0; i < len; ++i) {
498 if (data[i] == ' ')
499 output.append(" ");
500 else if (isprint(data[i]))
501 StringAppendF(&output, "%c", static_cast<char>(data[i]));
502 else
503 StringAppendF(&output, "\\x%2x", data[i]);
504 }
505
506 return output;
507}
508
509// static
Wade Guthried4977f22012-08-22 12:37:54 -0700510string UserBoundNlMessage::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700511 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700512 match = reason_code_string_->find(status);
513 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700514 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700515 if (status < IEEE_80211::kReasonCodeMax) {
516 StringAppendF(&output, "<Reserved Reason:%u>", status);
517 } else {
518 StringAppendF(&output, "<Unknown Reason:%u>", status);
519 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700520 return output;
521 }
522 return match->second;
523}
524
Wade Guthried4977f22012-08-22 12:37:54 -0700525// static
526string UserBoundNlMessage::StringFromStatus(uint16_t status) {
527 map<uint16_t, string>::const_iterator match;
528 match = status_code_string_->find(status);
529 if (match == status_code_string_->end()) {
530 string output;
531 if (status < IEEE_80211::kStatusCodeMax) {
532 StringAppendF(&output, "<Reserved Status:%u>", status);
533 } else {
534 StringAppendF(&output, "<Unknown Status:%u>", status);
535 }
536 return output;
537 }
538 return match->second;
539}
540
repo sync1538d442012-12-20 15:24:35 -0800541string UserBoundNlMessage::GenericToString() const {
542 string output;
543 StringAppendF(&output, "Received %s (%d)\n",
544 message_type_string(), message_type());
545 StringAppendF(&output, "%s", attributes_.ToString().c_str());
546 return output;
547}
548
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800549Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700550 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800551 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700552 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800553 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
554 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700555
556 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800557 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700558 mac_from_ =
559 UserBoundNlMessage::StringFromMacAddress(&frame->destination_mac[0]);
560 mac_to_ = UserBoundNlMessage::StringFromMacAddress(&frame->source_mac[0]);
561 frame_type_ = frame->frame_control & kFrameTypeMask;
562
563 switch (frame_type_) {
564 case kAssocResponseFrameType:
565 case kReassocResponseFrameType:
566 status_ = le16toh(frame->u.associate_response.status_code);
567 break;
568
569 case kAuthFrameType:
570 status_ = le16toh(frame->u.authentiate_message.status_code);
571 break;
572
573 case kDisassocFrameType:
574 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700575 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700576 break;
577
578 default:
579 break;
580 }
581 }
582}
583
Wade Guthried4977f22012-08-22 12:37:54 -0700584bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700585 if (!output) {
586 LOG(ERROR) << "NULL |output|";
587 return false;
588 }
589
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800590 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700591 output->append(" [no frame]");
592 return true;
593 }
594
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800595 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700596 output->append(" [invalid frame: ");
597 } else {
598 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
599
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800600 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700601 case kAssocResponseFrameType:
602 StringAppendF(output, "; AssocResponse status: %u: %s",
603 status_,
604 UserBoundNlMessage::StringFromStatus(status_).c_str());
605 break;
606 case kReassocResponseFrameType:
607 StringAppendF(output, "; ReassocResponse status: %u: %s",
608 status_,
609 UserBoundNlMessage::StringFromStatus(status_).c_str());
610 break;
611 case kAuthFrameType:
612 StringAppendF(output, "; Auth status: %u: %s",
613 status_,
614 UserBoundNlMessage::StringFromStatus(status_).c_str());
615 break;
616
617 case kDisassocFrameType:
618 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700619 reason_,
620 UserBoundNlMessage::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700621 break;
622 case kDeauthFrameType:
623 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700624 reason_,
625 UserBoundNlMessage::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700626 break;
627
628 default:
629 break;
630 }
631 output->append(" [frame: ");
632 }
633
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800634 const unsigned char *frame = frame_.GetConstData();
635 for (size_t i = 0; i < frame_.GetLength(); ++i) {
636 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700637 }
638 output->append("]");
639
640 return true;
641}
642
Wade Guthried4977f22012-08-22 12:37:54 -0700643bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800644 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700645}
646
Wade Guthried4977f22012-08-22 12:37:54 -0700647
Wade Guthrie0d438532012-05-18 14:18:50 -0700648//
649// Specific UserBoundNlMessage types.
650//
651
652const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
653const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
654
655string AssociateMessage::ToString() const {
656 string output(GetHeaderString());
657 output.append("assoc");
repo sync90ee0fa2012-12-18 10:08:08 -0800658 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL))
Wade Guthrie0d438532012-05-18 14:18:50 -0700659 output.append(StringFromFrame(NL80211_ATTR_FRAME));
repo sync90ee0fa2012-12-18 10:08:08 -0800660 else if (attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT))
Wade Guthrie0d438532012-05-18 14:18:50 -0700661 output.append(": timed out");
662 else
663 output.append(": unknown event");
664 return output;
665}
666
667const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
668const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
669
670string AuthenticateMessage::ToString() const {
671 string output(GetHeaderString());
672 output.append("auth");
repo sync90ee0fa2012-12-18 10:08:08 -0800673 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700674 output.append(StringFromFrame(NL80211_ATTR_FRAME));
675 } else {
repo sync90ee0fa2012-12-18 10:08:08 -0800676 output.append(attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT) ?
Wade Guthrie0d438532012-05-18 14:18:50 -0700677 ": timed out" : ": unknown event");
678 }
679 return output;
680}
681
682const uint8_t CancelRemainOnChannelMessage::kCommand =
683 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
684const char CancelRemainOnChannelMessage::kCommandString[] =
685 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
686
687string CancelRemainOnChannelMessage::ToString() const {
688 string output(GetHeaderString());
689 uint32_t freq;
690 uint64_t cookie;
691 StringAppendF(&output,
692 "done with remain on freq %" PRIu32 " (cookie %" PRIx64 ")",
repo sync90ee0fa2012-12-18 10:08:08 -0800693 (attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ,
694 &freq) ? 0 : freq),
695 (attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE,
696 &cookie) ? 0 : cookie));
Wade Guthrie0d438532012-05-18 14:18:50 -0700697 return output;
698}
699
700const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
701const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
702
703string ConnectMessage::ToString() const {
704 string output(GetHeaderString());
705
706 uint16_t status = UINT16_MAX;
707
repo sync90ee0fa2012-12-18 10:08:08 -0800708 if (!attributes().GetU16AttributeValue(NL80211_ATTR_STATUS_CODE, &status)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700709 output.append("unknown connect status");
710 } else if (status == 0) {
711 output.append("connected");
712 } else {
713 output.append("failed to connect");
714 }
715
repo sync90ee0fa2012-12-18 10:08:08 -0800716 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700717 string mac;
718 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
719 StringAppendF(&output, " to %s", mac.c_str());
720 }
721 if (status)
722 StringAppendF(&output, ", status: %u: %s", status,
723 StringFromStatus(status).c_str());
724 return output;
725}
726
727const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
728const char DeauthenticateMessage::kCommandString[] =
729 "NL80211_CMD_DEAUTHENTICATE";
730
731string DeauthenticateMessage::ToString() const {
732 string output(GetHeaderString());
733 StringAppendF(&output, "deauth%s",
734 StringFromFrame(NL80211_ATTR_FRAME).c_str());
735 return output;
736}
737
738const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
739const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
740
741string DeleteStationMessage::ToString() const {
742 string mac;
743 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
744 string output(GetHeaderString());
745 StringAppendF(&output, "del station %s", mac.c_str());
746 return output;
747}
748
749const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
750const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
751
752string DisassociateMessage::ToString() const {
753 string output(GetHeaderString());
754 StringAppendF(&output, "disassoc%s",
755 StringFromFrame(NL80211_ATTR_FRAME).c_str());
756 return output;
757}
758
759const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
760const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
761
762string DisconnectMessage::ToString() const {
763 string output(GetHeaderString());
764 StringAppendF(&output, "disconnected %s",
repo sync90ee0fa2012-12-18 10:08:08 -0800765 ((attributes().IsFlagAttributeTrue(
766 NL80211_ATTR_DISCONNECTED_BY_AP)) ?
767 "(by AP)" : "(local request)"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700768
769 uint16_t reason = UINT16_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800770 if (attributes().GetU16AttributeValue(NL80211_ATTR_REASON_CODE, &reason)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700771 StringAppendF(&output, " reason: %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700772 reason, StringFromReason(reason).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700773 }
774 return output;
775}
776
777const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
778const char FrameTxStatusMessage::kCommandString[] =
779 "NL80211_CMD_FRAME_TX_STATUS";
780
781string FrameTxStatusMessage::ToString() const {
782 string output(GetHeaderString());
783 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800784 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -0700785
repo sync90ee0fa2012-12-18 10:08:08 -0800786 StringAppendF(&output, "mgmt TX status (cookie %" PRIx64 "): %s", cookie,
787 (attributes().IsFlagAttributeTrue(NL80211_ATTR_ACK) ?
788 "acked" : "no ack"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700789 return output;
790}
791
792const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
793const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
794
795string JoinIbssMessage::ToString() const {
796 string mac;
797 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
798 string output(GetHeaderString());
799 StringAppendF(&output, "IBSS %s joined", mac.c_str());
800 return output;
801}
802
803const uint8_t MichaelMicFailureMessage::kCommand =
804 NL80211_CMD_MICHAEL_MIC_FAILURE;
805const char MichaelMicFailureMessage::kCommandString[] =
806 "NL80211_CMD_MICHAEL_MIC_FAILURE";
807
808string MichaelMicFailureMessage::ToString() const {
809 string output(GetHeaderString());
810
811 output.append("Michael MIC failure event:");
812
repo sync90ee0fa2012-12-18 10:08:08 -0800813 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700814 string mac;
815 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
816 StringAppendF(&output, " source MAC address %s", mac.c_str());
817 }
818
repo sync90ee0fa2012-12-18 10:08:08 -0800819 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800820 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800821 if (attributes().GetRawAttributeValue(NL80211_ATTR_KEY_SEQ,
822 &rawdata) &&
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800823 rawdata.GetLength() == UserBoundNlMessage::kEthernetAddressBytes) {
824 const unsigned char *seq = rawdata.GetConstData();
Wade Guthrie0d438532012-05-18 14:18:50 -0700825 StringAppendF(&output, " seq=%02x%02x%02x%02x%02x%02x",
826 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
827 }
828 }
829 uint32_t key_type_val = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800830 if (attributes().GetU32AttributeValue(NL80211_ATTR_KEY_TYPE, &key_type_val)) {
repo sync12cca802012-12-19 17:34:22 -0800831 nl80211_key_type key_type = static_cast<nl80211_key_type >(key_type_val);
Wade Guthrie0d438532012-05-18 14:18:50 -0700832 StringAppendF(&output, " Key Type %s", StringFromKeyType(key_type).c_str());
833 }
834
835 uint8_t key_index = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800836 if (attributes().GetU8AttributeValue(NL80211_ATTR_KEY_IDX, &key_index)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700837 StringAppendF(&output, " Key Id %u", key_index);
838 }
839
840 return output;
841}
842
843const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
844const char NewScanResultsMessage::kCommandString[] =
845 "NL80211_CMD_NEW_SCAN_RESULTS";
846
847string NewScanResultsMessage::ToString() const {
848 string output(GetHeaderString());
849 output.append("scan finished");
850
851 {
852 output.append("; frequencies: ");
853 vector<uint32_t> list;
854 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
855 string str;
856 for (vector<uint32_t>::const_iterator i = list.begin();
857 i != list.end(); ++i) {
858 StringAppendF(&str, " %" PRIu32 ", ", *i);
859 }
860 output.append(str);
861 }
862 }
863
864 {
865 output.append("; SSIDs: ");
866 vector<string> list;
867 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
868 string str;
869 for (vector<string>::const_iterator i = list.begin();
870 i != list.end(); ++i) {
871 StringAppendF(&str, "\"%s\", ", i->c_str());
872 }
873 output.append(str);
874 }
875 }
876
877 return output;
878}
879
880const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
881const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
882
883string NewStationMessage::ToString() const {
884 string mac;
885 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
886 string output(GetHeaderString());
887 StringAppendF(&output, "new station %s", mac.c_str());
888
889 return output;
890}
891
892const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
893const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
894
895string NewWifiMessage::ToString() const {
896 string output(GetHeaderString());
897 string wifi_name = "None";
repo sync90ee0fa2012-12-18 10:08:08 -0800898 attributes().GetStringAttributeValue(NL80211_ATTR_WIPHY_NAME, &wifi_name);
Wade Guthrie0d438532012-05-18 14:18:50 -0700899 StringAppendF(&output, "renamed to %s", wifi_name.c_str());
900 return output;
901}
902
903const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
904const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
905
906string NotifyCqmMessage::ToString() const {
repo sync90ee0fa2012-12-18 10:08:08 -0800907 // TODO(wdg): use attributes().GetNestedAttributeValue()...
repo sync12cca802012-12-19 17:34:22 -0800908 static const nla_policy kCqmValidationPolicy[NL80211_ATTR_CQM_MAX + 1] = {
Wade Guthrie0d438532012-05-18 14:18:50 -0700909 { NLA_U32, 0, 0 }, // Who Knows?
910 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
911 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
912 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
913 };
914
915 string output(GetHeaderString());
916 output.append("connection quality monitor event: ");
917
repo sync90ee0fa2012-12-18 10:08:08 -0800918 const Nl80211RawAttribute *attribute =
919 attributes().GetRawAttribute(NL80211_ATTR_CQM);
repo syncd316eb72012-12-10 15:48:47 -0800920 if (!attribute) {
921 output.append("missing data!");
922 return output;
923 }
924
925 const nlattr *const_data = attribute->data();
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800926 // Note that |nla_parse_nested| doesn't change |const_data| but doesn't
927 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -0700928 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
929
930 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
repo sync12cca802012-12-19 17:34:22 -0800931 if (!cqm_attr ||
932 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
933 const_cast<nla_policy *>(kCqmValidationPolicy))) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700934 output.append("missing data!");
935 return output;
936 }
937 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
repo sync12cca802012-12-19 17:34:22 -0800938 nl80211_cqm_rssi_threshold_event rssi_event =
939 static_cast<nl80211_cqm_rssi_threshold_event>(
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800940 Nl80211Attribute::NlaGetU32(
941 cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -0700942 if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
943 output.append("RSSI went above threshold");
944 else
945 output.append("RSSI went below threshold");
946 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
repo sync90ee0fa2012-12-18 10:08:08 -0800947 attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700948 string mac;
949 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
950 StringAppendF(&output, "peer %s didn't ACK %" PRIu32 " packets",
951 mac.c_str(),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800952 Nl80211Attribute::NlaGetU32(
953 cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -0700954 } else {
955 output.append("unknown event");
956 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700957 return output;
958}
959
960const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
961const char PmksaCandidateMessage::kCommandString[] =
962 "NL80211_ATTR_PMKSA_CANDIDATE";
963
964string PmksaCandidateMessage::ToString() const {
965 string output(GetHeaderString());
966 output.append("PMKSA candidate found");
967 return output;
968}
969
970const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
971const char RegBeaconHintMessage::kCommandString[] =
972 "NL80211_CMD_REG_BEACON_HINT";
973
974string RegBeaconHintMessage::ToString() const {
975 string output(GetHeaderString());
976 uint32_t wiphy_idx = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800977 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, &wiphy_idx);
Wade Guthrie0d438532012-05-18 14:18:50 -0700978
repo sync90ee0fa2012-12-18 10:08:08 -0800979 const Nl80211RawAttribute *freq_before =
980 attributes().GetRawAttribute(NL80211_ATTR_FREQ_BEFORE);
repo syncd316eb72012-12-10 15:48:47 -0800981 if (!freq_before)
982 return "";
983 const nlattr *const_before = freq_before->data();
Wade Guthrie0d438532012-05-18 14:18:50 -0700984 ieee80211_beacon_channel chan_before_beacon;
985 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
986 if (ParseBeaconHintChan(const_before, &chan_before_beacon))
987 return "";
988
repo sync90ee0fa2012-12-18 10:08:08 -0800989 const Nl80211RawAttribute *freq_after =
990 attributes().GetRawAttribute(NL80211_ATTR_FREQ_AFTER);
repo syncd316eb72012-12-10 15:48:47 -0800991 if (!freq_after)
992 return "";
993
994 const nlattr *const_after = freq_after->data();
Wade Guthrie0d438532012-05-18 14:18:50 -0700995 ieee80211_beacon_channel chan_after_beacon;
996 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
997 if (ParseBeaconHintChan(const_after, &chan_after_beacon))
998 return "";
999
1000 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
1001 return "";
1002
1003 /* A beacon hint is sent _only_ if something _did_ change */
1004 output.append("beacon hint:");
1005 StringAppendF(&output, "phy%" PRIu32 " %u MHz [%d]:",
1006 wiphy_idx, chan_before_beacon.center_freq,
1007 ChannelFromIeee80211Frequency(chan_before_beacon.center_freq));
1008
1009 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
1010 output.append("\to active scanning enabled");
1011 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
1012 output.append("\to beaconing enabled");
1013 return output;
1014}
1015
1016int RegBeaconHintMessage::ParseBeaconHintChan(const nlattr *tb,
1017 ieee80211_beacon_channel *chan)
1018 const {
1019 static const nla_policy kBeaconFreqPolicy[
1020 NL80211_FREQUENCY_ATTR_MAX + 1] = {
1021 {0, 0, 0},
1022 { NLA_U32, 0, 0 }, // [NL80211_FREQUENCY_ATTR_FREQ]
1023 {0, 0, 0},
1024 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]
1025 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_NO_IBSS]
1026 };
1027
1028 if (!tb) {
1029 LOG(ERROR) << "|tb| parameter is NULL.";
1030 return -EINVAL;
1031 }
1032
1033 if (!chan) {
1034 LOG(ERROR) << "|chan| parameter is NULL.";
1035 return -EINVAL;
1036 }
1037
1038 nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1039
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001040 // Note that |nla_parse_nested| doesn't change its parameters but doesn't
1041 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001042 if (nla_parse_nested(tb_freq,
1043 NL80211_FREQUENCY_ATTR_MAX,
1044 const_cast<nlattr *>(tb),
1045 const_cast<nla_policy *>(kBeaconFreqPolicy)))
1046 return -EINVAL;
1047
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001048 chan->center_freq = Nl80211Attribute::NlaGetU32(
1049 tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
Wade Guthrie0d438532012-05-18 14:18:50 -07001050
1051 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1052 chan->passive_scan = true;
1053 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1054 chan->no_ibss = true;
1055
1056 return 0;
1057}
1058
1059int RegBeaconHintMessage::ChannelFromIeee80211Frequency(int freq) {
1060 // TODO(wdg): get rid of these magic numbers.
1061 if (freq == 2484)
1062 return 14;
1063
1064 if (freq < 2484)
1065 return (freq - 2407) / 5;
1066
1067 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
1068 return freq/5 - 1000;
1069}
1070
1071const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
1072const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
1073
1074string RegChangeMessage::ToString() const {
1075 string output(GetHeaderString());
1076 output.append("regulatory domain change: ");
1077
1078 uint8_t reg_type = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001079 attributes().GetU8AttributeValue(NL80211_ATTR_REG_TYPE, &reg_type);
Wade Guthrie0d438532012-05-18 14:18:50 -07001080
1081 uint32_t initiator = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001082 attributes().GetU32AttributeValue(NL80211_ATTR_REG_INITIATOR, &initiator);
Wade Guthrie0d438532012-05-18 14:18:50 -07001083
1084 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001085 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
1086 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -07001087
1088 string alpha2 = "<None>";
repo sync90ee0fa2012-12-18 10:08:08 -08001089 attributes().GetStringAttributeValue(NL80211_ATTR_REG_ALPHA2, &alpha2);
Wade Guthrie0d438532012-05-18 14:18:50 -07001090
1091 switch (reg_type) {
1092 case NL80211_REGDOM_TYPE_COUNTRY:
1093 StringAppendF(&output, "set to %s by %s request",
1094 alpha2.c_str(), StringFromRegInitiator(initiator).c_str());
1095 if (wifi_exists)
1096 StringAppendF(&output, " on phy%" PRIu32, wifi);
1097 break;
1098
1099 case NL80211_REGDOM_TYPE_WORLD:
1100 StringAppendF(&output, "set to world roaming by %s request",
1101 StringFromRegInitiator(initiator).c_str());
1102 break;
1103
1104 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
1105 StringAppendF(&output,
1106 "custom world roaming rules in place on phy%" PRIu32
1107 " by %s request",
1108 wifi, StringFromRegInitiator(initiator).c_str());
1109 break;
1110
1111 case NL80211_REGDOM_TYPE_INTERSECTION:
1112 StringAppendF(&output, "intersection used due to a request made by %s",
1113 StringFromRegInitiator(initiator).c_str());
1114 if (wifi_exists)
1115 StringAppendF(&output, " on phy%" PRIu32, wifi);
1116 break;
1117
1118 default:
1119 output.append("unknown source");
1120 break;
1121 }
1122 return output;
1123}
1124
1125const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
1126const char RemainOnChannelMessage::kCommandString[] =
1127 "NL80211_CMD_REMAIN_ON_CHANNEL";
1128
1129string RemainOnChannelMessage::ToString() const {
1130 string output(GetHeaderString());
1131
1132 uint32_t wifi_freq = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001133 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ, &wifi_freq);
Wade Guthrie0d438532012-05-18 14:18:50 -07001134
1135 uint32_t duration = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001136 attributes().GetU32AttributeValue(NL80211_ATTR_DURATION, &duration);
Wade Guthrie0d438532012-05-18 14:18:50 -07001137
1138 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001139 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -07001140
1141 StringAppendF(&output, "remain on freq %" PRIu32 " (%" PRIu32 "ms, cookie %"
1142 PRIx64 ")",
1143 wifi_freq, duration, cookie);
1144 return output;
1145}
1146
1147const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
1148const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
1149
1150string RoamMessage::ToString() const {
1151 string output(GetHeaderString());
1152 output.append("roamed");
1153
repo sync90ee0fa2012-12-18 10:08:08 -08001154 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001155 string mac;
1156 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1157 StringAppendF(&output, " to %s", mac.c_str());
1158 }
1159 return output;
1160}
1161
1162const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
1163const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
1164
1165string ScanAbortedMessage::ToString() const {
1166 string output(GetHeaderString());
1167 output.append("scan aborted");
1168
1169 {
1170 output.append("; frequencies: ");
1171 vector<uint32_t> list;
1172 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1173 string str;
1174 for (vector<uint32_t>::const_iterator i = list.begin();
1175 i != list.end(); ++i) {
1176 StringAppendF(&str, " %" PRIu32 ", ", *i);
1177 }
1178 output.append(str);
1179 }
1180 }
1181
1182 {
1183 output.append("; SSIDs: ");
1184 vector<string> list;
1185 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1186 string str;
1187 for (vector<string>::const_iterator i = list.begin();
1188 i != list.end(); ++i) {
1189 StringAppendF(&str, "\"%s\", ", i->c_str());
1190 }
1191 output.append(str);
1192 }
1193 }
1194
1195 return output;
1196}
1197
1198const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
1199const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
1200
1201string TriggerScanMessage::ToString() const {
1202 string output(GetHeaderString());
1203 output.append("scan started");
1204
1205 {
1206 output.append("; frequencies: ");
1207 vector<uint32_t> list;
1208 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1209 string str;
1210 for (vector<uint32_t>::const_iterator i = list.begin();
1211 i != list.end(); ++i) {
1212 StringAppendF(&str, "%" PRIu32 ", ", *i);
1213 }
1214 output.append(str);
1215 }
1216 }
1217
1218 {
1219 output.append("; SSIDs: ");
1220 vector<string> list;
1221 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1222 string str;
1223 for (vector<string>::const_iterator i = list.begin();
1224 i != list.end(); ++i) {
1225 StringAppendF(&str, "\"%s\", ", i->c_str());
1226 }
1227 output.append(str);
1228 }
1229 }
1230
1231 return output;
1232}
1233
1234const uint8_t UnknownMessage::kCommand = 0xff;
1235const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
1236
1237string UnknownMessage::ToString() const {
1238 string output(GetHeaderString());
1239 StringAppendF(&output, "unknown event %u", command_);
1240 return output;
1241}
1242
1243const uint8_t UnprotDeauthenticateMessage::kCommand =
1244 NL80211_CMD_UNPROT_DEAUTHENTICATE;
1245const char UnprotDeauthenticateMessage::kCommandString[] =
1246 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
1247
1248string UnprotDeauthenticateMessage::ToString() const {
1249 string output(GetHeaderString());
1250 StringAppendF(&output, "unprotected deauth %s",
1251 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1252 return output;
1253}
1254
1255const uint8_t UnprotDisassociateMessage::kCommand =
1256 NL80211_CMD_UNPROT_DISASSOCIATE;
1257const char UnprotDisassociateMessage::kCommandString[] =
1258 "NL80211_CMD_UNPROT_DISASSOCIATE";
1259
1260string UnprotDisassociateMessage::ToString() const {
1261 string output(GetHeaderString());
1262 StringAppendF(&output, "unprotected disassoc %s",
1263 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1264 return output;
1265}
1266
1267//
1268// Factory class.
1269//
1270
1271UserBoundNlMessage *UserBoundNlMessageFactory::CreateMessage(nlmsghdr *msg) {
1272 if (!msg) {
1273 LOG(ERROR) << "NULL |msg| parameter";
1274 return NULL;
1275 }
1276
1277 scoped_ptr<UserBoundNlMessage> message;
1278 genlmsghdr *gnlh =
1279 reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
1280
1281 if (!gnlh) {
1282 LOG(ERROR) << "NULL gnlh";
1283 return NULL;
1284 }
1285
1286 switch (gnlh->cmd) {
1287 case AssociateMessage::kCommand:
1288 message.reset(new AssociateMessage()); break;
1289 case AuthenticateMessage::kCommand:
1290 message.reset(new AuthenticateMessage()); break;
1291 case CancelRemainOnChannelMessage::kCommand:
1292 message.reset(new CancelRemainOnChannelMessage()); break;
1293 case ConnectMessage::kCommand:
1294 message.reset(new ConnectMessage()); break;
1295 case DeauthenticateMessage::kCommand:
1296 message.reset(new DeauthenticateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001297 case DeleteStationMessage::kCommand:
1298 message.reset(new DeleteStationMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001299 case DisassociateMessage::kCommand:
1300 message.reset(new DisassociateMessage()); break;
1301 case DisconnectMessage::kCommand:
1302 message.reset(new DisconnectMessage()); break;
1303 case FrameTxStatusMessage::kCommand:
1304 message.reset(new FrameTxStatusMessage()); break;
1305 case JoinIbssMessage::kCommand:
1306 message.reset(new JoinIbssMessage()); break;
1307 case MichaelMicFailureMessage::kCommand:
1308 message.reset(new MichaelMicFailureMessage()); break;
1309 case NewScanResultsMessage::kCommand:
1310 message.reset(new NewScanResultsMessage()); break;
1311 case NewStationMessage::kCommand:
1312 message.reset(new NewStationMessage()); break;
1313 case NewWifiMessage::kCommand:
1314 message.reset(new NewWifiMessage()); break;
1315 case NotifyCqmMessage::kCommand:
1316 message.reset(new NotifyCqmMessage()); break;
1317 case PmksaCandidateMessage::kCommand:
1318 message.reset(new PmksaCandidateMessage()); break;
1319 case RegBeaconHintMessage::kCommand:
1320 message.reset(new RegBeaconHintMessage()); break;
1321 case RegChangeMessage::kCommand:
1322 message.reset(new RegChangeMessage()); break;
1323 case RemainOnChannelMessage::kCommand:
1324 message.reset(new RemainOnChannelMessage()); break;
1325 case RoamMessage::kCommand:
1326 message.reset(new RoamMessage()); break;
1327 case ScanAbortedMessage::kCommand:
1328 message.reset(new ScanAbortedMessage()); break;
1329 case TriggerScanMessage::kCommand:
1330 message.reset(new TriggerScanMessage()); break;
1331 case UnprotDeauthenticateMessage::kCommand:
1332 message.reset(new UnprotDeauthenticateMessage()); break;
1333 case UnprotDisassociateMessage::kCommand:
1334 message.reset(new UnprotDisassociateMessage()); break;
1335
1336 default:
1337 message.reset(new UnknownMessage(gnlh->cmd)); break;
1338 break;
1339 }
1340
1341 nlattr *tb[NL80211_ATTR_MAX + 1];
1342
1343 // Parse the attributes from the nl message payload (which starts at the
1344 // header) into the 'tb' array.
1345 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001346 genlmsg_attrlen(gnlh, 0), NULL);
Wade Guthrie0d438532012-05-18 14:18:50 -07001347
1348 if (!message->Init(tb, msg)) {
1349 LOG(ERROR) << "Message did not initialize properly";
1350 return NULL;
1351 }
1352
Wade Guthrie0d438532012-05-18 14:18:50 -07001353 return message.release();
1354}
1355
1356UserBoundNlMessageDataCollector *
1357 UserBoundNlMessageDataCollector::GetInstance() {
1358 return g_datacollector.Pointer();
1359}
1360
1361UserBoundNlMessageDataCollector::UserBoundNlMessageDataCollector() {
1362 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
1363 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
1364 need_to_print[NL80211_CMD_DEL_STATION] = true;
1365 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
1366 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
1367 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
1368 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
1369 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
1370 need_to_print[NL80211_CMD_REG_CHANGE] = true;
1371 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
1372 need_to_print[NL80211_CMD_ROAM] = true;
1373 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
1374 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
1375 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
1376}
1377
1378void UserBoundNlMessageDataCollector::CollectDebugData(
Wade Guthried4977f22012-08-22 12:37:54 -07001379 const UserBoundNlMessage &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001380 if (!msg) {
1381 LOG(ERROR) << "NULL |msg| parameter";
1382 return;
1383 }
1384
1385 bool doit = false;
1386
1387 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -08001388 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -07001389 if (node != need_to_print.end())
1390 doit = node->second;
1391
1392 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001393 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001394 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001395 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001396
Christopher Wileyefd521f2012-11-07 17:32:46 -08001397 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001398
1399 size_t bytes = nlmsg_total_size(payload_bytes);
1400 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001401 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001402 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001403 << std::hex << std::setfill('0') << std::setw(2)
1404 << + rawdata[i] << ",";
1405 }
Wade Guthried6153612012-08-23 11:36:14 -07001406 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001407 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001408 }
1409}
1410
1411} // namespace shill.