blob: 65551a16696b7d1873a4c5946ec8ffddaf370795 [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 agrawal132e96f2014-04-24 11:49:42 -0700394 Metrics::kMetricTimeToJoinMillisecondsSuffix,
mukesh agrawal6cfe53f2013-08-13 13:39:01 -0700395 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(
mukesh agrawal132e96f2014-04-24 11:49:42 -0700405 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannelSuffix,
Thieu Le48e6d6d2011-12-06 00:40:27 +0000406 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(
mukesh agrawal132e96f2014-04-24 11:49:42 -0700412 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyModeSuffix,
Thieu Lead1ec2c2012-01-05 23:39:48 +0000413 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(
mukesh agrawal132e96f2014-04-24 11:49:42 -0700425 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecuritySuffix,
Thieu Lead1ec2c2012-01-05 23:39:48 +0000426 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(
mukesh agrawal132e96f2014-04-24 11:49:42 -0700438 metrics()->GetFullMetricName(Metrics::kMetricNetworkSignalStrengthSuffix,
Paul Stewart23b393a2012-09-25 21:21:06 -0700439 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(
mukesh agrawal132e96f2014-04-24 11:49:42 -0700448 Metrics::kMetricTimeResumeToReadyMillisecondsSuffix, technology()),
Thieu Leb84ba342012-03-02 15:15:19 -0800449 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(
mukesh agrawal132e96f2014-04-24 11:49:42 -0700457 metrics()->GetFullMetricName(Metrics::kMetricNetworkApModeSuffix,
458 technology()),
Paul Stewarte4cedde2013-07-17 08:56:44 -0700459 ap_mode_uma,
460 Metrics::kWiFiApModeMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000461}
462
mukesh agrawal32399322011-09-01 10:53:43 -0700463// private methods
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700464void WiFiService::HelpRegisterConstDerivedString(
465 const string &name,
466 string(WiFiService::*get)(Error *)) {
467 mutable_store()->RegisterDerivedString(
468 name,
469 StringAccessor(
470 new CustomAccessor<WiFiService, string>(this, get, NULL)));
471}
472
Paul Stewart6df20bd2013-03-13 19:31:25 -0700473void WiFiService::HelpRegisterDerivedString(
474 const string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700475 string(WiFiService::*get)(Error *error),
476 bool(WiFiService::*set)(const string &, Error *)) {
Paul Stewart6df20bd2013-03-13 19:31:25 -0700477 mutable_store()->RegisterDerivedString(
478 name,
479 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
480}
481
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800482void WiFiService::HelpRegisterWriteOnlyDerivedString(
483 const string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700484 bool(WiFiService::*set)(const string &, Error *),
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800485 void(WiFiService::*clear)(Error *),
486 const string *default_value) {
487 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000488 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800489 StringAccessor(
490 new CustomWriteOnlyAccessor<WiFiService, string>(
491 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000492}
493
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700494void WiFiService::Connect(Error *error, const char *reason) {
Wade Guthrie005bd342012-05-02 09:37:07 -0700495 if (!connectable()) {
Darin Petkov457728b2013-01-09 09:49:08 +0100496 LOG(ERROR) << "Can't connect. Service " << unique_name()
497 << " is not connectable.";
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700498 Error::PopulateAndLog(error,
499 Error::kOperationFailed,
500 Error::GetDefaultMessage(Error::kOperationFailed));
501 return;
502 }
503 if (IsConnecting() || IsConnected()) {
Darin Petkov457728b2013-01-09 09:49:08 +0100504 LOG(WARNING) << "Can't connect. Service " << unique_name()
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700505 << " is already connecting or connected.";
506 Error::PopulateAndLog(error,
507 Error::kAlreadyConnected,
508 Error::GetDefaultMessage(Error::kAlreadyConnected));
Wade Guthrie005bd342012-05-02 09:37:07 -0700509 return;
510 }
Paul Stewart3c504012013-01-17 17:49:58 -0800511
512 WiFiRefPtr wifi = wifi_;
513 if (!wifi) {
514 // If this is a hidden service before it has been found in a scan, we
515 // may need to late-bind to any available WiFi Device. We don't actually
Wade Guthrie9ec08062013-09-25 15:22:24 -0700516 // set |wifi_| in this case since we do not yet see any endpoints. This
Paul Stewart3c504012013-01-17 17:49:58 -0800517 // will mean this service is not disconnectable until an endpoint is
518 // found.
519 wifi = ChooseDevice();
520 if (!wifi) {
521 LOG(ERROR) << "Can't connect. Service " << unique_name()
522 << " cannot find a WiFi device.";
523 Error::PopulateAndLog(error,
524 Error::kOperationFailed,
525 Error::GetDefaultMessage(Error::kOperationFailed));
526 return;
527 }
528 }
529
530 if (wifi->IsCurrentService(this)) {
Darin Petkov457728b2013-01-09 09:49:08 +0100531 LOG(WARNING) << "Can't connect. Service " << unique_name()
Wade Guthrie8bc50882012-10-31 16:23:20 -0700532 << " is the current service (but, in " << GetStateString()
Paul Stewart3c504012013-01-17 17:49:58 -0800533 << " state, not connected).";
Wade Guthrie8bc50882012-10-31 16:23:20 -0700534 Error::PopulateAndLog(error,
535 Error::kInProgress,
536 Error::GetDefaultMessage(Error::kInProgress));
537 return;
538 }
Wade Guthrie005bd342012-05-02 09:37:07 -0700539
Peter Qiu574996a2014-04-04 10:55:47 -0700540 // Report number of BSSes available for this service.
541 metrics()->NotifyWifiAvailableBSSes(endpoints_.size());
542
Paul Stewarta283e4e2013-10-22 20:50:14 -0700543 if (Is8021x()) {
544 // If EAP key management is not set, set to a default.
545 if (GetEAPKeyManagement().empty())
546 SetEAPKeyManagement("WPA-EAP");
547 ClearEAPCertification();
548 }
549
Peter Qiu49438222014-03-14 14:21:09 -0700550 expecting_disconnect_ = false;
Paul Stewarta283e4e2013-10-22 20:50:14 -0700551 Service::Connect(error, reason);
552 wifi->ConnectTo(this);
553}
554
555DBusPropertiesMap WiFiService::GetSupplicantConfigurationParameters() const {
556 DBusPropertiesMap params;
557 DBus::MessageIter writer;
558
Paul Stewart0654ece2013-03-26 15:21:26 -0700559 params[WPASupplicant::kNetworkPropertyMode].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700560 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
561
Ben Chanf024ef42013-09-20 14:21:38 -0700562 if (mode_ == kModeAdhoc && frequency_ != 0) {
Wade Guthrie9ec08062013-09-25 15:22:24 -0700563 // Frequency is required in order to successfully connect to an IBSS
Paul Stewarte2d7c502012-07-16 16:35:10 -0700564 // with wpa_supplicant. If we have one from our endpoint, insert it
565 // here.
Paul Stewart0654ece2013-03-26 15:21:26 -0700566 params[WPASupplicant::kNetworkPropertyFrequency].writer().
Paul Stewarte2d7c502012-07-16 16:35:10 -0700567 append_int32(frequency_);
568 }
569
Gaurav Shah29d68882012-01-30 19:06:42 -0800570 if (Is8021x()) {
Paul Stewart0654ece2013-03-26 15:21:26 -0700571 vector<char> nss_identifier(ssid_.begin(), ssid_.end());
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700572 eap()->PopulateSupplicantProperties(
573 certificate_file_.get(), nss_, nss_identifier, &params);
Ben Chanf024ef42013-09-20 14:21:38 -0700574 } else if (security_ == kSecurityPsk ||
575 security_ == kSecurityRsn ||
576 security_ == kSecurityWpa) {
Ben Chana0ddf462014-02-06 11:32:42 -0800577 const string psk_proto =
578 base::StringPrintf("%s %s",
579 WPASupplicant::kSecurityModeWPA,
580 WPASupplicant::kSecurityModeRSN);
Paul Stewart0654ece2013-03-26 15:21:26 -0700581 params[WPASupplicant::kPropertySecurityProtocol].writer().
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800582 append_string(psk_proto.c_str());
Paul Stewart0654ece2013-03-26 15:21:26 -0700583 params[WPASupplicant::kPropertyPreSharedKey].writer().
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800584 append_string(passphrase_.c_str());
Ben Chanf024ef42013-09-20 14:21:38 -0700585 } else if (security_ == kSecurityWep) {
Paul Stewart0654ece2013-03-26 15:21:26 -0700586 params[WPASupplicant::kPropertyAuthAlg].writer().
587 append_string(WPASupplicant::kSecurityAuthAlg);
Paul Stewarta283e4e2013-10-22 20:50:14 -0700588 Error unused_error;
Thieu Lef4cbda92011-11-10 23:41:24 +0000589 int key_index;
590 std::vector<uint8> password_bytes;
Paul Stewarta283e4e2013-10-22 20:50:14 -0700591 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &unused_error);
Paul Stewart0654ece2013-03-26 15:21:26 -0700592 writer = params[WPASupplicant::kPropertyWEPKey +
Thieu Lef4cbda92011-11-10 23:41:24 +0000593 base::IntToString(key_index)].writer();
594 writer << password_bytes;
Paul Stewart0654ece2013-03-26 15:21:26 -0700595 params[WPASupplicant::kPropertyWEPTxKeyIndex].writer().
Thieu Lef4cbda92011-11-10 23:41:24 +0000596 append_uint32(key_index);
Ben Chanf024ef42013-09-20 14:21:38 -0700597 } else if (security_ == kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800598 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700599 } else {
Paul Stewarta283e4e2013-10-22 20:50:14 -0700600 NOTIMPLEMENTED() << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700601 }
602
Paul Stewart0654ece2013-03-26 15:21:26 -0700603 params[WPASupplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700604 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800605
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800606 if (ieee80211w_required_) {
607 // TODO(pstew): We should also enable IEEE 802.11w if the user
608 // explicitly enables support for this through a service / device
Paul Stewartee6b3d72013-07-12 16:07:51 -0700609 // property. crbug.com/219950
Paul Stewart0654ece2013-03-26 15:21:26 -0700610 params[WPASupplicant::kNetworkPropertyIeee80211w].writer().
611 append_uint32(WPASupplicant::kNetworkIeee80211wEnabled);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800612 }
613
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800614 // See note in dbus_adaptor.cc on why we need to use a local.
Paul Stewart0654ece2013-03-26 15:21:26 -0700615 writer = params[WPASupplicant::kNetworkPropertySSID].writer();
mukesh agrawal6e277772011-09-29 15:04:23 -0700616 writer << ssid_;
617
Paul Stewarta283e4e2013-10-22 20:50:14 -0700618 return params;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700619}
620
Paul Stewarta283e4e2013-10-22 20:50:14 -0700621
Eric Shienbrood9a245532012-03-07 14:20:39 -0500622void WiFiService::Disconnect(Error *error) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500623 Service::Disconnect(error);
Paul Stewart3c504012013-01-17 17:49:58 -0800624 if (!wifi_) {
625 // If we are connecting to a hidden service, but have not yet found
626 // any endpoints, we could end up with a disconnect request without
627 // a wifi_ reference. This is not a fatal error.
628 LOG_IF(ERROR, IsConnecting())
629 << "WiFi endpoints do not (yet) exist. Cannot disconnect service "
630 << unique_name();
631 LOG_IF(FATAL, IsConnected())
632 << "WiFi device does not exist. Cannot disconnect service "
633 << unique_name();
634 error->Populate(Error::kOperationFailed);
635 return;
636 }
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000637 wifi_->DisconnectFrom(this);
638}
639
Paul Stewart1cf7eb82013-12-03 19:34:36 -0800640string WiFiService::GetDeviceRpcId(Error *error) const {
Paul Stewart3c504012013-01-17 17:49:58 -0800641 if (!wifi_) {
642 error->Populate(Error::kNotFound, "Not associated with a device");
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700643 return DBusAdaptor::kNullPath;
Paul Stewart3c504012013-01-17 17:49:58 -0800644 }
Chris Masone95207da2011-06-29 16:50:49 -0700645 return wifi_->GetRpcIdentifier();
646}
647
mukesh agrawal29c13a12011-11-24 00:09:19 +0000648void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800649 bool is_connectable = false;
Ben Chanf024ef42013-09-20 14:21:38 -0700650 if (security_ == kSecurityNone) {
mukesh agrawal29c13a12011-11-24 00:09:19 +0000651 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800652 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800653 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800654 } else if (Is8021x()) {
655 is_connectable = Is8021xConnectable();
Ben Chanf024ef42013-09-20 14:21:38 -0700656 } else if (security_ == kSecurityWep ||
657 security_ == kSecurityWpa ||
658 security_ == kSecurityPsk ||
659 security_ == kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800660 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800661 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000662 }
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700663 SetConnectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000664}
665
mukesh agrawale1d90e92012-02-15 17:36:08 -0800666void WiFiService::UpdateFromEndpoints() {
667 const WiFiEndpoint *representative_endpoint = NULL;
668
669 if (current_endpoint_) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800670 representative_endpoint = current_endpoint_;
671 } else {
672 int16 best_signal = std::numeric_limits<int16>::min();
Paul Stewart6db7b242014-05-02 15:34:21 -0700673 for (const auto &endpoint : endpoints_) {
674 if (endpoint->signal_strength() >= best_signal) {
675 best_signal = endpoint->signal_strength();
676 representative_endpoint = endpoint;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800677 }
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 Stewart6db7b242014-05-02 15:34:21 -0700691 for (const auto &endpoint : endpoints_) {
692 if (endpoint->ieee80211w_required()) {
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800693 // Never reset ieee80211w_required_ to false, so we track whether we have
694 // ever seen an AP that requires 802.11w.
695 ieee80211w_required_ = true;
696 }
697 }
698
mukesh agrawale7c7e652013-06-18 17:19:39 -0700699 set<uint16> frequency_set;
700 for (const auto &endpoint : endpoints_) {
701 frequency_set.insert(endpoint->frequency());
702 }
703 frequency_list_.assign(frequency_set.begin(), frequency_set.end());
704
mukesh agrawal43970a22013-02-15 16:00:07 -0800705 if (Is8021x())
706 cipher_8021x_ = ComputeCipher8021x(endpoints_);
707
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700708 uint16 frequency = 0;
709 int16 signal = std::numeric_limits<int16>::min();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700710 string bssid;
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700711 string country_code;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700712 Stringmap vendor_information;
mukesh agrawalf6b32092013-04-10 15:49:55 -0700713 uint16 physical_mode = Metrics::kWiFiNetworkPhyModeUndef;
Paul Stewart23b393a2012-09-25 21:21:06 -0700714 // Represent "unknown raw signal strength" as 0.
715 raw_signal_strength_ = 0;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700716 if (representative_endpoint) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800717 frequency = representative_endpoint->frequency();
718 signal = representative_endpoint->signal_strength();
Paul Stewart23b393a2012-09-25 21:21:06 -0700719 raw_signal_strength_ = signal;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700720 bssid = representative_endpoint->bssid_string();
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700721 country_code = representative_endpoint->country_code();
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700722 vendor_information = representative_endpoint->GetVendorInformation();
mukesh agrawalf6b32092013-04-10 15:49:55 -0700723 physical_mode = representative_endpoint->physical_mode();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800724 }
725
726 if (frequency_ != frequency) {
727 frequency_ = frequency;
Ben Chanf024ef42013-09-20 14:21:38 -0700728 adaptor()->EmitUint16Changed(kWifiFrequency, frequency_);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800729 }
mukesh agrawal923f14f2012-06-04 16:46:08 -0700730 if (bssid_ != bssid) {
731 bssid_ = bssid;
Ben Chanf024ef42013-09-20 14:21:38 -0700732 adaptor()->EmitStringChanged(kWifiBSsid, bssid_);
mukesh agrawal923f14f2012-06-04 16:46:08 -0700733 }
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700734 if (country_code_ != country_code) {
735 country_code_ = country_code;
Ben Chanf024ef42013-09-20 14:21:38 -0700736 adaptor()->EmitStringChanged(kCountryProperty, country_code_);
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700737 }
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700738 if (vendor_information_ != vendor_information) {
739 vendor_information_ = vendor_information;
740 adaptor()->EmitStringmapChanged(kWifiVendorInformationProperty,
741 vendor_information_);
742 }
mukesh agrawalf6b32092013-04-10 15:49:55 -0700743 if (physical_mode_ != physical_mode) {
744 physical_mode_ = physical_mode;
Ben Chanf024ef42013-09-20 14:21:38 -0700745 adaptor()->EmitUint16Changed(kWifiPhyMode, physical_mode_);
mukesh agrawalf6b32092013-04-10 15:49:55 -0700746 }
mukesh agrawale7c7e652013-06-18 17:19:39 -0700747 adaptor()->EmitUint16sChanged(kWifiFrequencyListProperty, frequency_list_);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800748 SetStrength(SignalToStrength(signal));
mukesh agrawal43970a22013-02-15 16:00:07 -0800749 UpdateSecurity();
750}
751
752void WiFiService::UpdateSecurity() {
753 CryptoAlgorithm algorithm = kCryptoNone;
754 bool key_rotation = false;
755 bool endpoint_auth = false;
756
Ben Chanf024ef42013-09-20 14:21:38 -0700757 if (security_ == kSecurityNone) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800758 // initial values apply
Ben Chanf024ef42013-09-20 14:21:38 -0700759 } else if (security_ == kSecurityWep) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800760 algorithm = kCryptoRc4;
761 key_rotation = Is8021x();
762 endpoint_auth = Is8021x();
Ben Chanf024ef42013-09-20 14:21:38 -0700763 } else if (security_ == kSecurityPsk ||
764 security_ == kSecurityWpa) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800765 algorithm = kCryptoRc4;
766 key_rotation = true;
767 endpoint_auth = false;
Ben Chanf024ef42013-09-20 14:21:38 -0700768 } else if (security_ == kSecurityRsn) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800769 algorithm = kCryptoAes;
770 key_rotation = true;
771 endpoint_auth = false;
Ben Chanf024ef42013-09-20 14:21:38 -0700772 } else if (security_ == kSecurity8021x) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800773 algorithm = cipher_8021x_;
774 key_rotation = true;
775 endpoint_auth = true;
776 }
777 SetSecurity(algorithm, key_rotation, endpoint_auth);
778}
779
780// static
781Service::CryptoAlgorithm WiFiService::ComputeCipher8021x(
782 const set<WiFiEndpointConstRefPtr> &endpoints) {
783
784 if (endpoints.empty())
785 return kCryptoNone; // Will update after scan results.
786
787 // Find weakest cipher (across endpoints) of the strongest ciphers
788 // (per endpoint).
789 Service::CryptoAlgorithm cipher = Service::kCryptoAes;
Paul Stewart6db7b242014-05-02 15:34:21 -0700790 for (const auto &endpoint : endpoints) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800791 Service::CryptoAlgorithm endpoint_cipher;
Paul Stewart6db7b242014-05-02 15:34:21 -0700792 if (endpoint->has_rsn_property()) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800793 endpoint_cipher = Service::kCryptoAes;
Paul Stewart6db7b242014-05-02 15:34:21 -0700794 } else if (endpoint->has_wpa_property()) {
mukesh agrawal43970a22013-02-15 16:00:07 -0800795 endpoint_cipher = Service::kCryptoRc4;
796 } else {
797 // We could be in the Dynamic WEP case here. But that's okay,
798 // because |cipher_8021x_| is not defined in that case.
799 endpoint_cipher = Service::kCryptoNone;
800 }
801 cipher = std::min(cipher, endpoint_cipher);
802 }
803 return cipher;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800804}
805
mukesh agrawal1a056262011-10-05 14:36:54 -0700806// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000807void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
808 Error *error) {
809 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700810}
811
812// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000813void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
814 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700815 unsigned int length = passphrase.length();
816 vector<uint8> passphrase_bytes;
817
818 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
819 if (length != IEEE_80211::kWPAHexLen &&
820 (length < IEEE_80211::kWPAAsciiMinLen ||
821 length > IEEE_80211::kWPAAsciiMaxLen)) {
822 error->Populate(Error::kInvalidPassphrase);
823 }
824 } else {
825 if (length < IEEE_80211::kWPAAsciiMinLen ||
826 length > IEEE_80211::kWPAAsciiMaxLen) {
827 error->Populate(Error::kInvalidPassphrase);
828 }
829 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000830}
mukesh agrawal1a056262011-10-05 14:36:54 -0700831
Thieu Lef4cbda92011-11-10 23:41:24 +0000832// static
833void WiFiService::ParseWEPPassphrase(const string &passphrase,
834 int *key_index,
835 std::vector<uint8> *password_bytes,
836 Error *error) {
837 unsigned int length = passphrase.length();
838 int key_index_local;
839 std::string password_text;
840 bool is_hex = false;
841
842 switch (length) {
843 case IEEE_80211::kWEP40AsciiLen:
844 case IEEE_80211::kWEP104AsciiLen:
845 key_index_local = 0;
846 password_text = passphrase;
847 break;
848 case IEEE_80211::kWEP40AsciiLen + 2:
849 case IEEE_80211::kWEP104AsciiLen + 2:
850 if (CheckWEPKeyIndex(passphrase, error)) {
851 base::StringToInt(passphrase.substr(0,1), &key_index_local);
852 password_text = passphrase.substr(2);
853 }
854 break;
855 case IEEE_80211::kWEP40HexLen:
856 case IEEE_80211::kWEP104HexLen:
857 if (CheckWEPIsHex(passphrase, error)) {
858 key_index_local = 0;
859 password_text = passphrase;
860 is_hex = true;
861 }
862 break;
863 case IEEE_80211::kWEP40HexLen + 2:
864 case IEEE_80211::kWEP104HexLen + 2:
865 if(CheckWEPKeyIndex(passphrase, error) &&
866 CheckWEPIsHex(passphrase.substr(2), error)) {
867 base::StringToInt(passphrase.substr(0,1), &key_index_local);
868 password_text = passphrase.substr(2);
869 is_hex = true;
870 } else if (CheckWEPPrefix(passphrase, error) &&
871 CheckWEPIsHex(passphrase.substr(2), error)) {
872 key_index_local = 0;
873 password_text = passphrase.substr(2);
874 is_hex = true;
875 }
876 break;
877 case IEEE_80211::kWEP40HexLen + 4:
878 case IEEE_80211::kWEP104HexLen + 4:
879 if (CheckWEPKeyIndex(passphrase, error) &&
880 CheckWEPPrefix(passphrase.substr(2), error) &&
881 CheckWEPIsHex(passphrase.substr(4), error)) {
882 base::StringToInt(passphrase.substr(0,1), &key_index_local);
883 password_text = passphrase.substr(4);
884 is_hex = true;
885 }
886 break;
887 default:
888 error->Populate(Error::kInvalidPassphrase);
889 break;
890 }
891
mukesh agrawal1a056262011-10-05 14:36:54 -0700892 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000893 if (key_index)
894 *key_index = key_index_local;
895 if (password_bytes) {
896 if (is_hex)
897 base::HexStringToBytes(password_text, password_bytes);
898 else
899 password_bytes->insert(password_bytes->end(),
900 password_text.begin(),
901 password_text.end());
902 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700903 }
904}
905
906// static
907bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
908 vector<uint8> passphrase_bytes;
909 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
910 return true;
911 } else {
912 error->Populate(Error::kInvalidPassphrase);
913 return false;
914 }
915}
916
917// static
918bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
919 if (StartsWithASCII(passphrase, "0:", false) ||
920 StartsWithASCII(passphrase, "1:", false) ||
921 StartsWithASCII(passphrase, "2:", false) ||
922 StartsWithASCII(passphrase, "3:", false)) {
923 return true;
924 } else {
925 error->Populate(Error::kInvalidPassphrase);
926 return false;
927 }
928}
929
930// static
931bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
932 if (StartsWithASCII(passphrase, "0x", false)) {
933 return true;
934 } else {
935 error->Populate(Error::kInvalidPassphrase);
936 return false;
937 }
938}
939
Paul Stewart6ab23a92011-11-09 17:17:47 -0800940// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800941string WiFiService::GetSecurityClass(const string &security) {
Ben Chanf024ef42013-09-20 14:21:38 -0700942 if (security == kSecurityRsn ||
943 security == kSecurityWpa) {
944 return kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700945 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800946 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700947 }
948}
949
Wade Guthrie9ec08062013-09-25 15:22:24 -0700950
951int16 WiFiService::SignalLevel() const {
952 return current_endpoint_ ? current_endpoint_->signal_strength() :
953 std::numeric_limits<int16>::min();
954}
955
Paul Stewarta41e38d2011-11-11 07:47:29 -0800956// static
957bool WiFiService::ParseStorageIdentifier(const string &storage_name,
958 string *address,
959 string *mode,
960 string *security) {
961 vector<string> wifi_parts;
962 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800963 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
Ben Chanf024ef42013-09-20 14:21:38 -0700964 wifi_parts[0] != kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800965 return false;
966 }
967 *address = wifi_parts[1];
968 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800969 if (wifi_parts.size() == 5) {
970 *security = wifi_parts[4];
971 } else {
972 // Account for security type "802_1x" which got split up above.
973 *security = wifi_parts[4] + "_" + wifi_parts[5];
974 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800975 return true;
976}
977
mukesh agrawale1d90e92012-02-15 17:36:08 -0800978// static
Paul Stewart85aea152013-01-22 09:31:56 -0800979bool WiFiService::FixupServiceEntries(StoreInterface *storage) {
980 bool fixed_entry = false;
981 set<string> groups = storage->GetGroups();
Paul Stewart6db7b242014-05-02 15:34:21 -0700982 for (const auto &id : groups) {
Paul Stewart85aea152013-01-22 09:31:56 -0800983 string device_address, network_mode, security;
984 if (!ParseStorageIdentifier(id, &device_address,
985 &network_mode, &security)) {
986 continue;
987 }
988 if (!storage->GetString(id, kStorageType, NULL)) {
Ben Chanf024ef42013-09-20 14:21:38 -0700989 storage->SetString(id, kStorageType, kTypeWifi);
Paul Stewart85aea152013-01-22 09:31:56 -0800990 fixed_entry = true;
991 }
992 if (!storage->GetString(id, kStorageMode, NULL)) {
993 storage->SetString(id, kStorageMode, network_mode);
994 fixed_entry = true;
995 }
996 if (!storage->GetString(id, kStorageSecurity, NULL)) {
997 storage->SetString(id, kStorageSecurity, security);
998 fixed_entry = true;
999 }
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001000 if (!storage->GetString(id, kStorageSecurityClass, NULL)) {
1001 storage->SetString(id, kStorageSecurityClass, GetSecurityClass(security));
1002 fixed_entry = true;
1003 }
Paul Stewart85aea152013-01-22 09:31:56 -08001004 }
1005 return fixed_entry;
1006}
1007
1008// static
Paul Stewartd2e1c362013-03-03 19:06:07 -08001009bool WiFiService::IsValidMode(const string &mode) {
Ben Chanf024ef42013-09-20 14:21:38 -07001010 return mode == kModeManaged || mode == kModeAdhoc;
Paul Stewartd2e1c362013-03-03 19:06:07 -08001011}
1012
1013// static
Paul Stewart3c504012013-01-17 17:49:58 -08001014bool WiFiService::IsValidSecurityMethod(const string &method) {
Ben Chanf024ef42013-09-20 14:21:38 -07001015 return method == kSecurityNone ||
1016 method == kSecurityWep ||
1017 method == kSecurityPsk ||
1018 method == kSecurityWpa ||
1019 method == kSecurityRsn ||
1020 method == kSecurity8021x;
Paul Stewart3c504012013-01-17 17:49:58 -08001021}
1022
1023// static
mukesh agrawale1d90e92012-02-15 17:36:08 -08001024uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
1025 int16 strength;
1026 if (signal_dbm > 0) {
1027 if (!logged_signal_warning) {
1028 LOG(WARNING) << "Signal strength is suspiciously high. "
1029 << "Assuming value " << signal_dbm << " is not in dBm.";
1030 logged_signal_warning = true;
1031 }
1032 strength = signal_dbm;
1033 } else {
1034 strength = 120 + signal_dbm; // Call -20dBm "perfect".
1035 }
1036
mukesh agrawal8f3f7752012-02-17 19:42:09 -08001037 if (strength > kStrengthMax) {
1038 strength = kStrengthMax;
1039 } else if (strength < kStrengthMin) {
1040 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -08001041 }
1042 return strength;
1043}
1044
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001045KeyValueStore WiFiService::GetStorageProperties() const {
1046 KeyValueStore args;
Ben Chanf024ef42013-09-20 14:21:38 -07001047 args.SetString(kStorageType, kTypeWifi);
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001048 args.SetString(kStorageSSID, hex_ssid_);
1049 args.SetString(kStorageMode, mode_);
1050 args.SetString(kStorageSecurityClass, GetSecurityClass(security_));
1051 return args;
Paul Stewart6ab23a92011-11-09 17:17:47 -08001052}
1053
Paul Stewart71a4d3b2013-01-18 18:12:56 -08001054string WiFiService::GetDefaultStorageIdentifier() const {
1055 string security = GetSecurityClass(security_);
1056 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
Ben Chanf024ef42013-09-20 14:21:38 -07001057 kTypeWifi,
Paul Stewart3c504012013-01-17 17:49:58 -08001058 kAnyDeviceAddress,
Paul Stewartd08f4432011-11-04 07:48:20 -07001059 hex_ssid_.c_str(),
1060 mode_.c_str(),
1061 security.c_str()));
1062}
1063
Paul Stewart6df20bd2013-03-13 19:31:25 -07001064string WiFiService::GetSecurity(Error */*error*/) {
1065 if (current_endpoint_) {
1066 return current_endpoint_->security_mode();
1067 }
1068 return security_;
1069}
1070
Paul Stewart835934a2012-12-06 19:27:09 -08001071void WiFiService::ClearCachedCredentials() {
Paul Stewart3c504012013-01-17 17:49:58 -08001072 if (wifi_) {
1073 wifi_->ClearCachedCredentials(this);
1074 }
Paul Stewart835934a2012-12-06 19:27:09 -08001075}
1076
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001077void WiFiService::OnEapCredentialsChanged() {
Paul Stewart835934a2012-12-06 19:27:09 -08001078 ClearCachedCredentials();
Gaurav Shah10109f22011-11-11 20:16:22 -08001079 UpdateConnectable();
1080}
1081
Paul Stewart4357f4e2012-04-26 17:39:26 -07001082void WiFiService::OnProfileConfigured() {
1083 if (profile() || !hidden_ssid()) {
1084 return;
1085 }
1086 // This situation occurs when a hidden WiFi service created via GetService
1087 // has been persisted to a profile in Manager::ConfigureService(). Now
1088 // that configuration is saved, we must join the service with its profile,
1089 // which will make this SSID eligible for directed probes during scans.
1090 manager()->RegisterService(this);
1091}
1092
Gaurav Shah29d68882012-01-30 19:06:42 -08001093bool WiFiService::Is8021x() const {
Ben Chanf024ef42013-09-20 14:21:38 -07001094 if (security_ == kSecurity8021x)
Gaurav Shah29d68882012-01-30 19:06:42 -08001095 return true;
1096
1097 // Dynamic WEP + 802.1x.
Ben Chanf024ef42013-09-20 14:21:38 -07001098 if (security_ == kSecurityWep &&
Paul Stewart9413bcc2013-04-04 16:12:43 -07001099 GetEAPKeyManagement() == WPASupplicant::kKeyManagementIeee8021X)
Gaurav Shah29d68882012-01-30 19:06:42 -08001100 return true;
1101 return false;
1102}
1103
Paul Stewart3c504012013-01-17 17:49:58 -08001104WiFiRefPtr WiFiService::ChooseDevice() {
Paul Stewartee6b3d72013-07-12 16:07:51 -07001105 // TODO(pstew): Style frowns on dynamic_cast. crbug.com/220387
Paul Stewart3c504012013-01-17 17:49:58 -08001106 DeviceRefPtr device =
1107 manager()->GetEnabledDeviceWithTechnology(Technology::kWifi);
1108 return dynamic_cast<WiFi *>(device.get());
1109}
1110
1111void WiFiService::ResetWiFi() {
1112 SetWiFi(NULL);
1113}
1114
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001115void WiFiService::SetWiFi(const WiFiRefPtr &new_wifi) {
1116 if (wifi_ == new_wifi) {
Paul Stewart3c504012013-01-17 17:49:58 -08001117 return;
1118 }
1119 ClearCachedCredentials();
1120 if (wifi_) {
1121 wifi_->DisassociateFromService(this);
1122 }
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001123 if (new_wifi) {
Ben Chanf024ef42013-09-20 14:21:38 -07001124 adaptor()->EmitRpcIdentifierChanged(kDeviceProperty,
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001125 new_wifi->GetRpcIdentifier());
1126 } else {
Ben Chanf024ef42013-09-20 14:21:38 -07001127 adaptor()->EmitRpcIdentifierChanged(kDeviceProperty,
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001128 DBusAdaptor::kNullPath);
1129 }
1130 wifi_ = new_wifi;
Paul Stewart3c504012013-01-17 17:49:58 -08001131}
1132
mukesh agrawalb54601c2011-06-07 17:39:22 -07001133} // namespace shill