blob: fdb69648c5b83e92ef934f8f6ecd491dca275a53 [file] [log] [blame]
mukesh agrawalb54601c2011-06-07 17:39:22 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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"
Paul Stewartd08f4432011-11-04 07:48:20 -070023#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070024#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070025#include "shill/wifi_endpoint.h"
26#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070027
28using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070029using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070030
31namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070032
Paul Stewartd08f4432011-11-04 07:48:20 -070033const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
34
mukesh agrawalb54601c2011-06-07 17:39:22 -070035WiFiService::WiFiService(ControlInterface *control_interface,
36 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070037 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070038 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080039 const vector<uint8_t> &ssid,
40 const string &mode,
41 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080042 bool hidden_ssid)
mukesh agrawal7a4e4002011-09-06 11:26:05 -070043 : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi),
Chris Masone75612302011-10-12 16:31:21 -070044 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070045 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070046 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080047 hidden_ssid_(hidden_ssid),
mukesh agrawalb54601c2011-06-07 17:39:22 -070048 task_factory_(this),
49 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070050 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070051 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070052 store->RegisterConstString(flimflam::kModeProperty, &mode_);
Gaurav Shahda6218a2011-11-11 12:09:33 -080053 store->RegisterWriteOnlyString(flimflam::kPassphraseProperty, &passphrase_);
Paul Stewartac4ac002011-08-26 12:04:26 -070054 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
55 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
56 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070057
Paul Stewartac4ac002011-08-26 12:04:26 -070058 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
59 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
60 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
61 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070062
mukesh agrawald835b202011-10-07 15:26:47 -070063 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
64 string ssid_string(
65 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
66 if (SanitizeSSID(&ssid_string)) {
67 // WifiHexSsid property should only be present if Name property
68 // has been munged.
69 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
70 }
71 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070072
mukesh agrawal6e277772011-09-29 15:04:23 -070073 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
74 // a service that is not 802.1x.
75 if (security_ == flimflam::kSecurity8021x) {
76 NOTIMPLEMENTED();
77 // XXX needs_passpharse_ = false ?
78 } else if (security_ == flimflam::kSecurityPsk) {
79 SetEAPKeyManagement("WPA-PSK");
80 need_passphrase_ = true;
81 } else if (security_ == flimflam::kSecurityRsn) {
82 SetEAPKeyManagement("WPA-PSK");
83 need_passphrase_ = true;
84 } else if (security_ == flimflam::kSecurityWpa) {
85 SetEAPKeyManagement("WPA-PSK");
86 need_passphrase_ = true;
87 } else if (security_ == flimflam::kSecurityWep) {
88 SetEAPKeyManagement("NONE");
89 need_passphrase_ = true;
90 } else if (security_ == flimflam::kSecurityNone) {
91 SetEAPKeyManagement("NONE");
92 need_passphrase_ = false;
93 } else {
94 LOG(ERROR) << "unsupported security method " << security_;
95 }
96
Paul Stewartd08f4432011-11-04 07:48:20 -070097 // Until we know better (at Profile load time), use the generic name.
98 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawalb54601c2011-06-07 17:39:22 -070099}
100
101WiFiService::~WiFiService() {
102 LOG(INFO) << __func__;
103}
104
mukesh agrawal1830fa12011-09-26 14:31:40 -0700105void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700106 LOG(INFO) << __func__;
107
108 // NB(quiche) defer handling, since dbus-c++ does not permit us to
109 // send an outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700110 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700111 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700112}
113
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700114void WiFiService::Disconnect() {
115 // TODO(quiche) RemoveNetwork from supplicant
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700116}
117
Paul Stewart22aa71b2011-09-16 12:15:11 -0700118bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
119 return wifi_->TechnologyIs(type);
120}
121
Chris Masone6515aab2011-10-12 16:19:09 -0700122string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700123 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700124}
Chris Masone092df3e2011-08-22 09:41:39 -0700125const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700126 return mode_;
127}
128
Chris Masone092df3e2011-08-22 09:41:39 -0700129const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700130 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700131}
132
Paul Stewarta41e38d2011-11-11 07:47:29 -0800133const vector<uint8_t> &WiFiService::ssid() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700134 return ssid_;
135}
136
mukesh agrawal1a056262011-10-05 14:36:54 -0700137void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
138 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000139 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700140 } else if (security_ == flimflam::kSecurityPsk ||
141 security_ == flimflam::kSecurityWpa ||
142 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000143 ValidateWPAPassphrase(passphrase, error);
144 } else {
145 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700146 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000147
148 if (error->IsSuccess())
149 passphrase_ = passphrase;
mukesh agrawal1a056262011-10-05 14:36:54 -0700150}
151
Paul Stewartd08f4432011-11-04 07:48:20 -0700152bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
153 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
154 storage->ContainsGroup(GetSpecificStorageIdentifier());
155}
156
Paul Stewarta41e38d2011-11-11 07:47:29 -0800157bool WiFiService::IsVisible() const {
158 // TODO(quiche): Write a function that returns whether (or which)
159 // endpoints are associated with this service. crosbug.com/22948
160 const bool is_visible_in_scan = true;
161
162 // WiFi Services should be displayed only if they are in range (have
163 // endpoints that have shown up in a scan) or if the service is actively
164 // being connected.
165 return is_visible_in_scan || IsConnected() || IsConnecting();
166}
167
Paul Stewartd08f4432011-11-04 07:48:20 -0700168bool WiFiService::Load(StoreInterface *storage) {
169 // First find out which storage identifier is available in priority order
170 // of specific, generic.
171 string id = GetSpecificStorageIdentifier();
172 if (!storage->ContainsGroup(id)) {
173 id = GetGenericStorageIdentifier();
174 if (!storage->ContainsGroup(id)) {
175 LOG(WARNING) << "Service is not available in the persistent store: "
176 << id;
177 return false;
178 }
179 }
180
181 // Set our storage identifier to match the storage name in the Profile.
182 storage_identifier_ = id;
183
184 // Load properties common to all Services.
185 if (!Service::Load(storage)) {
186 return false;
187 }
188
189 // Load properties specific to WiFi services.
190 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
191 return true;
192}
193
194bool WiFiService::Save(StoreInterface *storage) {
195 // Save properties common to all Services.
196 if (!Service::Save(storage)) {
197 return false;
198 }
199
200 // Save properties specific to WiFi services.
201 const string id = GetStorageIdentifier();
202 storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_);
203 return true;
204}
205
Paul Stewart6ab23a92011-11-09 17:17:47 -0800206bool WiFiService::IsSecurityMatch(const string &security) const {
207 return GetSecurityClass(security) == GetSecurityClass(security_);
208}
209
mukesh agrawal32399322011-09-01 10:53:43 -0700210// private methods
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700211void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700212 std::map<string, DBus::Variant> params;
213 DBus::MessageIter writer;
214
215 params[wpa_supplicant::kNetworkPropertyMode].writer().
216 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
217
218 if (security_ == flimflam::kSecurity8021x) {
219 NOTIMPLEMENTED();
220 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800221 const string psk_proto = StringPrintf("%s %s",
222 wpa_supplicant::kSecurityModeWPA,
223 wpa_supplicant::kSecurityModeRSN);
224 params[wpa_supplicant::kPropertySecurityProtocol].writer().
225 append_string(psk_proto.c_str());
226 params[wpa_supplicant::kPropertyPreSharedKey].writer().
227 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700228 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700229 params[wpa_supplicant::kPropertySecurityProtocol].writer().
230 append_string(wpa_supplicant::kSecurityModeRSN);
231 params[wpa_supplicant::kPropertyPreSharedKey].writer().
232 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700233 } else if (security_ == flimflam::kSecurityWpa) {
234 params[wpa_supplicant::kPropertySecurityProtocol].writer().
235 append_string(wpa_supplicant::kSecurityModeWPA);
236 params[wpa_supplicant::kPropertyPreSharedKey].writer().
237 append_string(passphrase_.c_str());
238 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000239 params[wpa_supplicant::kPropertyAuthAlg].writer().
240 append_string(wpa_supplicant::kSecurityAuthAlg);
241 Error error;
242 int key_index;
243 std::vector<uint8> password_bytes;
244 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
245 writer = params[wpa_supplicant::kPropertyWEPKey +
246 base::IntToString(key_index)].writer();
247 writer << password_bytes;
248 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
249 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700250 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800251 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700252 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800253 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700254 }
255
256 params[wpa_supplicant::kPropertyKeyManagement].writer().
257 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800258
259 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700260 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
261 writer << ssid_;
262
263 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700264}
265
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800266string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700267 return wifi_->GetRpcIdentifier();
268}
269
mukesh agrawal1a056262011-10-05 14:36:54 -0700270// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000271void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
272 Error *error) {
273 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700274}
275
276// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000277void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
278 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700279 unsigned int length = passphrase.length();
280 vector<uint8> passphrase_bytes;
281
282 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
283 if (length != IEEE_80211::kWPAHexLen &&
284 (length < IEEE_80211::kWPAAsciiMinLen ||
285 length > IEEE_80211::kWPAAsciiMaxLen)) {
286 error->Populate(Error::kInvalidPassphrase);
287 }
288 } else {
289 if (length < IEEE_80211::kWPAAsciiMinLen ||
290 length > IEEE_80211::kWPAAsciiMaxLen) {
291 error->Populate(Error::kInvalidPassphrase);
292 }
293 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000294}
mukesh agrawal1a056262011-10-05 14:36:54 -0700295
Thieu Lef4cbda92011-11-10 23:41:24 +0000296// static
297void WiFiService::ParseWEPPassphrase(const string &passphrase,
298 int *key_index,
299 std::vector<uint8> *password_bytes,
300 Error *error) {
301 unsigned int length = passphrase.length();
302 int key_index_local;
303 std::string password_text;
304 bool is_hex = false;
305
306 switch (length) {
307 case IEEE_80211::kWEP40AsciiLen:
308 case IEEE_80211::kWEP104AsciiLen:
309 key_index_local = 0;
310 password_text = passphrase;
311 break;
312 case IEEE_80211::kWEP40AsciiLen + 2:
313 case IEEE_80211::kWEP104AsciiLen + 2:
314 if (CheckWEPKeyIndex(passphrase, error)) {
315 base::StringToInt(passphrase.substr(0,1), &key_index_local);
316 password_text = passphrase.substr(2);
317 }
318 break;
319 case IEEE_80211::kWEP40HexLen:
320 case IEEE_80211::kWEP104HexLen:
321 if (CheckWEPIsHex(passphrase, error)) {
322 key_index_local = 0;
323 password_text = passphrase;
324 is_hex = true;
325 }
326 break;
327 case IEEE_80211::kWEP40HexLen + 2:
328 case IEEE_80211::kWEP104HexLen + 2:
329 if(CheckWEPKeyIndex(passphrase, error) &&
330 CheckWEPIsHex(passphrase.substr(2), error)) {
331 base::StringToInt(passphrase.substr(0,1), &key_index_local);
332 password_text = passphrase.substr(2);
333 is_hex = true;
334 } else if (CheckWEPPrefix(passphrase, error) &&
335 CheckWEPIsHex(passphrase.substr(2), error)) {
336 key_index_local = 0;
337 password_text = passphrase.substr(2);
338 is_hex = true;
339 }
340 break;
341 case IEEE_80211::kWEP40HexLen + 4:
342 case IEEE_80211::kWEP104HexLen + 4:
343 if (CheckWEPKeyIndex(passphrase, error) &&
344 CheckWEPPrefix(passphrase.substr(2), error) &&
345 CheckWEPIsHex(passphrase.substr(4), error)) {
346 base::StringToInt(passphrase.substr(0,1), &key_index_local);
347 password_text = passphrase.substr(4);
348 is_hex = true;
349 }
350 break;
351 default:
352 error->Populate(Error::kInvalidPassphrase);
353 break;
354 }
355
mukesh agrawal1a056262011-10-05 14:36:54 -0700356 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000357 if (key_index)
358 *key_index = key_index_local;
359 if (password_bytes) {
360 if (is_hex)
361 base::HexStringToBytes(password_text, password_bytes);
362 else
363 password_bytes->insert(password_bytes->end(),
364 password_text.begin(),
365 password_text.end());
366 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700367 }
368}
369
370// static
371bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
372 vector<uint8> passphrase_bytes;
373 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
374 return true;
375 } else {
376 error->Populate(Error::kInvalidPassphrase);
377 return false;
378 }
379}
380
381// static
382bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
383 if (StartsWithASCII(passphrase, "0:", false) ||
384 StartsWithASCII(passphrase, "1:", false) ||
385 StartsWithASCII(passphrase, "2:", false) ||
386 StartsWithASCII(passphrase, "3:", false)) {
387 return true;
388 } else {
389 error->Populate(Error::kInvalidPassphrase);
390 return false;
391 }
392}
393
394// static
395bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
396 if (StartsWithASCII(passphrase, "0x", false)) {
397 return true;
398 } else {
399 error->Populate(Error::kInvalidPassphrase);
400 return false;
401 }
402}
403
Paul Stewart6ab23a92011-11-09 17:17:47 -0800404// static
mukesh agrawald835b202011-10-07 15:26:47 -0700405bool WiFiService::SanitizeSSID(string *ssid) {
406 CHECK(ssid);
407
408 size_t ssid_len = ssid->length();
409 size_t i;
410 bool changed = false;
411
412 for (i=0; i < ssid_len; ++i) {
413 if (!g_ascii_isprint((*ssid)[i])) {
414 (*ssid)[i] = '?';
415 changed = true;
416 }
417 }
418
419 return changed;
420}
421
Paul Stewart6ab23a92011-11-09 17:17:47 -0800422// static
423string WiFiService::GetSecurityClass(const string &security) {
424 if (security == flimflam::kSecurityRsn ||
425 security == flimflam::kSecurityWpa) {
426 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700427 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800428 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700429 }
430}
431
Paul Stewarta41e38d2011-11-11 07:47:29 -0800432// static
433bool WiFiService::ParseStorageIdentifier(const string &storage_name,
434 string *address,
435 string *mode,
436 string *security) {
437 vector<string> wifi_parts;
438 base::SplitString(storage_name, '_', &wifi_parts);
439 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
440 return false;
441 }
442 *address = wifi_parts[1];
443 *mode = wifi_parts[3];
444 *security = wifi_parts[4];
445 return true;
446}
447
Paul Stewart6ab23a92011-11-09 17:17:47 -0800448string WiFiService::GetGenericStorageIdentifier() const {
449 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
450}
451
Paul Stewartd08f4432011-11-04 07:48:20 -0700452string WiFiService::GetSpecificStorageIdentifier() const {
453 return GetStorageIdentifierForSecurity(security_);
454}
455
456string WiFiService::GetStorageIdentifierForSecurity(
457 const string &security) const {
458 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
459 flimflam::kTypeWifi,
460 wifi_->address().c_str(),
461 hex_ssid_.c_str(),
462 mode_.c_str(),
463 security.c_str()));
464}
465
mukesh agrawalb54601c2011-06-07 17:39:22 -0700466} // namespace shill