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