Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -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 | #include "shill/netlink_message.h" |
| 6 | |
| 7 | #include <limits.h> |
| 8 | #include <netlink/msg.h> |
| 9 | #include <netlink/netlink.h> |
| 10 | |
| 11 | #include <map> |
| 12 | #include <string> |
| 13 | |
| 14 | #include <base/format_macros.h> |
| 15 | #include <base/stl_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame^] | 16 | #include <base/strings/stringprintf.h> |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/logging.h" |
| 19 | |
| 20 | using base::StringAppendF; |
| 21 | using base::StringPrintf; |
| 22 | using std::map; |
| 23 | using std::min; |
| 24 | using std::string; |
| 25 | |
| 26 | namespace shill { |
| 27 | |
| 28 | const uint32_t NetlinkMessage::kBroadcastSequenceNumber = 0; |
| 29 | const uint16_t NetlinkMessage::kIllegalMessageType = UINT16_MAX; |
| 30 | |
| 31 | // NetlinkMessage |
| 32 | |
| 33 | ByteString NetlinkMessage::EncodeHeader(uint32_t sequence_number) { |
| 34 | ByteString result; |
| 35 | if (message_type_ == kIllegalMessageType) { |
| 36 | LOG(ERROR) << "Message type not set"; |
| 37 | return result; |
| 38 | } |
| 39 | sequence_number_ = sequence_number; |
| 40 | if (sequence_number_ == kBroadcastSequenceNumber) { |
| 41 | LOG(ERROR) << "Couldn't get a legal sequence number"; |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | // Build netlink header. |
| 46 | nlmsghdr header; |
| 47 | size_t nlmsghdr_with_pad = NLMSG_ALIGN(sizeof(header)); |
| 48 | header.nlmsg_len = nlmsghdr_with_pad; |
| 49 | header.nlmsg_type = message_type_; |
| 50 | header.nlmsg_flags = NLM_F_REQUEST | flags_; |
| 51 | header.nlmsg_seq = sequence_number_; |
| 52 | header.nlmsg_pid = getpid(); |
| 53 | |
| 54 | // Netlink header + pad. |
| 55 | result.Append(ByteString(reinterpret_cast<unsigned char *>(&header), |
| 56 | sizeof(header))); |
| 57 | result.Resize(nlmsghdr_with_pad); // Zero-fill pad space (if any). |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | bool NetlinkMessage::InitAndStripHeader(ByteString *input) { |
| 62 | if (!input) { |
| 63 | LOG(ERROR) << "NULL input"; |
| 64 | return false; |
| 65 | } |
| 66 | if (input->GetLength() < sizeof(nlmsghdr)) { |
| 67 | LOG(ERROR) << "Insufficient input to extract nlmsghdr"; |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // Read the nlmsghdr. |
| 72 | nlmsghdr *header = reinterpret_cast<nlmsghdr *>(input->GetData()); |
| 73 | message_type_ = header->nlmsg_type; |
| 74 | flags_ = header->nlmsg_flags; |
| 75 | sequence_number_ = header->nlmsg_seq; |
| 76 | |
| 77 | // Strip the nlmsghdr. |
| 78 | input->RemovePrefix(NLMSG_ALIGN(sizeof(struct nlmsghdr))); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool NetlinkMessage::InitFromNlmsg(const nlmsghdr *const_msg) { |
| 83 | if (!const_msg) { |
| 84 | LOG(ERROR) << "Null |const_msg| parameter"; |
| 85 | return false; |
| 86 | } |
| 87 | ByteString message(reinterpret_cast<const unsigned char *>(const_msg), |
| 88 | const_msg->nlmsg_len); |
| 89 | if (!InitAndStripHeader(&message)) { |
| 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | // static |
| 96 | void NetlinkMessage::PrintBytes(int log_level, const unsigned char *buf, |
| 97 | size_t num_bytes) { |
| 98 | SLOG(WiFi, log_level) << "Netlink Message -- Examining Bytes"; |
| 99 | if (!buf) { |
| 100 | SLOG(WiFi, log_level) << "<NULL Buffer>"; |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (num_bytes >= sizeof(nlmsghdr)) { |
| 105 | const nlmsghdr *header = reinterpret_cast<const nlmsghdr *>(buf); |
| 106 | SLOG(WiFi, log_level) << StringPrintf( |
| 107 | "len: %02x %02x %02x %02x = %u bytes", |
| 108 | buf[0], buf[1], buf[2], buf[3], header->nlmsg_len); |
| 109 | |
| 110 | SLOG(WiFi, log_level) << StringPrintf( |
| 111 | "type | flags: %02x %02x %02x %02x - type:%u flags:%s%s%s%s%s", |
| 112 | buf[4], buf[5], buf[6], buf[7], header->nlmsg_type, |
| 113 | ((header->nlmsg_flags & NLM_F_REQUEST) ? " REQUEST" : ""), |
| 114 | ((header->nlmsg_flags & NLM_F_MULTI) ? " MULTI" : ""), |
| 115 | ((header->nlmsg_flags & NLM_F_ACK) ? " ACK" : ""), |
| 116 | ((header->nlmsg_flags & NLM_F_ECHO) ? " ECHO" : ""), |
| 117 | ((header->nlmsg_flags & NLM_F_DUMP_INTR) ? " BAD-SEQ" : "")); |
| 118 | |
| 119 | SLOG(WiFi, log_level) << StringPrintf( |
| 120 | "sequence: %02x %02x %02x %02x = %u", |
| 121 | buf[8], buf[9], buf[10], buf[11], header->nlmsg_seq); |
| 122 | SLOG(WiFi, log_level) << StringPrintf( |
| 123 | "pid: %02x %02x %02x %02x = %u", |
| 124 | buf[12], buf[13], buf[14], buf[15], header->nlmsg_pid); |
| 125 | buf += sizeof(nlmsghdr); |
| 126 | num_bytes -= sizeof(nlmsghdr); |
| 127 | } else { |
| 128 | SLOG(WiFi, log_level) << "Not enough bytes (" << num_bytes |
| 129 | << ") for a complete nlmsghdr (requires " |
| 130 | << sizeof(nlmsghdr) << ")."; |
| 131 | } |
| 132 | |
| 133 | while (num_bytes) { |
| 134 | string output; |
| 135 | size_t bytes_this_row = min(num_bytes, static_cast<size_t>(32)); |
| 136 | for (size_t i = 0; i < bytes_this_row; ++i) { |
| 137 | StringAppendF(&output, " %02x", *buf++); |
| 138 | } |
| 139 | SLOG(WiFi, log_level) << output; |
| 140 | num_bytes -= bytes_this_row; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // ErrorAckMessage. |
| 145 | |
| 146 | const uint16_t ErrorAckMessage::kMessageType = NLMSG_ERROR; |
| 147 | |
| 148 | bool ErrorAckMessage::InitFromNlmsg(const nlmsghdr *const_msg) { |
| 149 | if (!const_msg) { |
| 150 | LOG(ERROR) << "Null |const_msg| parameter"; |
| 151 | return false; |
| 152 | } |
| 153 | ByteString message(reinterpret_cast<const unsigned char *>(const_msg), |
| 154 | const_msg->nlmsg_len); |
| 155 | if (!InitAndStripHeader(&message)) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // Get the error code from the payload. |
| 160 | error_ = *(reinterpret_cast<const uint32_t *>(message.GetConstData())); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | ByteString ErrorAckMessage::Encode(uint32_t sequence_number) { |
| 165 | LOG(ERROR) << "We're not supposed to send errors or Acks to the kernel"; |
| 166 | return ByteString(); |
| 167 | } |
| 168 | |
| 169 | string ErrorAckMessage::ToString() const { |
| 170 | string output; |
| 171 | if (error()) { |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 172 | StringAppendF(&output, "NETLINK_ERROR 0x%" PRIx32 ": %s", |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 173 | -error_, strerror(-error_)); |
| 174 | } else { |
| 175 | StringAppendF(&output, "ACK"); |
| 176 | } |
| 177 | return output; |
| 178 | } |
| 179 | |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 180 | void ErrorAckMessage::Print(int header_log_level, |
| 181 | int /*detail_log_level*/) const { |
| 182 | SLOG(WiFi, header_log_level) << ToString(); |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // NoopMessage. |
| 186 | |
| 187 | const uint16_t NoopMessage::kMessageType = NLMSG_NOOP; |
| 188 | |
| 189 | ByteString NoopMessage::Encode(uint32_t sequence_number) { |
| 190 | LOG(ERROR) << "We're not supposed to send NOOP to the kernel"; |
| 191 | return ByteString(); |
| 192 | } |
| 193 | |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 194 | void NoopMessage::Print(int header_log_level, int /*detail_log_level*/) const { |
| 195 | SLOG(WiFi, header_log_level) << ToString(); |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // DoneMessage. |
| 199 | |
| 200 | const uint16_t DoneMessage::kMessageType = NLMSG_DONE; |
| 201 | |
| 202 | ByteString DoneMessage::Encode(uint32_t sequence_number) { |
Wade Guthrie | bbe59f7 | 2013-05-21 08:45:39 -0700 | [diff] [blame] | 203 | return EncodeHeader(sequence_number); |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 206 | void DoneMessage::Print(int header_log_level, int /*detail_log_level*/) const { |
| 207 | SLOG(WiFi, header_log_level) << ToString(); |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // OverrunMessage. |
| 211 | |
| 212 | const uint16_t OverrunMessage::kMessageType = NLMSG_OVERRUN; |
| 213 | |
| 214 | ByteString OverrunMessage::Encode(uint32_t sequence_number) { |
| 215 | LOG(ERROR) << "We're not supposed to send Overruns to the kernel"; |
| 216 | return ByteString(); |
| 217 | } |
| 218 | |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 219 | void OverrunMessage::Print(int header_log_level, |
| 220 | int /*detail_log_level*/) const { |
| 221 | SLOG(WiFi, header_log_level) << ToString(); |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // UnknownMessage. |
| 225 | |
| 226 | ByteString UnknownMessage::Encode(uint32_t sequence_number) { |
| 227 | LOG(ERROR) << "We're not supposed to send UNKNOWN messages to the kernel"; |
| 228 | return ByteString(); |
| 229 | } |
| 230 | |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 231 | void UnknownMessage::Print(int header_log_level, |
| 232 | int /*detail_log_level*/) const { |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 233 | int total_bytes = message_body_.GetLength(); |
| 234 | const uint8_t *const_data = message_body_.GetConstData(); |
| 235 | |
| 236 | string output = StringPrintf("%d bytes:", total_bytes); |
| 237 | for (int i = 0; i < total_bytes; ++i) { |
| 238 | StringAppendF(&output, " 0x%02x", const_data[i]); |
| 239 | } |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 240 | SLOG(WiFi, header_log_level) << output; |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // |
| 244 | // Factory class. |
| 245 | // |
| 246 | |
| 247 | bool NetlinkMessageFactory::AddFactoryMethod(uint16_t message_type, |
| 248 | FactoryMethod factory) { |
| 249 | if (ContainsKey(factories_, message_type)) { |
| 250 | LOG(WARNING) << "Message type " << message_type << " already exists."; |
| 251 | return false; |
| 252 | } |
| 253 | if (message_type == NetlinkMessage::kIllegalMessageType) { |
| 254 | LOG(ERROR) << "Not installing factory for illegal message type."; |
| 255 | return false; |
| 256 | } |
| 257 | factories_[message_type] = factory; |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | NetlinkMessage *NetlinkMessageFactory::CreateMessage( |
| 262 | const nlmsghdr *const_msg) const { |
| 263 | if (!const_msg) { |
| 264 | LOG(ERROR) << "NULL |const_msg| parameter"; |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | scoped_ptr<NetlinkMessage> message; |
| 269 | |
| 270 | if (const_msg->nlmsg_type == NoopMessage::kMessageType) { |
| 271 | message.reset(new NoopMessage()); |
| 272 | } else if (const_msg->nlmsg_type == DoneMessage::kMessageType) { |
| 273 | message.reset(new DoneMessage()); |
| 274 | } else if (const_msg->nlmsg_type == OverrunMessage::kMessageType) { |
| 275 | message.reset(new OverrunMessage()); |
| 276 | } else if (const_msg->nlmsg_type == ErrorAckMessage::kMessageType) { |
| 277 | message.reset(new ErrorAckMessage()); |
| 278 | } else if (ContainsKey(factories_, const_msg->nlmsg_type)) { |
| 279 | map<uint16_t, FactoryMethod>::const_iterator factory; |
| 280 | factory = factories_.find(const_msg->nlmsg_type); |
| 281 | message.reset(factory->second.Run(const_msg)); |
| 282 | } |
| 283 | |
| 284 | // If no factory exists for this message _or_ if a factory exists but it |
| 285 | // failed, there'll be no message. Handle either of those cases, by |
| 286 | // creating an |UnknownMessage|. |
| 287 | if (!message) { |
| 288 | // Casting away constness since, while nlmsg_data doesn't change its |
| 289 | // parameter, it also doesn't declare its paramenter as const. |
| 290 | nlmsghdr *msg = const_cast<nlmsghdr *>(const_msg); |
| 291 | ByteString payload(reinterpret_cast<char *>(nlmsg_data(msg)), |
| 292 | nlmsg_datalen(msg)); |
| 293 | message.reset(new UnknownMessage(msg->nlmsg_type, payload)); |
| 294 | } |
| 295 | |
| 296 | if (!message->InitFromNlmsg(const_msg)) { |
| 297 | LOG(ERROR) << "Message did not initialize properly"; |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | return message.release(); |
| 302 | } |
| 303 | |
| 304 | } // namespace shill. |