blob: d5a2ef5c890c599a3d10de2341ede42069e04bb3 [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
25#include "shill/user_bound_nlmessage.h"
26
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
40#include <iomanip>
41#include <string>
42
43#include <base/format_macros.h>
44#include <base/stl_util.h>
45#include <base/stringprintf.h>
46
repo sync90ee0fa2012-12-18 10:08:08 -080047#include "shill/attribute_list.h"
Wade Guthrie0d438532012-05-18 14:18:50 -070048#include "shill/ieee80211.h"
49#include "shill/logging.h"
50#include "shill/scope_logger.h"
51
52using base::LazyInstance;
53using base::StringAppendF;
54using base::StringPrintf;
55using std::map;
56using std::string;
57using std::vector;
58
59namespace shill {
60
61namespace {
62LazyInstance<UserBoundNlMessageDataCollector> g_datacollector =
63 LAZY_INSTANCE_INITIALIZER;
64} // namespace
65
Wade Guthrie0d438532012-05-18 14:18:50 -070066const char UserBoundNlMessage::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX";
67
68const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
69const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
70
71const uint32_t UserBoundNlMessage::kIllegalMessage = 0xFFFFFFFF;
Wade Guthrie8343f7f2012-12-04 13:52:32 -080072const unsigned int UserBoundNlMessage::kEthernetAddressBytes = 6;
Wade Guthried4977f22012-08-22 12:37:54 -070073map<uint16_t, string> *UserBoundNlMessage::reason_code_string_ = NULL;
74map<uint16_t, string> *UserBoundNlMessage::status_code_string_ = NULL;
Wade Guthrie0d438532012-05-18 14:18:50 -070075
76// The nl messages look like this:
77//
78// XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX
79// XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX
80// +-- gnhl nlmsg_tail(hdr) --+
81// | nlmsg_next(hdr) --+
82// v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V
83// -----+-----+-+----------------------------------------------+-++----
84// ... | | | payload | ||
85// | | +------+-+--------+-+--------------------------+ ||
86// | nl | | | | | | attribs | ||
87// | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ...
88// | hdr |a| msg |a| header |a| nl |p| pay |p| |a||
89// | |d| hdr |d| |d| attr |a| load |a| ... |d||
90// | | | | | | | |d| |d| | ||
91// -----+-----+-+----------------------------------------------+-++----
92// ^ ^ ^ ^
93// | | | XXXXXXX <-- nla_len(nlattr)
94// | | | +-- nla_data(nlattr)
95// | | X-nla_total_size-X
96// | | XXXXX-nlmsg_attrlen-XXXXXX
97// | +-- nlmsg_data(hdr) +-- nlmsg_attrdata()
98// +-- msg = nlmsg_hdr(raw_message)
99
100//
101// UserBoundNlMessage
102//
103
Wade Guthrie0d438532012-05-18 14:18:50 -0700104bool UserBoundNlMessage::Init(nlattr *tb[NL80211_ATTR_MAX + 1],
105 nlmsghdr *msg) {
106 if (!tb) {
107 LOG(ERROR) << "Null |tb| parameter";
108 return false;
109 }
110
111 message_ = msg;
112
113 SLOG(WiFi, 6) << "NL Message " << GetId() << " <===";
114
115 for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) {
116 if (tb[i]) {
repo sync90ee0fa2012-12-18 10:08:08 -0800117 attributes_.CreateAndInitFromNlAttr(static_cast<enum nl80211_attrs>(i),
repo syncd316eb72012-12-10 15:48:47 -0800118 tb[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700119 }
120 }
121
122 // Convert integer values provided by libnl (for example, from the
123 // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into
124 // strings describing the status.
Wade Guthried4977f22012-08-22 12:37:54 -0700125 if (!reason_code_string_) {
126 reason_code_string_ = new map<uint16_t, string>;
127 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] =
128 "Unspecified reason";
129 (*reason_code_string_)[
130 IEEE_80211::kReasonCodePreviousAuthenticationInvalid] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700131 "Previous authentication no longer valid";
Wade Guthried4977f22012-08-22 12:37:54 -0700132 (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] =
133 "Deauthentcated because sending STA is leaving (or has left) IBSS or "
134 "ESS";
135 (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] =
136 "Disassociated due to inactivity";
137 (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] =
138 "Disassociated because AP is unable to handle all currently associated "
139 "STAs";
140 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] =
141 "Class 2 frame received from nonauthenticated STA";
142 (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] =
143 "Class 3 frame received from nonassociated STA";
144 (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] =
145 "Disassociated because sending STA is leaving (or has left) BSS";
146 (*reason_code_string_)[
147 IEEE_80211::kReasonCodeReassociationNotAuthenticated] =
148 "STA requesting (re)association is not authenticated with responding "
149 "STA";
150 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] =
151 "Disassociated because the information in the Power Capability "
152 "element is unacceptable";
153 (*reason_code_string_)[
154 IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] =
155 "Disassociated because the information in the Supported Channels "
156 "element is unacceptable";
157 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] =
158 "Invalid information element, i.e., an information element defined in "
159 "this standard for which the content does not meet the specifications "
160 "in Clause 7";
161 (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] =
162 "Message integrity code (MIC) failure";
163 (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] =
164 "4-Way Handshake timeout";
165 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] =
166 "Group Key Handshake timeout";
167 (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] =
168 "Information element in 4-Way Handshake different from "
169 "(Re)Association Request/Probe Response/Beacon frame";
170 (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] =
171 "Invalid group cipher";
172 (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] =
173 "Invalid pairwise cipher";
174 (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] =
175 "Invalid AKMP";
176 (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] =
177 "Unsupported RSN information element version";
178 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] =
179 "Invalid RSN information element capabilities";
180 (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] =
181 "IEEE 802.1X authentication failed";
182 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] =
183 "Cipher suite rejected because of the security policy";
184 (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] =
185 "Disassociated for unspecified, QoS-related reason";
186 (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] =
187 "Disassociated because QoS AP lacks sufficient bandwidth for this "
188 "QoS STA";
189 (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] =
190 "Disassociated because excessive number of frames need to be "
191 "acknowledged, but are not acknowledged due to AP transmissions "
192 "and/or poor channel conditions";
193 (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] =
194 "Disassociated because STA is transmitting outside the limits of its "
195 "TXOPs";
196 (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] =
197 "Requested from peer STA as the STA is leaving the BSS (or resetting)";
198 (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] =
199 "Requested from peer STA as it does not want to use the mechanism";
200 (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] =
201 "Requested from peer STA as the STA received frames using the "
202 "mechanism for which a setup is required";
203 (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] =
204 "Requested from peer STA due to timeout";
205 (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] =
206 "Peer STA does not support the requested cipher suite";
207 (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>";
208 }
209
210 if (!status_code_string_) {
211 status_code_string_ = new map<uint16_t, string>;
212 (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful";
213 (*status_code_string_)[IEEE_80211::kStatusCodeFailure] =
214 "Unspecified failure";
215 (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700216 "Cannot support all requested capabilities in the capability "
217 "information field";
Wade Guthried4977f22012-08-22 12:37:54 -0700218 (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700219 "Reassociation denied due to inability to confirm that association "
220 "exists";
Wade Guthried4977f22012-08-22 12:37:54 -0700221 (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700222 "Association denied due to reason outside the scope of this standard";
Wade Guthried4977f22012-08-22 12:37:54 -0700223 (*status_code_string_)[
224 IEEE_80211::kStatusCodeAuthenticationUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700225 "Responding station does not support the specified authentication "
226 "algorithm";
Wade Guthried4977f22012-08-22 12:37:54 -0700227 (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700228 "Received an authentication frame with authentication transaction "
229 "sequence number out of expected sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700230 (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700231 "Authentication rejected because of challenge failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700232 (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700233 "Authentication rejected due to timeout waiting for next frame in "
234 "sequence";
Wade Guthried4977f22012-08-22 12:37:54 -0700235 (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] =
236 "Association denied because AP is unable to handle additional "
237 "associated STA";
238 (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700239 "Association denied due to requesting station not supporting all of "
240 "the data rates in the BSSBasicRateSet parameter";
Wade Guthried4977f22012-08-22 12:37:54 -0700241 (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700242 "Association denied due to requesting station not supporting the "
243 "short preamble option";
Wade Guthried4977f22012-08-22 12:37:54 -0700244 (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700245 "Association denied due to requesting station not supporting the PBCC "
246 "modulation option";
Wade Guthried4977f22012-08-22 12:37:54 -0700247 (*status_code_string_)[
248 IEEE_80211::kStatusCodeChannelAgilityUnsupported] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700249 "Association denied due to requesting station not supporting the "
250 "channel agility option";
Wade Guthried4977f22012-08-22 12:37:54 -0700251 (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700252 "Association request rejected because Spectrum Management capability "
253 "is required";
Wade Guthried4977f22012-08-22 12:37:54 -0700254 (*status_code_string_)[
255 IEEE_80211::kStatusCodeUnacceptablePowerCapability] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700256 "Association request rejected because the information in the Power "
257 "Capability element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700258 (*status_code_string_)[
259 IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700260 "Association request rejected because the information in the "
261 "Supported Channels element is unacceptable";
Wade Guthried4977f22012-08-22 12:37:54 -0700262 (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700263 "Association request rejected due to requesting station not "
Wade Guthried4977f22012-08-22 12:37:54 -0700264 "supporting the Short Slot Time option";
265 (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] =
266 "Association request rejected due to requesting station not "
267 "supporting the DSSS-OFDM option";
268 (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700269 "Unspecified, QoS related failure";
Wade Guthried4977f22012-08-22 12:37:54 -0700270 (*status_code_string_)[
271 IEEE_80211::kStatusCodeInsufficientBandwithForQsta] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700272 "Association denied due to QAP having insufficient bandwidth to handle "
273 "another QSTA";
Wade Guthried4977f22012-08-22 12:37:54 -0700274 (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700275 "Association denied due to poor channel conditions";
Wade Guthried4977f22012-08-22 12:37:54 -0700276 (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] =
277 "Association (with QoS BSS) denied due to requesting station not "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700278 "supporting the QoS facility";
Wade Guthried4977f22012-08-22 12:37:54 -0700279 (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] =
280 "The request has been declined";
281 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700282 "The request has not been successful as one or more parameters have "
283 "invalid values";
Wade Guthried4977f22012-08-22 12:37:54 -0700284 (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] =
285 "The TS has not been created because the request cannot be honored. "
Wade Guthrie64b4c142012-08-20 15:21:01 -0700286 "However, a suggested Tspec is provided so that the initiating QSTA "
287 "may attempt to send another TS with the suggested changes to the "
288 "TSpec";
Wade Guthried4977f22012-08-22 12:37:54 -0700289 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700290 "Invalid Information Element";
Wade Guthried4977f22012-08-22 12:37:54 -0700291 (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] =
292 "Invalid Group Cipher";
293 (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] =
294 "Invalid Pairwise Cipher";
295 (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP";
296 (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] =
297 "Unsupported RSN Information Element version";
298 (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] =
299 "Invalid RSN Information Element Capabilities";
300 (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700301 "Cipher suite is rejected per security policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700302 (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] =
303 "The TS has not been created. However, the HC may be capable of "
304 "creating a TS, in response to a request, after the time indicated in "
305 "the TS Delay element";
306 (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700307 "Direct link is not allowed in the BSS by policy";
Wade Guthried4977f22012-08-22 12:37:54 -0700308 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] =
309 "Destination STA is not present within this BSS";
310 (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] =
311 "The destination STA is not a QoS STA";
312 (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] =
Wade Guthrie64b4c142012-08-20 15:21:01 -0700313 "Association denied because Listen Interval is too large";
Wade Guthried4977f22012-08-22 12:37:54 -0700314 (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>";
Wade Guthrie0d438532012-05-18 14:18:50 -0700315 }
316
317 return true;
318}
319
Wade Guthrie0d438532012-05-18 14:18:50 -0700320uint32_t UserBoundNlMessage::GetId() const {
321 if (!message_) {
322 return kIllegalMessage;
323 }
324 return message_->nlmsg_seq;
325}
326
Wade Guthrie0d438532012-05-18 14:18:50 -0700327
328// Helper function to provide a string for a MAC address.
329bool UserBoundNlMessage::GetMacAttributeString(enum nl80211_attrs name,
330 string *value) const {
331 if (!value) {
332 LOG(ERROR) << "Null |value| parameter";
333 return false;
334 }
335
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800336 ByteString data;
repo sync90ee0fa2012-12-18 10:08:08 -0800337 if (!attributes().GetRawAttributeValue(name, &data)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700338 value->assign(kBogusMacAddress);
339 return false;
340 }
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800341 value->assign(StringFromMacAddress(data.GetConstData()));
Wade Guthrie0d438532012-05-18 14:18:50 -0700342
343 return true;
344}
345
346// Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
347bool UserBoundNlMessage::GetScanFrequenciesAttribute(
348 enum nl80211_attrs name, vector<uint32_t> *value) const {
349 if (!value) {
350 LOG(ERROR) << "Null |value| parameter";
351 return false;
352 }
353
354 value->clear();
repo syncd316eb72012-12-10 15:48:47 -0800355 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800356 if (!attributes().GetRawAttributeValue(name, &rawdata) && !rawdata.IsEmpty())
repo syncd316eb72012-12-10 15:48:47 -0800357 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700358
repo syncd316eb72012-12-10 15:48:47 -0800359 nlattr *nst = NULL;
360 // |nla_for_each_attr| requires a non-const parameter even though it
361 // doesn't change the data.
362 nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData());
363 int rem_nst;
364 int len = rawdata.GetLength();
365
366 nla_for_each_attr(nst, attr_data, len, rem_nst) {
367 value->push_back(Nl80211Attribute::NlaGetU32(nst));
Wade Guthrie0d438532012-05-18 14:18:50 -0700368 }
repo syncd316eb72012-12-10 15:48:47 -0800369 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700370}
371
372// Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS.
373bool UserBoundNlMessage::GetScanSsidsAttribute(
374 enum nl80211_attrs name, vector<string> *value) const {
375 if (!value) {
376 LOG(ERROR) << "Null |value| parameter";
377 return false;
378 }
379
repo sync90ee0fa2012-12-18 10:08:08 -0800380 ByteString rawdata;
381 if (!attributes().GetRawAttributeValue(name, &rawdata) || rawdata.IsEmpty())
382 return false;
Wade Guthrie0d438532012-05-18 14:18:50 -0700383
repo sync90ee0fa2012-12-18 10:08:08 -0800384 nlattr *nst = NULL;
385 // |nla_for_each_attr| requires a non-const parameter even though it
386 // doesn't change the data.
387 nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData());
388 int rem_nst;
389 int len = rawdata.GetLength();
390
391 nla_for_each_attr(nst, data, len, rem_nst) {
392 value->push_back(StringFromSsid(nla_len(nst),
393 reinterpret_cast<const uint8_t *>(
394 nla_data(nst))).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700395 }
repo sync90ee0fa2012-12-18 10:08:08 -0800396 return true;
Wade Guthrie0d438532012-05-18 14:18:50 -0700397}
398
Wade Guthrie0d438532012-05-18 14:18:50 -0700399// Protected members.
400
Wade Guthrie0d438532012-05-18 14:18:50 -0700401string UserBoundNlMessage::GetHeaderString() const {
402 char ifname[IF_NAMESIZE] = "";
403 uint32_t ifindex = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800404 bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX,
405 &ifindex);
Wade Guthrie0d438532012-05-18 14:18:50 -0700406
407 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800408 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
409 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -0700410
411 string output;
412 if (ifindex_exists && wifi_exists) {
413 StringAppendF(&output, "%s (phy #%" PRIu32 "): ",
414 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"),
415 wifi);
416 } else if (ifindex_exists) {
417 StringAppendF(&output, "%s: ",
418 (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"));
419 } else if (wifi_exists) {
420 StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi);
421 }
422
423 return output;
424}
425
426string UserBoundNlMessage::StringFromFrame(enum nl80211_attrs attr_name) const {
427 string output;
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800428 ByteString frame_data;
repo sync90ee0fa2012-12-18 10:08:08 -0800429 if (attributes().GetRawAttributeValue(attr_name,
430 &frame_data) && !frame_data.IsEmpty()) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800431 Nl80211Frame frame(frame_data);
Wade Guthrie0d438532012-05-18 14:18:50 -0700432 frame.ToString(&output);
433 } else {
434 output.append(" [no frame]");
435 }
436
437 return output;
438}
439
440// static
441string UserBoundNlMessage::StringFromKeyType(nl80211_key_type key_type) {
442 switch (key_type) {
443 case NL80211_KEYTYPE_GROUP:
444 return "Group";
445 case NL80211_KEYTYPE_PAIRWISE:
446 return "Pairwise";
447 case NL80211_KEYTYPE_PEERKEY:
448 return "PeerKey";
449 default:
450 return "<Unknown Key Type>";
451 }
452}
453
454// static
455string UserBoundNlMessage::StringFromMacAddress(const uint8_t *arg) {
456 string output;
457
458 if (!arg) {
459 output = kBogusMacAddress;
460 LOG(ERROR) << "|arg| parameter is NULL.";
461 } else {
462 StringAppendF(&output, "%02x", arg[0]);
463
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800464 for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700465 StringAppendF(&output, ":%02x", arg[i]);
466 }
467 }
468 return output;
469}
470
471// static
472string UserBoundNlMessage::StringFromRegInitiator(__u8 initiator) {
473 switch (initiator) {
474 case NL80211_REGDOM_SET_BY_CORE:
475 return "the wireless core upon initialization";
476 case NL80211_REGDOM_SET_BY_USER:
477 return "a user";
478 case NL80211_REGDOM_SET_BY_DRIVER:
479 return "a driver";
480 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
481 return "a country IE";
482 default:
483 return "<Unknown Reg Initiator>";
484 }
485}
486
487// static
488string UserBoundNlMessage::StringFromSsid(const uint8_t len,
489 const uint8_t *data) {
490 string output;
491 if (!data) {
492 StringAppendF(&output, "<Error from %s, NULL parameter>", __func__);
493 LOG(ERROR) << "|data| parameter is NULL.";
494 return output;
495 }
496
497 for (int i = 0; i < len; ++i) {
498 if (data[i] == ' ')
499 output.append(" ");
500 else if (isprint(data[i]))
501 StringAppendF(&output, "%c", static_cast<char>(data[i]));
502 else
503 StringAppendF(&output, "\\x%2x", data[i]);
504 }
505
506 return output;
507}
508
509// static
Wade Guthried4977f22012-08-22 12:37:54 -0700510string UserBoundNlMessage::StringFromReason(uint16_t status) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700511 map<uint16_t, string>::const_iterator match;
Wade Guthried4977f22012-08-22 12:37:54 -0700512 match = reason_code_string_->find(status);
513 if (match == reason_code_string_->end()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700514 string output;
Wade Guthried4977f22012-08-22 12:37:54 -0700515 if (status < IEEE_80211::kReasonCodeMax) {
516 StringAppendF(&output, "<Reserved Reason:%u>", status);
517 } else {
518 StringAppendF(&output, "<Unknown Reason:%u>", status);
519 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700520 return output;
521 }
522 return match->second;
523}
524
Wade Guthried4977f22012-08-22 12:37:54 -0700525// static
526string UserBoundNlMessage::StringFromStatus(uint16_t status) {
527 map<uint16_t, string>::const_iterator match;
528 match = status_code_string_->find(status);
529 if (match == status_code_string_->end()) {
530 string output;
531 if (status < IEEE_80211::kStatusCodeMax) {
532 StringAppendF(&output, "<Reserved Status:%u>", status);
533 } else {
534 StringAppendF(&output, "<Unknown Status:%u>", status);
535 }
536 return output;
537 }
538 return match->second;
539}
540
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800541Nl80211Frame::Nl80211Frame(const ByteString &raw_frame)
Wade Guthried4977f22012-08-22 12:37:54 -0700542 : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800543 frame_(raw_frame) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700544 const IEEE_80211::ieee80211_frame *frame =
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800545 reinterpret_cast<const IEEE_80211::ieee80211_frame *>(
546 frame_.GetConstData());
Wade Guthrie0d438532012-05-18 14:18:50 -0700547
548 // Now, let's populate the other stuff.
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800549 if (frame_.GetLength() >= kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700550 mac_from_ =
551 UserBoundNlMessage::StringFromMacAddress(&frame->destination_mac[0]);
552 mac_to_ = UserBoundNlMessage::StringFromMacAddress(&frame->source_mac[0]);
553 frame_type_ = frame->frame_control & kFrameTypeMask;
554
555 switch (frame_type_) {
556 case kAssocResponseFrameType:
557 case kReassocResponseFrameType:
558 status_ = le16toh(frame->u.associate_response.status_code);
559 break;
560
561 case kAuthFrameType:
562 status_ = le16toh(frame->u.authentiate_message.status_code);
563 break;
564
565 case kDisassocFrameType:
566 case kDeauthFrameType:
Wade Guthried4977f22012-08-22 12:37:54 -0700567 reason_ = le16toh(frame->u.deauthentiate_message.reason_code);
Wade Guthrie0d438532012-05-18 14:18:50 -0700568 break;
569
570 default:
571 break;
572 }
573 }
574}
575
Wade Guthried4977f22012-08-22 12:37:54 -0700576bool Nl80211Frame::ToString(string *output) const {
Wade Guthrie0d438532012-05-18 14:18:50 -0700577 if (!output) {
578 LOG(ERROR) << "NULL |output|";
579 return false;
580 }
581
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800582 if (frame_.IsEmpty()) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700583 output->append(" [no frame]");
584 return true;
585 }
586
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800587 if (frame_.GetLength() < kMinimumFrameByteCount) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700588 output->append(" [invalid frame: ");
589 } else {
590 StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str());
591
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800592 switch (frame_.GetConstData()[0] & kFrameTypeMask) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700593 case kAssocResponseFrameType:
594 StringAppendF(output, "; AssocResponse status: %u: %s",
595 status_,
596 UserBoundNlMessage::StringFromStatus(status_).c_str());
597 break;
598 case kReassocResponseFrameType:
599 StringAppendF(output, "; ReassocResponse status: %u: %s",
600 status_,
601 UserBoundNlMessage::StringFromStatus(status_).c_str());
602 break;
603 case kAuthFrameType:
604 StringAppendF(output, "; Auth status: %u: %s",
605 status_,
606 UserBoundNlMessage::StringFromStatus(status_).c_str());
607 break;
608
609 case kDisassocFrameType:
610 StringAppendF(output, "; Disassoc reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700611 reason_,
612 UserBoundNlMessage::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700613 break;
614 case kDeauthFrameType:
615 StringAppendF(output, "; Deauth reason %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700616 reason_,
617 UserBoundNlMessage::StringFromReason(reason_).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700618 break;
619
620 default:
621 break;
622 }
623 output->append(" [frame: ");
624 }
625
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800626 const unsigned char *frame = frame_.GetConstData();
627 for (size_t i = 0; i < frame_.GetLength(); ++i) {
628 StringAppendF(output, "%02x, ", frame[i]);
Wade Guthrie0d438532012-05-18 14:18:50 -0700629 }
630 output->append("]");
631
632 return true;
633}
634
Wade Guthried4977f22012-08-22 12:37:54 -0700635bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800636 return frame_.Equals(other.frame_);
Wade Guthrie0d438532012-05-18 14:18:50 -0700637}
638
Wade Guthried4977f22012-08-22 12:37:54 -0700639
Wade Guthrie0d438532012-05-18 14:18:50 -0700640//
641// Specific UserBoundNlMessage types.
642//
643
644const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE;
645const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE";
646
647string AssociateMessage::ToString() const {
648 string output(GetHeaderString());
649 output.append("assoc");
repo sync90ee0fa2012-12-18 10:08:08 -0800650 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL))
Wade Guthrie0d438532012-05-18 14:18:50 -0700651 output.append(StringFromFrame(NL80211_ATTR_FRAME));
repo sync90ee0fa2012-12-18 10:08:08 -0800652 else if (attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT))
Wade Guthrie0d438532012-05-18 14:18:50 -0700653 output.append(": timed out");
654 else
655 output.append(": unknown event");
656 return output;
657}
658
659const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE;
660const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE";
661
662string AuthenticateMessage::ToString() const {
663 string output(GetHeaderString());
664 output.append("auth");
repo sync90ee0fa2012-12-18 10:08:08 -0800665 if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700666 output.append(StringFromFrame(NL80211_ATTR_FRAME));
667 } else {
repo sync90ee0fa2012-12-18 10:08:08 -0800668 output.append(attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT) ?
Wade Guthrie0d438532012-05-18 14:18:50 -0700669 ": timed out" : ": unknown event");
670 }
671 return output;
672}
673
674const uint8_t CancelRemainOnChannelMessage::kCommand =
675 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL;
676const char CancelRemainOnChannelMessage::kCommandString[] =
677 "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL";
678
679string CancelRemainOnChannelMessage::ToString() const {
680 string output(GetHeaderString());
681 uint32_t freq;
682 uint64_t cookie;
683 StringAppendF(&output,
684 "done with remain on freq %" PRIu32 " (cookie %" PRIx64 ")",
repo sync90ee0fa2012-12-18 10:08:08 -0800685 (attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ,
686 &freq) ? 0 : freq),
687 (attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE,
688 &cookie) ? 0 : cookie));
Wade Guthrie0d438532012-05-18 14:18:50 -0700689 return output;
690}
691
692const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT;
693const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT";
694
695string ConnectMessage::ToString() const {
696 string output(GetHeaderString());
697
698 uint16_t status = UINT16_MAX;
699
repo sync90ee0fa2012-12-18 10:08:08 -0800700 if (!attributes().GetU16AttributeValue(NL80211_ATTR_STATUS_CODE, &status)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700701 output.append("unknown connect status");
702 } else if (status == 0) {
703 output.append("connected");
704 } else {
705 output.append("failed to connect");
706 }
707
repo sync90ee0fa2012-12-18 10:08:08 -0800708 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700709 string mac;
710 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
711 StringAppendF(&output, " to %s", mac.c_str());
712 }
713 if (status)
714 StringAppendF(&output, ", status: %u: %s", status,
715 StringFromStatus(status).c_str());
716 return output;
717}
718
719const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE;
720const char DeauthenticateMessage::kCommandString[] =
721 "NL80211_CMD_DEAUTHENTICATE";
722
723string DeauthenticateMessage::ToString() const {
724 string output(GetHeaderString());
725 StringAppendF(&output, "deauth%s",
726 StringFromFrame(NL80211_ATTR_FRAME).c_str());
727 return output;
728}
729
730const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION;
731const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION";
732
733string DeleteStationMessage::ToString() const {
734 string mac;
735 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
736 string output(GetHeaderString());
737 StringAppendF(&output, "del station %s", mac.c_str());
738 return output;
739}
740
741const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE;
742const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE";
743
744string DisassociateMessage::ToString() const {
745 string output(GetHeaderString());
746 StringAppendF(&output, "disassoc%s",
747 StringFromFrame(NL80211_ATTR_FRAME).c_str());
748 return output;
749}
750
751const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT;
752const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT";
753
754string DisconnectMessage::ToString() const {
755 string output(GetHeaderString());
756 StringAppendF(&output, "disconnected %s",
repo sync90ee0fa2012-12-18 10:08:08 -0800757 ((attributes().IsFlagAttributeTrue(
758 NL80211_ATTR_DISCONNECTED_BY_AP)) ?
759 "(by AP)" : "(local request)"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700760
761 uint16_t reason = UINT16_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800762 if (attributes().GetU16AttributeValue(NL80211_ATTR_REASON_CODE, &reason)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700763 StringAppendF(&output, " reason: %u: %s",
Wade Guthried4977f22012-08-22 12:37:54 -0700764 reason, StringFromReason(reason).c_str());
Wade Guthrie0d438532012-05-18 14:18:50 -0700765 }
766 return output;
767}
768
769const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS;
770const char FrameTxStatusMessage::kCommandString[] =
771 "NL80211_CMD_FRAME_TX_STATUS";
772
773string FrameTxStatusMessage::ToString() const {
774 string output(GetHeaderString());
775 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800776 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -0700777
repo sync90ee0fa2012-12-18 10:08:08 -0800778 StringAppendF(&output, "mgmt TX status (cookie %" PRIx64 "): %s", cookie,
779 (attributes().IsFlagAttributeTrue(NL80211_ATTR_ACK) ?
780 "acked" : "no ack"));
Wade Guthrie0d438532012-05-18 14:18:50 -0700781 return output;
782}
783
784const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS;
785const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS";
786
787string JoinIbssMessage::ToString() const {
788 string mac;
789 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
790 string output(GetHeaderString());
791 StringAppendF(&output, "IBSS %s joined", mac.c_str());
792 return output;
793}
794
795const uint8_t MichaelMicFailureMessage::kCommand =
796 NL80211_CMD_MICHAEL_MIC_FAILURE;
797const char MichaelMicFailureMessage::kCommandString[] =
798 "NL80211_CMD_MICHAEL_MIC_FAILURE";
799
800string MichaelMicFailureMessage::ToString() const {
801 string output(GetHeaderString());
802
803 output.append("Michael MIC failure event:");
804
repo sync90ee0fa2012-12-18 10:08:08 -0800805 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700806 string mac;
807 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
808 StringAppendF(&output, " source MAC address %s", mac.c_str());
809 }
810
repo sync90ee0fa2012-12-18 10:08:08 -0800811 {
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800812 ByteString rawdata;
repo sync90ee0fa2012-12-18 10:08:08 -0800813 if (attributes().GetRawAttributeValue(NL80211_ATTR_KEY_SEQ,
814 &rawdata) &&
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800815 rawdata.GetLength() == UserBoundNlMessage::kEthernetAddressBytes) {
816 const unsigned char *seq = rawdata.GetConstData();
Wade Guthrie0d438532012-05-18 14:18:50 -0700817 StringAppendF(&output, " seq=%02x%02x%02x%02x%02x%02x",
818 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
819 }
820 }
821 uint32_t key_type_val = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800822 if (attributes().GetU32AttributeValue(NL80211_ATTR_KEY_TYPE, &key_type_val)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700823 enum nl80211_key_type key_type =
824 static_cast<enum nl80211_key_type >(key_type_val);
825 StringAppendF(&output, " Key Type %s", StringFromKeyType(key_type).c_str());
826 }
827
828 uint8_t key_index = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800829 if (attributes().GetU8AttributeValue(NL80211_ATTR_KEY_IDX, &key_index)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700830 StringAppendF(&output, " Key Id %u", key_index);
831 }
832
833 return output;
834}
835
836const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS;
837const char NewScanResultsMessage::kCommandString[] =
838 "NL80211_CMD_NEW_SCAN_RESULTS";
839
840string NewScanResultsMessage::ToString() const {
841 string output(GetHeaderString());
842 output.append("scan finished");
843
844 {
845 output.append("; frequencies: ");
846 vector<uint32_t> list;
847 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
848 string str;
849 for (vector<uint32_t>::const_iterator i = list.begin();
850 i != list.end(); ++i) {
851 StringAppendF(&str, " %" PRIu32 ", ", *i);
852 }
853 output.append(str);
854 }
855 }
856
857 {
858 output.append("; SSIDs: ");
859 vector<string> list;
860 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
861 string str;
862 for (vector<string>::const_iterator i = list.begin();
863 i != list.end(); ++i) {
864 StringAppendF(&str, "\"%s\", ", i->c_str());
865 }
866 output.append(str);
867 }
868 }
869
870 return output;
871}
872
873const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION;
874const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION";
875
876string NewStationMessage::ToString() const {
877 string mac;
878 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
879 string output(GetHeaderString());
880 StringAppendF(&output, "new station %s", mac.c_str());
881
882 return output;
883}
884
885const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY;
886const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY";
887
888string NewWifiMessage::ToString() const {
889 string output(GetHeaderString());
890 string wifi_name = "None";
repo sync90ee0fa2012-12-18 10:08:08 -0800891 attributes().GetStringAttributeValue(NL80211_ATTR_WIPHY_NAME, &wifi_name);
Wade Guthrie0d438532012-05-18 14:18:50 -0700892 StringAppendF(&output, "renamed to %s", wifi_name.c_str());
893 return output;
894}
895
896const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM;
897const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM";
898
899string NotifyCqmMessage::ToString() const {
repo sync90ee0fa2012-12-18 10:08:08 -0800900 // TODO(wdg): use attributes().GetNestedAttributeValue()...
Wade Guthrie0d438532012-05-18 14:18:50 -0700901 static const nla_policy kCqmPolicy[NL80211_ATTR_CQM_MAX + 1] = {
902 { NLA_U32, 0, 0 }, // Who Knows?
903 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD]
904 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST]
905 { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]
906 };
907
908 string output(GetHeaderString());
909 output.append("connection quality monitor event: ");
910
repo sync90ee0fa2012-12-18 10:08:08 -0800911 const Nl80211RawAttribute *attribute =
912 attributes().GetRawAttribute(NL80211_ATTR_CQM);
repo syncd316eb72012-12-10 15:48:47 -0800913 if (!attribute) {
914 output.append("missing data!");
915 return output;
916 }
917
918 const nlattr *const_data = attribute->data();
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800919 // Note that |nla_parse_nested| doesn't change |const_data| but doesn't
920 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -0700921 nlattr *cqm_attr = const_cast<nlattr *>(const_data);
922
923 nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
repo syncd316eb72012-12-10 15:48:47 -0800924 if (!cqm_attr || nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr,
925 const_cast<nla_policy *>(kCqmPolicy))) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700926 output.append("missing data!");
927 return output;
928 }
929 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
930 enum nl80211_cqm_rssi_threshold_event rssi_event =
931 static_cast<enum nl80211_cqm_rssi_threshold_event>(
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800932 Nl80211Attribute::NlaGetU32(
933 cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -0700934 if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
935 output.append("RSSI went above threshold");
936 else
937 output.append("RSSI went below threshold");
938 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
repo sync90ee0fa2012-12-18 10:08:08 -0800939 attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -0700940 string mac;
941 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
942 StringAppendF(&output, "peer %s didn't ACK %" PRIu32 " packets",
943 mac.c_str(),
Wade Guthrie8343f7f2012-12-04 13:52:32 -0800944 Nl80211Attribute::NlaGetU32(
945 cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]));
Wade Guthrie0d438532012-05-18 14:18:50 -0700946 } else {
947 output.append("unknown event");
948 }
Wade Guthrie0d438532012-05-18 14:18:50 -0700949 return output;
950}
951
952const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE;
953const char PmksaCandidateMessage::kCommandString[] =
954 "NL80211_ATTR_PMKSA_CANDIDATE";
955
956string PmksaCandidateMessage::ToString() const {
957 string output(GetHeaderString());
958 output.append("PMKSA candidate found");
959 return output;
960}
961
962const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT;
963const char RegBeaconHintMessage::kCommandString[] =
964 "NL80211_CMD_REG_BEACON_HINT";
965
966string RegBeaconHintMessage::ToString() const {
967 string output(GetHeaderString());
968 uint32_t wiphy_idx = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -0800969 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, &wiphy_idx);
Wade Guthrie0d438532012-05-18 14:18:50 -0700970
repo sync90ee0fa2012-12-18 10:08:08 -0800971 const Nl80211RawAttribute *freq_before =
972 attributes().GetRawAttribute(NL80211_ATTR_FREQ_BEFORE);
repo syncd316eb72012-12-10 15:48:47 -0800973 if (!freq_before)
974 return "";
975 const nlattr *const_before = freq_before->data();
Wade Guthrie0d438532012-05-18 14:18:50 -0700976 ieee80211_beacon_channel chan_before_beacon;
977 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
978 if (ParseBeaconHintChan(const_before, &chan_before_beacon))
979 return "";
980
repo sync90ee0fa2012-12-18 10:08:08 -0800981 const Nl80211RawAttribute *freq_after =
982 attributes().GetRawAttribute(NL80211_ATTR_FREQ_AFTER);
repo syncd316eb72012-12-10 15:48:47 -0800983 if (!freq_after)
984 return "";
985
986 const nlattr *const_after = freq_after->data();
Wade Guthrie0d438532012-05-18 14:18:50 -0700987 ieee80211_beacon_channel chan_after_beacon;
988 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
989 if (ParseBeaconHintChan(const_after, &chan_after_beacon))
990 return "";
991
992 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
993 return "";
994
995 /* A beacon hint is sent _only_ if something _did_ change */
996 output.append("beacon hint:");
997 StringAppendF(&output, "phy%" PRIu32 " %u MHz [%d]:",
998 wiphy_idx, chan_before_beacon.center_freq,
999 ChannelFromIeee80211Frequency(chan_before_beacon.center_freq));
1000
1001 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
1002 output.append("\to active scanning enabled");
1003 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
1004 output.append("\to beaconing enabled");
1005 return output;
1006}
1007
1008int RegBeaconHintMessage::ParseBeaconHintChan(const nlattr *tb,
1009 ieee80211_beacon_channel *chan)
1010 const {
1011 static const nla_policy kBeaconFreqPolicy[
1012 NL80211_FREQUENCY_ATTR_MAX + 1] = {
1013 {0, 0, 0},
1014 { NLA_U32, 0, 0 }, // [NL80211_FREQUENCY_ATTR_FREQ]
1015 {0, 0, 0},
1016 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]
1017 { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_NO_IBSS]
1018 };
1019
1020 if (!tb) {
1021 LOG(ERROR) << "|tb| parameter is NULL.";
1022 return -EINVAL;
1023 }
1024
1025 if (!chan) {
1026 LOG(ERROR) << "|chan| parameter is NULL.";
1027 return -EINVAL;
1028 }
1029
1030 nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1031
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001032 // Note that |nla_parse_nested| doesn't change its parameters but doesn't
1033 // declare itself as 'const', either. Hence the cast.
Wade Guthrie0d438532012-05-18 14:18:50 -07001034 if (nla_parse_nested(tb_freq,
1035 NL80211_FREQUENCY_ATTR_MAX,
1036 const_cast<nlattr *>(tb),
1037 const_cast<nla_policy *>(kBeaconFreqPolicy)))
1038 return -EINVAL;
1039
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001040 chan->center_freq = Nl80211Attribute::NlaGetU32(
1041 tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
Wade Guthrie0d438532012-05-18 14:18:50 -07001042
1043 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1044 chan->passive_scan = true;
1045 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1046 chan->no_ibss = true;
1047
1048 return 0;
1049}
1050
1051int RegBeaconHintMessage::ChannelFromIeee80211Frequency(int freq) {
1052 // TODO(wdg): get rid of these magic numbers.
1053 if (freq == 2484)
1054 return 14;
1055
1056 if (freq < 2484)
1057 return (freq - 2407) / 5;
1058
1059 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
1060 return freq/5 - 1000;
1061}
1062
1063const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE;
1064const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE";
1065
1066string RegChangeMessage::ToString() const {
1067 string output(GetHeaderString());
1068 output.append("regulatory domain change: ");
1069
1070 uint8_t reg_type = UINT8_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001071 attributes().GetU8AttributeValue(NL80211_ATTR_REG_TYPE, &reg_type);
Wade Guthrie0d438532012-05-18 14:18:50 -07001072
1073 uint32_t initiator = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001074 attributes().GetU32AttributeValue(NL80211_ATTR_REG_INITIATOR, &initiator);
Wade Guthrie0d438532012-05-18 14:18:50 -07001075
1076 uint32_t wifi = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001077 bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY,
1078 &wifi);
Wade Guthrie0d438532012-05-18 14:18:50 -07001079
1080 string alpha2 = "<None>";
repo sync90ee0fa2012-12-18 10:08:08 -08001081 attributes().GetStringAttributeValue(NL80211_ATTR_REG_ALPHA2, &alpha2);
Wade Guthrie0d438532012-05-18 14:18:50 -07001082
1083 switch (reg_type) {
1084 case NL80211_REGDOM_TYPE_COUNTRY:
1085 StringAppendF(&output, "set to %s by %s request",
1086 alpha2.c_str(), StringFromRegInitiator(initiator).c_str());
1087 if (wifi_exists)
1088 StringAppendF(&output, " on phy%" PRIu32, wifi);
1089 break;
1090
1091 case NL80211_REGDOM_TYPE_WORLD:
1092 StringAppendF(&output, "set to world roaming by %s request",
1093 StringFromRegInitiator(initiator).c_str());
1094 break;
1095
1096 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
1097 StringAppendF(&output,
1098 "custom world roaming rules in place on phy%" PRIu32
1099 " by %s request",
1100 wifi, StringFromRegInitiator(initiator).c_str());
1101 break;
1102
1103 case NL80211_REGDOM_TYPE_INTERSECTION:
1104 StringAppendF(&output, "intersection used due to a request made by %s",
1105 StringFromRegInitiator(initiator).c_str());
1106 if (wifi_exists)
1107 StringAppendF(&output, " on phy%" PRIu32, wifi);
1108 break;
1109
1110 default:
1111 output.append("unknown source");
1112 break;
1113 }
1114 return output;
1115}
1116
1117const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL;
1118const char RemainOnChannelMessage::kCommandString[] =
1119 "NL80211_CMD_REMAIN_ON_CHANNEL";
1120
1121string RemainOnChannelMessage::ToString() const {
1122 string output(GetHeaderString());
1123
1124 uint32_t wifi_freq = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001125 attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ, &wifi_freq);
Wade Guthrie0d438532012-05-18 14:18:50 -07001126
1127 uint32_t duration = UINT32_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001128 attributes().GetU32AttributeValue(NL80211_ATTR_DURATION, &duration);
Wade Guthrie0d438532012-05-18 14:18:50 -07001129
1130 uint64_t cookie = UINT64_MAX;
repo sync90ee0fa2012-12-18 10:08:08 -08001131 attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie);
Wade Guthrie0d438532012-05-18 14:18:50 -07001132
1133 StringAppendF(&output, "remain on freq %" PRIu32 " (%" PRIu32 "ms, cookie %"
1134 PRIx64 ")",
1135 wifi_freq, duration, cookie);
1136 return output;
1137}
1138
1139const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM;
1140const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM";
1141
1142string RoamMessage::ToString() const {
1143 string output(GetHeaderString());
1144 output.append("roamed");
1145
repo sync90ee0fa2012-12-18 10:08:08 -08001146 if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001147 string mac;
1148 GetMacAttributeString(NL80211_ATTR_MAC, &mac);
1149 StringAppendF(&output, " to %s", mac.c_str());
1150 }
1151 return output;
1152}
1153
1154const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED;
1155const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED";
1156
1157string ScanAbortedMessage::ToString() const {
1158 string output(GetHeaderString());
1159 output.append("scan aborted");
1160
1161 {
1162 output.append("; frequencies: ");
1163 vector<uint32_t> list;
1164 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1165 string str;
1166 for (vector<uint32_t>::const_iterator i = list.begin();
1167 i != list.end(); ++i) {
1168 StringAppendF(&str, " %" PRIu32 ", ", *i);
1169 }
1170 output.append(str);
1171 }
1172 }
1173
1174 {
1175 output.append("; SSIDs: ");
1176 vector<string> list;
1177 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1178 string str;
1179 for (vector<string>::const_iterator i = list.begin();
1180 i != list.end(); ++i) {
1181 StringAppendF(&str, "\"%s\", ", i->c_str());
1182 }
1183 output.append(str);
1184 }
1185 }
1186
1187 return output;
1188}
1189
1190const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN;
1191const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN";
1192
1193string TriggerScanMessage::ToString() const {
1194 string output(GetHeaderString());
1195 output.append("scan started");
1196
1197 {
1198 output.append("; frequencies: ");
1199 vector<uint32_t> list;
1200 if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) {
1201 string str;
1202 for (vector<uint32_t>::const_iterator i = list.begin();
1203 i != list.end(); ++i) {
1204 StringAppendF(&str, "%" PRIu32 ", ", *i);
1205 }
1206 output.append(str);
1207 }
1208 }
1209
1210 {
1211 output.append("; SSIDs: ");
1212 vector<string> list;
1213 if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) {
1214 string str;
1215 for (vector<string>::const_iterator i = list.begin();
1216 i != list.end(); ++i) {
1217 StringAppendF(&str, "\"%s\", ", i->c_str());
1218 }
1219 output.append(str);
1220 }
1221 }
1222
1223 return output;
1224}
1225
1226const uint8_t UnknownMessage::kCommand = 0xff;
1227const char UnknownMessage::kCommandString[] = "<Unknown Message Type>";
1228
1229string UnknownMessage::ToString() const {
1230 string output(GetHeaderString());
1231 StringAppendF(&output, "unknown event %u", command_);
1232 return output;
1233}
1234
1235const uint8_t UnprotDeauthenticateMessage::kCommand =
1236 NL80211_CMD_UNPROT_DEAUTHENTICATE;
1237const char UnprotDeauthenticateMessage::kCommandString[] =
1238 "NL80211_CMD_UNPROT_DEAUTHENTICATE";
1239
1240string UnprotDeauthenticateMessage::ToString() const {
1241 string output(GetHeaderString());
1242 StringAppendF(&output, "unprotected deauth %s",
1243 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1244 return output;
1245}
1246
1247const uint8_t UnprotDisassociateMessage::kCommand =
1248 NL80211_CMD_UNPROT_DISASSOCIATE;
1249const char UnprotDisassociateMessage::kCommandString[] =
1250 "NL80211_CMD_UNPROT_DISASSOCIATE";
1251
1252string UnprotDisassociateMessage::ToString() const {
1253 string output(GetHeaderString());
1254 StringAppendF(&output, "unprotected disassoc %s",
1255 StringFromFrame(NL80211_ATTR_FRAME).c_str());
1256 return output;
1257}
1258
1259//
1260// Factory class.
1261//
1262
1263UserBoundNlMessage *UserBoundNlMessageFactory::CreateMessage(nlmsghdr *msg) {
1264 if (!msg) {
1265 LOG(ERROR) << "NULL |msg| parameter";
1266 return NULL;
1267 }
1268
1269 scoped_ptr<UserBoundNlMessage> message;
1270 genlmsghdr *gnlh =
1271 reinterpret_cast<genlmsghdr *>(nlmsg_data(msg));
1272
1273 if (!gnlh) {
1274 LOG(ERROR) << "NULL gnlh";
1275 return NULL;
1276 }
1277
1278 switch (gnlh->cmd) {
1279 case AssociateMessage::kCommand:
1280 message.reset(new AssociateMessage()); break;
1281 case AuthenticateMessage::kCommand:
1282 message.reset(new AuthenticateMessage()); break;
1283 case CancelRemainOnChannelMessage::kCommand:
1284 message.reset(new CancelRemainOnChannelMessage()); break;
1285 case ConnectMessage::kCommand:
1286 message.reset(new ConnectMessage()); break;
1287 case DeauthenticateMessage::kCommand:
1288 message.reset(new DeauthenticateMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001289 case DeleteStationMessage::kCommand:
1290 message.reset(new DeleteStationMessage()); break;
Wade Guthrie0d438532012-05-18 14:18:50 -07001291 case DisassociateMessage::kCommand:
1292 message.reset(new DisassociateMessage()); break;
1293 case DisconnectMessage::kCommand:
1294 message.reset(new DisconnectMessage()); break;
1295 case FrameTxStatusMessage::kCommand:
1296 message.reset(new FrameTxStatusMessage()); break;
1297 case JoinIbssMessage::kCommand:
1298 message.reset(new JoinIbssMessage()); break;
1299 case MichaelMicFailureMessage::kCommand:
1300 message.reset(new MichaelMicFailureMessage()); break;
1301 case NewScanResultsMessage::kCommand:
1302 message.reset(new NewScanResultsMessage()); break;
1303 case NewStationMessage::kCommand:
1304 message.reset(new NewStationMessage()); break;
1305 case NewWifiMessage::kCommand:
1306 message.reset(new NewWifiMessage()); break;
1307 case NotifyCqmMessage::kCommand:
1308 message.reset(new NotifyCqmMessage()); break;
1309 case PmksaCandidateMessage::kCommand:
1310 message.reset(new PmksaCandidateMessage()); break;
1311 case RegBeaconHintMessage::kCommand:
1312 message.reset(new RegBeaconHintMessage()); break;
1313 case RegChangeMessage::kCommand:
1314 message.reset(new RegChangeMessage()); break;
1315 case RemainOnChannelMessage::kCommand:
1316 message.reset(new RemainOnChannelMessage()); break;
1317 case RoamMessage::kCommand:
1318 message.reset(new RoamMessage()); break;
1319 case ScanAbortedMessage::kCommand:
1320 message.reset(new ScanAbortedMessage()); break;
1321 case TriggerScanMessage::kCommand:
1322 message.reset(new TriggerScanMessage()); break;
1323 case UnprotDeauthenticateMessage::kCommand:
1324 message.reset(new UnprotDeauthenticateMessage()); break;
1325 case UnprotDisassociateMessage::kCommand:
1326 message.reset(new UnprotDisassociateMessage()); break;
1327
1328 default:
1329 message.reset(new UnknownMessage(gnlh->cmd)); break;
1330 break;
1331 }
1332
1333 nlattr *tb[NL80211_ATTR_MAX + 1];
1334
1335 // Parse the attributes from the nl message payload (which starts at the
1336 // header) into the 'tb' array.
1337 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
Wade Guthrie8343f7f2012-12-04 13:52:32 -08001338 genlmsg_attrlen(gnlh, 0), NULL);
Wade Guthrie0d438532012-05-18 14:18:50 -07001339
1340 if (!message->Init(tb, msg)) {
1341 LOG(ERROR) << "Message did not initialize properly";
1342 return NULL;
1343 }
1344
Wade Guthrie0d438532012-05-18 14:18:50 -07001345 return message.release();
1346}
1347
1348UserBoundNlMessageDataCollector *
1349 UserBoundNlMessageDataCollector::GetInstance() {
1350 return g_datacollector.Pointer();
1351}
1352
1353UserBoundNlMessageDataCollector::UserBoundNlMessageDataCollector() {
1354 need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true;
1355 need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true;
1356 need_to_print[NL80211_CMD_DEL_STATION] = true;
1357 need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true;
1358 need_to_print[NL80211_CMD_JOIN_IBSS] = true;
1359 need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true;
1360 need_to_print[NL80211_CMD_NEW_WIPHY] = true;
1361 need_to_print[NL80211_CMD_REG_BEACON_HINT] = true;
1362 need_to_print[NL80211_CMD_REG_CHANGE] = true;
1363 need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true;
1364 need_to_print[NL80211_CMD_ROAM] = true;
1365 need_to_print[NL80211_CMD_SCAN_ABORTED] = true;
1366 need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true;
1367 need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true;
1368}
1369
1370void UserBoundNlMessageDataCollector::CollectDebugData(
Wade Guthried4977f22012-08-22 12:37:54 -07001371 const UserBoundNlMessage &message, nlmsghdr *msg) {
Wade Guthrie0d438532012-05-18 14:18:50 -07001372 if (!msg) {
1373 LOG(ERROR) << "NULL |msg| parameter";
1374 return;
1375 }
1376
1377 bool doit = false;
1378
1379 map<uint8_t, bool>::const_iterator node;
Christopher Wiley764538d2012-11-09 10:58:23 -08001380 node = need_to_print.find(message.message_type());
Wade Guthrie0d438532012-05-18 14:18:50 -07001381 if (node != need_to_print.end())
1382 doit = node->second;
1383
1384 if (doit) {
Wade Guthried6153612012-08-23 11:36:14 -07001385 LOG(INFO) << "@@const unsigned char "
Christopher Wiley764538d2012-11-09 10:58:23 -08001386 << "k" << message.message_type_string()
Wade Guthried6153612012-08-23 11:36:14 -07001387 << "[] = {";
Wade Guthrie0d438532012-05-18 14:18:50 -07001388
Christopher Wileyefd521f2012-11-07 17:32:46 -08001389 int payload_bytes = nlmsg_datalen(msg);
Wade Guthrie0d438532012-05-18 14:18:50 -07001390
1391 size_t bytes = nlmsg_total_size(payload_bytes);
1392 unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg);
Wade Guthried4977f22012-08-22 12:37:54 -07001393 for (size_t i = 0; i < bytes; ++i) {
Wade Guthried6153612012-08-23 11:36:14 -07001394 LOG(INFO) << " 0x"
Wade Guthrie0d438532012-05-18 14:18:50 -07001395 << std::hex << std::setfill('0') << std::setw(2)
1396 << + rawdata[i] << ",";
1397 }
Wade Guthried6153612012-08-23 11:36:14 -07001398 LOG(INFO) << "};";
Christopher Wiley764538d2012-11-09 10:58:23 -08001399 need_to_print[message.message_type()] = false;
Wade Guthrie0d438532012-05-18 14:18:50 -07001400 }
1401}
1402
1403} // namespace shill.