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