blob: 67dea40af4843651354ed241a8ad1c29a1542664 [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 Guthrief48a1952013-03-04 17:33:47 -080076const uint32_t NetlinkMessage::kBroadcastSequenceNumber = 0;
77const uint16_t NetlinkMessage::kIllegalMessageType = UINT16_MAX;
Wade Guthriebdcdaa72013-03-04 12:47:12 -080078
79const char Nl80211Message::kBogusMacAddress[] = "XX:XX:XX:XX:XX:XX";
repo syncdc085c82012-12-28 08:54:41 -080080const unsigned int Nl80211Message::kEthernetAddressBytes = 6;
81map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
82map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
Wade Guthriebdcdaa72013-03-04 12:47:12 -080083uint16_t Nl80211Message::nl80211_message_type_ = kIllegalMessageType;
84
Wade Guthrief48a1952013-03-04 17:33:47 -080085// NetlinkMessage
Wade Guthrie0d438532012-05-18 14:18:50 -070086
Wade Guthrief48a1952013-03-04 17:33:47 -080087ByteString NetlinkMessage::EncodeHeader(uint32_t sequence_number,
88 uint16_t nlmsg_type) {
89 ByteString result;
90 sequence_number_ = sequence_number;
91 if (sequence_number_ == kBroadcastSequenceNumber) {
92 LOG(ERROR) << "Couldn't get a legal sequence number";
93 return result;
94 }
Wade Guthrie0d438532012-05-18 14:18:50 -070095
Wade Guthrief48a1952013-03-04 17:33:47 -080096 // Build netlink header.
97 nlmsghdr header;
98 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
99 header.nlmsg_len = nlmsghdr_with_pad;
100 header.nlmsg_type = nlmsg_type;
101 header.nlmsg_flags = NLM_F_REQUEST | flags_;
102 header.nlmsg_seq = sequence_number_;
103 header.nlmsg_pid = getpid();
Wade Guthrie0d438532012-05-18 14:18:50 -0700104
Wade Guthrief48a1952013-03-04 17:33:47 -0800105 // Netlink header + pad.
106 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
107 sizeof(header)));
108 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
109 return result;
110}
111
112bool NetlinkMessage::InitAndStripHeader(ByteString *input) {
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800113 if (!input) {
114 LOG(ERROR) << "NULL input";
115 return false;
116 }
Wade Guthrief48a1952013-03-04 17:33:47 -0800117 if (input->GetLength() < sizeof(nlmsghdr)) {
118 LOG(ERROR) << "Insufficient input to extract nlmsghdr";
119 return false;
120 }
121
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800122 // Read the nlmsghdr.
123 nlmsghdr *header = reinterpret_cast<nlmsghdr *>(input->GetData());
124 message_type_ = header->nlmsg_type;
125 flags_ = header->nlmsg_flags;
126 sequence_number_ = header->nlmsg_seq;
127
128 // Strip the nlmsghdr.
129 input->RemovePrefix(NLMSG_ALIGN(sizeof(struct nlmsghdr)));
Wade Guthrief48a1952013-03-04 17:33:47 -0800130 return true;
131}
132
133bool NetlinkMessage::InitFromNlmsg(const nlmsghdr *const_msg) {
134 if (!const_msg) {
135 LOG(ERROR) << "Null |const_msg| parameter";
136 return false;
137 }
138 ByteString message(reinterpret_cast<const unsigned char *>(const_msg),
139 const_msg->nlmsg_len);
140 if (!InitAndStripHeader(&message)) {
141 return false;
142 }
143 return true;
144}
145
146// static
147void NetlinkMessage::PrintBytes(int log_level, const unsigned char *buf,
148 size_t num_bytes) {
149 SLOG(WiFi, log_level) << "Netlink Message -- Examining Bytes";
150 if (!buf) {
151 SLOG(WiFi, log_level) << "<NULL Buffer>";
152 return;
153 }
154
155 if (num_bytes >= sizeof(nlmsghdr)) {
156 const nlmsghdr *header = reinterpret_cast<const nlmsghdr *>(buf);
157 SLOG(WiFi, log_level) << StringPrintf(
158 "len: %02x %02x %02x %02x = %u bytes",
159 buf[0], buf[1], buf[2], buf[3], header->nlmsg_len);
160
161 SLOG(WiFi, log_level) << StringPrintf(
162 "type | flags: %02x %02x %02x %02x - type:%u flags:%s%s%s%s%s",
163 buf[4], buf[5], buf[6], buf[7], header->nlmsg_type,
164 ((header->nlmsg_flags & NLM_F_REQUEST) ? " REQUEST" : ""),
165 ((header->nlmsg_flags & NLM_F_MULTI) ? " MULTI" : ""),
166 ((header->nlmsg_flags & NLM_F_ACK) ? " ACK" : ""),
167 ((header->nlmsg_flags & NLM_F_ECHO) ? " ECHO" : ""),
168 ((header->nlmsg_flags & NLM_F_DUMP_INTR) ? " BAD-SEQ" : ""));
169
170 SLOG(WiFi, log_level) << StringPrintf(
171 "sequence: %02x %02x %02x %02x = %u",
172 buf[8], buf[9], buf[10], buf[11], header->nlmsg_seq);
173 SLOG(WiFi, log_level) << StringPrintf(
174 "pid: %02x %02x %02x %02x = %u",
175 buf[12], buf[13], buf[14], buf[15], header->nlmsg_pid);
176 buf += sizeof(nlmsghdr);
177 num_bytes -= sizeof(nlmsghdr);
178 } else {
179 SLOG(WiFi, log_level) << "Not enough bytes (" << num_bytes
180 << ") for a complete nlmsghdr (requires "
181 << sizeof(nlmsghdr) << ").";
182 }
183
184 while (num_bytes) {
185 string output;
186 size_t bytes_this_row = min(num_bytes, static_cast<size_t>(32));
187 for (size_t i = 0; i < bytes_this_row; ++i) {
188 StringAppendF(&output, " %02x", *buf++);
189 }
190 SLOG(WiFi, log_level) << output;
191 num_bytes -= bytes_this_row;
192 }
193}
194
195// GenericNetlinkMessage
196
197ByteString GenericNetlinkMessage::EncodeHeader(uint32_t sequence_number,
198 uint16_t nlmsg_type) {
199 // Build nlmsghdr.
200 ByteString result(NetlinkMessage::EncodeHeader(sequence_number, nlmsg_type));
201 if (result.GetLength() == 0) {
202 LOG(ERROR) << "Couldn't encode message header.";
203 return result;
204 }
205
206 // Build and append the genl message header.
207 genlmsghdr genl_header;
208 genl_header.cmd = command();
209 genl_header.version = 1;
210 genl_header.reserved = 0;
211
212 ByteString genl_header_string(
213 reinterpret_cast<unsigned char *>(&genl_header), sizeof(genl_header));
214 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
215 genl_header_string.Resize(genlmsghdr_with_pad); // Zero-fill.
216
217 nlmsghdr *pheader = reinterpret_cast<nlmsghdr *>(result.GetData());
218 pheader->nlmsg_len += genlmsghdr_with_pad;
219 result.Append(genl_header_string);
220 return result;
221}
222
223ByteString GenericNetlinkMessage::Encode(uint32_t sequence_number,
224 uint16_t nlmsg_type) {
225 ByteString result(EncodeHeader(sequence_number, nlmsg_type));
226 if (result.GetLength() == 0) {
227 LOG(ERROR) << "Couldn't encode message header.";
228 return result;
229 }
230
231 // Build and append attributes (padding is included by
232 // AttributeList::Encode).
233 ByteString attribute_string = attributes_->Encode();
234
235 // Need to re-calculate |header| since |Append|, above, moves the data.
236 nlmsghdr *pheader = reinterpret_cast<nlmsghdr *>(result.GetData());
237 pheader->nlmsg_len += attribute_string.GetLength();
238 result.Append(attribute_string);
239
240 return result;
241}
242
243bool GenericNetlinkMessage::InitAndStripHeader(ByteString *input) {
244 if (!input) {
245 LOG(ERROR) << "NULL input";
246 return false;
247 }
248 if (!NetlinkMessage::InitAndStripHeader(input)) {
249 return false;
250 }
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800251
252 // Read the genlmsghdr.
253 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(input->GetData());
254 if (command_ != gnlh->cmd) {
255 LOG(WARNING) << "This object thinks it's a " << command_
256 << " but the message thinks it's a " << gnlh->cmd;
257 }
258
259 // Strip the genlmsghdr.
260 input->RemovePrefix(NLMSG_ALIGN(sizeof(struct genlmsghdr)));
261 return true;
Wade Guthrie8e278612013-02-26 10:32:34 -0800262}
263
Wade Guthrief48a1952013-03-04 17:33:47 -0800264void GenericNetlinkMessage::Print(int log_level) const {
265 SLOG(WiFi, log_level) << StringPrintf("Message %s (%d)",
266 command_string(),
267 command());
268 attributes_->Print(log_level, 1);
269}
270
271// Nl80211Message
272
repo sync0efa9f02012-12-28 13:40:20 -0800273bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) {
274 if (!const_msg) {
275 LOG(ERROR) << "Null |msg| parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700276 return false;
277 }
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800278 ByteString message(reinterpret_cast<const unsigned char *>(const_msg),
279 const_msg->nlmsg_len);
Wade Guthrie0d438532012-05-18 14:18:50 -0700280
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800281 if (!InitAndStripHeader(&message)) {
282 return false;
283 }
repo sync0efa9f02012-12-28 13:40:20 -0800284
285 // Attributes.
286 // Parse the attributes from the nl message payload into the 'tb' array.
287 nlattr *tb[NL80211_ATTR_MAX + 1];
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800288 nla_parse(tb, NL80211_ATTR_MAX,
289 reinterpret_cast<nlattr *>(message.GetData()), message.GetLength(),
290 NULL);
repo sync0efa9f02012-12-28 13:40:20 -0800291
Wade Guthrie0d438532012-05-18 14:18:50 -0700292 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
293 if (tb[i]) {
repo sync0efa9f02012-12-28 13:40:20 -0800294 // TODO(wdg): When Nl80211Messages instantiate their own attributes,
295 // this call should, instead, call |SetAttributeFromNlAttr|.
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800296 attributes_->CreateAndInitAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800297 i, tb[i], Bind(&NetlinkAttribute::NewNl80211AttributeFromId));
Wade Guthrie0d438532012-05-18 14:18:50 -0700298 }
299 }
300
301 // Convert integer values provided by libnl (for example, from the
302 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
303 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700304 if (!reason_code_string_) {
305 reason_code_string_ = new map<uint16_t, string>;
306 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
307 "Unspecified reason";
308 (*reason_code_string_)[
309 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700310 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700311 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
312 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
313 "ESS";
314 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
315 "Disassociated due to inactivity";
316 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
317 "Disassociated because AP is unable to handle all currently associated "
318 "STAs";
319 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
320 "Class 2 frame received from nonauthenticated STA";
321 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
322 "Class 3 frame received from nonassociated STA";
323 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
324 "Disassociated because sending STA is leaving (or has left) BSS";
325 (*reason_code_string_)[
326 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
327 "STA requesting (re)association is not authenticated with responding "
328 "STA";
329 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
330 "Disassociated because the information in the Power Capability "
331 "element is unacceptable";
332 (*reason_code_string_)[
333 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
334 "Disassociated because the information in the Supported Channels "
335 "element is unacceptable";
336 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
337 "Invalid information element, i.e., an information element defined in "
338 "this standard for which the content does not meet the specifications "
339 "in Clause 7";
340 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
341 "Message integrity code (MIC) failure";
342 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
343 "4-Way Handshake timeout";
344 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
345 "Group Key Handshake timeout";
346 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
347 "Information element in 4-Way Handshake different from "
348 "(Re)Association Request/Probe Response/Beacon frame";
349 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
350 "Invalid group cipher";
351 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
352 "Invalid pairwise cipher";
353 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
354 "Invalid AKMP";
355 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
356 "Unsupported RSN information element version";
357 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
358 "Invalid RSN information element capabilities";
359 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
360 "IEEE 802.1X authentication failed";
361 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
362 "Cipher suite rejected because of the security policy";
363 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
364 "Disassociated for unspecified, QoS-related reason";
365 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
366 "Disassociated because QoS AP lacks sufficient bandwidth for this "
367 "QoS STA";
368 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
369 "Disassociated because excessive number of frames need to be "
370 "acknowledged, but are not acknowledged due to AP transmissions "
371 "and/or poor channel conditions";
372 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
373 "Disassociated because STA is transmitting outside the limits of its "
374 "TXOPs";
375 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
376 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
377 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
378 "Requested from peer STA as it does not want to use the mechanism";
379 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
380 "Requested from peer STA as the STA received frames using the "
381 "mechanism for which a setup is required";
382 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
383 "Requested from peer STA due to timeout";
384 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
385 "Peer STA does not support the requested cipher suite";
386 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
387 }
388
389 if (!status_code_string_) {
390 status_code_string_ = new map<uint16_t, string>;
391 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
392 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
393 "Unspecified failure";
394 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700395 "Cannot support all requested capabilities in the capability "
396 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700397 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700398 "Reassociation denied due to inability to confirm that association "
399 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700400 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700401 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700402 (*status_code_string_)[
403 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700404 "Responding station does not support the specified authentication "
405 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700406 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700407 "Received an authentication frame with authentication transaction "
408 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700409 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700410 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700411 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700412 "Authentication rejected due to timeout waiting for next frame in "
413 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700414 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
415 "Association denied because AP is unable to handle additional "
416 "associated STA";
417 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700418 "Association denied due to requesting station not supporting all of "
419 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700420 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700421 "Association denied due to requesting station not supporting the "
422 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700423 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700424 "Association denied due to requesting station not supporting the PBCC "
425 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700426 (*status_code_string_)[
427 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700428 "Association denied due to requesting station not supporting the "
429 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700430 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700431 "Association request rejected because Spectrum Management capability "
432 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700433 (*status_code_string_)[
434 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700435 "Association request rejected because the information in the Power "
436 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700437 (*status_code_string_)[
438 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700439 "Association request rejected because the information in the "
440 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700441 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700442 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700443 "supporting the Short Slot Time option";
444 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
445 "Association request rejected due to requesting station not "
446 "supporting the DSSS-OFDM option";
447 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700448 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700449 (*status_code_string_)[
450 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700451 "Association denied due to QAP having insufficient bandwidth to handle "
452 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700453 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700454 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700455 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
456 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700457 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700458 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
459 "The request has been declined";
460 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700461 "The request has not been successful as one or more parameters have "
462 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700463 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
464 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700465 "However, a suggested Tspec is provided so that the initiating QSTA "
466 "may attempt to send another TS with the suggested changes to the "
467 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700468 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700469 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700470 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
471 "Invalid Group Cipher";
472 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
473 "Invalid Pairwise Cipher";
474 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
475 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
476 "Unsupported RSN Information Element version";
477 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
478 "Invalid RSN Information Element Capabilities";
479 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700480 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700481 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
482 "The TS has not been created. However, the HC may be capable of "
483 "creating a TS, in response to a request, after the time indicated in "
484 "the TS Delay element";
485 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700486 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700487 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
488 "Destination STA is not present within this BSS";
489 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
490 "The destination STA is not a QoS STA";
491 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700492 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700493 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700494 }
495
496 return true;
497}
498
Wade Guthrie8e278612013-02-26 10:32:34 -0800499
Wade Guthrie0d438532012-05-18 14:18:50 -0700500// Helper function to provide a string for a MAC address.
Wade Guthrie68da97c2013-02-26 13:09:35 -0800501bool Nl80211Message::GetMacAttributeString(int id, string *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700502 if (!value) {
503 LOG(ERROR) << "Null |value| parameter";
504 return false;
505 }
506
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800507 ByteString data;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -0800508 if (!const_attributes()->GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700509 value->assign(kBogusMacAddress);
510 return false;
511 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800512 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700513
514 return true;
515}
516
517// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
repo syncdc085c82012-12-28 08:54:41 -0800518bool Nl80211Message::GetScanFrequenciesAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800519 int id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700520 if (!value) {
521 LOG(ERROR) << "Null |value| parameter";
522 return false;
523 }
524
525 value->clear();
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800526
527 AttributeListConstRefPtr frequency_list;
528 if (!const_attributes()->ConstGetNestedAttributeList(
529 NL80211_ATTR_SCAN_FREQUENCIES, &frequency_list) || !frequency_list) {
530 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_FREQUENCIES attribute";
repo syncd316eb72012-12-10 15:48:47 -0800531 return false;
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800532 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700533
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800534 // Assume IDs for the nested attribute array are linear starting from 1.
535 // Currently, that is enforced in the input to the nested attribute.
536 uint32_t freq;
537 int i = 1;
538 while (frequency_list->GetU32AttributeValue(i, &freq)) {
539 value->push_back(freq);
540 ++i;
Wade Guthrie0d438532012-05-18 14:18:50 -0700541 }
repo syncd316eb72012-12-10 15:48:47 -0800542 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700543}
544
545// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800546bool Nl80211Message::GetScanSsidsAttribute(
Wade Guthrie68da97c2013-02-26 13:09:35 -0800547 int id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700548 if (!value) {
549 LOG(ERROR) << "Null |value| parameter";
550 return false;
551 }
552
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800553 AttributeListConstRefPtr ssid_list;
554 if (!const_attributes()->ConstGetNestedAttributeList(
555 NL80211_ATTR_SCAN_SSIDS, &ssid_list) || !ssid_list) {
556 LOG(ERROR) << "Couldn't get NL80211_ATTR_SCAN_SSIDS attribute";
repo sync90ee0fa2012-12-18 10:08:08 -0800557 return false;
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800558 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700559
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800560 // Assume IDs for the nested attribute array are linear starting from 1.
561 // Currently, that is enforced in the input to the nested attribute.
562 string ssid;
563 int i = 1;
564 while (ssid_list->GetStringAttributeValue(i, &ssid)) {
565 value->push_back(ssid);
566 ++i;
Wade Guthrie0d438532012-05-18 14:18:50 -0700567 }
repo sync90ee0fa2012-12-18 10:08:08 -0800568 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700569}
570
Wade Guthrie0d438532012-05-18 14:18:50 -0700571// static
repo syncdc085c82012-12-28 08:54:41 -0800572string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700573 string output;
574
575 if (!arg) {
576 output = kBogusMacAddress;
577 LOG(ERROR) << "|arg| parameter is NULL.";
578 } else {
579 StringAppendF(&output, "%02x", arg[0]);
580
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800581 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700582 StringAppendF(&output, ":%02x", arg[i]);
583 }
584 }
585 return output;
586}
587
588// static
repo syncdc085c82012-12-28 08:54:41 -0800589string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700590 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700591 match = reason_code_string_->find(status);
592 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700593 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700594 if (status < IEEE_80211::kReasonCodeMax) {
595 StringAppendF(&output, "<Reserved Reason:%u>", status);
596 } else {
597 StringAppendF(&output, "<Unknown Reason:%u>", status);
598 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700599 return output;
600 }
601 return match->second;
602}
603
Wade Guthried4977f22012-08-22 12:37:54 -0700604// static
repo syncdc085c82012-12-28 08:54:41 -0800605string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700606 map<uint16_t, string>::const_iterator match;
607 match = status_code_string_->find(status);
608 if (match == status_code_string_->end()) {
609 string output;
610 if (status < IEEE_80211::kStatusCodeMax) {
611 StringAppendF(&output, "<Reserved Status:%u>", status);
612 } else {
613 StringAppendF(&output, "<Unknown Status:%u>", status);
614 }
615 return output;
616 }
617 return match->second;
618}
619
repo syncdc085c82012-12-28 08:54:41 -0800620
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800621Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700622 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800623 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700624 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800625 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
626 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700627
628 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800629 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700630 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800631 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
632 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700633 frame_type_ = frame->frame_control & kFrameTypeMask;
634
635 switch (frame_type_) {
636 case kAssocResponseFrameType:
637 case kReassocResponseFrameType:
638 status_ = le16toh(frame->u.associate_response.status_code);
639 break;
640
641 case kAuthFrameType:
642 status_ = le16toh(frame->u.authentiate_message.status_code);
643 break;
644
645 case kDisassocFrameType:
646 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700647 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700648 break;
649
650 default:
651 break;
652 }
653 }
654}
655
Wade Guthried4977f22012-08-22 12:37:54 -0700656bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700657 if (!output) {
658 LOG(ERROR) << "NULL |output|";
659 return false;
660 }
661
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800662 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700663 output->append(" [no frame]");
664 return true;
665 }
666
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800667 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700668 output->append(" [invalid frame: ");
669 } else {
670 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
671
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800672 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700673 case kAssocResponseFrameType:
674 StringAppendF(output, "; AssocResponse status: %u: %s",
675 status_,
repo syncdc085c82012-12-28 08:54:41 -0800676 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700677 break;
678 case kReassocResponseFrameType:
679 StringAppendF(output, "; ReassocResponse status: %u: %s",
680 status_,
repo syncdc085c82012-12-28 08:54:41 -0800681 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700682 break;
683 case kAuthFrameType:
684 StringAppendF(output, "; Auth status: %u: %s",
685 status_,
repo syncdc085c82012-12-28 08:54:41 -0800686 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700687 break;
688
689 case kDisassocFrameType:
690 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700691 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800692 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700693 break;
694 case kDeauthFrameType:
695 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700696 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800697 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700698 break;
699
700 default:
701 break;
702 }
703 output->append(" [frame: ");
704 }
705
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800706 const unsigned char *frame = frame_.GetConstData();
707 for (size_t i = 0; i < frame_.GetLength(); ++i) {
708 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700709 }
710 output->append("]");
711
712 return true;
713}
714
Wade Guthried4977f22012-08-22 12:37:54 -0700715bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800716 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700717}
718
Wade Guthried4977f22012-08-22 12:37:54 -0700719
Wade Guthrie0d438532012-05-18 14:18:50 -0700720//
repo syncdc085c82012-12-28 08:54:41 -0800721// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700722//
723
repo sync0efa9f02012-12-28 13:40:20 -0800724// An Ack is not a GENL message and, as such, has no command.
725const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
726const char AckMessage::kCommandString[] = "NL80211_ACK";
727
Wade Guthrie0d438532012-05-18 14:18:50 -0700728const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
729const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
730
Wade Guthrie0d438532012-05-18 14:18:50 -0700731const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
732const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
733
Wade Guthrie0d438532012-05-18 14:18:50 -0700734const uint8_t CancelRemainOnChannelMessage::kCommand =
735 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
736const char CancelRemainOnChannelMessage::kCommandString[] =
737 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
738
Wade Guthrie0d438532012-05-18 14:18:50 -0700739const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
740const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
741
Wade Guthrie0d438532012-05-18 14:18:50 -0700742const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
743const char DeauthenticateMessage::kCommandString[] =
744 "NL80211_CMD_DEAUTHENTICATE";
745
Wade Guthrie0d438532012-05-18 14:18:50 -0700746const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
747const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
748
Wade Guthrie0d438532012-05-18 14:18:50 -0700749const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
750const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
751
Wade Guthrie0d438532012-05-18 14:18:50 -0700752const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
753const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
754
repo sync0efa9f02012-12-28 13:40:20 -0800755// An Error is not a GENL message and, as such, has no command.
756const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
757const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
758
759ErrorMessage::ErrorMessage(uint32_t error)
760 : Nl80211Message(kCommand, kCommandString), error_(error) {}
761
762string ErrorMessage::ToString() const {
763 string output;
764 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
765 error_, strerror(error_));
766 return output;
767}
768
Wade Guthrie0d438532012-05-18 14:18:50 -0700769const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
770const char FrameTxStatusMessage::kCommandString[] =
771 "NL80211_CMD_FRAME_TX_STATUS";
772
repo sync0efa9f02012-12-28 13:40:20 -0800773const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
774const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
775
Wade Guthrie0d438532012-05-18 14:18:50 -0700776const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
777const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
778
Wade Guthrie0d438532012-05-18 14:18:50 -0700779const uint8_t MichaelMicFailureMessage::kCommand =
780 NL80211_CMD_MICHAEL_MIC_FAILURE;
781const char MichaelMicFailureMessage::kCommandString[] =
782 "NL80211_CMD_MICHAEL_MIC_FAILURE";
783
Wade Guthrie0d438532012-05-18 14:18:50 -0700784const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
785const char NewScanResultsMessage::kCommandString[] =
786 "NL80211_CMD_NEW_SCAN_RESULTS";
787
Wade Guthrie0d438532012-05-18 14:18:50 -0700788const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
789const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
790
Wade Guthrie0d438532012-05-18 14:18:50 -0700791const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
792const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
793
repo sync0efa9f02012-12-28 13:40:20 -0800794// A NOOP is not a GENL message and, as such, has no command.
795const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
796const char NoopMessage::kCommandString[] = "NL80211_NOOP";
797
Wade Guthrie0d438532012-05-18 14:18:50 -0700798const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
799const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
800
Wade Guthrie0d438532012-05-18 14:18:50 -0700801const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
802const char PmksaCandidateMessage::kCommandString[] =
803 "NL80211_ATTR_PMKSA_CANDIDATE";
804
Wade Guthrie0d438532012-05-18 14:18:50 -0700805const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
806const char RegBeaconHintMessage::kCommandString[] =
807 "NL80211_CMD_REG_BEACON_HINT";
808
Wade Guthrie0d438532012-05-18 14:18:50 -0700809const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
810const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
811
Wade Guthrie0d438532012-05-18 14:18:50 -0700812const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
813const char RemainOnChannelMessage::kCommandString[] =
814 "NL80211_CMD_REMAIN_ON_CHANNEL";
815
Wade Guthrie0d438532012-05-18 14:18:50 -0700816const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
817const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
818
Wade Guthrie0d438532012-05-18 14:18:50 -0700819const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
820const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
821
Wade Guthrie0d438532012-05-18 14:18:50 -0700822const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
823const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
824
Wade Guthrie0d438532012-05-18 14:18:50 -0700825const uint8_t UnknownMessage::kCommand = 0xff;
826const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
827
Wade Guthrie0d438532012-05-18 14:18:50 -0700828const uint8_t UnprotDeauthenticateMessage::kCommand =
829 NL80211_CMD_UNPROT_DEAUTHENTICATE;
830const char UnprotDeauthenticateMessage::kCommandString[] =
831 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
832
Wade Guthrie0d438532012-05-18 14:18:50 -0700833const uint8_t UnprotDisassociateMessage::kCommand =
834 NL80211_CMD_UNPROT_DISASSOCIATE;
835const char UnprotDisassociateMessage::kCommandString[] =
836 "NL80211_CMD_UNPROT_DISASSOCIATE";
837
Wade Guthrie0d438532012-05-18 14:18:50 -0700838//
839// Factory class.
840//
841
Wade Guthrief48a1952013-03-04 17:33:47 -0800842NetlinkMessage *NetlinkMessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700843 if (!msg) {
844 LOG(ERROR) << "NULL |msg| parameter";
845 return NULL;
846 }
847
Wade Guthrief48a1952013-03-04 17:33:47 -0800848 scoped_ptr<NetlinkMessage> message;
repo sync0efa9f02012-12-28 13:40:20 -0800849 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -0800850
repo sync0efa9f02012-12-28 13:40:20 -0800851 if (msg->nlmsg_type == NLMSG_NOOP) {
852 SLOG(WiFi, 6) << "Creating a NOP message";
853 message.reset(new NoopMessage());
854 } else if (msg->nlmsg_type == NLMSG_ERROR) {
855 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
856 if (error_code) {
857 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
858 message.reset(new ErrorMessage(error_code));
859 } else {
860 SLOG(WiFi, 6) << "Creating an ACK message";
861 message.reset(new AckMessage());
862 }
863 } else {
864 SLOG(WiFi, 6) << "Creating a Regular message";
865 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -0700866
repo sync0efa9f02012-12-28 13:40:20 -0800867 switch (gnlh->cmd) {
868 case AssociateMessage::kCommand:
869 message.reset(new AssociateMessage()); break;
870 case AuthenticateMessage::kCommand:
871 message.reset(new AuthenticateMessage()); break;
872 case CancelRemainOnChannelMessage::kCommand:
873 message.reset(new CancelRemainOnChannelMessage()); break;
874 case ConnectMessage::kCommand:
875 message.reset(new ConnectMessage()); break;
876 case DeauthenticateMessage::kCommand:
877 message.reset(new DeauthenticateMessage()); break;
878 case DeleteStationMessage::kCommand:
879 message.reset(new DeleteStationMessage()); break;
880 case DisassociateMessage::kCommand:
881 message.reset(new DisassociateMessage()); break;
882 case DisconnectMessage::kCommand:
883 message.reset(new DisconnectMessage()); break;
884 case FrameTxStatusMessage::kCommand:
885 message.reset(new FrameTxStatusMessage()); break;
886 case GetRegMessage::kCommand:
887 message.reset(new GetRegMessage()); break;
888 case JoinIbssMessage::kCommand:
889 message.reset(new JoinIbssMessage()); break;
890 case MichaelMicFailureMessage::kCommand:
891 message.reset(new MichaelMicFailureMessage()); break;
892 case NewScanResultsMessage::kCommand:
893 message.reset(new NewScanResultsMessage()); break;
894 case NewStationMessage::kCommand:
895 message.reset(new NewStationMessage()); break;
896 case NewWifiMessage::kCommand:
897 message.reset(new NewWifiMessage()); break;
898 case NotifyCqmMessage::kCommand:
899 message.reset(new NotifyCqmMessage()); break;
900 case PmksaCandidateMessage::kCommand:
901 message.reset(new PmksaCandidateMessage()); break;
902 case RegBeaconHintMessage::kCommand:
903 message.reset(new RegBeaconHintMessage()); break;
904 case RegChangeMessage::kCommand:
905 message.reset(new RegChangeMessage()); break;
906 case RemainOnChannelMessage::kCommand:
907 message.reset(new RemainOnChannelMessage()); break;
908 case RoamMessage::kCommand:
909 message.reset(new RoamMessage()); break;
910 case ScanAbortedMessage::kCommand:
911 message.reset(new ScanAbortedMessage()); break;
912 case TriggerScanMessage::kCommand:
913 message.reset(new TriggerScanMessage()); break;
914 case UnprotDeauthenticateMessage::kCommand:
915 message.reset(new UnprotDeauthenticateMessage()); break;
916 case UnprotDisassociateMessage::kCommand:
917 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -0700918
repo sync0efa9f02012-12-28 13:40:20 -0800919 default:
920 message.reset(new UnknownMessage(gnlh->cmd)); break;
921 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700922
repo sync0efa9f02012-12-28 13:40:20 -0800923 if (!message->InitFromNlmsg(msg)) {
924 LOG(ERROR) << "Message did not initialize properly";
925 return NULL;
926 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700927 }
928
Wade Guthrie0d438532012-05-18 14:18:50 -0700929 return message.release();
930}
931
repo syncdc085c82012-12-28 08:54:41 -0800932Nl80211MessageDataCollector *
933 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700934 return g_datacollector.Pointer();
935}
936
repo syncdc085c82012-12-28 08:54:41 -0800937Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700938 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
939 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
940 need_to_print[NL80211_CMD_DEL_STATION] = true;
941 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
942 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
943 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
944 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
945 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
946 need_to_print[NL80211_CMD_REG_CHANGE] = true;
947 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
948 need_to_print[NL80211_CMD_ROAM] = true;
949 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
950 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
951 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
952}
953
repo syncdc085c82012-12-28 08:54:41 -0800954void Nl80211MessageDataCollector::CollectDebugData(
955 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700956 if (!msg) {
957 LOG(ERROR) << "NULL |msg| parameter";
958 return;
959 }
960
961 bool doit = false;
962
963 map<uint8_t, bool>::const_iterator node;
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800964 node = need_to_print.find(message.command());
Wade Guthrie0d438532012-05-18 14:18:50 -0700965 if (node != need_to_print.end())
966 doit = node->second;
967
968 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -0700969 LOG(INFO) << "@@const unsigned char "
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800970 << "k" << message.command_string()
Wade Guthried6153612012-08-23 11:36:14 -0700971 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -0700972
Christopher Wileyefd521f2012-11-07 17:32:46 -0800973 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -0700974
975 size_t bytes = nlmsg_total_size(payload_bytes);
976 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -0700977 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -0700978 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -0700979 << std::hex << std::setfill('0') << std::setw(2)
980 << + rawdata[i] << ",";
981 }
Wade Guthried6153612012-08-23 11:36:14 -0700982 LOG(INFO) << "};";
Wade Guthriebdcdaa72013-03-04 12:47:12 -0800983 need_to_print[message.command()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700984 }
985}
986
987} // namespace shill.