blob: 618f40ad448c9137b5180eac346d12b5b12f98cd [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,
mukesh agrawal6e277772011-09-29 15:04:23 -070040 const std::string &security)
mukesh agrawal7a4e4002011-09-06 11:26:05 -070041 : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi),
Chris Masone75612302011-10-12 16:31:21 -070042 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070043 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070044 mode_(mode),
Chris Masone75612302011-10-12 16:31:21 -070045 hidden_ssid_(false),
mukesh agrawalb54601c2011-06-07 17:39:22 -070046 task_factory_(this),
47 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070048 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070049 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070050 store->RegisterConstString(flimflam::kModeProperty, &mode_);
51 store->RegisterString(flimflam::kPassphraseProperty, &passphrase_);
52 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
53 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
54 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070055
Paul Stewartac4ac002011-08-26 12:04:26 -070056 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
57 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
58 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
59 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070060
mukesh agrawald835b202011-10-07 15:26:47 -070061 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
62 string ssid_string(
63 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
64 if (SanitizeSSID(&ssid_string)) {
65 // WifiHexSsid property should only be present if Name property
66 // has been munged.
67 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
68 }
69 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070070
mukesh agrawal6e277772011-09-29 15:04:23 -070071 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
72 // a service that is not 802.1x.
73 if (security_ == flimflam::kSecurity8021x) {
74 NOTIMPLEMENTED();
75 // XXX needs_passpharse_ = false ?
76 } else if (security_ == flimflam::kSecurityPsk) {
77 SetEAPKeyManagement("WPA-PSK");
78 need_passphrase_ = true;
79 } else if (security_ == flimflam::kSecurityRsn) {
80 SetEAPKeyManagement("WPA-PSK");
81 need_passphrase_ = true;
82 } else if (security_ == flimflam::kSecurityWpa) {
83 SetEAPKeyManagement("WPA-PSK");
84 need_passphrase_ = true;
85 } else if (security_ == flimflam::kSecurityWep) {
86 SetEAPKeyManagement("NONE");
87 need_passphrase_ = true;
88 } else if (security_ == flimflam::kSecurityNone) {
89 SetEAPKeyManagement("NONE");
90 need_passphrase_ = false;
91 } else {
92 LOG(ERROR) << "unsupported security method " << security_;
93 }
94
mukesh agrawal32399322011-09-01 10:53:43 -070095 // TODO(quiche): figure out when to set true
96 hidden_ssid_ = false;
Paul Stewartd08f4432011-11-04 07:48:20 -070097
98 // Until we know better (at Profile load time), use the generic name.
99 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700100}
101
102WiFiService::~WiFiService() {
103 LOG(INFO) << __func__;
104}
105
mukesh agrawal1830fa12011-09-26 14:31:40 -0700106void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700107 LOG(INFO) << __func__;
108
109 // NB(quiche) defer handling, since dbus-c++ does not permit us to
110 // send an outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700111 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700112 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700113}
114
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700115void WiFiService::Disconnect() {
116 // TODO(quiche) RemoveNetwork from supplicant
117 // XXX remove from favorite networks list?
118}
119
Paul Stewart22aa71b2011-09-16 12:15:11 -0700120bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
121 return wifi_->TechnologyIs(type);
122}
123
Chris Masone6515aab2011-10-12 16:19:09 -0700124string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700125 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700126}
Chris Masone092df3e2011-08-22 09:41:39 -0700127const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700128 return mode_;
129}
130
Chris Masone092df3e2011-08-22 09:41:39 -0700131const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700132 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700133}
134
135const std::vector<uint8_t> &WiFiService::ssid() const {
136 return ssid_;
137}
138
mukesh agrawal1a056262011-10-05 14:36:54 -0700139void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
140 if (security_ == flimflam::kSecurityWep) {
141 passphrase_ = ParseWEPPassphrase(passphrase, error);
142 } else if (security_ == flimflam::kSecurityPsk ||
143 security_ == flimflam::kSecurityWpa ||
144 security_ == flimflam::kSecurityRsn) {
145 passphrase_ = ParseWPAPassphrase(passphrase, error);
146 }
147}
148
Paul Stewartd08f4432011-11-04 07:48:20 -0700149bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
150 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
151 storage->ContainsGroup(GetSpecificStorageIdentifier());
152}
153
154bool WiFiService::Load(StoreInterface *storage) {
155 // First find out which storage identifier is available in priority order
156 // of specific, generic.
157 string id = GetSpecificStorageIdentifier();
158 if (!storage->ContainsGroup(id)) {
159 id = GetGenericStorageIdentifier();
160 if (!storage->ContainsGroup(id)) {
161 LOG(WARNING) << "Service is not available in the persistent store: "
162 << id;
163 return false;
164 }
165 }
166
167 // Set our storage identifier to match the storage name in the Profile.
168 storage_identifier_ = id;
169
170 // Load properties common to all Services.
171 if (!Service::Load(storage)) {
172 return false;
173 }
174
175 // Load properties specific to WiFi services.
176 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
177 return true;
178}
179
180bool WiFiService::Save(StoreInterface *storage) {
181 // Save properties common to all Services.
182 if (!Service::Save(storage)) {
183 return false;
184 }
185
186 // Save properties specific to WiFi services.
187 const string id = GetStorageIdentifier();
188 storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_);
189 return true;
190}
191
mukesh agrawal32399322011-09-01 10:53:43 -0700192// private methods
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700193void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700194 std::map<string, DBus::Variant> params;
195 DBus::MessageIter writer;
196
197 params[wpa_supplicant::kNetworkPropertyMode].writer().
198 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
199
200 if (security_ == flimflam::kSecurity8021x) {
201 NOTIMPLEMENTED();
202 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800203 const string psk_proto = StringPrintf("%s %s",
204 wpa_supplicant::kSecurityModeWPA,
205 wpa_supplicant::kSecurityModeRSN);
206 params[wpa_supplicant::kPropertySecurityProtocol].writer().
207 append_string(psk_proto.c_str());
208 params[wpa_supplicant::kPropertyPreSharedKey].writer().
209 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700210 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700211 params[wpa_supplicant::kPropertySecurityProtocol].writer().
212 append_string(wpa_supplicant::kSecurityModeRSN);
213 params[wpa_supplicant::kPropertyPreSharedKey].writer().
214 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700215 } else if (security_ == flimflam::kSecurityWpa) {
216 params[wpa_supplicant::kPropertySecurityProtocol].writer().
217 append_string(wpa_supplicant::kSecurityModeWPA);
218 params[wpa_supplicant::kPropertyPreSharedKey].writer().
219 append_string(passphrase_.c_str());
220 } else if (security_ == flimflam::kSecurityWep) {
221 NOTIMPLEMENTED();
222 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800223 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700224 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800225 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700226 }
227
228 params[wpa_supplicant::kPropertyKeyManagement].writer().
229 append_string(key_management().c_str());
230 // TODO(quiche): figure out why we can't use operator<< without the
231 // temporary variable.
232 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
233 writer << ssid_;
234
235 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700236}
237
Chris Masone95207da2011-06-29 16:50:49 -0700238string WiFiService::GetDeviceRpcId() {
239 return wifi_->GetRpcIdentifier();
240}
241
mukesh agrawal1a056262011-10-05 14:36:54 -0700242// static
243string WiFiService::ParseWEPPassphrase(const string &passphrase, Error *error) {
244 unsigned int length = passphrase.length();
245
246 switch (length) {
247 case IEEE_80211::kWEP40AsciiLen:
248 case IEEE_80211::kWEP104AsciiLen:
249 break;
250 case IEEE_80211::kWEP40AsciiLen + 2:
251 case IEEE_80211::kWEP104AsciiLen + 2:
252 CheckWEPKeyIndex(passphrase, error);
253 break;
254 case IEEE_80211::kWEP40HexLen:
255 case IEEE_80211::kWEP104HexLen:
256 CheckWEPIsHex(passphrase, error);
257 break;
258 case IEEE_80211::kWEP40HexLen + 2:
259 case IEEE_80211::kWEP104HexLen + 2:
260 (CheckWEPKeyIndex(passphrase, error) ||
261 CheckWEPPrefix(passphrase, error)) &&
262 CheckWEPIsHex(passphrase.substr(2), error);
263 break;
264 case IEEE_80211::kWEP40HexLen + 4:
265 case IEEE_80211::kWEP104HexLen + 4:
266 CheckWEPKeyIndex(passphrase, error) &&
267 CheckWEPPrefix(passphrase.substr(2), error) &&
268 CheckWEPIsHex(passphrase.substr(4), error);
269 break;
270 default:
271 error->Populate(Error::kInvalidPassphrase);
272 break;
273 }
274
275 // TODO(quiche): may need to normalize passphrase format
276 if (error->IsSuccess()) {
277 return passphrase;
278 } else {
279 return "";
280 }
281}
282
283// static
284string WiFiService::ParseWPAPassphrase(const string &passphrase, Error *error) {
285 unsigned int length = passphrase.length();
286 vector<uint8> passphrase_bytes;
287
288 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
289 if (length != IEEE_80211::kWPAHexLen &&
290 (length < IEEE_80211::kWPAAsciiMinLen ||
291 length > IEEE_80211::kWPAAsciiMaxLen)) {
292 error->Populate(Error::kInvalidPassphrase);
293 }
294 } else {
295 if (length < IEEE_80211::kWPAAsciiMinLen ||
296 length > IEEE_80211::kWPAAsciiMaxLen) {
297 error->Populate(Error::kInvalidPassphrase);
298 }
299 }
300
301 // TODO(quiche): may need to normalize passphrase format
302 if (error->IsSuccess()) {
303 return passphrase;
304 } else {
305 return "";
306 }
307}
308
309// static
310bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
311 vector<uint8> passphrase_bytes;
312 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
313 return true;
314 } else {
315 error->Populate(Error::kInvalidPassphrase);
316 return false;
317 }
318}
319
320// static
321bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
322 if (StartsWithASCII(passphrase, "0:", false) ||
323 StartsWithASCII(passphrase, "1:", false) ||
324 StartsWithASCII(passphrase, "2:", false) ||
325 StartsWithASCII(passphrase, "3:", false)) {
326 return true;
327 } else {
328 error->Populate(Error::kInvalidPassphrase);
329 return false;
330 }
331}
332
333// static
334bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
335 if (StartsWithASCII(passphrase, "0x", false)) {
336 return true;
337 } else {
338 error->Populate(Error::kInvalidPassphrase);
339 return false;
340 }
341}
342
mukesh agrawald835b202011-10-07 15:26:47 -0700343bool WiFiService::SanitizeSSID(string *ssid) {
344 CHECK(ssid);
345
346 size_t ssid_len = ssid->length();
347 size_t i;
348 bool changed = false;
349
350 for (i=0; i < ssid_len; ++i) {
351 if (!g_ascii_isprint((*ssid)[i])) {
352 (*ssid)[i] = '?';
353 changed = true;
354 }
355 }
356
357 return changed;
358}
359
Paul Stewartd08f4432011-11-04 07:48:20 -0700360string WiFiService::GetGenericStorageIdentifier() const {
361 if (security_ == flimflam::kSecurityRsn ||
362 security_ == flimflam::kSecurityWpa) {
363 return GetStorageIdentifierForSecurity(flimflam::kSecurityPsk);
364 } else {
365 return GetStorageIdentifierForSecurity(security_);
366 }
367}
368
369string WiFiService::GetSpecificStorageIdentifier() const {
370 return GetStorageIdentifierForSecurity(security_);
371}
372
373string WiFiService::GetStorageIdentifierForSecurity(
374 const string &security) const {
375 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
376 flimflam::kTypeWifi,
377 wifi_->address().c_str(),
378 hex_ssid_.c_str(),
379 mode_.c_str(),
380 security.c_str()));
381}
382
mukesh agrawalb54601c2011-06-07 17:39:22 -0700383} // namespace shill