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