blob: 60ea97789a7d81d799e66d8b6c7ca92276045260 [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 Guthriebdcdaa72013-03-04 12:47:12 -080030#include <limits.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070031#include <linux/nl80211.h>
32#include <net/if.h>
Wade Guthrie8343f7f2012-12-04 13:52:32 -080033#include <netinet/in.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070034#include <netlink/attr.h>
35#include <netlink/genl/ctrl.h>
36#include <netlink/genl/family.h>
37#include <netlink/genl/genl.h>
38#include <netlink/msg.h>
39#include <netlink/netlink.h>
40
Wade Guthrie8e278612013-02-26 10:32:34 -080041#include <algorithm>
Wade Guthrie0d438532012-05-18 14:18:50 -070042#include <iomanip>
43#include <string>
44
Wade Guthrie68da97c2013-02-26 13:09:35 -080045#include <base/bind.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070046#include <base/format_macros.h>
47#include <base/stl_util.h>
48#include <base/stringprintf.h>
49
repo sync90ee0fa2012-12-18 10:08:08 -080050#include "shill/attribute_list.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070051#include "shill/ieee80211.h"
52#include "shill/logging.h"
Wade Guthrief162f8b2013-02-27 14:13:55 -080053#include "shill/netlink_attribute.h"
repo syncdc085c82012-12-28 08:54:41 -080054#include "shill/netlink_socket.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070055#include "shill/scope_logger.h"
56
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 Guthrie0d438532012-05-18 14:18:50 -070073const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
74const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
75
Wade Guthriebdcdaa72013-03-04 12:47:12 -080076// TODO(wdg): These will go into NetlinkMessage when that class exists.
77const uint32_t Nl80211Message::kBroadcastSequenceNumber = 0;
78const uint16_t Nl80211Message::kIllegalMessageType = UINT16_MAX;
79
80const char Nl80211Message::kBogusMacAddress[] = "XX:XX:XX:XX:XX:XX";
repo syncdc085c82012-12-28 08:54:41 -080081const unsigned int Nl80211Message::kEthernetAddressBytes = 6;
82map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
83map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
Wade Guthriebdcdaa72013-03-04 12:47:12 -080084uint16_t Nl80211Message::nl80211_message_type_ = kIllegalMessageType;
85
Wade Guthrie0d438532012-05-18 14:18:50 -070086
87// The nl messages look like this:
88//
89// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
90// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
91// +-- gnhl nlmsg_tail(hdr) --+
92// | nlmsg_next(hdr) --+
93// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
94// -----+-----+-+----------------------------------------------+-++----
95// ... | | | payload | ||
96// | | +------+-+--------+-+--------------------------+ ||
97// | nl | | | | | | attribs | ||
98// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
99// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
100// | |d| hdr |d| |d| attr |a| load |a| ... |d||
101// | | | | | | | |d| |d| | ||
102// -----+-----+-+----------------------------------------------+-++----
103// ^ ^ ^ ^
104// | | | XXXXXXX <-- nla_len(nlattr)
105// | | | +-- nla_data(nlattr)
106// | | X-nla_total_size-X
107// | | XXXXX-nlmsg_attrlen-XXXXXX
108// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
109// +-- msg = nlmsg_hdr(raw_message)
110
111//
repo syncdc085c82012-12-28 08:54:41 -0800112// Nl80211Message
Wade Guthrie0d438532012-05-18 14:18:50 -0700113//
114
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800115bool Nl80211Message::InitAndStripHeader(ByteString *input) {
116 if (!input) {
117 LOG(ERROR) << "NULL input";
118 return false;
119 }
120 // Read the nlmsghdr.
121 nlmsghdr *header = reinterpret_cast<nlmsghdr *>(input->GetData());
122 message_type_ = header->nlmsg_type;
123 flags_ = header->nlmsg_flags;
124 sequence_number_ = header->nlmsg_seq;
125
126 // Strip the nlmsghdr.
127 input->RemovePrefix(NLMSG_ALIGN(sizeof(struct nlmsghdr)));
128
129 // Read the genlmsghdr.
130 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(input->GetData());
131 if (command_ != gnlh->cmd) {
132 LOG(WARNING) << "This object thinks it's a " << command_
133 << " but the message thinks it's a " << gnlh->cmd;
134 }
135
136 // Strip the genlmsghdr.
137 input->RemovePrefix(NLMSG_ALIGN(sizeof(struct genlmsghdr)));
138 return true;
Wade Guthrie8e278612013-02-26 10:32:34 -0800139}
140
repo sync0efa9f02012-12-28 13:40:20 -0800141bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) {
142 if (!const_msg) {
143 LOG(ERROR) << "Null |msg| parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700144 return false;
145 }
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800146 ByteString message(reinterpret_cast<const unsigned char *>(const_msg),
147 const_msg->nlmsg_len);
Wade Guthrie0d438532012-05-18 14:18:50 -0700148
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800149 if (!InitAndStripHeader(&message)) {
150 return false;
151 }
repo sync0efa9f02012-12-28 13:40:20 -0800152
153 // Attributes.
154 // Parse the attributes from the nl message payload into the 'tb' array.
155 nlattr *tb[NL80211_ATTR_MAX + 1];
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800156 nla_parse(tb, NL80211_ATTR_MAX,
157 reinterpret_cast<nlattr *>(message.GetData()), message.GetLength(),
158 NULL);
repo sync0efa9f02012-12-28 13:40:20 -0800159
Wade Guthrie0d438532012-05-18 14:18:50 -0700160 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
161 if (tb[i]) {
repo sync0efa9f02012-12-28 13:40:20 -0800162 // TODO(wdg): When Nl80211Messages instantiate their own attributes,
163 // this call should, instead, call |SetAttributeFromNlAttr|.
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800164 attributes_->CreateAndInitAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800165 i, tb[i], Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
Wade Guthrie0d438532012-05-18 14:18:50 -0700166 }
167 }
168
169 // Convert integer values provided by libnl (for example, from the
170 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
171 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700172 if (!reason_code_string_) {
173 reason_code_string_ = new map<uint16_t, string>;
174 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
175 "Unspecified reason";
176 (*reason_code_string_)[
177 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700178 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700179 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
180 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
181 "ESS";
182 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
183 "Disassociated due to inactivity";
184 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
185 "Disassociated because AP is unable to handle all currently associated "
186 "STAs";
187 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
188 "Class 2 frame received from nonauthenticated STA";
189 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
190 "Class 3 frame received from nonassociated STA";
191 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
192 "Disassociated because sending STA is leaving (or has left) BSS";
193 (*reason_code_string_)[
194 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
195 "STA requesting (re)association is not authenticated with responding "
196 "STA";
197 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
198 "Disassociated because the information in the Power Capability "
199 "element is unacceptable";
200 (*reason_code_string_)[
201 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
202 "Disassociated because the information in the Supported Channels "
203 "element is unacceptable";
204 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
205 "Invalid information element, i.e., an information element defined in "
206 "this standard for which the content does not meet the specifications "
207 "in Clause 7";
208 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
209 "Message integrity code (MIC) failure";
210 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
211 "4-Way Handshake timeout";
212 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
213 "Group Key Handshake timeout";
214 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
215 "Information element in 4-Way Handshake different from "
216 "(Re)Association Request/Probe Response/Beacon frame";
217 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
218 "Invalid group cipher";
219 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
220 "Invalid pairwise cipher";
221 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
222 "Invalid AKMP";
223 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
224 "Unsupported RSN information element version";
225 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
226 "Invalid RSN information element capabilities";
227 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
228 "IEEE 802.1X authentication failed";
229 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
230 "Cipher suite rejected because of the security policy";
231 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
232 "Disassociated for unspecified, QoS-related reason";
233 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
234 "Disassociated because QoS AP lacks sufficient bandwidth for this "
235 "QoS STA";
236 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
237 "Disassociated because excessive number of frames need to be "
238 "acknowledged, but are not acknowledged due to AP transmissions "
239 "and/or poor channel conditions";
240 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
241 "Disassociated because STA is transmitting outside the limits of its "
242 "TXOPs";
243 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
244 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
245 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
246 "Requested from peer STA as it does not want to use the mechanism";
247 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
248 "Requested from peer STA as the STA received frames using the "
249 "mechanism for which a setup is required";
250 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
251 "Requested from peer STA due to timeout";
252 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
253 "Peer STA does not support the requested cipher suite";
254 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
255 }
256
257 if (!status_code_string_) {
258 status_code_string_ = new map<uint16_t, string>;
259 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
260 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
261 "Unspecified failure";
262 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700263 "Cannot support all requested capabilities in the capability "
264 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700265 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700266 "Reassociation denied due to inability to confirm that association "
267 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700268 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700269 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700270 (*status_code_string_)[
271 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700272 "Responding station does not support the specified authentication "
273 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700274 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700275 "Received an authentication frame with authentication transaction "
276 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700277 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700278 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700279 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700280 "Authentication rejected due to timeout waiting for next frame in "
281 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700282 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
283 "Association denied because AP is unable to handle additional "
284 "associated STA";
285 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700286 "Association denied due to requesting station not supporting all of "
287 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700288 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700289 "Association denied due to requesting station not supporting the "
290 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700291 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700292 "Association denied due to requesting station not supporting the PBCC "
293 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700294 (*status_code_string_)[
295 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700296 "Association denied due to requesting station not supporting the "
297 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700298 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700299 "Association request rejected because Spectrum Management capability "
300 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700301 (*status_code_string_)[
302 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700303 "Association request rejected because the information in the Power "
304 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700305 (*status_code_string_)[
306 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700307 "Association request rejected because the information in the "
308 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700309 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700310 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700311 "supporting the Short Slot Time option";
312 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
313 "Association request rejected due to requesting station not "
314 "supporting the DSSS-OFDM option";
315 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700316 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700317 (*status_code_string_)[
318 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700319 "Association denied due to QAP having insufficient bandwidth to handle "
320 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700321 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700322 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700323 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
324 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700325 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700326 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
327 "The request has been declined";
328 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700329 "The request has not been successful as one or more parameters have "
330 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700331 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
332 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700333 "However, a suggested Tspec is provided so that the initiating QSTA "
334 "may attempt to send another TS with the suggested changes to the "
335 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700336 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700337 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700338 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
339 "Invalid Group Cipher";
340 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
341 "Invalid Pairwise Cipher";
342 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
343 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
344 "Unsupported RSN Information Element version";
345 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
346 "Invalid RSN Information Element Capabilities";
347 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700348 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700349 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
350 "The TS has not been created. However, the HC may be capable of "
351 "creating a TS, in response to a request, after the time indicated in "
352 "the TS Delay element";
353 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700354 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700355 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
356 "Destination STA is not present within this BSS";
357 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
358 "The destination STA is not a QoS STA";
359 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700360 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700361 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700362 }
363
364 return true;
365}
366
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800367void Nl80211Message::Print(int log_level) const {
368 SLOG(WiFi, log_level) << StringPrintf("Message %s (%d)",
369 command_string(),
370 command());
371 attributes_->Print(log_level, 1);
372}
373
Wade Guthrie8e278612013-02-26 10:32:34 -0800374// static
375void Nl80211Message::PrintBytes(int log_level, const unsigned char *buf,
376 size_t num_bytes) {
377 SLOG(WiFi, log_level) << "Netlink Message -- Examining Bytes";
378 if (!buf) {
379 SLOG(WiFi, log_level) << "<NULL Buffer>";
380 return;
381 }
382
383 if (num_bytes >= sizeof(nlmsghdr)) {
384 const nlmsghdr *header = reinterpret_cast<const nlmsghdr *>(buf);
385 SLOG(WiFi, log_level) << StringPrintf(
386 "len: %02x %02x %02x %02x = %u bytes",
387 buf[0], buf[1], buf[2], buf[3], header->nlmsg_len);
388
389 SLOG(WiFi, log_level) << StringPrintf(
390 "type | flags: %02x %02x %02x %02x - type:%u flags:%s%s%s%s%s",
391 buf[4], buf[5], buf[6], buf[7], header->nlmsg_type,
392 ((header->nlmsg_flags & NLM_F_REQUEST) ? " REQUEST" : ""),
393 ((header->nlmsg_flags & NLM_F_MULTI) ? " MULTI" : ""),
394 ((header->nlmsg_flags & NLM_F_ACK) ? " ACK" : ""),
395 ((header->nlmsg_flags & NLM_F_ECHO) ? " ECHO" : ""),
396 ((header->nlmsg_flags & NLM_F_DUMP_INTR) ? " BAD-SEQ" : ""));
397
398 SLOG(WiFi, log_level) << StringPrintf(
399 "sequence: %02x %02x %02x %02x = %u",
400 buf[8], buf[9], buf[10], buf[11], header->nlmsg_seq);
401 SLOG(WiFi, log_level) << StringPrintf(
402 "pid: %02x %02x %02x %02x = %u",
403 buf[12], buf[13], buf[14], buf[15], header->nlmsg_pid);
404 buf += sizeof(nlmsghdr);
405 num_bytes -= sizeof(nlmsghdr);
406 } else {
407 SLOG(WiFi, log_level) << "Not enough bytes (" << num_bytes
408 << ") for a complete nlmsghdr (requires "
409 << sizeof(nlmsghdr) << ").";
410 }
411
412 while (num_bytes) {
413 string output;
414 size_t bytes_this_row = min(num_bytes, static_cast<size_t>(32));
415 for (size_t i = 0; i < bytes_this_row; ++i) {
416 StringAppendF(&output, " %02x", *buf++);
417 }
418 SLOG(WiFi, log_level) << output;
419 num_bytes -= bytes_this_row;
420 }
421}
422
Wade Guthrie0d438532012-05-18 14:18:50 -0700423// Helper function to provide a string for a MAC address.
Wade Guthrie68da97c2013-02-26 13:09:35 -0800424bool Nl80211Message::GetMacAttributeString(int id, string *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700425 if (!value) {
426 LOG(ERROR) << "Null |value| parameter";
427 return false;
428 }
429
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800430 ByteString data;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800431 if (!const_attributes()->GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700432 value->assign(kBogusMacAddress);
433 return false;
434 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800435 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700436
437 return true;
438}
439
440// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
repo syncdc085c82012-12-28 08:54:41 -0800441bool Nl80211Message::GetScanFrequenciesAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800442 int id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700443 if (!value) {
444 LOG(ERROR) << "Null |value| parameter";
445 return false;
446 }
447
448 value->clear();
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800449
450 AttributeListConstRefPtr frequency_list;
451 if (!const_attributes()->ConstGetNestedAttributeList(
452 NL80211_ATTR_SCAN_FREQUENCIES, &frequency_list) || !frequency_list) {
453 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_FREQUENCIES attribute";
repo syncd316eb72012-12-10 15:48:47 -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 uint32_t freq;
460 int i = 1;
461 while (frequency_list->GetU32AttributeValue(i, &freq)) {
462 value->push_back(freq);
463 ++i;
Wade Guthrie0d438532012-05-18 14:18:50 -0700464 }
repo syncd316eb72012-12-10 15:48:47 -0800465 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700466}
467
468// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800469bool Nl80211Message::GetScanSsidsAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800470 int id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700471 if (!value) {
472 LOG(ERROR) << "Null |value| parameter";
473 return false;
474 }
475
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800476 AttributeListConstRefPtr ssid_list;
477 if (!const_attributes()->ConstGetNestedAttributeList(
478 NL80211_ATTR_SCAN_SSIDS, &ssid_list) || !ssid_list) {
479 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_SSIDS attribute";
repo sync90ee0fa2012-12-18 10:08:08 -0800480 return false;
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800481 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700482
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800483 // Assume IDs for the nested attribute array are linear starting from 1.
484 // Currently, that is enforced in the input to the nested attribute.
485 string ssid;
486 int i = 1;
487 while (ssid_list->GetStringAttributeValue(i, &ssid)) {
488 value->push_back(ssid);
489 ++i;
Wade Guthrie0d438532012-05-18 14:18:50 -0700490 }
repo sync90ee0fa2012-12-18 10:08:08 -0800491 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700492}
493
Wade Guthrie0d438532012-05-18 14:18:50 -0700494// Protected members.
495
repo syncdc085c82012-12-28 08:54:41 -0800496string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700497 char ifname[IF_NAMESIZE] = "";
498 uint32_t ifindex = UINT32_MAX;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800499 bool ifindex_exists = const_attributes()->GetU32AttributeValue(
500 NL80211_ATTR_IFINDEX, &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700501
502 uint32_t wifi = UINT32_MAX;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800503 bool wifi_exists = const_attributes()->GetU32AttributeValue(
504 NL80211_ATTR_WIPHY, &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700505
506 string output;
507 if (ifindex_exists && wifi_exists) {
508 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
509 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
510 wifi);
511 } else if (ifindex_exists) {
512 StringAppendF(&output, "%s: ",
513 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
514 } else if (wifi_exists) {
515 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
516 }
517
518 return output;
519}
520
Wade Guthrie68da97c2013-02-26 13:09:35 -0800521string Nl80211Message::StringFromFrame(int attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700522 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800523 ByteString frame_data;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800524 if (const_attributes()->GetRawAttributeValue(attr_name, &frame_data) &&
525 !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800526 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700527 frame.ToString(&output);
528 } else {
529 output.append(" [no frame]");
530 }
531
532 return output;
533}
534
535// static
repo syncdc085c82012-12-28 08:54:41 -0800536string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700537 switch (key_type) {
538 case NL80211_KEYTYPE_GROUP:
539 return "Group";
540 case NL80211_KEYTYPE_PAIRWISE:
541 return "Pairwise";
542 case NL80211_KEYTYPE_PEERKEY:
543 return "PeerKey";
544 default:
545 return "<Unknown Key Type>";
546 }
547}
548
549// static
repo syncdc085c82012-12-28 08:54:41 -0800550string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700551 string output;
552
553 if (!arg) {
554 output = kBogusMacAddress;
555 LOG(ERROR) << "|arg| parameter is NULL.";
556 } else {
557 StringAppendF(&output, "%02x", arg[0]);
558
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800559 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700560 StringAppendF(&output, ":%02x", arg[i]);
561 }
562 }
563 return output;
564}
565
566// static
repo syncdc085c82012-12-28 08:54:41 -0800567string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700568 switch (initiator) {
569 case NL80211_REGDOM_SET_BY_CORE:
570 return "the wireless core upon initialization";
571 case NL80211_REGDOM_SET_BY_USER:
572 return "a user";
573 case NL80211_REGDOM_SET_BY_DRIVER:
574 return "a driver";
575 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
576 return "a country IE";
577 default:
578 return "<Unknown Reg Initiator>";
579 }
580}
581
582// static
repo syncdc085c82012-12-28 08:54:41 -0800583string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700584 const uint8_t *data) {
585 string output;
586 if (!data) {
587 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
588 LOG(ERROR) << "|data| parameter is NULL.";
589 return output;
590 }
591
592 for (int i = 0; i < len; ++i) {
593 if (data[i] == ' ')
594 output.append(" ");
595 else if (isprint(data[i]))
596 StringAppendF(&output, "%c", static_cast<char>(data[i]));
597 else
598 StringAppendF(&output, "\\x%2x", data[i]);
599 }
600
601 return output;
602}
603
604// static
repo syncdc085c82012-12-28 08:54:41 -0800605string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700606 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700607 match = reason_code_string_->find(status);
608 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700609 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700610 if (status < IEEE_80211::kReasonCodeMax) {
611 StringAppendF(&output, "<Reserved Reason:%u>", status);
612 } else {
613 StringAppendF(&output, "<Unknown Reason:%u>", status);
614 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700615 return output;
616 }
617 return match->second;
618}
619
Wade Guthried4977f22012-08-22 12:37:54 -0700620// static
repo syncdc085c82012-12-28 08:54:41 -0800621string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700622 map<uint16_t, string>::const_iterator match;
623 match = status_code_string_->find(status);
624 if (match == status_code_string_->end()) {
625 string output;
626 if (status < IEEE_80211::kStatusCodeMax) {
627 StringAppendF(&output, "<Reserved Status:%u>", status);
628 } else {
629 StringAppendF(&output, "<Unknown Status:%u>", status);
630 }
631 return output;
632 }
633 return match->second;
634}
635
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800636ByteString Nl80211Message::EncodeHeader(uint32_t sequence_number,
637 uint16_t nlmsg_type) {
638 ByteString result;
639 sequence_number_ = sequence_number;
640 if (sequence_number_ == kBroadcastSequenceNumber) {
641 LOG(ERROR) << "Couldn't get a legal sequence number";
642 return result;
643 }
644
repo syncdc085c82012-12-28 08:54:41 -0800645 // Build netlink header.
646 nlmsghdr header;
647 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
648 header.nlmsg_len = nlmsghdr_with_pad;
649 header.nlmsg_type = nlmsg_type;
650 header.nlmsg_flags = NLM_F_REQUEST;
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800651 header.nlmsg_seq = sequence_number_;
repo syncdc085c82012-12-28 08:54:41 -0800652 header.nlmsg_pid = getpid();
653
repo syncdc085c82012-12-28 08:54:41 -0800654 // Netlink header + pad.
655 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
656 sizeof(header)));
657 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
658
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800659 // Build and append the genl message header.
660 genlmsghdr genl_header;
661 genl_header.cmd = command();
662 genl_header.version = 1;
663 genl_header.reserved = 0;
repo syncdc085c82012-12-28 08:54:41 -0800664
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800665 ByteString genl_header_string(
666 reinterpret_cast<unsigned char *>(&genl_header), sizeof(genl_header));
667 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
668 genl_header_string.Resize(genlmsghdr_with_pad); // Zero-fill.
669
670 nlmsghdr *pheader = reinterpret_cast<nlmsghdr *>(result.GetData());
671 pheader->nlmsg_len += genlmsghdr_with_pad;
672 result.Append(genl_header_string);
673 return result;
674}
675
676ByteString Nl80211Message::Encode(uint32_t sequence_number,
677 uint16_t nlmsg_type) {
678 ByteString result(EncodeHeader(sequence_number, nlmsg_type));
679 if (result.GetLength() == 0) {
680 LOG(ERROR) << "Couldn't encode message header.";
681 return result;
682 }
683
684 // Build and append attributes (padding is included by
685 // AttributeList::Encode).
686 ByteString attribute_bytes = attributes_->Encode();
687
688 // Need to re-calculate |header| since |Append|, above, moves the data.
689 nlmsghdr *pheader = reinterpret_cast<nlmsghdr *>(result.GetData());
690 pheader->nlmsg_len += attribute_bytes.GetLength();
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800691 result.Append(attribute_bytes);
repo syncdc085c82012-12-28 08:54:41 -0800692
693 return result;
694}
695
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800696Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700697 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800698 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700699 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800700 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
701 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700702
703 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800704 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700705 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800706 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
707 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700708 frame_type_ = frame->frame_control & kFrameTypeMask;
709
710 switch (frame_type_) {
711 case kAssocResponseFrameType:
712 case kReassocResponseFrameType:
713 status_ = le16toh(frame->u.associate_response.status_code);
714 break;
715
716 case kAuthFrameType:
717 status_ = le16toh(frame->u.authentiate_message.status_code);
718 break;
719
720 case kDisassocFrameType:
721 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700722 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700723 break;
724
725 default:
726 break;
727 }
728 }
729}
730
Wade Guthried4977f22012-08-22 12:37:54 -0700731bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700732 if (!output) {
733 LOG(ERROR) << "NULL |output|";
734 return false;
735 }
736
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800737 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700738 output->append(" [no frame]");
739 return true;
740 }
741
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800742 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700743 output->append(" [invalid frame: ");
744 } else {
745 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
746
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800747 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700748 case kAssocResponseFrameType:
749 StringAppendF(output, "; AssocResponse status: %u: %s",
750 status_,
repo syncdc085c82012-12-28 08:54:41 -0800751 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700752 break;
753 case kReassocResponseFrameType:
754 StringAppendF(output, "; ReassocResponse status: %u: %s",
755 status_,
repo syncdc085c82012-12-28 08:54:41 -0800756 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700757 break;
758 case kAuthFrameType:
759 StringAppendF(output, "; Auth status: %u: %s",
760 status_,
repo syncdc085c82012-12-28 08:54:41 -0800761 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700762 break;
763
764 case kDisassocFrameType:
765 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700766 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800767 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700768 break;
769 case kDeauthFrameType:
770 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700771 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800772 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700773 break;
774
775 default:
776 break;
777 }
778 output->append(" [frame: ");
779 }
780
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800781 const unsigned char *frame = frame_.GetConstData();
782 for (size_t i = 0; i < frame_.GetLength(); ++i) {
783 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700784 }
785 output->append("]");
786
787 return true;
788}
789
Wade Guthried4977f22012-08-22 12:37:54 -0700790bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800791 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700792}
793
Wade Guthried4977f22012-08-22 12:37:54 -0700794
Wade Guthrie0d438532012-05-18 14:18:50 -0700795//
repo syncdc085c82012-12-28 08:54:41 -0800796// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700797//
798
repo sync0efa9f02012-12-28 13:40:20 -0800799// An Ack is not a GENL message and, as such, has no command.
800const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
801const char AckMessage::kCommandString[] = "NL80211_ACK";
802
Wade Guthrie0d438532012-05-18 14:18:50 -0700803const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
804const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
805
Wade Guthrie0d438532012-05-18 14:18:50 -0700806const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
807const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
808
Wade Guthrie0d438532012-05-18 14:18:50 -0700809const uint8_t CancelRemainOnChannelMessage::kCommand =
810 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
811const char CancelRemainOnChannelMessage::kCommandString[] =
812 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
813
Wade Guthrie0d438532012-05-18 14:18:50 -0700814const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
815const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
816
Wade Guthrie0d438532012-05-18 14:18:50 -0700817const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
818const char DeauthenticateMessage::kCommandString[] =
819 "NL80211_CMD_DEAUTHENTICATE";
820
Wade Guthrie0d438532012-05-18 14:18:50 -0700821const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
822const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
823
Wade Guthrie0d438532012-05-18 14:18:50 -0700824const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
825const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
826
Wade Guthrie0d438532012-05-18 14:18:50 -0700827const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
828const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
829
repo sync0efa9f02012-12-28 13:40:20 -0800830// An Error is not a GENL message and, as such, has no command.
831const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
832const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
833
834ErrorMessage::ErrorMessage(uint32_t error)
835 : Nl80211Message(kCommand, kCommandString), error_(error) {}
836
837string ErrorMessage::ToString() const {
838 string output;
839 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
840 error_, strerror(error_));
841 return output;
842}
843
Wade Guthrie0d438532012-05-18 14:18:50 -0700844const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
845const char FrameTxStatusMessage::kCommandString[] =
846 "NL80211_CMD_FRAME_TX_STATUS";
847
repo sync0efa9f02012-12-28 13:40:20 -0800848const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
849const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
850
Wade Guthrie0d438532012-05-18 14:18:50 -0700851const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
852const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
853
Wade Guthrie0d438532012-05-18 14:18:50 -0700854const uint8_t MichaelMicFailureMessage::kCommand =
855 NL80211_CMD_MICHAEL_MIC_FAILURE;
856const char MichaelMicFailureMessage::kCommandString[] =
857 "NL80211_CMD_MICHAEL_MIC_FAILURE";
858
Wade Guthrie0d438532012-05-18 14:18:50 -0700859const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
860const char NewScanResultsMessage::kCommandString[] =
861 "NL80211_CMD_NEW_SCAN_RESULTS";
862
Wade Guthrie0d438532012-05-18 14:18:50 -0700863const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
864const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
865
Wade Guthrie0d438532012-05-18 14:18:50 -0700866const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
867const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
868
repo sync0efa9f02012-12-28 13:40:20 -0800869// A NOOP is not a GENL message and, as such, has no command.
870const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
871const char NoopMessage::kCommandString[] = "NL80211_NOOP";
872
Wade Guthrie0d438532012-05-18 14:18:50 -0700873const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
874const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
875
Wade Guthrie0d438532012-05-18 14:18:50 -0700876const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
877const char PmksaCandidateMessage::kCommandString[] =
878 "NL80211_ATTR_PMKSA_CANDIDATE";
879
Wade Guthrie0d438532012-05-18 14:18:50 -0700880const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
881const char RegBeaconHintMessage::kCommandString[] =
882 "NL80211_CMD_REG_BEACON_HINT";
883
Wade Guthrie0d438532012-05-18 14:18:50 -0700884const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
885const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
886
Wade Guthrie0d438532012-05-18 14:18:50 -0700887const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
888const char RemainOnChannelMessage::kCommandString[] =
889 "NL80211_CMD_REMAIN_ON_CHANNEL";
890
Wade Guthrie0d438532012-05-18 14:18:50 -0700891const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
892const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
893
Wade Guthrie0d438532012-05-18 14:18:50 -0700894const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
895const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
896
Wade Guthrie0d438532012-05-18 14:18:50 -0700897const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
898const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
899
Wade Guthrie0d438532012-05-18 14:18:50 -0700900const uint8_t UnknownMessage::kCommand = 0xff;
901const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
902
Wade Guthrie0d438532012-05-18 14:18:50 -0700903const uint8_t UnprotDeauthenticateMessage::kCommand =
904 NL80211_CMD_UNPROT_DEAUTHENTICATE;
905const char UnprotDeauthenticateMessage::kCommandString[] =
906 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
907
Wade Guthrie0d438532012-05-18 14:18:50 -0700908const uint8_t UnprotDisassociateMessage::kCommand =
909 NL80211_CMD_UNPROT_DISASSOCIATE;
910const char UnprotDisassociateMessage::kCommandString[] =
911 "NL80211_CMD_UNPROT_DISASSOCIATE";
912
Wade Guthrie0d438532012-05-18 14:18:50 -0700913//
914// Factory class.
915//
916
repo syncdc085c82012-12-28 08:54:41 -0800917Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700918 if (!msg) {
919 LOG(ERROR) << "NULL |msg| parameter";
920 return NULL;
921 }
922
repo syncdc085c82012-12-28 08:54:41 -0800923 scoped_ptr<Nl80211Message> message;
repo sync0efa9f02012-12-28 13:40:20 -0800924 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -0800925
repo sync0efa9f02012-12-28 13:40:20 -0800926 if (msg->nlmsg_type == NLMSG_NOOP) {
927 SLOG(WiFi, 6) << "Creating a NOP message";
928 message.reset(new NoopMessage());
929 } else if (msg->nlmsg_type == NLMSG_ERROR) {
930 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
931 if (error_code) {
932 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
933 message.reset(new ErrorMessage(error_code));
934 } else {
935 SLOG(WiFi, 6) << "Creating an ACK message";
936 message.reset(new AckMessage());
937 }
938 } else {
939 SLOG(WiFi, 6) << "Creating a Regular message";
940 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -0700941
repo sync0efa9f02012-12-28 13:40:20 -0800942 switch (gnlh->cmd) {
943 case AssociateMessage::kCommand:
944 message.reset(new AssociateMessage()); break;
945 case AuthenticateMessage::kCommand:
946 message.reset(new AuthenticateMessage()); break;
947 case CancelRemainOnChannelMessage::kCommand:
948 message.reset(new CancelRemainOnChannelMessage()); break;
949 case ConnectMessage::kCommand:
950 message.reset(new ConnectMessage()); break;
951 case DeauthenticateMessage::kCommand:
952 message.reset(new DeauthenticateMessage()); break;
953 case DeleteStationMessage::kCommand:
954 message.reset(new DeleteStationMessage()); break;
955 case DisassociateMessage::kCommand:
956 message.reset(new DisassociateMessage()); break;
957 case DisconnectMessage::kCommand:
958 message.reset(new DisconnectMessage()); break;
959 case FrameTxStatusMessage::kCommand:
960 message.reset(new FrameTxStatusMessage()); break;
961 case GetRegMessage::kCommand:
962 message.reset(new GetRegMessage()); break;
963 case JoinIbssMessage::kCommand:
964 message.reset(new JoinIbssMessage()); break;
965 case MichaelMicFailureMessage::kCommand:
966 message.reset(new MichaelMicFailureMessage()); break;
967 case NewScanResultsMessage::kCommand:
968 message.reset(new NewScanResultsMessage()); break;
969 case NewStationMessage::kCommand:
970 message.reset(new NewStationMessage()); break;
971 case NewWifiMessage::kCommand:
972 message.reset(new NewWifiMessage()); break;
973 case NotifyCqmMessage::kCommand:
974 message.reset(new NotifyCqmMessage()); break;
975 case PmksaCandidateMessage::kCommand:
976 message.reset(new PmksaCandidateMessage()); break;
977 case RegBeaconHintMessage::kCommand:
978 message.reset(new RegBeaconHintMessage()); break;
979 case RegChangeMessage::kCommand:
980 message.reset(new RegChangeMessage()); break;
981 case RemainOnChannelMessage::kCommand:
982 message.reset(new RemainOnChannelMessage()); break;
983 case RoamMessage::kCommand:
984 message.reset(new RoamMessage()); break;
985 case ScanAbortedMessage::kCommand:
986 message.reset(new ScanAbortedMessage()); break;
987 case TriggerScanMessage::kCommand:
988 message.reset(new TriggerScanMessage()); break;
989 case UnprotDeauthenticateMessage::kCommand:
990 message.reset(new UnprotDeauthenticateMessage()); break;
991 case UnprotDisassociateMessage::kCommand:
992 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -0700993
repo sync0efa9f02012-12-28 13:40:20 -0800994 default:
995 message.reset(new UnknownMessage(gnlh->cmd)); break;
996 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700997
repo sync0efa9f02012-12-28 13:40:20 -0800998 if (!message->InitFromNlmsg(msg)) {
999 LOG(ERROR) << "Message did not initialize properly";
1000 return NULL;
1001 }
Wade Guthrie0d438532012-05-18 14:18:50 -07001002 }
1003
Wade Guthrie0d438532012-05-18 14:18:50 -07001004 return message.release();
1005}
1006
repo syncdc085c82012-12-28 08:54:41 -08001007Nl80211MessageDataCollector *
1008 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001009 return g_datacollector.Pointer();
1010}
1011
repo syncdc085c82012-12-28 08:54:41 -08001012Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -07001013 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
1014 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
1015 need_to_print[NL80211_CMD_DEL_STATION] = true;
1016 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
1017 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
1018 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
1019 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
1020 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
1021 need_to_print[NL80211_CMD_REG_CHANGE] = true;
1022 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
1023 need_to_print[NL80211_CMD_ROAM] = true;
1024 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
1025 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
1026 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
1027}
1028
repo syncdc085c82012-12-28 08:54:41 -08001029void Nl80211MessageDataCollector::CollectDebugData(
1030 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001031 if (!msg) {
1032 LOG(ERROR) << "NULL |msg| parameter";
1033 return;
1034 }
1035
1036 bool doit = false;
1037
1038 map<uint8_t, bool>::const_iterator node;
Wade Guthriebdcdaa72013-03-04 12:47:12 -08001039 node = need_to_print.find(message.command());
Wade Guthrie0d438532012-05-18 14:18:50 -07001040 if (node != need_to_print.end())
1041 doit = node->second;
1042
1043 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001044 LOG(INFO) << "@@const unsigned char "
Wade Guthriebdcdaa72013-03-04 12:47:12 -08001045 << "k" << message.command_string()
Wade Guthried6153612012-08-23 11:36:14 -07001046 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001047
Christopher Wileyefd521f2012-11-07 17:32:46 -08001048 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001049
1050 size_t bytes = nlmsg_total_size(payload_bytes);
1051 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001052 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001053 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001054 << std::hex << std::setfill('0') << std::setw(2)
1055 << + rawdata[i] << ",";
1056 }
Wade Guthried6153612012-08-23 11:36:14 -07001057 LOG(INFO) << "};";
Wade Guthriebdcdaa72013-03-04 12:47:12 -08001058 need_to_print[message.command()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001059 }
1060}
1061
1062} // namespace shill.