blob: 077f6665d3b31fa35d028e88d8628325cfd7bd73 [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
Wade Guthrie0d438532012-05-18 14:18:50 -070027#include <netlink/msg.h>
28#include <netlink/netlink.h>
29
Wade Guthrie92d06362013-04-25 15:41:30 -070030#include <limits>
Wade Guthrie89e6cb32013-03-07 08:03:45 -080031#include <map>
Wade Guthrie0d438532012-05-18 14:18:50 -070032#include <string>
Wade Guthrie89e6cb32013-03-07 08:03:45 -080033#include <vector>
Wade Guthrie0d438532012-05-18 14:18:50 -070034
Wade Guthrie68da97c2013-02-26 13:09:35 -080035#include <base/bind.h>
Ben Chana0ddf462014-02-06 11:32:42 -080036#include <base/strings/stringprintf.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070037
repo sync90ee0fa2012-12-18 10:08:08 -080038#include "shill/attribute_list.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070039#include "shill/ieee80211.h"
40#include "shill/logging.h"
Wade Guthrief162f8b2013-02-27 14:13:55 -080041#include "shill/netlink_attribute.h"
Wade Guthrie2193b232013-06-05 17:09:07 -070042#include "shill/nl80211_attribute.h" // For Nl80211AttributeMac
Wade Guthrie89e6cb32013-03-07 08:03:45 -080043#include "shill/refptr_types.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070044
Wade Guthrie68da97c2013-02-26 13:09:35 -080045using base::Bind;
Wade Guthrie0d438532012-05-18 14:18:50 -070046using base::LazyInstance;
47using base::StringAppendF;
Wade Guthrie0d438532012-05-18 14:18:50 -070048using std::map;
49using std::string;
50using std::vector;
51
52namespace shill {
53
54namespace {
repo syncdc085c82012-12-28 08:54:41 -080055LazyInstance<Nl80211MessageDataCollector> g_datacollector =
Wade Guthrie0d438532012-05-18 14:18:50 -070056 LAZY_INSTANCE_INITIALIZER;
57} // namespace
58
Wade Guthrie0d438532012-05-18 14:18:50 -070059const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
60const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
61
Wade Guthriebee87c22013-03-06 11:00:46 -080062const char Nl80211Message::kMessageTypeString[] = "nl80211";
repo syncdc085c82012-12-28 08:54:41 -080063map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
64map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
Wade Guthriebdcdaa72013-03-04 12:47:12 -080065uint16_t Nl80211Message::nl80211_message_type_ = kIllegalMessageType;
66
Wade Guthriebee87c22013-03-06 11:00:46 -080067// static
Paul Stewart2ddf2c62013-04-16 09:47:34 -070068uint16_t Nl80211Message::GetMessageType() {
69 return nl80211_message_type_;
70}
71
72// static
Wade Guthriebee87c22013-03-06 11:00:46 -080073void Nl80211Message::SetMessageType(uint16_t message_type) {
74 if (message_type == NetlinkMessage::kIllegalMessageType) {
75 LOG(FATAL) << "Absolutely need a legal message type for Nl80211 messages.";
76 }
77 nl80211_message_type_ = message_type;
78}
79
repo sync0efa9f02012-12-28 13:40:20 -080080bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) {
81 if (!const_msg) {
82 LOG(ERROR) << "Null |msg| parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -070083 return false;
84 }
Wade Guthriebdcdaa72013-03-04 12:47:12 -080085 ByteString message(reinterpret_cast<const unsigned char *>(const_msg),
86 const_msg->nlmsg_len);
Wade Guthrie0d438532012-05-18 14:18:50 -070087
Wade Guthriebdcdaa72013-03-04 12:47:12 -080088 if (!InitAndStripHeader(&message)) {
89 return false;
90 }
repo sync0efa9f02012-12-28 13:40:20 -080091
92 // Attributes.
93 // Parse the attributes from the nl message payload into the 'tb' array.
94 nlattr *tb[NL80211_ATTR_MAX + 1];
Wade Guthriebdcdaa72013-03-04 12:47:12 -080095 nla_parse(tb, NL80211_ATTR_MAX,
96 reinterpret_cast<nlattr *>(message.GetData()), message.GetLength(),
97 NULL);
repo sync0efa9f02012-12-28 13:40:20 -080098
Wade Guthrie0d438532012-05-18 14:18:50 -070099 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
100 if (tb[i]) {
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800101 attributes_->CreateAndInitAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800102 i, tb[i], Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
Wade Guthrie0d438532012-05-18 14:18:50 -0700103 }
104 }
105
106 // Convert integer values provided by libnl (for example, from the
107 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
108 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700109 if (!reason_code_string_) {
110 reason_code_string_ = new map<uint16_t, string>;
111 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
112 "Unspecified reason";
113 (*reason_code_string_)[
114 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700115 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700116 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
117 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
118 "ESS";
119 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
120 "Disassociated due to inactivity";
121 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
122 "Disassociated because AP is unable to handle all currently associated "
123 "STAs";
124 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
125 "Class 2 frame received from nonauthenticated STA";
126 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
127 "Class 3 frame received from nonassociated STA";
128 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
129 "Disassociated because sending STA is leaving (or has left) BSS";
130 (*reason_code_string_)[
131 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
132 "STA requesting (re)association is not authenticated with responding "
133 "STA";
134 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
135 "Disassociated because the information in the Power Capability "
136 "element is unacceptable";
137 (*reason_code_string_)[
138 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
139 "Disassociated because the information in the Supported Channels "
140 "element is unacceptable";
141 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
142 "Invalid information element, i.e., an information element defined in "
143 "this standard for which the content does not meet the specifications "
144 "in Clause 7";
145 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
146 "Message integrity code (MIC) failure";
147 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
148 "4-Way Handshake timeout";
149 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
150 "Group Key Handshake timeout";
151 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
152 "Information element in 4-Way Handshake different from "
153 "(Re)Association Request/Probe Response/Beacon frame";
154 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
155 "Invalid group cipher";
156 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
157 "Invalid pairwise cipher";
158 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
159 "Invalid AKMP";
160 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
161 "Unsupported RSN information element version";
162 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
163 "Invalid RSN information element capabilities";
164 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
165 "IEEE 802.1X authentication failed";
166 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
167 "Cipher suite rejected because of the security policy";
168 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
169 "Disassociated for unspecified, QoS-related reason";
170 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
171 "Disassociated because QoS AP lacks sufficient bandwidth for this "
172 "QoS STA";
173 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
174 "Disassociated because excessive number of frames need to be "
175 "acknowledged, but are not acknowledged due to AP transmissions "
176 "and/or poor channel conditions";
177 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
178 "Disassociated because STA is transmitting outside the limits of its "
179 "TXOPs";
180 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
181 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
182 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
183 "Requested from peer STA as it does not want to use the mechanism";
184 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
185 "Requested from peer STA as the STA received frames using the "
186 "mechanism for which a setup is required";
187 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
188 "Requested from peer STA due to timeout";
189 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
190 "Peer STA does not support the requested cipher suite";
191 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
192 }
193
194 if (!status_code_string_) {
195 status_code_string_ = new map<uint16_t, string>;
196 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
197 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
198 "Unspecified failure";
199 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700200 "Cannot support all requested capabilities in the capability "
201 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700202 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700203 "Reassociation denied due to inability to confirm that association "
204 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700205 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700206 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700207 (*status_code_string_)[
208 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700209 "Responding station does not support the specified authentication "
210 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700211 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700212 "Received an authentication frame with authentication transaction "
213 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700214 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700215 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700216 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700217 "Authentication rejected due to timeout waiting for next frame in "
218 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700219 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
220 "Association denied because AP is unable to handle additional "
221 "associated STA";
222 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700223 "Association denied due to requesting station not supporting all of "
224 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700225 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700226 "Association denied due to requesting station not supporting the "
227 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700228 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700229 "Association denied due to requesting station not supporting the PBCC "
230 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700231 (*status_code_string_)[
232 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700233 "Association denied due to requesting station not supporting the "
234 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700235 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700236 "Association request rejected because Spectrum Management capability "
237 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700238 (*status_code_string_)[
239 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700240 "Association request rejected because the information in the Power "
241 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700242 (*status_code_string_)[
243 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700244 "Association request rejected because the information in the "
245 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700246 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700247 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700248 "supporting the Short Slot Time option";
249 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
250 "Association request rejected due to requesting station not "
251 "supporting the DSSS-OFDM option";
252 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700253 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700254 (*status_code_string_)[
255 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700256 "Association denied due to QAP having insufficient bandwidth to handle "
257 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700258 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700259 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700260 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
261 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700262 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700263 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
264 "The request has been declined";
265 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700266 "The request has not been successful as one or more parameters have "
267 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700268 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
269 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700270 "However, a suggested Tspec is provided so that the initiating QSTA "
271 "may attempt to send another TS with the suggested changes to the "
272 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700273 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700274 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700275 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
276 "Invalid Group Cipher";
277 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
278 "Invalid Pairwise Cipher";
279 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
280 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
281 "Unsupported RSN Information Element version";
282 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
283 "Invalid RSN Information Element Capabilities";
284 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700285 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700286 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
287 "The TS has not been created. However, the HC may be capable of "
288 "creating a TS, in response to a request, after the time indicated in "
289 "the TS Delay element";
290 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700291 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700292 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
293 "Destination STA is not present within this BSS";
294 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
295 "The destination STA is not a QoS STA";
296 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700297 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700298 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700299 }
300
301 return true;
302}
303
Wade Guthrie0d438532012-05-18 14:18:50 -0700304// static
repo syncdc085c82012-12-28 08:54:41 -0800305string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700306 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700307 match = reason_code_string_->find(status);
308 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700309 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700310 if (status < IEEE_80211::kReasonCodeMax) {
311 StringAppendF(&output, "<Reserved Reason:%u>", status);
312 } else {
313 StringAppendF(&output, "<Unknown Reason:%u>", status);
314 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700315 return output;
316 }
317 return match->second;
318}
319
Wade Guthried4977f22012-08-22 12:37:54 -0700320// static
repo syncdc085c82012-12-28 08:54:41 -0800321string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700322 map<uint16_t, string>::const_iterator match;
323 match = status_code_string_->find(status);
324 if (match == status_code_string_->end()) {
325 string output;
326 if (status < IEEE_80211::kStatusCodeMax) {
327 StringAppendF(&output, "<Reserved Status:%u>", status);
328 } else {
329 StringAppendF(&output, "<Unknown Status:%u>", status);
330 }
331 return output;
332 }
333 return match->second;
334}
335
Wade Guthrie71872472013-03-05 10:33:38 -0800336// Nl80211Frame
337
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800338Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthrie92d06362013-04-25 15:41:30 -0700339 : frame_type_(kIllegalFrameType),
340 reason_(std::numeric_limits<uint16_t>::max()),
341 status_(std::numeric_limits<uint16_t>::max()),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800342 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700343 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800344 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
345 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700346
347 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800348 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700349 mac_from_ =
Wade Guthrie2193b232013-06-05 17:09:07 -0700350 Nl80211AttributeMac::StringFromMacAddress(&frame->destination_mac[0]);
351 mac_to_ = Nl80211AttributeMac::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700352 frame_type_ = frame->frame_control & kFrameTypeMask;
353
354 switch (frame_type_) {
355 case kAssocResponseFrameType:
356 case kReassocResponseFrameType:
357 status_ = le16toh(frame->u.associate_response.status_code);
358 break;
359
360 case kAuthFrameType:
361 status_ = le16toh(frame->u.authentiate_message.status_code);
362 break;
363
364 case kDisassocFrameType:
365 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700366 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700367 break;
368
369 default:
370 break;
371 }
372 }
373}
374
Wade Guthried4977f22012-08-22 12:37:54 -0700375bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700376 if (!output) {
377 LOG(ERROR) << "NULL |output|";
378 return false;
379 }
380
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800381 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700382 output->append(" [no frame]");
383 return true;
384 }
385
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800386 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700387 output->append(" [invalid frame: ");
388 } else {
389 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
390
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800391 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700392 case kAssocResponseFrameType:
393 StringAppendF(output, "; AssocResponse status: %u: %s",
394 status_,
repo syncdc085c82012-12-28 08:54:41 -0800395 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700396 break;
397 case kReassocResponseFrameType:
398 StringAppendF(output, "; ReassocResponse status: %u: %s",
399 status_,
repo syncdc085c82012-12-28 08:54:41 -0800400 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700401 break;
402 case kAuthFrameType:
403 StringAppendF(output, "; Auth status: %u: %s",
404 status_,
repo syncdc085c82012-12-28 08:54:41 -0800405 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700406 break;
407
408 case kDisassocFrameType:
409 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700410 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800411 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700412 break;
413 case kDeauthFrameType:
414 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700415 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800416 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700417 break;
418
419 default:
420 break;
421 }
422 output->append(" [frame: ");
423 }
424
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800425 const unsigned char *frame = frame_.GetConstData();
426 for (size_t i = 0; i < frame_.GetLength(); ++i) {
427 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700428 }
429 output->append("]");
430
431 return true;
432}
433
Wade Guthried4977f22012-08-22 12:37:54 -0700434bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800435 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700436}
437
438//
repo syncdc085c82012-12-28 08:54:41 -0800439// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700440//
441
442const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
443const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
444
Wade Guthrie0d438532012-05-18 14:18:50 -0700445const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
446const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
447
Wade Guthrie0d438532012-05-18 14:18:50 -0700448const uint8_t CancelRemainOnChannelMessage::kCommand =
449 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
450const char CancelRemainOnChannelMessage::kCommandString[] =
451 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
452
Wade Guthrie0d438532012-05-18 14:18:50 -0700453const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
454const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
455
Wade Guthrie0d438532012-05-18 14:18:50 -0700456const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
457const char DeauthenticateMessage::kCommandString[] =
458 "NL80211_CMD_DEAUTHENTICATE";
459
Wade Guthrie0d438532012-05-18 14:18:50 -0700460const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
461const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
462
Wade Guthrie0d438532012-05-18 14:18:50 -0700463const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
464const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
465
Wade Guthrie0d438532012-05-18 14:18:50 -0700466const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
467const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
468
Wade Guthrie0d438532012-05-18 14:18:50 -0700469const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
470const char FrameTxStatusMessage::kCommandString[] =
471 "NL80211_CMD_FRAME_TX_STATUS";
472
repo sync0efa9f02012-12-28 13:40:20 -0800473const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
474const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
475
Paul Stewart7cd45722013-08-12 14:50:14 -0700476const uint8_t GetStationMessage::kCommand = NL80211_CMD_GET_STATION;
477const char GetStationMessage::kCommandString[] = "NL80211_CMD_GET_STATION";
478
479GetStationMessage::GetStationMessage()
480 : Nl80211Message(kCommand, kCommandString) {
481 attributes()->CreateAttribute(
482 NL80211_ATTR_IFINDEX, Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
483 attributes()->CreateAttribute(
484 NL80211_ATTR_MAC, Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
485}
486
Samuel Tan9aa2fd32014-08-07 14:21:29 -0700487const uint8_t SetWakeOnPacketConnMessage::kCommand = NL80211_CMD_SET_WOWLAN;
488const char SetWakeOnPacketConnMessage::kCommandString[] =
489 "NL80211_CMD_SET_WOWLAN";
490
Wade Guthrie1f355e82013-04-11 15:46:12 -0700491const uint8_t GetWiphyMessage::kCommand = NL80211_CMD_GET_WIPHY;
492const char GetWiphyMessage::kCommandString[] = "NL80211_CMD_GET_WIPHY";
493
Wade Guthrie92d06362013-04-25 15:41:30 -0700494GetWiphyMessage::GetWiphyMessage() : Nl80211Message(kCommand, kCommandString) {
495 attributes()->CreateAttribute(
496 NL80211_ATTR_IFINDEX, Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
497}
498
Wade Guthrie0d438532012-05-18 14:18:50 -0700499const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
500const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
501
Wade Guthrie0d438532012-05-18 14:18:50 -0700502const uint8_t MichaelMicFailureMessage::kCommand =
503 NL80211_CMD_MICHAEL_MIC_FAILURE;
504const char MichaelMicFailureMessage::kCommandString[] =
505 "NL80211_CMD_MICHAEL_MIC_FAILURE";
506
Wade Guthrie0d438532012-05-18 14:18:50 -0700507const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
508const char NewScanResultsMessage::kCommandString[] =
509 "NL80211_CMD_NEW_SCAN_RESULTS";
510
Wade Guthrie0d438532012-05-18 14:18:50 -0700511const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
512const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
513
Wade Guthrie1f355e82013-04-11 15:46:12 -0700514const uint8_t NewWiphyMessage::kCommand = NL80211_CMD_NEW_WIPHY;
515const char NewWiphyMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
Wade Guthrie0d438532012-05-18 14:18:50 -0700516
Wade Guthrie0d438532012-05-18 14:18:50 -0700517const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
518const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
519
Wade Guthrie0d438532012-05-18 14:18:50 -0700520const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
521const char PmksaCandidateMessage::kCommandString[] =
522 "NL80211_ATTR_PMKSA_CANDIDATE";
523
Wade Guthrie0d438532012-05-18 14:18:50 -0700524const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
525const char RegBeaconHintMessage::kCommandString[] =
526 "NL80211_CMD_REG_BEACON_HINT";
527
Wade Guthrie0d438532012-05-18 14:18:50 -0700528const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
529const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
530
Wade Guthrie0d438532012-05-18 14:18:50 -0700531const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
532const char RemainOnChannelMessage::kCommandString[] =
533 "NL80211_CMD_REMAIN_ON_CHANNEL";
534
Wade Guthrie0d438532012-05-18 14:18:50 -0700535const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
536const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
537
Wade Guthrie0d438532012-05-18 14:18:50 -0700538const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
539const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
540
Wade Guthrie71872472013-03-05 10:33:38 -0800541const uint8_t GetScanMessage::kCommand = NL80211_CMD_GET_SCAN;
542const char GetScanMessage::kCommandString[] = "NL80211_CMD_GET_SCAN";
543
Wade Guthrie0d438532012-05-18 14:18:50 -0700544const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
545const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
546
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700547TriggerScanMessage::TriggerScanMessage()
Wade Guthrie2623f1a2013-05-14 15:14:54 -0700548 : Nl80211Message(kCommand, kCommandString) {
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700549 attributes()->CreateAttribute(
550 NL80211_ATTR_IFINDEX, Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
551 attributes()->CreateAttribute(
552 NL80211_ATTR_SCAN_FREQUENCIES,
553 Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
554 attributes()->CreateAttribute(
555 NL80211_ATTR_SCAN_SSIDS,
556 Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
557}
558
Wade Guthrie0d438532012-05-18 14:18:50 -0700559const uint8_t UnprotDeauthenticateMessage::kCommand =
560 NL80211_CMD_UNPROT_DEAUTHENTICATE;
561const char UnprotDeauthenticateMessage::kCommandString[] =
562 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
563
Wade Guthrie0d438532012-05-18 14:18:50 -0700564const uint8_t UnprotDisassociateMessage::kCommand =
565 NL80211_CMD_UNPROT_DISASSOCIATE;
566const char UnprotDisassociateMessage::kCommandString[] =
567 "NL80211_CMD_UNPROT_DISASSOCIATE";
568
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700569GetInterfaceMessage::GetInterfaceMessage()
570 : Nl80211Message(kCommand, kCommandString) {
571 attributes()->CreateAttribute(
572 NL80211_ATTR_IFINDEX, Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
573}
574
575const uint8_t GetInterfaceMessage::kCommand = NL80211_CMD_GET_INTERFACE;
576const char GetInterfaceMessage::kCommandString[] = "NL80211_CMD_GET_INTERFACE";
577
578const uint8_t NewInterfaceMessage::kCommand = NL80211_CMD_NEW_INTERFACE;
579const char NewInterfaceMessage::kCommandString[] = "NL80211_CMD_NEW_INTERFACE";
580
Wade Guthrie12f113a2013-03-12 17:15:46 -0700581// static
582NetlinkMessage *Nl80211Message::CreateMessage(const nlmsghdr *const_msg) {
583 if (!const_msg) {
584 LOG(ERROR) << "NULL |const_msg| parameter";
585 return NULL;
586 }
587 // Casting away constness since, while nlmsg_data doesn't change its
588 // parameter, it also doesn't declare its paramenter as const.
589 nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg);
590 void *payload = nlmsg_data(msg);
591 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
592 scoped_ptr<NetlinkMessage> message;
593
594 switch (gnlh->cmd) {
595 case AssociateMessage::kCommand:
596 return new AssociateMessage();
597 case AuthenticateMessage::kCommand:
598 return new AuthenticateMessage();
599 case CancelRemainOnChannelMessage::kCommand:
600 return new CancelRemainOnChannelMessage();
601 case ConnectMessage::kCommand:
602 return new ConnectMessage();
603 case DeauthenticateMessage::kCommand:
604 return new DeauthenticateMessage();
605 case DeleteStationMessage::kCommand:
606 return new DeleteStationMessage();
607 case DisassociateMessage::kCommand:
608 return new DisassociateMessage();
609 case DisconnectMessage::kCommand:
610 return new DisconnectMessage();
611 case FrameTxStatusMessage::kCommand:
612 return new FrameTxStatusMessage();
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700613 case GetInterfaceMessage::kCommand:
614 return new GetInterfaceMessage();
Wade Guthrie12f113a2013-03-12 17:15:46 -0700615 case GetRegMessage::kCommand:
616 return new GetRegMessage();
Paul Stewart7cd45722013-08-12 14:50:14 -0700617 case GetStationMessage::kCommand:
618 return new GetStationMessage();
Wade Guthrie1f355e82013-04-11 15:46:12 -0700619 case GetWiphyMessage::kCommand:
620 return new GetWiphyMessage();
Wade Guthrie12f113a2013-03-12 17:15:46 -0700621 case JoinIbssMessage::kCommand:
622 return new JoinIbssMessage();
623 case MichaelMicFailureMessage::kCommand:
624 return new MichaelMicFailureMessage();
Paul Stewart2ddf2c62013-04-16 09:47:34 -0700625 case NewInterfaceMessage::kCommand:
626 return new NewInterfaceMessage();
Wade Guthrie12f113a2013-03-12 17:15:46 -0700627 case NewScanResultsMessage::kCommand:
628 return new NewScanResultsMessage();
629 case NewStationMessage::kCommand:
630 return new NewStationMessage();
Wade Guthrie1f355e82013-04-11 15:46:12 -0700631 case NewWiphyMessage::kCommand:
632 return new NewWiphyMessage();
Wade Guthrie12f113a2013-03-12 17:15:46 -0700633 case NotifyCqmMessage::kCommand:
634 return new NotifyCqmMessage();
635 case PmksaCandidateMessage::kCommand:
636 return new PmksaCandidateMessage();
637 case RegBeaconHintMessage::kCommand:
638 return new RegBeaconHintMessage();
639 case RegChangeMessage::kCommand:
640 return new RegChangeMessage();
641 case RemainOnChannelMessage::kCommand:
642 return new RemainOnChannelMessage();
643 case RoamMessage::kCommand:
644 return new RoamMessage();
645 case ScanAbortedMessage::kCommand:
646 return new ScanAbortedMessage();
647 case TriggerScanMessage::kCommand:
648 return new TriggerScanMessage();
649 case UnprotDeauthenticateMessage::kCommand:
650 return new UnprotDeauthenticateMessage();
651 case UnprotDisassociateMessage::kCommand:
652 return new UnprotDisassociateMessage();
653 default:
Ben Chana0ddf462014-02-06 11:32:42 -0800654 LOG(WARNING) << base::StringPrintf(
Wade Guthrie40d992c2013-04-19 11:10:11 -0700655 "Unknown/unhandled netlink nl80211 message 0x%02x", gnlh->cmd);
656 return new UnknownNl80211Message(gnlh->cmd);
Wade Guthrie12f113a2013-03-12 17:15:46 -0700657 break;
658 }
659 return NULL;
660}
661
Wade Guthrie0d438532012-05-18 14:18:50 -0700662//
Wade Guthrie12f113a2013-03-12 17:15:46 -0700663// Data Collector
664//
665
repo syncdc085c82012-12-28 08:54:41 -0800666Nl80211MessageDataCollector *
667 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700668 return g_datacollector.Pointer();
669}
670
repo syncdc085c82012-12-28 08:54:41 -0800671Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700672 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
673 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
674 need_to_print[NL80211_CMD_DEL_STATION] = true;
675 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
676 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
677 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
678 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
679 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
680 need_to_print[NL80211_CMD_REG_CHANGE] = true;
681 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
682 need_to_print[NL80211_CMD_ROAM] = true;
683 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
684 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
685 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
686}
687
repo syncdc085c82012-12-28 08:54:41 -0800688void Nl80211MessageDataCollector::CollectDebugData(
689 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700690 if (!msg) {
691 LOG(ERROR) << "NULL |msg| parameter";
692 return;
693 }
694
695 bool doit = false;
696
697 map<uint8_t, bool>::const_iterator node;
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800698 node = need_to_print.find(message.command());
Wade Guthrie0d438532012-05-18 14:18:50 -0700699 if (node != need_to_print.end())
700 doit = node->second;
701
702 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -0700703 LOG(INFO) << "@@const unsigned char "
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800704 << "k" << message.command_string()
Wade Guthried6153612012-08-23 11:36:14 -0700705 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -0700706
Christopher Wileyefd521f2012-11-07 17:32:46 -0800707 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -0700708
709 size_t bytes = nlmsg_total_size(payload_bytes);
710 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -0700711 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -0700712 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -0700713 << std::hex << std::setfill('0') << std::setw(2)
714 << + rawdata[i] << ",";
715 }
Wade Guthried6153612012-08-23 11:36:14 -0700716 LOG(INFO) << "};";
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800717 need_to_print[message.command()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700718 }
719}
720
721} // namespace shill.