blob: 58a4a6169ba6b571a39b503f153554b2db1624fd [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
Chris Masone34af2182011-08-22 11:59:36 -070010#include <base/stringprintf.h>
11#include <base/string_number_conversions.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080012#include <base/string_split.h>
Chris Masone34af2182011-08-22 11:59:36 -070013#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070015#include <dbus/dbus.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070016
mukesh agrawale1d90e92012-02-15 17:36:08 -080017#include "shill/adaptor_interfaces.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070018#include "shill/control_interface.h"
19#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070020#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070022#include "shill/ieee80211.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070023#include "shill/logging.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_);
Paul Stewart0cab5682012-09-13 18:50:34 -070079 store->RegisterBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
Paul Stewartac4ac002011-08-26 12:04:26 -070080 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_);
Paul Stewart72b2fdc2012-06-02 08:58:51 -070083 store->RegisterConstStringmap(kWifiVendorInformationProperty,
84 &vendor_information_);
mukesh agrawal32399322011-09-01 10:53:43 -070085
mukesh agrawald835b202011-10-07 15:26:47 -070086 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
87 string ssid_string(
88 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080089 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070090 // WifiHexSsid property should only be present if Name property
91 // has been munged.
92 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
93 }
94 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070095
mukesh agrawal6e277772011-09-29 15:04:23 -070096 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
97 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080098 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080099 // Passphrases are not mandatory for 802.1X.
100 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -0700101 } else if (security_ == flimflam::kSecurityPsk) {
102 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700103 } else if (security_ == flimflam::kSecurityRsn) {
104 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700105 } else if (security_ == flimflam::kSecurityWpa) {
106 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700107 } else if (security_ == flimflam::kSecurityWep) {
108 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700109 } else if (security_ == flimflam::kSecurityNone) {
110 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700111 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800112 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700113 }
114
Paul Stewartd08f4432011-11-04 07:48:20 -0700115 // Until we know better (at Profile load time), use the generic name.
116 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000117 UpdateConnectable();
Paul Stewartcb59fed2012-03-21 21:14:46 -0700118
119 IgnoreParameterForConfigure(flimflam::kModeProperty);
120 IgnoreParameterForConfigure(flimflam::kSSIDProperty);
121 IgnoreParameterForConfigure(flimflam::kSecurityProperty);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700122}
123
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200124WiFiService::~WiFiService() {}
mukesh agrawalb54601c2011-06-07 17:39:22 -0700125
mukesh agrawalbf14e942012-03-02 14:36:34 -0800126bool WiFiService::IsAutoConnectable(const char **reason) const {
127 if (!Service::IsAutoConnectable(reason)) {
128 return false;
129 }
130
131 // Only auto-connect to Services which have visible Endpoints.
132 // (Needed because hidden Services may remain registered with
133 // Manager even without visible Endpoints.)
134 if (!HasEndpoints()) {
135 *reason = kAutoConnNoEndpoint;
136 return false;
137 }
138
139 // Do not preempt an existing connection (whether pending, or
140 // connected, and whether to this service, or another).
141 if (!wifi_->IsIdle()) {
142 *reason = kAutoConnBusy;
143 return false;
144 }
145
146 return true;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000147}
148
Darin Petkov4a66cc52012-06-15 10:08:29 +0200149void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000150 DCHECK(endpoint->ssid() == ssid());
151 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800152 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000153}
154
Darin Petkov4a66cc52012-06-15 10:08:29 +0200155void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000156 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
157 DCHECK(i != endpoints_.end());
158 if (i == endpoints_.end()) {
159 LOG(WARNING) << "In " << __func__ << "(): "
160 << "ignorning non-existent endpoint "
161 << endpoint->bssid_string();
162 return;
163 }
164 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800165 if (current_endpoint_ == endpoint) {
166 current_endpoint_ = NULL;
167 }
168 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000169}
170
mukesh agrawale1d90e92012-02-15 17:36:08 -0800171void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
172 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
173 current_endpoint_ = endpoint;
174 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000175}
176
mukesh agrawalb20776f2012-02-10 16:00:36 -0800177void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
178 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800179 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800180}
181
Chris Masone6515aab2011-10-12 16:19:09 -0700182string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700183 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700184}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700185
mukesh agrawal1a056262011-10-05 14:36:54 -0700186void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
187 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000188 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700189 } else if (security_ == flimflam::kSecurityPsk ||
190 security_ == flimflam::kSecurityWpa ||
191 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000192 ValidateWPAPassphrase(passphrase, error);
193 } else {
194 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700195 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000196
Paul Stewart2706aaf2011-12-14 16:44:04 -0800197 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000198 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800199 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000200
201 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700202}
203
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800204// ClearPassphrase is separate from SetPassphrase, because the default
205// value for |passphrase_| would not pass validation.
206void WiFiService::ClearPassphrase(Error */*error*/) {
207 passphrase_.clear();
208 UpdateConnectable();
209}
210
Paul Stewartd08f4432011-11-04 07:48:20 -0700211bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
212 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
213 storage->ContainsGroup(GetSpecificStorageIdentifier());
214}
215
Paul Stewarta41e38d2011-11-11 07:47:29 -0800216bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800217 // WiFi Services should be displayed only if they are in range (have
218 // endpoints that have shown up in a scan) or if the service is actively
219 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000220 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800221}
222
Paul Stewartd08f4432011-11-04 07:48:20 -0700223bool WiFiService::Load(StoreInterface *storage) {
224 // First find out which storage identifier is available in priority order
225 // of specific, generic.
226 string id = GetSpecificStorageIdentifier();
227 if (!storage->ContainsGroup(id)) {
228 id = GetGenericStorageIdentifier();
229 if (!storage->ContainsGroup(id)) {
230 LOG(WARNING) << "Service is not available in the persistent store: "
231 << id;
232 return false;
233 }
234 }
235
236 // Set our storage identifier to match the storage name in the Profile.
237 storage_identifier_ = id;
238
239 // Load properties common to all Services.
240 if (!Service::Load(storage)) {
241 return false;
242 }
243
244 // Load properties specific to WiFi services.
245 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000246
Paul Stewart2706aaf2011-12-14 16:44:04 -0800247 // NB: mode, security and ssid parameters are never read in from
248 // Load() as they are provided from the scan.
249
250 string passphrase;
251 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
252 Error error;
253 SetPassphrase(passphrase, &error);
254 if (!error.IsSuccess()) {
255 LOG(ERROR) << "Passphrase could not be set: "
256 << Error::GetName(error.type());
257 }
258 }
259
Paul Stewartd08f4432011-11-04 07:48:20 -0700260 return true;
261}
262
263bool WiFiService::Save(StoreInterface *storage) {
264 // Save properties common to all Services.
265 if (!Service::Save(storage)) {
266 return false;
267 }
268
269 // Save properties specific to WiFi services.
270 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800271 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
272 storage->SetString(id, kStorageMode, mode_);
273 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
274 storage->SetString(id, kStorageSecurity, security_);
275 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000276
Paul Stewartd08f4432011-11-04 07:48:20 -0700277 return true;
278}
279
Paul Stewart65512e12012-03-26 18:01:08 -0700280bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800281 Service::Unload();
282 hidden_ssid_ = false;
Wade Guthrie005bd342012-05-02 09:37:07 -0700283 Error unused_error;
284 ClearPassphrase(&unused_error);
Paul Stewart66c86002012-01-30 18:00:52 -0800285 if (security_ == flimflam::kSecurity8021x) {
286 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
287 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
288 // Make sure that these are cleared when credentials for networks
289 // of this type goes away.
290 //
291 // When wpa_supplicant gains the ability, do this credential
292 // clearing on a per-service basis. Also do this whenever the credentials
293 // for a service changes. crosbug.com/25670
294 wifi_->ClearCachedCredentials();
295 }
Paul Stewart65512e12012-03-26 18:01:08 -0700296 return !IsVisible();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800297}
298
Paul Stewart6ab23a92011-11-09 17:17:47 -0800299bool WiFiService::IsSecurityMatch(const string &security) const {
300 return GetSecurityClass(security) == GetSecurityClass(security_);
301}
302
Thieu Le48e6d6d2011-12-06 00:40:27 +0000303void WiFiService::InitializeCustomMetrics() const {
304 string histogram = metrics()->GetFullMetricName(
305 Metrics::kMetricTimeToJoinMilliseconds,
306 technology());
307 metrics()->AddServiceStateTransitionTimer(this,
308 histogram,
309 Service::kStateAssociating,
310 Service::kStateConfiguring);
311}
312
Thieu Leb84ba342012-03-02 15:15:19 -0800313void WiFiService::SendPostReadyStateMetrics(
314 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000315 metrics()->SendEnumToUMA(
316 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
317 technology()),
318 Metrics::WiFiFrequencyToChannel(frequency_),
319 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000320
321 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
322 metrics()->SendEnumToUMA(
323 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
324 technology()),
325 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
326 Metrics::kWiFiNetworkPhyModeMax);
327
328 Metrics::WiFiSecurity security_uma =
329 Metrics::WiFiSecurityStringToEnum(security_);
330 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
331 metrics()->SendEnumToUMA(
332 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
333 technology()),
334 security_uma,
335 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800336
337 if (time_resume_to_ready_milliseconds > 0) {
338 metrics()->SendToUMA(
339 metrics()->GetFullMetricName(
340 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
341 time_resume_to_ready_milliseconds,
342 Metrics::kTimerHistogramMillisecondsMin,
343 Metrics::kTimerHistogramMillisecondsMax,
344 Metrics::kTimerHistogramNumBuckets);
345 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000346}
347
mukesh agrawal32399322011-09-01 10:53:43 -0700348// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800349void WiFiService::HelpRegisterWriteOnlyDerivedString(
350 const string &name,
351 void(WiFiService::*set)(const string &, Error *),
352 void(WiFiService::*clear)(Error *),
353 const string *default_value) {
354 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000355 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800356 StringAccessor(
357 new CustomWriteOnlyAccessor<WiFiService, string>(
358 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000359}
360
Wade Guthrie005bd342012-05-02 09:37:07 -0700361void WiFiService::Connect(Error *error) {
362 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name();
mukesh agrawal6e277772011-09-29 15:04:23 -0700363 std::map<string, DBus::Variant> params;
364 DBus::MessageIter writer;
365
Wade Guthrie005bd342012-05-02 09:37:07 -0700366 if (!connectable()) {
367 LOG(ERROR) << "Can't connect. Service " << friendly_name()
368 << " is not connectable";
369 Error::PopulateAndLog(error, Error::kOperationFailed,
370 Error::GetDefaultMessage(Error::kOperationFailed));
371 return;
372 }
373
mukesh agrawal6e277772011-09-29 15:04:23 -0700374 params[wpa_supplicant::kNetworkPropertyMode].writer().
375 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
376
Paul Stewarte2d7c502012-07-16 16:35:10 -0700377 if (mode_ == flimflam::kModeAdhoc && frequency_ != 0) {
378 // Frequency is required in order to successfully conntect to an IBSS
379 // with wpa_supplicant. If we have one from our endpoint, insert it
380 // here.
381 params[wpa_supplicant::kNetworkPropertyFrequency].writer().
382 append_int32(frequency_);
383 }
384
Gaurav Shah29d68882012-01-30 19:06:42 -0800385 if (Is8021x()) {
386 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800387 if (GetEAPKeyManagement().empty())
388 SetEAPKeyManagement("WPA-EAP");
389 Populate8021xProperties(&params);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700390 ClearEAPCertification();
mukesh agrawal6e277772011-09-29 15:04:23 -0700391 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800392 const string psk_proto = StringPrintf("%s %s",
393 wpa_supplicant::kSecurityModeWPA,
394 wpa_supplicant::kSecurityModeRSN);
395 params[wpa_supplicant::kPropertySecurityProtocol].writer().
396 append_string(psk_proto.c_str());
397 params[wpa_supplicant::kPropertyPreSharedKey].writer().
398 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700399 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700400 params[wpa_supplicant::kPropertySecurityProtocol].writer().
401 append_string(wpa_supplicant::kSecurityModeRSN);
402 params[wpa_supplicant::kPropertyPreSharedKey].writer().
403 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700404 } else if (security_ == flimflam::kSecurityWpa) {
405 params[wpa_supplicant::kPropertySecurityProtocol].writer().
406 append_string(wpa_supplicant::kSecurityModeWPA);
407 params[wpa_supplicant::kPropertyPreSharedKey].writer().
408 append_string(passphrase_.c_str());
409 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000410 params[wpa_supplicant::kPropertyAuthAlg].writer().
411 append_string(wpa_supplicant::kSecurityAuthAlg);
412 Error error;
413 int key_index;
414 std::vector<uint8> password_bytes;
415 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
416 writer = params[wpa_supplicant::kPropertyWEPKey +
417 base::IntToString(key_index)].writer();
418 writer << password_bytes;
419 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
420 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700421 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800422 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700423 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800424 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700425 }
426
Gaurav Shah10109f22011-11-11 20:16:22 -0800427 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700428 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800429
430 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700431 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
432 writer << ssid_;
433
434 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700435}
436
Eric Shienbrood9a245532012-03-07 14:20:39 -0500437void WiFiService::Disconnect(Error *error) {
438 LOG(INFO) << __func__;
439 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000440 wifi_->DisconnectFrom(this);
441}
442
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800443string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700444 return wifi_->GetRpcIdentifier();
445}
446
mukesh agrawal29c13a12011-11-24 00:09:19 +0000447void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800448 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000449 if (security_ == flimflam::kSecurityNone) {
450 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800451 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800452 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800453 } else if (Is8021x()) {
454 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000455 } else if (security_ == flimflam::kSecurityWep ||
456 security_ == flimflam::kSecurityWpa ||
457 security_ == flimflam::kSecurityPsk ||
458 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800459 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800460 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000461 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800462 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000463}
464
mukesh agrawale1d90e92012-02-15 17:36:08 -0800465void WiFiService::UpdateFromEndpoints() {
466 const WiFiEndpoint *representative_endpoint = NULL;
467
468 if (current_endpoint_) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800469 representative_endpoint = current_endpoint_;
470 } else {
471 int16 best_signal = std::numeric_limits<int16>::min();
472 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
473 i != endpoints_.end(); ++i) {
474 if ((*i)->signal_strength() >= best_signal) {
475 best_signal = (*i)->signal_strength();
476 representative_endpoint = *i;
477 }
478 }
479 }
480
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700481 uint16 frequency = 0;
482 int16 signal = std::numeric_limits<int16>::min();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700483 string bssid;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700484 Stringmap vendor_information;
485 if (representative_endpoint) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800486 frequency = representative_endpoint->frequency();
487 signal = representative_endpoint->signal_strength();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700488 bssid = representative_endpoint->bssid_string();
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700489 vendor_information = representative_endpoint->GetVendorInformation();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800490 }
491
492 if (frequency_ != frequency) {
493 frequency_ = frequency;
494 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
495 }
mukesh agrawal923f14f2012-06-04 16:46:08 -0700496 if (bssid_ != bssid) {
497 bssid_ = bssid;
498 adaptor()->EmitStringChanged(flimflam::kWifiBSsid, bssid_);
499 }
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700500 if (vendor_information_ != vendor_information) {
501 vendor_information_ = vendor_information;
502 adaptor()->EmitStringmapChanged(kWifiVendorInformationProperty,
503 vendor_information_);
504 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800505 SetStrength(SignalToStrength(signal));
506}
507
mukesh agrawal1a056262011-10-05 14:36:54 -0700508// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000509void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
510 Error *error) {
511 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700512}
513
514// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000515void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
516 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700517 unsigned int length = passphrase.length();
518 vector<uint8> passphrase_bytes;
519
520 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
521 if (length != IEEE_80211::kWPAHexLen &&
522 (length < IEEE_80211::kWPAAsciiMinLen ||
523 length > IEEE_80211::kWPAAsciiMaxLen)) {
524 error->Populate(Error::kInvalidPassphrase);
525 }
526 } else {
527 if (length < IEEE_80211::kWPAAsciiMinLen ||
528 length > IEEE_80211::kWPAAsciiMaxLen) {
529 error->Populate(Error::kInvalidPassphrase);
530 }
531 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000532}
mukesh agrawal1a056262011-10-05 14:36:54 -0700533
Thieu Lef4cbda92011-11-10 23:41:24 +0000534// static
535void WiFiService::ParseWEPPassphrase(const string &passphrase,
536 int *key_index,
537 std::vector<uint8> *password_bytes,
538 Error *error) {
539 unsigned int length = passphrase.length();
540 int key_index_local;
541 std::string password_text;
542 bool is_hex = false;
543
544 switch (length) {
545 case IEEE_80211::kWEP40AsciiLen:
546 case IEEE_80211::kWEP104AsciiLen:
547 key_index_local = 0;
548 password_text = passphrase;
549 break;
550 case IEEE_80211::kWEP40AsciiLen + 2:
551 case IEEE_80211::kWEP104AsciiLen + 2:
552 if (CheckWEPKeyIndex(passphrase, error)) {
553 base::StringToInt(passphrase.substr(0,1), &key_index_local);
554 password_text = passphrase.substr(2);
555 }
556 break;
557 case IEEE_80211::kWEP40HexLen:
558 case IEEE_80211::kWEP104HexLen:
559 if (CheckWEPIsHex(passphrase, error)) {
560 key_index_local = 0;
561 password_text = passphrase;
562 is_hex = true;
563 }
564 break;
565 case IEEE_80211::kWEP40HexLen + 2:
566 case IEEE_80211::kWEP104HexLen + 2:
567 if(CheckWEPKeyIndex(passphrase, error) &&
568 CheckWEPIsHex(passphrase.substr(2), error)) {
569 base::StringToInt(passphrase.substr(0,1), &key_index_local);
570 password_text = passphrase.substr(2);
571 is_hex = true;
572 } else if (CheckWEPPrefix(passphrase, error) &&
573 CheckWEPIsHex(passphrase.substr(2), error)) {
574 key_index_local = 0;
575 password_text = passphrase.substr(2);
576 is_hex = true;
577 }
578 break;
579 case IEEE_80211::kWEP40HexLen + 4:
580 case IEEE_80211::kWEP104HexLen + 4:
581 if (CheckWEPKeyIndex(passphrase, error) &&
582 CheckWEPPrefix(passphrase.substr(2), error) &&
583 CheckWEPIsHex(passphrase.substr(4), error)) {
584 base::StringToInt(passphrase.substr(0,1), &key_index_local);
585 password_text = passphrase.substr(4);
586 is_hex = true;
587 }
588 break;
589 default:
590 error->Populate(Error::kInvalidPassphrase);
591 break;
592 }
593
mukesh agrawal1a056262011-10-05 14:36:54 -0700594 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000595 if (key_index)
596 *key_index = key_index_local;
597 if (password_bytes) {
598 if (is_hex)
599 base::HexStringToBytes(password_text, password_bytes);
600 else
601 password_bytes->insert(password_bytes->end(),
602 password_text.begin(),
603 password_text.end());
604 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700605 }
606}
607
608// static
609bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
610 vector<uint8> passphrase_bytes;
611 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
612 return true;
613 } else {
614 error->Populate(Error::kInvalidPassphrase);
615 return false;
616 }
617}
618
619// static
620bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
621 if (StartsWithASCII(passphrase, "0:", false) ||
622 StartsWithASCII(passphrase, "1:", false) ||
623 StartsWithASCII(passphrase, "2:", false) ||
624 StartsWithASCII(passphrase, "3:", false)) {
625 return true;
626 } else {
627 error->Populate(Error::kInvalidPassphrase);
628 return false;
629 }
630}
631
632// static
633bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
634 if (StartsWithASCII(passphrase, "0x", false)) {
635 return true;
636 } else {
637 error->Populate(Error::kInvalidPassphrase);
638 return false;
639 }
640}
641
Paul Stewart6ab23a92011-11-09 17:17:47 -0800642// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800643string WiFiService::GetSecurityClass(const string &security) {
644 if (security == flimflam::kSecurityRsn ||
645 security == flimflam::kSecurityWpa) {
646 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700647 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800648 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700649 }
650}
651
Paul Stewarta41e38d2011-11-11 07:47:29 -0800652// static
653bool WiFiService::ParseStorageIdentifier(const string &storage_name,
654 string *address,
655 string *mode,
656 string *security) {
657 vector<string> wifi_parts;
658 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800659 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
660 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800661 return false;
662 }
663 *address = wifi_parts[1];
664 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800665 if (wifi_parts.size() == 5) {
666 *security = wifi_parts[4];
667 } else {
668 // Account for security type "802_1x" which got split up above.
669 *security = wifi_parts[4] + "_" + wifi_parts[5];
670 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800671 return true;
672}
673
mukesh agrawale1d90e92012-02-15 17:36:08 -0800674// static
675uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
676 int16 strength;
677 if (signal_dbm > 0) {
678 if (!logged_signal_warning) {
679 LOG(WARNING) << "Signal strength is suspiciously high. "
680 << "Assuming value " << signal_dbm << " is not in dBm.";
681 logged_signal_warning = true;
682 }
683 strength = signal_dbm;
684 } else {
685 strength = 120 + signal_dbm; // Call -20dBm "perfect".
686 }
687
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800688 if (strength > kStrengthMax) {
689 strength = kStrengthMax;
690 } else if (strength < kStrengthMin) {
691 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800692 }
693 return strength;
694}
695
Paul Stewart6ab23a92011-11-09 17:17:47 -0800696string WiFiService::GetGenericStorageIdentifier() const {
697 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
698}
699
Paul Stewartd08f4432011-11-04 07:48:20 -0700700string WiFiService::GetSpecificStorageIdentifier() const {
701 return GetStorageIdentifierForSecurity(security_);
702}
703
704string WiFiService::GetStorageIdentifierForSecurity(
705 const string &security) const {
706 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
707 flimflam::kTypeWifi,
708 wifi_->address().c_str(),
709 hex_ssid_.c_str(),
710 mode_.c_str(),
711 security.c_str()));
712}
713
Gary Moraine4aaf5e2012-04-05 14:37:32 -0700714void WiFiService::set_eap(const EapCredentials &new_eap) {
715 EapCredentials modified_eap = new_eap;
716
717 // An empty key_management field is invalid. Prevent it, if possible.
718 if (modified_eap.key_management.empty()) {
719 modified_eap.key_management = eap().key_management;
720 }
721 Service::set_eap(modified_eap);
Gaurav Shah10109f22011-11-11 20:16:22 -0800722 UpdateConnectable();
723}
724
Paul Stewart4357f4e2012-04-26 17:39:26 -0700725void WiFiService::OnProfileConfigured() {
726 if (profile() || !hidden_ssid()) {
727 return;
728 }
729 // This situation occurs when a hidden WiFi service created via GetService
730 // has been persisted to a profile in Manager::ConfigureService(). Now
731 // that configuration is saved, we must join the service with its profile,
732 // which will make this SSID eligible for directed probes during scans.
733 manager()->RegisterService(this);
734}
735
Gaurav Shah29d68882012-01-30 19:06:42 -0800736bool WiFiService::Is8021x() const {
737 if (security_ == flimflam::kSecurity8021x)
738 return true;
739
740 // Dynamic WEP + 802.1x.
741 if (security_ == flimflam::kSecurityWep &&
742 GetEAPKeyManagement() == "IEEE8021X")
743 return true;
744 return false;
745}
746
Gaurav Shah10109f22011-11-11 20:16:22 -0800747void WiFiService::Populate8021xProperties(
748 std::map<string, DBus::Variant> *params) {
Paul Stewartecf4cd12012-04-17 11:08:39 -0700749 string ca_cert = eap().ca_cert;
750 if (!eap().ca_cert_nss.empty()) {
751 vector<char> id(ssid_.begin(), ssid_.end());
752 FilePath certfile = nss_->GetDERCertfile(eap().ca_cert_nss, id);
753 if (certfile.empty()) {
754 LOG(ERROR) << "Unable to extract certificate: " << eap().ca_cert_nss;
755 } else {
756 ca_cert = certfile.value();
757 }
758 }
759
760
Gaurav Shah10109f22011-11-11 20:16:22 -0800761 typedef std::pair<const char *, const char *> KeyVal;
Paul Stewart20550982012-04-16 12:16:11 -0700762 KeyVal init_propertyvals[] = {
Gaurav Shah10109f22011-11-11 20:16:22 -0800763 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
764 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
765 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
766 eap().inner_eap.c_str()),
767 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
768 eap().anonymous_identity.c_str()),
769 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
770 eap().client_cert.c_str()),
771 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
772 eap().private_key.c_str()),
773 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
774 eap().private_key_password.c_str()),
Paul Stewartecf4cd12012-04-17 11:08:39 -0700775 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, ca_cert.c_str()),
Gaurav Shah10109f22011-11-11 20:16:22 -0800776 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
777 eap().password.c_str()),
778 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
779 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
780 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
781 eap().ca_cert_id.c_str()),
Paul Stewartbc6e7392012-05-24 07:07:48 -0700782 KeyVal(wpa_supplicant::kNetworkPropertyEapSubjectMatch,
783 eap().subject_match.c_str())
Gaurav Shah10109f22011-11-11 20:16:22 -0800784 };
785
Paul Stewart20550982012-04-16 12:16:11 -0700786 vector<KeyVal> propertyvals(init_propertyvals,
787 init_propertyvals + arraysize(init_propertyvals));
788 if (eap().use_system_cas) {
789 propertyvals.push_back(KeyVal(
790 wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath));
Paul Stewartecf4cd12012-04-17 11:08:39 -0700791 } else if (ca_cert.empty()) {
Paul Stewart20550982012-04-16 12:16:11 -0700792 LOG(WARNING) << __func__
793 << ": No certificate authorities are configured."
794 << " Server certificates will be accepted"
795 << " unconditionally.";
796 }
797
798 if (!eap().cert_id.empty() || !eap().key_id.empty() ||
799 !eap().ca_cert_id.empty()) {
800 propertyvals.push_back(KeyVal(
801 wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()));
802 propertyvals.push_back(KeyVal(
803 wpa_supplicant::kNetworkPropertyEngineId,
804 wpa_supplicant::kEnginePKCS11));
805 // We can't use the propertyvals vector for this since this argument
806 // is a uint32, not a string.
807 (*params)[wpa_supplicant::kNetworkPropertyEngine].writer().
808 append_uint32(wpa_supplicant::kDefaultEngine);
809 }
810
811 vector<KeyVal>::iterator it;
812 for (it = propertyvals.begin(); it != propertyvals.end(); ++it) {
813 if (strlen((*it).second) > 0) {
814 (*params)[(*it).first].writer().append_string((*it).second);
Gaurav Shah10109f22011-11-11 20:16:22 -0800815 }
816 }
817}
818
mukesh agrawalb54601c2011-06-07 17:39:22 -0700819} // namespace shill