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