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