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