blob: f3dbe56d9eaf3ce5c0d15009ff585f768ff4c90d [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
337void WiFiService::SendPostReadyStateMetrics() const {
338 // TODO(thieule): Send physical mode and security metrics.
339 // crosbug.com/24441
340 metrics()->SendEnumToUMA(
341 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
342 technology()),
343 Metrics::WiFiFrequencyToChannel(frequency_),
344 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000345
346 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
347 metrics()->SendEnumToUMA(
348 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
349 technology()),
350 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
351 Metrics::kWiFiNetworkPhyModeMax);
352
353 Metrics::WiFiSecurity security_uma =
354 Metrics::WiFiSecurityStringToEnum(security_);
355 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
356 metrics()->SendEnumToUMA(
357 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
358 technology()),
359 security_uma,
360 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000361}
362
mukesh agrawal32399322011-09-01 10:53:43 -0700363// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800364void WiFiService::HelpRegisterWriteOnlyDerivedString(
365 const string &name,
366 void(WiFiService::*set)(const string &, Error *),
367 void(WiFiService::*clear)(Error *),
368 const string *default_value) {
369 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000370 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800371 StringAccessor(
372 new CustomWriteOnlyAccessor<WiFiService, string>(
373 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000374}
375
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700376void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700377 std::map<string, DBus::Variant> params;
378 DBus::MessageIter writer;
379
380 params[wpa_supplicant::kNetworkPropertyMode].writer().
381 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
382
Gaurav Shah29d68882012-01-30 19:06:42 -0800383 if (Is8021x()) {
384 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800385 if (GetEAPKeyManagement().empty())
386 SetEAPKeyManagement("WPA-EAP");
387 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700388 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800389 const string psk_proto = StringPrintf("%s %s",
390 wpa_supplicant::kSecurityModeWPA,
391 wpa_supplicant::kSecurityModeRSN);
392 params[wpa_supplicant::kPropertySecurityProtocol].writer().
393 append_string(psk_proto.c_str());
394 params[wpa_supplicant::kPropertyPreSharedKey].writer().
395 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700396 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700397 params[wpa_supplicant::kPropertySecurityProtocol].writer().
398 append_string(wpa_supplicant::kSecurityModeRSN);
399 params[wpa_supplicant::kPropertyPreSharedKey].writer().
400 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700401 } else if (security_ == flimflam::kSecurityWpa) {
402 params[wpa_supplicant::kPropertySecurityProtocol].writer().
403 append_string(wpa_supplicant::kSecurityModeWPA);
404 params[wpa_supplicant::kPropertyPreSharedKey].writer().
405 append_string(passphrase_.c_str());
406 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000407 params[wpa_supplicant::kPropertyAuthAlg].writer().
408 append_string(wpa_supplicant::kSecurityAuthAlg);
409 Error error;
410 int key_index;
411 std::vector<uint8> password_bytes;
412 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
413 writer = params[wpa_supplicant::kPropertyWEPKey +
414 base::IntToString(key_index)].writer();
415 writer << password_bytes;
416 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
417 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700418 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800419 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700420 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800421 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700422 }
423
Gaurav Shah10109f22011-11-11 20:16:22 -0800424 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700425 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800426
427 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700428 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
429 writer << ssid_;
430
431 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700432}
433
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000434void WiFiService::DisconnectTask() {
435 wifi_->DisconnectFrom(this);
436}
437
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800438string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700439 return wifi_->GetRpcIdentifier();
440}
441
mukesh agrawal29c13a12011-11-24 00:09:19 +0000442void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800443 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000444 if (security_ == flimflam::kSecurityNone) {
445 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800446 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800447 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800448 } else if (Is8021x()) {
449 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000450 } else if (security_ == flimflam::kSecurityWep ||
451 security_ == flimflam::kSecurityWpa ||
452 security_ == flimflam::kSecurityPsk ||
453 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800454 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800455 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000456 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800457 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000458}
459
mukesh agrawale1d90e92012-02-15 17:36:08 -0800460void WiFiService::UpdateFromEndpoints() {
461 const WiFiEndpoint *representative_endpoint = NULL;
462
463 if (current_endpoint_) {
464 // TODO: Copy BSSID here (crosbug.com/22377).
465 representative_endpoint = current_endpoint_;
466 } else {
467 int16 best_signal = std::numeric_limits<int16>::min();
468 for (set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.begin();
469 i != endpoints_.end(); ++i) {
470 if ((*i)->signal_strength() >= best_signal) {
471 best_signal = (*i)->signal_strength();
472 representative_endpoint = *i;
473 }
474 }
475 }
476
477 uint16 frequency;
478 int16 signal;
479 if (!representative_endpoint) {
480 frequency = 0;
481 signal = std::numeric_limits<int16>::min();
482 } else {
483 frequency = representative_endpoint->frequency();
484 signal = representative_endpoint->signal_strength();
485 }
486
487 if (frequency_ != frequency) {
488 frequency_ = frequency;
489 adaptor()->EmitUint16Changed(flimflam::kWifiFrequency, frequency_);
490 }
491 SetStrength(SignalToStrength(signal));
492}
493
mukesh agrawal1a056262011-10-05 14:36:54 -0700494// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000495void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
496 Error *error) {
497 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700498}
499
500// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000501void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
502 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700503 unsigned int length = passphrase.length();
504 vector<uint8> passphrase_bytes;
505
506 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
507 if (length != IEEE_80211::kWPAHexLen &&
508 (length < IEEE_80211::kWPAAsciiMinLen ||
509 length > IEEE_80211::kWPAAsciiMaxLen)) {
510 error->Populate(Error::kInvalidPassphrase);
511 }
512 } else {
513 if (length < IEEE_80211::kWPAAsciiMinLen ||
514 length > IEEE_80211::kWPAAsciiMaxLen) {
515 error->Populate(Error::kInvalidPassphrase);
516 }
517 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000518}
mukesh agrawal1a056262011-10-05 14:36:54 -0700519
Thieu Lef4cbda92011-11-10 23:41:24 +0000520// static
521void WiFiService::ParseWEPPassphrase(const string &passphrase,
522 int *key_index,
523 std::vector<uint8> *password_bytes,
524 Error *error) {
525 unsigned int length = passphrase.length();
526 int key_index_local;
527 std::string password_text;
528 bool is_hex = false;
529
530 switch (length) {
531 case IEEE_80211::kWEP40AsciiLen:
532 case IEEE_80211::kWEP104AsciiLen:
533 key_index_local = 0;
534 password_text = passphrase;
535 break;
536 case IEEE_80211::kWEP40AsciiLen + 2:
537 case IEEE_80211::kWEP104AsciiLen + 2:
538 if (CheckWEPKeyIndex(passphrase, error)) {
539 base::StringToInt(passphrase.substr(0,1), &key_index_local);
540 password_text = passphrase.substr(2);
541 }
542 break;
543 case IEEE_80211::kWEP40HexLen:
544 case IEEE_80211::kWEP104HexLen:
545 if (CheckWEPIsHex(passphrase, error)) {
546 key_index_local = 0;
547 password_text = passphrase;
548 is_hex = true;
549 }
550 break;
551 case IEEE_80211::kWEP40HexLen + 2:
552 case IEEE_80211::kWEP104HexLen + 2:
553 if(CheckWEPKeyIndex(passphrase, error) &&
554 CheckWEPIsHex(passphrase.substr(2), error)) {
555 base::StringToInt(passphrase.substr(0,1), &key_index_local);
556 password_text = passphrase.substr(2);
557 is_hex = true;
558 } else if (CheckWEPPrefix(passphrase, error) &&
559 CheckWEPIsHex(passphrase.substr(2), error)) {
560 key_index_local = 0;
561 password_text = passphrase.substr(2);
562 is_hex = true;
563 }
564 break;
565 case IEEE_80211::kWEP40HexLen + 4:
566 case IEEE_80211::kWEP104HexLen + 4:
567 if (CheckWEPKeyIndex(passphrase, error) &&
568 CheckWEPPrefix(passphrase.substr(2), error) &&
569 CheckWEPIsHex(passphrase.substr(4), error)) {
570 base::StringToInt(passphrase.substr(0,1), &key_index_local);
571 password_text = passphrase.substr(4);
572 is_hex = true;
573 }
574 break;
575 default:
576 error->Populate(Error::kInvalidPassphrase);
577 break;
578 }
579
mukesh agrawal1a056262011-10-05 14:36:54 -0700580 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000581 if (key_index)
582 *key_index = key_index_local;
583 if (password_bytes) {
584 if (is_hex)
585 base::HexStringToBytes(password_text, password_bytes);
586 else
587 password_bytes->insert(password_bytes->end(),
588 password_text.begin(),
589 password_text.end());
590 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700591 }
592}
593
594// static
595bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
596 vector<uint8> passphrase_bytes;
597 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
598 return true;
599 } else {
600 error->Populate(Error::kInvalidPassphrase);
601 return false;
602 }
603}
604
605// static
606bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
607 if (StartsWithASCII(passphrase, "0:", false) ||
608 StartsWithASCII(passphrase, "1:", false) ||
609 StartsWithASCII(passphrase, "2:", false) ||
610 StartsWithASCII(passphrase, "3:", false)) {
611 return true;
612 } else {
613 error->Populate(Error::kInvalidPassphrase);
614 return false;
615 }
616}
617
618// static
619bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
620 if (StartsWithASCII(passphrase, "0x", false)) {
621 return true;
622 } else {
623 error->Populate(Error::kInvalidPassphrase);
624 return false;
625 }
626}
627
Paul Stewart6ab23a92011-11-09 17:17:47 -0800628// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800629string WiFiService::GetSecurityClass(const string &security) {
630 if (security == flimflam::kSecurityRsn ||
631 security == flimflam::kSecurityWpa) {
632 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700633 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800634 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700635 }
636}
637
Paul Stewarta41e38d2011-11-11 07:47:29 -0800638// static
639bool WiFiService::ParseStorageIdentifier(const string &storage_name,
640 string *address,
641 string *mode,
642 string *security) {
643 vector<string> wifi_parts;
644 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800645 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
646 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800647 return false;
648 }
649 *address = wifi_parts[1];
650 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800651 if (wifi_parts.size() == 5) {
652 *security = wifi_parts[4];
653 } else {
654 // Account for security type "802_1x" which got split up above.
655 *security = wifi_parts[4] + "_" + wifi_parts[5];
656 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800657 return true;
658}
659
mukesh agrawale1d90e92012-02-15 17:36:08 -0800660// static
661uint8 WiFiService::SignalToStrength(int16 signal_dbm) {
662 int16 strength;
663 if (signal_dbm > 0) {
664 if (!logged_signal_warning) {
665 LOG(WARNING) << "Signal strength is suspiciously high. "
666 << "Assuming value " << signal_dbm << " is not in dBm.";
667 logged_signal_warning = true;
668 }
669 strength = signal_dbm;
670 } else {
671 strength = 120 + signal_dbm; // Call -20dBm "perfect".
672 }
673
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800674 if (strength > kStrengthMax) {
675 strength = kStrengthMax;
676 } else if (strength < kStrengthMin) {
677 strength = kStrengthMin;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800678 }
679 return strength;
680}
681
Paul Stewart6ab23a92011-11-09 17:17:47 -0800682string WiFiService::GetGenericStorageIdentifier() const {
683 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
684}
685
Paul Stewartd08f4432011-11-04 07:48:20 -0700686string WiFiService::GetSpecificStorageIdentifier() const {
687 return GetStorageIdentifierForSecurity(security_);
688}
689
690string WiFiService::GetStorageIdentifierForSecurity(
691 const string &security) const {
692 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
693 flimflam::kTypeWifi,
694 wifi_->address().c_str(),
695 hex_ssid_.c_str(),
696 mode_.c_str(),
697 security.c_str()));
698}
699
Gaurav Shah10109f22011-11-11 20:16:22 -0800700void WiFiService::set_eap(const EapCredentials &eap) {
701 Service::set_eap(eap);
702 UpdateConnectable();
703}
704
Gaurav Shah29d68882012-01-30 19:06:42 -0800705bool WiFiService::Is8021x() const {
706 if (security_ == flimflam::kSecurity8021x)
707 return true;
708
709 // Dynamic WEP + 802.1x.
710 if (security_ == flimflam::kSecurityWep &&
711 GetEAPKeyManagement() == "IEEE8021X")
712 return true;
713 return false;
714}
715
Gaurav Shah10109f22011-11-11 20:16:22 -0800716void WiFiService::Populate8021xProperties(
717 std::map<string, DBus::Variant> *params) {
718 typedef std::pair<const char *, const char *> KeyVal;
719 KeyVal propertyvals[] = {
720 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
721 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
722 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
723 eap().inner_eap.c_str()),
724 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
725 eap().anonymous_identity.c_str()),
726 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
727 eap().client_cert.c_str()),
728 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
729 eap().private_key.c_str()),
730 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
731 eap().private_key_password.c_str()),
732 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
733 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
734 eap().password.c_str()),
735 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
736 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
737 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
738 eap().ca_cert_id.c_str()),
739 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
740 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
741 // crosbug.com/25663
742 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
743 };
744
745 DBus::MessageIter writer;
746 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
747 if (strlen(propertyvals[i].second) > 0) {
748 (*params)[propertyvals[i].first].writer().
749 append_string(propertyvals[i].second);
750 }
751 }
752}
753
mukesh agrawalb54601c2011-06-07 17:39:22 -0700754} // namespace shill