blob: a10f665adb57b1af2c24ab119fe6f1ad535657b7 [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 agrawal923f14f2012-06-04 16:46:08 -070082 store->RegisterConstString(flimflam::kWifiBSsid, &bssid_);
mukesh agrawal32399322011-09-01 10:53:43 -070083
mukesh agrawald835b202011-10-07 15:26:47 -070084 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
85 string ssid_string(
86 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080087 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070088 // WifiHexSsid property should only be present if Name property
89 // has been munged.
90 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
91 }
92 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070093
mukesh agrawal6e277772011-09-29 15:04:23 -070094 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
95 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080096 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080097 // Passphrases are not mandatory for 802.1X.
98 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070099 } else if (security_ == flimflam::kSecurityPsk) {
100 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700101 } else if (security_ == flimflam::kSecurityRsn) {
102 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700103 } else if (security_ == flimflam::kSecurityWpa) {
104 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700105 } else if (security_ == flimflam::kSecurityWep) {
106 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700107 } else if (security_ == flimflam::kSecurityNone) {
108 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700109 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800110 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700111 }
112
Paul Stewartd08f4432011-11-04 07:48:20 -0700113 // Until we know better (at Profile load time), use the generic name.
114 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000115 UpdateConnectable();
Paul Stewartcb59fed2012-03-21 21:14:46 -0700116
117 IgnoreParameterForConfigure(flimflam::kModeProperty);
118 IgnoreParameterForConfigure(flimflam::kSSIDProperty);
119 IgnoreParameterForConfigure(flimflam::kSecurityProperty);
120 IgnoreParameterForConfigure(flimflam::kPassphraseProperty);
121 IgnoreParameterForConfigure(flimflam::kWifiHiddenSsid);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700122}
123
124WiFiService::~WiFiService() {
125 LOG(INFO) << __func__;
126}
127
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000128void WiFiService::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800129 const char *reason;
130 if (IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000131 // Execute immediately, for two reasons:
132 //
133 // 1. We need IsAutoConnectable to return the correct value for
134 // other WiFiServices, and that depends on WiFi's state.
135 //
136 // 2. We should probably limit the extent to which we queue up
137 // actions (such as AutoConnect) which depend on current state.
138 // If we queued AutoConnects, we could build a long queue of
139 // useless work (one AutoConnect per Service), which blocks
140 // more timely work.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500141 Connect(NULL);
mukesh agrawal592516d2012-01-12 14:01:00 -0800142 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800143 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
144 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000145 }
146}
147
Paul Stewart22aa71b2011-09-16 12:15:11 -0700148bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
149 return wifi_->TechnologyIs(type);
150}
151
mukesh agrawalbf14e942012-03-02 14:36:34 -0800152bool WiFiService::IsAutoConnectable(const char **reason) const {
153 if (!Service::IsAutoConnectable(reason)) {
154 return false;
155 }
156
157 // Only auto-connect to Services which have visible Endpoints.
158 // (Needed because hidden Services may remain registered with
159 // Manager even without visible Endpoints.)
160 if (!HasEndpoints()) {
161 *reason = kAutoConnNoEndpoint;
162 return false;
163 }
164
165 // Do not preempt an existing connection (whether pending, or
166 // connected, and whether to this service, or another).
167 if (!wifi_->IsIdle()) {
168 *reason = kAutoConnBusy;
169 return false;
170 }
171
172 return true;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000173}
174
175bool WiFiService::IsConnecting() const {
176 // WiFi does not move us into the associating state until it gets
177 // feedback from wpa_supplicant. So, to answer whether or
178 // not we're connecting, we consult with |wifi_|.
179 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800180}
181
mukesh agrawale1d90e92012-02-15 17:36:08 -0800182void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000183 DCHECK(endpoint->ssid() == ssid());
184 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800185 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000186}
187
mukesh agrawale1d90e92012-02-15 17:36:08 -0800188void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000189 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
190 DCHECK(i != endpoints_.end());
191 if (i == endpoints_.end()) {
192 LOG(WARNING) << "In " << __func__ << "(): "
193 << "ignorning non-existent endpoint "
194 << endpoint->bssid_string();
195 return;
196 }
197 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800198 if (current_endpoint_ == endpoint) {
199 current_endpoint_ = NULL;
200 }
201 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000202}
203
mukesh agrawale1d90e92012-02-15 17:36:08 -0800204void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
205 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
206 current_endpoint_ = endpoint;
207 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000208}
209
mukesh agrawalb20776f2012-02-10 16:00:36 -0800210void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
211 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800212 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800213}
214
Chris Masone6515aab2011-10-12 16:19:09 -0700215string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700216 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700217}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700218
mukesh agrawal1a056262011-10-05 14:36:54 -0700219void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
220 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000221 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700222 } else if (security_ == flimflam::kSecurityPsk ||
223 security_ == flimflam::kSecurityWpa ||
224 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000225 ValidateWPAPassphrase(passphrase, error);
226 } else {
227 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700228 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000229
Paul Stewart2706aaf2011-12-14 16:44:04 -0800230 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000231 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800232 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000233
234 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700235}
236
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800237// ClearPassphrase is separate from SetPassphrase, because the default
238// value for |passphrase_| would not pass validation.
239void WiFiService::ClearPassphrase(Error */*error*/) {
240 passphrase_.clear();
241 UpdateConnectable();
242}
243
Paul Stewartd08f4432011-11-04 07:48:20 -0700244bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
245 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
246 storage->ContainsGroup(GetSpecificStorageIdentifier());
247}
248
Paul Stewarta41e38d2011-11-11 07:47:29 -0800249bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800250 // WiFi Services should be displayed only if they are in range (have
251 // endpoints that have shown up in a scan) or if the service is actively
252 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000253 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800254}
255
Paul Stewartd08f4432011-11-04 07:48:20 -0700256bool WiFiService::Load(StoreInterface *storage) {
257 // First find out which storage identifier is available in priority order
258 // of specific, generic.
259 string id = GetSpecificStorageIdentifier();
260 if (!storage->ContainsGroup(id)) {
261 id = GetGenericStorageIdentifier();
262 if (!storage->ContainsGroup(id)) {
263 LOG(WARNING) << "Service is not available in the persistent store: "
264 << id;
265 return false;
266 }
267 }
268
269 // Set our storage identifier to match the storage name in the Profile.
270 storage_identifier_ = id;
271
272 // Load properties common to all Services.
273 if (!Service::Load(storage)) {
274 return false;
275 }
276
277 // Load properties specific to WiFi services.
278 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000279
Paul Stewart2706aaf2011-12-14 16:44:04 -0800280 // NB: mode, security and ssid parameters are never read in from
281 // Load() as they are provided from the scan.
282
283 string passphrase;
284 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
285 Error error;
286 SetPassphrase(passphrase, &error);
287 if (!error.IsSuccess()) {
288 LOG(ERROR) << "Passphrase could not be set: "
289 << Error::GetName(error.type());
290 }
291 }
292
Paul Stewartd08f4432011-11-04 07:48:20 -0700293 return true;
294}
295
296bool WiFiService::Save(StoreInterface *storage) {
297 // Save properties common to all Services.
298 if (!Service::Save(storage)) {
299 return false;
300 }
301
302 // Save properties specific to WiFi services.
303 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800304 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
305 storage->SetString(id, kStorageMode, mode_);
306 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
307 storage->SetString(id, kStorageSecurity, security_);
308 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000309
Paul Stewartd08f4432011-11-04 07:48:20 -0700310 return true;
311}
312
Paul Stewart65512e12012-03-26 18:01:08 -0700313bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800314 Service::Unload();
315 hidden_ssid_ = false;
Wade Guthrie005bd342012-05-02 09:37:07 -0700316 Error unused_error;
317 ClearPassphrase(&unused_error);
Paul Stewart66c86002012-01-30 18:00:52 -0800318 if (security_ == flimflam::kSecurity8021x) {
319 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
320 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
321 // Make sure that these are cleared when credentials for networks
322 // of this type goes away.
323 //
324 // When wpa_supplicant gains the ability, do this credential
325 // clearing on a per-service basis. Also do this whenever the credentials
326 // for a service changes. crosbug.com/25670
327 wifi_->ClearCachedCredentials();
328 }
Paul Stewart65512e12012-03-26 18:01:08 -0700329 return !IsVisible();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800330}
331
Paul Stewart6ab23a92011-11-09 17:17:47 -0800332bool WiFiService::IsSecurityMatch(const string &security) const {
333 return GetSecurityClass(security) == GetSecurityClass(security_);
334}
335
Thieu Le48e6d6d2011-12-06 00:40:27 +0000336void WiFiService::InitializeCustomMetrics() const {
337 string histogram = metrics()->GetFullMetricName(
338 Metrics::kMetricTimeToJoinMilliseconds,
339 technology());
340 metrics()->AddServiceStateTransitionTimer(this,
341 histogram,
342 Service::kStateAssociating,
343 Service::kStateConfiguring);
344}
345
Thieu Leb84ba342012-03-02 15:15:19 -0800346void WiFiService::SendPostReadyStateMetrics(
347 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000348 metrics()->SendEnumToUMA(
349 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
350 technology()),
351 Metrics::WiFiFrequencyToChannel(frequency_),
352 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000353
354 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
355 metrics()->SendEnumToUMA(
356 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
357 technology()),
358 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
359 Metrics::kWiFiNetworkPhyModeMax);
360
361 Metrics::WiFiSecurity security_uma =
362 Metrics::WiFiSecurityStringToEnum(security_);
363 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
364 metrics()->SendEnumToUMA(
365 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
366 technology()),
367 security_uma,
368 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800369
370 if (time_resume_to_ready_milliseconds > 0) {
371 metrics()->SendToUMA(
372 metrics()->GetFullMetricName(
373 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
374 time_resume_to_ready_milliseconds,
375 Metrics::kTimerHistogramMillisecondsMin,
376 Metrics::kTimerHistogramMillisecondsMax,
377 Metrics::kTimerHistogramNumBuckets);
378 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000379}
380
mukesh agrawal32399322011-09-01 10:53:43 -0700381// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800382void WiFiService::HelpRegisterWriteOnlyDerivedString(
383 const string &name,
384 void(WiFiService::*set)(const string &, Error *),
385 void(WiFiService::*clear)(Error *),
386 const string *default_value) {
387 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000388 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800389 StringAccessor(
390 new CustomWriteOnlyAccessor<WiFiService, string>(
391 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000392}
393
Wade Guthrie005bd342012-05-02 09:37:07 -0700394void WiFiService::Connect(Error *error) {
395 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name();
mukesh agrawal6e277772011-09-29 15:04:23 -0700396 std::map<string, DBus::Variant> params;
397 DBus::MessageIter writer;
398
Wade Guthrie005bd342012-05-02 09:37:07 -0700399 if (!connectable()) {
400 LOG(ERROR) << "Can't connect. Service " << friendly_name()
401 << " is not connectable";
402 Error::PopulateAndLog(error, Error::kOperationFailed,
403 Error::GetDefaultMessage(Error::kOperationFailed));
404 return;
405 }
406
mukesh agrawal6e277772011-09-29 15:04:23 -0700407 params[wpa_supplicant::kNetworkPropertyMode].writer().
408 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
409
Gaurav Shah29d68882012-01-30 19:06:42 -0800410 if (Is8021x()) {
411 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800412 if (GetEAPKeyManagement().empty())
413 SetEAPKeyManagement("WPA-EAP");
414 Populate8021xProperties(&params);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700415 ClearEAPCertification();
mukesh agrawal6e277772011-09-29 15:04:23 -0700416 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800417 const string psk_proto = StringPrintf("%s %s",
418 wpa_supplicant::kSecurityModeWPA,
419 wpa_supplicant::kSecurityModeRSN);
420 params[wpa_supplicant::kPropertySecurityProtocol].writer().
421 append_string(psk_proto.c_str());
422 params[wpa_supplicant::kPropertyPreSharedKey].writer().
423 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700424 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700425 params[wpa_supplicant::kPropertySecurityProtocol].writer().
426 append_string(wpa_supplicant::kSecurityModeRSN);
427 params[wpa_supplicant::kPropertyPreSharedKey].writer().
428 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700429 } else if (security_ == flimflam::kSecurityWpa) {
430 params[wpa_supplicant::kPropertySecurityProtocol].writer().
431 append_string(wpa_supplicant::kSecurityModeWPA);
432 params[wpa_supplicant::kPropertyPreSharedKey].writer().
433 append_string(passphrase_.c_str());
434 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000435 params[wpa_supplicant::kPropertyAuthAlg].writer().
436 append_string(wpa_supplicant::kSecurityAuthAlg);
437 Error error;
438 int key_index;
439 std::vector<uint8> password_bytes;
440 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
441 writer = params[wpa_supplicant::kPropertyWEPKey +
442 base::IntToString(key_index)].writer();
443 writer << password_bytes;
444 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
445 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700446 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800447 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700448 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800449 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700450 }
451
Gaurav Shah10109f22011-11-11 20:16:22 -0800452 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700453 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800454
455 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700456 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
457 writer << ssid_;
458
459 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700460}
461
Eric Shienbrood9a245532012-03-07 14:20:39 -0500462void WiFiService::Disconnect(Error *error) {
463 LOG(INFO) << __func__;
464 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000465 wifi_->DisconnectFrom(this);
466}
467
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800468string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700469 return wifi_->GetRpcIdentifier();
470}
471
mukesh agrawal29c13a12011-11-24 00:09:19 +0000472void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800473 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000474 if (security_ == flimflam::kSecurityNone) {
475 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800476 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800477 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800478 } else if (Is8021x()) {
479 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000480 } else if (security_ == flimflam::kSecurityWep ||
481 security_ == flimflam::kSecurityWpa ||
482 security_ == flimflam::kSecurityPsk ||
483 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800484 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800485 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000486 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800487 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000488}
489
mukesh agrawale1d90e92012-02-15 17:36:08 -0800490void WiFiService::UpdateFromEndpoints() {
491 const WiFiEndpoint *representative_endpoint = NULL;
492
493 if (current_endpoint_) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800494 representative_endpoint = current_endpoint_;
495 } else {
496 int16 best_signal = std::numeric_limits<int16>::min();
497 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
498 i != endpoints_.end(); ++i) {
499 if ((*i)->signal_strength() >= best_signal) {
500 best_signal = (*i)->signal_strength();
501 representative_endpoint = *i;
502 }
503 }
504 }
505
506 uint16 frequency;
507 int16 signal;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700508 string bssid;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800509 if (!representative_endpoint) {
510 frequency = 0;
511 signal = std::numeric_limits<int16>::min();
512 } else {
513 frequency = representative_endpoint->frequency();
514 signal = representative_endpoint->signal_strength();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700515 bssid = representative_endpoint->bssid_string();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800516 }
517
518 if (frequency_ != frequency) {
519 frequency_ = frequency;
520 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
521 }
mukesh agrawal923f14f2012-06-04 16:46:08 -0700522 if (bssid_ != bssid) {
523 bssid_ = bssid;
524 adaptor()->EmitStringChanged(flimflam::kWifiBSsid, bssid_);
525 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800526 SetStrength(SignalToStrength(signal));
527}
528
mukesh agrawal1a056262011-10-05 14:36:54 -0700529// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000530void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
531 Error *error) {
532 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700533}
534
535// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000536void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
537 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700538 unsigned int length = passphrase.length();
539 vector<uint8> passphrase_bytes;
540
541 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
542 if (length != IEEE_80211::kWPAHexLen &&
543 (length < IEEE_80211::kWPAAsciiMinLen ||
544 length > IEEE_80211::kWPAAsciiMaxLen)) {
545 error->Populate(Error::kInvalidPassphrase);
546 }
547 } else {
548 if (length < IEEE_80211::kWPAAsciiMinLen ||
549 length > IEEE_80211::kWPAAsciiMaxLen) {
550 error->Populate(Error::kInvalidPassphrase);
551 }
552 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000553}
mukesh agrawal1a056262011-10-05 14:36:54 -0700554
Thieu Lef4cbda92011-11-10 23:41:24 +0000555// static
556void WiFiService::ParseWEPPassphrase(const string &passphrase,
557 int *key_index,
558 std::vector<uint8> *password_bytes,
559 Error *error) {
560 unsigned int length = passphrase.length();
561 int key_index_local;
562 std::string password_text;
563 bool is_hex = false;
564
565 switch (length) {
566 case IEEE_80211::kWEP40AsciiLen:
567 case IEEE_80211::kWEP104AsciiLen:
568 key_index_local = 0;
569 password_text = passphrase;
570 break;
571 case IEEE_80211::kWEP40AsciiLen + 2:
572 case IEEE_80211::kWEP104AsciiLen + 2:
573 if (CheckWEPKeyIndex(passphrase, error)) {
574 base::StringToInt(passphrase.substr(0,1), &key_index_local);
575 password_text = passphrase.substr(2);
576 }
577 break;
578 case IEEE_80211::kWEP40HexLen:
579 case IEEE_80211::kWEP104HexLen:
580 if (CheckWEPIsHex(passphrase, error)) {
581 key_index_local = 0;
582 password_text = passphrase;
583 is_hex = true;
584 }
585 break;
586 case IEEE_80211::kWEP40HexLen + 2:
587 case IEEE_80211::kWEP104HexLen + 2:
588 if(CheckWEPKeyIndex(passphrase, error) &&
589 CheckWEPIsHex(passphrase.substr(2), error)) {
590 base::StringToInt(passphrase.substr(0,1), &key_index_local);
591 password_text = passphrase.substr(2);
592 is_hex = true;
593 } else if (CheckWEPPrefix(passphrase, error) &&
594 CheckWEPIsHex(passphrase.substr(2), error)) {
595 key_index_local = 0;
596 password_text = passphrase.substr(2);
597 is_hex = true;
598 }
599 break;
600 case IEEE_80211::kWEP40HexLen + 4:
601 case IEEE_80211::kWEP104HexLen + 4:
602 if (CheckWEPKeyIndex(passphrase, error) &&
603 CheckWEPPrefix(passphrase.substr(2), error) &&
604 CheckWEPIsHex(passphrase.substr(4), error)) {
605 base::StringToInt(passphrase.substr(0,1), &key_index_local);
606 password_text = passphrase.substr(4);
607 is_hex = true;
608 }
609 break;
610 default:
611 error->Populate(Error::kInvalidPassphrase);
612 break;
613 }
614
mukesh agrawal1a056262011-10-05 14:36:54 -0700615 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000616 if (key_index)
617 *key_index = key_index_local;
618 if (password_bytes) {
619 if (is_hex)
620 base::HexStringToBytes(password_text, password_bytes);
621 else
622 password_bytes->insert(password_bytes->end(),
623 password_text.begin(),
624 password_text.end());
625 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700626 }
627}
628
629// static
630bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
631 vector<uint8> passphrase_bytes;
632 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
633 return true;
634 } else {
635 error->Populate(Error::kInvalidPassphrase);
636 return false;
637 }
638}
639
640// static
641bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
642 if (StartsWithASCII(passphrase, "0:", false) ||
643 StartsWithASCII(passphrase, "1:", false) ||
644 StartsWithASCII(passphrase, "2:", false) ||
645 StartsWithASCII(passphrase, "3:", false)) {
646 return true;
647 } else {
648 error->Populate(Error::kInvalidPassphrase);
649 return false;
650 }
651}
652
653// static
654bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
655 if (StartsWithASCII(passphrase, "0x", false)) {
656 return true;
657 } else {
658 error->Populate(Error::kInvalidPassphrase);
659 return false;
660 }
661}
662
Paul Stewart6ab23a92011-11-09 17:17:47 -0800663// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800664string WiFiService::GetSecurityClass(const string &security) {
665 if (security == flimflam::kSecurityRsn ||
666 security == flimflam::kSecurityWpa) {
667 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700668 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800669 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700670 }
671}
672
Paul Stewarta41e38d2011-11-11 07:47:29 -0800673// static
674bool WiFiService::ParseStorageIdentifier(const string &storage_name,
675 string *address,
676 string *mode,
677 string *security) {
678 vector<string> wifi_parts;
679 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800680 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
681 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800682 return false;
683 }
684 *address = wifi_parts[1];
685 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800686 if (wifi_parts.size() == 5) {
687 *security = wifi_parts[4];
688 } else {
689 // Account for security type "802_1x" which got split up above.
690 *security = wifi_parts[4] + "_" + wifi_parts[5];
691 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800692 return true;
693}
694
mukesh agrawale1d90e92012-02-15 17:36:08 -0800695// static
696uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
697 int16 strength;
698 if (signal_dbm > 0) {
699 if (!logged_signal_warning) {
700 LOG(WARNING) << "Signal strength is suspiciously high. "
701 << "Assuming value " << signal_dbm << " is not in dBm.";
702 logged_signal_warning = true;
703 }
704 strength = signal_dbm;
705 } else {
706 strength = 120 + signal_dbm; // Call -20dBm "perfect".
707 }
708
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800709 if (strength > kStrengthMax) {
710 strength = kStrengthMax;
711 } else if (strength < kStrengthMin) {
712 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800713 }
714 return strength;
715}
716
Paul Stewart6ab23a92011-11-09 17:17:47 -0800717string WiFiService::GetGenericStorageIdentifier() const {
718 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
719}
720
Paul Stewartd08f4432011-11-04 07:48:20 -0700721string WiFiService::GetSpecificStorageIdentifier() const {
722 return GetStorageIdentifierForSecurity(security_);
723}
724
725string WiFiService::GetStorageIdentifierForSecurity(
726 const string &security) const {
727 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
728 flimflam::kTypeWifi,
729 wifi_->address().c_str(),
730 hex_ssid_.c_str(),
731 mode_.c_str(),
732 security.c_str()));
733}
734
Gary Moraine4aaf5e2012-04-05 14:37:32 -0700735void WiFiService::set_eap(const EapCredentials &new_eap) {
736 EapCredentials modified_eap = new_eap;
737
738 // An empty key_management field is invalid. Prevent it, if possible.
739 if (modified_eap.key_management.empty()) {
740 modified_eap.key_management = eap().key_management;
741 }
742 Service::set_eap(modified_eap);
Gaurav Shah10109f22011-11-11 20:16:22 -0800743 UpdateConnectable();
744}
745
Paul Stewart4357f4e2012-04-26 17:39:26 -0700746void WiFiService::OnProfileConfigured() {
747 if (profile() || !hidden_ssid()) {
748 return;
749 }
750 // This situation occurs when a hidden WiFi service created via GetService
751 // has been persisted to a profile in Manager::ConfigureService(). Now
752 // that configuration is saved, we must join the service with its profile,
753 // which will make this SSID eligible for directed probes during scans.
754 manager()->RegisterService(this);
755}
756
Gaurav Shah29d68882012-01-30 19:06:42 -0800757bool WiFiService::Is8021x() const {
758 if (security_ == flimflam::kSecurity8021x)
759 return true;
760
761 // Dynamic WEP + 802.1x.
762 if (security_ == flimflam::kSecurityWep &&
763 GetEAPKeyManagement() == "IEEE8021X")
764 return true;
765 return false;
766}
767
Gaurav Shah10109f22011-11-11 20:16:22 -0800768void WiFiService::Populate8021xProperties(
769 std::map<string, DBus::Variant> *params) {
Paul Stewartecf4cd12012-04-17 11:08:39 -0700770 string ca_cert = eap().ca_cert;
771 if (!eap().ca_cert_nss.empty()) {
772 vector<char> id(ssid_.begin(), ssid_.end());
773 FilePath certfile = nss_->GetDERCertfile(eap().ca_cert_nss, id);
774 if (certfile.empty()) {
775 LOG(ERROR) << "Unable to extract certificate: " << eap().ca_cert_nss;
776 } else {
777 ca_cert = certfile.value();
778 }
779 }
780
781
Gaurav Shah10109f22011-11-11 20:16:22 -0800782 typedef std::pair<const char *, const char *> KeyVal;
Paul Stewart20550982012-04-16 12:16:11 -0700783 KeyVal init_propertyvals[] = {
Gaurav Shah10109f22011-11-11 20:16:22 -0800784 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
785 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
786 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
787 eap().inner_eap.c_str()),
788 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
789 eap().anonymous_identity.c_str()),
790 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
791 eap().client_cert.c_str()),
792 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
793 eap().private_key.c_str()),
794 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
795 eap().private_key_password.c_str()),
Paul Stewartecf4cd12012-04-17 11:08:39 -0700796 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, ca_cert.c_str()),
Gaurav Shah10109f22011-11-11 20:16:22 -0800797 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
798 eap().password.c_str()),
799 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
800 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
801 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
802 eap().ca_cert_id.c_str()),
Paul Stewartbc6e7392012-05-24 07:07:48 -0700803 KeyVal(wpa_supplicant::kNetworkPropertyEapSubjectMatch,
804 eap().subject_match.c_str())
Gaurav Shah10109f22011-11-11 20:16:22 -0800805 };
806
Paul Stewart20550982012-04-16 12:16:11 -0700807 vector<KeyVal> propertyvals(init_propertyvals,
808 init_propertyvals + arraysize(init_propertyvals));
809 if (eap().use_system_cas) {
810 propertyvals.push_back(KeyVal(
811 wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath));
Paul Stewartecf4cd12012-04-17 11:08:39 -0700812 } else if (ca_cert.empty()) {
Paul Stewart20550982012-04-16 12:16:11 -0700813 LOG(WARNING) << __func__
814 << ": No certificate authorities are configured."
815 << " Server certificates will be accepted"
816 << " unconditionally.";
817 }
818
819 if (!eap().cert_id.empty() || !eap().key_id.empty() ||
820 !eap().ca_cert_id.empty()) {
821 propertyvals.push_back(KeyVal(
822 wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()));
823 propertyvals.push_back(KeyVal(
824 wpa_supplicant::kNetworkPropertyEngineId,
825 wpa_supplicant::kEnginePKCS11));
826 // We can't use the propertyvals vector for this since this argument
827 // is a uint32, not a string.
828 (*params)[wpa_supplicant::kNetworkPropertyEngine].writer().
829 append_uint32(wpa_supplicant::kDefaultEngine);
830 }
831
832 vector<KeyVal>::iterator it;
833 for (it = propertyvals.begin(); it != propertyvals.end(); ++it) {
834 if (strlen((*it).second) > 0) {
835 (*params)[(*it).first].writer().append_string((*it).second);
Gaurav Shah10109f22011-11-11 20:16:22 -0800836 }
837 }
838}
839
mukesh agrawalb54601c2011-06-07 17:39:22 -0700840} // namespace shill