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