blob: aa59bf63f21a5f7b65c9f413701f8495f668d027 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawalb54601c2011-06-07 17:39:22 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/wifi_service.h"
6
7#include <string>
Gaurav Shah10109f22011-11-11 20:16:22 -08008#include <utility>
mukesh agrawalb54601c2011-06-07 17:39:22 -07009
10#include <base/logging.h>
Chris Masone34af2182011-08-22 11:59:36 -070011#include <base/stringprintf.h>
12#include <base/string_number_conversions.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080013#include <base/string_split.h>
Chris Masone34af2182011-08-22 11:59:36 -070014#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070016#include <dbus/dbus.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070017
mukesh agrawale1d90e92012-02-15 17:36:08 -080018#include "shill/adaptor_interfaces.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070019#include "shill/control_interface.h"
20#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070021#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070022#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070023#include "shill/ieee80211.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000024#include "shill/metrics.h"
Thieu Lef7709452011-11-15 01:13:19 +000025#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070026#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070027#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070028#include "shill/wifi_endpoint.h"
29#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070030
mukesh agrawal261daca2011-12-02 18:56:56 +000031using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070032using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070033using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070034
35namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070036
mukesh agrawalbf14e942012-03-02 14:36:34 -080037const char WiFiService::kAutoConnBusy[] = "busy";
38const char WiFiService::kAutoConnNoEndpoint[] = "no endpoints";
39
Paul Stewartd08f4432011-11-04 07:48:20 -070040const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080041const char WiFiService::kStorageMode[] = "WiFi.Mode";
42const char WiFiService::kStoragePassphrase[] = "Passphrase";
43const char WiFiService::kStorageSecurity[] = "WiFi.Security";
44const char WiFiService::kStorageSSID[] = "SSID";
mukesh agrawale1d90e92012-02-15 17:36:08 -080045bool WiFiService::logged_signal_warning = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070046
mukesh agrawalb54601c2011-06-07 17:39:22 -070047WiFiService::WiFiService(ControlInterface *control_interface,
48 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080049 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070050 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070051 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080052 const vector<uint8_t> &ssid,
53 const string &mode,
54 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080055 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080056 : Service(control_interface, dispatcher, metrics, manager,
57 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070058 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070059 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070060 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080061 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000062 frequency_(0),
Paul Stewart20088d82012-02-16 06:58:55 -080063 physical_mode_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070064 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070065 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070066 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070067 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080068 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
69 &WiFiService::SetPassphrase,
70 &WiFiService::ClearPassphrase,
71 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070072 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
73 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070074
Paul Stewartac4ac002011-08-26 12:04:26 -070075 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
76 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
77 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
78 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070079
mukesh agrawald835b202011-10-07 15:26:47 -070080 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
81 string ssid_string(
82 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080083 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070084 // WifiHexSsid property should only be present if Name property
85 // has been munged.
86 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
87 }
88 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070089
mukesh agrawal6e277772011-09-29 15:04:23 -070090 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
91 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080092 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080093 // Passphrases are not mandatory for 802.1X.
94 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070095 } else if (security_ == flimflam::kSecurityPsk) {
96 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070097 } else if (security_ == flimflam::kSecurityRsn) {
98 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070099 } else if (security_ == flimflam::kSecurityWpa) {
100 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -0700101 } else if (security_ == flimflam::kSecurityWep) {
102 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700103 } else if (security_ == flimflam::kSecurityNone) {
104 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700105 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800106 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700107 }
108
Paul Stewartd08f4432011-11-04 07:48:20 -0700109 // Until we know better (at Profile load time), use the generic name.
110 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000111 UpdateConnectable();
Paul Stewartcb59fed2012-03-21 21:14:46 -0700112
113 IgnoreParameterForConfigure(flimflam::kModeProperty);
114 IgnoreParameterForConfigure(flimflam::kSSIDProperty);
115 IgnoreParameterForConfigure(flimflam::kSecurityProperty);
116 IgnoreParameterForConfigure(flimflam::kPassphraseProperty);
117 IgnoreParameterForConfigure(flimflam::kWifiHiddenSsid);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700118}
119
120WiFiService::~WiFiService() {
121 LOG(INFO) << __func__;
122}
123
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000124void WiFiService::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800125 const char *reason;
126 if (IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000127 // Execute immediately, for two reasons:
128 //
129 // 1. We need IsAutoConnectable to return the correct value for
130 // other WiFiServices, and that depends on WiFi's state.
131 //
132 // 2. We should probably limit the extent to which we queue up
133 // actions (such as AutoConnect) which depend on current state.
134 // If we queued AutoConnects, we could build a long queue of
135 // useless work (one AutoConnect per Service), which blocks
136 // more timely work.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500137 Connect(NULL);
mukesh agrawal592516d2012-01-12 14:01:00 -0800138 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800139 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
140 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000141 }
142}
143
Paul Stewart22aa71b2011-09-16 12:15:11 -0700144bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
145 return wifi_->TechnologyIs(type);
146}
147
mukesh agrawalbf14e942012-03-02 14:36:34 -0800148bool WiFiService::IsAutoConnectable(const char **reason) const {
149 if (!Service::IsAutoConnectable(reason)) {
150 return false;
151 }
152
153 // Only auto-connect to Services which have visible Endpoints.
154 // (Needed because hidden Services may remain registered with
155 // Manager even without visible Endpoints.)
156 if (!HasEndpoints()) {
157 *reason = kAutoConnNoEndpoint;
158 return false;
159 }
160
161 // Do not preempt an existing connection (whether pending, or
162 // connected, and whether to this service, or another).
163 if (!wifi_->IsIdle()) {
164 *reason = kAutoConnBusy;
165 return false;
166 }
167
168 return true;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000169}
170
171bool WiFiService::IsConnecting() const {
172 // WiFi does not move us into the associating state until it gets
173 // feedback from wpa_supplicant. So, to answer whether or
174 // not we're connecting, we consult with |wifi_|.
175 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800176}
177
mukesh agrawale1d90e92012-02-15 17:36:08 -0800178void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000179 DCHECK(endpoint->ssid() == ssid());
180 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800181 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000182}
183
mukesh agrawale1d90e92012-02-15 17:36:08 -0800184void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000185 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
186 DCHECK(i != endpoints_.end());
187 if (i == endpoints_.end()) {
188 LOG(WARNING) << "In " << __func__ << "(): "
189 << "ignorning non-existent endpoint "
190 << endpoint->bssid_string();
191 return;
192 }
193 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800194 if (current_endpoint_ == endpoint) {
195 current_endpoint_ = NULL;
196 }
197 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000198}
199
mukesh agrawale1d90e92012-02-15 17:36:08 -0800200void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
201 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
202 current_endpoint_ = endpoint;
203 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000204}
205
mukesh agrawalb20776f2012-02-10 16:00:36 -0800206void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
207 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800208 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800209}
210
Chris Masone6515aab2011-10-12 16:19:09 -0700211string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700212 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700213}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700214
mukesh agrawal1a056262011-10-05 14:36:54 -0700215void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
216 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000217 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700218 } else if (security_ == flimflam::kSecurityPsk ||
219 security_ == flimflam::kSecurityWpa ||
220 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000221 ValidateWPAPassphrase(passphrase, error);
222 } else {
223 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700224 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000225
Paul Stewart2706aaf2011-12-14 16:44:04 -0800226 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000227 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800228 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000229
230 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700231}
232
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800233// ClearPassphrase is separate from SetPassphrase, because the default
234// value for |passphrase_| would not pass validation.
235void WiFiService::ClearPassphrase(Error */*error*/) {
236 passphrase_.clear();
237 UpdateConnectable();
238}
239
Paul Stewartd08f4432011-11-04 07:48:20 -0700240bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
241 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
242 storage->ContainsGroup(GetSpecificStorageIdentifier());
243}
244
Paul Stewarta41e38d2011-11-11 07:47:29 -0800245bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800246 // WiFi Services should be displayed only if they are in range (have
247 // endpoints that have shown up in a scan) or if the service is actively
248 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000249 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800250}
251
Paul Stewartd08f4432011-11-04 07:48:20 -0700252bool WiFiService::Load(StoreInterface *storage) {
253 // First find out which storage identifier is available in priority order
254 // of specific, generic.
255 string id = GetSpecificStorageIdentifier();
256 if (!storage->ContainsGroup(id)) {
257 id = GetGenericStorageIdentifier();
258 if (!storage->ContainsGroup(id)) {
259 LOG(WARNING) << "Service is not available in the persistent store: "
260 << id;
261 return false;
262 }
263 }
264
265 // Set our storage identifier to match the storage name in the Profile.
266 storage_identifier_ = id;
267
268 // Load properties common to all Services.
269 if (!Service::Load(storage)) {
270 return false;
271 }
272
273 // Load properties specific to WiFi services.
274 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000275
Paul Stewart2706aaf2011-12-14 16:44:04 -0800276 // NB: mode, security and ssid parameters are never read in from
277 // Load() as they are provided from the scan.
278
279 string passphrase;
280 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
281 Error error;
282 SetPassphrase(passphrase, &error);
283 if (!error.IsSuccess()) {
284 LOG(ERROR) << "Passphrase could not be set: "
285 << Error::GetName(error.type());
286 }
287 }
288
Paul Stewartd08f4432011-11-04 07:48:20 -0700289 return true;
290}
291
292bool WiFiService::Save(StoreInterface *storage) {
293 // Save properties common to all Services.
294 if (!Service::Save(storage)) {
295 return false;
296 }
297
298 // Save properties specific to WiFi services.
299 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800300 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
301 storage->SetString(id, kStorageMode, mode_);
302 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
303 storage->SetString(id, kStorageSecurity, security_);
304 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000305
306 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700307 return true;
308}
309
Paul Stewart65512e12012-03-26 18:01:08 -0700310bool WiFiService::Unload() {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800311 Service::Unload();
312 hidden_ssid_ = false;
313 passphrase_ = "";
314 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800315 if (security_ == flimflam::kSecurity8021x) {
316 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
317 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
318 // Make sure that these are cleared when credentials for networks
319 // of this type goes away.
320 //
321 // When wpa_supplicant gains the ability, do this credential
322 // clearing on a per-service basis. Also do this whenever the credentials
323 // for a service changes. crosbug.com/25670
324 wifi_->ClearCachedCredentials();
325 }
Paul Stewart65512e12012-03-26 18:01:08 -0700326 return !IsVisible();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800327}
328
Paul Stewart6ab23a92011-11-09 17:17:47 -0800329bool WiFiService::IsSecurityMatch(const string &security) const {
330 return GetSecurityClass(security) == GetSecurityClass(security_);
331}
332
Thieu Le48e6d6d2011-12-06 00:40:27 +0000333void WiFiService::InitializeCustomMetrics() const {
334 string histogram = metrics()->GetFullMetricName(
335 Metrics::kMetricTimeToJoinMilliseconds,
336 technology());
337 metrics()->AddServiceStateTransitionTimer(this,
338 histogram,
339 Service::kStateAssociating,
340 Service::kStateConfiguring);
341}
342
Thieu Leb84ba342012-03-02 15:15:19 -0800343void WiFiService::SendPostReadyStateMetrics(
344 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000345 metrics()->SendEnumToUMA(
346 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
347 technology()),
348 Metrics::WiFiFrequencyToChannel(frequency_),
349 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000350
351 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
352 metrics()->SendEnumToUMA(
353 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
354 technology()),
355 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
356 Metrics::kWiFiNetworkPhyModeMax);
357
358 Metrics::WiFiSecurity security_uma =
359 Metrics::WiFiSecurityStringToEnum(security_);
360 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
361 metrics()->SendEnumToUMA(
362 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
363 technology()),
364 security_uma,
365 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800366
367 if (time_resume_to_ready_milliseconds > 0) {
368 metrics()->SendToUMA(
369 metrics()->GetFullMetricName(
370 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
371 time_resume_to_ready_milliseconds,
372 Metrics::kTimerHistogramMillisecondsMin,
373 Metrics::kTimerHistogramMillisecondsMax,
374 Metrics::kTimerHistogramNumBuckets);
375 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000376}
377
mukesh agrawal32399322011-09-01 10:53:43 -0700378// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800379void WiFiService::HelpRegisterWriteOnlyDerivedString(
380 const string &name,
381 void(WiFiService::*set)(const string &, Error *),
382 void(WiFiService::*clear)(Error *),
383 const string *default_value) {
384 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000385 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800386 StringAccessor(
387 new CustomWriteOnlyAccessor<WiFiService, string>(
388 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000389}
390
Eric Shienbrood9a245532012-03-07 14:20:39 -0500391void WiFiService::Connect(Error */*error*/) {
392 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal6e277772011-09-29 15:04:23 -0700393 std::map<string, DBus::Variant> params;
394 DBus::MessageIter writer;
395
396 params[wpa_supplicant::kNetworkPropertyMode].writer().
397 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
398
Gaurav Shah29d68882012-01-30 19:06:42 -0800399 if (Is8021x()) {
400 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800401 if (GetEAPKeyManagement().empty())
402 SetEAPKeyManagement("WPA-EAP");
403 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700404 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800405 const string psk_proto = StringPrintf("%s %s",
406 wpa_supplicant::kSecurityModeWPA,
407 wpa_supplicant::kSecurityModeRSN);
408 params[wpa_supplicant::kPropertySecurityProtocol].writer().
409 append_string(psk_proto.c_str());
410 params[wpa_supplicant::kPropertyPreSharedKey].writer().
411 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700412 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700413 params[wpa_supplicant::kPropertySecurityProtocol].writer().
414 append_string(wpa_supplicant::kSecurityModeRSN);
415 params[wpa_supplicant::kPropertyPreSharedKey].writer().
416 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700417 } else if (security_ == flimflam::kSecurityWpa) {
418 params[wpa_supplicant::kPropertySecurityProtocol].writer().
419 append_string(wpa_supplicant::kSecurityModeWPA);
420 params[wpa_supplicant::kPropertyPreSharedKey].writer().
421 append_string(passphrase_.c_str());
422 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000423 params[wpa_supplicant::kPropertyAuthAlg].writer().
424 append_string(wpa_supplicant::kSecurityAuthAlg);
425 Error error;
426 int key_index;
427 std::vector<uint8> password_bytes;
428 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
429 writer = params[wpa_supplicant::kPropertyWEPKey +
430 base::IntToString(key_index)].writer();
431 writer << password_bytes;
432 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
433 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700434 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800435 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700436 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800437 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700438 }
439
Gaurav Shah10109f22011-11-11 20:16:22 -0800440 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700441 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800442
443 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700444 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
445 writer << ssid_;
446
447 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700448}
449
Eric Shienbrood9a245532012-03-07 14:20:39 -0500450void WiFiService::Disconnect(Error *error) {
451 LOG(INFO) << __func__;
452 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000453 wifi_->DisconnectFrom(this);
454}
455
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800456string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700457 return wifi_->GetRpcIdentifier();
458}
459
mukesh agrawal29c13a12011-11-24 00:09:19 +0000460void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800461 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000462 if (security_ == flimflam::kSecurityNone) {
463 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800464 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800465 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800466 } else if (Is8021x()) {
467 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000468 } else if (security_ == flimflam::kSecurityWep ||
469 security_ == flimflam::kSecurityWpa ||
470 security_ == flimflam::kSecurityPsk ||
471 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800472 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800473 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000474 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800475 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000476}
477
mukesh agrawale1d90e92012-02-15 17:36:08 -0800478void WiFiService::UpdateFromEndpoints() {
479 const WiFiEndpoint *representative_endpoint = NULL;
480
481 if (current_endpoint_) {
482 // TODO: Copy BSSID here (crosbug.com/22377).
483 representative_endpoint = current_endpoint_;
484 } else {
485 int16 best_signal = std::numeric_limits<int16>::min();
486 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
487 i != endpoints_.end(); ++i) {
488 if ((*i)->signal_strength() >= best_signal) {
489 best_signal = (*i)->signal_strength();
490 representative_endpoint = *i;
491 }
492 }
493 }
494
495 uint16 frequency;
496 int16 signal;
497 if (!representative_endpoint) {
498 frequency = 0;
499 signal = std::numeric_limits<int16>::min();
500 } else {
501 frequency = representative_endpoint->frequency();
502 signal = representative_endpoint->signal_strength();
503 }
504
505 if (frequency_ != frequency) {
506 frequency_ = frequency;
507 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
508 }
509 SetStrength(SignalToStrength(signal));
510}
511
mukesh agrawal1a056262011-10-05 14:36:54 -0700512// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000513void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
514 Error *error) {
515 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700516}
517
518// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000519void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
520 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700521 unsigned int length = passphrase.length();
522 vector<uint8> passphrase_bytes;
523
524 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
525 if (length != IEEE_80211::kWPAHexLen &&
526 (length < IEEE_80211::kWPAAsciiMinLen ||
527 length > IEEE_80211::kWPAAsciiMaxLen)) {
528 error->Populate(Error::kInvalidPassphrase);
529 }
530 } else {
531 if (length < IEEE_80211::kWPAAsciiMinLen ||
532 length > IEEE_80211::kWPAAsciiMaxLen) {
533 error->Populate(Error::kInvalidPassphrase);
534 }
535 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000536}
mukesh agrawal1a056262011-10-05 14:36:54 -0700537
Thieu Lef4cbda92011-11-10 23:41:24 +0000538// static
539void WiFiService::ParseWEPPassphrase(const string &passphrase,
540 int *key_index,
541 std::vector<uint8> *password_bytes,
542 Error *error) {
543 unsigned int length = passphrase.length();
544 int key_index_local;
545 std::string password_text;
546 bool is_hex = false;
547
548 switch (length) {
549 case IEEE_80211::kWEP40AsciiLen:
550 case IEEE_80211::kWEP104AsciiLen:
551 key_index_local = 0;
552 password_text = passphrase;
553 break;
554 case IEEE_80211::kWEP40AsciiLen + 2:
555 case IEEE_80211::kWEP104AsciiLen + 2:
556 if (CheckWEPKeyIndex(passphrase, error)) {
557 base::StringToInt(passphrase.substr(0,1), &key_index_local);
558 password_text = passphrase.substr(2);
559 }
560 break;
561 case IEEE_80211::kWEP40HexLen:
562 case IEEE_80211::kWEP104HexLen:
563 if (CheckWEPIsHex(passphrase, error)) {
564 key_index_local = 0;
565 password_text = passphrase;
566 is_hex = true;
567 }
568 break;
569 case IEEE_80211::kWEP40HexLen + 2:
570 case IEEE_80211::kWEP104HexLen + 2:
571 if(CheckWEPKeyIndex(passphrase, error) &&
572 CheckWEPIsHex(passphrase.substr(2), error)) {
573 base::StringToInt(passphrase.substr(0,1), &key_index_local);
574 password_text = passphrase.substr(2);
575 is_hex = true;
576 } else if (CheckWEPPrefix(passphrase, error) &&
577 CheckWEPIsHex(passphrase.substr(2), error)) {
578 key_index_local = 0;
579 password_text = passphrase.substr(2);
580 is_hex = true;
581 }
582 break;
583 case IEEE_80211::kWEP40HexLen + 4:
584 case IEEE_80211::kWEP104HexLen + 4:
585 if (CheckWEPKeyIndex(passphrase, error) &&
586 CheckWEPPrefix(passphrase.substr(2), error) &&
587 CheckWEPIsHex(passphrase.substr(4), error)) {
588 base::StringToInt(passphrase.substr(0,1), &key_index_local);
589 password_text = passphrase.substr(4);
590 is_hex = true;
591 }
592 break;
593 default:
594 error->Populate(Error::kInvalidPassphrase);
595 break;
596 }
597
mukesh agrawal1a056262011-10-05 14:36:54 -0700598 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000599 if (key_index)
600 *key_index = key_index_local;
601 if (password_bytes) {
602 if (is_hex)
603 base::HexStringToBytes(password_text, password_bytes);
604 else
605 password_bytes->insert(password_bytes->end(),
606 password_text.begin(),
607 password_text.end());
608 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700609 }
610}
611
612// static
613bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
614 vector<uint8> passphrase_bytes;
615 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
616 return true;
617 } else {
618 error->Populate(Error::kInvalidPassphrase);
619 return false;
620 }
621}
622
623// static
624bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
625 if (StartsWithASCII(passphrase, "0:", false) ||
626 StartsWithASCII(passphrase, "1:", false) ||
627 StartsWithASCII(passphrase, "2:", false) ||
628 StartsWithASCII(passphrase, "3:", false)) {
629 return true;
630 } else {
631 error->Populate(Error::kInvalidPassphrase);
632 return false;
633 }
634}
635
636// static
637bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
638 if (StartsWithASCII(passphrase, "0x", false)) {
639 return true;
640 } else {
641 error->Populate(Error::kInvalidPassphrase);
642 return false;
643 }
644}
645
Paul Stewart6ab23a92011-11-09 17:17:47 -0800646// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800647string WiFiService::GetSecurityClass(const string &security) {
648 if (security == flimflam::kSecurityRsn ||
649 security == flimflam::kSecurityWpa) {
650 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700651 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800652 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700653 }
654}
655
Paul Stewarta41e38d2011-11-11 07:47:29 -0800656// static
657bool WiFiService::ParseStorageIdentifier(const string &storage_name,
658 string *address,
659 string *mode,
660 string *security) {
661 vector<string> wifi_parts;
662 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800663 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
664 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800665 return false;
666 }
667 *address = wifi_parts[1];
668 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800669 if (wifi_parts.size() == 5) {
670 *security = wifi_parts[4];
671 } else {
672 // Account for security type "802_1x" which got split up above.
673 *security = wifi_parts[4] + "_" + wifi_parts[5];
674 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800675 return true;
676}
677
mukesh agrawale1d90e92012-02-15 17:36:08 -0800678// static
679uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
680 int16 strength;
681 if (signal_dbm > 0) {
682 if (!logged_signal_warning) {
683 LOG(WARNING) << "Signal strength is suspiciously high. "
684 << "Assuming value " << signal_dbm << " is not in dBm.";
685 logged_signal_warning = true;
686 }
687 strength = signal_dbm;
688 } else {
689 strength = 120 + signal_dbm; // Call -20dBm "perfect".
690 }
691
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800692 if (strength > kStrengthMax) {
693 strength = kStrengthMax;
694 } else if (strength < kStrengthMin) {
695 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800696 }
697 return strength;
698}
699
Paul Stewart6ab23a92011-11-09 17:17:47 -0800700string WiFiService::GetGenericStorageIdentifier() const {
701 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
702}
703
Paul Stewartd08f4432011-11-04 07:48:20 -0700704string WiFiService::GetSpecificStorageIdentifier() const {
705 return GetStorageIdentifierForSecurity(security_);
706}
707
708string WiFiService::GetStorageIdentifierForSecurity(
709 const string &security) const {
710 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
711 flimflam::kTypeWifi,
712 wifi_->address().c_str(),
713 hex_ssid_.c_str(),
714 mode_.c_str(),
715 security.c_str()));
716}
717
Gaurav Shah10109f22011-11-11 20:16:22 -0800718void WiFiService::set_eap(const EapCredentials &eap) {
719 Service::set_eap(eap);
720 UpdateConnectable();
721}
722
Gaurav Shah29d68882012-01-30 19:06:42 -0800723bool WiFiService::Is8021x() const {
724 if (security_ == flimflam::kSecurity8021x)
725 return true;
726
727 // Dynamic WEP + 802.1x.
728 if (security_ == flimflam::kSecurityWep &&
729 GetEAPKeyManagement() == "IEEE8021X")
730 return true;
731 return false;
732}
733
Gaurav Shah10109f22011-11-11 20:16:22 -0800734void WiFiService::Populate8021xProperties(
735 std::map<string, DBus::Variant> *params) {
736 typedef std::pair<const char *, const char *> KeyVal;
737 KeyVal propertyvals[] = {
738 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
739 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
740 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
741 eap().inner_eap.c_str()),
742 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
743 eap().anonymous_identity.c_str()),
744 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
745 eap().client_cert.c_str()),
746 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
747 eap().private_key.c_str()),
748 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
749 eap().private_key_password.c_str()),
750 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
751 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
752 eap().password.c_str()),
753 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
754 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
755 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
756 eap().ca_cert_id.c_str()),
757 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
758 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
759 // crosbug.com/25663
760 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
761 };
762
763 DBus::MessageIter writer;
764 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
765 if (strlen(propertyvals[i].second) > 0) {
766 (*params)[propertyvals[i].first].writer().
767 append_string(propertyvals[i].second);
768 }
769 }
770}
771
mukesh agrawalb54601c2011-06-07 17:39:22 -0700772} // namespace shill