mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -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 | |
| 5 | #include "shill/wifi_service.h" |
| 6 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 7 | #include <algorithm> |
Wade Guthrie | 9ec0806 | 2013-09-25 15:22:24 -0700 | [diff] [blame] | 8 | #include <limits> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 9 | #include <string> |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 10 | #include <utility> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 11 | |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 12 | #include <base/strings/stringprintf.h> |
| 13 | #include <base/strings/string_number_conversions.h> |
| 14 | #include <base/strings/string_split.h> |
| 15 | #include <base/strings/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 17 | #include <dbus/dbus.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 18 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 19 | #include "shill/adaptor_interfaces.h" |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 20 | #include "shill/certificate_file.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 21 | #include "shill/control_interface.h" |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 22 | #include "shill/dbus_adaptor.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 23 | #include "shill/device.h" |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 24 | #include "shill/eap_credentials.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 25 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 26 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 27 | #include "shill/ieee80211.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 28 | #include "shill/logging.h" |
Paul Stewart | 4357f4e | 2012-04-26 17:39:26 -0700 | [diff] [blame] | 29 | #include "shill/manager.h" |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 30 | #include "shill/metrics.h" |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 31 | #include "shill/nss.h" |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 32 | #include "shill/property_accessor.h" |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 33 | #include "shill/store_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 34 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 35 | #include "shill/wifi_endpoint.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 36 | #include "shill/wifi_provider.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 37 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 38 | |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 39 | using std::map; |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 40 | using std::set; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 41 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 42 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 43 | |
| 44 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 45 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 46 | const char WiFiService::kAutoConnNoEndpoint[] = "no endpoints"; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 47 | const char WiFiService::kAnyDeviceAddress[] = "any"; |
Paul Stewart | bca08f8 | 2013-07-09 16:32:37 -0700 | [diff] [blame] | 48 | const int WiFiService::kSuspectedCredentialFailureThreshold = 3; |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 49 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 50 | const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID"; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 51 | const char WiFiService::kStorageMode[] = "WiFi.Mode"; |
| 52 | const char WiFiService::kStoragePassphrase[] = "Passphrase"; |
| 53 | const char WiFiService::kStorageSecurity[] = "WiFi.Security"; |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 54 | const char WiFiService::kStorageSecurityClass[] = "WiFi.SecurityClass"; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 55 | const char WiFiService::kStorageSSID[] = "SSID"; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 56 | bool WiFiService::logged_signal_warning = false; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 57 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 58 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 59 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 60 | Metrics *metrics, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 61 | Manager *manager, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 62 | WiFiProvider *provider, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 63 | const vector<uint8_t> &ssid, |
| 64 | const string &mode, |
| 65 | const string &security, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 66 | bool hidden_ssid) |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 67 | : Service(control_interface, dispatcher, metrics, manager, |
| 68 | Technology::kWifi), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame] | 69 | need_passphrase_(false), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 70 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 71 | mode_(mode), |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 72 | hidden_ssid_(hidden_ssid), |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 73 | frequency_(0), |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 74 | physical_mode_(Metrics::kWiFiNetworkPhyModeUndef), |
Paul Stewart | 23b393a | 2012-09-25 21:21:06 -0700 | [diff] [blame] | 75 | raw_signal_strength_(0), |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 76 | cipher_8021x_(kCryptoNone), |
Paul Stewart | bca08f8 | 2013-07-09 16:32:37 -0700 | [diff] [blame] | 77 | suspected_credential_failures_(0), |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 78 | ssid_(ssid), |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 79 | ieee80211w_required_(false), |
Peter Qiu | 4943822 | 2014-03-14 14:21:09 -0700 | [diff] [blame] | 80 | expecting_disconnect_(false), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 81 | nss_(NSS::GetInstance()), |
Paul Stewart | eb713e8 | 2013-06-28 14:51:54 -0700 | [diff] [blame] | 82 | certificate_file_(new CertificateFile()), |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 83 | provider_(provider) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 84 | PropertyStore *store = this->mutable_store(); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 85 | store->RegisterConstString(kModeProperty, &mode_); |
| 86 | HelpRegisterWriteOnlyDerivedString(kPassphraseProperty, |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 87 | &WiFiService::SetPassphrase, |
| 88 | &WiFiService::ClearPassphrase, |
| 89 | NULL); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 90 | store->RegisterBool(kPassphraseRequiredProperty, &need_passphrase_); |
| 91 | HelpRegisterDerivedString(kSecurityProperty, |
Paul Stewart | 6df20bd | 2013-03-13 19:31:25 -0700 | [diff] [blame] | 92 | &WiFiService::GetSecurity, |
| 93 | NULL); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 94 | |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 95 | store->RegisterConstString(kWifiAuthMode, &auth_mode_); |
| 96 | store->RegisterBool(kWifiHiddenSsid, &hidden_ssid_); |
| 97 | store->RegisterConstUint16(kWifiFrequency, &frequency_); |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 98 | store->RegisterConstUint16s(kWifiFrequencyListProperty, &frequency_list_); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 99 | store->RegisterConstUint16(kWifiPhyMode, &physical_mode_); |
| 100 | store->RegisterConstString(kWifiBSsid, &bssid_); |
| 101 | store->RegisterConstString(kCountryProperty, &country_code_); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 102 | store->RegisterConstStringmap(kWifiVendorInformationProperty, |
| 103 | &vendor_information_); |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 104 | store->RegisterConstBool(kWifiProtectedManagementFrameRequiredProperty, |
| 105 | &ieee80211w_required_); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 106 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 107 | hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size()); |
Paul Stewart | 9d95dad | 2013-10-10 12:08:14 -0700 | [diff] [blame] | 108 | store->RegisterConstString(kWifiHexSsid, &hex_ssid_); |
| 109 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 110 | string ssid_string( |
| 111 | reinterpret_cast<const char *>(ssid_.data()), ssid_.size()); |
Paul Stewart | 9d95dad | 2013-10-10 12:08:14 -0700 | [diff] [blame] | 112 | WiFi::SanitizeSSID(&ssid_string); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 113 | set_friendly_name(ssid_string); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 114 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 115 | SetEapCredentials(new EapCredentials()); |
| 116 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 117 | // TODO(quiche): determine if it is okay to set EAP.KeyManagement for |
| 118 | // a service that is not 802.1x. |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 119 | if (Is8021x()) { |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 120 | // Passphrases are not mandatory for 802.1X. |
| 121 | need_passphrase_ = false; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 122 | } else if (security_ == kSecurityPsk) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 123 | SetEAPKeyManagement("WPA-PSK"); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 124 | } else if (security_ == kSecurityRsn) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 125 | SetEAPKeyManagement("WPA-PSK"); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 126 | } else if (security_ == kSecurityWpa) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 127 | SetEAPKeyManagement("WPA-PSK"); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 128 | } else if (security_ == kSecurityWep) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 129 | SetEAPKeyManagement("NONE"); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 130 | } else if (security_ == kSecurityNone) { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 131 | SetEAPKeyManagement("NONE"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 132 | } else { |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 133 | LOG(ERROR) << "Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 136 | // Until we know better (at Profile load time), use the generic name. |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 137 | storage_identifier_ = GetDefaultStorageIdentifier(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 138 | UpdateConnectable(); |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 139 | UpdateSecurity(); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 140 | |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 141 | IgnoreParameterForConfigure(kModeProperty); |
| 142 | IgnoreParameterForConfigure(kSSIDProperty); |
| 143 | IgnoreParameterForConfigure(kSecurityProperty); |
Paul Stewart | 4539d26 | 2013-10-10 12:56:31 -0700 | [diff] [blame] | 144 | IgnoreParameterForConfigure(kWifiHexSsid); |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 145 | |
mukesh agrawal | 6cfe53f | 2013-08-13 13:39:01 -0700 | [diff] [blame] | 146 | InitializeCustomMetrics(); |
| 147 | |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 148 | // Log the |unique_name| to |friendly_name| mapping for debugging purposes. |
| 149 | // The latter will be tagged for scrubbing. |
| 150 | LOG(INFO) << "Constructed WiFi service " << unique_name() |
| 151 | << " name: " << WiFi::LogSSID(friendly_name()); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Darin Petkov | 9cd7ca1 | 2012-07-03 11:06:40 +0200 | [diff] [blame] | 154 | WiFiService::~WiFiService() {} |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 155 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 156 | bool WiFiService::IsAutoConnectable(const char **reason) const { |
| 157 | if (!Service::IsAutoConnectable(reason)) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | // Only auto-connect to Services which have visible Endpoints. |
| 162 | // (Needed because hidden Services may remain registered with |
| 163 | // Manager even without visible Endpoints.) |
| 164 | if (!HasEndpoints()) { |
| 165 | *reason = kAutoConnNoEndpoint; |
| 166 | return false; |
| 167 | } |
| 168 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 169 | CHECK(wifi_) << "We have endpoints but no WiFi device is selected?"; |
| 170 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 171 | // Do not preempt an existing connection (whether pending, or |
| 172 | // connected, and whether to this service, or another). |
| 173 | if (!wifi_->IsIdle()) { |
| 174 | *reason = kAutoConnBusy; |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | return true; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 179 | } |
| 180 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 181 | void WiFiService::SetEAPKeyManagement(const string &key_management) { |
| 182 | Service::SetEAPKeyManagement(key_management); |
| 183 | UpdateSecurity(); |
| 184 | } |
| 185 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 186 | void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr &endpoint) { |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 187 | DCHECK(endpoint->ssid() == ssid()); |
| 188 | endpoints_.insert(endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 189 | UpdateFromEndpoints(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 192 | void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint) { |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 193 | set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint); |
| 194 | DCHECK(i != endpoints_.end()); |
| 195 | if (i == endpoints_.end()) { |
| 196 | LOG(WARNING) << "In " << __func__ << "(): " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 197 | << "ignoring non-existent endpoint " |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 198 | << endpoint->bssid_string(); |
| 199 | return; |
| 200 | } |
| 201 | endpoints_.erase(i); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 202 | if (current_endpoint_ == endpoint) { |
| 203 | current_endpoint_ = NULL; |
| 204 | } |
| 205 | UpdateFromEndpoints(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 208 | void WiFiService::NotifyCurrentEndpoint( |
| 209 | const WiFiEndpointConstRefPtr &endpoint) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 210 | DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end())); |
| 211 | current_endpoint_ = endpoint; |
| 212 | UpdateFromEndpoints(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 215 | void WiFiService::NotifyEndpointUpdated( |
| 216 | const WiFiEndpointConstRefPtr &endpoint) { |
| 217 | DCHECK(endpoints_.find(endpoint) != endpoints_.end()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 218 | UpdateFromEndpoints(); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 221 | string WiFiService::GetStorageIdentifier() const { |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 222 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 223 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 224 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 225 | bool WiFiService::SetPassphrase(const string &passphrase, Error *error) { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 226 | if (security_ == kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 227 | ValidateWEPPassphrase(passphrase, error); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 228 | } else if (security_ == kSecurityPsk || |
| 229 | security_ == kSecurityWpa || |
| 230 | security_ == kSecurityRsn) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 231 | ValidateWPAPassphrase(passphrase, error); |
| 232 | } else { |
| 233 | error->Populate(Error::kNotSupported); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 234 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 235 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 236 | if (!error->IsSuccess()) { |
| 237 | return false; |
| 238 | } |
| 239 | if (passphrase_ == passphrase) { |
| 240 | // After a user logs in, Chrome may reconfigure a Service with the |
| 241 | // same credentials as before login. When that occurs, we don't |
| 242 | // want to bump the user off the network. Hence, we MUST return |
| 243 | // early. (See crbug.com/231456#c17) |
| 244 | return false; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 245 | } |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 246 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 247 | passphrase_ = passphrase; |
| 248 | ClearCachedCredentials(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 249 | UpdateConnectable(); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 250 | return true; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 251 | } |
| 252 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 253 | // ClearPassphrase is separate from SetPassphrase, because the default |
| 254 | // value for |passphrase_| would not pass validation. |
| 255 | void WiFiService::ClearPassphrase(Error */*error*/) { |
| 256 | passphrase_.clear(); |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 257 | ClearCachedCredentials(); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 258 | UpdateConnectable(); |
| 259 | } |
| 260 | |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 261 | string WiFiService::GetTethering(Error */*error*/) const { |
| 262 | if (IsConnected() && wifi_ && wifi_->IsConnectedViaTether()) { |
| 263 | return kTetheringConfirmedState; |
| 264 | } |
| 265 | |
| 266 | // Only perform BSSID tests if there is exactly one matching endpoint, |
| 267 | // so we ignore campuses that may use locally administered BSSIDs. |
Paul Stewart | 7f5d9c0 | 2013-12-03 18:26:00 -0800 | [diff] [blame] | 268 | if (endpoints_.size() == 1 && |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 269 | (*endpoints_.begin())->has_tethering_signature()) { |
| 270 | return kTetheringSuspectedState; |
| 271 | } |
| 272 | |
| 273 | return kTetheringNotDetectedState; |
| 274 | } |
| 275 | |
Paul Stewart | e7de294 | 2013-04-25 17:07:31 -0700 | [diff] [blame] | 276 | string WiFiService::GetLoadableStorageIdentifier( |
| 277 | const StoreInterface &storage) const { |
| 278 | set<string> groups = storage.GetGroupsWithProperties(GetStorageProperties()); |
| 279 | if (groups.empty()) { |
| 280 | LOG(WARNING) << "Configuration for service " |
| 281 | << unique_name() |
| 282 | << " is not available in the persistent store"; |
| 283 | return ""; |
| 284 | } |
| 285 | if (groups.size() > 1) { |
| 286 | LOG(WARNING) << "More than one configuration for service " |
| 287 | << unique_name() |
| 288 | << " is available; choosing the first."; |
| 289 | } |
| 290 | return *groups.begin(); |
| 291 | } |
| 292 | |
| 293 | bool WiFiService::IsLoadableFrom(const StoreInterface &storage) const { |
| 294 | return !storage.GetGroupsWithProperties(GetStorageProperties()).empty(); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 297 | bool WiFiService::IsVisible() const { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 298 | // WiFi Services should be displayed only if they are in range (have |
| 299 | // endpoints that have shown up in a scan) or if the service is actively |
| 300 | // being connected. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 301 | return HasEndpoints() || IsConnected() || IsConnecting(); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 304 | bool WiFiService::Load(StoreInterface *storage) { |
Paul Stewart | e7de294 | 2013-04-25 17:07:31 -0700 | [diff] [blame] | 305 | string id = GetLoadableStorageIdentifier(*storage); |
| 306 | if (id.empty()) { |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 307 | return false; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Set our storage identifier to match the storage name in the Profile. |
| 311 | storage_identifier_ = id; |
| 312 | |
| 313 | // Load properties common to all Services. |
| 314 | if (!Service::Load(storage)) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | // Load properties specific to WiFi services. |
| 319 | storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 320 | |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 321 | // NB: mode, security and ssid parameters are never read in from |
| 322 | // Load() as they are provided from the scan. |
| 323 | |
| 324 | string passphrase; |
| 325 | if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) { |
| 326 | Error error; |
| 327 | SetPassphrase(passphrase, &error); |
Paul Stewart | fa013ab | 2013-04-11 07:12:03 -0700 | [diff] [blame] | 328 | if (!error.IsSuccess() && |
| 329 | !(passphrase.empty() && error.type() == Error::kNotSupported)) { |
Paul Stewart | 71b9ed5 | 2014-01-29 08:53:06 -0800 | [diff] [blame] | 330 | LOG(ERROR) << "Passphrase could not be set: " << error; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Peter Qiu | 4943822 | 2014-03-14 14:21:09 -0700 | [diff] [blame] | 334 | expecting_disconnect_ = false; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | |
| 338 | bool WiFiService::Save(StoreInterface *storage) { |
| 339 | // Save properties common to all Services. |
| 340 | if (!Service::Save(storage)) { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | // Save properties specific to WiFi services. |
| 345 | const string id = GetStorageIdentifier(); |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 346 | storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_); |
| 347 | storage->SetString(id, kStorageMode, mode_); |
| 348 | storage->SetCryptedString(id, kStoragePassphrase, passphrase_); |
| 349 | storage->SetString(id, kStorageSecurity, security_); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 350 | storage->SetString(id, kStorageSecurityClass, GetSecurityClass(security_)); |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 351 | storage->SetString(id, kStorageSSID, hex_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 352 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 356 | bool WiFiService::Unload() { |
Peter Qiu | 91b461f | 2014-03-26 10:19:45 -0700 | [diff] [blame] | 357 | // Expect the service to be disconnected if is currently connected or |
| 358 | // in the process of connecting. |
| 359 | if (IsConnected() || IsConnecting()) { |
| 360 | expecting_disconnect_ = true; |
| 361 | } else { |
| 362 | expecting_disconnect_ = false; |
| 363 | } |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 364 | Service::Unload(); |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 365 | if (wifi_) { |
| 366 | wifi_->DestroyServiceLease(*this); |
| 367 | } |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 368 | hidden_ssid_ = false; |
Paul Stewart | bca08f8 | 2013-07-09 16:32:37 -0700 | [diff] [blame] | 369 | ResetSuspectedCredentialFailures(); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 370 | Error unused_error; |
| 371 | ClearPassphrase(&unused_error); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 372 | return provider_->OnServiceUnloaded(this); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 375 | bool WiFiService::IsSecurityMatch(const string &security) const { |
| 376 | return GetSecurityClass(security) == GetSecurityClass(security_); |
| 377 | } |
| 378 | |
Paul Stewart | bca08f8 | 2013-07-09 16:32:37 -0700 | [diff] [blame] | 379 | bool WiFiService::AddSuspectedCredentialFailure() { |
| 380 | if (!has_ever_connected()) { |
| 381 | return true; |
| 382 | } |
| 383 | ++suspected_credential_failures_; |
| 384 | return suspected_credential_failures_ >= kSuspectedCredentialFailureThreshold; |
| 385 | } |
| 386 | |
| 387 | void WiFiService::ResetSuspectedCredentialFailures() { |
| 388 | suspected_credential_failures_ = 0; |
| 389 | } |
| 390 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 391 | void WiFiService::InitializeCustomMetrics() const { |
mukesh agrawal | 6cfe53f | 2013-08-13 13:39:01 -0700 | [diff] [blame] | 392 | SLOG(Metrics, 2) << __func__ << " for " << unique_name(); |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 393 | string histogram = metrics()->GetFullMetricName( |
mukesh agrawal | 6cfe53f | 2013-08-13 13:39:01 -0700 | [diff] [blame] | 394 | Metrics::kMetricTimeToJoinMilliseconds, |
| 395 | technology()); |
Wade Guthrie | 7ac610b | 2013-10-01 17:48:14 -0700 | [diff] [blame] | 396 | metrics()->AddServiceStateTransitionTimer(*this, |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 397 | histogram, |
| 398 | Service::kStateAssociating, |
| 399 | Service::kStateConfiguring); |
| 400 | } |
| 401 | |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 402 | void WiFiService::SendPostReadyStateMetrics( |
| 403 | int64 time_resume_to_ready_milliseconds) const { |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 404 | metrics()->SendEnumToUMA( |
| 405 | metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel, |
| 406 | technology()), |
| 407 | Metrics::WiFiFrequencyToChannel(frequency_), |
| 408 | Metrics::kMetricNetworkChannelMax); |
Thieu Le | ad1ec2c | 2012-01-05 23:39:48 +0000 | [diff] [blame] | 409 | |
| 410 | DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax); |
| 411 | metrics()->SendEnumToUMA( |
| 412 | metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode, |
| 413 | technology()), |
| 414 | static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_), |
| 415 | Metrics::kWiFiNetworkPhyModeMax); |
| 416 | |
Paul Stewart | 4108db9 | 2013-03-11 12:13:24 -0700 | [diff] [blame] | 417 | string security_mode = security_; |
| 418 | if (current_endpoint_) { |
| 419 | security_mode = current_endpoint_->security_mode(); |
| 420 | } |
Thieu Le | ad1ec2c | 2012-01-05 23:39:48 +0000 | [diff] [blame] | 421 | Metrics::WiFiSecurity security_uma = |
Paul Stewart | 4108db9 | 2013-03-11 12:13:24 -0700 | [diff] [blame] | 422 | Metrics::WiFiSecurityStringToEnum(security_mode); |
Thieu Le | ad1ec2c | 2012-01-05 23:39:48 +0000 | [diff] [blame] | 423 | DCHECK(security_uma != Metrics::kWiFiSecurityUnknown); |
| 424 | metrics()->SendEnumToUMA( |
| 425 | metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity, |
| 426 | technology()), |
| 427 | security_uma, |
| 428 | Metrics::kMetricNetworkSecurityMax); |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 429 | |
Paul Stewart | 21f4096 | 2013-03-01 14:27:28 -0800 | [diff] [blame] | 430 | if (Is8021x()) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 431 | eap()->OutputConnectionMetrics(metrics(), technology()); |
Paul Stewart | 21f4096 | 2013-03-01 14:27:28 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Paul Stewart | 23b393a | 2012-09-25 21:21:06 -0700 | [diff] [blame] | 434 | // We invert the sign of the signal strength value, since UMA histograms |
| 435 | // cannot represent negative numbers (it stores them but cannot display |
| 436 | // them), and dBm values of interest start at 0 and go negative from there. |
| 437 | metrics()->SendToUMA( |
| 438 | metrics()->GetFullMetricName(Metrics::kMetricNetworkSignalStrength, |
| 439 | technology()), |
| 440 | -raw_signal_strength_, |
| 441 | Metrics::kMetricNetworkSignalStrengthMin, |
| 442 | Metrics::kMetricNetworkSignalStrengthMax, |
| 443 | Metrics::kMetricNetworkSignalStrengthNumBuckets); |
| 444 | |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 445 | if (time_resume_to_ready_milliseconds > 0) { |
| 446 | metrics()->SendToUMA( |
| 447 | metrics()->GetFullMetricName( |
| 448 | Metrics::kMetricTimeResumeToReadyMilliseconds, technology()), |
| 449 | time_resume_to_ready_milliseconds, |
| 450 | Metrics::kTimerHistogramMillisecondsMin, |
| 451 | Metrics::kTimerHistogramMillisecondsMax, |
| 452 | Metrics::kTimerHistogramNumBuckets); |
| 453 | } |
Paul Stewart | e4cedde | 2013-07-17 08:56:44 -0700 | [diff] [blame] | 454 | |
| 455 | Metrics::WiFiApMode ap_mode_uma = Metrics::WiFiApModeStringToEnum(mode_); |
| 456 | metrics()->SendEnumToUMA( |
| 457 | metrics()->GetFullMetricName(Metrics::kMetricNetworkApMode, technology()), |
| 458 | ap_mode_uma, |
| 459 | Metrics::kWiFiApModeMax); |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 460 | } |
| 461 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 462 | // private methods |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 463 | void WiFiService::HelpRegisterConstDerivedString( |
| 464 | const string &name, |
| 465 | string(WiFiService::*get)(Error *)) { |
| 466 | mutable_store()->RegisterDerivedString( |
| 467 | name, |
| 468 | StringAccessor( |
| 469 | new CustomAccessor<WiFiService, string>(this, get, NULL))); |
| 470 | } |
| 471 | |
Paul Stewart | 6df20bd | 2013-03-13 19:31:25 -0700 | [diff] [blame] | 472 | void WiFiService::HelpRegisterDerivedString( |
| 473 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 474 | string(WiFiService::*get)(Error *error), |
| 475 | bool(WiFiService::*set)(const string &, Error *)) { |
Paul Stewart | 6df20bd | 2013-03-13 19:31:25 -0700 | [diff] [blame] | 476 | mutable_store()->RegisterDerivedString( |
| 477 | name, |
| 478 | StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set))); |
| 479 | } |
| 480 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 481 | void WiFiService::HelpRegisterWriteOnlyDerivedString( |
| 482 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 483 | bool(WiFiService::*set)(const string &, Error *), |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 484 | void(WiFiService::*clear)(Error *), |
| 485 | const string *default_value) { |
| 486 | mutable_store()->RegisterDerivedString( |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 487 | name, |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 488 | StringAccessor( |
| 489 | new CustomWriteOnlyAccessor<WiFiService, string>( |
| 490 | this, set, clear, default_value))); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 491 | } |
| 492 | |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 493 | void WiFiService::Connect(Error *error, const char *reason) { |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 494 | if (!connectable()) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 495 | LOG(ERROR) << "Can't connect. Service " << unique_name() |
| 496 | << " is not connectable."; |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 497 | Error::PopulateAndLog(error, |
| 498 | Error::kOperationFailed, |
| 499 | Error::GetDefaultMessage(Error::kOperationFailed)); |
| 500 | return; |
| 501 | } |
| 502 | if (IsConnecting() || IsConnected()) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 503 | LOG(WARNING) << "Can't connect. Service " << unique_name() |
Christopher Wiley | 1ce658d | 2012-10-10 10:02:03 -0700 | [diff] [blame] | 504 | << " is already connecting or connected."; |
| 505 | Error::PopulateAndLog(error, |
| 506 | Error::kAlreadyConnected, |
| 507 | Error::GetDefaultMessage(Error::kAlreadyConnected)); |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 508 | return; |
| 509 | } |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 510 | |
| 511 | WiFiRefPtr wifi = wifi_; |
| 512 | if (!wifi) { |
| 513 | // If this is a hidden service before it has been found in a scan, we |
| 514 | // may need to late-bind to any available WiFi Device. We don't actually |
Wade Guthrie | 9ec0806 | 2013-09-25 15:22:24 -0700 | [diff] [blame] | 515 | // set |wifi_| in this case since we do not yet see any endpoints. This |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 516 | // will mean this service is not disconnectable until an endpoint is |
| 517 | // found. |
| 518 | wifi = ChooseDevice(); |
| 519 | if (!wifi) { |
| 520 | LOG(ERROR) << "Can't connect. Service " << unique_name() |
| 521 | << " cannot find a WiFi device."; |
| 522 | Error::PopulateAndLog(error, |
| 523 | Error::kOperationFailed, |
| 524 | Error::GetDefaultMessage(Error::kOperationFailed)); |
| 525 | return; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (wifi->IsCurrentService(this)) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 530 | LOG(WARNING) << "Can't connect. Service " << unique_name() |
Wade Guthrie | 8bc5088 | 2012-10-31 16:23:20 -0700 | [diff] [blame] | 531 | << " is the current service (but, in " << GetStateString() |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 532 | << " state, not connected)."; |
Wade Guthrie | 8bc5088 | 2012-10-31 16:23:20 -0700 | [diff] [blame] | 533 | Error::PopulateAndLog(error, |
| 534 | Error::kInProgress, |
| 535 | Error::GetDefaultMessage(Error::kInProgress)); |
| 536 | return; |
| 537 | } |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 538 | |
Peter Qiu | 574996a | 2014-04-04 10:55:47 -0700 | [diff] [blame^] | 539 | // Report number of BSSes available for this service. |
| 540 | metrics()->NotifyWifiAvailableBSSes(endpoints_.size()); |
| 541 | |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 542 | if (Is8021x()) { |
| 543 | // If EAP key management is not set, set to a default. |
| 544 | if (GetEAPKeyManagement().empty()) |
| 545 | SetEAPKeyManagement("WPA-EAP"); |
| 546 | ClearEAPCertification(); |
| 547 | } |
| 548 | |
Peter Qiu | 4943822 | 2014-03-14 14:21:09 -0700 | [diff] [blame] | 549 | expecting_disconnect_ = false; |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 550 | Service::Connect(error, reason); |
| 551 | wifi->ConnectTo(this); |
| 552 | } |
| 553 | |
| 554 | DBusPropertiesMap WiFiService::GetSupplicantConfigurationParameters() const { |
| 555 | DBusPropertiesMap params; |
| 556 | DBus::MessageIter writer; |
| 557 | |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 558 | params[WPASupplicant::kNetworkPropertyMode].writer(). |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 559 | append_uint32(WiFiEndpoint::ModeStringToUint(mode_)); |
| 560 | |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 561 | if (mode_ == kModeAdhoc && frequency_ != 0) { |
Wade Guthrie | 9ec0806 | 2013-09-25 15:22:24 -0700 | [diff] [blame] | 562 | // Frequency is required in order to successfully connect to an IBSS |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 563 | // with wpa_supplicant. If we have one from our endpoint, insert it |
| 564 | // here. |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 565 | params[WPASupplicant::kNetworkPropertyFrequency].writer(). |
Paul Stewart | e2d7c50 | 2012-07-16 16:35:10 -0700 | [diff] [blame] | 566 | append_int32(frequency_); |
| 567 | } |
| 568 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 569 | if (Is8021x()) { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 570 | vector<char> nss_identifier(ssid_.begin(), ssid_.end()); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 571 | eap()->PopulateSupplicantProperties( |
| 572 | certificate_file_.get(), nss_, nss_identifier, ¶ms); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 573 | } else if (security_ == kSecurityPsk || |
| 574 | security_ == kSecurityRsn || |
| 575 | security_ == kSecurityWpa) { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 576 | const string psk_proto = |
| 577 | base::StringPrintf("%s %s", |
| 578 | WPASupplicant::kSecurityModeWPA, |
| 579 | WPASupplicant::kSecurityModeRSN); |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 580 | params[WPASupplicant::kPropertySecurityProtocol].writer(). |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 581 | append_string(psk_proto.c_str()); |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 582 | params[WPASupplicant::kPropertyPreSharedKey].writer(). |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 583 | append_string(passphrase_.c_str()); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 584 | } else if (security_ == kSecurityWep) { |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 585 | params[WPASupplicant::kPropertyAuthAlg].writer(). |
| 586 | append_string(WPASupplicant::kSecurityAuthAlg); |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 587 | Error unused_error; |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 588 | int key_index; |
| 589 | std::vector<uint8> password_bytes; |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 590 | ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &unused_error); |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 591 | writer = params[WPASupplicant::kPropertyWEPKey + |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 592 | base::IntToString(key_index)].writer(); |
| 593 | writer << password_bytes; |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 594 | params[WPASupplicant::kPropertyWEPTxKeyIndex].writer(). |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 595 | append_uint32(key_index); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 596 | } else if (security_ == kSecurityNone) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 597 | // Nothing special to do here. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 598 | } else { |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 599 | NOTIMPLEMENTED() << "Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 602 | params[WPASupplicant::kNetworkPropertyEapKeyManagement].writer(). |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 603 | append_string(key_management().c_str()); |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 604 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 605 | if (ieee80211w_required_) { |
| 606 | // TODO(pstew): We should also enable IEEE 802.11w if the user |
| 607 | // explicitly enables support for this through a service / device |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 608 | // property. crbug.com/219950 |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 609 | params[WPASupplicant::kNetworkPropertyIeee80211w].writer(). |
| 610 | append_uint32(WPASupplicant::kNetworkIeee80211wEnabled); |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 613 | // See note in dbus_adaptor.cc on why we need to use a local. |
Paul Stewart | 0654ece | 2013-03-26 15:21:26 -0700 | [diff] [blame] | 614 | writer = params[WPASupplicant::kNetworkPropertySSID].writer(); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 615 | writer << ssid_; |
| 616 | |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 617 | return params; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Paul Stewart | a283e4e | 2013-10-22 20:50:14 -0700 | [diff] [blame] | 620 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 621 | void WiFiService::Disconnect(Error *error) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 622 | Service::Disconnect(error); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 623 | if (!wifi_) { |
| 624 | // If we are connecting to a hidden service, but have not yet found |
| 625 | // any endpoints, we could end up with a disconnect request without |
| 626 | // a wifi_ reference. This is not a fatal error. |
| 627 | LOG_IF(ERROR, IsConnecting()) |
| 628 | << "WiFi endpoints do not (yet) exist. Cannot disconnect service " |
| 629 | << unique_name(); |
| 630 | LOG_IF(FATAL, IsConnected()) |
| 631 | << "WiFi device does not exist. Cannot disconnect service " |
| 632 | << unique_name(); |
| 633 | error->Populate(Error::kOperationFailed); |
| 634 | return; |
| 635 | } |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 636 | wifi_->DisconnectFrom(this); |
| 637 | } |
| 638 | |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 639 | string WiFiService::GetDeviceRpcId(Error *error) const { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 640 | if (!wifi_) { |
| 641 | error->Populate(Error::kNotFound, "Not associated with a device"); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 642 | return DBusAdaptor::kNullPath; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 643 | } |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 644 | return wifi_->GetRpcIdentifier(); |
| 645 | } |
| 646 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 647 | void WiFiService::UpdateConnectable() { |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 648 | bool is_connectable = false; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 649 | if (security_ == kSecurityNone) { |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 650 | DCHECK(passphrase_.empty()); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 651 | need_passphrase_ = false; |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 652 | is_connectable = true; |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 653 | } else if (Is8021x()) { |
| 654 | is_connectable = Is8021xConnectable(); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 655 | } else if (security_ == kSecurityWep || |
| 656 | security_ == kSecurityWpa || |
| 657 | security_ == kSecurityPsk || |
| 658 | security_ == kSecurityRsn) { |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 659 | need_passphrase_ = passphrase_.empty(); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 660 | is_connectable = !need_passphrase_; |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 661 | } |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 662 | SetConnectable(is_connectable); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 663 | } |
| 664 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 665 | void WiFiService::UpdateFromEndpoints() { |
| 666 | const WiFiEndpoint *representative_endpoint = NULL; |
| 667 | |
| 668 | if (current_endpoint_) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 669 | representative_endpoint = current_endpoint_; |
| 670 | } else { |
| 671 | int16 best_signal = std::numeric_limits<int16>::min(); |
| 672 | for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin(); |
| 673 | i != endpoints_.end(); ++i) { |
| 674 | if ((*i)->signal_strength() >= best_signal) { |
| 675 | best_signal = (*i)->signal_strength(); |
| 676 | representative_endpoint = *i; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 681 | WiFiRefPtr wifi; |
| 682 | if (representative_endpoint) { |
| 683 | wifi = representative_endpoint->device(); |
Paul Stewart | 8653f46 | 2013-02-06 12:21:05 -0800 | [diff] [blame] | 684 | } else if (IsConnected() || IsConnecting()) { |
| 685 | LOG(WARNING) << "Service " << unique_name() |
| 686 | << " will disconnect due to no remaining endpoints."; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | SetWiFi(wifi); |
| 690 | |
Paul Stewart | a5e7d5f | 2013-01-09 18:06:15 -0800 | [diff] [blame] | 691 | for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin(); |
| 692 | i != endpoints_.end(); ++i) { |
| 693 | if ((*i)->ieee80211w_required()) { |
| 694 | // Never reset ieee80211w_required_ to false, so we track whether we have |
| 695 | // ever seen an AP that requires 802.11w. |
| 696 | ieee80211w_required_ = true; |
| 697 | } |
| 698 | } |
| 699 | |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 700 | set<uint16> frequency_set; |
| 701 | for (const auto &endpoint : endpoints_) { |
| 702 | frequency_set.insert(endpoint->frequency()); |
| 703 | } |
| 704 | frequency_list_.assign(frequency_set.begin(), frequency_set.end()); |
| 705 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 706 | if (Is8021x()) |
| 707 | cipher_8021x_ = ComputeCipher8021x(endpoints_); |
| 708 | |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 709 | uint16 frequency = 0; |
| 710 | int16 signal = std::numeric_limits<int16>::min(); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 711 | string bssid; |
Paul Stewart | bdbd3c3 | 2013-04-17 09:47:21 -0700 | [diff] [blame] | 712 | string country_code; |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 713 | Stringmap vendor_information; |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 714 | uint16 physical_mode = Metrics::kWiFiNetworkPhyModeUndef; |
Paul Stewart | 23b393a | 2012-09-25 21:21:06 -0700 | [diff] [blame] | 715 | // Represent "unknown raw signal strength" as 0. |
| 716 | raw_signal_strength_ = 0; |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 717 | if (representative_endpoint) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 718 | frequency = representative_endpoint->frequency(); |
| 719 | signal = representative_endpoint->signal_strength(); |
Paul Stewart | 23b393a | 2012-09-25 21:21:06 -0700 | [diff] [blame] | 720 | raw_signal_strength_ = signal; |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 721 | bssid = representative_endpoint->bssid_string(); |
Paul Stewart | bdbd3c3 | 2013-04-17 09:47:21 -0700 | [diff] [blame] | 722 | country_code = representative_endpoint->country_code(); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 723 | vendor_information = representative_endpoint->GetVendorInformation(); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 724 | physical_mode = representative_endpoint->physical_mode(); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | if (frequency_ != frequency) { |
| 728 | frequency_ = frequency; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 729 | adaptor()->EmitUint16Changed(kWifiFrequency, frequency_); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 730 | } |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 731 | if (bssid_ != bssid) { |
| 732 | bssid_ = bssid; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 733 | adaptor()->EmitStringChanged(kWifiBSsid, bssid_); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 734 | } |
Paul Stewart | bdbd3c3 | 2013-04-17 09:47:21 -0700 | [diff] [blame] | 735 | if (country_code_ != country_code) { |
| 736 | country_code_ = country_code; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 737 | adaptor()->EmitStringChanged(kCountryProperty, country_code_); |
Paul Stewart | bdbd3c3 | 2013-04-17 09:47:21 -0700 | [diff] [blame] | 738 | } |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 739 | if (vendor_information_ != vendor_information) { |
| 740 | vendor_information_ = vendor_information; |
| 741 | adaptor()->EmitStringmapChanged(kWifiVendorInformationProperty, |
| 742 | vendor_information_); |
| 743 | } |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 744 | if (physical_mode_ != physical_mode) { |
| 745 | physical_mode_ = physical_mode; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 746 | adaptor()->EmitUint16Changed(kWifiPhyMode, physical_mode_); |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 747 | } |
mukesh agrawal | e7c7e65 | 2013-06-18 17:19:39 -0700 | [diff] [blame] | 748 | adaptor()->EmitUint16sChanged(kWifiFrequencyListProperty, frequency_list_); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 749 | SetStrength(SignalToStrength(signal)); |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 750 | UpdateSecurity(); |
| 751 | } |
| 752 | |
| 753 | void WiFiService::UpdateSecurity() { |
| 754 | CryptoAlgorithm algorithm = kCryptoNone; |
| 755 | bool key_rotation = false; |
| 756 | bool endpoint_auth = false; |
| 757 | |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 758 | if (security_ == kSecurityNone) { |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 759 | // initial values apply |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 760 | } else if (security_ == kSecurityWep) { |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 761 | algorithm = kCryptoRc4; |
| 762 | key_rotation = Is8021x(); |
| 763 | endpoint_auth = Is8021x(); |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 764 | } else if (security_ == kSecurityPsk || |
| 765 | security_ == kSecurityWpa) { |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 766 | algorithm = kCryptoRc4; |
| 767 | key_rotation = true; |
| 768 | endpoint_auth = false; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 769 | } else if (security_ == kSecurityRsn) { |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 770 | algorithm = kCryptoAes; |
| 771 | key_rotation = true; |
| 772 | endpoint_auth = false; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 773 | } else if (security_ == kSecurity8021x) { |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 774 | algorithm = cipher_8021x_; |
| 775 | key_rotation = true; |
| 776 | endpoint_auth = true; |
| 777 | } |
| 778 | SetSecurity(algorithm, key_rotation, endpoint_auth); |
| 779 | } |
| 780 | |
| 781 | // static |
| 782 | Service::CryptoAlgorithm WiFiService::ComputeCipher8021x( |
| 783 | const set<WiFiEndpointConstRefPtr> &endpoints) { |
| 784 | |
| 785 | if (endpoints.empty()) |
| 786 | return kCryptoNone; // Will update after scan results. |
| 787 | |
| 788 | // Find weakest cipher (across endpoints) of the strongest ciphers |
| 789 | // (per endpoint). |
| 790 | Service::CryptoAlgorithm cipher = Service::kCryptoAes; |
| 791 | for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints.begin(); |
| 792 | i != endpoints.end(); ++i) { |
| 793 | Service::CryptoAlgorithm endpoint_cipher; |
| 794 | if ((*i)->has_rsn_property()) { |
| 795 | endpoint_cipher = Service::kCryptoAes; |
| 796 | } else if ((*i)->has_wpa_property()) { |
| 797 | endpoint_cipher = Service::kCryptoRc4; |
| 798 | } else { |
| 799 | // We could be in the Dynamic WEP case here. But that's okay, |
| 800 | // because |cipher_8021x_| is not defined in that case. |
| 801 | endpoint_cipher = Service::kCryptoNone; |
| 802 | } |
| 803 | cipher = std::min(cipher, endpoint_cipher); |
| 804 | } |
| 805 | return cipher; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 806 | } |
| 807 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 808 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 809 | void WiFiService::ValidateWEPPassphrase(const std::string &passphrase, |
| 810 | Error *error) { |
| 811 | ParseWEPPassphrase(passphrase, NULL, NULL, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 815 | void WiFiService::ValidateWPAPassphrase(const std::string &passphrase, |
| 816 | Error *error) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 817 | unsigned int length = passphrase.length(); |
| 818 | vector<uint8> passphrase_bytes; |
| 819 | |
| 820 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 821 | if (length != IEEE_80211::kWPAHexLen && |
| 822 | (length < IEEE_80211::kWPAAsciiMinLen || |
| 823 | length > IEEE_80211::kWPAAsciiMaxLen)) { |
| 824 | error->Populate(Error::kInvalidPassphrase); |
| 825 | } |
| 826 | } else { |
| 827 | if (length < IEEE_80211::kWPAAsciiMinLen || |
| 828 | length > IEEE_80211::kWPAAsciiMaxLen) { |
| 829 | error->Populate(Error::kInvalidPassphrase); |
| 830 | } |
| 831 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 832 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 833 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 834 | // static |
| 835 | void WiFiService::ParseWEPPassphrase(const string &passphrase, |
| 836 | int *key_index, |
| 837 | std::vector<uint8> *password_bytes, |
| 838 | Error *error) { |
| 839 | unsigned int length = passphrase.length(); |
| 840 | int key_index_local; |
| 841 | std::string password_text; |
| 842 | bool is_hex = false; |
| 843 | |
| 844 | switch (length) { |
| 845 | case IEEE_80211::kWEP40AsciiLen: |
| 846 | case IEEE_80211::kWEP104AsciiLen: |
| 847 | key_index_local = 0; |
| 848 | password_text = passphrase; |
| 849 | break; |
| 850 | case IEEE_80211::kWEP40AsciiLen + 2: |
| 851 | case IEEE_80211::kWEP104AsciiLen + 2: |
| 852 | if (CheckWEPKeyIndex(passphrase, error)) { |
| 853 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 854 | password_text = passphrase.substr(2); |
| 855 | } |
| 856 | break; |
| 857 | case IEEE_80211::kWEP40HexLen: |
| 858 | case IEEE_80211::kWEP104HexLen: |
| 859 | if (CheckWEPIsHex(passphrase, error)) { |
| 860 | key_index_local = 0; |
| 861 | password_text = passphrase; |
| 862 | is_hex = true; |
| 863 | } |
| 864 | break; |
| 865 | case IEEE_80211::kWEP40HexLen + 2: |
| 866 | case IEEE_80211::kWEP104HexLen + 2: |
| 867 | if(CheckWEPKeyIndex(passphrase, error) && |
| 868 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 869 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 870 | password_text = passphrase.substr(2); |
| 871 | is_hex = true; |
| 872 | } else if (CheckWEPPrefix(passphrase, error) && |
| 873 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 874 | key_index_local = 0; |
| 875 | password_text = passphrase.substr(2); |
| 876 | is_hex = true; |
| 877 | } |
| 878 | break; |
| 879 | case IEEE_80211::kWEP40HexLen + 4: |
| 880 | case IEEE_80211::kWEP104HexLen + 4: |
| 881 | if (CheckWEPKeyIndex(passphrase, error) && |
| 882 | CheckWEPPrefix(passphrase.substr(2), error) && |
| 883 | CheckWEPIsHex(passphrase.substr(4), error)) { |
| 884 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 885 | password_text = passphrase.substr(4); |
| 886 | is_hex = true; |
| 887 | } |
| 888 | break; |
| 889 | default: |
| 890 | error->Populate(Error::kInvalidPassphrase); |
| 891 | break; |
| 892 | } |
| 893 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 894 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 895 | if (key_index) |
| 896 | *key_index = key_index_local; |
| 897 | if (password_bytes) { |
| 898 | if (is_hex) |
| 899 | base::HexStringToBytes(password_text, password_bytes); |
| 900 | else |
| 901 | password_bytes->insert(password_bytes->end(), |
| 902 | password_text.begin(), |
| 903 | password_text.end()); |
| 904 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
| 908 | // static |
| 909 | bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) { |
| 910 | vector<uint8> passphrase_bytes; |
| 911 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 912 | return true; |
| 913 | } else { |
| 914 | error->Populate(Error::kInvalidPassphrase); |
| 915 | return false; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | // static |
| 920 | bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) { |
| 921 | if (StartsWithASCII(passphrase, "0:", false) || |
| 922 | StartsWithASCII(passphrase, "1:", false) || |
| 923 | StartsWithASCII(passphrase, "2:", false) || |
| 924 | StartsWithASCII(passphrase, "3:", false)) { |
| 925 | return true; |
| 926 | } else { |
| 927 | error->Populate(Error::kInvalidPassphrase); |
| 928 | return false; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | // static |
| 933 | bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) { |
| 934 | if (StartsWithASCII(passphrase, "0x", false)) { |
| 935 | return true; |
| 936 | } else { |
| 937 | error->Populate(Error::kInvalidPassphrase); |
| 938 | return false; |
| 939 | } |
| 940 | } |
| 941 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 942 | // static |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 943 | string WiFiService::GetSecurityClass(const string &security) { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 944 | if (security == kSecurityRsn || |
| 945 | security == kSecurityWpa) { |
| 946 | return kSecurityPsk; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 947 | } else { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 948 | return security; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
Wade Guthrie | 9ec0806 | 2013-09-25 15:22:24 -0700 | [diff] [blame] | 952 | |
| 953 | int16 WiFiService::SignalLevel() const { |
| 954 | return current_endpoint_ ? current_endpoint_->signal_strength() : |
| 955 | std::numeric_limits<int16>::min(); |
| 956 | } |
| 957 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 958 | // static |
| 959 | bool WiFiService::ParseStorageIdentifier(const string &storage_name, |
| 960 | string *address, |
| 961 | string *mode, |
| 962 | string *security) { |
| 963 | vector<string> wifi_parts; |
| 964 | base::SplitString(storage_name, '_', &wifi_parts); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 965 | if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) || |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 966 | wifi_parts[0] != kTypeWifi) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 967 | return false; |
| 968 | } |
| 969 | *address = wifi_parts[1]; |
| 970 | *mode = wifi_parts[3]; |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 971 | if (wifi_parts.size() == 5) { |
| 972 | *security = wifi_parts[4]; |
| 973 | } else { |
| 974 | // Account for security type "802_1x" which got split up above. |
| 975 | *security = wifi_parts[4] + "_" + wifi_parts[5]; |
| 976 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 977 | return true; |
| 978 | } |
| 979 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 980 | // static |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 981 | bool WiFiService::FixupServiceEntries(StoreInterface *storage) { |
| 982 | bool fixed_entry = false; |
| 983 | set<string> groups = storage->GetGroups(); |
| 984 | for (set<string>::const_iterator it = groups.begin(); it != groups.end(); |
| 985 | ++it) { |
| 986 | const string &id = *it; |
| 987 | string device_address, network_mode, security; |
| 988 | if (!ParseStorageIdentifier(id, &device_address, |
| 989 | &network_mode, &security)) { |
| 990 | continue; |
| 991 | } |
| 992 | if (!storage->GetString(id, kStorageType, NULL)) { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 993 | storage->SetString(id, kStorageType, kTypeWifi); |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 994 | fixed_entry = true; |
| 995 | } |
| 996 | if (!storage->GetString(id, kStorageMode, NULL)) { |
| 997 | storage->SetString(id, kStorageMode, network_mode); |
| 998 | fixed_entry = true; |
| 999 | } |
| 1000 | if (!storage->GetString(id, kStorageSecurity, NULL)) { |
| 1001 | storage->SetString(id, kStorageSecurity, security); |
| 1002 | fixed_entry = true; |
| 1003 | } |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1004 | if (!storage->GetString(id, kStorageSecurityClass, NULL)) { |
| 1005 | storage->SetString(id, kStorageSecurityClass, GetSecurityClass(security)); |
| 1006 | fixed_entry = true; |
| 1007 | } |
Paul Stewart | 85aea15 | 2013-01-22 09:31:56 -0800 | [diff] [blame] | 1008 | } |
| 1009 | return fixed_entry; |
| 1010 | } |
| 1011 | |
| 1012 | // static |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1013 | bool WiFiService::IsValidMode(const string &mode) { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1014 | return mode == kModeManaged || mode == kModeAdhoc; |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | // static |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1018 | bool WiFiService::IsValidSecurityMethod(const string &method) { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1019 | return method == kSecurityNone || |
| 1020 | method == kSecurityWep || |
| 1021 | method == kSecurityPsk || |
| 1022 | method == kSecurityWpa || |
| 1023 | method == kSecurityRsn || |
| 1024 | method == kSecurity8021x; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | // static |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1028 | uint8 WiFiService::SignalToStrength(int16 signal_dbm) { |
| 1029 | int16 strength; |
| 1030 | if (signal_dbm > 0) { |
| 1031 | if (!logged_signal_warning) { |
| 1032 | LOG(WARNING) << "Signal strength is suspiciously high. " |
| 1033 | << "Assuming value " << signal_dbm << " is not in dBm."; |
| 1034 | logged_signal_warning = true; |
| 1035 | } |
| 1036 | strength = signal_dbm; |
| 1037 | } else { |
| 1038 | strength = 120 + signal_dbm; // Call -20dBm "perfect". |
| 1039 | } |
| 1040 | |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 1041 | if (strength > kStrengthMax) { |
| 1042 | strength = kStrengthMax; |
| 1043 | } else if (strength < kStrengthMin) { |
| 1044 | strength = kStrengthMin; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 1045 | } |
| 1046 | return strength; |
| 1047 | } |
| 1048 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1049 | KeyValueStore WiFiService::GetStorageProperties() const { |
| 1050 | KeyValueStore args; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1051 | args.SetString(kStorageType, kTypeWifi); |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1052 | args.SetString(kStorageSSID, hex_ssid_); |
| 1053 | args.SetString(kStorageMode, mode_); |
| 1054 | args.SetString(kStorageSecurityClass, GetSecurityClass(security_)); |
| 1055 | return args; |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Paul Stewart | 71a4d3b | 2013-01-18 18:12:56 -0800 | [diff] [blame] | 1058 | string WiFiService::GetDefaultStorageIdentifier() const { |
| 1059 | string security = GetSecurityClass(security_); |
| 1060 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1061 | kTypeWifi, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1062 | kAnyDeviceAddress, |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 1063 | hex_ssid_.c_str(), |
| 1064 | mode_.c_str(), |
| 1065 | security.c_str())); |
| 1066 | } |
| 1067 | |
Paul Stewart | 6df20bd | 2013-03-13 19:31:25 -0700 | [diff] [blame] | 1068 | string WiFiService::GetSecurity(Error */*error*/) { |
| 1069 | if (current_endpoint_) { |
| 1070 | return current_endpoint_->security_mode(); |
| 1071 | } |
| 1072 | return security_; |
| 1073 | } |
| 1074 | |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 1075 | void WiFiService::ClearCachedCredentials() { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1076 | if (wifi_) { |
| 1077 | wifi_->ClearCachedCredentials(this); |
| 1078 | } |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1081 | void WiFiService::OnEapCredentialsChanged() { |
Paul Stewart | 835934a | 2012-12-06 19:27:09 -0800 | [diff] [blame] | 1082 | ClearCachedCredentials(); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1083 | UpdateConnectable(); |
| 1084 | } |
| 1085 | |
Paul Stewart | 4357f4e | 2012-04-26 17:39:26 -0700 | [diff] [blame] | 1086 | void WiFiService::OnProfileConfigured() { |
| 1087 | if (profile() || !hidden_ssid()) { |
| 1088 | return; |
| 1089 | } |
| 1090 | // This situation occurs when a hidden WiFi service created via GetService |
| 1091 | // has been persisted to a profile in Manager::ConfigureService(). Now |
| 1092 | // that configuration is saved, we must join the service with its profile, |
| 1093 | // which will make this SSID eligible for directed probes during scans. |
| 1094 | manager()->RegisterService(this); |
| 1095 | } |
| 1096 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 1097 | bool WiFiService::Is8021x() const { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1098 | if (security_ == kSecurity8021x) |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 1099 | return true; |
| 1100 | |
| 1101 | // Dynamic WEP + 802.1x. |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1102 | if (security_ == kSecurityWep && |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 1103 | GetEAPKeyManagement() == WPASupplicant::kKeyManagementIeee8021X) |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 1104 | return true; |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1108 | WiFiRefPtr WiFiService::ChooseDevice() { |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 1109 | // TODO(pstew): Style frowns on dynamic_cast. crbug.com/220387 |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1110 | DeviceRefPtr device = |
| 1111 | manager()->GetEnabledDeviceWithTechnology(Technology::kWifi); |
| 1112 | return dynamic_cast<WiFi *>(device.get()); |
| 1113 | } |
| 1114 | |
| 1115 | void WiFiService::ResetWiFi() { |
| 1116 | SetWiFi(NULL); |
| 1117 | } |
| 1118 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1119 | void WiFiService::SetWiFi(const WiFiRefPtr &new_wifi) { |
| 1120 | if (wifi_ == new_wifi) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1121 | return; |
| 1122 | } |
| 1123 | ClearCachedCredentials(); |
| 1124 | if (wifi_) { |
| 1125 | wifi_->DisassociateFromService(this); |
| 1126 | } |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1127 | if (new_wifi) { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1128 | adaptor()->EmitRpcIdentifierChanged(kDeviceProperty, |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1129 | new_wifi->GetRpcIdentifier()); |
| 1130 | } else { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 1131 | adaptor()->EmitRpcIdentifierChanged(kDeviceProperty, |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1132 | DBusAdaptor::kNullPath); |
| 1133 | } |
| 1134 | wifi_ = new_wifi; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1137 | } // namespace shill |