blob: 42c1329194c61e5a50b584bc454eb0d70f21d969 [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
18#include "shill/control_interface.h"
19#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070020#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070022#include "shill/ieee80211.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000023#include "shill/metrics.h"
Thieu Lef7709452011-11-15 01:13:19 +000024#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070025#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070026#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070027#include "shill/wifi_endpoint.h"
28#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070029
mukesh agrawal261daca2011-12-02 18:56:56 +000030using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070031using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070032using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070033
34namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070035
Paul Stewartd08f4432011-11-04 07:48:20 -070036const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080037const char WiFiService::kStorageMode[] = "WiFi.Mode";
38const char WiFiService::kStoragePassphrase[] = "Passphrase";
39const char WiFiService::kStorageSecurity[] = "WiFi.Security";
40const char WiFiService::kStorageSSID[] = "SSID";
Paul Stewartd08f4432011-11-04 07:48:20 -070041
mukesh agrawalb54601c2011-06-07 17:39:22 -070042WiFiService::WiFiService(ControlInterface *control_interface,
43 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080044 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070045 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070046 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080047 const vector<uint8_t> &ssid,
48 const string &mode,
49 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080050 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080051 : Service(control_interface, dispatcher, metrics, manager,
52 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070053 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070054 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070055 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080056 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000057 frequency_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070058 task_factory_(this),
59 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070060 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070061 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070062 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080063 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
64 &WiFiService::SetPassphrase,
65 &WiFiService::ClearPassphrase,
66 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070067 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
68 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070069
Paul Stewartac4ac002011-08-26 12:04:26 -070070 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
71 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
72 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
73 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070074
mukesh agrawald835b202011-10-07 15:26:47 -070075 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
76 string ssid_string(
77 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080078 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070079 // WifiHexSsid property should only be present if Name property
80 // has been munged.
81 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
82 }
83 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070084
mukesh agrawal6e277772011-09-29 15:04:23 -070085 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
86 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080087 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080088 // Passphrases are not mandatory for 802.1X.
89 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070090 } else if (security_ == flimflam::kSecurityPsk) {
91 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070092 } else if (security_ == flimflam::kSecurityRsn) {
93 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070094 } else if (security_ == flimflam::kSecurityWpa) {
95 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070096 } else if (security_ == flimflam::kSecurityWep) {
97 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -070098 } else if (security_ == flimflam::kSecurityNone) {
99 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700100 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800101 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700102 }
103
Paul Stewartd08f4432011-11-04 07:48:20 -0700104 // Until we know better (at Profile load time), use the generic name.
105 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000106 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700107}
108
109WiFiService::~WiFiService() {
110 LOG(INFO) << __func__;
111}
112
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000113void WiFiService::AutoConnect() {
114 if (IsAutoConnectable()) {
115 // Execute immediately, for two reasons:
116 //
117 // 1. We need IsAutoConnectable to return the correct value for
118 // other WiFiServices, and that depends on WiFi's state.
119 //
120 // 2. We should probably limit the extent to which we queue up
121 // actions (such as AutoConnect) which depend on current state.
122 // If we queued AutoConnects, we could build a long queue of
123 // useless work (one AutoConnect per Service), which blocks
124 // more timely work.
125 ConnectTask();
mukesh agrawal592516d2012-01-12 14:01:00 -0800126 } else {
127 LOG(INFO) << "Suppressed autoconnect to " << friendly_name();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000128 }
129}
130
mukesh agrawal1830fa12011-09-26 14:31:40 -0700131void WiFiService::Connect(Error */*error*/) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800132 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000133 // Defer handling, since dbus-c++ does not permit us to send an
134 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700135 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700136 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700137}
138
mukesh agrawaladb68482012-01-17 16:31:51 -0800139void WiFiService::Disconnect(Error *error) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000140 LOG(INFO) << __func__;
mukesh agrawaladb68482012-01-17 16:31:51 -0800141 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000142 // Defer handling, since dbus-c++ does not permit us to send an
143 // outbound request while processing an inbound one.
144 dispatcher()->PostTask(
145 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700146}
147
Paul Stewart22aa71b2011-09-16 12:15:11 -0700148bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
149 return wifi_->TechnologyIs(type);
150}
151
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000152bool WiFiService::IsAutoConnectable() const {
mukesh agrawaladb68482012-01-17 16:31:51 -0800153 return Service::IsAutoConnectable() &&
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000154 // Only auto-connect to Services which have visible Endpoints.
155 // (Needed because hidden Services may remain registered with
156 // Manager even without visible Endpoints.)
mukesh agrawaladb68482012-01-17 16:31:51 -0800157 HasEndpoints() &&
mukesh agrawal76d13882012-01-12 15:23:11 -0800158 // Do not preempt an existing connection (whether pending, or
159 // connected, and whether to this service, or another).
mukesh agrawaladb68482012-01-17 16:31:51 -0800160 wifi_->IsIdle();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000161}
162
163bool WiFiService::IsConnecting() const {
164 // WiFi does not move us into the associating state until it gets
165 // feedback from wpa_supplicant. So, to answer whether or
166 // not we're connecting, we consult with |wifi_|.
167 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800168}
169
mukesh agrawal261daca2011-12-02 18:56:56 +0000170void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
171 DCHECK(endpoint->ssid() == ssid());
172 endpoints_.insert(endpoint);
Thieu Lee41a72d2012-02-06 20:46:51 +0000173 // TODO: Track signal strength (crosbug.com/16786).
mukesh agrawal261daca2011-12-02 18:56:56 +0000174}
175
176void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
177 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
178 DCHECK(i != endpoints_.end());
179 if (i == endpoints_.end()) {
180 LOG(WARNING) << "In " << __func__ << "(): "
181 << "ignorning non-existent endpoint "
182 << endpoint->bssid_string();
183 return;
184 }
185 endpoints_.erase(i);
186}
187
Thieu Lee41a72d2012-02-06 20:46:51 +0000188void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint &endpoint) {
189 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
190 frequency_ = endpoint.frequency();
191 // TODO: Copy BSSID here (crosbug.com/22377).
192 // TODO: Copy signal strength (crosbug.com/16786).
193 // TODO(thieule): Update these values when supplicant signals that they
194 // have changed.
195 // (crosbug.com/16786)
196}
197
mukesh agrawalb20776f2012-02-10 16:00:36 -0800198void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
199 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
200 // TODO(quiche): If this is the connected endpoint, or the "representative"
201 // endpoint (when the service is disconnected), then update signal and
202 // frequency. crosbug.com/16786
203}
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 agrawal1a056262011-10-05 14:36:54 -0700459// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000460void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
461 Error *error) {
462 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700463}
464
465// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000466void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
467 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700468 unsigned int length = passphrase.length();
469 vector<uint8> passphrase_bytes;
470
471 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
472 if (length != IEEE_80211::kWPAHexLen &&
473 (length < IEEE_80211::kWPAAsciiMinLen ||
474 length > IEEE_80211::kWPAAsciiMaxLen)) {
475 error->Populate(Error::kInvalidPassphrase);
476 }
477 } else {
478 if (length < IEEE_80211::kWPAAsciiMinLen ||
479 length > IEEE_80211::kWPAAsciiMaxLen) {
480 error->Populate(Error::kInvalidPassphrase);
481 }
482 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000483}
mukesh agrawal1a056262011-10-05 14:36:54 -0700484
Thieu Lef4cbda92011-11-10 23:41:24 +0000485// static
486void WiFiService::ParseWEPPassphrase(const string &passphrase,
487 int *key_index,
488 std::vector<uint8> *password_bytes,
489 Error *error) {
490 unsigned int length = passphrase.length();
491 int key_index_local;
492 std::string password_text;
493 bool is_hex = false;
494
495 switch (length) {
496 case IEEE_80211::kWEP40AsciiLen:
497 case IEEE_80211::kWEP104AsciiLen:
498 key_index_local = 0;
499 password_text = passphrase;
500 break;
501 case IEEE_80211::kWEP40AsciiLen + 2:
502 case IEEE_80211::kWEP104AsciiLen + 2:
503 if (CheckWEPKeyIndex(passphrase, error)) {
504 base::StringToInt(passphrase.substr(0,1), &key_index_local);
505 password_text = passphrase.substr(2);
506 }
507 break;
508 case IEEE_80211::kWEP40HexLen:
509 case IEEE_80211::kWEP104HexLen:
510 if (CheckWEPIsHex(passphrase, error)) {
511 key_index_local = 0;
512 password_text = passphrase;
513 is_hex = true;
514 }
515 break;
516 case IEEE_80211::kWEP40HexLen + 2:
517 case IEEE_80211::kWEP104HexLen + 2:
518 if(CheckWEPKeyIndex(passphrase, error) &&
519 CheckWEPIsHex(passphrase.substr(2), error)) {
520 base::StringToInt(passphrase.substr(0,1), &key_index_local);
521 password_text = passphrase.substr(2);
522 is_hex = true;
523 } else if (CheckWEPPrefix(passphrase, error) &&
524 CheckWEPIsHex(passphrase.substr(2), error)) {
525 key_index_local = 0;
526 password_text = passphrase.substr(2);
527 is_hex = true;
528 }
529 break;
530 case IEEE_80211::kWEP40HexLen + 4:
531 case IEEE_80211::kWEP104HexLen + 4:
532 if (CheckWEPKeyIndex(passphrase, error) &&
533 CheckWEPPrefix(passphrase.substr(2), error) &&
534 CheckWEPIsHex(passphrase.substr(4), error)) {
535 base::StringToInt(passphrase.substr(0,1), &key_index_local);
536 password_text = passphrase.substr(4);
537 is_hex = true;
538 }
539 break;
540 default:
541 error->Populate(Error::kInvalidPassphrase);
542 break;
543 }
544
mukesh agrawal1a056262011-10-05 14:36:54 -0700545 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000546 if (key_index)
547 *key_index = key_index_local;
548 if (password_bytes) {
549 if (is_hex)
550 base::HexStringToBytes(password_text, password_bytes);
551 else
552 password_bytes->insert(password_bytes->end(),
553 password_text.begin(),
554 password_text.end());
555 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700556 }
557}
558
559// static
560bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
561 vector<uint8> passphrase_bytes;
562 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
563 return true;
564 } else {
565 error->Populate(Error::kInvalidPassphrase);
566 return false;
567 }
568}
569
570// static
571bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
572 if (StartsWithASCII(passphrase, "0:", false) ||
573 StartsWithASCII(passphrase, "1:", false) ||
574 StartsWithASCII(passphrase, "2:", false) ||
575 StartsWithASCII(passphrase, "3:", false)) {
576 return true;
577 } else {
578 error->Populate(Error::kInvalidPassphrase);
579 return false;
580 }
581}
582
583// static
584bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
585 if (StartsWithASCII(passphrase, "0x", false)) {
586 return true;
587 } else {
588 error->Populate(Error::kInvalidPassphrase);
589 return false;
590 }
591}
592
Paul Stewart6ab23a92011-11-09 17:17:47 -0800593// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800594string WiFiService::GetSecurityClass(const string &security) {
595 if (security == flimflam::kSecurityRsn ||
596 security == flimflam::kSecurityWpa) {
597 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700598 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800599 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700600 }
601}
602
Paul Stewarta41e38d2011-11-11 07:47:29 -0800603// static
604bool WiFiService::ParseStorageIdentifier(const string &storage_name,
605 string *address,
606 string *mode,
607 string *security) {
608 vector<string> wifi_parts;
609 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800610 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
611 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800612 return false;
613 }
614 *address = wifi_parts[1];
615 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800616 if (wifi_parts.size() == 5) {
617 *security = wifi_parts[4];
618 } else {
619 // Account for security type "802_1x" which got split up above.
620 *security = wifi_parts[4] + "_" + wifi_parts[5];
621 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800622 return true;
623}
624
Paul Stewart6ab23a92011-11-09 17:17:47 -0800625string WiFiService::GetGenericStorageIdentifier() const {
626 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
627}
628
Paul Stewartd08f4432011-11-04 07:48:20 -0700629string WiFiService::GetSpecificStorageIdentifier() const {
630 return GetStorageIdentifierForSecurity(security_);
631}
632
633string WiFiService::GetStorageIdentifierForSecurity(
634 const string &security) const {
635 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
636 flimflam::kTypeWifi,
637 wifi_->address().c_str(),
638 hex_ssid_.c_str(),
639 mode_.c_str(),
640 security.c_str()));
641}
642
Gaurav Shah10109f22011-11-11 20:16:22 -0800643void WiFiService::set_eap(const EapCredentials &eap) {
644 Service::set_eap(eap);
645 UpdateConnectable();
646}
647
Gaurav Shah29d68882012-01-30 19:06:42 -0800648bool WiFiService::Is8021x() const {
649 if (security_ == flimflam::kSecurity8021x)
650 return true;
651
652 // Dynamic WEP + 802.1x.
653 if (security_ == flimflam::kSecurityWep &&
654 GetEAPKeyManagement() == "IEEE8021X")
655 return true;
656 return false;
657}
658
Gaurav Shah10109f22011-11-11 20:16:22 -0800659void WiFiService::Populate8021xProperties(
660 std::map<string, DBus::Variant> *params) {
661 typedef std::pair<const char *, const char *> KeyVal;
662 KeyVal propertyvals[] = {
663 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
664 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
665 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
666 eap().inner_eap.c_str()),
667 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
668 eap().anonymous_identity.c_str()),
669 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
670 eap().client_cert.c_str()),
671 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
672 eap().private_key.c_str()),
673 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
674 eap().private_key_password.c_str()),
675 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
676 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
677 eap().password.c_str()),
678 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
679 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
680 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
681 eap().ca_cert_id.c_str()),
682 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
683 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
684 // crosbug.com/25663
685 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
686 };
687
688 DBus::MessageIter writer;
689 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
690 if (strlen(propertyvals[i].second) > 0) {
691 (*params)[propertyvals[i].first].writer().
692 append_string(propertyvals[i].second);
693 }
694 }
695}
696
mukesh agrawalb54601c2011-06-07 17:39:22 -0700697} // namespace shill