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