blob: 8390273180a40278b064ffa13558b05dada3ad6e [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>
12#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070014#include <dbus/dbus.h>
mukesh agrawald835b202011-10-07 15:26:47 -070015#include <glib.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070016
17#include "shill/control_interface.h"
18#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070019#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070020#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070021#include "shill/ieee80211.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070022#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070023#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070024#include "shill/wifi_endpoint.h"
25#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070026
27using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070028using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070029
30namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070031
Paul Stewartd08f4432011-11-04 07:48:20 -070032const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
33
mukesh agrawalb54601c2011-06-07 17:39:22 -070034WiFiService::WiFiService(ControlInterface *control_interface,
35 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070036 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070037 const WiFiRefPtr &device,
mukesh agrawal7ec71312011-11-10 02:08:26 +000038 const std::vector<uint8_t> &ssid,
Chris Masone092df3e2011-08-22 09:41:39 -070039 const std::string &mode,
Paul Stewartced6a0b2011-11-08 15:32:04 -080040 const std::string &security,
41 bool hidden_ssid)
mukesh agrawal7a4e4002011-09-06 11:26:05 -070042 : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi),
Chris Masone75612302011-10-12 16:31:21 -070043 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070044 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070045 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080046 hidden_ssid_(hidden_ssid),
mukesh agrawalb54601c2011-06-07 17:39:22 -070047 task_factory_(this),
48 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070049 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070050 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070051 store->RegisterConstString(flimflam::kModeProperty, &mode_);
Gaurav Shahda6218a2011-11-11 12:09:33 -080052 store->RegisterWriteOnlyString(flimflam::kPassphraseProperty, &passphrase_);
Paul Stewartac4ac002011-08-26 12:04:26 -070053 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
54 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
55 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070056
Paul Stewartac4ac002011-08-26 12:04:26 -070057 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
58 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
59 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
60 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070061
mukesh agrawald835b202011-10-07 15:26:47 -070062 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
63 string ssid_string(
64 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
65 if (SanitizeSSID(&ssid_string)) {
66 // WifiHexSsid property should only be present if Name property
67 // has been munged.
68 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
69 }
70 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070071
mukesh agrawal6e277772011-09-29 15:04:23 -070072 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
73 // a service that is not 802.1x.
74 if (security_ == flimflam::kSecurity8021x) {
75 NOTIMPLEMENTED();
76 // XXX needs_passpharse_ = false ?
77 } else if (security_ == flimflam::kSecurityPsk) {
78 SetEAPKeyManagement("WPA-PSK");
79 need_passphrase_ = true;
80 } else if (security_ == flimflam::kSecurityRsn) {
81 SetEAPKeyManagement("WPA-PSK");
82 need_passphrase_ = true;
83 } else if (security_ == flimflam::kSecurityWpa) {
84 SetEAPKeyManagement("WPA-PSK");
85 need_passphrase_ = true;
86 } else if (security_ == flimflam::kSecurityWep) {
87 SetEAPKeyManagement("NONE");
88 need_passphrase_ = true;
89 } else if (security_ == flimflam::kSecurityNone) {
90 SetEAPKeyManagement("NONE");
91 need_passphrase_ = false;
92 } else {
93 LOG(ERROR) << "unsupported security method " << security_;
94 }
95
Paul Stewartd08f4432011-11-04 07:48:20 -070096 // Until we know better (at Profile load time), use the generic name.
97 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawalb54601c2011-06-07 17:39:22 -070098}
99
100WiFiService::~WiFiService() {
101 LOG(INFO) << __func__;
102}
103
mukesh agrawal1830fa12011-09-26 14:31:40 -0700104void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700105 LOG(INFO) << __func__;
106
107 // NB(quiche) defer handling, since dbus-c++ does not permit us to
108 // send an outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700109 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700110 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700111}
112
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700113void WiFiService::Disconnect() {
114 // TODO(quiche) RemoveNetwork from supplicant
115 // XXX remove from favorite networks list?
116}
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
133const std::vector<uint8_t> &WiFiService::ssid() const {
134 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
157bool WiFiService::Load(StoreInterface *storage) {
158 // First find out which storage identifier is available in priority order
159 // of specific, generic.
160 string id = GetSpecificStorageIdentifier();
161 if (!storage->ContainsGroup(id)) {
162 id = GetGenericStorageIdentifier();
163 if (!storage->ContainsGroup(id)) {
164 LOG(WARNING) << "Service is not available in the persistent store: "
165 << id;
166 return false;
167 }
168 }
169
170 // Set our storage identifier to match the storage name in the Profile.
171 storage_identifier_ = id;
172
173 // Load properties common to all Services.
174 if (!Service::Load(storage)) {
175 return false;
176 }
177
178 // Load properties specific to WiFi services.
179 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
180 return true;
181}
182
183bool WiFiService::Save(StoreInterface *storage) {
184 // Save properties common to all Services.
185 if (!Service::Save(storage)) {
186 return false;
187 }
188
189 // Save properties specific to WiFi services.
190 const string id = GetStorageIdentifier();
191 storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_);
192 return true;
193}
194
Paul Stewart6ab23a92011-11-09 17:17:47 -0800195bool WiFiService::IsSecurityMatch(const string &security) const {
196 return GetSecurityClass(security) == GetSecurityClass(security_);
197}
198
mukesh agrawal32399322011-09-01 10:53:43 -0700199// private methods
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700200void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700201 std::map<string, DBus::Variant> params;
202 DBus::MessageIter writer;
203
204 params[wpa_supplicant::kNetworkPropertyMode].writer().
205 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
206
207 if (security_ == flimflam::kSecurity8021x) {
208 NOTIMPLEMENTED();
209 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800210 const string psk_proto = StringPrintf("%s %s",
211 wpa_supplicant::kSecurityModeWPA,
212 wpa_supplicant::kSecurityModeRSN);
213 params[wpa_supplicant::kPropertySecurityProtocol].writer().
214 append_string(psk_proto.c_str());
215 params[wpa_supplicant::kPropertyPreSharedKey].writer().
216 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700217 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700218 params[wpa_supplicant::kPropertySecurityProtocol].writer().
219 append_string(wpa_supplicant::kSecurityModeRSN);
220 params[wpa_supplicant::kPropertyPreSharedKey].writer().
221 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700222 } else if (security_ == flimflam::kSecurityWpa) {
223 params[wpa_supplicant::kPropertySecurityProtocol].writer().
224 append_string(wpa_supplicant::kSecurityModeWPA);
225 params[wpa_supplicant::kPropertyPreSharedKey].writer().
226 append_string(passphrase_.c_str());
227 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000228 params[wpa_supplicant::kPropertyAuthAlg].writer().
229 append_string(wpa_supplicant::kSecurityAuthAlg);
230 Error error;
231 int key_index;
232 std::vector<uint8> password_bytes;
233 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
234 writer = params[wpa_supplicant::kPropertyWEPKey +
235 base::IntToString(key_index)].writer();
236 writer << password_bytes;
237 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
238 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700239 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800240 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700241 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800242 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700243 }
244
245 params[wpa_supplicant::kPropertyKeyManagement].writer().
246 append_string(key_management().c_str());
247 // TODO(quiche): figure out why we can't use operator<< without the
248 // temporary variable.
249 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
250 writer << ssid_;
251
252 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700253}
254
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800255string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700256 return wifi_->GetRpcIdentifier();
257}
258
mukesh agrawal1a056262011-10-05 14:36:54 -0700259// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000260void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
261 Error *error) {
262 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700263}
264
265// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000266void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
267 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700268 unsigned int length = passphrase.length();
269 vector<uint8> passphrase_bytes;
270
271 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
272 if (length != IEEE_80211::kWPAHexLen &&
273 (length < IEEE_80211::kWPAAsciiMinLen ||
274 length > IEEE_80211::kWPAAsciiMaxLen)) {
275 error->Populate(Error::kInvalidPassphrase);
276 }
277 } else {
278 if (length < IEEE_80211::kWPAAsciiMinLen ||
279 length > IEEE_80211::kWPAAsciiMaxLen) {
280 error->Populate(Error::kInvalidPassphrase);
281 }
282 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000283}
mukesh agrawal1a056262011-10-05 14:36:54 -0700284
Thieu Lef4cbda92011-11-10 23:41:24 +0000285// static
286void WiFiService::ParseWEPPassphrase(const string &passphrase,
287 int *key_index,
288 std::vector<uint8> *password_bytes,
289 Error *error) {
290 unsigned int length = passphrase.length();
291 int key_index_local;
292 std::string password_text;
293 bool is_hex = false;
294
295 switch (length) {
296 case IEEE_80211::kWEP40AsciiLen:
297 case IEEE_80211::kWEP104AsciiLen:
298 key_index_local = 0;
299 password_text = passphrase;
300 break;
301 case IEEE_80211::kWEP40AsciiLen + 2:
302 case IEEE_80211::kWEP104AsciiLen + 2:
303 if (CheckWEPKeyIndex(passphrase, error)) {
304 base::StringToInt(passphrase.substr(0,1), &key_index_local);
305 password_text = passphrase.substr(2);
306 }
307 break;
308 case IEEE_80211::kWEP40HexLen:
309 case IEEE_80211::kWEP104HexLen:
310 if (CheckWEPIsHex(passphrase, error)) {
311 key_index_local = 0;
312 password_text = passphrase;
313 is_hex = true;
314 }
315 break;
316 case IEEE_80211::kWEP40HexLen + 2:
317 case IEEE_80211::kWEP104HexLen + 2:
318 if(CheckWEPKeyIndex(passphrase, error) &&
319 CheckWEPIsHex(passphrase.substr(2), error)) {
320 base::StringToInt(passphrase.substr(0,1), &key_index_local);
321 password_text = passphrase.substr(2);
322 is_hex = true;
323 } else if (CheckWEPPrefix(passphrase, error) &&
324 CheckWEPIsHex(passphrase.substr(2), error)) {
325 key_index_local = 0;
326 password_text = passphrase.substr(2);
327 is_hex = true;
328 }
329 break;
330 case IEEE_80211::kWEP40HexLen + 4:
331 case IEEE_80211::kWEP104HexLen + 4:
332 if (CheckWEPKeyIndex(passphrase, error) &&
333 CheckWEPPrefix(passphrase.substr(2), error) &&
334 CheckWEPIsHex(passphrase.substr(4), error)) {
335 base::StringToInt(passphrase.substr(0,1), &key_index_local);
336 password_text = passphrase.substr(4);
337 is_hex = true;
338 }
339 break;
340 default:
341 error->Populate(Error::kInvalidPassphrase);
342 break;
343 }
344
mukesh agrawal1a056262011-10-05 14:36:54 -0700345 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000346 if (key_index)
347 *key_index = key_index_local;
348 if (password_bytes) {
349 if (is_hex)
350 base::HexStringToBytes(password_text, password_bytes);
351 else
352 password_bytes->insert(password_bytes->end(),
353 password_text.begin(),
354 password_text.end());
355 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700356 }
357}
358
359// static
360bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
361 vector<uint8> passphrase_bytes;
362 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
363 return true;
364 } else {
365 error->Populate(Error::kInvalidPassphrase);
366 return false;
367 }
368}
369
370// static
371bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
372 if (StartsWithASCII(passphrase, "0:", false) ||
373 StartsWithASCII(passphrase, "1:", false) ||
374 StartsWithASCII(passphrase, "2:", false) ||
375 StartsWithASCII(passphrase, "3:", false)) {
376 return true;
377 } else {
378 error->Populate(Error::kInvalidPassphrase);
379 return false;
380 }
381}
382
383// static
384bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
385 if (StartsWithASCII(passphrase, "0x", false)) {
386 return true;
387 } else {
388 error->Populate(Error::kInvalidPassphrase);
389 return false;
390 }
391}
392
Paul Stewart6ab23a92011-11-09 17:17:47 -0800393// static
mukesh agrawald835b202011-10-07 15:26:47 -0700394bool WiFiService::SanitizeSSID(string *ssid) {
395 CHECK(ssid);
396
397 size_t ssid_len = ssid->length();
398 size_t i;
399 bool changed = false;
400
401 for (i=0; i < ssid_len; ++i) {
402 if (!g_ascii_isprint((*ssid)[i])) {
403 (*ssid)[i] = '?';
404 changed = true;
405 }
406 }
407
408 return changed;
409}
410
Paul Stewart6ab23a92011-11-09 17:17:47 -0800411// static
412string WiFiService::GetSecurityClass(const string &security) {
413 if (security == flimflam::kSecurityRsn ||
414 security == flimflam::kSecurityWpa) {
415 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700416 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800417 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700418 }
419}
420
Paul Stewart6ab23a92011-11-09 17:17:47 -0800421string WiFiService::GetGenericStorageIdentifier() const {
422 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
423}
424
Paul Stewartd08f4432011-11-04 07:48:20 -0700425string WiFiService::GetSpecificStorageIdentifier() const {
426 return GetStorageIdentifierForSecurity(security_);
427}
428
429string WiFiService::GetStorageIdentifierForSecurity(
430 const string &security) const {
431 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
432 flimflam::kTypeWifi,
433 wifi_->address().c_str(),
434 hex_ssid_.c_str(),
435 mode_.c_str(),
436 security.c_str()));
437}
438
mukesh agrawalb54601c2011-06-07 17:39:22 -0700439} // namespace shill