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 | |
| 40 | #include <iomanip> |
| 41 | #include <string> |
| 42 | |
| 43 | #include <base/format_macros.h> |
| 44 | #include <base/stl_util.h> |
| 45 | #include <base/stringprintf.h> |
| 46 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 47 | #include "shill/attribute_list.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 48 | #include "shill/ieee80211.h" |
| 49 | #include "shill/logging.h" |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 50 | #include "shill/netlink_socket.h" |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 51 | #include "shill/nl80211_attribute.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 52 | #include "shill/scope_logger.h" |
| 53 | |
| 54 | using base::LazyInstance; |
| 55 | using base::StringAppendF; |
| 56 | using base::StringPrintf; |
| 57 | using std::map; |
| 58 | using std::string; |
| 59 | using std::vector; |
| 60 | |
| 61 | namespace shill { |
| 62 | |
| 63 | namespace { |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 64 | LazyInstance<Nl80211MessageDataCollector> g_datacollector = |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 65 | LAZY_INSTANCE_INITIALIZER; |
| 66 | } // namespace |
| 67 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 68 | const char Nl80211Message::kBogusMacAddress[]="XX:XX:XX:XX:XX:XX"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 69 | |
| 70 | const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26; |
| 71 | const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc; |
| 72 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 73 | const uint32_t Nl80211Message::kIllegalMessage = 0; |
| 74 | const unsigned int Nl80211Message::kEthernetAddressBytes = 6; |
| 75 | map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL; |
| 76 | map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 77 | |
| 78 | // The nl messages look like this: |
| 79 | // |
| 80 | // XXXXXXXXXXXXXXXXXXX-nlmsg_total_size-XXXXXXXXXXXXXXXXXXX |
| 81 | // XXXXXXXXXXXXXXXXXXX-nlmsg_msg_size-XXXXXXXXXXXXXXXXXXX |
| 82 | // +-- gnhl nlmsg_tail(hdr) --+ |
| 83 | // | nlmsg_next(hdr) --+ |
| 84 | // v XXXXXXXXXXXX-nlmsg_len-XXXXXXXXXXXXXX V |
| 85 | // -----+-----+-+----------------------------------------------+-++---- |
| 86 | // ... | | | payload | || |
| 87 | // | | +------+-+--------+-+--------------------------+ || |
| 88 | // | nl | | | | | | attribs | || |
| 89 | // | msg |p| genl |p| family |p+------+-+-------+-+-------+p|| ... |
| 90 | // | hdr |a| msg |a| header |a| nl |p| pay |p| |a|| |
| 91 | // | |d| hdr |d| |d| attr |a| load |a| ... |d|| |
| 92 | // | | | | | | | |d| |d| | || |
| 93 | // -----+-----+-+----------------------------------------------+-++---- |
| 94 | // ^ ^ ^ ^ |
| 95 | // | | | XXXXXXX <-- nla_len(nlattr) |
| 96 | // | | | +-- nla_data(nlattr) |
| 97 | // | | X-nla_total_size-X |
| 98 | // | | XXXXX-nlmsg_attrlen-XXXXXX |
| 99 | // | +-- nlmsg_data(hdr) +-- nlmsg_attrdata() |
| 100 | // +-- msg = nlmsg_hdr(raw_message) |
| 101 | |
| 102 | // |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 103 | // Nl80211Message |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 104 | // |
| 105 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 106 | bool Nl80211Message::InitFromNlmsg(const nlmsghdr *const_msg) { |
| 107 | if (!const_msg) { |
| 108 | LOG(ERROR) << "Null |msg| parameter"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 112 | // Netlink header. |
| 113 | sequence_number_ = const_msg->nlmsg_seq; |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 114 | SLOG(WiFi, 6) << "NL Message " << sequence_number() << " <==="; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 115 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 116 | // Casting away constness, here, since the libnl code doesn't properly label |
| 117 | // their stuff as const (even though it is). |
| 118 | nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg); |
| 119 | |
| 120 | // Genl message header. |
| 121 | genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(nlmsg_data(msg)); |
| 122 | |
| 123 | // Attributes. |
| 124 | // Parse the attributes from the nl message payload into the 'tb' array. |
| 125 | nlattr *tb[NL80211_ATTR_MAX + 1]; |
| 126 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 127 | genlmsg_attrlen(gnlh, 0), NULL); |
| 128 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 129 | for (int i = 0; i < NL80211_ATTR_MAX + 1; ++i) { |
| 130 | if (tb[i]) { |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 131 | // TODO(wdg): When Nl80211Messages instantiate their own attributes, |
| 132 | // this call should, instead, call |SetAttributeFromNlAttr|. |
| 133 | attributes_.CreateAndInitFromNlAttr(static_cast<enum nl80211_attrs>(i), |
| 134 | tb[i]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | // Convert integer values provided by libnl (for example, from the |
| 139 | // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute) into |
| 140 | // strings describing the status. |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 141 | if (!reason_code_string_) { |
| 142 | reason_code_string_ = new map<uint16_t, string>; |
| 143 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecified] = |
| 144 | "Unspecified reason"; |
| 145 | (*reason_code_string_)[ |
| 146 | IEEE_80211::kReasonCodePreviousAuthenticationInvalid] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 147 | "Previous authentication no longer valid"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 148 | (*reason_code_string_)[IEEE_80211::kReasonCodeSenderHasLeft] = |
| 149 | "Deauthentcated because sending STA is leaving (or has left) IBSS or " |
| 150 | "ESS"; |
| 151 | (*reason_code_string_)[IEEE_80211::kReasonCodeInactivity] = |
| 152 | "Disassociated due to inactivity"; |
| 153 | (*reason_code_string_)[IEEE_80211::kReasonCodeTooManySTAs] = |
| 154 | "Disassociated because AP is unable to handle all currently associated " |
| 155 | "STAs"; |
| 156 | (*reason_code_string_)[IEEE_80211::kReasonCodeNonAuthenticated] = |
| 157 | "Class 2 frame received from nonauthenticated STA"; |
| 158 | (*reason_code_string_)[IEEE_80211::kReasonCodeNonAssociated] = |
| 159 | "Class 3 frame received from nonassociated STA"; |
| 160 | (*reason_code_string_)[IEEE_80211::kReasonCodeDisassociatedHasLeft] = |
| 161 | "Disassociated because sending STA is leaving (or has left) BSS"; |
| 162 | (*reason_code_string_)[ |
| 163 | IEEE_80211::kReasonCodeReassociationNotAuthenticated] = |
| 164 | "STA requesting (re)association is not authenticated with responding " |
| 165 | "STA"; |
| 166 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptablePowerCapability] = |
| 167 | "Disassociated because the information in the Power Capability " |
| 168 | "element is unacceptable"; |
| 169 | (*reason_code_string_)[ |
| 170 | IEEE_80211::kReasonCodeUnacceptableSupportedChannelInfo] = |
| 171 | "Disassociated because the information in the Supported Channels " |
| 172 | "element is unacceptable"; |
| 173 | (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidInfoElement] = |
| 174 | "Invalid information element, i.e., an information element defined in " |
| 175 | "this standard for which the content does not meet the specifications " |
| 176 | "in Clause 7"; |
| 177 | (*reason_code_string_)[IEEE_80211::kReasonCodeMICFailure] = |
| 178 | "Message integrity code (MIC) failure"; |
| 179 | (*reason_code_string_)[IEEE_80211::kReasonCode4WayTimeout] = |
| 180 | "4-Way Handshake timeout"; |
| 181 | (*reason_code_string_)[IEEE_80211::kReasonCodeGroupKeyHandshakeTimeout] = |
| 182 | "Group Key Handshake timeout"; |
| 183 | (*reason_code_string_)[IEEE_80211::kReasonCodeDifferenIE] = |
| 184 | "Information element in 4-Way Handshake different from " |
| 185 | "(Re)Association Request/Probe Response/Beacon frame"; |
| 186 | (*reason_code_string_)[IEEE_80211::kReasonCodeGroupCipherInvalid] = |
| 187 | "Invalid group cipher"; |
| 188 | (*reason_code_string_)[IEEE_80211::kReasonCodePairwiseCipherInvalid] = |
| 189 | "Invalid pairwise cipher"; |
| 190 | (*reason_code_string_)[IEEE_80211::kReasonCodeAkmpInvalid] = |
| 191 | "Invalid AKMP"; |
| 192 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnsupportedRsnIeVersion] = |
| 193 | "Unsupported RSN information element version"; |
| 194 | (*reason_code_string_)[IEEE_80211::kReasonCodeInvalidRsnIeCaps] = |
| 195 | "Invalid RSN information element capabilities"; |
| 196 | (*reason_code_string_)[IEEE_80211::kReasonCode8021XAuth] = |
| 197 | "IEEE 802.1X authentication failed"; |
| 198 | (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteRejected] = |
| 199 | "Cipher suite rejected because of the security policy"; |
| 200 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnspecifiedQoS] = |
| 201 | "Disassociated for unspecified, QoS-related reason"; |
| 202 | (*reason_code_string_)[IEEE_80211::kReasonCodeQoSBandwidth] = |
| 203 | "Disassociated because QoS AP lacks sufficient bandwidth for this " |
| 204 | "QoS STA"; |
| 205 | (*reason_code_string_)[IEEE_80211::kReasonCodeiPoorConditions] = |
| 206 | "Disassociated because excessive number of frames need to be " |
| 207 | "acknowledged, but are not acknowledged due to AP transmissions " |
| 208 | "and/or poor channel conditions"; |
| 209 | (*reason_code_string_)[IEEE_80211::kReasonCodeOutsideTxop] = |
| 210 | "Disassociated because STA is transmitting outside the limits of its " |
| 211 | "TXOPs"; |
| 212 | (*reason_code_string_)[IEEE_80211::kReasonCodeStaLeaving] = |
| 213 | "Requested from peer STA as the STA is leaving the BSS (or resetting)"; |
| 214 | (*reason_code_string_)[IEEE_80211::kReasonCodeUnacceptableMechanism] = |
| 215 | "Requested from peer STA as it does not want to use the mechanism"; |
| 216 | (*reason_code_string_)[IEEE_80211::kReasonCodeSetupRequired] = |
| 217 | "Requested from peer STA as the STA received frames using the " |
| 218 | "mechanism for which a setup is required"; |
| 219 | (*reason_code_string_)[IEEE_80211::kReasonCodeTimeout] = |
| 220 | "Requested from peer STA due to timeout"; |
| 221 | (*reason_code_string_)[IEEE_80211::kReasonCodeCipherSuiteNotSupported] = |
| 222 | "Peer STA does not support the requested cipher suite"; |
| 223 | (*reason_code_string_)[IEEE_80211::kReasonCodeInvalid] = "<INVALID REASON>"; |
| 224 | } |
| 225 | |
| 226 | if (!status_code_string_) { |
| 227 | status_code_string_ = new map<uint16_t, string>; |
| 228 | (*status_code_string_)[IEEE_80211::kStatusCodeSuccessful] = "Successful"; |
| 229 | (*status_code_string_)[IEEE_80211::kStatusCodeFailure] = |
| 230 | "Unspecified failure"; |
| 231 | (*status_code_string_)[IEEE_80211::kStatusCodeAllCapabilitiesNotSupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 232 | "Cannot support all requested capabilities in the capability " |
| 233 | "information field"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 234 | (*status_code_string_)[IEEE_80211::kStatusCodeCantConfirmAssociation] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 235 | "Reassociation denied due to inability to confirm that association " |
| 236 | "exists"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 237 | (*status_code_string_)[IEEE_80211::kStatusCodeAssociationDenied] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 238 | "Association denied due to reason outside the scope of this standard"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 239 | (*status_code_string_)[ |
| 240 | IEEE_80211::kStatusCodeAuthenticationUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 241 | "Responding station does not support the specified authentication " |
| 242 | "algorithm"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 243 | (*status_code_string_)[IEEE_80211::kStatusCodeOutOfSequence] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 244 | "Received an authentication frame with authentication transaction " |
| 245 | "sequence number out of expected sequence"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 246 | (*status_code_string_)[IEEE_80211::kStatusCodeChallengeFailure] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 247 | "Authentication rejected because of challenge failure"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 248 | (*status_code_string_)[IEEE_80211::kStatusCodeFrameTimeout] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 249 | "Authentication rejected due to timeout waiting for next frame in " |
| 250 | "sequence"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 251 | (*status_code_string_)[IEEE_80211::kStatusCodeMaxSta] = |
| 252 | "Association denied because AP is unable to handle additional " |
| 253 | "associated STA"; |
| 254 | (*status_code_string_)[IEEE_80211::kStatusCodeDataRateUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 255 | "Association denied due to requesting station not supporting all of " |
| 256 | "the data rates in the BSSBasicRateSet parameter"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 257 | (*status_code_string_)[IEEE_80211::kStatusCodeShortPreambleUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 258 | "Association denied due to requesting station not supporting the " |
| 259 | "short preamble option"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 260 | (*status_code_string_)[IEEE_80211::kStatusCodePbccUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 261 | "Association denied due to requesting station not supporting the PBCC " |
| 262 | "modulation option"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 263 | (*status_code_string_)[ |
| 264 | IEEE_80211::kStatusCodeChannelAgilityUnsupported] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 265 | "Association denied due to requesting station not supporting the " |
| 266 | "channel agility option"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 267 | (*status_code_string_)[IEEE_80211::kStatusCodeNeedSpectrumManagement] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 268 | "Association request rejected because Spectrum Management capability " |
| 269 | "is required"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 270 | (*status_code_string_)[ |
| 271 | IEEE_80211::kStatusCodeUnacceptablePowerCapability] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 272 | "Association request rejected because the information in the Power " |
| 273 | "Capability element is unacceptable"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 274 | (*status_code_string_)[ |
| 275 | IEEE_80211::kStatusCodeUnacceptableSupportedChannelInfo] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 276 | "Association request rejected because the information in the " |
| 277 | "Supported Channels element is unacceptable"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 278 | (*status_code_string_)[IEEE_80211::kStatusCodeShortTimeSlotRequired] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 279 | "Association request rejected due to requesting station not " |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 280 | "supporting the Short Slot Time option"; |
| 281 | (*status_code_string_)[IEEE_80211::kStatusCodeDssOfdmRequired] = |
| 282 | "Association request rejected due to requesting station not " |
| 283 | "supporting the DSSS-OFDM option"; |
| 284 | (*status_code_string_)[IEEE_80211::kStatusCodeQosFailure] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 285 | "Unspecified, QoS related failure"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 286 | (*status_code_string_)[ |
| 287 | IEEE_80211::kStatusCodeInsufficientBandwithForQsta] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 288 | "Association denied due to QAP having insufficient bandwidth to handle " |
| 289 | "another QSTA"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 290 | (*status_code_string_)[IEEE_80211::kStatusCodePoorConditions] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 291 | "Association denied due to poor channel conditions"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 292 | (*status_code_string_)[IEEE_80211::kStatusCodeQosNotSupported] = |
| 293 | "Association (with QoS BSS) denied due to requesting station not " |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 294 | "supporting the QoS facility"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 295 | (*status_code_string_)[IEEE_80211::kStatusCodeDeclined] = |
| 296 | "The request has been declined"; |
| 297 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalidParameterValues] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 298 | "The request has not been successful as one or more parameters have " |
| 299 | "invalid values"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 300 | (*status_code_string_)[IEEE_80211::kStatusCodeCannotBeHonored] = |
| 301 | "The TS has not been created because the request cannot be honored. " |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 302 | "However, a suggested Tspec is provided so that the initiating QSTA " |
| 303 | "may attempt to send another TS with the suggested changes to the " |
| 304 | "TSpec"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 305 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalidInfoElement] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 306 | "Invalid Information Element"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 307 | (*status_code_string_)[IEEE_80211::kStatusCodeGroupCipherInvalid] = |
| 308 | "Invalid Group Cipher"; |
| 309 | (*status_code_string_)[IEEE_80211::kStatusCodePairwiseCipherInvalid] = |
| 310 | "Invalid Pairwise Cipher"; |
| 311 | (*status_code_string_)[IEEE_80211::kStatusCodeAkmpInvalid] = "Invalid AKMP"; |
| 312 | (*status_code_string_)[IEEE_80211::kStatusCodeUnsupportedRsnIeVersion] = |
| 313 | "Unsupported RSN Information Element version"; |
| 314 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalidRsnIeCaps] = |
| 315 | "Invalid RSN Information Element Capabilities"; |
| 316 | (*status_code_string_)[IEEE_80211::kStatusCodeCipherSuiteRejected] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 317 | "Cipher suite is rejected per security policy"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 318 | (*status_code_string_)[IEEE_80211::kStatusCodeTsDelayNotMet] = |
| 319 | "The TS has not been created. However, the HC may be capable of " |
| 320 | "creating a TS, in response to a request, after the time indicated in " |
| 321 | "the TS Delay element"; |
| 322 | (*status_code_string_)[IEEE_80211::kStatusCodeDirectLinkIllegal] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 323 | "Direct link is not allowed in the BSS by policy"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 324 | (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInBss] = |
| 325 | "Destination STA is not present within this BSS"; |
| 326 | (*status_code_string_)[IEEE_80211::kStatusCodeStaNotInQsta] = |
| 327 | "The destination STA is not a QoS STA"; |
| 328 | (*status_code_string_)[IEEE_80211::kStatusCodeExcessiveListenInterval] = |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 329 | "Association denied because Listen Interval is too large"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 330 | (*status_code_string_)[IEEE_80211::kStatusCodeInvalid] = "<INVALID STATUS>"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 336 | // Helper function to provide a string for a MAC address. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 337 | bool Nl80211Message::GetMacAttributeString(nl80211_attrs id, |
| 338 | string *value) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 339 | if (!value) { |
| 340 | LOG(ERROR) << "Null |value| parameter"; |
| 341 | return false; |
| 342 | } |
| 343 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 344 | ByteString data; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 345 | if (!attributes().GetRawAttributeValue(id, &data)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 346 | value->assign(kBogusMacAddress); |
| 347 | return false; |
| 348 | } |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 349 | value->assign(StringFromMacAddress(data.GetConstData())); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 350 | |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | // Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 355 | bool Nl80211Message::GetScanFrequenciesAttribute( |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 356 | nl80211_attrs id, vector<uint32_t> *value) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 357 | if (!value) { |
| 358 | LOG(ERROR) << "Null |value| parameter"; |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | value->clear(); |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 363 | ByteString rawdata; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 364 | if (!attributes().GetRawAttributeValue(id, &rawdata) && !rawdata.IsEmpty()) |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 365 | return false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 366 | |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 367 | nlattr *nst = NULL; |
| 368 | // |nla_for_each_attr| requires a non-const parameter even though it |
| 369 | // doesn't change the data. |
| 370 | nlattr *attr_data = reinterpret_cast<nlattr *>(rawdata.GetData()); |
| 371 | int rem_nst; |
| 372 | int len = rawdata.GetLength(); |
| 373 | |
| 374 | nla_for_each_attr(nst, attr_data, len, rem_nst) { |
| 375 | value->push_back(Nl80211Attribute::NlaGetU32(nst)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 376 | } |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 377 | return true; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Helper function to provide a string for NL80211_ATTR_SCAN_SSIDS. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 381 | bool Nl80211Message::GetScanSsidsAttribute( |
| 382 | nl80211_attrs id, vector<string> *value) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 383 | if (!value) { |
| 384 | LOG(ERROR) << "Null |value| parameter"; |
| 385 | return false; |
| 386 | } |
| 387 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 388 | ByteString rawdata; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 389 | if (!attributes().GetRawAttributeValue(id, &rawdata) || rawdata.IsEmpty()) |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 390 | return false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 391 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 392 | nlattr *nst = NULL; |
| 393 | // |nla_for_each_attr| requires a non-const parameter even though it |
| 394 | // doesn't change the data. |
| 395 | nlattr *data = reinterpret_cast<nlattr *>(rawdata.GetData()); |
| 396 | int rem_nst; |
| 397 | int len = rawdata.GetLength(); |
| 398 | |
| 399 | nla_for_each_attr(nst, data, len, rem_nst) { |
| 400 | value->push_back(StringFromSsid(nla_len(nst), |
| 401 | reinterpret_cast<const uint8_t *>( |
| 402 | nla_data(nst))).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 403 | } |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 404 | return true; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 407 | // Protected members. |
| 408 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 409 | string Nl80211Message::GetHeaderString() const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 410 | char ifname[IF_NAMESIZE] = ""; |
| 411 | uint32_t ifindex = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 412 | bool ifindex_exists = attributes().GetU32AttributeValue(NL80211_ATTR_IFINDEX, |
| 413 | &ifindex); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 414 | |
| 415 | uint32_t wifi = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 416 | bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, |
| 417 | &wifi); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 418 | |
| 419 | string output; |
| 420 | if (ifindex_exists && wifi_exists) { |
| 421 | StringAppendF(&output, "%s (phy #%" PRIu32 "): ", |
| 422 | (if_indextoname(ifindex, ifname) ? ifname : "<unknown>"), |
| 423 | wifi); |
| 424 | } else if (ifindex_exists) { |
| 425 | StringAppendF(&output, "%s: ", |
| 426 | (if_indextoname(ifindex, ifname) ? ifname : "<unknown>")); |
| 427 | } else if (wifi_exists) { |
| 428 | StringAppendF(&output, "phy #%" PRIu32 "u: ", wifi); |
| 429 | } |
| 430 | |
| 431 | return output; |
| 432 | } |
| 433 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 434 | string Nl80211Message::StringFromFrame(nl80211_attrs attr_name) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 435 | string output; |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 436 | ByteString frame_data; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 437 | if (attributes().GetRawAttributeValue(attr_name, |
| 438 | &frame_data) && !frame_data.IsEmpty()) { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 439 | Nl80211Frame frame(frame_data); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 440 | frame.ToString(&output); |
| 441 | } else { |
| 442 | output.append(" [no frame]"); |
| 443 | } |
| 444 | |
| 445 | return output; |
| 446 | } |
| 447 | |
| 448 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 449 | string Nl80211Message::StringFromKeyType(nl80211_key_type key_type) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 450 | switch (key_type) { |
| 451 | case NL80211_KEYTYPE_GROUP: |
| 452 | return "Group"; |
| 453 | case NL80211_KEYTYPE_PAIRWISE: |
| 454 | return "Pairwise"; |
| 455 | case NL80211_KEYTYPE_PEERKEY: |
| 456 | return "PeerKey"; |
| 457 | default: |
| 458 | return "<Unknown Key Type>"; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 463 | string Nl80211Message::StringFromMacAddress(const uint8_t *arg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 464 | string output; |
| 465 | |
| 466 | if (!arg) { |
| 467 | output = kBogusMacAddress; |
| 468 | LOG(ERROR) << "|arg| parameter is NULL."; |
| 469 | } else { |
| 470 | StringAppendF(&output, "%02x", arg[0]); |
| 471 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 472 | for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 473 | StringAppendF(&output, ":%02x", arg[i]); |
| 474 | } |
| 475 | } |
| 476 | return output; |
| 477 | } |
| 478 | |
| 479 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 480 | string Nl80211Message::StringFromRegInitiator(__u8 initiator) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 481 | switch (initiator) { |
| 482 | case NL80211_REGDOM_SET_BY_CORE: |
| 483 | return "the wireless core upon initialization"; |
| 484 | case NL80211_REGDOM_SET_BY_USER: |
| 485 | return "a user"; |
| 486 | case NL80211_REGDOM_SET_BY_DRIVER: |
| 487 | return "a driver"; |
| 488 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
| 489 | return "a country IE"; |
| 490 | default: |
| 491 | return "<Unknown Reg Initiator>"; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 496 | string Nl80211Message::StringFromSsid(const uint8_t len, |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 497 | const uint8_t *data) { |
| 498 | string output; |
| 499 | if (!data) { |
| 500 | StringAppendF(&output, "<Error from %s, NULL parameter>", __func__); |
| 501 | LOG(ERROR) << "|data| parameter is NULL."; |
| 502 | return output; |
| 503 | } |
| 504 | |
| 505 | for (int i = 0; i < len; ++i) { |
| 506 | if (data[i] == ' ') |
| 507 | output.append(" "); |
| 508 | else if (isprint(data[i])) |
| 509 | StringAppendF(&output, "%c", static_cast<char>(data[i])); |
| 510 | else |
| 511 | StringAppendF(&output, "\\x%2x", data[i]); |
| 512 | } |
| 513 | |
| 514 | return output; |
| 515 | } |
| 516 | |
| 517 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 518 | string Nl80211Message::StringFromReason(uint16_t status) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 519 | map<uint16_t, string>::const_iterator match; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 520 | match = reason_code_string_->find(status); |
| 521 | if (match == reason_code_string_->end()) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 522 | string output; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 523 | if (status < IEEE_80211::kReasonCodeMax) { |
| 524 | StringAppendF(&output, "<Reserved Reason:%u>", status); |
| 525 | } else { |
| 526 | StringAppendF(&output, "<Unknown Reason:%u>", status); |
| 527 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 528 | return output; |
| 529 | } |
| 530 | return match->second; |
| 531 | } |
| 532 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 533 | // static |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 534 | string Nl80211Message::StringFromStatus(uint16_t status) { |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 535 | map<uint16_t, string>::const_iterator match; |
| 536 | match = status_code_string_->find(status); |
| 537 | if (match == status_code_string_->end()) { |
| 538 | string output; |
| 539 | if (status < IEEE_80211::kStatusCodeMax) { |
| 540 | StringAppendF(&output, "<Reserved Status:%u>", status); |
| 541 | } else { |
| 542 | StringAppendF(&output, "<Unknown Status:%u>", status); |
| 543 | } |
| 544 | return output; |
| 545 | } |
| 546 | return match->second; |
| 547 | } |
| 548 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 549 | string Nl80211Message::GenericToString() const { |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 550 | string output; |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 551 | StringAppendF(&output, "Message %s (%d)\n", |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 552 | message_type_string(), message_type()); |
| 553 | StringAppendF(&output, "%s", attributes_.ToString().c_str()); |
| 554 | return output; |
| 555 | } |
| 556 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 557 | ByteString Nl80211Message::Encode(uint16_t nlmsg_type) const { |
| 558 | // Build netlink header. |
| 559 | nlmsghdr header; |
| 560 | size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header)); |
| 561 | header.nlmsg_len = nlmsghdr_with_pad; |
| 562 | header.nlmsg_type = nlmsg_type; |
| 563 | header.nlmsg_flags = NLM_F_REQUEST; |
| 564 | header.nlmsg_seq = sequence_number(); |
| 565 | header.nlmsg_pid = getpid(); |
| 566 | |
| 567 | // Build genl message header. |
| 568 | genlmsghdr genl_header; |
| 569 | size_t genlmsghdr_with_pad = NLMSG_ALIGN(sizeof(genl_header)); |
| 570 | header.nlmsg_len += genlmsghdr_with_pad; |
| 571 | genl_header.cmd = message_type(); |
| 572 | genl_header.version = 1; |
| 573 | genl_header.reserved = 0; |
| 574 | |
| 575 | // Assemble attributes (padding is included by AttributeList::Encode). |
| 576 | ByteString attributes = attributes_.Encode(); |
| 577 | header.nlmsg_len += attributes.GetLength(); |
| 578 | |
| 579 | // Now that we know the total message size, build the output ByteString. |
| 580 | ByteString result; |
| 581 | |
| 582 | // Netlink header + pad. |
| 583 | result.Append(ByteString(reinterpret_cast<unsigned char *>(&header), |
| 584 | sizeof(header))); |
| 585 | result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any). |
| 586 | |
| 587 | // Genl message header + pad. |
| 588 | result.Append(ByteString(reinterpret_cast<unsigned char *>(&genl_header), |
| 589 | sizeof(genl_header))); |
| 590 | result.Resize(nlmsghdr_with_pad + genlmsghdr_with_pad); // Zero-fill. |
| 591 | |
| 592 | // Attributes including pad. |
| 593 | result.Append(attributes); |
| 594 | |
| 595 | return result; |
| 596 | } |
| 597 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 598 | Nl80211Frame::Nl80211Frame(const ByteString &raw_frame) |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 599 | : frame_type_(kIllegalFrameType), reason_(UINT16_MAX), status_(UINT16_MAX), |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 600 | frame_(raw_frame) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 601 | const IEEE_80211::ieee80211_frame *frame = |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 602 | reinterpret_cast<const IEEE_80211::ieee80211_frame *>( |
| 603 | frame_.GetConstData()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 604 | |
| 605 | // Now, let's populate the other stuff. |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 606 | if (frame_.GetLength() >= kMinimumFrameByteCount) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 607 | mac_from_ = |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 608 | Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]); |
| 609 | mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 610 | frame_type_ = frame->frame_control & kFrameTypeMask; |
| 611 | |
| 612 | switch (frame_type_) { |
| 613 | case kAssocResponseFrameType: |
| 614 | case kReassocResponseFrameType: |
| 615 | status_ = le16toh(frame->u.associate_response.status_code); |
| 616 | break; |
| 617 | |
| 618 | case kAuthFrameType: |
| 619 | status_ = le16toh(frame->u.authentiate_message.status_code); |
| 620 | break; |
| 621 | |
| 622 | case kDisassocFrameType: |
| 623 | case kDeauthFrameType: |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 624 | reason_ = le16toh(frame->u.deauthentiate_message.reason_code); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 625 | break; |
| 626 | |
| 627 | default: |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 633 | bool Nl80211Frame::ToString(string *output) const { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 634 | if (!output) { |
| 635 | LOG(ERROR) << "NULL |output|"; |
| 636 | return false; |
| 637 | } |
| 638 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 639 | if (frame_.IsEmpty()) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 640 | output->append(" [no frame]"); |
| 641 | return true; |
| 642 | } |
| 643 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 644 | if (frame_.GetLength() < kMinimumFrameByteCount) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 645 | output->append(" [invalid frame: "); |
| 646 | } else { |
| 647 | StringAppendF(output, " %s -> %s", mac_from_.c_str(), mac_to_.c_str()); |
| 648 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 649 | switch (frame_.GetConstData()[0] & kFrameTypeMask) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 650 | case kAssocResponseFrameType: |
| 651 | StringAppendF(output, "; AssocResponse status: %u: %s", |
| 652 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 653 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 654 | break; |
| 655 | case kReassocResponseFrameType: |
| 656 | StringAppendF(output, "; ReassocResponse status: %u: %s", |
| 657 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 658 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 659 | break; |
| 660 | case kAuthFrameType: |
| 661 | StringAppendF(output, "; Auth status: %u: %s", |
| 662 | status_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 663 | Nl80211Message::StringFromStatus(status_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 664 | break; |
| 665 | |
| 666 | case kDisassocFrameType: |
| 667 | StringAppendF(output, "; Disassoc reason %u: %s", |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 668 | reason_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 669 | Nl80211Message::StringFromReason(reason_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 670 | break; |
| 671 | case kDeauthFrameType: |
| 672 | StringAppendF(output, "; Deauth reason %u: %s", |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 673 | reason_, |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 674 | Nl80211Message::StringFromReason(reason_).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 675 | break; |
| 676 | |
| 677 | default: |
| 678 | break; |
| 679 | } |
| 680 | output->append(" [frame: "); |
| 681 | } |
| 682 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 683 | const unsigned char *frame = frame_.GetConstData(); |
| 684 | for (size_t i = 0; i < frame_.GetLength(); ++i) { |
| 685 | StringAppendF(output, "%02x, ", frame[i]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 686 | } |
| 687 | output->append("]"); |
| 688 | |
| 689 | return true; |
| 690 | } |
| 691 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 692 | bool Nl80211Frame::IsEqual(const Nl80211Frame &other) const { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 693 | return frame_.Equals(other.frame_); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 696 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 697 | // |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 698 | // Specific Nl80211Message types. |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 699 | // |
| 700 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 701 | // An Ack is not a GENL message and, as such, has no command. |
| 702 | const uint8_t AckMessage::kCommand = NL80211_CMD_UNSPEC; |
| 703 | const char AckMessage::kCommandString[] = "NL80211_ACK"; |
| 704 | |
| 705 | string AckMessage::ToString() const { |
| 706 | return "NL80211_ACK"; |
| 707 | } |
| 708 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 709 | const uint8_t AssociateMessage::kCommand = NL80211_CMD_ASSOCIATE; |
| 710 | const char AssociateMessage::kCommandString[] = "NL80211_CMD_ASSOCIATE"; |
| 711 | |
| 712 | string AssociateMessage::ToString() const { |
| 713 | string output(GetHeaderString()); |
| 714 | output.append("assoc"); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 715 | if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL)) |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 716 | output.append(StringFromFrame(NL80211_ATTR_FRAME)); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 717 | else if (attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT)) |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 718 | output.append(": timed out"); |
| 719 | else |
| 720 | output.append(": unknown event"); |
| 721 | return output; |
| 722 | } |
| 723 | |
| 724 | const uint8_t AuthenticateMessage::kCommand = NL80211_CMD_AUTHENTICATE; |
| 725 | const char AuthenticateMessage::kCommandString[] = "NL80211_CMD_AUTHENTICATE"; |
| 726 | |
| 727 | string AuthenticateMessage::ToString() const { |
| 728 | string output(GetHeaderString()); |
| 729 | output.append("auth"); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 730 | if (attributes().GetRawAttributeValue(NL80211_ATTR_FRAME, NULL)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 731 | output.append(StringFromFrame(NL80211_ATTR_FRAME)); |
| 732 | } else { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 733 | output.append(attributes().IsFlagAttributeTrue(NL80211_ATTR_TIMED_OUT) ? |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 734 | ": timed out" : ": unknown event"); |
| 735 | } |
| 736 | return output; |
| 737 | } |
| 738 | |
| 739 | const uint8_t CancelRemainOnChannelMessage::kCommand = |
| 740 | NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL; |
| 741 | const char CancelRemainOnChannelMessage::kCommandString[] = |
| 742 | "NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL"; |
| 743 | |
| 744 | string CancelRemainOnChannelMessage::ToString() const { |
| 745 | string output(GetHeaderString()); |
| 746 | uint32_t freq; |
| 747 | uint64_t cookie; |
| 748 | StringAppendF(&output, |
| 749 | "done with remain on freq %" PRIu32 " (cookie %" PRIx64 ")", |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 750 | (attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ, |
| 751 | &freq) ? 0 : freq), |
| 752 | (attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, |
| 753 | &cookie) ? 0 : cookie)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 754 | return output; |
| 755 | } |
| 756 | |
| 757 | const uint8_t ConnectMessage::kCommand = NL80211_CMD_CONNECT; |
| 758 | const char ConnectMessage::kCommandString[] = "NL80211_CMD_CONNECT"; |
| 759 | |
| 760 | string ConnectMessage::ToString() const { |
| 761 | string output(GetHeaderString()); |
| 762 | |
| 763 | uint16_t status = UINT16_MAX; |
| 764 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 765 | if (!attributes().GetU16AttributeValue(NL80211_ATTR_STATUS_CODE, &status)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 766 | output.append("unknown connect status"); |
| 767 | } else if (status == 0) { |
| 768 | output.append("connected"); |
| 769 | } else { |
| 770 | output.append("failed to connect"); |
| 771 | } |
| 772 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 773 | if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 774 | string mac; |
| 775 | GetMacAttributeString(NL80211_ATTR_MAC, &mac); |
| 776 | StringAppendF(&output, " to %s", mac.c_str()); |
| 777 | } |
| 778 | if (status) |
| 779 | StringAppendF(&output, ", status: %u: %s", status, |
| 780 | StringFromStatus(status).c_str()); |
| 781 | return output; |
| 782 | } |
| 783 | |
| 784 | const uint8_t DeauthenticateMessage::kCommand = NL80211_CMD_DEAUTHENTICATE; |
| 785 | const char DeauthenticateMessage::kCommandString[] = |
| 786 | "NL80211_CMD_DEAUTHENTICATE"; |
| 787 | |
| 788 | string DeauthenticateMessage::ToString() const { |
| 789 | string output(GetHeaderString()); |
| 790 | StringAppendF(&output, "deauth%s", |
| 791 | StringFromFrame(NL80211_ATTR_FRAME).c_str()); |
| 792 | return output; |
| 793 | } |
| 794 | |
| 795 | const uint8_t DeleteStationMessage::kCommand = NL80211_CMD_DEL_STATION; |
| 796 | const char DeleteStationMessage::kCommandString[] = "NL80211_CMD_DEL_STATION"; |
| 797 | |
| 798 | string DeleteStationMessage::ToString() const { |
| 799 | string mac; |
| 800 | GetMacAttributeString(NL80211_ATTR_MAC, &mac); |
| 801 | string output(GetHeaderString()); |
| 802 | StringAppendF(&output, "del station %s", mac.c_str()); |
| 803 | return output; |
| 804 | } |
| 805 | |
| 806 | const uint8_t DisassociateMessage::kCommand = NL80211_CMD_DISASSOCIATE; |
| 807 | const char DisassociateMessage::kCommandString[] = "NL80211_CMD_DISASSOCIATE"; |
| 808 | |
| 809 | string DisassociateMessage::ToString() const { |
| 810 | string output(GetHeaderString()); |
| 811 | StringAppendF(&output, "disassoc%s", |
| 812 | StringFromFrame(NL80211_ATTR_FRAME).c_str()); |
| 813 | return output; |
| 814 | } |
| 815 | |
| 816 | const uint8_t DisconnectMessage::kCommand = NL80211_CMD_DISCONNECT; |
| 817 | const char DisconnectMessage::kCommandString[] = "NL80211_CMD_DISCONNECT"; |
| 818 | |
| 819 | string DisconnectMessage::ToString() const { |
| 820 | string output(GetHeaderString()); |
| 821 | StringAppendF(&output, "disconnected %s", |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 822 | ((attributes().IsFlagAttributeTrue( |
| 823 | NL80211_ATTR_DISCONNECTED_BY_AP)) ? |
| 824 | "(by AP)" : "(local request)")); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 825 | |
| 826 | uint16_t reason = UINT16_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 827 | if (attributes().GetU16AttributeValue(NL80211_ATTR_REASON_CODE, &reason)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 828 | StringAppendF(&output, " reason: %u: %s", |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 829 | reason, StringFromReason(reason).c_str()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 830 | } |
| 831 | return output; |
| 832 | } |
| 833 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 834 | // An Error is not a GENL message and, as such, has no command. |
| 835 | const uint8_t ErrorMessage::kCommand = NL80211_CMD_UNSPEC; |
| 836 | const char ErrorMessage::kCommandString[] = "NL80211_ERROR"; |
| 837 | |
| 838 | ErrorMessage::ErrorMessage(uint32_t error) |
| 839 | : Nl80211Message(kCommand, kCommandString), error_(error) {} |
| 840 | |
| 841 | string ErrorMessage::ToString() const { |
| 842 | string output; |
| 843 | StringAppendF(&output, "NL80211_ERROR %" PRIx32 ": %s", |
| 844 | error_, strerror(error_)); |
| 845 | return output; |
| 846 | } |
| 847 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 848 | const uint8_t FrameTxStatusMessage::kCommand = NL80211_CMD_FRAME_TX_STATUS; |
| 849 | const char FrameTxStatusMessage::kCommandString[] = |
| 850 | "NL80211_CMD_FRAME_TX_STATUS"; |
| 851 | |
| 852 | string FrameTxStatusMessage::ToString() const { |
| 853 | string output(GetHeaderString()); |
| 854 | uint64_t cookie = UINT64_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 855 | attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 856 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 857 | StringAppendF(&output, "mgmt TX status (cookie %" PRIx64 "): %s", cookie, |
| 858 | (attributes().IsFlagAttributeTrue(NL80211_ATTR_ACK) ? |
| 859 | "acked" : "no ack")); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 860 | return output; |
| 861 | } |
| 862 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 863 | const uint8_t GetRegMessage::kCommand = NL80211_CMD_GET_REG; |
| 864 | const char GetRegMessage::kCommandString[] = "NL80211_CMD_GET_REG"; |
| 865 | |
| 866 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 867 | const uint8_t JoinIbssMessage::kCommand = NL80211_CMD_JOIN_IBSS; |
| 868 | const char JoinIbssMessage::kCommandString[] = "NL80211_CMD_JOIN_IBSS"; |
| 869 | |
| 870 | string JoinIbssMessage::ToString() const { |
| 871 | string mac; |
| 872 | GetMacAttributeString(NL80211_ATTR_MAC, &mac); |
| 873 | string output(GetHeaderString()); |
| 874 | StringAppendF(&output, "IBSS %s joined", mac.c_str()); |
| 875 | return output; |
| 876 | } |
| 877 | |
| 878 | const uint8_t MichaelMicFailureMessage::kCommand = |
| 879 | NL80211_CMD_MICHAEL_MIC_FAILURE; |
| 880 | const char MichaelMicFailureMessage::kCommandString[] = |
| 881 | "NL80211_CMD_MICHAEL_MIC_FAILURE"; |
| 882 | |
| 883 | string MichaelMicFailureMessage::ToString() const { |
| 884 | string output(GetHeaderString()); |
| 885 | |
| 886 | output.append("Michael MIC failure event:"); |
| 887 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 888 | if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 889 | string mac; |
| 890 | GetMacAttributeString(NL80211_ATTR_MAC, &mac); |
| 891 | StringAppendF(&output, " source MAC address %s", mac.c_str()); |
| 892 | } |
| 893 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 894 | { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 895 | ByteString rawdata; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 896 | if (attributes().GetRawAttributeValue(NL80211_ATTR_KEY_SEQ, |
| 897 | &rawdata) && |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 898 | rawdata.GetLength() == Nl80211Message::kEthernetAddressBytes) { |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 899 | const unsigned char *seq = rawdata.GetConstData(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 900 | StringAppendF(&output, " seq=%02x%02x%02x%02x%02x%02x", |
| 901 | seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]); |
| 902 | } |
| 903 | } |
| 904 | uint32_t key_type_val = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 905 | if (attributes().GetU32AttributeValue(NL80211_ATTR_KEY_TYPE, &key_type_val)) { |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 906 | nl80211_key_type key_type = static_cast<nl80211_key_type>(key_type_val); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 907 | StringAppendF(&output, " Key Type %s", StringFromKeyType(key_type).c_str()); |
| 908 | } |
| 909 | |
| 910 | uint8_t key_index = UINT8_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 911 | if (attributes().GetU8AttributeValue(NL80211_ATTR_KEY_IDX, &key_index)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 912 | StringAppendF(&output, " Key Id %u", key_index); |
| 913 | } |
| 914 | |
| 915 | return output; |
| 916 | } |
| 917 | |
| 918 | const uint8_t NewScanResultsMessage::kCommand = NL80211_CMD_NEW_SCAN_RESULTS; |
| 919 | const char NewScanResultsMessage::kCommandString[] = |
| 920 | "NL80211_CMD_NEW_SCAN_RESULTS"; |
| 921 | |
| 922 | string NewScanResultsMessage::ToString() const { |
| 923 | string output(GetHeaderString()); |
| 924 | output.append("scan finished"); |
| 925 | |
| 926 | { |
| 927 | output.append("; frequencies: "); |
| 928 | vector<uint32_t> list; |
| 929 | if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) { |
| 930 | string str; |
| 931 | for (vector<uint32_t>::const_iterator i = list.begin(); |
| 932 | i != list.end(); ++i) { |
| 933 | StringAppendF(&str, " %" PRIu32 ", ", *i); |
| 934 | } |
| 935 | output.append(str); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | { |
| 940 | output.append("; SSIDs: "); |
| 941 | vector<string> list; |
| 942 | if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) { |
| 943 | string str; |
| 944 | for (vector<string>::const_iterator i = list.begin(); |
| 945 | i != list.end(); ++i) { |
| 946 | StringAppendF(&str, "\"%s\", ", i->c_str()); |
| 947 | } |
| 948 | output.append(str); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | return output; |
| 953 | } |
| 954 | |
| 955 | const uint8_t NewStationMessage::kCommand = NL80211_CMD_NEW_STATION; |
| 956 | const char NewStationMessage::kCommandString[] = "NL80211_CMD_NEW_STATION"; |
| 957 | |
| 958 | string NewStationMessage::ToString() const { |
| 959 | string mac; |
| 960 | GetMacAttributeString(NL80211_ATTR_MAC, &mac); |
| 961 | string output(GetHeaderString()); |
| 962 | StringAppendF(&output, "new station %s", mac.c_str()); |
| 963 | |
| 964 | return output; |
| 965 | } |
| 966 | |
| 967 | const uint8_t NewWifiMessage::kCommand = NL80211_CMD_NEW_WIPHY; |
| 968 | const char NewWifiMessage::kCommandString[] = "NL80211_CMD_NEW_WIPHY"; |
| 969 | |
| 970 | string NewWifiMessage::ToString() const { |
| 971 | string output(GetHeaderString()); |
| 972 | string wifi_name = "None"; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 973 | attributes().GetStringAttributeValue(NL80211_ATTR_WIPHY_NAME, &wifi_name); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 974 | StringAppendF(&output, "renamed to %s", wifi_name.c_str()); |
| 975 | return output; |
| 976 | } |
| 977 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 978 | // A NOOP is not a GENL message and, as such, has no command. |
| 979 | const uint8_t NoopMessage::kCommand = NL80211_CMD_UNSPEC; |
| 980 | const char NoopMessage::kCommandString[] = "NL80211_NOOP"; |
| 981 | |
| 982 | string NoopMessage::ToString() const { |
| 983 | return "NL80211_NOOP"; |
| 984 | } |
| 985 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 986 | const uint8_t NotifyCqmMessage::kCommand = NL80211_CMD_NOTIFY_CQM; |
| 987 | const char NotifyCqmMessage::kCommandString[] = "NL80211_CMD_NOTIFY_CQM"; |
| 988 | |
| 989 | string NotifyCqmMessage::ToString() const { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 990 | // TODO(wdg): use attributes().GetNestedAttributeValue()... |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 991 | static const nla_policy kCqmValidationPolicy[NL80211_ATTR_CQM_MAX + 1] = { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 992 | { NLA_U32, 0, 0 }, // Who Knows? |
| 993 | { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THOLD] |
| 994 | { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_HYST] |
| 995 | { NLA_U32, 0, 0 }, // [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] |
| 996 | }; |
| 997 | |
| 998 | string output(GetHeaderString()); |
| 999 | output.append("connection quality monitor event: "); |
| 1000 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1001 | const Nl80211RawAttribute *attribute = |
| 1002 | attributes().GetRawAttribute(NL80211_ATTR_CQM); |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 1003 | if (!attribute) { |
| 1004 | output.append("missing data!"); |
| 1005 | return output; |
| 1006 | } |
| 1007 | |
| 1008 | const nlattr *const_data = attribute->data(); |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 1009 | // Note that |nla_parse_nested| doesn't change |const_data| but doesn't |
| 1010 | // declare itself as 'const', either. Hence the cast. |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1011 | nlattr *cqm_attr = const_cast<nlattr *>(const_data); |
| 1012 | |
| 1013 | nlattr *cqm[NL80211_ATTR_CQM_MAX + 1]; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 1014 | if (!cqm_attr || |
| 1015 | nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr, |
| 1016 | const_cast<nla_policy *>(kCqmValidationPolicy))) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1017 | output.append("missing data!"); |
| 1018 | return output; |
| 1019 | } |
| 1020 | if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) { |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 1021 | nl80211_cqm_rssi_threshold_event rssi_event = |
| 1022 | static_cast<nl80211_cqm_rssi_threshold_event>( |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 1023 | Nl80211Attribute::NlaGetU32( |
| 1024 | cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT])); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1025 | if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) |
| 1026 | output.append("RSSI went above threshold"); |
| 1027 | else |
| 1028 | output.append("RSSI went below threshold"); |
| 1029 | } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] && |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1030 | attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1031 | string mac; |
| 1032 | GetMacAttributeString(NL80211_ATTR_MAC, &mac); |
| 1033 | StringAppendF(&output, "peer %s didn't ACK %" PRIu32 " packets", |
| 1034 | mac.c_str(), |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 1035 | Nl80211Attribute::NlaGetU32( |
| 1036 | cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT])); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1037 | } else { |
| 1038 | output.append("unknown event"); |
| 1039 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1040 | return output; |
| 1041 | } |
| 1042 | |
| 1043 | const uint8_t PmksaCandidateMessage::kCommand = NL80211_ATTR_PMKSA_CANDIDATE; |
| 1044 | const char PmksaCandidateMessage::kCommandString[] = |
| 1045 | "NL80211_ATTR_PMKSA_CANDIDATE"; |
| 1046 | |
| 1047 | string PmksaCandidateMessage::ToString() const { |
| 1048 | string output(GetHeaderString()); |
| 1049 | output.append("PMKSA candidate found"); |
| 1050 | return output; |
| 1051 | } |
| 1052 | |
| 1053 | const uint8_t RegBeaconHintMessage::kCommand = NL80211_CMD_REG_BEACON_HINT; |
| 1054 | const char RegBeaconHintMessage::kCommandString[] = |
| 1055 | "NL80211_CMD_REG_BEACON_HINT"; |
| 1056 | |
| 1057 | string RegBeaconHintMessage::ToString() const { |
| 1058 | string output(GetHeaderString()); |
| 1059 | uint32_t wiphy_idx = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1060 | attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, &wiphy_idx); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1061 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1062 | const Nl80211RawAttribute *freq_before = |
| 1063 | attributes().GetRawAttribute(NL80211_ATTR_FREQ_BEFORE); |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 1064 | if (!freq_before) |
| 1065 | return ""; |
| 1066 | const nlattr *const_before = freq_before->data(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1067 | ieee80211_beacon_channel chan_before_beacon; |
| 1068 | memset(&chan_before_beacon, 0, sizeof(chan_before_beacon)); |
| 1069 | if (ParseBeaconHintChan(const_before, &chan_before_beacon)) |
| 1070 | return ""; |
| 1071 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1072 | const Nl80211RawAttribute *freq_after = |
| 1073 | attributes().GetRawAttribute(NL80211_ATTR_FREQ_AFTER); |
repo sync | d316eb7 | 2012-12-10 15:48:47 -0800 | [diff] [blame] | 1074 | if (!freq_after) |
| 1075 | return ""; |
| 1076 | |
| 1077 | const nlattr *const_after = freq_after->data(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1078 | ieee80211_beacon_channel chan_after_beacon; |
| 1079 | memset(&chan_after_beacon, 0, sizeof(chan_after_beacon)); |
| 1080 | if (ParseBeaconHintChan(const_after, &chan_after_beacon)) |
| 1081 | return ""; |
| 1082 | |
| 1083 | if (chan_before_beacon.center_freq != chan_after_beacon.center_freq) |
| 1084 | return ""; |
| 1085 | |
| 1086 | /* A beacon hint is sent _only_ if something _did_ change */ |
| 1087 | output.append("beacon hint:"); |
| 1088 | StringAppendF(&output, "phy%" PRIu32 " %u MHz [%d]:", |
| 1089 | wiphy_idx, chan_before_beacon.center_freq, |
| 1090 | ChannelFromIeee80211Frequency(chan_before_beacon.center_freq)); |
| 1091 | |
| 1092 | if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan) |
| 1093 | output.append("\to active scanning enabled"); |
| 1094 | if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) |
| 1095 | output.append("\to beaconing enabled"); |
| 1096 | return output; |
| 1097 | } |
| 1098 | |
| 1099 | int RegBeaconHintMessage::ParseBeaconHintChan(const nlattr *tb, |
| 1100 | ieee80211_beacon_channel *chan) |
| 1101 | const { |
| 1102 | static const nla_policy kBeaconFreqPolicy[ |
| 1103 | NL80211_FREQUENCY_ATTR_MAX + 1] = { |
| 1104 | {0, 0, 0}, |
| 1105 | { NLA_U32, 0, 0 }, // [NL80211_FREQUENCY_ATTR_FREQ] |
| 1106 | {0, 0, 0}, |
| 1107 | { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] |
| 1108 | { NLA_FLAG, 0, 0 }, // [NL80211_FREQUENCY_ATTR_NO_IBSS] |
| 1109 | }; |
| 1110 | |
| 1111 | if (!tb) { |
| 1112 | LOG(ERROR) << "|tb| parameter is NULL."; |
| 1113 | return -EINVAL; |
| 1114 | } |
| 1115 | |
| 1116 | if (!chan) { |
| 1117 | LOG(ERROR) << "|chan| parameter is NULL."; |
| 1118 | return -EINVAL; |
| 1119 | } |
| 1120 | |
| 1121 | nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; |
| 1122 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 1123 | // Note that |nla_parse_nested| doesn't change its parameters but doesn't |
| 1124 | // declare itself as 'const', either. Hence the cast. |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1125 | if (nla_parse_nested(tb_freq, |
| 1126 | NL80211_FREQUENCY_ATTR_MAX, |
| 1127 | const_cast<nlattr *>(tb), |
| 1128 | const_cast<nla_policy *>(kBeaconFreqPolicy))) |
| 1129 | return -EINVAL; |
| 1130 | |
Wade Guthrie | 8343f7f | 2012-12-04 13:52:32 -0800 | [diff] [blame] | 1131 | chan->center_freq = Nl80211Attribute::NlaGetU32( |
| 1132 | tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1133 | |
| 1134 | if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) |
| 1135 | chan->passive_scan = true; |
| 1136 | if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) |
| 1137 | chan->no_ibss = true; |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | int RegBeaconHintMessage::ChannelFromIeee80211Frequency(int freq) { |
| 1143 | // TODO(wdg): get rid of these magic numbers. |
| 1144 | if (freq == 2484) |
| 1145 | return 14; |
| 1146 | |
| 1147 | if (freq < 2484) |
| 1148 | return (freq - 2407) / 5; |
| 1149 | |
| 1150 | /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */ |
| 1151 | return freq/5 - 1000; |
| 1152 | } |
| 1153 | |
| 1154 | const uint8_t RegChangeMessage::kCommand = NL80211_CMD_REG_CHANGE; |
| 1155 | const char RegChangeMessage::kCommandString[] = "NL80211_CMD_REG_CHANGE"; |
| 1156 | |
| 1157 | string RegChangeMessage::ToString() const { |
| 1158 | string output(GetHeaderString()); |
| 1159 | output.append("regulatory domain change: "); |
| 1160 | |
| 1161 | uint8_t reg_type = UINT8_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1162 | attributes().GetU8AttributeValue(NL80211_ATTR_REG_TYPE, ®_type); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1163 | |
| 1164 | uint32_t initiator = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1165 | attributes().GetU32AttributeValue(NL80211_ATTR_REG_INITIATOR, &initiator); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1166 | |
| 1167 | uint32_t wifi = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1168 | bool wifi_exists = attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY, |
| 1169 | &wifi); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1170 | |
| 1171 | string alpha2 = "<None>"; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1172 | attributes().GetStringAttributeValue(NL80211_ATTR_REG_ALPHA2, &alpha2); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1173 | |
| 1174 | switch (reg_type) { |
| 1175 | case NL80211_REGDOM_TYPE_COUNTRY: |
| 1176 | StringAppendF(&output, "set to %s by %s request", |
| 1177 | alpha2.c_str(), StringFromRegInitiator(initiator).c_str()); |
| 1178 | if (wifi_exists) |
| 1179 | StringAppendF(&output, " on phy%" PRIu32, wifi); |
| 1180 | break; |
| 1181 | |
| 1182 | case NL80211_REGDOM_TYPE_WORLD: |
| 1183 | StringAppendF(&output, "set to world roaming by %s request", |
| 1184 | StringFromRegInitiator(initiator).c_str()); |
| 1185 | break; |
| 1186 | |
| 1187 | case NL80211_REGDOM_TYPE_CUSTOM_WORLD: |
| 1188 | StringAppendF(&output, |
| 1189 | "custom world roaming rules in place on phy%" PRIu32 |
| 1190 | " by %s request", |
| 1191 | wifi, StringFromRegInitiator(initiator).c_str()); |
| 1192 | break; |
| 1193 | |
| 1194 | case NL80211_REGDOM_TYPE_INTERSECTION: |
| 1195 | StringAppendF(&output, "intersection used due to a request made by %s", |
| 1196 | StringFromRegInitiator(initiator).c_str()); |
| 1197 | if (wifi_exists) |
| 1198 | StringAppendF(&output, " on phy%" PRIu32, wifi); |
| 1199 | break; |
| 1200 | |
| 1201 | default: |
| 1202 | output.append("unknown source"); |
| 1203 | break; |
| 1204 | } |
| 1205 | return output; |
| 1206 | } |
| 1207 | |
| 1208 | const uint8_t RemainOnChannelMessage::kCommand = NL80211_CMD_REMAIN_ON_CHANNEL; |
| 1209 | const char RemainOnChannelMessage::kCommandString[] = |
| 1210 | "NL80211_CMD_REMAIN_ON_CHANNEL"; |
| 1211 | |
| 1212 | string RemainOnChannelMessage::ToString() const { |
| 1213 | string output(GetHeaderString()); |
| 1214 | |
| 1215 | uint32_t wifi_freq = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1216 | attributes().GetU32AttributeValue(NL80211_ATTR_WIPHY_FREQ, &wifi_freq); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1217 | |
| 1218 | uint32_t duration = UINT32_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1219 | attributes().GetU32AttributeValue(NL80211_ATTR_DURATION, &duration); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1220 | |
| 1221 | uint64_t cookie = UINT64_MAX; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1222 | attributes().GetU64AttributeValue(NL80211_ATTR_COOKIE, &cookie); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1223 | |
| 1224 | StringAppendF(&output, "remain on freq %" PRIu32 " (%" PRIu32 "ms, cookie %" |
| 1225 | PRIx64 ")", |
| 1226 | wifi_freq, duration, cookie); |
| 1227 | return output; |
| 1228 | } |
| 1229 | |
| 1230 | const uint8_t RoamMessage::kCommand = NL80211_CMD_ROAM; |
| 1231 | const char RoamMessage::kCommandString[] = "NL80211_CMD_ROAM"; |
| 1232 | |
| 1233 | string RoamMessage::ToString() const { |
| 1234 | string output(GetHeaderString()); |
| 1235 | output.append("roamed"); |
| 1236 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1237 | if (attributes().GetRawAttributeValue(NL80211_ATTR_MAC, NULL)) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1238 | string mac; |
| 1239 | GetMacAttributeString(NL80211_ATTR_MAC, &mac); |
| 1240 | StringAppendF(&output, " to %s", mac.c_str()); |
| 1241 | } |
| 1242 | return output; |
| 1243 | } |
| 1244 | |
| 1245 | const uint8_t ScanAbortedMessage::kCommand = NL80211_CMD_SCAN_ABORTED; |
| 1246 | const char ScanAbortedMessage::kCommandString[] = "NL80211_CMD_SCAN_ABORTED"; |
| 1247 | |
| 1248 | string ScanAbortedMessage::ToString() const { |
| 1249 | string output(GetHeaderString()); |
| 1250 | output.append("scan aborted"); |
| 1251 | |
| 1252 | { |
| 1253 | output.append("; frequencies: "); |
| 1254 | vector<uint32_t> list; |
| 1255 | if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) { |
| 1256 | string str; |
| 1257 | for (vector<uint32_t>::const_iterator i = list.begin(); |
| 1258 | i != list.end(); ++i) { |
| 1259 | StringAppendF(&str, " %" PRIu32 ", ", *i); |
| 1260 | } |
| 1261 | output.append(str); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | { |
| 1266 | output.append("; SSIDs: "); |
| 1267 | vector<string> list; |
| 1268 | if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) { |
| 1269 | string str; |
| 1270 | for (vector<string>::const_iterator i = list.begin(); |
| 1271 | i != list.end(); ++i) { |
| 1272 | StringAppendF(&str, "\"%s\", ", i->c_str()); |
| 1273 | } |
| 1274 | output.append(str); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | return output; |
| 1279 | } |
| 1280 | |
| 1281 | const uint8_t TriggerScanMessage::kCommand = NL80211_CMD_TRIGGER_SCAN; |
| 1282 | const char TriggerScanMessage::kCommandString[] = "NL80211_CMD_TRIGGER_SCAN"; |
| 1283 | |
| 1284 | string TriggerScanMessage::ToString() const { |
| 1285 | string output(GetHeaderString()); |
| 1286 | output.append("scan started"); |
| 1287 | |
| 1288 | { |
| 1289 | output.append("; frequencies: "); |
| 1290 | vector<uint32_t> list; |
| 1291 | if (GetScanFrequenciesAttribute(NL80211_ATTR_SCAN_FREQUENCIES, &list)) { |
| 1292 | string str; |
| 1293 | for (vector<uint32_t>::const_iterator i = list.begin(); |
| 1294 | i != list.end(); ++i) { |
| 1295 | StringAppendF(&str, "%" PRIu32 ", ", *i); |
| 1296 | } |
| 1297 | output.append(str); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | { |
| 1302 | output.append("; SSIDs: "); |
| 1303 | vector<string> list; |
| 1304 | if (GetScanSsidsAttribute(NL80211_ATTR_SCAN_SSIDS, &list)) { |
| 1305 | string str; |
| 1306 | for (vector<string>::const_iterator i = list.begin(); |
| 1307 | i != list.end(); ++i) { |
| 1308 | StringAppendF(&str, "\"%s\", ", i->c_str()); |
| 1309 | } |
| 1310 | output.append(str); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | return output; |
| 1315 | } |
| 1316 | |
| 1317 | const uint8_t UnknownMessage::kCommand = 0xff; |
| 1318 | const char UnknownMessage::kCommandString[] = "<Unknown Message Type>"; |
| 1319 | |
| 1320 | string UnknownMessage::ToString() const { |
| 1321 | string output(GetHeaderString()); |
| 1322 | StringAppendF(&output, "unknown event %u", command_); |
| 1323 | return output; |
| 1324 | } |
| 1325 | |
| 1326 | const uint8_t UnprotDeauthenticateMessage::kCommand = |
| 1327 | NL80211_CMD_UNPROT_DEAUTHENTICATE; |
| 1328 | const char UnprotDeauthenticateMessage::kCommandString[] = |
| 1329 | "NL80211_CMD_UNPROT_DEAUTHENTICATE"; |
| 1330 | |
| 1331 | string UnprotDeauthenticateMessage::ToString() const { |
| 1332 | string output(GetHeaderString()); |
| 1333 | StringAppendF(&output, "unprotected deauth %s", |
| 1334 | StringFromFrame(NL80211_ATTR_FRAME).c_str()); |
| 1335 | return output; |
| 1336 | } |
| 1337 | |
| 1338 | const uint8_t UnprotDisassociateMessage::kCommand = |
| 1339 | NL80211_CMD_UNPROT_DISASSOCIATE; |
| 1340 | const char UnprotDisassociateMessage::kCommandString[] = |
| 1341 | "NL80211_CMD_UNPROT_DISASSOCIATE"; |
| 1342 | |
| 1343 | string UnprotDisassociateMessage::ToString() const { |
| 1344 | string output(GetHeaderString()); |
| 1345 | StringAppendF(&output, "unprotected disassoc %s", |
| 1346 | StringFromFrame(NL80211_ATTR_FRAME).c_str()); |
| 1347 | return output; |
| 1348 | } |
| 1349 | |
| 1350 | // |
| 1351 | // Factory class. |
| 1352 | // |
| 1353 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 1354 | Nl80211Message *Nl80211MessageFactory::CreateMessage(nlmsghdr *msg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1355 | if (!msg) { |
| 1356 | LOG(ERROR) << "NULL |msg| parameter"; |
| 1357 | return NULL; |
| 1358 | } |
| 1359 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 1360 | scoped_ptr<Nl80211Message> message; |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 1361 | void *payload = nlmsg_data(msg); |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 1362 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 1363 | if (msg->nlmsg_type == NLMSG_NOOP) { |
| 1364 | SLOG(WiFi, 6) << "Creating a NOP message"; |
| 1365 | message.reset(new NoopMessage()); |
| 1366 | } else if (msg->nlmsg_type == NLMSG_ERROR) { |
| 1367 | uint32_t error_code = *(reinterpret_cast<uint32_t *>(payload)); |
| 1368 | if (error_code) { |
| 1369 | SLOG(WiFi, 6) << "Creating an ERROR message:" << error_code; |
| 1370 | message.reset(new ErrorMessage(error_code)); |
| 1371 | } else { |
| 1372 | SLOG(WiFi, 6) << "Creating an ACK message"; |
| 1373 | message.reset(new AckMessage()); |
| 1374 | } |
| 1375 | } else { |
| 1376 | SLOG(WiFi, 6) << "Creating a Regular message"; |
| 1377 | genlmsghdr *gnlh = reinterpret_cast<genlmsghdr *>(payload); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1378 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 1379 | switch (gnlh->cmd) { |
| 1380 | case AssociateMessage::kCommand: |
| 1381 | message.reset(new AssociateMessage()); break; |
| 1382 | case AuthenticateMessage::kCommand: |
| 1383 | message.reset(new AuthenticateMessage()); break; |
| 1384 | case CancelRemainOnChannelMessage::kCommand: |
| 1385 | message.reset(new CancelRemainOnChannelMessage()); break; |
| 1386 | case ConnectMessage::kCommand: |
| 1387 | message.reset(new ConnectMessage()); break; |
| 1388 | case DeauthenticateMessage::kCommand: |
| 1389 | message.reset(new DeauthenticateMessage()); break; |
| 1390 | case DeleteStationMessage::kCommand: |
| 1391 | message.reset(new DeleteStationMessage()); break; |
| 1392 | case DisassociateMessage::kCommand: |
| 1393 | message.reset(new DisassociateMessage()); break; |
| 1394 | case DisconnectMessage::kCommand: |
| 1395 | message.reset(new DisconnectMessage()); break; |
| 1396 | case FrameTxStatusMessage::kCommand: |
| 1397 | message.reset(new FrameTxStatusMessage()); break; |
| 1398 | case GetRegMessage::kCommand: |
| 1399 | message.reset(new GetRegMessage()); break; |
| 1400 | case JoinIbssMessage::kCommand: |
| 1401 | message.reset(new JoinIbssMessage()); break; |
| 1402 | case MichaelMicFailureMessage::kCommand: |
| 1403 | message.reset(new MichaelMicFailureMessage()); break; |
| 1404 | case NewScanResultsMessage::kCommand: |
| 1405 | message.reset(new NewScanResultsMessage()); break; |
| 1406 | case NewStationMessage::kCommand: |
| 1407 | message.reset(new NewStationMessage()); break; |
| 1408 | case NewWifiMessage::kCommand: |
| 1409 | message.reset(new NewWifiMessage()); break; |
| 1410 | case NotifyCqmMessage::kCommand: |
| 1411 | message.reset(new NotifyCqmMessage()); break; |
| 1412 | case PmksaCandidateMessage::kCommand: |
| 1413 | message.reset(new PmksaCandidateMessage()); break; |
| 1414 | case RegBeaconHintMessage::kCommand: |
| 1415 | message.reset(new RegBeaconHintMessage()); break; |
| 1416 | case RegChangeMessage::kCommand: |
| 1417 | message.reset(new RegChangeMessage()); break; |
| 1418 | case RemainOnChannelMessage::kCommand: |
| 1419 | message.reset(new RemainOnChannelMessage()); break; |
| 1420 | case RoamMessage::kCommand: |
| 1421 | message.reset(new RoamMessage()); break; |
| 1422 | case ScanAbortedMessage::kCommand: |
| 1423 | message.reset(new ScanAbortedMessage()); break; |
| 1424 | case TriggerScanMessage::kCommand: |
| 1425 | message.reset(new TriggerScanMessage()); break; |
| 1426 | case UnprotDeauthenticateMessage::kCommand: |
| 1427 | message.reset(new UnprotDeauthenticateMessage()); break; |
| 1428 | case UnprotDisassociateMessage::kCommand: |
| 1429 | message.reset(new UnprotDisassociateMessage()); break; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1430 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 1431 | default: |
| 1432 | message.reset(new UnknownMessage(gnlh->cmd)); break; |
| 1433 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1434 | |
repo sync | 0efa9f0 | 2012-12-28 13:40:20 -0800 | [diff] [blame] | 1435 | if (!message->InitFromNlmsg(msg)) { |
| 1436 | LOG(ERROR) << "Message did not initialize properly"; |
| 1437 | return NULL; |
| 1438 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1441 | return message.release(); |
| 1442 | } |
| 1443 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 1444 | Nl80211MessageDataCollector * |
| 1445 | Nl80211MessageDataCollector::GetInstance() { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1446 | return g_datacollector.Pointer(); |
| 1447 | } |
| 1448 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 1449 | Nl80211MessageDataCollector::Nl80211MessageDataCollector() { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1450 | need_to_print[NL80211_ATTR_PMKSA_CANDIDATE] = true; |
| 1451 | need_to_print[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = true; |
| 1452 | need_to_print[NL80211_CMD_DEL_STATION] = true; |
| 1453 | need_to_print[NL80211_CMD_FRAME_TX_STATUS] = true; |
| 1454 | need_to_print[NL80211_CMD_JOIN_IBSS] = true; |
| 1455 | need_to_print[NL80211_CMD_MICHAEL_MIC_FAILURE] = true; |
| 1456 | need_to_print[NL80211_CMD_NEW_WIPHY] = true; |
| 1457 | need_to_print[NL80211_CMD_REG_BEACON_HINT] = true; |
| 1458 | need_to_print[NL80211_CMD_REG_CHANGE] = true; |
| 1459 | need_to_print[NL80211_CMD_REMAIN_ON_CHANNEL] = true; |
| 1460 | need_to_print[NL80211_CMD_ROAM] = true; |
| 1461 | need_to_print[NL80211_CMD_SCAN_ABORTED] = true; |
| 1462 | need_to_print[NL80211_CMD_UNPROT_DEAUTHENTICATE] = true; |
| 1463 | need_to_print[NL80211_CMD_UNPROT_DISASSOCIATE] = true; |
| 1464 | } |
| 1465 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 1466 | void Nl80211MessageDataCollector::CollectDebugData( |
| 1467 | const Nl80211Message &message, nlmsghdr *msg) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1468 | if (!msg) { |
| 1469 | LOG(ERROR) << "NULL |msg| parameter"; |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | bool doit = false; |
| 1474 | |
| 1475 | map<uint8_t, bool>::const_iterator node; |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 1476 | node = need_to_print.find(message.message_type()); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1477 | if (node != need_to_print.end()) |
| 1478 | doit = node->second; |
| 1479 | |
| 1480 | if (doit) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1481 | LOG(INFO) << "@@const unsigned char " |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 1482 | << "k" << message.message_type_string() |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1483 | << "[] = {"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1484 | |
Christopher Wiley | efd521f | 2012-11-07 17:32:46 -0800 | [diff] [blame] | 1485 | int payload_bytes = nlmsg_datalen(msg); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1486 | |
| 1487 | size_t bytes = nlmsg_total_size(payload_bytes); |
| 1488 | unsigned char *rawdata = reinterpret_cast<unsigned char *>(msg); |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 1489 | for (size_t i = 0; i < bytes; ++i) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1490 | LOG(INFO) << " 0x" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1491 | << std::hex << std::setfill('0') << std::setw(2) |
| 1492 | << + rawdata[i] << ","; |
| 1493 | } |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 1494 | LOG(INFO) << "};"; |
Christopher Wiley | 764538d | 2012-11-09 10:58:23 -0800 | [diff] [blame] | 1495 | need_to_print[message.message_type()] = false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | } // namespace shill. |