blob: 8c5af6794120c723a0e526c5232e4724e0c0fb46 [file] [log] [blame]
Wade Guthrie0d438532012-05-18 14:18:50 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This code is derived from the 'iw' source code. The copyright and license
6// of that code is as follows:
7//
8// Copyright (c) 2007, 2008 Johannes Berg
9// Copyright (c) 2007 Andy Lutomirski
10// Copyright (c) 2007 Mike Kershaw
11// Copyright (c) 2008-2009 Luis R. Rodriguez
12//
13// Permission to use, copy, modify, and/or distribute this software for any
14// purpose with or without fee is hereby granted, provided that the above
15// copyright notice and this permission notice appear in all copies.
16//
17// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25#include "shill/user_bound_nlmessage.h"
26
27#include <ctype.h>
28#include <endian.h>
29#include <errno.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070030#include <linux/nl80211.h>
31#include <net/if.h>
Wade Guthrie8343f7f2012-12-04 13:52:32 -080032#include <netinet/in.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070033#include <netlink/attr.h>
34#include <netlink/genl/ctrl.h>
35#include <netlink/genl/family.h>
36#include <netlink/genl/genl.h>
37#include <netlink/msg.h>
38#include <netlink/netlink.h>
39
40#include <iomanip>
41#include <string>
42
43#include <base/format_macros.h>
44#include <base/stl_util.h>
45#include <base/stringprintf.h>
46
47#include "shill/ieee80211.h"
48#include "shill/logging.h"
Wade Guthrie8343f7f2012-12-04 13:52:32 -080049#include "shill/nl80211_attribute.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070050#include "shill/scope_logger.h"
51
52using base::LazyInstance;
53using base::StringAppendF;
54using base::StringPrintf;
55using std::map;
56using std::string;
57using std::vector;
58
59namespace shill {
60
61namespace {
62LazyInstance<UserBoundNlMessageDataCollector> g_datacollector =
63 LAZY_INSTANCE_INITIALIZER;
64} // namespace
65
Wade Guthrie0d438532012-05-18 14:18:50 -070066const char UserBoundNlMessage::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX";
67
68const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
69const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
70
71const uint32_t UserBoundNlMessage::kIllegalMessage = 0xFFFFFFFF;
Wade Guthrie8343f7f2012-12-04 13:52:32 -080072const unsigned int UserBoundNlMessage::kEthernetAddressBytes = 6;
Wade Guthried4977f22012-08-22 12:37:54 -070073map<uint16_t, string> *UserBoundNlMessage::reason_code_string_ = NULL;
74map<uint16_t, string> *UserBoundNlMessage::status_code_string_ = NULL;
Wade Guthrie0d438532012-05-18 14:18:50 -070075
76// The nl messages look like this:
77//
78// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
79// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
80// +-- gnhl nlmsg_tail(hdr) --+
81// | nlmsg_next(hdr) --+
82// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
83// -----+-----+-+----------------------------------------------+-++----
84// ... | | | payload | ||
85// | | +------+-+--------+-+--------------------------+ ||
86// | nl | | | | | | attribs | ||
87// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
88// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
89// | |d| hdr |d| |d| attr |a| load |a| ... |d||
90// | | | | | | | |d| |d| | ||
91// -----+-----+-+----------------------------------------------+-++----
92// ^ ^ ^ ^
93// | | | XXXXXXX <-- nla_len(nlattr)
94// | | | +-- nla_data(nlattr)
95// | | X-nla_total_size-X
96// | | XXXXX-nlmsg_attrlen-XXXXXX
97// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
98// +-- msg = nlmsg_hdr(raw_message)
99
100//
101// UserBoundNlMessage
102//
103
104UserBoundNlMessage::~UserBoundNlMessage() {
repo syncd316eb72012-12-10 15:48:47 -0800105 map<nl80211_attrs, Nl80211Attribute *>::iterator i;
Wade Guthrie0d438532012-05-18 14:18:50 -0700106 for (i = attributes_.begin(); i != attributes_.end(); ++i) {
repo syncd316eb72012-12-10 15:48:47 -0800107 delete i->second;
Wade Guthrie0d438532012-05-18 14:18:50 -0700108 }
109}
110
111bool UserBoundNlMessage::Init(nlattr *tb[NL80211_ATTR_MAX + 1],
112 nlmsghdr *msg) {
113 if (!tb) {
114 LOG(ERROR) << "Null |tb| parameter";
115 return false;
116 }
117
118 message_ = msg;
119
120 SLOG(WiFi, 6) << "NL Message " << GetId() << " <===";
121
122 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
123 if (tb[i]) {
repo syncd316eb72012-12-10 15:48:47 -0800124 Nl80211Attribute *attribute =
125 Nl80211Attribute::NewFromNlAttr(static_cast<enum nl80211_attrs>(i),
126 tb[i]);
127 AddAttribute(attribute);
Wade Guthrie0d438532012-05-18 14:18:50 -0700128 }
129 }
130
131 // Convert integer values provided by libnl (for example, from the
132 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
133 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700134 if (!reason_code_string_) {
135 reason_code_string_ = new map<uint16_t, string>;
136 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
137 "Unspecified reason";
138 (*reason_code_string_)[
139 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700140 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700141 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
142 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
143 "ESS";
144 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
145 "Disassociated due to inactivity";
146 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
147 "Disassociated because AP is unable to handle all currently associated "
148 "STAs";
149 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
150 "Class 2 frame received from nonauthenticated STA";
151 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
152 "Class 3 frame received from nonassociated STA";
153 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
154 "Disassociated because sending STA is leaving (or has left) BSS";
155 (*reason_code_string_)[
156 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
157 "STA requesting (re)association is not authenticated with responding "
158 "STA";
159 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
160 "Disassociated because the information in the Power Capability "
161 "element is unacceptable";
162 (*reason_code_string_)[
163 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
164 "Disassociated because the information in the Supported Channels "
165 "element is unacceptable";
166 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
167 "Invalid information element, i.e., an information element defined in "
168 "this standard for which the content does not meet the specifications "
169 "in Clause 7";
170 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
171 "Message integrity code (MIC) failure";
172 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
173 "4-Way Handshake timeout";
174 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
175 "Group Key Handshake timeout";
176 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
177 "Information element in 4-Way Handshake different from "
178 "(Re)Association Request/Probe Response/Beacon frame";
179 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
180 "Invalid group cipher";
181 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
182 "Invalid pairwise cipher";
183 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
184 "Invalid AKMP";
185 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
186 "Unsupported RSN information element version";
187 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
188 "Invalid RSN information element capabilities";
189 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
190 "IEEE 802.1X authentication failed";
191 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
192 "Cipher suite rejected because of the security policy";
193 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
194 "Disassociated for unspecified, QoS-related reason";
195 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
196 "Disassociated because QoS AP lacks sufficient bandwidth for this "
197 "QoS STA";
198 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
199 "Disassociated because excessive number of frames need to be "
200 "acknowledged, but are not acknowledged due to AP transmissions "
201 "and/or poor channel conditions";
202 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
203 "Disassociated because STA is transmitting outside the limits of its "
204 "TXOPs";
205 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
206 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
207 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
208 "Requested from peer STA as it does not want to use the mechanism";
209 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
210 "Requested from peer STA as the STA received frames using the "
211 "mechanism for which a setup is required";
212 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
213 "Requested from peer STA due to timeout";
214 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
215 "Peer STA does not support the requested cipher suite";
216 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
217 }
218
219 if (!status_code_string_) {
220 status_code_string_ = new map<uint16_t, string>;
221 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
222 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
223 "Unspecified failure";
224 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700225 "Cannot support all requested capabilities in the capability "
226 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700227 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700228 "Reassociation denied due to inability to confirm that association "
229 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700230 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700231 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700232 (*status_code_string_)[
233 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700234 "Responding station does not support the specified authentication "
235 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700236 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700237 "Received an authentication frame with authentication transaction "
238 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700239 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700240 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700241 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700242 "Authentication rejected due to timeout waiting for next frame in "
243 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700244 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
245 "Association denied because AP is unable to handle additional "
246 "associated STA";
247 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700248 "Association denied due to requesting station not supporting all of "
249 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700250 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700251 "Association denied due to requesting station not supporting the "
252 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700253 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700254 "Association denied due to requesting station not supporting the PBCC "
255 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700256 (*status_code_string_)[
257 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700258 "Association denied due to requesting station not supporting the "
259 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700260 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700261 "Association request rejected because Spectrum Management capability "
262 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700263 (*status_code_string_)[
264 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700265 "Association request rejected because the information in the Power "
266 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700267 (*status_code_string_)[
268 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700269 "Association request rejected because the information in the "
270 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700271 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700272 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700273 "supporting the Short Slot Time option";
274 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
275 "Association request rejected due to requesting station not "
276 "supporting the DSSS-OFDM option";
277 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700278 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700279 (*status_code_string_)[
280 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700281 "Association denied due to QAP having insufficient bandwidth to handle "
282 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700283 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700284 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700285 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
286 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700287 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700288 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
289 "The request has been declined";
290 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700291 "The request has not been successful as one or more parameters have "
292 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700293 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
294 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700295 "However, a suggested Tspec is provided so that the initiating QSTA "
296 "may attempt to send another TS with the suggested changes to the "
297 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700298 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700299 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700300 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
301 "Invalid Group Cipher";
302 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
303 "Invalid Pairwise Cipher";
304 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
305 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
306 "Unsupported RSN Information Element version";
307 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
308 "Invalid RSN Information Element Capabilities";
309 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700310 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700311 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
312 "The TS has not been created. However, the HC may be capable of "
313 "creating a TS, in response to a request, after the time indicated in "
314 "the TS Delay element";
315 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700316 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700317 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
318 "Destination STA is not present within this BSS";
319 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
320 "The destination STA is not a QoS STA";
321 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700322 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700323 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700324 }
325
326 return true;
327}
328
repo syncd316eb72012-12-10 15:48:47 -0800329UserBoundNlMessage::AttributeIterator
330 UserBoundNlMessage::GetAttributeIterator() const {
331 return UserBoundNlMessage::AttributeIterator(attributes_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700332}
333
334// Return true if the attribute is in our map, regardless of the value of
335// the attribute, itself.
336bool UserBoundNlMessage::AttributeExists(enum nl80211_attrs name) const {
337 return ContainsKey(attributes_, name);
338}
339
340uint32_t UserBoundNlMessage::GetId() const {
341 if (!message_) {
342 return kIllegalMessage;
343 }
344 return message_->nlmsg_seq;
345}
346
Wade Guthrie0d438532012-05-18 14:18:50 -0700347// Returns the raw attribute data but not the header.
348bool UserBoundNlMessage::GetRawAttributeData(enum nl80211_attrs name,
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800349 ByteString *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700350 if (!value) {
351 LOG(ERROR) << "Null |value| parameter";
352 return false;
353 }
repo syncd316eb72012-12-10 15:48:47 -0800354 const Nl80211Attribute *raw_attr = GetAttribute(name);
355 if (!raw_attr) {
356 LOG(ERROR) << "No attribute - returning FALSE";
Wade Guthrie0d438532012-05-18 14:18:50 -0700357 return false;
358 }
repo syncd316eb72012-12-10 15:48:47 -0800359 if (raw_attr->type() != Nl80211Attribute::kTypeRaw) {
360 LOG(ERROR) << "Attribute with name " << raw_attr->name_string()
361 << " has type " << raw_attr->type_string()
362 << " rather than RAW";
363 return false;
364 }
365 const Nl80211RawAttribute *attr
366 = reinterpret_cast<const Nl80211RawAttribute *>(raw_attr);
367
368 ByteString raw_data;
369 if (!attr->GetRawValue(&raw_data))
370 return false;
371
372 const nlattr *const_data =
373 reinterpret_cast<const nlattr *>(raw_data.GetConstData());
374 // nla_data and nla_len don't change their parameters but don't declare
375 // them to be const. Hence the cast.
376 nlattr *data_nlattr = const_cast<nlattr *>(const_data);
377 *value = ByteString(
378 reinterpret_cast<unsigned char *>(nla_data(data_nlattr)),
379 nla_len(data_nlattr));
Wade Guthrie0d438532012-05-18 14:18:50 -0700380 return true;
381}
382
383bool UserBoundNlMessage::GetStringAttribute(enum nl80211_attrs name,
384 string *value) const {
385 if (!value) {
386 LOG(ERROR) << "Null |value| parameter";
387 return false;
388 }
389
repo syncd316eb72012-12-10 15:48:47 -0800390 const Nl80211Attribute *raw_attr = GetAttribute(name);
391 if (!raw_attr) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700392 return false;
393 }
repo syncd316eb72012-12-10 15:48:47 -0800394 if (raw_attr->type() != Nl80211Attribute::kTypeString) {
395 LOG(ERROR) << "Attribute with name " << raw_attr->name_string()
396 << " has type " << raw_attr->type_string()
397 << " rather than STRING";
398 return false;
399 }
400 const Nl80211StringAttribute *attr
401 = reinterpret_cast<const Nl80211StringAttribute *>(raw_attr);
402 return attr->GetStringValue(value);
Wade Guthrie0d438532012-05-18 14:18:50 -0700403}
404
405bool UserBoundNlMessage::GetU8Attribute(enum nl80211_attrs name,
406 uint8_t *value) const {
407 if (!value) {
408 LOG(ERROR) << "Null |value| parameter";
409 return false;
410 }
411
repo syncd316eb72012-12-10 15:48:47 -0800412 const Nl80211Attribute *raw_attr = GetAttribute(name);
413 if (!raw_attr) {
414 LOG(ERROR) << "No attribute - returning FALSE";
Wade Guthrie0d438532012-05-18 14:18:50 -0700415 return false;
416 }
repo syncd316eb72012-12-10 15:48:47 -0800417 if (raw_attr->type() != Nl80211Attribute::kTypeU8) {
418 LOG(ERROR) << "Attribute with name " << raw_attr->name_string()
419 << " has type " << raw_attr->type_string()
420 << " rather than U8";
421 return false;
422 }
423 const Nl80211U8Attribute *attr
424 = reinterpret_cast<const Nl80211U8Attribute *>(raw_attr);
425 return attr->GetU8Value(value);
Wade Guthrie0d438532012-05-18 14:18:50 -0700426}
427
428bool UserBoundNlMessage::GetU16Attribute(enum nl80211_attrs name,
429 uint16_t *value) const {
430 if (!value) {
431 LOG(ERROR) << "Null |value| parameter";
432 return false;
433 }
434
repo syncd316eb72012-12-10 15:48:47 -0800435 const Nl80211Attribute *raw_attr = GetAttribute(name);
436 if (!raw_attr) {
437 LOG(ERROR) << "No attribute - returning FALSE";
Wade Guthrie0d438532012-05-18 14:18:50 -0700438 return false;
439 }
repo syncd316eb72012-12-10 15:48:47 -0800440 if (raw_attr->type() != Nl80211Attribute::kTypeU16) {
441 LOG(ERROR) << "Attribute with name " << raw_attr->name_string()
442 << " has type " << raw_attr->type_string()
443 << " rather than U16";
444 return false;
445 }
446 const Nl80211U16Attribute *attr
447 = reinterpret_cast<const Nl80211U16Attribute *>(raw_attr);
448 return attr->GetU16Value(value);
Wade Guthrie0d438532012-05-18 14:18:50 -0700449}
450
451bool UserBoundNlMessage::GetU32Attribute(enum nl80211_attrs name,
452 uint32_t *value) const {
453 if (!value) {
454 LOG(ERROR) << "Null |value| parameter";
455 return false;
456 }
457
repo syncd316eb72012-12-10 15:48:47 -0800458 const Nl80211Attribute *raw_attr = GetAttribute(name);
459 if (!raw_attr) {
460 LOG(ERROR) << "No attribute - returning FALSE";
Wade Guthrie0d438532012-05-18 14:18:50 -0700461 return false;
462 }
repo syncd316eb72012-12-10 15:48:47 -0800463 if (raw_attr->type() != Nl80211Attribute::kTypeU32) {
464 LOG(ERROR) << "Attribute with name " << raw_attr->name_string()
465 << " has type " << raw_attr->type_string()
466 << " rather than U32";
467 return false;
468 }
469 const Nl80211U32Attribute *attr
470 = reinterpret_cast<const Nl80211U32Attribute *>(raw_attr);
471 return attr->GetU32Value(value);
Wade Guthrie0d438532012-05-18 14:18:50 -0700472}
473
474bool UserBoundNlMessage::GetU64Attribute(enum nl80211_attrs name,
475 uint64_t *value) const {
476 if (!value) {
477 LOG(ERROR) << "Null |value| parameter";
478 return false;
479 }
480
repo syncd316eb72012-12-10 15:48:47 -0800481 const Nl80211Attribute *raw_attr = GetAttribute(name);
482 if (!raw_attr) {
483 LOG(ERROR) << "No attribute - returning FALSE";
Wade Guthrie0d438532012-05-18 14:18:50 -0700484 return false;
485 }
repo syncd316eb72012-12-10 15:48:47 -0800486 if (raw_attr->type() != Nl80211Attribute::kTypeU64) {
487 LOG(ERROR) << "Attribute with name " << raw_attr->name_string()
488 << " has type " << raw_attr->type_string()
489 << " rather than U64";
490 return false;
491 }
492 const Nl80211U64Attribute *attr
493 = reinterpret_cast<const Nl80211U64Attribute *>(raw_attr);
494 return attr->GetU64Value(value);
Wade Guthrie0d438532012-05-18 14:18:50 -0700495}
496
497// Helper function to provide a string for a MAC address.
498bool UserBoundNlMessage::GetMacAttributeString(enum nl80211_attrs name,
499 string *value) const {
500 if (!value) {
501 LOG(ERROR) << "Null |value| parameter";
502 return false;
503 }
504
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800505 ByteString data;
506 if (!GetRawAttributeData(name, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700507 value->assign(kBogusMacAddress);
508 return false;
509 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800510 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700511
512 return true;
513}
514
515// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
516bool UserBoundNlMessage::GetScanFrequenciesAttribute(
517 enum nl80211_attrs name, vector<uint32_t> *value) const {
518 if (!value) {
519 LOG(ERROR) << "Null |value| parameter";
520 return false;
521 }
522
523 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800524 ByteString rawdata;
525 if (!GetRawAttributeData(name, &rawdata) && !rawdata.IsEmpty())
526 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700527
repo syncd316eb72012-12-10 15:48:47 -0800528 nlattr *nst = NULL;
529 // |nla_for_each_attr| requires a non-const parameter even though it
530 // doesn't change the data.
531 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
532 int rem_nst;
533 int len = rawdata.GetLength();
534
535 nla_for_each_attr(nst, attr_data, len, rem_nst) {
536 value->push_back(Nl80211Attribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700537 }
repo syncd316eb72012-12-10 15:48:47 -0800538 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700539}
540
541// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
542bool UserBoundNlMessage::GetScanSsidsAttribute(
543 enum nl80211_attrs name, vector<string> *value) const {
544 if (!value) {
545 LOG(ERROR) << "Null |value| parameter";
546 return false;
547 }
548
549 if (AttributeExists(name)) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800550 ByteString rawdata;
551 if (GetRawAttributeData(name, &rawdata) && !rawdata.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700552 nlattr *nst = NULL;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800553 // |nla_for_each_attr| requires a non-const parameter even though it
554 // doesn't change the data.
555 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700556 int rem_nst;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800557 int len = rawdata.GetLength();
Wade Guthrie0d438532012-05-18 14:18:50 -0700558
559 nla_for_each_attr(nst, data, len, rem_nst) {
560 value->push_back(StringFromSsid(nla_len(nst),
561 reinterpret_cast<const uint8_t *>(
562 nla_data(nst))).c_str());
563 }
564 }
565 return true;
566 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700567 return false;
568}
569
Wade Guthrie0d438532012-05-18 14:18:50 -0700570// Protected members.
571
repo syncd316eb72012-12-10 15:48:47 -0800572bool UserBoundNlMessage::AddAttribute(Nl80211Attribute *attr) {
573 if (!attr) {
574 LOG(ERROR) << "Not adding NULL attribute";
Wade Guthrie0d438532012-05-18 14:18:50 -0700575 return false;
576 }
repo syncd316eb72012-12-10 15:48:47 -0800577 if (ContainsKey(attributes_, attr->name())) {
578 LOG(ERROR) << "Already have attribute name " << attr->name_string();
579 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700580 }
repo syncd316eb72012-12-10 15:48:47 -0800581 attributes_[attr->name()] = attr;
Wade Guthrie0d438532012-05-18 14:18:50 -0700582 return true;
583}
584
repo syncd316eb72012-12-10 15:48:47 -0800585const Nl80211Attribute *UserBoundNlMessage::GetAttribute(nl80211_attrs name)
Wade Guthrie0d438532012-05-18 14:18:50 -0700586 const {
repo syncd316eb72012-12-10 15:48:47 -0800587 map<nl80211_attrs, Nl80211Attribute *>::const_iterator match;
Wade Guthrie0d438532012-05-18 14:18:50 -0700588 match = attributes_.find(name);
589 // This method may be called to explore the existence of the attribute so
590 // we'll not emit an error if it's not found.
591 if (match == attributes_.end()) {
592 return NULL;
593 }
594 return match->second;
595}
596
597string UserBoundNlMessage::GetHeaderString() const {
598 char ifname[IF_NAMESIZE] = "";
599 uint32_t ifindex = UINT32_MAX;
600 bool ifindex_exists = GetU32Attribute(NL80211_ATTR_IFINDEX, &ifindex);
601
602 uint32_t wifi = UINT32_MAX;
603 bool wifi_exists = GetU32Attribute(NL80211_ATTR_WIPHY, &wifi);
604
605 string output;
606 if (ifindex_exists && wifi_exists) {
607 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
608 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
609 wifi);
610 } else if (ifindex_exists) {
611 StringAppendF(&output, "%s: ",
612 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
613 } else if (wifi_exists) {
614 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
615 }
616
617 return output;
618}
619
620string UserBoundNlMessage::StringFromFrame(enum nl80211_attrs attr_name) const {
621 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800622 ByteString frame_data;
623 if (GetRawAttributeData(attr_name, &frame_data) && !frame_data.IsEmpty()) {
624 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700625 frame.ToString(&output);
626 } else {
627 output.append(" [no frame]");
628 }
629
630 return output;
631}
632
633// static
634string UserBoundNlMessage::StringFromKeyType(nl80211_key_type key_type) {
635 switch (key_type) {
636 case NL80211_KEYTYPE_GROUP:
637 return "Group";
638 case NL80211_KEYTYPE_PAIRWISE:
639 return "Pairwise";
640 case NL80211_KEYTYPE_PEERKEY:
641 return "PeerKey";
642 default:
643 return "<Unknown Key Type>";
644 }
645}
646
647// static
648string UserBoundNlMessage::StringFromMacAddress(const uint8_t *arg) {
649 string output;
650
651 if (!arg) {
652 output = kBogusMacAddress;
653 LOG(ERROR) << "|arg| parameter is NULL.";
654 } else {
655 StringAppendF(&output, "%02x", arg[0]);
656
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800657 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700658 StringAppendF(&output, ":%02x", arg[i]);
659 }
660 }
661 return output;
662}
663
664// static
665string UserBoundNlMessage::StringFromRegInitiator(__u8 initiator) {
666 switch (initiator) {
667 case NL80211_REGDOM_SET_BY_CORE:
668 return "the wireless core upon initialization";
669 case NL80211_REGDOM_SET_BY_USER:
670 return "a user";
671 case NL80211_REGDOM_SET_BY_DRIVER:
672 return "a driver";
673 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
674 return "a country IE";
675 default:
676 return "<Unknown Reg Initiator>";
677 }
678}
679
680// static
681string UserBoundNlMessage::StringFromSsid(const uint8_t len,
682 const uint8_t *data) {
683 string output;
684 if (!data) {
685 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
686 LOG(ERROR) << "|data| parameter is NULL.";
687 return output;
688 }
689
690 for (int i = 0; i < len; ++i) {
691 if (data[i] == ' ')
692 output.append(" ");
693 else if (isprint(data[i]))
694 StringAppendF(&output, "%c", static_cast<char>(data[i]));
695 else
696 StringAppendF(&output, "\\x%2x", data[i]);
697 }
698
699 return output;
700}
701
702// static
Wade Guthried4977f22012-08-22 12:37:54 -0700703string UserBoundNlMessage::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700704 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700705 match = reason_code_string_->find(status);
706 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700707 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700708 if (status < IEEE_80211::kReasonCodeMax) {
709 StringAppendF(&output, "<Reserved Reason:%u>", status);
710 } else {
711 StringAppendF(&output, "<Unknown Reason:%u>", status);
712 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700713 return output;
714 }
715 return match->second;
716}
717
Wade Guthried4977f22012-08-22 12:37:54 -0700718// static
719string UserBoundNlMessage::StringFromStatus(uint16_t status) {
720 map<uint16_t, string>::const_iterator match;
721 match = status_code_string_->find(status);
722 if (match == status_code_string_->end()) {
723 string output;
724 if (status < IEEE_80211::kStatusCodeMax) {
725 StringAppendF(&output, "<Reserved Status:%u>", status);
726 } else {
727 StringAppendF(&output, "<Unknown Status:%u>", status);
728 }
729 return output;
730 }
731 return match->second;
732}
733
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800734Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700735 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800736 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700737 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800738 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
739 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700740
741 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800742 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700743 mac_from_ =
744 UserBoundNlMessage::StringFromMacAddress(&frame->destination_mac[0]);
745 mac_to_ = UserBoundNlMessage::StringFromMacAddress(&frame->source_mac[0]);
746 frame_type_ = frame->frame_control & kFrameTypeMask;
747
748 switch (frame_type_) {
749 case kAssocResponseFrameType:
750 case kReassocResponseFrameType:
751 status_ = le16toh(frame->u.associate_response.status_code);
752 break;
753
754 case kAuthFrameType:
755 status_ = le16toh(frame->u.authentiate_message.status_code);
756 break;
757
758 case kDisassocFrameType:
759 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700760 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700761 break;
762
763 default:
764 break;
765 }
766 }
767}
768
Wade Guthried4977f22012-08-22 12:37:54 -0700769bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700770 if (!output) {
771 LOG(ERROR) << "NULL |output|";
772 return false;
773 }
774
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800775 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700776 output->append(" [no frame]");
777 return true;
778 }
779
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800780 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700781 output->append(" [invalid frame: ");
782 } else {
783 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
784
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800785 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700786 case kAssocResponseFrameType:
787 StringAppendF(output, "; AssocResponse status: %u: %s",
788 status_,
789 UserBoundNlMessage::StringFromStatus(status_).c_str());
790 break;
791 case kReassocResponseFrameType:
792 StringAppendF(output, "; ReassocResponse status: %u: %s",
793 status_,
794 UserBoundNlMessage::StringFromStatus(status_).c_str());
795 break;
796 case kAuthFrameType:
797 StringAppendF(output, "; Auth status: %u: %s",
798 status_,
799 UserBoundNlMessage::StringFromStatus(status_).c_str());
800 break;
801
802 case kDisassocFrameType:
803 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700804 reason_,
805 UserBoundNlMessage::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700806 break;
807 case kDeauthFrameType:
808 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700809 reason_,
810 UserBoundNlMessage::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700811 break;
812
813 default:
814 break;
815 }
816 output->append(" [frame: ");
817 }
818
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800819 const unsigned char *frame = frame_.GetConstData();
820 for (size_t i = 0; i < frame_.GetLength(); ++i) {
821 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700822 }
823 output->append("]");
824
825 return true;
826}
827
Wade Guthried4977f22012-08-22 12:37:54 -0700828bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800829 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700830}
831
Wade Guthried4977f22012-08-22 12:37:54 -0700832
Wade Guthrie0d438532012-05-18 14:18:50 -0700833//
834// Specific UserBoundNlMessage types.
835//
836
837const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
838const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
839
840string AssociateMessage::ToString() const {
841 string output(GetHeaderString());
842 output.append("assoc");
843 if (AttributeExists(NL80211_ATTR_FRAME))
844 output.append(StringFromFrame(NL80211_ATTR_FRAME));
845 else if (AttributeExists(NL80211_ATTR_TIMED_OUT))
846 output.append(": timed out");
847 else
848 output.append(": unknown event");
849 return output;
850}
851
852const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
853const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
854
855string AuthenticateMessage::ToString() const {
856 string output(GetHeaderString());
857 output.append("auth");
858 if (AttributeExists(NL80211_ATTR_FRAME)) {
859 output.append(StringFromFrame(NL80211_ATTR_FRAME));
860 } else {
861 output.append(AttributeExists(NL80211_ATTR_TIMED_OUT) ?
862 ": timed out" : ": unknown event");
863 }
864 return output;
865}
866
867const uint8_t CancelRemainOnChannelMessage::kCommand =
868 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
869const char CancelRemainOnChannelMessage::kCommandString[] =
870 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
871
872string CancelRemainOnChannelMessage::ToString() const {
873 string output(GetHeaderString());
874 uint32_t freq;
875 uint64_t cookie;
876 StringAppendF(&output,
877 "done with remain on freq %" PRIu32 " (cookie %" PRIx64 ")",
878 (GetU32Attribute(NL80211_ATTR_WIPHY_FREQ, &freq) ? 0 : freq),
879 (GetU64Attribute(NL80211_ATTR_COOKIE, &cookie) ? 0 : cookie));
880 return output;
881}
882
883const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
884const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
885
886string ConnectMessage::ToString() const {
887 string output(GetHeaderString());
888
889 uint16_t status = UINT16_MAX;
890
891 if (!GetU16Attribute(NL80211_ATTR_STATUS_CODE, &status)) {
892 output.append("unknown connect status");
893 } else if (status == 0) {
894 output.append("connected");
895 } else {
896 output.append("failed to connect");
897 }
898
899 if (AttributeExists(NL80211_ATTR_MAC)) {
900 string mac;
901 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
902 StringAppendF(&output, " to %s", mac.c_str());
903 }
904 if (status)
905 StringAppendF(&output, ", status: %u: %s", status,
906 StringFromStatus(status).c_str());
907 return output;
908}
909
910const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
911const char DeauthenticateMessage::kCommandString[] =
912 "NL80211_CMD_DEAUTHENTICATE";
913
914string DeauthenticateMessage::ToString() const {
915 string output(GetHeaderString());
916 StringAppendF(&output, "deauth%s",
917 StringFromFrame(NL80211_ATTR_FRAME).c_str());
918 return output;
919}
920
921const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
922const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
923
924string DeleteStationMessage::ToString() const {
925 string mac;
926 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
927 string output(GetHeaderString());
928 StringAppendF(&output, "del station %s", mac.c_str());
929 return output;
930}
931
932const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
933const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
934
935string DisassociateMessage::ToString() const {
936 string output(GetHeaderString());
937 StringAppendF(&output, "disassoc%s",
938 StringFromFrame(NL80211_ATTR_FRAME).c_str());
939 return output;
940}
941
942const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
943const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
944
945string DisconnectMessage::ToString() const {
946 string output(GetHeaderString());
947 StringAppendF(&output, "disconnected %s",
948 ((AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP)) ?
949 "(by AP)" : "(local request)"));
950
951 uint16_t reason = UINT16_MAX;
952 if (GetU16Attribute(NL80211_ATTR_REASON_CODE, &reason)) {
953 StringAppendF(&output, " reason: %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700954 reason, StringFromReason(reason).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700955 }
956 return output;
957}
958
959const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
960const char FrameTxStatusMessage::kCommandString[] =
961 "NL80211_CMD_FRAME_TX_STATUS";
962
963string FrameTxStatusMessage::ToString() const {
964 string output(GetHeaderString());
965 uint64_t cookie = UINT64_MAX;
966 GetU64Attribute(NL80211_ATTR_COOKIE, &cookie);
967
968 StringAppendF(&output, "mgmt TX status (cookie %" PRIx64 "): %s",
969 cookie,
970 (AttributeExists(NL80211_ATTR_ACK) ? "acked" : "no ack"));
971 return output;
972}
973
974const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
975const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
976
977string JoinIbssMessage::ToString() const {
978 string mac;
979 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
980 string output(GetHeaderString());
981 StringAppendF(&output, "IBSS %s joined", mac.c_str());
982 return output;
983}
984
985const uint8_t MichaelMicFailureMessage::kCommand =
986 NL80211_CMD_MICHAEL_MIC_FAILURE;
987const char MichaelMicFailureMessage::kCommandString[] =
988 "NL80211_CMD_MICHAEL_MIC_FAILURE";
989
990string MichaelMicFailureMessage::ToString() const {
991 string output(GetHeaderString());
992
993 output.append("Michael MIC failure event:");
994
995 if (AttributeExists(NL80211_ATTR_MAC)) {
996 string mac;
997 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
998 StringAppendF(&output, " source MAC address %s", mac.c_str());
999 }
1000
1001 if (AttributeExists(NL80211_ATTR_KEY_SEQ)) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001002 ByteString rawdata;
1003 if (GetRawAttributeData(NL80211_ATTR_KEY_SEQ, &rawdata) &&
1004 rawdata.GetLength() == UserBoundNlMessage::kEthernetAddressBytes) {
1005 const unsigned char *seq = rawdata.GetConstData();
Wade Guthrie0d438532012-05-18 14:18:50 -07001006 StringAppendF(&output, " seq=%02x%02x%02x%02x%02x%02x",
1007 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
1008 }
1009 }
1010 uint32_t key_type_val = UINT32_MAX;
1011 if (GetU32Attribute(NL80211_ATTR_KEY_TYPE, &key_type_val)) {
1012 enum nl80211_key_type key_type =
1013 static_cast<enum nl80211_key_type >(key_type_val);
1014 StringAppendF(&output, " Key Type %s", StringFromKeyType(key_type).c_str());
1015 }
1016
1017 uint8_t key_index = UINT8_MAX;
1018 if (GetU8Attribute(NL80211_ATTR_KEY_IDX, &key_index)) {
1019 StringAppendF(&output, " Key Id %u", key_index);
1020 }
1021
1022 return output;
1023}
1024
1025const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
1026const char NewScanResultsMessage::kCommandString[] =
1027 "NL80211_CMD_NEW_SCAN_RESULTS";
1028
1029string NewScanResultsMessage::ToString() const {
1030 string output(GetHeaderString());
1031 output.append("scan finished");
1032
1033 {
1034 output.append("; frequencies: ");
1035 vector<uint32_t> list;
1036 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1037 string str;
1038 for (vector<uint32_t>::const_iterator i = list.begin();
1039 i != list.end(); ++i) {
1040 StringAppendF(&str, " %" PRIu32 ", ", *i);
1041 }
1042 output.append(str);
1043 }
1044 }
1045
1046 {
1047 output.append("; SSIDs: ");
1048 vector<string> list;
1049 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1050 string str;
1051 for (vector<string>::const_iterator i = list.begin();
1052 i != list.end(); ++i) {
1053 StringAppendF(&str, "\"%s\", ", i->c_str());
1054 }
1055 output.append(str);
1056 }
1057 }
1058
1059 return output;
1060}
1061
1062const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
1063const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
1064
1065string NewStationMessage::ToString() const {
1066 string mac;
1067 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1068 string output(GetHeaderString());
1069 StringAppendF(&output, "new station %s", mac.c_str());
1070
1071 return output;
1072}
1073
1074const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
1075const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
1076
1077string NewWifiMessage::ToString() const {
1078 string output(GetHeaderString());
1079 string wifi_name = "None";
1080 GetStringAttribute(NL80211_ATTR_WIPHY_NAME, &wifi_name);
1081 StringAppendF(&output, "renamed to %s", wifi_name.c_str());
1082 return output;
1083}
1084
1085const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
1086const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
1087
1088string NotifyCqmMessage::ToString() const {
1089 static const nla_policy kCqmPolicy[NL80211_ATTR_CQM_MAX + 1] = {
1090 { NLA_U32, 0, 0 }, // Who Knows?
1091 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
1092 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
1093 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
1094 };
1095
1096 string output(GetHeaderString());
1097 output.append("connection quality monitor event: ");
1098
repo syncd316eb72012-12-10 15:48:47 -08001099 const Nl80211Attribute *attribute = GetAttribute(NL80211_ATTR_CQM);
1100 if (!attribute) {
1101 output.append("missing data!");
1102 return output;
1103 }
1104
1105 const nlattr *const_data = attribute->data();
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001106 // Note that |nla_parse_nested| doesn't change |const_data| but doesn't
1107 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001108 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
1109
1110 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
repo syncd316eb72012-12-10 15:48:47 -08001111 if (!cqm_attr || nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
1112 const_cast<nla_policy *>(kCqmPolicy))) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001113 output.append("missing data!");
1114 return output;
1115 }
1116 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
1117 enum nl80211_cqm_rssi_threshold_event rssi_event =
1118 static_cast<enum nl80211_cqm_rssi_threshold_event>(
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001119 Nl80211Attribute::NlaGetU32(
1120 cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -07001121 if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
1122 output.append("RSSI went above threshold");
1123 else
1124 output.append("RSSI went below threshold");
1125 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
1126 AttributeExists(NL80211_ATTR_MAC)) {
1127 string mac;
1128 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1129 StringAppendF(&output, "peer %s didn't ACK %" PRIu32 " packets",
1130 mac.c_str(),
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001131 Nl80211Attribute::NlaGetU32(
1132 cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -07001133 } else {
1134 output.append("unknown event");
1135 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001136 return output;
1137}
1138
1139const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
1140const char PmksaCandidateMessage::kCommandString[] =
1141 "NL80211_ATTR_PMKSA_CANDIDATE";
1142
1143string PmksaCandidateMessage::ToString() const {
1144 string output(GetHeaderString());
1145 output.append("PMKSA candidate found");
1146 return output;
1147}
1148
1149const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
1150const char RegBeaconHintMessage::kCommandString[] =
1151 "NL80211_CMD_REG_BEACON_HINT";
1152
1153string RegBeaconHintMessage::ToString() const {
1154 string output(GetHeaderString());
1155 uint32_t wiphy_idx = UINT32_MAX;
1156 GetU32Attribute(NL80211_ATTR_WIPHY, &wiphy_idx);
1157
repo syncd316eb72012-12-10 15:48:47 -08001158 const Nl80211Attribute *freq_before = GetAttribute(NL80211_ATTR_FREQ_BEFORE);
1159 if (!freq_before)
1160 return "";
1161 const nlattr *const_before = freq_before->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001162 ieee80211_beacon_channel chan_before_beacon;
1163 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
1164 if (ParseBeaconHintChan(const_before, &chan_before_beacon))
1165 return "";
1166
repo syncd316eb72012-12-10 15:48:47 -08001167 const Nl80211Attribute *freq_after = GetAttribute(NL80211_ATTR_FREQ_AFTER);
1168 if (!freq_after)
1169 return "";
1170
1171 const nlattr *const_after = freq_after->data();
Wade Guthrie0d438532012-05-18 14:18:50 -07001172 ieee80211_beacon_channel chan_after_beacon;
1173 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
1174 if (ParseBeaconHintChan(const_after, &chan_after_beacon))
1175 return "";
1176
1177 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
1178 return "";
1179
1180 /* A beacon hint is sent _only_ if something _did_ change */
1181 output.append("beacon hint:");
1182 StringAppendF(&output, "phy%" PRIu32 " %u MHz [%d]:",
1183 wiphy_idx, chan_before_beacon.center_freq,
1184 ChannelFromIeee80211Frequency(chan_before_beacon.center_freq));
1185
1186 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
1187 output.append("\to active scanning enabled");
1188 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
1189 output.append("\to beaconing enabled");
1190 return output;
1191}
1192
1193int RegBeaconHintMessage::ParseBeaconHintChan(const nlattr *tb,
1194 ieee80211_beacon_channel *chan)
1195 const {
1196 static const nla_policy kBeaconFreqPolicy[
1197 NL80211_FREQUENCY_ATTR_MAX + 1] = {
1198 {0, 0, 0},
1199 { NLA_U32, 0, 0 }, // [NL80211_FREQUENCY_ATTR_FREQ]
1200 {0, 0, 0},
1201 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]
1202 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_NO_IBSS]
1203 };
1204
1205 if (!tb) {
1206 LOG(ERROR) << "|tb| parameter is NULL.";
1207 return -EINVAL;
1208 }
1209
1210 if (!chan) {
1211 LOG(ERROR) << "|chan| parameter is NULL.";
1212 return -EINVAL;
1213 }
1214
1215 nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1216
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001217 // Note that |nla_parse_nested| doesn't change its parameters but doesn't
1218 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001219 if (nla_parse_nested(tb_freq,
1220 NL80211_FREQUENCY_ATTR_MAX,
1221 const_cast<nlattr *>(tb),
1222 const_cast<nla_policy *>(kBeaconFreqPolicy)))
1223 return -EINVAL;
1224
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001225 chan->center_freq = Nl80211Attribute::NlaGetU32(
1226 tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
Wade Guthrie0d438532012-05-18 14:18:50 -07001227
1228 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1229 chan->passive_scan = true;
1230 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1231 chan->no_ibss = true;
1232
1233 return 0;
1234}
1235
1236int RegBeaconHintMessage::ChannelFromIeee80211Frequency(int freq) {
1237 // TODO(wdg): get rid of these magic numbers.
1238 if (freq == 2484)
1239 return 14;
1240
1241 if (freq < 2484)
1242 return (freq - 2407) / 5;
1243
1244 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
1245 return freq/5 - 1000;
1246}
1247
1248const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
1249const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
1250
1251string RegChangeMessage::ToString() const {
1252 string output(GetHeaderString());
1253 output.append("regulatory domain change: ");
1254
1255 uint8_t reg_type = UINT8_MAX;
1256 GetU8Attribute(NL80211_ATTR_REG_TYPE, &reg_type);
1257
1258 uint32_t initiator = UINT32_MAX;
1259 GetU32Attribute(NL80211_ATTR_REG_INITIATOR, &initiator);
1260
1261 uint32_t wifi = UINT32_MAX;
1262 bool wifi_exists = GetU32Attribute(NL80211_ATTR_WIPHY, &wifi);
1263
1264 string alpha2 = "<None>";
1265 GetStringAttribute(NL80211_ATTR_REG_ALPHA2, &alpha2);
1266
1267 switch (reg_type) {
1268 case NL80211_REGDOM_TYPE_COUNTRY:
1269 StringAppendF(&output, "set to %s by %s request",
1270 alpha2.c_str(), StringFromRegInitiator(initiator).c_str());
1271 if (wifi_exists)
1272 StringAppendF(&output, " on phy%" PRIu32, wifi);
1273 break;
1274
1275 case NL80211_REGDOM_TYPE_WORLD:
1276 StringAppendF(&output, "set to world roaming by %s request",
1277 StringFromRegInitiator(initiator).c_str());
1278 break;
1279
1280 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
1281 StringAppendF(&output,
1282 "custom world roaming rules in place on phy%" PRIu32
1283 " by %s request",
1284 wifi, StringFromRegInitiator(initiator).c_str());
1285 break;
1286
1287 case NL80211_REGDOM_TYPE_INTERSECTION:
1288 StringAppendF(&output, "intersection used due to a request made by %s",
1289 StringFromRegInitiator(initiator).c_str());
1290 if (wifi_exists)
1291 StringAppendF(&output, " on phy%" PRIu32, wifi);
1292 break;
1293
1294 default:
1295 output.append("unknown source");
1296 break;
1297 }
1298 return output;
1299}
1300
1301const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
1302const char RemainOnChannelMessage::kCommandString[] =
1303 "NL80211_CMD_REMAIN_ON_CHANNEL";
1304
1305string RemainOnChannelMessage::ToString() const {
1306 string output(GetHeaderString());
1307
1308 uint32_t wifi_freq = UINT32_MAX;
1309 GetU32Attribute(NL80211_ATTR_WIPHY_FREQ, &wifi_freq);
1310
1311 uint32_t duration = UINT32_MAX;
1312 GetU32Attribute(NL80211_ATTR_DURATION, &duration);
1313
1314 uint64_t cookie = UINT64_MAX;
1315 GetU64Attribute(NL80211_ATTR_COOKIE, &cookie);
1316
1317 StringAppendF(&output, "remain on freq %" PRIu32 " (%" PRIu32 "ms, cookie %"
1318 PRIx64 ")",
1319 wifi_freq, duration, cookie);
1320 return output;
1321}
1322
1323const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
1324const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
1325
1326string RoamMessage::ToString() const {
1327 string output(GetHeaderString());
1328 output.append("roamed");
1329
1330 if (AttributeExists(NL80211_ATTR_MAC)) {
1331 string mac;
1332 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1333 StringAppendF(&output, " to %s", mac.c_str());
1334 }
1335 return output;
1336}
1337
1338const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
1339const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
1340
1341string ScanAbortedMessage::ToString() const {
1342 string output(GetHeaderString());
1343 output.append("scan aborted");
1344
1345 {
1346 output.append("; frequencies: ");
1347 vector<uint32_t> list;
1348 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1349 string str;
1350 for (vector<uint32_t>::const_iterator i = list.begin();
1351 i != list.end(); ++i) {
1352 StringAppendF(&str, " %" PRIu32 ", ", *i);
1353 }
1354 output.append(str);
1355 }
1356 }
1357
1358 {
1359 output.append("; SSIDs: ");
1360 vector<string> list;
1361 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1362 string str;
1363 for (vector<string>::const_iterator i = list.begin();
1364 i != list.end(); ++i) {
1365 StringAppendF(&str, "\"%s\", ", i->c_str());
1366 }
1367 output.append(str);
1368 }
1369 }
1370
1371 return output;
1372}
1373
1374const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
1375const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
1376
1377string TriggerScanMessage::ToString() const {
1378 string output(GetHeaderString());
1379 output.append("scan started");
1380
1381 {
1382 output.append("; frequencies: ");
1383 vector<uint32_t> list;
1384 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1385 string str;
1386 for (vector<uint32_t>::const_iterator i = list.begin();
1387 i != list.end(); ++i) {
1388 StringAppendF(&str, "%" PRIu32 ", ", *i);
1389 }
1390 output.append(str);
1391 }
1392 }
1393
1394 {
1395 output.append("; SSIDs: ");
1396 vector<string> list;
1397 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1398 string str;
1399 for (vector<string>::const_iterator i = list.begin();
1400 i != list.end(); ++i) {
1401 StringAppendF(&str, "\"%s\", ", i->c_str());
1402 }
1403 output.append(str);
1404 }
1405 }
1406
1407 return output;
1408}
1409
1410const uint8_t UnknownMessage::kCommand = 0xff;
1411const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
1412
1413string UnknownMessage::ToString() const {
1414 string output(GetHeaderString());
1415 StringAppendF(&output, "unknown event %u", command_);
1416 return output;
1417}
1418
1419const uint8_t UnprotDeauthenticateMessage::kCommand =
1420 NL80211_CMD_UNPROT_DEAUTHENTICATE;
1421const char UnprotDeauthenticateMessage::kCommandString[] =
1422 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
1423
1424string UnprotDeauthenticateMessage::ToString() const {
1425 string output(GetHeaderString());
1426 StringAppendF(&output, "unprotected deauth %s",
1427 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1428 return output;
1429}
1430
1431const uint8_t UnprotDisassociateMessage::kCommand =
1432 NL80211_CMD_UNPROT_DISASSOCIATE;
1433const char UnprotDisassociateMessage::kCommandString[] =
1434 "NL80211_CMD_UNPROT_DISASSOCIATE";
1435
1436string UnprotDisassociateMessage::ToString() const {
1437 string output(GetHeaderString());
1438 StringAppendF(&output, "unprotected disassoc %s",
1439 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1440 return output;
1441}
1442
1443//
1444// Factory class.
1445//
1446
1447UserBoundNlMessage *UserBoundNlMessageFactory::CreateMessage(nlmsghdr *msg) {
1448 if (!msg) {
1449 LOG(ERROR) << "NULL |msg| parameter";
1450 return NULL;
1451 }
1452
1453 scoped_ptr<UserBoundNlMessage> message;
1454 genlmsghdr *gnlh =
1455 reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
1456
1457 if (!gnlh) {
1458 LOG(ERROR) << "NULL gnlh";
1459 return NULL;
1460 }
1461
1462 switch (gnlh->cmd) {
1463 case AssociateMessage::kCommand:
1464 message.reset(new AssociateMessage()); break;
1465 case AuthenticateMessage::kCommand:
1466 message.reset(new AuthenticateMessage()); break;
1467 case CancelRemainOnChannelMessage::kCommand:
1468 message.reset(new CancelRemainOnChannelMessage()); break;
1469 case ConnectMessage::kCommand:
1470 message.reset(new ConnectMessage()); break;
1471 case DeauthenticateMessage::kCommand:
1472 message.reset(new DeauthenticateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001473 case DeleteStationMessage::kCommand:
1474 message.reset(new DeleteStationMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001475 case DisassociateMessage::kCommand:
1476 message.reset(new DisassociateMessage()); break;
1477 case DisconnectMessage::kCommand:
1478 message.reset(new DisconnectMessage()); break;
1479 case FrameTxStatusMessage::kCommand:
1480 message.reset(new FrameTxStatusMessage()); break;
1481 case JoinIbssMessage::kCommand:
1482 message.reset(new JoinIbssMessage()); break;
1483 case MichaelMicFailureMessage::kCommand:
1484 message.reset(new MichaelMicFailureMessage()); break;
1485 case NewScanResultsMessage::kCommand:
1486 message.reset(new NewScanResultsMessage()); break;
1487 case NewStationMessage::kCommand:
1488 message.reset(new NewStationMessage()); break;
1489 case NewWifiMessage::kCommand:
1490 message.reset(new NewWifiMessage()); break;
1491 case NotifyCqmMessage::kCommand:
1492 message.reset(new NotifyCqmMessage()); break;
1493 case PmksaCandidateMessage::kCommand:
1494 message.reset(new PmksaCandidateMessage()); break;
1495 case RegBeaconHintMessage::kCommand:
1496 message.reset(new RegBeaconHintMessage()); break;
1497 case RegChangeMessage::kCommand:
1498 message.reset(new RegChangeMessage()); break;
1499 case RemainOnChannelMessage::kCommand:
1500 message.reset(new RemainOnChannelMessage()); break;
1501 case RoamMessage::kCommand:
1502 message.reset(new RoamMessage()); break;
1503 case ScanAbortedMessage::kCommand:
1504 message.reset(new ScanAbortedMessage()); break;
1505 case TriggerScanMessage::kCommand:
1506 message.reset(new TriggerScanMessage()); break;
1507 case UnprotDeauthenticateMessage::kCommand:
1508 message.reset(new UnprotDeauthenticateMessage()); break;
1509 case UnprotDisassociateMessage::kCommand:
1510 message.reset(new UnprotDisassociateMessage()); break;
1511
1512 default:
1513 message.reset(new UnknownMessage(gnlh->cmd)); break;
1514 break;
1515 }
1516
1517 nlattr *tb[NL80211_ATTR_MAX + 1];
1518
1519 // Parse the attributes from the nl message payload (which starts at the
1520 // header) into the 'tb' array.
1521 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001522 genlmsg_attrlen(gnlh, 0), NULL);
Wade Guthrie0d438532012-05-18 14:18:50 -07001523
1524 if (!message->Init(tb, msg)) {
1525 LOG(ERROR) << "Message did not initialize properly";
1526 return NULL;
1527 }
1528
Wade Guthrie0d438532012-05-18 14:18:50 -07001529 return message.release();
1530}
1531
1532UserBoundNlMessageDataCollector *
1533 UserBoundNlMessageDataCollector::GetInstance() {
1534 return g_datacollector.Pointer();
1535}
1536
1537UserBoundNlMessageDataCollector::UserBoundNlMessageDataCollector() {
1538 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
1539 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
1540 need_to_print[NL80211_CMD_DEL_STATION] = true;
1541 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
1542 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
1543 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
1544 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
1545 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
1546 need_to_print[NL80211_CMD_REG_CHANGE] = true;
1547 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
1548 need_to_print[NL80211_CMD_ROAM] = true;
1549 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
1550 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
1551 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
1552}
1553
1554void UserBoundNlMessageDataCollector::CollectDebugData(
Wade Guthried4977f22012-08-22 12:37:54 -07001555 const UserBoundNlMessage &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001556 if (!msg) {
1557 LOG(ERROR) << "NULL |msg| parameter";
1558 return;
1559 }
1560
1561 bool doit = false;
1562
1563 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -08001564 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -07001565 if (node != need_to_print.end())
1566 doit = node->second;
1567
1568 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001569 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001570 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001571 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001572
Christopher Wileyefd521f2012-11-07 17:32:46 -08001573 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001574
1575 size_t bytes = nlmsg_total_size(payload_bytes);
1576 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001577 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001578 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001579 << std::hex << std::setfill('0') << std::setw(2)
1580 << + rawdata[i] << ",";
1581 }
Wade Guthried6153612012-08-23 11:36:14 -07001582 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001583 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001584 }
1585}
1586
1587} // namespace shill.