blob: b4ef8bbfd6d10aad5d973ffc9e90fa23eba3b497 [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 agrawald835b202011-10-07 15:26:47 -070017#include <glib.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070018
19#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";
Paul Stewartd08f4432011-11-04 07:48:20 -070042
mukesh agrawalb54601c2011-06-07 17:39:22 -070043WiFiService::WiFiService(ControlInterface *control_interface,
44 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080045 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070046 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070047 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080048 const vector<uint8_t> &ssid,
49 const string &mode,
50 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080051 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080052 : Service(control_interface, dispatcher, metrics, manager,
53 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070054 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070055 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070056 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080057 hidden_ssid_(hidden_ssid),
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_);
Thieu Lef7709452011-11-15 01:13:19 +000063 HelpRegisterDerivedString(store,
64 flimflam::kPassphraseProperty,
65 NULL,
66 &WiFiService::SetPassphrase);
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());
78 if (SanitizeSSID(&ssid_string)) {
79 // 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);
173}
174
175void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
176 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
177 DCHECK(i != endpoints_.end());
178 if (i == endpoints_.end()) {
179 LOG(WARNING) << "In " << __func__ << "(): "
180 << "ignorning non-existent endpoint "
181 << endpoint->bssid_string();
182 return;
183 }
184 endpoints_.erase(i);
185}
186
Chris Masone6515aab2011-10-12 16:19:09 -0700187string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700188 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700189}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700190
mukesh agrawal1a056262011-10-05 14:36:54 -0700191void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
192 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000193 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700194 } else if (security_ == flimflam::kSecurityPsk ||
195 security_ == flimflam::kSecurityWpa ||
196 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000197 ValidateWPAPassphrase(passphrase, error);
198 } else {
199 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700200 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000201
Paul Stewart2706aaf2011-12-14 16:44:04 -0800202 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000203 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800204 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000205
206 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700207}
208
Paul Stewartd08f4432011-11-04 07:48:20 -0700209bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
210 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
211 storage->ContainsGroup(GetSpecificStorageIdentifier());
212}
213
Paul Stewarta41e38d2011-11-11 07:47:29 -0800214bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800215 // WiFi Services should be displayed only if they are in range (have
216 // endpoints that have shown up in a scan) or if the service is actively
217 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000218 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800219}
220
Paul Stewartd08f4432011-11-04 07:48:20 -0700221bool WiFiService::Load(StoreInterface *storage) {
222 // First find out which storage identifier is available in priority order
223 // of specific, generic.
224 string id = GetSpecificStorageIdentifier();
225 if (!storage->ContainsGroup(id)) {
226 id = GetGenericStorageIdentifier();
227 if (!storage->ContainsGroup(id)) {
228 LOG(WARNING) << "Service is not available in the persistent store: "
229 << id;
230 return false;
231 }
232 }
233
234 // Set our storage identifier to match the storage name in the Profile.
235 storage_identifier_ = id;
236
237 // Load properties common to all Services.
238 if (!Service::Load(storage)) {
239 return false;
240 }
241
242 // Load properties specific to WiFi services.
243 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000244
Paul Stewart2706aaf2011-12-14 16:44:04 -0800245 // NB: mode, security and ssid parameters are never read in from
246 // Load() as they are provided from the scan.
247
248 string passphrase;
249 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
250 Error error;
251 SetPassphrase(passphrase, &error);
252 if (!error.IsSuccess()) {
253 LOG(ERROR) << "Passphrase could not be set: "
254 << Error::GetName(error.type());
255 }
256 }
257
Paul Stewartd08f4432011-11-04 07:48:20 -0700258 return true;
259}
260
261bool WiFiService::Save(StoreInterface *storage) {
262 // Save properties common to all Services.
263 if (!Service::Save(storage)) {
264 return false;
265 }
266
267 // Save properties specific to WiFi services.
268 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800269 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
270 storage->SetString(id, kStorageMode, mode_);
271 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
272 storage->SetString(id, kStorageSecurity, security_);
273 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000274
275 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700276 return true;
277}
278
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800279void WiFiService::Unload() {
280 Service::Unload();
281 hidden_ssid_ = false;
282 passphrase_ = "";
283 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800284 if (security_ == flimflam::kSecurity8021x) {
285 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
286 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
287 // Make sure that these are cleared when credentials for networks
288 // of this type goes away.
289 //
290 // When wpa_supplicant gains the ability, do this credential
291 // clearing on a per-service basis. Also do this whenever the credentials
292 // for a service changes. crosbug.com/25670
293 wifi_->ClearCachedCredentials();
294 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800295}
296
Paul Stewart6ab23a92011-11-09 17:17:47 -0800297bool WiFiService::IsSecurityMatch(const string &security) const {
298 return GetSecurityClass(security) == GetSecurityClass(security_);
299}
300
Thieu Le48e6d6d2011-12-06 00:40:27 +0000301void WiFiService::InitializeCustomMetrics() const {
302 string histogram = metrics()->GetFullMetricName(
303 Metrics::kMetricTimeToJoinMilliseconds,
304 technology());
305 metrics()->AddServiceStateTransitionTimer(this,
306 histogram,
307 Service::kStateAssociating,
308 Service::kStateConfiguring);
309}
310
311void WiFiService::SendPostReadyStateMetrics() const {
312 // TODO(thieule): Send physical mode and security metrics.
313 // crosbug.com/24441
314 metrics()->SendEnumToUMA(
315 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
316 technology()),
317 Metrics::WiFiFrequencyToChannel(frequency_),
318 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000319
320 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
321 metrics()->SendEnumToUMA(
322 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
323 technology()),
324 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
325 Metrics::kWiFiNetworkPhyModeMax);
326
327 Metrics::WiFiSecurity security_uma =
328 Metrics::WiFiSecurityStringToEnum(security_);
329 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
330 metrics()->SendEnumToUMA(
331 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
332 technology()),
333 security_uma,
334 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000335}
336
mukesh agrawal32399322011-09-01 10:53:43 -0700337// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000338void WiFiService::HelpRegisterDerivedString(
339 PropertyStore *store,
340 const std::string &name,
341 std::string(WiFiService::*get)(Error *),
342 void(WiFiService::*set)(const std::string&, Error *)) {
343 store->RegisterDerivedString(
344 name,
345 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
346}
347
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700348void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700349 std::map<string, DBus::Variant> params;
350 DBus::MessageIter writer;
351
352 params[wpa_supplicant::kNetworkPropertyMode].writer().
353 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
354
Gaurav Shah29d68882012-01-30 19:06:42 -0800355 if (Is8021x()) {
356 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800357 if (GetEAPKeyManagement().empty())
358 SetEAPKeyManagement("WPA-EAP");
359 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700360 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800361 const string psk_proto = StringPrintf("%s %s",
362 wpa_supplicant::kSecurityModeWPA,
363 wpa_supplicant::kSecurityModeRSN);
364 params[wpa_supplicant::kPropertySecurityProtocol].writer().
365 append_string(psk_proto.c_str());
366 params[wpa_supplicant::kPropertyPreSharedKey].writer().
367 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700368 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700369 params[wpa_supplicant::kPropertySecurityProtocol].writer().
370 append_string(wpa_supplicant::kSecurityModeRSN);
371 params[wpa_supplicant::kPropertyPreSharedKey].writer().
372 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700373 } else if (security_ == flimflam::kSecurityWpa) {
374 params[wpa_supplicant::kPropertySecurityProtocol].writer().
375 append_string(wpa_supplicant::kSecurityModeWPA);
376 params[wpa_supplicant::kPropertyPreSharedKey].writer().
377 append_string(passphrase_.c_str());
378 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000379 params[wpa_supplicant::kPropertyAuthAlg].writer().
380 append_string(wpa_supplicant::kSecurityAuthAlg);
381 Error error;
382 int key_index;
383 std::vector<uint8> password_bytes;
384 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
385 writer = params[wpa_supplicant::kPropertyWEPKey +
386 base::IntToString(key_index)].writer();
387 writer << password_bytes;
388 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
389 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700390 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800391 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700392 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800393 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700394 }
395
Gaurav Shah10109f22011-11-11 20:16:22 -0800396 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700397 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800398
399 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700400 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
401 writer << ssid_;
402
403 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700404}
405
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000406void WiFiService::DisconnectTask() {
407 wifi_->DisconnectFrom(this);
408}
409
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800410string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700411 return wifi_->GetRpcIdentifier();
412}
413
mukesh agrawal29c13a12011-11-24 00:09:19 +0000414void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800415 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000416 if (security_ == flimflam::kSecurityNone) {
417 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800418 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800419 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800420 } else if (Is8021x()) {
421 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000422 } else if (security_ == flimflam::kSecurityWep ||
423 security_ == flimflam::kSecurityWpa ||
424 security_ == flimflam::kSecurityPsk ||
425 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800426 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800427 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000428 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800429 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000430}
431
mukesh agrawal1a056262011-10-05 14:36:54 -0700432// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000433void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
434 Error *error) {
435 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700436}
437
438// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000439void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
440 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700441 unsigned int length = passphrase.length();
442 vector<uint8> passphrase_bytes;
443
444 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
445 if (length != IEEE_80211::kWPAHexLen &&
446 (length < IEEE_80211::kWPAAsciiMinLen ||
447 length > IEEE_80211::kWPAAsciiMaxLen)) {
448 error->Populate(Error::kInvalidPassphrase);
449 }
450 } else {
451 if (length < IEEE_80211::kWPAAsciiMinLen ||
452 length > IEEE_80211::kWPAAsciiMaxLen) {
453 error->Populate(Error::kInvalidPassphrase);
454 }
455 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000456}
mukesh agrawal1a056262011-10-05 14:36:54 -0700457
Thieu Lef4cbda92011-11-10 23:41:24 +0000458// static
459void WiFiService::ParseWEPPassphrase(const string &passphrase,
460 int *key_index,
461 std::vector<uint8> *password_bytes,
462 Error *error) {
463 unsigned int length = passphrase.length();
464 int key_index_local;
465 std::string password_text;
466 bool is_hex = false;
467
468 switch (length) {
469 case IEEE_80211::kWEP40AsciiLen:
470 case IEEE_80211::kWEP104AsciiLen:
471 key_index_local = 0;
472 password_text = passphrase;
473 break;
474 case IEEE_80211::kWEP40AsciiLen + 2:
475 case IEEE_80211::kWEP104AsciiLen + 2:
476 if (CheckWEPKeyIndex(passphrase, error)) {
477 base::StringToInt(passphrase.substr(0,1), &key_index_local);
478 password_text = passphrase.substr(2);
479 }
480 break;
481 case IEEE_80211::kWEP40HexLen:
482 case IEEE_80211::kWEP104HexLen:
483 if (CheckWEPIsHex(passphrase, error)) {
484 key_index_local = 0;
485 password_text = passphrase;
486 is_hex = true;
487 }
488 break;
489 case IEEE_80211::kWEP40HexLen + 2:
490 case IEEE_80211::kWEP104HexLen + 2:
491 if(CheckWEPKeyIndex(passphrase, error) &&
492 CheckWEPIsHex(passphrase.substr(2), error)) {
493 base::StringToInt(passphrase.substr(0,1), &key_index_local);
494 password_text = passphrase.substr(2);
495 is_hex = true;
496 } else if (CheckWEPPrefix(passphrase, error) &&
497 CheckWEPIsHex(passphrase.substr(2), error)) {
498 key_index_local = 0;
499 password_text = passphrase.substr(2);
500 is_hex = true;
501 }
502 break;
503 case IEEE_80211::kWEP40HexLen + 4:
504 case IEEE_80211::kWEP104HexLen + 4:
505 if (CheckWEPKeyIndex(passphrase, error) &&
506 CheckWEPPrefix(passphrase.substr(2), error) &&
507 CheckWEPIsHex(passphrase.substr(4), error)) {
508 base::StringToInt(passphrase.substr(0,1), &key_index_local);
509 password_text = passphrase.substr(4);
510 is_hex = true;
511 }
512 break;
513 default:
514 error->Populate(Error::kInvalidPassphrase);
515 break;
516 }
517
mukesh agrawal1a056262011-10-05 14:36:54 -0700518 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000519 if (key_index)
520 *key_index = key_index_local;
521 if (password_bytes) {
522 if (is_hex)
523 base::HexStringToBytes(password_text, password_bytes);
524 else
525 password_bytes->insert(password_bytes->end(),
526 password_text.begin(),
527 password_text.end());
528 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700529 }
530}
531
532// static
533bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
534 vector<uint8> passphrase_bytes;
535 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
536 return true;
537 } else {
538 error->Populate(Error::kInvalidPassphrase);
539 return false;
540 }
541}
542
543// static
544bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
545 if (StartsWithASCII(passphrase, "0:", false) ||
546 StartsWithASCII(passphrase, "1:", false) ||
547 StartsWithASCII(passphrase, "2:", false) ||
548 StartsWithASCII(passphrase, "3:", false)) {
549 return true;
550 } else {
551 error->Populate(Error::kInvalidPassphrase);
552 return false;
553 }
554}
555
556// static
557bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
558 if (StartsWithASCII(passphrase, "0x", false)) {
559 return true;
560 } else {
561 error->Populate(Error::kInvalidPassphrase);
562 return false;
563 }
564}
565
Paul Stewart6ab23a92011-11-09 17:17:47 -0800566// static
mukesh agrawald835b202011-10-07 15:26:47 -0700567bool WiFiService::SanitizeSSID(string *ssid) {
568 CHECK(ssid);
569
570 size_t ssid_len = ssid->length();
571 size_t i;
572 bool changed = false;
573
574 for (i=0; i < ssid_len; ++i) {
575 if (!g_ascii_isprint((*ssid)[i])) {
576 (*ssid)[i] = '?';
577 changed = true;
578 }
579 }
580
581 return changed;
582}
583
Paul Stewart6ab23a92011-11-09 17:17:47 -0800584// static
585string WiFiService::GetSecurityClass(const string &security) {
586 if (security == flimflam::kSecurityRsn ||
587 security == flimflam::kSecurityWpa) {
588 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700589 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800590 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700591 }
592}
593
Paul Stewarta41e38d2011-11-11 07:47:29 -0800594// static
595bool WiFiService::ParseStorageIdentifier(const string &storage_name,
596 string *address,
597 string *mode,
598 string *security) {
599 vector<string> wifi_parts;
600 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800601 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
602 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800603 return false;
604 }
605 *address = wifi_parts[1];
606 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800607 if (wifi_parts.size() == 5) {
608 *security = wifi_parts[4];
609 } else {
610 // Account for security type "802_1x" which got split up above.
611 *security = wifi_parts[4] + "_" + wifi_parts[5];
612 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800613 return true;
614}
615
Paul Stewart6ab23a92011-11-09 17:17:47 -0800616string WiFiService::GetGenericStorageIdentifier() const {
617 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
618}
619
Paul Stewartd08f4432011-11-04 07:48:20 -0700620string WiFiService::GetSpecificStorageIdentifier() const {
621 return GetStorageIdentifierForSecurity(security_);
622}
623
624string WiFiService::GetStorageIdentifierForSecurity(
625 const string &security) const {
626 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
627 flimflam::kTypeWifi,
628 wifi_->address().c_str(),
629 hex_ssid_.c_str(),
630 mode_.c_str(),
631 security.c_str()));
632}
633
Gaurav Shah10109f22011-11-11 20:16:22 -0800634void WiFiService::set_eap(const EapCredentials &eap) {
635 Service::set_eap(eap);
636 UpdateConnectable();
637}
638
Gaurav Shah29d68882012-01-30 19:06:42 -0800639bool WiFiService::Is8021x() const {
640 if (security_ == flimflam::kSecurity8021x)
641 return true;
642
643 // Dynamic WEP + 802.1x.
644 if (security_ == flimflam::kSecurityWep &&
645 GetEAPKeyManagement() == "IEEE8021X")
646 return true;
647 return false;
648}
649
Gaurav Shah10109f22011-11-11 20:16:22 -0800650void WiFiService::Populate8021xProperties(
651 std::map<string, DBus::Variant> *params) {
652 typedef std::pair<const char *, const char *> KeyVal;
653 KeyVal propertyvals[] = {
654 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
655 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
656 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
657 eap().inner_eap.c_str()),
658 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
659 eap().anonymous_identity.c_str()),
660 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
661 eap().client_cert.c_str()),
662 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
663 eap().private_key.c_str()),
664 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
665 eap().private_key_password.c_str()),
666 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
667 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
668 eap().password.c_str()),
669 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
670 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
671 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
672 eap().ca_cert_id.c_str()),
673 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
674 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
675 // crosbug.com/25663
676 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
677 };
678
679 DBus::MessageIter writer;
680 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
681 if (strlen(propertyvals[i].second) > 0) {
682 (*params)[propertyvals[i].first].writer().
683 append_string(propertyvals[i].second);
684 }
685 }
686}
687
mukesh agrawalb54601c2011-06-07 17:39:22 -0700688} // namespace shill