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