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 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 5 | #include "shill/netlink_manager.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 6 | |
Wade Guthrie | 89e6cb3 | 2013-03-07 08:03:45 -0800 | [diff] [blame] | 7 | #include <netlink/netlink.h> |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 8 | #include <sys/select.h> |
| 9 | #include <sys/time.h> |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 10 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 11 | #include <map> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 12 | |
| 13 | #include <base/memory/weak_ptr.h> |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 14 | #include <base/stl_util.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 15 | |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 16 | #include "shill/attribute_list.h" |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 17 | #include "shill/error.h" |
Wade Guthrie | 89e6cb3 | 2013-03-07 08:03:45 -0800 | [diff] [blame] | 18 | #include "shill/event_dispatcher.h" |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 19 | #include "shill/generic_netlink_message.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 20 | #include "shill/io_handler.h" |
| 21 | #include "shill/logging.h" |
Wade Guthrie | 0ae4b8e | 2013-04-10 16:49:15 -0700 | [diff] [blame] | 22 | #include "shill/netlink_message.h" |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 23 | #include "shill/netlink_socket.h" |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 24 | #include "shill/nl80211_message.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 25 | #include "shill/scope_logger.h" |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 26 | #include "shill/shill_time.h" |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 27 | #include "shill/sockets.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 28 | |
| 29 | using base::Bind; |
| 30 | using base::LazyInstance; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 31 | using std::list; |
Wade Guthrie | c6c8196 | 2013-03-06 15:47:13 -0800 | [diff] [blame] | 32 | using std::map; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 33 | using std::string; |
| 34 | |
| 35 | namespace shill { |
| 36 | |
| 37 | namespace { |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 38 | LazyInstance<NetlinkManager> g_netlink_manager = LAZY_INSTANCE_INITIALIZER; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 39 | } // namespace |
| 40 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 41 | const char NetlinkManager::kEventTypeConfig[] = "config"; |
| 42 | const char NetlinkManager::kEventTypeScan[] = "scan"; |
| 43 | const char NetlinkManager::kEventTypeRegulatory[] = "regulatory"; |
| 44 | const char NetlinkManager::kEventTypeMlme[] = "mlme"; |
| 45 | const long NetlinkManager::kMaximumNewFamilyWaitSeconds = 1; |
| 46 | const long NetlinkManager::kMaximumNewFamilyWaitMicroSeconds = 0; |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 47 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 48 | NetlinkManager::NetlinkResponseHandler::NetlinkResponseHandler( |
| 49 | const NetlinkManager::NetlinkAuxilliaryMessageHandler &error_handler) |
| 50 | : error_handler_(error_handler) {} |
| 51 | |
| 52 | NetlinkManager::NetlinkResponseHandler::~NetlinkResponseHandler() {} |
| 53 | |
| 54 | void NetlinkManager::NetlinkResponseHandler::HandleError( |
| 55 | const NetlinkMessage *netlink_message) const { |
| 56 | if (!error_handler_.is_null()) |
| 57 | error_handler_.Run(netlink_message); |
| 58 | } |
| 59 | |
| 60 | class ControlResponseHandler : public NetlinkManager::NetlinkResponseHandler { |
| 61 | public: |
| 62 | ControlResponseHandler( |
| 63 | const NetlinkManager::ControlNetlinkMessageHandler &handler, |
| 64 | const NetlinkManager::NetlinkAuxilliaryMessageHandler &error_handler) |
| 65 | : NetlinkManager::NetlinkResponseHandler(error_handler), |
| 66 | handler_(handler) {} |
| 67 | |
| 68 | virtual bool HandleMessage( |
| 69 | const NetlinkMessage &netlink_message) const override { |
| 70 | if (netlink_message.message_type() != |
| 71 | ControlNetlinkMessage::GetMessageType()) { |
| 72 | LOG(ERROR) << "Message is type " << netlink_message.message_type() |
| 73 | << ", not " << ControlNetlinkMessage::GetMessageType() |
| 74 | << " (Control)."; |
| 75 | return false; |
| 76 | } |
| 77 | if (!handler_.is_null()) { |
| 78 | const ControlNetlinkMessage *message = |
| 79 | dynamic_cast<const ControlNetlinkMessage *>(&netlink_message); |
| 80 | handler_.Run(*message); |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | NetlinkManager::ControlNetlinkMessageHandler handler_; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(ControlResponseHandler); |
| 89 | }; |
| 90 | |
| 91 | class Nl80211ResponseHandler : public NetlinkManager::NetlinkResponseHandler { |
| 92 | public: |
| 93 | Nl80211ResponseHandler( |
| 94 | const NetlinkManager::Nl80211MessageHandler &handler, |
| 95 | const NetlinkManager::NetlinkAuxilliaryMessageHandler &error_handler) |
| 96 | : NetlinkManager::NetlinkResponseHandler(error_handler), |
| 97 | handler_(handler) {} |
| 98 | |
| 99 | virtual bool HandleMessage( |
| 100 | const NetlinkMessage &netlink_message) const override { |
| 101 | if (netlink_message.message_type() != Nl80211Message::GetMessageType()) { |
| 102 | LOG(ERROR) << "Message is type " << netlink_message.message_type() |
| 103 | << ", not " << Nl80211Message::GetMessageType() |
| 104 | << " (Nl80211)."; |
| 105 | return false; |
| 106 | } |
| 107 | if (!handler_.is_null()) { |
| 108 | const Nl80211Message *message = |
| 109 | dynamic_cast<const Nl80211Message *>(&netlink_message); |
| 110 | handler_.Run(*message); |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | NetlinkManager::Nl80211MessageHandler handler_; |
| 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(Nl80211ResponseHandler); |
| 119 | }; |
| 120 | |
| 121 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 122 | NetlinkManager::MessageType::MessageType() : |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 123 | family_id(NetlinkMessage::kIllegalMessageType) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 124 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 125 | NetlinkManager::NetlinkManager() |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 126 | : dispatcher_(NULL), |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 127 | weak_ptr_factory_(this), |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 128 | dispatcher_callback_(Bind(&NetlinkManager::OnRawNlMessageReceived, |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 129 | weak_ptr_factory_.GetWeakPtr())), |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 130 | sock_(NULL), |
| 131 | time_(Time::GetInstance()) {} |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 132 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 133 | NetlinkManager *NetlinkManager::GetInstance() { |
| 134 | return g_netlink_manager.Pointer(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 137 | void NetlinkManager::Reset(bool full) { |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 138 | ClearBroadcastHandlers(); |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 139 | message_handlers_.clear(); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 140 | message_types_.clear(); |
Darin Petkov | d581838 | 2013-01-28 16:27:07 +0100 | [diff] [blame] | 141 | if (full) { |
| 142 | dispatcher_ = NULL; |
| 143 | delete sock_; |
| 144 | sock_ = NULL; |
| 145 | } |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 148 | void NetlinkManager::OnNewFamilyMessage(const ControlNetlinkMessage &message) { |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 149 | uint16_t family_id; |
| 150 | string family_name; |
| 151 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 152 | if (!message.const_attributes()->GetU16AttributeValue(CTRL_ATTR_FAMILY_ID, |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 153 | &family_id)) { |
| 154 | LOG(ERROR) << __func__ << ": Couldn't get family_id attribute"; |
| 155 | return; |
| 156 | } |
| 157 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 158 | if (!message.const_attributes()->GetStringAttributeValue( |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 159 | CTRL_ATTR_FAMILY_NAME, &family_name)) { |
| 160 | LOG(ERROR) << __func__ << ": Couldn't get family_name attribute"; |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | SLOG(WiFi, 3) << "Socket family '" << family_name << "' has id=" << family_id; |
| 165 | |
| 166 | // Extract the available multicast groups from the message. |
| 167 | AttributeListConstRefPtr multicast_groups; |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 168 | if (message.const_attributes()->ConstGetNestedAttributeList( |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 169 | CTRL_ATTR_MCAST_GROUPS, &multicast_groups)) { |
| 170 | AttributeListConstRefPtr current_group; |
| 171 | |
| 172 | for (int i = 1; |
| 173 | multicast_groups->ConstGetNestedAttributeList(i, ¤t_group); |
| 174 | ++i) { |
| 175 | string group_name; |
| 176 | uint32_t group_id; |
| 177 | if (!current_group->GetStringAttributeValue(CTRL_ATTR_MCAST_GRP_NAME, |
| 178 | &group_name)) { |
| 179 | LOG(WARNING) << "Expected CTRL_ATTR_MCAST_GRP_NAME, found none"; |
| 180 | continue; |
| 181 | } |
| 182 | if (!current_group->GetU32AttributeValue(CTRL_ATTR_MCAST_GRP_ID, |
| 183 | &group_id)) { |
| 184 | LOG(WARNING) << "Expected CTRL_ATTR_MCAST_GRP_ID, found none"; |
| 185 | continue; |
| 186 | } |
| 187 | SLOG(WiFi, 3) << " Adding group '" << group_name << "' = " << group_id; |
| 188 | message_types_[family_name].groups[group_name] = group_id; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | message_types_[family_name].family_id = family_id; |
| 193 | } |
| 194 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 195 | // static |
| 196 | void NetlinkManager::OnNetlinkMessageError(const NetlinkMessage *raw_message) { |
| 197 | if (!raw_message) { |
| 198 | LOG(ERROR) << "NetlinkManager error."; |
| 199 | return; |
| 200 | } |
| 201 | if (raw_message->message_type() == ErrorAckMessage::GetMessageType()) { |
| 202 | const ErrorAckMessage *error_ack_message = |
| 203 | dynamic_cast<const ErrorAckMessage *>(raw_message); |
| 204 | if (error_ack_message->error()) { |
| 205 | LOG(ERROR) << __func__ << ": Message (seq: " |
| 206 | << error_ack_message->sequence_number() << ") failed: " |
| 207 | << error_ack_message->ToString(); |
| 208 | } else { |
| 209 | SLOG(WiFi, 6) << __func__ << ": Message (seq: " |
| 210 | << error_ack_message->sequence_number() << ") ACKed"; |
| 211 | } |
| 212 | return; |
| 213 | } |
| 214 | LOG(ERROR) << "Not expecting this message:"; |
| 215 | raw_message->Print(0, 0); |
| 216 | } |
| 217 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 218 | bool NetlinkManager::Init() { |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 219 | // Install message factory for control class of messages, which has |
| 220 | // statically-known message type. |
| 221 | message_factory_.AddFactoryMethod( |
| 222 | ControlNetlinkMessage::kMessageType, |
| 223 | Bind(&ControlNetlinkMessage::CreateMessage)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 224 | if (!sock_) { |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 225 | sock_ = new NetlinkSocket; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 226 | if (!sock_) { |
| 227 | LOG(ERROR) << "No memory"; |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | if (!sock_->Init()) { |
| 232 | return false; |
| 233 | } |
| 234 | } |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 235 | return true; |
| 236 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 237 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 238 | void NetlinkManager::Start(EventDispatcher *dispatcher) { |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 239 | dispatcher_ = dispatcher; |
| 240 | CHECK(dispatcher_); |
| 241 | // Install ourselves in the shill mainloop so we receive messages on the |
| 242 | // netlink socket. |
| 243 | dispatcher_handler_.reset(dispatcher_->CreateInputHandler( |
| 244 | file_descriptor(), |
| 245 | dispatcher_callback_, |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 246 | Bind(&NetlinkManager::OnReadError, weak_ptr_factory_.GetWeakPtr()))); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 249 | int NetlinkManager::file_descriptor() const { |
Wade Guthrie | 89e6cb3 | 2013-03-07 08:03:45 -0800 | [diff] [blame] | 250 | return (sock_ ? sock_->file_descriptor() : -1); |
| 251 | } |
| 252 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 253 | uint16_t NetlinkManager::GetFamily(const string &name, |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 254 | const NetlinkMessageFactory::FactoryMethod &message_factory) { |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 255 | MessageType &message_type = message_types_[name]; |
| 256 | if (message_type.family_id != NetlinkMessage::kIllegalMessageType) { |
| 257 | return message_type.family_id; |
| 258 | } |
| 259 | if (!sock_) { |
| 260 | LOG(FATAL) << "Must call |Init| before this method."; |
| 261 | return false; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 264 | GetFamilyMessage msg; |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 265 | if (!msg.attributes()->SetStringAttributeValue(CTRL_ATTR_FAMILY_NAME, name)) { |
| 266 | LOG(ERROR) << "Couldn't set string attribute"; |
| 267 | return false; |
| 268 | } |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 269 | SendControlMessage(&msg, |
| 270 | Bind(&NetlinkManager::OnNewFamilyMessage, |
| 271 | weak_ptr_factory_.GetWeakPtr()), |
| 272 | Bind(&NetlinkManager::OnNetlinkMessageError)); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 273 | |
| 274 | // Wait for a response. The code absolutely needs family_ids for its |
| 275 | // message types so we do a synchronous wait. It's OK to do this because |
| 276 | // a) libnl does a synchronous wait (so there's prior art), b) waiting |
| 277 | // asynchronously would add significant and unnecessary complexity to the |
| 278 | // code that deals with pending messages that could, potentially, be waiting |
| 279 | // for a message type, and c) it really doesn't take very long for the |
| 280 | // GETFAMILY / NEWFAMILY transaction to transpire (this transaction was timed |
| 281 | // over 20 times and found a maximum duration of 11.1 microseconds and an |
| 282 | // average of 4.0 microseconds). |
| 283 | struct timeval start_time, now, end_time; |
| 284 | struct timeval maximum_wait_duration = {kMaximumNewFamilyWaitSeconds, |
| 285 | kMaximumNewFamilyWaitMicroSeconds}; |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 286 | time_->GetTimeMonotonic(&start_time); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 287 | now = start_time; |
| 288 | timeradd(&start_time, &maximum_wait_duration, &end_time); |
| 289 | |
| 290 | do { |
| 291 | // Wait with timeout for a message from the netlink socket. |
| 292 | fd_set read_fds; |
| 293 | FD_ZERO(&read_fds); |
| 294 | FD_SET(file_descriptor(), &read_fds); |
| 295 | struct timeval wait_duration; |
| 296 | timersub(&end_time, &now, &wait_duration); |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 297 | int result = sock_->sockets()->Select(file_descriptor() + 1, |
| 298 | &read_fds, |
| 299 | NULL, |
| 300 | NULL, |
| 301 | &wait_duration); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 302 | if (result < 0) { |
| 303 | PLOG(ERROR) << "Select failed"; |
| 304 | return NetlinkMessage::kIllegalMessageType; |
| 305 | } |
| 306 | if (result == 0) { |
| 307 | LOG(WARNING) << "Timed out waiting for family_id for family '" |
| 308 | << name << "'."; |
| 309 | return NetlinkMessage::kIllegalMessageType; |
| 310 | } |
| 311 | |
| 312 | // Read and process any messages. |
| 313 | ByteString received; |
| 314 | sock_->RecvMessage(&received); |
| 315 | InputData input_data(received.GetData(), received.GetLength()); |
| 316 | OnRawNlMessageReceived(&input_data); |
| 317 | if (message_type.family_id != NetlinkMessage::kIllegalMessageType) { |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 318 | uint16_t family_id = message_type.family_id; |
| 319 | if (family_id != NetlinkMessage::kIllegalMessageType) { |
| 320 | message_factory_.AddFactoryMethod(family_id, message_factory); |
| 321 | } |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 322 | time_->GetTimeMonotonic(&now); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 323 | timersub(&now, &start_time, &wait_duration); |
| 324 | SLOG(WiFi, 5) << "Found id " << message_type.family_id |
| 325 | << " for name '" << name << "' in " |
| 326 | << wait_duration.tv_sec << " sec, " |
| 327 | << wait_duration.tv_usec << " usec."; |
| 328 | return message_type.family_id; |
| 329 | } |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 330 | time_->GetTimeMonotonic(&now); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 331 | } while (timercmp(&now, &end_time, <)); |
| 332 | |
| 333 | LOG(ERROR) << "Timed out waiting for family_id for family '" << name << "'."; |
| 334 | return NetlinkMessage::kIllegalMessageType; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 337 | bool NetlinkManager::AddBroadcastHandler(const NetlinkMessageHandler &handler) { |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 338 | list<NetlinkMessageHandler>::iterator i; |
| 339 | if (FindBroadcastHandler(handler)) { |
| 340 | LOG(WARNING) << "Trying to re-add a handler"; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 341 | return false; // Should only be one copy in the list. |
| 342 | } |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 343 | if (handler.is_null()) { |
| 344 | LOG(WARNING) << "Trying to add a NULL handler"; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 345 | return false; |
| 346 | } |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 347 | // And add the handler to the list. |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 348 | SLOG(WiFi, 3) << "NetlinkManager::" << __func__ << " - adding handler"; |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 349 | broadcast_handlers_.push_back(handler); |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 353 | bool NetlinkManager::RemoveBroadcastHandler( |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 354 | const NetlinkMessageHandler &handler) { |
| 355 | list<NetlinkMessageHandler>::iterator i; |
| 356 | for (i = broadcast_handlers_.begin(); i != broadcast_handlers_.end(); ++i) { |
| 357 | if ((*i).Equals(handler)) { |
| 358 | broadcast_handlers_.erase(i); |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 359 | // Should only be one copy in the list so we don't have to continue |
| 360 | // looking for another one. |
| 361 | return true; |
| 362 | } |
| 363 | } |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 364 | LOG(WARNING) << "NetlinkMessageHandler not found."; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 365 | return false; |
| 366 | } |
| 367 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 368 | bool NetlinkManager::FindBroadcastHandler(const NetlinkMessageHandler &handler) |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 369 | const { |
| 370 | list<NetlinkMessageHandler>::const_iterator i; |
| 371 | for (i = broadcast_handlers_.begin(); i != broadcast_handlers_.end(); ++i) { |
| 372 | if ((*i).Equals(handler)) { |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 373 | return true; |
| 374 | } |
| 375 | } |
| 376 | return false; |
| 377 | } |
| 378 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 379 | void NetlinkManager::ClearBroadcastHandlers() { |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 380 | broadcast_handlers_.clear(); |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 383 | bool NetlinkManager::SendControlMessage( |
| 384 | ControlNetlinkMessage *message, |
| 385 | const ControlNetlinkMessageHandler &message_handler, |
| 386 | const NetlinkAuxilliaryMessageHandler &error_handler) { |
| 387 | return SendMessageInternal(message, |
| 388 | new ControlResponseHandler(message_handler, |
| 389 | error_handler)); |
| 390 | } |
| 391 | |
| 392 | bool NetlinkManager::SendNl80211Message( |
| 393 | Nl80211Message *message, |
| 394 | const Nl80211MessageHandler &message_handler, |
| 395 | const NetlinkAuxilliaryMessageHandler &error_handler) { |
| 396 | return SendMessageInternal(message, |
| 397 | new Nl80211ResponseHandler(message_handler, |
| 398 | error_handler)); |
| 399 | } |
| 400 | |
| 401 | bool NetlinkManager::SendMessageInternal( |
| 402 | NetlinkMessage *message, |
| 403 | NetlinkManager::NetlinkResponseHandler *response_handler) { |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 404 | if (!message) { |
| 405 | LOG(ERROR) << "Message is NULL."; |
| 406 | return false; |
| 407 | } |
Wade Guthrie | bdcdaa7 | 2013-03-04 12:47:12 -0800 | [diff] [blame] | 408 | |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 409 | ByteString message_string = message->Encode(this->GetSequenceNumber()); |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 410 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 411 | if (!response_handler) { |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 412 | SLOG(WiFi, 3) << "Handler for message was null."; |
| 413 | } else if (ContainsKey(message_handlers_, message->sequence_number())) { |
| 414 | LOG(ERROR) << "A handler already existed for sequence: " |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 415 | << message->sequence_number(); |
| 416 | return false; |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 417 | } else { |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 418 | message_handlers_[message->sequence_number()] = |
| 419 | NetlinkResponseHandlerRefPtr(response_handler); |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 420 | } |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 421 | |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 422 | SLOG(WiFi, 5) << "NL Message " << message->sequence_number() |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 423 | << " Sending (" << message_string.GetLength() |
| 424 | << " bytes) ===>"; |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 425 | message->Print(6, 7); |
| 426 | NetlinkMessage::PrintBytes(8, message_string.GetConstData(), |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 427 | message_string.GetLength()); |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 428 | |
Wade Guthrie | bdcdaa7 | 2013-03-04 12:47:12 -0800 | [diff] [blame] | 429 | if (!sock_->SendMessage(message_string)) { |
Wade Guthrie | f48a195 | 2013-03-04 17:33:47 -0800 | [diff] [blame] | 430 | LOG(ERROR) << "Failed to send Netlink message."; |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 431 | return false; |
| 432 | } |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 433 | return true; |
| 434 | } |
| 435 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 436 | bool NetlinkManager::RemoveMessageHandler(const NetlinkMessage &message) { |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 437 | if (!ContainsKey(message_handlers_, message.sequence_number())) { |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 438 | return false; |
| 439 | } |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 440 | message_handlers_.erase(message.sequence_number()); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 441 | return true; |
| 442 | } |
| 443 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 444 | uint32_t NetlinkManager::GetSequenceNumber() { |
Wade Guthrie | bdcdaa7 | 2013-03-04 12:47:12 -0800 | [diff] [blame] | 445 | return sock_ ? |
Wade Guthrie | f48a195 | 2013-03-04 17:33:47 -0800 | [diff] [blame] | 446 | sock_->GetSequenceNumber() : NetlinkMessage::kBroadcastSequenceNumber; |
Wade Guthrie | bdcdaa7 | 2013-03-04 12:47:12 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 449 | bool NetlinkManager::SubscribeToEvents(const string &family_id, |
| 450 | const string &group_name) { |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 451 | if (!ContainsKey(message_types_, family_id)) { |
| 452 | LOG(ERROR) << "Family '" << family_id << "' doesn't exist"; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 453 | return false; |
| 454 | } |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 455 | |
| 456 | if (!ContainsKey(message_types_[family_id].groups, group_name)) { |
| 457 | LOG(ERROR) << "Group '" << group_name << "' doesn't exist in family '" |
| 458 | << family_id << "'"; |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | uint32_t group_id = message_types_[family_id].groups[group_name]; |
| 463 | if (!sock_) { |
| 464 | LOG(FATAL) << "Need to call |Init| first."; |
| 465 | } |
| 466 | return sock_->SubscribeToEvents(group_id); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 469 | void NetlinkManager::OnRawNlMessageReceived(InputData *data) { |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 470 | if (!data) { |
| 471 | LOG(ERROR) << __func__ << "() called with null header."; |
| 472 | return; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 473 | } |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 474 | unsigned char *buf = data->buf; |
| 475 | unsigned char *end = buf + data->len; |
| 476 | while (buf < end) { |
| 477 | nlmsghdr *msg = reinterpret_cast<nlmsghdr *>(buf); |
| 478 | // Discard the message if there're not enough bytes to a) tell the code how |
| 479 | // much space is in the message (i.e., to access nlmsg_len) or b) to hold |
| 480 | // the entire message. The odd calculation is there to keep the code from |
| 481 | // potentially calculating an illegal address (causes a segfault on some |
| 482 | // architectures). |
| 483 | size_t bytes_left = end - buf; |
| 484 | if (((bytes_left < (offsetof(nlmsghdr, nlmsg_len) + |
Liam McLoughlin | f4baef2 | 2012-08-01 19:08:25 -0700 | [diff] [blame] | 485 | sizeof(msg->nlmsg_len))) || |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 486 | (bytes_left < msg->nlmsg_len))) { |
| 487 | LOG(ERROR) << "Discarding incomplete message."; |
| 488 | return; |
| 489 | } |
| 490 | OnNlMessageReceived(msg); |
| 491 | buf += msg->nlmsg_len; |
| 492 | } |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 493 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 494 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 495 | void NetlinkManager::OnNlMessageReceived(nlmsghdr *msg) { |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 496 | if (!msg) { |
| 497 | LOG(ERROR) << __func__ << "() called with null header."; |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 498 | return; |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 499 | } |
| 500 | const uint32 sequence_number = msg->nlmsg_seq; |
Wade Guthrie | 12f113a | 2013-03-12 17:15:46 -0700 | [diff] [blame] | 501 | scoped_ptr<NetlinkMessage> message(message_factory_.CreateMessage(msg)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 502 | if (message == NULL) { |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 503 | SLOG(WiFi, 3) << "NL Message " << sequence_number << " <==="; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 504 | SLOG(WiFi, 3) << __func__ << "(msg:NULL)"; |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 505 | return; // Skip current message, continue parsing buffer. |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 506 | } |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 507 | SLOG(WiFi, 5) << "NL Message " << sequence_number |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 508 | << " Received (" << msg->nlmsg_len << " bytes) <==="; |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 509 | message->Print(6, 7); |
Wade Guthrie | f48a195 | 2013-03-04 17:33:47 -0800 | [diff] [blame] | 510 | NetlinkMessage::PrintBytes(8, reinterpret_cast<const unsigned char *>(msg), |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 511 | msg->nlmsg_len); |
| 512 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 513 | if (message->message_type() == ErrorAckMessage::GetMessageType()) { |
| 514 | SLOG(WiFi, 3) << "Error response to message " << sequence_number; |
| 515 | const ErrorAckMessage *error_ack_message = |
| 516 | dynamic_cast<const ErrorAckMessage *>(message.get()); |
| 517 | if (error_ack_message->error()) { |
| 518 | if (ContainsKey(message_handlers_, sequence_number)) { |
| 519 | SLOG(WiFi, 6) << "Found message-specific error handler"; |
| 520 | message_handlers_[sequence_number]->HandleError(message.get()); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 521 | message_handlers_.erase(sequence_number); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 522 | } |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 523 | } else { |
| 524 | SLOG(WiFi, 3) << "ACK message -- not removing callback"; |
| 525 | } |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | if (ContainsKey(message_handlers_, sequence_number)) { |
| 530 | SLOG(WiFi, 6) << "Found message-specific handler"; |
| 531 | if (!message_handlers_[sequence_number]->HandleMessage(*message)) { |
| 532 | LOG(ERROR) << "Couldn't call message handler for " << sequence_number; |
| 533 | // Call the error handler but, since we don't have an |ErrorAckMessage|, |
| 534 | // we'll have to pass a NULL pointer. |
| 535 | message_handlers_[sequence_number]->HandleError(NULL); |
| 536 | } |
| 537 | if ((message->flags() & NLM_F_MULTI) && |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 538 | (message->message_type() != NLMSG_DONE)) { |
Wade Guthrie | 0b1ebf1 | 2013-04-12 09:53:33 -0700 | [diff] [blame] | 539 | SLOG(WiFi, 6) << "Multi-part message -- not removing callback"; |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 540 | } else { |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 541 | SLOG(WiFi, 6) << "Removing callbacks"; |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 542 | message_handlers_.erase(sequence_number); |
| 543 | } |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | |
| 547 | for (const auto &handler : broadcast_handlers_) { |
| 548 | SLOG(WiFi, 6) << "Calling broadcast handler"; |
| 549 | if (!handler.is_null()) { |
| 550 | handler.Run(*message); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 551 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 552 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 555 | void NetlinkManager::OnReadError(const Error &error) { |
| 556 | // TODO(wdg): When netlink_manager is used for scan, et al., this should |
| 557 | // either be LOG(FATAL) or the code should properly deal with errors, |
| 558 | // e.g., dropped messages due to the socket buffer being full. |
| 559 | LOG(ERROR) << "NetlinkManager's netlink Socket read returns error: " |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame] | 560 | << error.message(); |
| 561 | } |
| 562 | |
| 563 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 564 | } // namespace shill. |