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 | |
| 7 | #include <string> |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 8 | #include <utility> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 9 | |
| 10 | #include <base/logging.h> |
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" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 19 | #include "shill/control_interface.h" |
| 20 | #include "shill/device.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 21 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 22 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 23 | #include "shill/ieee80211.h" |
Paul Stewart | 4357f4e | 2012-04-26 17:39:26 -0700 | [diff] [blame] | 24 | #include "shill/manager.h" |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 25 | #include "shill/metrics.h" |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 26 | #include "shill/nss.h" |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 27 | #include "shill/property_accessor.h" |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 28 | #include "shill/store_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 29 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 30 | #include "shill/wifi_endpoint.h" |
| 31 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 32 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 33 | using std::set; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 34 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 35 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 36 | |
| 37 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 38 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 39 | const char WiFiService::kAutoConnBusy[] = "busy"; |
| 40 | const char WiFiService::kAutoConnNoEndpoint[] = "no endpoints"; |
| 41 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 42 | const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID"; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 43 | const char WiFiService::kStorageMode[] = "WiFi.Mode"; |
| 44 | const char WiFiService::kStoragePassphrase[] = "Passphrase"; |
| 45 | const char WiFiService::kStorageSecurity[] = "WiFi.Security"; |
| 46 | const char WiFiService::kStorageSSID[] = "SSID"; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 47 | bool WiFiService::logged_signal_warning = false; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 48 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 49 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 50 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 51 | Metrics *metrics, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 52 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 53 | const WiFiRefPtr &device, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 54 | const vector<uint8_t> &ssid, |
| 55 | const string &mode, |
| 56 | const string &security, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 57 | bool hidden_ssid) |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 58 | : Service(control_interface, dispatcher, metrics, manager, |
| 59 | Technology::kWifi), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame] | 60 | need_passphrase_(false), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 61 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 62 | mode_(mode), |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 63 | hidden_ssid_(hidden_ssid), |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 64 | frequency_(0), |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 65 | physical_mode_(0), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 66 | wifi_(device), |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 67 | ssid_(ssid), |
| 68 | nss_(NSS::GetInstance()) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 69 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 70 | store->RegisterConstString(flimflam::kModeProperty, &mode_); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 71 | HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty, |
| 72 | &WiFiService::SetPassphrase, |
| 73 | &WiFiService::ClearPassphrase, |
| 74 | NULL); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 75 | store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_); |
| 76 | store->RegisterConstString(flimflam::kSecurityProperty, &security_); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 77 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 78 | store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_); |
| 79 | store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_); |
| 80 | store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_); |
| 81 | store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 82 | store->RegisterConstString(flimflam::kWifiBSsid, &bssid_); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 83 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 84 | hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size()); |
| 85 | string ssid_string( |
| 86 | reinterpret_cast<const char *>(ssid_.data()), ssid_.size()); |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 87 | if (WiFi::SanitizeSSID(&ssid_string)) { |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 88 | // WifiHexSsid property should only be present if Name property |
| 89 | // has been munged. |
| 90 | store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_); |
| 91 | } |
| 92 | set_friendly_name(ssid_string); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 93 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 94 | // TODO(quiche): determine if it is okay to set EAP.KeyManagement for |
| 95 | // a service that is not 802.1x. |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 96 | if (Is8021x()) { |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 97 | // Passphrases are not mandatory for 802.1X. |
| 98 | need_passphrase_ = false; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 99 | } else if (security_ == flimflam::kSecurityPsk) { |
| 100 | SetEAPKeyManagement("WPA-PSK"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 101 | } else if (security_ == flimflam::kSecurityRsn) { |
| 102 | SetEAPKeyManagement("WPA-PSK"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 103 | } else if (security_ == flimflam::kSecurityWpa) { |
| 104 | SetEAPKeyManagement("WPA-PSK"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 105 | } else if (security_ == flimflam::kSecurityWep) { |
| 106 | SetEAPKeyManagement("NONE"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 107 | } else if (security_ == flimflam::kSecurityNone) { |
| 108 | SetEAPKeyManagement("NONE"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 109 | } else { |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 110 | LOG(ERROR) << "Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 113 | // Until we know better (at Profile load time), use the generic name. |
| 114 | storage_identifier_ = GetGenericStorageIdentifier(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 115 | UpdateConnectable(); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 116 | |
| 117 | IgnoreParameterForConfigure(flimflam::kModeProperty); |
| 118 | IgnoreParameterForConfigure(flimflam::kSSIDProperty); |
| 119 | IgnoreParameterForConfigure(flimflam::kSecurityProperty); |
| 120 | IgnoreParameterForConfigure(flimflam::kPassphraseProperty); |
| 121 | IgnoreParameterForConfigure(flimflam::kWifiHiddenSsid); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Darin Petkov | 9cd7ca1 | 2012-07-03 11:06:40 +0200 | [diff] [blame^] | 124 | WiFiService::~WiFiService() {} |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 125 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 126 | bool WiFiService::IsAutoConnectable(const char **reason) const { |
| 127 | if (!Service::IsAutoConnectable(reason)) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // Only auto-connect to Services which have visible Endpoints. |
| 132 | // (Needed because hidden Services may remain registered with |
| 133 | // Manager even without visible Endpoints.) |
| 134 | if (!HasEndpoints()) { |
| 135 | *reason = kAutoConnNoEndpoint; |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | // Do not preempt an existing connection (whether pending, or |
| 140 | // connected, and whether to this service, or another). |
| 141 | if (!wifi_->IsIdle()) { |
| 142 | *reason = kAutoConnBusy; |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | return true; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool WiFiService::IsConnecting() const { |
| 150 | // WiFi does not move us into the associating state until it gets |
| 151 | // feedback from wpa_supplicant. So, to answer whether or |
| 152 | // not we're connecting, we consult with |wifi_|. |
| 153 | return wifi_->IsConnectingTo(*this); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 156 | void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr &endpoint) { |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 157 | DCHECK(endpoint->ssid() == ssid()); |
| 158 | endpoints_.insert(endpoint); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 159 | UpdateFromEndpoints(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 162 | void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint) { |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 163 | set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint); |
| 164 | DCHECK(i != endpoints_.end()); |
| 165 | if (i == endpoints_.end()) { |
| 166 | LOG(WARNING) << "In " << __func__ << "(): " |
| 167 | << "ignorning non-existent endpoint " |
| 168 | << endpoint->bssid_string(); |
| 169 | return; |
| 170 | } |
| 171 | endpoints_.erase(i); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 172 | if (current_endpoint_ == endpoint) { |
| 173 | current_endpoint_ = NULL; |
| 174 | } |
| 175 | UpdateFromEndpoints(); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 176 | } |
| 177 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 178 | void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) { |
| 179 | DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end())); |
| 180 | current_endpoint_ = endpoint; |
| 181 | UpdateFromEndpoints(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 182 | } |
| 183 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 184 | void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) { |
| 185 | DCHECK(endpoints_.find(&endpoint) != endpoints_.end()); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 186 | UpdateFromEndpoints(); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 189 | string WiFiService::GetStorageIdentifier() const { |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 190 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 191 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 192 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 193 | void WiFiService::SetPassphrase(const string &passphrase, Error *error) { |
| 194 | if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 195 | ValidateWEPPassphrase(passphrase, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 196 | } else if (security_ == flimflam::kSecurityPsk || |
| 197 | security_ == flimflam::kSecurityWpa || |
| 198 | security_ == flimflam::kSecurityRsn) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 199 | ValidateWPAPassphrase(passphrase, error); |
| 200 | } else { |
| 201 | error->Populate(Error::kNotSupported); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 202 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 203 | |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 204 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 205 | passphrase_ = passphrase; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 206 | } |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 207 | |
| 208 | UpdateConnectable(); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 209 | } |
| 210 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 211 | // ClearPassphrase is separate from SetPassphrase, because the default |
| 212 | // value for |passphrase_| would not pass validation. |
| 213 | void WiFiService::ClearPassphrase(Error */*error*/) { |
| 214 | passphrase_.clear(); |
| 215 | UpdateConnectable(); |
| 216 | } |
| 217 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 218 | bool WiFiService::IsLoadableFrom(StoreInterface *storage) const { |
| 219 | return storage->ContainsGroup(GetGenericStorageIdentifier()) || |
| 220 | storage->ContainsGroup(GetSpecificStorageIdentifier()); |
| 221 | } |
| 222 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 223 | bool WiFiService::IsVisible() const { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 224 | // WiFi Services should be displayed only if they are in range (have |
| 225 | // endpoints that have shown up in a scan) or if the service is actively |
| 226 | // being connected. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 227 | return HasEndpoints() || IsConnected() || IsConnecting(); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 230 | bool WiFiService::Load(StoreInterface *storage) { |
| 231 | // First find out which storage identifier is available in priority order |
| 232 | // of specific, generic. |
| 233 | string id = GetSpecificStorageIdentifier(); |
| 234 | if (!storage->ContainsGroup(id)) { |
| 235 | id = GetGenericStorageIdentifier(); |
| 236 | if (!storage->ContainsGroup(id)) { |
| 237 | LOG(WARNING) << "Service is not available in the persistent store: " |
| 238 | << id; |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Set our storage identifier to match the storage name in the Profile. |
| 244 | storage_identifier_ = id; |
| 245 | |
| 246 | // Load properties common to all Services. |
| 247 | if (!Service::Load(storage)) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | // Load properties specific to WiFi services. |
| 252 | storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 253 | |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 254 | // NB: mode, security and ssid parameters are never read in from |
| 255 | // Load() as they are provided from the scan. |
| 256 | |
| 257 | string passphrase; |
| 258 | if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) { |
| 259 | Error error; |
| 260 | SetPassphrase(passphrase, &error); |
| 261 | if (!error.IsSuccess()) { |
| 262 | LOG(ERROR) << "Passphrase could not be set: " |
| 263 | << Error::GetName(error.type()); |
| 264 | } |
| 265 | } |
| 266 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | |
| 270 | bool WiFiService::Save(StoreInterface *storage) { |
| 271 | // Save properties common to all Services. |
| 272 | if (!Service::Save(storage)) { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | // Save properties specific to WiFi services. |
| 277 | const string id = GetStorageIdentifier(); |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 278 | storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_); |
| 279 | storage->SetString(id, kStorageMode, mode_); |
| 280 | storage->SetCryptedString(id, kStoragePassphrase, passphrase_); |
| 281 | storage->SetString(id, kStorageSecurity, security_); |
| 282 | storage->SetString(id, kStorageSSID, hex_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 283 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 287 | bool WiFiService::Unload() { |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 288 | Service::Unload(); |
| 289 | hidden_ssid_ = false; |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 290 | Error unused_error; |
| 291 | ClearPassphrase(&unused_error); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 292 | if (security_ == flimflam::kSecurity8021x) { |
| 293 | // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or |
| 294 | // 802.1x/WEP) have the ability to cache WPA PMK credentials. |
| 295 | // Make sure that these are cleared when credentials for networks |
| 296 | // of this type goes away. |
| 297 | // |
| 298 | // When wpa_supplicant gains the ability, do this credential |
| 299 | // clearing on a per-service basis. Also do this whenever the credentials |
| 300 | // for a service changes. crosbug.com/25670 |
| 301 | wifi_->ClearCachedCredentials(); |
| 302 | } |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 303 | return !IsVisible(); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 306 | bool WiFiService::IsSecurityMatch(const string &security) const { |
| 307 | return GetSecurityClass(security) == GetSecurityClass(security_); |
| 308 | } |
| 309 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 310 | void WiFiService::InitializeCustomMetrics() const { |
| 311 | string histogram = metrics()->GetFullMetricName( |
| 312 | Metrics::kMetricTimeToJoinMilliseconds, |
| 313 | technology()); |
| 314 | metrics()->AddServiceStateTransitionTimer(this, |
| 315 | histogram, |
| 316 | Service::kStateAssociating, |
| 317 | Service::kStateConfiguring); |
| 318 | } |
| 319 | |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 320 | void WiFiService::SendPostReadyStateMetrics( |
| 321 | int64 time_resume_to_ready_milliseconds) const { |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 322 | metrics()->SendEnumToUMA( |
| 323 | metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel, |
| 324 | technology()), |
| 325 | Metrics::WiFiFrequencyToChannel(frequency_), |
| 326 | Metrics::kMetricNetworkChannelMax); |
Thieu Le | ad1ec2c | 2012-01-05 23:39:48 +0000 | [diff] [blame] | 327 | |
| 328 | DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax); |
| 329 | metrics()->SendEnumToUMA( |
| 330 | metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode, |
| 331 | technology()), |
| 332 | static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_), |
| 333 | Metrics::kWiFiNetworkPhyModeMax); |
| 334 | |
| 335 | Metrics::WiFiSecurity security_uma = |
| 336 | Metrics::WiFiSecurityStringToEnum(security_); |
| 337 | DCHECK(security_uma != Metrics::kWiFiSecurityUnknown); |
| 338 | metrics()->SendEnumToUMA( |
| 339 | metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity, |
| 340 | technology()), |
| 341 | security_uma, |
| 342 | Metrics::kMetricNetworkSecurityMax); |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 343 | |
| 344 | if (time_resume_to_ready_milliseconds > 0) { |
| 345 | metrics()->SendToUMA( |
| 346 | metrics()->GetFullMetricName( |
| 347 | Metrics::kMetricTimeResumeToReadyMilliseconds, technology()), |
| 348 | time_resume_to_ready_milliseconds, |
| 349 | Metrics::kTimerHistogramMillisecondsMin, |
| 350 | Metrics::kTimerHistogramMillisecondsMax, |
| 351 | Metrics::kTimerHistogramNumBuckets); |
| 352 | } |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 353 | } |
| 354 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 355 | // private methods |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 356 | void WiFiService::HelpRegisterWriteOnlyDerivedString( |
| 357 | const string &name, |
| 358 | void(WiFiService::*set)(const string &, Error *), |
| 359 | void(WiFiService::*clear)(Error *), |
| 360 | const string *default_value) { |
| 361 | mutable_store()->RegisterDerivedString( |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 362 | name, |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 363 | StringAccessor( |
| 364 | new CustomWriteOnlyAccessor<WiFiService, string>( |
| 365 | this, set, clear, default_value))); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 368 | void WiFiService::Connect(Error *error) { |
| 369 | LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name(); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 370 | std::map<string, DBus::Variant> params; |
| 371 | DBus::MessageIter writer; |
| 372 | |
Wade Guthrie | 005bd34 | 2012-05-02 09:37:07 -0700 | [diff] [blame] | 373 | if (!connectable()) { |
| 374 | LOG(ERROR) << "Can't connect. Service " << friendly_name() |
| 375 | << " is not connectable"; |
| 376 | Error::PopulateAndLog(error, Error::kOperationFailed, |
| 377 | Error::GetDefaultMessage(Error::kOperationFailed)); |
| 378 | return; |
| 379 | } |
| 380 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 381 | params[wpa_supplicant::kNetworkPropertyMode].writer(). |
| 382 | append_uint32(WiFiEndpoint::ModeStringToUint(mode_)); |
| 383 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 384 | if (Is8021x()) { |
| 385 | // Is EAP key management is not set, set to a default. |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 386 | if (GetEAPKeyManagement().empty()) |
| 387 | SetEAPKeyManagement("WPA-EAP"); |
| 388 | Populate8021xProperties(¶ms); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 389 | ClearEAPCertification(); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 390 | } else if (security_ == flimflam::kSecurityPsk) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 391 | const string psk_proto = StringPrintf("%s %s", |
| 392 | wpa_supplicant::kSecurityModeWPA, |
| 393 | wpa_supplicant::kSecurityModeRSN); |
| 394 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 395 | append_string(psk_proto.c_str()); |
| 396 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 397 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 398 | } else if (security_ == flimflam::kSecurityRsn) { |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 399 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 400 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 401 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 402 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 403 | } else if (security_ == flimflam::kSecurityWpa) { |
| 404 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 405 | append_string(wpa_supplicant::kSecurityModeWPA); |
| 406 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 407 | append_string(passphrase_.c_str()); |
| 408 | } else if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 409 | params[wpa_supplicant::kPropertyAuthAlg].writer(). |
| 410 | append_string(wpa_supplicant::kSecurityAuthAlg); |
| 411 | Error error; |
| 412 | int key_index; |
| 413 | std::vector<uint8> password_bytes; |
| 414 | ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error); |
| 415 | writer = params[wpa_supplicant::kPropertyWEPKey + |
| 416 | base::IntToString(key_index)].writer(); |
| 417 | writer << password_bytes; |
| 418 | params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer(). |
| 419 | append_uint32(key_index); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 420 | } else if (security_ == flimflam::kSecurityNone) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 421 | // Nothing special to do here. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 422 | } else { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 423 | LOG(ERROR) << "Can't connect. Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 426 | params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer(). |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 427 | append_string(key_management().c_str()); |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 428 | |
| 429 | // See note in dbus_adaptor.cc on why we need to use a local. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 430 | writer = params[wpa_supplicant::kNetworkPropertySSID].writer(); |
| 431 | writer << ssid_; |
| 432 | |
| 433 | wifi_->ConnectTo(this, params); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 436 | void WiFiService::Disconnect(Error *error) { |
| 437 | LOG(INFO) << __func__; |
| 438 | Service::Disconnect(error); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 439 | wifi_->DisconnectFrom(this); |
| 440 | } |
| 441 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 442 | string WiFiService::GetDeviceRpcId(Error */*error*/) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 443 | return wifi_->GetRpcIdentifier(); |
| 444 | } |
| 445 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 446 | void WiFiService::UpdateConnectable() { |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 447 | bool is_connectable = false; |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 448 | if (security_ == flimflam::kSecurityNone) { |
| 449 | DCHECK(passphrase_.empty()); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 450 | need_passphrase_ = false; |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 451 | is_connectable = true; |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 452 | } else if (Is8021x()) { |
| 453 | is_connectable = Is8021xConnectable(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 454 | } else if (security_ == flimflam::kSecurityWep || |
| 455 | security_ == flimflam::kSecurityWpa || |
| 456 | security_ == flimflam::kSecurityPsk || |
| 457 | security_ == flimflam::kSecurityRsn) { |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 458 | need_passphrase_ = passphrase_.empty(); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 459 | is_connectable = !need_passphrase_; |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 460 | } |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 461 | set_connectable(is_connectable); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 462 | } |
| 463 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 464 | void WiFiService::UpdateFromEndpoints() { |
| 465 | const WiFiEndpoint *representative_endpoint = NULL; |
| 466 | |
| 467 | if (current_endpoint_) { |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 468 | representative_endpoint = current_endpoint_; |
| 469 | } else { |
| 470 | int16 best_signal = std::numeric_limits<int16>::min(); |
| 471 | for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin(); |
| 472 | i != endpoints_.end(); ++i) { |
| 473 | if ((*i)->signal_strength() >= best_signal) { |
| 474 | best_signal = (*i)->signal_strength(); |
| 475 | representative_endpoint = *i; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | uint16 frequency; |
| 481 | int16 signal; |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 482 | string bssid; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 483 | if (!representative_endpoint) { |
| 484 | frequency = 0; |
| 485 | signal = std::numeric_limits<int16>::min(); |
| 486 | } else { |
| 487 | frequency = representative_endpoint->frequency(); |
| 488 | signal = representative_endpoint->signal_strength(); |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 489 | bssid = representative_endpoint->bssid_string(); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | if (frequency_ != frequency) { |
| 493 | frequency_ = frequency; |
| 494 | adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_); |
| 495 | } |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 496 | if (bssid_ != bssid) { |
| 497 | bssid_ = bssid; |
| 498 | adaptor()->EmitStringChanged(flimflam::kWifiBSsid, bssid_); |
| 499 | } |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 500 | SetStrength(SignalToStrength(signal)); |
| 501 | } |
| 502 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 503 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 504 | void WiFiService::ValidateWEPPassphrase(const std::string &passphrase, |
| 505 | Error *error) { |
| 506 | ParseWEPPassphrase(passphrase, NULL, NULL, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 510 | void WiFiService::ValidateWPAPassphrase(const std::string &passphrase, |
| 511 | Error *error) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 512 | unsigned int length = passphrase.length(); |
| 513 | vector<uint8> passphrase_bytes; |
| 514 | |
| 515 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 516 | if (length != IEEE_80211::kWPAHexLen && |
| 517 | (length < IEEE_80211::kWPAAsciiMinLen || |
| 518 | length > IEEE_80211::kWPAAsciiMaxLen)) { |
| 519 | error->Populate(Error::kInvalidPassphrase); |
| 520 | } |
| 521 | } else { |
| 522 | if (length < IEEE_80211::kWPAAsciiMinLen || |
| 523 | length > IEEE_80211::kWPAAsciiMaxLen) { |
| 524 | error->Populate(Error::kInvalidPassphrase); |
| 525 | } |
| 526 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 527 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 528 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 529 | // static |
| 530 | void WiFiService::ParseWEPPassphrase(const string &passphrase, |
| 531 | int *key_index, |
| 532 | std::vector<uint8> *password_bytes, |
| 533 | Error *error) { |
| 534 | unsigned int length = passphrase.length(); |
| 535 | int key_index_local; |
| 536 | std::string password_text; |
| 537 | bool is_hex = false; |
| 538 | |
| 539 | switch (length) { |
| 540 | case IEEE_80211::kWEP40AsciiLen: |
| 541 | case IEEE_80211::kWEP104AsciiLen: |
| 542 | key_index_local = 0; |
| 543 | password_text = passphrase; |
| 544 | break; |
| 545 | case IEEE_80211::kWEP40AsciiLen + 2: |
| 546 | case IEEE_80211::kWEP104AsciiLen + 2: |
| 547 | if (CheckWEPKeyIndex(passphrase, error)) { |
| 548 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 549 | password_text = passphrase.substr(2); |
| 550 | } |
| 551 | break; |
| 552 | case IEEE_80211::kWEP40HexLen: |
| 553 | case IEEE_80211::kWEP104HexLen: |
| 554 | if (CheckWEPIsHex(passphrase, error)) { |
| 555 | key_index_local = 0; |
| 556 | password_text = passphrase; |
| 557 | is_hex = true; |
| 558 | } |
| 559 | break; |
| 560 | case IEEE_80211::kWEP40HexLen + 2: |
| 561 | case IEEE_80211::kWEP104HexLen + 2: |
| 562 | if(CheckWEPKeyIndex(passphrase, error) && |
| 563 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 564 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 565 | password_text = passphrase.substr(2); |
| 566 | is_hex = true; |
| 567 | } else if (CheckWEPPrefix(passphrase, error) && |
| 568 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 569 | key_index_local = 0; |
| 570 | password_text = passphrase.substr(2); |
| 571 | is_hex = true; |
| 572 | } |
| 573 | break; |
| 574 | case IEEE_80211::kWEP40HexLen + 4: |
| 575 | case IEEE_80211::kWEP104HexLen + 4: |
| 576 | if (CheckWEPKeyIndex(passphrase, error) && |
| 577 | CheckWEPPrefix(passphrase.substr(2), error) && |
| 578 | CheckWEPIsHex(passphrase.substr(4), error)) { |
| 579 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 580 | password_text = passphrase.substr(4); |
| 581 | is_hex = true; |
| 582 | } |
| 583 | break; |
| 584 | default: |
| 585 | error->Populate(Error::kInvalidPassphrase); |
| 586 | break; |
| 587 | } |
| 588 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 589 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 590 | if (key_index) |
| 591 | *key_index = key_index_local; |
| 592 | if (password_bytes) { |
| 593 | if (is_hex) |
| 594 | base::HexStringToBytes(password_text, password_bytes); |
| 595 | else |
| 596 | password_bytes->insert(password_bytes->end(), |
| 597 | password_text.begin(), |
| 598 | password_text.end()); |
| 599 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | // static |
| 604 | bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) { |
| 605 | vector<uint8> passphrase_bytes; |
| 606 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 607 | return true; |
| 608 | } else { |
| 609 | error->Populate(Error::kInvalidPassphrase); |
| 610 | return false; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | // static |
| 615 | bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) { |
| 616 | if (StartsWithASCII(passphrase, "0:", false) || |
| 617 | StartsWithASCII(passphrase, "1:", false) || |
| 618 | StartsWithASCII(passphrase, "2:", false) || |
| 619 | StartsWithASCII(passphrase, "3:", false)) { |
| 620 | return true; |
| 621 | } else { |
| 622 | error->Populate(Error::kInvalidPassphrase); |
| 623 | return false; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | // static |
| 628 | bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) { |
| 629 | if (StartsWithASCII(passphrase, "0x", false)) { |
| 630 | return true; |
| 631 | } else { |
| 632 | error->Populate(Error::kInvalidPassphrase); |
| 633 | return false; |
| 634 | } |
| 635 | } |
| 636 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 637 | // static |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 638 | string WiFiService::GetSecurityClass(const string &security) { |
| 639 | if (security == flimflam::kSecurityRsn || |
| 640 | security == flimflam::kSecurityWpa) { |
| 641 | return flimflam::kSecurityPsk; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 642 | } else { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 643 | return security; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 647 | // static |
| 648 | bool WiFiService::ParseStorageIdentifier(const string &storage_name, |
| 649 | string *address, |
| 650 | string *mode, |
| 651 | string *security) { |
| 652 | vector<string> wifi_parts; |
| 653 | base::SplitString(storage_name, '_', &wifi_parts); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 654 | if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) || |
| 655 | wifi_parts[0] != flimflam::kTypeWifi) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 656 | return false; |
| 657 | } |
| 658 | *address = wifi_parts[1]; |
| 659 | *mode = wifi_parts[3]; |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 660 | if (wifi_parts.size() == 5) { |
| 661 | *security = wifi_parts[4]; |
| 662 | } else { |
| 663 | // Account for security type "802_1x" which got split up above. |
| 664 | *security = wifi_parts[4] + "_" + wifi_parts[5]; |
| 665 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 666 | return true; |
| 667 | } |
| 668 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 669 | // static |
| 670 | uint8 WiFiService::SignalToStrength(int16 signal_dbm) { |
| 671 | int16 strength; |
| 672 | if (signal_dbm > 0) { |
| 673 | if (!logged_signal_warning) { |
| 674 | LOG(WARNING) << "Signal strength is suspiciously high. " |
| 675 | << "Assuming value " << signal_dbm << " is not in dBm."; |
| 676 | logged_signal_warning = true; |
| 677 | } |
| 678 | strength = signal_dbm; |
| 679 | } else { |
| 680 | strength = 120 + signal_dbm; // Call -20dBm "perfect". |
| 681 | } |
| 682 | |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 683 | if (strength > kStrengthMax) { |
| 684 | strength = kStrengthMax; |
| 685 | } else if (strength < kStrengthMin) { |
| 686 | strength = kStrengthMin; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 687 | } |
| 688 | return strength; |
| 689 | } |
| 690 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 691 | string WiFiService::GetGenericStorageIdentifier() const { |
| 692 | return GetStorageIdentifierForSecurity(GetSecurityClass(security_)); |
| 693 | } |
| 694 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 695 | string WiFiService::GetSpecificStorageIdentifier() const { |
| 696 | return GetStorageIdentifierForSecurity(security_); |
| 697 | } |
| 698 | |
| 699 | string WiFiService::GetStorageIdentifierForSecurity( |
| 700 | const string &security) const { |
| 701 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
| 702 | flimflam::kTypeWifi, |
| 703 | wifi_->address().c_str(), |
| 704 | hex_ssid_.c_str(), |
| 705 | mode_.c_str(), |
| 706 | security.c_str())); |
| 707 | } |
| 708 | |
Gary Morain | e4aaf5e | 2012-04-05 14:37:32 -0700 | [diff] [blame] | 709 | void WiFiService::set_eap(const EapCredentials &new_eap) { |
| 710 | EapCredentials modified_eap = new_eap; |
| 711 | |
| 712 | // An empty key_management field is invalid. Prevent it, if possible. |
| 713 | if (modified_eap.key_management.empty()) { |
| 714 | modified_eap.key_management = eap().key_management; |
| 715 | } |
| 716 | Service::set_eap(modified_eap); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 717 | UpdateConnectable(); |
| 718 | } |
| 719 | |
Paul Stewart | 4357f4e | 2012-04-26 17:39:26 -0700 | [diff] [blame] | 720 | void WiFiService::OnProfileConfigured() { |
| 721 | if (profile() || !hidden_ssid()) { |
| 722 | return; |
| 723 | } |
| 724 | // This situation occurs when a hidden WiFi service created via GetService |
| 725 | // has been persisted to a profile in Manager::ConfigureService(). Now |
| 726 | // that configuration is saved, we must join the service with its profile, |
| 727 | // which will make this SSID eligible for directed probes during scans. |
| 728 | manager()->RegisterService(this); |
| 729 | } |
| 730 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 731 | bool WiFiService::Is8021x() const { |
| 732 | if (security_ == flimflam::kSecurity8021x) |
| 733 | return true; |
| 734 | |
| 735 | // Dynamic WEP + 802.1x. |
| 736 | if (security_ == flimflam::kSecurityWep && |
| 737 | GetEAPKeyManagement() == "IEEE8021X") |
| 738 | return true; |
| 739 | return false; |
| 740 | } |
| 741 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 742 | void WiFiService::Populate8021xProperties( |
| 743 | std::map<string, DBus::Variant> *params) { |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 744 | string ca_cert = eap().ca_cert; |
| 745 | if (!eap().ca_cert_nss.empty()) { |
| 746 | vector<char> id(ssid_.begin(), ssid_.end()); |
| 747 | FilePath certfile = nss_->GetDERCertfile(eap().ca_cert_nss, id); |
| 748 | if (certfile.empty()) { |
| 749 | LOG(ERROR) << "Unable to extract certificate: " << eap().ca_cert_nss; |
| 750 | } else { |
| 751 | ca_cert = certfile.value(); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 756 | typedef std::pair<const char *, const char *> KeyVal; |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 757 | KeyVal init_propertyvals[] = { |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 758 | KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()), |
| 759 | KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()), |
| 760 | KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap, |
| 761 | eap().inner_eap.c_str()), |
| 762 | KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity, |
| 763 | eap().anonymous_identity.c_str()), |
| 764 | KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert, |
| 765 | eap().client_cert.c_str()), |
| 766 | KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey, |
| 767 | eap().private_key.c_str()), |
| 768 | KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword, |
| 769 | eap().private_key_password.c_str()), |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 770 | KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, ca_cert.c_str()), |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 771 | KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword, |
| 772 | eap().password.c_str()), |
| 773 | KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()), |
| 774 | KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()), |
| 775 | KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId, |
| 776 | eap().ca_cert_id.c_str()), |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 777 | KeyVal(wpa_supplicant::kNetworkPropertyEapSubjectMatch, |
| 778 | eap().subject_match.c_str()) |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 779 | }; |
| 780 | |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 781 | vector<KeyVal> propertyvals(init_propertyvals, |
| 782 | init_propertyvals + arraysize(init_propertyvals)); |
| 783 | if (eap().use_system_cas) { |
| 784 | propertyvals.push_back(KeyVal( |
| 785 | wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)); |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 786 | } else if (ca_cert.empty()) { |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 787 | LOG(WARNING) << __func__ |
| 788 | << ": No certificate authorities are configured." |
| 789 | << " Server certificates will be accepted" |
| 790 | << " unconditionally."; |
| 791 | } |
| 792 | |
| 793 | if (!eap().cert_id.empty() || !eap().key_id.empty() || |
| 794 | !eap().ca_cert_id.empty()) { |
| 795 | propertyvals.push_back(KeyVal( |
| 796 | wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str())); |
| 797 | propertyvals.push_back(KeyVal( |
| 798 | wpa_supplicant::kNetworkPropertyEngineId, |
| 799 | wpa_supplicant::kEnginePKCS11)); |
| 800 | // We can't use the propertyvals vector for this since this argument |
| 801 | // is a uint32, not a string. |
| 802 | (*params)[wpa_supplicant::kNetworkPropertyEngine].writer(). |
| 803 | append_uint32(wpa_supplicant::kDefaultEngine); |
| 804 | } |
| 805 | |
| 806 | vector<KeyVal>::iterator it; |
| 807 | for (it = propertyvals.begin(); it != propertyvals.end(); ++it) { |
| 808 | if (strlen((*it).second) > 0) { |
| 809 | (*params)[(*it).first].writer().append_string((*it).second); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 814 | } // namespace shill |