blob: fe60011ec082c79fc17475228151e32418f91ae9 [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"
Wade Guthrief162f8b2013-02-27 14:13:55 -080052#include "shill/netlink_attribute.h"
repo syncdc085c82012-12-28 08:54:41 -080053#include "shill/netlink_socket.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
Wade Guthrie71cb0a72013-02-27 10:27:18 -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();
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800423
424 AttributeListConstRefPtr frequency_list;
425 if (!const_attributes()->ConstGetNestedAttributeList(
426 NL80211_ATTR_SCAN_FREQUENCIES, &frequency_list) || !frequency_list) {
427 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_FREQUENCIES attribute";
repo syncd316eb72012-12-10 15:48:47 -0800428 return false;
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800429 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700430
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800431 // Assume IDs for the nested attribute array are linear starting from 1.
432 // Currently, that is enforced in the input to the nested attribute.
433 uint32_t freq;
434 int i = 1;
435 while (frequency_list->GetU32AttributeValue(i, &freq)) {
436 value->push_back(freq);
437 ++i;
Wade Guthrie0d438532012-05-18 14:18:50 -0700438 }
repo syncd316eb72012-12-10 15:48:47 -0800439 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700440}
441
442// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800443bool Nl80211Message::GetScanSsidsAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800444 int id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700445 if (!value) {
446 LOG(ERROR) << "Null |value| parameter";
447 return false;
448 }
449
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800450 AttributeListConstRefPtr ssid_list;
451 if (!const_attributes()->ConstGetNestedAttributeList(
452 NL80211_ATTR_SCAN_SSIDS, &ssid_list) || !ssid_list) {
453 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_SSIDS attribute";
repo sync90ee0fa2012-12-18 10:08:08 -0800454 return false;
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800455 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700456
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800457 // Assume IDs for the nested attribute array are linear starting from 1.
458 // Currently, that is enforced in the input to the nested attribute.
459 string ssid;
460 int i = 1;
461 while (ssid_list->GetStringAttributeValue(i, &ssid)) {
462 value->push_back(ssid);
463 ++i;
Wade Guthrie0d438532012-05-18 14:18:50 -0700464 }
repo sync90ee0fa2012-12-18 10:08:08 -0800465 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700466}
467
Wade Guthrie0d438532012-05-18 14:18:50 -0700468// Protected members.
469
repo syncdc085c82012-12-28 08:54:41 -0800470string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700471 char ifname[IF_NAMESIZE] = "";
472 uint32_t ifindex = UINT32_MAX;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800473 bool ifindex_exists = const_attributes()->GetU32AttributeValue(
474 NL80211_ATTR_IFINDEX, &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700475
476 uint32_t wifi = UINT32_MAX;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800477 bool wifi_exists = const_attributes()->GetU32AttributeValue(
478 NL80211_ATTR_WIPHY, &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700479
480 string output;
481 if (ifindex_exists && wifi_exists) {
482 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
483 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
484 wifi);
485 } else if (ifindex_exists) {
486 StringAppendF(&output, "%s: ",
487 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
488 } else if (wifi_exists) {
489 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
490 }
491
492 return output;
493}
494
Wade Guthrie68da97c2013-02-26 13:09:35 -0800495string Nl80211Message::StringFromFrame(int attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700496 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800497 ByteString frame_data;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800498 if (const_attributes()->GetRawAttributeValue(attr_name, &frame_data) &&
499 !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800500 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700501 frame.ToString(&output);
502 } else {
503 output.append(" [no frame]");
504 }
505
506 return output;
507}
508
509// static
repo syncdc085c82012-12-28 08:54:41 -0800510string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700511 switch (key_type) {
512 case NL80211_KEYTYPE_GROUP:
513 return "Group";
514 case NL80211_KEYTYPE_PAIRWISE:
515 return "Pairwise";
516 case NL80211_KEYTYPE_PEERKEY:
517 return "PeerKey";
518 default:
519 return "<Unknown Key Type>";
520 }
521}
522
523// static
repo syncdc085c82012-12-28 08:54:41 -0800524string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700525 string output;
526
527 if (!arg) {
528 output = kBogusMacAddress;
529 LOG(ERROR) << "|arg| parameter is NULL.";
530 } else {
531 StringAppendF(&output, "%02x", arg[0]);
532
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800533 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700534 StringAppendF(&output, ":%02x", arg[i]);
535 }
536 }
537 return output;
538}
539
540// static
repo syncdc085c82012-12-28 08:54:41 -0800541string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700542 switch (initiator) {
543 case NL80211_REGDOM_SET_BY_CORE:
544 return "the wireless core upon initialization";
545 case NL80211_REGDOM_SET_BY_USER:
546 return "a user";
547 case NL80211_REGDOM_SET_BY_DRIVER:
548 return "a driver";
549 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
550 return "a country IE";
551 default:
552 return "<Unknown Reg Initiator>";
553 }
554}
555
556// static
repo syncdc085c82012-12-28 08:54:41 -0800557string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700558 const uint8_t *data) {
559 string output;
560 if (!data) {
561 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
562 LOG(ERROR) << "|data| parameter is NULL.";
563 return output;
564 }
565
566 for (int i = 0; i < len; ++i) {
567 if (data[i] == ' ')
568 output.append(" ");
569 else if (isprint(data[i]))
570 StringAppendF(&output, "%c", static_cast<char>(data[i]));
571 else
572 StringAppendF(&output, "\\x%2x", data[i]);
573 }
574
575 return output;
576}
577
578// static
repo syncdc085c82012-12-28 08:54:41 -0800579string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700580 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700581 match = reason_code_string_->find(status);
582 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700583 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700584 if (status < IEEE_80211::kReasonCodeMax) {
585 StringAppendF(&output, "<Reserved Reason:%u>", status);
586 } else {
587 StringAppendF(&output, "<Unknown Reason:%u>", status);
588 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700589 return output;
590 }
591 return match->second;
592}
593
Wade Guthried4977f22012-08-22 12:37:54 -0700594// static
repo syncdc085c82012-12-28 08:54:41 -0800595string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700596 map<uint16_t, string>::const_iterator match;
597 match = status_code_string_->find(status);
598 if (match == status_code_string_->end()) {
599 string output;
600 if (status < IEEE_80211::kStatusCodeMax) {
601 StringAppendF(&output, "<Reserved Status:%u>", status);
602 } else {
603 StringAppendF(&output, "<Unknown Status:%u>", status);
604 }
605 return output;
606 }
607 return match->second;
608}
609
repo syncdc085c82012-12-28 08:54:41 -0800610ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const {
611 // Build netlink header.
612 nlmsghdr header;
613 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
614 header.nlmsg_len = nlmsghdr_with_pad;
615 header.nlmsg_type = nlmsg_type;
616 header.nlmsg_flags = NLM_F_REQUEST;
617 header.nlmsg_seq = sequence_number();
618 header.nlmsg_pid = getpid();
619
620 // Build genl message header.
621 genlmsghdr genl_header;
622 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
623 header.nlmsg_len += genlmsghdr_with_pad;
624 genl_header.cmd = message_type();
625 genl_header.version = 1;
626 genl_header.reserved = 0;
627
628 // Assemble attributes (padding is included by AttributeList::Encode).
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800629 ByteString attribute_bytes = attributes_->Encode();
630 header.nlmsg_len += attribute_bytes.GetLength();
repo syncdc085c82012-12-28 08:54:41 -0800631
632 // Now that we know the total message size, build the output ByteString.
633 ByteString result;
634
635 // Netlink header + pad.
636 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
637 sizeof(header)));
638 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
639
640 // Genl message header + pad.
641 result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header),
642 sizeof(genl_header)));
643 result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill.
644
645 // Attributes including pad.
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800646 result.Append(attribute_bytes);
repo syncdc085c82012-12-28 08:54:41 -0800647
648 return result;
649}
650
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800651Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700652 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800653 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700654 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800655 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
656 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700657
658 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800659 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700660 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800661 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
662 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700663 frame_type_ = frame->frame_control & kFrameTypeMask;
664
665 switch (frame_type_) {
666 case kAssocResponseFrameType:
667 case kReassocResponseFrameType:
668 status_ = le16toh(frame->u.associate_response.status_code);
669 break;
670
671 case kAuthFrameType:
672 status_ = le16toh(frame->u.authentiate_message.status_code);
673 break;
674
675 case kDisassocFrameType:
676 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700677 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700678 break;
679
680 default:
681 break;
682 }
683 }
684}
685
Wade Guthried4977f22012-08-22 12:37:54 -0700686bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700687 if (!output) {
688 LOG(ERROR) << "NULL |output|";
689 return false;
690 }
691
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800692 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700693 output->append(" [no frame]");
694 return true;
695 }
696
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800697 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700698 output->append(" [invalid frame: ");
699 } else {
700 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
701
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800702 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700703 case kAssocResponseFrameType:
704 StringAppendF(output, "; AssocResponse status: %u: %s",
705 status_,
repo syncdc085c82012-12-28 08:54:41 -0800706 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700707 break;
708 case kReassocResponseFrameType:
709 StringAppendF(output, "; ReassocResponse status: %u: %s",
710 status_,
repo syncdc085c82012-12-28 08:54:41 -0800711 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700712 break;
713 case kAuthFrameType:
714 StringAppendF(output, "; Auth status: %u: %s",
715 status_,
repo syncdc085c82012-12-28 08:54:41 -0800716 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700717 break;
718
719 case kDisassocFrameType:
720 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700721 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800722 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700723 break;
724 case kDeauthFrameType:
725 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700726 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800727 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700728 break;
729
730 default:
731 break;
732 }
733 output->append(" [frame: ");
734 }
735
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800736 const unsigned char *frame = frame_.GetConstData();
737 for (size_t i = 0; i < frame_.GetLength(); ++i) {
738 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700739 }
740 output->append("]");
741
742 return true;
743}
744
Wade Guthried4977f22012-08-22 12:37:54 -0700745bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800746 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700747}
748
Wade Guthried4977f22012-08-22 12:37:54 -0700749
Wade Guthrie0d438532012-05-18 14:18:50 -0700750//
repo syncdc085c82012-12-28 08:54:41 -0800751// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700752//
753
repo sync0efa9f02012-12-28 13:40:20 -0800754// An Ack is not a GENL message and, as such, has no command.
755const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
756const char AckMessage::kCommandString[] = "NL80211_ACK";
757
Wade Guthrie0d438532012-05-18 14:18:50 -0700758const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
759const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
760
Wade Guthrie0d438532012-05-18 14:18:50 -0700761const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
762const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
763
Wade Guthrie0d438532012-05-18 14:18:50 -0700764const uint8_t CancelRemainOnChannelMessage::kCommand =
765 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
766const char CancelRemainOnChannelMessage::kCommandString[] =
767 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
768
Wade Guthrie0d438532012-05-18 14:18:50 -0700769const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
770const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
771
Wade Guthrie0d438532012-05-18 14:18:50 -0700772const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
773const char DeauthenticateMessage::kCommandString[] =
774 "NL80211_CMD_DEAUTHENTICATE";
775
Wade Guthrie0d438532012-05-18 14:18:50 -0700776const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
777const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
778
Wade Guthrie0d438532012-05-18 14:18:50 -0700779const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
780const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
781
Wade Guthrie0d438532012-05-18 14:18:50 -0700782const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
783const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
784
repo sync0efa9f02012-12-28 13:40:20 -0800785// An Error is not a GENL message and, as such, has no command.
786const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
787const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
788
789ErrorMessage::ErrorMessage(uint32_t error)
790 : Nl80211Message(kCommand, kCommandString), error_(error) {}
791
792string ErrorMessage::ToString() const {
793 string output;
794 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
795 error_, strerror(error_));
796 return output;
797}
798
Wade Guthrie0d438532012-05-18 14:18:50 -0700799const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
800const char FrameTxStatusMessage::kCommandString[] =
801 "NL80211_CMD_FRAME_TX_STATUS";
802
repo sync0efa9f02012-12-28 13:40:20 -0800803const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
804const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
805
Wade Guthrie0d438532012-05-18 14:18:50 -0700806const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
807const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
808
Wade Guthrie0d438532012-05-18 14:18:50 -0700809const uint8_t MichaelMicFailureMessage::kCommand =
810 NL80211_CMD_MICHAEL_MIC_FAILURE;
811const char MichaelMicFailureMessage::kCommandString[] =
812 "NL80211_CMD_MICHAEL_MIC_FAILURE";
813
Wade Guthrie0d438532012-05-18 14:18:50 -0700814const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
815const char NewScanResultsMessage::kCommandString[] =
816 "NL80211_CMD_NEW_SCAN_RESULTS";
817
Wade Guthrie0d438532012-05-18 14:18:50 -0700818const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
819const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
820
Wade Guthrie0d438532012-05-18 14:18:50 -0700821const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
822const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
823
repo sync0efa9f02012-12-28 13:40:20 -0800824// A NOOP is not a GENL message and, as such, has no command.
825const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
826const char NoopMessage::kCommandString[] = "NL80211_NOOP";
827
Wade Guthrie0d438532012-05-18 14:18:50 -0700828const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
829const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
830
Wade Guthrie0d438532012-05-18 14:18:50 -0700831const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
832const char PmksaCandidateMessage::kCommandString[] =
833 "NL80211_ATTR_PMKSA_CANDIDATE";
834
Wade Guthrie0d438532012-05-18 14:18:50 -0700835const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
836const char RegBeaconHintMessage::kCommandString[] =
837 "NL80211_CMD_REG_BEACON_HINT";
838
Wade Guthrie0d438532012-05-18 14:18:50 -0700839const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
840const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
841
Wade Guthrie0d438532012-05-18 14:18:50 -0700842const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
843const char RemainOnChannelMessage::kCommandString[] =
844 "NL80211_CMD_REMAIN_ON_CHANNEL";
845
Wade Guthrie0d438532012-05-18 14:18:50 -0700846const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
847const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
848
Wade Guthrie0d438532012-05-18 14:18:50 -0700849const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
850const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
851
Wade Guthrie0d438532012-05-18 14:18:50 -0700852const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
853const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
854
Wade Guthrie0d438532012-05-18 14:18:50 -0700855const uint8_t UnknownMessage::kCommand = 0xff;
856const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
857
Wade Guthrie0d438532012-05-18 14:18:50 -0700858const uint8_t UnprotDeauthenticateMessage::kCommand =
859 NL80211_CMD_UNPROT_DEAUTHENTICATE;
860const char UnprotDeauthenticateMessage::kCommandString[] =
861 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
862
Wade Guthrie0d438532012-05-18 14:18:50 -0700863const uint8_t UnprotDisassociateMessage::kCommand =
864 NL80211_CMD_UNPROT_DISASSOCIATE;
865const char UnprotDisassociateMessage::kCommandString[] =
866 "NL80211_CMD_UNPROT_DISASSOCIATE";
867
Wade Guthrie0d438532012-05-18 14:18:50 -0700868//
869// Factory class.
870//
871
repo syncdc085c82012-12-28 08:54:41 -0800872Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700873 if (!msg) {
874 LOG(ERROR) << "NULL |msg| parameter";
875 return NULL;
876 }
877
repo syncdc085c82012-12-28 08:54:41 -0800878 scoped_ptr<Nl80211Message> message;
repo sync0efa9f02012-12-28 13:40:20 -0800879 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -0800880
repo sync0efa9f02012-12-28 13:40:20 -0800881 if (msg->nlmsg_type == NLMSG_NOOP) {
882 SLOG(WiFi, 6) << "Creating a NOP message";
883 message.reset(new NoopMessage());
884 } else if (msg->nlmsg_type == NLMSG_ERROR) {
885 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
886 if (error_code) {
887 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
888 message.reset(new ErrorMessage(error_code));
889 } else {
890 SLOG(WiFi, 6) << "Creating an ACK message";
891 message.reset(new AckMessage());
892 }
893 } else {
894 SLOG(WiFi, 6) << "Creating a Regular message";
895 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -0700896
repo sync0efa9f02012-12-28 13:40:20 -0800897 switch (gnlh->cmd) {
898 case AssociateMessage::kCommand:
899 message.reset(new AssociateMessage()); break;
900 case AuthenticateMessage::kCommand:
901 message.reset(new AuthenticateMessage()); break;
902 case CancelRemainOnChannelMessage::kCommand:
903 message.reset(new CancelRemainOnChannelMessage()); break;
904 case ConnectMessage::kCommand:
905 message.reset(new ConnectMessage()); break;
906 case DeauthenticateMessage::kCommand:
907 message.reset(new DeauthenticateMessage()); break;
908 case DeleteStationMessage::kCommand:
909 message.reset(new DeleteStationMessage()); break;
910 case DisassociateMessage::kCommand:
911 message.reset(new DisassociateMessage()); break;
912 case DisconnectMessage::kCommand:
913 message.reset(new DisconnectMessage()); break;
914 case FrameTxStatusMessage::kCommand:
915 message.reset(new FrameTxStatusMessage()); break;
916 case GetRegMessage::kCommand:
917 message.reset(new GetRegMessage()); break;
918 case JoinIbssMessage::kCommand:
919 message.reset(new JoinIbssMessage()); break;
920 case MichaelMicFailureMessage::kCommand:
921 message.reset(new MichaelMicFailureMessage()); break;
922 case NewScanResultsMessage::kCommand:
923 message.reset(new NewScanResultsMessage()); break;
924 case NewStationMessage::kCommand:
925 message.reset(new NewStationMessage()); break;
926 case NewWifiMessage::kCommand:
927 message.reset(new NewWifiMessage()); break;
928 case NotifyCqmMessage::kCommand:
929 message.reset(new NotifyCqmMessage()); break;
930 case PmksaCandidateMessage::kCommand:
931 message.reset(new PmksaCandidateMessage()); break;
932 case RegBeaconHintMessage::kCommand:
933 message.reset(new RegBeaconHintMessage()); break;
934 case RegChangeMessage::kCommand:
935 message.reset(new RegChangeMessage()); break;
936 case RemainOnChannelMessage::kCommand:
937 message.reset(new RemainOnChannelMessage()); break;
938 case RoamMessage::kCommand:
939 message.reset(new RoamMessage()); break;
940 case ScanAbortedMessage::kCommand:
941 message.reset(new ScanAbortedMessage()); break;
942 case TriggerScanMessage::kCommand:
943 message.reset(new TriggerScanMessage()); break;
944 case UnprotDeauthenticateMessage::kCommand:
945 message.reset(new UnprotDeauthenticateMessage()); break;
946 case UnprotDisassociateMessage::kCommand:
947 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -0700948
repo sync0efa9f02012-12-28 13:40:20 -0800949 default:
950 message.reset(new UnknownMessage(gnlh->cmd)); break;
951 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700952
repo sync0efa9f02012-12-28 13:40:20 -0800953 if (!message->InitFromNlmsg(msg)) {
954 LOG(ERROR) << "Message did not initialize properly";
955 return NULL;
956 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700957 }
958
Wade Guthrie0d438532012-05-18 14:18:50 -0700959 return message.release();
960}
961
repo syncdc085c82012-12-28 08:54:41 -0800962Nl80211MessageDataCollector *
963 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700964 return g_datacollector.Pointer();
965}
966
repo syncdc085c82012-12-28 08:54:41 -0800967Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700968 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
969 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
970 need_to_print[NL80211_CMD_DEL_STATION] = true;
971 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
972 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
973 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
974 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
975 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
976 need_to_print[NL80211_CMD_REG_CHANGE] = true;
977 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
978 need_to_print[NL80211_CMD_ROAM] = true;
979 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
980 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
981 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
982}
983
repo syncdc085c82012-12-28 08:54:41 -0800984void Nl80211MessageDataCollector::CollectDebugData(
985 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700986 if (!msg) {
987 LOG(ERROR) << "NULL |msg| parameter";
988 return;
989 }
990
991 bool doit = false;
992
993 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -0800994 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -0700995 if (node != need_to_print.end())
996 doit = node->second;
997
998 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -0700999 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001000 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001001 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001002
Christopher Wileyefd521f2012-11-07 17:32:46 -08001003 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001004
1005 size_t bytes = nlmsg_total_size(payload_bytes);
1006 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001007 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001008 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001009 << std::hex << std::setfill('0') << std::setw(2)
1010 << + rawdata[i] << ",";
1011 }
Wade Guthried6153612012-08-23 11:36:14 -07001012 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001013 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001014 }
1015}
1016
1017} // namespace shill.