blob: 91513787a82f66a4c37711535f993e9b9d0c330c [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"
Darin Petkov50cb78a2013-02-06 16:17:49 +010053#include "shill/wifi.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070054
55using base::LazyInstance;
56using base::StringAppendF;
57using base::StringPrintf;
58using std::map;
59using std::string;
60using std::vector;
61
62namespace shill {
63
64namespace {
repo syncdc085c82012-12-28 08:54:41 -080065LazyInstance<Nl80211MessageDataCollector> g_datacollector =
Wade Guthrie0d438532012-05-18 14:18:50 -070066 LAZY_INSTANCE_INITIALIZER;
67} // namespace
68
repo syncdc085c82012-12-28 08:54:41 -080069const char Nl80211Message::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX";
Wade Guthrie0d438532012-05-18 14:18:50 -070070
71const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
72const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
73
repo syncdc085c82012-12-28 08:54:41 -080074const uint32_t Nl80211Message::kIllegalMessage = 0;
75const unsigned int Nl80211Message::kEthernetAddressBytes = 6;
76map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
77map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
Wade Guthrie0d438532012-05-18 14:18:50 -070078
79// The nl messages look like this:
80//
81// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
82// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
83// +-- gnhl nlmsg_tail(hdr) --+
84// | nlmsg_next(hdr) --+
85// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
86// -----+-----+-+----------------------------------------------+-++----
87// ... | | | payload | ||
88// | | +------+-+--------+-+--------------------------+ ||
89// | nl | | | | | | attribs | ||
90// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
91// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
92// | |d| hdr |d| |d| attr |a| load |a| ... |d||
93// | | | | | | | |d| |d| | ||
94// -----+-----+-+----------------------------------------------+-++----
95// ^ ^ ^ ^
96// | | | XXXXXXX <-- nla_len(nlattr)
97// | | | +-- nla_data(nlattr)
98// | | X-nla_total_size-X
99// | | XXXXX-nlmsg_attrlen-XXXXXX
100// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
101// +-- msg = nlmsg_hdr(raw_message)
102
103//
repo syncdc085c82012-12-28 08:54:41 -0800104// Nl80211Message
Wade Guthrie0d438532012-05-18 14:18:50 -0700105//
106
repo sync0efa9f02012-12-28 13:40:20 -0800107bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) {
108 if (!const_msg) {
109 LOG(ERROR) << "Null |msg| parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700110 return false;
111 }
112
repo sync0efa9f02012-12-28 13:40:20 -0800113 // Netlink header.
114 sequence_number_ = const_msg->nlmsg_seq;
repo syncdc085c82012-12-28 08:54:41 -0800115 SLOG(WiFi, 6) << "NL Message " << sequence_number() << " <===";
Wade Guthrie0d438532012-05-18 14:18:50 -0700116
repo sync0efa9f02012-12-28 13:40:20 -0800117 // Casting away constness, here, since the libnl code doesn't properly label
118 // their stuff as const (even though it is).
119 nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg);
120
121 // Genl message header.
122 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
123
124 // Attributes.
125 // Parse the attributes from the nl message payload into the 'tb' array.
126 nlattr *tb[NL80211_ATTR_MAX + 1];
127 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
128 genlmsg_attrlen(gnlh, 0), NULL);
129
Wade Guthrie0d438532012-05-18 14:18:50 -0700130 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
131 if (tb[i]) {
repo sync0efa9f02012-12-28 13:40:20 -0800132 // TODO(wdg): When Nl80211Messages instantiate their own attributes,
133 // this call should, instead, call |SetAttributeFromNlAttr|.
134 attributes_.CreateAndInitFromNlAttr(static_cast<enum nl80211_attrs>(i),
135 tb[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700136 }
137 }
138
139 // Convert integer values provided by libnl (for example, from the
140 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
141 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700142 if (!reason_code_string_) {
143 reason_code_string_ = new map<uint16_t, string>;
144 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
145 "Unspecified reason";
146 (*reason_code_string_)[
147 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700148 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700149 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
150 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
151 "ESS";
152 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
153 "Disassociated due to inactivity";
154 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
155 "Disassociated because AP is unable to handle all currently associated "
156 "STAs";
157 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
158 "Class 2 frame received from nonauthenticated STA";
159 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
160 "Class 3 frame received from nonassociated STA";
161 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
162 "Disassociated because sending STA is leaving (or has left) BSS";
163 (*reason_code_string_)[
164 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
165 "STA requesting (re)association is not authenticated with responding "
166 "STA";
167 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
168 "Disassociated because the information in the Power Capability "
169 "element is unacceptable";
170 (*reason_code_string_)[
171 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
172 "Disassociated because the information in the Supported Channels "
173 "element is unacceptable";
174 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
175 "Invalid information element, i.e., an information element defined in "
176 "this standard for which the content does not meet the specifications "
177 "in Clause 7";
178 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
179 "Message integrity code (MIC) failure";
180 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
181 "4-Way Handshake timeout";
182 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
183 "Group Key Handshake timeout";
184 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
185 "Information element in 4-Way Handshake different from "
186 "(Re)Association Request/Probe Response/Beacon frame";
187 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
188 "Invalid group cipher";
189 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
190 "Invalid pairwise cipher";
191 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
192 "Invalid AKMP";
193 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
194 "Unsupported RSN information element version";
195 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
196 "Invalid RSN information element capabilities";
197 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
198 "IEEE 802.1X authentication failed";
199 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
200 "Cipher suite rejected because of the security policy";
201 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
202 "Disassociated for unspecified, QoS-related reason";
203 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
204 "Disassociated because QoS AP lacks sufficient bandwidth for this "
205 "QoS STA";
206 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
207 "Disassociated because excessive number of frames need to be "
208 "acknowledged, but are not acknowledged due to AP transmissions "
209 "and/or poor channel conditions";
210 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
211 "Disassociated because STA is transmitting outside the limits of its "
212 "TXOPs";
213 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
214 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
215 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
216 "Requested from peer STA as it does not want to use the mechanism";
217 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
218 "Requested from peer STA as the STA received frames using the "
219 "mechanism for which a setup is required";
220 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
221 "Requested from peer STA due to timeout";
222 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
223 "Peer STA does not support the requested cipher suite";
224 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
225 }
226
227 if (!status_code_string_) {
228 status_code_string_ = new map<uint16_t, string>;
229 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
230 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
231 "Unspecified failure";
232 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700233 "Cannot support all requested capabilities in the capability "
234 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700235 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700236 "Reassociation denied due to inability to confirm that association "
237 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700238 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700239 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700240 (*status_code_string_)[
241 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700242 "Responding station does not support the specified authentication "
243 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700244 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700245 "Received an authentication frame with authentication transaction "
246 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700247 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700248 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700249 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700250 "Authentication rejected due to timeout waiting for next frame in "
251 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700252 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
253 "Association denied because AP is unable to handle additional "
254 "associated STA";
255 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700256 "Association denied due to requesting station not supporting all of "
257 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700258 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700259 "Association denied due to requesting station not supporting the "
260 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700261 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700262 "Association denied due to requesting station not supporting the PBCC "
263 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700264 (*status_code_string_)[
265 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700266 "Association denied due to requesting station not supporting the "
267 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700268 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700269 "Association request rejected because Spectrum Management capability "
270 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700271 (*status_code_string_)[
272 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700273 "Association request rejected because the information in the Power "
274 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700275 (*status_code_string_)[
276 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700277 "Association request rejected because the information in the "
278 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700279 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700280 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700281 "supporting the Short Slot Time option";
282 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
283 "Association request rejected due to requesting station not "
284 "supporting the DSSS-OFDM option";
285 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700286 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700287 (*status_code_string_)[
288 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700289 "Association denied due to QAP having insufficient bandwidth to handle "
290 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700291 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700292 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700293 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
294 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700295 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700296 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
297 "The request has been declined";
298 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700299 "The request has not been successful as one or more parameters have "
300 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700301 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
302 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700303 "However, a suggested Tspec is provided so that the initiating QSTA "
304 "may attempt to send another TS with the suggested changes to the "
305 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700306 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700307 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700308 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
309 "Invalid Group Cipher";
310 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
311 "Invalid Pairwise Cipher";
312 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
313 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
314 "Unsupported RSN Information Element version";
315 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
316 "Invalid RSN Information Element Capabilities";
317 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700318 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700319 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
320 "The TS has not been created. However, the HC may be capable of "
321 "creating a TS, in response to a request, after the time indicated in "
322 "the TS Delay element";
323 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700324 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700325 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
326 "Destination STA is not present within this BSS";
327 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
328 "The destination STA is not a QoS STA";
329 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700330 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700331 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700332 }
333
334 return true;
335}
336
Wade Guthrie0d438532012-05-18 14:18:50 -0700337// Helper function to provide a string for a MAC address.
repo syncdc085c82012-12-28 08:54:41 -0800338bool Nl80211Message::GetMacAttributeString(nl80211_attrs id,
339 string *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700340 if (!value) {
341 LOG(ERROR) << "Null |value| parameter";
342 return false;
343 }
344
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800345 ByteString data;
repo sync12cca802012-12-19 17:34:22 -0800346 if (!attributes().GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700347 value->assign(kBogusMacAddress);
348 return false;
349 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800350 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700351
352 return true;
353}
354
355// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
repo syncdc085c82012-12-28 08:54:41 -0800356bool Nl80211Message::GetScanFrequenciesAttribute(
repo sync12cca802012-12-19 17:34:22 -0800357 nl80211_attrs id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700358 if (!value) {
359 LOG(ERROR) << "Null |value| parameter";
360 return false;
361 }
362
363 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800364 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800365 if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800366 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700367
repo syncd316eb72012-12-10 15:48:47 -0800368 nlattr *nst = NULL;
369 // |nla_for_each_attr| requires a non-const parameter even though it
370 // doesn't change the data.
371 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
372 int rem_nst;
373 int len = rawdata.GetLength();
374
375 nla_for_each_attr(nst, attr_data, len, rem_nst) {
376 value->push_back(Nl80211Attribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700377 }
repo syncd316eb72012-12-10 15:48:47 -0800378 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700379}
380
381// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800382bool Nl80211Message::GetScanSsidsAttribute(
383 nl80211_attrs id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700384 if (!value) {
385 LOG(ERROR) << "Null |value| parameter";
386 return false;
387 }
388
repo sync90ee0fa2012-12-18 10:08:08 -0800389 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800390 if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty())
repo sync90ee0fa2012-12-18 10:08:08 -0800391 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700392
repo sync90ee0fa2012-12-18 10:08:08 -0800393 nlattr *nst = NULL;
394 // |nla_for_each_attr| requires a non-const parameter even though it
395 // doesn't change the data.
396 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
397 int rem_nst;
398 int len = rawdata.GetLength();
399
400 nla_for_each_attr(nst, data, len, rem_nst) {
401 value->push_back(StringFromSsid(nla_len(nst),
402 reinterpret_cast<const uint8_t *>(
403 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700404 }
repo sync90ee0fa2012-12-18 10:08:08 -0800405 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700406}
407
Wade Guthrie0d438532012-05-18 14:18:50 -0700408// Protected members.
409
repo syncdc085c82012-12-28 08:54:41 -0800410string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700411 char ifname[IF_NAMESIZE] = "";
412 uint32_t ifindex = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800413 bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
414 &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700415
416 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800417 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
418 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700419
420 string output;
421 if (ifindex_exists && wifi_exists) {
422 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
423 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
424 wifi);
425 } else if (ifindex_exists) {
426 StringAppendF(&output, "%s: ",
427 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
428 } else if (wifi_exists) {
429 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
430 }
431
432 return output;
433}
434
repo syncdc085c82012-12-28 08:54:41 -0800435string Nl80211Message::StringFromFrame(nl80211_attrs attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700436 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800437 ByteString frame_data;
repo sync90ee0fa2012-12-18 10:08:08 -0800438 if (attributes().GetRawAttributeValue(attr_name,
439 &frame_data) && !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800440 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700441 frame.ToString(&output);
442 } else {
443 output.append(" [no frame]");
444 }
445
446 return output;
447}
448
449// static
repo syncdc085c82012-12-28 08:54:41 -0800450string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700451 switch (key_type) {
452 case NL80211_KEYTYPE_GROUP:
453 return "Group";
454 case NL80211_KEYTYPE_PAIRWISE:
455 return "Pairwise";
456 case NL80211_KEYTYPE_PEERKEY:
457 return "PeerKey";
458 default:
459 return "<Unknown Key Type>";
460 }
461}
462
463// static
repo syncdc085c82012-12-28 08:54:41 -0800464string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700465 string output;
466
467 if (!arg) {
468 output = kBogusMacAddress;
469 LOG(ERROR) << "|arg| parameter is NULL.";
470 } else {
471 StringAppendF(&output, "%02x", arg[0]);
472
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800473 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700474 StringAppendF(&output, ":%02x", arg[i]);
475 }
476 }
477 return output;
478}
479
480// static
repo syncdc085c82012-12-28 08:54:41 -0800481string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700482 switch (initiator) {
483 case NL80211_REGDOM_SET_BY_CORE:
484 return "the wireless core upon initialization";
485 case NL80211_REGDOM_SET_BY_USER:
486 return "a user";
487 case NL80211_REGDOM_SET_BY_DRIVER:
488 return "a driver";
489 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
490 return "a country IE";
491 default:
492 return "<Unknown Reg Initiator>";
493 }
494}
495
496// static
repo syncdc085c82012-12-28 08:54:41 -0800497string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700498 const uint8_t *data) {
499 string output;
500 if (!data) {
501 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
502 LOG(ERROR) << "|data| parameter is NULL.";
503 return output;
504 }
505
506 for (int i = 0; i < len; ++i) {
507 if (data[i] == ' ')
508 output.append(" ");
509 else if (isprint(data[i]))
510 StringAppendF(&output, "%c", static_cast<char>(data[i]));
511 else
512 StringAppendF(&output, "\\x%2x", data[i]);
513 }
514
515 return output;
516}
517
518// static
repo syncdc085c82012-12-28 08:54:41 -0800519string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700520 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700521 match = reason_code_string_->find(status);
522 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700523 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700524 if (status < IEEE_80211::kReasonCodeMax) {
525 StringAppendF(&output, "<Reserved Reason:%u>", status);
526 } else {
527 StringAppendF(&output, "<Unknown Reason:%u>", status);
528 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700529 return output;
530 }
531 return match->second;
532}
533
Wade Guthried4977f22012-08-22 12:37:54 -0700534// static
repo syncdc085c82012-12-28 08:54:41 -0800535string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700536 map<uint16_t, string>::const_iterator match;
537 match = status_code_string_->find(status);
538 if (match == status_code_string_->end()) {
539 string output;
540 if (status < IEEE_80211::kStatusCodeMax) {
541 StringAppendF(&output, "<Reserved Status:%u>", status);
542 } else {
543 StringAppendF(&output, "<Unknown Status:%u>", status);
544 }
545 return output;
546 }
547 return match->second;
548}
549
repo syncdc085c82012-12-28 08:54:41 -0800550string Nl80211Message::GenericToString() const {
repo sync1538d442012-12-20 15:24:35 -0800551 string output;
repo syncdc085c82012-12-28 08:54:41 -0800552 StringAppendF(&output, "Message %s (%d)\n",
repo sync1538d442012-12-20 15:24:35 -0800553 message_type_string(), message_type());
554 StringAppendF(&output, "%s", attributes_.ToString().c_str());
555 return output;
556}
557
Darin Petkov50cb78a2013-02-06 16:17:49 +0100558string Nl80211Message::GetScanFrequenciesAttributeAsString() const {
559 string output = "frequencies: ";
560 vector<uint32_t> list;
561 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
562 string str;
563 for (vector<uint32_t>::const_iterator i = list.begin();
564 i != list.end(); ++i) {
565 StringAppendF(&str, " %" PRIu32 ", ", *i);
566 }
567 output.append(str);
568 }
569 return output;
570}
571
572string Nl80211Message::GetScanSsidsAttributeAsString() const {
573 string output = "SSIDs: ";
574 vector<string> list;
575 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
576 string str;
577 for (vector<string>::const_iterator i = list.begin();
578 i != list.end(); ++i) {
579 StringAppendF(&str, "%s, ", WiFi::LogSSID(*i).c_str());
580 }
581 output.append(str);
582 }
583 return output;
584}
585
repo syncdc085c82012-12-28 08:54:41 -0800586ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const {
587 // Build netlink header.
588 nlmsghdr header;
589 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
590 header.nlmsg_len = nlmsghdr_with_pad;
591 header.nlmsg_type = nlmsg_type;
592 header.nlmsg_flags = NLM_F_REQUEST;
593 header.nlmsg_seq = sequence_number();
594 header.nlmsg_pid = getpid();
595
596 // Build genl message header.
597 genlmsghdr genl_header;
598 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
599 header.nlmsg_len += genlmsghdr_with_pad;
600 genl_header.cmd = message_type();
601 genl_header.version = 1;
602 genl_header.reserved = 0;
603
604 // Assemble attributes (padding is included by AttributeList::Encode).
605 ByteString attributes = attributes_.Encode();
606 header.nlmsg_len += attributes.GetLength();
607
608 // Now that we know the total message size, build the output ByteString.
609 ByteString result;
610
611 // Netlink header + pad.
612 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
613 sizeof(header)));
614 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
615
616 // Genl message header + pad.
617 result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header),
618 sizeof(genl_header)));
619 result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill.
620
621 // Attributes including pad.
622 result.Append(attributes);
623
624 return result;
625}
626
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800627Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700628 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800629 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700630 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800631 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
632 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700633
634 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800635 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700636 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800637 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
638 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700639 frame_type_ = frame->frame_control & kFrameTypeMask;
640
641 switch (frame_type_) {
642 case kAssocResponseFrameType:
643 case kReassocResponseFrameType:
644 status_ = le16toh(frame->u.associate_response.status_code);
645 break;
646
647 case kAuthFrameType:
648 status_ = le16toh(frame->u.authentiate_message.status_code);
649 break;
650
651 case kDisassocFrameType:
652 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700653 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700654 break;
655
656 default:
657 break;
658 }
659 }
660}
661
Wade Guthried4977f22012-08-22 12:37:54 -0700662bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700663 if (!output) {
664 LOG(ERROR) << "NULL |output|";
665 return false;
666 }
667
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800668 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700669 output->append(" [no frame]");
670 return true;
671 }
672
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800673 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700674 output->append(" [invalid frame: ");
675 } else {
676 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
677
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800678 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700679 case kAssocResponseFrameType:
680 StringAppendF(output, "; AssocResponse status: %u: %s",
681 status_,
repo syncdc085c82012-12-28 08:54:41 -0800682 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700683 break;
684 case kReassocResponseFrameType:
685 StringAppendF(output, "; ReassocResponse status: %u: %s",
686 status_,
repo syncdc085c82012-12-28 08:54:41 -0800687 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700688 break;
689 case kAuthFrameType:
690 StringAppendF(output, "; Auth status: %u: %s",
691 status_,
repo syncdc085c82012-12-28 08:54:41 -0800692 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700693 break;
694
695 case kDisassocFrameType:
696 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700697 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800698 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700699 break;
700 case kDeauthFrameType:
701 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700702 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800703 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700704 break;
705
706 default:
707 break;
708 }
709 output->append(" [frame: ");
710 }
711
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800712 const unsigned char *frame = frame_.GetConstData();
713 for (size_t i = 0; i < frame_.GetLength(); ++i) {
714 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700715 }
716 output->append("]");
717
718 return true;
719}
720
Wade Guthried4977f22012-08-22 12:37:54 -0700721bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800722 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700723}
724
Wade Guthried4977f22012-08-22 12:37:54 -0700725
Wade Guthrie0d438532012-05-18 14:18:50 -0700726//
repo syncdc085c82012-12-28 08:54:41 -0800727// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700728//
729
repo sync0efa9f02012-12-28 13:40:20 -0800730// An Ack is not a GENL message and, as such, has no command.
731const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
732const char AckMessage::kCommandString[] = "NL80211_ACK";
733
734string AckMessage::ToString() const {
735 return "NL80211_ACK";
736}
737
Wade Guthrie0d438532012-05-18 14:18:50 -0700738const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
739const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
740
741string AssociateMessage::ToString() const {
742 string output(GetHeaderString());
743 output.append("assoc");
repo sync90ee0fa2012-12-18 10:08:08 -0800744 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL))
Wade Guthrie0d438532012-05-18 14:18:50 -0700745 output.append(StringFromFrame(NL80211_ATTR_FRAME));
repo sync90ee0fa2012-12-18 10:08:08 -0800746 else if (attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT))
Wade Guthrie0d438532012-05-18 14:18:50 -0700747 output.append(": timed out");
748 else
749 output.append(": unknown event");
750 return output;
751}
752
753const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
754const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
755
756string AuthenticateMessage::ToString() const {
757 string output(GetHeaderString());
758 output.append("auth");
repo sync90ee0fa2012-12-18 10:08:08 -0800759 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700760 output.append(StringFromFrame(NL80211_ATTR_FRAME));
761 } else {
repo sync90ee0fa2012-12-18 10:08:08 -0800762 output.append(attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT) ?
Wade Guthrie0d438532012-05-18 14:18:50 -0700763 ": timed out" : ": unknown event");
764 }
765 return output;
766}
767
768const uint8_t CancelRemainOnChannelMessage::kCommand =
769 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
770const char CancelRemainOnChannelMessage::kCommandString[] =
771 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
772
773string CancelRemainOnChannelMessage::ToString() const {
774 string output(GetHeaderString());
775 uint32_t freq;
776 uint64_t cookie;
777 StringAppendF(&output,
778 "done with remain on freq %" PRIu32 " (cookie %" PRIx64 ")",
repo sync90ee0fa2012-12-18 10:08:08 -0800779 (attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ,
780 &freq) ? 0 : freq),
781 (attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE,
782 &cookie) ? 0 : cookie));
Wade Guthrie0d438532012-05-18 14:18:50 -0700783 return output;
784}
785
786const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
787const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
788
789string ConnectMessage::ToString() const {
790 string output(GetHeaderString());
791
792 uint16_t status = UINT16_MAX;
793
repo sync90ee0fa2012-12-18 10:08:08 -0800794 if (!attributes().GetU16AttributeValue(NL80211_ATTR_STATUS_CODE, &status)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700795 output.append("unknown connect status");
796 } else if (status == 0) {
797 output.append("connected");
798 } else {
799 output.append("failed to connect");
800 }
801
repo sync90ee0fa2012-12-18 10:08:08 -0800802 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700803 string mac;
804 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
805 StringAppendF(&output, " to %s", mac.c_str());
806 }
807 if (status)
808 StringAppendF(&output, ", status: %u: %s", status,
809 StringFromStatus(status).c_str());
810 return output;
811}
812
813const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
814const char DeauthenticateMessage::kCommandString[] =
815 "NL80211_CMD_DEAUTHENTICATE";
816
817string DeauthenticateMessage::ToString() const {
818 string output(GetHeaderString());
819 StringAppendF(&output, "deauth%s",
820 StringFromFrame(NL80211_ATTR_FRAME).c_str());
821 return output;
822}
823
824const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
825const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
826
827string DeleteStationMessage::ToString() const {
828 string mac;
829 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
830 string output(GetHeaderString());
831 StringAppendF(&output, "del station %s", mac.c_str());
832 return output;
833}
834
835const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
836const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
837
838string DisassociateMessage::ToString() const {
839 string output(GetHeaderString());
840 StringAppendF(&output, "disassoc%s",
841 StringFromFrame(NL80211_ATTR_FRAME).c_str());
842 return output;
843}
844
845const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
846const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
847
848string DisconnectMessage::ToString() const {
849 string output(GetHeaderString());
850 StringAppendF(&output, "disconnected %s",
repo sync90ee0fa2012-12-18 10:08:08 -0800851 ((attributes().IsFlagAttributeTrue(
852 NL80211_ATTR_DISCONNECTED_BY_AP)) ?
853 "(by AP)" : "(local request)"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700854
855 uint16_t reason = UINT16_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800856 if (attributes().GetU16AttributeValue(NL80211_ATTR_REASON_CODE, &reason)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700857 StringAppendF(&output, " reason: %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700858 reason, StringFromReason(reason).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700859 }
860 return output;
861}
862
repo sync0efa9f02012-12-28 13:40:20 -0800863// An Error is not a GENL message and, as such, has no command.
864const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
865const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
866
867ErrorMessage::ErrorMessage(uint32_t error)
868 : Nl80211Message(kCommand, kCommandString), error_(error) {}
869
870string ErrorMessage::ToString() const {
871 string output;
872 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
873 error_, strerror(error_));
874 return output;
875}
876
Wade Guthrie0d438532012-05-18 14:18:50 -0700877const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
878const char FrameTxStatusMessage::kCommandString[] =
879 "NL80211_CMD_FRAME_TX_STATUS";
880
881string FrameTxStatusMessage::ToString() const {
882 string output(GetHeaderString());
883 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800884 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -0700885
repo sync90ee0fa2012-12-18 10:08:08 -0800886 StringAppendF(&output, "mgmt TX status (cookie %" PRIx64 "): %s", cookie,
887 (attributes().IsFlagAttributeTrue(NL80211_ATTR_ACK) ?
888 "acked" : "no ack"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700889 return output;
890}
891
repo sync0efa9f02012-12-28 13:40:20 -0800892const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
893const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
894
895
Wade Guthrie0d438532012-05-18 14:18:50 -0700896const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
897const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
898
899string JoinIbssMessage::ToString() const {
900 string mac;
901 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
902 string output(GetHeaderString());
903 StringAppendF(&output, "IBSS %s joined", mac.c_str());
904 return output;
905}
906
907const uint8_t MichaelMicFailureMessage::kCommand =
908 NL80211_CMD_MICHAEL_MIC_FAILURE;
909const char MichaelMicFailureMessage::kCommandString[] =
910 "NL80211_CMD_MICHAEL_MIC_FAILURE";
911
912string MichaelMicFailureMessage::ToString() const {
913 string output(GetHeaderString());
914
915 output.append("Michael MIC failure event:");
916
repo sync90ee0fa2012-12-18 10:08:08 -0800917 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700918 string mac;
919 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
920 StringAppendF(&output, " source MAC address %s", mac.c_str());
921 }
922
repo sync90ee0fa2012-12-18 10:08:08 -0800923 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800924 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800925 if (attributes().GetRawAttributeValue(NL80211_ATTR_KEY_SEQ,
926 &rawdata) &&
repo syncdc085c82012-12-28 08:54:41 -0800927 rawdata.GetLength() == Nl80211Message::kEthernetAddressBytes) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800928 const unsigned char *seq = rawdata.GetConstData();
Wade Guthrie0d438532012-05-18 14:18:50 -0700929 StringAppendF(&output, " seq=%02x%02x%02x%02x%02x%02x",
930 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
931 }
932 }
933 uint32_t key_type_val = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800934 if (attributes().GetU32AttributeValue(NL80211_ATTR_KEY_TYPE, &key_type_val)) {
repo sync0efa9f02012-12-28 13:40:20 -0800935 nl80211_key_type key_type = static_cast<nl80211_key_type>(key_type_val);
Wade Guthrie0d438532012-05-18 14:18:50 -0700936 StringAppendF(&output, " Key Type %s", StringFromKeyType(key_type).c_str());
937 }
938
939 uint8_t key_index = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800940 if (attributes().GetU8AttributeValue(NL80211_ATTR_KEY_IDX, &key_index)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700941 StringAppendF(&output, " Key Id %u", key_index);
942 }
943
944 return output;
945}
946
947const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
948const char NewScanResultsMessage::kCommandString[] =
949 "NL80211_CMD_NEW_SCAN_RESULTS";
950
951string NewScanResultsMessage::ToString() const {
952 string output(GetHeaderString());
953 output.append("scan finished");
Darin Petkov50cb78a2013-02-06 16:17:49 +0100954 output.append("; " + GetScanFrequenciesAttributeAsString());
955 output.append("; " + GetScanSsidsAttributeAsString());
Wade Guthrie0d438532012-05-18 14:18:50 -0700956 return output;
957}
958
959const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
960const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
961
962string NewStationMessage::ToString() const {
963 string mac;
964 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
965 string output(GetHeaderString());
966 StringAppendF(&output, "new station %s", mac.c_str());
967
968 return output;
969}
970
971const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
972const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
973
974string NewWifiMessage::ToString() const {
975 string output(GetHeaderString());
976 string wifi_name = "None";
repo sync90ee0fa2012-12-18 10:08:08 -0800977 attributes().GetStringAttributeValue(NL80211_ATTR_WIPHY_NAME, &wifi_name);
Wade Guthrie0d438532012-05-18 14:18:50 -0700978 StringAppendF(&output, "renamed to %s", wifi_name.c_str());
979 return output;
980}
981
repo sync0efa9f02012-12-28 13:40:20 -0800982// A NOOP is not a GENL message and, as such, has no command.
983const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
984const char NoopMessage::kCommandString[] = "NL80211_NOOP";
985
986string NoopMessage::ToString() const {
987 return "NL80211_NOOP";
988}
989
Wade Guthrie0d438532012-05-18 14:18:50 -0700990const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
991const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
992
993string NotifyCqmMessage::ToString() const {
repo sync90ee0fa2012-12-18 10:08:08 -0800994 // TODO(wdg): use attributes().GetNestedAttributeValue()...
repo sync12cca802012-12-19 17:34:22 -0800995 static const nla_policy kCqmValidationPolicy[NL80211_ATTR_CQM_MAX + 1] = {
Wade Guthrie0d438532012-05-18 14:18:50 -0700996 { NLA_U32, 0, 0 }, // Who Knows?
997 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
998 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
999 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
1000 };
1001
1002 string output(GetHeaderString());
1003 output.append("connection quality monitor event: ");
1004
repo sync90ee0fa2012-12-18 10:08:08 -08001005 const Nl80211RawAttribute *attribute =
1006 attributes().GetRawAttribute(NL80211_ATTR_CQM);
repo syncd316eb72012-12-10 15:48:47 -08001007 if (!attribute) {
1008 output.append("missing data!");
1009 return output;
1010 }
1011
1012 const nlattr *const_data = attribute->data();
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001013 // Note that |nla_parse_nested| doesn't change |const_data| but doesn't
1014 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001015 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
1016
1017 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
repo sync12cca802012-12-19 17:34:22 -08001018 if (!cqm_attr ||
1019 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
1020 const_cast<nla_policy *>(kCqmValidationPolicy))) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001021 output.append("missing data!");
1022 return output;
1023 }
1024 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
repo sync12cca802012-12-19 17:34:22 -08001025 nl80211_cqm_rssi_threshold_event rssi_event =
1026 static_cast<nl80211_cqm_rssi_threshold_event>(
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001027 Nl80211Attribute::NlaGetU32(
1028 cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -07001029 if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
1030 output.append("RSSI went above threshold");
1031 else
1032 output.append("RSSI went below threshold");
1033 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
repo sync90ee0fa2012-12-18 10:08:08 -08001034 attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001035 string mac;
1036 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1037 StringAppendF(&output, "peer %s didn't ACK %" PRIu32 " packets",
1038 mac.c_str(),
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001039 Nl80211Attribute::NlaGetU32(
1040 cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -07001041 } else {
1042 output.append("unknown event");
1043 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001044 return output;
1045}
1046
1047const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
1048const char PmksaCandidateMessage::kCommandString[] =
1049 "NL80211_ATTR_PMKSA_CANDIDATE";
1050
1051string PmksaCandidateMessage::ToString() const {
1052 string output(GetHeaderString());
1053 output.append("PMKSA candidate found");
1054 return output;
1055}
1056
1057const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
1058const char RegBeaconHintMessage::kCommandString[] =
1059 "NL80211_CMD_REG_BEACON_HINT";
1060
1061string RegBeaconHintMessage::ToString() const {
1062 string output(GetHeaderString());
1063 uint32_t wiphy_idx = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001064 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, &wiphy_idx);
Wade Guthrie0d438532012-05-18 14:18:50 -07001065
repo sync90ee0fa2012-12-18 10:08:08 -08001066 const Nl80211RawAttribute *freq_before =
1067 attributes().GetRawAttribute(NL80211_ATTR_FREQ_BEFORE);
repo syncd316eb72012-12-10 15:48:47 -08001068 if (!freq_before)
1069 return "";
1070 const nlattr *const_before = freq_before->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001071 ieee80211_beacon_channel chan_before_beacon;
1072 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
1073 if (ParseBeaconHintChan(const_before, &chan_before_beacon))
1074 return "";
1075
repo sync90ee0fa2012-12-18 10:08:08 -08001076 const Nl80211RawAttribute *freq_after =
1077 attributes().GetRawAttribute(NL80211_ATTR_FREQ_AFTER);
repo syncd316eb72012-12-10 15:48:47 -08001078 if (!freq_after)
1079 return "";
1080
1081 const nlattr *const_after = freq_after->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001082 ieee80211_beacon_channel chan_after_beacon;
1083 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
1084 if (ParseBeaconHintChan(const_after, &chan_after_beacon))
1085 return "";
1086
1087 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
1088 return "";
1089
1090 /* A beacon hint is sent _only_ if something _did_ change */
1091 output.append("beacon hint:");
1092 StringAppendF(&output, "phy%" PRIu32 " %u MHz [%d]:",
1093 wiphy_idx, chan_before_beacon.center_freq,
1094 ChannelFromIeee80211Frequency(chan_before_beacon.center_freq));
1095
1096 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
1097 output.append("\to active scanning enabled");
1098 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
1099 output.append("\to beaconing enabled");
1100 return output;
1101}
1102
1103int RegBeaconHintMessage::ParseBeaconHintChan(const nlattr *tb,
1104 ieee80211_beacon_channel *chan)
1105 const {
1106 static const nla_policy kBeaconFreqPolicy[
1107 NL80211_FREQUENCY_ATTR_MAX + 1] = {
1108 {0, 0, 0},
1109 { NLA_U32, 0, 0 }, // [NL80211_FREQUENCY_ATTR_FREQ]
1110 {0, 0, 0},
1111 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]
1112 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_NO_IBSS]
1113 };
1114
1115 if (!tb) {
1116 LOG(ERROR) << "|tb| parameter is NULL.";
1117 return -EINVAL;
1118 }
1119
1120 if (!chan) {
1121 LOG(ERROR) << "|chan| parameter is NULL.";
1122 return -EINVAL;
1123 }
1124
1125 nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1126
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001127 // Note that |nla_parse_nested| doesn't change its parameters but doesn't
1128 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001129 if (nla_parse_nested(tb_freq,
1130 NL80211_FREQUENCY_ATTR_MAX,
1131 const_cast<nlattr *>(tb),
1132 const_cast<nla_policy *>(kBeaconFreqPolicy)))
1133 return -EINVAL;
1134
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001135 chan->center_freq = Nl80211Attribute::NlaGetU32(
1136 tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
Wade Guthrie0d438532012-05-18 14:18:50 -07001137
1138 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1139 chan->passive_scan = true;
1140 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1141 chan->no_ibss = true;
1142
1143 return 0;
1144}
1145
1146int RegBeaconHintMessage::ChannelFromIeee80211Frequency(int freq) {
1147 // TODO(wdg): get rid of these magic numbers.
1148 if (freq == 2484)
1149 return 14;
1150
1151 if (freq < 2484)
1152 return (freq - 2407) / 5;
1153
1154 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
1155 return freq/5 - 1000;
1156}
1157
1158const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
1159const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
1160
1161string RegChangeMessage::ToString() const {
1162 string output(GetHeaderString());
1163 output.append("regulatory domain change: ");
1164
1165 uint8_t reg_type = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001166 attributes().GetU8AttributeValue(NL80211_ATTR_REG_TYPE, &reg_type);
Wade Guthrie0d438532012-05-18 14:18:50 -07001167
1168 uint32_t initiator = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001169 attributes().GetU32AttributeValue(NL80211_ATTR_REG_INITIATOR, &initiator);
Wade Guthrie0d438532012-05-18 14:18:50 -07001170
1171 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001172 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
1173 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -07001174
1175 string alpha2 = "<None>";
repo sync90ee0fa2012-12-18 10:08:08 -08001176 attributes().GetStringAttributeValue(NL80211_ATTR_REG_ALPHA2, &alpha2);
Wade Guthrie0d438532012-05-18 14:18:50 -07001177
1178 switch (reg_type) {
1179 case NL80211_REGDOM_TYPE_COUNTRY:
1180 StringAppendF(&output, "set to %s by %s request",
1181 alpha2.c_str(), StringFromRegInitiator(initiator).c_str());
1182 if (wifi_exists)
1183 StringAppendF(&output, " on phy%" PRIu32, wifi);
1184 break;
1185
1186 case NL80211_REGDOM_TYPE_WORLD:
1187 StringAppendF(&output, "set to world roaming by %s request",
1188 StringFromRegInitiator(initiator).c_str());
1189 break;
1190
1191 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
1192 StringAppendF(&output,
1193 "custom world roaming rules in place on phy%" PRIu32
1194 " by %s request",
1195 wifi, StringFromRegInitiator(initiator).c_str());
1196 break;
1197
1198 case NL80211_REGDOM_TYPE_INTERSECTION:
1199 StringAppendF(&output, "intersection used due to a request made by %s",
1200 StringFromRegInitiator(initiator).c_str());
1201 if (wifi_exists)
1202 StringAppendF(&output, " on phy%" PRIu32, wifi);
1203 break;
1204
1205 default:
1206 output.append("unknown source");
1207 break;
1208 }
1209 return output;
1210}
1211
1212const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
1213const char RemainOnChannelMessage::kCommandString[] =
1214 "NL80211_CMD_REMAIN_ON_CHANNEL";
1215
1216string RemainOnChannelMessage::ToString() const {
1217 string output(GetHeaderString());
1218
1219 uint32_t wifi_freq = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001220 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ, &wifi_freq);
Wade Guthrie0d438532012-05-18 14:18:50 -07001221
1222 uint32_t duration = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001223 attributes().GetU32AttributeValue(NL80211_ATTR_DURATION, &duration);
Wade Guthrie0d438532012-05-18 14:18:50 -07001224
1225 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001226 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -07001227
1228 StringAppendF(&output, "remain on freq %" PRIu32 " (%" PRIu32 "ms, cookie %"
1229 PRIx64 ")",
1230 wifi_freq, duration, cookie);
1231 return output;
1232}
1233
1234const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
1235const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
1236
1237string RoamMessage::ToString() const {
1238 string output(GetHeaderString());
1239 output.append("roamed");
1240
repo sync90ee0fa2012-12-18 10:08:08 -08001241 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001242 string mac;
1243 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1244 StringAppendF(&output, " to %s", mac.c_str());
1245 }
1246 return output;
1247}
1248
1249const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
1250const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
1251
1252string ScanAbortedMessage::ToString() const {
1253 string output(GetHeaderString());
1254 output.append("scan aborted");
Darin Petkov50cb78a2013-02-06 16:17:49 +01001255 output.append("; " + GetScanFrequenciesAttributeAsString());
1256 output.append("; " + GetScanSsidsAttributeAsString());
Wade Guthrie0d438532012-05-18 14:18:50 -07001257 return output;
1258}
1259
1260const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
1261const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
1262
1263string TriggerScanMessage::ToString() const {
1264 string output(GetHeaderString());
1265 output.append("scan started");
Darin Petkov50cb78a2013-02-06 16:17:49 +01001266 output.append("; " + GetScanFrequenciesAttributeAsString());
1267 output.append("; " + GetScanSsidsAttributeAsString());
Wade Guthrie0d438532012-05-18 14:18:50 -07001268 return output;
1269}
1270
1271const uint8_t UnknownMessage::kCommand = 0xff;
1272const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
1273
1274string UnknownMessage::ToString() const {
1275 string output(GetHeaderString());
1276 StringAppendF(&output, "unknown event %u", command_);
1277 return output;
1278}
1279
1280const uint8_t UnprotDeauthenticateMessage::kCommand =
1281 NL80211_CMD_UNPROT_DEAUTHENTICATE;
1282const char UnprotDeauthenticateMessage::kCommandString[] =
1283 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
1284
1285string UnprotDeauthenticateMessage::ToString() const {
1286 string output(GetHeaderString());
1287 StringAppendF(&output, "unprotected deauth %s",
1288 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1289 return output;
1290}
1291
1292const uint8_t UnprotDisassociateMessage::kCommand =
1293 NL80211_CMD_UNPROT_DISASSOCIATE;
1294const char UnprotDisassociateMessage::kCommandString[] =
1295 "NL80211_CMD_UNPROT_DISASSOCIATE";
1296
1297string UnprotDisassociateMessage::ToString() const {
1298 string output(GetHeaderString());
1299 StringAppendF(&output, "unprotected disassoc %s",
1300 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1301 return output;
1302}
1303
1304//
1305// Factory class.
1306//
1307
repo syncdc085c82012-12-28 08:54:41 -08001308Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001309 if (!msg) {
1310 LOG(ERROR) << "NULL |msg| parameter";
1311 return NULL;
1312 }
1313
repo syncdc085c82012-12-28 08:54:41 -08001314 scoped_ptr<Nl80211Message> message;
repo sync0efa9f02012-12-28 13:40:20 -08001315 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -08001316
repo sync0efa9f02012-12-28 13:40:20 -08001317 if (msg->nlmsg_type == NLMSG_NOOP) {
1318 SLOG(WiFi, 6) << "Creating a NOP message";
1319 message.reset(new NoopMessage());
1320 } else if (msg->nlmsg_type == NLMSG_ERROR) {
1321 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
1322 if (error_code) {
1323 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
1324 message.reset(new ErrorMessage(error_code));
1325 } else {
1326 SLOG(WiFi, 6) << "Creating an ACK message";
1327 message.reset(new AckMessage());
1328 }
1329 } else {
1330 SLOG(WiFi, 6) << "Creating a Regular message";
1331 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -07001332
repo sync0efa9f02012-12-28 13:40:20 -08001333 switch (gnlh->cmd) {
1334 case AssociateMessage::kCommand:
1335 message.reset(new AssociateMessage()); break;
1336 case AuthenticateMessage::kCommand:
1337 message.reset(new AuthenticateMessage()); break;
1338 case CancelRemainOnChannelMessage::kCommand:
1339 message.reset(new CancelRemainOnChannelMessage()); break;
1340 case ConnectMessage::kCommand:
1341 message.reset(new ConnectMessage()); break;
1342 case DeauthenticateMessage::kCommand:
1343 message.reset(new DeauthenticateMessage()); break;
1344 case DeleteStationMessage::kCommand:
1345 message.reset(new DeleteStationMessage()); break;
1346 case DisassociateMessage::kCommand:
1347 message.reset(new DisassociateMessage()); break;
1348 case DisconnectMessage::kCommand:
1349 message.reset(new DisconnectMessage()); break;
1350 case FrameTxStatusMessage::kCommand:
1351 message.reset(new FrameTxStatusMessage()); break;
1352 case GetRegMessage::kCommand:
1353 message.reset(new GetRegMessage()); break;
1354 case JoinIbssMessage::kCommand:
1355 message.reset(new JoinIbssMessage()); break;
1356 case MichaelMicFailureMessage::kCommand:
1357 message.reset(new MichaelMicFailureMessage()); break;
1358 case NewScanResultsMessage::kCommand:
1359 message.reset(new NewScanResultsMessage()); break;
1360 case NewStationMessage::kCommand:
1361 message.reset(new NewStationMessage()); break;
1362 case NewWifiMessage::kCommand:
1363 message.reset(new NewWifiMessage()); break;
1364 case NotifyCqmMessage::kCommand:
1365 message.reset(new NotifyCqmMessage()); break;
1366 case PmksaCandidateMessage::kCommand:
1367 message.reset(new PmksaCandidateMessage()); break;
1368 case RegBeaconHintMessage::kCommand:
1369 message.reset(new RegBeaconHintMessage()); break;
1370 case RegChangeMessage::kCommand:
1371 message.reset(new RegChangeMessage()); break;
1372 case RemainOnChannelMessage::kCommand:
1373 message.reset(new RemainOnChannelMessage()); break;
1374 case RoamMessage::kCommand:
1375 message.reset(new RoamMessage()); break;
1376 case ScanAbortedMessage::kCommand:
1377 message.reset(new ScanAbortedMessage()); break;
1378 case TriggerScanMessage::kCommand:
1379 message.reset(new TriggerScanMessage()); break;
1380 case UnprotDeauthenticateMessage::kCommand:
1381 message.reset(new UnprotDeauthenticateMessage()); break;
1382 case UnprotDisassociateMessage::kCommand:
1383 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001384
repo sync0efa9f02012-12-28 13:40:20 -08001385 default:
1386 message.reset(new UnknownMessage(gnlh->cmd)); break;
1387 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001388
repo sync0efa9f02012-12-28 13:40:20 -08001389 if (!message->InitFromNlmsg(msg)) {
1390 LOG(ERROR) << "Message did not initialize properly";
1391 return NULL;
1392 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001393 }
1394
Wade Guthrie0d438532012-05-18 14:18:50 -07001395 return message.release();
1396}
1397
repo syncdc085c82012-12-28 08:54:41 -08001398Nl80211MessageDataCollector *
1399 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001400 return g_datacollector.Pointer();
1401}
1402
repo syncdc085c82012-12-28 08:54:41 -08001403Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001404 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
1405 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
1406 need_to_print[NL80211_CMD_DEL_STATION] = true;
1407 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
1408 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
1409 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
1410 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
1411 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
1412 need_to_print[NL80211_CMD_REG_CHANGE] = true;
1413 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
1414 need_to_print[NL80211_CMD_ROAM] = true;
1415 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
1416 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
1417 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
1418}
1419
repo syncdc085c82012-12-28 08:54:41 -08001420void Nl80211MessageDataCollector::CollectDebugData(
1421 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001422 if (!msg) {
1423 LOG(ERROR) << "NULL |msg| parameter";
1424 return;
1425 }
1426
1427 bool doit = false;
1428
1429 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -08001430 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -07001431 if (node != need_to_print.end())
1432 doit = node->second;
1433
1434 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001435 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001436 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001437 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001438
Christopher Wileyefd521f2012-11-07 17:32:46 -08001439 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001440
1441 size_t bytes = nlmsg_total_size(payload_bytes);
1442 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001443 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001444 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001445 << std::hex << std::setfill('0') << std::setw(2)
1446 << + rawdata[i] << ",";
1447 }
Wade Guthried6153612012-08-23 11:36:14 -07001448 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001449 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001450 }
1451}
1452
1453} // namespace shill.