blob: 8b9250fb99658c41c26c8f2229ea0647a58dea93 [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
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";
mukesh agrawale1d90e92012-02-15 17:36:08 -080042bool WiFiService::logged_signal_warning = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070043
mukesh agrawalb54601c2011-06-07 17:39:22 -070044WiFiService::WiFiService(ControlInterface *control_interface,
45 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080046 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070047 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070048 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080049 const vector<uint8_t> &ssid,
50 const string &mode,
51 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080052 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080053 : Service(control_interface, dispatcher, metrics, manager,
54 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070055 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070056 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070057 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080058 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000059 frequency_(0),
Paul Stewart20088d82012-02-16 06:58:55 -080060 physical_mode_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070061 task_factory_(this),
62 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070063 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070064 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070065 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080066 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
67 &WiFiService::SetPassphrase,
68 &WiFiService::ClearPassphrase,
69 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070070 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
71 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070072
Paul Stewartac4ac002011-08-26 12:04:26 -070073 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
74 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
75 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
76 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070077
mukesh agrawald835b202011-10-07 15:26:47 -070078 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
79 string ssid_string(
80 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080081 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070082 // WifiHexSsid property should only be present if Name property
83 // has been munged.
84 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
85 }
86 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070087
mukesh agrawal6e277772011-09-29 15:04:23 -070088 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
89 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080090 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080091 // Passphrases are not mandatory for 802.1X.
92 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070093 } else if (security_ == flimflam::kSecurityPsk) {
94 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070095 } else if (security_ == flimflam::kSecurityRsn) {
96 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070097 } else if (security_ == flimflam::kSecurityWpa) {
98 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070099 } else if (security_ == flimflam::kSecurityWep) {
100 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700101 } else if (security_ == flimflam::kSecurityNone) {
102 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700103 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800104 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700105 }
106
Paul Stewartd08f4432011-11-04 07:48:20 -0700107 // Until we know better (at Profile load time), use the generic name.
108 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000109 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700110}
111
112WiFiService::~WiFiService() {
113 LOG(INFO) << __func__;
114}
115
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000116void WiFiService::AutoConnect() {
117 if (IsAutoConnectable()) {
118 // Execute immediately, for two reasons:
119 //
120 // 1. We need IsAutoConnectable to return the correct value for
121 // other WiFiServices, and that depends on WiFi's state.
122 //
123 // 2. We should probably limit the extent to which we queue up
124 // actions (such as AutoConnect) which depend on current state.
125 // If we queued AutoConnects, we could build a long queue of
126 // useless work (one AutoConnect per Service), which blocks
127 // more timely work.
128 ConnectTask();
mukesh agrawal592516d2012-01-12 14:01:00 -0800129 } else {
130 LOG(INFO) << "Suppressed autoconnect to " << friendly_name();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000131 }
132}
133
mukesh agrawal1830fa12011-09-26 14:31:40 -0700134void WiFiService::Connect(Error */*error*/) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800135 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000136 // Defer handling, since dbus-c++ does not permit us to send an
137 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700138 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700139 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700140}
141
mukesh agrawaladb68482012-01-17 16:31:51 -0800142void WiFiService::Disconnect(Error *error) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000143 LOG(INFO) << __func__;
mukesh agrawaladb68482012-01-17 16:31:51 -0800144 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000145 // Defer handling, since dbus-c++ does not permit us to send an
146 // outbound request while processing an inbound one.
147 dispatcher()->PostTask(
148 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700149}
150
Paul Stewart22aa71b2011-09-16 12:15:11 -0700151bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
152 return wifi_->TechnologyIs(type);
153}
154
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000155bool WiFiService::IsAutoConnectable() const {
mukesh agrawaladb68482012-01-17 16:31:51 -0800156 return Service::IsAutoConnectable() &&
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000157 // Only auto-connect to Services which have visible Endpoints.
158 // (Needed because hidden Services may remain registered with
159 // Manager even without visible Endpoints.)
mukesh agrawaladb68482012-01-17 16:31:51 -0800160 HasEndpoints() &&
mukesh agrawal76d13882012-01-12 15:23:11 -0800161 // Do not preempt an existing connection (whether pending, or
162 // connected, and whether to this service, or another).
mukesh agrawaladb68482012-01-17 16:31:51 -0800163 wifi_->IsIdle();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000164}
165
166bool WiFiService::IsConnecting() const {
167 // WiFi does not move us into the associating state until it gets
168 // feedback from wpa_supplicant. So, to answer whether or
169 // not we're connecting, we consult with |wifi_|.
170 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800171}
172
mukesh agrawale1d90e92012-02-15 17:36:08 -0800173void WiFiService::AddEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000174 DCHECK(endpoint->ssid() == ssid());
175 endpoints_.insert(endpoint);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800176 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000177}
178
mukesh agrawale1d90e92012-02-15 17:36:08 -0800179void WiFiService::RemoveEndpoint(const WiFiEndpointConstRefPtr endpoint) {
mukesh agrawal261daca2011-12-02 18:56:56 +0000180 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
181 DCHECK(i != endpoints_.end());
182 if (i == endpoints_.end()) {
183 LOG(WARNING) << "In " << __func__ << "(): "
184 << "ignorning non-existent endpoint "
185 << endpoint->bssid_string();
186 return;
187 }
188 endpoints_.erase(i);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800189 if (current_endpoint_ == endpoint) {
190 current_endpoint_ = NULL;
191 }
192 UpdateFromEndpoints();
mukesh agrawal261daca2011-12-02 18:56:56 +0000193}
194
mukesh agrawale1d90e92012-02-15 17:36:08 -0800195void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint *endpoint) {
196 DCHECK(!endpoint || (endpoints_.find(endpoint) != endpoints_.end()));
197 current_endpoint_ = endpoint;
198 UpdateFromEndpoints();
Thieu Lee41a72d2012-02-06 20:46:51 +0000199}
200
mukesh agrawalb20776f2012-02-10 16:00:36 -0800201void WiFiService::NotifyEndpointUpdated(const WiFiEndpoint &endpoint) {
202 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
mukesh agrawale1d90e92012-02-15 17:36:08 -0800203 UpdateFromEndpoints();
mukesh agrawalb20776f2012-02-10 16:00:36 -0800204}
205
Chris Masone6515aab2011-10-12 16:19:09 -0700206string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700207 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700208}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700209
mukesh agrawal1a056262011-10-05 14:36:54 -0700210void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
211 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000212 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700213 } else if (security_ == flimflam::kSecurityPsk ||
214 security_ == flimflam::kSecurityWpa ||
215 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000216 ValidateWPAPassphrase(passphrase, error);
217 } else {
218 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700219 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000220
Paul Stewart2706aaf2011-12-14 16:44:04 -0800221 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000222 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800223 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000224
225 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700226}
227
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800228// ClearPassphrase is separate from SetPassphrase, because the default
229// value for |passphrase_| would not pass validation.
230void WiFiService::ClearPassphrase(Error */*error*/) {
231 passphrase_.clear();
232 UpdateConnectable();
233}
234
Paul Stewartd08f4432011-11-04 07:48:20 -0700235bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
236 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
237 storage->ContainsGroup(GetSpecificStorageIdentifier());
238}
239
Paul Stewarta41e38d2011-11-11 07:47:29 -0800240bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800241 // WiFi Services should be displayed only if they are in range (have
242 // endpoints that have shown up in a scan) or if the service is actively
243 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000244 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800245}
246
Paul Stewartd08f4432011-11-04 07:48:20 -0700247bool WiFiService::Load(StoreInterface *storage) {
248 // First find out which storage identifier is available in priority order
249 // of specific, generic.
250 string id = GetSpecificStorageIdentifier();
251 if (!storage->ContainsGroup(id)) {
252 id = GetGenericStorageIdentifier();
253 if (!storage->ContainsGroup(id)) {
254 LOG(WARNING) << "Service is not available in the persistent store: "
255 << id;
256 return false;
257 }
258 }
259
260 // Set our storage identifier to match the storage name in the Profile.
261 storage_identifier_ = id;
262
263 // Load properties common to all Services.
264 if (!Service::Load(storage)) {
265 return false;
266 }
267
268 // Load properties specific to WiFi services.
269 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000270
Paul Stewart2706aaf2011-12-14 16:44:04 -0800271 // NB: mode, security and ssid parameters are never read in from
272 // Load() as they are provided from the scan.
273
274 string passphrase;
275 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
276 Error error;
277 SetPassphrase(passphrase, &error);
278 if (!error.IsSuccess()) {
279 LOG(ERROR) << "Passphrase could not be set: "
280 << Error::GetName(error.type());
281 }
282 }
283
Paul Stewartd08f4432011-11-04 07:48:20 -0700284 return true;
285}
286
287bool WiFiService::Save(StoreInterface *storage) {
288 // Save properties common to all Services.
289 if (!Service::Save(storage)) {
290 return false;
291 }
292
293 // Save properties specific to WiFi services.
294 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800295 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
296 storage->SetString(id, kStorageMode, mode_);
297 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
298 storage->SetString(id, kStorageSecurity, security_);
299 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000300
301 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700302 return true;
303}
304
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800305void WiFiService::Unload() {
306 Service::Unload();
307 hidden_ssid_ = false;
308 passphrase_ = "";
309 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800310 if (security_ == flimflam::kSecurity8021x) {
311 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
312 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
313 // Make sure that these are cleared when credentials for networks
314 // of this type goes away.
315 //
316 // When wpa_supplicant gains the ability, do this credential
317 // clearing on a per-service basis. Also do this whenever the credentials
318 // for a service changes. crosbug.com/25670
319 wifi_->ClearCachedCredentials();
320 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800321}
322
Paul Stewart6ab23a92011-11-09 17:17:47 -0800323bool WiFiService::IsSecurityMatch(const string &security) const {
324 return GetSecurityClass(security) == GetSecurityClass(security_);
325}
326
Thieu Le48e6d6d2011-12-06 00:40:27 +0000327void WiFiService::InitializeCustomMetrics() const {
328 string histogram = metrics()->GetFullMetricName(
329 Metrics::kMetricTimeToJoinMilliseconds,
330 technology());
331 metrics()->AddServiceStateTransitionTimer(this,
332 histogram,
333 Service::kStateAssociating,
334 Service::kStateConfiguring);
335}
336
Thieu Leb84ba342012-03-02 15:15:19 -0800337void WiFiService::SendPostReadyStateMetrics(
338 int64 time_resume_to_ready_milliseconds) const {
Thieu Le48e6d6d2011-12-06 00:40:27 +0000339 metrics()->SendEnumToUMA(
340 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
341 technology()),
342 Metrics::WiFiFrequencyToChannel(frequency_),
343 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000344
345 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
346 metrics()->SendEnumToUMA(
347 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
348 technology()),
349 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
350 Metrics::kWiFiNetworkPhyModeMax);
351
352 Metrics::WiFiSecurity security_uma =
353 Metrics::WiFiSecurityStringToEnum(security_);
354 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
355 metrics()->SendEnumToUMA(
356 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
357 technology()),
358 security_uma,
359 Metrics::kMetricNetworkSecurityMax);
Thieu Leb84ba342012-03-02 15:15:19 -0800360
361 if (time_resume_to_ready_milliseconds > 0) {
362 metrics()->SendToUMA(
363 metrics()->GetFullMetricName(
364 Metrics::kMetricTimeResumeToReadyMilliseconds, technology()),
365 time_resume_to_ready_milliseconds,
366 Metrics::kTimerHistogramMillisecondsMin,
367 Metrics::kTimerHistogramMillisecondsMax,
368 Metrics::kTimerHistogramNumBuckets);
369 }
Thieu Le48e6d6d2011-12-06 00:40:27 +0000370}
371
mukesh agrawal32399322011-09-01 10:53:43 -0700372// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800373void WiFiService::HelpRegisterWriteOnlyDerivedString(
374 const string &name,
375 void(WiFiService::*set)(const string &, Error *),
376 void(WiFiService::*clear)(Error *),
377 const string *default_value) {
378 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000379 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800380 StringAccessor(
381 new CustomWriteOnlyAccessor<WiFiService, string>(
382 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000383}
384
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700385void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700386 std::map<string, DBus::Variant> params;
387 DBus::MessageIter writer;
388
389 params[wpa_supplicant::kNetworkPropertyMode].writer().
390 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
391
Gaurav Shah29d68882012-01-30 19:06:42 -0800392 if (Is8021x()) {
393 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800394 if (GetEAPKeyManagement().empty())
395 SetEAPKeyManagement("WPA-EAP");
396 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700397 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800398 const string psk_proto = StringPrintf("%s %s",
399 wpa_supplicant::kSecurityModeWPA,
400 wpa_supplicant::kSecurityModeRSN);
401 params[wpa_supplicant::kPropertySecurityProtocol].writer().
402 append_string(psk_proto.c_str());
403 params[wpa_supplicant::kPropertyPreSharedKey].writer().
404 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700405 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700406 params[wpa_supplicant::kPropertySecurityProtocol].writer().
407 append_string(wpa_supplicant::kSecurityModeRSN);
408 params[wpa_supplicant::kPropertyPreSharedKey].writer().
409 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700410 } else if (security_ == flimflam::kSecurityWpa) {
411 params[wpa_supplicant::kPropertySecurityProtocol].writer().
412 append_string(wpa_supplicant::kSecurityModeWPA);
413 params[wpa_supplicant::kPropertyPreSharedKey].writer().
414 append_string(passphrase_.c_str());
415 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000416 params[wpa_supplicant::kPropertyAuthAlg].writer().
417 append_string(wpa_supplicant::kSecurityAuthAlg);
418 Error error;
419 int key_index;
420 std::vector<uint8> password_bytes;
421 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
422 writer = params[wpa_supplicant::kPropertyWEPKey +
423 base::IntToString(key_index)].writer();
424 writer << password_bytes;
425 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
426 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700427 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800428 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700429 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800430 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700431 }
432
Gaurav Shah10109f22011-11-11 20:16:22 -0800433 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700434 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800435
436 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700437 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
438 writer << ssid_;
439
440 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700441}
442
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000443void WiFiService::DisconnectTask() {
444 wifi_->DisconnectFrom(this);
445}
446
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800447string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700448 return wifi_->GetRpcIdentifier();
449}
450
mukesh agrawal29c13a12011-11-24 00:09:19 +0000451void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800452 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000453 if (security_ == flimflam::kSecurityNone) {
454 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800455 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800456 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800457 } else if (Is8021x()) {
458 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000459 } else if (security_ == flimflam::kSecurityWep ||
460 security_ == flimflam::kSecurityWpa ||
461 security_ == flimflam::kSecurityPsk ||
462 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800463 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800464 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000465 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800466 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000467}
468
mukesh agrawale1d90e92012-02-15 17:36:08 -0800469void WiFiService::UpdateFromEndpoints() {
470 const WiFiEndpoint *representative_endpoint = NULL;
471
472 if (current_endpoint_) {
473 // TODO: Copy BSSID here (crosbug.com/22377).
474 representative_endpoint = current_endpoint_;
475 } else {
476 int16 best_signal = std::numeric_limits<int16>::min();
477 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
478 i != endpoints_.end(); ++i) {
479 if ((*i)->signal_strength() >= best_signal) {
480 best_signal = (*i)->signal_strength();
481 representative_endpoint = *i;
482 }
483 }
484 }
485
486 uint16 frequency;
487 int16 signal;
488 if (!representative_endpoint) {
489 frequency = 0;
490 signal = std::numeric_limits<int16>::min();
491 } else {
492 frequency = representative_endpoint->frequency();
493 signal = representative_endpoint->signal_strength();
494 }
495
496 if (frequency_ != frequency) {
497 frequency_ = frequency;
498 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
499 }
500 SetStrength(SignalToStrength(signal));
501}
502
mukesh agrawal1a056262011-10-05 14:36:54 -0700503// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000504void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
505 Error *error) {
506 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700507}
508
509// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000510void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
511 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700512 unsigned int length = passphrase.length();
513 vector<uint8> passphrase_bytes;
514
515 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
516 if (length != IEEE_80211::kWPAHexLen &&
517 (length < IEEE_80211::kWPAAsciiMinLen ||
518 length > IEEE_80211::kWPAAsciiMaxLen)) {
519 error->Populate(Error::kInvalidPassphrase);
520 }
521 } else {
522 if (length < IEEE_80211::kWPAAsciiMinLen ||
523 length > IEEE_80211::kWPAAsciiMaxLen) {
524 error->Populate(Error::kInvalidPassphrase);
525 }
526 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000527}
mukesh agrawal1a056262011-10-05 14:36:54 -0700528
Thieu Lef4cbda92011-11-10 23:41:24 +0000529// static
530void WiFiService::ParseWEPPassphrase(const string &passphrase,
531 int *key_index,
532 std::vector<uint8> *password_bytes,
533 Error *error) {
534 unsigned int length = passphrase.length();
535 int key_index_local;
536 std::string password_text;
537 bool is_hex = false;
538
539 switch (length) {
540 case IEEE_80211::kWEP40AsciiLen:
541 case IEEE_80211::kWEP104AsciiLen:
542 key_index_local = 0;
543 password_text = passphrase;
544 break;
545 case IEEE_80211::kWEP40AsciiLen + 2:
546 case IEEE_80211::kWEP104AsciiLen + 2:
547 if (CheckWEPKeyIndex(passphrase, error)) {
548 base::StringToInt(passphrase.substr(0,1), &key_index_local);
549 password_text = passphrase.substr(2);
550 }
551 break;
552 case IEEE_80211::kWEP40HexLen:
553 case IEEE_80211::kWEP104HexLen:
554 if (CheckWEPIsHex(passphrase, error)) {
555 key_index_local = 0;
556 password_text = passphrase;
557 is_hex = true;
558 }
559 break;
560 case IEEE_80211::kWEP40HexLen + 2:
561 case IEEE_80211::kWEP104HexLen + 2:
562 if(CheckWEPKeyIndex(passphrase, error) &&
563 CheckWEPIsHex(passphrase.substr(2), error)) {
564 base::StringToInt(passphrase.substr(0,1), &key_index_local);
565 password_text = passphrase.substr(2);
566 is_hex = true;
567 } else if (CheckWEPPrefix(passphrase, error) &&
568 CheckWEPIsHex(passphrase.substr(2), error)) {
569 key_index_local = 0;
570 password_text = passphrase.substr(2);
571 is_hex = true;
572 }
573 break;
574 case IEEE_80211::kWEP40HexLen + 4:
575 case IEEE_80211::kWEP104HexLen + 4:
576 if (CheckWEPKeyIndex(passphrase, error) &&
577 CheckWEPPrefix(passphrase.substr(2), error) &&
578 CheckWEPIsHex(passphrase.substr(4), error)) {
579 base::StringToInt(passphrase.substr(0,1), &key_index_local);
580 password_text = passphrase.substr(4);
581 is_hex = true;
582 }
583 break;
584 default:
585 error->Populate(Error::kInvalidPassphrase);
586 break;
587 }
588
mukesh agrawal1a056262011-10-05 14:36:54 -0700589 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000590 if (key_index)
591 *key_index = key_index_local;
592 if (password_bytes) {
593 if (is_hex)
594 base::HexStringToBytes(password_text, password_bytes);
595 else
596 password_bytes->insert(password_bytes->end(),
597 password_text.begin(),
598 password_text.end());
599 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700600 }
601}
602
603// static
604bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
605 vector<uint8> passphrase_bytes;
606 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
607 return true;
608 } else {
609 error->Populate(Error::kInvalidPassphrase);
610 return false;
611 }
612}
613
614// static
615bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
616 if (StartsWithASCII(passphrase, "0:", false) ||
617 StartsWithASCII(passphrase, "1:", false) ||
618 StartsWithASCII(passphrase, "2:", false) ||
619 StartsWithASCII(passphrase, "3:", false)) {
620 return true;
621 } else {
622 error->Populate(Error::kInvalidPassphrase);
623 return false;
624 }
625}
626
627// static
628bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
629 if (StartsWithASCII(passphrase, "0x", false)) {
630 return true;
631 } else {
632 error->Populate(Error::kInvalidPassphrase);
633 return false;
634 }
635}
636
Paul Stewart6ab23a92011-11-09 17:17:47 -0800637// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800638string WiFiService::GetSecurityClass(const string &security) {
639 if (security == flimflam::kSecurityRsn ||
640 security == flimflam::kSecurityWpa) {
641 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700642 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800643 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700644 }
645}
646
Paul Stewarta41e38d2011-11-11 07:47:29 -0800647// static
648bool WiFiService::ParseStorageIdentifier(const string &storage_name,
649 string *address,
650 string *mode,
651 string *security) {
652 vector<string> wifi_parts;
653 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800654 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
655 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800656 return false;
657 }
658 *address = wifi_parts[1];
659 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800660 if (wifi_parts.size() == 5) {
661 *security = wifi_parts[4];
662 } else {
663 // Account for security type "802_1x" which got split up above.
664 *security = wifi_parts[4] + "_" + wifi_parts[5];
665 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800666 return true;
667}
668
mukesh agrawale1d90e92012-02-15 17:36:08 -0800669// static
670uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
671 int16 strength;
672 if (signal_dbm > 0) {
673 if (!logged_signal_warning) {
674 LOG(WARNING) << "Signal strength is suspiciously high. "
675 << "Assuming value " << signal_dbm << " is not in dBm.";
676 logged_signal_warning = true;
677 }
678 strength = signal_dbm;
679 } else {
680 strength = 120 + signal_dbm; // Call -20dBm "perfect".
681 }
682
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800683 if (strength > kStrengthMax) {
684 strength = kStrengthMax;
685 } else if (strength < kStrengthMin) {
686 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800687 }
688 return strength;
689}
690
Paul Stewart6ab23a92011-11-09 17:17:47 -0800691string WiFiService::GetGenericStorageIdentifier() const {
692 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
693}
694
Paul Stewartd08f4432011-11-04 07:48:20 -0700695string WiFiService::GetSpecificStorageIdentifier() const {
696 return GetStorageIdentifierForSecurity(security_);
697}
698
699string WiFiService::GetStorageIdentifierForSecurity(
700 const string &security) const {
701 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
702 flimflam::kTypeWifi,
703 wifi_->address().c_str(),
704 hex_ssid_.c_str(),
705 mode_.c_str(),
706 security.c_str()));
707}
708
Gaurav Shah10109f22011-11-11 20:16:22 -0800709void WiFiService::set_eap(const EapCredentials &eap) {
710 Service::set_eap(eap);
711 UpdateConnectable();
712}
713
Gaurav Shah29d68882012-01-30 19:06:42 -0800714bool WiFiService::Is8021x() const {
715 if (security_ == flimflam::kSecurity8021x)
716 return true;
717
718 // Dynamic WEP + 802.1x.
719 if (security_ == flimflam::kSecurityWep &&
720 GetEAPKeyManagement() == "IEEE8021X")
721 return true;
722 return false;
723}
724
Gaurav Shah10109f22011-11-11 20:16:22 -0800725void WiFiService::Populate8021xProperties(
726 std::map<string, DBus::Variant> *params) {
727 typedef std::pair<const char *, const char *> KeyVal;
728 KeyVal propertyvals[] = {
729 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
730 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
731 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
732 eap().inner_eap.c_str()),
733 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
734 eap().anonymous_identity.c_str()),
735 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
736 eap().client_cert.c_str()),
737 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
738 eap().private_key.c_str()),
739 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
740 eap().private_key_password.c_str()),
741 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
742 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
743 eap().password.c_str()),
744 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
745 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
746 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
747 eap().ca_cert_id.c_str()),
748 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
749 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
750 // crosbug.com/25663
751 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
752 };
753
754 DBus::MessageIter writer;
755 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
756 if (strlen(propertyvals[i].second) > 0) {
757 (*params)[propertyvals[i].first].writer().
758 append_string(propertyvals[i].second);
759 }
760 }
761}
762
mukesh agrawalb54601c2011-06-07 17:39:22 -0700763} // namespace shill