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 | #include "shill/config80211.h" |
| 6 | |
| 7 | #include <ctype.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 8 | #include <netlink/msg.h> |
| 9 | |
| 10 | #include <map> |
| 11 | #include <sstream> |
| 12 | #include <string> |
| 13 | |
| 14 | #include <base/memory/weak_ptr.h> |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 15 | #include <base/stl_util.h> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 16 | |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 17 | #include "shill/error.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 18 | #include "shill/io_handler.h" |
| 19 | #include "shill/logging.h" |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 20 | #include "shill/nl80211_message.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 21 | #include "shill/nl80211_socket.h" |
| 22 | #include "shill/scope_logger.h" |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 23 | |
| 24 | using base::Bind; |
| 25 | using base::LazyInstance; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 26 | using std::list; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 27 | using std::string; |
| 28 | |
| 29 | namespace shill { |
| 30 | |
| 31 | namespace { |
| 32 | LazyInstance<Config80211> g_config80211 = LAZY_INSTANCE_INITIALIZER; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 33 | } // namespace |
| 34 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 35 | Config80211::EventTypeStrings *Config80211::event_types_ = NULL; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 36 | |
| 37 | Config80211::Config80211() |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 38 | : wifi_state_(kWifiDown), |
| 39 | dispatcher_(NULL), |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 40 | weak_ptr_factory_(this), |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 41 | dispatcher_callback_(Bind(&Config80211::OnRawNlMessageReceived, |
| 42 | weak_ptr_factory_.GetWeakPtr())), |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 43 | sock_(NULL) { |
| 44 | } |
| 45 | |
| 46 | Config80211::~Config80211() { |
| 47 | // Since Config80211 is a singleton, it should be safe to delete a static |
| 48 | // member. |
| 49 | delete event_types_; |
| 50 | event_types_ = NULL; |
| 51 | } |
| 52 | |
| 53 | Config80211 *Config80211::GetInstance() { |
| 54 | return g_config80211.Pointer(); |
| 55 | } |
| 56 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 57 | void Config80211::Reset() { |
| 58 | wifi_state_ = kWifiDown; |
| 59 | subscribed_events_.clear(); |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 60 | ClearBroadcastCallbacks(); |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 63 | bool Config80211::Init(EventDispatcher *dispatcher) { |
| 64 | if (!sock_) { |
| 65 | sock_ = new Nl80211Socket; |
| 66 | if (!sock_) { |
| 67 | LOG(ERROR) << "No memory"; |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if (!sock_->Init()) { |
| 72 | return false; |
| 73 | } |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 74 | // Don't make libnl do the sequence checking (because it'll not like |
| 75 | // the broadcast messages for which we'll eventually ask). We'll do all the |
| 76 | // sequence checking we need. |
| 77 | sock_->DisableSequenceChecking(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (!event_types_) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 81 | event_types_ = new EventTypeStrings; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 82 | (*event_types_)[Config80211::kEventTypeConfig] = "config"; |
| 83 | (*event_types_)[Config80211::kEventTypeScan] = "scan"; |
| 84 | (*event_types_)[Config80211::kEventTypeRegulatory] = "regulatory"; |
| 85 | (*event_types_)[Config80211::kEventTypeMlme] = "mlme"; |
| 86 | } |
| 87 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 88 | dispatcher_ = dispatcher; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 92 | bool Config80211::AddBroadcastCallback(const Callback &callback) { |
| 93 | list<Callback>::iterator i; |
| 94 | if (FindBroadcastCallback(callback)) { |
| 95 | LOG(WARNING) << "Trying to re-add a callback"; |
| 96 | return false; // Should only be one copy in the list. |
| 97 | } |
| 98 | if (callback.is_null()) { |
| 99 | LOG(WARNING) << "Trying to add a NULL callback"; |
| 100 | return false; |
| 101 | } |
| 102 | // And add the callback to the list. |
| 103 | SLOG(WiFi, 3) << "Config80211::" << __func__ << " - adding callback"; |
| 104 | broadcast_callbacks_.push_back(callback); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool Config80211::RemoveBroadcastCallback(const Callback &callback) { |
| 109 | list<Callback>::iterator i; |
| 110 | for (i = broadcast_callbacks_.begin(); i != broadcast_callbacks_.end(); ++i) { |
| 111 | if ((*i).Equals(callback)) { |
| 112 | broadcast_callbacks_.erase(i); |
| 113 | // Should only be one copy in the list so we don't have to continue |
| 114 | // looking for another one. |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | LOG(WARNING) << "Callback not found."; |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | bool Config80211::FindBroadcastCallback(const Callback &callback) const { |
| 123 | list<Callback>::const_iterator i; |
| 124 | for (i = broadcast_callbacks_.begin(); i != broadcast_callbacks_.end(); ++i) { |
| 125 | if ((*i).Equals(callback)) { |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | void Config80211::ClearBroadcastCallbacks() { |
| 133 | broadcast_callbacks_.clear(); |
| 134 | } |
| 135 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 136 | bool Config80211::SendMessage(Nl80211Message *message, |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 137 | const Callback &callback) { |
| 138 | if (!message) { |
| 139 | LOG(ERROR) << "Message is NULL."; |
| 140 | return false; |
| 141 | } |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 142 | uint32 sequence_number = message->sequence_number(); |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 143 | if (!sequence_number) { |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 144 | sequence_number = sock_->GetSequenceNumber(); |
| 145 | message->set_sequence_number(sequence_number); |
| 146 | } |
| 147 | if (!sock_->SendMessage(message)) { |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 148 | LOG(ERROR) << "Failed to send nl80211 message."; |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 149 | return false; |
| 150 | } |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 151 | if (callback.is_null()) { |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 152 | LOG(INFO) << "Callback for message was null."; |
| 153 | return true; |
| 154 | } |
| 155 | if (ContainsKey(message_callbacks_, sequence_number)) { |
| 156 | LOG(ERROR) << "Sent message, but already had a callback for this message?"; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 159 | message_callbacks_[sequence_number] = callback; |
| 160 | LOG(INFO) << "Sent nl80211 message with sequence number: " << sequence_number; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 164 | bool Config80211::RemoveMessageCallback(const Nl80211Message &message) { |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 165 | if (!ContainsKey(message_callbacks_, message.sequence_number())) { |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 166 | return false; |
| 167 | } |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 168 | message_callbacks_.erase(message.sequence_number()); |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 172 | // static |
| 173 | bool Config80211::GetEventTypeString(EventType type, string *value) { |
| 174 | if (!value) { |
| 175 | LOG(ERROR) << "NULL |value|"; |
| 176 | return false; |
| 177 | } |
| 178 | if (!event_types_) { |
| 179 | LOG(ERROR) << "NULL |event_types_|"; |
| 180 | return false; |
| 181 | } |
| 182 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 183 | EventTypeStrings::iterator match = (*event_types_).find(type); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 184 | if (match == (*event_types_).end()) { |
| 185 | LOG(ERROR) << "Event type " << type << " not found"; |
| 186 | return false; |
| 187 | } |
| 188 | *value = match->second; |
| 189 | return true; |
| 190 | } |
| 191 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 192 | void Config80211::SetWifiState(WifiState new_state) { |
| 193 | if (wifi_state_ == new_state) { |
| 194 | return; |
| 195 | } |
| 196 | |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 197 | if (!sock_) { |
| 198 | LOG(ERROR) << "Config80211::Init needs to be called before this"; |
| 199 | return; |
| 200 | } |
| 201 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 202 | if (new_state == kWifiUp) { |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 203 | // Install ourselves in the shill mainloop so we receive messages on the |
| 204 | // nl80211 socket. |
| 205 | if (dispatcher_) { |
| 206 | dispatcher_handler_.reset(dispatcher_->CreateInputHandler( |
| 207 | GetFd(), |
| 208 | dispatcher_callback_, |
| 209 | Bind(&Config80211::OnReadError, weak_ptr_factory_.GetWeakPtr()))); |
| 210 | } |
| 211 | |
| 212 | // If we're newly-up, subscribe to all the event types that have been |
| 213 | // requested. |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 214 | SubscribedEvents::const_iterator i; |
| 215 | for (i = subscribed_events_.begin(); i != subscribed_events_.end(); ++i) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 216 | ActuallySubscribeToEvents(*i); |
| 217 | } |
| 218 | } |
| 219 | wifi_state_ = new_state; |
| 220 | } |
| 221 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 222 | bool Config80211::SubscribeToEvents(EventType type) { |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 223 | bool it_worked = true; |
| 224 | if (!ContainsKey(subscribed_events_, type)) { |
| 225 | if (wifi_state_ == kWifiUp) { |
| 226 | it_worked = ActuallySubscribeToEvents(type); |
| 227 | } |
| 228 | // |subscribed_events_| is a list of events to which we want to subscribe |
| 229 | // when wifi comes up (including when it comes _back_ up after it goes |
| 230 | // down sometime in the future). |
| 231 | subscribed_events_.insert(type); |
| 232 | } |
| 233 | return it_worked; |
| 234 | } |
| 235 | |
| 236 | bool Config80211::ActuallySubscribeToEvents(EventType type) { |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 237 | string group_name; |
| 238 | |
| 239 | if (!GetEventTypeString(type, &group_name)) { |
| 240 | return false; |
| 241 | } |
| 242 | if (!sock_->AddGroupMembership(group_name)) { |
| 243 | return false; |
| 244 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 248 | void Config80211::OnRawNlMessageReceived(InputData *data) { |
| 249 | if (!data) { |
| 250 | LOG(ERROR) << __func__ << "() called with null header."; |
| 251 | return; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 252 | } |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 253 | unsigned char *buf = data->buf; |
| 254 | unsigned char *end = buf + data->len; |
| 255 | while (buf < end) { |
| 256 | nlmsghdr *msg = reinterpret_cast<nlmsghdr *>(buf); |
| 257 | // Discard the message if there're not enough bytes to a) tell the code how |
| 258 | // much space is in the message (i.e., to access nlmsg_len) or b) to hold |
| 259 | // the entire message. The odd calculation is there to keep the code from |
| 260 | // potentially calculating an illegal address (causes a segfault on some |
| 261 | // architectures). |
| 262 | size_t bytes_left = end - buf; |
| 263 | if (((bytes_left < (offsetof(nlmsghdr, nlmsg_len) + |
| 264 | sizeof(nlmsghdr::nlmsg_len))) || |
| 265 | (bytes_left < msg->nlmsg_len))) { |
| 266 | LOG(ERROR) << "Discarding incomplete message."; |
| 267 | return; |
| 268 | } |
| 269 | OnNlMessageReceived(msg); |
| 270 | buf += msg->nlmsg_len; |
| 271 | } |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 272 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 273 | |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 274 | void Config80211::OnNlMessageReceived(nlmsghdr *msg) { |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 275 | if (!msg) { |
| 276 | LOG(ERROR) << __func__ << "() called with null header."; |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 277 | return; |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 278 | } |
| 279 | const uint32 sequence_number = msg->nlmsg_seq; |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 280 | SLOG(WiFi, 3) << "\t Entering " << __func__ |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 281 | << " (msg:" << sequence_number << ")"; |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 282 | scoped_ptr<Nl80211Message> message(Nl80211MessageFactory::CreateMessage(msg)); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 283 | if (message == NULL) { |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 284 | SLOG(WiFi, 3) << __func__ << "(msg:NULL)"; |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 285 | return; // Skip current message, continue parsing buffer. |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 286 | } |
| 287 | // Call (then erase) any message-specific callback. |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 288 | // TODO(wdg): Support multi-part messages; don't delete callback until last |
| 289 | // part appears. |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 290 | if (ContainsKey(message_callbacks_, sequence_number)) { |
| 291 | SLOG(WiFi, 3) << "found message-specific callback"; |
| 292 | if (message_callbacks_[sequence_number].is_null()) { |
| 293 | LOG(ERROR) << "Callback exists but is NULL for ID " << sequence_number; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 294 | } else { |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 295 | message_callbacks_[sequence_number].Run(*message); |
| 296 | } |
| 297 | message_callbacks_.erase(sequence_number); |
| 298 | } else { |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 299 | list<Callback>::const_iterator i = broadcast_callbacks_.begin(); |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 300 | while (i != broadcast_callbacks_.end()) { |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 301 | SLOG(WiFi, 3) << " " << __func__ << " - calling callback"; |
| 302 | i->Run(*message); |
| 303 | ++i; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 304 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 305 | } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 306 | } |
| 307 | |
repo sync | bcaa694 | 2013-01-02 15:38:21 -0800 | [diff] [blame^] | 308 | void Config80211::OnReadError(const Error &error) { |
| 309 | // TODO(wdg): When config80211 is used for scan, et al., this should either be |
| 310 | // LOG(FATAL) or the code should properly deal with errors, e.g., dropped |
| 311 | // messages due to the socket buffer being full. |
| 312 | LOG(ERROR) << "Config80211's netlink Socket read returns error: " |
| 313 | << error.message(); |
| 314 | } |
| 315 | |
| 316 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 317 | } // namespace shill. |