blob: fd3ec1007c90df97a4b3396c9f546182a199a913 [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 sync0efa9f02012-12-28 13:40:20 -0800106bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) {
107 if (!const_msg) {
108 LOG(ERROR) << "Null |msg| parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700109 return false;
110 }
111
repo sync0efa9f02012-12-28 13:40:20 -0800112 // Netlink header.
113 sequence_number_ = const_msg->nlmsg_seq;
repo syncdc085c82012-12-28 08:54:41 -0800114 SLOG(WiFi, 6) << "NL Message " << sequence_number() << " <===";
Wade Guthrie0d438532012-05-18 14:18:50 -0700115
repo sync0efa9f02012-12-28 13:40:20 -0800116 // Casting away constness, here, since the libnl code doesn't properly label
117 // their stuff as const (even though it is).
118 nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg);
119
120 // Genl message header.
121 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
122
123 // Attributes.
124 // Parse the attributes from the nl message payload into the 'tb' array.
125 nlattr *tb[NL80211_ATTR_MAX + 1];
126 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
127 genlmsg_attrlen(gnlh, 0), NULL);
128
Wade Guthrie0d438532012-05-18 14:18:50 -0700129 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
130 if (tb[i]) {
repo sync0efa9f02012-12-28 13:40:20 -0800131 // TODO(wdg): When Nl80211Messages instantiate their own attributes,
132 // this call should, instead, call |SetAttributeFromNlAttr|.
133 attributes_.CreateAndInitFromNlAttr(static_cast<enum nl80211_attrs>(i),
134 tb[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700135 }
136 }
137
138 // Convert integer values provided by libnl (for example, from the
139 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
140 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700141 if (!reason_code_string_) {
142 reason_code_string_ = new map<uint16_t, string>;
143 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
144 "Unspecified reason";
145 (*reason_code_string_)[
146 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700147 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700148 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
149 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
150 "ESS";
151 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
152 "Disassociated due to inactivity";
153 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
154 "Disassociated because AP is unable to handle all currently associated "
155 "STAs";
156 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
157 "Class 2 frame received from nonauthenticated STA";
158 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
159 "Class 3 frame received from nonassociated STA";
160 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
161 "Disassociated because sending STA is leaving (or has left) BSS";
162 (*reason_code_string_)[
163 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
164 "STA requesting (re)association is not authenticated with responding "
165 "STA";
166 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
167 "Disassociated because the information in the Power Capability "
168 "element is unacceptable";
169 (*reason_code_string_)[
170 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
171 "Disassociated because the information in the Supported Channels "
172 "element is unacceptable";
173 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
174 "Invalid information element, i.e., an information element defined in "
175 "this standard for which the content does not meet the specifications "
176 "in Clause 7";
177 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
178 "Message integrity code (MIC) failure";
179 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
180 "4-Way Handshake timeout";
181 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
182 "Group Key Handshake timeout";
183 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
184 "Information element in 4-Way Handshake different from "
185 "(Re)Association Request/Probe Response/Beacon frame";
186 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
187 "Invalid group cipher";
188 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
189 "Invalid pairwise cipher";
190 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
191 "Invalid AKMP";
192 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
193 "Unsupported RSN information element version";
194 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
195 "Invalid RSN information element capabilities";
196 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
197 "IEEE 802.1X authentication failed";
198 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
199 "Cipher suite rejected because of the security policy";
200 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
201 "Disassociated for unspecified, QoS-related reason";
202 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
203 "Disassociated because QoS AP lacks sufficient bandwidth for this "
204 "QoS STA";
205 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
206 "Disassociated because excessive number of frames need to be "
207 "acknowledged, but are not acknowledged due to AP transmissions "
208 "and/or poor channel conditions";
209 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
210 "Disassociated because STA is transmitting outside the limits of its "
211 "TXOPs";
212 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
213 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
214 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
215 "Requested from peer STA as it does not want to use the mechanism";
216 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
217 "Requested from peer STA as the STA received frames using the "
218 "mechanism for which a setup is required";
219 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
220 "Requested from peer STA due to timeout";
221 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
222 "Peer STA does not support the requested cipher suite";
223 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
224 }
225
226 if (!status_code_string_) {
227 status_code_string_ = new map<uint16_t, string>;
228 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
229 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
230 "Unspecified failure";
231 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700232 "Cannot support all requested capabilities in the capability "
233 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700234 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700235 "Reassociation denied due to inability to confirm that association "
236 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700237 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700238 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700239 (*status_code_string_)[
240 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700241 "Responding station does not support the specified authentication "
242 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700243 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700244 "Received an authentication frame with authentication transaction "
245 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700246 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700247 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700248 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700249 "Authentication rejected due to timeout waiting for next frame in "
250 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700251 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
252 "Association denied because AP is unable to handle additional "
253 "associated STA";
254 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700255 "Association denied due to requesting station not supporting all of "
256 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700257 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700258 "Association denied due to requesting station not supporting the "
259 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700260 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700261 "Association denied due to requesting station not supporting the PBCC "
262 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700263 (*status_code_string_)[
264 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700265 "Association denied due to requesting station not supporting the "
266 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700267 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700268 "Association request rejected because Spectrum Management capability "
269 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700270 (*status_code_string_)[
271 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700272 "Association request rejected because the information in the Power "
273 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700274 (*status_code_string_)[
275 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700276 "Association request rejected because the information in the "
277 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700278 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700279 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700280 "supporting the Short Slot Time option";
281 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
282 "Association request rejected due to requesting station not "
283 "supporting the DSSS-OFDM option";
284 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700285 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700286 (*status_code_string_)[
287 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700288 "Association denied due to QAP having insufficient bandwidth to handle "
289 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700290 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700291 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700292 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
293 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700294 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700295 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
296 "The request has been declined";
297 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700298 "The request has not been successful as one or more parameters have "
299 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700300 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
301 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700302 "However, a suggested Tspec is provided so that the initiating QSTA "
303 "may attempt to send another TS with the suggested changes to the "
304 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700305 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700306 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700307 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
308 "Invalid Group Cipher";
309 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
310 "Invalid Pairwise Cipher";
311 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
312 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
313 "Unsupported RSN Information Element version";
314 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
315 "Invalid RSN Information Element Capabilities";
316 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700317 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700318 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
319 "The TS has not been created. However, the HC may be capable of "
320 "creating a TS, in response to a request, after the time indicated in "
321 "the TS Delay element";
322 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700323 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700324 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
325 "Destination STA is not present within this BSS";
326 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
327 "The destination STA is not a QoS STA";
328 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700329 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700330 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700331 }
332
333 return true;
334}
335
Wade Guthrie0d438532012-05-18 14:18:50 -0700336// Helper function to provide a string for a MAC address.
repo syncdc085c82012-12-28 08:54:41 -0800337bool Nl80211Message::GetMacAttributeString(nl80211_attrs id,
338 string *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700339 if (!value) {
340 LOG(ERROR) << "Null |value| parameter";
341 return false;
342 }
343
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800344 ByteString data;
repo sync12cca802012-12-19 17:34:22 -0800345 if (!attributes().GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700346 value->assign(kBogusMacAddress);
347 return false;
348 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800349 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700350
351 return true;
352}
353
354// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
repo syncdc085c82012-12-28 08:54:41 -0800355bool Nl80211Message::GetScanFrequenciesAttribute(
repo sync12cca802012-12-19 17:34:22 -0800356 nl80211_attrs id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700357 if (!value) {
358 LOG(ERROR) << "Null |value| parameter";
359 return false;
360 }
361
362 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800363 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800364 if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800365 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700366
repo syncd316eb72012-12-10 15:48:47 -0800367 nlattr *nst = NULL;
368 // |nla_for_each_attr| requires a non-const parameter even though it
369 // doesn't change the data.
370 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
371 int rem_nst;
372 int len = rawdata.GetLength();
373
374 nla_for_each_attr(nst, attr_data, len, rem_nst) {
375 value->push_back(Nl80211Attribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700376 }
repo syncd316eb72012-12-10 15:48:47 -0800377 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700378}
379
380// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800381bool Nl80211Message::GetScanSsidsAttribute(
382 nl80211_attrs id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700383 if (!value) {
384 LOG(ERROR) << "Null |value| parameter";
385 return false;
386 }
387
repo sync90ee0fa2012-12-18 10:08:08 -0800388 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800389 if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty())
repo sync90ee0fa2012-12-18 10:08:08 -0800390 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700391
repo sync90ee0fa2012-12-18 10:08:08 -0800392 nlattr *nst = NULL;
393 // |nla_for_each_attr| requires a non-const parameter even though it
394 // doesn't change the data.
395 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
396 int rem_nst;
397 int len = rawdata.GetLength();
398
399 nla_for_each_attr(nst, data, len, rem_nst) {
400 value->push_back(StringFromSsid(nla_len(nst),
401 reinterpret_cast<const uint8_t *>(
402 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700403 }
repo sync90ee0fa2012-12-18 10:08:08 -0800404 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700405}
406
Wade Guthrie0d438532012-05-18 14:18:50 -0700407// Protected members.
408
repo syncdc085c82012-12-28 08:54:41 -0800409string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700410 char ifname[IF_NAMESIZE] = "";
411 uint32_t ifindex = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800412 bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
413 &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700414
415 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800416 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
417 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700418
419 string output;
420 if (ifindex_exists && wifi_exists) {
421 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
422 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
423 wifi);
424 } else if (ifindex_exists) {
425 StringAppendF(&output, "%s: ",
426 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
427 } else if (wifi_exists) {
428 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
429 }
430
431 return output;
432}
433
repo syncdc085c82012-12-28 08:54:41 -0800434string Nl80211Message::StringFromFrame(nl80211_attrs attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700435 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800436 ByteString frame_data;
repo sync90ee0fa2012-12-18 10:08:08 -0800437 if (attributes().GetRawAttributeValue(attr_name,
438 &frame_data) && !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800439 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700440 frame.ToString(&output);
441 } else {
442 output.append(" [no frame]");
443 }
444
445 return output;
446}
447
448// static
repo syncdc085c82012-12-28 08:54:41 -0800449string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700450 switch (key_type) {
451 case NL80211_KEYTYPE_GROUP:
452 return "Group";
453 case NL80211_KEYTYPE_PAIRWISE:
454 return "Pairwise";
455 case NL80211_KEYTYPE_PEERKEY:
456 return "PeerKey";
457 default:
458 return "<Unknown Key Type>";
459 }
460}
461
462// static
repo syncdc085c82012-12-28 08:54:41 -0800463string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700464 string output;
465
466 if (!arg) {
467 output = kBogusMacAddress;
468 LOG(ERROR) << "|arg| parameter is NULL.";
469 } else {
470 StringAppendF(&output, "%02x", arg[0]);
471
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800472 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700473 StringAppendF(&output, ":%02x", arg[i]);
474 }
475 }
476 return output;
477}
478
479// static
repo syncdc085c82012-12-28 08:54:41 -0800480string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700481 switch (initiator) {
482 case NL80211_REGDOM_SET_BY_CORE:
483 return "the wireless core upon initialization";
484 case NL80211_REGDOM_SET_BY_USER:
485 return "a user";
486 case NL80211_REGDOM_SET_BY_DRIVER:
487 return "a driver";
488 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
489 return "a country IE";
490 default:
491 return "<Unknown Reg Initiator>";
492 }
493}
494
495// static
repo syncdc085c82012-12-28 08:54:41 -0800496string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700497 const uint8_t *data) {
498 string output;
499 if (!data) {
500 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
501 LOG(ERROR) << "|data| parameter is NULL.";
502 return output;
503 }
504
505 for (int i = 0; i < len; ++i) {
506 if (data[i] == ' ')
507 output.append(" ");
508 else if (isprint(data[i]))
509 StringAppendF(&output, "%c", static_cast<char>(data[i]));
510 else
511 StringAppendF(&output, "\\x%2x", data[i]);
512 }
513
514 return output;
515}
516
517// static
repo syncdc085c82012-12-28 08:54:41 -0800518string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700519 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700520 match = reason_code_string_->find(status);
521 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700522 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700523 if (status < IEEE_80211::kReasonCodeMax) {
524 StringAppendF(&output, "<Reserved Reason:%u>", status);
525 } else {
526 StringAppendF(&output, "<Unknown Reason:%u>", status);
527 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700528 return output;
529 }
530 return match->second;
531}
532
Wade Guthried4977f22012-08-22 12:37:54 -0700533// static
repo syncdc085c82012-12-28 08:54:41 -0800534string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700535 map<uint16_t, string>::const_iterator match;
536 match = status_code_string_->find(status);
537 if (match == status_code_string_->end()) {
538 string output;
539 if (status < IEEE_80211::kStatusCodeMax) {
540 StringAppendF(&output, "<Reserved Status:%u>", status);
541 } else {
542 StringAppendF(&output, "<Unknown Status:%u>", status);
543 }
544 return output;
545 }
546 return match->second;
547}
548
repo syncdc085c82012-12-28 08:54:41 -0800549string Nl80211Message::GenericToString() const {
repo sync1538d442012-12-20 15:24:35 -0800550 string output;
repo syncdc085c82012-12-28 08:54:41 -0800551 StringAppendF(&output, "Message %s (%d)\n",
repo sync1538d442012-12-20 15:24:35 -0800552 message_type_string(), message_type());
553 StringAppendF(&output, "%s", attributes_.ToString().c_str());
554 return output;
555}
556
repo syncdc085c82012-12-28 08:54:41 -0800557ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const {
558 // Build netlink header.
559 nlmsghdr header;
560 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
561 header.nlmsg_len = nlmsghdr_with_pad;
562 header.nlmsg_type = nlmsg_type;
563 header.nlmsg_flags = NLM_F_REQUEST;
564 header.nlmsg_seq = sequence_number();
565 header.nlmsg_pid = getpid();
566
567 // Build genl message header.
568 genlmsghdr genl_header;
569 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
570 header.nlmsg_len += genlmsghdr_with_pad;
571 genl_header.cmd = message_type();
572 genl_header.version = 1;
573 genl_header.reserved = 0;
574
575 // Assemble attributes (padding is included by AttributeList::Encode).
576 ByteString attributes = attributes_.Encode();
577 header.nlmsg_len += attributes.GetLength();
578
579 // Now that we know the total message size, build the output ByteString.
580 ByteString result;
581
582 // Netlink header + pad.
583 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
584 sizeof(header)));
585 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
586
587 // Genl message header + pad.
588 result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header),
589 sizeof(genl_header)));
590 result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill.
591
592 // Attributes including pad.
593 result.Append(attributes);
594
595 return result;
596}
597
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800598Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700599 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800600 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700601 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800602 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
603 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700604
605 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800606 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700607 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800608 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
609 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700610 frame_type_ = frame->frame_control & kFrameTypeMask;
611
612 switch (frame_type_) {
613 case kAssocResponseFrameType:
614 case kReassocResponseFrameType:
615 status_ = le16toh(frame->u.associate_response.status_code);
616 break;
617
618 case kAuthFrameType:
619 status_ = le16toh(frame->u.authentiate_message.status_code);
620 break;
621
622 case kDisassocFrameType:
623 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700624 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700625 break;
626
627 default:
628 break;
629 }
630 }
631}
632
Wade Guthried4977f22012-08-22 12:37:54 -0700633bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700634 if (!output) {
635 LOG(ERROR) << "NULL |output|";
636 return false;
637 }
638
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800639 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700640 output->append(" [no frame]");
641 return true;
642 }
643
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800644 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700645 output->append(" [invalid frame: ");
646 } else {
647 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
648
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800649 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700650 case kAssocResponseFrameType:
651 StringAppendF(output, "; AssocResponse status: %u: %s",
652 status_,
repo syncdc085c82012-12-28 08:54:41 -0800653 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700654 break;
655 case kReassocResponseFrameType:
656 StringAppendF(output, "; ReassocResponse status: %u: %s",
657 status_,
repo syncdc085c82012-12-28 08:54:41 -0800658 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700659 break;
660 case kAuthFrameType:
661 StringAppendF(output, "; Auth status: %u: %s",
662 status_,
repo syncdc085c82012-12-28 08:54:41 -0800663 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700664 break;
665
666 case kDisassocFrameType:
667 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700668 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800669 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700670 break;
671 case kDeauthFrameType:
672 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700673 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800674 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700675 break;
676
677 default:
678 break;
679 }
680 output->append(" [frame: ");
681 }
682
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800683 const unsigned char *frame = frame_.GetConstData();
684 for (size_t i = 0; i < frame_.GetLength(); ++i) {
685 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700686 }
687 output->append("]");
688
689 return true;
690}
691
Wade Guthried4977f22012-08-22 12:37:54 -0700692bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800693 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700694}
695
Wade Guthried4977f22012-08-22 12:37:54 -0700696
Wade Guthrie0d438532012-05-18 14:18:50 -0700697//
repo syncdc085c82012-12-28 08:54:41 -0800698// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700699//
700
repo sync0efa9f02012-12-28 13:40:20 -0800701// An Ack is not a GENL message and, as such, has no command.
702const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
703const char AckMessage::kCommandString[] = "NL80211_ACK";
704
705string AckMessage::ToString() const {
706 return "NL80211_ACK";
707}
708
Wade Guthrie0d438532012-05-18 14:18:50 -0700709const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
710const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
711
712string AssociateMessage::ToString() const {
713 string output(GetHeaderString());
714 output.append("assoc");
repo sync90ee0fa2012-12-18 10:08:08 -0800715 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL))
Wade Guthrie0d438532012-05-18 14:18:50 -0700716 output.append(StringFromFrame(NL80211_ATTR_FRAME));
repo sync90ee0fa2012-12-18 10:08:08 -0800717 else if (attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT))
Wade Guthrie0d438532012-05-18 14:18:50 -0700718 output.append(": timed out");
719 else
720 output.append(": unknown event");
721 return output;
722}
723
724const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
725const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
726
727string AuthenticateMessage::ToString() const {
728 string output(GetHeaderString());
729 output.append("auth");
repo sync90ee0fa2012-12-18 10:08:08 -0800730 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700731 output.append(StringFromFrame(NL80211_ATTR_FRAME));
732 } else {
repo sync90ee0fa2012-12-18 10:08:08 -0800733 output.append(attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT) ?
Wade Guthrie0d438532012-05-18 14:18:50 -0700734 ": timed out" : ": unknown event");
735 }
736 return output;
737}
738
739const uint8_t CancelRemainOnChannelMessage::kCommand =
740 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
741const char CancelRemainOnChannelMessage::kCommandString[] =
742 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
743
744string CancelRemainOnChannelMessage::ToString() const {
745 string output(GetHeaderString());
746 uint32_t freq;
747 uint64_t cookie;
748 StringAppendF(&output,
749 "done with remain on freq %" PRIu32 " (cookie %" PRIx64 ")",
repo sync90ee0fa2012-12-18 10:08:08 -0800750 (attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ,
751 &freq) ? 0 : freq),
752 (attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE,
753 &cookie) ? 0 : cookie));
Wade Guthrie0d438532012-05-18 14:18:50 -0700754 return output;
755}
756
757const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
758const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
759
760string ConnectMessage::ToString() const {
761 string output(GetHeaderString());
762
763 uint16_t status = UINT16_MAX;
764
repo sync90ee0fa2012-12-18 10:08:08 -0800765 if (!attributes().GetU16AttributeValue(NL80211_ATTR_STATUS_CODE, &status)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700766 output.append("unknown connect status");
767 } else if (status == 0) {
768 output.append("connected");
769 } else {
770 output.append("failed to connect");
771 }
772
repo sync90ee0fa2012-12-18 10:08:08 -0800773 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700774 string mac;
775 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
776 StringAppendF(&output, " to %s", mac.c_str());
777 }
778 if (status)
779 StringAppendF(&output, ", status: %u: %s", status,
780 StringFromStatus(status).c_str());
781 return output;
782}
783
784const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
785const char DeauthenticateMessage::kCommandString[] =
786 "NL80211_CMD_DEAUTHENTICATE";
787
788string DeauthenticateMessage::ToString() const {
789 string output(GetHeaderString());
790 StringAppendF(&output, "deauth%s",
791 StringFromFrame(NL80211_ATTR_FRAME).c_str());
792 return output;
793}
794
795const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
796const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
797
798string DeleteStationMessage::ToString() const {
799 string mac;
800 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
801 string output(GetHeaderString());
802 StringAppendF(&output, "del station %s", mac.c_str());
803 return output;
804}
805
806const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
807const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
808
809string DisassociateMessage::ToString() const {
810 string output(GetHeaderString());
811 StringAppendF(&output, "disassoc%s",
812 StringFromFrame(NL80211_ATTR_FRAME).c_str());
813 return output;
814}
815
816const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
817const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
818
819string DisconnectMessage::ToString() const {
820 string output(GetHeaderString());
821 StringAppendF(&output, "disconnected %s",
repo sync90ee0fa2012-12-18 10:08:08 -0800822 ((attributes().IsFlagAttributeTrue(
823 NL80211_ATTR_DISCONNECTED_BY_AP)) ?
824 "(by AP)" : "(local request)"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700825
826 uint16_t reason = UINT16_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800827 if (attributes().GetU16AttributeValue(NL80211_ATTR_REASON_CODE, &reason)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700828 StringAppendF(&output, " reason: %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700829 reason, StringFromReason(reason).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700830 }
831 return output;
832}
833
repo sync0efa9f02012-12-28 13:40:20 -0800834// An Error is not a GENL message and, as such, has no command.
835const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
836const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
837
838ErrorMessage::ErrorMessage(uint32_t error)
839 : Nl80211Message(kCommand, kCommandString), error_(error) {}
840
841string ErrorMessage::ToString() const {
842 string output;
843 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
844 error_, strerror(error_));
845 return output;
846}
847
Wade Guthrie0d438532012-05-18 14:18:50 -0700848const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
849const char FrameTxStatusMessage::kCommandString[] =
850 "NL80211_CMD_FRAME_TX_STATUS";
851
852string FrameTxStatusMessage::ToString() const {
853 string output(GetHeaderString());
854 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800855 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -0700856
repo sync90ee0fa2012-12-18 10:08:08 -0800857 StringAppendF(&output, "mgmt TX status (cookie %" PRIx64 "): %s", cookie,
858 (attributes().IsFlagAttributeTrue(NL80211_ATTR_ACK) ?
859 "acked" : "no ack"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700860 return output;
861}
862
repo sync0efa9f02012-12-28 13:40:20 -0800863const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
864const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
865
866
Wade Guthrie0d438532012-05-18 14:18:50 -0700867const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
868const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
869
870string JoinIbssMessage::ToString() const {
871 string mac;
872 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
873 string output(GetHeaderString());
874 StringAppendF(&output, "IBSS %s joined", mac.c_str());
875 return output;
876}
877
878const uint8_t MichaelMicFailureMessage::kCommand =
879 NL80211_CMD_MICHAEL_MIC_FAILURE;
880const char MichaelMicFailureMessage::kCommandString[] =
881 "NL80211_CMD_MICHAEL_MIC_FAILURE";
882
883string MichaelMicFailureMessage::ToString() const {
884 string output(GetHeaderString());
885
886 output.append("Michael MIC failure event:");
887
repo sync90ee0fa2012-12-18 10:08:08 -0800888 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700889 string mac;
890 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
891 StringAppendF(&output, " source MAC address %s", mac.c_str());
892 }
893
repo sync90ee0fa2012-12-18 10:08:08 -0800894 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800895 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800896 if (attributes().GetRawAttributeValue(NL80211_ATTR_KEY_SEQ,
897 &rawdata) &&
repo syncdc085c82012-12-28 08:54:41 -0800898 rawdata.GetLength() == Nl80211Message::kEthernetAddressBytes) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800899 const unsigned char *seq = rawdata.GetConstData();
Wade Guthrie0d438532012-05-18 14:18:50 -0700900 StringAppendF(&output, " seq=%02x%02x%02x%02x%02x%02x",
901 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
902 }
903 }
904 uint32_t key_type_val = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800905 if (attributes().GetU32AttributeValue(NL80211_ATTR_KEY_TYPE, &key_type_val)) {
repo sync0efa9f02012-12-28 13:40:20 -0800906 nl80211_key_type key_type = static_cast<nl80211_key_type>(key_type_val);
Wade Guthrie0d438532012-05-18 14:18:50 -0700907 StringAppendF(&output, " Key Type %s", StringFromKeyType(key_type).c_str());
908 }
909
910 uint8_t key_index = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800911 if (attributes().GetU8AttributeValue(NL80211_ATTR_KEY_IDX, &key_index)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700912 StringAppendF(&output, " Key Id %u", key_index);
913 }
914
915 return output;
916}
917
918const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
919const char NewScanResultsMessage::kCommandString[] =
920 "NL80211_CMD_NEW_SCAN_RESULTS";
921
922string NewScanResultsMessage::ToString() const {
923 string output(GetHeaderString());
924 output.append("scan finished");
925
926 {
927 output.append("; frequencies: ");
928 vector<uint32_t> list;
929 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
930 string str;
931 for (vector<uint32_t>::const_iterator i = list.begin();
932 i != list.end(); ++i) {
933 StringAppendF(&str, " %" PRIu32 ", ", *i);
934 }
935 output.append(str);
936 }
937 }
938
939 {
940 output.append("; SSIDs: ");
941 vector<string> list;
942 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
943 string str;
944 for (vector<string>::const_iterator i = list.begin();
945 i != list.end(); ++i) {
946 StringAppendF(&str, "\"%s\", ", i->c_str());
947 }
948 output.append(str);
949 }
950 }
951
952 return output;
953}
954
955const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
956const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
957
958string NewStationMessage::ToString() const {
959 string mac;
960 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
961 string output(GetHeaderString());
962 StringAppendF(&output, "new station %s", mac.c_str());
963
964 return output;
965}
966
967const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
968const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
969
970string NewWifiMessage::ToString() const {
971 string output(GetHeaderString());
972 string wifi_name = "None";
repo sync90ee0fa2012-12-18 10:08:08 -0800973 attributes().GetStringAttributeValue(NL80211_ATTR_WIPHY_NAME, &wifi_name);
Wade Guthrie0d438532012-05-18 14:18:50 -0700974 StringAppendF(&output, "renamed to %s", wifi_name.c_str());
975 return output;
976}
977
repo sync0efa9f02012-12-28 13:40:20 -0800978// A NOOP is not a GENL message and, as such, has no command.
979const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
980const char NoopMessage::kCommandString[] = "NL80211_NOOP";
981
982string NoopMessage::ToString() const {
983 return "NL80211_NOOP";
984}
985
Wade Guthrie0d438532012-05-18 14:18:50 -0700986const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
987const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
988
989string NotifyCqmMessage::ToString() const {
repo sync90ee0fa2012-12-18 10:08:08 -0800990 // TODO(wdg): use attributes().GetNestedAttributeValue()...
repo sync12cca802012-12-19 17:34:22 -0800991 static const nla_policy kCqmValidationPolicy[NL80211_ATTR_CQM_MAX + 1] = {
Wade Guthrie0d438532012-05-18 14:18:50 -0700992 { NLA_U32, 0, 0 }, // Who Knows?
993 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
994 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
995 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
996 };
997
998 string output(GetHeaderString());
999 output.append("connection quality monitor event: ");
1000
repo sync90ee0fa2012-12-18 10:08:08 -08001001 const Nl80211RawAttribute *attribute =
1002 attributes().GetRawAttribute(NL80211_ATTR_CQM);
repo syncd316eb72012-12-10 15:48:47 -08001003 if (!attribute) {
1004 output.append("missing data!");
1005 return output;
1006 }
1007
1008 const nlattr *const_data = attribute->data();
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001009 // Note that |nla_parse_nested| doesn't change |const_data| but doesn't
1010 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001011 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
1012
1013 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
repo sync12cca802012-12-19 17:34:22 -08001014 if (!cqm_attr ||
1015 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
1016 const_cast<nla_policy *>(kCqmValidationPolicy))) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001017 output.append("missing data!");
1018 return output;
1019 }
1020 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
repo sync12cca802012-12-19 17:34:22 -08001021 nl80211_cqm_rssi_threshold_event rssi_event =
1022 static_cast<nl80211_cqm_rssi_threshold_event>(
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001023 Nl80211Attribute::NlaGetU32(
1024 cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -07001025 if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
1026 output.append("RSSI went above threshold");
1027 else
1028 output.append("RSSI went below threshold");
1029 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
repo sync90ee0fa2012-12-18 10:08:08 -08001030 attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001031 string mac;
1032 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1033 StringAppendF(&output, "peer %s didn't ACK %" PRIu32 " packets",
1034 mac.c_str(),
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001035 Nl80211Attribute::NlaGetU32(
1036 cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -07001037 } else {
1038 output.append("unknown event");
1039 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001040 return output;
1041}
1042
1043const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
1044const char PmksaCandidateMessage::kCommandString[] =
1045 "NL80211_ATTR_PMKSA_CANDIDATE";
1046
1047string PmksaCandidateMessage::ToString() const {
1048 string output(GetHeaderString());
1049 output.append("PMKSA candidate found");
1050 return output;
1051}
1052
1053const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
1054const char RegBeaconHintMessage::kCommandString[] =
1055 "NL80211_CMD_REG_BEACON_HINT";
1056
1057string RegBeaconHintMessage::ToString() const {
1058 string output(GetHeaderString());
1059 uint32_t wiphy_idx = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001060 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, &wiphy_idx);
Wade Guthrie0d438532012-05-18 14:18:50 -07001061
repo sync90ee0fa2012-12-18 10:08:08 -08001062 const Nl80211RawAttribute *freq_before =
1063 attributes().GetRawAttribute(NL80211_ATTR_FREQ_BEFORE);
repo syncd316eb72012-12-10 15:48:47 -08001064 if (!freq_before)
1065 return "";
1066 const nlattr *const_before = freq_before->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001067 ieee80211_beacon_channel chan_before_beacon;
1068 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
1069 if (ParseBeaconHintChan(const_before, &chan_before_beacon))
1070 return "";
1071
repo sync90ee0fa2012-12-18 10:08:08 -08001072 const Nl80211RawAttribute *freq_after =
1073 attributes().GetRawAttribute(NL80211_ATTR_FREQ_AFTER);
repo syncd316eb72012-12-10 15:48:47 -08001074 if (!freq_after)
1075 return "";
1076
1077 const nlattr *const_after = freq_after->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001078 ieee80211_beacon_channel chan_after_beacon;
1079 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
1080 if (ParseBeaconHintChan(const_after, &chan_after_beacon))
1081 return "";
1082
1083 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
1084 return "";
1085
1086 /* A beacon hint is sent _only_ if something _did_ change */
1087 output.append("beacon hint:");
1088 StringAppendF(&output, "phy%" PRIu32 " %u MHz [%d]:",
1089 wiphy_idx, chan_before_beacon.center_freq,
1090 ChannelFromIeee80211Frequency(chan_before_beacon.center_freq));
1091
1092 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
1093 output.append("\to active scanning enabled");
1094 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
1095 output.append("\to beaconing enabled");
1096 return output;
1097}
1098
1099int RegBeaconHintMessage::ParseBeaconHintChan(const nlattr *tb,
1100 ieee80211_beacon_channel *chan)
1101 const {
1102 static const nla_policy kBeaconFreqPolicy[
1103 NL80211_FREQUENCY_ATTR_MAX + 1] = {
1104 {0, 0, 0},
1105 { NLA_U32, 0, 0 }, // [NL80211_FREQUENCY_ATTR_FREQ]
1106 {0, 0, 0},
1107 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]
1108 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_NO_IBSS]
1109 };
1110
1111 if (!tb) {
1112 LOG(ERROR) << "|tb| parameter is NULL.";
1113 return -EINVAL;
1114 }
1115
1116 if (!chan) {
1117 LOG(ERROR) << "|chan| parameter is NULL.";
1118 return -EINVAL;
1119 }
1120
1121 nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1122
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001123 // Note that |nla_parse_nested| doesn't change its parameters but doesn't
1124 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001125 if (nla_parse_nested(tb_freq,
1126 NL80211_FREQUENCY_ATTR_MAX,
1127 const_cast<nlattr *>(tb),
1128 const_cast<nla_policy *>(kBeaconFreqPolicy)))
1129 return -EINVAL;
1130
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001131 chan->center_freq = Nl80211Attribute::NlaGetU32(
1132 tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
Wade Guthrie0d438532012-05-18 14:18:50 -07001133
1134 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1135 chan->passive_scan = true;
1136 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1137 chan->no_ibss = true;
1138
1139 return 0;
1140}
1141
1142int RegBeaconHintMessage::ChannelFromIeee80211Frequency(int freq) {
1143 // TODO(wdg): get rid of these magic numbers.
1144 if (freq == 2484)
1145 return 14;
1146
1147 if (freq < 2484)
1148 return (freq - 2407) / 5;
1149
1150 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
1151 return freq/5 - 1000;
1152}
1153
1154const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
1155const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
1156
1157string RegChangeMessage::ToString() const {
1158 string output(GetHeaderString());
1159 output.append("regulatory domain change: ");
1160
1161 uint8_t reg_type = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001162 attributes().GetU8AttributeValue(NL80211_ATTR_REG_TYPE, &reg_type);
Wade Guthrie0d438532012-05-18 14:18:50 -07001163
1164 uint32_t initiator = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001165 attributes().GetU32AttributeValue(NL80211_ATTR_REG_INITIATOR, &initiator);
Wade Guthrie0d438532012-05-18 14:18:50 -07001166
1167 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001168 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
1169 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -07001170
1171 string alpha2 = "<None>";
repo sync90ee0fa2012-12-18 10:08:08 -08001172 attributes().GetStringAttributeValue(NL80211_ATTR_REG_ALPHA2, &alpha2);
Wade Guthrie0d438532012-05-18 14:18:50 -07001173
1174 switch (reg_type) {
1175 case NL80211_REGDOM_TYPE_COUNTRY:
1176 StringAppendF(&output, "set to %s by %s request",
1177 alpha2.c_str(), StringFromRegInitiator(initiator).c_str());
1178 if (wifi_exists)
1179 StringAppendF(&output, " on phy%" PRIu32, wifi);
1180 break;
1181
1182 case NL80211_REGDOM_TYPE_WORLD:
1183 StringAppendF(&output, "set to world roaming by %s request",
1184 StringFromRegInitiator(initiator).c_str());
1185 break;
1186
1187 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
1188 StringAppendF(&output,
1189 "custom world roaming rules in place on phy%" PRIu32
1190 " by %s request",
1191 wifi, StringFromRegInitiator(initiator).c_str());
1192 break;
1193
1194 case NL80211_REGDOM_TYPE_INTERSECTION:
1195 StringAppendF(&output, "intersection used due to a request made by %s",
1196 StringFromRegInitiator(initiator).c_str());
1197 if (wifi_exists)
1198 StringAppendF(&output, " on phy%" PRIu32, wifi);
1199 break;
1200
1201 default:
1202 output.append("unknown source");
1203 break;
1204 }
1205 return output;
1206}
1207
1208const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
1209const char RemainOnChannelMessage::kCommandString[] =
1210 "NL80211_CMD_REMAIN_ON_CHANNEL";
1211
1212string RemainOnChannelMessage::ToString() const {
1213 string output(GetHeaderString());
1214
1215 uint32_t wifi_freq = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001216 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ, &wifi_freq);
Wade Guthrie0d438532012-05-18 14:18:50 -07001217
1218 uint32_t duration = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001219 attributes().GetU32AttributeValue(NL80211_ATTR_DURATION, &duration);
Wade Guthrie0d438532012-05-18 14:18:50 -07001220
1221 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001222 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -07001223
1224 StringAppendF(&output, "remain on freq %" PRIu32 " (%" PRIu32 "ms, cookie %"
1225 PRIx64 ")",
1226 wifi_freq, duration, cookie);
1227 return output;
1228}
1229
1230const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
1231const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
1232
1233string RoamMessage::ToString() const {
1234 string output(GetHeaderString());
1235 output.append("roamed");
1236
repo sync90ee0fa2012-12-18 10:08:08 -08001237 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001238 string mac;
1239 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1240 StringAppendF(&output, " to %s", mac.c_str());
1241 }
1242 return output;
1243}
1244
1245const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
1246const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
1247
1248string ScanAbortedMessage::ToString() const {
1249 string output(GetHeaderString());
1250 output.append("scan aborted");
1251
1252 {
1253 output.append("; frequencies: ");
1254 vector<uint32_t> list;
1255 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1256 string str;
1257 for (vector<uint32_t>::const_iterator i = list.begin();
1258 i != list.end(); ++i) {
1259 StringAppendF(&str, " %" PRIu32 ", ", *i);
1260 }
1261 output.append(str);
1262 }
1263 }
1264
1265 {
1266 output.append("; SSIDs: ");
1267 vector<string> list;
1268 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1269 string str;
1270 for (vector<string>::const_iterator i = list.begin();
1271 i != list.end(); ++i) {
1272 StringAppendF(&str, "\"%s\", ", i->c_str());
1273 }
1274 output.append(str);
1275 }
1276 }
1277
1278 return output;
1279}
1280
1281const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
1282const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
1283
1284string TriggerScanMessage::ToString() const {
1285 string output(GetHeaderString());
1286 output.append("scan started");
1287
1288 {
1289 output.append("; frequencies: ");
1290 vector<uint32_t> list;
1291 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1292 string str;
1293 for (vector<uint32_t>::const_iterator i = list.begin();
1294 i != list.end(); ++i) {
1295 StringAppendF(&str, "%" PRIu32 ", ", *i);
1296 }
1297 output.append(str);
1298 }
1299 }
1300
1301 {
1302 output.append("; SSIDs: ");
1303 vector<string> list;
1304 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1305 string str;
1306 for (vector<string>::const_iterator i = list.begin();
1307 i != list.end(); ++i) {
1308 StringAppendF(&str, "\"%s\", ", i->c_str());
1309 }
1310 output.append(str);
1311 }
1312 }
1313
1314 return output;
1315}
1316
1317const uint8_t UnknownMessage::kCommand = 0xff;
1318const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
1319
1320string UnknownMessage::ToString() const {
1321 string output(GetHeaderString());
1322 StringAppendF(&output, "unknown event %u", command_);
1323 return output;
1324}
1325
1326const uint8_t UnprotDeauthenticateMessage::kCommand =
1327 NL80211_CMD_UNPROT_DEAUTHENTICATE;
1328const char UnprotDeauthenticateMessage::kCommandString[] =
1329 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
1330
1331string UnprotDeauthenticateMessage::ToString() const {
1332 string output(GetHeaderString());
1333 StringAppendF(&output, "unprotected deauth %s",
1334 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1335 return output;
1336}
1337
1338const uint8_t UnprotDisassociateMessage::kCommand =
1339 NL80211_CMD_UNPROT_DISASSOCIATE;
1340const char UnprotDisassociateMessage::kCommandString[] =
1341 "NL80211_CMD_UNPROT_DISASSOCIATE";
1342
1343string UnprotDisassociateMessage::ToString() const {
1344 string output(GetHeaderString());
1345 StringAppendF(&output, "unprotected disassoc %s",
1346 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1347 return output;
1348}
1349
1350//
1351// Factory class.
1352//
1353
repo syncdc085c82012-12-28 08:54:41 -08001354Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001355 if (!msg) {
1356 LOG(ERROR) << "NULL |msg| parameter";
1357 return NULL;
1358 }
1359
repo syncdc085c82012-12-28 08:54:41 -08001360 scoped_ptr<Nl80211Message> message;
repo sync0efa9f02012-12-28 13:40:20 -08001361 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -08001362
repo sync0efa9f02012-12-28 13:40:20 -08001363 if (msg->nlmsg_type == NLMSG_NOOP) {
1364 SLOG(WiFi, 6) << "Creating a NOP message";
1365 message.reset(new NoopMessage());
1366 } else if (msg->nlmsg_type == NLMSG_ERROR) {
1367 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
1368 if (error_code) {
1369 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
1370 message.reset(new ErrorMessage(error_code));
1371 } else {
1372 SLOG(WiFi, 6) << "Creating an ACK message";
1373 message.reset(new AckMessage());
1374 }
1375 } else {
1376 SLOG(WiFi, 6) << "Creating a Regular message";
1377 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -07001378
repo sync0efa9f02012-12-28 13:40:20 -08001379 switch (gnlh->cmd) {
1380 case AssociateMessage::kCommand:
1381 message.reset(new AssociateMessage()); break;
1382 case AuthenticateMessage::kCommand:
1383 message.reset(new AuthenticateMessage()); break;
1384 case CancelRemainOnChannelMessage::kCommand:
1385 message.reset(new CancelRemainOnChannelMessage()); break;
1386 case ConnectMessage::kCommand:
1387 message.reset(new ConnectMessage()); break;
1388 case DeauthenticateMessage::kCommand:
1389 message.reset(new DeauthenticateMessage()); break;
1390 case DeleteStationMessage::kCommand:
1391 message.reset(new DeleteStationMessage()); break;
1392 case DisassociateMessage::kCommand:
1393 message.reset(new DisassociateMessage()); break;
1394 case DisconnectMessage::kCommand:
1395 message.reset(new DisconnectMessage()); break;
1396 case FrameTxStatusMessage::kCommand:
1397 message.reset(new FrameTxStatusMessage()); break;
1398 case GetRegMessage::kCommand:
1399 message.reset(new GetRegMessage()); break;
1400 case JoinIbssMessage::kCommand:
1401 message.reset(new JoinIbssMessage()); break;
1402 case MichaelMicFailureMessage::kCommand:
1403 message.reset(new MichaelMicFailureMessage()); break;
1404 case NewScanResultsMessage::kCommand:
1405 message.reset(new NewScanResultsMessage()); break;
1406 case NewStationMessage::kCommand:
1407 message.reset(new NewStationMessage()); break;
1408 case NewWifiMessage::kCommand:
1409 message.reset(new NewWifiMessage()); break;
1410 case NotifyCqmMessage::kCommand:
1411 message.reset(new NotifyCqmMessage()); break;
1412 case PmksaCandidateMessage::kCommand:
1413 message.reset(new PmksaCandidateMessage()); break;
1414 case RegBeaconHintMessage::kCommand:
1415 message.reset(new RegBeaconHintMessage()); break;
1416 case RegChangeMessage::kCommand:
1417 message.reset(new RegChangeMessage()); break;
1418 case RemainOnChannelMessage::kCommand:
1419 message.reset(new RemainOnChannelMessage()); break;
1420 case RoamMessage::kCommand:
1421 message.reset(new RoamMessage()); break;
1422 case ScanAbortedMessage::kCommand:
1423 message.reset(new ScanAbortedMessage()); break;
1424 case TriggerScanMessage::kCommand:
1425 message.reset(new TriggerScanMessage()); break;
1426 case UnprotDeauthenticateMessage::kCommand:
1427 message.reset(new UnprotDeauthenticateMessage()); break;
1428 case UnprotDisassociateMessage::kCommand:
1429 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001430
repo sync0efa9f02012-12-28 13:40:20 -08001431 default:
1432 message.reset(new UnknownMessage(gnlh->cmd)); break;
1433 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001434
repo sync0efa9f02012-12-28 13:40:20 -08001435 if (!message->InitFromNlmsg(msg)) {
1436 LOG(ERROR) << "Message did not initialize properly";
1437 return NULL;
1438 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001439 }
1440
Wade Guthrie0d438532012-05-18 14:18:50 -07001441 return message.release();
1442}
1443
repo syncdc085c82012-12-28 08:54:41 -08001444Nl80211MessageDataCollector *
1445 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001446 return g_datacollector.Pointer();
1447}
1448
repo syncdc085c82012-12-28 08:54:41 -08001449Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001450 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
1451 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
1452 need_to_print[NL80211_CMD_DEL_STATION] = true;
1453 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
1454 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
1455 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
1456 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
1457 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
1458 need_to_print[NL80211_CMD_REG_CHANGE] = true;
1459 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
1460 need_to_print[NL80211_CMD_ROAM] = true;
1461 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
1462 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
1463 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
1464}
1465
repo syncdc085c82012-12-28 08:54:41 -08001466void Nl80211MessageDataCollector::CollectDebugData(
1467 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001468 if (!msg) {
1469 LOG(ERROR) << "NULL |msg| parameter";
1470 return;
1471 }
1472
1473 bool doit = false;
1474
1475 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -08001476 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -07001477 if (node != need_to_print.end())
1478 doit = node->second;
1479
1480 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001481 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001482 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001483 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001484
Christopher Wileyefd521f2012-11-07 17:32:46 -08001485 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001486
1487 size_t bytes = nlmsg_total_size(payload_bytes);
1488 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001489 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001490 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001491 << std::hex << std::setfill('0') << std::setw(2)
1492 << + rawdata[i] << ",";
1493 }
Wade Guthried6153612012-08-23 11:36:14 -07001494 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001495 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001496 }
1497}
1498
1499} // namespace shill.