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