mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/wifi.h" |
| 6 | |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 7 | #include <linux/if.h> // Needs definitions from netinet/ether.h |
| 8 | #include <netinet/ether.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 10 | #include <string.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 11 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 12 | #include <algorithm> |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 13 | #include <limits> |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 14 | #include <map> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 15 | #include <string> |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 16 | #include <vector> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 17 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 18 | #include <base/bind.h> |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 19 | #include <base/file_path.h> |
| 20 | #include <base/file_util.h> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 21 | #include <base/string_util.h> |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 22 | #include <base/stringprintf.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 23 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 24 | #include <glib.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 25 | |
| 26 | #include "shill/control_interface.h" |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 27 | #include "shill/dbus_adaptor.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 28 | #include "shill/device.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 29 | #include "shill/error.h" |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 30 | #include "shill/geolocation_info.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 31 | #include "shill/ieee80211.h" |
Paul Stewart | 3c508e1 | 2012-08-09 11:40:06 -0700 | [diff] [blame] | 32 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 33 | #include "shill/logging.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 34 | #include "shill/manager.h" |
Thieu Le | 67370f6 | 2012-02-14 23:01:42 +0000 | [diff] [blame] | 35 | #include "shill/metrics.h" |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 36 | #include "shill/netlink_manager.h" |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 37 | #include "shill/netlink_message.h" |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 38 | #include "shill/nl80211_message.h" |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 39 | #include "shill/property_accessor.h" |
Darin Petkov | d196726 | 2011-07-18 14:55:18 -0700 | [diff] [blame] | 40 | #include "shill/proxy_factory.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 41 | #include "shill/rtnl_handler.h" |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 42 | #include "shill/scan_session.h" |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 43 | #include "shill/scope_logger.h" |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 44 | #include "shill/shill_time.h" |
Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -0700 | [diff] [blame] | 45 | #include "shill/supplicant_eap_state_handler.h" |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 46 | #include "shill/supplicant_interface_proxy_interface.h" |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 47 | #include "shill/supplicant_network_proxy_interface.h" |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 48 | #include "shill/supplicant_process_proxy_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 49 | #include "shill/technology.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 50 | #include "shill/wifi_endpoint.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 51 | #include "shill/wifi_provider.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 52 | #include "shill/wifi_service.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 53 | #include "shill/wpa_supplicant.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 54 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 55 | using base::Bind; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 56 | using base::StringPrintf; |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 57 | using file_util::PathExists; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 58 | using std::map; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 59 | using std::string; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 60 | using std::vector; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 61 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 62 | namespace shill { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 63 | |
| 64 | // statics |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 65 | const char *WiFi::kDefaultBgscanMethod = |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 66 | WPASupplicant::kNetworkBgscanMethodSimple; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 67 | const uint16 WiFi::kDefaultBgscanShortIntervalSeconds = 30; |
| 68 | const int32 WiFi::kDefaultBgscanSignalThresholdDbm = -50; |
| 69 | const uint16 WiFi::kDefaultScanIntervalSeconds = 180; |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 70 | // Scan interval while connected. |
| 71 | const uint16 WiFi::kBackgroundScanIntervalSeconds = 3601; |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 72 | // Age (in seconds) beyond which a BSS cache entry will not be preserved, |
| 73 | // across a suspend/resume. |
| 74 | const time_t WiFi::kMaxBSSResumeAgeSeconds = 10; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 75 | const char WiFi::kInterfaceStateUnknown[] = "shill-unknown"; |
mukesh agrawal | f202817 | 2012-03-13 14:20:22 -0700 | [diff] [blame] | 76 | const time_t WiFi::kRescanIntervalSeconds = 1; |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 77 | const int WiFi::kNumFastScanAttempts = 3; |
| 78 | const int WiFi::kFastScanIntervalSeconds = 10; |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 79 | const int WiFi::kPendingTimeoutSeconds = 15; |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 80 | const int WiFi::kReconnectTimeoutSeconds = 10; |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 81 | const size_t WiFi::kMinumumFrequenciesToScan = 4; // Arbitrary but > 0. |
| 82 | const char WiFi::kProgressiveScanFlagFile[] = "/home/chronos/.progressive_scan"; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 83 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 84 | WiFi::WiFi(ControlInterface *control_interface, |
| 85 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 86 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 87 | Manager *manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 88 | const string& link, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 89 | const string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 90 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 91 | : Device(control_interface, |
| 92 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 93 | metrics, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 94 | manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 95 | link, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 96 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 97 | interface_index, |
| 98 | Technology::kWifi), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 99 | provider_(manager->wifi_provider()), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 100 | weak_ptr_factory_(this), |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 101 | proxy_factory_(ProxyFactory::GetInstance()), |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 102 | time_(Time::GetInstance()), |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 103 | supplicant_present_(false), |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 104 | supplicant_state_(kInterfaceStateUnknown), |
| 105 | supplicant_bss_("(unknown)"), |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 106 | need_bss_flush_(false), |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 107 | resumed_at_((struct timeval) {0}), |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 108 | fast_scans_remaining_(kNumFastScanAttempts), |
Christopher Wiley | 8f81e2a | 2012-10-17 16:51:32 -0700 | [diff] [blame] | 109 | has_already_completed_(false), |
Paul Stewart | a47c3c6 | 2012-12-18 12:14:29 -0800 | [diff] [blame] | 110 | is_debugging_connection_(false), |
Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -0700 | [diff] [blame] | 111 | eap_state_handler_(new SupplicantEAPStateHandler()), |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 112 | bgscan_short_interval_seconds_(kDefaultBgscanShortIntervalSeconds), |
| 113 | bgscan_signal_threshold_dbm_(kDefaultBgscanSignalThresholdDbm), |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 114 | scan_pending_(false), |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 115 | scan_interval_seconds_(kDefaultScanIntervalSeconds), |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 116 | progressive_scan_enabled_(false), |
| 117 | netlink_manager_(NetlinkManager::GetInstance()), |
| 118 | min_frequencies_to_scan_(kMinumumFrequenciesToScan), |
| 119 | max_frequencies_to_scan_(std::numeric_limits<int>::max()) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 120 | PropertyStore *store = this->mutable_store(); |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 121 | store->RegisterDerivedString( |
| 122 | flimflam::kBgscanMethodProperty, |
| 123 | StringAccessor( |
| 124 | // TODO(petkov): CustomMappedAccessor is used for convenience because |
| 125 | // it provides a way to define a custom clearer (unlike |
| 126 | // CustomAccessor). We need to implement a fully custom accessor with |
| 127 | // no extra argument. |
| 128 | new CustomMappedAccessor<WiFi, string, int>(this, |
| 129 | &WiFi::ClearBgscanMethod, |
| 130 | &WiFi::GetBgscanMethod, |
| 131 | &WiFi::SetBgscanMethod, |
| 132 | 0))); // Unused. |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 133 | HelpRegisterDerivedUint16(store, |
| 134 | flimflam::kBgscanShortIntervalProperty, |
| 135 | &WiFi::GetBgscanShortInterval, |
| 136 | &WiFi::SetBgscanShortInterval); |
| 137 | HelpRegisterDerivedInt32(store, |
| 138 | flimflam::kBgscanSignalThresholdProperty, |
| 139 | &WiFi::GetBgscanSignalThreshold, |
| 140 | &WiFi::SetBgscanSignalThreshold); |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 141 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 142 | // TODO(quiche): Decide if scan_pending_ is close enough to |
| 143 | // "currently scanning" that we don't care, or if we want to track |
| 144 | // scan pending/currently scanning/no scan scheduled as a tri-state |
| 145 | // kind of thing. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 146 | store->RegisterConstBool(flimflam::kScanningProperty, &scan_pending_); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 147 | HelpRegisterDerivedUint16(store, |
| 148 | flimflam::kScanIntervalProperty, |
| 149 | &WiFi::GetScanInterval, |
| 150 | &WiFi::SetScanInterval); |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 151 | ScopeLogger::GetInstance()->RegisterScopeEnableChangedCallback( |
| 152 | ScopeLogger::kWiFi, |
| 153 | Bind(&WiFi::OnWiFiDebugScopeChanged, weak_ptr_factory_.GetWeakPtr())); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 154 | CHECK(netlink_manager_); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 155 | progressive_scan_enabled_ = PathExists(FilePath(kProgressiveScanFlagFile)); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 156 | SLOG(WiFi, 2) << "WiFi device " << link_name() << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 157 | } |
| 158 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 159 | WiFi::~WiFi() {} |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 160 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 161 | void WiFi::Start(Error *error, const EnabledStateChangedCallback &callback) { |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 162 | SLOG(WiFi, 2) << "WiFi " << link_name() << " starting."; |
| 163 | if (enabled()) { |
| 164 | return; |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 165 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 166 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 167 | if (error) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 168 | error->Reset(); // indicate immediate completion |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 169 | } |
| 170 | if (on_supplicant_appear_.IsCancelled()) { |
| 171 | // Registers the WPA supplicant appear/vanish callbacks only once per WiFi |
| 172 | // device instance. |
| 173 | on_supplicant_appear_.Reset( |
| 174 | Bind(&WiFi::OnSupplicantAppear, Unretained(this))); |
| 175 | on_supplicant_vanish_.Reset( |
| 176 | Bind(&WiFi::OnSupplicantVanish, Unretained(this))); |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 177 | manager()->dbus_manager()->WatchName(WPASupplicant::kDBusAddr, |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 178 | on_supplicant_appear_.callback(), |
| 179 | on_supplicant_vanish_.callback()); |
| 180 | } |
| 181 | // Connect to WPA supplicant if it's already present. If not, we'll connect to |
| 182 | // it when it appears. |
| 183 | ConnectToSupplicant(); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 184 | // Subscribe to multicast events. |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 185 | netlink_manager_->SubscribeToEvents(Nl80211Message::kMessageTypeString, |
| 186 | NetlinkManager::kEventTypeConfig); |
| 187 | netlink_manager_->SubscribeToEvents(Nl80211Message::kMessageTypeString, |
| 188 | NetlinkManager::kEventTypeScan); |
| 189 | netlink_manager_->SubscribeToEvents(Nl80211Message::kMessageTypeString, |
| 190 | NetlinkManager::kEventTypeRegulatory); |
| 191 | netlink_manager_->SubscribeToEvents(Nl80211Message::kMessageTypeString, |
| 192 | NetlinkManager::kEventTypeMlme); |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 193 | ConfigureScanFrequencies(); |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 196 | void WiFi::Stop(Error *error, const EnabledStateChangedCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 197 | SLOG(WiFi, 2) << "WiFi " << link_name() << " stopping."; |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 198 | DropConnection(); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 199 | StopScanTimer(); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 200 | for (EndpointMap::iterator it = endpoint_by_rpcid_.begin(); |
| 201 | it != endpoint_by_rpcid_.end(); ++it) { |
| 202 | provider_->OnEndpointRemoved(it->second); |
| 203 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 204 | endpoint_by_rpcid_.clear(); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 205 | for (ReverseServiceMap::const_iterator it = rpcid_by_service_.begin(); |
| 206 | it != rpcid_by_service_.end(); ++it) { |
| 207 | RemoveNetwork(it->second); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 208 | } |
Paul Stewart | 549d44c | 2012-07-03 12:40:25 -0700 | [diff] [blame] | 209 | rpcid_by_service_.clear(); |
Paul Stewart | 549d44c | 2012-07-03 12:40:25 -0700 | [diff] [blame] | 210 | supplicant_interface_proxy_.reset(); // breaks a reference cycle |
| 211 | // TODO(quiche): Remove interface from supplicant. |
| 212 | supplicant_process_proxy_.reset(); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 213 | current_service_ = NULL; // breaks a reference cycle |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 214 | pending_service_ = NULL; // breaks a reference cycle |
Paul Stewart | a47c3c6 | 2012-12-18 12:14:29 -0800 | [diff] [blame] | 215 | is_debugging_connection_ = false; |
Paul Stewart | d2db2b1 | 2013-01-17 13:11:07 -0800 | [diff] [blame] | 216 | SetScanPending(false); |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 217 | StopPendingTimer(); |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 218 | StopReconnectTimer(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 219 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 220 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 221 | if (error) |
| 222 | error->Reset(); // indicate immediate completion |
mukesh agrawal | c4f368f | 2012-06-04 19:45:52 -0700 | [diff] [blame] | 223 | weak_ptr_factory_.InvalidateWeakPtrs(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 224 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 225 | SLOG(WiFi, 3) << "WiFi " << link_name() << " supplicant_process_proxy_ " |
| 226 | << (supplicant_process_proxy_.get() ? |
| 227 | "is set." : "is not set."); |
| 228 | SLOG(WiFi, 3) << "WiFi " << link_name() << " supplicant_interface_proxy_ " |
| 229 | << (supplicant_interface_proxy_.get() ? |
| 230 | "is set." : "is not set."); |
| 231 | SLOG(WiFi, 3) << "WiFi " << link_name() << " pending_service_ " |
| 232 | << (pending_service_.get() ? "is set." : "is not set."); |
| 233 | SLOG(WiFi, 3) << "WiFi " << link_name() << " has " |
| 234 | << endpoint_by_rpcid_.size() << " EndpointMap entries."; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 237 | void WiFi::Scan(ScanType scan_type, Error */*error*/) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 238 | LOG(INFO) << __func__; |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 239 | // Measure scans that are supposed to be "progressive" regardless of whether |
| 240 | // we are actually doing a progressive scan. |
| 241 | if (scan_type == kProgressiveScan) { |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 242 | metrics()->NotifyDeviceScanStarted(interface_index()); |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 243 | } |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 244 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 245 | if (progressive_scan_enabled_ && scan_type == kProgressiveScan) { |
| 246 | SLOG(WiFi, 4) << "Doing progressive scan on " << link_name(); |
| 247 | if (!scan_session_) { |
| 248 | // TODO(wdg): Perform in-depth testing to determine the best values for |
| 249 | // the different scans. chromium:235293 |
| 250 | ScanSession::FractionList scan_fractions; |
| 251 | scan_fractions.push_back(.33); // First scan gets 33 percentile. |
| 252 | scan_fractions.push_back(.33); // Second scan gets 66th percentile. |
| 253 | scan_fractions.push_back(ScanSession::kAllFrequencies); |
| 254 | scan_session_.reset( |
| 255 | new ScanSession(netlink_manager_, |
| 256 | dispatcher(), |
| 257 | provider_->GetScanFrequencies(), |
| 258 | all_scan_frequencies_, |
| 259 | interface_index(), |
| 260 | scan_fractions, |
| 261 | min_frequencies_to_scan_, |
| 262 | max_frequencies_to_scan_, |
| 263 | Bind(&WiFi::OnFailedProgressiveScan, |
| 264 | weak_ptr_factory_.GetWeakPtr()))); |
| 265 | for (const auto &ssid : provider_->GetHiddenSSIDList()) { |
| 266 | scan_session_->AddSsid(ByteString(&ssid.front(), ssid.size())); |
| 267 | } |
| 268 | } |
| 269 | dispatcher()->PostTask( |
| 270 | Bind(&WiFi::ProgressiveScanTask, weak_ptr_factory_.GetWeakPtr())); |
| 271 | } else { |
| 272 | SLOG(WiFi, 4) << "Doing full scan - progressive scan " |
| 273 | << (progressive_scan_enabled_ ? "ENABLED" : "DISABLED"); |
| 274 | // Needs to send a D-Bus message, but may be called from D-Bus |
| 275 | // signal handler context (via Manager::RequestScan). So defer work |
| 276 | // to event loop. |
| 277 | dispatcher()->PostTask( |
| 278 | Bind(&WiFi::ScanTask, weak_ptr_factory_.GetWeakPtr())); |
| 279 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 280 | } |
| 281 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 282 | void WiFi::BSSAdded(const ::DBus::Path &path, |
| 283 | const map<string, ::DBus::Variant> &properties) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 284 | // Called from a D-Bus signal handler, and may need to send a D-Bus |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 285 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 286 | dispatcher()->PostTask(Bind(&WiFi::BSSAddedTask, |
| 287 | weak_ptr_factory_.GetWeakPtr(), |
| 288 | path, properties)); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void WiFi::BSSRemoved(const ::DBus::Path &path) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 292 | // Called from a D-Bus signal handler, and may need to send a D-Bus |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 293 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 294 | dispatcher()->PostTask(Bind(&WiFi::BSSRemovedTask, |
| 295 | weak_ptr_factory_.GetWeakPtr(), path)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 298 | void WiFi::Certification(const map<string, ::DBus::Variant> &properties) { |
| 299 | dispatcher()->PostTask(Bind(&WiFi::CertificationTask, |
| 300 | weak_ptr_factory_.GetWeakPtr(), properties)); |
| 301 | } |
| 302 | |
Paul Stewart | db0f917 | 2012-11-30 16:48:09 -0800 | [diff] [blame] | 303 | void WiFi::EAPEvent(const string &status, const string ¶meter) { |
| 304 | dispatcher()->PostTask(Bind(&WiFi::EAPEventTask, |
| 305 | weak_ptr_factory_.GetWeakPtr(), |
| 306 | status, |
| 307 | parameter)); |
| 308 | } |
| 309 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 310 | void WiFi::PropertiesChanged(const map<string, ::DBus::Variant> &properties) { |
Darin Petkov | 9cd7ca1 | 2012-07-03 11:06:40 +0200 | [diff] [blame] | 311 | SLOG(WiFi, 2) << __func__; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 312 | // Called from D-Bus signal handler, but may need to send a D-Bus |
| 313 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 314 | dispatcher()->PostTask(Bind(&WiFi::PropertiesChangedTask, |
| 315 | weak_ptr_factory_.GetWeakPtr(), properties)); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 316 | } |
| 317 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 318 | void WiFi::ScanDone() { |
| 319 | LOG(INFO) << __func__; |
| 320 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 321 | // Defer handling of scan result processing, because that processing |
| 322 | // may require the the registration of new D-Bus objects. And such |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 323 | // registration can't be done in the context of a D-Bus signal |
| 324 | // handler. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 325 | dispatcher()->PostTask(Bind(&WiFi::ScanDoneTask, |
| 326 | weak_ptr_factory_.GetWeakPtr())); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 327 | } |
| 328 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 329 | void WiFi::ConnectTo(WiFiService *service, |
mukesh agrawal | 6489632 | 2011-12-01 01:13:10 +0000 | [diff] [blame] | 330 | map<string, DBus::Variant> service_params) { |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 331 | CHECK(service) << "Can't connect to NULL service."; |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 332 | DBus::Path network_path; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 333 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 334 | // TODO(quiche): Handle cases where already connected. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 335 | if (pending_service_ && pending_service_ == service) { |
| 336 | // TODO(quiche): Return an error to the caller. crosbug.com/23832 |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 337 | LOG(INFO) << "WiFi " << link_name() << " ignoring ConnectTo service " |
| 338 | << service->unique_name() |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 339 | << ", which is already pending."; |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | if (pending_service_ && pending_service_ != service) { |
| 344 | DisconnectFrom(pending_service_); |
| 345 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 346 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 347 | Error unused_error; |
| 348 | network_path = FindNetworkRpcidForService(service, &unused_error); |
| 349 | if (network_path.empty()) { |
| 350 | try { |
| 351 | const uint32_t scan_ssid = 1; // "True": Use directed probe. |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 352 | service_params[WPASupplicant::kNetworkPropertyScanSSID].writer(). |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 353 | append_uint32(scan_ssid); |
| 354 | AppendBgscan(service, &service_params); |
| 355 | network_path = supplicant_interface_proxy_->AddNetwork(service_params); |
| 356 | CHECK(!network_path.empty()); // No DBus path should be empty. |
| 357 | rpcid_by_service_[service] = network_path; |
| 358 | } catch (const DBus::Error &e) { // NOLINT |
| 359 | LOG(ERROR) << "exception while adding network: " << e.what(); |
| 360 | return; |
| 361 | } |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 362 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 363 | |
Paul Stewart | a47c3c6 | 2012-12-18 12:14:29 -0800 | [diff] [blame] | 364 | if (service->HasRecentConnectionIssues()) { |
| 365 | SetConnectionDebugging(true); |
| 366 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 367 | supplicant_interface_proxy_->SelectNetwork(network_path); |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 368 | SetPendingService(service); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 369 | CHECK(current_service_.get() != pending_service_.get()); |
| 370 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 371 | // SelectService here (instead of in LinkEvent, like Ethernet), so |
| 372 | // that, if we fail to bring up L2, we can attribute failure correctly. |
| 373 | // |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 374 | // TODO(quiche): When we add code for dealing with connection failures, |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 375 | // reconsider if this is the right place to change the selected service. |
| 376 | // see discussion in crosbug.com/20191. |
| 377 | SelectService(service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 380 | void WiFi::DisconnectFrom(WiFiService *service) { |
| 381 | if (service != current_service_ && service != pending_service_) { |
| 382 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 383 | // generate a D-Bus error here. (crosbug.com/23832) |
| 384 | LOG(WARNING) << "In " << __func__ << "(): " |
| 385 | << " ignoring request to disconnect from service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 386 | << service->unique_name() |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 387 | << " which is neither current nor pending"; |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | if (pending_service_ && service != pending_service_) { |
| 392 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 393 | // generate a D-Bus error here. (crosbug.com/23832) |
| 394 | LOG(WARNING) << "In " << __func__ << "(): " |
| 395 | << " ignoring request to disconnect from service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 396 | << service->unique_name() |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 397 | << " which is not the pending service."; |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (!pending_service_ && service != current_service_) { |
| 402 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 403 | // generate a D-Bus error here. (crosbug.com/23832) |
| 404 | LOG(WARNING) << "In " << __func__ << "(): " |
| 405 | << " ignoring request to disconnect from service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 406 | << service->unique_name() |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 407 | << " which is not the current service."; |
| 408 | return; |
| 409 | } |
| 410 | |
Paul Stewart | ff96a84 | 2012-08-13 15:59:10 -0700 | [diff] [blame] | 411 | if (pending_service_) { |
| 412 | // Since wpa_supplicant has not yet set CurrentBSS, we can't depend |
| 413 | // on this to drive the service state back to idle. Do that here. |
| 414 | pending_service_->SetState(Service::kStateIdle); |
| 415 | } |
| 416 | |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 417 | SetPendingService(NULL); |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 418 | StopReconnectTimer(); |
Paul Stewart | 549d44c | 2012-07-03 12:40:25 -0700 | [diff] [blame] | 419 | |
| 420 | if (!supplicant_present_) { |
Christopher Wiley | c618448 | 2012-10-24 15:31:56 -0700 | [diff] [blame] | 421 | LOG(ERROR) << "In " << __func__ << "(): " |
| 422 | << "wpa_supplicant is not present; silently resetting " |
| 423 | << "current_service_."; |
| 424 | if (current_service_ == selected_service()) { |
| 425 | DropConnection(); |
| 426 | } |
Paul Stewart | 549d44c | 2012-07-03 12:40:25 -0700 | [diff] [blame] | 427 | current_service_ = NULL; |
| 428 | return; |
| 429 | } |
| 430 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 431 | try { |
| 432 | supplicant_interface_proxy_->Disconnect(); |
| 433 | // We'll call RemoveNetwork and reset |current_service_| after |
| 434 | // supplicant notifies us that the CurrentBSS has changed. |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 435 | } catch (const DBus::Error &e) { // NOLINT |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 436 | // Can't depend on getting a notification of CurrentBSS change. |
Christopher Wiley | c618448 | 2012-10-24 15:31:56 -0700 | [diff] [blame] | 437 | // So effect changes immediately. For instance, this can happen when |
| 438 | // a disconnect is triggered by a BSS going away. |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 439 | Error unused_error; |
| 440 | RemoveNetworkForService(service, &unused_error); |
Christopher Wiley | c618448 | 2012-10-24 15:31:56 -0700 | [diff] [blame] | 441 | if (service == selected_service()) { |
| 442 | DropConnection(); |
| 443 | } |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 444 | current_service_ = NULL; |
| 445 | } |
| 446 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 447 | CHECK(current_service_ == NULL || |
| 448 | current_service_.get() != pending_service_.get()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 451 | bool WiFi::DisableNetwork(const ::DBus::Path &network) { |
| 452 | scoped_ptr<SupplicantNetworkProxyInterface> supplicant_network_proxy( |
| 453 | proxy_factory_->CreateSupplicantNetworkProxy( |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 454 | network, WPASupplicant::kDBusAddr)); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 455 | try { |
| 456 | supplicant_network_proxy->SetEnabled(false); |
| 457 | } catch (const DBus::Error &e) { // NOLINT |
| 458 | LOG(ERROR) << "DisableNetwork for " << network << " failed."; |
| 459 | return false; |
| 460 | } |
| 461 | return true; |
| 462 | } |
| 463 | |
Paul Stewart | 71f6ecd | 2012-09-13 14:52:18 -0700 | [diff] [blame] | 464 | bool WiFi::RemoveNetwork(const ::DBus::Path &network) { |
| 465 | try { |
| 466 | supplicant_interface_proxy_->RemoveNetwork(network); |
| 467 | } catch (const DBus::Error &e) { // NOLINT |
Ben Chan | 381fdcc | 2012-10-14 21:10:36 -0700 | [diff] [blame] | 468 | // RemoveNetwork can fail with three different errors. |
| 469 | // |
| 470 | // If RemoveNetwork fails with a NetworkUnknown error, supplicant has |
| 471 | // already removed the network object, so return true as if |
| 472 | // RemoveNetwork removes the network object successfully. |
| 473 | // |
| 474 | // As shill always passes a valid network object path, RemoveNetwork |
| 475 | // should not fail with an InvalidArgs error. Return false in such case |
| 476 | // as something weird may have happened. Similarly, return false in case |
| 477 | // of an UnknownError. |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 478 | if (strcmp(e.name(), WPASupplicant::kErrorNetworkUnknown) != 0) { |
Ben Chan | 381fdcc | 2012-10-14 21:10:36 -0700 | [diff] [blame] | 479 | return false; |
| 480 | } |
Paul Stewart | 71f6ecd | 2012-09-13 14:52:18 -0700 | [diff] [blame] | 481 | } |
| 482 | return true; |
| 483 | } |
| 484 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 485 | bool WiFi::IsIdle() const { |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 486 | return !current_service_ && !pending_service_; |
| 487 | } |
| 488 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 489 | void WiFi::ClearCachedCredentials(const WiFiService *service) { |
| 490 | Error unused_error; |
| 491 | RemoveNetworkForService(service, &unused_error); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 494 | void WiFi::NotifyEndpointChanged(const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 495 | provider_->OnEndpointUpdated(endpoint); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 498 | void WiFi::AppendBgscan(WiFiService *service, |
| 499 | map<string, DBus::Variant> *service_params) const { |
| 500 | int scan_interval = kBackgroundScanIntervalSeconds; |
| 501 | string method = bgscan_method_; |
| 502 | if (method.empty()) { |
| 503 | // If multiple APs are detected for this SSID, configure the default method. |
| 504 | // Otherwise, disable background scanning completely. |
| 505 | if (service->GetEndpointCount() > 1) { |
| 506 | method = kDefaultBgscanMethod; |
| 507 | } else { |
| 508 | LOG(INFO) << "Background scan disabled -- single Endpoint for Service."; |
| 509 | return; |
| 510 | } |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 511 | } else if (method.compare(WPASupplicant::kNetworkBgscanMethodNone) == 0) { |
Christopher Wiley | a998df2 | 2012-07-11 15:14:55 -0700 | [diff] [blame] | 512 | LOG(INFO) << "Background scan disabled -- chose None method."; |
| 513 | return; |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 514 | } else { |
| 515 | // If the background scan method was explicitly specified, honor the |
| 516 | // configured background scan interval. |
| 517 | scan_interval = scan_interval_seconds_; |
| 518 | } |
| 519 | DCHECK(!method.empty()); |
| 520 | string config_string = StringPrintf("%s:%d:%d:%d", |
| 521 | method.c_str(), |
| 522 | bgscan_short_interval_seconds_, |
| 523 | bgscan_signal_threshold_dbm_, |
| 524 | scan_interval); |
| 525 | LOG(INFO) << "Background scan: " << config_string; |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 526 | (*service_params)[WPASupplicant::kNetworkPropertyBgscan].writer() |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 527 | .append_string(config_string.c_str()); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 530 | string WiFi::GetBgscanMethod(const int &/*argument*/, Error */* error */) { |
| 531 | return bgscan_method_.empty() ? kDefaultBgscanMethod : bgscan_method_; |
| 532 | } |
| 533 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 534 | bool WiFi::SetBgscanMethod( |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 535 | const int &/*argument*/, const string &method, Error *error) { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 536 | if (method != WPASupplicant::kNetworkBgscanMethodSimple && |
| 537 | method != WPASupplicant::kNetworkBgscanMethodLearn && |
| 538 | method != WPASupplicant::kNetworkBgscanMethodNone) { |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 539 | const string error_message = |
| 540 | StringPrintf("Unrecognized bgscan method %s", method.c_str()); |
| 541 | LOG(WARNING) << error_message; |
| 542 | error->Populate(Error::kInvalidArguments, error_message); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 543 | return false; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 544 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 545 | if (bgscan_method_ == method) { |
| 546 | return false; |
| 547 | } |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 548 | bgscan_method_ = method; |
| 549 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 550 | // |current_service_|, because supplicant does not allow for |
| 551 | // reconfiguration without disconnect and reconnect. |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 552 | return true; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 553 | } |
| 554 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 555 | bool WiFi::SetBgscanShortInterval(const uint16 &seconds, Error */*error*/) { |
| 556 | if (bgscan_short_interval_seconds_ == seconds) { |
| 557 | return false; |
| 558 | } |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 559 | bgscan_short_interval_seconds_ = seconds; |
| 560 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 561 | // |current_service_|, because supplicant does not allow for |
| 562 | // reconfiguration without disconnect and reconnect. |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 563 | return true; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 564 | } |
| 565 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 566 | bool WiFi::SetBgscanSignalThreshold(const int32 &dbm, Error */*error*/) { |
| 567 | if (bgscan_signal_threshold_dbm_ == dbm) { |
| 568 | return false; |
| 569 | } |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 570 | bgscan_signal_threshold_dbm_ = dbm; |
| 571 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 572 | // |current_service_|, because supplicant does not allow for |
| 573 | // reconfiguration without disconnect and reconnect. |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 574 | return true; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 575 | } |
| 576 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 577 | bool WiFi::SetScanInterval(const uint16 &seconds, Error */*error*/) { |
| 578 | if (scan_interval_seconds_ == seconds) { |
| 579 | return false; |
| 580 | } |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 581 | scan_interval_seconds_ = seconds; |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 582 | if (running()) { |
| 583 | StartScanTimer(); |
| 584 | } |
| 585 | // The scan interval affects both foreground scans (handled by |
| 586 | // |scan_timer_callback_|), and background scans (handled by |
| 587 | // supplicant). However, we do not update |pending_service_| or |
| 588 | // |current_service_|, because supplicant does not allow for |
| 589 | // reconfiguration without disconnect and reconnect. |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 590 | return true; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 593 | void WiFi::ClearBgscanMethod(const int &/*argument*/, Error */*error*/) { |
| 594 | bgscan_method_.clear(); |
| 595 | } |
| 596 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 597 | void WiFi::CurrentBSSChanged(const ::DBus::Path &new_bss) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 598 | SLOG(WiFi, 3) << "WiFi " << link_name() << " CurrentBSS " |
| 599 | << supplicant_bss_ << " -> " << new_bss; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 600 | supplicant_bss_ = new_bss; |
Christopher Wiley | 8f81e2a | 2012-10-17 16:51:32 -0700 | [diff] [blame] | 601 | has_already_completed_ = false; |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 602 | |
| 603 | // Any change in CurrentBSS means supplicant is actively changing our |
| 604 | // connectivity. We no longer need to track any previously pending |
| 605 | // reconnect. |
| 606 | StopReconnectTimer(); |
| 607 | |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 608 | if (new_bss == WPASupplicant::kCurrentBSSNull) { |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 609 | HandleDisconnect(); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 610 | if (!provider_->GetHiddenSSIDList().empty()) { |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 611 | // Before disconnecting, wpa_supplicant probably scanned for |
| 612 | // APs. So, in the normal case, we defer to the timer for the next scan. |
| 613 | // |
| 614 | // However, in the case of hidden SSIDs, supplicant knows about |
| 615 | // at most one of them. (That would be the hidden SSID we were |
| 616 | // connected to, if applicable.) |
| 617 | // |
| 618 | // So, in this case, we initiate an immediate scan. This scan |
| 619 | // will include the hidden SSIDs we know about (up to the limit of |
| 620 | // kScanMAxSSIDsPerScan). |
| 621 | // |
| 622 | // We may want to reconsider this immediate scan, if/when shill |
| 623 | // takes greater responsibility for scanning (vs. letting |
| 624 | // supplicant handle most of it). |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 625 | Scan(kProgressiveScan, NULL); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 626 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 627 | } else { |
| 628 | HandleRoam(new_bss); |
| 629 | } |
| 630 | |
Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -0700 | [diff] [blame] | 631 | // Reset the EAP handler only after calling HandleDisconnect() above |
| 632 | // so our EAP state could be used to detect a failed authentication. |
| 633 | eap_state_handler_->Reset(); |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 634 | |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 635 | // If we are selecting a new service, or if we're clearing selection |
| 636 | // of a something other than the pending service, call SelectService. |
| 637 | // Otherwise skip SelectService, since this will cause the pending |
| 638 | // service to be marked as Idle. |
| 639 | if (current_service_ || selected_service() != pending_service_) { |
| 640 | SelectService(current_service_); |
| 641 | } |
| 642 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 643 | // Invariant check: a Service can either be current, or pending, but |
| 644 | // not both. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 645 | CHECK(current_service_.get() != pending_service_.get() || |
| 646 | current_service_.get() == NULL); |
Paul Stewart | a47c3c6 | 2012-12-18 12:14:29 -0800 | [diff] [blame] | 647 | |
| 648 | // If we are no longer debugging a problematic WiFi connection, return |
| 649 | // to the debugging level indicated by the WiFi debugging scope. |
| 650 | if ((!current_service_ || !current_service_->HasRecentConnectionIssues()) && |
| 651 | (!pending_service_ || !pending_service_->HasRecentConnectionIssues())) { |
| 652 | SetConnectionDebugging(false); |
| 653 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void WiFi::HandleDisconnect() { |
| 657 | // Identify the affected service. We expect to get a disconnect |
| 658 | // event when we fall off a Service that we were connected |
| 659 | // to. However, we also allow for the case where we get a disconnect |
| 660 | // event while attempting to connect from a disconnected state. |
| 661 | WiFiService *affected_service = |
| 662 | current_service_.get() ? current_service_.get() : pending_service_.get(); |
| 663 | |
| 664 | current_service_ = NULL; |
| 665 | if (!affected_service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 666 | SLOG(WiFi, 2) << "WiFi " << link_name() |
| 667 | << " disconnected while not connected or connecting"; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 668 | return; |
Christopher Wiley | c618448 | 2012-10-24 15:31:56 -0700 | [diff] [blame] | 669 | } |
| 670 | if (affected_service == selected_service()) { |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 671 | // If our selected service has disconnected, destroy IP configuration state. |
Christopher Wiley | c618448 | 2012-10-24 15:31:56 -0700 | [diff] [blame] | 672 | DropConnection(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 675 | Error error; |
| 676 | if (!DisableNetworkForService(affected_service, &error)) { |
| 677 | if (error.type() == Error::kNotFound) { |
| 678 | SLOG(WiFi, 2) << "WiFi " << link_name() << " disconnected from " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 679 | << " (or failed to connect to) service " |
| 680 | << affected_service->unique_name() << ", " |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 681 | << "but could not find supplicant network to disable."; |
| 682 | } else { |
| 683 | LOG(FATAL) << "DisableNetwork failed."; |
Paul Stewart | 71f6ecd | 2012-09-13 14:52:18 -0700 | [diff] [blame] | 684 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 687 | SLOG(WiFi, 2) << "WiFi " << link_name() << " disconnected from " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 688 | << " (or failed to connect to) service " |
| 689 | << affected_service->unique_name(); |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 690 | Service::ConnectFailure failure; |
| 691 | if (SuspectCredentials(*affected_service, &failure)) { |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 692 | // If we suspect bad credentials, set failure, to trigger an error |
mukesh agrawal | 56e3220 | 2012-07-26 16:32:11 -0700 | [diff] [blame] | 693 | // mole in Chrome. |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 694 | affected_service->SetFailure(failure); |
| 695 | LOG(ERROR) << "Connection failure is due to suspect credentials: returning " |
| 696 | << Service::ConnectFailureToString(failure); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 697 | } else { |
| 698 | affected_service->SetFailureSilent(Service::kFailureUnknown); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 699 | } |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 700 | affected_service->NotifyCurrentEndpoint(NULL); |
Thieu Le | 67370f6 | 2012-02-14 23:01:42 +0000 | [diff] [blame] | 701 | metrics()->NotifyServiceDisconnect(affected_service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 702 | |
| 703 | if (affected_service == pending_service_.get()) { |
| 704 | // The attempt to connect to |pending_service_| failed. Clear |
| 705 | // |pending_service_|, to indicate we're no longer in the middle |
| 706 | // of a connect request. |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 707 | SetPendingService(NULL); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 708 | } else if (pending_service_.get()) { |
| 709 | // We've attributed the disconnection to what was the |
| 710 | // |current_service_|, rather than the |pending_service_|. |
| 711 | // |
| 712 | // If we're wrong about that (i.e. supplicant reported this |
| 713 | // CurrentBSS change after attempting to connect to |
| 714 | // |pending_service_|), we're depending on supplicant to retry |
| 715 | // connecting to |pending_service_|, and delivering another |
| 716 | // CurrentBSS change signal in the future. |
| 717 | // |
| 718 | // Log this fact, to help us debug (in case our assumptions are |
| 719 | // wrong). |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 720 | SLOG(WiFi, 2) << "WiFi " << link_name() << " pending connection to service " |
| 721 | << pending_service_->unique_name() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 722 | << " after disconnect"; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 723 | } |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 724 | |
| 725 | // If we disconnect, initially scan at a faster frequency, to make sure |
| 726 | // we've found all available APs. |
| 727 | RestartFastScanAttempts(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // We use the term "Roam" loosely. In particular, we include the case |
| 731 | // where we "Roam" to a BSS from the disconnected state. |
| 732 | void WiFi::HandleRoam(const ::DBus::Path &new_bss) { |
| 733 | EndpointMap::iterator endpoint_it = endpoint_by_rpcid_.find(new_bss); |
| 734 | if (endpoint_it == endpoint_by_rpcid_.end()) { |
| 735 | LOG(WARNING) << "WiFi " << link_name() << " connected to unknown BSS " |
| 736 | << new_bss; |
| 737 | return; |
| 738 | } |
| 739 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 740 | const WiFiEndpointConstRefPtr endpoint(endpoint_it->second); |
| 741 | WiFiServiceRefPtr service = provider_->FindServiceForEndpoint(endpoint); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 742 | if (!service.get()) { |
| 743 | LOG(WARNING) << "WiFi " << link_name() |
| 744 | << " could not find Service for Endpoint " |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 745 | << endpoint->bssid_string() |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 746 | << " (service will be unchanged)"; |
| 747 | return; |
| 748 | } |
| 749 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 750 | SLOG(WiFi, 2) << "WiFi " << link_name() |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 751 | << " roamed to Endpoint " << endpoint->bssid_string() |
| 752 | << " " << LogSSID(endpoint->ssid_string()); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 753 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 754 | service->NotifyCurrentEndpoint(endpoint); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 755 | provider_->IncrementConnectCount(endpoint->frequency()); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 756 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 757 | if (pending_service_.get() && |
| 758 | service.get() != pending_service_.get()) { |
| 759 | // The Service we've roamed on to is not the one we asked for. |
| 760 | // We assume that this is transient, and that wpa_supplicant |
| 761 | // is trying / will try to connect to |pending_service_|. |
| 762 | // |
| 763 | // If it succeeds, we'll end up back here, but with |service| |
| 764 | // pointing at the same service as |pending_service_|. |
| 765 | // |
| 766 | // If it fails, we'll process things in HandleDisconnect. |
| 767 | // |
| 768 | // So we leave |pending_service_| untouched. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 769 | SLOG(WiFi, 2) << "WiFi " << link_name() |
| 770 | << " new current Endpoint " |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 771 | << endpoint->bssid_string() |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 772 | << " is not part of pending service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 773 | << pending_service_->unique_name(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 774 | |
| 775 | // Sanity check: if we didn't roam onto |pending_service_|, we |
| 776 | // should still be on |current_service_|. |
| 777 | if (service.get() != current_service_.get()) { |
| 778 | LOG(WARNING) << "WiFi " << link_name() |
| 779 | << " new current Endpoint " |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 780 | << endpoint->bssid_string() |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 781 | << " is neither part of pending service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 782 | << pending_service_->unique_name() |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 783 | << " nor part of current service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 784 | << (current_service_ ? |
| 785 | current_service_->unique_name() : |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 786 | "(NULL)"); |
| 787 | // Although we didn't expect to get here, we should keep |
| 788 | // |current_service_| in sync with what supplicant has done. |
| 789 | current_service_ = service; |
| 790 | } |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | if (pending_service_.get()) { |
| 795 | // We assume service.get() == pending_service_.get() here, because |
| 796 | // of the return in the previous if clause. |
| 797 | // |
| 798 | // Boring case: we've connected to the service we asked |
| 799 | // for. Simply update |current_service_| and |pending_service_|. |
| 800 | current_service_ = service; |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 801 | SetPendingService(NULL); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 802 | return; |
| 803 | } |
| 804 | |
| 805 | // |pending_service_| was NULL, so we weren't attempting to connect |
| 806 | // to a new Service. Sanity check that we're still on |
| 807 | // |current_service_|. |
| 808 | if (service.get() != current_service_.get()) { |
| 809 | LOG(WARNING) |
| 810 | << "WiFi " << link_name() |
| 811 | << " new current Endpoint " |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 812 | << endpoint->bssid_string() |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 813 | << (current_service_.get() ? |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 814 | StringPrintf(" is not part of current service %s", |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 815 | current_service_->unique_name().c_str()) : |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 816 | " with no current service"); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 817 | // We didn't expect to be here, but let's cope as well as we |
| 818 | // can. Update |current_service_| to keep it in sync with |
| 819 | // supplicant. |
| 820 | current_service_ = service; |
Paul Stewart | abbe279 | 2012-07-15 07:50:35 -0700 | [diff] [blame] | 821 | |
| 822 | // If this service isn't already marked as actively connecting (likely, |
| 823 | // since this service is a bit of a surprise) set the service as |
| 824 | // associating. |
| 825 | if (!current_service_->IsConnecting()) { |
| 826 | current_service_->SetState(Service::kStateAssociating); |
| 827 | } |
| 828 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
| 832 | // At this point, we know that |pending_service_| was NULL, and that |
| 833 | // we're still on |current_service_|. This is the most boring case |
| 834 | // of all, because there's no state to update here. |
| 835 | return; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 838 | string WiFi::FindNetworkRpcidForService( |
| 839 | const WiFiService *service, Error *error) { |
| 840 | ReverseServiceMap::const_iterator rpcid_it = |
| 841 | rpcid_by_service_.find(service); |
| 842 | if (rpcid_it == rpcid_by_service_.end()) { |
| 843 | const string error_message = |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 844 | StringPrintf( |
| 845 | "WiFi %s cannot find supplicant network rpcid for service %s", |
| 846 | link_name().c_str(), service->unique_name().c_str()); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 847 | // There are contexts where this is not an error, such as when a service |
| 848 | // is clearing whatever cached credentials may not exist. |
| 849 | SLOG(WiFi, 2) << error_message; |
| 850 | if (error) { |
| 851 | error->Populate(Error::kNotFound, error_message); |
| 852 | } |
| 853 | return ""; |
| 854 | } |
| 855 | |
| 856 | return rpcid_it->second; |
| 857 | } |
| 858 | |
| 859 | bool WiFi::DisableNetworkForService(const WiFiService *service, Error *error) { |
| 860 | string rpcid = FindNetworkRpcidForService(service, error); |
| 861 | if (rpcid.empty()) { |
| 862 | // Error is already populated. |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | if (!DisableNetwork(rpcid)) { |
| 867 | const string error_message = |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 868 | StringPrintf("WiFi %s cannot disable network for service %s: " |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 869 | "DBus operation failed for rpcid %s.", |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 870 | link_name().c_str(), service->unique_name().c_str(), |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 871 | rpcid.c_str()); |
| 872 | Error::PopulateAndLog(error, Error::kOperationFailed, error_message); |
| 873 | |
| 874 | // Make sure that such errored networks are removed, so problems do not |
| 875 | // propogate to future connection attempts. |
| 876 | RemoveNetwork(rpcid); |
| 877 | rpcid_by_service_.erase(service); |
| 878 | |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | return true; |
| 883 | } |
| 884 | |
| 885 | bool WiFi::RemoveNetworkForService(const WiFiService *service, Error *error) { |
| 886 | string rpcid = FindNetworkRpcidForService(service, error); |
| 887 | if (rpcid.empty()) { |
| 888 | // Error is already populated. |
| 889 | return false; |
| 890 | } |
| 891 | |
| 892 | // Erase the rpcid from our tables regardless of failure below, since even |
| 893 | // if in failure, we never want to use this network again. |
| 894 | rpcid_by_service_.erase(service); |
| 895 | |
| 896 | // TODO(quiche): Reconsider giving up immediately. Maybe give |
| 897 | // wpa_supplicant some time to retry, first. |
| 898 | if (!RemoveNetwork(rpcid)) { |
| 899 | const string error_message = |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 900 | StringPrintf("WiFi %s cannot remove network for service %s: " |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 901 | "DBus operation failed for rpcid %s.", |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 902 | link_name().c_str(), service->unique_name().c_str(), |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 903 | rpcid.c_str()); |
| 904 | Error::PopulateAndLog(error, Error::kOperationFailed, error_message); |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | return true; |
| 909 | } |
| 910 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 911 | void WiFi::BSSAddedTask( |
| 912 | const ::DBus::Path &path, |
| 913 | const map<string, ::DBus::Variant> &properties) { |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 914 | // Note: we assume that BSSIDs are unique across endpoints. This |
| 915 | // means that if an AP reuses the same BSSID for multiple SSIDs, we |
| 916 | // lose. |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 917 | WiFiEndpointRefPtr endpoint( |
| 918 | new WiFiEndpoint(proxy_factory_, this, path, properties)); |
Wade Guthrie | 592ecd5 | 2012-11-12 13:12:30 -0800 | [diff] [blame] | 919 | SLOG(WiFi, 1) << "Found endpoint. " |
| 920 | << "RPC path: " << path << ", " |
Darin Petkov | 50cb78a | 2013-02-06 16:17:49 +0100 | [diff] [blame] | 921 | << LogSSID(endpoint->ssid_string()) << ", " |
Wade Guthrie | 592ecd5 | 2012-11-12 13:12:30 -0800 | [diff] [blame] | 922 | << "bssid: " << endpoint->bssid_string() << ", " |
| 923 | << "signal: " << endpoint->signal_strength() << ", " |
| 924 | << "security: " << endpoint->security_mode() << ", " |
| 925 | << "frequency: " << endpoint->frequency(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 926 | |
| 927 | if (endpoint->ssid_string().empty()) { |
| 928 | // Don't bother trying to find or create a Service for an Endpoint |
| 929 | // without an SSID. We wouldn't be able to connect to it anyway. |
| 930 | return; |
| 931 | } |
| 932 | |
mukesh agrawal | b385761 | 2012-01-18 16:23:29 -0800 | [diff] [blame] | 933 | if (endpoint->ssid()[0] == 0) { |
| 934 | // Assume that an SSID starting with NULL is bogus/misconfigured, |
| 935 | // and filter it out. |
| 936 | return; |
| 937 | } |
| 938 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 939 | provider_->OnEndpointAdded(endpoint); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 940 | |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 941 | // Do this last, to maintain the invariant that any Endpoint we |
| 942 | // know about has a corresponding Service. |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 943 | // |
| 944 | // TODO(quiche): Write test to verify correct behavior in the case |
| 945 | // where we get multiple BSSAdded events for a single endpoint. |
| 946 | // (Old Endpoint's refcount should fall to zero, and old Endpoint |
| 947 | // should be destroyed.) |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 948 | endpoint_by_rpcid_[path] = endpoint; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 949 | endpoint->Start(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | void WiFi::BSSRemovedTask(const ::DBus::Path &path) { |
| 953 | EndpointMap::iterator i = endpoint_by_rpcid_.find(path); |
| 954 | if (i == endpoint_by_rpcid_.end()) { |
| 955 | LOG(WARNING) << "WiFi " << link_name() |
| 956 | << " could not find BSS " << path |
| 957 | << " to remove."; |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | WiFiEndpointRefPtr endpoint = i->second; |
| 962 | CHECK(endpoint); |
| 963 | endpoint_by_rpcid_.erase(i); |
| 964 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 965 | WiFiServiceRefPtr service = provider_->OnEndpointRemoved(endpoint); |
| 966 | if (!service) { |
| 967 | return; |
| 968 | } |
| 969 | Error unused_error; |
| 970 | RemoveNetworkForService(service, &unused_error); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 971 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 972 | bool disconnect_service = !service->HasEndpoints() && |
| 973 | (service->IsConnecting() || service->IsConnected()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 974 | |
| 975 | if (disconnect_service) { |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 976 | LOG(INFO) << "Disconnecting from service " << service->unique_name() |
| 977 | << ": BSSRemoved"; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 978 | DisconnectFrom(service); |
| 979 | } |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 982 | void WiFi::CertificationTask( |
| 983 | const map<string, ::DBus::Variant> &properties) { |
| 984 | if (!current_service_) { |
| 985 | LOG(ERROR) << "WiFi " << link_name() << " " << __func__ |
| 986 | << " with no current service."; |
| 987 | return; |
| 988 | } |
| 989 | |
Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -0700 | [diff] [blame] | 990 | string subject; |
| 991 | uint32 depth; |
| 992 | if (WPASupplicant::ExtractRemoteCertification(properties, &subject, &depth)) { |
| 993 | current_service_->AddEAPCertification(subject, depth); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 994 | } |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Paul Stewart | db0f917 | 2012-11-30 16:48:09 -0800 | [diff] [blame] | 997 | void WiFi::EAPEventTask(const string &status, const string ¶meter) { |
Paul Stewart | db0f917 | 2012-11-30 16:48:09 -0800 | [diff] [blame] | 998 | if (!current_service_) { |
| 999 | LOG(ERROR) << "WiFi " << link_name() << " " << __func__ |
| 1000 | << " with no current service."; |
| 1001 | return; |
| 1002 | } |
Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -0700 | [diff] [blame] | 1003 | Service::ConnectFailure failure = Service::kFailureUnknown; |
| 1004 | eap_state_handler_->ParseStatus(status, parameter, &failure); |
Paul Stewart | db0f917 | 2012-11-30 16:48:09 -0800 | [diff] [blame] | 1005 | if (failure != Service::kFailureUnknown) { |
Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -0700 | [diff] [blame] | 1006 | // Avoid a reporting failure twice by resetting EAP state handler early. |
| 1007 | eap_state_handler_->Reset(); |
mukesh agrawal | d4dc083 | 2013-03-25 14:38:26 -0700 | [diff] [blame] | 1008 | Error unused_error; |
mukesh agrawal | d4dc083 | 2013-03-25 14:38:26 -0700 | [diff] [blame] | 1009 | current_service_->DisconnectWithFailure(failure, &unused_error); |
Paul Stewart | db0f917 | 2012-11-30 16:48:09 -0800 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1013 | void WiFi::PropertiesChangedTask( |
| 1014 | const map<string, ::DBus::Variant> &properties) { |
| 1015 | // TODO(quiche): Handle changes in other properties (e.g. signal |
| 1016 | // strength). |
| 1017 | |
| 1018 | // Note that order matters here. In particular, we want to process |
| 1019 | // changes in the current BSS before changes in state. This is so |
| 1020 | // that we update the state of the correct Endpoint/Service. |
| 1021 | |
| 1022 | map<string, ::DBus::Variant>::const_iterator properties_it = |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1023 | properties.find(WPASupplicant::kInterfacePropertyCurrentBSS); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1024 | if (properties_it != properties.end()) { |
| 1025 | CurrentBSSChanged(properties_it->second.reader().get_path()); |
| 1026 | } |
| 1027 | |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1028 | properties_it = properties.find(WPASupplicant::kInterfacePropertyState); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1029 | if (properties_it != properties.end()) { |
| 1030 | StateChanged(properties_it->second.reader().get_string()); |
| 1031 | } |
| 1032 | } |
| 1033 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 1034 | void WiFi::ScanDoneTask() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1035 | SLOG(WiFi, 2) << __func__ << " need_bss_flush_ " << need_bss_flush_; |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 1036 | |
| 1037 | if (scan_session_) { |
| 1038 | // Post |ProgressiveScanTask| so it runs after any |BSSAddedTask|s that have |
| 1039 | // been posted. This allows connections on new BSSes to be started before |
| 1040 | // we decide whether to abort the progressive scan or continue scanning. |
| 1041 | dispatcher()->PostTask( |
| 1042 | Bind(&WiFi::ProgressiveScanTask, weak_ptr_factory_.GetWeakPtr())); |
| 1043 | } else { |
| 1044 | metrics()->NotifyDeviceScanFinished(interface_index()); |
| 1045 | } |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 1046 | if (need_bss_flush_) { |
| 1047 | CHECK(supplicant_interface_proxy_ != NULL); |
| 1048 | // Compute |max_age| relative to |resumed_at_|, to account for the |
| 1049 | // time taken to scan. |
| 1050 | struct timeval now; |
| 1051 | uint32_t max_age; |
| 1052 | time_->GetTimeMonotonic(&now); |
| 1053 | max_age = kMaxBSSResumeAgeSeconds + (now.tv_sec - resumed_at_.tv_sec); |
| 1054 | supplicant_interface_proxy_->FlushBSS(max_age); |
| 1055 | need_bss_flush_ = false; |
| 1056 | } |
Paul Stewart | d2db2b1 | 2013-01-17 13:11:07 -0800 | [diff] [blame] | 1057 | SetScanPending(false); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 1058 | StartScanTimer(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1061 | void WiFi::ScanTask() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1062 | SLOG(WiFi, 2) << "WiFi " << link_name() << " scan requested."; |
Paul Stewart | fae4dae | 2012-09-13 07:43:32 -0700 | [diff] [blame] | 1063 | if (!enabled()) { |
| 1064 | SLOG(WiFi, 2) << "Ignoring scan request while device is not enabled."; |
| 1065 | return; |
| 1066 | } |
| 1067 | if (!supplicant_present_ || !supplicant_interface_proxy_.get()) { |
| 1068 | SLOG(WiFi, 2) << "Ignoring scan request while supplicant is not present."; |
| 1069 | return; |
| 1070 | } |
Christopher Wiley | c68c867 | 2012-11-20 16:52:21 -0800 | [diff] [blame] | 1071 | if ((pending_service_.get() && pending_service_->IsConnecting()) || |
| 1072 | (current_service_.get() && current_service_->IsConnecting())) { |
| 1073 | SLOG(WiFi, 2) << "Ignoring scan request while connecting to an AP."; |
| 1074 | return; |
| 1075 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1076 | map<string, DBus::Variant> scan_args; |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1077 | scan_args[WPASupplicant::kPropertyScanType].writer(). |
| 1078 | append_string(WPASupplicant::kScanTypeActive); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1079 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1080 | ByteArrays hidden_ssids = provider_->GetHiddenSSIDList(); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1081 | if (!hidden_ssids.empty()) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1082 | // TODO(pstew): Devise a better method for time-sharing with SSIDs that do |
| 1083 | // not fit in. |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1084 | if (hidden_ssids.size() >= WPASupplicant::kScanMaxSSIDsPerScan) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1085 | hidden_ssids.erase( |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1086 | hidden_ssids.begin() + WPASupplicant::kScanMaxSSIDsPerScan - 1, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1087 | hidden_ssids.end()); |
| 1088 | } |
| 1089 | // Add Broadcast SSID, signified by an empty ByteArray. If we specify |
| 1090 | // SSIDs to wpa_supplicant, we need to explicitly specify the default |
| 1091 | // behavior of doing a broadcast probe. |
| 1092 | hidden_ssids.push_back(ByteArray()); |
| 1093 | |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1094 | scan_args[WPASupplicant::kPropertyScanSSIDs] = |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1095 | DBusAdaptor::ByteArraysToVariant(hidden_ssids); |
| 1096 | } |
| 1097 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1098 | try { |
| 1099 | supplicant_interface_proxy_->Scan(scan_args); |
Paul Stewart | d2db2b1 | 2013-01-17 13:11:07 -0800 | [diff] [blame] | 1100 | SetScanPending(true); |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 1101 | } catch (const DBus::Error &e) { // NOLINT |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1102 | // A scan may fail if, for example, the wpa_supplicant vanishing |
| 1103 | // notification is posted after this task has already started running. |
| 1104 | LOG(WARNING) << "Scan failed: " << e.what(); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1105 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 1108 | void WiFi::ProgressiveScanTask() { |
| 1109 | SLOG(WiFi, 2) << __func__ << " - scan requested for " << link_name(); |
| 1110 | if (!enabled()) { |
| 1111 | LOG(INFO) << "Ignoring scan request while device is not enabled."; |
| 1112 | metrics()->NotifyDeviceScanFinished(interface_index()); |
| 1113 | return; |
| 1114 | } |
| 1115 | if (!scan_session_) { |
| 1116 | SLOG(WiFi, 2) << "No scan session -- returning"; |
| 1117 | metrics()->NotifyDeviceScanFinished(interface_index()); |
| 1118 | return; |
| 1119 | } |
| 1120 | if (!IsIdle()) { |
| 1121 | SLOG(WiFi, 2) << "Ignoring scan request while connecting to an AP."; |
| 1122 | scan_session_.reset(); |
| 1123 | metrics()->NotifyDeviceScanFinished(interface_index()); |
| 1124 | return; |
| 1125 | } |
| 1126 | if (scan_session_->HasMoreFrequencies()) { |
| 1127 | SLOG(WiFi, 2) << "Initiating a scan -- returning"; |
| 1128 | SetScanPending(true); |
| 1129 | // After us initiating a scan, supplicant will gather the scan results and |
| 1130 | // send us zero or more |BSSAdded| events followed by a |ScanDone|. |
| 1131 | scan_session_->InitiateScan(); |
| 1132 | return; |
| 1133 | } |
| 1134 | LOG(ERROR) << "A complete progressive scan turned-up nothing -- " |
| 1135 | << "do a regular scan"; |
| 1136 | scan_session_.reset(); |
| 1137 | Scan(kFullScan, NULL); |
| 1138 | } |
| 1139 | |
| 1140 | void WiFi::OnFailedProgressiveScan() { |
| 1141 | LOG(ERROR) << "Couldn't issue a scan on " << link_name() |
| 1142 | << " -- doing a regular scan"; |
| 1143 | scan_session_.reset(); |
| 1144 | Scan(kFullScan, NULL); |
| 1145 | } |
| 1146 | |
Paul Stewart | d2db2b1 | 2013-01-17 13:11:07 -0800 | [diff] [blame] | 1147 | void WiFi::SetScanPending(bool pending) { |
| 1148 | if (scan_pending_ != pending) { |
| 1149 | scan_pending_ = pending; |
| 1150 | adaptor()->EmitBoolChanged(flimflam::kScanningProperty, pending); |
| 1151 | } |
| 1152 | } |
| 1153 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 1154 | string WiFi::GetServiceLeaseName(const WiFiService &service) { |
| 1155 | return service.GetStorageIdentifier(); |
| 1156 | } |
| 1157 | |
| 1158 | void WiFi::DestroyServiceLease(const WiFiService &service) { |
| 1159 | DestroyIPConfigLease(GetServiceLeaseName(service)); |
| 1160 | } |
| 1161 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1162 | void WiFi::StateChanged(const string &new_state) { |
| 1163 | const string old_state = supplicant_state_; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1164 | supplicant_state_ = new_state; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1165 | LOG(INFO) << "WiFi " << link_name() << " " << __func__ << " " |
| 1166 | << old_state << " -> " << new_state; |
| 1167 | |
| 1168 | WiFiService *affected_service; |
| 1169 | // Identify the service to which the state change applies. If |
| 1170 | // |pending_service_| is non-NULL, then the state change applies to |
| 1171 | // |pending_service_|. Otherwise, it applies to |current_service_|. |
| 1172 | // |
| 1173 | // This policy is driven by the fact that the |pending_service_| |
| 1174 | // doesn't become the |current_service_| until wpa_supplicant |
| 1175 | // reports a CurrentBSS change to the |pending_service_|. And the |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1176 | // CurrentBSS change won't be reported until the |pending_service_| |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1177 | // reaches the WPASupplicant::kInterfaceStateCompleted state. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1178 | affected_service = |
| 1179 | pending_service_.get() ? pending_service_.get() : current_service_.get(); |
| 1180 | if (!affected_service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1181 | SLOG(WiFi, 2) << "WiFi " << link_name() << " " << __func__ |
| 1182 | << " with no service"; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1183 | return; |
| 1184 | } |
| 1185 | |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1186 | if (new_state == WPASupplicant::kInterfaceStateCompleted) { |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 1187 | if (affected_service->IsConnected()) { |
| 1188 | StopReconnectTimer(); |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 1189 | EnableHighBitrates(); |
Christopher Wiley | 8f81e2a | 2012-10-17 16:51:32 -0700 | [diff] [blame] | 1190 | } else if (has_already_completed_) { |
| 1191 | LOG(INFO) << link_name() << " L3 configuration already started."; |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 1192 | } else if (AcquireIPConfigWithLeaseName( |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 1193 | GetServiceLeaseName(*affected_service))) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1194 | LOG(INFO) << link_name() << " is up; started L3 configuration."; |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1195 | affected_service->SetState(Service::kStateConfiguring); |
| 1196 | } else { |
| 1197 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 1198 | } |
Christopher Wiley | 8f81e2a | 2012-10-17 16:51:32 -0700 | [diff] [blame] | 1199 | has_already_completed_ = true; |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1200 | } else if (new_state == WPASupplicant::kInterfaceStateAssociated) { |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1201 | affected_service->SetState(Service::kStateAssociating); |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1202 | } else if (new_state == WPASupplicant::kInterfaceStateAuthenticating || |
| 1203 | new_state == WPASupplicant::kInterfaceStateAssociating || |
| 1204 | new_state == WPASupplicant::kInterfaceState4WayHandshake || |
| 1205 | new_state == WPASupplicant::kInterfaceStateGroupHandshake) { |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1206 | // Ignore transitions into these states from Completed, to avoid |
| 1207 | // bothering the user when roaming, or re-keying. |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1208 | if (old_state != WPASupplicant::kInterfaceStateCompleted) |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1209 | affected_service->SetState(Service::kStateAssociating); |
| 1210 | // TOOD(quiche): On backwards transitions, we should probably set |
| 1211 | // a timeout for getting back into the completed state. At present, |
| 1212 | // we depend on wpa_supplicant eventually reporting that CurrentBSS |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1213 | // has changed. But there may be cases where that signal is not sent. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1214 | // (crosbug.com/23207) |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1215 | } else if (new_state == WPASupplicant::kInterfaceStateDisconnected && |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 1216 | affected_service == current_service_ && |
| 1217 | affected_service->IsConnected()) { |
| 1218 | // This means that wpa_supplicant failed in a re-connect attempt, but |
| 1219 | // may still be reconnecting. Give wpa_supplicant a limited amount of |
| 1220 | // time to transition out this condition by either connecting or changing |
| 1221 | // CurrentBSS. |
| 1222 | StartReconnectTimer(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1223 | } else { |
| 1224 | // Other transitions do not affect Service state. |
| 1225 | // |
| 1226 | // Note in particular that we ignore a State change into |
| 1227 | // kInterfaceStateDisconnected, in favor of observing the corresponding |
| 1228 | // change in CurrentBSS. |
| 1229 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 1232 | bool WiFi::SuspectCredentials( |
| 1233 | const WiFiService &service, Service::ConnectFailure *failure) const { |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 1234 | if (service.IsSecurityMatch(flimflam::kSecurityPsk)) { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1235 | if (supplicant_state_ == WPASupplicant::kInterfaceState4WayHandshake && |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 1236 | !service.has_ever_connected()) { |
| 1237 | if (failure) { |
| 1238 | *failure = Service::kFailureBadPassphrase; |
| 1239 | } |
| 1240 | return true; |
| 1241 | } |
| 1242 | } else if (service.IsSecurityMatch(flimflam::kSecurity8021x)) { |
Paul Stewart | 735eab5 | 2013-03-29 09:19:23 -0700 | [diff] [blame] | 1243 | if (eap_state_handler_->is_eap_in_progress() && |
| 1244 | !service.has_ever_connected()) { |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 1245 | if (failure) { |
| 1246 | *failure = Service::kFailureEAPAuthentication; |
| 1247 | } |
| 1248 | return true; |
| 1249 | } |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Paul Stewart | 1369c2b | 2013-01-11 05:41:26 -0800 | [diff] [blame] | 1252 | return false; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 1255 | // static |
| 1256 | bool WiFi::SanitizeSSID(string *ssid) { |
| 1257 | CHECK(ssid); |
| 1258 | |
| 1259 | size_t ssid_len = ssid->length(); |
| 1260 | size_t i; |
| 1261 | bool changed = false; |
| 1262 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1263 | for (i = 0; i < ssid_len; ++i) { |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 1264 | if (!g_ascii_isprint((*ssid)[i])) { |
| 1265 | (*ssid)[i] = '?'; |
| 1266 | changed = true; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | return changed; |
| 1271 | } |
| 1272 | |
Darin Petkov | 50cb78a | 2013-02-06 16:17:49 +0100 | [diff] [blame] | 1273 | // static |
| 1274 | string WiFi::LogSSID(const string &ssid) { |
| 1275 | string out; |
| 1276 | for (string::const_iterator it = ssid.begin(); it != ssid.end(); ++it) { |
| 1277 | // Replace '[' and ']' (in addition to non-printable characters) so that |
| 1278 | // it's easy to match the right substring through a non-greedy regex. |
| 1279 | if (*it == '[' || *it == ']' || !g_ascii_isprint(*it)) { |
| 1280 | base::StringAppendF(&out, "\\x%02x", *it); |
| 1281 | } else { |
| 1282 | out += *it; |
| 1283 | } |
| 1284 | } |
| 1285 | return StringPrintf("[SSID=%s]", out.c_str()); |
| 1286 | } |
| 1287 | |
Paul Stewart | 3c508e1 | 2012-08-09 11:40:06 -0700 | [diff] [blame] | 1288 | void WiFi::OnLinkMonitorFailure() { |
| 1289 | // If we have never found the gateway, let's be conservative and not |
| 1290 | // do anything, in case this network topology does not have a gateway. |
| 1291 | if (!link_monitor()->IsGatewayFound()) { |
| 1292 | LOG(INFO) << "In " << __func__ << "(): " |
| 1293 | << "Skipping reassociate since gateway was never found."; |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | if (!supplicant_present_) { |
| 1298 | LOG(ERROR) << "In " << __func__ << "(): " |
| 1299 | << "wpa_supplicant is not present. Cannot reassociate."; |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | try { |
Christopher Wiley | e0b2a01 | 2012-10-31 13:11:27 -0700 | [diff] [blame] | 1304 | // This will force a transition out of connected, if we are actually |
| 1305 | // connected. |
Paul Stewart | 3c508e1 | 2012-08-09 11:40:06 -0700 | [diff] [blame] | 1306 | supplicant_interface_proxy_->Reassociate(); |
Christopher Wiley | e0b2a01 | 2012-10-31 13:11:27 -0700 | [diff] [blame] | 1307 | // If we don't eventually get a transition back into a connected state, |
| 1308 | // there is something wrong. |
| 1309 | StartReconnectTimer(); |
Paul Stewart | 3c508e1 | 2012-08-09 11:40:06 -0700 | [diff] [blame] | 1310 | LOG(INFO) << "In " << __func__ << "(): Called Reassociate()."; |
| 1311 | } catch (const DBus::Error &e) { // NOLINT |
| 1312 | LOG(ERROR) << "In " << __func__ << "(): failed to call Reassociate()."; |
| 1313 | return; |
| 1314 | } |
| 1315 | } |
| 1316 | |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 1317 | bool WiFi::ShouldUseArpGateway() const { |
| 1318 | return true; |
| 1319 | } |
| 1320 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1321 | void WiFi::DisassociateFromService(const WiFiServiceRefPtr &service) { |
| 1322 | DisconnectFrom(service); |
| 1323 | if (service == selected_service()) { |
| 1324 | DropConnection(); |
| 1325 | } |
| 1326 | Error unused_error; |
| 1327 | RemoveNetworkForService(service, &unused_error); |
| 1328 | } |
| 1329 | |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 1330 | vector<GeolocationInfo> WiFi::GetGeolocationObjects() const { |
| 1331 | vector<GeolocationInfo> objects; |
| 1332 | for (EndpointMap::const_iterator it = endpoint_by_rpcid_.begin(); |
| 1333 | it != endpoint_by_rpcid_.end(); |
| 1334 | ++it) { |
| 1335 | GeolocationInfo geoinfo; |
| 1336 | WiFiEndpointRefPtr endpoint = it->second; |
| 1337 | geoinfo.AddField(kGeoMacAddressProperty, endpoint->bssid_string()); |
| 1338 | geoinfo.AddField(kGeoSignalStrengthProperty, |
| 1339 | StringPrintf("%d", endpoint->signal_strength())); |
| 1340 | geoinfo.AddField( |
| 1341 | kGeoChannelProperty, |
| 1342 | StringPrintf("%d", |
| 1343 | Metrics::WiFiFrequencyToChannel(endpoint->frequency()))); |
| 1344 | // TODO(gauravsh): Include age field. crosbug.com/35445 |
| 1345 | objects.push_back(geoinfo); |
| 1346 | } |
| 1347 | return objects; |
| 1348 | } |
| 1349 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1350 | void WiFi::HelpRegisterDerivedInt32( |
| 1351 | PropertyStore *store, |
| 1352 | const string &name, |
| 1353 | int32(WiFi::*get)(Error *error), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1354 | bool(WiFi::*set)(const int32 &value, Error *error)) { |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1355 | store->RegisterDerivedInt32( |
| 1356 | name, |
| 1357 | Int32Accessor(new CustomAccessor<WiFi, int32>(this, get, set))); |
| 1358 | } |
| 1359 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1360 | void WiFi::HelpRegisterDerivedUint16( |
| 1361 | PropertyStore *store, |
| 1362 | const string &name, |
| 1363 | uint16(WiFi::*get)(Error *error), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1364 | bool(WiFi::*set)(const uint16 &value, Error *error)) { |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1365 | store->RegisterDerivedUint16( |
| 1366 | name, |
| 1367 | Uint16Accessor(new CustomAccessor<WiFi, uint16>(this, get, set))); |
| 1368 | } |
| 1369 | |
mukesh agrawal | 2f9df4e | 2012-08-08 12:29:20 -0700 | [diff] [blame] | 1370 | void WiFi::OnAfterResume() { |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 1371 | LOG(INFO) << __func__; |
mukesh agrawal | 2f9df4e | 2012-08-08 12:29:20 -0700 | [diff] [blame] | 1372 | Device::OnAfterResume(); // May refresh ipconfig_ |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 1373 | |
mukesh agrawal | 2f9df4e | 2012-08-08 12:29:20 -0700 | [diff] [blame] | 1374 | // We want to flush the BSS cache, but we don't want to conflict |
| 1375 | // with a running scan or an active connection attempt. So record |
| 1376 | // the need to flush, and take care of flushing when the next scan |
| 1377 | // completes. |
| 1378 | // |
| 1379 | // Note that supplicant will automatically expire old cache |
| 1380 | // entries (after, e.g., a BSS is not found in two consecutive |
| 1381 | // scans). However, our explicit flush accelerates re-association |
| 1382 | // in cases where a BSS disappeared while we were asleep. (See, |
| 1383 | // e.g. WiFiRoaming.005SuspendRoam.) |
| 1384 | time_->GetTimeMonotonic(&resumed_at_); |
| 1385 | need_bss_flush_ = true; |
| 1386 | |
| 1387 | if (!scan_pending_ && IsIdle()) { |
| 1388 | // Not scanning/connecting/connected, so let's get things rolling. |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1389 | Scan(kProgressiveScan, NULL); |
mukesh agrawal | 2f9df4e | 2012-08-08 12:29:20 -0700 | [diff] [blame] | 1390 | } else { |
| 1391 | SLOG(WiFi, 1) << __func__ |
| 1392 | << " skipping scan, already scanning or connected."; |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 1396 | void WiFi::OnConnected() { |
| 1397 | Device::OnConnected(); |
| 1398 | EnableHighBitrates(); |
| 1399 | } |
| 1400 | |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 1401 | void WiFi::RestartFastScanAttempts() { |
| 1402 | fast_scans_remaining_ = kNumFastScanAttempts; |
| 1403 | StartScanTimer(); |
| 1404 | } |
| 1405 | |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 1406 | void WiFi::StartScanTimer() { |
| 1407 | if (scan_interval_seconds_ == 0) { |
| 1408 | StopScanTimer(); |
| 1409 | return; |
| 1410 | } |
| 1411 | scan_timer_callback_.Reset( |
| 1412 | Bind(&WiFi::ScanTimerHandler, weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 1413 | // Repeat the first few scans after disconnect relatively quickly so we |
| 1414 | // have reasonable trust that no APs we are looking for are present. |
| 1415 | dispatcher()->PostDelayedTask(scan_timer_callback_.callback(), |
| 1416 | fast_scans_remaining_ > 0 ? |
| 1417 | kFastScanIntervalSeconds * 1000 : scan_interval_seconds_ * 1000); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | void WiFi::StopScanTimer() { |
| 1421 | scan_timer_callback_.Cancel(); |
| 1422 | } |
| 1423 | |
| 1424 | void WiFi::ScanTimerHandler() { |
| 1425 | SLOG(WiFi, 2) << "WiFi Device " << link_name() << ": " << __func__; |
| 1426 | if (IsIdle() && !scan_pending_) { |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1427 | Scan(kProgressiveScan, NULL); |
Paul Stewart | e369ece | 2012-05-22 09:11:03 -0700 | [diff] [blame] | 1428 | if (fast_scans_remaining_ > 0) { |
| 1429 | --fast_scans_remaining_; |
| 1430 | } |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 1431 | } |
| 1432 | StartScanTimer(); |
| 1433 | } |
| 1434 | |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 1435 | void WiFi::StartPendingTimer() { |
| 1436 | pending_timeout_callback_.Reset( |
| 1437 | Bind(&WiFi::PendingTimeoutHandler, weak_ptr_factory_.GetWeakPtr())); |
| 1438 | dispatcher()->PostDelayedTask(pending_timeout_callback_.callback(), |
| 1439 | kPendingTimeoutSeconds * 1000); |
| 1440 | } |
| 1441 | |
| 1442 | void WiFi::StopPendingTimer() { |
| 1443 | pending_timeout_callback_.Cancel(); |
| 1444 | } |
| 1445 | |
| 1446 | void WiFi::SetPendingService(const WiFiServiceRefPtr &service) { |
Paul Stewart | ff96a84 | 2012-08-13 15:59:10 -0700 | [diff] [blame] | 1447 | SLOG(WiFi, 2) << "WiFi " << link_name() << " setting pending service to " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1448 | << (service ? service->unique_name(): "NULL"); |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 1449 | if (service) { |
| 1450 | service->SetState(Service::kStateAssociating); |
| 1451 | StartPendingTimer(); |
| 1452 | } else if (pending_service_) { |
| 1453 | StopPendingTimer(); |
| 1454 | } |
| 1455 | pending_service_ = service; |
| 1456 | } |
| 1457 | |
| 1458 | void WiFi::PendingTimeoutHandler() { |
mukesh agrawal | d4dc083 | 2013-03-25 14:38:26 -0700 | [diff] [blame] | 1459 | Error unused_error; |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 1460 | LOG(INFO) << "WiFi Device " << link_name() << ": " << __func__; |
| 1461 | CHECK(pending_service_); |
mukesh agrawal | d4dc083 | 2013-03-25 14:38:26 -0700 | [diff] [blame] | 1462 | pending_service_->DisconnectWithFailure( |
| 1463 | Service::kFailureOutOfRange, &unused_error); |
Paul Stewart | 17d9065 | 2013-04-04 15:09:11 -0700 | [diff] [blame] | 1464 | |
| 1465 | // A hidden service may have no endpoints, since wpa_supplicant |
| 1466 | // failed to attain a CurrentBSS. If so, the service has no |
| 1467 | // reference to |this| device and cannot call WiFi::DisconnectFrom() |
| 1468 | // to reset pending_service_. In this case, we must perform the |
| 1469 | // disconnect here ourselves. |
| 1470 | if (pending_service_) { |
| 1471 | CHECK(!pending_service_->HasEndpoints()); |
| 1472 | LOG(INFO) << "Hidden service was not found."; |
| 1473 | DisconnectFrom(pending_service_); |
| 1474 | } |
Paul Stewart | 2b05e62 | 2012-07-13 20:38:44 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 1477 | void WiFi::StartReconnectTimer() { |
Paul Stewart | 1aff730 | 2012-08-04 20:04:47 -0700 | [diff] [blame] | 1478 | if (!reconnect_timeout_callback_.IsCancelled()) { |
| 1479 | LOG(INFO) << "WiFi Device " << link_name() << ": " << __func__ |
| 1480 | << ": reconnect timer already running."; |
| 1481 | return; |
| 1482 | } |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 1483 | LOG(INFO) << "WiFi Device " << link_name() << ": " << __func__; |
| 1484 | reconnect_timeout_callback_.Reset( |
| 1485 | Bind(&WiFi::ReconnectTimeoutHandler, weak_ptr_factory_.GetWeakPtr())); |
| 1486 | dispatcher()->PostDelayedTask(reconnect_timeout_callback_.callback(), |
| 1487 | kReconnectTimeoutSeconds * 1000); |
| 1488 | } |
| 1489 | |
| 1490 | void WiFi::StopReconnectTimer() { |
| 1491 | SLOG(WiFi, 2) << "WiFi Device " << link_name() << ": " << __func__; |
| 1492 | reconnect_timeout_callback_.Cancel(); |
| 1493 | } |
| 1494 | |
| 1495 | void WiFi::ReconnectTimeoutHandler() { |
| 1496 | LOG(INFO) << "WiFi Device " << link_name() << ": " << __func__; |
Paul Stewart | 1aff730 | 2012-08-04 20:04:47 -0700 | [diff] [blame] | 1497 | reconnect_timeout_callback_.Cancel(); |
Paul Stewart | 4466392 | 2012-07-30 11:03:03 -0700 | [diff] [blame] | 1498 | CHECK(current_service_); |
| 1499 | current_service_->SetFailureSilent(Service::kFailureConnect); |
| 1500 | DisconnectFrom(current_service_); |
| 1501 | } |
| 1502 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1503 | void WiFi::OnSupplicantAppear(const string &/*owner*/) { |
| 1504 | LOG(INFO) << "WPA supplicant appeared."; |
| 1505 | if (supplicant_present_) { |
Darin Petkov | 9cd7ca1 | 2012-07-03 11:06:40 +0200 | [diff] [blame] | 1506 | // Restart the WiFi device if it's started already. This will reset the |
| 1507 | // state and connect the device to the new WPA supplicant instance. |
| 1508 | if (enabled()) { |
| 1509 | Restart(); |
| 1510 | } |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1511 | return; |
| 1512 | } |
| 1513 | supplicant_present_ = true; |
| 1514 | ConnectToSupplicant(); |
| 1515 | } |
| 1516 | |
| 1517 | void WiFi::OnSupplicantVanish() { |
| 1518 | LOG(INFO) << "WPA supplicant vanished."; |
| 1519 | if (!supplicant_present_) { |
| 1520 | return; |
| 1521 | } |
| 1522 | supplicant_present_ = false; |
| 1523 | // Restart the WiFi device if it's started already. This will effectively |
| 1524 | // suspend the device until the WPA supplicant reappears. |
| 1525 | if (enabled()) { |
| 1526 | Restart(); |
| 1527 | } |
| 1528 | } |
| 1529 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 1530 | void WiFi::OnWiFiDebugScopeChanged(bool enabled) { |
| 1531 | SLOG(WiFi, 2) << "WiFi debug scope changed; enable is now " << enabled; |
| 1532 | if (!supplicant_process_proxy_.get()) { |
| 1533 | SLOG(WiFi, 2) << "Suplicant process proxy not present."; |
| 1534 | return; |
| 1535 | } |
| 1536 | string current_level; |
| 1537 | try { |
| 1538 | current_level = supplicant_process_proxy_->GetDebugLevel(); |
| 1539 | } catch (const DBus::Error &e) { // NOLINT |
| 1540 | LOG(ERROR) << __func__ << ": Failed to get wpa_supplicant debug level."; |
| 1541 | return; |
| 1542 | } |
| 1543 | |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1544 | if (current_level != WPASupplicant::kDebugLevelInfo && |
| 1545 | current_level != WPASupplicant::kDebugLevelDebug) { |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 1546 | SLOG(WiFi, 2) << "WiFi debug level is currently " |
| 1547 | << current_level |
| 1548 | << "; assuming that it is being controlled elsewhere."; |
| 1549 | return; |
| 1550 | } |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1551 | string new_level = enabled ? WPASupplicant::kDebugLevelDebug : |
| 1552 | WPASupplicant::kDebugLevelInfo; |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 1553 | |
| 1554 | if (new_level == current_level) { |
| 1555 | SLOG(WiFi, 2) << "WiFi debug level is already the desired level " |
| 1556 | << current_level; |
| 1557 | return; |
| 1558 | } |
| 1559 | |
| 1560 | try { |
| 1561 | supplicant_process_proxy_->SetDebugLevel(new_level); |
| 1562 | } catch (const DBus::Error &e) { // NOLINT |
| 1563 | LOG(ERROR) << __func__ << ": Failed to set wpa_supplicant debug level."; |
| 1564 | } |
| 1565 | } |
| 1566 | |
Paul Stewart | a47c3c6 | 2012-12-18 12:14:29 -0800 | [diff] [blame] | 1567 | void WiFi::SetConnectionDebugging(bool enabled) { |
| 1568 | if (is_debugging_connection_ == enabled) { |
| 1569 | return; |
| 1570 | } |
| 1571 | OnWiFiDebugScopeChanged( |
| 1572 | enabled || |
| 1573 | ScopeLogger::GetInstance()->IsScopeEnabled(ScopeLogger::kWiFi)); |
| 1574 | is_debugging_connection_ = enabled; |
| 1575 | } |
| 1576 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1577 | void WiFi::ConnectToSupplicant() { |
| 1578 | LOG(INFO) << link_name() << ": " << (enabled() ? "enabled" : "disabled") |
| 1579 | << " supplicant: " |
| 1580 | << (supplicant_present_ ? "present" : "absent") |
| 1581 | << " proxy: " |
| 1582 | << (supplicant_process_proxy_.get() ? "non-null" : "null"); |
| 1583 | if (!enabled() || !supplicant_present_ || supplicant_process_proxy_.get()) { |
| 1584 | return; |
| 1585 | } |
| 1586 | supplicant_process_proxy_.reset( |
| 1587 | proxy_factory_->CreateSupplicantProcessProxy( |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1588 | WPASupplicant::kDBusPath, WPASupplicant::kDBusAddr)); |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 1589 | OnWiFiDebugScopeChanged( |
| 1590 | ScopeLogger::GetInstance()->IsScopeEnabled(ScopeLogger::kWiFi)); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1591 | ::DBus::Path interface_path; |
| 1592 | try { |
| 1593 | map<string, DBus::Variant> create_interface_args; |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1594 | create_interface_args[WPASupplicant::kInterfacePropertyName].writer(). |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1595 | append_string(link_name().c_str()); |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1596 | create_interface_args[WPASupplicant::kInterfacePropertyDriver].writer(). |
| 1597 | append_string(WPASupplicant::kDriverNL80211); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1598 | create_interface_args[ |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1599 | WPASupplicant::kInterfacePropertyConfigFile].writer(). |
Paul Stewart | 196f50f | 2013-03-27 18:02:11 -0700 | [diff] [blame] | 1600 | append_string(WPASupplicant::kSupplicantConfPath); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1601 | interface_path = |
| 1602 | supplicant_process_proxy_->CreateInterface(create_interface_args); |
| 1603 | } catch (const DBus::Error &e) { // NOLINT |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1604 | if (!strcmp(e.name(), WPASupplicant::kErrorInterfaceExists)) { |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1605 | interface_path = |
| 1606 | supplicant_process_proxy_->GetInterface(link_name()); |
| 1607 | // TODO(quiche): Is it okay to crash here, if device is missing? |
| 1608 | } else { |
Paul Stewart | b80c81c | 2012-06-28 13:05:45 -0700 | [diff] [blame] | 1609 | LOG(ERROR) << __func__ << ": Failed to create interface with supplicant."; |
| 1610 | return; |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | supplicant_interface_proxy_.reset( |
| 1615 | proxy_factory_->CreateSupplicantInterfaceProxy( |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 1616 | this, interface_path, WPASupplicant::kDBusAddr)); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1617 | |
| 1618 | RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP, |
| 1619 | IFF_UP); |
| 1620 | // TODO(quiche) Set ApScan=1 and BSSExpireAge=190, like flimflam does? |
| 1621 | |
| 1622 | // Clear out any networks that might previously have been configured |
| 1623 | // for this interface. |
| 1624 | supplicant_interface_proxy_->RemoveAllNetworks(); |
| 1625 | |
| 1626 | // Flush interface's BSS cache, so that we get BSSAdded signals for |
| 1627 | // all BSSes (not just new ones since the last scan). |
| 1628 | supplicant_interface_proxy_->FlushBSS(0); |
| 1629 | |
| 1630 | try { |
| 1631 | // TODO(pstew): Disable fast_reauth until supplicant can properly deal |
| 1632 | // with RADIUS servers that respond strangely to such requests. |
| 1633 | // crosbug.com/25630 |
| 1634 | supplicant_interface_proxy_->SetFastReauth(false); |
| 1635 | } catch (const DBus::Error &e) { // NOLINT |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 1636 | LOG(ERROR) << "Failed to disable fast_reauth. " |
| 1637 | << "May be running an older version of wpa_supplicant."; |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | try { |
| 1641 | // Helps with passing WiFiRomaing.001SSIDSwitchBack. |
| 1642 | supplicant_interface_proxy_->SetScanInterval(kRescanIntervalSeconds); |
| 1643 | } catch (const DBus::Error &e) { // NOLINT |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 1644 | LOG(ERROR) << "Failed to set scan_interval. " |
| 1645 | << "May be running an older version of wpa_supplicant."; |
| 1646 | } |
| 1647 | |
| 1648 | try { |
| 1649 | supplicant_interface_proxy_->SetDisableHighBitrates(true); |
| 1650 | } catch (const DBus::Error &e) { // NOLINT |
| 1651 | LOG(ERROR) << "Failed to disable high bitrates. " |
| 1652 | << "May be running an older version of wpa_supplicant."; |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 1655 | Scan(kProgressiveScan, NULL); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1656 | StartScanTimer(); |
| 1657 | } |
| 1658 | |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 1659 | void WiFi::EnableHighBitrates() { |
| 1660 | LOG(INFO) << "Enabling high bitrates."; |
| 1661 | try { |
| 1662 | supplicant_interface_proxy_->EnableHighBitrates(); |
| 1663 | } catch (const DBus::Error &e) { // NOLINT |
| 1664 | LOG(ERROR) << "exception while enabling high rates: " << e.what(); |
| 1665 | } |
| 1666 | } |
| 1667 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 1668 | void WiFi::Restart() { |
| 1669 | LOG(INFO) << link_name() << " restarting."; |
| 1670 | WiFiRefPtr me = this; // Make sure we don't get destructed. |
| 1671 | // Go through the manager rather than starting and stopping the device |
| 1672 | // directly so that the device can be configured with the profile. |
| 1673 | manager()->DeregisterDevice(me); |
| 1674 | manager()->RegisterDevice(me); |
| 1675 | } |
| 1676 | |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 1677 | void WiFi::ConfigureScanFrequencies() { |
| 1678 | GetWiphyMessage get_wiphy; |
| 1679 | get_wiphy.attributes()->SetU32AttributeValue(NL80211_ATTR_IFINDEX, |
| 1680 | interface_index()); |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1681 | netlink_manager_->SendNl80211Message( |
| 1682 | &get_wiphy, |
| 1683 | Bind(&WiFi::OnNewWiphy, weak_ptr_factory_.GetWeakPtr()), |
| 1684 | Bind(&NetlinkManager::OnNetlinkMessageError)); |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1687 | void WiFi::OnNewWiphy(const Nl80211Message &nl80211_message) { |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 1688 | // Verify NL80211_CMD_NEW_WIPHY |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1689 | if (nl80211_message.command() != NewWiphyMessage::kCommand) { |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 1690 | LOG(ERROR) << "Received unexpected command:" |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1691 | << nl80211_message.command(); |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 1692 | return; |
| 1693 | } |
| 1694 | |
| 1695 | // The attributes, for this message, are complicated. |
| 1696 | // NL80211_ATTR_BANDS contains an array of bands... |
| 1697 | AttributeListConstRefPtr wiphy_bands; |
Wade Guthrie | 7347bf2 | 2013-04-30 11:21:51 -0700 | [diff] [blame] | 1698 | if (!nl80211_message.const_attributes()->ConstGetNestedAttributeList( |
Wade Guthrie | 92d0636 | 2013-04-25 15:41:30 -0700 | [diff] [blame] | 1699 | NL80211_ATTR_WIPHY_BANDS, &wiphy_bands)) { |
| 1700 | LOG(ERROR) << "NL80211_CMD_NEW_WIPHY had no NL80211_ATTR_WIPHY_BANDS"; |
| 1701 | return; |
| 1702 | } |
| 1703 | |
| 1704 | AttributeIdIterator band_iter(*wiphy_bands); |
| 1705 | for (; !band_iter.AtEnd(); band_iter.Advance()) { |
| 1706 | AttributeListConstRefPtr wiphy_band; |
| 1707 | if (!wiphy_bands->ConstGetNestedAttributeList(band_iter.GetId(), |
| 1708 | &wiphy_band)) { |
| 1709 | LOG(WARNING) << "WiFi band " << band_iter.GetId() << " not found"; |
| 1710 | continue; |
| 1711 | } |
| 1712 | |
| 1713 | // ...Each band has a FREQS attribute... |
| 1714 | AttributeListConstRefPtr frequencies; |
| 1715 | if (!wiphy_band->ConstGetNestedAttributeList(NL80211_BAND_ATTR_FREQS, |
| 1716 | &frequencies)) { |
| 1717 | LOG(ERROR) << "BAND " << band_iter.GetId() |
| 1718 | << " had no 'frequencies' attribute"; |
| 1719 | continue; |
| 1720 | } |
| 1721 | |
| 1722 | // ...And each FREQS attribute contains an array of information about the |
| 1723 | // frequency... |
| 1724 | AttributeIdIterator freq_iter(*frequencies); |
| 1725 | for (; !freq_iter.AtEnd(); freq_iter.Advance()) { |
| 1726 | AttributeListConstRefPtr frequency; |
| 1727 | if (frequencies->ConstGetNestedAttributeList(freq_iter.GetId(), |
| 1728 | &frequency)) { |
| 1729 | // ...Including the frequency, itself (the part we want). |
| 1730 | uint32_t frequency_value = 0; |
| 1731 | if (frequency->GetU32AttributeValue(NL80211_FREQUENCY_ATTR_FREQ, |
| 1732 | &frequency_value)) { |
| 1733 | SLOG(WiFi, 5) << "Found frequency[" << freq_iter.GetId() |
| 1734 | << "] = " << frequency_value; |
| 1735 | all_scan_frequencies_.insert(frequency_value); |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 1742 | } // namespace shill |