blob: 8ef1722c83252ddaf01d91070797a8f54722f9d8 [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 agrawald835b202011-10-07 15:26:47 -070017#include <glib.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070018
19#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";
Paul Stewartd08f4432011-11-04 07:48:20 -070042
mukesh agrawalb54601c2011-06-07 17:39:22 -070043WiFiService::WiFiService(ControlInterface *control_interface,
44 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080045 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070046 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070047 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080048 const vector<uint8_t> &ssid,
49 const string &mode,
50 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080051 bool hidden_ssid)
Thieu Le3426c8f2012-01-11 17:35:11 -080052 : Service(control_interface, dispatcher, metrics, manager,
53 Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070054 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070055 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070056 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080057 hidden_ssid_(hidden_ssid),
Thieu Lee41a72d2012-02-06 20:46:51 +000058 frequency_(0),
mukesh agrawalb54601c2011-06-07 17:39:22 -070059 task_factory_(this),
60 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070061 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070062 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070063 store->RegisterConstString(flimflam::kModeProperty, &mode_);
mukesh agrawal292dc0f2012-01-26 18:02:46 -080064 HelpRegisterWriteOnlyDerivedString(flimflam::kPassphraseProperty,
65 &WiFiService::SetPassphrase,
66 &WiFiService::ClearPassphrase,
67 NULL);
Paul Stewartac4ac002011-08-26 12:04:26 -070068 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
69 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070070
Paul Stewartac4ac002011-08-26 12:04:26 -070071 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
72 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
73 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
74 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070075
mukesh agrawald835b202011-10-07 15:26:47 -070076 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
77 string ssid_string(
78 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
79 if (SanitizeSSID(&ssid_string)) {
80 // WifiHexSsid property should only be present if Name property
81 // has been munged.
82 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
83 }
84 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070085
mukesh agrawal6e277772011-09-29 15:04:23 -070086 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
87 // a service that is not 802.1x.
Gaurav Shah29d68882012-01-30 19:06:42 -080088 if (Is8021x()) {
Gaurav Shah10109f22011-11-11 20:16:22 -080089 // Passphrases are not mandatory for 802.1X.
90 need_passphrase_ = false;
mukesh agrawal6e277772011-09-29 15:04:23 -070091 } else if (security_ == flimflam::kSecurityPsk) {
92 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070093 } else if (security_ == flimflam::kSecurityRsn) {
94 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070095 } else if (security_ == flimflam::kSecurityWpa) {
96 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070097 } else if (security_ == flimflam::kSecurityWep) {
98 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -070099 } else if (security_ == flimflam::kSecurityNone) {
100 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -0700101 } else {
Gaurav Shah10109f22011-11-11 20:16:22 -0800102 LOG(ERROR) << "Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700103 }
104
Paul Stewartd08f4432011-11-04 07:48:20 -0700105 // Until we know better (at Profile load time), use the generic name.
106 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000107 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700108}
109
110WiFiService::~WiFiService() {
111 LOG(INFO) << __func__;
112}
113
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000114void WiFiService::AutoConnect() {
115 if (IsAutoConnectable()) {
116 // Execute immediately, for two reasons:
117 //
118 // 1. We need IsAutoConnectable to return the correct value for
119 // other WiFiServices, and that depends on WiFi's state.
120 //
121 // 2. We should probably limit the extent to which we queue up
122 // actions (such as AutoConnect) which depend on current state.
123 // If we queued AutoConnects, we could build a long queue of
124 // useless work (one AutoConnect per Service), which blocks
125 // more timely work.
126 ConnectTask();
mukesh agrawal592516d2012-01-12 14:01:00 -0800127 } else {
128 LOG(INFO) << "Suppressed autoconnect to " << friendly_name();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000129 }
130}
131
mukesh agrawal1830fa12011-09-26 14:31:40 -0700132void WiFiService::Connect(Error */*error*/) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800133 LOG(INFO) << "In " << __func__ << "():";
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000134 // Defer handling, since dbus-c++ does not permit us to send an
135 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700136 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700137 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700138}
139
mukesh agrawaladb68482012-01-17 16:31:51 -0800140void WiFiService::Disconnect(Error *error) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000141 LOG(INFO) << __func__;
mukesh agrawaladb68482012-01-17 16:31:51 -0800142 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000143 // Defer handling, since dbus-c++ does not permit us to send an
144 // outbound request while processing an inbound one.
145 dispatcher()->PostTask(
146 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700147}
148
Paul Stewart22aa71b2011-09-16 12:15:11 -0700149bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
150 return wifi_->TechnologyIs(type);
151}
152
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000153bool WiFiService::IsAutoConnectable() const {
mukesh agrawaladb68482012-01-17 16:31:51 -0800154 return Service::IsAutoConnectable() &&
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000155 // Only auto-connect to Services which have visible Endpoints.
156 // (Needed because hidden Services may remain registered with
157 // Manager even without visible Endpoints.)
mukesh agrawaladb68482012-01-17 16:31:51 -0800158 HasEndpoints() &&
mukesh agrawal76d13882012-01-12 15:23:11 -0800159 // Do not preempt an existing connection (whether pending, or
160 // connected, and whether to this service, or another).
mukesh agrawaladb68482012-01-17 16:31:51 -0800161 wifi_->IsIdle();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000162}
163
164bool WiFiService::IsConnecting() const {
165 // WiFi does not move us into the associating state until it gets
166 // feedback from wpa_supplicant. So, to answer whether or
167 // not we're connecting, we consult with |wifi_|.
168 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800169}
170
mukesh agrawal261daca2011-12-02 18:56:56 +0000171void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
172 DCHECK(endpoint->ssid() == ssid());
173 endpoints_.insert(endpoint);
Thieu Lee41a72d2012-02-06 20:46:51 +0000174 // TODO: Track signal strength (crosbug.com/16786).
mukesh agrawal261daca2011-12-02 18:56:56 +0000175}
176
177void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
178 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
179 DCHECK(i != endpoints_.end());
180 if (i == endpoints_.end()) {
181 LOG(WARNING) << "In " << __func__ << "(): "
182 << "ignorning non-existent endpoint "
183 << endpoint->bssid_string();
184 return;
185 }
186 endpoints_.erase(i);
187}
188
Thieu Lee41a72d2012-02-06 20:46:51 +0000189void WiFiService::NotifyCurrentEndpoint(const WiFiEndpoint &endpoint) {
190 DCHECK(endpoints_.find(&endpoint) != endpoints_.end());
191 frequency_ = endpoint.frequency();
192 // TODO: Copy BSSID here (crosbug.com/22377).
193 // TODO: Copy signal strength (crosbug.com/16786).
194 // TODO(thieule): Update these values when supplicant signals that they
195 // have changed.
196 // (crosbug.com/16786)
197}
198
Chris Masone6515aab2011-10-12 16:19:09 -0700199string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700200 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700201}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700202
mukesh agrawal1a056262011-10-05 14:36:54 -0700203void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
204 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000205 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700206 } else if (security_ == flimflam::kSecurityPsk ||
207 security_ == flimflam::kSecurityWpa ||
208 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000209 ValidateWPAPassphrase(passphrase, error);
210 } else {
211 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700212 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000213
Paul Stewart2706aaf2011-12-14 16:44:04 -0800214 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000215 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800216 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000217
218 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700219}
220
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800221// ClearPassphrase is separate from SetPassphrase, because the default
222// value for |passphrase_| would not pass validation.
223void WiFiService::ClearPassphrase(Error */*error*/) {
224 passphrase_.clear();
225 UpdateConnectable();
226}
227
Paul Stewartd08f4432011-11-04 07:48:20 -0700228bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
229 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
230 storage->ContainsGroup(GetSpecificStorageIdentifier());
231}
232
Paul Stewarta41e38d2011-11-11 07:47:29 -0800233bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800234 // WiFi Services should be displayed only if they are in range (have
235 // endpoints that have shown up in a scan) or if the service is actively
236 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000237 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800238}
239
Paul Stewartd08f4432011-11-04 07:48:20 -0700240bool WiFiService::Load(StoreInterface *storage) {
241 // First find out which storage identifier is available in priority order
242 // of specific, generic.
243 string id = GetSpecificStorageIdentifier();
244 if (!storage->ContainsGroup(id)) {
245 id = GetGenericStorageIdentifier();
246 if (!storage->ContainsGroup(id)) {
247 LOG(WARNING) << "Service is not available in the persistent store: "
248 << id;
249 return false;
250 }
251 }
252
253 // Set our storage identifier to match the storage name in the Profile.
254 storage_identifier_ = id;
255
256 // Load properties common to all Services.
257 if (!Service::Load(storage)) {
258 return false;
259 }
260
261 // Load properties specific to WiFi services.
262 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000263
Paul Stewart2706aaf2011-12-14 16:44:04 -0800264 // NB: mode, security and ssid parameters are never read in from
265 // Load() as they are provided from the scan.
266
267 string passphrase;
268 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
269 Error error;
270 SetPassphrase(passphrase, &error);
271 if (!error.IsSuccess()) {
272 LOG(ERROR) << "Passphrase could not be set: "
273 << Error::GetName(error.type());
274 }
275 }
276
Paul Stewartd08f4432011-11-04 07:48:20 -0700277 return true;
278}
279
280bool WiFiService::Save(StoreInterface *storage) {
281 // Save properties common to all Services.
282 if (!Service::Save(storage)) {
283 return false;
284 }
285
286 // Save properties specific to WiFi services.
287 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800288 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
289 storage->SetString(id, kStorageMode, mode_);
290 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
291 storage->SetString(id, kStorageSecurity, security_);
292 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000293
294 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700295 return true;
296}
297
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800298void WiFiService::Unload() {
299 Service::Unload();
300 hidden_ssid_ = false;
301 passphrase_ = "";
302 UpdateConnectable();
Paul Stewart66c86002012-01-30 18:00:52 -0800303 if (security_ == flimflam::kSecurity8021x) {
304 // TODO(pstew): 802.1x/RSN networks (as opposed to 802.1x/WPA or
305 // 802.1x/WEP) have the ability to cache WPA PMK credentials.
306 // Make sure that these are cleared when credentials for networks
307 // of this type goes away.
308 //
309 // When wpa_supplicant gains the ability, do this credential
310 // clearing on a per-service basis. Also do this whenever the credentials
311 // for a service changes. crosbug.com/25670
312 wifi_->ClearCachedCredentials();
313 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800314}
315
Paul Stewart6ab23a92011-11-09 17:17:47 -0800316bool WiFiService::IsSecurityMatch(const string &security) const {
317 return GetSecurityClass(security) == GetSecurityClass(security_);
318}
319
Thieu Le48e6d6d2011-12-06 00:40:27 +0000320void WiFiService::InitializeCustomMetrics() const {
321 string histogram = metrics()->GetFullMetricName(
322 Metrics::kMetricTimeToJoinMilliseconds,
323 technology());
324 metrics()->AddServiceStateTransitionTimer(this,
325 histogram,
326 Service::kStateAssociating,
327 Service::kStateConfiguring);
328}
329
330void WiFiService::SendPostReadyStateMetrics() const {
331 // TODO(thieule): Send physical mode and security metrics.
332 // crosbug.com/24441
333 metrics()->SendEnumToUMA(
334 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
335 technology()),
336 Metrics::WiFiFrequencyToChannel(frequency_),
337 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000338
339 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
340 metrics()->SendEnumToUMA(
341 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
342 technology()),
343 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
344 Metrics::kWiFiNetworkPhyModeMax);
345
346 Metrics::WiFiSecurity security_uma =
347 Metrics::WiFiSecurityStringToEnum(security_);
348 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
349 metrics()->SendEnumToUMA(
350 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
351 technology()),
352 security_uma,
353 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000354}
355
mukesh agrawal32399322011-09-01 10:53:43 -0700356// private methods
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800357void WiFiService::HelpRegisterWriteOnlyDerivedString(
358 const string &name,
359 void(WiFiService::*set)(const string &, Error *),
360 void(WiFiService::*clear)(Error *),
361 const string *default_value) {
362 mutable_store()->RegisterDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000363 name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800364 StringAccessor(
365 new CustomWriteOnlyAccessor<WiFiService, string>(
366 this, set, clear, default_value)));
Thieu Lef7709452011-11-15 01:13:19 +0000367}
368
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700369void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700370 std::map<string, DBus::Variant> params;
371 DBus::MessageIter writer;
372
373 params[wpa_supplicant::kNetworkPropertyMode].writer().
374 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
375
Gaurav Shah29d68882012-01-30 19:06:42 -0800376 if (Is8021x()) {
377 // Is EAP key management is not set, set to a default.
Gaurav Shah10109f22011-11-11 20:16:22 -0800378 if (GetEAPKeyManagement().empty())
379 SetEAPKeyManagement("WPA-EAP");
380 Populate8021xProperties(&params);
mukesh agrawal6e277772011-09-29 15:04:23 -0700381 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800382 const string psk_proto = StringPrintf("%s %s",
383 wpa_supplicant::kSecurityModeWPA,
384 wpa_supplicant::kSecurityModeRSN);
385 params[wpa_supplicant::kPropertySecurityProtocol].writer().
386 append_string(psk_proto.c_str());
387 params[wpa_supplicant::kPropertyPreSharedKey].writer().
388 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700389 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700390 params[wpa_supplicant::kPropertySecurityProtocol].writer().
391 append_string(wpa_supplicant::kSecurityModeRSN);
392 params[wpa_supplicant::kPropertyPreSharedKey].writer().
393 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700394 } else if (security_ == flimflam::kSecurityWpa) {
395 params[wpa_supplicant::kPropertySecurityProtocol].writer().
396 append_string(wpa_supplicant::kSecurityModeWPA);
397 params[wpa_supplicant::kPropertyPreSharedKey].writer().
398 append_string(passphrase_.c_str());
399 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000400 params[wpa_supplicant::kPropertyAuthAlg].writer().
401 append_string(wpa_supplicant::kSecurityAuthAlg);
402 Error error;
403 int key_index;
404 std::vector<uint8> password_bytes;
405 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
406 writer = params[wpa_supplicant::kPropertyWEPKey +
407 base::IntToString(key_index)].writer();
408 writer << password_bytes;
409 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
410 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700411 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800412 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700413 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800414 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700415 }
416
Gaurav Shah10109f22011-11-11 20:16:22 -0800417 params[wpa_supplicant::kNetworkPropertyEapKeyManagement].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700418 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800419
420 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700421 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
422 writer << ssid_;
423
424 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700425}
426
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000427void WiFiService::DisconnectTask() {
428 wifi_->DisconnectFrom(this);
429}
430
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800431string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700432 return wifi_->GetRpcIdentifier();
433}
434
mukesh agrawal29c13a12011-11-24 00:09:19 +0000435void WiFiService::UpdateConnectable() {
Gaurav Shah10109f22011-11-11 20:16:22 -0800436 bool is_connectable = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000437 if (security_ == flimflam::kSecurityNone) {
438 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800439 need_passphrase_ = false;
Gaurav Shah10109f22011-11-11 20:16:22 -0800440 is_connectable = true;
Gaurav Shah29d68882012-01-30 19:06:42 -0800441 } else if (Is8021x()) {
442 is_connectable = Is8021xConnectable();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000443 } else if (security_ == flimflam::kSecurityWep ||
444 security_ == flimflam::kSecurityWpa ||
445 security_ == flimflam::kSecurityPsk ||
446 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800447 need_passphrase_ = passphrase_.empty();
Gaurav Shah10109f22011-11-11 20:16:22 -0800448 is_connectable = !need_passphrase_;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000449 }
Gaurav Shah10109f22011-11-11 20:16:22 -0800450 set_connectable(is_connectable);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000451}
452
mukesh agrawal1a056262011-10-05 14:36:54 -0700453// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000454void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
455 Error *error) {
456 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700457}
458
459// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000460void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
461 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700462 unsigned int length = passphrase.length();
463 vector<uint8> passphrase_bytes;
464
465 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
466 if (length != IEEE_80211::kWPAHexLen &&
467 (length < IEEE_80211::kWPAAsciiMinLen ||
468 length > IEEE_80211::kWPAAsciiMaxLen)) {
469 error->Populate(Error::kInvalidPassphrase);
470 }
471 } else {
472 if (length < IEEE_80211::kWPAAsciiMinLen ||
473 length > IEEE_80211::kWPAAsciiMaxLen) {
474 error->Populate(Error::kInvalidPassphrase);
475 }
476 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000477}
mukesh agrawal1a056262011-10-05 14:36:54 -0700478
Thieu Lef4cbda92011-11-10 23:41:24 +0000479// static
480void WiFiService::ParseWEPPassphrase(const string &passphrase,
481 int *key_index,
482 std::vector<uint8> *password_bytes,
483 Error *error) {
484 unsigned int length = passphrase.length();
485 int key_index_local;
486 std::string password_text;
487 bool is_hex = false;
488
489 switch (length) {
490 case IEEE_80211::kWEP40AsciiLen:
491 case IEEE_80211::kWEP104AsciiLen:
492 key_index_local = 0;
493 password_text = passphrase;
494 break;
495 case IEEE_80211::kWEP40AsciiLen + 2:
496 case IEEE_80211::kWEP104AsciiLen + 2:
497 if (CheckWEPKeyIndex(passphrase, error)) {
498 base::StringToInt(passphrase.substr(0,1), &key_index_local);
499 password_text = passphrase.substr(2);
500 }
501 break;
502 case IEEE_80211::kWEP40HexLen:
503 case IEEE_80211::kWEP104HexLen:
504 if (CheckWEPIsHex(passphrase, error)) {
505 key_index_local = 0;
506 password_text = passphrase;
507 is_hex = true;
508 }
509 break;
510 case IEEE_80211::kWEP40HexLen + 2:
511 case IEEE_80211::kWEP104HexLen + 2:
512 if(CheckWEPKeyIndex(passphrase, error) &&
513 CheckWEPIsHex(passphrase.substr(2), error)) {
514 base::StringToInt(passphrase.substr(0,1), &key_index_local);
515 password_text = passphrase.substr(2);
516 is_hex = true;
517 } else if (CheckWEPPrefix(passphrase, error) &&
518 CheckWEPIsHex(passphrase.substr(2), error)) {
519 key_index_local = 0;
520 password_text = passphrase.substr(2);
521 is_hex = true;
522 }
523 break;
524 case IEEE_80211::kWEP40HexLen + 4:
525 case IEEE_80211::kWEP104HexLen + 4:
526 if (CheckWEPKeyIndex(passphrase, error) &&
527 CheckWEPPrefix(passphrase.substr(2), error) &&
528 CheckWEPIsHex(passphrase.substr(4), error)) {
529 base::StringToInt(passphrase.substr(0,1), &key_index_local);
530 password_text = passphrase.substr(4);
531 is_hex = true;
532 }
533 break;
534 default:
535 error->Populate(Error::kInvalidPassphrase);
536 break;
537 }
538
mukesh agrawal1a056262011-10-05 14:36:54 -0700539 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000540 if (key_index)
541 *key_index = key_index_local;
542 if (password_bytes) {
543 if (is_hex)
544 base::HexStringToBytes(password_text, password_bytes);
545 else
546 password_bytes->insert(password_bytes->end(),
547 password_text.begin(),
548 password_text.end());
549 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700550 }
551}
552
553// static
554bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
555 vector<uint8> passphrase_bytes;
556 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
557 return true;
558 } else {
559 error->Populate(Error::kInvalidPassphrase);
560 return false;
561 }
562}
563
564// static
565bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
566 if (StartsWithASCII(passphrase, "0:", false) ||
567 StartsWithASCII(passphrase, "1:", false) ||
568 StartsWithASCII(passphrase, "2:", false) ||
569 StartsWithASCII(passphrase, "3:", false)) {
570 return true;
571 } else {
572 error->Populate(Error::kInvalidPassphrase);
573 return false;
574 }
575}
576
577// static
578bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
579 if (StartsWithASCII(passphrase, "0x", false)) {
580 return true;
581 } else {
582 error->Populate(Error::kInvalidPassphrase);
583 return false;
584 }
585}
586
Paul Stewart6ab23a92011-11-09 17:17:47 -0800587// static
mukesh agrawald835b202011-10-07 15:26:47 -0700588bool WiFiService::SanitizeSSID(string *ssid) {
589 CHECK(ssid);
590
591 size_t ssid_len = ssid->length();
592 size_t i;
593 bool changed = false;
594
595 for (i=0; i < ssid_len; ++i) {
596 if (!g_ascii_isprint((*ssid)[i])) {
597 (*ssid)[i] = '?';
598 changed = true;
599 }
600 }
601
602 return changed;
603}
604
Paul Stewart6ab23a92011-11-09 17:17:47 -0800605// static
606string WiFiService::GetSecurityClass(const string &security) {
607 if (security == flimflam::kSecurityRsn ||
608 security == flimflam::kSecurityWpa) {
609 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700610 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800611 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700612 }
613}
614
Paul Stewarta41e38d2011-11-11 07:47:29 -0800615// static
616bool WiFiService::ParseStorageIdentifier(const string &storage_name,
617 string *address,
618 string *mode,
619 string *security) {
620 vector<string> wifi_parts;
621 base::SplitString(storage_name, '_', &wifi_parts);
Paul Stewart0756db92012-01-27 08:34:47 -0800622 if ((wifi_parts.size() != 5 && wifi_parts.size() != 6) ||
623 wifi_parts[0] != flimflam::kTypeWifi) {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800624 return false;
625 }
626 *address = wifi_parts[1];
627 *mode = wifi_parts[3];
Paul Stewart0756db92012-01-27 08:34:47 -0800628 if (wifi_parts.size() == 5) {
629 *security = wifi_parts[4];
630 } else {
631 // Account for security type "802_1x" which got split up above.
632 *security = wifi_parts[4] + "_" + wifi_parts[5];
633 }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800634 return true;
635}
636
Paul Stewart6ab23a92011-11-09 17:17:47 -0800637string WiFiService::GetGenericStorageIdentifier() const {
638 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
639}
640
Paul Stewartd08f4432011-11-04 07:48:20 -0700641string WiFiService::GetSpecificStorageIdentifier() const {
642 return GetStorageIdentifierForSecurity(security_);
643}
644
645string WiFiService::GetStorageIdentifierForSecurity(
646 const string &security) const {
647 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
648 flimflam::kTypeWifi,
649 wifi_->address().c_str(),
650 hex_ssid_.c_str(),
651 mode_.c_str(),
652 security.c_str()));
653}
654
Gaurav Shah10109f22011-11-11 20:16:22 -0800655void WiFiService::set_eap(const EapCredentials &eap) {
656 Service::set_eap(eap);
657 UpdateConnectable();
658}
659
Gaurav Shah29d68882012-01-30 19:06:42 -0800660bool WiFiService::Is8021x() const {
661 if (security_ == flimflam::kSecurity8021x)
662 return true;
663
664 // Dynamic WEP + 802.1x.
665 if (security_ == flimflam::kSecurityWep &&
666 GetEAPKeyManagement() == "IEEE8021X")
667 return true;
668 return false;
669}
670
Gaurav Shah10109f22011-11-11 20:16:22 -0800671void WiFiService::Populate8021xProperties(
672 std::map<string, DBus::Variant> *params) {
673 typedef std::pair<const char *, const char *> KeyVal;
674 KeyVal propertyvals[] = {
675 KeyVal(wpa_supplicant::kNetworkPropertyEapIdentity, eap().identity.c_str()),
676 KeyVal(wpa_supplicant::kNetworkPropertyEapEap, eap().eap.c_str()),
677 KeyVal(wpa_supplicant::kNetworkPropertyEapInnerEap,
678 eap().inner_eap.c_str()),
679 KeyVal(wpa_supplicant::kNetworkPropertyEapAnonymousIdentity,
680 eap().anonymous_identity.c_str()),
681 KeyVal(wpa_supplicant::kNetworkPropertyEapClientCert,
682 eap().client_cert.c_str()),
683 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKey,
684 eap().private_key.c_str()),
685 KeyVal(wpa_supplicant::kNetworkPropertyEapPrivateKeyPassword,
686 eap().private_key_password.c_str()),
687 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCert, eap().ca_cert.c_str()),
688 KeyVal(wpa_supplicant::kNetworkPropertyEapCaPassword,
689 eap().password.c_str()),
690 KeyVal(wpa_supplicant::kNetworkPropertyEapCertId, eap().cert_id.c_str()),
691 KeyVal(wpa_supplicant::kNetworkPropertyEapKeyId, eap().key_id.c_str()),
692 KeyVal(wpa_supplicant::kNetworkPropertyEapCaCertId,
693 eap().ca_cert_id.c_str()),
694 KeyVal(wpa_supplicant::kNetworkPropertyEapPin, eap().pin.c_str()),
695 // TODO(gauravsh): Support getting CA certificates out of the NSS certdb.
696 // crosbug.com/25663
697 KeyVal(wpa_supplicant::kNetworkPropertyCaPath, wpa_supplicant::kCaPath)
698 };
699
700 DBus::MessageIter writer;
701 for (size_t i = 0; i < arraysize(propertyvals); ++i) {
702 if (strlen(propertyvals[i].second) > 0) {
703 (*params)[propertyvals[i].first].writer().
704 append_string(propertyvals[i].second);
705 }
706 }
707}
708
mukesh agrawalb54601c2011-06-07 17:39:22 -0700709} // namespace shill