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> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 19 | #include <base/logging.h> |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 20 | #include <base/stringprintf.h> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 21 | #include <base/string_number_conversions.h> |
| 22 | #include <base/string_util.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 23 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 24 | #include <glib.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 25 | |
| 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" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 33 | #include "shill/manager.h" |
Thieu Le | 67370f6 | 2012-02-14 23:01:42 +0000 | [diff] [blame] | 34 | #include "shill/metrics.h" |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 35 | #include "shill/power_manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 36 | #include "shill/profile.h" |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 37 | #include "shill/property_accessor.h" |
Darin Petkov | d196726 | 2011-07-18 14:55:18 -0700 | [diff] [blame] | 38 | #include "shill/proxy_factory.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 39 | #include "shill/rtnl_handler.h" |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 40 | #include "shill/shill_time.h" |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 41 | #include "shill/store_interface.h" |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 42 | #include "shill/supplicant_interface_proxy_interface.h" |
| 43 | #include "shill/supplicant_process_proxy_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 44 | #include "shill/technology.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 45 | #include "shill/wifi_endpoint.h" |
| 46 | #include "shill/wifi_service.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 47 | #include "shill/wpa_supplicant.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 48 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 49 | using base::Bind; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 50 | using base::StringPrintf; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 51 | using std::map; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 52 | using std::set; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 53 | using std::string; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 54 | using std::vector; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 55 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 56 | namespace shill { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 57 | |
| 58 | // statics |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 59 | const char *WiFi::kDefaultBgscanMethod = |
| 60 | wpa_supplicant::kNetworkBgscanMethodSimple; |
| 61 | const uint16 WiFi::kDefaultBgscanShortIntervalSeconds = 30; |
| 62 | const int32 WiFi::kDefaultBgscanSignalThresholdDbm = -50; |
| 63 | const uint16 WiFi::kDefaultScanIntervalSeconds = 180; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 64 | // Note that WiFi generates some manager-level errors, because it implements |
| 65 | // the Manager.GetWiFiService flimflam API. The API is implemented here, |
| 66 | // rather than in manager, to keep WiFi-specific logic in the right place. |
| 67 | const char WiFi::kManagerErrorPassphraseRequired[] = "must specify passphrase"; |
| 68 | const char WiFi::kManagerErrorSSIDRequired[] = "must specify SSID"; |
| 69 | const char WiFi::kManagerErrorSSIDTooLong[] = "SSID is too long"; |
| 70 | const char WiFi::kManagerErrorSSIDTooShort[] = "SSID is too short"; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 71 | const char WiFi::kManagerErrorUnsupportedSecurityMode[] = |
| 72 | "security mode is unsupported"; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 73 | const char WiFi::kManagerErrorUnsupportedServiceMode[] = |
| 74 | "service mode is unsupported"; |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 75 | // Age (in seconds) beyond which a BSS cache entry will not be preserved, |
| 76 | // across a suspend/resume. |
| 77 | const time_t WiFi::kMaxBSSResumeAgeSeconds = 10; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 78 | const char WiFi::kInterfaceStateUnknown[] = "shill-unknown"; |
mukesh agrawal | f202817 | 2012-03-13 14:20:22 -0700 | [diff] [blame] | 79 | const time_t WiFi::kRescanIntervalSeconds = 1; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 80 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 81 | WiFi::WiFi(ControlInterface *control_interface, |
| 82 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 83 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 84 | Manager *manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 85 | const string& link, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 86 | const string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 87 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 88 | : Device(control_interface, |
| 89 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 90 | metrics, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 91 | manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 92 | link, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 93 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 94 | interface_index, |
| 95 | Technology::kWifi), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 96 | weak_ptr_factory_(this), |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 97 | proxy_factory_(ProxyFactory::GetInstance()), |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 98 | time_(Time::GetInstance()), |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 99 | supplicant_state_(kInterfaceStateUnknown), |
| 100 | supplicant_bss_("(unknown)"), |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 101 | clear_cached_credentials_pending_(false), |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 102 | need_bss_flush_(false), |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 103 | bgscan_method_(kDefaultBgscanMethod), |
| 104 | bgscan_short_interval_seconds_(kDefaultBgscanShortIntervalSeconds), |
| 105 | bgscan_signal_threshold_dbm_(kDefaultBgscanSignalThresholdDbm), |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 106 | scan_pending_(false), |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 107 | scan_interval_seconds_(kDefaultScanIntervalSeconds) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 108 | PropertyStore *store = this->mutable_store(); |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 109 | resumed_at_.tv_sec = 0; |
| 110 | resumed_at_.tv_usec = 0; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 111 | HelpRegisterDerivedString(store, |
| 112 | flimflam::kBgscanMethodProperty, |
| 113 | &WiFi::GetBgscanMethod, |
| 114 | &WiFi::SetBgscanMethod); |
| 115 | HelpRegisterDerivedUint16(store, |
| 116 | flimflam::kBgscanShortIntervalProperty, |
| 117 | &WiFi::GetBgscanShortInterval, |
| 118 | &WiFi::SetBgscanShortInterval); |
| 119 | HelpRegisterDerivedInt32(store, |
| 120 | flimflam::kBgscanSignalThresholdProperty, |
| 121 | &WiFi::GetBgscanSignalThreshold, |
| 122 | &WiFi::SetBgscanSignalThreshold); |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 123 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 124 | // TODO(quiche): Decide if scan_pending_ is close enough to |
| 125 | // "currently scanning" that we don't care, or if we want to track |
| 126 | // scan pending/currently scanning/no scan scheduled as a tri-state |
| 127 | // kind of thing. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 128 | store->RegisterConstBool(flimflam::kScanningProperty, &scan_pending_); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 129 | HelpRegisterDerivedUint16(store, |
| 130 | flimflam::kScanIntervalProperty, |
| 131 | &WiFi::GetScanInterval, |
| 132 | &WiFi::SetScanInterval); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 133 | VLOG(2) << "WiFi device " << link_name() << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 134 | } |
| 135 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 136 | WiFi::~WiFi() {} |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 137 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 138 | void WiFi::Start(Error *error, const EnabledStateChangedCallback &callback) { |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 139 | ::DBus::Path interface_path; |
| 140 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 141 | supplicant_process_proxy_.reset( |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 142 | proxy_factory_->CreateSupplicantProcessProxy( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 143 | wpa_supplicant::kDBusPath, wpa_supplicant::kDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 144 | try { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 145 | map<string, DBus::Variant> create_interface_args; |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame^] | 146 | create_interface_args[wpa_supplicant::kInterfacePropertyName].writer(). |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 147 | append_string(link_name().c_str()); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame^] | 148 | create_interface_args[wpa_supplicant::kInterfacePropertyDriver].writer(). |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 149 | append_string(wpa_supplicant::kDriverNL80211); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame^] | 150 | create_interface_args[ |
| 151 | wpa_supplicant::kInterfacePropertyConfigFile].writer(). |
| 152 | append_string(SCRIPTDIR "/wpa_supplicant.conf"); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 153 | interface_path = |
| 154 | supplicant_process_proxy_->CreateInterface(create_interface_args); |
| 155 | } catch (const DBus::Error e) { // NOLINT |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 156 | if (!strcmp(e.name(), wpa_supplicant::kErrorInterfaceExists)) { |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 157 | interface_path = |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 158 | supplicant_process_proxy_->GetInterface(link_name()); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 159 | // TODO(quiche): Is it okay to crash here, if device is missing? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 160 | } else { |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 161 | // TODO(quiche): Log error. |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 165 | supplicant_interface_proxy_.reset( |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 166 | proxy_factory_->CreateSupplicantInterfaceProxy( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 167 | this, interface_path, wpa_supplicant::kDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 168 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 169 | RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP, |
| 170 | IFF_UP); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 171 | // TODO(quiche) Set ApScan=1 and BSSExpireAge=190, like flimflam does? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 172 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 173 | // Clear out any networks that might previously have been configured |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 174 | // for this interface. |
| 175 | supplicant_interface_proxy_->RemoveAllNetworks(); |
| 176 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 177 | // Flush interface's BSS cache, so that we get BSSAdded signals for |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 178 | // all BSSes (not just new ones since the last scan). |
| 179 | supplicant_interface_proxy_->FlushBSS(0); |
| 180 | |
mukesh agrawal | 23e9f28 | 2012-02-17 15:36:36 -0800 | [diff] [blame] | 181 | try { |
| 182 | // TODO(pstew): Disable fast_reauth until supplicant can properly deal |
| 183 | // with RADIUS servers that respond strangely to such requests. |
| 184 | // crosbug.com/25630 |
| 185 | supplicant_interface_proxy_->SetFastReauth(false); |
| 186 | } catch (const DBus::Error e) { // NOLINT |
mukesh agrawal | f202817 | 2012-03-13 14:20:22 -0700 | [diff] [blame] | 187 | LOG(INFO) << "Failed to disable fast_reauth." |
| 188 | << "May be running an older version of wpa_supplicant."; |
| 189 | } |
| 190 | |
| 191 | try { |
| 192 | // Helps with passing WiFiRomaing.001SSIDSwitchBack. |
| 193 | supplicant_interface_proxy_->SetScanInterval(kRescanIntervalSeconds); |
| 194 | } catch (const DBus::Error e) { // NOLINT |
| 195 | LOG(INFO) << "Failed to set scan_interval. " |
mukesh agrawal | 23e9f28 | 2012-02-17 15:36:36 -0800 | [diff] [blame] | 196 | << "May be running an older version of wpa_supplicant."; |
| 197 | } |
Paul Stewart | 2987dcf | 2012-01-30 15:47:42 -0800 | [diff] [blame] | 198 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 199 | // Register for power state changes. HandlePowerStateChange() will be called |
| 200 | // when the power state changes. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 201 | manager()->power_manager()->AddStateChangeCallback( |
| 202 | UniqueName(), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 203 | Bind(&WiFi::HandlePowerStateChange, weak_ptr_factory_.GetWeakPtr())); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 204 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 205 | Scan(NULL); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 206 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 207 | if (error) |
| 208 | error->Reset(); // indicate immediate completion |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 211 | void WiFi::Stop(Error *error, const EnabledStateChangedCallback &callback) { |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 212 | VLOG(2) << "WiFi " << link_name() << " stopping."; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 213 | // TODO(quiche): Remove interface from supplicant. |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 214 | supplicant_interface_proxy_.reset(); // breaks a reference cycle |
| 215 | supplicant_process_proxy_.reset(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 216 | endpoint_by_rpcid_.clear(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 217 | rpcid_by_service_.clear(); |
Gary Morain | 9100112 | 2012-02-29 16:22:26 -0800 | [diff] [blame] | 218 | manager()->power_manager()->RemoveStateChangeCallback(UniqueName()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 219 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 220 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 221 | it != services_.end(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 222 | ++it) { |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 223 | VLOG(3) << "WiFi " << link_name() << " deregistering service " |
| 224 | << (*it)->friendly_name(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 225 | manager()->DeregisterService(*it); |
| 226 | } |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 227 | services_.clear(); // breaks reference cycles |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 228 | current_service_ = NULL; // breaks a reference cycle |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 229 | pending_service_ = NULL; // breaks a reference cycle |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 230 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 231 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 232 | if (error) |
| 233 | error->Reset(); // indicate immediate completion |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 234 | // TODO(quiche): Anything else to do? |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 235 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 236 | VLOG(3) << "WiFi " << link_name() << " supplicant_process_proxy_ " |
| 237 | << (supplicant_process_proxy_.get() ? "is set." : "is not set."); |
| 238 | VLOG(3) << "WiFi " << link_name() << " supplicant_interface_proxy_ " |
| 239 | << (supplicant_interface_proxy_.get() ? "is set." : "is not set."); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 240 | VLOG(3) << "WiFi " << link_name() << " pending_service_ " |
| 241 | << (pending_service_.get() ? "is set." : "is not set."); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 242 | VLOG(3) << "WiFi " << link_name() << " has " << endpoint_by_rpcid_.size() |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 243 | << " EndpointMap entries."; |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 244 | VLOG(3) << "WiFi " << link_name() << " has " << services_.size() |
| 245 | << " Services."; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 248 | bool WiFi::Load(StoreInterface *storage) { |
| 249 | LoadHiddenServices(storage); |
| 250 | return Device::Load(storage); |
| 251 | } |
| 252 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 253 | void WiFi::Scan(Error */*error*/) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 254 | LOG(INFO) << __func__; |
| 255 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 256 | // Needs to send a D-Bus message, but may be called from D-Bus |
| 257 | // signal handler context (via Manager::RequestScan). So defer work |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 258 | // to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 259 | dispatcher()->PostTask(Bind(&WiFi::ScanTask, weak_ptr_factory_.GetWeakPtr())); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 262 | bool WiFi::TechnologyIs(const Technology::Identifier type) const { |
| 263 | return type == Technology::kWifi; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 264 | } |
| 265 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 266 | bool WiFi::IsConnectingTo(const WiFiService &service) const { |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 267 | return pending_service_ == &service || |
| 268 | (current_service_ == &service && |
| 269 | service.state() != Service::kStateConnected); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 270 | } |
| 271 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 272 | void WiFi::BSSAdded(const ::DBus::Path &path, |
| 273 | const map<string, ::DBus::Variant> &properties) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 274 | // 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] | 275 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 276 | dispatcher()->PostTask(Bind(&WiFi::BSSAddedTask, |
| 277 | weak_ptr_factory_.GetWeakPtr(), |
| 278 | path, properties)); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void WiFi::BSSRemoved(const ::DBus::Path &path) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 282 | // 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] | 283 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 284 | dispatcher()->PostTask(Bind(&WiFi::BSSRemovedTask, |
| 285 | weak_ptr_factory_.GetWeakPtr(), path)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 286 | } |
| 287 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 288 | void WiFi::PropertiesChanged(const map<string, ::DBus::Variant> &properties) { |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 289 | LOG(INFO) << "In " << __func__ << "(): called"; |
| 290 | // Called from D-Bus signal handler, but may need to send a D-Bus |
| 291 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 292 | dispatcher()->PostTask(Bind(&WiFi::PropertiesChangedTask, |
| 293 | weak_ptr_factory_.GetWeakPtr(), properties)); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 294 | } |
| 295 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 296 | void WiFi::ScanDone() { |
| 297 | LOG(INFO) << __func__; |
| 298 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 299 | // Defer handling of scan result processing, because that processing |
| 300 | // may require the the registration of new D-Bus objects. And such |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 301 | // registration can't be done in the context of a D-Bus signal |
| 302 | // handler. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 303 | dispatcher()->PostTask(Bind(&WiFi::ScanDoneTask, |
| 304 | weak_ptr_factory_.GetWeakPtr())); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 305 | } |
| 306 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 307 | void WiFi::ConnectTo(WiFiService *service, |
mukesh agrawal | 6489632 | 2011-12-01 01:13:10 +0000 | [diff] [blame] | 308 | map<string, DBus::Variant> service_params) { |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 309 | CHECK(service) << "Can't connect to NULL service."; |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 310 | DBus::Path network_path; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 311 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 312 | // TODO(quiche): Handle cases where already connected. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 313 | if (pending_service_ && pending_service_ == service) { |
| 314 | // TODO(quiche): Return an error to the caller. crosbug.com/23832 |
| 315 | LOG(INFO) << "WiFi " << link_name() << " ignoring ConnectTo " |
| 316 | << service->friendly_name() |
| 317 | << ", which is already pending."; |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | if (pending_service_ && pending_service_ != service) { |
| 322 | DisconnectFrom(pending_service_); |
| 323 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 324 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 325 | try { |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 326 | // TODO(quiche): Set a timeout here. In the normal case, we expect |
| 327 | // that, if wpa_supplicant fails to connect, it will eventually send |
| 328 | // a signal that its CurrentBSS has changed. But there may be cases |
| 329 | // where the signal is not sent. (crosbug.com/23206) |
mukesh agrawal | 6489632 | 2011-12-01 01:13:10 +0000 | [diff] [blame] | 330 | const uint32_t scan_ssid = 1; // "True": Use directed probe. |
| 331 | service_params[wpa_supplicant::kNetworkPropertyScanSSID].writer(). |
| 332 | append_uint32(scan_ssid); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 333 | service_params[wpa_supplicant::kNetworkPropertyBgscan].writer(). |
| 334 | append_string(CreateBgscanConfigString().c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 335 | network_path = |
| 336 | supplicant_interface_proxy_->AddNetwork(service_params); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 337 | rpcid_by_service_[service] = network_path; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 338 | } catch (const DBus::Error e) { // NOLINT |
| 339 | LOG(ERROR) << "exception while adding network: " << e.what(); |
| 340 | return; |
| 341 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 342 | |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 343 | supplicant_interface_proxy_->SelectNetwork(network_path); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 344 | pending_service_ = service; |
| 345 | CHECK(current_service_.get() != pending_service_.get()); |
| 346 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 347 | // SelectService here (instead of in LinkEvent, like Ethernet), so |
| 348 | // that, if we fail to bring up L2, we can attribute failure correctly. |
| 349 | // |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 350 | // TODO(quiche): When we add code for dealing with connection failures, |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 351 | // reconsider if this is the right place to change the selected service. |
| 352 | // see discussion in crosbug.com/20191. |
| 353 | SelectService(service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 356 | void WiFi::DisconnectFrom(WiFiService *service) { |
| 357 | if (service != current_service_ && service != pending_service_) { |
| 358 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 359 | // generate a D-Bus error here. (crosbug.com/23832) |
| 360 | LOG(WARNING) << "In " << __func__ << "(): " |
| 361 | << " ignoring request to disconnect from service " |
| 362 | << service->friendly_name() |
| 363 | << " which is neither current nor pending"; |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | if (pending_service_ && service != pending_service_) { |
| 368 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 369 | // generate a D-Bus error here. (crosbug.com/23832) |
| 370 | LOG(WARNING) << "In " << __func__ << "(): " |
| 371 | << " ignoring request to disconnect from service " |
| 372 | << service->friendly_name() |
| 373 | << " which is not the pending service."; |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | if (!pending_service_ && service != current_service_) { |
| 378 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 379 | // generate a D-Bus error here. (crosbug.com/23832) |
| 380 | LOG(WARNING) << "In " << __func__ << "(): " |
| 381 | << " ignoring request to disconnect from service " |
| 382 | << service->friendly_name() |
| 383 | << " which is not the current service."; |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | pending_service_ = NULL; |
| 388 | try { |
| 389 | supplicant_interface_proxy_->Disconnect(); |
| 390 | // We'll call RemoveNetwork and reset |current_service_| after |
| 391 | // supplicant notifies us that the CurrentBSS has changed. |
| 392 | } catch (const DBus::Error e) { // NOLINT |
| 393 | // Can't depend on getting a notification of CurrentBSS change. |
| 394 | // So effect changes immediately. |
| 395 | ReverseServiceMap::const_iterator rpcid_it = |
| 396 | rpcid_by_service_.find(service); |
| 397 | DCHECK(rpcid_it != rpcid_by_service_.end()); |
| 398 | if (rpcid_it == rpcid_by_service_.end()) { |
| 399 | LOG(WARNING) << "WiFi " << link_name() << " can not disconnect from " |
| 400 | << service->friendly_name() << ": " |
| 401 | << "could not find supplicant network to disable."; |
| 402 | } else { |
| 403 | supplicant_interface_proxy_->RemoveNetwork(rpcid_it->second); |
| 404 | } |
| 405 | current_service_ = NULL; |
| 406 | } |
| 407 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 408 | CHECK(current_service_ == NULL || |
| 409 | current_service_.get() != pending_service_.get()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 410 | } |
| 411 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 412 | bool WiFi::IsIdle() const { |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 413 | return !current_service_ && !pending_service_; |
| 414 | } |
| 415 | |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 416 | void WiFi::ClearCachedCredentials() { |
| 417 | LOG(INFO) << __func__; |
| 418 | |
| 419 | // Needs to send a D-Bus message, but may be called from D-Bus |
| 420 | // caller context (via Manager::PopProfile). So defer work |
| 421 | // to event loop. |
| 422 | if (!clear_cached_credentials_pending_) { |
| 423 | clear_cached_credentials_pending_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 424 | dispatcher()->PostTask(Bind(&WiFi::ClearCachedCredentialsTask, |
| 425 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 429 | void WiFi::NotifyEndpointChanged(const WiFiEndpoint &endpoint) { |
| 430 | WiFiService *service = FindServiceForEndpoint(endpoint); |
| 431 | DCHECK(service); |
| 432 | if (service) { |
| 433 | service->NotifyEndpointUpdated(endpoint); |
| 434 | return; |
| 435 | } |
| 436 | } |
| 437 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 438 | string WiFi::CreateBgscanConfigString() { |
| 439 | return StringPrintf("%s:%d:%d:%d", |
| 440 | bgscan_method_.c_str(), |
| 441 | bgscan_short_interval_seconds_, |
| 442 | bgscan_signal_threshold_dbm_, |
| 443 | scan_interval_seconds_); |
| 444 | } |
| 445 | |
| 446 | void WiFi::SetBgscanMethod(const string &method, Error *error) { |
| 447 | if (method != wpa_supplicant::kNetworkBgscanMethodSimple && |
| 448 | method != wpa_supplicant::kNetworkBgscanMethodLearn) { |
| 449 | const string error_message = |
| 450 | StringPrintf("Unrecognized bgscan method %s", method.c_str()); |
| 451 | LOG(WARNING) << error_message; |
| 452 | error->Populate(Error::kInvalidArguments, error_message); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | bgscan_method_ = method; |
| 457 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 458 | // |current_service_|, because supplicant does not allow for |
| 459 | // reconfiguration without disconnect and reconnect. |
| 460 | } |
| 461 | |
| 462 | void WiFi::SetBgscanShortInterval(const uint16 &seconds, Error */*error*/) { |
| 463 | bgscan_short_interval_seconds_ = seconds; |
| 464 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 465 | // |current_service_|, because supplicant does not allow for |
| 466 | // reconfiguration without disconnect and reconnect. |
| 467 | } |
| 468 | |
| 469 | void WiFi::SetBgscanSignalThreshold(const int32 &dbm, Error */*error*/) { |
| 470 | bgscan_signal_threshold_dbm_ = dbm; |
| 471 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 472 | // |current_service_|, because supplicant does not allow for |
| 473 | // reconfiguration without disconnect and reconnect. |
| 474 | } |
| 475 | |
| 476 | void WiFi::SetScanInterval(const uint16 &seconds, Error */*error*/) { |
| 477 | scan_interval_seconds_ = seconds; |
| 478 | // We do not update |pending_service_| or |current_service_|, because |
| 479 | // supplicant does not allow for reconfiguration without disconnect |
| 480 | // and reconnect. |
| 481 | |
| 482 | // TODO(quiche): Update scan timer. crosbug.com/24337 |
| 483 | } |
| 484 | |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 485 | // To avoid creating duplicate services, call FindServiceForEndpoint |
| 486 | // before calling this method. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 487 | WiFiServiceRefPtr WiFi::CreateServiceForEndpoint(const WiFiEndpoint &endpoint, |
| 488 | bool hidden_ssid) { |
| 489 | WiFiServiceRefPtr service = |
| 490 | new WiFiService(control_interface(), |
| 491 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 492 | metrics(), |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 493 | manager(), |
| 494 | this, |
| 495 | endpoint.ssid(), |
| 496 | endpoint.network_mode(), |
| 497 | endpoint.security_mode(), |
| 498 | hidden_ssid); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 499 | services_.push_back(service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 500 | return service; |
| 501 | } |
| 502 | |
| 503 | void WiFi::CurrentBSSChanged(const ::DBus::Path &new_bss) { |
| 504 | VLOG(3) << "WiFi " << link_name() << " CurrentBSS " |
| 505 | << supplicant_bss_ << " -> " << new_bss; |
| 506 | supplicant_bss_ = new_bss; |
| 507 | if (new_bss == wpa_supplicant::kCurrentBSSNull) { |
| 508 | HandleDisconnect(); |
| 509 | } else { |
| 510 | HandleRoam(new_bss); |
| 511 | } |
| 512 | |
| 513 | SelectService(current_service_); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 514 | // Invariant check: a Service can either be current, or pending, but |
| 515 | // not both. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 516 | CHECK(current_service_.get() != pending_service_.get() || |
| 517 | current_service_.get() == NULL); |
| 518 | |
| 519 | // TODO(quiche): Update BSSID property on the Service |
| 520 | // (crosbug.com/22377). |
| 521 | } |
| 522 | |
| 523 | void WiFi::HandleDisconnect() { |
| 524 | // Identify the affected service. We expect to get a disconnect |
| 525 | // event when we fall off a Service that we were connected |
| 526 | // to. However, we also allow for the case where we get a disconnect |
| 527 | // event while attempting to connect from a disconnected state. |
| 528 | WiFiService *affected_service = |
| 529 | current_service_.get() ? current_service_.get() : pending_service_.get(); |
| 530 | |
| 531 | current_service_ = NULL; |
| 532 | if (!affected_service) { |
| 533 | VLOG(2) << "WiFi " << link_name() |
| 534 | << " disconnected while not connected or connecting"; |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | ReverseServiceMap::const_iterator rpcid_it = |
| 539 | rpcid_by_service_.find(affected_service); |
| 540 | if (rpcid_it == rpcid_by_service_.end()) { |
| 541 | VLOG(2) << "WiFi " << link_name() << " disconnected from " |
| 542 | << " (or failed to connect to) " |
| 543 | << affected_service->friendly_name() << ", " |
| 544 | << "but could not find supplicant network to disable."; |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | VLOG(2) << "WiFi " << link_name() << " disconnected from " |
| 549 | << " (or failed to connect to) " |
| 550 | << affected_service->friendly_name(); |
| 551 | // TODO(quiche): Reconsider giving up immediately. Maybe give |
| 552 | // wpa_supplicant some time to retry, first. |
| 553 | supplicant_interface_proxy_->RemoveNetwork(rpcid_it->second); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 554 | // TOOD(quiche): In the event that the disconnect was deliberate, we |
| 555 | // might want to go to SetState(kStateIdle), rather than reporting a |
| 556 | // failure. crosbug.com/24700. |
| 557 | // TODO(quiche): In the event that we suspect a password failure, |
| 558 | // we should not be silent. crosbug.com/23211. |
| 559 | affected_service->SetFailureSilent(Service::kFailureUnknown); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 560 | affected_service->NotifyCurrentEndpoint(NULL); |
Thieu Le | 67370f6 | 2012-02-14 23:01:42 +0000 | [diff] [blame] | 561 | metrics()->NotifyServiceDisconnect(affected_service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 562 | |
| 563 | if (affected_service == pending_service_.get()) { |
| 564 | // The attempt to connect to |pending_service_| failed. Clear |
| 565 | // |pending_service_|, to indicate we're no longer in the middle |
| 566 | // of a connect request. |
| 567 | pending_service_ = NULL; |
| 568 | } else if (pending_service_.get()) { |
| 569 | // We've attributed the disconnection to what was the |
| 570 | // |current_service_|, rather than the |pending_service_|. |
| 571 | // |
| 572 | // If we're wrong about that (i.e. supplicant reported this |
| 573 | // CurrentBSS change after attempting to connect to |
| 574 | // |pending_service_|), we're depending on supplicant to retry |
| 575 | // connecting to |pending_service_|, and delivering another |
| 576 | // CurrentBSS change signal in the future. |
| 577 | // |
| 578 | // Log this fact, to help us debug (in case our assumptions are |
| 579 | // wrong). |
| 580 | VLOG(2) << "WiFi " << link_name() << " pending connection to " |
| 581 | << pending_service_->friendly_name() |
| 582 | << " after disconnect"; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // We use the term "Roam" loosely. In particular, we include the case |
| 587 | // where we "Roam" to a BSS from the disconnected state. |
| 588 | void WiFi::HandleRoam(const ::DBus::Path &new_bss) { |
| 589 | EndpointMap::iterator endpoint_it = endpoint_by_rpcid_.find(new_bss); |
| 590 | if (endpoint_it == endpoint_by_rpcid_.end()) { |
| 591 | LOG(WARNING) << "WiFi " << link_name() << " connected to unknown BSS " |
| 592 | << new_bss; |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | const WiFiEndpoint &endpoint(*endpoint_it->second); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 597 | WiFiServiceRefPtr service = FindServiceForEndpoint(endpoint); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 598 | if (!service.get()) { |
| 599 | LOG(WARNING) << "WiFi " << link_name() |
| 600 | << " could not find Service for Endpoint " |
| 601 | << endpoint.bssid_string() |
| 602 | << " (service will be unchanged)"; |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | VLOG(2) << "WiFi " << link_name() |
| 607 | << " roamed to Endpoint " << endpoint.bssid_string() |
| 608 | << " (SSID " << endpoint.ssid_string() << ")"; |
| 609 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 610 | service->NotifyCurrentEndpoint(&endpoint); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 611 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 612 | if (pending_service_.get() && |
| 613 | service.get() != pending_service_.get()) { |
| 614 | // The Service we've roamed on to is not the one we asked for. |
| 615 | // We assume that this is transient, and that wpa_supplicant |
| 616 | // is trying / will try to connect to |pending_service_|. |
| 617 | // |
| 618 | // If it succeeds, we'll end up back here, but with |service| |
| 619 | // pointing at the same service as |pending_service_|. |
| 620 | // |
| 621 | // If it fails, we'll process things in HandleDisconnect. |
| 622 | // |
| 623 | // So we leave |pending_service_| untouched. |
| 624 | VLOG(2) << "WiFi " << link_name() |
| 625 | << " new current Endpoint " |
| 626 | << endpoint.bssid_string() |
| 627 | << " is not part of pending service " |
| 628 | << pending_service_->friendly_name(); |
| 629 | |
| 630 | // Sanity check: if we didn't roam onto |pending_service_|, we |
| 631 | // should still be on |current_service_|. |
| 632 | if (service.get() != current_service_.get()) { |
| 633 | LOG(WARNING) << "WiFi " << link_name() |
| 634 | << " new current Endpoint " |
| 635 | << endpoint.bssid_string() |
| 636 | << " is neither part of pending service " |
| 637 | << pending_service_->friendly_name() |
| 638 | << " nor part of current service " |
| 639 | << (current_service_.get() ? |
| 640 | current_service_->friendly_name() : |
| 641 | "(NULL)"); |
| 642 | // Although we didn't expect to get here, we should keep |
| 643 | // |current_service_| in sync with what supplicant has done. |
| 644 | current_service_ = service; |
| 645 | } |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | if (pending_service_.get()) { |
| 650 | // We assume service.get() == pending_service_.get() here, because |
| 651 | // of the return in the previous if clause. |
| 652 | // |
| 653 | // Boring case: we've connected to the service we asked |
| 654 | // for. Simply update |current_service_| and |pending_service_|. |
| 655 | current_service_ = service; |
| 656 | pending_service_ = NULL; |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | // |pending_service_| was NULL, so we weren't attempting to connect |
| 661 | // to a new Service. Sanity check that we're still on |
| 662 | // |current_service_|. |
| 663 | if (service.get() != current_service_.get()) { |
| 664 | LOG(WARNING) |
| 665 | << "WiFi " << link_name() |
| 666 | << " new current Endpoint " |
| 667 | << endpoint.bssid_string() |
| 668 | << (current_service_.get() ? |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 669 | StringPrintf(" is not part of current service %s", |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 670 | current_service_->friendly_name().c_str()) : |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 671 | " with no current service"); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 672 | // We didn't expect to be here, but let's cope as well as we |
| 673 | // can. Update |current_service_| to keep it in sync with |
| 674 | // supplicant. |
| 675 | current_service_ = service; |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | // At this point, we know that |pending_service_| was NULL, and that |
| 680 | // we're still on |current_service_|. This is the most boring case |
| 681 | // of all, because there's no state to update here. |
| 682 | return; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 685 | WiFiServiceRefPtr WiFi::FindService(const vector<uint8_t> &ssid, |
| 686 | const string &mode, |
| 687 | const string &security) const { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 688 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 689 | it != services_.end(); |
| 690 | ++it) { |
| 691 | if ((*it)->ssid() == ssid && (*it)->mode() == mode && |
| 692 | (*it)->IsSecurityMatch(security)) { |
| 693 | return *it; |
| 694 | } |
| 695 | } |
| 696 | return NULL; |
| 697 | } |
| 698 | |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 699 | WiFiServiceRefPtr WiFi::FindServiceForEndpoint(const WiFiEndpoint &endpoint) { |
| 700 | return FindService(endpoint.ssid(), |
| 701 | endpoint.network_mode(), |
| 702 | endpoint.security_mode()); |
| 703 | } |
| 704 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 705 | ByteArrays WiFi::GetHiddenSSIDList() { |
| 706 | // Create a unique set of hidden SSIDs. |
| 707 | set<ByteArray> hidden_ssids_set; |
| 708 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 709 | it != services_.end(); |
| 710 | ++it) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 711 | if ((*it)->hidden_ssid() && (*it)->favorite()) { |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 712 | hidden_ssids_set.insert((*it)->ssid()); |
| 713 | } |
| 714 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 715 | VLOG(2) << "Found " << hidden_ssids_set.size() << " hidden services"; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 716 | ByteArrays hidden_ssids(hidden_ssids_set.begin(), hidden_ssids_set.end()); |
| 717 | if (!hidden_ssids.empty()) { |
| 718 | // TODO(pstew): Devise a better method for time-sharing with SSIDs that do |
| 719 | // not fit in. |
| 720 | if (hidden_ssids.size() >= wpa_supplicant::kScanMaxSSIDsPerScan) { |
| 721 | hidden_ssids.erase( |
| 722 | hidden_ssids.begin() + wpa_supplicant::kScanMaxSSIDsPerScan - 1, |
| 723 | hidden_ssids.end()); |
| 724 | } |
| 725 | // Add Broadcast SSID, signified by an empty ByteArray. If we specify |
| 726 | // SSIDs to wpa_supplicant, we need to explicitly specify the default |
| 727 | // behavior of doing a broadcast probe. |
| 728 | hidden_ssids.push_back(ByteArray()); |
| 729 | } |
| 730 | return hidden_ssids; |
| 731 | } |
| 732 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 733 | bool WiFi::LoadHiddenServices(StoreInterface *storage) { |
| 734 | bool created_hidden_service = false; |
| 735 | set<string> groups = storage->GetGroupsWithKey(flimflam::kWifiHiddenSsid); |
| 736 | for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) { |
| 737 | bool is_hidden = false; |
| 738 | if (!storage->GetBool(*it, flimflam::kWifiHiddenSsid, &is_hidden)) { |
| 739 | VLOG(2) << "Storage group " << *it << " returned by GetGroupsWithKey " |
| 740 | << "failed for GetBool(" << flimflam::kWifiHiddenSsid |
| 741 | << ") -- possible non-bool key"; |
| 742 | continue; |
| 743 | } |
| 744 | if (!is_hidden) { |
| 745 | continue; |
| 746 | } |
| 747 | string ssid_hex; |
| 748 | vector<uint8_t> ssid_bytes; |
| 749 | if (!storage->GetString(*it, flimflam::kSSIDProperty, &ssid_hex) || |
| 750 | !base::HexStringToBytes(ssid_hex, &ssid_bytes)) { |
| 751 | VLOG(2) << "Hidden network is missing/invalid \"" |
| 752 | << flimflam::kSSIDProperty << "\" property"; |
| 753 | continue; |
| 754 | } |
| 755 | string device_address; |
| 756 | string network_mode; |
| 757 | string security; |
| 758 | // It is gross that we have to do this, but the only place we can |
| 759 | // get information about the service is from its storage name. |
| 760 | if (!WiFiService::ParseStorageIdentifier(*it, &device_address, |
| 761 | &network_mode, &security) || |
| 762 | device_address != address()) { |
| 763 | VLOG(2) << "Hidden network has unparsable storage identifier \"" |
| 764 | << *it << "\""; |
| 765 | continue; |
| 766 | } |
| 767 | if (FindService(ssid_bytes, network_mode, security).get()) { |
| 768 | // If service already exists, we have nothing to do, since the |
| 769 | // service has already loaded its configuration from storage. |
| 770 | // This is guaranteed to happen in both cases where Load() is |
| 771 | // called on a device (via a ConfigureDevice() call on the |
| 772 | // profile): |
| 773 | // - In RegisterDevice() the Device hasn't been started yet, |
| 774 | // so it has no services registered, except for those |
| 775 | // created by previous iterations of LoadHiddenService(). |
| 776 | // The latter can happen if another profile in the Manager's |
| 777 | // stack defines the same ssid/mode/security. Even this |
| 778 | // case is okay, since even if the profiles differ |
| 779 | // materially on configuration and credentials, the "right" |
| 780 | // one will be configured in the course of the |
| 781 | // RegisterService() call below. |
| 782 | // - In PushProfile(), all registered services have been |
| 783 | // introduced to the profile via ConfigureService() prior |
| 784 | // to calling ConfigureDevice() on the profile. |
| 785 | continue; |
| 786 | } |
| 787 | WiFiServiceRefPtr service(new WiFiService(control_interface(), |
| 788 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 789 | metrics(), |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 790 | manager(), |
| 791 | this, |
| 792 | ssid_bytes, |
| 793 | network_mode, |
| 794 | security, |
| 795 | true)); |
| 796 | services_.push_back(service); |
| 797 | |
| 798 | // By registering the service, the rest of the configuration |
| 799 | // will be loaded from the profile into the service via ConfigureService(). |
| 800 | manager()->RegisterService(service); |
| 801 | |
| 802 | created_hidden_service = true; |
| 803 | } |
| 804 | |
| 805 | // If we are idle and we created a service as a result of opening the |
| 806 | // profile, we should initiate a scan for our new hidden SSID. |
| 807 | if (running() && created_hidden_service && |
| 808 | supplicant_state_ == wpa_supplicant::kInterfaceStateInactive) { |
| 809 | Scan(NULL); |
| 810 | } |
| 811 | |
| 812 | return created_hidden_service; |
| 813 | } |
| 814 | |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 815 | void WiFi::ClearCachedCredentialsTask() { |
Paul Stewart | 57f3d9d | 2012-04-11 18:21:41 -0700 | [diff] [blame] | 816 | try { |
| 817 | supplicant_interface_proxy_->ClearCachedCredentials(); |
| 818 | } catch (const DBus::Error e) { // NOLINT |
| 819 | LOG(WARNING) << "Clear of cached credentials failed."; |
| 820 | } |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 821 | clear_cached_credentials_pending_ = false; |
| 822 | } |
| 823 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 824 | void WiFi::BSSAddedTask( |
| 825 | const ::DBus::Path &path, |
| 826 | const map<string, ::DBus::Variant> &properties) { |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 827 | // Note: we assume that BSSIDs are unique across endpoints. This |
| 828 | // means that if an AP reuses the same BSSID for multiple SSIDs, we |
| 829 | // lose. |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 830 | WiFiEndpointRefPtr endpoint( |
| 831 | new WiFiEndpoint(proxy_factory_, this, path, properties)); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 832 | LOG(INFO) << "Found endpoint. " |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 833 | << "RPC path: " << path << ", " |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 834 | << "ssid: " << endpoint->ssid_string() << ", " |
| 835 | << "bssid: " << endpoint->bssid_string() << ", " |
| 836 | << "signal: " << endpoint->signal_strength() << ", " |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 837 | << "security: " << endpoint->security_mode() << ", " |
| 838 | << "frequency: " << endpoint->frequency(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 839 | |
| 840 | if (endpoint->ssid_string().empty()) { |
| 841 | // Don't bother trying to find or create a Service for an Endpoint |
| 842 | // without an SSID. We wouldn't be able to connect to it anyway. |
| 843 | return; |
| 844 | } |
| 845 | |
mukesh agrawal | b385761 | 2012-01-18 16:23:29 -0800 | [diff] [blame] | 846 | if (endpoint->ssid()[0] == 0) { |
| 847 | // Assume that an SSID starting with NULL is bogus/misconfigured, |
| 848 | // and filter it out. |
| 849 | return; |
| 850 | } |
| 851 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 852 | WiFiServiceRefPtr service = FindServiceForEndpoint(*endpoint); |
| 853 | if (service) { |
| 854 | LOG(INFO) << "Assigned endpoint " << endpoint->bssid_string() |
| 855 | << " to service " << service->friendly_name() << "."; |
| 856 | service->AddEndpoint(endpoint); |
| 857 | |
| 858 | if (manager()->HasService(service)) { |
| 859 | manager()->UpdateService(service); |
| 860 | } else { |
| 861 | DCHECK_EQ(1, service->NumEndpoints()); // Expect registered by now if >1. |
| 862 | manager()->RegisterService(service); |
| 863 | } |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 864 | } else { |
| 865 | const bool hidden_ssid = false; |
| 866 | service = CreateServiceForEndpoint(*endpoint, hidden_ssid); |
| 867 | LOG(INFO) << "New service " << service->GetRpcIdentifier() |
| 868 | << " (" << service->friendly_name() << ")"; |
| 869 | service->AddEndpoint(endpoint); |
| 870 | manager()->RegisterService(service); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 871 | } |
| 872 | |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 873 | // Do this last, to maintain the invariant that any Endpoint we |
| 874 | // know about has a corresponding Service. |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 875 | // |
| 876 | // TODO(quiche): Write test to verify correct behavior in the case |
| 877 | // where we get multiple BSSAdded events for a single endpoint. |
| 878 | // (Old Endpoint's refcount should fall to zero, and old Endpoint |
| 879 | // should be destroyed.) |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 880 | endpoint_by_rpcid_[path] = endpoint; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 881 | endpoint->Start(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | void WiFi::BSSRemovedTask(const ::DBus::Path &path) { |
| 885 | EndpointMap::iterator i = endpoint_by_rpcid_.find(path); |
| 886 | if (i == endpoint_by_rpcid_.end()) { |
| 887 | LOG(WARNING) << "WiFi " << link_name() |
| 888 | << " could not find BSS " << path |
| 889 | << " to remove."; |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | WiFiEndpointRefPtr endpoint = i->second; |
| 894 | CHECK(endpoint); |
| 895 | endpoint_by_rpcid_.erase(i); |
| 896 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 897 | WiFiServiceRefPtr service = FindServiceForEndpoint(*endpoint); |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 898 | CHECK(service) << "Can't find Service for Endpoint " |
| 899 | << path << " " |
| 900 | << "(with BSSID " << endpoint->bssid_string() << ")."; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 901 | VLOG(2) << "Removing Endpoint " << endpoint->bssid_string() |
| 902 | << " from Service " << service->friendly_name(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 903 | service->RemoveEndpoint(endpoint); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 904 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 905 | bool disconnect_service = !service->HasEndpoints() && |
| 906 | (service->IsConnecting() || service->IsConnected()); |
| 907 | bool forget_service = |
| 908 | // Forget Services without Endpoints, except that we always keep |
| 909 | // hidden services around. (We need them around to populate the |
| 910 | // hidden SSIDs list.) |
| 911 | !service->HasEndpoints() && !service->hidden_ssid(); |
| 912 | bool deregister_service = |
| 913 | // Only deregister a Service if we're forgetting it. Otherwise, |
| 914 | // Manager can't keep our configuration up-to-date (as Profiles |
| 915 | // change). |
| 916 | forget_service; |
| 917 | |
| 918 | if (disconnect_service) { |
| 919 | DisconnectFrom(service); |
| 920 | } |
| 921 | |
| 922 | if (deregister_service) { |
| 923 | manager()->DeregisterService(service); |
| 924 | } else { |
| 925 | manager()->UpdateService(service); |
| 926 | } |
| 927 | |
| 928 | if (forget_service) { |
| 929 | vector<WiFiServiceRefPtr>::iterator it; |
| 930 | it = std::find(services_.begin(), services_.end(), service); |
| 931 | if (it != services_.end()) { |
| 932 | services_.erase(it); |
| 933 | } |
| 934 | } |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 935 | } |
| 936 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 937 | void WiFi::PropertiesChangedTask( |
| 938 | const map<string, ::DBus::Variant> &properties) { |
| 939 | // TODO(quiche): Handle changes in other properties (e.g. signal |
| 940 | // strength). |
| 941 | |
| 942 | // Note that order matters here. In particular, we want to process |
| 943 | // changes in the current BSS before changes in state. This is so |
| 944 | // that we update the state of the correct Endpoint/Service. |
| 945 | |
| 946 | map<string, ::DBus::Variant>::const_iterator properties_it = |
| 947 | properties.find(wpa_supplicant::kInterfacePropertyCurrentBSS); |
| 948 | if (properties_it != properties.end()) { |
| 949 | CurrentBSSChanged(properties_it->second.reader().get_path()); |
| 950 | } |
| 951 | |
| 952 | properties_it = properties.find(wpa_supplicant::kInterfacePropertyState); |
| 953 | if (properties_it != properties.end()) { |
| 954 | StateChanged(properties_it->second.reader().get_string()); |
| 955 | } |
| 956 | } |
| 957 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 958 | void WiFi::ScanDoneTask() { |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 959 | VLOG(2) << __func__ << " need_bss_flush_ " << need_bss_flush_; |
| 960 | if (need_bss_flush_) { |
| 961 | CHECK(supplicant_interface_proxy_ != NULL); |
| 962 | // Compute |max_age| relative to |resumed_at_|, to account for the |
| 963 | // time taken to scan. |
| 964 | struct timeval now; |
| 965 | uint32_t max_age; |
| 966 | time_->GetTimeMonotonic(&now); |
| 967 | max_age = kMaxBSSResumeAgeSeconds + (now.tv_sec - resumed_at_.tv_sec); |
| 968 | supplicant_interface_proxy_->FlushBSS(max_age); |
| 969 | need_bss_flush_ = false; |
| 970 | } |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 971 | scan_pending_ = false; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 972 | } |
| 973 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 974 | void WiFi::ScanTask() { |
| 975 | VLOG(2) << "WiFi " << link_name() << " scan requested."; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 976 | map<string, DBus::Variant> scan_args; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 977 | scan_args[wpa_supplicant::kPropertyScanType].writer(). |
| 978 | append_string(wpa_supplicant::kScanTypeActive); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 979 | |
| 980 | ByteArrays hidden_ssids = GetHiddenSSIDList(); |
| 981 | if (!hidden_ssids.empty()) { |
| 982 | scan_args[wpa_supplicant::kPropertyScanSSIDs] = |
| 983 | DBusAdaptor::ByteArraysToVariant(hidden_ssids); |
| 984 | } |
| 985 | |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 986 | // TODO(quiche): Indicate scanning in UI. crosbug.com/14887 |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 987 | // FIXME(gauravsh): A scan can fail if, for example, wpa_supplicant |
| 988 | // was restarted. This is done by a few of the 802.1x tests. |
| 989 | // crosbug.com/25657 |
| 990 | try { |
| 991 | supplicant_interface_proxy_->Scan(scan_args); |
| 992 | scan_pending_ = true; |
| 993 | } catch (const DBus::Error e) { // NOLINT |
| 994 | LOG(WARNING) << "Scan failed. Attempting to re-connect to the supplicant."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 995 | EnabledStateChangedCallback null_callback; |
| 996 | Stop(NULL, null_callback); |
| 997 | Start(NULL, null_callback); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 998 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1001 | void WiFi::StateChanged(const string &new_state) { |
| 1002 | const string old_state = supplicant_state_; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1003 | supplicant_state_ = new_state; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1004 | LOG(INFO) << "WiFi " << link_name() << " " << __func__ << " " |
| 1005 | << old_state << " -> " << new_state; |
| 1006 | |
| 1007 | WiFiService *affected_service; |
| 1008 | // Identify the service to which the state change applies. If |
| 1009 | // |pending_service_| is non-NULL, then the state change applies to |
| 1010 | // |pending_service_|. Otherwise, it applies to |current_service_|. |
| 1011 | // |
| 1012 | // This policy is driven by the fact that the |pending_service_| |
| 1013 | // doesn't become the |current_service_| until wpa_supplicant |
| 1014 | // reports a CurrentBSS change to the |pending_service_|. And the |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1015 | // CurrentBSS change won't be reported until the |pending_service_| |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1016 | // reaches the wpa_supplicant::kInterfaceStateCompleted state. |
| 1017 | affected_service = |
| 1018 | pending_service_.get() ? pending_service_.get() : current_service_.get(); |
| 1019 | if (!affected_service) { |
| 1020 | VLOG(2) << "WiFi " << link_name() << " " << __func__ |
| 1021 | << " with no service"; |
| 1022 | return; |
| 1023 | } |
| 1024 | |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1025 | if (new_state == wpa_supplicant::kInterfaceStateCompleted && |
| 1026 | !affected_service->IsConnected()) { |
| 1027 | if (AcquireIPConfig()) { |
| 1028 | LOG(INFO) << link_name() << " is up; should start L3!"; |
| 1029 | affected_service->SetState(Service::kStateConfiguring); |
| 1030 | } else { |
| 1031 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 1032 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1033 | } else if (new_state == wpa_supplicant::kInterfaceStateAssociated) { |
| 1034 | affected_service->SetState(Service::kStateAssociating); |
| 1035 | } else if (new_state == wpa_supplicant::kInterfaceStateAuthenticating || |
| 1036 | new_state == wpa_supplicant::kInterfaceStateAssociating || |
| 1037 | new_state == wpa_supplicant::kInterfaceState4WayHandshake || |
| 1038 | new_state == wpa_supplicant::kInterfaceStateGroupHandshake) { |
| 1039 | // Ignore transitions into these states from Completed, to avoid |
| 1040 | // bothering the user when roaming, or re-keying. |
| 1041 | if (old_state != wpa_supplicant::kInterfaceStateCompleted) |
| 1042 | affected_service->SetState(Service::kStateAssociating); |
| 1043 | // TOOD(quiche): On backwards transitions, we should probably set |
| 1044 | // a timeout for getting back into the completed state. At present, |
| 1045 | // we depend on wpa_supplicant eventually reporting that CurrentBSS |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1046 | // 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] | 1047 | // (crosbug.com/23207) |
| 1048 | } else { |
| 1049 | // Other transitions do not affect Service state. |
| 1050 | // |
| 1051 | // Note in particular that we ignore a State change into |
| 1052 | // kInterfaceStateDisconnected, in favor of observing the corresponding |
| 1053 | // change in CurrentBSS. |
| 1054 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // Used by Manager. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1058 | WiFiServiceRefPtr WiFi::GetService(const KeyValueStore &args, Error *error) { |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1059 | CHECK_EQ(args.GetString(flimflam::kTypeProperty), flimflam::kTypeWifi); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1060 | |
| 1061 | if (args.ContainsString(flimflam::kModeProperty) && |
| 1062 | args.GetString(flimflam::kModeProperty) != |
| 1063 | flimflam::kModeManaged) { |
| 1064 | error->Populate(Error::kNotSupported, kManagerErrorUnsupportedServiceMode); |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | |
| 1068 | if (!args.ContainsString(flimflam::kSSIDProperty)) { |
| 1069 | error->Populate(Error::kInvalidArguments, kManagerErrorSSIDRequired); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | |
| 1073 | string ssid = args.GetString(flimflam::kSSIDProperty); |
| 1074 | if (ssid.length() < 1) { |
| 1075 | error->Populate(Error::kInvalidNetworkName, kManagerErrorSSIDTooShort); |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | |
| 1079 | if (ssid.length() > IEEE_80211::kMaxSSIDLen) { |
| 1080 | error->Populate(Error::kInvalidNetworkName, kManagerErrorSSIDTooLong); |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | |
| 1084 | string security_method; |
| 1085 | if (args.ContainsString(flimflam::kSecurityProperty)) { |
| 1086 | security_method = args.GetString(flimflam::kSecurityProperty); |
| 1087 | } else { |
| 1088 | security_method = flimflam::kSecurityNone; |
| 1089 | } |
| 1090 | |
| 1091 | if (security_method != flimflam::kSecurityNone && |
| 1092 | security_method != flimflam::kSecurityWep && |
| 1093 | security_method != flimflam::kSecurityPsk && |
| 1094 | security_method != flimflam::kSecurityWpa && |
| 1095 | security_method != flimflam::kSecurityRsn && |
| 1096 | security_method != flimflam::kSecurity8021x) { |
| 1097 | error->Populate(Error::kNotSupported, |
| 1098 | kManagerErrorUnsupportedSecurityMode); |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
| 1102 | if ((security_method == flimflam::kSecurityWep || |
| 1103 | security_method == flimflam::kSecurityPsk || |
| 1104 | security_method == flimflam::kSecurityWpa || |
| 1105 | security_method == flimflam::kSecurityRsn) && |
| 1106 | !args.ContainsString(flimflam::kPassphraseProperty)) { |
| 1107 | error->Populate(Error::kInvalidArguments, |
| 1108 | kManagerErrorPassphraseRequired); |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1112 | bool hidden_ssid; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1113 | if (args.ContainsBool(flimflam::kWifiHiddenSsid)) { |
| 1114 | hidden_ssid = args.GetBool(flimflam::kWifiHiddenSsid); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1115 | } else { |
| 1116 | // If the service is not found, and the caller hasn't specified otherwise, |
| 1117 | // we assume this is is a hidden network. |
| 1118 | hidden_ssid = true; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1119 | } |
| 1120 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1121 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
| 1122 | WiFiServiceRefPtr service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1123 | security_method)); |
| 1124 | if (!service.get()) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1125 | service = new WiFiService(control_interface(), |
| 1126 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1127 | metrics(), |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1128 | manager(), |
| 1129 | this, |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1130 | ssid_bytes, |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1131 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1132 | security_method, |
| 1133 | hidden_ssid); |
| 1134 | services_.push_back(service); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 1135 | // NB: We do not register the newly created Service with the Manager. |
| 1136 | // The Service will be registered if/when we find Endpoints for it. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1139 | if (security_method == flimflam::kSecurityWep || |
| 1140 | security_method == flimflam::kSecurityPsk || |
| 1141 | security_method == flimflam::kSecurityWpa || |
| 1142 | security_method == flimflam::kSecurityRsn) { |
| 1143 | service->SetPassphrase(args.GetString(flimflam::kPassphraseProperty), |
| 1144 | error); |
| 1145 | if (error->IsFailure()) { |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | } |
| 1149 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 1150 | service->Configure(args, error); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1151 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1152 | // TODO(pstew): Schedule a task to forget up all non-hidden services that |
| 1153 | // have no endpoints like the one we may have just created. crosbug.com/28224 |
| 1154 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1155 | return service; |
| 1156 | } |
| 1157 | |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 1158 | // static |
| 1159 | bool WiFi::SanitizeSSID(string *ssid) { |
| 1160 | CHECK(ssid); |
| 1161 | |
| 1162 | size_t ssid_len = ssid->length(); |
| 1163 | size_t i; |
| 1164 | bool changed = false; |
| 1165 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1166 | for (i = 0; i < ssid_len; ++i) { |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 1167 | if (!g_ascii_isprint((*ssid)[i])) { |
| 1168 | (*ssid)[i] = '?'; |
| 1169 | changed = true; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | return changed; |
| 1174 | } |
| 1175 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1176 | void WiFi::HelpRegisterDerivedInt32( |
| 1177 | PropertyStore *store, |
| 1178 | const string &name, |
| 1179 | int32(WiFi::*get)(Error *error), |
| 1180 | void(WiFi::*set)(const int32 &value, Error *error)) { |
| 1181 | store->RegisterDerivedInt32( |
| 1182 | name, |
| 1183 | Int32Accessor(new CustomAccessor<WiFi, int32>(this, get, set))); |
| 1184 | } |
| 1185 | |
| 1186 | void WiFi::HelpRegisterDerivedString( |
| 1187 | PropertyStore *store, |
| 1188 | const string &name, |
| 1189 | string(WiFi::*get)(Error *error), |
| 1190 | void(WiFi::*set)(const string &value, Error *error)) { |
| 1191 | store->RegisterDerivedString( |
| 1192 | name, |
| 1193 | StringAccessor(new CustomAccessor<WiFi, string>(this, get, set))); |
| 1194 | } |
| 1195 | |
| 1196 | void WiFi::HelpRegisterDerivedUint16( |
| 1197 | PropertyStore *store, |
| 1198 | const string &name, |
| 1199 | uint16(WiFi::*get)(Error *error), |
| 1200 | void(WiFi::*set)(const uint16 &value, Error *error)) { |
| 1201 | store->RegisterDerivedUint16( |
| 1202 | name, |
| 1203 | Uint16Accessor(new CustomAccessor<WiFi, uint16>(this, get, set))); |
| 1204 | } |
| 1205 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1206 | void WiFi::HandlePowerStateChange(PowerManager::SuspendState new_state) { |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 1207 | LOG(INFO) << __func__; |
| 1208 | if (new_state == PowerManagerProxyDelegate::kOn) { |
| 1209 | // We want to flush the BSS cache, but we don't want to conflict |
| 1210 | // with a running scan or an active connection attempt. So record |
| 1211 | // the need to flush, and take care of flushing when the next scan |
| 1212 | // completes. |
| 1213 | // |
| 1214 | // Note that supplicant will automatically expire old cache |
| 1215 | // entries (after, e.g., a BSS is not found in two consecutive |
| 1216 | // scans). However, our explicit flush accelerates re-association |
| 1217 | // in cases where a BSS disappeared while we were asleep. (See, |
| 1218 | // e.g. WiFiRoaming.005SuspendRoam.) |
| 1219 | time_->GetTimeMonotonic(&resumed_at_); |
| 1220 | need_bss_flush_ = true; |
| 1221 | |
| 1222 | if (!scan_pending_ && IsIdle()) { |
| 1223 | // Not scanning/connecting/connected, so let's get things rolling. |
| 1224 | Scan(NULL); |
| 1225 | } |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 1229 | } // namespace shill |