blob: 1b2583ffae54d75600b979ee348a27b9b505e5cf [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
Wade Guthrie8e278612013-02-26 10:32:34 -080040#include <algorithm>
Wade Guthrie0d438532012-05-18 14:18:50 -070041#include <iomanip>
42#include <string>
43
Wade Guthrie68da97c2013-02-26 13:09:35 -080044#include <base/bind.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070045#include <base/format_macros.h>
46#include <base/stl_util.h>
47#include <base/stringprintf.h>
48
repo sync90ee0fa2012-12-18 10:08:08 -080049#include "shill/attribute_list.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070050#include "shill/ieee80211.h"
51#include "shill/logging.h"
repo syncdc085c82012-12-28 08:54:41 -080052#include "shill/netlink_socket.h"
repo sync1538d442012-12-20 15:24:35 -080053#include "shill/nl80211_attribute.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070054#include "shill/scope_logger.h"
Darin Petkov50cb78a2013-02-06 16:17:49 +010055#include "shill/wifi.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070056
Wade Guthrie68da97c2013-02-26 13:09:35 -080057using base::Bind;
Wade Guthrie0d438532012-05-18 14:18:50 -070058using base::LazyInstance;
59using base::StringAppendF;
60using base::StringPrintf;
61using std::map;
Wade Guthrie8e278612013-02-26 10:32:34 -080062using std::min;
Wade Guthrie0d438532012-05-18 14:18:50 -070063using std::string;
64using std::vector;
65
66namespace shill {
67
68namespace {
repo syncdc085c82012-12-28 08:54:41 -080069LazyInstance<Nl80211MessageDataCollector> g_datacollector =
Wade Guthrie0d438532012-05-18 14:18:50 -070070 LAZY_INSTANCE_INITIALIZER;
71} // namespace
72
repo syncdc085c82012-12-28 08:54:41 -080073const char Nl80211Message::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX";
Wade Guthrie0d438532012-05-18 14:18:50 -070074
75const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
76const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
77
repo syncdc085c82012-12-28 08:54:41 -080078const uint32_t Nl80211Message::kIllegalMessage = 0;
79const unsigned int Nl80211Message::kEthernetAddressBytes = 6;
80map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
81map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
Wade Guthrie0d438532012-05-18 14:18:50 -070082
83// The nl messages look like this:
84//
85// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
86// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
87// +-- gnhl nlmsg_tail(hdr) --+
88// | nlmsg_next(hdr) --+
89// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
90// -----+-----+-+----------------------------------------------+-++----
91// ... | | | payload | ||
92// | | +------+-+--------+-+--------------------------+ ||
93// | nl | | | | | | attribs | ||
94// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
95// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
96// | |d| hdr |d| |d| attr |a| load |a| ... |d||
97// | | | | | | | |d| |d| | ||
98// -----+-----+-+----------------------------------------------+-++----
99// ^ ^ ^ ^
100// | | | XXXXXXX <-- nla_len(nlattr)
101// | | | +-- nla_data(nlattr)
102// | | X-nla_total_size-X
103// | | XXXXX-nlmsg_attrlen-XXXXXX
104// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
105// +-- msg = nlmsg_hdr(raw_message)
106
107//
repo syncdc085c82012-12-28 08:54:41 -0800108// Nl80211Message
Wade Guthrie0d438532012-05-18 14:18:50 -0700109//
110
Wade Guthrie8e278612013-02-26 10:32:34 -0800111void Nl80211Message::Print(int log_level) const {
112 SLOG(WiFi, log_level) << StringPrintf("Message %s (%d)",
113 message_type_string(),
114 message_type());
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800115 attributes_->Print(log_level, 1);
Wade Guthrie8e278612013-02-26 10:32:34 -0800116}
117
repo sync0efa9f02012-12-28 13:40:20 -0800118bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) {
119 if (!const_msg) {
120 LOG(ERROR) << "Null |msg| parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700121 return false;
122 }
123
repo sync0efa9f02012-12-28 13:40:20 -0800124 // Netlink header.
125 sequence_number_ = const_msg->nlmsg_seq;
repo syncdc085c82012-12-28 08:54:41 -0800126 SLOG(WiFi, 6) << "NL Message " << sequence_number() << " <===";
Wade Guthrie0d438532012-05-18 14:18:50 -0700127
repo sync0efa9f02012-12-28 13:40:20 -0800128 // Casting away constness, here, since the libnl code doesn't properly label
129 // their stuff as const (even though it is).
130 nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg);
131
132 // Genl message header.
133 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
134
135 // Attributes.
136 // Parse the attributes from the nl message payload into the 'tb' array.
137 nlattr *tb[NL80211_ATTR_MAX + 1];
138 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
139 genlmsg_attrlen(gnlh, 0), NULL);
140
Wade Guthrie0d438532012-05-18 14:18:50 -0700141 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
142 if (tb[i]) {
repo sync0efa9f02012-12-28 13:40:20 -0800143 // TODO(wdg): When Nl80211Messages instantiate their own attributes,
144 // this call should, instead, call |SetAttributeFromNlAttr|.
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800145 attributes_->CreateAndInitAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800146 i, tb[i], Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
Wade Guthrie0d438532012-05-18 14:18:50 -0700147 }
148 }
149
150 // Convert integer values provided by libnl (for example, from the
151 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
152 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700153 if (!reason_code_string_) {
154 reason_code_string_ = new map<uint16_t, string>;
155 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
156 "Unspecified reason";
157 (*reason_code_string_)[
158 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700159 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700160 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
161 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
162 "ESS";
163 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
164 "Disassociated due to inactivity";
165 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
166 "Disassociated because AP is unable to handle all currently associated "
167 "STAs";
168 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
169 "Class 2 frame received from nonauthenticated STA";
170 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
171 "Class 3 frame received from nonassociated STA";
172 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
173 "Disassociated because sending STA is leaving (or has left) BSS";
174 (*reason_code_string_)[
175 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
176 "STA requesting (re)association is not authenticated with responding "
177 "STA";
178 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
179 "Disassociated because the information in the Power Capability "
180 "element is unacceptable";
181 (*reason_code_string_)[
182 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
183 "Disassociated because the information in the Supported Channels "
184 "element is unacceptable";
185 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
186 "Invalid information element, i.e., an information element defined in "
187 "this standard for which the content does not meet the specifications "
188 "in Clause 7";
189 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
190 "Message integrity code (MIC) failure";
191 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
192 "4-Way Handshake timeout";
193 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
194 "Group Key Handshake timeout";
195 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
196 "Information element in 4-Way Handshake different from "
197 "(Re)Association Request/Probe Response/Beacon frame";
198 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
199 "Invalid group cipher";
200 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
201 "Invalid pairwise cipher";
202 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
203 "Invalid AKMP";
204 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
205 "Unsupported RSN information element version";
206 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
207 "Invalid RSN information element capabilities";
208 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
209 "IEEE 802.1X authentication failed";
210 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
211 "Cipher suite rejected because of the security policy";
212 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
213 "Disassociated for unspecified, QoS-related reason";
214 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
215 "Disassociated because QoS AP lacks sufficient bandwidth for this "
216 "QoS STA";
217 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
218 "Disassociated because excessive number of frames need to be "
219 "acknowledged, but are not acknowledged due to AP transmissions "
220 "and/or poor channel conditions";
221 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
222 "Disassociated because STA is transmitting outside the limits of its "
223 "TXOPs";
224 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
225 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
226 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
227 "Requested from peer STA as it does not want to use the mechanism";
228 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
229 "Requested from peer STA as the STA received frames using the "
230 "mechanism for which a setup is required";
231 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
232 "Requested from peer STA due to timeout";
233 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
234 "Peer STA does not support the requested cipher suite";
235 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
236 }
237
238 if (!status_code_string_) {
239 status_code_string_ = new map<uint16_t, string>;
240 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
241 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
242 "Unspecified failure";
243 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700244 "Cannot support all requested capabilities in the capability "
245 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700246 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700247 "Reassociation denied due to inability to confirm that association "
248 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700249 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700250 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700251 (*status_code_string_)[
252 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700253 "Responding station does not support the specified authentication "
254 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700255 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700256 "Received an authentication frame with authentication transaction "
257 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700258 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700259 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700260 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700261 "Authentication rejected due to timeout waiting for next frame in "
262 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700263 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
264 "Association denied because AP is unable to handle additional "
265 "associated STA";
266 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700267 "Association denied due to requesting station not supporting all of "
268 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700269 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700270 "Association denied due to requesting station not supporting the "
271 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700272 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700273 "Association denied due to requesting station not supporting the PBCC "
274 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700275 (*status_code_string_)[
276 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700277 "Association denied due to requesting station not supporting the "
278 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700279 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700280 "Association request rejected because Spectrum Management capability "
281 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700282 (*status_code_string_)[
283 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700284 "Association request rejected because the information in the Power "
285 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700286 (*status_code_string_)[
287 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700288 "Association request rejected because the information in the "
289 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700290 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700291 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700292 "supporting the Short Slot Time option";
293 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
294 "Association request rejected due to requesting station not "
295 "supporting the DSSS-OFDM option";
296 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700297 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700298 (*status_code_string_)[
299 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700300 "Association denied due to QAP having insufficient bandwidth to handle "
301 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700302 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700303 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700304 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
305 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700306 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700307 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
308 "The request has been declined";
309 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700310 "The request has not been successful as one or more parameters have "
311 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700312 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
313 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700314 "However, a suggested Tspec is provided so that the initiating QSTA "
315 "may attempt to send another TS with the suggested changes to the "
316 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700317 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700318 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700319 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
320 "Invalid Group Cipher";
321 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
322 "Invalid Pairwise Cipher";
323 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
324 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
325 "Unsupported RSN Information Element version";
326 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
327 "Invalid RSN Information Element Capabilities";
328 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700329 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700330 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
331 "The TS has not been created. However, the HC may be capable of "
332 "creating a TS, in response to a request, after the time indicated in "
333 "the TS Delay element";
334 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700335 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700336 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
337 "Destination STA is not present within this BSS";
338 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
339 "The destination STA is not a QoS STA";
340 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700341 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700342 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700343 }
344
345 return true;
346}
347
Wade Guthrie8e278612013-02-26 10:32:34 -0800348// static
349void Nl80211Message::PrintBytes(int log_level, const unsigned char *buf,
350 size_t num_bytes) {
351 SLOG(WiFi, log_level) << "Netlink Message -- Examining Bytes";
352 if (!buf) {
353 SLOG(WiFi, log_level) << "<NULL Buffer>";
354 return;
355 }
356
357 if (num_bytes >= sizeof(nlmsghdr)) {
358 const nlmsghdr *header = reinterpret_cast<const nlmsghdr *>(buf);
359 SLOG(WiFi, log_level) << StringPrintf(
360 "len: %02x %02x %02x %02x = %u bytes",
361 buf[0], buf[1], buf[2], buf[3], header->nlmsg_len);
362
363 SLOG(WiFi, log_level) << StringPrintf(
364 "type | flags: %02x %02x %02x %02x - type:%u flags:%s%s%s%s%s",
365 buf[4], buf[5], buf[6], buf[7], header->nlmsg_type,
366 ((header->nlmsg_flags & NLM_F_REQUEST) ? " REQUEST" : ""),
367 ((header->nlmsg_flags & NLM_F_MULTI) ? " MULTI" : ""),
368 ((header->nlmsg_flags & NLM_F_ACK) ? " ACK" : ""),
369 ((header->nlmsg_flags & NLM_F_ECHO) ? " ECHO" : ""),
370 ((header->nlmsg_flags & NLM_F_DUMP_INTR) ? " BAD-SEQ" : ""));
371
372 SLOG(WiFi, log_level) << StringPrintf(
373 "sequence: %02x %02x %02x %02x = %u",
374 buf[8], buf[9], buf[10], buf[11], header->nlmsg_seq);
375 SLOG(WiFi, log_level) << StringPrintf(
376 "pid: %02x %02x %02x %02x = %u",
377 buf[12], buf[13], buf[14], buf[15], header->nlmsg_pid);
378 buf += sizeof(nlmsghdr);
379 num_bytes -= sizeof(nlmsghdr);
380 } else {
381 SLOG(WiFi, log_level) << "Not enough bytes (" << num_bytes
382 << ") for a complete nlmsghdr (requires "
383 << sizeof(nlmsghdr) << ").";
384 }
385
386 while (num_bytes) {
387 string output;
388 size_t bytes_this_row = min(num_bytes, static_cast<size_t>(32));
389 for (size_t i = 0; i < bytes_this_row; ++i) {
390 StringAppendF(&output, " %02x", *buf++);
391 }
392 SLOG(WiFi, log_level) << output;
393 num_bytes -= bytes_this_row;
394 }
395}
396
Wade Guthrie0d438532012-05-18 14:18:50 -0700397// Helper function to provide a string for a MAC address.
Wade Guthrie68da97c2013-02-26 13:09:35 -0800398bool Nl80211Message::GetMacAttributeString(int id, string *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700399 if (!value) {
400 LOG(ERROR) << "Null |value| parameter";
401 return false;
402 }
403
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800404 ByteString data;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800405 if (!const_attributes()->GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700406 value->assign(kBogusMacAddress);
407 return false;
408 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800409 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700410
411 return true;
412}
413
414// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
repo syncdc085c82012-12-28 08:54:41 -0800415bool Nl80211Message::GetScanFrequenciesAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800416 int id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700417 if (!value) {
418 LOG(ERROR) << "Null |value| parameter";
419 return false;
420 }
421
422 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800423 ByteString rawdata;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800424 if (!const_attributes()->GetRawAttributeValue(id, &rawdata) &&
425 !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800426 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700427
repo syncd316eb72012-12-10 15:48:47 -0800428 nlattr *nst = NULL;
429 // |nla_for_each_attr| requires a non-const parameter even though it
430 // doesn't change the data.
431 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
432 int rem_nst;
433 int len = rawdata.GetLength();
434
435 nla_for_each_attr(nst, attr_data, len, rem_nst) {
Wade Guthrie68da97c2013-02-26 13:09:35 -0800436 value->push_back(NetlinkAttribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700437 }
repo syncd316eb72012-12-10 15:48:47 -0800438 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700439}
440
441// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800442bool Nl80211Message::GetScanSsidsAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800443 int id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700444 if (!value) {
445 LOG(ERROR) << "Null |value| parameter";
446 return false;
447 }
448
repo sync90ee0fa2012-12-18 10:08:08 -0800449 ByteString rawdata;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800450 if (!const_attributes()->GetRawAttributeValue(id, &rawdata) ||
451 rawdata.IsEmpty())
repo sync90ee0fa2012-12-18 10:08:08 -0800452 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700453
repo sync90ee0fa2012-12-18 10:08:08 -0800454 nlattr *nst = NULL;
455 // |nla_for_each_attr| requires a non-const parameter even though it
456 // doesn't change the data.
457 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
458 int rem_nst;
459 int len = rawdata.GetLength();
460
461 nla_for_each_attr(nst, data, len, rem_nst) {
462 value->push_back(StringFromSsid(nla_len(nst),
463 reinterpret_cast<const uint8_t *>(
464 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700465 }
repo sync90ee0fa2012-12-18 10:08:08 -0800466 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700467}
468
Wade Guthrie0d438532012-05-18 14:18:50 -0700469// Protected members.
470
repo syncdc085c82012-12-28 08:54:41 -0800471string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700472 char ifname[IF_NAMESIZE] = "";
473 uint32_t ifindex = UINT32_MAX;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800474 bool ifindex_exists = const_attributes()->GetU32AttributeValue(
475 NL80211_ATTR_IFINDEX, &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700476
477 uint32_t wifi = UINT32_MAX;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800478 bool wifi_exists = const_attributes()->GetU32AttributeValue(
479 NL80211_ATTR_WIPHY, &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700480
481 string output;
482 if (ifindex_exists && wifi_exists) {
483 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
484 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
485 wifi);
486 } else if (ifindex_exists) {
487 StringAppendF(&output, "%s: ",
488 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
489 } else if (wifi_exists) {
490 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
491 }
492
493 return output;
494}
495
Wade Guthrie68da97c2013-02-26 13:09:35 -0800496string Nl80211Message::StringFromFrame(int attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700497 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800498 ByteString frame_data;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800499 if (const_attributes()->GetRawAttributeValue(attr_name, &frame_data) &&
500 !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800501 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700502 frame.ToString(&output);
503 } else {
504 output.append(" [no frame]");
505 }
506
507 return output;
508}
509
510// static
repo syncdc085c82012-12-28 08:54:41 -0800511string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700512 switch (key_type) {
513 case NL80211_KEYTYPE_GROUP:
514 return "Group";
515 case NL80211_KEYTYPE_PAIRWISE:
516 return "Pairwise";
517 case NL80211_KEYTYPE_PEERKEY:
518 return "PeerKey";
519 default:
520 return "<Unknown Key Type>";
521 }
522}
523
524// static
repo syncdc085c82012-12-28 08:54:41 -0800525string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700526 string output;
527
528 if (!arg) {
529 output = kBogusMacAddress;
530 LOG(ERROR) << "|arg| parameter is NULL.";
531 } else {
532 StringAppendF(&output, "%02x", arg[0]);
533
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800534 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700535 StringAppendF(&output, ":%02x", arg[i]);
536 }
537 }
538 return output;
539}
540
541// static
repo syncdc085c82012-12-28 08:54:41 -0800542string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700543 switch (initiator) {
544 case NL80211_REGDOM_SET_BY_CORE:
545 return "the wireless core upon initialization";
546 case NL80211_REGDOM_SET_BY_USER:
547 return "a user";
548 case NL80211_REGDOM_SET_BY_DRIVER:
549 return "a driver";
550 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
551 return "a country IE";
552 default:
553 return "<Unknown Reg Initiator>";
554 }
555}
556
557// static
repo syncdc085c82012-12-28 08:54:41 -0800558string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700559 const uint8_t *data) {
560 string output;
561 if (!data) {
562 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
563 LOG(ERROR) << "|data| parameter is NULL.";
564 return output;
565 }
566
567 for (int i = 0; i < len; ++i) {
568 if (data[i] == ' ')
569 output.append(" ");
570 else if (isprint(data[i]))
571 StringAppendF(&output, "%c", static_cast<char>(data[i]));
572 else
573 StringAppendF(&output, "\\x%2x", data[i]);
574 }
575
576 return output;
577}
578
579// static
repo syncdc085c82012-12-28 08:54:41 -0800580string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700581 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700582 match = reason_code_string_->find(status);
583 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700584 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700585 if (status < IEEE_80211::kReasonCodeMax) {
586 StringAppendF(&output, "<Reserved Reason:%u>", status);
587 } else {
588 StringAppendF(&output, "<Unknown Reason:%u>", status);
589 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700590 return output;
591 }
592 return match->second;
593}
594
Wade Guthried4977f22012-08-22 12:37:54 -0700595// static
repo syncdc085c82012-12-28 08:54:41 -0800596string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700597 map<uint16_t, string>::const_iterator match;
598 match = status_code_string_->find(status);
599 if (match == status_code_string_->end()) {
600 string output;
601 if (status < IEEE_80211::kStatusCodeMax) {
602 StringAppendF(&output, "<Reserved Status:%u>", status);
603 } else {
604 StringAppendF(&output, "<Unknown Status:%u>", status);
605 }
606 return output;
607 }
608 return match->second;
609}
610
repo syncdc085c82012-12-28 08:54:41 -0800611ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const {
612 // Build netlink header.
613 nlmsghdr header;
614 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
615 header.nlmsg_len = nlmsghdr_with_pad;
616 header.nlmsg_type = nlmsg_type;
617 header.nlmsg_flags = NLM_F_REQUEST;
618 header.nlmsg_seq = sequence_number();
619 header.nlmsg_pid = getpid();
620
621 // Build genl message header.
622 genlmsghdr genl_header;
623 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
624 header.nlmsg_len += genlmsghdr_with_pad;
625 genl_header.cmd = message_type();
626 genl_header.version = 1;
627 genl_header.reserved = 0;
628
629 // Assemble attributes (padding is included by AttributeList::Encode).
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800630 ByteString attribute_bytes = attributes_->Encode();
631 header.nlmsg_len += attribute_bytes.GetLength();
repo syncdc085c82012-12-28 08:54:41 -0800632
633 // Now that we know the total message size, build the output ByteString.
634 ByteString result;
635
636 // Netlink header + pad.
637 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
638 sizeof(header)));
639 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
640
641 // Genl message header + pad.
642 result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header),
643 sizeof(genl_header)));
644 result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill.
645
646 // Attributes including pad.
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800647 result.Append(attribute_bytes);
repo syncdc085c82012-12-28 08:54:41 -0800648
649 return result;
650}
651
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800652Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700653 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800654 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700655 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800656 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
657 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700658
659 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800660 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700661 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800662 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
663 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700664 frame_type_ = frame->frame_control & kFrameTypeMask;
665
666 switch (frame_type_) {
667 case kAssocResponseFrameType:
668 case kReassocResponseFrameType:
669 status_ = le16toh(frame->u.associate_response.status_code);
670 break;
671
672 case kAuthFrameType:
673 status_ = le16toh(frame->u.authentiate_message.status_code);
674 break;
675
676 case kDisassocFrameType:
677 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700678 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700679 break;
680
681 default:
682 break;
683 }
684 }
685}
686
Wade Guthried4977f22012-08-22 12:37:54 -0700687bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700688 if (!output) {
689 LOG(ERROR) << "NULL |output|";
690 return false;
691 }
692
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800693 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700694 output->append(" [no frame]");
695 return true;
696 }
697
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800698 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700699 output->append(" [invalid frame: ");
700 } else {
701 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
702
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800703 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700704 case kAssocResponseFrameType:
705 StringAppendF(output, "; AssocResponse status: %u: %s",
706 status_,
repo syncdc085c82012-12-28 08:54:41 -0800707 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700708 break;
709 case kReassocResponseFrameType:
710 StringAppendF(output, "; ReassocResponse status: %u: %s",
711 status_,
repo syncdc085c82012-12-28 08:54:41 -0800712 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700713 break;
714 case kAuthFrameType:
715 StringAppendF(output, "; Auth status: %u: %s",
716 status_,
repo syncdc085c82012-12-28 08:54:41 -0800717 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700718 break;
719
720 case kDisassocFrameType:
721 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700722 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800723 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700724 break;
725 case kDeauthFrameType:
726 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700727 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800728 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700729 break;
730
731 default:
732 break;
733 }
734 output->append(" [frame: ");
735 }
736
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800737 const unsigned char *frame = frame_.GetConstData();
738 for (size_t i = 0; i < frame_.GetLength(); ++i) {
739 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700740 }
741 output->append("]");
742
743 return true;
744}
745
Wade Guthried4977f22012-08-22 12:37:54 -0700746bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800747 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700748}
749
Wade Guthried4977f22012-08-22 12:37:54 -0700750
Wade Guthrie0d438532012-05-18 14:18:50 -0700751//
repo syncdc085c82012-12-28 08:54:41 -0800752// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700753//
754
repo sync0efa9f02012-12-28 13:40:20 -0800755// An Ack is not a GENL message and, as such, has no command.
756const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
757const char AckMessage::kCommandString[] = "NL80211_ACK";
758
Wade Guthrie0d438532012-05-18 14:18:50 -0700759const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
760const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
761
Wade Guthrie0d438532012-05-18 14:18:50 -0700762const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
763const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
764
Wade Guthrie0d438532012-05-18 14:18:50 -0700765const uint8_t CancelRemainOnChannelMessage::kCommand =
766 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
767const char CancelRemainOnChannelMessage::kCommandString[] =
768 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
769
Wade Guthrie0d438532012-05-18 14:18:50 -0700770const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
771const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
772
Wade Guthrie0d438532012-05-18 14:18:50 -0700773const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
774const char DeauthenticateMessage::kCommandString[] =
775 "NL80211_CMD_DEAUTHENTICATE";
776
Wade Guthrie0d438532012-05-18 14:18:50 -0700777const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
778const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
779
Wade Guthrie0d438532012-05-18 14:18:50 -0700780const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
781const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
782
Wade Guthrie0d438532012-05-18 14:18:50 -0700783const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
784const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
785
repo sync0efa9f02012-12-28 13:40:20 -0800786// An Error is not a GENL message and, as such, has no command.
787const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
788const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
789
790ErrorMessage::ErrorMessage(uint32_t error)
791 : Nl80211Message(kCommand, kCommandString), error_(error) {}
792
793string ErrorMessage::ToString() const {
794 string output;
795 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
796 error_, strerror(error_));
797 return output;
798}
799
Wade Guthrie0d438532012-05-18 14:18:50 -0700800const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
801const char FrameTxStatusMessage::kCommandString[] =
802 "NL80211_CMD_FRAME_TX_STATUS";
803
repo sync0efa9f02012-12-28 13:40:20 -0800804const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
805const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
806
Wade Guthrie0d438532012-05-18 14:18:50 -0700807const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
808const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
809
Wade Guthrie0d438532012-05-18 14:18:50 -0700810const uint8_t MichaelMicFailureMessage::kCommand =
811 NL80211_CMD_MICHAEL_MIC_FAILURE;
812const char MichaelMicFailureMessage::kCommandString[] =
813 "NL80211_CMD_MICHAEL_MIC_FAILURE";
814
Wade Guthrie0d438532012-05-18 14:18:50 -0700815const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
816const char NewScanResultsMessage::kCommandString[] =
817 "NL80211_CMD_NEW_SCAN_RESULTS";
818
Wade Guthrie0d438532012-05-18 14:18:50 -0700819const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
820const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
821
Wade Guthrie0d438532012-05-18 14:18:50 -0700822const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
823const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
824
repo sync0efa9f02012-12-28 13:40:20 -0800825// A NOOP is not a GENL message and, as such, has no command.
826const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
827const char NoopMessage::kCommandString[] = "NL80211_NOOP";
828
Wade Guthrie0d438532012-05-18 14:18:50 -0700829const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
830const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
831
Wade Guthrie0d438532012-05-18 14:18:50 -0700832const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
833const char PmksaCandidateMessage::kCommandString[] =
834 "NL80211_ATTR_PMKSA_CANDIDATE";
835
Wade Guthrie0d438532012-05-18 14:18:50 -0700836const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
837const char RegBeaconHintMessage::kCommandString[] =
838 "NL80211_CMD_REG_BEACON_HINT";
839
Wade Guthrie0d438532012-05-18 14:18:50 -0700840const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
841const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
842
Wade Guthrie0d438532012-05-18 14:18:50 -0700843const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
844const char RemainOnChannelMessage::kCommandString[] =
845 "NL80211_CMD_REMAIN_ON_CHANNEL";
846
Wade Guthrie0d438532012-05-18 14:18:50 -0700847const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
848const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
849
Wade Guthrie0d438532012-05-18 14:18:50 -0700850const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
851const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
852
Wade Guthrie0d438532012-05-18 14:18:50 -0700853const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
854const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
855
Wade Guthrie0d438532012-05-18 14:18:50 -0700856const uint8_t UnknownMessage::kCommand = 0xff;
857const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
858
Wade Guthrie0d438532012-05-18 14:18:50 -0700859const uint8_t UnprotDeauthenticateMessage::kCommand =
860 NL80211_CMD_UNPROT_DEAUTHENTICATE;
861const char UnprotDeauthenticateMessage::kCommandString[] =
862 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
863
Wade Guthrie0d438532012-05-18 14:18:50 -0700864const uint8_t UnprotDisassociateMessage::kCommand =
865 NL80211_CMD_UNPROT_DISASSOCIATE;
866const char UnprotDisassociateMessage::kCommandString[] =
867 "NL80211_CMD_UNPROT_DISASSOCIATE";
868
Wade Guthrie0d438532012-05-18 14:18:50 -0700869//
870// Factory class.
871//
872
repo syncdc085c82012-12-28 08:54:41 -0800873Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700874 if (!msg) {
875 LOG(ERROR) << "NULL |msg| parameter";
876 return NULL;
877 }
878
repo syncdc085c82012-12-28 08:54:41 -0800879 scoped_ptr<Nl80211Message> message;
repo sync0efa9f02012-12-28 13:40:20 -0800880 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -0800881
repo sync0efa9f02012-12-28 13:40:20 -0800882 if (msg->nlmsg_type == NLMSG_NOOP) {
883 SLOG(WiFi, 6) << "Creating a NOP message";
884 message.reset(new NoopMessage());
885 } else if (msg->nlmsg_type == NLMSG_ERROR) {
886 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
887 if (error_code) {
888 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
889 message.reset(new ErrorMessage(error_code));
890 } else {
891 SLOG(WiFi, 6) << "Creating an ACK message";
892 message.reset(new AckMessage());
893 }
894 } else {
895 SLOG(WiFi, 6) << "Creating a Regular message";
896 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -0700897
repo sync0efa9f02012-12-28 13:40:20 -0800898 switch (gnlh->cmd) {
899 case AssociateMessage::kCommand:
900 message.reset(new AssociateMessage()); break;
901 case AuthenticateMessage::kCommand:
902 message.reset(new AuthenticateMessage()); break;
903 case CancelRemainOnChannelMessage::kCommand:
904 message.reset(new CancelRemainOnChannelMessage()); break;
905 case ConnectMessage::kCommand:
906 message.reset(new ConnectMessage()); break;
907 case DeauthenticateMessage::kCommand:
908 message.reset(new DeauthenticateMessage()); break;
909 case DeleteStationMessage::kCommand:
910 message.reset(new DeleteStationMessage()); break;
911 case DisassociateMessage::kCommand:
912 message.reset(new DisassociateMessage()); break;
913 case DisconnectMessage::kCommand:
914 message.reset(new DisconnectMessage()); break;
915 case FrameTxStatusMessage::kCommand:
916 message.reset(new FrameTxStatusMessage()); break;
917 case GetRegMessage::kCommand:
918 message.reset(new GetRegMessage()); break;
919 case JoinIbssMessage::kCommand:
920 message.reset(new JoinIbssMessage()); break;
921 case MichaelMicFailureMessage::kCommand:
922 message.reset(new MichaelMicFailureMessage()); break;
923 case NewScanResultsMessage::kCommand:
924 message.reset(new NewScanResultsMessage()); break;
925 case NewStationMessage::kCommand:
926 message.reset(new NewStationMessage()); break;
927 case NewWifiMessage::kCommand:
928 message.reset(new NewWifiMessage()); break;
929 case NotifyCqmMessage::kCommand:
930 message.reset(new NotifyCqmMessage()); break;
931 case PmksaCandidateMessage::kCommand:
932 message.reset(new PmksaCandidateMessage()); break;
933 case RegBeaconHintMessage::kCommand:
934 message.reset(new RegBeaconHintMessage()); break;
935 case RegChangeMessage::kCommand:
936 message.reset(new RegChangeMessage()); break;
937 case RemainOnChannelMessage::kCommand:
938 message.reset(new RemainOnChannelMessage()); break;
939 case RoamMessage::kCommand:
940 message.reset(new RoamMessage()); break;
941 case ScanAbortedMessage::kCommand:
942 message.reset(new ScanAbortedMessage()); break;
943 case TriggerScanMessage::kCommand:
944 message.reset(new TriggerScanMessage()); break;
945 case UnprotDeauthenticateMessage::kCommand:
946 message.reset(new UnprotDeauthenticateMessage()); break;
947 case UnprotDisassociateMessage::kCommand:
948 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -0700949
repo sync0efa9f02012-12-28 13:40:20 -0800950 default:
951 message.reset(new UnknownMessage(gnlh->cmd)); break;
952 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700953
repo sync0efa9f02012-12-28 13:40:20 -0800954 if (!message->InitFromNlmsg(msg)) {
955 LOG(ERROR) << "Message did not initialize properly";
956 return NULL;
957 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700958 }
959
Wade Guthrie0d438532012-05-18 14:18:50 -0700960 return message.release();
961}
962
repo syncdc085c82012-12-28 08:54:41 -0800963Nl80211MessageDataCollector *
964 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700965 return g_datacollector.Pointer();
966}
967
repo syncdc085c82012-12-28 08:54:41 -0800968Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700969 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
970 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
971 need_to_print[NL80211_CMD_DEL_STATION] = true;
972 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
973 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
974 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
975 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
976 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
977 need_to_print[NL80211_CMD_REG_CHANGE] = true;
978 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
979 need_to_print[NL80211_CMD_ROAM] = true;
980 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
981 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
982 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
983}
984
repo syncdc085c82012-12-28 08:54:41 -0800985void Nl80211MessageDataCollector::CollectDebugData(
986 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700987 if (!msg) {
988 LOG(ERROR) << "NULL |msg| parameter";
989 return;
990 }
991
992 bool doit = false;
993
994 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -0800995 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -0700996 if (node != need_to_print.end())
997 doit = node->second;
998
999 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001000 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001001 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001002 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001003
Christopher Wileyefd521f2012-11-07 17:32:46 -08001004 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001005
1006 size_t bytes = nlmsg_total_size(payload_bytes);
1007 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001008 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001009 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001010 << std::hex << std::setfill('0') << std::setw(2)
1011 << + rawdata[i] << ",";
1012 }
Wade Guthried6153612012-08-23 11:36:14 -07001013 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001014 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001015 }
1016}
1017
1018} // namespace shill.