blob: 4b5ef2c858c8be755a3ecc57fa69bcc64e2371e9 [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());
115 attributes_.Print(log_level, 1);
116}
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 Guthrie68da97c2013-02-26 13:09:35 -0800145 attributes_.CreateAndInitAttribute(
146 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;
repo sync12cca802012-12-19 17:34:22 -0800405 if (!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;
repo sync12cca802012-12-19 17:34:22 -0800424 if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800425 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700426
repo syncd316eb72012-12-10 15:48:47 -0800427 nlattr *nst = NULL;
428 // |nla_for_each_attr| requires a non-const parameter even though it
429 // doesn't change the data.
430 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
431 int rem_nst;
432 int len = rawdata.GetLength();
433
434 nla_for_each_attr(nst, attr_data, len, rem_nst) {
Wade Guthrie68da97c2013-02-26 13:09:35 -0800435 value->push_back(NetlinkAttribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700436 }
repo syncd316eb72012-12-10 15:48:47 -0800437 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700438}
439
440// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800441bool Nl80211Message::GetScanSsidsAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800442 int id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700443 if (!value) {
444 LOG(ERROR) << "Null |value| parameter";
445 return false;
446 }
447
repo sync90ee0fa2012-12-18 10:08:08 -0800448 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800449 if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty())
repo sync90ee0fa2012-12-18 10:08:08 -0800450 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700451
repo sync90ee0fa2012-12-18 10:08:08 -0800452 nlattr *nst = NULL;
453 // |nla_for_each_attr| requires a non-const parameter even though it
454 // doesn't change the data.
455 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
456 int rem_nst;
457 int len = rawdata.GetLength();
458
459 nla_for_each_attr(nst, data, len, rem_nst) {
460 value->push_back(StringFromSsid(nla_len(nst),
461 reinterpret_cast<const uint8_t *>(
462 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700463 }
repo sync90ee0fa2012-12-18 10:08:08 -0800464 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700465}
466
Wade Guthrie0d438532012-05-18 14:18:50 -0700467// Protected members.
468
repo syncdc085c82012-12-28 08:54:41 -0800469string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700470 char ifname[IF_NAMESIZE] = "";
471 uint32_t ifindex = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800472 bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
473 &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700474
475 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800476 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
477 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700478
479 string output;
480 if (ifindex_exists && wifi_exists) {
481 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
482 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
483 wifi);
484 } else if (ifindex_exists) {
485 StringAppendF(&output, "%s: ",
486 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
487 } else if (wifi_exists) {
488 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
489 }
490
491 return output;
492}
493
Wade Guthrie68da97c2013-02-26 13:09:35 -0800494string Nl80211Message::StringFromFrame(int attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700495 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800496 ByteString frame_data;
repo sync90ee0fa2012-12-18 10:08:08 -0800497 if (attributes().GetRawAttributeValue(attr_name,
498 &frame_data) && !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800499 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700500 frame.ToString(&output);
501 } else {
502 output.append(" [no frame]");
503 }
504
505 return output;
506}
507
508// static
repo syncdc085c82012-12-28 08:54:41 -0800509string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700510 switch (key_type) {
511 case NL80211_KEYTYPE_GROUP:
512 return "Group";
513 case NL80211_KEYTYPE_PAIRWISE:
514 return "Pairwise";
515 case NL80211_KEYTYPE_PEERKEY:
516 return "PeerKey";
517 default:
518 return "<Unknown Key Type>";
519 }
520}
521
522// static
repo syncdc085c82012-12-28 08:54:41 -0800523string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700524 string output;
525
526 if (!arg) {
527 output = kBogusMacAddress;
528 LOG(ERROR) << "|arg| parameter is NULL.";
529 } else {
530 StringAppendF(&output, "%02x", arg[0]);
531
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800532 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700533 StringAppendF(&output, ":%02x", arg[i]);
534 }
535 }
536 return output;
537}
538
539// static
repo syncdc085c82012-12-28 08:54:41 -0800540string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700541 switch (initiator) {
542 case NL80211_REGDOM_SET_BY_CORE:
543 return "the wireless core upon initialization";
544 case NL80211_REGDOM_SET_BY_USER:
545 return "a user";
546 case NL80211_REGDOM_SET_BY_DRIVER:
547 return "a driver";
548 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
549 return "a country IE";
550 default:
551 return "<Unknown Reg Initiator>";
552 }
553}
554
555// static
repo syncdc085c82012-12-28 08:54:41 -0800556string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700557 const uint8_t *data) {
558 string output;
559 if (!data) {
560 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
561 LOG(ERROR) << "|data| parameter is NULL.";
562 return output;
563 }
564
565 for (int i = 0; i < len; ++i) {
566 if (data[i] == ' ')
567 output.append(" ");
568 else if (isprint(data[i]))
569 StringAppendF(&output, "%c", static_cast<char>(data[i]));
570 else
571 StringAppendF(&output, "\\x%2x", data[i]);
572 }
573
574 return output;
575}
576
577// static
repo syncdc085c82012-12-28 08:54:41 -0800578string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700579 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700580 match = reason_code_string_->find(status);
581 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700582 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700583 if (status < IEEE_80211::kReasonCodeMax) {
584 StringAppendF(&output, "<Reserved Reason:%u>", status);
585 } else {
586 StringAppendF(&output, "<Unknown Reason:%u>", status);
587 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700588 return output;
589 }
590 return match->second;
591}
592
Wade Guthried4977f22012-08-22 12:37:54 -0700593// static
repo syncdc085c82012-12-28 08:54:41 -0800594string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700595 map<uint16_t, string>::const_iterator match;
596 match = status_code_string_->find(status);
597 if (match == status_code_string_->end()) {
598 string output;
599 if (status < IEEE_80211::kStatusCodeMax) {
600 StringAppendF(&output, "<Reserved Status:%u>", status);
601 } else {
602 StringAppendF(&output, "<Unknown Status:%u>", status);
603 }
604 return output;
605 }
606 return match->second;
607}
608
repo syncdc085c82012-12-28 08:54:41 -0800609ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const {
610 // Build netlink header.
611 nlmsghdr header;
612 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
613 header.nlmsg_len = nlmsghdr_with_pad;
614 header.nlmsg_type = nlmsg_type;
615 header.nlmsg_flags = NLM_F_REQUEST;
616 header.nlmsg_seq = sequence_number();
617 header.nlmsg_pid = getpid();
618
619 // Build genl message header.
620 genlmsghdr genl_header;
621 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
622 header.nlmsg_len += genlmsghdr_with_pad;
623 genl_header.cmd = message_type();
624 genl_header.version = 1;
625 genl_header.reserved = 0;
626
627 // Assemble attributes (padding is included by AttributeList::Encode).
628 ByteString attributes = attributes_.Encode();
629 header.nlmsg_len += attributes.GetLength();
630
631 // Now that we know the total message size, build the output ByteString.
632 ByteString result;
633
634 // Netlink header + pad.
635 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
636 sizeof(header)));
637 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
638
639 // Genl message header + pad.
640 result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header),
641 sizeof(genl_header)));
642 result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill.
643
644 // Attributes including pad.
645 result.Append(attributes);
646
647 return result;
648}
649
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800650Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700651 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800652 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700653 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800654 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
655 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700656
657 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800658 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700659 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800660 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
661 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700662 frame_type_ = frame->frame_control & kFrameTypeMask;
663
664 switch (frame_type_) {
665 case kAssocResponseFrameType:
666 case kReassocResponseFrameType:
667 status_ = le16toh(frame->u.associate_response.status_code);
668 break;
669
670 case kAuthFrameType:
671 status_ = le16toh(frame->u.authentiate_message.status_code);
672 break;
673
674 case kDisassocFrameType:
675 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700676 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700677 break;
678
679 default:
680 break;
681 }
682 }
683}
684
Wade Guthried4977f22012-08-22 12:37:54 -0700685bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700686 if (!output) {
687 LOG(ERROR) << "NULL |output|";
688 return false;
689 }
690
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800691 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700692 output->append(" [no frame]");
693 return true;
694 }
695
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800696 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700697 output->append(" [invalid frame: ");
698 } else {
699 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
700
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800701 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700702 case kAssocResponseFrameType:
703 StringAppendF(output, "; AssocResponse status: %u: %s",
704 status_,
repo syncdc085c82012-12-28 08:54:41 -0800705 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700706 break;
707 case kReassocResponseFrameType:
708 StringAppendF(output, "; ReassocResponse status: %u: %s",
709 status_,
repo syncdc085c82012-12-28 08:54:41 -0800710 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700711 break;
712 case kAuthFrameType:
713 StringAppendF(output, "; Auth status: %u: %s",
714 status_,
repo syncdc085c82012-12-28 08:54:41 -0800715 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700716 break;
717
718 case kDisassocFrameType:
719 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700720 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800721 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700722 break;
723 case kDeauthFrameType:
724 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700725 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800726 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700727 break;
728
729 default:
730 break;
731 }
732 output->append(" [frame: ");
733 }
734
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800735 const unsigned char *frame = frame_.GetConstData();
736 for (size_t i = 0; i < frame_.GetLength(); ++i) {
737 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700738 }
739 output->append("]");
740
741 return true;
742}
743
Wade Guthried4977f22012-08-22 12:37:54 -0700744bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800745 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700746}
747
Wade Guthried4977f22012-08-22 12:37:54 -0700748
Wade Guthrie0d438532012-05-18 14:18:50 -0700749//
repo syncdc085c82012-12-28 08:54:41 -0800750// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700751//
752
repo sync0efa9f02012-12-28 13:40:20 -0800753// An Ack is not a GENL message and, as such, has no command.
754const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
755const char AckMessage::kCommandString[] = "NL80211_ACK";
756
Wade Guthrie0d438532012-05-18 14:18:50 -0700757const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
758const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
759
Wade Guthrie0d438532012-05-18 14:18:50 -0700760const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
761const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
762
Wade Guthrie0d438532012-05-18 14:18:50 -0700763const uint8_t CancelRemainOnChannelMessage::kCommand =
764 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
765const char CancelRemainOnChannelMessage::kCommandString[] =
766 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
767
Wade Guthrie0d438532012-05-18 14:18:50 -0700768const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
769const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
770
Wade Guthrie0d438532012-05-18 14:18:50 -0700771const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
772const char DeauthenticateMessage::kCommandString[] =
773 "NL80211_CMD_DEAUTHENTICATE";
774
Wade Guthrie0d438532012-05-18 14:18:50 -0700775const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
776const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
777
Wade Guthrie0d438532012-05-18 14:18:50 -0700778const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
779const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
780
Wade Guthrie0d438532012-05-18 14:18:50 -0700781const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
782const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
783
repo sync0efa9f02012-12-28 13:40:20 -0800784// An Error is not a GENL message and, as such, has no command.
785const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
786const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
787
788ErrorMessage::ErrorMessage(uint32_t error)
789 : Nl80211Message(kCommand, kCommandString), error_(error) {}
790
791string ErrorMessage::ToString() const {
792 string output;
793 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
794 error_, strerror(error_));
795 return output;
796}
797
Wade Guthrie0d438532012-05-18 14:18:50 -0700798const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
799const char FrameTxStatusMessage::kCommandString[] =
800 "NL80211_CMD_FRAME_TX_STATUS";
801
repo sync0efa9f02012-12-28 13:40:20 -0800802const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
803const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
804
Wade Guthrie0d438532012-05-18 14:18:50 -0700805const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
806const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
807
Wade Guthrie0d438532012-05-18 14:18:50 -0700808const uint8_t MichaelMicFailureMessage::kCommand =
809 NL80211_CMD_MICHAEL_MIC_FAILURE;
810const char MichaelMicFailureMessage::kCommandString[] =
811 "NL80211_CMD_MICHAEL_MIC_FAILURE";
812
Wade Guthrie0d438532012-05-18 14:18:50 -0700813const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
814const char NewScanResultsMessage::kCommandString[] =
815 "NL80211_CMD_NEW_SCAN_RESULTS";
816
Wade Guthrie0d438532012-05-18 14:18:50 -0700817const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
818const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
819
Wade Guthrie0d438532012-05-18 14:18:50 -0700820const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
821const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
822
repo sync0efa9f02012-12-28 13:40:20 -0800823// A NOOP is not a GENL message and, as such, has no command.
824const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
825const char NoopMessage::kCommandString[] = "NL80211_NOOP";
826
Wade Guthrie0d438532012-05-18 14:18:50 -0700827const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
828const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
829
Wade Guthrie0d438532012-05-18 14:18:50 -0700830const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
831const char PmksaCandidateMessage::kCommandString[] =
832 "NL80211_ATTR_PMKSA_CANDIDATE";
833
Wade Guthrie0d438532012-05-18 14:18:50 -0700834const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
835const char RegBeaconHintMessage::kCommandString[] =
836 "NL80211_CMD_REG_BEACON_HINT";
837
Wade Guthrie0d438532012-05-18 14:18:50 -0700838const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
839const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
840
Wade Guthrie0d438532012-05-18 14:18:50 -0700841const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
842const char RemainOnChannelMessage::kCommandString[] =
843 "NL80211_CMD_REMAIN_ON_CHANNEL";
844
Wade Guthrie0d438532012-05-18 14:18:50 -0700845const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
846const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
847
Wade Guthrie0d438532012-05-18 14:18:50 -0700848const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
849const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
850
Wade Guthrie0d438532012-05-18 14:18:50 -0700851const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
852const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
853
Wade Guthrie0d438532012-05-18 14:18:50 -0700854const uint8_t UnknownMessage::kCommand = 0xff;
855const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
856
Wade Guthrie0d438532012-05-18 14:18:50 -0700857const uint8_t UnprotDeauthenticateMessage::kCommand =
858 NL80211_CMD_UNPROT_DEAUTHENTICATE;
859const char UnprotDeauthenticateMessage::kCommandString[] =
860 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
861
Wade Guthrie0d438532012-05-18 14:18:50 -0700862const uint8_t UnprotDisassociateMessage::kCommand =
863 NL80211_CMD_UNPROT_DISASSOCIATE;
864const char UnprotDisassociateMessage::kCommandString[] =
865 "NL80211_CMD_UNPROT_DISASSOCIATE";
866
Wade Guthrie0d438532012-05-18 14:18:50 -0700867//
868// Factory class.
869//
870
repo syncdc085c82012-12-28 08:54:41 -0800871Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700872 if (!msg) {
873 LOG(ERROR) << "NULL |msg| parameter";
874 return NULL;
875 }
876
repo syncdc085c82012-12-28 08:54:41 -0800877 scoped_ptr<Nl80211Message> message;
repo sync0efa9f02012-12-28 13:40:20 -0800878 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -0800879
repo sync0efa9f02012-12-28 13:40:20 -0800880 if (msg->nlmsg_type == NLMSG_NOOP) {
881 SLOG(WiFi, 6) << "Creating a NOP message";
882 message.reset(new NoopMessage());
883 } else if (msg->nlmsg_type == NLMSG_ERROR) {
884 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
885 if (error_code) {
886 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
887 message.reset(new ErrorMessage(error_code));
888 } else {
889 SLOG(WiFi, 6) << "Creating an ACK message";
890 message.reset(new AckMessage());
891 }
892 } else {
893 SLOG(WiFi, 6) << "Creating a Regular message";
894 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -0700895
repo sync0efa9f02012-12-28 13:40:20 -0800896 switch (gnlh->cmd) {
897 case AssociateMessage::kCommand:
898 message.reset(new AssociateMessage()); break;
899 case AuthenticateMessage::kCommand:
900 message.reset(new AuthenticateMessage()); break;
901 case CancelRemainOnChannelMessage::kCommand:
902 message.reset(new CancelRemainOnChannelMessage()); break;
903 case ConnectMessage::kCommand:
904 message.reset(new ConnectMessage()); break;
905 case DeauthenticateMessage::kCommand:
906 message.reset(new DeauthenticateMessage()); break;
907 case DeleteStationMessage::kCommand:
908 message.reset(new DeleteStationMessage()); break;
909 case DisassociateMessage::kCommand:
910 message.reset(new DisassociateMessage()); break;
911 case DisconnectMessage::kCommand:
912 message.reset(new DisconnectMessage()); break;
913 case FrameTxStatusMessage::kCommand:
914 message.reset(new FrameTxStatusMessage()); break;
915 case GetRegMessage::kCommand:
916 message.reset(new GetRegMessage()); break;
917 case JoinIbssMessage::kCommand:
918 message.reset(new JoinIbssMessage()); break;
919 case MichaelMicFailureMessage::kCommand:
920 message.reset(new MichaelMicFailureMessage()); break;
921 case NewScanResultsMessage::kCommand:
922 message.reset(new NewScanResultsMessage()); break;
923 case NewStationMessage::kCommand:
924 message.reset(new NewStationMessage()); break;
925 case NewWifiMessage::kCommand:
926 message.reset(new NewWifiMessage()); break;
927 case NotifyCqmMessage::kCommand:
928 message.reset(new NotifyCqmMessage()); break;
929 case PmksaCandidateMessage::kCommand:
930 message.reset(new PmksaCandidateMessage()); break;
931 case RegBeaconHintMessage::kCommand:
932 message.reset(new RegBeaconHintMessage()); break;
933 case RegChangeMessage::kCommand:
934 message.reset(new RegChangeMessage()); break;
935 case RemainOnChannelMessage::kCommand:
936 message.reset(new RemainOnChannelMessage()); break;
937 case RoamMessage::kCommand:
938 message.reset(new RoamMessage()); break;
939 case ScanAbortedMessage::kCommand:
940 message.reset(new ScanAbortedMessage()); break;
941 case TriggerScanMessage::kCommand:
942 message.reset(new TriggerScanMessage()); break;
943 case UnprotDeauthenticateMessage::kCommand:
944 message.reset(new UnprotDeauthenticateMessage()); break;
945 case UnprotDisassociateMessage::kCommand:
946 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -0700947
repo sync0efa9f02012-12-28 13:40:20 -0800948 default:
949 message.reset(new UnknownMessage(gnlh->cmd)); break;
950 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700951
repo sync0efa9f02012-12-28 13:40:20 -0800952 if (!message->InitFromNlmsg(msg)) {
953 LOG(ERROR) << "Message did not initialize properly";
954 return NULL;
955 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700956 }
957
Wade Guthrie0d438532012-05-18 14:18:50 -0700958 return message.release();
959}
960
repo syncdc085c82012-12-28 08:54:41 -0800961Nl80211MessageDataCollector *
962 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700963 return g_datacollector.Pointer();
964}
965
repo syncdc085c82012-12-28 08:54:41 -0800966Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700967 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
968 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
969 need_to_print[NL80211_CMD_DEL_STATION] = true;
970 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
971 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
972 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
973 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
974 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
975 need_to_print[NL80211_CMD_REG_CHANGE] = true;
976 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
977 need_to_print[NL80211_CMD_ROAM] = true;
978 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
979 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
980 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
981}
982
repo syncdc085c82012-12-28 08:54:41 -0800983void Nl80211MessageDataCollector::CollectDebugData(
984 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700985 if (!msg) {
986 LOG(ERROR) << "NULL |msg| parameter";
987 return;
988 }
989
990 bool doit = false;
991
992 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -0800993 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -0700994 if (node != need_to_print.end())
995 doit = node->second;
996
997 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -0700998 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -0800999 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001000 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001001
Christopher Wileyefd521f2012-11-07 17:32:46 -08001002 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001003
1004 size_t bytes = nlmsg_total_size(payload_bytes);
1005 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001006 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001007 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001008 << std::hex << std::setfill('0') << std::setw(2)
1009 << + rawdata[i] << ",";
1010 }
Wade Guthried6153612012-08-23 11:36:14 -07001011 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001012 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001013 }
1014}
1015
1016} // namespace shill.