blob: 6ef3dbbe630fd0edf919d850916f39edb125462b [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_);
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());
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
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800209// ClearPassphrase is separate from SetPassphrase, because the default
210// value for |passphrase_| would not pass validation.
211void WiFiService::ClearPassphrase(Error */*error*/) {
212 passphrase_.clear();
213 UpdateConnectable();
214}
215
Paul Stewartd08f4432011-11-04 07:48:20 -0700216bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
217 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
218 storage->ContainsGroup(GetSpecificStorageIdentifier());
219}
220
Paul Stewarta41e38d2011-11-11 07:47:29 -0800221bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800222 // WiFi Services should be displayed only if they are in range (have
223 // endpoints that have shown up in a scan) or if the service is actively
224 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000225 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800226}
227
Paul Stewartd08f4432011-11-04 07:48:20 -0700228bool WiFiService::Load(StoreInterface *storage) {
229 // First find out which storage identifier is available in priority order
230 // of specific, generic.
231 string id = GetSpecificStorageIdentifier();
232 if (!storage->ContainsGroup(id)) {
233 id = GetGenericStorageIdentifier();
234 if (!storage->ContainsGroup(id)) {
235 LOG(WARNING) << "Service is not available in the persistent store: "
236 << id;
237 return false;
238 }
239 }
240
241 // Set our storage identifier to match the storage name in the Profile.
242 storage_identifier_ = id;
243
244 // Load properties common to all Services.
245 if (!Service::Load(storage)) {
246 return false;
247 }
248
249 // Load properties specific to WiFi services.
250 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000251
Paul Stewart2706aaf2011-12-14 16:44:04 -0800252 // NB: mode, security and ssid parameters are never read in from
253 // Load() as they are provided from the scan.
254
255 string passphrase;
256 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
257 Error error;
258 SetPassphrase(passphrase, &error);
259 if (!error.IsSuccess()) {
260 LOG(ERROR) << "Passphrase could not be set: "
261 << Error::GetName(error.type());
262 }
263 }
264
Paul Stewartd08f4432011-11-04 07:48:20 -0700265 return true;
266}
267
268bool WiFiService::Save(StoreInterface *storage) {
269 // Save properties common to all Services.
270 if (!Service::Save(storage)) {
271 return false;
272 }
273
274 // Save properties specific to WiFi services.
275 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800276 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
277 storage->SetString(id, kStorageMode, mode_);
278 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
279 storage->SetString(id, kStorageSecurity, security_);
280 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000281
282 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700283 return true;
284}
285
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800286void WiFiService::Unload() {
287 Service::Unload();
288 hidden_ssid_ = false;
289 passphrase_ = "";
290 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800291 if (security_ == flimflam::kSecurity8021x) {
292 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
293 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
294 // Make sure that these are cleared when credentials for networks
295 // of this type goes away.
296 //
297 // When wpa_supplicant gains the ability, do this credential
298 // clearing on a per-service basis. Also do this whenever the credentials
299 // for a service changes. crosbug.com/25670
300 wifi_->ClearCachedCredentials();
301 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800302}
303
Paul Stewart6ab23a92011-11-09 17:17:47 -0800304bool WiFiService::IsSecurityMatch(const string &security) const {
305 return GetSecurityClass(security) == GetSecurityClass(security_);
306}
307
Thieu Le48e6d6d2011-12-06 00:40:27 +0000308void WiFiService::InitializeCustomMetrics() const {
309 string histogram = metrics()->GetFullMetricName(
310 Metrics::kMetricTimeToJoinMilliseconds,
311 technology());
312 metrics()->AddServiceStateTransitionTimer(this,
313 histogram,
314 Service::kStateAssociating,
315 Service::kStateConfiguring);
316}
317
318void WiFiService::SendPostReadyStateMetrics() const {
319 // TODO(thieule): Send physical mode and security metrics.
320 // crosbug.com/24441
321 metrics()->SendEnumToUMA(
322 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
323 technology()),
324 Metrics::WiFiFrequencyToChannel(frequency_),
325 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000326
327 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
328 metrics()->SendEnumToUMA(
329 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
330 technology()),
331 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
332 Metrics::kWiFiNetworkPhyModeMax);
333
334 Metrics::WiFiSecurity security_uma =
335 Metrics::WiFiSecurityStringToEnum(security_);
336 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
337 metrics()->SendEnumToUMA(
338 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
339 technology()),
340 security_uma,
341 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000342}
343
mukesh agrawal32399322011-09-01 10:53:43 -0700344// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800345void WiFiService::HelpRegisterWriteOnlyDerivedString(
346 const string &name,
347 void(WiFiService::*set)(const string &, Error *),
348 void(WiFiService::*clear)(Error *),
349 const string *default_value) {
350 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000351 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800352 StringAccessor(
353 new CustomWriteOnlyAccessor<WiFiService, string>(
354 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000355}
356
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700357void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700358 std::map<string, DBus::Variant> params;
359 DBus::MessageIter writer;
360
361 params[wpa_supplicant::kNetworkPropertyMode].writer().
362 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
363
Gaurav Shah29d68882012-01-30 19:06:42 -0800364 if (Is8021x()) {
365 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800366 if (GetEAPKeyManagement().empty())
367 SetEAPKeyManagement("WPA-EAP");
368 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700369 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800370 const string psk_proto = StringPrintf("%s %s",
371 wpa_supplicant::kSecurityModeWPA,
372 wpa_supplicant::kSecurityModeRSN);
373 params[wpa_supplicant::kPropertySecurityProtocol].writer().
374 append_string(psk_proto.c_str());
375 params[wpa_supplicant::kPropertyPreSharedKey].writer().
376 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700377 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700378 params[wpa_supplicant::kPropertySecurityProtocol].writer().
379 append_string(wpa_supplicant::kSecurityModeRSN);
380 params[wpa_supplicant::kPropertyPreSharedKey].writer().
381 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700382 } else if (security_ == flimflam::kSecurityWpa) {
383 params[wpa_supplicant::kPropertySecurityProtocol].writer().
384 append_string(wpa_supplicant::kSecurityModeWPA);
385 params[wpa_supplicant::kPropertyPreSharedKey].writer().
386 append_string(passphrase_.c_str());
387 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000388 params[wpa_supplicant::kPropertyAuthAlg].writer().
389 append_string(wpa_supplicant::kSecurityAuthAlg);
390 Error error;
391 int key_index;
392 std::vector<uint8> password_bytes;
393 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
394 writer = params[wpa_supplicant::kPropertyWEPKey +
395 base::IntToString(key_index)].writer();
396 writer << password_bytes;
397 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
398 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700399 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800400 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700401 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800402 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700403 }
404
Gaurav Shah10109f22011-11-11 20:16:22 -0800405 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700406 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800407
408 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700409 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
410 writer << ssid_;
411
412 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700413}
414
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000415void WiFiService::DisconnectTask() {
416 wifi_->DisconnectFrom(this);
417}
418
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800419string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700420 return wifi_->GetRpcIdentifier();
421}
422
mukesh agrawal29c13a12011-11-24 00:09:19 +0000423void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800424 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000425 if (security_ == flimflam::kSecurityNone) {
426 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800427 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800428 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800429 } else if (Is8021x()) {
430 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000431 } else if (security_ == flimflam::kSecurityWep ||
432 security_ == flimflam::kSecurityWpa ||
433 security_ == flimflam::kSecurityPsk ||
434 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800435 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800436 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000437 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800438 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000439}
440
mukesh agrawal1a056262011-10-05 14:36:54 -0700441// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000442void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
443 Error *error) {
444 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700445}
446
447// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000448void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
449 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700450 unsigned int length = passphrase.length();
451 vector<uint8> passphrase_bytes;
452
453 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
454 if (length != IEEE_80211::kWPAHexLen &&
455 (length < IEEE_80211::kWPAAsciiMinLen ||
456 length > IEEE_80211::kWPAAsciiMaxLen)) {
457 error->Populate(Error::kInvalidPassphrase);
458 }
459 } else {
460 if (length < IEEE_80211::kWPAAsciiMinLen ||
461 length > IEEE_80211::kWPAAsciiMaxLen) {
462 error->Populate(Error::kInvalidPassphrase);
463 }
464 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000465}
mukesh agrawal1a056262011-10-05 14:36:54 -0700466
Thieu Lef4cbda92011-11-10 23:41:24 +0000467// static
468void WiFiService::ParseWEPPassphrase(const string &passphrase,
469 int *key_index,
470 std::vector<uint8> *password_bytes,
471 Error *error) {
472 unsigned int length = passphrase.length();
473 int key_index_local;
474 std::string password_text;
475 bool is_hex = false;
476
477 switch (length) {
478 case IEEE_80211::kWEP40AsciiLen:
479 case IEEE_80211::kWEP104AsciiLen:
480 key_index_local = 0;
481 password_text = passphrase;
482 break;
483 case IEEE_80211::kWEP40AsciiLen + 2:
484 case IEEE_80211::kWEP104AsciiLen + 2:
485 if (CheckWEPKeyIndex(passphrase, error)) {
486 base::StringToInt(passphrase.substr(0,1), &key_index_local);
487 password_text = passphrase.substr(2);
488 }
489 break;
490 case IEEE_80211::kWEP40HexLen:
491 case IEEE_80211::kWEP104HexLen:
492 if (CheckWEPIsHex(passphrase, error)) {
493 key_index_local = 0;
494 password_text = passphrase;
495 is_hex = true;
496 }
497 break;
498 case IEEE_80211::kWEP40HexLen + 2:
499 case IEEE_80211::kWEP104HexLen + 2:
500 if(CheckWEPKeyIndex(passphrase, error) &&
501 CheckWEPIsHex(passphrase.substr(2), error)) {
502 base::StringToInt(passphrase.substr(0,1), &key_index_local);
503 password_text = passphrase.substr(2);
504 is_hex = true;
505 } else if (CheckWEPPrefix(passphrase, error) &&
506 CheckWEPIsHex(passphrase.substr(2), error)) {
507 key_index_local = 0;
508 password_text = passphrase.substr(2);
509 is_hex = true;
510 }
511 break;
512 case IEEE_80211::kWEP40HexLen + 4:
513 case IEEE_80211::kWEP104HexLen + 4:
514 if (CheckWEPKeyIndex(passphrase, error) &&
515 CheckWEPPrefix(passphrase.substr(2), error) &&
516 CheckWEPIsHex(passphrase.substr(4), error)) {
517 base::StringToInt(passphrase.substr(0,1), &key_index_local);
518 password_text = passphrase.substr(4);
519 is_hex = true;
520 }
521 break;
522 default:
523 error->Populate(Error::kInvalidPassphrase);
524 break;
525 }
526
mukesh agrawal1a056262011-10-05 14:36:54 -0700527 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000528 if (key_index)
529 *key_index = key_index_local;
530 if (password_bytes) {
531 if (is_hex)
532 base::HexStringToBytes(password_text, password_bytes);
533 else
534 password_bytes->insert(password_bytes->end(),
535 password_text.begin(),
536 password_text.end());
537 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700538 }
539}
540
541// static
542bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
543 vector<uint8> passphrase_bytes;
544 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
545 return true;
546 } else {
547 error->Populate(Error::kInvalidPassphrase);
548 return false;
549 }
550}
551
552// static
553bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
554 if (StartsWithASCII(passphrase, "0:", false) ||
555 StartsWithASCII(passphrase, "1:", false) ||
556 StartsWithASCII(passphrase, "2:", false) ||
557 StartsWithASCII(passphrase, "3:", false)) {
558 return true;
559 } else {
560 error->Populate(Error::kInvalidPassphrase);
561 return false;
562 }
563}
564
565// static
566bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
567 if (StartsWithASCII(passphrase, "0x", false)) {
568 return true;
569 } else {
570 error->Populate(Error::kInvalidPassphrase);
571 return false;
572 }
573}
574
Paul Stewart6ab23a92011-11-09 17:17:47 -0800575// static
mukesh agrawald835b202011-10-07 15:26:47 -0700576bool WiFiService::SanitizeSSID(string *ssid) {
577 CHECK(ssid);
578
579 size_t ssid_len = ssid->length();
580 size_t i;
581 bool changed = false;
582
583 for (i=0; i < ssid_len; ++i) {
584 if (!g_ascii_isprint((*ssid)[i])) {
585 (*ssid)[i] = '?';
586 changed = true;
587 }
588 }
589
590 return changed;
591}
592
Paul Stewart6ab23a92011-11-09 17:17:47 -0800593// static
594string 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