blob: a362548ae70859bb13453a16bef4562fe1464069 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawalb54601c2011-06-07 17:39:22 -07002// 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
mukesh agrawal43970a22013-02-15 16:00:07 -08007#include <algorithm>
Wade Guthrie9ec08062013-09-25 15:22:24 -07008#include <limits>
mukesh agrawalb54601c2011-06-07 17:39:22 -07009#include <string>
Gaurav Shah10109f22011-11-11 20:16:22 -080010#include <utility>
mukesh agrawalb54601c2011-06-07 17:39:22 -070011
Ben Chana0ddf462014-02-06 11:32:42 -080012#include <base/strings/stringprintf.h>
13#include <base/strings/string_number_conversions.h>
14#include <base/strings/string_split.h>
15#include <base/strings/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070017#include <dbus/dbus.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070018
mukesh agrawale1d90e92012-02-15 17:36:08 -080019#include "shill/adaptor_interfaces.h"
Paul Stewart5baebb72013-03-14 11:43:29 -070020#include "shill/certificate_file.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070021#include "shill/control_interface.h"
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070022#include "shill/dbus_adaptor.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070023#include "shill/device.h"
Paul Stewartc43cbbe2013-04-11 06:29:30 -070024#include "shill/eap_credentials.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070025#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070026#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070027#include "shill/ieee80211.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070028#include "shill/logging.h"
Paul Stewart4357f4e2012-04-26 17:39:26 -070029#include "shill/manager.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000030#include "shill/metrics.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070031#include "shill/nss.h"
Thieu Lef7709452011-11-15 01:13:19 +000032#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070033#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070034#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070035#include "shill/wifi_endpoint.h"
Paul Stewart3c504012013-01-17 17:49:58 -080036#include "shill/wifi_provider.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070037#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070038
Paul Stewarta283e4e2013-10-22 20:50:14 -070039using std::map;
mukesh agrawal261daca2011-12-02 18:56:56 +000040using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070041using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070042using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070043
44namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070045
mukesh agrawalbf14e942012-03-02 14:36:34 -080046const char WiFiService::kAutoConnNoEndpoint[] = "no endpoints";
Paul Stewart3c504012013-01-17 17:49:58 -080047const char WiFiService::kAnyDeviceAddress[] = "any";
Paul Stewartbca08f82013-07-09 16:32:37 -070048const int WiFiService::kSuspectedCredentialFailureThreshold = 3;
mukesh agrawalbf14e942012-03-02 14:36:34 -080049
Paul Stewartd08f4432011-11-04 07:48:20 -070050const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080051const char WiFiService::kStorageMode[] = "WiFi.Mode";
52const char WiFiService::kStoragePassphrase[] = "Passphrase";
53const char WiFiService::kStorageSecurity[] = "WiFi.Security";
Paul Stewart71a4d3b2013-01-18 18:12:56 -080054const char WiFiService::kStorageSecurityClass[] = "WiFi.SecurityClass";
Paul Stewart2706aaf2011-12-14 16:44:04 -080055const char WiFiService::kStorageSSID[] = "SSID";
mukesh agrawale1d90e92012-02-15 17:36:08 -080056bool WiFiService::logged_signal_warning = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070057
mukesh agrawalb54601c2011-06-07 17:39:22 -070058WiFiService::WiFiService(ControlInterface *control_interface,
59 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080060 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070061 Manager *manager,
Paul Stewart3c504012013-01-17 17:49:58 -080062 WiFiProvider *provider,
Paul Stewarta41e38d2011-11-11 07:47:29 -080063 const vector<uint8_t> &ssid,
64 const string &mode,
65 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080066 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080067 : Service(control_interface, dispatcher, metrics, manager,
68 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070069 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070070 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070071 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080072 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000073 frequency_(0),
mukesh agrawalf6b32092013-04-10 15:49:55 -070074 physical_mode_(Metrics::kWiFiNetworkPhyModeUndef),
Paul Stewart23b393a2012-09-25 21:21:06 -070075 raw_signal_strength_(0),
mukesh agrawal43970a22013-02-15 16:00:07 -080076 cipher_8021x_(kCryptoNone),
Paul Stewartbca08f82013-07-09 16:32:37 -070077 suspected_credential_failures_(0),
Paul Stewartecf4cd12012-04-17 11:08:39 -070078 ssid_(ssid),
Paul Stewarta5e7d5f2013-01-09 18:06:15 -080079 ieee80211w_required_(false),
Peter Qiu49438222014-03-14 14:21:09 -070080 expecting_disconnect_(false),
Paul Stewart3c504012013-01-17 17:49:58 -080081 nss_(NSS::GetInstance()),
Paul Stewarteb713e82013-06-28 14:51:54 -070082 certificate_file_(new CertificateFile()),
Paul Stewart3c504012013-01-17 17:49:58 -080083 provider_(provider) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070084 PropertyStore *store = this->mutable_store();
Ben Chanf024ef42013-09-20 14:21:38 -070085 store->RegisterConstString(kModeProperty, &mode_);
86 HelpRegisterWriteOnlyDerivedString(kPassphraseProperty,
mukesh agrawal292dc0f2012-01-26 18:02:46 -080087 &WiFiService::SetPassphrase,
88 &WiFiService::ClearPassphrase,
89 NULL);
Ben Chanf024ef42013-09-20 14:21:38 -070090 store->RegisterBool(kPassphraseRequiredProperty, &need_passphrase_);
91 HelpRegisterDerivedString(kSecurityProperty,
Paul Stewart6df20bd2013-03-13 19:31:25 -070092 &WiFiService::GetSecurity,
93 NULL);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070094
Ben Chanf024ef42013-09-20 14:21:38 -070095 store->RegisterConstString(kWifiAuthMode, &auth_mode_);
96 store->RegisterBool(kWifiHiddenSsid, &hidden_ssid_);
97 store->RegisterConstUint16(kWifiFrequency, &frequency_);
mukesh agrawale7c7e652013-06-18 17:19:39 -070098 store->RegisterConstUint16s(kWifiFrequencyListProperty, &frequency_list_);
Ben Chanf024ef42013-09-20 14:21:38 -070099 store->RegisterConstUint16(kWifiPhyMode, &physical_mode_);
100 store->RegisterConstString(kWifiBSsid, &bssid_);
101 store->RegisterConstString(kCountryProperty, &country_code_);
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700102 store->RegisterConstStringmap(kWifiVendorInformationProperty,
103 &vendor_information_);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800104 store->RegisterConstBool(kWifiProtectedManagementFrameRequiredProperty,
105 &ieee80211w_required_);
mukesh agrawal32399322011-09-01 10:53:43 -0700106
mukesh agrawald835b202011-10-07 15:26:47 -0700107 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
Paul Stewart9d95dad2013-10-10 12:08:14 -0700108 store->RegisterConstString(kWifiHexSsid, &hex_ssid_);
109
mukesh agrawald835b202011-10-07 15:26:47 -0700110 string ssid_string(
111 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
Paul Stewart9d95dad2013-10-10 12:08:14 -0700112 WiFi::SanitizeSSID(&ssid_string);
mukesh agrawald835b202011-10-07 15:26:47 -0700113 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -0700114
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700115 SetEapCredentials(new EapCredentials());
116
mukesh agrawal6e277772011-09-29 15:04:23 -0700117 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
118 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -0800119 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800120 // Passphrases are not mandatory for 802.1X.
121 need_passphrase_ = false;
Ben Chanf024ef42013-09-20 14:21:38 -0700122 } else if (security_ == kSecurityPsk) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700123 SetEAPKeyManagement("WPA-PSK");
Ben Chanf024ef42013-09-20 14:21:38 -0700124 } else if (security_ == kSecurityRsn) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700125 SetEAPKeyManagement("WPA-PSK");
Ben Chanf024ef42013-09-20 14:21:38 -0700126 } else if (security_ == kSecurityWpa) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700127 SetEAPKeyManagement("WPA-PSK");
Ben Chanf024ef42013-09-20 14:21:38 -0700128 } else if (security_ == kSecurityWep) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700129 SetEAPKeyManagement("NONE");
Ben Chanf024ef42013-09-20 14:21:38 -0700130 } else if (security_ == kSecurityNone) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700131 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700132 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800133 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700134 }
135
Paul Stewartd08f4432011-11-04 07:48:20 -0700136 // Until we know better (at Profile load time), use the generic name.
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800137 storage_identifier_ = GetDefaultStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000138 UpdateConnectable();
mukesh agrawal43970a22013-02-15 16:00:07 -0800139 UpdateSecurity();
Paul Stewartcb59fed2012-03-21 21:14:46 -0700140
Ben Chanf024ef42013-09-20 14:21:38 -0700141 IgnoreParameterForConfigure(kModeProperty);
142 IgnoreParameterForConfigure(kSSIDProperty);
143 IgnoreParameterForConfigure(kSecurityProperty);
Paul Stewart4539d262013-10-10 12:56:31 -0700144 IgnoreParameterForConfigure(kWifiHexSsid);
Darin Petkov457728b2013-01-09 09:49:08 +0100145
mukesh agrawal6cfe53f2013-08-13 13:39:01 -0700146 InitializeCustomMetrics();
147
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700148 // Log the |unique_name| to |friendly_name| mapping for debugging purposes.
149 // The latter will be tagged for scrubbing.
150 LOG(INFO) << "Constructed WiFi service " << unique_name()
151 << " name: " << WiFi::LogSSID(friendly_name());
mukesh agrawalb54601c2011-06-07 17:39:22 -0700152}
153
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200154WiFiService::~WiFiService() {}
mukesh agrawalb54601c2011-06-07 17:39:22 -0700155
mukesh agrawalbf14e942012-03-02 14:36:34 -0800156bool WiFiService::IsAutoConnectable(const char **reason) const {
157 if (!Service::IsAutoConnectable(reason)) {
158 return false;
159 }
160
161 // Only auto-connect to Services which have visible Endpoints.
162 // (Needed because hidden Services may remain registered with
163 // Manager even without visible Endpoints.)
164 if (!HasEndpoints()) {
165 *reason = kAutoConnNoEndpoint;
166 return false;
167 }
168
Paul Stewart3c504012013-01-17 17:49:58 -0800169 CHECK(wifi_) << "We have endpoints but no WiFi device is selected?";
170
mukesh agrawalbf14e942012-03-02 14:36:34 -0800171 // Do not preempt an existing connection (whether pending, or
172 // connected, and whether to this service, or another).
173 if (!wifi_->IsIdle()) {
174 *reason = kAutoConnBusy;
175 return false;
176 }
177
178 return true;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000179}
180
mukesh agrawal43970a22013-02-15 16:00:07 -0800181void WiFiService::SetEAPKeyManagement(const string &key_management) {
182 Service::SetEAPKeyManagement(key_management);
183 UpdateSecurity();
184}
185
Darin Petkov4a66cc52012-06-15 10:08:29 +0200186void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000187 DCHECK(endpoint->ssid() == ssid());
188 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800189 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000190}
191
Darin Petkov4a66cc52012-06-15 10:08:29 +0200192void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000193 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
194 DCHECK(i != endpoints_.end());
195 if (i == endpoints_.end()) {
196 LOG(WARNING) << "In " << __func__ << "(): "
Darin Petkov457728b2013-01-09 09:49:08 +0100197 << "ignoring non-existent endpoint "
mukesh agrawal261daca2011-12-02 18:56:56 +0000198 << endpoint->bssid_string();
199 return;
200 }
201 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800202 if (current_endpoint_ == endpoint) {
203 current_endpoint_ = NULL;
204 }
205 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000206}
207
Paul Stewart3c504012013-01-17 17:49:58 -0800208void WiFiService::NotifyCurrentEndpoint(
209 const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800210 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
211 current_endpoint_ = endpoint;
212 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000213}
214
Paul Stewart3c504012013-01-17 17:49:58 -0800215void WiFiService::NotifyEndpointUpdated(
216 const WiFiEndpointConstRefPtr &endpoint) {
217 DCHECK(endpoints_.find(endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800218 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800219}
220
Chris Masone6515aab2011-10-12 16:19:09 -0700221string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700222 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700223}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700224
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700225bool WiFiService::SetPassphrase(const string &passphrase, Error *error) {
Ben Chanf024ef42013-09-20 14:21:38 -0700226 if (security_ == kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000227 ValidateWEPPassphrase(passphrase, error);
Ben Chanf024ef42013-09-20 14:21:38 -0700228 } else if (security_ == kSecurityPsk ||
229 security_ == kSecurityWpa ||
230 security_ == kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000231 ValidateWPAPassphrase(passphrase, error);
232 } else {
233 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700234 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000235
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700236 if (!error->IsSuccess()) {
237 return false;
238 }
239 if (passphrase_ == passphrase) {
240 // After a user logs in, Chrome may reconfigure a Service with the
241 // same credentials as before login. When that occurs, we don't
242 // want to bump the user off the network. Hence, we MUST return
243 // early. (See crbug.com/231456#c17)
244 return false;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800245 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000246
Paul Stewart835934a2012-12-06 19:27:09 -0800247 passphrase_ = passphrase;
248 ClearCachedCredentials();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000249 UpdateConnectable();
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700250 return true;
mukesh agrawal1a056262011-10-05 14:36:54 -0700251}
252
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800253// ClearPassphrase is separate from SetPassphrase, because the default
254// value for |passphrase_| would not pass validation.
255void WiFiService::ClearPassphrase(Error */*error*/) {
256 passphrase_.clear();
Paul Stewart835934a2012-12-06 19:27:09 -0800257 ClearCachedCredentials();
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800258 UpdateConnectable();
259}
260
Paul Stewartfa11e282013-12-02 22:04:25 -0800261string WiFiService::GetTethering(Error */*error*/) const {
262 if (IsConnected() && wifi_ && wifi_->IsConnectedViaTether()) {
263 return kTetheringConfirmedState;
264 }
265
266 // Only perform BSSID tests if there is exactly one matching endpoint,
267 // so we ignore campuses that may use locally administered BSSIDs.
Paul Stewart7f5d9c02013-12-03 18:26:00 -0800268 if (endpoints_.size() == 1 &&
Paul Stewartfa11e282013-12-02 22:04:25 -0800269 (*endpoints_.begin())->has_tethering_signature()) {
270 return kTetheringSuspectedState;
271 }
272
273 return kTetheringNotDetectedState;
274}
275
Paul Stewarte7de2942013-04-25 17:07:31 -0700276string WiFiService::GetLoadableStorageIdentifier(
277 const StoreInterface &storage) const {
278 set<string> groups = storage.GetGroupsWithProperties(GetStorageProperties());
279 if (groups.empty()) {
280 LOG(WARNING) << "Configuration for service "
281 << unique_name()
282 << " is not available in the persistent store";
283 return "";
284 }
285 if (groups.size() > 1) {
286 LOG(WARNING) << "More than one configuration for service "
287 << unique_name()
288 << " is available; choosing the first.";
289 }
290 return *groups.begin();
291}
292
293bool WiFiService::IsLoadableFrom(const StoreInterface &storage) const {
294 return !storage.GetGroupsWithProperties(GetStorageProperties()).empty();
Paul Stewartd08f4432011-11-04 07:48:20 -0700295}
296
Paul Stewarta41e38d2011-11-11 07:47:29 -0800297bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800298 // WiFi Services should be displayed only if they are in range (have
299 // endpoints that have shown up in a scan) or if the service is actively
300 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000301 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800302}
303
Paul Stewartd08f4432011-11-04 07:48:20 -0700304bool WiFiService::Load(StoreInterface *storage) {
Paul Stewarte7de2942013-04-25 17:07:31 -0700305 string id = GetLoadableStorageIdentifier(*storage);
306 if (id.empty()) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800307 return false;
Paul Stewartd08f4432011-11-04 07:48:20 -0700308 }
309
310 // Set our storage identifier to match the storage name in the Profile.
311 storage_identifier_ = id;
312
313 // Load properties common to all Services.
314 if (!Service::Load(storage)) {
315 return false;
316 }
317
318 // Load properties specific to WiFi services.
319 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000320
Paul Stewart2706aaf2011-12-14 16:44:04 -0800321 // NB: mode, security and ssid parameters are never read in from
322 // Load() as they are provided from the scan.
323
324 string passphrase;
325 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
326 Error error;
327 SetPassphrase(passphrase, &error);
Paul Stewartfa013ab2013-04-11 07:12:03 -0700328 if (!error.IsSuccess() &&
329 !(passphrase.empty() && error.type() == Error::kNotSupported)) {
Paul Stewart71b9ed52014-01-29 08:53:06 -0800330 LOG(ERROR) << "Passphrase could not be set: " << error;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800331 }
332 }
333
Peter Qiu49438222014-03-14 14:21:09 -0700334 expecting_disconnect_ = false;
Paul Stewartd08f4432011-11-04 07:48:20 -0700335 return true;
336}
337
338bool WiFiService::Save(StoreInterface *storage) {
339 // Save properties common to all Services.
340 if (!Service::Save(storage)) {
341 return false;
342 }
343
344 // Save properties specific to WiFi services.
345 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800346 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
347 storage->SetString(id, kStorageMode, mode_);
348 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
349 storage->SetString(id, kStorageSecurity, security_);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800350 storage->SetString(id, kStorageSecurityClass, GetSecurityClass(security_));
Paul Stewart2706aaf2011-12-14 16:44:04 -0800351 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000352
Paul Stewartd08f4432011-11-04 07:48:20 -0700353 return true;
354}
355
Paul Stewart65512e12012-03-26 18:01:08 -0700356bool WiFiService::Unload() {
Peter Qiu91b461f2014-03-26 10:19:45 -0700357 // Expect the service to be disconnected if is currently connected or
358 // in the process of connecting.
359 if (IsConnected() || IsConnecting()) {
360 expecting_disconnect_ = true;
361 } else {
362 expecting_disconnect_ = false;
363 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800364 Service::Unload();
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800365 if (wifi_) {
366 wifi_->DestroyServiceLease(*this);
367 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800368 hidden_ssid_ = false;
Paul Stewartbca08f82013-07-09 16:32:37 -0700369 ResetSuspectedCredentialFailures();
Wade Guthrie005bd342012-05-02 09:37:07 -0700370 Error unused_error;
371 ClearPassphrase(&unused_error);
Paul Stewart3c504012013-01-17 17:49:58 -0800372 return provider_->OnServiceUnloaded(this);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800373}
374
Paul Stewart6ab23a92011-11-09 17:17:47 -0800375bool WiFiService::IsSecurityMatch(const string &security) const {
376 return GetSecurityClass(security) == GetSecurityClass(security_);
377}
378
Paul Stewartbca08f82013-07-09 16:32:37 -0700379bool WiFiService::AddSuspectedCredentialFailure() {
380 if (!has_ever_connected()) {
381 return true;
382 }
383 ++suspected_credential_failures_;
384 return suspected_credential_failures_ >= kSuspectedCredentialFailureThreshold;
385}
386
387void WiFiService::ResetSuspectedCredentialFailures() {
388 suspected_credential_failures_ = 0;
389}
390
Thieu Le48e6d6d2011-12-06 00:40:27 +0000391void WiFiService::InitializeCustomMetrics() const {
mukesh agrawal6cfe53f2013-08-13 13:39:01 -0700392 SLOG(Metrics, 2) << __func__ << " for " << unique_name();
Thieu Le48e6d6d2011-12-06 00:40:27 +0000393 string histogram = metrics()->GetFullMetricName(
mukesh agrawal6cfe53f2013-08-13 13:39:01 -0700394 Metrics::kMetricTimeToJoinMilliseconds,
395 technology());
Wade Guthrie7ac610b2013-10-01 17:48:14 -0700396 metrics()->AddServiceStateTransitionTimer(*this,
Thieu Le48e6d6d2011-12-06 00:40:27 +0000397 histogram,
398 Service::kStateAssociating,
399 Service::kStateConfiguring);
400}
401
Thieu Leb84ba342012-03-02 15:15:19 -0800402void WiFiService::SendPostReadyStateMetrics(
403 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000404 metrics()->SendEnumToUMA(
405 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
406 technology()),
407 Metrics::WiFiFrequencyToChannel(frequency_),
408 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000409
410 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
411 metrics()->SendEnumToUMA(
412 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
413 technology()),
414 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
415 Metrics::kWiFiNetworkPhyModeMax);
416
Paul Stewart4108db92013-03-11 12:13:24 -0700417 string security_mode = security_;
418 if (current_endpoint_) {
419 security_mode = current_endpoint_->security_mode();
420 }
Thieu Lead1ec2c2012-01-05 23:39:48 +0000421 Metrics::WiFiSecurity security_uma =
Paul Stewart4108db92013-03-11 12:13:24 -0700422 Metrics::WiFiSecurityStringToEnum(security_mode);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000423 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
424 metrics()->SendEnumToUMA(
425 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
426 technology()),
427 security_uma,
428 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800429
Paul Stewart21f40962013-03-01 14:27:28 -0800430 if (Is8021x()) {
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700431 eap()->OutputConnectionMetrics(metrics(), technology());
Paul Stewart21f40962013-03-01 14:27:28 -0800432 }
433
Paul Stewart23b393a2012-09-25 21:21:06 -0700434 // We invert the sign of the signal strength value, since UMA histograms
435 // cannot represent negative numbers (it stores them but cannot display
436 // them), and dBm values of interest start at 0 and go negative from there.
437 metrics()->SendToUMA(
438 metrics()->GetFullMetricName(Metrics::kMetricNetworkSignalStrength,
439 technology()),
440 -raw_signal_strength_,
441 Metrics::kMetricNetworkSignalStrengthMin,
442 Metrics::kMetricNetworkSignalStrengthMax,
443 Metrics::kMetricNetworkSignalStrengthNumBuckets);
444
Thieu Leb84ba342012-03-02 15:15:19 -0800445 if (time_resume_to_ready_milliseconds > 0) {
446 metrics()->SendToUMA(
447 metrics()->GetFullMetricName(
448 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
449 time_resume_to_ready_milliseconds,
450 Metrics::kTimerHistogramMillisecondsMin,
451 Metrics::kTimerHistogramMillisecondsMax,
452 Metrics::kTimerHistogramNumBuckets);
453 }
Paul Stewarte4cedde2013-07-17 08:56:44 -0700454
455 Metrics::WiFiApMode ap_mode_uma = Metrics::WiFiApModeStringToEnum(mode_);
456 metrics()->SendEnumToUMA(
457 metrics()->GetFullMetricName(Metrics::kMetricNetworkApMode, technology()),
458 ap_mode_uma,
459 Metrics::kWiFiApModeMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000460}
461
mukesh agrawal32399322011-09-01 10:53:43 -0700462// private methods
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700463void WiFiService::HelpRegisterConstDerivedString(
464 const string &name,
465 string(WiFiService::*get)(Error *)) {
466 mutable_store()->RegisterDerivedString(
467 name,
468 StringAccessor(
469 new CustomAccessor<WiFiService, string>(this, get, NULL)));
470}
471
Paul Stewart6df20bd2013-03-13 19:31:25 -0700472void WiFiService::HelpRegisterDerivedString(
473 const string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700474 string(WiFiService::*get)(Error *error),
475 bool(WiFiService::*set)(const string &, Error *)) {
Paul Stewart6df20bd2013-03-13 19:31:25 -0700476 mutable_store()->RegisterDerivedString(
477 name,
478 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
479}
480
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800481void WiFiService::HelpRegisterWriteOnlyDerivedString(
482 const string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700483 bool(WiFiService::*set)(const string &, Error *),
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800484 void(WiFiService::*clear)(Error *),
485 const string *default_value) {
486 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000487 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800488 StringAccessor(
489 new CustomWriteOnlyAccessor<WiFiService, string>(
490 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000491}
492
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700493void WiFiService::Connect(Error *error, const char *reason) {
Wade Guthrie005bd342012-05-02 09:37:07 -0700494 if (!connectable()) {
Darin Petkov457728b2013-01-09 09:49:08 +0100495 LOG(ERROR) << "Can't connect. Service " << unique_name()
496 << " is not connectable.";
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700497 Error::PopulateAndLog(error,
498 Error::kOperationFailed,
499 Error::GetDefaultMessage(Error::kOperationFailed));
500 return;
501 }
502 if (IsConnecting() || IsConnected()) {
Darin Petkov457728b2013-01-09 09:49:08 +0100503 LOG(WARNING) << "Can't connect. Service " << unique_name()
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700504 << " is already connecting or connected.";
505 Error::PopulateAndLog(error,
506 Error::kAlreadyConnected,
507 Error::GetDefaultMessage(Error::kAlreadyConnected));
Wade Guthrie005bd342012-05-02 09:37:07 -0700508 return;
509 }
Paul Stewart3c504012013-01-17 17:49:58 -0800510
511 WiFiRefPtr wifi = wifi_;
512 if (!wifi) {
513 // If this is a hidden service before it has been found in a scan, we
514 // may need to late-bind to any available WiFi Device. We don't actually
Wade Guthrie9ec08062013-09-25 15:22:24 -0700515 // set |wifi_| in this case since we do not yet see any endpoints. This
Paul Stewart3c504012013-01-17 17:49:58 -0800516 // will mean this service is not disconnectable until an endpoint is
517 // found.
518 wifi = ChooseDevice();
519 if (!wifi) {
520 LOG(ERROR) << "Can't connect. Service " << unique_name()
521 << " cannot find a WiFi device.";
522 Error::PopulateAndLog(error,
523 Error::kOperationFailed,
524 Error::GetDefaultMessage(Error::kOperationFailed));
525 return;
526 }
527 }
528
529 if (wifi->IsCurrentService(this)) {
Darin Petkov457728b2013-01-09 09:49:08 +0100530 LOG(WARNING) << "Can't connect. Service " << unique_name()
Wade Guthrie8bc50882012-10-31 16:23:20 -0700531 << " is the current service (but, in " << GetStateString()
Paul Stewart3c504012013-01-17 17:49:58 -0800532 << " state, not connected).";
Wade Guthrie8bc50882012-10-31 16:23:20 -0700533 Error::PopulateAndLog(error,
534 Error::kInProgress,
535 Error::GetDefaultMessage(Error::kInProgress));
536 return;
537 }
Wade Guthrie005bd342012-05-02 09:37:07 -0700538
Peter Qiu574996a2014-04-04 10:55:47 -0700539 // Report number of BSSes available for this service.
540 metrics()->NotifyWifiAvailableBSSes(endpoints_.size());
541
Paul Stewarta283e4e2013-10-22 20:50:14 -0700542 if (Is8021x()) {
543 // If EAP key management is not set, set to a default.
544 if (GetEAPKeyManagement().empty())
545 SetEAPKeyManagement("WPA-EAP");
546 ClearEAPCertification();
547 }
548
Peter Qiu49438222014-03-14 14:21:09 -0700549 expecting_disconnect_ = false;
Paul Stewarta283e4e2013-10-22 20:50:14 -0700550 Service::Connect(error, reason);
551 wifi->ConnectTo(this);
552}
553
554DBusPropertiesMap WiFiService::GetSupplicantConfigurationParameters() const {
555 DBusPropertiesMap params;
556 DBus::MessageIter writer;
557
Paul Stewart0654ece2013-03-26 15:21:26 -0700558 params[WPASupplicant::kNetworkPropertyMode].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700559 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
560
Ben Chanf024ef42013-09-20 14:21:38 -0700561 if (mode_ == kModeAdhoc && frequency_ != 0) {
Wade Guthrie9ec08062013-09-25 15:22:24 -0700562 // Frequency is required in order to successfully connect to an IBSS
Paul Stewarte2d7c502012-07-16 16:35:10 -0700563 // with wpa_supplicant. If we have one from our endpoint, insert it
564 // here.
Paul Stewart0654ece2013-03-26 15:21:26 -0700565 params[WPASupplicant::kNetworkPropertyFrequency].writer().
Paul Stewarte2d7c502012-07-16 16:35:10 -0700566 append_int32(frequency_);
567 }
568
Gaurav Shah29d68882012-01-30 19:06:42 -0800569 if (Is8021x()) {
Paul Stewart0654ece2013-03-26 15:21:26 -0700570 vector<char> nss_identifier(ssid_.begin(), ssid_.end());
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700571 eap()->PopulateSupplicantProperties(
572 certificate_file_.get(), nss_, nss_identifier, &params);
Ben Chanf024ef42013-09-20 14:21:38 -0700573 } else if (security_ == kSecurityPsk ||
574 security_ == kSecurityRsn ||
575 security_ == kSecurityWpa) {
Ben Chana0ddf462014-02-06 11:32:42 -0800576 const string psk_proto =
577 base::StringPrintf("%s %s",
578 WPASupplicant::kSecurityModeWPA,
579 WPASupplicant::kSecurityModeRSN);
Paul Stewart0654ece2013-03-26 15:21:26 -0700580 params[WPASupplicant::kPropertySecurityProtocol].writer().
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800581 append_string(psk_proto.c_str());
Paul Stewart0654ece2013-03-26 15:21:26 -0700582 params[WPASupplicant::kPropertyPreSharedKey].writer().
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800583 append_string(passphrase_.c_str());
Ben Chanf024ef42013-09-20 14:21:38 -0700584 } else if (security_ == kSecurityWep) {
Paul Stewart0654ece2013-03-26 15:21:26 -0700585 params[WPASupplicant::kPropertyAuthAlg].writer().
586 append_string(WPASupplicant::kSecurityAuthAlg);
Paul Stewarta283e4e2013-10-22 20:50:14 -0700587 Error unused_error;
Thieu Lef4cbda92011-11-10 23:41:24 +0000588 int key_index;
589 std::vector<uint8> password_bytes;
Paul Stewarta283e4e2013-10-22 20:50:14 -0700590 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &unused_error);
Paul Stewart0654ece2013-03-26 15:21:26 -0700591 writer = params[WPASupplicant::kPropertyWEPKey +
Thieu Lef4cbda92011-11-10 23:41:24 +0000592 base::IntToString(key_index)].writer();
593 writer << password_bytes;
Paul Stewart0654ece2013-03-26 15:21:26 -0700594 params[WPASupplicant::kPropertyWEPTxKeyIndex].writer().
Thieu Lef4cbda92011-11-10 23:41:24 +0000595 append_uint32(key_index);
Ben Chanf024ef42013-09-20 14:21:38 -0700596 } else if (security_ == kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800597 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700598 } else {
Paul Stewarta283e4e2013-10-22 20:50:14 -0700599 NOTIMPLEMENTED() << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700600 }
601
Paul Stewart0654ece2013-03-26 15:21:26 -0700602 params[WPASupplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700603 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800604
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800605 if (ieee80211w_required_) {
606 // TODO(pstew): We should also enable IEEE 802.11w if the user
607 // explicitly enables support for this through a service / device
Paul Stewartee6b3d72013-07-12 16:07:51 -0700608 // property. crbug.com/219950
Paul Stewart0654ece2013-03-26 15:21:26 -0700609 params[WPASupplicant::kNetworkPropertyIeee80211w].writer().
610 append_uint32(WPASupplicant::kNetworkIeee80211wEnabled);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800611 }
612
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800613 // See note in dbus_adaptor.cc on why we need to use a local.
Paul Stewart0654ece2013-03-26 15:21:26 -0700614 writer = params[WPASupplicant::kNetworkPropertySSID].writer();
mukesh agrawal6e277772011-09-29 15:04:23 -0700615 writer << ssid_;
616
Paul Stewarta283e4e2013-10-22 20:50:14 -0700617 return params;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700618}
619
Paul Stewarta283e4e2013-10-22 20:50:14 -0700620
Eric Shienbrood9a245532012-03-07 14:20:39 -0500621void WiFiService::Disconnect(Error *error) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500622 Service::Disconnect(error);
Paul Stewart3c504012013-01-17 17:49:58 -0800623 if (!wifi_) {
624 // If we are connecting to a hidden service, but have not yet found
625 // any endpoints, we could end up with a disconnect request without
626 // a wifi_ reference. This is not a fatal error.
627 LOG_IF(ERROR, IsConnecting())
628 << "WiFi endpoints do not (yet) exist. Cannot disconnect service "
629 << unique_name();
630 LOG_IF(FATAL, IsConnected())
631 << "WiFi device does not exist. Cannot disconnect service "
632 << unique_name();
633 error->Populate(Error::kOperationFailed);
634 return;
635 }
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000636 wifi_->DisconnectFrom(this);
637}
638
Paul Stewart1cf7eb82013-12-03 19:34:36 -0800639string WiFiService::GetDeviceRpcId(Error *error) const {
Paul Stewart3c504012013-01-17 17:49:58 -0800640 if (!wifi_) {
641 error->Populate(Error::kNotFound, "Not associated with a device");
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700642 return DBusAdaptor::kNullPath;
Paul Stewart3c504012013-01-17 17:49:58 -0800643 }
Chris Masone95207da2011-06-29 16:50:49 -0700644 return wifi_->GetRpcIdentifier();
645}
646
mukesh agrawal29c13a12011-11-24 00:09:19 +0000647void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800648 bool is_connectable = false;
Ben Chanf024ef42013-09-20 14:21:38 -0700649 if (security_ == kSecurityNone) {
mukesh agrawal29c13a12011-11-24 00:09:19 +0000650 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800651 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800652 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800653 } else if (Is8021x()) {
654 is_connectable = Is8021xConnectable();
Ben Chanf024ef42013-09-20 14:21:38 -0700655 } else if (security_ == kSecurityWep ||
656 security_ == kSecurityWpa ||
657 security_ == kSecurityPsk ||
658 security_ == kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800659 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800660 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000661 }
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700662 SetConnectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000663}
664
mukesh agrawale1d90e92012-02-15 17:36:08 -0800665void WiFiService::UpdateFromEndpoints() {
666 const WiFiEndpoint *representative_endpoint = NULL;
667
668 if (current_endpoint_) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800669 representative_endpoint = current_endpoint_;
670 } else {
671 int16 best_signal = std::numeric_limits<int16>::min();
672 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
673 i != endpoints_.end(); ++i) {
674 if ((*i)->signal_strength() >= best_signal) {
675 best_signal = (*i)->signal_strength();
676 representative_endpoint = *i;
677 }
678 }
679 }
680
Paul Stewart3c504012013-01-17 17:49:58 -0800681 WiFiRefPtr wifi;
682 if (representative_endpoint) {
683 wifi = representative_endpoint->device();
Paul Stewart8653f462013-02-06 12:21:05 -0800684 } else if (IsConnected() || IsConnecting()) {
685 LOG(WARNING) << "Service " << unique_name()
686 << " will disconnect due to no remaining endpoints.";
Paul Stewart3c504012013-01-17 17:49:58 -0800687 }
688
689 SetWiFi(wifi);
690
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800691 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
692 i != endpoints_.end(); ++i) {
693 if ((*i)->ieee80211w_required()) {
694 // Never reset ieee80211w_required_ to false, so we track whether we have
695 // ever seen an AP that requires 802.11w.
696 ieee80211w_required_ = true;
697 }
698 }
699
mukesh agrawale7c7e652013-06-18 17:19:39 -0700700 set<uint16> frequency_set;
701 for (const auto &endpoint : endpoints_) {
702 frequency_set.insert(endpoint->frequency());
703 }
704 frequency_list_.assign(frequency_set.begin(), frequency_set.end());
705
mukesh agrawal43970a22013-02-15 16:00:07 -0800706 if (Is8021x())
707 cipher_8021x_ = ComputeCipher8021x(endpoints_);
708
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700709 uint16 frequency = 0;
710 int16 signal = std::numeric_limits<int16>::min();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700711 string bssid;
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700712 string country_code;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700713 Stringmap vendor_information;
mukesh agrawalf6b32092013-04-10 15:49:55 -0700714 uint16 physical_mode = Metrics::kWiFiNetworkPhyModeUndef;
Paul Stewart23b393a2012-09-25 21:21:06 -0700715 // Represent "unknown raw signal strength" as 0.
716 raw_signal_strength_ = 0;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700717 if (representative_endpoint) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800718 frequency = representative_endpoint->frequency();
719 signal = representative_endpoint->signal_strength();
Paul Stewart23b393a2012-09-25 21:21:06 -0700720 raw_signal_strength_ = signal;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700721 bssid = representative_endpoint->bssid_string();
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700722 country_code = representative_endpoint->country_code();
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700723 vendor_information = representative_endpoint->GetVendorInformation();
mukesh agrawalf6b32092013-04-10 15:49:55 -0700724 physical_mode = representative_endpoint->physical_mode();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800725 }
726
727 if (frequency_ != frequency) {
728 frequency_ = frequency;
Ben Chanf024ef42013-09-20 14:21:38 -0700729 adaptor()->EmitUint16Changed(kWifiFrequency, frequency_);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800730 }
mukesh agrawal923f14f2012-06-04 16:46:08 -0700731 if (bssid_ != bssid) {
732 bssid_ = bssid;
Ben Chanf024ef42013-09-20 14:21:38 -0700733 adaptor()->EmitStringChanged(kWifiBSsid, bssid_);
mukesh agrawal923f14f2012-06-04 16:46:08 -0700734 }
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700735 if (country_code_ != country_code) {
736 country_code_ = country_code;
Ben Chanf024ef42013-09-20 14:21:38 -0700737 adaptor()->EmitStringChanged(kCountryProperty, country_code_);
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700738 }
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700739 if (vendor_information_ != vendor_information) {
740 vendor_information_ = vendor_information;
741 adaptor()->EmitStringmapChanged(kWifiVendorInformationProperty,
742 vendor_information_);
743 }
mukesh agrawalf6b32092013-04-10 15:49:55 -0700744 if (physical_mode_ != physical_mode) {
745 physical_mode_ = physical_mode;
Ben Chanf024ef42013-09-20 14:21:38 -0700746 adaptor()->EmitUint16Changed(kWifiPhyMode, physical_mode_);
mukesh agrawalf6b32092013-04-10 15:49:55 -0700747 }
mukesh agrawale7c7e652013-06-18 17:19:39 -0700748 adaptor()->EmitUint16sChanged(kWifiFrequencyListProperty, frequency_list_);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800749 SetStrength(SignalToStrength(signal));
mukesh agrawal43970a22013-02-15 16:00:07 -0800750 UpdateSecurity();
751}
752
753void WiFiService::UpdateSecurity() {
754 CryptoAlgorithm algorithm = kCryptoNone;
755 bool key_rotation = false;
756 bool endpoint_auth = false;
757
Ben Chanf024ef42013-09-20 14:21:38 -0700758 if (security_ == kSecurityNone) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800759 // initial values apply
Ben Chanf024ef42013-09-20 14:21:38 -0700760 } else if (security_ == kSecurityWep) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800761 algorithm = kCryptoRc4;
762 key_rotation = Is8021x();
763 endpoint_auth = Is8021x();
Ben Chanf024ef42013-09-20 14:21:38 -0700764 } else if (security_ == kSecurityPsk ||
765 security_ == kSecurityWpa) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800766 algorithm = kCryptoRc4;
767 key_rotation = true;
768 endpoint_auth = false;
Ben Chanf024ef42013-09-20 14:21:38 -0700769 } else if (security_ == kSecurityRsn) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800770 algorithm = kCryptoAes;
771 key_rotation = true;
772 endpoint_auth = false;
Ben Chanf024ef42013-09-20 14:21:38 -0700773 } else if (security_ == kSecurity8021x) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800774 algorithm = cipher_8021x_;
775 key_rotation = true;
776 endpoint_auth = true;
777 }
778 SetSecurity(algorithm, key_rotation, endpoint_auth);
779}
780
781// static
782Service::CryptoAlgorithm WiFiService::ComputeCipher8021x(
783 const set<WiFiEndpointConstRefPtr> &endpoints) {
784
785 if (endpoints.empty())
786 return kCryptoNone; // Will update after scan results.
787
788 // Find weakest cipher (across endpoints) of the strongest ciphers
789 // (per endpoint).
790 Service::CryptoAlgorithm cipher = Service::kCryptoAes;
791 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints.begin();
792 i != endpoints.end(); ++i) {
793 Service::CryptoAlgorithm endpoint_cipher;
794 if ((*i)->has_rsn_property()) {
795 endpoint_cipher = Service::kCryptoAes;
796 } else if ((*i)->has_wpa_property()) {
797 endpoint_cipher = Service::kCryptoRc4;
798 } else {
799 // We could be in the Dynamic WEP case here. But that's okay,
800 // because |cipher_8021x_| is not defined in that case.
801 endpoint_cipher = Service::kCryptoNone;
802 }
803 cipher = std::min(cipher, endpoint_cipher);
804 }
805 return cipher;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800806}
807
mukesh agrawal1a056262011-10-05 14:36:54 -0700808// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000809void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
810 Error *error) {
811 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700812}
813
814// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000815void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
816 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700817 unsigned int length = passphrase.length();
818 vector<uint8> passphrase_bytes;
819
820 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
821 if (length != IEEE_80211::kWPAHexLen &&
822 (length < IEEE_80211::kWPAAsciiMinLen ||
823 length > IEEE_80211::kWPAAsciiMaxLen)) {
824 error->Populate(Error::kInvalidPassphrase);
825 }
826 } else {
827 if (length < IEEE_80211::kWPAAsciiMinLen ||
828 length > IEEE_80211::kWPAAsciiMaxLen) {
829 error->Populate(Error::kInvalidPassphrase);
830 }
831 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000832}
mukesh agrawal1a056262011-10-05 14:36:54 -0700833
Thieu Lef4cbda92011-11-10 23:41:24 +0000834// static
835void WiFiService::ParseWEPPassphrase(const string &passphrase,
836 int *key_index,
837 std::vector<uint8> *password_bytes,
838 Error *error) {
839 unsigned int length = passphrase.length();
840 int key_index_local;
841 std::string password_text;
842 bool is_hex = false;
843
844 switch (length) {
845 case IEEE_80211::kWEP40AsciiLen:
846 case IEEE_80211::kWEP104AsciiLen:
847 key_index_local = 0;
848 password_text = passphrase;
849 break;
850 case IEEE_80211::kWEP40AsciiLen + 2:
851 case IEEE_80211::kWEP104AsciiLen + 2:
852 if (CheckWEPKeyIndex(passphrase, error)) {
853 base::StringToInt(passphrase.substr(0,1), &key_index_local);
854 password_text = passphrase.substr(2);
855 }
856 break;
857 case IEEE_80211::kWEP40HexLen:
858 case IEEE_80211::kWEP104HexLen:
859 if (CheckWEPIsHex(passphrase, error)) {
860 key_index_local = 0;
861 password_text = passphrase;
862 is_hex = true;
863 }
864 break;
865 case IEEE_80211::kWEP40HexLen + 2:
866 case IEEE_80211::kWEP104HexLen + 2:
867 if(CheckWEPKeyIndex(passphrase, error) &&
868 CheckWEPIsHex(passphrase.substr(2), error)) {
869 base::StringToInt(passphrase.substr(0,1), &key_index_local);
870 password_text = passphrase.substr(2);
871 is_hex = true;
872 } else if (CheckWEPPrefix(passphrase, error) &&
873 CheckWEPIsHex(passphrase.substr(2), error)) {
874 key_index_local = 0;
875 password_text = passphrase.substr(2);
876 is_hex = true;
877 }
878 break;
879 case IEEE_80211::kWEP40HexLen + 4:
880 case IEEE_80211::kWEP104HexLen + 4:
881 if (CheckWEPKeyIndex(passphrase, error) &&
882 CheckWEPPrefix(passphrase.substr(2), error) &&
883 CheckWEPIsHex(passphrase.substr(4), error)) {
884 base::StringToInt(passphrase.substr(0,1), &key_index_local);
885 password_text = passphrase.substr(4);
886 is_hex = true;
887 }
888 break;
889 default:
890 error->Populate(Error::kInvalidPassphrase);
891 break;
892 }
893
mukesh agrawal1a056262011-10-05 14:36:54 -0700894 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000895 if (key_index)
896 *key_index = key_index_local;
897 if (password_bytes) {
898 if (is_hex)
899 base::HexStringToBytes(password_text, password_bytes);
900 else
901 password_bytes->insert(password_bytes->end(),
902 password_text.begin(),
903 password_text.end());
904 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700905 }
906}
907
908// static
909bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
910 vector<uint8> passphrase_bytes;
911 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
912 return true;
913 } else {
914 error->Populate(Error::kInvalidPassphrase);
915 return false;
916 }
917}
918
919// static
920bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
921 if (StartsWithASCII(passphrase, "0:", false) ||
922 StartsWithASCII(passphrase, "1:", false) ||
923 StartsWithASCII(passphrase, "2:", false) ||
924 StartsWithASCII(passphrase, "3:", false)) {
925 return true;
926 } else {
927 error->Populate(Error::kInvalidPassphrase);
928 return false;
929 }
930}
931
932// static
933bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
934 if (StartsWithASCII(passphrase, "0x", false)) {
935 return true;
936 } else {
937 error->Populate(Error::kInvalidPassphrase);
938 return false;
939 }
940}
941
Paul Stewart6ab23a92011-11-09 17:17:47 -0800942// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800943string WiFiService::GetSecurityClass(const string &security) {
Ben Chanf024ef42013-09-20 14:21:38 -0700944 if (security == kSecurityRsn ||
945 security == kSecurityWpa) {
946 return kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700947 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800948 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700949 }
950}
951
Wade Guthrie9ec08062013-09-25 15:22:24 -0700952
953int16 WiFiService::SignalLevel() const {
954 return current_endpoint_ ? current_endpoint_->signal_strength() :
955 std::numeric_limits<int16>::min();
956}
957
Paul Stewarta41e38d2011-11-11 07:47:29 -0800958// static
959bool WiFiService::ParseStorageIdentifier(const string &storage_name,
960 string *address,
961 string *mode,
962 string *security) {
963 vector<string> wifi_parts;
964 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800965 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
Ben Chanf024ef42013-09-20 14:21:38 -0700966 wifi_parts[0] != kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800967 return false;
968 }
969 *address = wifi_parts[1];
970 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800971 if (wifi_parts.size() == 5) {
972 *security = wifi_parts[4];
973 } else {
974 // Account for security type "802_1x" which got split up above.
975 *security = wifi_parts[4] + "_" + wifi_parts[5];
976 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800977 return true;
978}
979
mukesh agrawale1d90e92012-02-15 17:36:08 -0800980// static
Paul Stewart85aea152013-01-22 09:31:56 -0800981bool WiFiService::FixupServiceEntries(StoreInterface *storage) {
982 bool fixed_entry = false;
983 set<string> groups = storage->GetGroups();
984 for (set<string>::const_iterator it = groups.begin(); it != groups.end();
985 ++it) {
986 const string &id = *it;
987 string device_address, network_mode, security;
988 if (!ParseStorageIdentifier(id, &device_address,
989 &network_mode, &security)) {
990 continue;
991 }
992 if (!storage->GetString(id, kStorageType, NULL)) {
Ben Chanf024ef42013-09-20 14:21:38 -0700993 storage->SetString(id, kStorageType, kTypeWifi);
Paul Stewart85aea152013-01-22 09:31:56 -0800994 fixed_entry = true;
995 }
996 if (!storage->GetString(id, kStorageMode, NULL)) {
997 storage->SetString(id, kStorageMode, network_mode);
998 fixed_entry = true;
999 }
1000 if (!storage->GetString(id, kStorageSecurity, NULL)) {
1001 storage->SetString(id, kStorageSecurity, security);
1002 fixed_entry = true;
1003 }
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001004 if (!storage->GetString(id, kStorageSecurityClass, NULL)) {
1005 storage->SetString(id, kStorageSecurityClass, GetSecurityClass(security));
1006 fixed_entry = true;
1007 }
Paul Stewart85aea152013-01-22 09:31:56 -08001008 }
1009 return fixed_entry;
1010}
1011
1012// static
Paul Stewartd2e1c362013-03-03 19:06:07 -08001013bool WiFiService::IsValidMode(const string &mode) {
Ben Chanf024ef42013-09-20 14:21:38 -07001014 return mode == kModeManaged || mode == kModeAdhoc;
Paul Stewartd2e1c362013-03-03 19:06:07 -08001015}
1016
1017// static
Paul Stewart3c504012013-01-17 17:49:58 -08001018bool WiFiService::IsValidSecurityMethod(const string &method) {
Ben Chanf024ef42013-09-20 14:21:38 -07001019 return method == kSecurityNone ||
1020 method == kSecurityWep ||
1021 method == kSecurityPsk ||
1022 method == kSecurityWpa ||
1023 method == kSecurityRsn ||
1024 method == kSecurity8021x;
Paul Stewart3c504012013-01-17 17:49:58 -08001025}
1026
1027// static
mukesh agrawale1d90e92012-02-15 17:36:08 -08001028uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
1029 int16 strength;
1030 if (signal_dbm > 0) {
1031 if (!logged_signal_warning) {
1032 LOG(WARNING) << "Signal strength is suspiciously high. "
1033 << "Assuming value " << signal_dbm << " is not in dBm.";
1034 logged_signal_warning = true;
1035 }
1036 strength = signal_dbm;
1037 } else {
1038 strength = 120 + signal_dbm; // Call -20dBm "perfect".
1039 }
1040
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001041 if (strength > kStrengthMax) {
1042 strength = kStrengthMax;
1043 } else if (strength < kStrengthMin) {
1044 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -08001045 }
1046 return strength;
1047}
1048
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001049KeyValueStore WiFiService::GetStorageProperties() const {
1050 KeyValueStore args;
Ben Chanf024ef42013-09-20 14:21:38 -07001051 args.SetString(kStorageType, kTypeWifi);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001052 args.SetString(kStorageSSID, hex_ssid_);
1053 args.SetString(kStorageMode, mode_);
1054 args.SetString(kStorageSecurityClass, GetSecurityClass(security_));
1055 return args;
Paul Stewart6ab23a92011-11-09 17:17:47 -08001056}
1057
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001058string WiFiService::GetDefaultStorageIdentifier() const {
1059 string security = GetSecurityClass(security_);
1060 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
Ben Chanf024ef42013-09-20 14:21:38 -07001061 kTypeWifi,
Paul Stewart3c504012013-01-17 17:49:58 -08001062 kAnyDeviceAddress,
Paul Stewartd08f4432011-11-04 07:48:20 -07001063 hex_ssid_.c_str(),
1064 mode_.c_str(),
1065 security.c_str()));
1066}
1067
Paul Stewart6df20bd2013-03-13 19:31:25 -07001068string WiFiService::GetSecurity(Error */*error*/) {
1069 if (current_endpoint_) {
1070 return current_endpoint_->security_mode();
1071 }
1072 return security_;
1073}
1074
Paul Stewart835934a2012-12-06 19:27:09 -08001075void WiFiService::ClearCachedCredentials() {
Paul Stewart3c504012013-01-17 17:49:58 -08001076 if (wifi_) {
1077 wifi_->ClearCachedCredentials(this);
1078 }
Paul Stewart835934a2012-12-06 19:27:09 -08001079}
1080
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001081void WiFiService::OnEapCredentialsChanged() {
Paul Stewart835934a2012-12-06 19:27:09 -08001082 ClearCachedCredentials();
Gaurav Shah10109f22011-11-11 20:16:22 -08001083 UpdateConnectable();
1084}
1085
Paul Stewart4357f4e2012-04-26 17:39:26 -07001086void WiFiService::OnProfileConfigured() {
1087 if (profile() || !hidden_ssid()) {
1088 return;
1089 }
1090 // This situation occurs when a hidden WiFi service created via GetService
1091 // has been persisted to a profile in Manager::ConfigureService(). Now
1092 // that configuration is saved, we must join the service with its profile,
1093 // which will make this SSID eligible for directed probes during scans.
1094 manager()->RegisterService(this);
1095}
1096
Gaurav Shah29d68882012-01-30 19:06:42 -08001097bool WiFiService::Is8021x() const {
Ben Chanf024ef42013-09-20 14:21:38 -07001098 if (security_ == kSecurity8021x)
Gaurav Shah29d68882012-01-30 19:06:42 -08001099 return true;
1100
1101 // Dynamic WEP + 802.1x.
Ben Chanf024ef42013-09-20 14:21:38 -07001102 if (security_ == kSecurityWep &&
Paul Stewart9413bcc2013-04-04 16:12:43 -07001103 GetEAPKeyManagement() == WPASupplicant::kKeyManagementIeee8021X)
Gaurav Shah29d68882012-01-30 19:06:42 -08001104 return true;
1105 return false;
1106}
1107
Paul Stewart3c504012013-01-17 17:49:58 -08001108WiFiRefPtr WiFiService::ChooseDevice() {
Paul Stewartee6b3d72013-07-12 16:07:51 -07001109 // TODO(pstew): Style frowns on dynamic_cast. crbug.com/220387
Paul Stewart3c504012013-01-17 17:49:58 -08001110 DeviceRefPtr device =
1111 manager()->GetEnabledDeviceWithTechnology(Technology::kWifi);
1112 return dynamic_cast<WiFi *>(device.get());
1113}
1114
1115void WiFiService::ResetWiFi() {
1116 SetWiFi(NULL);
1117}
1118
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001119void WiFiService::SetWiFi(const WiFiRefPtr &new_wifi) {
1120 if (wifi_ == new_wifi) {
Paul Stewart3c504012013-01-17 17:49:58 -08001121 return;
1122 }
1123 ClearCachedCredentials();
1124 if (wifi_) {
1125 wifi_->DisassociateFromService(this);
1126 }
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001127 if (new_wifi) {
Ben Chanf024ef42013-09-20 14:21:38 -07001128 adaptor()->EmitRpcIdentifierChanged(kDeviceProperty,
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001129 new_wifi->GetRpcIdentifier());
1130 } else {
Ben Chanf024ef42013-09-20 14:21:38 -07001131 adaptor()->EmitRpcIdentifierChanged(kDeviceProperty,
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001132 DBusAdaptor::kNullPath);
1133 }
1134 wifi_ = new_wifi;
Paul Stewart3c504012013-01-17 17:49:58 -08001135}
1136
mukesh agrawalb54601c2011-06-07 17:39:22 -07001137} // namespace shill