blob: cd4cfe60ef1c1dad1c42450ee57add19106d579b [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"
20#include "shill/ieee80211.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070021#include "shill/shill_event.h"
22#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070023#include "shill/wifi_endpoint.h"
24#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070025
26using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070027using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070028
29namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070030
31WiFiService::WiFiService(ControlInterface *control_interface,
32 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070033 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070034 const WiFiRefPtr &device,
mukesh agrawalb54601c2011-06-07 17:39:22 -070035 const std::vector<uint8_t> ssid,
Chris Masone092df3e2011-08-22 09:41:39 -070036 const std::string &mode,
mukesh agrawal6e277772011-09-29 15:04:23 -070037 const std::string &security)
mukesh agrawal7a4e4002011-09-06 11:26:05 -070038 : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi),
mukesh agrawal6e277772011-09-29 15:04:23 -070039 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070040 mode_(mode),
mukesh agrawalb54601c2011-06-07 17:39:22 -070041 task_factory_(this),
42 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070043 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070044 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070045 store->RegisterConstString(flimflam::kModeProperty, &mode_);
46 store->RegisterString(flimflam::kPassphraseProperty, &passphrase_);
47 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
48 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
49 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070050
Paul Stewartac4ac002011-08-26 12:04:26 -070051 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
52 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
53 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
54 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070055
mukesh agrawald835b202011-10-07 15:26:47 -070056 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
57 string ssid_string(
58 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
59 if (SanitizeSSID(&ssid_string)) {
60 // WifiHexSsid property should only be present if Name property
61 // has been munged.
62 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
63 }
64 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070065
mukesh agrawal6e277772011-09-29 15:04:23 -070066 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
67 // a service that is not 802.1x.
68 if (security_ == flimflam::kSecurity8021x) {
69 NOTIMPLEMENTED();
70 // XXX needs_passpharse_ = false ?
71 } else if (security_ == flimflam::kSecurityPsk) {
72 SetEAPKeyManagement("WPA-PSK");
73 need_passphrase_ = true;
74 } else if (security_ == flimflam::kSecurityRsn) {
75 SetEAPKeyManagement("WPA-PSK");
76 need_passphrase_ = true;
77 } else if (security_ == flimflam::kSecurityWpa) {
78 SetEAPKeyManagement("WPA-PSK");
79 need_passphrase_ = true;
80 } else if (security_ == flimflam::kSecurityWep) {
81 SetEAPKeyManagement("NONE");
82 need_passphrase_ = true;
83 } else if (security_ == flimflam::kSecurityNone) {
84 SetEAPKeyManagement("NONE");
85 need_passphrase_ = false;
86 } else {
87 LOG(ERROR) << "unsupported security method " << security_;
88 }
89
mukesh agrawal32399322011-09-01 10:53:43 -070090 // TODO(quiche): figure out when to set true
91 hidden_ssid_ = false;
mukesh agrawalb54601c2011-06-07 17:39:22 -070092}
93
94WiFiService::~WiFiService() {
95 LOG(INFO) << __func__;
96}
97
mukesh agrawal1830fa12011-09-26 14:31:40 -070098void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -070099 LOG(INFO) << __func__;
100
101 // NB(quiche) defer handling, since dbus-c++ does not permit us to
102 // send an outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700103 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700104 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700105}
106
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700107void WiFiService::Disconnect() {
108 // TODO(quiche) RemoveNetwork from supplicant
109 // XXX remove from favorite networks list?
110}
111
Paul Stewart22aa71b2011-09-16 12:15:11 -0700112bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
113 return wifi_->TechnologyIs(type);
114}
115
Chris Masone9d779932011-08-25 16:33:41 -0700116string WiFiService::GetStorageIdentifier() {
Chris Masone34af2182011-08-22 11:59:36 -0700117 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
mukesh agrawal32399322011-09-01 10:53:43 -0700118 flimflam::kTypeWifi,
Chris Masone9d779932011-08-25 16:33:41 -0700119 wifi_->address().c_str(),
mukesh agrawal32399322011-09-01 10:53:43 -0700120 hex_ssid_.c_str(),
Chris Masone34af2182011-08-22 11:59:36 -0700121 mode_.c_str(),
122 security_.c_str()));
123}
124
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
mukesh agrawal32399322011-09-01 10:53:43 -0700147// private methods
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700148void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700149 std::map<string, DBus::Variant> params;
150 DBus::MessageIter writer;
151
152 params[wpa_supplicant::kNetworkPropertyMode].writer().
153 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
154
155 if (security_ == flimflam::kSecurity8021x) {
156 NOTIMPLEMENTED();
157 } else if (security_ == flimflam::kSecurityPsk) {
158 NOTIMPLEMENTED();
159 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700160 params[wpa_supplicant::kPropertySecurityProtocol].writer().
161 append_string(wpa_supplicant::kSecurityModeRSN);
162 params[wpa_supplicant::kPropertyPreSharedKey].writer().
163 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700164 } else if (security_ == flimflam::kSecurityWpa) {
165 params[wpa_supplicant::kPropertySecurityProtocol].writer().
166 append_string(wpa_supplicant::kSecurityModeWPA);
167 params[wpa_supplicant::kPropertyPreSharedKey].writer().
168 append_string(passphrase_.c_str());
169 } else if (security_ == flimflam::kSecurityWep) {
170 NOTIMPLEMENTED();
171 } else if (security_ == flimflam::kSecurityNone) {
172 // nothing special to do here
173 } else {
174 LOG(ERROR) << "can't connect. unsupported security method " << security_;
175 }
176
177 params[wpa_supplicant::kPropertyKeyManagement].writer().
178 append_string(key_management().c_str());
179 // TODO(quiche): figure out why we can't use operator<< without the
180 // temporary variable.
181 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
182 writer << ssid_;
183
184 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700185}
186
Chris Masone95207da2011-06-29 16:50:49 -0700187string WiFiService::GetDeviceRpcId() {
188 return wifi_->GetRpcIdentifier();
189}
190
mukesh agrawal1a056262011-10-05 14:36:54 -0700191// static
192string WiFiService::ParseWEPPassphrase(const string &passphrase, Error *error) {
193 unsigned int length = passphrase.length();
194
195 switch (length) {
196 case IEEE_80211::kWEP40AsciiLen:
197 case IEEE_80211::kWEP104AsciiLen:
198 break;
199 case IEEE_80211::kWEP40AsciiLen + 2:
200 case IEEE_80211::kWEP104AsciiLen + 2:
201 CheckWEPKeyIndex(passphrase, error);
202 break;
203 case IEEE_80211::kWEP40HexLen:
204 case IEEE_80211::kWEP104HexLen:
205 CheckWEPIsHex(passphrase, error);
206 break;
207 case IEEE_80211::kWEP40HexLen + 2:
208 case IEEE_80211::kWEP104HexLen + 2:
209 (CheckWEPKeyIndex(passphrase, error) ||
210 CheckWEPPrefix(passphrase, error)) &&
211 CheckWEPIsHex(passphrase.substr(2), error);
212 break;
213 case IEEE_80211::kWEP40HexLen + 4:
214 case IEEE_80211::kWEP104HexLen + 4:
215 CheckWEPKeyIndex(passphrase, error) &&
216 CheckWEPPrefix(passphrase.substr(2), error) &&
217 CheckWEPIsHex(passphrase.substr(4), error);
218 break;
219 default:
220 error->Populate(Error::kInvalidPassphrase);
221 break;
222 }
223
224 // TODO(quiche): may need to normalize passphrase format
225 if (error->IsSuccess()) {
226 return passphrase;
227 } else {
228 return "";
229 }
230}
231
232// static
233string WiFiService::ParseWPAPassphrase(const string &passphrase, Error *error) {
234 unsigned int length = passphrase.length();
235 vector<uint8> passphrase_bytes;
236
237 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
238 if (length != IEEE_80211::kWPAHexLen &&
239 (length < IEEE_80211::kWPAAsciiMinLen ||
240 length > IEEE_80211::kWPAAsciiMaxLen)) {
241 error->Populate(Error::kInvalidPassphrase);
242 }
243 } else {
244 if (length < IEEE_80211::kWPAAsciiMinLen ||
245 length > IEEE_80211::kWPAAsciiMaxLen) {
246 error->Populate(Error::kInvalidPassphrase);
247 }
248 }
249
250 // TODO(quiche): may need to normalize passphrase format
251 if (error->IsSuccess()) {
252 return passphrase;
253 } else {
254 return "";
255 }
256}
257
258// static
259bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
260 vector<uint8> passphrase_bytes;
261 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
262 return true;
263 } else {
264 error->Populate(Error::kInvalidPassphrase);
265 return false;
266 }
267}
268
269// static
270bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
271 if (StartsWithASCII(passphrase, "0:", false) ||
272 StartsWithASCII(passphrase, "1:", false) ||
273 StartsWithASCII(passphrase, "2:", false) ||
274 StartsWithASCII(passphrase, "3:", false)) {
275 return true;
276 } else {
277 error->Populate(Error::kInvalidPassphrase);
278 return false;
279 }
280}
281
282// static
283bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
284 if (StartsWithASCII(passphrase, "0x", false)) {
285 return true;
286 } else {
287 error->Populate(Error::kInvalidPassphrase);
288 return false;
289 }
290}
291
mukesh agrawald835b202011-10-07 15:26:47 -0700292bool WiFiService::SanitizeSSID(string *ssid) {
293 CHECK(ssid);
294
295 size_t ssid_len = ssid->length();
296 size_t i;
297 bool changed = false;
298
299 for (i=0; i < ssid_len; ++i) {
300 if (!g_ascii_isprint((*ssid)[i])) {
301 (*ssid)[i] = '?';
302 changed = true;
303 }
304 }
305
306 return changed;
307}
308
mukesh agrawalb54601c2011-06-07 17:39:22 -0700309} // namespace shill