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