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