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