blob: bc533d081fe87af7479a1f723c7e637457f02abb [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
7#include <string>
Gaurav Shah10109f22011-11-11 20:16:22 -08008#include <utility>
mukesh agrawalb54601c2011-06-07 17:39:22 -07009
10#include <base/logging.h>
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"
mukesh agrawalb54601c2011-06-07 17:39:22 -070019#include "shill/control_interface.h"
20#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070021#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070022#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070023#include "shill/ieee80211.h"
Paul Stewart4357f4e2012-04-26 17:39:26 -070024#include "shill/manager.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000025#include "shill/metrics.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070026#include "shill/nss.h"
Thieu Lef7709452011-11-15 01:13:19 +000027#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070028#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070029#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070030#include "shill/wifi_endpoint.h"
31#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070032
mukesh agrawal261daca2011-12-02 18:56:56 +000033using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070034using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070035using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070036
37namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070038
mukesh agrawalbf14e942012-03-02 14:36:34 -080039const char WiFiService::kAutoConnBusy[] = "busy";
40const char WiFiService::kAutoConnNoEndpoint[] = "no endpoints";
41
Paul Stewartd08f4432011-11-04 07:48:20 -070042const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080043const char WiFiService::kStorageMode[] = "WiFi.Mode";
44const char WiFiService::kStoragePassphrase[] = "Passphrase";
45const char WiFiService::kStorageSecurity[] = "WiFi.Security";
46const char WiFiService::kStorageSSID[] = "SSID";
mukesh agrawale1d90e92012-02-15 17:36:08 -080047bool WiFiService::logged_signal_warning = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070048
mukesh agrawalb54601c2011-06-07 17:39:22 -070049WiFiService::WiFiService(ControlInterface *control_interface,
50 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080051 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070052 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070053 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080054 const vector<uint8_t> &ssid,
55 const string &mode,
56 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080057 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080058 : Service(control_interface, dispatcher, metrics, manager,
59 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070060 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070061 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070062 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080063 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000064 frequency_(0),
Paul Stewart20088d82012-02-16 06:58:55 -080065 physical_mode_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070066 wifi_(device),
Paul Stewartecf4cd12012-04-17 11:08:39 -070067 ssid_(ssid),
68 nss_(NSS::GetInstance()) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070069 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070070 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080071 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
72 &WiFiService::SetPassphrase,
73 &WiFiService::ClearPassphrase,
74 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070075 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
76 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070077
Paul Stewartac4ac002011-08-26 12:04:26 -070078 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
79 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
80 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
81 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070082
mukesh agrawald835b202011-10-07 15:26:47 -070083 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
84 string ssid_string(
85 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080086 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070087 // WifiHexSsid property should only be present if Name property
88 // has been munged.
89 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
90 }
91 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070092
mukesh agrawal6e277772011-09-29 15:04:23 -070093 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
94 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080095 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080096 // Passphrases are not mandatory for 802.1X.
97 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070098 } else if (security_ == flimflam::kSecurityPsk) {
99 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700100 } else if (security_ == flimflam::kSecurityRsn) {
101 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700102 } else if (security_ == flimflam::kSecurityWpa) {
103 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700104 } else if (security_ == flimflam::kSecurityWep) {
105 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700106 } else if (security_ == flimflam::kSecurityNone) {
107 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700108 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800109 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700110 }
111
Paul Stewartd08f4432011-11-04 07:48:20 -0700112 // Until we know better (at Profile load time), use the generic name.
113 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000114 UpdateConnectable();
Paul Stewartcb59fed2012-03-21 21:14:46 -0700115
116 IgnoreParameterForConfigure(flimflam::kModeProperty);
117 IgnoreParameterForConfigure(flimflam::kSSIDProperty);
118 IgnoreParameterForConfigure(flimflam::kSecurityProperty);
119 IgnoreParameterForConfigure(flimflam::kPassphraseProperty);
120 IgnoreParameterForConfigure(flimflam::kWifiHiddenSsid);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700121}
122
123WiFiService::~WiFiService() {
124 LOG(INFO) << __func__;
125}
126
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000127void WiFiService::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800128 const char *reason;
129 if (IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000130 // Execute immediately, for two reasons:
131 //
132 // 1. We need IsAutoConnectable to return the correct value for
133 // other WiFiServices, and that depends on WiFi's state.
134 //
135 // 2. We should probably limit the extent to which we queue up
136 // actions (such as AutoConnect) which depend on current state.
137 // If we queued AutoConnects, we could build a long queue of
138 // useless work (one AutoConnect per Service), which blocks
139 // more timely work.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500140 Connect(NULL);
mukesh agrawal592516d2012-01-12 14:01:00 -0800141 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800142 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
143 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000144 }
145}
146
Paul Stewart22aa71b2011-09-16 12:15:11 -0700147bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
148 return wifi_->TechnologyIs(type);
149}
150
mukesh agrawalbf14e942012-03-02 14:36:34 -0800151bool WiFiService::IsAutoConnectable(const char **reason) const {
152 if (!Service::IsAutoConnectable(reason)) {
153 return false;
154 }
155
156 // Only auto-connect to Services which have visible Endpoints.
157 // (Needed because hidden Services may remain registered with
158 // Manager even without visible Endpoints.)
159 if (!HasEndpoints()) {
160 *reason = kAutoConnNoEndpoint;
161 return false;
162 }
163
164 // 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
174bool WiFiService::IsConnecting() const {
175 // WiFi does not move us into the associating state until it gets
176 // feedback from wpa_supplicant. So, to answer whether or
177 // not we're connecting, we consult with |wifi_|.
178 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800179}
180
mukesh agrawale1d90e92012-02-15 17:36:08 -0800181void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000182 DCHECK(endpoint->ssid() == ssid());
183 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800184 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000185}
186
mukesh agrawale1d90e92012-02-15 17:36:08 -0800187void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000188 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
189 DCHECK(i != endpoints_.end());
190 if (i == endpoints_.end()) {
191 LOG(WARNING) << "In " << __func__ << "(): "
192 << "ignorning non-existent endpoint "
193 << endpoint->bssid_string();
194 return;
195 }
196 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800197 if (current_endpoint_ == endpoint) {
198 current_endpoint_ = NULL;
199 }
200 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000201}
202
mukesh agrawale1d90e92012-02-15 17:36:08 -0800203void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
204 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
205 current_endpoint_ = endpoint;
206 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000207}
208
mukesh agrawalb20776f2012-02-10 16:00:36 -0800209void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &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 agrawal1a056262011-10-05 14:36:54 -0700218void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
219 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
Paul Stewart2706aaf2011-12-14 16:44:04 -0800229 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000230 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800231 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000232
233 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700234}
235
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800236// ClearPassphrase is separate from SetPassphrase, because the default
237// value for |passphrase_| would not pass validation.
238void WiFiService::ClearPassphrase(Error */*error*/) {
239 passphrase_.clear();
240 UpdateConnectable();
241}
242
Paul Stewartd08f4432011-11-04 07:48:20 -0700243bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
244 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
245 storage->ContainsGroup(GetSpecificStorageIdentifier());
246}
247
Paul Stewarta41e38d2011-11-11 07:47:29 -0800248bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800249 // WiFi Services should be displayed only if they are in range (have
250 // endpoints that have shown up in a scan) or if the service is actively
251 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000252 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800253}
254
Paul Stewartd08f4432011-11-04 07:48:20 -0700255bool WiFiService::Load(StoreInterface *storage) {
256 // First find out which storage identifier is available in priority order
257 // of specific, generic.
258 string id = GetSpecificStorageIdentifier();
259 if (!storage->ContainsGroup(id)) {
260 id = GetGenericStorageIdentifier();
261 if (!storage->ContainsGroup(id)) {
262 LOG(WARNING) << "Service is not available in the persistent store: "
263 << id;
264 return false;
265 }
266 }
267
268 // Set our storage identifier to match the storage name in the Profile.
269 storage_identifier_ = id;
270
271 // Load properties common to all Services.
272 if (!Service::Load(storage)) {
273 return false;
274 }
275
276 // Load properties specific to WiFi services.
277 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000278
Paul Stewart2706aaf2011-12-14 16:44:04 -0800279 // NB: mode, security and ssid parameters are never read in from
280 // Load() as they are provided from the scan.
281
282 string passphrase;
283 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
284 Error error;
285 SetPassphrase(passphrase, &error);
286 if (!error.IsSuccess()) {
287 LOG(ERROR) << "Passphrase could not be set: "
288 << Error::GetName(error.type());
289 }
290 }
291
Paul Stewartd08f4432011-11-04 07:48:20 -0700292 return true;
293}
294
295bool WiFiService::Save(StoreInterface *storage) {
296 // Save properties common to all Services.
297 if (!Service::Save(storage)) {
298 return false;
299 }
300
301 // Save properties specific to WiFi services.
302 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800303 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
304 storage->SetString(id, kStorageMode, mode_);
305 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
306 storage->SetString(id, kStorageSecurity, security_);
307 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000308
Paul Stewartd08f4432011-11-04 07:48:20 -0700309 return true;
310}
311
Paul Stewart65512e12012-03-26 18:01:08 -0700312bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800313 Service::Unload();
314 hidden_ssid_ = false;
Wade Guthrie005bd342012-05-02 09:37:07 -0700315 Error unused_error;
316 ClearPassphrase(&unused_error);
Paul Stewart66c86002012-01-30 18:00:52 -0800317 if (security_ == flimflam::kSecurity8021x) {
318 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
319 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
320 // Make sure that these are cleared when credentials for networks
321 // of this type goes away.
322 //
323 // When wpa_supplicant gains the ability, do this credential
324 // clearing on a per-service basis. Also do this whenever the credentials
325 // for a service changes. crosbug.com/25670
326 wifi_->ClearCachedCredentials();
327 }
Paul Stewart65512e12012-03-26 18:01:08 -0700328 return !IsVisible();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800329}
330
Paul Stewart6ab23a92011-11-09 17:17:47 -0800331bool WiFiService::IsSecurityMatch(const string &security) const {
332 return GetSecurityClass(security) == GetSecurityClass(security_);
333}
334
Thieu Le48e6d6d2011-12-06 00:40:27 +0000335void WiFiService::InitializeCustomMetrics() const {
336 string histogram = metrics()->GetFullMetricName(
337 Metrics::kMetricTimeToJoinMilliseconds,
338 technology());
339 metrics()->AddServiceStateTransitionTimer(this,
340 histogram,
341 Service::kStateAssociating,
342 Service::kStateConfiguring);
343}
344
Thieu Leb84ba342012-03-02 15:15:19 -0800345void WiFiService::SendPostReadyStateMetrics(
346 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000347 metrics()->SendEnumToUMA(
348 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
349 technology()),
350 Metrics::WiFiFrequencyToChannel(frequency_),
351 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000352
353 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
354 metrics()->SendEnumToUMA(
355 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
356 technology()),
357 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
358 Metrics::kWiFiNetworkPhyModeMax);
359
360 Metrics::WiFiSecurity security_uma =
361 Metrics::WiFiSecurityStringToEnum(security_);
362 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
363 metrics()->SendEnumToUMA(
364 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
365 technology()),
366 security_uma,
367 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800368
369 if (time_resume_to_ready_milliseconds > 0) {
370 metrics()->SendToUMA(
371 metrics()->GetFullMetricName(
372 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
373 time_resume_to_ready_milliseconds,
374 Metrics::kTimerHistogramMillisecondsMin,
375 Metrics::kTimerHistogramMillisecondsMax,
376 Metrics::kTimerHistogramNumBuckets);
377 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000378}
379
mukesh agrawal32399322011-09-01 10:53:43 -0700380// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800381void WiFiService::HelpRegisterWriteOnlyDerivedString(
382 const string &name,
383 void(WiFiService::*set)(const string &, Error *),
384 void(WiFiService::*clear)(Error *),
385 const string *default_value) {
386 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000387 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800388 StringAccessor(
389 new CustomWriteOnlyAccessor<WiFiService, string>(
390 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000391}
392
Wade Guthrie005bd342012-05-02 09:37:07 -0700393void WiFiService::Connect(Error *error) {
394 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name();
mukesh agrawal6e277772011-09-29 15:04:23 -0700395 std::map<string, DBus::Variant> params;
396 DBus::MessageIter writer;
397
Wade Guthrie005bd342012-05-02 09:37:07 -0700398 if (!connectable()) {
399 LOG(ERROR) << "Can't connect. Service " << friendly_name()
400 << " is not connectable";
401 Error::PopulateAndLog(error, Error::kOperationFailed,
402 Error::GetDefaultMessage(Error::kOperationFailed));
403 return;
404 }
405
mukesh agrawal6e277772011-09-29 15:04:23 -0700406 params[wpa_supplicant::kNetworkPropertyMode].writer().
407 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
408
Gaurav Shah29d68882012-01-30 19:06:42 -0800409 if (Is8021x()) {
410 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800411 if (GetEAPKeyManagement().empty())
412 SetEAPKeyManagement("WPA-EAP");
413 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700414 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800415 const string psk_proto = StringPrintf("%s %s",
416 wpa_supplicant::kSecurityModeWPA,
417 wpa_supplicant::kSecurityModeRSN);
418 params[wpa_supplicant::kPropertySecurityProtocol].writer().
419 append_string(psk_proto.c_str());
420 params[wpa_supplicant::kPropertyPreSharedKey].writer().
421 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700422 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700423 params[wpa_supplicant::kPropertySecurityProtocol].writer().
424 append_string(wpa_supplicant::kSecurityModeRSN);
425 params[wpa_supplicant::kPropertyPreSharedKey].writer().
426 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700427 } else if (security_ == flimflam::kSecurityWpa) {
428 params[wpa_supplicant::kPropertySecurityProtocol].writer().
429 append_string(wpa_supplicant::kSecurityModeWPA);
430 params[wpa_supplicant::kPropertyPreSharedKey].writer().
431 append_string(passphrase_.c_str());
432 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000433 params[wpa_supplicant::kPropertyAuthAlg].writer().
434 append_string(wpa_supplicant::kSecurityAuthAlg);
435 Error error;
436 int key_index;
437 std::vector<uint8> password_bytes;
438 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
439 writer = params[wpa_supplicant::kPropertyWEPKey +
440 base::IntToString(key_index)].writer();
441 writer << password_bytes;
442 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
443 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700444 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800445 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700446 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800447 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700448 }
449
Gaurav Shah10109f22011-11-11 20:16:22 -0800450 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700451 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800452
453 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700454 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
455 writer << ssid_;
456
457 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700458}
459
Eric Shienbrood9a245532012-03-07 14:20:39 -0500460void WiFiService::Disconnect(Error *error) {
461 LOG(INFO) << __func__;
462 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000463 wifi_->DisconnectFrom(this);
464}
465
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800466string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700467 return wifi_->GetRpcIdentifier();
468}
469
mukesh agrawal29c13a12011-11-24 00:09:19 +0000470void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800471 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000472 if (security_ == flimflam::kSecurityNone) {
473 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800474 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800475 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800476 } else if (Is8021x()) {
477 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000478 } else if (security_ == flimflam::kSecurityWep ||
479 security_ == flimflam::kSecurityWpa ||
480 security_ == flimflam::kSecurityPsk ||
481 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800482 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800483 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000484 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800485 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000486}
487
mukesh agrawale1d90e92012-02-15 17:36:08 -0800488void WiFiService::UpdateFromEndpoints() {
489 const WiFiEndpoint *representative_endpoint = NULL;
490
491 if (current_endpoint_) {
492 // TODO: Copy BSSID here (crosbug.com/22377).
493 representative_endpoint = current_endpoint_;
494 } else {
495 int16 best_signal = std::numeric_limits<int16>::min();
496 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
497 i != endpoints_.end(); ++i) {
498 if ((*i)->signal_strength() >= best_signal) {
499 best_signal = (*i)->signal_strength();
500 representative_endpoint = *i;
501 }
502 }
503 }
504
505 uint16 frequency;
506 int16 signal;
507 if (!representative_endpoint) {
508 frequency = 0;
509 signal = std::numeric_limits<int16>::min();
510 } else {
511 frequency = representative_endpoint->frequency();
512 signal = representative_endpoint->signal_strength();
513 }
514
515 if (frequency_ != frequency) {
516 frequency_ = frequency;
517 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
518 }
519 SetStrength(SignalToStrength(signal));
520}
521
mukesh agrawal1a056262011-10-05 14:36:54 -0700522// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000523void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
524 Error *error) {
525 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700526}
527
528// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000529void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
530 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700531 unsigned int length = passphrase.length();
532 vector<uint8> passphrase_bytes;
533
534 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
535 if (length != IEEE_80211::kWPAHexLen &&
536 (length < IEEE_80211::kWPAAsciiMinLen ||
537 length > IEEE_80211::kWPAAsciiMaxLen)) {
538 error->Populate(Error::kInvalidPassphrase);
539 }
540 } else {
541 if (length < IEEE_80211::kWPAAsciiMinLen ||
542 length > IEEE_80211::kWPAAsciiMaxLen) {
543 error->Populate(Error::kInvalidPassphrase);
544 }
545 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000546}
mukesh agrawal1a056262011-10-05 14:36:54 -0700547
Thieu Lef4cbda92011-11-10 23:41:24 +0000548// static
549void WiFiService::ParseWEPPassphrase(const string &passphrase,
550 int *key_index,
551 std::vector<uint8> *password_bytes,
552 Error *error) {
553 unsigned int length = passphrase.length();
554 int key_index_local;
555 std::string password_text;
556 bool is_hex = false;
557
558 switch (length) {
559 case IEEE_80211::kWEP40AsciiLen:
560 case IEEE_80211::kWEP104AsciiLen:
561 key_index_local = 0;
562 password_text = passphrase;
563 break;
564 case IEEE_80211::kWEP40AsciiLen + 2:
565 case IEEE_80211::kWEP104AsciiLen + 2:
566 if (CheckWEPKeyIndex(passphrase, error)) {
567 base::StringToInt(passphrase.substr(0,1), &key_index_local);
568 password_text = passphrase.substr(2);
569 }
570 break;
571 case IEEE_80211::kWEP40HexLen:
572 case IEEE_80211::kWEP104HexLen:
573 if (CheckWEPIsHex(passphrase, error)) {
574 key_index_local = 0;
575 password_text = passphrase;
576 is_hex = true;
577 }
578 break;
579 case IEEE_80211::kWEP40HexLen + 2:
580 case IEEE_80211::kWEP104HexLen + 2:
581 if(CheckWEPKeyIndex(passphrase, error) &&
582 CheckWEPIsHex(passphrase.substr(2), error)) {
583 base::StringToInt(passphrase.substr(0,1), &key_index_local);
584 password_text = passphrase.substr(2);
585 is_hex = true;
586 } else if (CheckWEPPrefix(passphrase, error) &&
587 CheckWEPIsHex(passphrase.substr(2), error)) {
588 key_index_local = 0;
589 password_text = passphrase.substr(2);
590 is_hex = true;
591 }
592 break;
593 case IEEE_80211::kWEP40HexLen + 4:
594 case IEEE_80211::kWEP104HexLen + 4:
595 if (CheckWEPKeyIndex(passphrase, error) &&
596 CheckWEPPrefix(passphrase.substr(2), error) &&
597 CheckWEPIsHex(passphrase.substr(4), error)) {
598 base::StringToInt(passphrase.substr(0,1), &key_index_local);
599 password_text = passphrase.substr(4);
600 is_hex = true;
601 }
602 break;
603 default:
604 error->Populate(Error::kInvalidPassphrase);
605 break;
606 }
607
mukesh agrawal1a056262011-10-05 14:36:54 -0700608 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000609 if (key_index)
610 *key_index = key_index_local;
611 if (password_bytes) {
612 if (is_hex)
613 base::HexStringToBytes(password_text, password_bytes);
614 else
615 password_bytes->insert(password_bytes->end(),
616 password_text.begin(),
617 password_text.end());
618 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700619 }
620}
621
622// static
623bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
624 vector<uint8> passphrase_bytes;
625 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
626 return true;
627 } else {
628 error->Populate(Error::kInvalidPassphrase);
629 return false;
630 }
631}
632
633// static
634bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
635 if (StartsWithASCII(passphrase, "0:", false) ||
636 StartsWithASCII(passphrase, "1:", false) ||
637 StartsWithASCII(passphrase, "2:", false) ||
638 StartsWithASCII(passphrase, "3:", false)) {
639 return true;
640 } else {
641 error->Populate(Error::kInvalidPassphrase);
642 return false;
643 }
644}
645
646// static
647bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
648 if (StartsWithASCII(passphrase, "0x", false)) {
649 return true;
650 } else {
651 error->Populate(Error::kInvalidPassphrase);
652 return false;
653 }
654}
655
Paul Stewart6ab23a92011-11-09 17:17:47 -0800656// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800657string WiFiService::GetSecurityClass(const string &security) {
658 if (security == flimflam::kSecurityRsn ||
659 security == flimflam::kSecurityWpa) {
660 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700661 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800662 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700663 }
664}
665
Paul Stewarta41e38d2011-11-11 07:47:29 -0800666// static
667bool WiFiService::ParseStorageIdentifier(const string &storage_name,
668 string *address,
669 string *mode,
670 string *security) {
671 vector<string> wifi_parts;
672 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800673 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
674 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800675 return false;
676 }
677 *address = wifi_parts[1];
678 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800679 if (wifi_parts.size() == 5) {
680 *security = wifi_parts[4];
681 } else {
682 // Account for security type "802_1x" which got split up above.
683 *security = wifi_parts[4] + "_" + wifi_parts[5];
684 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800685 return true;
686}
687
mukesh agrawale1d90e92012-02-15 17:36:08 -0800688// static
689uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
690 int16 strength;
691 if (signal_dbm > 0) {
692 if (!logged_signal_warning) {
693 LOG(WARNING) << "Signal strength is suspiciously high. "
694 << "Assuming value " << signal_dbm << " is not in dBm.";
695 logged_signal_warning = true;
696 }
697 strength = signal_dbm;
698 } else {
699 strength = 120 + signal_dbm; // Call -20dBm "perfect".
700 }
701
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800702 if (strength > kStrengthMax) {
703 strength = kStrengthMax;
704 } else if (strength < kStrengthMin) {
705 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800706 }
707 return strength;
708}
709
Paul Stewart6ab23a92011-11-09 17:17:47 -0800710string WiFiService::GetGenericStorageIdentifier() const {
711 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
712}
713
Paul Stewartd08f4432011-11-04 07:48:20 -0700714string WiFiService::GetSpecificStorageIdentifier() const {
715 return GetStorageIdentifierForSecurity(security_);
716}
717
718string WiFiService::GetStorageIdentifierForSecurity(
719 const string &security) const {
720 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
721 flimflam::kTypeWifi,
722 wifi_->address().c_str(),
723 hex_ssid_.c_str(),
724 mode_.c_str(),
725 security.c_str()));
726}
727
Gary Moraine4aaf5e2012-04-05 14:37:32 -0700728void WiFiService::set_eap(const EapCredentials &new_eap) {
729 EapCredentials modified_eap = new_eap;
730
731 // An empty key_management field is invalid. Prevent it, if possible.
732 if (modified_eap.key_management.empty()) {
733 modified_eap.key_management = eap().key_management;
734 }
735 Service::set_eap(modified_eap);
Gaurav Shah10109f22011-11-11 20:16:22 -0800736 UpdateConnectable();
737}
738
Paul Stewart4357f4e2012-04-26 17:39:26 -0700739void WiFiService::OnProfileConfigured() {
740 if (profile() || !hidden_ssid()) {
741 return;
742 }
743 // This situation occurs when a hidden WiFi service created via GetService
744 // has been persisted to a profile in Manager::ConfigureService(). Now
745 // that configuration is saved, we must join the service with its profile,
746 // which will make this SSID eligible for directed probes during scans.
747 manager()->RegisterService(this);
748}
749
Gaurav Shah29d68882012-01-30 19:06:42 -0800750bool WiFiService::Is8021x() const {
751 if (security_ == flimflam::kSecurity8021x)
752 return true;
753
754 // Dynamic WEP + 802.1x.
755 if (security_ == flimflam::kSecurityWep &&
756 GetEAPKeyManagement() == "IEEE8021X")
757 return true;
758 return false;
759}
760
Gaurav Shah10109f22011-11-11 20:16:22 -0800761void WiFiService::Populate8021xProperties(
762 std::map<string, DBus::Variant> *params) {
Paul Stewartecf4cd12012-04-17 11:08:39 -0700763 string ca_cert = eap().ca_cert;
764 if (!eap().ca_cert_nss.empty()) {
765 vector<char> id(ssid_.begin(), ssid_.end());
766 FilePath certfile = nss_->GetDERCertfile(eap().ca_cert_nss, id);
767 if (certfile.empty()) {
768 LOG(ERROR) << "Unable to extract certificate: " << eap().ca_cert_nss;
769 } else {
770 ca_cert = certfile.value();
771 }
772 }
773
774
Gaurav Shah10109f22011-11-11 20:16:22 -0800775 typedef std::pair<const char *, const char *> KeyVal;
Paul Stewart20550982012-04-16 12:16:11 -0700776 KeyVal init_propertyvals[] = {
Gaurav Shah10109f22011-11-11 20:16:22 -0800777 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
778 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
779 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
780 eap().inner_eap.c_str()),
781 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
782 eap().anonymous_identity.c_str()),
783 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
784 eap().client_cert.c_str()),
785 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
786 eap().private_key.c_str()),
787 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
788 eap().private_key_password.c_str()),
Paul Stewartecf4cd12012-04-17 11:08:39 -0700789 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, ca_cert.c_str()),
Gaurav Shah10109f22011-11-11 20:16:22 -0800790 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
791 eap().password.c_str()),
792 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
793 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
794 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
795 eap().ca_cert_id.c_str()),
Gaurav Shah10109f22011-11-11 20:16:22 -0800796 };
797
Paul Stewart20550982012-04-16 12:16:11 -0700798 vector<KeyVal> propertyvals(init_propertyvals,
799 init_propertyvals + arraysize(init_propertyvals));
800 if (eap().use_system_cas) {
801 propertyvals.push_back(KeyVal(
802 wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath));
Paul Stewartecf4cd12012-04-17 11:08:39 -0700803 } else if (ca_cert.empty()) {
Paul Stewart20550982012-04-16 12:16:11 -0700804 LOG(WARNING) << __func__
805 << ": No certificate authorities are configured."
806 << " Server certificates will be accepted"
807 << " unconditionally.";
808 }
809
810 if (!eap().cert_id.empty() || !eap().key_id.empty() ||
811 !eap().ca_cert_id.empty()) {
812 propertyvals.push_back(KeyVal(
813 wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()));
814 propertyvals.push_back(KeyVal(
815 wpa_supplicant::kNetworkPropertyEngineId,
816 wpa_supplicant::kEnginePKCS11));
817 // We can't use the propertyvals vector for this since this argument
818 // is a uint32, not a string.
819 (*params)[wpa_supplicant::kNetworkPropertyEngine].writer().
820 append_uint32(wpa_supplicant::kDefaultEngine);
821 }
822
823 vector<KeyVal>::iterator it;
824 for (it = propertyvals.begin(); it != propertyvals.end(); ++it) {
825 if (strlen((*it).second) > 0) {
826 (*params)[(*it).first].writer().append_string((*it).second);
Gaurav Shah10109f22011-11-11 20:16:22 -0800827 }
828 }
829}
830
mukesh agrawalb54601c2011-06-07 17:39:22 -0700831} // namespace shill