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