blob: f0ee9e575fb490f3589231ea3e25844e02d08db8 [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>
8
9#include <base/logging.h>
Chris Masone34af2182011-08-22 11:59:36 -070010#include <base/stringprintf.h>
11#include <base/string_number_conversions.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080012#include <base/string_split.h>
Chris Masone34af2182011-08-22 11:59:36 -070013#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070015#include <dbus/dbus.h>
mukesh agrawald835b202011-10-07 15:26:47 -070016#include <glib.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),
mukesh agrawalb54601c2011-06-07 17:39:22 -070057 task_factory_(this),
58 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070059 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070060 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070061 store->RegisterConstString(flimflam::kModeProperty, &mode_);
Thieu Lef7709452011-11-15 01:13:19 +000062 HelpRegisterDerivedString(store,
63 flimflam::kPassphraseProperty,
64 NULL,
65 &WiFiService::SetPassphrase);
Paul Stewartac4ac002011-08-26 12:04:26 -070066 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
67 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070068
Paul Stewartac4ac002011-08-26 12:04:26 -070069 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
70 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
71 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
72 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070073
mukesh agrawald835b202011-10-07 15:26:47 -070074 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
75 string ssid_string(
76 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
77 if (SanitizeSSID(&ssid_string)) {
78 // WifiHexSsid property should only be present if Name property
79 // has been munged.
80 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
81 }
82 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070083
mukesh agrawal6e277772011-09-29 15:04:23 -070084 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
85 // a service that is not 802.1x.
86 if (security_ == flimflam::kSecurity8021x) {
87 NOTIMPLEMENTED();
88 // XXX needs_passpharse_ = false ?
89 } else if (security_ == flimflam::kSecurityPsk) {
90 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070091 } else if (security_ == flimflam::kSecurityRsn) {
92 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070093 } else if (security_ == flimflam::kSecurityWpa) {
94 SetEAPKeyManagement("WPA-PSK");
mukesh agrawal6e277772011-09-29 15:04:23 -070095 } else if (security_ == flimflam::kSecurityWep) {
96 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -070097 } else if (security_ == flimflam::kSecurityNone) {
98 SetEAPKeyManagement("NONE");
mukesh agrawal6e277772011-09-29 15:04:23 -070099 } else {
100 LOG(ERROR) << "unsupported security method " << security_;
101 }
102
Paul Stewartd08f4432011-11-04 07:48:20 -0700103 // Until we know better (at Profile load time), use the generic name.
104 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000105 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700106}
107
108WiFiService::~WiFiService() {
109 LOG(INFO) << __func__;
110}
111
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000112void WiFiService::AutoConnect() {
113 if (IsAutoConnectable()) {
114 // Execute immediately, for two reasons:
115 //
116 // 1. We need IsAutoConnectable to return the correct value for
117 // other WiFiServices, and that depends on WiFi's state.
118 //
119 // 2. We should probably limit the extent to which we queue up
120 // actions (such as AutoConnect) which depend on current state.
121 // If we queued AutoConnects, we could build a long queue of
122 // useless work (one AutoConnect per Service), which blocks
123 // more timely work.
124 ConnectTask();
mukesh agrawal592516d2012-01-12 14:01:00 -0800125 } else {
126 LOG(INFO) << "Suppressed autoconnect to " << friendly_name();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000127 }
128}
129
mukesh agrawal1830fa12011-09-26 14:31:40 -0700130void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700131 LOG(INFO) << __func__;
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000132 // Defer handling, since dbus-c++ does not permit us to send an
133 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700134 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700135 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700136}
137
mukesh agrawaladb68482012-01-17 16:31:51 -0800138void WiFiService::Disconnect(Error *error) {
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000139 LOG(INFO) << __func__;
mukesh agrawaladb68482012-01-17 16:31:51 -0800140 Service::Disconnect(error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000141 // Defer handling, since dbus-c++ does not permit us to send an
142 // outbound request while processing an inbound one.
143 dispatcher()->PostTask(
144 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700145}
146
Paul Stewart22aa71b2011-09-16 12:15:11 -0700147bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
148 return wifi_->TechnologyIs(type);
149}
150
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000151bool WiFiService::IsAutoConnectable() const {
mukesh agrawaladb68482012-01-17 16:31:51 -0800152 return Service::IsAutoConnectable() &&
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000153 // Only auto-connect to Services which have visible Endpoints.
154 // (Needed because hidden Services may remain registered with
155 // Manager even without visible Endpoints.)
mukesh agrawaladb68482012-01-17 16:31:51 -0800156 HasEndpoints() &&
mukesh agrawal76d13882012-01-12 15:23:11 -0800157 // Do not preempt an existing connection (whether pending, or
158 // connected, and whether to this service, or another).
mukesh agrawaladb68482012-01-17 16:31:51 -0800159 wifi_->IsIdle();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000160}
161
162bool WiFiService::IsConnecting() const {
163 // WiFi does not move us into the associating state until it gets
164 // feedback from wpa_supplicant. So, to answer whether or
165 // not we're connecting, we consult with |wifi_|.
166 return wifi_->IsConnectingTo(*this);
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800167}
168
mukesh agrawal261daca2011-12-02 18:56:56 +0000169void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
170 DCHECK(endpoint->ssid() == ssid());
171 endpoints_.insert(endpoint);
172}
173
174void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
175 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
176 DCHECK(i != endpoints_.end());
177 if (i == endpoints_.end()) {
178 LOG(WARNING) << "In " << __func__ << "(): "
179 << "ignorning non-existent endpoint "
180 << endpoint->bssid_string();
181 return;
182 }
183 endpoints_.erase(i);
184}
185
Chris Masone6515aab2011-10-12 16:19:09 -0700186string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700187 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700188}
mukesh agrawal445e72c2011-06-22 11:13:50 -0700189
mukesh agrawal1a056262011-10-05 14:36:54 -0700190void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
191 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000192 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700193 } else if (security_ == flimflam::kSecurityPsk ||
194 security_ == flimflam::kSecurityWpa ||
195 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000196 ValidateWPAPassphrase(passphrase, error);
197 } else {
198 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700199 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000200
Paul Stewart2706aaf2011-12-14 16:44:04 -0800201 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000202 passphrase_ = passphrase;
Paul Stewart2706aaf2011-12-14 16:44:04 -0800203 }
mukesh agrawal29c13a12011-11-24 00:09:19 +0000204
205 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700206}
207
Paul Stewartd08f4432011-11-04 07:48:20 -0700208bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
209 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
210 storage->ContainsGroup(GetSpecificStorageIdentifier());
211}
212
Paul Stewarta41e38d2011-11-11 07:47:29 -0800213bool WiFiService::IsVisible() const {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800214 // WiFi Services should be displayed only if they are in range (have
215 // endpoints that have shown up in a scan) or if the service is actively
216 // being connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000217 return HasEndpoints() || IsConnected() || IsConnecting();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800218}
219
Paul Stewartd08f4432011-11-04 07:48:20 -0700220bool WiFiService::Load(StoreInterface *storage) {
221 // First find out which storage identifier is available in priority order
222 // of specific, generic.
223 string id = GetSpecificStorageIdentifier();
224 if (!storage->ContainsGroup(id)) {
225 id = GetGenericStorageIdentifier();
226 if (!storage->ContainsGroup(id)) {
227 LOG(WARNING) << "Service is not available in the persistent store: "
228 << id;
229 return false;
230 }
231 }
232
233 // Set our storage identifier to match the storage name in the Profile.
234 storage_identifier_ = id;
235
236 // Load properties common to all Services.
237 if (!Service::Load(storage)) {
238 return false;
239 }
240
241 // Load properties specific to WiFi services.
242 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000243
Paul Stewart2706aaf2011-12-14 16:44:04 -0800244 // NB: mode, security and ssid parameters are never read in from
245 // Load() as they are provided from the scan.
246
247 string passphrase;
248 if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) {
249 Error error;
250 SetPassphrase(passphrase, &error);
251 if (!error.IsSuccess()) {
252 LOG(ERROR) << "Passphrase could not be set: "
253 << Error::GetName(error.type());
254 }
255 }
256
Paul Stewartd08f4432011-11-04 07:48:20 -0700257 return true;
258}
259
260bool WiFiService::Save(StoreInterface *storage) {
261 // Save properties common to all Services.
262 if (!Service::Save(storage)) {
263 return false;
264 }
265
266 // Save properties specific to WiFi services.
267 const string id = GetStorageIdentifier();
Paul Stewart2706aaf2011-12-14 16:44:04 -0800268 storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_);
269 storage->SetString(id, kStorageMode, mode_);
270 storage->SetCryptedString(id, kStoragePassphrase, passphrase_);
271 storage->SetString(id, kStorageSecurity, security_);
272 storage->SetString(id, kStorageSSID, hex_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000273
274 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700275 return true;
276}
277
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800278void WiFiService::Unload() {
279 Service::Unload();
280 hidden_ssid_ = false;
281 passphrase_ = "";
282 UpdateConnectable();
283}
284
Paul Stewart6ab23a92011-11-09 17:17:47 -0800285bool WiFiService::IsSecurityMatch(const string &security) const {
286 return GetSecurityClass(security) == GetSecurityClass(security_);
287}
288
Thieu Le48e6d6d2011-12-06 00:40:27 +0000289void WiFiService::InitializeCustomMetrics() const {
290 string histogram = metrics()->GetFullMetricName(
291 Metrics::kMetricTimeToJoinMilliseconds,
292 technology());
293 metrics()->AddServiceStateTransitionTimer(this,
294 histogram,
295 Service::kStateAssociating,
296 Service::kStateConfiguring);
297}
298
299void WiFiService::SendPostReadyStateMetrics() const {
300 // TODO(thieule): Send physical mode and security metrics.
301 // crosbug.com/24441
302 metrics()->SendEnumToUMA(
303 metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel,
304 technology()),
305 Metrics::WiFiFrequencyToChannel(frequency_),
306 Metrics::kMetricNetworkChannelMax);
Thieu Lead1ec2c2012-01-05 23:39:48 +0000307
308 DCHECK(physical_mode_ < Metrics::kWiFiNetworkPhyModeMax);
309 metrics()->SendEnumToUMA(
310 metrics()->GetFullMetricName(Metrics::kMetricNetworkPhyMode,
311 technology()),
312 static_cast<Metrics::WiFiNetworkPhyMode>(physical_mode_),
313 Metrics::kWiFiNetworkPhyModeMax);
314
315 Metrics::WiFiSecurity security_uma =
316 Metrics::WiFiSecurityStringToEnum(security_);
317 DCHECK(security_uma != Metrics::kWiFiSecurityUnknown);
318 metrics()->SendEnumToUMA(
319 metrics()->GetFullMetricName(Metrics::kMetricNetworkSecurity,
320 technology()),
321 security_uma,
322 Metrics::kMetricNetworkSecurityMax);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000323}
324
mukesh agrawal32399322011-09-01 10:53:43 -0700325// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000326void WiFiService::HelpRegisterDerivedString(
327 PropertyStore *store,
328 const std::string &name,
329 std::string(WiFiService::*get)(Error *),
330 void(WiFiService::*set)(const std::string&, Error *)) {
331 store->RegisterDerivedString(
332 name,
333 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
334}
335
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700336void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700337 std::map<string, DBus::Variant> params;
338 DBus::MessageIter writer;
339
340 params[wpa_supplicant::kNetworkPropertyMode].writer().
341 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
342
343 if (security_ == flimflam::kSecurity8021x) {
344 NOTIMPLEMENTED();
345 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800346 const string psk_proto = StringPrintf("%s %s",
347 wpa_supplicant::kSecurityModeWPA,
348 wpa_supplicant::kSecurityModeRSN);
349 params[wpa_supplicant::kPropertySecurityProtocol].writer().
350 append_string(psk_proto.c_str());
351 params[wpa_supplicant::kPropertyPreSharedKey].writer().
352 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700353 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700354 params[wpa_supplicant::kPropertySecurityProtocol].writer().
355 append_string(wpa_supplicant::kSecurityModeRSN);
356 params[wpa_supplicant::kPropertyPreSharedKey].writer().
357 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700358 } else if (security_ == flimflam::kSecurityWpa) {
359 params[wpa_supplicant::kPropertySecurityProtocol].writer().
360 append_string(wpa_supplicant::kSecurityModeWPA);
361 params[wpa_supplicant::kPropertyPreSharedKey].writer().
362 append_string(passphrase_.c_str());
363 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000364 params[wpa_supplicant::kPropertyAuthAlg].writer().
365 append_string(wpa_supplicant::kSecurityAuthAlg);
366 Error error;
367 int key_index;
368 std::vector<uint8> password_bytes;
369 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
370 writer = params[wpa_supplicant::kPropertyWEPKey +
371 base::IntToString(key_index)].writer();
372 writer << password_bytes;
373 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
374 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700375 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800376 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700377 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800378 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700379 }
380
381 params[wpa_supplicant::kPropertyKeyManagement].writer().
382 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800383
384 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700385 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
386 writer << ssid_;
387
388 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700389}
390
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000391void WiFiService::DisconnectTask() {
392 wifi_->DisconnectFrom(this);
393}
394
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800395string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700396 return wifi_->GetRpcIdentifier();
397}
398
mukesh agrawal29c13a12011-11-24 00:09:19 +0000399void WiFiService::UpdateConnectable() {
400 if (security_ == flimflam::kSecurityNone) {
401 DCHECK(passphrase_.empty());
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800402 need_passphrase_ = false;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000403 } else if (security_ == flimflam::kSecurityWep ||
404 security_ == flimflam::kSecurityWpa ||
405 security_ == flimflam::kSecurityPsk ||
406 security_ == flimflam::kSecurityRsn) {
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800407 need_passphrase_ = passphrase_.empty();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000408 } else {
409 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800410 need_passphrase_ = true;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000411 }
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800412 set_connectable(!need_passphrase_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000413}
414
mukesh agrawal1a056262011-10-05 14:36:54 -0700415// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000416void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
417 Error *error) {
418 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700419}
420
421// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000422void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
423 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700424 unsigned int length = passphrase.length();
425 vector<uint8> passphrase_bytes;
426
427 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
428 if (length != IEEE_80211::kWPAHexLen &&
429 (length < IEEE_80211::kWPAAsciiMinLen ||
430 length > IEEE_80211::kWPAAsciiMaxLen)) {
431 error->Populate(Error::kInvalidPassphrase);
432 }
433 } else {
434 if (length < IEEE_80211::kWPAAsciiMinLen ||
435 length > IEEE_80211::kWPAAsciiMaxLen) {
436 error->Populate(Error::kInvalidPassphrase);
437 }
438 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000439}
mukesh agrawal1a056262011-10-05 14:36:54 -0700440
Thieu Lef4cbda92011-11-10 23:41:24 +0000441// static
442void WiFiService::ParseWEPPassphrase(const string &passphrase,
443 int *key_index,
444 std::vector<uint8> *password_bytes,
445 Error *error) {
446 unsigned int length = passphrase.length();
447 int key_index_local;
448 std::string password_text;
449 bool is_hex = false;
450
451 switch (length) {
452 case IEEE_80211::kWEP40AsciiLen:
453 case IEEE_80211::kWEP104AsciiLen:
454 key_index_local = 0;
455 password_text = passphrase;
456 break;
457 case IEEE_80211::kWEP40AsciiLen + 2:
458 case IEEE_80211::kWEP104AsciiLen + 2:
459 if (CheckWEPKeyIndex(passphrase, error)) {
460 base::StringToInt(passphrase.substr(0,1), &key_index_local);
461 password_text = passphrase.substr(2);
462 }
463 break;
464 case IEEE_80211::kWEP40HexLen:
465 case IEEE_80211::kWEP104HexLen:
466 if (CheckWEPIsHex(passphrase, error)) {
467 key_index_local = 0;
468 password_text = passphrase;
469 is_hex = true;
470 }
471 break;
472 case IEEE_80211::kWEP40HexLen + 2:
473 case IEEE_80211::kWEP104HexLen + 2:
474 if(CheckWEPKeyIndex(passphrase, error) &&
475 CheckWEPIsHex(passphrase.substr(2), error)) {
476 base::StringToInt(passphrase.substr(0,1), &key_index_local);
477 password_text = passphrase.substr(2);
478 is_hex = true;
479 } else if (CheckWEPPrefix(passphrase, error) &&
480 CheckWEPIsHex(passphrase.substr(2), error)) {
481 key_index_local = 0;
482 password_text = passphrase.substr(2);
483 is_hex = true;
484 }
485 break;
486 case IEEE_80211::kWEP40HexLen + 4:
487 case IEEE_80211::kWEP104HexLen + 4:
488 if (CheckWEPKeyIndex(passphrase, error) &&
489 CheckWEPPrefix(passphrase.substr(2), error) &&
490 CheckWEPIsHex(passphrase.substr(4), error)) {
491 base::StringToInt(passphrase.substr(0,1), &key_index_local);
492 password_text = passphrase.substr(4);
493 is_hex = true;
494 }
495 break;
496 default:
497 error->Populate(Error::kInvalidPassphrase);
498 break;
499 }
500
mukesh agrawal1a056262011-10-05 14:36:54 -0700501 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000502 if (key_index)
503 *key_index = key_index_local;
504 if (password_bytes) {
505 if (is_hex)
506 base::HexStringToBytes(password_text, password_bytes);
507 else
508 password_bytes->insert(password_bytes->end(),
509 password_text.begin(),
510 password_text.end());
511 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700512 }
513}
514
515// static
516bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
517 vector<uint8> passphrase_bytes;
518 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
519 return true;
520 } else {
521 error->Populate(Error::kInvalidPassphrase);
522 return false;
523 }
524}
525
526// static
527bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
528 if (StartsWithASCII(passphrase, "0:", false) ||
529 StartsWithASCII(passphrase, "1:", false) ||
530 StartsWithASCII(passphrase, "2:", false) ||
531 StartsWithASCII(passphrase, "3:", false)) {
532 return true;
533 } else {
534 error->Populate(Error::kInvalidPassphrase);
535 return false;
536 }
537}
538
539// static
540bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
541 if (StartsWithASCII(passphrase, "0x", false)) {
542 return true;
543 } else {
544 error->Populate(Error::kInvalidPassphrase);
545 return false;
546 }
547}
548
Paul Stewart6ab23a92011-11-09 17:17:47 -0800549// static
mukesh agrawald835b202011-10-07 15:26:47 -0700550bool WiFiService::SanitizeSSID(string *ssid) {
551 CHECK(ssid);
552
553 size_t ssid_len = ssid->length();
554 size_t i;
555 bool changed = false;
556
557 for (i=0; i < ssid_len; ++i) {
558 if (!g_ascii_isprint((*ssid)[i])) {
559 (*ssid)[i] = '?';
560 changed = true;
561 }
562 }
563
564 return changed;
565}
566
Paul Stewart6ab23a92011-11-09 17:17:47 -0800567// static
568string WiFiService::GetSecurityClass(const string &security) {
569 if (security == flimflam::kSecurityRsn ||
570 security == flimflam::kSecurityWpa) {
571 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700572 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800573 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700574 }
575}
576
Paul Stewarta41e38d2011-11-11 07:47:29 -0800577// static
578bool WiFiService::ParseStorageIdentifier(const string &storage_name,
579 string *address,
580 string *mode,
581 string *security) {
582 vector<string> wifi_parts;
583 base::SplitString(storage_name, '_', &wifi_parts);
584 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
585 return false;
586 }
587 *address = wifi_parts[1];
588 *mode = wifi_parts[3];
589 *security = wifi_parts[4];
590 return true;
591}
592
Paul Stewart6ab23a92011-11-09 17:17:47 -0800593string WiFiService::GetGenericStorageIdentifier() const {
594 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
595}
596
Paul Stewartd08f4432011-11-04 07:48:20 -0700597string WiFiService::GetSpecificStorageIdentifier() const {
598 return GetStorageIdentifierForSecurity(security_);
599}
600
601string WiFiService::GetStorageIdentifierForSecurity(
602 const string &security) const {
603 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
604 flimflam::kTypeWifi,
605 wifi_->address().c_str(),
606 hex_ssid_.c_str(),
607 mode_.c_str(),
608 security.c_str()));
609}
610
mukesh agrawalb54601c2011-06-07 17:39:22 -0700611} // namespace shill