Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1 | // 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 sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 25 | #include "shill/nl80211_message.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 26 | |
| 27 | #include <ctype.h> |
| 28 | #include <endian.h> |
| 29 | #include <errno.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 30 | #include <linux/nl80211.h> |
| 31 | #include <net/if.h> |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 32 | #include <netinet/in.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 33 | #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 Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 40 | #include <algorithm> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 41 | #include <iomanip> |
| 42 | #include <string> |
| 43 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 44 | #include <base/bind.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 45 | #include <base/format_macros.h> |
| 46 | #include <base/stl_util.h> |
| 47 | #include <base/stringprintf.h> |
| 48 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 49 | #include "shill/attribute_list.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 50 | #include "shill/ieee80211.h" |
| 51 | #include "shill/logging.h" |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 52 | #include "shill/netlink_socket.h" |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 53 | #include "shill/nl80211_attribute.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 54 | #include "shill/scope_logger.h" |
Darin Petkov | 50cb78a | 2013-02-06 16:17:49 +0100 | [diff] [blame] | 55 | #include "shill/wifi.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 56 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 57 | using base::Bind; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 58 | using base::LazyInstance; |
| 59 | using base::StringAppendF; |
| 60 | using base::StringPrintf; |
| 61 | using std::map; |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 62 | using std::min; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 63 | using std::string; |
| 64 | using std::vector; |
| 65 | |
| 66 | namespace shill { |
| 67 | |
| 68 | namespace { |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 69 | LazyInstance<Nl80211MessageDataCollector> g_datacollector = |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 70 | LAZY_INSTANCE_INITIALIZER; |
| 71 | } // namespace |
| 72 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 73 | const char Nl80211Message::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 74 | |
| 75 | const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26; |
| 76 | const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc; |
| 77 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 78 | const uint32_t Nl80211Message::kIllegalMessage = 0; |
| 79 | const unsigned int Nl80211Message::kEthernetAddressBytes = 6; |
| 80 | map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL; |
| 81 | map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 82 | |
| 83 | // The nl messages look like this: |
| 84 | // |
| 85 | // XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX |
| 86 | // XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX |
| 87 | // +-- gnhl nlmsg_tail(hdr) --+ |
| 88 | // | nlmsg_next(hdr) --+ |
| 89 | // v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V |
| 90 | // -----+-----+-+----------------------------------------------+-++---- |
| 91 | // ... | | | payload | || |
| 92 | // | | +------+-+--------+-+--------------------------+ || |
| 93 | // | nl | | | | | | attribs | || |
| 94 | // | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ... |
| 95 | // | hdr |a| msg |a| header |a| nl |p| pay |p| |a|| |
| 96 | // | |d| hdr |d| |d| attr |a| load |a| ... |d|| |
| 97 | // | | | | | | | |d| |d| | || |
| 98 | // -----+-----+-+----------------------------------------------+-++---- |
| 99 | // ^ ^ ^ ^ |
| 100 | // | | | XXXXXXX <-- nla_len(nlattr) |
| 101 | // | | | +-- nla_data(nlattr) |
| 102 | // | | X-nla_total_size-X |
| 103 | // | | XXXXX-nlmsg_attrlen-XXXXXX |
| 104 | // | +-- nlmsg_data(hdr) +-- nlmsg_attrdata() |
| 105 | // +-- msg = nlmsg_hdr(raw_message) |
| 106 | |
| 107 | // |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 108 | // Nl80211Message |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 109 | // |
| 110 | |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 111 | void Nl80211Message::Print(int log_level) const { |
| 112 | SLOG(WiFi, log_level) << StringPrintf("Message %s (%d)", |
| 113 | message_type_string(), |
| 114 | message_type()); |
| 115 | attributes_.Print(log_level, 1); |
| 116 | } |
| 117 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 118 | bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) { |
| 119 | if (!const_msg) { |
| 120 | LOG(ERROR) << "Null |msg| parameter"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 124 | // Netlink header. |
| 125 | sequence_number_ = const_msg->nlmsg_seq; |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 126 | SLOG(WiFi, 6) << "NL Message " << sequence_number() << " <==="; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 127 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 128 | // Casting away constness, here, since the libnl code doesn't properly label |
| 129 | // their stuff as const (even though it is). |
| 130 | nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg); |
| 131 | |
| 132 | // Genl message header. |
| 133 | genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(nlmsg_data(msg)); |
| 134 | |
| 135 | // Attributes. |
| 136 | // Parse the attributes from the nl message payload into the 'tb' array. |
| 137 | nlattr *tb[NL80211_ATTR_MAX + 1]; |
| 138 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 139 | genlmsg_attrlen(gnlh, 0), NULL); |
| 140 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 141 | for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) { |
| 142 | if (tb[i]) { |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 143 | // TODO(wdg): When Nl80211Messages instantiate their own attributes, |
| 144 | // this call should, instead, call |SetAttributeFromNlAttr|. |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 145 | attributes_.CreateAndInitAttribute( |
| 146 | i, tb[i], Bind(&NetlinkAttribute::NewNl80211AttributeFromId)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | // Convert integer values provided by libnl (for example, from the |
| 151 | // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into |
| 152 | // strings describing the status. |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 153 | if (!reason_code_string_) { |
| 154 | reason_code_string_ = new map<uint16_t, string>; |
| 155 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] = |
| 156 | "Unspecified reason"; |
| 157 | (*reason_code_string_)[ |
| 158 | IEEE_80211::kReasonCodePreviousAuthenticationInvalid] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 159 | "Previous authentication no longer valid"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 160 | (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] = |
| 161 | "Deauthentcated because sending STA is leaving (or has left) IBSS or " |
| 162 | "ESS"; |
| 163 | (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] = |
| 164 | "Disassociated due to inactivity"; |
| 165 | (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] = |
| 166 | "Disassociated because AP is unable to handle all currently associated " |
| 167 | "STAs"; |
| 168 | (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] = |
| 169 | "Class 2 frame received from nonauthenticated STA"; |
| 170 | (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] = |
| 171 | "Class 3 frame received from nonassociated STA"; |
| 172 | (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] = |
| 173 | "Disassociated because sending STA is leaving (or has left) BSS"; |
| 174 | (*reason_code_string_)[ |
| 175 | IEEE_80211::kReasonCodeReassociationNotAuthenticated] = |
| 176 | "STA requesting (re)association is not authenticated with responding " |
| 177 | "STA"; |
| 178 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] = |
| 179 | "Disassociated because the information in the Power Capability " |
| 180 | "element is unacceptable"; |
| 181 | (*reason_code_string_)[ |
| 182 | IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] = |
| 183 | "Disassociated because the information in the Supported Channels " |
| 184 | "element is unacceptable"; |
| 185 | (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] = |
| 186 | "Invalid information element, i.e., an information element defined in " |
| 187 | "this standard for which the content does not meet the specifications " |
| 188 | "in Clause 7"; |
| 189 | (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] = |
| 190 | "Message integrity code (MIC) failure"; |
| 191 | (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] = |
| 192 | "4-Way Handshake timeout"; |
| 193 | (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] = |
| 194 | "Group Key Handshake timeout"; |
| 195 | (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] = |
| 196 | "Information element in 4-Way Handshake different from " |
| 197 | "(Re)Association Request/Probe Response/Beacon frame"; |
| 198 | (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] = |
| 199 | "Invalid group cipher"; |
| 200 | (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] = |
| 201 | "Invalid pairwise cipher"; |
| 202 | (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] = |
| 203 | "Invalid AKMP"; |
| 204 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] = |
| 205 | "Unsupported RSN information element version"; |
| 206 | (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] = |
| 207 | "Invalid RSN information element capabilities"; |
| 208 | (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] = |
| 209 | "IEEE 802.1X authentication failed"; |
| 210 | (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] = |
| 211 | "Cipher suite rejected because of the security policy"; |
| 212 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] = |
| 213 | "Disassociated for unspecified, QoS-related reason"; |
| 214 | (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] = |
| 215 | "Disassociated because QoS AP lacks sufficient bandwidth for this " |
| 216 | "QoS STA"; |
| 217 | (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] = |
| 218 | "Disassociated because excessive number of frames need to be " |
| 219 | "acknowledged, but are not acknowledged due to AP transmissions " |
| 220 | "and/or poor channel conditions"; |
| 221 | (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] = |
| 222 | "Disassociated because STA is transmitting outside the limits of its " |
| 223 | "TXOPs"; |
| 224 | (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] = |
| 225 | "Requested from peer STA as the STA is leaving the BSS (or resetting)"; |
| 226 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] = |
| 227 | "Requested from peer STA as it does not want to use the mechanism"; |
| 228 | (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] = |
| 229 | "Requested from peer STA as the STA received frames using the " |
| 230 | "mechanism for which a setup is required"; |
| 231 | (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] = |
| 232 | "Requested from peer STA due to timeout"; |
| 233 | (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] = |
| 234 | "Peer STA does not support the requested cipher suite"; |
| 235 | (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>"; |
| 236 | } |
| 237 | |
| 238 | if (!status_code_string_) { |
| 239 | status_code_string_ = new map<uint16_t, string>; |
| 240 | (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful"; |
| 241 | (*status_code_string_)[IEEE_80211::kStatusCodeFailure] = |
| 242 | "Unspecified failure"; |
| 243 | (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 244 | "Cannot support all requested capabilities in the capability " |
| 245 | "information field"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 246 | (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 247 | "Reassociation denied due to inability to confirm that association " |
| 248 | "exists"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 249 | (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 250 | "Association denied due to reason outside the scope of this standard"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 251 | (*status_code_string_)[ |
| 252 | IEEE_80211::kStatusCodeAuthenticationUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 253 | "Responding station does not support the specified authentication " |
| 254 | "algorithm"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 255 | (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 256 | "Received an authentication frame with authentication transaction " |
| 257 | "sequence number out of expected sequence"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 258 | (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 259 | "Authentication rejected because of challenge failure"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 260 | (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 261 | "Authentication rejected due to timeout waiting for next frame in " |
| 262 | "sequence"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 263 | (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] = |
| 264 | "Association denied because AP is unable to handle additional " |
| 265 | "associated STA"; |
| 266 | (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 267 | "Association denied due to requesting station not supporting all of " |
| 268 | "the data rates in the BSSBasicRateSet parameter"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 269 | (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 270 | "Association denied due to requesting station not supporting the " |
| 271 | "short preamble option"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 272 | (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 273 | "Association denied due to requesting station not supporting the PBCC " |
| 274 | "modulation option"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 275 | (*status_code_string_)[ |
| 276 | IEEE_80211::kStatusCodeChannelAgilityUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 277 | "Association denied due to requesting station not supporting the " |
| 278 | "channel agility option"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 279 | (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 280 | "Association request rejected because Spectrum Management capability " |
| 281 | "is required"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 282 | (*status_code_string_)[ |
| 283 | IEEE_80211::kStatusCodeUnacceptablePowerCapability] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 284 | "Association request rejected because the information in the Power " |
| 285 | "Capability element is unacceptable"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 286 | (*status_code_string_)[ |
| 287 | IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 288 | "Association request rejected because the information in the " |
| 289 | "Supported Channels element is unacceptable"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 290 | (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 291 | "Association request rejected due to requesting station not " |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 292 | "supporting the Short Slot Time option"; |
| 293 | (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] = |
| 294 | "Association request rejected due to requesting station not " |
| 295 | "supporting the DSSS-OFDM option"; |
| 296 | (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 297 | "Unspecified, QoS related failure"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 298 | (*status_code_string_)[ |
| 299 | IEEE_80211::kStatusCodeInsufficientBandwithForQsta] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 300 | "Association denied due to QAP having insufficient bandwidth to handle " |
| 301 | "another QSTA"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 302 | (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 303 | "Association denied due to poor channel conditions"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 304 | (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] = |
| 305 | "Association (with QoS BSS) denied due to requesting station not " |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 306 | "supporting the QoS facility"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 307 | (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] = |
| 308 | "The request has been declined"; |
| 309 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 310 | "The request has not been successful as one or more parameters have " |
| 311 | "invalid values"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 312 | (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] = |
| 313 | "The TS has not been created because the request cannot be honored. " |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 314 | "However, a suggested Tspec is provided so that the initiating QSTA " |
| 315 | "may attempt to send another TS with the suggested changes to the " |
| 316 | "TSpec"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 317 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 318 | "Invalid Information Element"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 319 | (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] = |
| 320 | "Invalid Group Cipher"; |
| 321 | (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] = |
| 322 | "Invalid Pairwise Cipher"; |
| 323 | (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP"; |
| 324 | (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] = |
| 325 | "Unsupported RSN Information Element version"; |
| 326 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] = |
| 327 | "Invalid RSN Information Element Capabilities"; |
| 328 | (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 329 | "Cipher suite is rejected per security policy"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 330 | (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] = |
| 331 | "The TS has not been created. However, the HC may be capable of " |
| 332 | "creating a TS, in response to a request, after the time indicated in " |
| 333 | "the TS Delay element"; |
| 334 | (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 335 | "Direct link is not allowed in the BSS by policy"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 336 | (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] = |
| 337 | "Destination STA is not present within this BSS"; |
| 338 | (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] = |
| 339 | "The destination STA is not a QoS STA"; |
| 340 | (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 341 | "Association denied because Listen Interval is too large"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 342 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | return true; |
| 346 | } |
| 347 | |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 348 | // static |
| 349 | void Nl80211Message::PrintBytes(int log_level, const unsigned char *buf, |
| 350 | size_t num_bytes) { |
| 351 | SLOG(WiFi, log_level) << "Netlink Message -- Examining Bytes"; |
| 352 | if (!buf) { |
| 353 | SLOG(WiFi, log_level) << "<NULL Buffer>"; |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | if (num_bytes >= sizeof(nlmsghdr)) { |
| 358 | const nlmsghdr *header = reinterpret_cast<const nlmsghdr *>(buf); |
| 359 | SLOG(WiFi, log_level) << StringPrintf( |
| 360 | "len: %02x %02x %02x %02x = %u bytes", |
| 361 | buf[0], buf[1], buf[2], buf[3], header->nlmsg_len); |
| 362 | |
| 363 | SLOG(WiFi, log_level) << StringPrintf( |
| 364 | "type | flags: %02x %02x %02x %02x - type:%u flags:%s%s%s%s%s", |
| 365 | buf[4], buf[5], buf[6], buf[7], header->nlmsg_type, |
| 366 | ((header->nlmsg_flags & NLM_F_REQUEST) ? " REQUEST" : ""), |
| 367 | ((header->nlmsg_flags & NLM_F_MULTI) ? " MULTI" : ""), |
| 368 | ((header->nlmsg_flags & NLM_F_ACK) ? " ACK" : ""), |
| 369 | ((header->nlmsg_flags & NLM_F_ECHO) ? " ECHO" : ""), |
| 370 | ((header->nlmsg_flags & NLM_F_DUMP_INTR) ? " BAD-SEQ" : "")); |
| 371 | |
| 372 | SLOG(WiFi, log_level) << StringPrintf( |
| 373 | "sequence: %02x %02x %02x %02x = %u", |
| 374 | buf[8], buf[9], buf[10], buf[11], header->nlmsg_seq); |
| 375 | SLOG(WiFi, log_level) << StringPrintf( |
| 376 | "pid: %02x %02x %02x %02x = %u", |
| 377 | buf[12], buf[13], buf[14], buf[15], header->nlmsg_pid); |
| 378 | buf += sizeof(nlmsghdr); |
| 379 | num_bytes -= sizeof(nlmsghdr); |
| 380 | } else { |
| 381 | SLOG(WiFi, log_level) << "Not enough bytes (" << num_bytes |
| 382 | << ") for a complete nlmsghdr (requires " |
| 383 | << sizeof(nlmsghdr) << ")."; |
| 384 | } |
| 385 | |
| 386 | while (num_bytes) { |
| 387 | string output; |
| 388 | size_t bytes_this_row = min(num_bytes, static_cast<size_t>(32)); |
| 389 | for (size_t i = 0; i < bytes_this_row; ++i) { |
| 390 | StringAppendF(&output, " %02x", *buf++); |
| 391 | } |
| 392 | SLOG(WiFi, log_level) << output; |
| 393 | num_bytes -= bytes_this_row; |
| 394 | } |
| 395 | } |
| 396 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 397 | // Helper function to provide a string for a MAC address. |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 398 | bool Nl80211Message::GetMacAttributeString(int id, string *value) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 399 | if (!value) { |
| 400 | LOG(ERROR) << "Null |value| parameter"; |
| 401 | return false; |
| 402 | } |
| 403 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 404 | ByteString data; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 405 | if (!attributes().GetRawAttributeValue(id, &data)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 406 | value->assign(kBogusMacAddress); |
| 407 | return false; |
| 408 | } |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 409 | value->assign(StringFromMacAddress(data.GetConstData())); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 410 | |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | // Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 415 | bool Nl80211Message::GetScanFrequenciesAttribute( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 416 | int id, vector<uint32_t> *value) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 417 | if (!value) { |
| 418 | LOG(ERROR) << "Null |value| parameter"; |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | value->clear(); |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 423 | ByteString rawdata; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 424 | if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty()) |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 425 | return false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 426 | |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 427 | nlattr *nst = NULL; |
| 428 | // |nla_for_each_attr| requires a non-const parameter even though it |
| 429 | // doesn't change the data. |
| 430 | nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData()); |
| 431 | int rem_nst; |
| 432 | int len = rawdata.GetLength(); |
| 433 | |
| 434 | nla_for_each_attr(nst, attr_data, len, rem_nst) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 435 | value->push_back(NetlinkAttribute::NlaGetU32(nst)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 436 | } |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 437 | return true; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 441 | bool Nl80211Message::GetScanSsidsAttribute( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 442 | int id, vector<string> *value) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 443 | if (!value) { |
| 444 | LOG(ERROR) << "Null |value| parameter"; |
| 445 | return false; |
| 446 | } |
| 447 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 448 | ByteString rawdata; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 449 | if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty()) |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 450 | return false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 451 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 452 | nlattr *nst = NULL; |
| 453 | // |nla_for_each_attr| requires a non-const parameter even though it |
| 454 | // doesn't change the data. |
| 455 | nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData()); |
| 456 | int rem_nst; |
| 457 | int len = rawdata.GetLength(); |
| 458 | |
| 459 | nla_for_each_attr(nst, data, len, rem_nst) { |
| 460 | value->push_back(StringFromSsid(nla_len(nst), |
| 461 | reinterpret_cast<const uint8_t *>( |
| 462 | nla_data(nst))).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 463 | } |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 464 | return true; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 467 | // Protected members. |
| 468 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 469 | string Nl80211Message::GetHeaderString() const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 470 | char ifname[IF_NAMESIZE] = ""; |
| 471 | uint32_t ifindex = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 472 | bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX, |
| 473 | &ifindex); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 474 | |
| 475 | uint32_t wifi = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 476 | bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, |
| 477 | &wifi); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 478 | |
| 479 | string output; |
| 480 | if (ifindex_exists && wifi_exists) { |
| 481 | StringAppendF(&output, "%s (phy #%" PRIu32 "): ", |
| 482 | (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"), |
| 483 | wifi); |
| 484 | } else if (ifindex_exists) { |
| 485 | StringAppendF(&output, "%s: ", |
| 486 | (if_indextoname(ifindex, ifname) ? ifname : "<unknown>")); |
| 487 | } else if (wifi_exists) { |
| 488 | StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi); |
| 489 | } |
| 490 | |
| 491 | return output; |
| 492 | } |
| 493 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame^] | 494 | string Nl80211Message::StringFromFrame(int attr_name) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 495 | string output; |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 496 | ByteString frame_data; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 497 | if (attributes().GetRawAttributeValue(attr_name, |
| 498 | &frame_data) && !frame_data.IsEmpty()) { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 499 | Nl80211Frame frame(frame_data); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 500 | frame.ToString(&output); |
| 501 | } else { |
| 502 | output.append(" [no frame]"); |
| 503 | } |
| 504 | |
| 505 | return output; |
| 506 | } |
| 507 | |
| 508 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 509 | string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 510 | switch (key_type) { |
| 511 | case NL80211_KEYTYPE_GROUP: |
| 512 | return "Group"; |
| 513 | case NL80211_KEYTYPE_PAIRWISE: |
| 514 | return "Pairwise"; |
| 515 | case NL80211_KEYTYPE_PEERKEY: |
| 516 | return "PeerKey"; |
| 517 | default: |
| 518 | return "<Unknown Key Type>"; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 523 | string Nl80211Message::StringFromMacAddress(const uint8_t *arg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 524 | string output; |
| 525 | |
| 526 | if (!arg) { |
| 527 | output = kBogusMacAddress; |
| 528 | LOG(ERROR) << "|arg| parameter is NULL."; |
| 529 | } else { |
| 530 | StringAppendF(&output, "%02x", arg[0]); |
| 531 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 532 | for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 533 | StringAppendF(&output, ":%02x", arg[i]); |
| 534 | } |
| 535 | } |
| 536 | return output; |
| 537 | } |
| 538 | |
| 539 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 540 | string Nl80211Message::StringFromRegInitiator(__u8 initiator) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 541 | switch (initiator) { |
| 542 | case NL80211_REGDOM_SET_BY_CORE: |
| 543 | return "the wireless core upon initialization"; |
| 544 | case NL80211_REGDOM_SET_BY_USER: |
| 545 | return "a user"; |
| 546 | case NL80211_REGDOM_SET_BY_DRIVER: |
| 547 | return "a driver"; |
| 548 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
| 549 | return "a country IE"; |
| 550 | default: |
| 551 | return "<Unknown Reg Initiator>"; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 556 | string Nl80211Message::StringFromSsid(const uint8_t len, |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 557 | const uint8_t *data) { |
| 558 | string output; |
| 559 | if (!data) { |
| 560 | StringAppendF(&output, "<Error from %s, NULL parameter>", __func__); |
| 561 | LOG(ERROR) << "|data| parameter is NULL."; |
| 562 | return output; |
| 563 | } |
| 564 | |
| 565 | for (int i = 0; i < len; ++i) { |
| 566 | if (data[i] == ' ') |
| 567 | output.append(" "); |
| 568 | else if (isprint(data[i])) |
| 569 | StringAppendF(&output, "%c", static_cast<char>(data[i])); |
| 570 | else |
| 571 | StringAppendF(&output, "\\x%2x", data[i]); |
| 572 | } |
| 573 | |
| 574 | return output; |
| 575 | } |
| 576 | |
| 577 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 578 | string Nl80211Message::StringFromReason(uint16_t status) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 579 | map<uint16_t, string>::const_iterator match; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 580 | match = reason_code_string_->find(status); |
| 581 | if (match == reason_code_string_->end()) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 582 | string output; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 583 | if (status < IEEE_80211::kReasonCodeMax) { |
| 584 | StringAppendF(&output, "<Reserved Reason:%u>", status); |
| 585 | } else { |
| 586 | StringAppendF(&output, "<Unknown Reason:%u>", status); |
| 587 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 588 | return output; |
| 589 | } |
| 590 | return match->second; |
| 591 | } |
| 592 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 593 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 594 | string Nl80211Message::StringFromStatus(uint16_t status) { |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 595 | map<uint16_t, string>::const_iterator match; |
| 596 | match = status_code_string_->find(status); |
| 597 | if (match == status_code_string_->end()) { |
| 598 | string output; |
| 599 | if (status < IEEE_80211::kStatusCodeMax) { |
| 600 | StringAppendF(&output, "<Reserved Status:%u>", status); |
| 601 | } else { |
| 602 | StringAppendF(&output, "<Unknown Status:%u>", status); |
| 603 | } |
| 604 | return output; |
| 605 | } |
| 606 | return match->second; |
| 607 | } |
| 608 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 609 | ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const { |
| 610 | // Build netlink header. |
| 611 | nlmsghdr header; |
| 612 | size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header)); |
| 613 | header.nlmsg_len = nlmsghdr_with_pad; |
| 614 | header.nlmsg_type = nlmsg_type; |
| 615 | header.nlmsg_flags = NLM_F_REQUEST; |
| 616 | header.nlmsg_seq = sequence_number(); |
| 617 | header.nlmsg_pid = getpid(); |
| 618 | |
| 619 | // Build genl message header. |
| 620 | genlmsghdr genl_header; |
| 621 | size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header)); |
| 622 | header.nlmsg_len += genlmsghdr_with_pad; |
| 623 | genl_header.cmd = message_type(); |
| 624 | genl_header.version = 1; |
| 625 | genl_header.reserved = 0; |
| 626 | |
| 627 | // Assemble attributes (padding is included by AttributeList::Encode). |
| 628 | ByteString attributes = attributes_.Encode(); |
| 629 | header.nlmsg_len += attributes.GetLength(); |
| 630 | |
| 631 | // Now that we know the total message size, build the output ByteString. |
| 632 | ByteString result; |
| 633 | |
| 634 | // Netlink header + pad. |
| 635 | result.Append(ByteString(reinterpret_cast<unsigned char *>(&header), |
| 636 | sizeof(header))); |
| 637 | result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any). |
| 638 | |
| 639 | // Genl message header + pad. |
| 640 | result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header), |
| 641 | sizeof(genl_header))); |
| 642 | result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill. |
| 643 | |
| 644 | // Attributes including pad. |
| 645 | result.Append(attributes); |
| 646 | |
| 647 | return result; |
| 648 | } |
| 649 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 650 | Nl80211Frame::Nl80211Frame(const ByteString &raw_frame) |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 651 | : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX), |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 652 | frame_(raw_frame) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 653 | const IEEE_80211::ieee80211_frame *frame = |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 654 | reinterpret_cast<const IEEE_80211::ieee80211_frame *>( |
| 655 | frame_.GetConstData()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 656 | |
| 657 | // Now, let's populate the other stuff. |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 658 | if (frame_.GetLength() >= kMinimumFrameByteCount) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 659 | mac_from_ = |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 660 | Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]); |
| 661 | mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 662 | frame_type_ = frame->frame_control & kFrameTypeMask; |
| 663 | |
| 664 | switch (frame_type_) { |
| 665 | case kAssocResponseFrameType: |
| 666 | case kReassocResponseFrameType: |
| 667 | status_ = le16toh(frame->u.associate_response.status_code); |
| 668 | break; |
| 669 | |
| 670 | case kAuthFrameType: |
| 671 | status_ = le16toh(frame->u.authentiate_message.status_code); |
| 672 | break; |
| 673 | |
| 674 | case kDisassocFrameType: |
| 675 | case kDeauthFrameType: |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 676 | reason_ = le16toh(frame->u.deauthentiate_message.reason_code); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 677 | break; |
| 678 | |
| 679 | default: |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 685 | bool Nl80211Frame::ToString(string *output) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 686 | if (!output) { |
| 687 | LOG(ERROR) << "NULL |output|"; |
| 688 | return false; |
| 689 | } |
| 690 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 691 | if (frame_.IsEmpty()) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 692 | output->append(" [no frame]"); |
| 693 | return true; |
| 694 | } |
| 695 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 696 | if (frame_.GetLength() < kMinimumFrameByteCount) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 697 | output->append(" [invalid frame: "); |
| 698 | } else { |
| 699 | StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str()); |
| 700 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 701 | switch (frame_.GetConstData()[0] & kFrameTypeMask) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 702 | case kAssocResponseFrameType: |
| 703 | StringAppendF(output, "; AssocResponse status: %u: %s", |
| 704 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 705 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 706 | break; |
| 707 | case kReassocResponseFrameType: |
| 708 | StringAppendF(output, "; ReassocResponse status: %u: %s", |
| 709 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 710 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 711 | break; |
| 712 | case kAuthFrameType: |
| 713 | StringAppendF(output, "; Auth status: %u: %s", |
| 714 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 715 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 716 | break; |
| 717 | |
| 718 | case kDisassocFrameType: |
| 719 | StringAppendF(output, "; Disassoc reason %u: %s", |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 720 | reason_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 721 | Nl80211Message::StringFromReason(reason_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 722 | break; |
| 723 | case kDeauthFrameType: |
| 724 | StringAppendF(output, "; Deauth reason %u: %s", |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 725 | reason_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 726 | Nl80211Message::StringFromReason(reason_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 727 | break; |
| 728 | |
| 729 | default: |
| 730 | break; |
| 731 | } |
| 732 | output->append(" [frame: "); |
| 733 | } |
| 734 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 735 | const unsigned char *frame = frame_.GetConstData(); |
| 736 | for (size_t i = 0; i < frame_.GetLength(); ++i) { |
| 737 | StringAppendF(output, "%02x, ", frame[i]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 738 | } |
| 739 | output->append("]"); |
| 740 | |
| 741 | return true; |
| 742 | } |
| 743 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 744 | bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 745 | return frame_.Equals(other.frame_); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 748 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 749 | // |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 750 | // Specific Nl80211Message types. |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 751 | // |
| 752 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 753 | // An Ack is not a GENL message and, as such, has no command. |
| 754 | const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC; |
| 755 | const char AckMessage::kCommandString[] = "NL80211_ACK"; |
| 756 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 757 | const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE; |
| 758 | const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE"; |
| 759 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 760 | const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE; |
| 761 | const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE"; |
| 762 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 763 | const uint8_t CancelRemainOnChannelMessage::kCommand = |
| 764 | NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL; |
| 765 | const char CancelRemainOnChannelMessage::kCommandString[] = |
| 766 | "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL"; |
| 767 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 768 | const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT; |
| 769 | const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT"; |
| 770 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 771 | const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE; |
| 772 | const char DeauthenticateMessage::kCommandString[] = |
| 773 | "NL80211_CMD_DEAUTHENTICATE"; |
| 774 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 775 | const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION; |
| 776 | const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION"; |
| 777 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 778 | const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE; |
| 779 | const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE"; |
| 780 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 781 | const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT; |
| 782 | const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT"; |
| 783 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 784 | // An Error is not a GENL message and, as such, has no command. |
| 785 | const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC; |
| 786 | const char ErrorMessage::kCommandString[] = "NL80211_ERROR"; |
| 787 | |
| 788 | ErrorMessage::ErrorMessage(uint32_t error) |
| 789 | : Nl80211Message(kCommand, kCommandString), error_(error) {} |
| 790 | |
| 791 | string ErrorMessage::ToString() const { |
| 792 | string output; |
| 793 | StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s", |
| 794 | error_, strerror(error_)); |
| 795 | return output; |
| 796 | } |
| 797 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 798 | const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS; |
| 799 | const char FrameTxStatusMessage::kCommandString[] = |
| 800 | "NL80211_CMD_FRAME_TX_STATUS"; |
| 801 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 802 | const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG; |
| 803 | const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG"; |
| 804 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 805 | const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS; |
| 806 | const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS"; |
| 807 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 808 | const uint8_t MichaelMicFailureMessage::kCommand = |
| 809 | NL80211_CMD_MICHAEL_MIC_FAILURE; |
| 810 | const char MichaelMicFailureMessage::kCommandString[] = |
| 811 | "NL80211_CMD_MICHAEL_MIC_FAILURE"; |
| 812 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 813 | const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS; |
| 814 | const char NewScanResultsMessage::kCommandString[] = |
| 815 | "NL80211_CMD_NEW_SCAN_RESULTS"; |
| 816 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 817 | const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION; |
| 818 | const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION"; |
| 819 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 820 | const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY; |
| 821 | const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY"; |
| 822 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 823 | // A NOOP is not a GENL message and, as such, has no command. |
| 824 | const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC; |
| 825 | const char NoopMessage::kCommandString[] = "NL80211_NOOP"; |
| 826 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 827 | const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM; |
| 828 | const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM"; |
| 829 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 830 | const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE; |
| 831 | const char PmksaCandidateMessage::kCommandString[] = |
| 832 | "NL80211_ATTR_PMKSA_CANDIDATE"; |
| 833 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 834 | const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT; |
| 835 | const char RegBeaconHintMessage::kCommandString[] = |
| 836 | "NL80211_CMD_REG_BEACON_HINT"; |
| 837 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 838 | const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE; |
| 839 | const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE"; |
| 840 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 841 | const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL; |
| 842 | const char RemainOnChannelMessage::kCommandString[] = |
| 843 | "NL80211_CMD_REMAIN_ON_CHANNEL"; |
| 844 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 845 | const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM; |
| 846 | const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM"; |
| 847 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 848 | const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED; |
| 849 | const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED"; |
| 850 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 851 | const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN; |
| 852 | const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN"; |
| 853 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 854 | const uint8_t UnknownMessage::kCommand = 0xff; |
| 855 | const char UnknownMessage::kCommandString[] = "<Unknown Message Type>"; |
| 856 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 857 | const uint8_t UnprotDeauthenticateMessage::kCommand = |
| 858 | NL80211_CMD_UNPROT_DEAUTHENTICATE; |
| 859 | const char UnprotDeauthenticateMessage::kCommandString[] = |
| 860 | "NL80211_CMD_UNPROT_DEAUTHENTICATE"; |
| 861 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 862 | const uint8_t UnprotDisassociateMessage::kCommand = |
| 863 | NL80211_CMD_UNPROT_DISASSOCIATE; |
| 864 | const char UnprotDisassociateMessage::kCommandString[] = |
| 865 | "NL80211_CMD_UNPROT_DISASSOCIATE"; |
| 866 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 867 | // |
| 868 | // Factory class. |
| 869 | // |
| 870 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 871 | Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 872 | if (!msg) { |
| 873 | LOG(ERROR) << "NULL |msg| parameter"; |
| 874 | return NULL; |
| 875 | } |
| 876 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 877 | scoped_ptr<Nl80211Message> message; |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 878 | void *payload = nlmsg_data(msg); |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 879 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 880 | if (msg->nlmsg_type == NLMSG_NOOP) { |
| 881 | SLOG(WiFi, 6) << "Creating a NOP message"; |
| 882 | message.reset(new NoopMessage()); |
| 883 | } else if (msg->nlmsg_type == NLMSG_ERROR) { |
| 884 | uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload)); |
| 885 | if (error_code) { |
| 886 | SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code; |
| 887 | message.reset(new ErrorMessage(error_code)); |
| 888 | } else { |
| 889 | SLOG(WiFi, 6) << "Creating an ACK message"; |
| 890 | message.reset(new AckMessage()); |
| 891 | } |
| 892 | } else { |
| 893 | SLOG(WiFi, 6) << "Creating a Regular message"; |
| 894 | genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 895 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 896 | switch (gnlh->cmd) { |
| 897 | case AssociateMessage::kCommand: |
| 898 | message.reset(new AssociateMessage()); break; |
| 899 | case AuthenticateMessage::kCommand: |
| 900 | message.reset(new AuthenticateMessage()); break; |
| 901 | case CancelRemainOnChannelMessage::kCommand: |
| 902 | message.reset(new CancelRemainOnChannelMessage()); break; |
| 903 | case ConnectMessage::kCommand: |
| 904 | message.reset(new ConnectMessage()); break; |
| 905 | case DeauthenticateMessage::kCommand: |
| 906 | message.reset(new DeauthenticateMessage()); break; |
| 907 | case DeleteStationMessage::kCommand: |
| 908 | message.reset(new DeleteStationMessage()); break; |
| 909 | case DisassociateMessage::kCommand: |
| 910 | message.reset(new DisassociateMessage()); break; |
| 911 | case DisconnectMessage::kCommand: |
| 912 | message.reset(new DisconnectMessage()); break; |
| 913 | case FrameTxStatusMessage::kCommand: |
| 914 | message.reset(new FrameTxStatusMessage()); break; |
| 915 | case GetRegMessage::kCommand: |
| 916 | message.reset(new GetRegMessage()); break; |
| 917 | case JoinIbssMessage::kCommand: |
| 918 | message.reset(new JoinIbssMessage()); break; |
| 919 | case MichaelMicFailureMessage::kCommand: |
| 920 | message.reset(new MichaelMicFailureMessage()); break; |
| 921 | case NewScanResultsMessage::kCommand: |
| 922 | message.reset(new NewScanResultsMessage()); break; |
| 923 | case NewStationMessage::kCommand: |
| 924 | message.reset(new NewStationMessage()); break; |
| 925 | case NewWifiMessage::kCommand: |
| 926 | message.reset(new NewWifiMessage()); break; |
| 927 | case NotifyCqmMessage::kCommand: |
| 928 | message.reset(new NotifyCqmMessage()); break; |
| 929 | case PmksaCandidateMessage::kCommand: |
| 930 | message.reset(new PmksaCandidateMessage()); break; |
| 931 | case RegBeaconHintMessage::kCommand: |
| 932 | message.reset(new RegBeaconHintMessage()); break; |
| 933 | case RegChangeMessage::kCommand: |
| 934 | message.reset(new RegChangeMessage()); break; |
| 935 | case RemainOnChannelMessage::kCommand: |
| 936 | message.reset(new RemainOnChannelMessage()); break; |
| 937 | case RoamMessage::kCommand: |
| 938 | message.reset(new RoamMessage()); break; |
| 939 | case ScanAbortedMessage::kCommand: |
| 940 | message.reset(new ScanAbortedMessage()); break; |
| 941 | case TriggerScanMessage::kCommand: |
| 942 | message.reset(new TriggerScanMessage()); break; |
| 943 | case UnprotDeauthenticateMessage::kCommand: |
| 944 | message.reset(new UnprotDeauthenticateMessage()); break; |
| 945 | case UnprotDisassociateMessage::kCommand: |
| 946 | message.reset(new UnprotDisassociateMessage()); break; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 947 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 948 | default: |
| 949 | message.reset(new UnknownMessage(gnlh->cmd)); break; |
| 950 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 951 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 952 | if (!message->InitFromNlmsg(msg)) { |
| 953 | LOG(ERROR) << "Message did not initialize properly"; |
| 954 | return NULL; |
| 955 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 958 | return message.release(); |
| 959 | } |
| 960 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 961 | Nl80211MessageDataCollector * |
| 962 | Nl80211MessageDataCollector::GetInstance() { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 963 | return g_datacollector.Pointer(); |
| 964 | } |
| 965 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 966 | Nl80211MessageDataCollector::Nl80211MessageDataCollector() { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 967 | need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true; |
| 968 | need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true; |
| 969 | need_to_print[NL80211_CMD_DEL_STATION] = true; |
| 970 | need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true; |
| 971 | need_to_print[NL80211_CMD_JOIN_IBSS] = true; |
| 972 | need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true; |
| 973 | need_to_print[NL80211_CMD_NEW_WIPHY] = true; |
| 974 | need_to_print[NL80211_CMD_REG_BEACON_HINT] = true; |
| 975 | need_to_print[NL80211_CMD_REG_CHANGE] = true; |
| 976 | need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true; |
| 977 | need_to_print[NL80211_CMD_ROAM] = true; |
| 978 | need_to_print[NL80211_CMD_SCAN_ABORTED] = true; |
| 979 | need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true; |
| 980 | need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true; |
| 981 | } |
| 982 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 983 | void Nl80211MessageDataCollector::CollectDebugData( |
| 984 | const Nl80211Message &message, nlmsghdr *msg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 985 | if (!msg) { |
| 986 | LOG(ERROR) << "NULL |msg| parameter"; |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | bool doit = false; |
| 991 | |
| 992 | map<uint8_t, bool>::const_iterator node; |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 993 | node = need_to_print.find(message.message_type()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 994 | if (node != need_to_print.end()) |
| 995 | doit = node->second; |
| 996 | |
| 997 | if (doit) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 998 | LOG(INFO) << "@@const unsigned char " |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 999 | << "k" << message.message_type_string() |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1000 | << "[] = {"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1001 | |
Christopher Wiley | efd521f | 2012-11-07 17:32:46 -0800 | [diff] [blame] | 1002 | int payload_bytes = nlmsg_datalen(msg); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1003 | |
| 1004 | size_t bytes = nlmsg_total_size(payload_bytes); |
| 1005 | unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg); |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 1006 | for (size_t i = 0; i < bytes; ++i) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1007 | LOG(INFO) << " 0x" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1008 | << std::hex << std::setfill('0') << std::setw(2) |
| 1009 | << + rawdata[i] << ","; |
| 1010 | } |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1011 | LOG(INFO) << "};"; |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 1012 | need_to_print[message.message_type()] = false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | } // namespace shill. |