blob: 68f528afc1b3d6a5f3ae6ebbf460161115811d61 [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
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
149bool WiFiService::IsConnecting() const {
150 // WiFi does not move us into the associating state until it gets
151 // feedback from wpa_supplicant. So, to answer whether or
152 // not we're connecting, we consult with |wifi_|.
153 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800154}
155
Darin Petkov4a66cc52012-06-15 10:08:29 +0200156void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000157 DCHECK(endpoint->ssid() == ssid());
158 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800159 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000160}
161
Darin Petkov4a66cc52012-06-15 10:08:29 +0200162void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000163 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
164 DCHECK(i != endpoints_.end());
165 if (i == endpoints_.end()) {
166 LOG(WARNING) << "In " << __func__ << "(): "
167 << "ignorning non-existent endpoint "
168 << endpoint->bssid_string();
169 return;
170 }
171 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800172 if (current_endpoint_ == endpoint) {
173 current_endpoint_ = NULL;
174 }
175 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000176}
177
mukesh agrawale1d90e92012-02-15 17:36:08 -0800178void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
179 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
180 current_endpoint_ = endpoint;
181 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000182}
183
mukesh agrawalb20776f2012-02-10 16:00:36 -0800184void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
185 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800186 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800187}
188
Chris Masone6515aab2011-10-12 16:19:09 -0700189string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700190 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700191}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700192
mukesh agrawal1a056262011-10-05 14:36:54 -0700193void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
194 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000195 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700196 } else if (security_ == flimflam::kSecurityPsk ||
197 security_ == flimflam::kSecurityWpa ||
198 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000199 ValidateWPAPassphrase(passphrase, error);
200 } else {
201 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700202 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000203
Paul Stewart2706aaf2011-12-14 16:44:04 -0800204 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000205 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800206 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000207
208 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700209}
210
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800211// ClearPassphrase is separate from SetPassphrase, because the default
212// value for |passphrase_| would not pass validation.
213void WiFiService::ClearPassphrase(Error */*error*/) {
214 passphrase_.clear();
215 UpdateConnectable();
216}
217
Paul Stewartd08f4432011-11-04 07:48:20 -0700218bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
219 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
220 storage->ContainsGroup(GetSpecificStorageIdentifier());
221}
222
Paul Stewarta41e38d2011-11-11 07:47:29 -0800223bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800224 // WiFi Services should be displayed only if they are in range (have
225 // endpoints that have shown up in a scan) or if the service is actively
226 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000227 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800228}
229
Paul Stewartd08f4432011-11-04 07:48:20 -0700230bool WiFiService::Load(StoreInterface *storage) {
231 // First find out which storage identifier is available in priority order
232 // of specific, generic.
233 string id = GetSpecificStorageIdentifier();
234 if (!storage->ContainsGroup(id)) {
235 id = GetGenericStorageIdentifier();
236 if (!storage->ContainsGroup(id)) {
237 LOG(WARNING) << "Service is not available in the persistent store: "
238 << id;
239 return false;
240 }
241 }
242
243 // Set our storage identifier to match the storage name in the Profile.
244 storage_identifier_ = id;
245
246 // Load properties common to all Services.
247 if (!Service::Load(storage)) {
248 return false;
249 }
250
251 // Load properties specific to WiFi services.
252 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000253
Paul Stewart2706aaf2011-12-14 16:44:04 -0800254 // NB: mode, security and ssid parameters are never read in from
255 // Load() as they are provided from the scan.
256
257 string passphrase;
258 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
259 Error error;
260 SetPassphrase(passphrase, &error);
261 if (!error.IsSuccess()) {
262 LOG(ERROR) << "Passphrase could not be set: "
263 << Error::GetName(error.type());
264 }
265 }
266
Paul Stewartd08f4432011-11-04 07:48:20 -0700267 return true;
268}
269
270bool WiFiService::Save(StoreInterface *storage) {
271 // Save properties common to all Services.
272 if (!Service::Save(storage)) {
273 return false;
274 }
275
276 // Save properties specific to WiFi services.
277 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800278 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
279 storage->SetString(id, kStorageMode, mode_);
280 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
281 storage->SetString(id, kStorageSecurity, security_);
282 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000283
Paul Stewartd08f4432011-11-04 07:48:20 -0700284 return true;
285}
286
Paul Stewart65512e12012-03-26 18:01:08 -0700287bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800288 Service::Unload();
289 hidden_ssid_ = false;
Wade Guthrie005bd342012-05-02 09:37:07 -0700290 Error unused_error;
291 ClearPassphrase(&unused_error);
Paul Stewart66c86002012-01-30 18:00:52 -0800292 if (security_ == flimflam::kSecurity8021x) {
293 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
294 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
295 // Make sure that these are cleared when credentials for networks
296 // of this type goes away.
297 //
298 // When wpa_supplicant gains the ability, do this credential
299 // clearing on a per-service basis. Also do this whenever the credentials
300 // for a service changes. crosbug.com/25670
301 wifi_->ClearCachedCredentials();
302 }
Paul Stewart65512e12012-03-26 18:01:08 -0700303 return !IsVisible();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800304}
305
Paul Stewart6ab23a92011-11-09 17:17:47 -0800306bool WiFiService::IsSecurityMatch(const string &security) const {
307 return GetSecurityClass(security) == GetSecurityClass(security_);
308}
309
Thieu Le48e6d6d2011-12-06 00:40:27 +0000310void WiFiService::InitializeCustomMetrics() const {
311 string histogram = metrics()->GetFullMetricName(
312 Metrics::kMetricTimeToJoinMilliseconds,
313 technology());
314 metrics()->AddServiceStateTransitionTimer(this,
315 histogram,
316 Service::kStateAssociating,
317 Service::kStateConfiguring);
318}
319
Thieu Leb84ba342012-03-02 15:15:19 -0800320void WiFiService::SendPostReadyStateMetrics(
321 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000322 metrics()->SendEnumToUMA(
323 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
324 technology()),
325 Metrics::WiFiFrequencyToChannel(frequency_),
326 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000327
328 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
329 metrics()->SendEnumToUMA(
330 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
331 technology()),
332 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
333 Metrics::kWiFiNetworkPhyModeMax);
334
335 Metrics::WiFiSecurity security_uma =
336 Metrics::WiFiSecurityStringToEnum(security_);
337 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
338 metrics()->SendEnumToUMA(
339 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
340 technology()),
341 security_uma,
342 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800343
344 if (time_resume_to_ready_milliseconds > 0) {
345 metrics()->SendToUMA(
346 metrics()->GetFullMetricName(
347 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
348 time_resume_to_ready_milliseconds,
349 Metrics::kTimerHistogramMillisecondsMin,
350 Metrics::kTimerHistogramMillisecondsMax,
351 Metrics::kTimerHistogramNumBuckets);
352 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000353}
354
mukesh agrawal32399322011-09-01 10:53:43 -0700355// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800356void WiFiService::HelpRegisterWriteOnlyDerivedString(
357 const string &name,
358 void(WiFiService::*set)(const string &, Error *),
359 void(WiFiService::*clear)(Error *),
360 const string *default_value) {
361 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000362 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800363 StringAccessor(
364 new CustomWriteOnlyAccessor<WiFiService, string>(
365 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000366}
367
Wade Guthrie005bd342012-05-02 09:37:07 -0700368void WiFiService::Connect(Error *error) {
369 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name();
mukesh agrawal6e277772011-09-29 15:04:23 -0700370 std::map<string, DBus::Variant> params;
371 DBus::MessageIter writer;
372
Wade Guthrie005bd342012-05-02 09:37:07 -0700373 if (!connectable()) {
374 LOG(ERROR) << "Can't connect. Service " << friendly_name()
375 << " is not connectable";
376 Error::PopulateAndLog(error, Error::kOperationFailed,
377 Error::GetDefaultMessage(Error::kOperationFailed));
378 return;
379 }
380
mukesh agrawal6e277772011-09-29 15:04:23 -0700381 params[wpa_supplicant::kNetworkPropertyMode].writer().
382 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
383
Gaurav Shah29d68882012-01-30 19:06:42 -0800384 if (Is8021x()) {
385 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800386 if (GetEAPKeyManagement().empty())
387 SetEAPKeyManagement("WPA-EAP");
388 Populate8021xProperties(&params);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700389 ClearEAPCertification();
mukesh agrawal6e277772011-09-29 15:04:23 -0700390 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800391 const string psk_proto = StringPrintf("%s %s",
392 wpa_supplicant::kSecurityModeWPA,
393 wpa_supplicant::kSecurityModeRSN);
394 params[wpa_supplicant::kPropertySecurityProtocol].writer().
395 append_string(psk_proto.c_str());
396 params[wpa_supplicant::kPropertyPreSharedKey].writer().
397 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700398 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700399 params[wpa_supplicant::kPropertySecurityProtocol].writer().
400 append_string(wpa_supplicant::kSecurityModeRSN);
401 params[wpa_supplicant::kPropertyPreSharedKey].writer().
402 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700403 } else if (security_ == flimflam::kSecurityWpa) {
404 params[wpa_supplicant::kPropertySecurityProtocol].writer().
405 append_string(wpa_supplicant::kSecurityModeWPA);
406 params[wpa_supplicant::kPropertyPreSharedKey].writer().
407 append_string(passphrase_.c_str());
408 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000409 params[wpa_supplicant::kPropertyAuthAlg].writer().
410 append_string(wpa_supplicant::kSecurityAuthAlg);
411 Error error;
412 int key_index;
413 std::vector<uint8> password_bytes;
414 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
415 writer = params[wpa_supplicant::kPropertyWEPKey +
416 base::IntToString(key_index)].writer();
417 writer << password_bytes;
418 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
419 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700420 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800421 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700422 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800423 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700424 }
425
Gaurav Shah10109f22011-11-11 20:16:22 -0800426 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700427 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800428
429 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700430 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
431 writer << ssid_;
432
433 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700434}
435
Eric Shienbrood9a245532012-03-07 14:20:39 -0500436void WiFiService::Disconnect(Error *error) {
437 LOG(INFO) << __func__;
438 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000439 wifi_->DisconnectFrom(this);
440}
441
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800442string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700443 return wifi_->GetRpcIdentifier();
444}
445
mukesh agrawal29c13a12011-11-24 00:09:19 +0000446void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800447 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000448 if (security_ == flimflam::kSecurityNone) {
449 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800450 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800451 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800452 } else if (Is8021x()) {
453 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000454 } else if (security_ == flimflam::kSecurityWep ||
455 security_ == flimflam::kSecurityWpa ||
456 security_ == flimflam::kSecurityPsk ||
457 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800458 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800459 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000460 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800461 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000462}
463
mukesh agrawale1d90e92012-02-15 17:36:08 -0800464void WiFiService::UpdateFromEndpoints() {
465 const WiFiEndpoint *representative_endpoint = NULL;
466
467 if (current_endpoint_) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800468 representative_endpoint = current_endpoint_;
469 } else {
470 int16 best_signal = std::numeric_limits<int16>::min();
471 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
472 i != endpoints_.end(); ++i) {
473 if ((*i)->signal_strength() >= best_signal) {
474 best_signal = (*i)->signal_strength();
475 representative_endpoint = *i;
476 }
477 }
478 }
479
480 uint16 frequency;
481 int16 signal;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700482 string bssid;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800483 if (!representative_endpoint) {
484 frequency = 0;
485 signal = std::numeric_limits<int16>::min();
486 } else {
487 frequency = representative_endpoint->frequency();
488 signal = representative_endpoint->signal_strength();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700489 bssid = representative_endpoint->bssid_string();
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 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800500 SetStrength(SignalToStrength(signal));
501}
502
mukesh agrawal1a056262011-10-05 14:36:54 -0700503// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000504void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
505 Error *error) {
506 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700507}
508
509// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000510void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
511 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700512 unsigned int length = passphrase.length();
513 vector<uint8> passphrase_bytes;
514
515 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
516 if (length != IEEE_80211::kWPAHexLen &&
517 (length < IEEE_80211::kWPAAsciiMinLen ||
518 length > IEEE_80211::kWPAAsciiMaxLen)) {
519 error->Populate(Error::kInvalidPassphrase);
520 }
521 } else {
522 if (length < IEEE_80211::kWPAAsciiMinLen ||
523 length > IEEE_80211::kWPAAsciiMaxLen) {
524 error->Populate(Error::kInvalidPassphrase);
525 }
526 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000527}
mukesh agrawal1a056262011-10-05 14:36:54 -0700528
Thieu Lef4cbda92011-11-10 23:41:24 +0000529// static
530void WiFiService::ParseWEPPassphrase(const string &passphrase,
531 int *key_index,
532 std::vector<uint8> *password_bytes,
533 Error *error) {
534 unsigned int length = passphrase.length();
535 int key_index_local;
536 std::string password_text;
537 bool is_hex = false;
538
539 switch (length) {
540 case IEEE_80211::kWEP40AsciiLen:
541 case IEEE_80211::kWEP104AsciiLen:
542 key_index_local = 0;
543 password_text = passphrase;
544 break;
545 case IEEE_80211::kWEP40AsciiLen + 2:
546 case IEEE_80211::kWEP104AsciiLen + 2:
547 if (CheckWEPKeyIndex(passphrase, error)) {
548 base::StringToInt(passphrase.substr(0,1), &key_index_local);
549 password_text = passphrase.substr(2);
550 }
551 break;
552 case IEEE_80211::kWEP40HexLen:
553 case IEEE_80211::kWEP104HexLen:
554 if (CheckWEPIsHex(passphrase, error)) {
555 key_index_local = 0;
556 password_text = passphrase;
557 is_hex = true;
558 }
559 break;
560 case IEEE_80211::kWEP40HexLen + 2:
561 case IEEE_80211::kWEP104HexLen + 2:
562 if(CheckWEPKeyIndex(passphrase, error) &&
563 CheckWEPIsHex(passphrase.substr(2), error)) {
564 base::StringToInt(passphrase.substr(0,1), &key_index_local);
565 password_text = passphrase.substr(2);
566 is_hex = true;
567 } else if (CheckWEPPrefix(passphrase, error) &&
568 CheckWEPIsHex(passphrase.substr(2), error)) {
569 key_index_local = 0;
570 password_text = passphrase.substr(2);
571 is_hex = true;
572 }
573 break;
574 case IEEE_80211::kWEP40HexLen + 4:
575 case IEEE_80211::kWEP104HexLen + 4:
576 if (CheckWEPKeyIndex(passphrase, error) &&
577 CheckWEPPrefix(passphrase.substr(2), error) &&
578 CheckWEPIsHex(passphrase.substr(4), error)) {
579 base::StringToInt(passphrase.substr(0,1), &key_index_local);
580 password_text = passphrase.substr(4);
581 is_hex = true;
582 }
583 break;
584 default:
585 error->Populate(Error::kInvalidPassphrase);
586 break;
587 }
588
mukesh agrawal1a056262011-10-05 14:36:54 -0700589 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000590 if (key_index)
591 *key_index = key_index_local;
592 if (password_bytes) {
593 if (is_hex)
594 base::HexStringToBytes(password_text, password_bytes);
595 else
596 password_bytes->insert(password_bytes->end(),
597 password_text.begin(),
598 password_text.end());
599 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700600 }
601}
602
603// static
604bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
605 vector<uint8> passphrase_bytes;
606 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
607 return true;
608 } else {
609 error->Populate(Error::kInvalidPassphrase);
610 return false;
611 }
612}
613
614// static
615bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
616 if (StartsWithASCII(passphrase, "0:", false) ||
617 StartsWithASCII(passphrase, "1:", false) ||
618 StartsWithASCII(passphrase, "2:", false) ||
619 StartsWithASCII(passphrase, "3:", false)) {
620 return true;
621 } else {
622 error->Populate(Error::kInvalidPassphrase);
623 return false;
624 }
625}
626
627// static
628bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
629 if (StartsWithASCII(passphrase, "0x", false)) {
630 return true;
631 } else {
632 error->Populate(Error::kInvalidPassphrase);
633 return false;
634 }
635}
636
Paul Stewart6ab23a92011-11-09 17:17:47 -0800637// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800638string WiFiService::GetSecurityClass(const string &security) {
639 if (security == flimflam::kSecurityRsn ||
640 security == flimflam::kSecurityWpa) {
641 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700642 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800643 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700644 }
645}
646
Paul Stewarta41e38d2011-11-11 07:47:29 -0800647// static
648bool WiFiService::ParseStorageIdentifier(const string &storage_name,
649 string *address,
650 string *mode,
651 string *security) {
652 vector<string> wifi_parts;
653 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800654 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
655 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800656 return false;
657 }
658 *address = wifi_parts[1];
659 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800660 if (wifi_parts.size() == 5) {
661 *security = wifi_parts[4];
662 } else {
663 // Account for security type "802_1x" which got split up above.
664 *security = wifi_parts[4] + "_" + wifi_parts[5];
665 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800666 return true;
667}
668
mukesh agrawale1d90e92012-02-15 17:36:08 -0800669// static
670uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
671 int16 strength;
672 if (signal_dbm > 0) {
673 if (!logged_signal_warning) {
674 LOG(WARNING) << "Signal strength is suspiciously high. "
675 << "Assuming value " << signal_dbm << " is not in dBm.";
676 logged_signal_warning = true;
677 }
678 strength = signal_dbm;
679 } else {
680 strength = 120 + signal_dbm; // Call -20dBm "perfect".
681 }
682
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800683 if (strength > kStrengthMax) {
684 strength = kStrengthMax;
685 } else if (strength < kStrengthMin) {
686 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800687 }
688 return strength;
689}
690
Paul Stewart6ab23a92011-11-09 17:17:47 -0800691string WiFiService::GetGenericStorageIdentifier() const {
692 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
693}
694
Paul Stewartd08f4432011-11-04 07:48:20 -0700695string WiFiService::GetSpecificStorageIdentifier() const {
696 return GetStorageIdentifierForSecurity(security_);
697}
698
699string WiFiService::GetStorageIdentifierForSecurity(
700 const string &security) const {
701 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
702 flimflam::kTypeWifi,
703 wifi_->address().c_str(),
704 hex_ssid_.c_str(),
705 mode_.c_str(),
706 security.c_str()));
707}
708
Gary Moraine4aaf5e2012-04-05 14:37:32 -0700709void WiFiService::set_eap(const EapCredentials &new_eap) {
710 EapCredentials modified_eap = new_eap;
711
712 // An empty key_management field is invalid. Prevent it, if possible.
713 if (modified_eap.key_management.empty()) {
714 modified_eap.key_management = eap().key_management;
715 }
716 Service::set_eap(modified_eap);
Gaurav Shah10109f22011-11-11 20:16:22 -0800717 UpdateConnectable();
718}
719
Paul Stewart4357f4e2012-04-26 17:39:26 -0700720void WiFiService::OnProfileConfigured() {
721 if (profile() || !hidden_ssid()) {
722 return;
723 }
724 // This situation occurs when a hidden WiFi service created via GetService
725 // has been persisted to a profile in Manager::ConfigureService(). Now
726 // that configuration is saved, we must join the service with its profile,
727 // which will make this SSID eligible for directed probes during scans.
728 manager()->RegisterService(this);
729}
730
Gaurav Shah29d68882012-01-30 19:06:42 -0800731bool WiFiService::Is8021x() const {
732 if (security_ == flimflam::kSecurity8021x)
733 return true;
734
735 // Dynamic WEP + 802.1x.
736 if (security_ == flimflam::kSecurityWep &&
737 GetEAPKeyManagement() == "IEEE8021X")
738 return true;
739 return false;
740}
741
Gaurav Shah10109f22011-11-11 20:16:22 -0800742void WiFiService::Populate8021xProperties(
743 std::map<string, DBus::Variant> *params) {
Paul Stewartecf4cd12012-04-17 11:08:39 -0700744 string ca_cert = eap().ca_cert;
745 if (!eap().ca_cert_nss.empty()) {
746 vector<char> id(ssid_.begin(), ssid_.end());
747 FilePath certfile = nss_->GetDERCertfile(eap().ca_cert_nss, id);
748 if (certfile.empty()) {
749 LOG(ERROR) << "Unable to extract certificate: " << eap().ca_cert_nss;
750 } else {
751 ca_cert = certfile.value();
752 }
753 }
754
755
Gaurav Shah10109f22011-11-11 20:16:22 -0800756 typedef std::pair<const char *, const char *> KeyVal;
Paul Stewart20550982012-04-16 12:16:11 -0700757 KeyVal init_propertyvals[] = {
Gaurav Shah10109f22011-11-11 20:16:22 -0800758 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
759 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
760 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
761 eap().inner_eap.c_str()),
762 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
763 eap().anonymous_identity.c_str()),
764 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
765 eap().client_cert.c_str()),
766 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
767 eap().private_key.c_str()),
768 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
769 eap().private_key_password.c_str()),
Paul Stewartecf4cd12012-04-17 11:08:39 -0700770 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, ca_cert.c_str()),
Gaurav Shah10109f22011-11-11 20:16:22 -0800771 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
772 eap().password.c_str()),
773 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
774 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
775 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
776 eap().ca_cert_id.c_str()),
Paul Stewartbc6e7392012-05-24 07:07:48 -0700777 KeyVal(wpa_supplicant::kNetworkPropertyEapSubjectMatch,
778 eap().subject_match.c_str())
Gaurav Shah10109f22011-11-11 20:16:22 -0800779 };
780
Paul Stewart20550982012-04-16 12:16:11 -0700781 vector<KeyVal> propertyvals(init_propertyvals,
782 init_propertyvals + arraysize(init_propertyvals));
783 if (eap().use_system_cas) {
784 propertyvals.push_back(KeyVal(
785 wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath));
Paul Stewartecf4cd12012-04-17 11:08:39 -0700786 } else if (ca_cert.empty()) {
Paul Stewart20550982012-04-16 12:16:11 -0700787 LOG(WARNING) << __func__
788 << ": No certificate authorities are configured."
789 << " Server certificates will be accepted"
790 << " unconditionally.";
791 }
792
793 if (!eap().cert_id.empty() || !eap().key_id.empty() ||
794 !eap().ca_cert_id.empty()) {
795 propertyvals.push_back(KeyVal(
796 wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()));
797 propertyvals.push_back(KeyVal(
798 wpa_supplicant::kNetworkPropertyEngineId,
799 wpa_supplicant::kEnginePKCS11));
800 // We can't use the propertyvals vector for this since this argument
801 // is a uint32, not a string.
802 (*params)[wpa_supplicant::kNetworkPropertyEngine].writer().
803 append_uint32(wpa_supplicant::kDefaultEngine);
804 }
805
806 vector<KeyVal>::iterator it;
807 for (it = propertyvals.begin(); it != propertyvals.end(); ++it) {
808 if (strlen((*it).second) > 0) {
809 (*params)[(*it).first].writer().append_string((*it).second);
Gaurav Shah10109f22011-11-11 20:16:22 -0800810 }
811 }
812}
813
mukesh agrawalb54601c2011-06-07 17:39:22 -0700814} // namespace shill