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