blob: 0567278994b40fefc3099ad23c05592b847ea841 [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
18#include "shill/control_interface.h"
19#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070020#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070022#include "shill/ieee80211.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000023#include "shill/metrics.h"
Thieu Lef7709452011-11-15 01:13:19 +000024#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070025#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070026#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070027#include "shill/wifi_endpoint.h"
28#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070029
mukesh agrawal261daca2011-12-02 18:56:56 +000030using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070031using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070032using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070033
34namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070035
Paul Stewartd08f4432011-11-04 07:48:20 -070036const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
Paul Stewart2706aaf2011-12-14 16:44:04 -080037const char WiFiService::kStorageMode[] = "WiFi.Mode";
38const char WiFiService::kStoragePassphrase[] = "Passphrase";
39const char WiFiService::kStorageSecurity[] = "WiFi.Security";
40const char WiFiService::kStorageSSID[] = "SSID";
Paul Stewartd08f4432011-11-04 07:48:20 -070041
mukesh agrawalb54601c2011-06-07 17:39:22 -070042WiFiService::WiFiService(ControlInterface *control_interface,
43 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080044 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070045 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070046 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080047 const vector<uint8_t> &ssid,
48 const string &mode,
49 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080050 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080051 : Service(control_interface, dispatcher, metrics, manager,
52 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070053 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070054 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070055 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080056 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000057 frequency_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070058 task_factory_(this),
59 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070060 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070061 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070062 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080063 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
64 &WiFiService::SetPassphrase,
65 &WiFiService::ClearPassphrase,
66 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070067 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
68 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070069
Paul Stewartac4ac002011-08-26 12:04:26 -070070 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
71 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
72 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
73 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070074
mukesh agrawald835b202011-10-07 15:26:47 -070075 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
76 string ssid_string(
77 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
mukesh agrawal16bc1b82012-02-09 18:38:26 -080078 if (WiFi::SanitizeSSID(&ssid_string)) {
mukesh agrawald835b202011-10-07 15:26:47 -070079 // WifiHexSsid property should only be present if Name property
80 // has been munged.
81 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
82 }
83 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070084
mukesh agrawal6e277772011-09-29 15:04:23 -070085 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
86 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080087 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080088 // Passphrases are not mandatory for 802.1X.
89 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070090 } else if (security_ == flimflam::kSecurityPsk) {
91 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070092 } else if (security_ == flimflam::kSecurityRsn) {
93 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070094 } else if (security_ == flimflam::kSecurityWpa) {
95 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070096 } else if (security_ == flimflam::kSecurityWep) {
97 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -070098 } else if (security_ == flimflam::kSecurityNone) {
99 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700100 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800101 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700102 }
103
Paul Stewartd08f4432011-11-04 07:48:20 -0700104 // Until we know better (at Profile load time), use the generic name.
105 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000106 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700107}
108
109WiFiService::~WiFiService() {
110 LOG(INFO) << __func__;
111}
112
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000113void WiFiService::AutoConnect() {
114 if (IsAutoConnectable()) {
115 // Execute immediately, for two reasons:
116 //
117 // 1. We need IsAutoConnectable to return the correct value for
118 // other WiFiServices, and that depends on WiFi's state.
119 //
120 // 2. We should probably limit the extent to which we queue up
121 // actions (such as AutoConnect) which depend on current state.
122 // If we queued AutoConnects, we could build a long queue of
123 // useless work (one AutoConnect per Service), which blocks
124 // more timely work.
125 ConnectTask();
mukesh agrawal592516d2012-01-12 14:01:00 -0800126 } else {
127 LOG(INFO) << "Suppressed autoconnect to " << friendly_name();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000128 }
129}
130
mukesh agrawal1830fa12011-09-26 14:31:40 -0700131void WiFiService::Connect(Error */*error*/) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800132 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000133 // Defer handling, since dbus-c++ does not permit us to send an
134 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700135 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700136 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700137}
138
mukesh agrawaladb68482012-01-17 16:31:51 -0800139void WiFiService::Disconnect(Error *error) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000140 LOG(INFO) << __func__;
mukesh agrawaladb68482012-01-17 16:31:51 -0800141 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000142 // Defer handling, since dbus-c++ does not permit us to send an
143 // outbound request while processing an inbound one.
144 dispatcher()->PostTask(
145 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700146}
147
Paul Stewart22aa71b2011-09-16 12:15:11 -0700148bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
149 return wifi_->TechnologyIs(type);
150}
151
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000152bool WiFiService::IsAutoConnectable() const {
mukesh agrawaladb68482012-01-17 16:31:51 -0800153 return Service::IsAutoConnectable() &&
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000154 // Only auto-connect to Services which have visible Endpoints.
155 // (Needed because hidden Services may remain registered with
156 // Manager even without visible Endpoints.)
mukesh agrawaladb68482012-01-17 16:31:51 -0800157 HasEndpoints() &&
mukesh agrawal76d13882012-01-12 15:23:11 -0800158 // Do not preempt an existing connection (whether pending, or
159 // connected, and whether to this service, or another).
mukesh agrawaladb68482012-01-17 16:31:51 -0800160 wifi_->IsIdle();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000161}
162
163bool WiFiService::IsConnecting() const {
164 // WiFi does not move us into the associating state until it gets
165 // feedback from wpa_supplicant. So, to answer whether or
166 // not we're connecting, we consult with |wifi_|.
167 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800168}
169
mukesh agrawal261daca2011-12-02 18:56:56 +0000170void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
171 DCHECK(endpoint->ssid() == ssid());
172 endpoints_.insert(endpoint);
Thieu Lee41a72d2012-02-06 20:46:51 +0000173 // TODO: Track signal strength (crosbug.com/16786).
mukesh agrawal261daca2011-12-02 18:56:56 +0000174}
175
176void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
177 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
178 DCHECK(i != endpoints_.end());
179 if (i == endpoints_.end()) {
180 LOG(WARNING) << "In " << __func__ << "(): "
181 << "ignorning non-existent endpoint "
182 << endpoint->bssid_string();
183 return;
184 }
185 endpoints_.erase(i);
186}
187
Thieu Lee41a72d2012-02-06 20:46:51 +0000188void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint &endpoint) {
189 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
190 frequency_ = endpoint.frequency();
191 // TODO: Copy BSSID here (crosbug.com/22377).
192 // TODO: Copy signal strength (crosbug.com/16786).
193 // TODO(thieule): Update these values when supplicant signals that they
194 // have changed.
195 // (crosbug.com/16786)
196}
197
Chris Masone6515aab2011-10-12 16:19:09 -0700198string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700199 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700200}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700201
mukesh agrawal1a056262011-10-05 14:36:54 -0700202void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
203 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000204 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700205 } else if (security_ == flimflam::kSecurityPsk ||
206 security_ == flimflam::kSecurityWpa ||
207 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000208 ValidateWPAPassphrase(passphrase, error);
209 } else {
210 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700211 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000212
Paul Stewart2706aaf2011-12-14 16:44:04 -0800213 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000214 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800215 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000216
217 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700218}
219
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800220// ClearPassphrase is separate from SetPassphrase, because the default
221// value for |passphrase_| would not pass validation.
222void WiFiService::ClearPassphrase(Error */*error*/) {
223 passphrase_.clear();
224 UpdateConnectable();
225}
226
Paul Stewartd08f4432011-11-04 07:48:20 -0700227bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
228 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
229 storage->ContainsGroup(GetSpecificStorageIdentifier());
230}
231
Paul Stewarta41e38d2011-11-11 07:47:29 -0800232bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800233 // WiFi Services should be displayed only if they are in range (have
234 // endpoints that have shown up in a scan) or if the service is actively
235 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000236 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800237}
238
Paul Stewartd08f4432011-11-04 07:48:20 -0700239bool WiFiService::Load(StoreInterface *storage) {
240 // First find out which storage identifier is available in priority order
241 // of specific, generic.
242 string id = GetSpecificStorageIdentifier();
243 if (!storage->ContainsGroup(id)) {
244 id = GetGenericStorageIdentifier();
245 if (!storage->ContainsGroup(id)) {
246 LOG(WARNING) << "Service is not available in the persistent store: "
247 << id;
248 return false;
249 }
250 }
251
252 // Set our storage identifier to match the storage name in the Profile.
253 storage_identifier_ = id;
254
255 // Load properties common to all Services.
256 if (!Service::Load(storage)) {
257 return false;
258 }
259
260 // Load properties specific to WiFi services.
261 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000262
Paul Stewart2706aaf2011-12-14 16:44:04 -0800263 // NB: mode, security and ssid parameters are never read in from
264 // Load() as they are provided from the scan.
265
266 string passphrase;
267 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
268 Error error;
269 SetPassphrase(passphrase, &error);
270 if (!error.IsSuccess()) {
271 LOG(ERROR) << "Passphrase could not be set: "
272 << Error::GetName(error.type());
273 }
274 }
275
Paul Stewartd08f4432011-11-04 07:48:20 -0700276 return true;
277}
278
279bool WiFiService::Save(StoreInterface *storage) {
280 // Save properties common to all Services.
281 if (!Service::Save(storage)) {
282 return false;
283 }
284
285 // Save properties specific to WiFi services.
286 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800287 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
288 storage->SetString(id, kStorageMode, mode_);
289 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
290 storage->SetString(id, kStorageSecurity, security_);
291 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000292
293 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700294 return true;
295}
296
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800297void WiFiService::Unload() {
298 Service::Unload();
299 hidden_ssid_ = false;
300 passphrase_ = "";
301 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800302 if (security_ == flimflam::kSecurity8021x) {
303 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
304 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
305 // Make sure that these are cleared when credentials for networks
306 // of this type goes away.
307 //
308 // When wpa_supplicant gains the ability, do this credential
309 // clearing on a per-service basis. Also do this whenever the credentials
310 // for a service changes. crosbug.com/25670
311 wifi_->ClearCachedCredentials();
312 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800313}
314
Paul Stewart6ab23a92011-11-09 17:17:47 -0800315bool WiFiService::IsSecurityMatch(const string &security) const {
316 return GetSecurityClass(security) == GetSecurityClass(security_);
317}
318
Thieu Le48e6d6d2011-12-06 00:40:27 +0000319void WiFiService::InitializeCustomMetrics() const {
320 string histogram = metrics()->GetFullMetricName(
321 Metrics::kMetricTimeToJoinMilliseconds,
322 technology());
323 metrics()->AddServiceStateTransitionTimer(this,
324 histogram,
325 Service::kStateAssociating,
326 Service::kStateConfiguring);
327}
328
329void WiFiService::SendPostReadyStateMetrics() const {
330 // TODO(thieule): Send physical mode and security metrics.
331 // crosbug.com/24441
332 metrics()->SendEnumToUMA(
333 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
334 technology()),
335 Metrics::WiFiFrequencyToChannel(frequency_),
336 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000337
338 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
339 metrics()->SendEnumToUMA(
340 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
341 technology()),
342 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
343 Metrics::kWiFiNetworkPhyModeMax);
344
345 Metrics::WiFiSecurity security_uma =
346 Metrics::WiFiSecurityStringToEnum(security_);
347 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
348 metrics()->SendEnumToUMA(
349 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
350 technology()),
351 security_uma,
352 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000353}
354
mukesh agrawal32399322011-09-01 10:53:43 -0700355// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800356void WiFiService::HelpRegisterWriteOnlyDerivedString(
357 const string &name,
358 void(WiFiService::*set)(const string &, Error *),
359 void(WiFiService::*clear)(Error *),
360 const string *default_value) {
361 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000362 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800363 StringAccessor(
364 new CustomWriteOnlyAccessor<WiFiService, string>(
365 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000366}
367
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700368void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700369 std::map<string, DBus::Variant> params;
370 DBus::MessageIter writer;
371
372 params[wpa_supplicant::kNetworkPropertyMode].writer().
373 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
374
Gaurav Shah29d68882012-01-30 19:06:42 -0800375 if (Is8021x()) {
376 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800377 if (GetEAPKeyManagement().empty())
378 SetEAPKeyManagement("WPA-EAP");
379 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700380 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800381 const string psk_proto = StringPrintf("%s %s",
382 wpa_supplicant::kSecurityModeWPA,
383 wpa_supplicant::kSecurityModeRSN);
384 params[wpa_supplicant::kPropertySecurityProtocol].writer().
385 append_string(psk_proto.c_str());
386 params[wpa_supplicant::kPropertyPreSharedKey].writer().
387 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700388 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700389 params[wpa_supplicant::kPropertySecurityProtocol].writer().
390 append_string(wpa_supplicant::kSecurityModeRSN);
391 params[wpa_supplicant::kPropertyPreSharedKey].writer().
392 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700393 } else if (security_ == flimflam::kSecurityWpa) {
394 params[wpa_supplicant::kPropertySecurityProtocol].writer().
395 append_string(wpa_supplicant::kSecurityModeWPA);
396 params[wpa_supplicant::kPropertyPreSharedKey].writer().
397 append_string(passphrase_.c_str());
398 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000399 params[wpa_supplicant::kPropertyAuthAlg].writer().
400 append_string(wpa_supplicant::kSecurityAuthAlg);
401 Error error;
402 int key_index;
403 std::vector<uint8> password_bytes;
404 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
405 writer = params[wpa_supplicant::kPropertyWEPKey +
406 base::IntToString(key_index)].writer();
407 writer << password_bytes;
408 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
409 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700410 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800411 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700412 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800413 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700414 }
415
Gaurav Shah10109f22011-11-11 20:16:22 -0800416 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700417 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800418
419 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700420 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
421 writer << ssid_;
422
423 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700424}
425
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000426void WiFiService::DisconnectTask() {
427 wifi_->DisconnectFrom(this);
428}
429
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800430string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700431 return wifi_->GetRpcIdentifier();
432}
433
mukesh agrawal29c13a12011-11-24 00:09:19 +0000434void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800435 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000436 if (security_ == flimflam::kSecurityNone) {
437 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800438 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800439 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800440 } else if (Is8021x()) {
441 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000442 } else if (security_ == flimflam::kSecurityWep ||
443 security_ == flimflam::kSecurityWpa ||
444 security_ == flimflam::kSecurityPsk ||
445 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800446 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800447 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000448 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800449 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000450}
451
mukesh agrawal1a056262011-10-05 14:36:54 -0700452// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000453void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
454 Error *error) {
455 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700456}
457
458// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000459void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
460 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700461 unsigned int length = passphrase.length();
462 vector<uint8> passphrase_bytes;
463
464 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
465 if (length != IEEE_80211::kWPAHexLen &&
466 (length < IEEE_80211::kWPAAsciiMinLen ||
467 length > IEEE_80211::kWPAAsciiMaxLen)) {
468 error->Populate(Error::kInvalidPassphrase);
469 }
470 } else {
471 if (length < IEEE_80211::kWPAAsciiMinLen ||
472 length > IEEE_80211::kWPAAsciiMaxLen) {
473 error->Populate(Error::kInvalidPassphrase);
474 }
475 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000476}
mukesh agrawal1a056262011-10-05 14:36:54 -0700477
Thieu Lef4cbda92011-11-10 23:41:24 +0000478// static
479void WiFiService::ParseWEPPassphrase(const string &passphrase,
480 int *key_index,
481 std::vector<uint8> *password_bytes,
482 Error *error) {
483 unsigned int length = passphrase.length();
484 int key_index_local;
485 std::string password_text;
486 bool is_hex = false;
487
488 switch (length) {
489 case IEEE_80211::kWEP40AsciiLen:
490 case IEEE_80211::kWEP104AsciiLen:
491 key_index_local = 0;
492 password_text = passphrase;
493 break;
494 case IEEE_80211::kWEP40AsciiLen + 2:
495 case IEEE_80211::kWEP104AsciiLen + 2:
496 if (CheckWEPKeyIndex(passphrase, error)) {
497 base::StringToInt(passphrase.substr(0,1), &key_index_local);
498 password_text = passphrase.substr(2);
499 }
500 break;
501 case IEEE_80211::kWEP40HexLen:
502 case IEEE_80211::kWEP104HexLen:
503 if (CheckWEPIsHex(passphrase, error)) {
504 key_index_local = 0;
505 password_text = passphrase;
506 is_hex = true;
507 }
508 break;
509 case IEEE_80211::kWEP40HexLen + 2:
510 case IEEE_80211::kWEP104HexLen + 2:
511 if(CheckWEPKeyIndex(passphrase, error) &&
512 CheckWEPIsHex(passphrase.substr(2), error)) {
513 base::StringToInt(passphrase.substr(0,1), &key_index_local);
514 password_text = passphrase.substr(2);
515 is_hex = true;
516 } else if (CheckWEPPrefix(passphrase, error) &&
517 CheckWEPIsHex(passphrase.substr(2), error)) {
518 key_index_local = 0;
519 password_text = passphrase.substr(2);
520 is_hex = true;
521 }
522 break;
523 case IEEE_80211::kWEP40HexLen + 4:
524 case IEEE_80211::kWEP104HexLen + 4:
525 if (CheckWEPKeyIndex(passphrase, error) &&
526 CheckWEPPrefix(passphrase.substr(2), error) &&
527 CheckWEPIsHex(passphrase.substr(4), error)) {
528 base::StringToInt(passphrase.substr(0,1), &key_index_local);
529 password_text = passphrase.substr(4);
530 is_hex = true;
531 }
532 break;
533 default:
534 error->Populate(Error::kInvalidPassphrase);
535 break;
536 }
537
mukesh agrawal1a056262011-10-05 14:36:54 -0700538 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000539 if (key_index)
540 *key_index = key_index_local;
541 if (password_bytes) {
542 if (is_hex)
543 base::HexStringToBytes(password_text, password_bytes);
544 else
545 password_bytes->insert(password_bytes->end(),
546 password_text.begin(),
547 password_text.end());
548 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700549 }
550}
551
552// static
553bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
554 vector<uint8> passphrase_bytes;
555 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
556 return true;
557 } else {
558 error->Populate(Error::kInvalidPassphrase);
559 return false;
560 }
561}
562
563// static
564bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
565 if (StartsWithASCII(passphrase, "0:", false) ||
566 StartsWithASCII(passphrase, "1:", false) ||
567 StartsWithASCII(passphrase, "2:", false) ||
568 StartsWithASCII(passphrase, "3:", false)) {
569 return true;
570 } else {
571 error->Populate(Error::kInvalidPassphrase);
572 return false;
573 }
574}
575
576// static
577bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
578 if (StartsWithASCII(passphrase, "0x", false)) {
579 return true;
580 } else {
581 error->Populate(Error::kInvalidPassphrase);
582 return false;
583 }
584}
585
Paul Stewart6ab23a92011-11-09 17:17:47 -0800586// static
Paul Stewart6ab23a92011-11-09 17:17:47 -0800587string WiFiService::GetSecurityClass(const string &security) {
588 if (security == flimflam::kSecurityRsn ||
589 security == flimflam::kSecurityWpa) {
590 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700591 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800592 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700593 }
594}
595
Paul Stewarta41e38d2011-11-11 07:47:29 -0800596// static
597bool WiFiService::ParseStorageIdentifier(const string &storage_name,
598 string *address,
599 string *mode,
600 string *security) {
601 vector<string> wifi_parts;
602 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800603 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
604 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800605 return false;
606 }
607 *address = wifi_parts[1];
608 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800609 if (wifi_parts.size() == 5) {
610 *security = wifi_parts[4];
611 } else {
612 // Account for security type "802_1x" which got split up above.
613 *security = wifi_parts[4] + "_" + wifi_parts[5];
614 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800615 return true;
616}
617
Paul Stewart6ab23a92011-11-09 17:17:47 -0800618string WiFiService::GetGenericStorageIdentifier() const {
619 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
620}
621
Paul Stewartd08f4432011-11-04 07:48:20 -0700622string WiFiService::GetSpecificStorageIdentifier() const {
623 return GetStorageIdentifierForSecurity(security_);
624}
625
626string WiFiService::GetStorageIdentifierForSecurity(
627 const string &security) const {
628 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
629 flimflam::kTypeWifi,
630 wifi_->address().c_str(),
631 hex_ssid_.c_str(),
632 mode_.c_str(),
633 security.c_str()));
634}
635
Gaurav Shah10109f22011-11-11 20:16:22 -0800636void WiFiService::set_eap(const EapCredentials &eap) {
637 Service::set_eap(eap);
638 UpdateConnectable();
639}
640
Gaurav Shah29d68882012-01-30 19:06:42 -0800641bool WiFiService::Is8021x() const {
642 if (security_ == flimflam::kSecurity8021x)
643 return true;
644
645 // Dynamic WEP + 802.1x.
646 if (security_ == flimflam::kSecurityWep &&
647 GetEAPKeyManagement() == "IEEE8021X")
648 return true;
649 return false;
650}
651
Gaurav Shah10109f22011-11-11 20:16:22 -0800652void WiFiService::Populate8021xProperties(
653 std::map<string, DBus::Variant> *params) {
654 typedef std::pair<const char *, const char *> KeyVal;
655 KeyVal propertyvals[] = {
656 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
657 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
658 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
659 eap().inner_eap.c_str()),
660 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
661 eap().anonymous_identity.c_str()),
662 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
663 eap().client_cert.c_str()),
664 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
665 eap().private_key.c_str()),
666 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
667 eap().private_key_password.c_str()),
668 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
669 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
670 eap().password.c_str()),
671 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
672 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
673 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
674 eap().ca_cert_id.c_str()),
675 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
676 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
677 // crosbug.com/25663
678 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
679 };
680
681 DBus::MessageIter writer;
682 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
683 if (strlen(propertyvals[i].second) > 0) {
684 (*params)[propertyvals[i].first].writer().
685 append_string(propertyvals[i].second);
686 }
687 }
688}
689
mukesh agrawalb54601c2011-06-07 17:39:22 -0700690} // namespace shill