blob: a189698a8861daf6f631b3e3409fe3a95787cc02 [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
mukesh agrawal32399322011-09-01 10:53:43 -0700190// private methods
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700191void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700192 std::map<string, DBus::Variant> params;
193 DBus::MessageIter writer;
194
195 params[wpa_supplicant::kNetworkPropertyMode].writer().
196 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
197
198 if (security_ == flimflam::kSecurity8021x) {
199 NOTIMPLEMENTED();
200 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800201 const string psk_proto = StringPrintf("%s %s",
202 wpa_supplicant::kSecurityModeWPA,
203 wpa_supplicant::kSecurityModeRSN);
204 params[wpa_supplicant::kPropertySecurityProtocol].writer().
205 append_string(psk_proto.c_str());
206 params[wpa_supplicant::kPropertyPreSharedKey].writer().
207 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700208 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700209 params[wpa_supplicant::kPropertySecurityProtocol].writer().
210 append_string(wpa_supplicant::kSecurityModeRSN);
211 params[wpa_supplicant::kPropertyPreSharedKey].writer().
212 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700213 } else if (security_ == flimflam::kSecurityWpa) {
214 params[wpa_supplicant::kPropertySecurityProtocol].writer().
215 append_string(wpa_supplicant::kSecurityModeWPA);
216 params[wpa_supplicant::kPropertyPreSharedKey].writer().
217 append_string(passphrase_.c_str());
218 } else if (security_ == flimflam::kSecurityWep) {
219 NOTIMPLEMENTED();
220 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800221 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700222 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800223 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700224 }
225
226 params[wpa_supplicant::kPropertyKeyManagement].writer().
227 append_string(key_management().c_str());
228 // TODO(quiche): figure out why we can't use operator<< without the
229 // temporary variable.
230 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
231 writer << ssid_;
232
233 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700234}
235
Chris Masone95207da2011-06-29 16:50:49 -0700236string WiFiService::GetDeviceRpcId() {
237 return wifi_->GetRpcIdentifier();
238}
239
mukesh agrawal1a056262011-10-05 14:36:54 -0700240// static
241string WiFiService::ParseWEPPassphrase(const string &passphrase, Error *error) {
242 unsigned int length = passphrase.length();
243
244 switch (length) {
245 case IEEE_80211::kWEP40AsciiLen:
246 case IEEE_80211::kWEP104AsciiLen:
247 break;
248 case IEEE_80211::kWEP40AsciiLen + 2:
249 case IEEE_80211::kWEP104AsciiLen + 2:
250 CheckWEPKeyIndex(passphrase, error);
251 break;
252 case IEEE_80211::kWEP40HexLen:
253 case IEEE_80211::kWEP104HexLen:
254 CheckWEPIsHex(passphrase, error);
255 break;
256 case IEEE_80211::kWEP40HexLen + 2:
257 case IEEE_80211::kWEP104HexLen + 2:
258 (CheckWEPKeyIndex(passphrase, error) ||
259 CheckWEPPrefix(passphrase, error)) &&
260 CheckWEPIsHex(passphrase.substr(2), error);
261 break;
262 case IEEE_80211::kWEP40HexLen + 4:
263 case IEEE_80211::kWEP104HexLen + 4:
264 CheckWEPKeyIndex(passphrase, error) &&
265 CheckWEPPrefix(passphrase.substr(2), error) &&
266 CheckWEPIsHex(passphrase.substr(4), error);
267 break;
268 default:
269 error->Populate(Error::kInvalidPassphrase);
270 break;
271 }
272
273 // TODO(quiche): may need to normalize passphrase format
274 if (error->IsSuccess()) {
275 return passphrase;
276 } else {
277 return "";
278 }
279}
280
281// static
282string WiFiService::ParseWPAPassphrase(const string &passphrase, Error *error) {
283 unsigned int length = passphrase.length();
284 vector<uint8> passphrase_bytes;
285
286 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
287 if (length != IEEE_80211::kWPAHexLen &&
288 (length < IEEE_80211::kWPAAsciiMinLen ||
289 length > IEEE_80211::kWPAAsciiMaxLen)) {
290 error->Populate(Error::kInvalidPassphrase);
291 }
292 } else {
293 if (length < IEEE_80211::kWPAAsciiMinLen ||
294 length > IEEE_80211::kWPAAsciiMaxLen) {
295 error->Populate(Error::kInvalidPassphrase);
296 }
297 }
298
299 // TODO(quiche): may need to normalize passphrase format
300 if (error->IsSuccess()) {
301 return passphrase;
302 } else {
303 return "";
304 }
305}
306
307// static
308bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
309 vector<uint8> passphrase_bytes;
310 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
311 return true;
312 } else {
313 error->Populate(Error::kInvalidPassphrase);
314 return false;
315 }
316}
317
318// static
319bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
320 if (StartsWithASCII(passphrase, "0:", false) ||
321 StartsWithASCII(passphrase, "1:", false) ||
322 StartsWithASCII(passphrase, "2:", false) ||
323 StartsWithASCII(passphrase, "3:", false)) {
324 return true;
325 } else {
326 error->Populate(Error::kInvalidPassphrase);
327 return false;
328 }
329}
330
331// static
332bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
333 if (StartsWithASCII(passphrase, "0x", false)) {
334 return true;
335 } else {
336 error->Populate(Error::kInvalidPassphrase);
337 return false;
338 }
339}
340
mukesh agrawald835b202011-10-07 15:26:47 -0700341bool WiFiService::SanitizeSSID(string *ssid) {
342 CHECK(ssid);
343
344 size_t ssid_len = ssid->length();
345 size_t i;
346 bool changed = false;
347
348 for (i=0; i < ssid_len; ++i) {
349 if (!g_ascii_isprint((*ssid)[i])) {
350 (*ssid)[i] = '?';
351 changed = true;
352 }
353 }
354
355 return changed;
356}
357
Paul Stewartd08f4432011-11-04 07:48:20 -0700358string WiFiService::GetGenericStorageIdentifier() const {
359 if (security_ == flimflam::kSecurityRsn ||
360 security_ == flimflam::kSecurityWpa) {
361 return GetStorageIdentifierForSecurity(flimflam::kSecurityPsk);
362 } else {
363 return GetStorageIdentifierForSecurity(security_);
364 }
365}
366
367string WiFiService::GetSpecificStorageIdentifier() const {
368 return GetStorageIdentifierForSecurity(security_);
369}
370
371string WiFiService::GetStorageIdentifierForSecurity(
372 const string &security) const {
373 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
374 flimflam::kTypeWifi,
375 wifi_->address().c_str(),
376 hex_ssid_.c_str(),
377 mode_.c_str(),
378 security.c_str()));
379}
380
mukesh agrawalb54601c2011-06-07 17:39:22 -0700381} // namespace shill