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()); |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 115 | attributes_->Print(log_level, 1); |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 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 | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 145 | attributes_->CreateAndInitAttribute( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 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; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 405 | if (!const_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; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 424 | if (!const_attributes()->GetRawAttributeValue(id, &rawdata) && |
| 425 | !rawdata.IsEmpty()) |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 426 | return false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 427 | |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 428 | nlattr *nst = NULL; |
| 429 | // |nla_for_each_attr| requires a non-const parameter even though it |
| 430 | // doesn't change the data. |
| 431 | nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData()); |
| 432 | int rem_nst; |
| 433 | int len = rawdata.GetLength(); |
| 434 | |
| 435 | nla_for_each_attr(nst, attr_data, len, rem_nst) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 436 | value->push_back(NetlinkAttribute::NlaGetU32(nst)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 437 | } |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 438 | return true; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 442 | bool Nl80211Message::GetScanSsidsAttribute( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 443 | int id, vector<string> *value) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 444 | if (!value) { |
| 445 | LOG(ERROR) << "Null |value| parameter"; |
| 446 | return false; |
| 447 | } |
| 448 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 449 | ByteString rawdata; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 450 | if (!const_attributes()->GetRawAttributeValue(id, &rawdata) || |
| 451 | rawdata.IsEmpty()) |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 452 | return false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 453 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 454 | nlattr *nst = NULL; |
| 455 | // |nla_for_each_attr| requires a non-const parameter even though it |
| 456 | // doesn't change the data. |
| 457 | nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData()); |
| 458 | int rem_nst; |
| 459 | int len = rawdata.GetLength(); |
| 460 | |
| 461 | nla_for_each_attr(nst, data, len, rem_nst) { |
| 462 | value->push_back(StringFromSsid(nla_len(nst), |
| 463 | reinterpret_cast<const uint8_t *>( |
| 464 | nla_data(nst))).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 465 | } |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 466 | return true; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 469 | // Protected members. |
| 470 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 471 | string Nl80211Message::GetHeaderString() const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 472 | char ifname[IF_NAMESIZE] = ""; |
| 473 | uint32_t ifindex = UINT32_MAX; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 474 | bool ifindex_exists = const_attributes()->GetU32AttributeValue( |
| 475 | NL80211_ATTR_IFINDEX, &ifindex); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 476 | |
| 477 | uint32_t wifi = UINT32_MAX; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 478 | bool wifi_exists = const_attributes()->GetU32AttributeValue( |
| 479 | NL80211_ATTR_WIPHY, &wifi); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 480 | |
| 481 | string output; |
| 482 | if (ifindex_exists && wifi_exists) { |
| 483 | StringAppendF(&output, "%s (phy #%" PRIu32 "): ", |
| 484 | (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"), |
| 485 | wifi); |
| 486 | } else if (ifindex_exists) { |
| 487 | StringAppendF(&output, "%s: ", |
| 488 | (if_indextoname(ifindex, ifname) ? ifname : "<unknown>")); |
| 489 | } else if (wifi_exists) { |
| 490 | StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi); |
| 491 | } |
| 492 | |
| 493 | return output; |
| 494 | } |
| 495 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 496 | string Nl80211Message::StringFromFrame(int attr_name) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 497 | string output; |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 498 | ByteString frame_data; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 499 | if (const_attributes()->GetRawAttributeValue(attr_name, &frame_data) && |
| 500 | !frame_data.IsEmpty()) { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 501 | Nl80211Frame frame(frame_data); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 502 | frame.ToString(&output); |
| 503 | } else { |
| 504 | output.append(" [no frame]"); |
| 505 | } |
| 506 | |
| 507 | return output; |
| 508 | } |
| 509 | |
| 510 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 511 | string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 512 | switch (key_type) { |
| 513 | case NL80211_KEYTYPE_GROUP: |
| 514 | return "Group"; |
| 515 | case NL80211_KEYTYPE_PAIRWISE: |
| 516 | return "Pairwise"; |
| 517 | case NL80211_KEYTYPE_PEERKEY: |
| 518 | return "PeerKey"; |
| 519 | default: |
| 520 | return "<Unknown Key Type>"; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 525 | string Nl80211Message::StringFromMacAddress(const uint8_t *arg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 526 | string output; |
| 527 | |
| 528 | if (!arg) { |
| 529 | output = kBogusMacAddress; |
| 530 | LOG(ERROR) << "|arg| parameter is NULL."; |
| 531 | } else { |
| 532 | StringAppendF(&output, "%02x", arg[0]); |
| 533 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 534 | for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 535 | StringAppendF(&output, ":%02x", arg[i]); |
| 536 | } |
| 537 | } |
| 538 | return output; |
| 539 | } |
| 540 | |
| 541 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 542 | string Nl80211Message::StringFromRegInitiator(__u8 initiator) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 543 | switch (initiator) { |
| 544 | case NL80211_REGDOM_SET_BY_CORE: |
| 545 | return "the wireless core upon initialization"; |
| 546 | case NL80211_REGDOM_SET_BY_USER: |
| 547 | return "a user"; |
| 548 | case NL80211_REGDOM_SET_BY_DRIVER: |
| 549 | return "a driver"; |
| 550 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
| 551 | return "a country IE"; |
| 552 | default: |
| 553 | return "<Unknown Reg Initiator>"; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 558 | string Nl80211Message::StringFromSsid(const uint8_t len, |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 559 | const uint8_t *data) { |
| 560 | string output; |
| 561 | if (!data) { |
| 562 | StringAppendF(&output, "<Error from %s, NULL parameter>", __func__); |
| 563 | LOG(ERROR) << "|data| parameter is NULL."; |
| 564 | return output; |
| 565 | } |
| 566 | |
| 567 | for (int i = 0; i < len; ++i) { |
| 568 | if (data[i] == ' ') |
| 569 | output.append(" "); |
| 570 | else if (isprint(data[i])) |
| 571 | StringAppendF(&output, "%c", static_cast<char>(data[i])); |
| 572 | else |
| 573 | StringAppendF(&output, "\\x%2x", data[i]); |
| 574 | } |
| 575 | |
| 576 | return output; |
| 577 | } |
| 578 | |
| 579 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 580 | string Nl80211Message::StringFromReason(uint16_t status) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 581 | map<uint16_t, string>::const_iterator match; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 582 | match = reason_code_string_->find(status); |
| 583 | if (match == reason_code_string_->end()) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 584 | string output; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 585 | if (status < IEEE_80211::kReasonCodeMax) { |
| 586 | StringAppendF(&output, "<Reserved Reason:%u>", status); |
| 587 | } else { |
| 588 | StringAppendF(&output, "<Unknown Reason:%u>", status); |
| 589 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 590 | return output; |
| 591 | } |
| 592 | return match->second; |
| 593 | } |
| 594 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 595 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 596 | string Nl80211Message::StringFromStatus(uint16_t status) { |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 597 | map<uint16_t, string>::const_iterator match; |
| 598 | match = status_code_string_->find(status); |
| 599 | if (match == status_code_string_->end()) { |
| 600 | string output; |
| 601 | if (status < IEEE_80211::kStatusCodeMax) { |
| 602 | StringAppendF(&output, "<Reserved Status:%u>", status); |
| 603 | } else { |
| 604 | StringAppendF(&output, "<Unknown Status:%u>", status); |
| 605 | } |
| 606 | return output; |
| 607 | } |
| 608 | return match->second; |
| 609 | } |
| 610 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 611 | ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const { |
| 612 | // Build netlink header. |
| 613 | nlmsghdr header; |
| 614 | size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header)); |
| 615 | header.nlmsg_len = nlmsghdr_with_pad; |
| 616 | header.nlmsg_type = nlmsg_type; |
| 617 | header.nlmsg_flags = NLM_F_REQUEST; |
| 618 | header.nlmsg_seq = sequence_number(); |
| 619 | header.nlmsg_pid = getpid(); |
| 620 | |
| 621 | // Build genl message header. |
| 622 | genlmsghdr genl_header; |
| 623 | size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header)); |
| 624 | header.nlmsg_len += genlmsghdr_with_pad; |
| 625 | genl_header.cmd = message_type(); |
| 626 | genl_header.version = 1; |
| 627 | genl_header.reserved = 0; |
| 628 | |
| 629 | // Assemble attributes (padding is included by AttributeList::Encode). |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 630 | ByteString attribute_bytes = attributes_->Encode(); |
| 631 | header.nlmsg_len += attribute_bytes.GetLength(); |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 632 | |
| 633 | // Now that we know the total message size, build the output ByteString. |
| 634 | ByteString result; |
| 635 | |
| 636 | // Netlink header + pad. |
| 637 | result.Append(ByteString(reinterpret_cast<unsigned char *>(&header), |
| 638 | sizeof(header))); |
| 639 | result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any). |
| 640 | |
| 641 | // Genl message header + pad. |
| 642 | result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header), |
| 643 | sizeof(genl_header))); |
| 644 | result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill. |
| 645 | |
| 646 | // Attributes including pad. |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 647 | result.Append(attribute_bytes); |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 648 | |
| 649 | return result; |
| 650 | } |
| 651 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 652 | Nl80211Frame::Nl80211Frame(const ByteString &raw_frame) |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 653 | : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX), |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 654 | frame_(raw_frame) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 655 | const IEEE_80211::ieee80211_frame *frame = |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 656 | reinterpret_cast<const IEEE_80211::ieee80211_frame *>( |
| 657 | frame_.GetConstData()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 658 | |
| 659 | // Now, let's populate the other stuff. |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 660 | if (frame_.GetLength() >= kMinimumFrameByteCount) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 661 | mac_from_ = |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 662 | Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]); |
| 663 | mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 664 | frame_type_ = frame->frame_control & kFrameTypeMask; |
| 665 | |
| 666 | switch (frame_type_) { |
| 667 | case kAssocResponseFrameType: |
| 668 | case kReassocResponseFrameType: |
| 669 | status_ = le16toh(frame->u.associate_response.status_code); |
| 670 | break; |
| 671 | |
| 672 | case kAuthFrameType: |
| 673 | status_ = le16toh(frame->u.authentiate_message.status_code); |
| 674 | break; |
| 675 | |
| 676 | case kDisassocFrameType: |
| 677 | case kDeauthFrameType: |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 678 | reason_ = le16toh(frame->u.deauthentiate_message.reason_code); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 679 | break; |
| 680 | |
| 681 | default: |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 687 | bool Nl80211Frame::ToString(string *output) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 688 | if (!output) { |
| 689 | LOG(ERROR) << "NULL |output|"; |
| 690 | return false; |
| 691 | } |
| 692 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 693 | if (frame_.IsEmpty()) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 694 | output->append(" [no frame]"); |
| 695 | return true; |
| 696 | } |
| 697 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 698 | if (frame_.GetLength() < kMinimumFrameByteCount) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 699 | output->append(" [invalid frame: "); |
| 700 | } else { |
| 701 | StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str()); |
| 702 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 703 | switch (frame_.GetConstData()[0] & kFrameTypeMask) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 704 | case kAssocResponseFrameType: |
| 705 | StringAppendF(output, "; AssocResponse status: %u: %s", |
| 706 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 707 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 708 | break; |
| 709 | case kReassocResponseFrameType: |
| 710 | StringAppendF(output, "; ReassocResponse status: %u: %s", |
| 711 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 712 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 713 | break; |
| 714 | case kAuthFrameType: |
| 715 | StringAppendF(output, "; Auth status: %u: %s", |
| 716 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 717 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 718 | break; |
| 719 | |
| 720 | case kDisassocFrameType: |
| 721 | StringAppendF(output, "; Disassoc reason %u: %s", |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 722 | reason_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 723 | Nl80211Message::StringFromReason(reason_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 724 | break; |
| 725 | case kDeauthFrameType: |
| 726 | StringAppendF(output, "; Deauth reason %u: %s", |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 727 | reason_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 728 | Nl80211Message::StringFromReason(reason_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 729 | break; |
| 730 | |
| 731 | default: |
| 732 | break; |
| 733 | } |
| 734 | output->append(" [frame: "); |
| 735 | } |
| 736 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 737 | const unsigned char *frame = frame_.GetConstData(); |
| 738 | for (size_t i = 0; i < frame_.GetLength(); ++i) { |
| 739 | StringAppendF(output, "%02x, ", frame[i]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 740 | } |
| 741 | output->append("]"); |
| 742 | |
| 743 | return true; |
| 744 | } |
| 745 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 746 | bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 747 | return frame_.Equals(other.frame_); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 750 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 751 | // |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 752 | // Specific Nl80211Message types. |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 753 | // |
| 754 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 755 | // An Ack is not a GENL message and, as such, has no command. |
| 756 | const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC; |
| 757 | const char AckMessage::kCommandString[] = "NL80211_ACK"; |
| 758 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 759 | const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE; |
| 760 | const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE"; |
| 761 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 762 | const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE; |
| 763 | const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE"; |
| 764 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 765 | const uint8_t CancelRemainOnChannelMessage::kCommand = |
| 766 | NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL; |
| 767 | const char CancelRemainOnChannelMessage::kCommandString[] = |
| 768 | "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL"; |
| 769 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 770 | const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT; |
| 771 | const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT"; |
| 772 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 773 | const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE; |
| 774 | const char DeauthenticateMessage::kCommandString[] = |
| 775 | "NL80211_CMD_DEAUTHENTICATE"; |
| 776 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 777 | const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION; |
| 778 | const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION"; |
| 779 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 780 | const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE; |
| 781 | const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE"; |
| 782 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 783 | const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT; |
| 784 | const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT"; |
| 785 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 786 | // An Error is not a GENL message and, as such, has no command. |
| 787 | const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC; |
| 788 | const char ErrorMessage::kCommandString[] = "NL80211_ERROR"; |
| 789 | |
| 790 | ErrorMessage::ErrorMessage(uint32_t error) |
| 791 | : Nl80211Message(kCommand, kCommandString), error_(error) {} |
| 792 | |
| 793 | string ErrorMessage::ToString() const { |
| 794 | string output; |
| 795 | StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s", |
| 796 | error_, strerror(error_)); |
| 797 | return output; |
| 798 | } |
| 799 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 800 | const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS; |
| 801 | const char FrameTxStatusMessage::kCommandString[] = |
| 802 | "NL80211_CMD_FRAME_TX_STATUS"; |
| 803 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 804 | const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG; |
| 805 | const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG"; |
| 806 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 807 | const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS; |
| 808 | const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS"; |
| 809 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 810 | const uint8_t MichaelMicFailureMessage::kCommand = |
| 811 | NL80211_CMD_MICHAEL_MIC_FAILURE; |
| 812 | const char MichaelMicFailureMessage::kCommandString[] = |
| 813 | "NL80211_CMD_MICHAEL_MIC_FAILURE"; |
| 814 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 815 | const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS; |
| 816 | const char NewScanResultsMessage::kCommandString[] = |
| 817 | "NL80211_CMD_NEW_SCAN_RESULTS"; |
| 818 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 819 | const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION; |
| 820 | const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION"; |
| 821 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 822 | const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY; |
| 823 | const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY"; |
| 824 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 825 | // A NOOP is not a GENL message and, as such, has no command. |
| 826 | const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC; |
| 827 | const char NoopMessage::kCommandString[] = "NL80211_NOOP"; |
| 828 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 829 | const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM; |
| 830 | const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM"; |
| 831 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 832 | const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE; |
| 833 | const char PmksaCandidateMessage::kCommandString[] = |
| 834 | "NL80211_ATTR_PMKSA_CANDIDATE"; |
| 835 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 836 | const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT; |
| 837 | const char RegBeaconHintMessage::kCommandString[] = |
| 838 | "NL80211_CMD_REG_BEACON_HINT"; |
| 839 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 840 | const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE; |
| 841 | const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE"; |
| 842 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 843 | const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL; |
| 844 | const char RemainOnChannelMessage::kCommandString[] = |
| 845 | "NL80211_CMD_REMAIN_ON_CHANNEL"; |
| 846 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 847 | const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM; |
| 848 | const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM"; |
| 849 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 850 | const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED; |
| 851 | const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED"; |
| 852 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 853 | const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN; |
| 854 | const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN"; |
| 855 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 856 | const uint8_t UnknownMessage::kCommand = 0xff; |
| 857 | const char UnknownMessage::kCommandString[] = "<Unknown Message Type>"; |
| 858 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 859 | const uint8_t UnprotDeauthenticateMessage::kCommand = |
| 860 | NL80211_CMD_UNPROT_DEAUTHENTICATE; |
| 861 | const char UnprotDeauthenticateMessage::kCommandString[] = |
| 862 | "NL80211_CMD_UNPROT_DEAUTHENTICATE"; |
| 863 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 864 | const uint8_t UnprotDisassociateMessage::kCommand = |
| 865 | NL80211_CMD_UNPROT_DISASSOCIATE; |
| 866 | const char UnprotDisassociateMessage::kCommandString[] = |
| 867 | "NL80211_CMD_UNPROT_DISASSOCIATE"; |
| 868 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 869 | // |
| 870 | // Factory class. |
| 871 | // |
| 872 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 873 | Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 874 | if (!msg) { |
| 875 | LOG(ERROR) << "NULL |msg| parameter"; |
| 876 | return NULL; |
| 877 | } |
| 878 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 879 | scoped_ptr<Nl80211Message> message; |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 880 | void *payload = nlmsg_data(msg); |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 881 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 882 | if (msg->nlmsg_type == NLMSG_NOOP) { |
| 883 | SLOG(WiFi, 6) << "Creating a NOP message"; |
| 884 | message.reset(new NoopMessage()); |
| 885 | } else if (msg->nlmsg_type == NLMSG_ERROR) { |
| 886 | uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload)); |
| 887 | if (error_code) { |
| 888 | SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code; |
| 889 | message.reset(new ErrorMessage(error_code)); |
| 890 | } else { |
| 891 | SLOG(WiFi, 6) << "Creating an ACK message"; |
| 892 | message.reset(new AckMessage()); |
| 893 | } |
| 894 | } else { |
| 895 | SLOG(WiFi, 6) << "Creating a Regular message"; |
| 896 | genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 897 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 898 | switch (gnlh->cmd) { |
| 899 | case AssociateMessage::kCommand: |
| 900 | message.reset(new AssociateMessage()); break; |
| 901 | case AuthenticateMessage::kCommand: |
| 902 | message.reset(new AuthenticateMessage()); break; |
| 903 | case CancelRemainOnChannelMessage::kCommand: |
| 904 | message.reset(new CancelRemainOnChannelMessage()); break; |
| 905 | case ConnectMessage::kCommand: |
| 906 | message.reset(new ConnectMessage()); break; |
| 907 | case DeauthenticateMessage::kCommand: |
| 908 | message.reset(new DeauthenticateMessage()); break; |
| 909 | case DeleteStationMessage::kCommand: |
| 910 | message.reset(new DeleteStationMessage()); break; |
| 911 | case DisassociateMessage::kCommand: |
| 912 | message.reset(new DisassociateMessage()); break; |
| 913 | case DisconnectMessage::kCommand: |
| 914 | message.reset(new DisconnectMessage()); break; |
| 915 | case FrameTxStatusMessage::kCommand: |
| 916 | message.reset(new FrameTxStatusMessage()); break; |
| 917 | case GetRegMessage::kCommand: |
| 918 | message.reset(new GetRegMessage()); break; |
| 919 | case JoinIbssMessage::kCommand: |
| 920 | message.reset(new JoinIbssMessage()); break; |
| 921 | case MichaelMicFailureMessage::kCommand: |
| 922 | message.reset(new MichaelMicFailureMessage()); break; |
| 923 | case NewScanResultsMessage::kCommand: |
| 924 | message.reset(new NewScanResultsMessage()); break; |
| 925 | case NewStationMessage::kCommand: |
| 926 | message.reset(new NewStationMessage()); break; |
| 927 | case NewWifiMessage::kCommand: |
| 928 | message.reset(new NewWifiMessage()); break; |
| 929 | case NotifyCqmMessage::kCommand: |
| 930 | message.reset(new NotifyCqmMessage()); break; |
| 931 | case PmksaCandidateMessage::kCommand: |
| 932 | message.reset(new PmksaCandidateMessage()); break; |
| 933 | case RegBeaconHintMessage::kCommand: |
| 934 | message.reset(new RegBeaconHintMessage()); break; |
| 935 | case RegChangeMessage::kCommand: |
| 936 | message.reset(new RegChangeMessage()); break; |
| 937 | case RemainOnChannelMessage::kCommand: |
| 938 | message.reset(new RemainOnChannelMessage()); break; |
| 939 | case RoamMessage::kCommand: |
| 940 | message.reset(new RoamMessage()); break; |
| 941 | case ScanAbortedMessage::kCommand: |
| 942 | message.reset(new ScanAbortedMessage()); break; |
| 943 | case TriggerScanMessage::kCommand: |
| 944 | message.reset(new TriggerScanMessage()); break; |
| 945 | case UnprotDeauthenticateMessage::kCommand: |
| 946 | message.reset(new UnprotDeauthenticateMessage()); break; |
| 947 | case UnprotDisassociateMessage::kCommand: |
| 948 | message.reset(new UnprotDisassociateMessage()); break; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 949 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 950 | default: |
| 951 | message.reset(new UnknownMessage(gnlh->cmd)); break; |
| 952 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 953 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 954 | if (!message->InitFromNlmsg(msg)) { |
| 955 | LOG(ERROR) << "Message did not initialize properly"; |
| 956 | return NULL; |
| 957 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 960 | return message.release(); |
| 961 | } |
| 962 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 963 | Nl80211MessageDataCollector * |
| 964 | Nl80211MessageDataCollector::GetInstance() { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 965 | return g_datacollector.Pointer(); |
| 966 | } |
| 967 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 968 | Nl80211MessageDataCollector::Nl80211MessageDataCollector() { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 969 | need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true; |
| 970 | need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true; |
| 971 | need_to_print[NL80211_CMD_DEL_STATION] = true; |
| 972 | need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true; |
| 973 | need_to_print[NL80211_CMD_JOIN_IBSS] = true; |
| 974 | need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true; |
| 975 | need_to_print[NL80211_CMD_NEW_WIPHY] = true; |
| 976 | need_to_print[NL80211_CMD_REG_BEACON_HINT] = true; |
| 977 | need_to_print[NL80211_CMD_REG_CHANGE] = true; |
| 978 | need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true; |
| 979 | need_to_print[NL80211_CMD_ROAM] = true; |
| 980 | need_to_print[NL80211_CMD_SCAN_ABORTED] = true; |
| 981 | need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true; |
| 982 | need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true; |
| 983 | } |
| 984 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 985 | void Nl80211MessageDataCollector::CollectDebugData( |
| 986 | const Nl80211Message &message, nlmsghdr *msg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 987 | if (!msg) { |
| 988 | LOG(ERROR) << "NULL |msg| parameter"; |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | bool doit = false; |
| 993 | |
| 994 | map<uint8_t, bool>::const_iterator node; |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 995 | node = need_to_print.find(message.message_type()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 996 | if (node != need_to_print.end()) |
| 997 | doit = node->second; |
| 998 | |
| 999 | if (doit) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1000 | LOG(INFO) << "@@const unsigned char " |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 1001 | << "k" << message.message_type_string() |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1002 | << "[] = {"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1003 | |
Christopher Wiley | efd521f | 2012-11-07 17:32:46 -0800 | [diff] [blame] | 1004 | int payload_bytes = nlmsg_datalen(msg); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1005 | |
| 1006 | size_t bytes = nlmsg_total_size(payload_bytes); |
| 1007 | unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg); |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 1008 | for (size_t i = 0; i < bytes; ++i) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1009 | LOG(INFO) << " 0x" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1010 | << std::hex << std::setfill('0') << std::setw(2) |
| 1011 | << + rawdata[i] << ","; |
| 1012 | } |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1013 | LOG(INFO) << "};"; |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 1014 | need_to_print[message.message_type()] = false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | } // namespace shill. |