blob: e61e0c16ebfd68842e051ec631ff4016fcda978b [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"
Thieu Le48e6d6d2011-12-06 00:40:27 +000024#include "shill/metrics.h"
Paul Stewartecf4cd12012-04-17 11:08:39 -070025#include "shill/nss.h"
Thieu Lef7709452011-11-15 01:13:19 +000026#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070027#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070028#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070029#include "shill/wifi_endpoint.h"
30#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070031
mukesh agrawal261daca2011-12-02 18:56:56 +000032using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070033using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070034using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070035
36namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070037
mukesh agrawalbf14e942012-03-02 14:36:34 -080038const char WiFiService::kAutoConnBusy[] = "busy";
39const char WiFiService::kAutoConnNoEndpoint[] = "no endpoints";
40
Paul Stewartd08f4432011-11-04 07:48:20 -070041const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080042const char WiFiService::kStorageMode[] = "WiFi.Mode";
43const char WiFiService::kStoragePassphrase[] = "Passphrase";
44const char WiFiService::kStorageSecurity[] = "WiFi.Security";
45const char WiFiService::kStorageSSID[] = "SSID";
mukesh agrawale1d90e92012-02-15 17:36:08 -080046bool WiFiService::logged_signal_warning = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070047
mukesh agrawalb54601c2011-06-07 17:39:22 -070048WiFiService::WiFiService(ControlInterface *control_interface,
49 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080050 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070051 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070052 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080053 const vector<uint8_t> &ssid,
54 const string &mode,
55 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080056 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080057 : Service(control_interface, dispatcher, metrics, manager,
58 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070059 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070060 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070061 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080062 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000063 frequency_(0),
Paul Stewart20088d82012-02-16 06:58:55 -080064 physical_mode_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070065 wifi_(device),
Paul Stewartecf4cd12012-04-17 11:08:39 -070066 ssid_(ssid),
67 nss_(NSS::GetInstance()) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070068 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070069 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080070 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
71 &WiFiService::SetPassphrase,
72 &WiFiService::ClearPassphrase,
73 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070074 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
75 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070076
Paul Stewartac4ac002011-08-26 12:04:26 -070077 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
78 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
79 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
80 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070081
mukesh agrawald835b202011-10-07 15:26:47 -070082 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
83 string ssid_string(
84 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080085 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070086 // WifiHexSsid property should only be present if Name property
87 // has been munged.
88 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
89 }
90 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070091
mukesh agrawal6e277772011-09-29 15:04:23 -070092 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
93 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080094 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080095 // Passphrases are not mandatory for 802.1X.
96 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070097 } else if (security_ == flimflam::kSecurityPsk) {
98 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070099 } else if (security_ == flimflam::kSecurityRsn) {
100 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700101 } else if (security_ == flimflam::kSecurityWpa) {
102 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700103 } else if (security_ == flimflam::kSecurityWep) {
104 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700105 } else if (security_ == flimflam::kSecurityNone) {
106 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700107 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800108 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700109 }
110
Paul Stewartd08f4432011-11-04 07:48:20 -0700111 // Until we know better (at Profile load time), use the generic name.
112 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000113 UpdateConnectable();
Paul Stewartcb59fed2012-03-21 21:14:46 -0700114
115 IgnoreParameterForConfigure(flimflam::kModeProperty);
116 IgnoreParameterForConfigure(flimflam::kSSIDProperty);
117 IgnoreParameterForConfigure(flimflam::kSecurityProperty);
118 IgnoreParameterForConfigure(flimflam::kPassphraseProperty);
119 IgnoreParameterForConfigure(flimflam::kWifiHiddenSsid);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700120}
121
122WiFiService::~WiFiService() {
123 LOG(INFO) << __func__;
124}
125
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000126void WiFiService::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800127 const char *reason;
128 if (IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000129 // Execute immediately, for two reasons:
130 //
131 // 1. We need IsAutoConnectable to return the correct value for
132 // other WiFiServices, and that depends on WiFi's state.
133 //
134 // 2. We should probably limit the extent to which we queue up
135 // actions (such as AutoConnect) which depend on current state.
136 // If we queued AutoConnects, we could build a long queue of
137 // useless work (one AutoConnect per Service), which blocks
138 // more timely work.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500139 Connect(NULL);
mukesh agrawal592516d2012-01-12 14:01:00 -0800140 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800141 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
142 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000143 }
144}
145
Paul Stewart22aa71b2011-09-16 12:15:11 -0700146bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
147 return wifi_->TechnologyIs(type);
148}
149
mukesh agrawalbf14e942012-03-02 14:36:34 -0800150bool WiFiService::IsAutoConnectable(const char **reason) const {
151 if (!Service::IsAutoConnectable(reason)) {
152 return false;
153 }
154
155 // Only auto-connect to Services which have visible Endpoints.
156 // (Needed because hidden Services may remain registered with
157 // Manager even without visible Endpoints.)
158 if (!HasEndpoints()) {
159 *reason = kAutoConnNoEndpoint;
160 return false;
161 }
162
163 // Do not preempt an existing connection (whether pending, or
164 // connected, and whether to this service, or another).
165 if (!wifi_->IsIdle()) {
166 *reason = kAutoConnBusy;
167 return false;
168 }
169
170 return true;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000171}
172
173bool WiFiService::IsConnecting() const {
174 // WiFi does not move us into the associating state until it gets
175 // feedback from wpa_supplicant. So, to answer whether or
176 // not we're connecting, we consult with |wifi_|.
177 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800178}
179
mukesh agrawale1d90e92012-02-15 17:36:08 -0800180void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000181 DCHECK(endpoint->ssid() == ssid());
182 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800183 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000184}
185
mukesh agrawale1d90e92012-02-15 17:36:08 -0800186void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000187 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
188 DCHECK(i != endpoints_.end());
189 if (i == endpoints_.end()) {
190 LOG(WARNING) << "In " << __func__ << "(): "
191 << "ignorning non-existent endpoint "
192 << endpoint->bssid_string();
193 return;
194 }
195 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800196 if (current_endpoint_ == endpoint) {
197 current_endpoint_ = NULL;
198 }
199 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000200}
201
mukesh agrawale1d90e92012-02-15 17:36:08 -0800202void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
203 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
204 current_endpoint_ = endpoint;
205 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000206}
207
mukesh agrawalb20776f2012-02-10 16:00:36 -0800208void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
209 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800210 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800211}
212
Chris Masone6515aab2011-10-12 16:19:09 -0700213string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700214 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700215}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700216
mukesh agrawal1a056262011-10-05 14:36:54 -0700217void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
218 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000219 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700220 } else if (security_ == flimflam::kSecurityPsk ||
221 security_ == flimflam::kSecurityWpa ||
222 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000223 ValidateWPAPassphrase(passphrase, error);
224 } else {
225 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700226 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000227
Paul Stewart2706aaf2011-12-14 16:44:04 -0800228 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000229 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800230 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000231
232 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700233}
234
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800235// ClearPassphrase is separate from SetPassphrase, because the default
236// value for |passphrase_| would not pass validation.
237void WiFiService::ClearPassphrase(Error */*error*/) {
238 passphrase_.clear();
239 UpdateConnectable();
240}
241
Paul Stewartd08f4432011-11-04 07:48:20 -0700242bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
243 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
244 storage->ContainsGroup(GetSpecificStorageIdentifier());
245}
246
Paul Stewarta41e38d2011-11-11 07:47:29 -0800247bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800248 // WiFi Services should be displayed only if they are in range (have
249 // endpoints that have shown up in a scan) or if the service is actively
250 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000251 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800252}
253
Paul Stewartd08f4432011-11-04 07:48:20 -0700254bool WiFiService::Load(StoreInterface *storage) {
255 // First find out which storage identifier is available in priority order
256 // of specific, generic.
257 string id = GetSpecificStorageIdentifier();
258 if (!storage->ContainsGroup(id)) {
259 id = GetGenericStorageIdentifier();
260 if (!storage->ContainsGroup(id)) {
261 LOG(WARNING) << "Service is not available in the persistent store: "
262 << id;
263 return false;
264 }
265 }
266
267 // Set our storage identifier to match the storage name in the Profile.
268 storage_identifier_ = id;
269
270 // Load properties common to all Services.
271 if (!Service::Load(storage)) {
272 return false;
273 }
274
275 // Load properties specific to WiFi services.
276 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000277
Paul Stewart2706aaf2011-12-14 16:44:04 -0800278 // NB: mode, security and ssid parameters are never read in from
279 // Load() as they are provided from the scan.
280
281 string passphrase;
282 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
283 Error error;
284 SetPassphrase(passphrase, &error);
285 if (!error.IsSuccess()) {
286 LOG(ERROR) << "Passphrase could not be set: "
287 << Error::GetName(error.type());
288 }
289 }
290
Paul Stewartd08f4432011-11-04 07:48:20 -0700291 return true;
292}
293
294bool WiFiService::Save(StoreInterface *storage) {
295 // Save properties common to all Services.
296 if (!Service::Save(storage)) {
297 return false;
298 }
299
300 // Save properties specific to WiFi services.
301 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800302 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
303 storage->SetString(id, kStorageMode, mode_);
304 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
305 storage->SetString(id, kStorageSecurity, security_);
306 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000307
308 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700309 return true;
310}
311
Paul Stewart65512e12012-03-26 18:01:08 -0700312bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800313 Service::Unload();
314 hidden_ssid_ = false;
315 passphrase_ = "";
316 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800317 if (security_ == flimflam::kSecurity8021x) {
318 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
319 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
320 // Make sure that these are cleared when credentials for networks
321 // of this type goes away.
322 //
323 // When wpa_supplicant gains the ability, do this credential
324 // clearing on a per-service basis. Also do this whenever the credentials
325 // for a service changes. crosbug.com/25670
326 wifi_->ClearCachedCredentials();
327 }
Paul Stewart65512e12012-03-26 18:01:08 -0700328 return !IsVisible();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800329}
330
Paul Stewart6ab23a92011-11-09 17:17:47 -0800331bool WiFiService::IsSecurityMatch(const string &security) const {
332 return GetSecurityClass(security) == GetSecurityClass(security_);
333}
334
Thieu Le48e6d6d2011-12-06 00:40:27 +0000335void WiFiService::InitializeCustomMetrics() const {
336 string histogram = metrics()->GetFullMetricName(
337 Metrics::kMetricTimeToJoinMilliseconds,
338 technology());
339 metrics()->AddServiceStateTransitionTimer(this,
340 histogram,
341 Service::kStateAssociating,
342 Service::kStateConfiguring);
343}
344
Thieu Leb84ba342012-03-02 15:15:19 -0800345void WiFiService::SendPostReadyStateMetrics(
346 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000347 metrics()->SendEnumToUMA(
348 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
349 technology()),
350 Metrics::WiFiFrequencyToChannel(frequency_),
351 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000352
353 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
354 metrics()->SendEnumToUMA(
355 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
356 technology()),
357 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
358 Metrics::kWiFiNetworkPhyModeMax);
359
360 Metrics::WiFiSecurity security_uma =
361 Metrics::WiFiSecurityStringToEnum(security_);
362 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
363 metrics()->SendEnumToUMA(
364 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
365 technology()),
366 security_uma,
367 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800368
369 if (time_resume_to_ready_milliseconds > 0) {
370 metrics()->SendToUMA(
371 metrics()->GetFullMetricName(
372 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
373 time_resume_to_ready_milliseconds,
374 Metrics::kTimerHistogramMillisecondsMin,
375 Metrics::kTimerHistogramMillisecondsMax,
376 Metrics::kTimerHistogramNumBuckets);
377 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000378}
379
mukesh agrawal32399322011-09-01 10:53:43 -0700380// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800381void WiFiService::HelpRegisterWriteOnlyDerivedString(
382 const string &name,
383 void(WiFiService::*set)(const string &, Error *),
384 void(WiFiService::*clear)(Error *),
385 const string *default_value) {
386 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000387 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800388 StringAccessor(
389 new CustomWriteOnlyAccessor<WiFiService, string>(
390 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000391}
392
Eric Shienbrood9a245532012-03-07 14:20:39 -0500393void WiFiService::Connect(Error */*error*/) {
394 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal6e277772011-09-29 15:04:23 -0700395 std::map<string, DBus::Variant> params;
396 DBus::MessageIter writer;
397
398 params[wpa_supplicant::kNetworkPropertyMode].writer().
399 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
400
Gaurav Shah29d68882012-01-30 19:06:42 -0800401 if (Is8021x()) {
402 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800403 if (GetEAPKeyManagement().empty())
404 SetEAPKeyManagement("WPA-EAP");
405 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700406 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800407 const string psk_proto = StringPrintf("%s %s",
408 wpa_supplicant::kSecurityModeWPA,
409 wpa_supplicant::kSecurityModeRSN);
410 params[wpa_supplicant::kPropertySecurityProtocol].writer().
411 append_string(psk_proto.c_str());
412 params[wpa_supplicant::kPropertyPreSharedKey].writer().
413 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700414 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700415 params[wpa_supplicant::kPropertySecurityProtocol].writer().
416 append_string(wpa_supplicant::kSecurityModeRSN);
417 params[wpa_supplicant::kPropertyPreSharedKey].writer().
418 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700419 } else if (security_ == flimflam::kSecurityWpa) {
420 params[wpa_supplicant::kPropertySecurityProtocol].writer().
421 append_string(wpa_supplicant::kSecurityModeWPA);
422 params[wpa_supplicant::kPropertyPreSharedKey].writer().
423 append_string(passphrase_.c_str());
424 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000425 params[wpa_supplicant::kPropertyAuthAlg].writer().
426 append_string(wpa_supplicant::kSecurityAuthAlg);
427 Error error;
428 int key_index;
429 std::vector<uint8> password_bytes;
430 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
431 writer = params[wpa_supplicant::kPropertyWEPKey +
432 base::IntToString(key_index)].writer();
433 writer << password_bytes;
434 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
435 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700436 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800437 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700438 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800439 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700440 }
441
Gaurav Shah10109f22011-11-11 20:16:22 -0800442 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700443 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800444
445 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700446 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
447 writer << ssid_;
448
449 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700450}
451
Eric Shienbrood9a245532012-03-07 14:20:39 -0500452void WiFiService::Disconnect(Error *error) {
453 LOG(INFO) << __func__;
454 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000455 wifi_->DisconnectFrom(this);
456}
457
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800458string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700459 return wifi_->GetRpcIdentifier();
460}
461
mukesh agrawal29c13a12011-11-24 00:09:19 +0000462void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800463 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000464 if (security_ == flimflam::kSecurityNone) {
465 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800466 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800467 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800468 } else if (Is8021x()) {
469 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000470 } else if (security_ == flimflam::kSecurityWep ||
471 security_ == flimflam::kSecurityWpa ||
472 security_ == flimflam::kSecurityPsk ||
473 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800474 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800475 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000476 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800477 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000478}
479
mukesh agrawale1d90e92012-02-15 17:36:08 -0800480void WiFiService::UpdateFromEndpoints() {
481 const WiFiEndpoint *representative_endpoint = NULL;
482
483 if (current_endpoint_) {
484 // TODO: Copy BSSID here (crosbug.com/22377).
485 representative_endpoint = current_endpoint_;
486 } else {
487 int16 best_signal = std::numeric_limits<int16>::min();
488 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
489 i != endpoints_.end(); ++i) {
490 if ((*i)->signal_strength() >= best_signal) {
491 best_signal = (*i)->signal_strength();
492 representative_endpoint = *i;
493 }
494 }
495 }
496
497 uint16 frequency;
498 int16 signal;
499 if (!representative_endpoint) {
500 frequency = 0;
501 signal = std::numeric_limits<int16>::min();
502 } else {
503 frequency = representative_endpoint->frequency();
504 signal = representative_endpoint->signal_strength();
505 }
506
507 if (frequency_ != frequency) {
508 frequency_ = frequency;
509 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
510 }
511 SetStrength(SignalToStrength(signal));
512}
513
mukesh agrawal1a056262011-10-05 14:36:54 -0700514// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000515void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
516 Error *error) {
517 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700518}
519
520// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000521void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
522 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700523 unsigned int length = passphrase.length();
524 vector<uint8> passphrase_bytes;
525
526 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
527 if (length != IEEE_80211::kWPAHexLen &&
528 (length < IEEE_80211::kWPAAsciiMinLen ||
529 length > IEEE_80211::kWPAAsciiMaxLen)) {
530 error->Populate(Error::kInvalidPassphrase);
531 }
532 } else {
533 if (length < IEEE_80211::kWPAAsciiMinLen ||
534 length > IEEE_80211::kWPAAsciiMaxLen) {
535 error->Populate(Error::kInvalidPassphrase);
536 }
537 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000538}
mukesh agrawal1a056262011-10-05 14:36:54 -0700539
Thieu Lef4cbda92011-11-10 23:41:24 +0000540// static
541void WiFiService::ParseWEPPassphrase(const string &passphrase,
542 int *key_index,
543 std::vector<uint8> *password_bytes,
544 Error *error) {
545 unsigned int length = passphrase.length();
546 int key_index_local;
547 std::string password_text;
548 bool is_hex = false;
549
550 switch (length) {
551 case IEEE_80211::kWEP40AsciiLen:
552 case IEEE_80211::kWEP104AsciiLen:
553 key_index_local = 0;
554 password_text = passphrase;
555 break;
556 case IEEE_80211::kWEP40AsciiLen + 2:
557 case IEEE_80211::kWEP104AsciiLen + 2:
558 if (CheckWEPKeyIndex(passphrase, error)) {
559 base::StringToInt(passphrase.substr(0,1), &key_index_local);
560 password_text = passphrase.substr(2);
561 }
562 break;
563 case IEEE_80211::kWEP40HexLen:
564 case IEEE_80211::kWEP104HexLen:
565 if (CheckWEPIsHex(passphrase, error)) {
566 key_index_local = 0;
567 password_text = passphrase;
568 is_hex = true;
569 }
570 break;
571 case IEEE_80211::kWEP40HexLen + 2:
572 case IEEE_80211::kWEP104HexLen + 2:
573 if(CheckWEPKeyIndex(passphrase, error) &&
574 CheckWEPIsHex(passphrase.substr(2), error)) {
575 base::StringToInt(passphrase.substr(0,1), &key_index_local);
576 password_text = passphrase.substr(2);
577 is_hex = true;
578 } else if (CheckWEPPrefix(passphrase, error) &&
579 CheckWEPIsHex(passphrase.substr(2), error)) {
580 key_index_local = 0;
581 password_text = passphrase.substr(2);
582 is_hex = true;
583 }
584 break;
585 case IEEE_80211::kWEP40HexLen + 4:
586 case IEEE_80211::kWEP104HexLen + 4:
587 if (CheckWEPKeyIndex(passphrase, error) &&
588 CheckWEPPrefix(passphrase.substr(2), error) &&
589 CheckWEPIsHex(passphrase.substr(4), error)) {
590 base::StringToInt(passphrase.substr(0,1), &key_index_local);
591 password_text = passphrase.substr(4);
592 is_hex = true;
593 }
594 break;
595 default:
596 error->Populate(Error::kInvalidPassphrase);
597 break;
598 }
599
mukesh agrawal1a056262011-10-05 14:36:54 -0700600 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000601 if (key_index)
602 *key_index = key_index_local;
603 if (password_bytes) {
604 if (is_hex)
605 base::HexStringToBytes(password_text, password_bytes);
606 else
607 password_bytes->insert(password_bytes->end(),
608 password_text.begin(),
609 password_text.end());
610 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700611 }
612}
613
614// static
615bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
616 vector<uint8> passphrase_bytes;
617 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
618 return true;
619 } else {
620 error->Populate(Error::kInvalidPassphrase);
621 return false;
622 }
623}
624
625// static
626bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
627 if (StartsWithASCII(passphrase, "0:", false) ||
628 StartsWithASCII(passphrase, "1:", false) ||
629 StartsWithASCII(passphrase, "2:", false) ||
630 StartsWithASCII(passphrase, "3:", false)) {
631 return true;
632 } else {
633 error->Populate(Error::kInvalidPassphrase);
634 return false;
635 }
636}
637
638// static
639bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
640 if (StartsWithASCII(passphrase, "0x", false)) {
641 return true;
642 } else {
643 error->Populate(Error::kInvalidPassphrase);
644 return false;
645 }
646}
647
Paul Stewart6ab23a92011-11-09 17:17:47 -0800648// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800649string WiFiService::GetSecurityClass(const string &security) {
650 if (security == flimflam::kSecurityRsn ||
651 security == flimflam::kSecurityWpa) {
652 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700653 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800654 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700655 }
656}
657
Paul Stewarta41e38d2011-11-11 07:47:29 -0800658// static
659bool WiFiService::ParseStorageIdentifier(const string &storage_name,
660 string *address,
661 string *mode,
662 string *security) {
663 vector<string> wifi_parts;
664 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800665 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
666 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800667 return false;
668 }
669 *address = wifi_parts[1];
670 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800671 if (wifi_parts.size() == 5) {
672 *security = wifi_parts[4];
673 } else {
674 // Account for security type "802_1x" which got split up above.
675 *security = wifi_parts[4] + "_" + wifi_parts[5];
676 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800677 return true;
678}
679
mukesh agrawale1d90e92012-02-15 17:36:08 -0800680// static
681uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
682 int16 strength;
683 if (signal_dbm > 0) {
684 if (!logged_signal_warning) {
685 LOG(WARNING) << "Signal strength is suspiciously high. "
686 << "Assuming value " << signal_dbm << " is not in dBm.";
687 logged_signal_warning = true;
688 }
689 strength = signal_dbm;
690 } else {
691 strength = 120 + signal_dbm; // Call -20dBm "perfect".
692 }
693
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800694 if (strength > kStrengthMax) {
695 strength = kStrengthMax;
696 } else if (strength < kStrengthMin) {
697 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800698 }
699 return strength;
700}
701
Paul Stewart6ab23a92011-11-09 17:17:47 -0800702string WiFiService::GetGenericStorageIdentifier() const {
703 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
704}
705
Paul Stewartd08f4432011-11-04 07:48:20 -0700706string WiFiService::GetSpecificStorageIdentifier() const {
707 return GetStorageIdentifierForSecurity(security_);
708}
709
710string WiFiService::GetStorageIdentifierForSecurity(
711 const string &security) const {
712 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
713 flimflam::kTypeWifi,
714 wifi_->address().c_str(),
715 hex_ssid_.c_str(),
716 mode_.c_str(),
717 security.c_str()));
718}
719
Gary Moraine4aaf5e2012-04-05 14:37:32 -0700720void WiFiService::set_eap(const EapCredentials &new_eap) {
721 EapCredentials modified_eap = new_eap;
722
723 // An empty key_management field is invalid. Prevent it, if possible.
724 if (modified_eap.key_management.empty()) {
725 modified_eap.key_management = eap().key_management;
726 }
727 Service::set_eap(modified_eap);
Gaurav Shah10109f22011-11-11 20:16:22 -0800728 UpdateConnectable();
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()),
Gaurav Shah10109f22011-11-11 20:16:22 -0800777 };
778
Paul Stewart20550982012-04-16 12:16:11 -0700779 vector<KeyVal> propertyvals(init_propertyvals,
780 init_propertyvals + arraysize(init_propertyvals));
781 if (eap().use_system_cas) {
782 propertyvals.push_back(KeyVal(
783 wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath));
Paul Stewartecf4cd12012-04-17 11:08:39 -0700784 } else if (ca_cert.empty()) {
Paul Stewart20550982012-04-16 12:16:11 -0700785 LOG(WARNING) << __func__
786 << ": No certificate authorities are configured."
787 << " Server certificates will be accepted"
788 << " unconditionally.";
789 }
790
791 if (!eap().cert_id.empty() || !eap().key_id.empty() ||
792 !eap().ca_cert_id.empty()) {
793 propertyvals.push_back(KeyVal(
794 wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()));
795 propertyvals.push_back(KeyVal(
796 wpa_supplicant::kNetworkPropertyEngineId,
797 wpa_supplicant::kEnginePKCS11));
798 // We can't use the propertyvals vector for this since this argument
799 // is a uint32, not a string.
800 (*params)[wpa_supplicant::kNetworkPropertyEngine].writer().
801 append_uint32(wpa_supplicant::kDefaultEngine);
802 }
803
804 vector<KeyVal>::iterator it;
805 for (it = propertyvals.begin(); it != propertyvals.end(); ++it) {
806 if (strlen((*it).second) > 0) {
807 (*params)[(*it).first].writer().append_string((*it).second);
Gaurav Shah10109f22011-11-11 20:16:22 -0800808 }
809 }
810}
811
mukesh agrawalb54601c2011-06-07 17:39:22 -0700812} // namespace shill