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