blob: a60256358e214f2e323e0dcc70fc84027928c695 [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_);
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);
122 IgnoreParameterForConfigure(flimflam::kPassphraseProperty);
123 IgnoreParameterForConfigure(flimflam::kWifiHiddenSsid);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700124}
125
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200126WiFiService::~WiFiService() {}
mukesh agrawalb54601c2011-06-07 17:39:22 -0700127
mukesh agrawalbf14e942012-03-02 14:36:34 -0800128bool WiFiService::IsAutoConnectable(const char **reason) const {
129 if (!Service::IsAutoConnectable(reason)) {
130 return false;
131 }
132
133 // Only auto-connect to Services which have visible Endpoints.
134 // (Needed because hidden Services may remain registered with
135 // Manager even without visible Endpoints.)
136 if (!HasEndpoints()) {
137 *reason = kAutoConnNoEndpoint;
138 return false;
139 }
140
141 // Do not preempt an existing connection (whether pending, or
142 // connected, and whether to this service, or another).
143 if (!wifi_->IsIdle()) {
144 *reason = kAutoConnBusy;
145 return false;
146 }
147
148 return true;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000149}
150
151bool WiFiService::IsConnecting() const {
152 // WiFi does not move us into the associating state until it gets
153 // feedback from wpa_supplicant. So, to answer whether or
154 // not we're connecting, we consult with |wifi_|.
155 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800156}
157
Darin Petkov4a66cc52012-06-15 10:08:29 +0200158void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000159 DCHECK(endpoint->ssid() == ssid());
160 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800161 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000162}
163
Darin Petkov4a66cc52012-06-15 10:08:29 +0200164void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000165 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
166 DCHECK(i != endpoints_.end());
167 if (i == endpoints_.end()) {
168 LOG(WARNING) << "In " << __func__ << "(): "
169 << "ignorning non-existent endpoint "
170 << endpoint->bssid_string();
171 return;
172 }
173 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800174 if (current_endpoint_ == endpoint) {
175 current_endpoint_ = NULL;
176 }
177 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000178}
179
mukesh agrawale1d90e92012-02-15 17:36:08 -0800180void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
181 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
182 current_endpoint_ = endpoint;
183 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000184}
185
mukesh agrawalb20776f2012-02-10 16:00:36 -0800186void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
187 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800188 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800189}
190
Chris Masone6515aab2011-10-12 16:19:09 -0700191string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700192 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700193}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700194
mukesh agrawal1a056262011-10-05 14:36:54 -0700195void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
196 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000197 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700198 } else if (security_ == flimflam::kSecurityPsk ||
199 security_ == flimflam::kSecurityWpa ||
200 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000201 ValidateWPAPassphrase(passphrase, error);
202 } else {
203 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700204 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000205
Paul Stewart2706aaf2011-12-14 16:44:04 -0800206 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000207 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800208 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000209
210 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700211}
212
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800213// ClearPassphrase is separate from SetPassphrase, because the default
214// value for |passphrase_| would not pass validation.
215void WiFiService::ClearPassphrase(Error */*error*/) {
216 passphrase_.clear();
217 UpdateConnectable();
218}
219
Paul Stewartd08f4432011-11-04 07:48:20 -0700220bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
221 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
222 storage->ContainsGroup(GetSpecificStorageIdentifier());
223}
224
Paul Stewarta41e38d2011-11-11 07:47:29 -0800225bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800226 // WiFi Services should be displayed only if they are in range (have
227 // endpoints that have shown up in a scan) or if the service is actively
228 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000229 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800230}
231
Paul Stewartd08f4432011-11-04 07:48:20 -0700232bool WiFiService::Load(StoreInterface *storage) {
233 // First find out which storage identifier is available in priority order
234 // of specific, generic.
235 string id = GetSpecificStorageIdentifier();
236 if (!storage->ContainsGroup(id)) {
237 id = GetGenericStorageIdentifier();
238 if (!storage->ContainsGroup(id)) {
239 LOG(WARNING) << "Service is not available in the persistent store: "
240 << id;
241 return false;
242 }
243 }
244
245 // Set our storage identifier to match the storage name in the Profile.
246 storage_identifier_ = id;
247
248 // Load properties common to all Services.
249 if (!Service::Load(storage)) {
250 return false;
251 }
252
253 // Load properties specific to WiFi services.
254 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000255
Paul Stewart2706aaf2011-12-14 16:44:04 -0800256 // NB: mode, security and ssid parameters are never read in from
257 // Load() as they are provided from the scan.
258
259 string passphrase;
260 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
261 Error error;
262 SetPassphrase(passphrase, &error);
263 if (!error.IsSuccess()) {
264 LOG(ERROR) << "Passphrase could not be set: "
265 << Error::GetName(error.type());
266 }
267 }
268
Paul Stewartd08f4432011-11-04 07:48:20 -0700269 return true;
270}
271
272bool WiFiService::Save(StoreInterface *storage) {
273 // Save properties common to all Services.
274 if (!Service::Save(storage)) {
275 return false;
276 }
277
278 // Save properties specific to WiFi services.
279 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800280 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
281 storage->SetString(id, kStorageMode, mode_);
282 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
283 storage->SetString(id, kStorageSecurity, security_);
284 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000285
Paul Stewartd08f4432011-11-04 07:48:20 -0700286 return true;
287}
288
Paul Stewart65512e12012-03-26 18:01:08 -0700289bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800290 Service::Unload();
291 hidden_ssid_ = false;
Wade Guthrie005bd342012-05-02 09:37:07 -0700292 Error unused_error;
293 ClearPassphrase(&unused_error);
Paul Stewart66c86002012-01-30 18:00:52 -0800294 if (security_ == flimflam::kSecurity8021x) {
295 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
296 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
297 // Make sure that these are cleared when credentials for networks
298 // of this type goes away.
299 //
300 // When wpa_supplicant gains the ability, do this credential
301 // clearing on a per-service basis. Also do this whenever the credentials
302 // for a service changes. crosbug.com/25670
303 wifi_->ClearCachedCredentials();
304 }
Paul Stewart65512e12012-03-26 18:01:08 -0700305 return !IsVisible();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800306}
307
Paul Stewart6ab23a92011-11-09 17:17:47 -0800308bool WiFiService::IsSecurityMatch(const string &security) const {
309 return GetSecurityClass(security) == GetSecurityClass(security_);
310}
311
Thieu Le48e6d6d2011-12-06 00:40:27 +0000312void WiFiService::InitializeCustomMetrics() const {
313 string histogram = metrics()->GetFullMetricName(
314 Metrics::kMetricTimeToJoinMilliseconds,
315 technology());
316 metrics()->AddServiceStateTransitionTimer(this,
317 histogram,
318 Service::kStateAssociating,
319 Service::kStateConfiguring);
320}
321
Thieu Leb84ba342012-03-02 15:15:19 -0800322void WiFiService::SendPostReadyStateMetrics(
323 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000324 metrics()->SendEnumToUMA(
325 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
326 technology()),
327 Metrics::WiFiFrequencyToChannel(frequency_),
328 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000329
330 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
331 metrics()->SendEnumToUMA(
332 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
333 technology()),
334 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
335 Metrics::kWiFiNetworkPhyModeMax);
336
337 Metrics::WiFiSecurity security_uma =
338 Metrics::WiFiSecurityStringToEnum(security_);
339 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
340 metrics()->SendEnumToUMA(
341 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
342 technology()),
343 security_uma,
344 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800345
346 if (time_resume_to_ready_milliseconds > 0) {
347 metrics()->SendToUMA(
348 metrics()->GetFullMetricName(
349 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
350 time_resume_to_ready_milliseconds,
351 Metrics::kTimerHistogramMillisecondsMin,
352 Metrics::kTimerHistogramMillisecondsMax,
353 Metrics::kTimerHistogramNumBuckets);
354 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000355}
356
mukesh agrawal32399322011-09-01 10:53:43 -0700357// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800358void WiFiService::HelpRegisterWriteOnlyDerivedString(
359 const string &name,
360 void(WiFiService::*set)(const string &, Error *),
361 void(WiFiService::*clear)(Error *),
362 const string *default_value) {
363 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000364 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800365 StringAccessor(
366 new CustomWriteOnlyAccessor<WiFiService, string>(
367 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000368}
369
Wade Guthrie005bd342012-05-02 09:37:07 -0700370void WiFiService::Connect(Error *error) {
371 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name();
mukesh agrawal6e277772011-09-29 15:04:23 -0700372 std::map<string, DBus::Variant> params;
373 DBus::MessageIter writer;
374
Wade Guthrie005bd342012-05-02 09:37:07 -0700375 if (!connectable()) {
376 LOG(ERROR) << "Can't connect. Service " << friendly_name()
377 << " is not connectable";
378 Error::PopulateAndLog(error, Error::kOperationFailed,
379 Error::GetDefaultMessage(Error::kOperationFailed));
380 return;
381 }
382
mukesh agrawal6e277772011-09-29 15:04:23 -0700383 params[wpa_supplicant::kNetworkPropertyMode].writer().
384 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
385
Gaurav Shah29d68882012-01-30 19:06:42 -0800386 if (Is8021x()) {
387 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800388 if (GetEAPKeyManagement().empty())
389 SetEAPKeyManagement("WPA-EAP");
390 Populate8021xProperties(&params);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700391 ClearEAPCertification();
mukesh agrawal6e277772011-09-29 15:04:23 -0700392 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800393 const string psk_proto = StringPrintf("%s %s",
394 wpa_supplicant::kSecurityModeWPA,
395 wpa_supplicant::kSecurityModeRSN);
396 params[wpa_supplicant::kPropertySecurityProtocol].writer().
397 append_string(psk_proto.c_str());
398 params[wpa_supplicant::kPropertyPreSharedKey].writer().
399 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700400 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700401 params[wpa_supplicant::kPropertySecurityProtocol].writer().
402 append_string(wpa_supplicant::kSecurityModeRSN);
403 params[wpa_supplicant::kPropertyPreSharedKey].writer().
404 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700405 } else if (security_ == flimflam::kSecurityWpa) {
406 params[wpa_supplicant::kPropertySecurityProtocol].writer().
407 append_string(wpa_supplicant::kSecurityModeWPA);
408 params[wpa_supplicant::kPropertyPreSharedKey].writer().
409 append_string(passphrase_.c_str());
410 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000411 params[wpa_supplicant::kPropertyAuthAlg].writer().
412 append_string(wpa_supplicant::kSecurityAuthAlg);
413 Error error;
414 int key_index;
415 std::vector<uint8> password_bytes;
416 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
417 writer = params[wpa_supplicant::kPropertyWEPKey +
418 base::IntToString(key_index)].writer();
419 writer << password_bytes;
420 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
421 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700422 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800423 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700424 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800425 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700426 }
427
Gaurav Shah10109f22011-11-11 20:16:22 -0800428 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700429 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800430
431 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700432 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
433 writer << ssid_;
434
435 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700436}
437
Eric Shienbrood9a245532012-03-07 14:20:39 -0500438void WiFiService::Disconnect(Error *error) {
439 LOG(INFO) << __func__;
440 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000441 wifi_->DisconnectFrom(this);
442}
443
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800444string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700445 return wifi_->GetRpcIdentifier();
446}
447
mukesh agrawal29c13a12011-11-24 00:09:19 +0000448void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800449 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000450 if (security_ == flimflam::kSecurityNone) {
451 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800452 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800453 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800454 } else if (Is8021x()) {
455 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000456 } else if (security_ == flimflam::kSecurityWep ||
457 security_ == flimflam::kSecurityWpa ||
458 security_ == flimflam::kSecurityPsk ||
459 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800460 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800461 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000462 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800463 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000464}
465
mukesh agrawale1d90e92012-02-15 17:36:08 -0800466void WiFiService::UpdateFromEndpoints() {
467 const WiFiEndpoint *representative_endpoint = NULL;
468
469 if (current_endpoint_) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800470 representative_endpoint = current_endpoint_;
471 } else {
472 int16 best_signal = std::numeric_limits<int16>::min();
473 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
474 i != endpoints_.end(); ++i) {
475 if ((*i)->signal_strength() >= best_signal) {
476 best_signal = (*i)->signal_strength();
477 representative_endpoint = *i;
478 }
479 }
480 }
481
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700482 uint16 frequency = 0;
483 int16 signal = std::numeric_limits<int16>::min();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700484 string bssid;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700485 Stringmap vendor_information;
486 if (representative_endpoint) {
mukesh agrawale1d90e92012-02-15 17:36:08 -0800487 frequency = representative_endpoint->frequency();
488 signal = representative_endpoint->signal_strength();
mukesh agrawal923f14f2012-06-04 16:46:08 -0700489 bssid = representative_endpoint->bssid_string();
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700490 vendor_information = representative_endpoint->GetVendorInformation();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800491 }
492
493 if (frequency_ != frequency) {
494 frequency_ = frequency;
495 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
496 }
mukesh agrawal923f14f2012-06-04 16:46:08 -0700497 if (bssid_ != bssid) {
498 bssid_ = bssid;
499 adaptor()->EmitStringChanged(flimflam::kWifiBSsid, bssid_);
500 }
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700501 if (vendor_information_ != vendor_information) {
502 vendor_information_ = vendor_information;
503 adaptor()->EmitStringmapChanged(kWifiVendorInformationProperty,
504 vendor_information_);
505 }
mukesh agrawale1d90e92012-02-15 17:36:08 -0800506 SetStrength(SignalToStrength(signal));
507}
508
mukesh agrawal1a056262011-10-05 14:36:54 -0700509// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000510void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
511 Error *error) {
512 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700513}
514
515// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000516void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
517 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700518 unsigned int length = passphrase.length();
519 vector<uint8> passphrase_bytes;
520
521 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
522 if (length != IEEE_80211::kWPAHexLen &&
523 (length < IEEE_80211::kWPAAsciiMinLen ||
524 length > IEEE_80211::kWPAAsciiMaxLen)) {
525 error->Populate(Error::kInvalidPassphrase);
526 }
527 } else {
528 if (length < IEEE_80211::kWPAAsciiMinLen ||
529 length > IEEE_80211::kWPAAsciiMaxLen) {
530 error->Populate(Error::kInvalidPassphrase);
531 }
532 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000533}
mukesh agrawal1a056262011-10-05 14:36:54 -0700534
Thieu Lef4cbda92011-11-10 23:41:24 +0000535// static
536void WiFiService::ParseWEPPassphrase(const string &passphrase,
537 int *key_index,
538 std::vector<uint8> *password_bytes,
539 Error *error) {
540 unsigned int length = passphrase.length();
541 int key_index_local;
542 std::string password_text;
543 bool is_hex = false;
544
545 switch (length) {
546 case IEEE_80211::kWEP40AsciiLen:
547 case IEEE_80211::kWEP104AsciiLen:
548 key_index_local = 0;
549 password_text = passphrase;
550 break;
551 case IEEE_80211::kWEP40AsciiLen + 2:
552 case IEEE_80211::kWEP104AsciiLen + 2:
553 if (CheckWEPKeyIndex(passphrase, error)) {
554 base::StringToInt(passphrase.substr(0,1), &key_index_local);
555 password_text = passphrase.substr(2);
556 }
557 break;
558 case IEEE_80211::kWEP40HexLen:
559 case IEEE_80211::kWEP104HexLen:
560 if (CheckWEPIsHex(passphrase, error)) {
561 key_index_local = 0;
562 password_text = passphrase;
563 is_hex = true;
564 }
565 break;
566 case IEEE_80211::kWEP40HexLen + 2:
567 case IEEE_80211::kWEP104HexLen + 2:
568 if(CheckWEPKeyIndex(passphrase, error) &&
569 CheckWEPIsHex(passphrase.substr(2), error)) {
570 base::StringToInt(passphrase.substr(0,1), &key_index_local);
571 password_text = passphrase.substr(2);
572 is_hex = true;
573 } else if (CheckWEPPrefix(passphrase, error) &&
574 CheckWEPIsHex(passphrase.substr(2), error)) {
575 key_index_local = 0;
576 password_text = passphrase.substr(2);
577 is_hex = true;
578 }
579 break;
580 case IEEE_80211::kWEP40HexLen + 4:
581 case IEEE_80211::kWEP104HexLen + 4:
582 if (CheckWEPKeyIndex(passphrase, error) &&
583 CheckWEPPrefix(passphrase.substr(2), error) &&
584 CheckWEPIsHex(passphrase.substr(4), error)) {
585 base::StringToInt(passphrase.substr(0,1), &key_index_local);
586 password_text = passphrase.substr(4);
587 is_hex = true;
588 }
589 break;
590 default:
591 error->Populate(Error::kInvalidPassphrase);
592 break;
593 }
594
mukesh agrawal1a056262011-10-05 14:36:54 -0700595 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000596 if (key_index)
597 *key_index = key_index_local;
598 if (password_bytes) {
599 if (is_hex)
600 base::HexStringToBytes(password_text, password_bytes);
601 else
602 password_bytes->insert(password_bytes->end(),
603 password_text.begin(),
604 password_text.end());
605 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700606 }
607}
608
609// static
610bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
611 vector<uint8> passphrase_bytes;
612 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
613 return true;
614 } else {
615 error->Populate(Error::kInvalidPassphrase);
616 return false;
617 }
618}
619
620// static
621bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
622 if (StartsWithASCII(passphrase, "0:", false) ||
623 StartsWithASCII(passphrase, "1:", false) ||
624 StartsWithASCII(passphrase, "2:", false) ||
625 StartsWithASCII(passphrase, "3:", false)) {
626 return true;
627 } else {
628 error->Populate(Error::kInvalidPassphrase);
629 return false;
630 }
631}
632
633// static
634bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
635 if (StartsWithASCII(passphrase, "0x", false)) {
636 return true;
637 } else {
638 error->Populate(Error::kInvalidPassphrase);
639 return false;
640 }
641}
642
Paul Stewart6ab23a92011-11-09 17:17:47 -0800643// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800644string WiFiService::GetSecurityClass(const string &security) {
645 if (security == flimflam::kSecurityRsn ||
646 security == flimflam::kSecurityWpa) {
647 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700648 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800649 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700650 }
651}
652
Paul Stewarta41e38d2011-11-11 07:47:29 -0800653// static
654bool WiFiService::ParseStorageIdentifier(const string &storage_name,
655 string *address,
656 string *mode,
657 string *security) {
658 vector<string> wifi_parts;
659 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800660 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
661 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800662 return false;
663 }
664 *address = wifi_parts[1];
665 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800666 if (wifi_parts.size() == 5) {
667 *security = wifi_parts[4];
668 } else {
669 // Account for security type "802_1x" which got split up above.
670 *security = wifi_parts[4] + "_" + wifi_parts[5];
671 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800672 return true;
673}
674
mukesh agrawale1d90e92012-02-15 17:36:08 -0800675// static
676uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
677 int16 strength;
678 if (signal_dbm > 0) {
679 if (!logged_signal_warning) {
680 LOG(WARNING) << "Signal strength is suspiciously high. "
681 << "Assuming value " << signal_dbm << " is not in dBm.";
682 logged_signal_warning = true;
683 }
684 strength = signal_dbm;
685 } else {
686 strength = 120 + signal_dbm; // Call -20dBm "perfect".
687 }
688
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800689 if (strength > kStrengthMax) {
690 strength = kStrengthMax;
691 } else if (strength < kStrengthMin) {
692 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800693 }
694 return strength;
695}
696
Paul Stewart6ab23a92011-11-09 17:17:47 -0800697string WiFiService::GetGenericStorageIdentifier() const {
698 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
699}
700
Paul Stewartd08f4432011-11-04 07:48:20 -0700701string WiFiService::GetSpecificStorageIdentifier() const {
702 return GetStorageIdentifierForSecurity(security_);
703}
704
705string WiFiService::GetStorageIdentifierForSecurity(
706 const string &security) const {
707 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
708 flimflam::kTypeWifi,
709 wifi_->address().c_str(),
710 hex_ssid_.c_str(),
711 mode_.c_str(),
712 security.c_str()));
713}
714
Gary Moraine4aaf5e2012-04-05 14:37:32 -0700715void WiFiService::set_eap(const EapCredentials &new_eap) {
716 EapCredentials modified_eap = new_eap;
717
718 // An empty key_management field is invalid. Prevent it, if possible.
719 if (modified_eap.key_management.empty()) {
720 modified_eap.key_management = eap().key_management;
721 }
722 Service::set_eap(modified_eap);
Gaurav Shah10109f22011-11-11 20:16:22 -0800723 UpdateConnectable();
724}
725
Paul Stewart4357f4e2012-04-26 17:39:26 -0700726void WiFiService::OnProfileConfigured() {
727 if (profile() || !hidden_ssid()) {
728 return;
729 }
730 // This situation occurs when a hidden WiFi service created via GetService
731 // has been persisted to a profile in Manager::ConfigureService(). Now
732 // that configuration is saved, we must join the service with its profile,
733 // which will make this SSID eligible for directed probes during scans.
734 manager()->RegisterService(this);
735}
736
Gaurav Shah29d68882012-01-30 19:06:42 -0800737bool WiFiService::Is8021x() const {
738 if (security_ == flimflam::kSecurity8021x)
739 return true;
740
741 // Dynamic WEP + 802.1x.
742 if (security_ == flimflam::kSecurityWep &&
743 GetEAPKeyManagement() == "IEEE8021X")
744 return true;
745 return false;
746}
747
Gaurav Shah10109f22011-11-11 20:16:22 -0800748void WiFiService::Populate8021xProperties(
749 std::map<string, DBus::Variant> *params) {
Paul Stewartecf4cd12012-04-17 11:08:39 -0700750 string ca_cert = eap().ca_cert;
751 if (!eap().ca_cert_nss.empty()) {
752 vector<char> id(ssid_.begin(), ssid_.end());
753 FilePath certfile = nss_->GetDERCertfile(eap().ca_cert_nss, id);
754 if (certfile.empty()) {
755 LOG(ERROR) << "Unable to extract certificate: " << eap().ca_cert_nss;
756 } else {
757 ca_cert = certfile.value();
758 }
759 }
760
761
Gaurav Shah10109f22011-11-11 20:16:22 -0800762 typedef std::pair<const char *, const char *> KeyVal;
Paul Stewart20550982012-04-16 12:16:11 -0700763 KeyVal init_propertyvals[] = {
Gaurav Shah10109f22011-11-11 20:16:22 -0800764 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
765 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
766 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
767 eap().inner_eap.c_str()),
768 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
769 eap().anonymous_identity.c_str()),
770 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
771 eap().client_cert.c_str()),
772 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
773 eap().private_key.c_str()),
774 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
775 eap().private_key_password.c_str()),
Paul Stewartecf4cd12012-04-17 11:08:39 -0700776 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, ca_cert.c_str()),
Gaurav Shah10109f22011-11-11 20:16:22 -0800777 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
778 eap().password.c_str()),
779 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
780 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
781 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
782 eap().ca_cert_id.c_str()),
Paul Stewartbc6e7392012-05-24 07:07:48 -0700783 KeyVal(wpa_supplicant::kNetworkPropertyEapSubjectMatch,
784 eap().subject_match.c_str())
Gaurav Shah10109f22011-11-11 20:16:22 -0800785 };
786
Paul Stewart20550982012-04-16 12:16:11 -0700787 vector<KeyVal> propertyvals(init_propertyvals,
788 init_propertyvals + arraysize(init_propertyvals));
789 if (eap().use_system_cas) {
790 propertyvals.push_back(KeyVal(
791 wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath));
Paul Stewartecf4cd12012-04-17 11:08:39 -0700792 } else if (ca_cert.empty()) {
Paul Stewart20550982012-04-16 12:16:11 -0700793 LOG(WARNING) << __func__
794 << ": No certificate authorities are configured."
795 << " Server certificates will be accepted"
796 << " unconditionally.";
797 }
798
799 if (!eap().cert_id.empty() || !eap().key_id.empty() ||
800 !eap().ca_cert_id.empty()) {
801 propertyvals.push_back(KeyVal(
802 wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()));
803 propertyvals.push_back(KeyVal(
804 wpa_supplicant::kNetworkPropertyEngineId,
805 wpa_supplicant::kEnginePKCS11));
806 // We can't use the propertyvals vector for this since this argument
807 // is a uint32, not a string.
808 (*params)[wpa_supplicant::kNetworkPropertyEngine].writer().
809 append_uint32(wpa_supplicant::kDefaultEngine);
810 }
811
812 vector<KeyVal>::iterator it;
813 for (it = propertyvals.begin(); it != propertyvals.end(); ++it) {
814 if (strlen((*it).second) > 0) {
815 (*params)[(*it).first].writer().append_string((*it).second);
Gaurav Shah10109f22011-11-11 20:16:22 -0800816 }
817 }
818}
819
mukesh agrawalb54601c2011-06-07 17:39:22 -0700820} // namespace shill