blob: 952ada6aeb0c4aace8a53b360bc6d891a335a571 [file] [log] [blame]
Wade Guthrie0d438532012-05-18 14:18:50 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This code is derived from the 'iw' source code. The copyright and license
6// of that code is as follows:
7//
8// Copyright (c) 2007, 2008 Johannes Berg
9// Copyright (c) 2007 Andy Lutomirski
10// Copyright (c) 2007 Mike Kershaw
11// Copyright (c) 2008-2009 Luis R. Rodriguez
12//
13// Permission to use, copy, modify, and/or distribute this software for any
14// purpose with or without fee is hereby granted, provided that the above
15// copyright notice and this permission notice appear in all copies.
16//
17// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
repo syncdc085c82012-12-28 08:54:41 -080025#include "shill/nl80211_message.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070026
27#include <ctype.h>
28#include <endian.h>
29#include <errno.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070030#include <linux/nl80211.h>
31#include <net/if.h>
Wade Guthrie8343f7f2012-12-04 13:52:32 -080032#include <netinet/in.h>
Wade Guthrie0d438532012-05-18 14:18:50 -070033#include <netlink/attr.h>
34#include <netlink/genl/ctrl.h>
35#include <netlink/genl/family.h>
36#include <netlink/genl/genl.h>
37#include <netlink/msg.h>
38#include <netlink/netlink.h>
39
Wade Guthrie8e278612013-02-26 10:32:34 -080040#include <algorithm>
Wade Guthrie0d438532012-05-18 14:18:50 -070041#include <iomanip>
42#include <string>
43
44#include <base/format_macros.h>
45#include <base/stl_util.h>
46#include <base/stringprintf.h>
47
repo sync90ee0fa2012-12-18 10:08:08 -080048#include "shill/attribute_list.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070049#include "shill/ieee80211.h"
50#include "shill/logging.h"
repo syncdc085c82012-12-28 08:54:41 -080051#include "shill/netlink_socket.h"
repo sync1538d442012-12-20 15:24:35 -080052#include "shill/nl80211_attribute.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070053#include "shill/scope_logger.h"
Darin Petkov50cb78a2013-02-06 16:17:49 +010054#include "shill/wifi.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070055
56using base::LazyInstance;
57using base::StringAppendF;
58using base::StringPrintf;
59using std::map;
Wade Guthrie8e278612013-02-26 10:32:34 -080060using std::min;
Wade Guthrie0d438532012-05-18 14:18:50 -070061using std::string;
62using std::vector;
63
64namespace shill {
65
66namespace {
repo syncdc085c82012-12-28 08:54:41 -080067LazyInstance<Nl80211MessageDataCollector> g_datacollector =
Wade Guthrie0d438532012-05-18 14:18:50 -070068 LAZY_INSTANCE_INITIALIZER;
69} // namespace
70
repo syncdc085c82012-12-28 08:54:41 -080071const char Nl80211Message::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX";
Wade Guthrie0d438532012-05-18 14:18:50 -070072
73const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
74const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
75
repo syncdc085c82012-12-28 08:54:41 -080076const uint32_t Nl80211Message::kIllegalMessage = 0;
77const unsigned int Nl80211Message::kEthernetAddressBytes = 6;
78map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
79map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
Wade Guthrie0d438532012-05-18 14:18:50 -070080
81// The nl messages look like this:
82//
83// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
84// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
85// +-- gnhl nlmsg_tail(hdr) --+
86// | nlmsg_next(hdr) --+
87// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
88// -----+-----+-+----------------------------------------------+-++----
89// ... | | | payload | ||
90// | | +------+-+--------+-+--------------------------+ ||
91// | nl | | | | | | attribs | ||
92// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
93// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
94// | |d| hdr |d| |d| attr |a| load |a| ... |d||
95// | | | | | | | |d| |d| | ||
96// -----+-----+-+----------------------------------------------+-++----
97// ^ ^ ^ ^
98// | | | XXXXXXX <-- nla_len(nlattr)
99// | | | +-- nla_data(nlattr)
100// | | X-nla_total_size-X
101// | | XXXXX-nlmsg_attrlen-XXXXXX
102// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
103// +-- msg = nlmsg_hdr(raw_message)
104
105//
repo syncdc085c82012-12-28 08:54:41 -0800106// Nl80211Message
Wade Guthrie0d438532012-05-18 14:18:50 -0700107//
108
Wade Guthrie8e278612013-02-26 10:32:34 -0800109void Nl80211Message::Print(int log_level) const {
110 SLOG(WiFi, log_level) << StringPrintf("Message %s (%d)",
111 message_type_string(),
112 message_type());
113 attributes_.Print(log_level, 1);
114}
115
repo sync0efa9f02012-12-28 13:40:20 -0800116bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) {
117 if (!const_msg) {
118 LOG(ERROR) << "Null |msg| parameter";
Wade Guthrie0d438532012-05-18 14:18:50 -0700119 return false;
120 }
121
repo sync0efa9f02012-12-28 13:40:20 -0800122 // Netlink header.
123 sequence_number_ = const_msg->nlmsg_seq;
repo syncdc085c82012-12-28 08:54:41 -0800124 SLOG(WiFi, 6) << "NL Message " << sequence_number() << " <===";
Wade Guthrie0d438532012-05-18 14:18:50 -0700125
repo sync0efa9f02012-12-28 13:40:20 -0800126 // Casting away constness, here, since the libnl code doesn't properly label
127 // their stuff as const (even though it is).
128 nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg);
129
130 // Genl message header.
131 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
132
133 // Attributes.
134 // Parse the attributes from the nl message payload into the 'tb' array.
135 nlattr *tb[NL80211_ATTR_MAX + 1];
136 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
137 genlmsg_attrlen(gnlh, 0), NULL);
138
Wade Guthrie0d438532012-05-18 14:18:50 -0700139 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
140 if (tb[i]) {
repo sync0efa9f02012-12-28 13:40:20 -0800141 // TODO(wdg): When Nl80211Messages instantiate their own attributes,
142 // this call should, instead, call |SetAttributeFromNlAttr|.
143 attributes_.CreateAndInitFromNlAttr(static_cast<enum nl80211_attrs>(i),
144 tb[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700145 }
146 }
147
148 // Convert integer values provided by libnl (for example, from the
149 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
150 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700151 if (!reason_code_string_) {
152 reason_code_string_ = new map<uint16_t, string>;
153 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
154 "Unspecified reason";
155 (*reason_code_string_)[
156 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700157 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700158 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
159 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
160 "ESS";
161 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
162 "Disassociated due to inactivity";
163 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
164 "Disassociated because AP is unable to handle all currently associated "
165 "STAs";
166 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
167 "Class 2 frame received from nonauthenticated STA";
168 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
169 "Class 3 frame received from nonassociated STA";
170 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
171 "Disassociated because sending STA is leaving (or has left) BSS";
172 (*reason_code_string_)[
173 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
174 "STA requesting (re)association is not authenticated with responding "
175 "STA";
176 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
177 "Disassociated because the information in the Power Capability "
178 "element is unacceptable";
179 (*reason_code_string_)[
180 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
181 "Disassociated because the information in the Supported Channels "
182 "element is unacceptable";
183 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
184 "Invalid information element, i.e., an information element defined in "
185 "this standard for which the content does not meet the specifications "
186 "in Clause 7";
187 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
188 "Message integrity code (MIC) failure";
189 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
190 "4-Way Handshake timeout";
191 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
192 "Group Key Handshake timeout";
193 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
194 "Information element in 4-Way Handshake different from "
195 "(Re)Association Request/Probe Response/Beacon frame";
196 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
197 "Invalid group cipher";
198 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
199 "Invalid pairwise cipher";
200 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
201 "Invalid AKMP";
202 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
203 "Unsupported RSN information element version";
204 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
205 "Invalid RSN information element capabilities";
206 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
207 "IEEE 802.1X authentication failed";
208 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
209 "Cipher suite rejected because of the security policy";
210 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
211 "Disassociated for unspecified, QoS-related reason";
212 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
213 "Disassociated because QoS AP lacks sufficient bandwidth for this "
214 "QoS STA";
215 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
216 "Disassociated because excessive number of frames need to be "
217 "acknowledged, but are not acknowledged due to AP transmissions "
218 "and/or poor channel conditions";
219 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
220 "Disassociated because STA is transmitting outside the limits of its "
221 "TXOPs";
222 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
223 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
224 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
225 "Requested from peer STA as it does not want to use the mechanism";
226 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
227 "Requested from peer STA as the STA received frames using the "
228 "mechanism for which a setup is required";
229 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
230 "Requested from peer STA due to timeout";
231 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
232 "Peer STA does not support the requested cipher suite";
233 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
234 }
235
236 if (!status_code_string_) {
237 status_code_string_ = new map<uint16_t, string>;
238 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
239 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
240 "Unspecified failure";
241 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700242 "Cannot support all requested capabilities in the capability "
243 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700244 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700245 "Reassociation denied due to inability to confirm that association "
246 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700247 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700248 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700249 (*status_code_string_)[
250 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700251 "Responding station does not support the specified authentication "
252 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700253 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700254 "Received an authentication frame with authentication transaction "
255 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700256 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700257 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700258 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700259 "Authentication rejected due to timeout waiting for next frame in "
260 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700261 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
262 "Association denied because AP is unable to handle additional "
263 "associated STA";
264 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700265 "Association denied due to requesting station not supporting all of "
266 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700267 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700268 "Association denied due to requesting station not supporting the "
269 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700270 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700271 "Association denied due to requesting station not supporting the PBCC "
272 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700273 (*status_code_string_)[
274 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700275 "Association denied due to requesting station not supporting the "
276 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700277 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700278 "Association request rejected because Spectrum Management capability "
279 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700280 (*status_code_string_)[
281 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700282 "Association request rejected because the information in the Power "
283 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700284 (*status_code_string_)[
285 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700286 "Association request rejected because the information in the "
287 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700288 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700289 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700290 "supporting the Short Slot Time option";
291 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
292 "Association request rejected due to requesting station not "
293 "supporting the DSSS-OFDM option";
294 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700295 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700296 (*status_code_string_)[
297 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700298 "Association denied due to QAP having insufficient bandwidth to handle "
299 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700300 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700301 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700302 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
303 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700304 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700305 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
306 "The request has been declined";
307 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700308 "The request has not been successful as one or more parameters have "
309 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700310 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
311 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700312 "However, a suggested Tspec is provided so that the initiating QSTA "
313 "may attempt to send another TS with the suggested changes to the "
314 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700315 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700316 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700317 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
318 "Invalid Group Cipher";
319 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
320 "Invalid Pairwise Cipher";
321 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
322 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
323 "Unsupported RSN Information Element version";
324 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
325 "Invalid RSN Information Element Capabilities";
326 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700327 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700328 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
329 "The TS has not been created. However, the HC may be capable of "
330 "creating a TS, in response to a request, after the time indicated in "
331 "the TS Delay element";
332 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700333 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700334 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
335 "Destination STA is not present within this BSS";
336 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
337 "The destination STA is not a QoS STA";
338 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700339 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700340 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700341 }
342
343 return true;
344}
345
Wade Guthrie8e278612013-02-26 10:32:34 -0800346// static
347void Nl80211Message::PrintBytes(int log_level, const unsigned char *buf,
348 size_t num_bytes) {
349 SLOG(WiFi, log_level) << "Netlink Message -- Examining Bytes";
350 if (!buf) {
351 SLOG(WiFi, log_level) << "<NULL Buffer>";
352 return;
353 }
354
355 if (num_bytes >= sizeof(nlmsghdr)) {
356 const nlmsghdr *header = reinterpret_cast<const nlmsghdr *>(buf);
357 SLOG(WiFi, log_level) << StringPrintf(
358 "len: %02x %02x %02x %02x = %u bytes",
359 buf[0], buf[1], buf[2], buf[3], header->nlmsg_len);
360
361 SLOG(WiFi, log_level) << StringPrintf(
362 "type | flags: %02x %02x %02x %02x - type:%u flags:%s%s%s%s%s",
363 buf[4], buf[5], buf[6], buf[7], header->nlmsg_type,
364 ((header->nlmsg_flags & NLM_F_REQUEST) ? " REQUEST" : ""),
365 ((header->nlmsg_flags & NLM_F_MULTI) ? " MULTI" : ""),
366 ((header->nlmsg_flags & NLM_F_ACK) ? " ACK" : ""),
367 ((header->nlmsg_flags & NLM_F_ECHO) ? " ECHO" : ""),
368 ((header->nlmsg_flags & NLM_F_DUMP_INTR) ? " BAD-SEQ" : ""));
369
370 SLOG(WiFi, log_level) << StringPrintf(
371 "sequence: %02x %02x %02x %02x = %u",
372 buf[8], buf[9], buf[10], buf[11], header->nlmsg_seq);
373 SLOG(WiFi, log_level) << StringPrintf(
374 "pid: %02x %02x %02x %02x = %u",
375 buf[12], buf[13], buf[14], buf[15], header->nlmsg_pid);
376 buf += sizeof(nlmsghdr);
377 num_bytes -= sizeof(nlmsghdr);
378 } else {
379 SLOG(WiFi, log_level) << "Not enough bytes (" << num_bytes
380 << ") for a complete nlmsghdr (requires "
381 << sizeof(nlmsghdr) << ").";
382 }
383
384 while (num_bytes) {
385 string output;
386 size_t bytes_this_row = min(num_bytes, static_cast<size_t>(32));
387 for (size_t i = 0; i < bytes_this_row; ++i) {
388 StringAppendF(&output, " %02x", *buf++);
389 }
390 SLOG(WiFi, log_level) << output;
391 num_bytes -= bytes_this_row;
392 }
393}
394
Wade Guthrie0d438532012-05-18 14:18:50 -0700395// Helper function to provide a string for a MAC address.
repo syncdc085c82012-12-28 08:54:41 -0800396bool Nl80211Message::GetMacAttributeString(nl80211_attrs id,
397 string *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700398 if (!value) {
399 LOG(ERROR) << "Null |value| parameter";
400 return false;
401 }
402
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800403 ByteString data;
repo sync12cca802012-12-19 17:34:22 -0800404 if (!attributes().GetRawAttributeValue(id, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700405 value->assign(kBogusMacAddress);
406 return false;
407 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800408 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700409
410 return true;
411}
412
413// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
repo syncdc085c82012-12-28 08:54:41 -0800414bool Nl80211Message::GetScanFrequenciesAttribute(
repo sync12cca802012-12-19 17:34:22 -0800415 nl80211_attrs id, vector<uint32_t> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700416 if (!value) {
417 LOG(ERROR) << "Null |value| parameter";
418 return false;
419 }
420
421 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800422 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800423 if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800424 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700425
repo syncd316eb72012-12-10 15:48:47 -0800426 nlattr *nst = NULL;
427 // |nla_for_each_attr| requires a non-const parameter even though it
428 // doesn't change the data.
429 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
430 int rem_nst;
431 int len = rawdata.GetLength();
432
433 nla_for_each_attr(nst, attr_data, len, rem_nst) {
434 value->push_back(Nl80211Attribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700435 }
repo syncd316eb72012-12-10 15:48:47 -0800436 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700437}
438
439// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
repo syncdc085c82012-12-28 08:54:41 -0800440bool Nl80211Message::GetScanSsidsAttribute(
441 nl80211_attrs id, vector<string> *value) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700442 if (!value) {
443 LOG(ERROR) << "Null |value| parameter";
444 return false;
445 }
446
repo sync90ee0fa2012-12-18 10:08:08 -0800447 ByteString rawdata;
repo sync12cca802012-12-19 17:34:22 -0800448 if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty())
repo sync90ee0fa2012-12-18 10:08:08 -0800449 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700450
repo sync90ee0fa2012-12-18 10:08:08 -0800451 nlattr *nst = NULL;
452 // |nla_for_each_attr| requires a non-const parameter even though it
453 // doesn't change the data.
454 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
455 int rem_nst;
456 int len = rawdata.GetLength();
457
458 nla_for_each_attr(nst, data, len, rem_nst) {
459 value->push_back(StringFromSsid(nla_len(nst),
460 reinterpret_cast<const uint8_t *>(
461 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700462 }
repo sync90ee0fa2012-12-18 10:08:08 -0800463 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700464}
465
Wade Guthrie0d438532012-05-18 14:18:50 -0700466// Protected members.
467
repo syncdc085c82012-12-28 08:54:41 -0800468string Nl80211Message::GetHeaderString() const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700469 char ifname[IF_NAMESIZE] = "";
470 uint32_t ifindex = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800471 bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
472 &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700473
474 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800475 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
476 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700477
478 string output;
479 if (ifindex_exists && wifi_exists) {
480 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
481 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
482 wifi);
483 } else if (ifindex_exists) {
484 StringAppendF(&output, "%s: ",
485 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
486 } else if (wifi_exists) {
487 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
488 }
489
490 return output;
491}
492
repo syncdc085c82012-12-28 08:54:41 -0800493string Nl80211Message::StringFromFrame(nl80211_attrs attr_name) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700494 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800495 ByteString frame_data;
repo sync90ee0fa2012-12-18 10:08:08 -0800496 if (attributes().GetRawAttributeValue(attr_name,
497 &frame_data) && !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800498 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700499 frame.ToString(&output);
500 } else {
501 output.append(" [no frame]");
502 }
503
504 return output;
505}
506
507// static
repo syncdc085c82012-12-28 08:54:41 -0800508string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700509 switch (key_type) {
510 case NL80211_KEYTYPE_GROUP:
511 return "Group";
512 case NL80211_KEYTYPE_PAIRWISE:
513 return "Pairwise";
514 case NL80211_KEYTYPE_PEERKEY:
515 return "PeerKey";
516 default:
517 return "<Unknown Key Type>";
518 }
519}
520
521// static
repo syncdc085c82012-12-28 08:54:41 -0800522string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700523 string output;
524
525 if (!arg) {
526 output = kBogusMacAddress;
527 LOG(ERROR) << "|arg| parameter is NULL.";
528 } else {
529 StringAppendF(&output, "%02x", arg[0]);
530
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800531 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700532 StringAppendF(&output, ":%02x", arg[i]);
533 }
534 }
535 return output;
536}
537
538// static
repo syncdc085c82012-12-28 08:54:41 -0800539string Nl80211Message::StringFromRegInitiator(__u8 initiator) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700540 switch (initiator) {
541 case NL80211_REGDOM_SET_BY_CORE:
542 return "the wireless core upon initialization";
543 case NL80211_REGDOM_SET_BY_USER:
544 return "a user";
545 case NL80211_REGDOM_SET_BY_DRIVER:
546 return "a driver";
547 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
548 return "a country IE";
549 default:
550 return "<Unknown Reg Initiator>";
551 }
552}
553
554// static
repo syncdc085c82012-12-28 08:54:41 -0800555string Nl80211Message::StringFromSsid(const uint8_t len,
Wade Guthrie0d438532012-05-18 14:18:50 -0700556 const uint8_t *data) {
557 string output;
558 if (!data) {
559 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
560 LOG(ERROR) << "|data| parameter is NULL.";
561 return output;
562 }
563
564 for (int i = 0; i < len; ++i) {
565 if (data[i] == ' ')
566 output.append(" ");
567 else if (isprint(data[i]))
568 StringAppendF(&output, "%c", static_cast<char>(data[i]));
569 else
570 StringAppendF(&output, "\\x%2x", data[i]);
571 }
572
573 return output;
574}
575
576// static
repo syncdc085c82012-12-28 08:54:41 -0800577string Nl80211Message::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700578 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700579 match = reason_code_string_->find(status);
580 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700581 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700582 if (status < IEEE_80211::kReasonCodeMax) {
583 StringAppendF(&output, "<Reserved Reason:%u>", status);
584 } else {
585 StringAppendF(&output, "<Unknown Reason:%u>", status);
586 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700587 return output;
588 }
589 return match->second;
590}
591
Wade Guthried4977f22012-08-22 12:37:54 -0700592// static
repo syncdc085c82012-12-28 08:54:41 -0800593string Nl80211Message::StringFromStatus(uint16_t status) {
Wade Guthried4977f22012-08-22 12:37:54 -0700594 map<uint16_t, string>::const_iterator match;
595 match = status_code_string_->find(status);
596 if (match == status_code_string_->end()) {
597 string output;
598 if (status < IEEE_80211::kStatusCodeMax) {
599 StringAppendF(&output, "<Reserved Status:%u>", status);
600 } else {
601 StringAppendF(&output, "<Unknown Status:%u>", status);
602 }
603 return output;
604 }
605 return match->second;
606}
607
repo syncdc085c82012-12-28 08:54:41 -0800608ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const {
609 // Build netlink header.
610 nlmsghdr header;
611 size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header));
612 header.nlmsg_len = nlmsghdr_with_pad;
613 header.nlmsg_type = nlmsg_type;
614 header.nlmsg_flags = NLM_F_REQUEST;
615 header.nlmsg_seq = sequence_number();
616 header.nlmsg_pid = getpid();
617
618 // Build genl message header.
619 genlmsghdr genl_header;
620 size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header));
621 header.nlmsg_len += genlmsghdr_with_pad;
622 genl_header.cmd = message_type();
623 genl_header.version = 1;
624 genl_header.reserved = 0;
625
626 // Assemble attributes (padding is included by AttributeList::Encode).
627 ByteString attributes = attributes_.Encode();
628 header.nlmsg_len += attributes.GetLength();
629
630 // Now that we know the total message size, build the output ByteString.
631 ByteString result;
632
633 // Netlink header + pad.
634 result.Append(ByteString(reinterpret_cast<unsigned char *>(&header),
635 sizeof(header)));
636 result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any).
637
638 // Genl message header + pad.
639 result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header),
640 sizeof(genl_header)));
641 result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill.
642
643 // Attributes including pad.
644 result.Append(attributes);
645
646 return result;
647}
648
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800649Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700650 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800651 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700652 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800653 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
654 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700655
656 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800657 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700658 mac_from_ =
repo syncdc085c82012-12-28 08:54:41 -0800659 Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
660 mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700661 frame_type_ = frame->frame_control & kFrameTypeMask;
662
663 switch (frame_type_) {
664 case kAssocResponseFrameType:
665 case kReassocResponseFrameType:
666 status_ = le16toh(frame->u.associate_response.status_code);
667 break;
668
669 case kAuthFrameType:
670 status_ = le16toh(frame->u.authentiate_message.status_code);
671 break;
672
673 case kDisassocFrameType:
674 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700675 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700676 break;
677
678 default:
679 break;
680 }
681 }
682}
683
Wade Guthried4977f22012-08-22 12:37:54 -0700684bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700685 if (!output) {
686 LOG(ERROR) << "NULL |output|";
687 return false;
688 }
689
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800690 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700691 output->append(" [no frame]");
692 return true;
693 }
694
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800695 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700696 output->append(" [invalid frame: ");
697 } else {
698 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
699
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800700 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700701 case kAssocResponseFrameType:
702 StringAppendF(output, "; AssocResponse status: %u: %s",
703 status_,
repo syncdc085c82012-12-28 08:54:41 -0800704 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700705 break;
706 case kReassocResponseFrameType:
707 StringAppendF(output, "; ReassocResponse status: %u: %s",
708 status_,
repo syncdc085c82012-12-28 08:54:41 -0800709 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700710 break;
711 case kAuthFrameType:
712 StringAppendF(output, "; Auth status: %u: %s",
713 status_,
repo syncdc085c82012-12-28 08:54:41 -0800714 Nl80211Message::StringFromStatus(status_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700715 break;
716
717 case kDisassocFrameType:
718 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700719 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800720 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700721 break;
722 case kDeauthFrameType:
723 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700724 reason_,
repo syncdc085c82012-12-28 08:54:41 -0800725 Nl80211Message::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700726 break;
727
728 default:
729 break;
730 }
731 output->append(" [frame: ");
732 }
733
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800734 const unsigned char *frame = frame_.GetConstData();
735 for (size_t i = 0; i < frame_.GetLength(); ++i) {
736 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700737 }
738 output->append("]");
739
740 return true;
741}
742
Wade Guthried4977f22012-08-22 12:37:54 -0700743bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800744 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700745}
746
Wade Guthried4977f22012-08-22 12:37:54 -0700747
Wade Guthrie0d438532012-05-18 14:18:50 -0700748//
repo syncdc085c82012-12-28 08:54:41 -0800749// Specific Nl80211Message types.
Wade Guthrie0d438532012-05-18 14:18:50 -0700750//
751
repo sync0efa9f02012-12-28 13:40:20 -0800752// An Ack is not a GENL message and, as such, has no command.
753const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC;
754const char AckMessage::kCommandString[] = "NL80211_ACK";
755
Wade Guthrie0d438532012-05-18 14:18:50 -0700756const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
757const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
758
Wade Guthrie0d438532012-05-18 14:18:50 -0700759const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
760const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
761
Wade Guthrie0d438532012-05-18 14:18:50 -0700762const uint8_t CancelRemainOnChannelMessage::kCommand =
763 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
764const char CancelRemainOnChannelMessage::kCommandString[] =
765 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
766
Wade Guthrie0d438532012-05-18 14:18:50 -0700767const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
768const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
769
Wade Guthrie0d438532012-05-18 14:18:50 -0700770const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
771const char DeauthenticateMessage::kCommandString[] =
772 "NL80211_CMD_DEAUTHENTICATE";
773
Wade Guthrie0d438532012-05-18 14:18:50 -0700774const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
775const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
776
Wade Guthrie0d438532012-05-18 14:18:50 -0700777const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
778const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
779
Wade Guthrie0d438532012-05-18 14:18:50 -0700780const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
781const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
782
repo sync0efa9f02012-12-28 13:40:20 -0800783// An Error is not a GENL message and, as such, has no command.
784const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC;
785const char ErrorMessage::kCommandString[] = "NL80211_ERROR";
786
787ErrorMessage::ErrorMessage(uint32_t error)
788 : Nl80211Message(kCommand, kCommandString), error_(error) {}
789
790string ErrorMessage::ToString() const {
791 string output;
792 StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s",
793 error_, strerror(error_));
794 return output;
795}
796
Wade Guthrie0d438532012-05-18 14:18:50 -0700797const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
798const char FrameTxStatusMessage::kCommandString[] =
799 "NL80211_CMD_FRAME_TX_STATUS";
800
repo sync0efa9f02012-12-28 13:40:20 -0800801const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG;
802const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG";
803
Wade Guthrie0d438532012-05-18 14:18:50 -0700804const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
805const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
806
Wade Guthrie0d438532012-05-18 14:18:50 -0700807const uint8_t MichaelMicFailureMessage::kCommand =
808 NL80211_CMD_MICHAEL_MIC_FAILURE;
809const char MichaelMicFailureMessage::kCommandString[] =
810 "NL80211_CMD_MICHAEL_MIC_FAILURE";
811
Wade Guthrie0d438532012-05-18 14:18:50 -0700812const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
813const char NewScanResultsMessage::kCommandString[] =
814 "NL80211_CMD_NEW_SCAN_RESULTS";
815
Wade Guthrie0d438532012-05-18 14:18:50 -0700816const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
817const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
818
Wade Guthrie0d438532012-05-18 14:18:50 -0700819const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
820const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
821
repo sync0efa9f02012-12-28 13:40:20 -0800822// A NOOP is not a GENL message and, as such, has no command.
823const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC;
824const char NoopMessage::kCommandString[] = "NL80211_NOOP";
825
Wade Guthrie0d438532012-05-18 14:18:50 -0700826const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
827const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
828
Wade Guthrie0d438532012-05-18 14:18:50 -0700829const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
830const char PmksaCandidateMessage::kCommandString[] =
831 "NL80211_ATTR_PMKSA_CANDIDATE";
832
Wade Guthrie0d438532012-05-18 14:18:50 -0700833const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
834const char RegBeaconHintMessage::kCommandString[] =
835 "NL80211_CMD_REG_BEACON_HINT";
836
Wade Guthrie0d438532012-05-18 14:18:50 -0700837const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
838const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
839
Wade Guthrie0d438532012-05-18 14:18:50 -0700840const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
841const char RemainOnChannelMessage::kCommandString[] =
842 "NL80211_CMD_REMAIN_ON_CHANNEL";
843
Wade Guthrie0d438532012-05-18 14:18:50 -0700844const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
845const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
846
Wade Guthrie0d438532012-05-18 14:18:50 -0700847const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
848const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
849
Wade Guthrie0d438532012-05-18 14:18:50 -0700850const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
851const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
852
Wade Guthrie0d438532012-05-18 14:18:50 -0700853const uint8_t UnknownMessage::kCommand = 0xff;
854const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
855
Wade Guthrie0d438532012-05-18 14:18:50 -0700856const uint8_t UnprotDeauthenticateMessage::kCommand =
857 NL80211_CMD_UNPROT_DEAUTHENTICATE;
858const char UnprotDeauthenticateMessage::kCommandString[] =
859 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
860
Wade Guthrie0d438532012-05-18 14:18:50 -0700861const uint8_t UnprotDisassociateMessage::kCommand =
862 NL80211_CMD_UNPROT_DISASSOCIATE;
863const char UnprotDisassociateMessage::kCommandString[] =
864 "NL80211_CMD_UNPROT_DISASSOCIATE";
865
Wade Guthrie0d438532012-05-18 14:18:50 -0700866//
867// Factory class.
868//
869
repo syncdc085c82012-12-28 08:54:41 -0800870Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700871 if (!msg) {
872 LOG(ERROR) << "NULL |msg| parameter";
873 return NULL;
874 }
875
repo syncdc085c82012-12-28 08:54:41 -0800876 scoped_ptr<Nl80211Message> message;
repo sync0efa9f02012-12-28 13:40:20 -0800877 void *payload = nlmsg_data(msg);
repo syncdc085c82012-12-28 08:54:41 -0800878
repo sync0efa9f02012-12-28 13:40:20 -0800879 if (msg->nlmsg_type == NLMSG_NOOP) {
880 SLOG(WiFi, 6) << "Creating a NOP message";
881 message.reset(new NoopMessage());
882 } else if (msg->nlmsg_type == NLMSG_ERROR) {
883 uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload));
884 if (error_code) {
885 SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code;
886 message.reset(new ErrorMessage(error_code));
887 } else {
888 SLOG(WiFi, 6) << "Creating an ACK message";
889 message.reset(new AckMessage());
890 }
891 } else {
892 SLOG(WiFi, 6) << "Creating a Regular message";
893 genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload);
Wade Guthrie0d438532012-05-18 14:18:50 -0700894
repo sync0efa9f02012-12-28 13:40:20 -0800895 switch (gnlh->cmd) {
896 case AssociateMessage::kCommand:
897 message.reset(new AssociateMessage()); break;
898 case AuthenticateMessage::kCommand:
899 message.reset(new AuthenticateMessage()); break;
900 case CancelRemainOnChannelMessage::kCommand:
901 message.reset(new CancelRemainOnChannelMessage()); break;
902 case ConnectMessage::kCommand:
903 message.reset(new ConnectMessage()); break;
904 case DeauthenticateMessage::kCommand:
905 message.reset(new DeauthenticateMessage()); break;
906 case DeleteStationMessage::kCommand:
907 message.reset(new DeleteStationMessage()); break;
908 case DisassociateMessage::kCommand:
909 message.reset(new DisassociateMessage()); break;
910 case DisconnectMessage::kCommand:
911 message.reset(new DisconnectMessage()); break;
912 case FrameTxStatusMessage::kCommand:
913 message.reset(new FrameTxStatusMessage()); break;
914 case GetRegMessage::kCommand:
915 message.reset(new GetRegMessage()); break;
916 case JoinIbssMessage::kCommand:
917 message.reset(new JoinIbssMessage()); break;
918 case MichaelMicFailureMessage::kCommand:
919 message.reset(new MichaelMicFailureMessage()); break;
920 case NewScanResultsMessage::kCommand:
921 message.reset(new NewScanResultsMessage()); break;
922 case NewStationMessage::kCommand:
923 message.reset(new NewStationMessage()); break;
924 case NewWifiMessage::kCommand:
925 message.reset(new NewWifiMessage()); break;
926 case NotifyCqmMessage::kCommand:
927 message.reset(new NotifyCqmMessage()); break;
928 case PmksaCandidateMessage::kCommand:
929 message.reset(new PmksaCandidateMessage()); break;
930 case RegBeaconHintMessage::kCommand:
931 message.reset(new RegBeaconHintMessage()); break;
932 case RegChangeMessage::kCommand:
933 message.reset(new RegChangeMessage()); break;
934 case RemainOnChannelMessage::kCommand:
935 message.reset(new RemainOnChannelMessage()); break;
936 case RoamMessage::kCommand:
937 message.reset(new RoamMessage()); break;
938 case ScanAbortedMessage::kCommand:
939 message.reset(new ScanAbortedMessage()); break;
940 case TriggerScanMessage::kCommand:
941 message.reset(new TriggerScanMessage()); break;
942 case UnprotDeauthenticateMessage::kCommand:
943 message.reset(new UnprotDeauthenticateMessage()); break;
944 case UnprotDisassociateMessage::kCommand:
945 message.reset(new UnprotDisassociateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -0700946
repo sync0efa9f02012-12-28 13:40:20 -0800947 default:
948 message.reset(new UnknownMessage(gnlh->cmd)); break;
949 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700950
repo sync0efa9f02012-12-28 13:40:20 -0800951 if (!message->InitFromNlmsg(msg)) {
952 LOG(ERROR) << "Message did not initialize properly";
953 return NULL;
954 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700955 }
956
Wade Guthrie0d438532012-05-18 14:18:50 -0700957 return message.release();
958}
959
repo syncdc085c82012-12-28 08:54:41 -0800960Nl80211MessageDataCollector *
961 Nl80211MessageDataCollector::GetInstance() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700962 return g_datacollector.Pointer();
963}
964
repo syncdc085c82012-12-28 08:54:41 -0800965Nl80211MessageDataCollector::Nl80211MessageDataCollector() {
Wade Guthrie0d438532012-05-18 14:18:50 -0700966 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
967 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
968 need_to_print[NL80211_CMD_DEL_STATION] = true;
969 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
970 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
971 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
972 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
973 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
974 need_to_print[NL80211_CMD_REG_CHANGE] = true;
975 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
976 need_to_print[NL80211_CMD_ROAM] = true;
977 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
978 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
979 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
980}
981
repo syncdc085c82012-12-28 08:54:41 -0800982void Nl80211MessageDataCollector::CollectDebugData(
983 const Nl80211Message &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700984 if (!msg) {
985 LOG(ERROR) << "NULL |msg| parameter";
986 return;
987 }
988
989 bool doit = false;
990
991 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -0800992 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -0700993 if (node != need_to_print.end())
994 doit = node->second;
995
996 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -0700997 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -0800998 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -0700999 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001000
Christopher Wileyefd521f2012-11-07 17:32:46 -08001001 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001002
1003 size_t bytes = nlmsg_total_size(payload_bytes);
1004 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001005 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001006 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001007 << std::hex << std::setfill('0') << std::setw(2)
1008 << + rawdata[i] << ",";
1009 }
Wade Guthried6153612012-08-23 11:36:14 -07001010 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001011 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001012 }
1013}
1014
1015} // namespace shill.