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