blob: 4f2ba2ebdd94a3e6d6a097b1b64c5ffbe01b0919 [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>
mukesh agrawalb54601c2011-06-07 17:39:22 -07008#include <string>
Gaurav Shah10109f22011-11-11 20:16:22 -08009#include <utility>
mukesh agrawalb54601c2011-06-07 17:39:22 -070010
Chris Masone34af2182011-08-22 11:59:36 -070011#include <base/stringprintf.h>
12#include <base/string_number_conversions.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080013#include <base/string_split.h>
Chris Masone34af2182011-08-22 11:59:36 -070014#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070016#include <dbus/dbus.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070017
mukesh agrawale1d90e92012-02-15 17:36:08 -080018#include "shill/adaptor_interfaces.h"
Paul Stewart5baebb72013-03-14 11:43:29 -070019#include "shill/certificate_file.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070020#include "shill/control_interface.h"
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070021#include "shill/dbus_adaptor.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070022#include "shill/device.h"
Paul Stewartc43cbbe2013-04-11 06:29:30 -070023#include "shill/eap_credentials.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070024#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070025#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070026#include "shill/ieee80211.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070027#include "shill/logging.h"
Paul Stewart4357f4e2012-04-26 17:39:26 -070028#include "shill/manager.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000029#include "shill/metrics.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070030#include "shill/nss.h"
Thieu Lef7709452011-11-15 01:13:19 +000031#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070032#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070033#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070034#include "shill/wifi_endpoint.h"
Paul Stewart3c504012013-01-17 17:49:58 -080035#include "shill/wifi_provider.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070036#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070037
mukesh agrawal261daca2011-12-02 18:56:56 +000038using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070039using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070040using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070041
42namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070043
mukesh agrawalbf14e942012-03-02 14:36:34 -080044const char WiFiService::kAutoConnNoEndpoint[] = "no endpoints";
Paul Stewart3c504012013-01-17 17:49:58 -080045const char WiFiService::kAnyDeviceAddress[] = "any";
mukesh agrawalbf14e942012-03-02 14:36:34 -080046
Paul Stewartd08f4432011-11-04 07:48:20 -070047const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080048const char WiFiService::kStorageMode[] = "WiFi.Mode";
49const char WiFiService::kStoragePassphrase[] = "Passphrase";
50const char WiFiService::kStorageSecurity[] = "WiFi.Security";
Paul Stewart71a4d3b2013-01-18 18:12:56 -080051const char WiFiService::kStorageSecurityClass[] = "WiFi.SecurityClass";
Paul Stewart2706aaf2011-12-14 16:44:04 -080052const char WiFiService::kStorageSSID[] = "SSID";
mukesh agrawale1d90e92012-02-15 17:36:08 -080053bool WiFiService::logged_signal_warning = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070054
mukesh agrawalb54601c2011-06-07 17:39:22 -070055WiFiService::WiFiService(ControlInterface *control_interface,
56 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080057 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070058 Manager *manager,
Paul Stewart3c504012013-01-17 17:49:58 -080059 WiFiProvider *provider,
Paul Stewarta41e38d2011-11-11 07:47:29 -080060 const vector<uint8_t> &ssid,
61 const string &mode,
62 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080063 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080064 : Service(control_interface, dispatcher, metrics, manager,
65 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070066 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070067 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070068 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080069 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000070 frequency_(0),
mukesh agrawalf6b32092013-04-10 15:49:55 -070071 physical_mode_(Metrics::kWiFiNetworkPhyModeUndef),
Paul Stewart23b393a2012-09-25 21:21:06 -070072 raw_signal_strength_(0),
mukesh agrawal43970a22013-02-15 16:00:07 -080073 cipher_8021x_(kCryptoNone),
Paul Stewartecf4cd12012-04-17 11:08:39 -070074 ssid_(ssid),
Paul Stewarta5e7d5f2013-01-09 18:06:15 -080075 ieee80211w_required_(false),
Paul Stewart3c504012013-01-17 17:49:58 -080076 nss_(NSS::GetInstance()),
Paul Stewart5baebb72013-03-14 11:43:29 -070077 certificate_file_(new CertificateFile(manager->glib())),
Paul Stewart3c504012013-01-17 17:49:58 -080078 provider_(provider) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070079 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070080 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080081 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
82 &WiFiService::SetPassphrase,
83 &WiFiService::ClearPassphrase,
84 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070085 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
Paul Stewart6df20bd2013-03-13 19:31:25 -070086 HelpRegisterDerivedString(flimflam::kSecurityProperty,
87 &WiFiService::GetSecurity,
88 NULL);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070089
Paul Stewartac4ac002011-08-26 12:04:26 -070090 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
Paul Stewart0cab5682012-09-13 18:50:34 -070091 store->RegisterBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
Paul Stewartac4ac002011-08-26 12:04:26 -070092 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
93 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal923f14f2012-06-04 16:46:08 -070094 store->RegisterConstString(flimflam::kWifiBSsid, &bssid_);
Paul Stewartbdbd3c32013-04-17 09:47:21 -070095 store->RegisterConstString(flimflam::kCountryProperty, &country_code_);
Paul Stewart72b2fdc2012-06-02 08:58:51 -070096 store->RegisterConstStringmap(kWifiVendorInformationProperty,
97 &vendor_information_);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -080098 store->RegisterConstBool(kWifiProtectedManagementFrameRequiredProperty,
99 &ieee80211w_required_);
mukesh agrawal32399322011-09-01 10:53:43 -0700100
mukesh agrawald835b202011-10-07 15:26:47 -0700101 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
102 string ssid_string(
103 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -0800104 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -0700105 // WifiHexSsid property should only be present if Name property
106 // has been munged.
107 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
108 }
109 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -0700110
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700111 SetEapCredentials(new EapCredentials());
112
mukesh agrawal6e277772011-09-29 15:04:23 -0700113 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
114 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -0800115 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800116 // Passphrases are not mandatory for 802.1X.
117 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -0700118 } else if (security_ == flimflam::kSecurityPsk) {
119 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700120 } else if (security_ == flimflam::kSecurityRsn) {
121 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700122 } else if (security_ == flimflam::kSecurityWpa) {
123 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700124 } else if (security_ == flimflam::kSecurityWep) {
125 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700126 } else if (security_ == flimflam::kSecurityNone) {
127 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700128 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800129 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700130 }
131
Paul Stewartd08f4432011-11-04 07:48:20 -0700132 // Until we know better (at Profile load time), use the generic name.
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800133 storage_identifier_ = GetDefaultStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000134 UpdateConnectable();
mukesh agrawal43970a22013-02-15 16:00:07 -0800135 UpdateSecurity();
Paul Stewartcb59fed2012-03-21 21:14:46 -0700136
137 IgnoreParameterForConfigure(flimflam::kModeProperty);
138 IgnoreParameterForConfigure(flimflam::kSSIDProperty);
139 IgnoreParameterForConfigure(flimflam::kSecurityProperty);
Darin Petkov457728b2013-01-09 09:49:08 +0100140
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700141 // Log the |unique_name| to |friendly_name| mapping for debugging purposes.
142 // The latter will be tagged for scrubbing.
143 LOG(INFO) << "Constructed WiFi service " << unique_name()
144 << " name: " << WiFi::LogSSID(friendly_name());
mukesh agrawalb54601c2011-06-07 17:39:22 -0700145}
146
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200147WiFiService::~WiFiService() {}
mukesh agrawalb54601c2011-06-07 17:39:22 -0700148
mukesh agrawalbf14e942012-03-02 14:36:34 -0800149bool WiFiService::IsAutoConnectable(const char **reason) const {
150 if (!Service::IsAutoConnectable(reason)) {
151 return false;
152 }
153
154 // Only auto-connect to Services which have visible Endpoints.
155 // (Needed because hidden Services may remain registered with
156 // Manager even without visible Endpoints.)
157 if (!HasEndpoints()) {
158 *reason = kAutoConnNoEndpoint;
159 return false;
160 }
161
Paul Stewart3c504012013-01-17 17:49:58 -0800162 CHECK(wifi_) << "We have endpoints but no WiFi device is selected?";
163
mukesh agrawalbf14e942012-03-02 14:36:34 -0800164 // Do not preempt an existing connection (whether pending, or
165 // connected, and whether to this service, or another).
166 if (!wifi_->IsIdle()) {
167 *reason = kAutoConnBusy;
168 return false;
169 }
170
171 return true;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000172}
173
mukesh agrawal43970a22013-02-15 16:00:07 -0800174void WiFiService::SetEAPKeyManagement(const string &key_management) {
175 Service::SetEAPKeyManagement(key_management);
176 UpdateSecurity();
177}
178
Darin Petkov4a66cc52012-06-15 10:08:29 +0200179void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000180 DCHECK(endpoint->ssid() == ssid());
181 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800182 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000183}
184
Darin Petkov4a66cc52012-06-15 10:08:29 +0200185void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000186 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
187 DCHECK(i != endpoints_.end());
188 if (i == endpoints_.end()) {
189 LOG(WARNING) << "In " << __func__ << "(): "
Darin Petkov457728b2013-01-09 09:49:08 +0100190 << "ignoring non-existent endpoint "
mukesh agrawal261daca2011-12-02 18:56:56 +0000191 << endpoint->bssid_string();
192 return;
193 }
194 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800195 if (current_endpoint_ == endpoint) {
196 current_endpoint_ = NULL;
197 }
198 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000199}
200
Paul Stewart3c504012013-01-17 17:49:58 -0800201void WiFiService::NotifyCurrentEndpoint(
202 const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800203 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
204 current_endpoint_ = endpoint;
205 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000206}
207
Paul Stewart3c504012013-01-17 17:49:58 -0800208void WiFiService::NotifyEndpointUpdated(
209 const WiFiEndpointConstRefPtr &endpoint) {
210 DCHECK(endpoints_.find(endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800211 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800212}
213
Chris Masone6515aab2011-10-12 16:19:09 -0700214string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700215 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700216}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700217
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700218bool WiFiService::SetPassphrase(const string &passphrase, Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700219 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000220 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700221 } else if (security_ == flimflam::kSecurityPsk ||
222 security_ == flimflam::kSecurityWpa ||
223 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000224 ValidateWPAPassphrase(passphrase, error);
225 } else {
226 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700227 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000228
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700229 if (!error->IsSuccess()) {
230 return false;
231 }
232 if (passphrase_ == passphrase) {
233 // After a user logs in, Chrome may reconfigure a Service with the
234 // same credentials as before login. When that occurs, we don't
235 // want to bump the user off the network. Hence, we MUST return
236 // early. (See crbug.com/231456#c17)
237 return false;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800238 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000239
Paul Stewart835934a2012-12-06 19:27:09 -0800240 passphrase_ = passphrase;
241 ClearCachedCredentials();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000242 UpdateConnectable();
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700243 return true;
mukesh agrawal1a056262011-10-05 14:36:54 -0700244}
245
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800246// ClearPassphrase is separate from SetPassphrase, because the default
247// value for |passphrase_| would not pass validation.
248void WiFiService::ClearPassphrase(Error */*error*/) {
249 passphrase_.clear();
Paul Stewart835934a2012-12-06 19:27:09 -0800250 ClearCachedCredentials();
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800251 UpdateConnectable();
252}
253
Paul Stewarte7de2942013-04-25 17:07:31 -0700254string WiFiService::GetLoadableStorageIdentifier(
255 const StoreInterface &storage) const {
256 set<string> groups = storage.GetGroupsWithProperties(GetStorageProperties());
257 if (groups.empty()) {
258 LOG(WARNING) << "Configuration for service "
259 << unique_name()
260 << " is not available in the persistent store";
261 return "";
262 }
263 if (groups.size() > 1) {
264 LOG(WARNING) << "More than one configuration for service "
265 << unique_name()
266 << " is available; choosing the first.";
267 }
268 return *groups.begin();
269}
270
271bool WiFiService::IsLoadableFrom(const StoreInterface &storage) const {
272 return !storage.GetGroupsWithProperties(GetStorageProperties()).empty();
Paul Stewartd08f4432011-11-04 07:48:20 -0700273}
274
Paul Stewarta41e38d2011-11-11 07:47:29 -0800275bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800276 // WiFi Services should be displayed only if they are in range (have
277 // endpoints that have shown up in a scan) or if the service is actively
278 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000279 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800280}
281
Paul Stewartd08f4432011-11-04 07:48:20 -0700282bool WiFiService::Load(StoreInterface *storage) {
Paul Stewarte7de2942013-04-25 17:07:31 -0700283 string id = GetLoadableStorageIdentifier(*storage);
284 if (id.empty()) {
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800285 return false;
Paul Stewartd08f4432011-11-04 07:48:20 -0700286 }
287
288 // Set our storage identifier to match the storage name in the Profile.
289 storage_identifier_ = id;
290
291 // Load properties common to all Services.
292 if (!Service::Load(storage)) {
293 return false;
294 }
295
296 // Load properties specific to WiFi services.
297 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000298
Paul Stewart2706aaf2011-12-14 16:44:04 -0800299 // NB: mode, security and ssid parameters are never read in from
300 // Load() as they are provided from the scan.
301
302 string passphrase;
303 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
304 Error error;
305 SetPassphrase(passphrase, &error);
Paul Stewartfa013ab2013-04-11 07:12:03 -0700306 if (!error.IsSuccess() &&
307 !(passphrase.empty() && error.type() == Error::kNotSupported)) {
Paul Stewart2706aaf2011-12-14 16:44:04 -0800308 LOG(ERROR) << "Passphrase could not be set: "
309 << Error::GetName(error.type());
310 }
311 }
312
Paul Stewartd08f4432011-11-04 07:48:20 -0700313 return true;
314}
315
316bool WiFiService::Save(StoreInterface *storage) {
317 // Save properties common to all Services.
318 if (!Service::Save(storage)) {
319 return false;
320 }
321
322 // Save properties specific to WiFi services.
323 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800324 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
325 storage->SetString(id, kStorageMode, mode_);
326 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
327 storage->SetString(id, kStorageSecurity, security_);
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800328 storage->SetString(id, kStorageSecurityClass, GetSecurityClass(security_));
Paul Stewart2706aaf2011-12-14 16:44:04 -0800329 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000330
Paul Stewartd08f4432011-11-04 07:48:20 -0700331 return true;
332}
333
Paul Stewart65512e12012-03-26 18:01:08 -0700334bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800335 Service::Unload();
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800336 if (wifi_) {
337 wifi_->DestroyServiceLease(*this);
338 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800339 hidden_ssid_ = false;
Wade Guthrie005bd342012-05-02 09:37:07 -0700340 Error unused_error;
341 ClearPassphrase(&unused_error);
Paul Stewart3c504012013-01-17 17:49:58 -0800342 return provider_->OnServiceUnloaded(this);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800343}
344
Paul Stewart6ab23a92011-11-09 17:17:47 -0800345bool WiFiService::IsSecurityMatch(const string &security) const {
346 return GetSecurityClass(security) == GetSecurityClass(security_);
347}
348
Thieu Le48e6d6d2011-12-06 00:40:27 +0000349void WiFiService::InitializeCustomMetrics() const {
350 string histogram = metrics()->GetFullMetricName(
351 Metrics::kMetricTimeToJoinMilliseconds,
352 technology());
353 metrics()->AddServiceStateTransitionTimer(this,
354 histogram,
355 Service::kStateAssociating,
356 Service::kStateConfiguring);
357}
358
Thieu Leb84ba342012-03-02 15:15:19 -0800359void WiFiService::SendPostReadyStateMetrics(
360 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000361 metrics()->SendEnumToUMA(
362 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
363 technology()),
364 Metrics::WiFiFrequencyToChannel(frequency_),
365 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000366
367 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
368 metrics()->SendEnumToUMA(
369 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
370 technology()),
371 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
372 Metrics::kWiFiNetworkPhyModeMax);
373
Paul Stewart4108db92013-03-11 12:13:24 -0700374 string security_mode = security_;
375 if (current_endpoint_) {
376 security_mode = current_endpoint_->security_mode();
377 }
Thieu Lead1ec2c2012-01-05 23:39:48 +0000378 Metrics::WiFiSecurity security_uma =
Paul Stewart4108db92013-03-11 12:13:24 -0700379 Metrics::WiFiSecurityStringToEnum(security_mode);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000380 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
381 metrics()->SendEnumToUMA(
382 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
383 technology()),
384 security_uma,
385 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800386
Paul Stewart21f40962013-03-01 14:27:28 -0800387 if (Is8021x()) {
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700388 eap()->OutputConnectionMetrics(metrics(), technology());
Paul Stewart21f40962013-03-01 14:27:28 -0800389 }
390
Paul Stewart23b393a2012-09-25 21:21:06 -0700391 // We invert the sign of the signal strength value, since UMA histograms
392 // cannot represent negative numbers (it stores them but cannot display
393 // them), and dBm values of interest start at 0 and go negative from there.
394 metrics()->SendToUMA(
395 metrics()->GetFullMetricName(Metrics::kMetricNetworkSignalStrength,
396 technology()),
397 -raw_signal_strength_,
398 Metrics::kMetricNetworkSignalStrengthMin,
399 Metrics::kMetricNetworkSignalStrengthMax,
400 Metrics::kMetricNetworkSignalStrengthNumBuckets);
401
Thieu Leb84ba342012-03-02 15:15:19 -0800402 if (time_resume_to_ready_milliseconds > 0) {
403 metrics()->SendToUMA(
404 metrics()->GetFullMetricName(
405 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
406 time_resume_to_ready_milliseconds,
407 Metrics::kTimerHistogramMillisecondsMin,
408 Metrics::kTimerHistogramMillisecondsMax,
409 Metrics::kTimerHistogramNumBuckets);
410 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000411}
412
mukesh agrawal32399322011-09-01 10:53:43 -0700413// private methods
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700414void WiFiService::HelpRegisterConstDerivedString(
415 const string &name,
416 string(WiFiService::*get)(Error *)) {
417 mutable_store()->RegisterDerivedString(
418 name,
419 StringAccessor(
420 new CustomAccessor<WiFiService, string>(this, get, NULL)));
421}
422
Paul Stewart6df20bd2013-03-13 19:31:25 -0700423void WiFiService::HelpRegisterDerivedString(
424 const string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700425 string(WiFiService::*get)(Error *error),
426 bool(WiFiService::*set)(const string &, Error *)) {
Paul Stewart6df20bd2013-03-13 19:31:25 -0700427 mutable_store()->RegisterDerivedString(
428 name,
429 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
430}
431
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800432void WiFiService::HelpRegisterWriteOnlyDerivedString(
433 const string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700434 bool(WiFiService::*set)(const string &, Error *),
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800435 void(WiFiService::*clear)(Error *),
436 const string *default_value) {
437 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000438 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800439 StringAccessor(
440 new CustomWriteOnlyAccessor<WiFiService, string>(
441 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000442}
443
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700444void WiFiService::Connect(Error *error, const char *reason) {
mukesh agrawal6e277772011-09-29 15:04:23 -0700445 std::map<string, DBus::Variant> params;
446 DBus::MessageIter writer;
447
Wade Guthrie005bd342012-05-02 09:37:07 -0700448 if (!connectable()) {
Darin Petkov457728b2013-01-09 09:49:08 +0100449 LOG(ERROR) << "Can't connect. Service " << unique_name()
450 << " is not connectable.";
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700451 Error::PopulateAndLog(error,
452 Error::kOperationFailed,
453 Error::GetDefaultMessage(Error::kOperationFailed));
454 return;
455 }
456 if (IsConnecting() || IsConnected()) {
Darin Petkov457728b2013-01-09 09:49:08 +0100457 LOG(WARNING) << "Can't connect. Service " << unique_name()
Christopher Wiley1ce658d2012-10-10 10:02:03 -0700458 << " is already connecting or connected.";
459 Error::PopulateAndLog(error,
460 Error::kAlreadyConnected,
461 Error::GetDefaultMessage(Error::kAlreadyConnected));
Wade Guthrie005bd342012-05-02 09:37:07 -0700462 return;
463 }
Paul Stewart3c504012013-01-17 17:49:58 -0800464
465 WiFiRefPtr wifi = wifi_;
466 if (!wifi) {
467 // If this is a hidden service before it has been found in a scan, we
468 // may need to late-bind to any available WiFi Device. We don't actually
469 // set |wifi_| in this case snce we do not yet see any endpoints. This
470 // will mean this service is not disconnectable until an endpoint is
471 // found.
472 wifi = ChooseDevice();
473 if (!wifi) {
474 LOG(ERROR) << "Can't connect. Service " << unique_name()
475 << " cannot find a WiFi device.";
476 Error::PopulateAndLog(error,
477 Error::kOperationFailed,
478 Error::GetDefaultMessage(Error::kOperationFailed));
479 return;
480 }
481 }
482
483 if (wifi->IsCurrentService(this)) {
Darin Petkov457728b2013-01-09 09:49:08 +0100484 LOG(WARNING) << "Can't connect. Service " << unique_name()
Wade Guthrie8bc50882012-10-31 16:23:20 -0700485 << " is the current service (but, in " << GetStateString()
Paul Stewart3c504012013-01-17 17:49:58 -0800486 << " state, not connected).";
Wade Guthrie8bc50882012-10-31 16:23:20 -0700487 Error::PopulateAndLog(error,
488 Error::kInProgress,
489 Error::GetDefaultMessage(Error::kInProgress));
490 return;
491 }
Wade Guthrie005bd342012-05-02 09:37:07 -0700492
Paul Stewart0654ece2013-03-26 15:21:26 -0700493 params[WPASupplicant::kNetworkPropertyMode].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700494 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
495
Paul Stewarte2d7c502012-07-16 16:35:10 -0700496 if (mode_ == flimflam::kModeAdhoc && frequency_ != 0) {
497 // Frequency is required in order to successfully conntect to an IBSS
498 // with wpa_supplicant. If we have one from our endpoint, insert it
499 // here.
Paul Stewart0654ece2013-03-26 15:21:26 -0700500 params[WPASupplicant::kNetworkPropertyFrequency].writer().
Paul Stewarte2d7c502012-07-16 16:35:10 -0700501 append_int32(frequency_);
502 }
503
Gaurav Shah29d68882012-01-30 19:06:42 -0800504 if (Is8021x()) {
505 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800506 if (GetEAPKeyManagement().empty())
507 SetEAPKeyManagement("WPA-EAP");
Paul Stewart0654ece2013-03-26 15:21:26 -0700508 vector<char> nss_identifier(ssid_.begin(), ssid_.end());
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700509 eap()->PopulateSupplicantProperties(
510 certificate_file_.get(), nss_, nss_identifier, &params);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700511 ClearEAPCertification();
Paul Stewart4108db92013-03-11 12:13:24 -0700512 } else if (security_ == flimflam::kSecurityPsk ||
513 security_ == flimflam::kSecurityRsn ||
514 security_ == flimflam::kSecurityWpa) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800515 const string psk_proto = StringPrintf("%s %s",
Paul Stewart0654ece2013-03-26 15:21:26 -0700516 WPASupplicant::kSecurityModeWPA,
517 WPASupplicant::kSecurityModeRSN);
518 params[WPASupplicant::kPropertySecurityProtocol].writer().
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800519 append_string(psk_proto.c_str());
Paul Stewart0654ece2013-03-26 15:21:26 -0700520 params[WPASupplicant::kPropertyPreSharedKey].writer().
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800521 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700522 } else if (security_ == flimflam::kSecurityWep) {
Paul Stewart0654ece2013-03-26 15:21:26 -0700523 params[WPASupplicant::kPropertyAuthAlg].writer().
524 append_string(WPASupplicant::kSecurityAuthAlg);
Thieu Lef4cbda92011-11-10 23:41:24 +0000525 Error error;
526 int key_index;
527 std::vector<uint8> password_bytes;
528 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
Paul Stewart0654ece2013-03-26 15:21:26 -0700529 writer = params[WPASupplicant::kPropertyWEPKey +
Thieu Lef4cbda92011-11-10 23:41:24 +0000530 base::IntToString(key_index)].writer();
531 writer << password_bytes;
Paul Stewart0654ece2013-03-26 15:21:26 -0700532 params[WPASupplicant::kPropertyWEPTxKeyIndex].writer().
Thieu Lef4cbda92011-11-10 23:41:24 +0000533 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700534 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800535 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700536 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800537 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700538 Error::PopulateAndLog(error,
539 Error::kInvalidArguments,
540 Error::GetDefaultMessage(Error::kInvalidArguments));
541 return;
mukesh agrawal6e277772011-09-29 15:04:23 -0700542 }
543
Paul Stewart0654ece2013-03-26 15:21:26 -0700544 params[WPASupplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700545 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800546
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800547 if (ieee80211w_required_) {
548 // TODO(pstew): We should also enable IEEE 802.11w if the user
549 // explicitly enables support for this through a service / device
550 // property. crosbug.com/37800
Paul Stewart0654ece2013-03-26 15:21:26 -0700551 params[WPASupplicant::kNetworkPropertyIeee80211w].writer().
552 append_uint32(WPASupplicant::kNetworkIeee80211wEnabled);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800553 }
554
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800555 // See note in dbus_adaptor.cc on why we need to use a local.
Paul Stewart0654ece2013-03-26 15:21:26 -0700556 writer = params[WPASupplicant::kNetworkPropertySSID].writer();
mukesh agrawal6e277772011-09-29 15:04:23 -0700557 writer << ssid_;
558
mukesh agrawaldc7b8442012-09-27 13:48:14 -0700559 Service::Connect(error, reason);
Paul Stewart3c504012013-01-17 17:49:58 -0800560 wifi->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700561}
562
Eric Shienbrood9a245532012-03-07 14:20:39 -0500563void WiFiService::Disconnect(Error *error) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500564 Service::Disconnect(error);
Paul Stewart3c504012013-01-17 17:49:58 -0800565 if (!wifi_) {
566 // If we are connecting to a hidden service, but have not yet found
567 // any endpoints, we could end up with a disconnect request without
568 // a wifi_ reference. This is not a fatal error.
569 LOG_IF(ERROR, IsConnecting())
570 << "WiFi endpoints do not (yet) exist. Cannot disconnect service "
571 << unique_name();
572 LOG_IF(FATAL, IsConnected())
573 << "WiFi device does not exist. Cannot disconnect service "
574 << unique_name();
575 error->Populate(Error::kOperationFailed);
576 return;
577 }
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000578 wifi_->DisconnectFrom(this);
579}
580
Paul Stewart3c504012013-01-17 17:49:58 -0800581string WiFiService::GetDeviceRpcId(Error *error) {
582 if (!wifi_) {
583 error->Populate(Error::kNotFound, "Not associated with a device");
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700584 return DBusAdaptor::kNullPath;
Paul Stewart3c504012013-01-17 17:49:58 -0800585 }
Chris Masone95207da2011-06-29 16:50:49 -0700586 return wifi_->GetRpcIdentifier();
587}
588
mukesh agrawal29c13a12011-11-24 00:09:19 +0000589void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800590 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000591 if (security_ == flimflam::kSecurityNone) {
592 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800593 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800594 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800595 } else if (Is8021x()) {
596 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000597 } else if (security_ == flimflam::kSecurityWep ||
598 security_ == flimflam::kSecurityWpa ||
599 security_ == flimflam::kSecurityPsk ||
600 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800601 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800602 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000603 }
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700604 SetConnectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000605}
606
mukesh agrawale1d90e92012-02-15 17:36:08 -0800607void WiFiService::UpdateFromEndpoints() {
608 const WiFiEndpoint *representative_endpoint = NULL;
609
610 if (current_endpoint_) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800611 representative_endpoint = current_endpoint_;
612 } else {
613 int16 best_signal = std::numeric_limits<int16>::min();
614 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
615 i != endpoints_.end(); ++i) {
616 if ((*i)->signal_strength() >= best_signal) {
617 best_signal = (*i)->signal_strength();
618 representative_endpoint = *i;
619 }
620 }
621 }
622
Paul Stewart3c504012013-01-17 17:49:58 -0800623 WiFiRefPtr wifi;
624 if (representative_endpoint) {
625 wifi = representative_endpoint->device();
Paul Stewart8653f462013-02-06 12:21:05 -0800626 } else if (IsConnected() || IsConnecting()) {
627 LOG(WARNING) << "Service " << unique_name()
628 << " will disconnect due to no remaining endpoints.";
Paul Stewart3c504012013-01-17 17:49:58 -0800629 }
630
631 SetWiFi(wifi);
632
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800633 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
634 i != endpoints_.end(); ++i) {
635 if ((*i)->ieee80211w_required()) {
636 // Never reset ieee80211w_required_ to false, so we track whether we have
637 // ever seen an AP that requires 802.11w.
638 ieee80211w_required_ = true;
639 }
640 }
641
mukesh agrawal43970a22013-02-15 16:00:07 -0800642 if (Is8021x())
643 cipher_8021x_ = ComputeCipher8021x(endpoints_);
644
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700645 uint16 frequency = 0;
646 int16 signal = std::numeric_limits<int16>::min();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700647 string bssid;
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700648 string country_code;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700649 Stringmap vendor_information;
mukesh agrawalf6b32092013-04-10 15:49:55 -0700650 uint16 physical_mode = Metrics::kWiFiNetworkPhyModeUndef;
Paul Stewart23b393a2012-09-25 21:21:06 -0700651 // Represent "unknown raw signal strength" as 0.
652 raw_signal_strength_ = 0;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700653 if (representative_endpoint) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800654 frequency = representative_endpoint->frequency();
655 signal = representative_endpoint->signal_strength();
Paul Stewart23b393a2012-09-25 21:21:06 -0700656 raw_signal_strength_ = signal;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700657 bssid = representative_endpoint->bssid_string();
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700658 country_code = representative_endpoint->country_code();
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700659 vendor_information = representative_endpoint->GetVendorInformation();
mukesh agrawalf6b32092013-04-10 15:49:55 -0700660 physical_mode = representative_endpoint->physical_mode();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800661 }
662
663 if (frequency_ != frequency) {
664 frequency_ = frequency;
665 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
666 }
mukesh agrawal923f14f2012-06-04 16:46:08 -0700667 if (bssid_ != bssid) {
668 bssid_ = bssid;
669 adaptor()->EmitStringChanged(flimflam::kWifiBSsid, bssid_);
670 }
Paul Stewartbdbd3c32013-04-17 09:47:21 -0700671 if (country_code_ != country_code) {
672 country_code_ = country_code;
673 adaptor()->EmitStringChanged(flimflam::kCountryProperty, country_code_);
674 }
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700675 if (vendor_information_ != vendor_information) {
676 vendor_information_ = vendor_information;
677 adaptor()->EmitStringmapChanged(kWifiVendorInformationProperty,
678 vendor_information_);
679 }
mukesh agrawalf6b32092013-04-10 15:49:55 -0700680 if (physical_mode_ != physical_mode) {
681 physical_mode_ = physical_mode;
682 adaptor()->EmitUint16Changed(flimflam::kWifiPhyMode, physical_mode_);
683 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800684 SetStrength(SignalToStrength(signal));
mukesh agrawal43970a22013-02-15 16:00:07 -0800685 UpdateSecurity();
686}
687
688void WiFiService::UpdateSecurity() {
689 CryptoAlgorithm algorithm = kCryptoNone;
690 bool key_rotation = false;
691 bool endpoint_auth = false;
692
693 if (security_ == flimflam::kSecurityNone) {
694 // initial values apply
695 } else if (security_ == flimflam::kSecurityWep) {
696 algorithm = kCryptoRc4;
697 key_rotation = Is8021x();
698 endpoint_auth = Is8021x();
699 } else if (security_ == flimflam::kSecurityPsk ||
700 security_ == flimflam::kSecurityWpa) {
701 algorithm = kCryptoRc4;
702 key_rotation = true;
703 endpoint_auth = false;
704 } else if (security_ == flimflam::kSecurityRsn) {
705 algorithm = kCryptoAes;
706 key_rotation = true;
707 endpoint_auth = false;
708 } else if (security_ == flimflam::kSecurity8021x) {
709 algorithm = cipher_8021x_;
710 key_rotation = true;
711 endpoint_auth = true;
712 }
713 SetSecurity(algorithm, key_rotation, endpoint_auth);
714}
715
716// static
717Service::CryptoAlgorithm WiFiService::ComputeCipher8021x(
718 const set<WiFiEndpointConstRefPtr> &endpoints) {
719
720 if (endpoints.empty())
721 return kCryptoNone; // Will update after scan results.
722
723 // Find weakest cipher (across endpoints) of the strongest ciphers
724 // (per endpoint).
725 Service::CryptoAlgorithm cipher = Service::kCryptoAes;
726 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints.begin();
727 i != endpoints.end(); ++i) {
728 Service::CryptoAlgorithm endpoint_cipher;
729 if ((*i)->has_rsn_property()) {
730 endpoint_cipher = Service::kCryptoAes;
731 } else if ((*i)->has_wpa_property()) {
732 endpoint_cipher = Service::kCryptoRc4;
733 } else {
734 // We could be in the Dynamic WEP case here. But that's okay,
735 // because |cipher_8021x_| is not defined in that case.
736 endpoint_cipher = Service::kCryptoNone;
737 }
738 cipher = std::min(cipher, endpoint_cipher);
739 }
740 return cipher;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800741}
742
mukesh agrawal1a056262011-10-05 14:36:54 -0700743// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000744void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
745 Error *error) {
746 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700747}
748
749// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000750void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
751 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700752 unsigned int length = passphrase.length();
753 vector<uint8> passphrase_bytes;
754
755 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
756 if (length != IEEE_80211::kWPAHexLen &&
757 (length < IEEE_80211::kWPAAsciiMinLen ||
758 length > IEEE_80211::kWPAAsciiMaxLen)) {
759 error->Populate(Error::kInvalidPassphrase);
760 }
761 } else {
762 if (length < IEEE_80211::kWPAAsciiMinLen ||
763 length > IEEE_80211::kWPAAsciiMaxLen) {
764 error->Populate(Error::kInvalidPassphrase);
765 }
766 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000767}
mukesh agrawal1a056262011-10-05 14:36:54 -0700768
Thieu Lef4cbda92011-11-10 23:41:24 +0000769// static
770void WiFiService::ParseWEPPassphrase(const string &passphrase,
771 int *key_index,
772 std::vector<uint8> *password_bytes,
773 Error *error) {
774 unsigned int length = passphrase.length();
775 int key_index_local;
776 std::string password_text;
777 bool is_hex = false;
778
779 switch (length) {
780 case IEEE_80211::kWEP40AsciiLen:
781 case IEEE_80211::kWEP104AsciiLen:
782 key_index_local = 0;
783 password_text = passphrase;
784 break;
785 case IEEE_80211::kWEP40AsciiLen + 2:
786 case IEEE_80211::kWEP104AsciiLen + 2:
787 if (CheckWEPKeyIndex(passphrase, error)) {
788 base::StringToInt(passphrase.substr(0,1), &key_index_local);
789 password_text = passphrase.substr(2);
790 }
791 break;
792 case IEEE_80211::kWEP40HexLen:
793 case IEEE_80211::kWEP104HexLen:
794 if (CheckWEPIsHex(passphrase, error)) {
795 key_index_local = 0;
796 password_text = passphrase;
797 is_hex = true;
798 }
799 break;
800 case IEEE_80211::kWEP40HexLen + 2:
801 case IEEE_80211::kWEP104HexLen + 2:
802 if(CheckWEPKeyIndex(passphrase, error) &&
803 CheckWEPIsHex(passphrase.substr(2), error)) {
804 base::StringToInt(passphrase.substr(0,1), &key_index_local);
805 password_text = passphrase.substr(2);
806 is_hex = true;
807 } else if (CheckWEPPrefix(passphrase, error) &&
808 CheckWEPIsHex(passphrase.substr(2), error)) {
809 key_index_local = 0;
810 password_text = passphrase.substr(2);
811 is_hex = true;
812 }
813 break;
814 case IEEE_80211::kWEP40HexLen + 4:
815 case IEEE_80211::kWEP104HexLen + 4:
816 if (CheckWEPKeyIndex(passphrase, error) &&
817 CheckWEPPrefix(passphrase.substr(2), error) &&
818 CheckWEPIsHex(passphrase.substr(4), error)) {
819 base::StringToInt(passphrase.substr(0,1), &key_index_local);
820 password_text = passphrase.substr(4);
821 is_hex = true;
822 }
823 break;
824 default:
825 error->Populate(Error::kInvalidPassphrase);
826 break;
827 }
828
mukesh agrawal1a056262011-10-05 14:36:54 -0700829 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000830 if (key_index)
831 *key_index = key_index_local;
832 if (password_bytes) {
833 if (is_hex)
834 base::HexStringToBytes(password_text, password_bytes);
835 else
836 password_bytes->insert(password_bytes->end(),
837 password_text.begin(),
838 password_text.end());
839 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700840 }
841}
842
843// static
844bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
845 vector<uint8> passphrase_bytes;
846 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
847 return true;
848 } else {
849 error->Populate(Error::kInvalidPassphrase);
850 return false;
851 }
852}
853
854// static
855bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
856 if (StartsWithASCII(passphrase, "0:", false) ||
857 StartsWithASCII(passphrase, "1:", false) ||
858 StartsWithASCII(passphrase, "2:", false) ||
859 StartsWithASCII(passphrase, "3:", false)) {
860 return true;
861 } else {
862 error->Populate(Error::kInvalidPassphrase);
863 return false;
864 }
865}
866
867// static
868bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
869 if (StartsWithASCII(passphrase, "0x", false)) {
870 return true;
871 } else {
872 error->Populate(Error::kInvalidPassphrase);
873 return false;
874 }
875}
876
Paul Stewart6ab23a92011-11-09 17:17:47 -0800877// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800878string WiFiService::GetSecurityClass(const string &security) {
879 if (security == flimflam::kSecurityRsn ||
880 security == flimflam::kSecurityWpa) {
881 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700882 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800883 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700884 }
885}
886
Paul Stewarta41e38d2011-11-11 07:47:29 -0800887// static
888bool WiFiService::ParseStorageIdentifier(const string &storage_name,
889 string *address,
890 string *mode,
891 string *security) {
892 vector<string> wifi_parts;
893 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800894 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
895 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800896 return false;
897 }
898 *address = wifi_parts[1];
899 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800900 if (wifi_parts.size() == 5) {
901 *security = wifi_parts[4];
902 } else {
903 // Account for security type "802_1x" which got split up above.
904 *security = wifi_parts[4] + "_" + wifi_parts[5];
905 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800906 return true;
907}
908
mukesh agrawale1d90e92012-02-15 17:36:08 -0800909// static
Paul Stewart85aea152013-01-22 09:31:56 -0800910bool WiFiService::FixupServiceEntries(StoreInterface *storage) {
911 bool fixed_entry = false;
912 set<string> groups = storage->GetGroups();
913 for (set<string>::const_iterator it = groups.begin(); it != groups.end();
914 ++it) {
915 const string &id = *it;
916 string device_address, network_mode, security;
917 if (!ParseStorageIdentifier(id, &device_address,
918 &network_mode, &security)) {
919 continue;
920 }
921 if (!storage->GetString(id, kStorageType, NULL)) {
922 storage->SetString(id, kStorageType, flimflam::kTypeWifi);
923 fixed_entry = true;
924 }
925 if (!storage->GetString(id, kStorageMode, NULL)) {
926 storage->SetString(id, kStorageMode, network_mode);
927 fixed_entry = true;
928 }
929 if (!storage->GetString(id, kStorageSecurity, NULL)) {
930 storage->SetString(id, kStorageSecurity, security);
931 fixed_entry = true;
932 }
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800933 if (!storage->GetString(id, kStorageSecurityClass, NULL)) {
934 storage->SetString(id, kStorageSecurityClass, GetSecurityClass(security));
935 fixed_entry = true;
936 }
Paul Stewart85aea152013-01-22 09:31:56 -0800937 }
938 return fixed_entry;
939}
940
941// static
Paul Stewartd2e1c362013-03-03 19:06:07 -0800942bool WiFiService::IsValidMode(const string &mode) {
943 return mode == flimflam::kModeManaged ||
944 mode == flimflam::kModeAdhoc;
945}
946
947// static
Paul Stewart3c504012013-01-17 17:49:58 -0800948bool WiFiService::IsValidSecurityMethod(const string &method) {
949 return method == flimflam::kSecurityNone ||
950 method == flimflam::kSecurityWep ||
951 method == flimflam::kSecurityPsk ||
952 method == flimflam::kSecurityWpa ||
953 method == flimflam::kSecurityRsn ||
954 method == flimflam::kSecurity8021x;
955}
956
957// static
mukesh agrawale1d90e92012-02-15 17:36:08 -0800958uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
959 int16 strength;
960 if (signal_dbm > 0) {
961 if (!logged_signal_warning) {
962 LOG(WARNING) << "Signal strength is suspiciously high. "
963 << "Assuming value " << signal_dbm << " is not in dBm.";
964 logged_signal_warning = true;
965 }
966 strength = signal_dbm;
967 } else {
968 strength = 120 + signal_dbm; // Call -20dBm "perfect".
969 }
970
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800971 if (strength > kStrengthMax) {
972 strength = kStrengthMax;
973 } else if (strength < kStrengthMin) {
974 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800975 }
976 return strength;
977}
978
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800979KeyValueStore WiFiService::GetStorageProperties() const {
980 KeyValueStore args;
981 args.SetString(kStorageType, flimflam::kTypeWifi);
982 args.SetString(kStorageSSID, hex_ssid_);
983 args.SetString(kStorageMode, mode_);
984 args.SetString(kStorageSecurityClass, GetSecurityClass(security_));
985 return args;
Paul Stewart6ab23a92011-11-09 17:17:47 -0800986}
987
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800988string WiFiService::GetDefaultStorageIdentifier() const {
989 string security = GetSecurityClass(security_);
990 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
Paul Stewartd08f4432011-11-04 07:48:20 -0700991 flimflam::kTypeWifi,
Paul Stewart3c504012013-01-17 17:49:58 -0800992 kAnyDeviceAddress,
Paul Stewartd08f4432011-11-04 07:48:20 -0700993 hex_ssid_.c_str(),
994 mode_.c_str(),
995 security.c_str()));
996}
997
Paul Stewart6df20bd2013-03-13 19:31:25 -0700998string WiFiService::GetSecurity(Error */*error*/) {
999 if (current_endpoint_) {
1000 return current_endpoint_->security_mode();
1001 }
1002 return security_;
1003}
1004
Paul Stewart835934a2012-12-06 19:27:09 -08001005void WiFiService::ClearCachedCredentials() {
Paul Stewart3c504012013-01-17 17:49:58 -08001006 if (wifi_) {
1007 wifi_->ClearCachedCredentials(this);
1008 }
Paul Stewart835934a2012-12-06 19:27:09 -08001009}
1010
Paul Stewartc43cbbe2013-04-11 06:29:30 -07001011void WiFiService::OnEapCredentialsChanged() {
Paul Stewart835934a2012-12-06 19:27:09 -08001012 ClearCachedCredentials();
Gaurav Shah10109f22011-11-11 20:16:22 -08001013 UpdateConnectable();
1014}
1015
Paul Stewart4357f4e2012-04-26 17:39:26 -07001016void WiFiService::OnProfileConfigured() {
1017 if (profile() || !hidden_ssid()) {
1018 return;
1019 }
1020 // This situation occurs when a hidden WiFi service created via GetService
1021 // has been persisted to a profile in Manager::ConfigureService(). Now
1022 // that configuration is saved, we must join the service with its profile,
1023 // which will make this SSID eligible for directed probes during scans.
1024 manager()->RegisterService(this);
1025}
1026
Gaurav Shah29d68882012-01-30 19:06:42 -08001027bool WiFiService::Is8021x() const {
1028 if (security_ == flimflam::kSecurity8021x)
1029 return true;
1030
1031 // Dynamic WEP + 802.1x.
1032 if (security_ == flimflam::kSecurityWep &&
Paul Stewart9413bcc2013-04-04 16:12:43 -07001033 GetEAPKeyManagement() == WPASupplicant::kKeyManagementIeee8021X)
Gaurav Shah29d68882012-01-30 19:06:42 -08001034 return true;
1035 return false;
1036}
1037
Paul Stewart3c504012013-01-17 17:49:58 -08001038WiFiRefPtr WiFiService::ChooseDevice() {
1039 // TODO(pstew): Style frowns on dynamic_cast. crosbug.com/38237
1040 DeviceRefPtr device =
1041 manager()->GetEnabledDeviceWithTechnology(Technology::kWifi);
1042 return dynamic_cast<WiFi *>(device.get());
1043}
1044
1045void WiFiService::ResetWiFi() {
1046 SetWiFi(NULL);
1047}
1048
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001049void WiFiService::SetWiFi(const WiFiRefPtr &new_wifi) {
1050 if (wifi_ == new_wifi) {
Paul Stewart3c504012013-01-17 17:49:58 -08001051 return;
1052 }
1053 ClearCachedCredentials();
1054 if (wifi_) {
1055 wifi_->DisassociateFromService(this);
1056 }
mukesh agrawalcbfb34e2013-04-17 19:33:25 -07001057 if (new_wifi) {
1058 adaptor()->EmitRpcIdentifierChanged(flimflam::kDeviceProperty,
1059 new_wifi->GetRpcIdentifier());
1060 } else {
1061 adaptor()->EmitRpcIdentifierChanged(flimflam::kDeviceProperty,
1062 DBusAdaptor::kNullPath);
1063 }
1064 wifi_ = new_wifi;
Paul Stewart3c504012013-01-17 17:49:58 -08001065}
1066
mukesh agrawalb54601c2011-06-07 17:39:22 -07001067} // namespace shill