blob: 2faa4521f7ba8979a2c643ed5beeb60d4961eab2 [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 Stewartd8ad3c42012-01-09 12:39:38 -0800310void WiFiService::Unload() {
311 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 Stewartd8ad3c42012-01-09 12:39:38 -0800326}
327
Paul Stewart6ab23a92011-11-09 17:17:47 -0800328bool WiFiService::IsSecurityMatch(const string &security) const {
329 return GetSecurityClass(security) == GetSecurityClass(security_);
330}
331
Thieu Le48e6d6d2011-12-06 00:40:27 +0000332void WiFiService::InitializeCustomMetrics() const {
333 string histogram = metrics()->GetFullMetricName(
334 Metrics::kMetricTimeToJoinMilliseconds,
335 technology());
336 metrics()->AddServiceStateTransitionTimer(this,
337 histogram,
338 Service::kStateAssociating,
339 Service::kStateConfiguring);
340}
341
Thieu Leb84ba342012-03-02 15:15:19 -0800342void WiFiService::SendPostReadyStateMetrics(
343 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000344 metrics()->SendEnumToUMA(
345 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
346 technology()),
347 Metrics::WiFiFrequencyToChannel(frequency_),
348 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000349
350 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
351 metrics()->SendEnumToUMA(
352 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
353 technology()),
354 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
355 Metrics::kWiFiNetworkPhyModeMax);
356
357 Metrics::WiFiSecurity security_uma =
358 Metrics::WiFiSecurityStringToEnum(security_);
359 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
360 metrics()->SendEnumToUMA(
361 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
362 technology()),
363 security_uma,
364 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800365
366 if (time_resume_to_ready_milliseconds > 0) {
367 metrics()->SendToUMA(
368 metrics()->GetFullMetricName(
369 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
370 time_resume_to_ready_milliseconds,
371 Metrics::kTimerHistogramMillisecondsMin,
372 Metrics::kTimerHistogramMillisecondsMax,
373 Metrics::kTimerHistogramNumBuckets);
374 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000375}
376
mukesh agrawal32399322011-09-01 10:53:43 -0700377// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800378void WiFiService::HelpRegisterWriteOnlyDerivedString(
379 const string &name,
380 void(WiFiService::*set)(const string &, Error *),
381 void(WiFiService::*clear)(Error *),
382 const string *default_value) {
383 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000384 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800385 StringAccessor(
386 new CustomWriteOnlyAccessor<WiFiService, string>(
387 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000388}
389
Eric Shienbrood9a245532012-03-07 14:20:39 -0500390void WiFiService::Connect(Error */*error*/) {
391 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal6e277772011-09-29 15:04:23 -0700392 std::map<string, DBus::Variant> params;
393 DBus::MessageIter writer;
394
395 params[wpa_supplicant::kNetworkPropertyMode].writer().
396 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
397
Gaurav Shah29d68882012-01-30 19:06:42 -0800398 if (Is8021x()) {
399 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800400 if (GetEAPKeyManagement().empty())
401 SetEAPKeyManagement("WPA-EAP");
402 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700403 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800404 const string psk_proto = StringPrintf("%s %s",
405 wpa_supplicant::kSecurityModeWPA,
406 wpa_supplicant::kSecurityModeRSN);
407 params[wpa_supplicant::kPropertySecurityProtocol].writer().
408 append_string(psk_proto.c_str());
409 params[wpa_supplicant::kPropertyPreSharedKey].writer().
410 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700411 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700412 params[wpa_supplicant::kPropertySecurityProtocol].writer().
413 append_string(wpa_supplicant::kSecurityModeRSN);
414 params[wpa_supplicant::kPropertyPreSharedKey].writer().
415 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700416 } else if (security_ == flimflam::kSecurityWpa) {
417 params[wpa_supplicant::kPropertySecurityProtocol].writer().
418 append_string(wpa_supplicant::kSecurityModeWPA);
419 params[wpa_supplicant::kPropertyPreSharedKey].writer().
420 append_string(passphrase_.c_str());
421 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000422 params[wpa_supplicant::kPropertyAuthAlg].writer().
423 append_string(wpa_supplicant::kSecurityAuthAlg);
424 Error error;
425 int key_index;
426 std::vector<uint8> password_bytes;
427 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
428 writer = params[wpa_supplicant::kPropertyWEPKey +
429 base::IntToString(key_index)].writer();
430 writer << password_bytes;
431 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
432 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700433 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800434 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700435 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800436 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700437 }
438
Gaurav Shah10109f22011-11-11 20:16:22 -0800439 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700440 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800441
442 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700443 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
444 writer << ssid_;
445
446 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700447}
448
Eric Shienbrood9a245532012-03-07 14:20:39 -0500449void WiFiService::Disconnect(Error *error) {
450 LOG(INFO) << __func__;
451 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000452 wifi_->DisconnectFrom(this);
453}
454
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800455string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700456 return wifi_->GetRpcIdentifier();
457}
458
mukesh agrawal29c13a12011-11-24 00:09:19 +0000459void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800460 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000461 if (security_ == flimflam::kSecurityNone) {
462 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800463 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800464 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800465 } else if (Is8021x()) {
466 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000467 } else if (security_ == flimflam::kSecurityWep ||
468 security_ == flimflam::kSecurityWpa ||
469 security_ == flimflam::kSecurityPsk ||
470 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800471 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800472 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000473 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800474 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000475}
476
mukesh agrawale1d90e92012-02-15 17:36:08 -0800477void WiFiService::UpdateFromEndpoints() {
478 const WiFiEndpoint *representative_endpoint = NULL;
479
480 if (current_endpoint_) {
481 // TODO: Copy BSSID here (crosbug.com/22377).
482 representative_endpoint = current_endpoint_;
483 } else {
484 int16 best_signal = std::numeric_limits<int16>::min();
485 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
486 i != endpoints_.end(); ++i) {
487 if ((*i)->signal_strength() >= best_signal) {
488 best_signal = (*i)->signal_strength();
489 representative_endpoint = *i;
490 }
491 }
492 }
493
494 uint16 frequency;
495 int16 signal;
496 if (!representative_endpoint) {
497 frequency = 0;
498 signal = std::numeric_limits<int16>::min();
499 } else {
500 frequency = representative_endpoint->frequency();
501 signal = representative_endpoint->signal_strength();
502 }
503
504 if (frequency_ != frequency) {
505 frequency_ = frequency;
506 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
507 }
508 SetStrength(SignalToStrength(signal));
509}
510
mukesh agrawal1a056262011-10-05 14:36:54 -0700511// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000512void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
513 Error *error) {
514 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700515}
516
517// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000518void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
519 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700520 unsigned int length = passphrase.length();
521 vector<uint8> passphrase_bytes;
522
523 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
524 if (length != IEEE_80211::kWPAHexLen &&
525 (length < IEEE_80211::kWPAAsciiMinLen ||
526 length > IEEE_80211::kWPAAsciiMaxLen)) {
527 error->Populate(Error::kInvalidPassphrase);
528 }
529 } else {
530 if (length < IEEE_80211::kWPAAsciiMinLen ||
531 length > IEEE_80211::kWPAAsciiMaxLen) {
532 error->Populate(Error::kInvalidPassphrase);
533 }
534 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000535}
mukesh agrawal1a056262011-10-05 14:36:54 -0700536
Thieu Lef4cbda92011-11-10 23:41:24 +0000537// static
538void WiFiService::ParseWEPPassphrase(const string &passphrase,
539 int *key_index,
540 std::vector<uint8> *password_bytes,
541 Error *error) {
542 unsigned int length = passphrase.length();
543 int key_index_local;
544 std::string password_text;
545 bool is_hex = false;
546
547 switch (length) {
548 case IEEE_80211::kWEP40AsciiLen:
549 case IEEE_80211::kWEP104AsciiLen:
550 key_index_local = 0;
551 password_text = passphrase;
552 break;
553 case IEEE_80211::kWEP40AsciiLen + 2:
554 case IEEE_80211::kWEP104AsciiLen + 2:
555 if (CheckWEPKeyIndex(passphrase, error)) {
556 base::StringToInt(passphrase.substr(0,1), &key_index_local);
557 password_text = passphrase.substr(2);
558 }
559 break;
560 case IEEE_80211::kWEP40HexLen:
561 case IEEE_80211::kWEP104HexLen:
562 if (CheckWEPIsHex(passphrase, error)) {
563 key_index_local = 0;
564 password_text = passphrase;
565 is_hex = true;
566 }
567 break;
568 case IEEE_80211::kWEP40HexLen + 2:
569 case IEEE_80211::kWEP104HexLen + 2:
570 if(CheckWEPKeyIndex(passphrase, error) &&
571 CheckWEPIsHex(passphrase.substr(2), error)) {
572 base::StringToInt(passphrase.substr(0,1), &key_index_local);
573 password_text = passphrase.substr(2);
574 is_hex = true;
575 } else if (CheckWEPPrefix(passphrase, error) &&
576 CheckWEPIsHex(passphrase.substr(2), error)) {
577 key_index_local = 0;
578 password_text = passphrase.substr(2);
579 is_hex = true;
580 }
581 break;
582 case IEEE_80211::kWEP40HexLen + 4:
583 case IEEE_80211::kWEP104HexLen + 4:
584 if (CheckWEPKeyIndex(passphrase, error) &&
585 CheckWEPPrefix(passphrase.substr(2), error) &&
586 CheckWEPIsHex(passphrase.substr(4), error)) {
587 base::StringToInt(passphrase.substr(0,1), &key_index_local);
588 password_text = passphrase.substr(4);
589 is_hex = true;
590 }
591 break;
592 default:
593 error->Populate(Error::kInvalidPassphrase);
594 break;
595 }
596
mukesh agrawal1a056262011-10-05 14:36:54 -0700597 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000598 if (key_index)
599 *key_index = key_index_local;
600 if (password_bytes) {
601 if (is_hex)
602 base::HexStringToBytes(password_text, password_bytes);
603 else
604 password_bytes->insert(password_bytes->end(),
605 password_text.begin(),
606 password_text.end());
607 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700608 }
609}
610
611// static
612bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
613 vector<uint8> passphrase_bytes;
614 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
615 return true;
616 } else {
617 error->Populate(Error::kInvalidPassphrase);
618 return false;
619 }
620}
621
622// static
623bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
624 if (StartsWithASCII(passphrase, "0:", false) ||
625 StartsWithASCII(passphrase, "1:", false) ||
626 StartsWithASCII(passphrase, "2:", false) ||
627 StartsWithASCII(passphrase, "3:", false)) {
628 return true;
629 } else {
630 error->Populate(Error::kInvalidPassphrase);
631 return false;
632 }
633}
634
635// static
636bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
637 if (StartsWithASCII(passphrase, "0x", false)) {
638 return true;
639 } else {
640 error->Populate(Error::kInvalidPassphrase);
641 return false;
642 }
643}
644
Paul Stewart6ab23a92011-11-09 17:17:47 -0800645// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800646string WiFiService::GetSecurityClass(const string &security) {
647 if (security == flimflam::kSecurityRsn ||
648 security == flimflam::kSecurityWpa) {
649 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700650 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800651 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700652 }
653}
654
Paul Stewarta41e38d2011-11-11 07:47:29 -0800655// static
656bool WiFiService::ParseStorageIdentifier(const string &storage_name,
657 string *address,
658 string *mode,
659 string *security) {
660 vector<string> wifi_parts;
661 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800662 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
663 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800664 return false;
665 }
666 *address = wifi_parts[1];
667 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800668 if (wifi_parts.size() == 5) {
669 *security = wifi_parts[4];
670 } else {
671 // Account for security type "802_1x" which got split up above.
672 *security = wifi_parts[4] + "_" + wifi_parts[5];
673 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800674 return true;
675}
676
mukesh agrawale1d90e92012-02-15 17:36:08 -0800677// static
678uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
679 int16 strength;
680 if (signal_dbm > 0) {
681 if (!logged_signal_warning) {
682 LOG(WARNING) << "Signal strength is suspiciously high. "
683 << "Assuming value " << signal_dbm << " is not in dBm.";
684 logged_signal_warning = true;
685 }
686 strength = signal_dbm;
687 } else {
688 strength = 120 + signal_dbm; // Call -20dBm "perfect".
689 }
690
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800691 if (strength > kStrengthMax) {
692 strength = kStrengthMax;
693 } else if (strength < kStrengthMin) {
694 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800695 }
696 return strength;
697}
698
Paul Stewart6ab23a92011-11-09 17:17:47 -0800699string WiFiService::GetGenericStorageIdentifier() const {
700 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
701}
702
Paul Stewartd08f4432011-11-04 07:48:20 -0700703string WiFiService::GetSpecificStorageIdentifier() const {
704 return GetStorageIdentifierForSecurity(security_);
705}
706
707string WiFiService::GetStorageIdentifierForSecurity(
708 const string &security) const {
709 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
710 flimflam::kTypeWifi,
711 wifi_->address().c_str(),
712 hex_ssid_.c_str(),
713 mode_.c_str(),
714 security.c_str()));
715}
716
Gaurav Shah10109f22011-11-11 20:16:22 -0800717void WiFiService::set_eap(const EapCredentials &eap) {
718 Service::set_eap(eap);
719 UpdateConnectable();
720}
721
Gaurav Shah29d68882012-01-30 19:06:42 -0800722bool WiFiService::Is8021x() const {
723 if (security_ == flimflam::kSecurity8021x)
724 return true;
725
726 // Dynamic WEP + 802.1x.
727 if (security_ == flimflam::kSecurityWep &&
728 GetEAPKeyManagement() == "IEEE8021X")
729 return true;
730 return false;
731}
732
Gaurav Shah10109f22011-11-11 20:16:22 -0800733void WiFiService::Populate8021xProperties(
734 std::map<string, DBus::Variant> *params) {
735 typedef std::pair<const char *, const char *> KeyVal;
736 KeyVal propertyvals[] = {
737 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
738 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
739 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
740 eap().inner_eap.c_str()),
741 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
742 eap().anonymous_identity.c_str()),
743 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
744 eap().client_cert.c_str()),
745 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
746 eap().private_key.c_str()),
747 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
748 eap().private_key_password.c_str()),
749 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
750 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
751 eap().password.c_str()),
752 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
753 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
754 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
755 eap().ca_cert_id.c_str()),
756 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
757 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
758 // crosbug.com/25663
759 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
760 };
761
762 DBus::MessageIter writer;
763 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
764 if (strlen(propertyvals[i].second) > 0) {
765 (*params)[propertyvals[i].first].writer().
766 append_string(propertyvals[i].second);
767 }
768 }
769}
770
mukesh agrawalb54601c2011-06-07 17:39:22 -0700771} // namespace shill