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