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> |
| 8 | |
| 9 | #include <base/logging.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
| 11 | #include <base/string_number_conversions.h> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 12 | #include <base/string_split.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 13 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 15 | #include <dbus/dbus.h> |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 16 | #include <glib.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/control_interface.h" |
| 19 | #include "shill/device.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 20 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 21 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 22 | #include "shill/ieee80211.h" |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 23 | #include "shill/metrics.h" |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 24 | #include "shill/property_accessor.h" |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 25 | #include "shill/store_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 26 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 27 | #include "shill/wifi_endpoint.h" |
| 28 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 29 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 30 | using std::set; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 31 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 32 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 33 | |
| 34 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 35 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 36 | const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID"; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 37 | const char WiFiService::kStorageMode[] = "WiFi.Mode"; |
| 38 | const char WiFiService::kStoragePassphrase[] = "Passphrase"; |
| 39 | const char WiFiService::kStorageSecurity[] = "WiFi.Security"; |
| 40 | const char WiFiService::kStorageSSID[] = "SSID"; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 41 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 42 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 43 | EventDispatcher *dispatcher, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 44 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 45 | const WiFiRefPtr &device, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 46 | const vector<uint8_t> &ssid, |
| 47 | const string &mode, |
| 48 | const string &security, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 49 | bool hidden_ssid) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 50 | : Service(control_interface, dispatcher, manager, Technology::kWifi), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame] | 51 | need_passphrase_(false), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 52 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 53 | mode_(mode), |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 54 | hidden_ssid_(hidden_ssid), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 55 | task_factory_(this), |
| 56 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 57 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 58 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 59 | store->RegisterConstString(flimflam::kModeProperty, &mode_); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 60 | HelpRegisterDerivedString(store, |
| 61 | flimflam::kPassphraseProperty, |
| 62 | NULL, |
| 63 | &WiFiService::SetPassphrase); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 64 | store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_); |
| 65 | store->RegisterConstString(flimflam::kSecurityProperty, &security_); |
| 66 | store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 67 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 68 | store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_); |
| 69 | store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_); |
| 70 | store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_); |
| 71 | store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 72 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 73 | hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size()); |
| 74 | string ssid_string( |
| 75 | reinterpret_cast<const char *>(ssid_.data()), ssid_.size()); |
| 76 | if (SanitizeSSID(&ssid_string)) { |
| 77 | // WifiHexSsid property should only be present if Name property |
| 78 | // has been munged. |
| 79 | store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_); |
| 80 | } |
| 81 | set_friendly_name(ssid_string); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 82 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 83 | // TODO(quiche): determine if it is okay to set EAP.KeyManagement for |
| 84 | // a service that is not 802.1x. |
| 85 | if (security_ == flimflam::kSecurity8021x) { |
| 86 | NOTIMPLEMENTED(); |
| 87 | // XXX needs_passpharse_ = false ? |
| 88 | } else if (security_ == flimflam::kSecurityPsk) { |
| 89 | SetEAPKeyManagement("WPA-PSK"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 90 | } else if (security_ == flimflam::kSecurityRsn) { |
| 91 | SetEAPKeyManagement("WPA-PSK"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 92 | } else if (security_ == flimflam::kSecurityWpa) { |
| 93 | SetEAPKeyManagement("WPA-PSK"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 94 | } else if (security_ == flimflam::kSecurityWep) { |
| 95 | SetEAPKeyManagement("NONE"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 96 | } else if (security_ == flimflam::kSecurityNone) { |
| 97 | SetEAPKeyManagement("NONE"); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 98 | } else { |
| 99 | LOG(ERROR) << "unsupported security method " << security_; |
| 100 | } |
| 101 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 102 | // Until we know better (at Profile load time), use the generic name. |
| 103 | storage_identifier_ = GetGenericStorageIdentifier(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 104 | UpdateConnectable(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | WiFiService::~WiFiService() { |
| 108 | LOG(INFO) << __func__; |
| 109 | } |
| 110 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 111 | void WiFiService::AutoConnect() { |
| 112 | if (IsAutoConnectable()) { |
| 113 | // Execute immediately, for two reasons: |
| 114 | // |
| 115 | // 1. We need IsAutoConnectable to return the correct value for |
| 116 | // other WiFiServices, and that depends on WiFi's state. |
| 117 | // |
| 118 | // 2. We should probably limit the extent to which we queue up |
| 119 | // actions (such as AutoConnect) which depend on current state. |
| 120 | // If we queued AutoConnects, we could build a long queue of |
| 121 | // useless work (one AutoConnect per Service), which blocks |
| 122 | // more timely work. |
| 123 | ConnectTask(); |
mukesh agrawal | 592516d | 2012-01-12 14:01:00 -0800 | [diff] [blame] | 124 | } else { |
| 125 | LOG(INFO) << "Suppressed autoconnect to " << friendly_name(); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 129 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 130 | LOG(INFO) << __func__; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 131 | // Defer handling, since dbus-c++ does not permit us to send an |
| 132 | // outbound request while processing an inbound one. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 133 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 134 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 135 | } |
| 136 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 137 | void WiFiService::Disconnect(Error */*error*/) { |
| 138 | LOG(INFO) << __func__; |
| 139 | // Defer handling, since dbus-c++ does not permit us to send an |
| 140 | // outbound request while processing an inbound one. |
| 141 | dispatcher()->PostTask( |
| 142 | task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 145 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 146 | return wifi_->TechnologyIs(type); |
| 147 | } |
| 148 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 149 | bool WiFiService::IsAutoConnectable() const { |
| 150 | return connectable() |
| 151 | // Only auto-connect to Services which have visible Endpoints. |
| 152 | // (Needed because hidden Services may remain registered with |
| 153 | // Manager even without visible Endpoints.) |
| 154 | && HasEndpoints() |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame^] | 155 | // Do not preempt an existing connection (whether pending, or |
| 156 | // connected, and whether to this service, or another). |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 157 | && wifi_->IsIdle(); |
| 158 | } |
| 159 | |
| 160 | bool WiFiService::IsConnecting() const { |
| 161 | // WiFi does not move us into the associating state until it gets |
| 162 | // feedback from wpa_supplicant. So, to answer whether or |
| 163 | // not we're connecting, we consult with |wifi_|. |
| 164 | return wifi_->IsConnectingTo(*this); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 165 | } |
| 166 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 167 | void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) { |
| 168 | DCHECK(endpoint->ssid() == ssid()); |
| 169 | endpoints_.insert(endpoint); |
| 170 | } |
| 171 | |
| 172 | void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) { |
| 173 | set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint); |
| 174 | DCHECK(i != endpoints_.end()); |
| 175 | if (i == endpoints_.end()) { |
| 176 | LOG(WARNING) << "In " << __func__ << "(): " |
| 177 | << "ignorning non-existent endpoint " |
| 178 | << endpoint->bssid_string(); |
| 179 | return; |
| 180 | } |
| 181 | endpoints_.erase(i); |
| 182 | } |
| 183 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 184 | string WiFiService::GetStorageIdentifier() const { |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 185 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 186 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 187 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 188 | void WiFiService::SetPassphrase(const string &passphrase, Error *error) { |
| 189 | if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 190 | ValidateWEPPassphrase(passphrase, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 191 | } else if (security_ == flimflam::kSecurityPsk || |
| 192 | security_ == flimflam::kSecurityWpa || |
| 193 | security_ == flimflam::kSecurityRsn) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 194 | ValidateWPAPassphrase(passphrase, error); |
| 195 | } else { |
| 196 | error->Populate(Error::kNotSupported); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 197 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 198 | |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 199 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 200 | passphrase_ = passphrase; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 201 | } |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 202 | |
| 203 | UpdateConnectable(); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 206 | bool WiFiService::IsLoadableFrom(StoreInterface *storage) const { |
| 207 | return storage->ContainsGroup(GetGenericStorageIdentifier()) || |
| 208 | storage->ContainsGroup(GetSpecificStorageIdentifier()); |
| 209 | } |
| 210 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 211 | bool WiFiService::IsVisible() const { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 212 | // WiFi Services should be displayed only if they are in range (have |
| 213 | // endpoints that have shown up in a scan) or if the service is actively |
| 214 | // being connected. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 215 | return HasEndpoints() || IsConnected() || IsConnecting(); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 218 | bool WiFiService::Load(StoreInterface *storage) { |
| 219 | // First find out which storage identifier is available in priority order |
| 220 | // of specific, generic. |
| 221 | string id = GetSpecificStorageIdentifier(); |
| 222 | if (!storage->ContainsGroup(id)) { |
| 223 | id = GetGenericStorageIdentifier(); |
| 224 | if (!storage->ContainsGroup(id)) { |
| 225 | LOG(WARNING) << "Service is not available in the persistent store: " |
| 226 | << id; |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Set our storage identifier to match the storage name in the Profile. |
| 232 | storage_identifier_ = id; |
| 233 | |
| 234 | // Load properties common to all Services. |
| 235 | if (!Service::Load(storage)) { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | // Load properties specific to WiFi services. |
| 240 | storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 241 | |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 242 | // NB: mode, security and ssid parameters are never read in from |
| 243 | // Load() as they are provided from the scan. |
| 244 | |
| 245 | string passphrase; |
| 246 | if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) { |
| 247 | Error error; |
| 248 | SetPassphrase(passphrase, &error); |
| 249 | if (!error.IsSuccess()) { |
| 250 | LOG(ERROR) << "Passphrase could not be set: " |
| 251 | << Error::GetName(error.type()); |
| 252 | } |
| 253 | } |
| 254 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
| 258 | bool WiFiService::Save(StoreInterface *storage) { |
| 259 | // Save properties common to all Services. |
| 260 | if (!Service::Save(storage)) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | // Save properties specific to WiFi services. |
| 265 | const string id = GetStorageIdentifier(); |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 266 | storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_); |
| 267 | storage->SetString(id, kStorageMode, mode_); |
| 268 | storage->SetCryptedString(id, kStoragePassphrase, passphrase_); |
| 269 | storage->SetString(id, kStorageSecurity, security_); |
| 270 | storage->SetString(id, kStorageSSID, hex_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 271 | |
| 272 | // TODO(quiche): Save Passphrase property. (crosbug.com/23467) |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 276 | void WiFiService::Unload() { |
| 277 | Service::Unload(); |
| 278 | hidden_ssid_ = false; |
| 279 | passphrase_ = ""; |
| 280 | UpdateConnectable(); |
| 281 | } |
| 282 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 283 | bool WiFiService::IsSecurityMatch(const string &security) const { |
| 284 | return GetSecurityClass(security) == GetSecurityClass(security_); |
| 285 | } |
| 286 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 287 | void WiFiService::InitializeCustomMetrics() const { |
| 288 | string histogram = metrics()->GetFullMetricName( |
| 289 | Metrics::kMetricTimeToJoinMilliseconds, |
| 290 | technology()); |
| 291 | metrics()->AddServiceStateTransitionTimer(this, |
| 292 | histogram, |
| 293 | Service::kStateAssociating, |
| 294 | Service::kStateConfiguring); |
| 295 | } |
| 296 | |
| 297 | void WiFiService::SendPostReadyStateMetrics() const { |
| 298 | // TODO(thieule): Send physical mode and security metrics. |
| 299 | // crosbug.com/24441 |
| 300 | metrics()->SendEnumToUMA( |
| 301 | metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel, |
| 302 | technology()), |
| 303 | Metrics::WiFiFrequencyToChannel(frequency_), |
| 304 | Metrics::kMetricNetworkChannelMax); |
Thieu Le | ad1ec2c | 2012-01-05 23:39:48 +0000 | [diff] [blame] | 305 | |
| 306 | DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax); |
| 307 | metrics()->SendEnumToUMA( |
| 308 | metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode, |
| 309 | technology()), |
| 310 | static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_), |
| 311 | Metrics::kWiFiNetworkPhyModeMax); |
| 312 | |
| 313 | Metrics::WiFiSecurity security_uma = |
| 314 | Metrics::WiFiSecurityStringToEnum(security_); |
| 315 | DCHECK(security_uma != Metrics::kWiFiSecurityUnknown); |
| 316 | metrics()->SendEnumToUMA( |
| 317 | metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity, |
| 318 | technology()), |
| 319 | security_uma, |
| 320 | Metrics::kMetricNetworkSecurityMax); |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 321 | } |
| 322 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 323 | // private methods |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 324 | void WiFiService::HelpRegisterDerivedString( |
| 325 | PropertyStore *store, |
| 326 | const std::string &name, |
| 327 | std::string(WiFiService::*get)(Error *), |
| 328 | void(WiFiService::*set)(const std::string&, Error *)) { |
| 329 | store->RegisterDerivedString( |
| 330 | name, |
| 331 | StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set))); |
| 332 | } |
| 333 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 334 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 335 | std::map<string, DBus::Variant> params; |
| 336 | DBus::MessageIter writer; |
| 337 | |
| 338 | params[wpa_supplicant::kNetworkPropertyMode].writer(). |
| 339 | append_uint32(WiFiEndpoint::ModeStringToUint(mode_)); |
| 340 | |
| 341 | if (security_ == flimflam::kSecurity8021x) { |
| 342 | NOTIMPLEMENTED(); |
| 343 | } else if (security_ == flimflam::kSecurityPsk) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 344 | const string psk_proto = StringPrintf("%s %s", |
| 345 | wpa_supplicant::kSecurityModeWPA, |
| 346 | wpa_supplicant::kSecurityModeRSN); |
| 347 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 348 | append_string(psk_proto.c_str()); |
| 349 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 350 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 351 | } else if (security_ == flimflam::kSecurityRsn) { |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 352 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 353 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 354 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 355 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 356 | } else if (security_ == flimflam::kSecurityWpa) { |
| 357 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 358 | append_string(wpa_supplicant::kSecurityModeWPA); |
| 359 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 360 | append_string(passphrase_.c_str()); |
| 361 | } else if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 362 | params[wpa_supplicant::kPropertyAuthAlg].writer(). |
| 363 | append_string(wpa_supplicant::kSecurityAuthAlg); |
| 364 | Error error; |
| 365 | int key_index; |
| 366 | std::vector<uint8> password_bytes; |
| 367 | ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error); |
| 368 | writer = params[wpa_supplicant::kPropertyWEPKey + |
| 369 | base::IntToString(key_index)].writer(); |
| 370 | writer << password_bytes; |
| 371 | params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer(). |
| 372 | append_uint32(key_index); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 373 | } else if (security_ == flimflam::kSecurityNone) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 374 | // Nothing special to do here. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 375 | } else { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 376 | LOG(ERROR) << "Can't connect. Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | params[wpa_supplicant::kPropertyKeyManagement].writer(). |
| 380 | append_string(key_management().c_str()); |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 381 | |
| 382 | // 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] | 383 | writer = params[wpa_supplicant::kNetworkPropertySSID].writer(); |
| 384 | writer << ssid_; |
| 385 | |
| 386 | wifi_->ConnectTo(this, params); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 387 | } |
| 388 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 389 | void WiFiService::DisconnectTask() { |
| 390 | wifi_->DisconnectFrom(this); |
| 391 | } |
| 392 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 393 | string WiFiService::GetDeviceRpcId(Error */*error*/) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 394 | return wifi_->GetRpcIdentifier(); |
| 395 | } |
| 396 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 397 | void WiFiService::UpdateConnectable() { |
| 398 | if (security_ == flimflam::kSecurityNone) { |
| 399 | DCHECK(passphrase_.empty()); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 400 | need_passphrase_ = false; |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 401 | } else if (security_ == flimflam::kSecurityWep || |
| 402 | security_ == flimflam::kSecurityWpa || |
| 403 | security_ == flimflam::kSecurityPsk || |
| 404 | security_ == flimflam::kSecurityRsn) { |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 405 | need_passphrase_ = passphrase_.empty(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 406 | } else { |
| 407 | // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466) |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 408 | need_passphrase_ = true; |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 409 | } |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 410 | set_connectable(!need_passphrase_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 411 | } |
| 412 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 413 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 414 | void WiFiService::ValidateWEPPassphrase(const std::string &passphrase, |
| 415 | Error *error) { |
| 416 | ParseWEPPassphrase(passphrase, NULL, NULL, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 420 | void WiFiService::ValidateWPAPassphrase(const std::string &passphrase, |
| 421 | Error *error) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 422 | unsigned int length = passphrase.length(); |
| 423 | vector<uint8> passphrase_bytes; |
| 424 | |
| 425 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 426 | if (length != IEEE_80211::kWPAHexLen && |
| 427 | (length < IEEE_80211::kWPAAsciiMinLen || |
| 428 | length > IEEE_80211::kWPAAsciiMaxLen)) { |
| 429 | error->Populate(Error::kInvalidPassphrase); |
| 430 | } |
| 431 | } else { |
| 432 | if (length < IEEE_80211::kWPAAsciiMinLen || |
| 433 | length > IEEE_80211::kWPAAsciiMaxLen) { |
| 434 | error->Populate(Error::kInvalidPassphrase); |
| 435 | } |
| 436 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 437 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 438 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 439 | // static |
| 440 | void WiFiService::ParseWEPPassphrase(const string &passphrase, |
| 441 | int *key_index, |
| 442 | std::vector<uint8> *password_bytes, |
| 443 | Error *error) { |
| 444 | unsigned int length = passphrase.length(); |
| 445 | int key_index_local; |
| 446 | std::string password_text; |
| 447 | bool is_hex = false; |
| 448 | |
| 449 | switch (length) { |
| 450 | case IEEE_80211::kWEP40AsciiLen: |
| 451 | case IEEE_80211::kWEP104AsciiLen: |
| 452 | key_index_local = 0; |
| 453 | password_text = passphrase; |
| 454 | break; |
| 455 | case IEEE_80211::kWEP40AsciiLen + 2: |
| 456 | case IEEE_80211::kWEP104AsciiLen + 2: |
| 457 | if (CheckWEPKeyIndex(passphrase, error)) { |
| 458 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 459 | password_text = passphrase.substr(2); |
| 460 | } |
| 461 | break; |
| 462 | case IEEE_80211::kWEP40HexLen: |
| 463 | case IEEE_80211::kWEP104HexLen: |
| 464 | if (CheckWEPIsHex(passphrase, error)) { |
| 465 | key_index_local = 0; |
| 466 | password_text = passphrase; |
| 467 | is_hex = true; |
| 468 | } |
| 469 | break; |
| 470 | case IEEE_80211::kWEP40HexLen + 2: |
| 471 | case IEEE_80211::kWEP104HexLen + 2: |
| 472 | if(CheckWEPKeyIndex(passphrase, error) && |
| 473 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 474 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 475 | password_text = passphrase.substr(2); |
| 476 | is_hex = true; |
| 477 | } else if (CheckWEPPrefix(passphrase, error) && |
| 478 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 479 | key_index_local = 0; |
| 480 | password_text = passphrase.substr(2); |
| 481 | is_hex = true; |
| 482 | } |
| 483 | break; |
| 484 | case IEEE_80211::kWEP40HexLen + 4: |
| 485 | case IEEE_80211::kWEP104HexLen + 4: |
| 486 | if (CheckWEPKeyIndex(passphrase, error) && |
| 487 | CheckWEPPrefix(passphrase.substr(2), error) && |
| 488 | CheckWEPIsHex(passphrase.substr(4), error)) { |
| 489 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 490 | password_text = passphrase.substr(4); |
| 491 | is_hex = true; |
| 492 | } |
| 493 | break; |
| 494 | default: |
| 495 | error->Populate(Error::kInvalidPassphrase); |
| 496 | break; |
| 497 | } |
| 498 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 499 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 500 | if (key_index) |
| 501 | *key_index = key_index_local; |
| 502 | if (password_bytes) { |
| 503 | if (is_hex) |
| 504 | base::HexStringToBytes(password_text, password_bytes); |
| 505 | else |
| 506 | password_bytes->insert(password_bytes->end(), |
| 507 | password_text.begin(), |
| 508 | password_text.end()); |
| 509 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | // static |
| 514 | bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) { |
| 515 | vector<uint8> passphrase_bytes; |
| 516 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 517 | return true; |
| 518 | } else { |
| 519 | error->Populate(Error::kInvalidPassphrase); |
| 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // static |
| 525 | bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) { |
| 526 | if (StartsWithASCII(passphrase, "0:", false) || |
| 527 | StartsWithASCII(passphrase, "1:", false) || |
| 528 | StartsWithASCII(passphrase, "2:", false) || |
| 529 | StartsWithASCII(passphrase, "3:", false)) { |
| 530 | return true; |
| 531 | } else { |
| 532 | error->Populate(Error::kInvalidPassphrase); |
| 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // static |
| 538 | bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) { |
| 539 | if (StartsWithASCII(passphrase, "0x", false)) { |
| 540 | return true; |
| 541 | } else { |
| 542 | error->Populate(Error::kInvalidPassphrase); |
| 543 | return false; |
| 544 | } |
| 545 | } |
| 546 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 547 | // static |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 548 | bool WiFiService::SanitizeSSID(string *ssid) { |
| 549 | CHECK(ssid); |
| 550 | |
| 551 | size_t ssid_len = ssid->length(); |
| 552 | size_t i; |
| 553 | bool changed = false; |
| 554 | |
| 555 | for (i=0; i < ssid_len; ++i) { |
| 556 | if (!g_ascii_isprint((*ssid)[i])) { |
| 557 | (*ssid)[i] = '?'; |
| 558 | changed = true; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return changed; |
| 563 | } |
| 564 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 565 | // static |
| 566 | string WiFiService::GetSecurityClass(const string &security) { |
| 567 | if (security == flimflam::kSecurityRsn || |
| 568 | security == flimflam::kSecurityWpa) { |
| 569 | return flimflam::kSecurityPsk; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 570 | } else { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 571 | return security; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 575 | // static |
| 576 | bool WiFiService::ParseStorageIdentifier(const string &storage_name, |
| 577 | string *address, |
| 578 | string *mode, |
| 579 | string *security) { |
| 580 | vector<string> wifi_parts; |
| 581 | base::SplitString(storage_name, '_', &wifi_parts); |
| 582 | if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) { |
| 583 | return false; |
| 584 | } |
| 585 | *address = wifi_parts[1]; |
| 586 | *mode = wifi_parts[3]; |
| 587 | *security = wifi_parts[4]; |
| 588 | return true; |
| 589 | } |
| 590 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 591 | string WiFiService::GetGenericStorageIdentifier() const { |
| 592 | return GetStorageIdentifierForSecurity(GetSecurityClass(security_)); |
| 593 | } |
| 594 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 595 | string WiFiService::GetSpecificStorageIdentifier() const { |
| 596 | return GetStorageIdentifierForSecurity(security_); |
| 597 | } |
| 598 | |
| 599 | string WiFiService::GetStorageIdentifierForSecurity( |
| 600 | const string &security) const { |
| 601 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
| 602 | flimflam::kTypeWifi, |
| 603 | wifi_->address().c_str(), |
| 604 | hex_ssid_.c_str(), |
| 605 | mode_.c_str(), |
| 606 | security.c_str())); |
| 607 | } |
| 608 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 609 | } // namespace shill |