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