blob: 90c20330d5dad66576c0cd9ae16ae6098f18b272 [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"
Thieu Lef7709452011-11-15 01:13:19 +000025#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070026#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070027#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070028#include "shill/wifi_endpoint.h"
29#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070030
mukesh agrawal261daca2011-12-02 18:56:56 +000031using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070032using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070033using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070034
35namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070036
Paul Stewartd08f4432011-11-04 07:48:20 -070037const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080038const char WiFiService::kStorageMode[] = "WiFi.Mode";
39const char WiFiService::kStoragePassphrase[] = "Passphrase";
40const char WiFiService::kStorageSecurity[] = "WiFi.Security";
41const char WiFiService::kStorageSSID[] = "SSID";
mukesh agrawale1d90e92012-02-15 17:36:08 -080042bool WiFiService::logged_signal_warning = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070043
mukesh agrawalb54601c2011-06-07 17:39:22 -070044WiFiService::WiFiService(ControlInterface *control_interface,
45 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080046 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070047 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070048 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080049 const vector<uint8_t> &ssid,
50 const string &mode,
51 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080052 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080053 : Service(control_interface, dispatcher, metrics, manager,
54 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070055 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070056 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070057 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080058 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000059 frequency_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070060 task_factory_(this),
61 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070062 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070063 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070064 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080065 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
66 &WiFiService::SetPassphrase,
67 &WiFiService::ClearPassphrase,
68 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070069 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
70 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070071
Paul Stewartac4ac002011-08-26 12:04:26 -070072 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
73 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
74 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
75 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070076
mukesh agrawald835b202011-10-07 15:26:47 -070077 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
78 string ssid_string(
79 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080080 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070081 // WifiHexSsid property should only be present if Name property
82 // has been munged.
83 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
84 }
85 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070086
mukesh agrawal6e277772011-09-29 15:04:23 -070087 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
88 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080089 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080090 // Passphrases are not mandatory for 802.1X.
91 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070092 } else if (security_ == flimflam::kSecurityPsk) {
93 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070094 } else if (security_ == flimflam::kSecurityRsn) {
95 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070096 } else if (security_ == flimflam::kSecurityWpa) {
97 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070098 } else if (security_ == flimflam::kSecurityWep) {
99 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700100 } else if (security_ == flimflam::kSecurityNone) {
101 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700102 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800103 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700104 }
105
Paul Stewartd08f4432011-11-04 07:48:20 -0700106 // Until we know better (at Profile load time), use the generic name.
107 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000108 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700109}
110
111WiFiService::~WiFiService() {
112 LOG(INFO) << __func__;
113}
114
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000115void WiFiService::AutoConnect() {
116 if (IsAutoConnectable()) {
117 // Execute immediately, for two reasons:
118 //
119 // 1. We need IsAutoConnectable to return the correct value for
120 // other WiFiServices, and that depends on WiFi's state.
121 //
122 // 2. We should probably limit the extent to which we queue up
123 // actions (such as AutoConnect) which depend on current state.
124 // If we queued AutoConnects, we could build a long queue of
125 // useless work (one AutoConnect per Service), which blocks
126 // more timely work.
127 ConnectTask();
mukesh agrawal592516d2012-01-12 14:01:00 -0800128 } else {
129 LOG(INFO) << "Suppressed autoconnect to " << friendly_name();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000130 }
131}
132
mukesh agrawal1830fa12011-09-26 14:31:40 -0700133void WiFiService::Connect(Error */*error*/) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800134 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000135 // Defer handling, since dbus-c++ does not permit us to send an
136 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700137 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700138 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700139}
140
mukesh agrawaladb68482012-01-17 16:31:51 -0800141void WiFiService::Disconnect(Error *error) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000142 LOG(INFO) << __func__;
mukesh agrawaladb68482012-01-17 16:31:51 -0800143 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000144 // Defer handling, since dbus-c++ does not permit us to send an
145 // outbound request while processing an inbound one.
146 dispatcher()->PostTask(
147 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700148}
149
Paul Stewart22aa71b2011-09-16 12:15:11 -0700150bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
151 return wifi_->TechnologyIs(type);
152}
153
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000154bool WiFiService::IsAutoConnectable() const {
mukesh agrawaladb68482012-01-17 16:31:51 -0800155 return Service::IsAutoConnectable() &&
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000156 // Only auto-connect to Services which have visible Endpoints.
157 // (Needed because hidden Services may remain registered with
158 // Manager even without visible Endpoints.)
mukesh agrawaladb68482012-01-17 16:31:51 -0800159 HasEndpoints() &&
mukesh agrawal76d13882012-01-12 15:23:11 -0800160 // Do not preempt an existing connection (whether pending, or
161 // connected, and whether to this service, or another).
mukesh agrawaladb68482012-01-17 16:31:51 -0800162 wifi_->IsIdle();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000163}
164
165bool WiFiService::IsConnecting() const {
166 // WiFi does not move us into the associating state until it gets
167 // feedback from wpa_supplicant. So, to answer whether or
168 // not we're connecting, we consult with |wifi_|.
169 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800170}
171
mukesh agrawale1d90e92012-02-15 17:36:08 -0800172void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000173 DCHECK(endpoint->ssid() == ssid());
174 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800175 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000176}
177
mukesh agrawale1d90e92012-02-15 17:36:08 -0800178void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000179 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
180 DCHECK(i != endpoints_.end());
181 if (i == endpoints_.end()) {
182 LOG(WARNING) << "In " << __func__ << "(): "
183 << "ignorning non-existent endpoint "
184 << endpoint->bssid_string();
185 return;
186 }
187 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800188 if (current_endpoint_ == endpoint) {
189 current_endpoint_ = NULL;
190 }
191 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000192}
193
mukesh agrawale1d90e92012-02-15 17:36:08 -0800194void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
195 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
196 current_endpoint_ = endpoint;
197 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000198}
199
mukesh agrawalb20776f2012-02-10 16:00:36 -0800200void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
201 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800202 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800203}
204
Chris Masone6515aab2011-10-12 16:19:09 -0700205string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700206 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700207}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700208
mukesh agrawal1a056262011-10-05 14:36:54 -0700209void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
210 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000211 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700212 } else if (security_ == flimflam::kSecurityPsk ||
213 security_ == flimflam::kSecurityWpa ||
214 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000215 ValidateWPAPassphrase(passphrase, error);
216 } else {
217 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700218 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000219
Paul Stewart2706aaf2011-12-14 16:44:04 -0800220 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000221 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800222 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000223
224 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700225}
226
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800227// ClearPassphrase is separate from SetPassphrase, because the default
228// value for |passphrase_| would not pass validation.
229void WiFiService::ClearPassphrase(Error */*error*/) {
230 passphrase_.clear();
231 UpdateConnectable();
232}
233
Paul Stewartd08f4432011-11-04 07:48:20 -0700234bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
235 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
236 storage->ContainsGroup(GetSpecificStorageIdentifier());
237}
238
Paul Stewarta41e38d2011-11-11 07:47:29 -0800239bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800240 // WiFi Services should be displayed only if they are in range (have
241 // endpoints that have shown up in a scan) or if the service is actively
242 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000243 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800244}
245
Paul Stewartd08f4432011-11-04 07:48:20 -0700246bool WiFiService::Load(StoreInterface *storage) {
247 // First find out which storage identifier is available in priority order
248 // of specific, generic.
249 string id = GetSpecificStorageIdentifier();
250 if (!storage->ContainsGroup(id)) {
251 id = GetGenericStorageIdentifier();
252 if (!storage->ContainsGroup(id)) {
253 LOG(WARNING) << "Service is not available in the persistent store: "
254 << id;
255 return false;
256 }
257 }
258
259 // Set our storage identifier to match the storage name in the Profile.
260 storage_identifier_ = id;
261
262 // Load properties common to all Services.
263 if (!Service::Load(storage)) {
264 return false;
265 }
266
267 // Load properties specific to WiFi services.
268 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000269
Paul Stewart2706aaf2011-12-14 16:44:04 -0800270 // NB: mode, security and ssid parameters are never read in from
271 // Load() as they are provided from the scan.
272
273 string passphrase;
274 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
275 Error error;
276 SetPassphrase(passphrase, &error);
277 if (!error.IsSuccess()) {
278 LOG(ERROR) << "Passphrase could not be set: "
279 << Error::GetName(error.type());
280 }
281 }
282
Paul Stewartd08f4432011-11-04 07:48:20 -0700283 return true;
284}
285
286bool WiFiService::Save(StoreInterface *storage) {
287 // Save properties common to all Services.
288 if (!Service::Save(storage)) {
289 return false;
290 }
291
292 // Save properties specific to WiFi services.
293 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800294 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
295 storage->SetString(id, kStorageMode, mode_);
296 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
297 storage->SetString(id, kStorageSecurity, security_);
298 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000299
300 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700301 return true;
302}
303
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800304void WiFiService::Unload() {
305 Service::Unload();
306 hidden_ssid_ = false;
307 passphrase_ = "";
308 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800309 if (security_ == flimflam::kSecurity8021x) {
310 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
311 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
312 // Make sure that these are cleared when credentials for networks
313 // of this type goes away.
314 //
315 // When wpa_supplicant gains the ability, do this credential
316 // clearing on a per-service basis. Also do this whenever the credentials
317 // for a service changes. crosbug.com/25670
318 wifi_->ClearCachedCredentials();
319 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800320}
321
Paul Stewart6ab23a92011-11-09 17:17:47 -0800322bool WiFiService::IsSecurityMatch(const string &security) const {
323 return GetSecurityClass(security) == GetSecurityClass(security_);
324}
325
Thieu Le48e6d6d2011-12-06 00:40:27 +0000326void WiFiService::InitializeCustomMetrics() const {
327 string histogram = metrics()->GetFullMetricName(
328 Metrics::kMetricTimeToJoinMilliseconds,
329 technology());
330 metrics()->AddServiceStateTransitionTimer(this,
331 histogram,
332 Service::kStateAssociating,
333 Service::kStateConfiguring);
334}
335
336void WiFiService::SendPostReadyStateMetrics() const {
337 // TODO(thieule): Send physical mode and security metrics.
338 // crosbug.com/24441
339 metrics()->SendEnumToUMA(
340 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
341 technology()),
342 Metrics::WiFiFrequencyToChannel(frequency_),
343 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000344
345 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
346 metrics()->SendEnumToUMA(
347 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
348 technology()),
349 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
350 Metrics::kWiFiNetworkPhyModeMax);
351
352 Metrics::WiFiSecurity security_uma =
353 Metrics::WiFiSecurityStringToEnum(security_);
354 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
355 metrics()->SendEnumToUMA(
356 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
357 technology()),
358 security_uma,
359 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000360}
361
mukesh agrawal32399322011-09-01 10:53:43 -0700362// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800363void WiFiService::HelpRegisterWriteOnlyDerivedString(
364 const string &name,
365 void(WiFiService::*set)(const string &, Error *),
366 void(WiFiService::*clear)(Error *),
367 const string *default_value) {
368 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000369 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800370 StringAccessor(
371 new CustomWriteOnlyAccessor<WiFiService, string>(
372 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000373}
374
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700375void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700376 std::map<string, DBus::Variant> params;
377 DBus::MessageIter writer;
378
379 params[wpa_supplicant::kNetworkPropertyMode].writer().
380 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
381
Gaurav Shah29d68882012-01-30 19:06:42 -0800382 if (Is8021x()) {
383 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800384 if (GetEAPKeyManagement().empty())
385 SetEAPKeyManagement("WPA-EAP");
386 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700387 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800388 const string psk_proto = StringPrintf("%s %s",
389 wpa_supplicant::kSecurityModeWPA,
390 wpa_supplicant::kSecurityModeRSN);
391 params[wpa_supplicant::kPropertySecurityProtocol].writer().
392 append_string(psk_proto.c_str());
393 params[wpa_supplicant::kPropertyPreSharedKey].writer().
394 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700395 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700396 params[wpa_supplicant::kPropertySecurityProtocol].writer().
397 append_string(wpa_supplicant::kSecurityModeRSN);
398 params[wpa_supplicant::kPropertyPreSharedKey].writer().
399 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700400 } else if (security_ == flimflam::kSecurityWpa) {
401 params[wpa_supplicant::kPropertySecurityProtocol].writer().
402 append_string(wpa_supplicant::kSecurityModeWPA);
403 params[wpa_supplicant::kPropertyPreSharedKey].writer().
404 append_string(passphrase_.c_str());
405 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000406 params[wpa_supplicant::kPropertyAuthAlg].writer().
407 append_string(wpa_supplicant::kSecurityAuthAlg);
408 Error error;
409 int key_index;
410 std::vector<uint8> password_bytes;
411 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
412 writer = params[wpa_supplicant::kPropertyWEPKey +
413 base::IntToString(key_index)].writer();
414 writer << password_bytes;
415 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
416 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700417 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800418 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700419 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800420 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700421 }
422
Gaurav Shah10109f22011-11-11 20:16:22 -0800423 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700424 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800425
426 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700427 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
428 writer << ssid_;
429
430 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700431}
432
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000433void WiFiService::DisconnectTask() {
434 wifi_->DisconnectFrom(this);
435}
436
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800437string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700438 return wifi_->GetRpcIdentifier();
439}
440
mukesh agrawal29c13a12011-11-24 00:09:19 +0000441void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800442 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000443 if (security_ == flimflam::kSecurityNone) {
444 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800445 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800446 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800447 } else if (Is8021x()) {
448 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000449 } else if (security_ == flimflam::kSecurityWep ||
450 security_ == flimflam::kSecurityWpa ||
451 security_ == flimflam::kSecurityPsk ||
452 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800453 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800454 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000455 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800456 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000457}
458
mukesh agrawale1d90e92012-02-15 17:36:08 -0800459void WiFiService::UpdateFromEndpoints() {
460 const WiFiEndpoint *representative_endpoint = NULL;
461
462 if (current_endpoint_) {
463 // TODO: Copy BSSID here (crosbug.com/22377).
464 representative_endpoint = current_endpoint_;
465 } else {
466 int16 best_signal = std::numeric_limits<int16>::min();
467 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
468 i != endpoints_.end(); ++i) {
469 if ((*i)->signal_strength() >= best_signal) {
470 best_signal = (*i)->signal_strength();
471 representative_endpoint = *i;
472 }
473 }
474 }
475
476 uint16 frequency;
477 int16 signal;
478 if (!representative_endpoint) {
479 frequency = 0;
480 signal = std::numeric_limits<int16>::min();
481 } else {
482 frequency = representative_endpoint->frequency();
483 signal = representative_endpoint->signal_strength();
484 }
485
486 if (frequency_ != frequency) {
487 frequency_ = frequency;
488 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
489 }
490 SetStrength(SignalToStrength(signal));
491}
492
mukesh agrawal1a056262011-10-05 14:36:54 -0700493// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000494void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
495 Error *error) {
496 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700497}
498
499// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000500void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
501 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700502 unsigned int length = passphrase.length();
503 vector<uint8> passphrase_bytes;
504
505 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
506 if (length != IEEE_80211::kWPAHexLen &&
507 (length < IEEE_80211::kWPAAsciiMinLen ||
508 length > IEEE_80211::kWPAAsciiMaxLen)) {
509 error->Populate(Error::kInvalidPassphrase);
510 }
511 } else {
512 if (length < IEEE_80211::kWPAAsciiMinLen ||
513 length > IEEE_80211::kWPAAsciiMaxLen) {
514 error->Populate(Error::kInvalidPassphrase);
515 }
516 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000517}
mukesh agrawal1a056262011-10-05 14:36:54 -0700518
Thieu Lef4cbda92011-11-10 23:41:24 +0000519// static
520void WiFiService::ParseWEPPassphrase(const string &passphrase,
521 int *key_index,
522 std::vector<uint8> *password_bytes,
523 Error *error) {
524 unsigned int length = passphrase.length();
525 int key_index_local;
526 std::string password_text;
527 bool is_hex = false;
528
529 switch (length) {
530 case IEEE_80211::kWEP40AsciiLen:
531 case IEEE_80211::kWEP104AsciiLen:
532 key_index_local = 0;
533 password_text = passphrase;
534 break;
535 case IEEE_80211::kWEP40AsciiLen + 2:
536 case IEEE_80211::kWEP104AsciiLen + 2:
537 if (CheckWEPKeyIndex(passphrase, error)) {
538 base::StringToInt(passphrase.substr(0,1), &key_index_local);
539 password_text = passphrase.substr(2);
540 }
541 break;
542 case IEEE_80211::kWEP40HexLen:
543 case IEEE_80211::kWEP104HexLen:
544 if (CheckWEPIsHex(passphrase, error)) {
545 key_index_local = 0;
546 password_text = passphrase;
547 is_hex = true;
548 }
549 break;
550 case IEEE_80211::kWEP40HexLen + 2:
551 case IEEE_80211::kWEP104HexLen + 2:
552 if(CheckWEPKeyIndex(passphrase, error) &&
553 CheckWEPIsHex(passphrase.substr(2), error)) {
554 base::StringToInt(passphrase.substr(0,1), &key_index_local);
555 password_text = passphrase.substr(2);
556 is_hex = true;
557 } else if (CheckWEPPrefix(passphrase, error) &&
558 CheckWEPIsHex(passphrase.substr(2), error)) {
559 key_index_local = 0;
560 password_text = passphrase.substr(2);
561 is_hex = true;
562 }
563 break;
564 case IEEE_80211::kWEP40HexLen + 4:
565 case IEEE_80211::kWEP104HexLen + 4:
566 if (CheckWEPKeyIndex(passphrase, error) &&
567 CheckWEPPrefix(passphrase.substr(2), error) &&
568 CheckWEPIsHex(passphrase.substr(4), error)) {
569 base::StringToInt(passphrase.substr(0,1), &key_index_local);
570 password_text = passphrase.substr(4);
571 is_hex = true;
572 }
573 break;
574 default:
575 error->Populate(Error::kInvalidPassphrase);
576 break;
577 }
578
mukesh agrawal1a056262011-10-05 14:36:54 -0700579 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000580 if (key_index)
581 *key_index = key_index_local;
582 if (password_bytes) {
583 if (is_hex)
584 base::HexStringToBytes(password_text, password_bytes);
585 else
586 password_bytes->insert(password_bytes->end(),
587 password_text.begin(),
588 password_text.end());
589 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700590 }
591}
592
593// static
594bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
595 vector<uint8> passphrase_bytes;
596 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
597 return true;
598 } else {
599 error->Populate(Error::kInvalidPassphrase);
600 return false;
601 }
602}
603
604// static
605bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
606 if (StartsWithASCII(passphrase, "0:", false) ||
607 StartsWithASCII(passphrase, "1:", false) ||
608 StartsWithASCII(passphrase, "2:", false) ||
609 StartsWithASCII(passphrase, "3:", false)) {
610 return true;
611 } else {
612 error->Populate(Error::kInvalidPassphrase);
613 return false;
614 }
615}
616
617// static
618bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
619 if (StartsWithASCII(passphrase, "0x", false)) {
620 return true;
621 } else {
622 error->Populate(Error::kInvalidPassphrase);
623 return false;
624 }
625}
626
Paul Stewart6ab23a92011-11-09 17:17:47 -0800627// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800628string WiFiService::GetSecurityClass(const string &security) {
629 if (security == flimflam::kSecurityRsn ||
630 security == flimflam::kSecurityWpa) {
631 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700632 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800633 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700634 }
635}
636
Paul Stewarta41e38d2011-11-11 07:47:29 -0800637// static
638bool WiFiService::ParseStorageIdentifier(const string &storage_name,
639 string *address,
640 string *mode,
641 string *security) {
642 vector<string> wifi_parts;
643 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800644 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
645 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800646 return false;
647 }
648 *address = wifi_parts[1];
649 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800650 if (wifi_parts.size() == 5) {
651 *security = wifi_parts[4];
652 } else {
653 // Account for security type "802_1x" which got split up above.
654 *security = wifi_parts[4] + "_" + wifi_parts[5];
655 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800656 return true;
657}
658
mukesh agrawale1d90e92012-02-15 17:36:08 -0800659// static
660uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
661 int16 strength;
662 if (signal_dbm > 0) {
663 if (!logged_signal_warning) {
664 LOG(WARNING) << "Signal strength is suspiciously high. "
665 << "Assuming value " << signal_dbm << " is not in dBm.";
666 logged_signal_warning = true;
667 }
668 strength = signal_dbm;
669 } else {
670 strength = 120 + signal_dbm; // Call -20dBm "perfect".
671 }
672
673 if (strength > 100) {
674 strength = 100;
675 } else if (strength < 0) {
676 strength = 0;
677 }
678 return strength;
679}
680
Paul Stewart6ab23a92011-11-09 17:17:47 -0800681string WiFiService::GetGenericStorageIdentifier() const {
682 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
683}
684
Paul Stewartd08f4432011-11-04 07:48:20 -0700685string WiFiService::GetSpecificStorageIdentifier() const {
686 return GetStorageIdentifierForSecurity(security_);
687}
688
689string WiFiService::GetStorageIdentifierForSecurity(
690 const string &security) const {
691 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
692 flimflam::kTypeWifi,
693 wifi_->address().c_str(),
694 hex_ssid_.c_str(),
695 mode_.c_str(),
696 security.c_str()));
697}
698
Gaurav Shah10109f22011-11-11 20:16:22 -0800699void WiFiService::set_eap(const EapCredentials &eap) {
700 Service::set_eap(eap);
701 UpdateConnectable();
702}
703
Gaurav Shah29d68882012-01-30 19:06:42 -0800704bool WiFiService::Is8021x() const {
705 if (security_ == flimflam::kSecurity8021x)
706 return true;
707
708 // Dynamic WEP + 802.1x.
709 if (security_ == flimflam::kSecurityWep &&
710 GetEAPKeyManagement() == "IEEE8021X")
711 return true;
712 return false;
713}
714
Gaurav Shah10109f22011-11-11 20:16:22 -0800715void WiFiService::Populate8021xProperties(
716 std::map<string, DBus::Variant> *params) {
717 typedef std::pair<const char *, const char *> KeyVal;
718 KeyVal propertyvals[] = {
719 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
720 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
721 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
722 eap().inner_eap.c_str()),
723 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
724 eap().anonymous_identity.c_str()),
725 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
726 eap().client_cert.c_str()),
727 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
728 eap().private_key.c_str()),
729 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
730 eap().private_key_password.c_str()),
731 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
732 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
733 eap().password.c_str()),
734 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
735 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
736 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
737 eap().ca_cert_id.c_str()),
738 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
739 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
740 // crosbug.com/25663
741 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
742 };
743
744 DBus::MessageIter writer;
745 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
746 if (strlen(propertyvals[i].second) > 0) {
747 (*params)[propertyvals[i].first].writer().
748 append_string(propertyvals[i].second);
749 }
750 }
751}
752
mukesh agrawalb54601c2011-06-07 17:39:22 -0700753} // namespace shill