blob: 9f61866e8188f09ab49c97eb8f40680a11ca2ae5 [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 agrawalb54601c2011-06-07 17:39:22 -070038 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_);
52 store->RegisterString(flimflam::kPassphraseProperty, &passphrase_);
53 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) {
139 passphrase_ = ParseWEPPassphrase(passphrase, error);
140 } else if (security_ == flimflam::kSecurityPsk ||
141 security_ == flimflam::kSecurityWpa ||
142 security_ == flimflam::kSecurityRsn) {
143 passphrase_ = ParseWPAPassphrase(passphrase, error);
144 }
145}
146
Paul Stewartd08f4432011-11-04 07:48:20 -0700147bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
148 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
149 storage->ContainsGroup(GetSpecificStorageIdentifier());
150}
151
152bool WiFiService::Load(StoreInterface *storage) {
153 // First find out which storage identifier is available in priority order
154 // of specific, generic.
155 string id = GetSpecificStorageIdentifier();
156 if (!storage->ContainsGroup(id)) {
157 id = GetGenericStorageIdentifier();
158 if (!storage->ContainsGroup(id)) {
159 LOG(WARNING) << "Service is not available in the persistent store: "
160 << id;
161 return false;
162 }
163 }
164
165 // Set our storage identifier to match the storage name in the Profile.
166 storage_identifier_ = id;
167
168 // Load properties common to all Services.
169 if (!Service::Load(storage)) {
170 return false;
171 }
172
173 // Load properties specific to WiFi services.
174 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
175 return true;
176}
177
178bool WiFiService::Save(StoreInterface *storage) {
179 // Save properties common to all Services.
180 if (!Service::Save(storage)) {
181 return false;
182 }
183
184 // Save properties specific to WiFi services.
185 const string id = GetStorageIdentifier();
186 storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_);
187 return true;
188}
189
Paul Stewart6ab23a92011-11-09 17:17:47 -0800190bool WiFiService::IsSecurityMatch(const string &security) const {
191 return GetSecurityClass(security) == GetSecurityClass(security_);
192}
193
mukesh agrawal32399322011-09-01 10:53:43 -0700194// private methods
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700195void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700196 std::map<string, DBus::Variant> params;
197 DBus::MessageIter writer;
198
199 params[wpa_supplicant::kNetworkPropertyMode].writer().
200 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
201
202 if (security_ == flimflam::kSecurity8021x) {
203 NOTIMPLEMENTED();
204 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800205 const string psk_proto = StringPrintf("%s %s",
206 wpa_supplicant::kSecurityModeWPA,
207 wpa_supplicant::kSecurityModeRSN);
208 params[wpa_supplicant::kPropertySecurityProtocol].writer().
209 append_string(psk_proto.c_str());
210 params[wpa_supplicant::kPropertyPreSharedKey].writer().
211 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700212 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700213 params[wpa_supplicant::kPropertySecurityProtocol].writer().
214 append_string(wpa_supplicant::kSecurityModeRSN);
215 params[wpa_supplicant::kPropertyPreSharedKey].writer().
216 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700217 } else if (security_ == flimflam::kSecurityWpa) {
218 params[wpa_supplicant::kPropertySecurityProtocol].writer().
219 append_string(wpa_supplicant::kSecurityModeWPA);
220 params[wpa_supplicant::kPropertyPreSharedKey].writer().
221 append_string(passphrase_.c_str());
222 } else if (security_ == flimflam::kSecurityWep) {
223 NOTIMPLEMENTED();
224 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800225 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700226 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800227 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700228 }
229
230 params[wpa_supplicant::kPropertyKeyManagement].writer().
231 append_string(key_management().c_str());
232 // TODO(quiche): figure out why we can't use operator<< without the
233 // temporary variable.
234 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
235 writer << ssid_;
236
237 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700238}
239
Chris Masone95207da2011-06-29 16:50:49 -0700240string WiFiService::GetDeviceRpcId() {
241 return wifi_->GetRpcIdentifier();
242}
243
mukesh agrawal1a056262011-10-05 14:36:54 -0700244// static
245string WiFiService::ParseWEPPassphrase(const string &passphrase, Error *error) {
246 unsigned int length = passphrase.length();
247
248 switch (length) {
249 case IEEE_80211::kWEP40AsciiLen:
250 case IEEE_80211::kWEP104AsciiLen:
251 break;
252 case IEEE_80211::kWEP40AsciiLen + 2:
253 case IEEE_80211::kWEP104AsciiLen + 2:
254 CheckWEPKeyIndex(passphrase, error);
255 break;
256 case IEEE_80211::kWEP40HexLen:
257 case IEEE_80211::kWEP104HexLen:
258 CheckWEPIsHex(passphrase, error);
259 break;
260 case IEEE_80211::kWEP40HexLen + 2:
261 case IEEE_80211::kWEP104HexLen + 2:
262 (CheckWEPKeyIndex(passphrase, error) ||
263 CheckWEPPrefix(passphrase, error)) &&
264 CheckWEPIsHex(passphrase.substr(2), error);
265 break;
266 case IEEE_80211::kWEP40HexLen + 4:
267 case IEEE_80211::kWEP104HexLen + 4:
268 CheckWEPKeyIndex(passphrase, error) &&
269 CheckWEPPrefix(passphrase.substr(2), error) &&
270 CheckWEPIsHex(passphrase.substr(4), error);
271 break;
272 default:
273 error->Populate(Error::kInvalidPassphrase);
274 break;
275 }
276
277 // TODO(quiche): may need to normalize passphrase format
278 if (error->IsSuccess()) {
279 return passphrase;
280 } else {
281 return "";
282 }
283}
284
285// static
286string WiFiService::ParseWPAPassphrase(const string &passphrase, Error *error) {
287 unsigned int length = passphrase.length();
288 vector<uint8> passphrase_bytes;
289
290 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
291 if (length != IEEE_80211::kWPAHexLen &&
292 (length < IEEE_80211::kWPAAsciiMinLen ||
293 length > IEEE_80211::kWPAAsciiMaxLen)) {
294 error->Populate(Error::kInvalidPassphrase);
295 }
296 } else {
297 if (length < IEEE_80211::kWPAAsciiMinLen ||
298 length > IEEE_80211::kWPAAsciiMaxLen) {
299 error->Populate(Error::kInvalidPassphrase);
300 }
301 }
302
303 // TODO(quiche): may need to normalize passphrase format
304 if (error->IsSuccess()) {
305 return passphrase;
306 } else {
307 return "";
308 }
309}
310
311// static
312bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
313 vector<uint8> passphrase_bytes;
314 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
315 return true;
316 } else {
317 error->Populate(Error::kInvalidPassphrase);
318 return false;
319 }
320}
321
322// static
323bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
324 if (StartsWithASCII(passphrase, "0:", false) ||
325 StartsWithASCII(passphrase, "1:", false) ||
326 StartsWithASCII(passphrase, "2:", false) ||
327 StartsWithASCII(passphrase, "3:", false)) {
328 return true;
329 } else {
330 error->Populate(Error::kInvalidPassphrase);
331 return false;
332 }
333}
334
335// static
336bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
337 if (StartsWithASCII(passphrase, "0x", false)) {
338 return true;
339 } else {
340 error->Populate(Error::kInvalidPassphrase);
341 return false;
342 }
343}
344
Paul Stewart6ab23a92011-11-09 17:17:47 -0800345// static
mukesh agrawald835b202011-10-07 15:26:47 -0700346bool WiFiService::SanitizeSSID(string *ssid) {
347 CHECK(ssid);
348
349 size_t ssid_len = ssid->length();
350 size_t i;
351 bool changed = false;
352
353 for (i=0; i < ssid_len; ++i) {
354 if (!g_ascii_isprint((*ssid)[i])) {
355 (*ssid)[i] = '?';
356 changed = true;
357 }
358 }
359
360 return changed;
361}
362
Paul Stewart6ab23a92011-11-09 17:17:47 -0800363// static
364string WiFiService::GetSecurityClass(const string &security) {
365 if (security == flimflam::kSecurityRsn ||
366 security == flimflam::kSecurityWpa) {
367 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700368 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800369 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700370 }
371}
372
Paul Stewart6ab23a92011-11-09 17:17:47 -0800373string WiFiService::GetGenericStorageIdentifier() const {
374 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
375}
376
Paul Stewartd08f4432011-11-04 07:48:20 -0700377string WiFiService::GetSpecificStorageIdentifier() const {
378 return GetStorageIdentifierForSecurity(security_);
379}
380
381string WiFiService::GetStorageIdentifierForSecurity(
382 const string &security) const {
383 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
384 flimflam::kTypeWifi,
385 wifi_->address().c_str(),
386 hex_ssid_.c_str(),
387 mode_.c_str(),
388 security.c_str()));
389}
390
mukesh agrawalb54601c2011-06-07 17:39:22 -0700391} // namespace shill