blob: c9713ef15859478798e637389e6ef98f498ca89d [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 agrawalb54601c2011-06-07 17:39:22 -070015
16#include "shill/control_interface.h"
17#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070018#include "shill/error.h"
19#include "shill/ieee80211.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070020#include "shill/shill_event.h"
21#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070022#include "shill/wifi_endpoint.h"
23#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070024
25using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070026using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070027
28namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070029
30WiFiService::WiFiService(ControlInterface *control_interface,
31 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070032 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070033 const WiFiRefPtr &device,
mukesh agrawalb54601c2011-06-07 17:39:22 -070034 const std::vector<uint8_t> ssid,
Chris Masone092df3e2011-08-22 09:41:39 -070035 const std::string &mode,
mukesh agrawal6e277772011-09-29 15:04:23 -070036 const std::string &security)
mukesh agrawal7a4e4002011-09-06 11:26:05 -070037 : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi),
mukesh agrawal6e277772011-09-29 15:04:23 -070038 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070039 mode_(mode),
mukesh agrawalb54601c2011-06-07 17:39:22 -070040 task_factory_(this),
41 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070042 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070043 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070044 store->RegisterConstString(flimflam::kModeProperty, &mode_);
45 store->RegisterString(flimflam::kPassphraseProperty, &passphrase_);
46 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
47 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
48 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070049
Paul Stewartac4ac002011-08-26 12:04:26 -070050 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
51 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
52 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
53 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070054 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
55
56 hex_ssid_ = base::HexEncode(&(*ssid_.begin()), ssid_.size());
Chris Masone9d779932011-08-25 16:33:41 -070057 set_name(name() +
58 "_" +
59 string(reinterpret_cast<const char*>(ssid_.data()), ssid_.size()));
60
mukesh agrawal6e277772011-09-29 15:04:23 -070061 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
62 // a service that is not 802.1x.
63 if (security_ == flimflam::kSecurity8021x) {
64 NOTIMPLEMENTED();
65 // XXX needs_passpharse_ = false ?
66 } else if (security_ == flimflam::kSecurityPsk) {
67 SetEAPKeyManagement("WPA-PSK");
68 need_passphrase_ = true;
69 } else if (security_ == flimflam::kSecurityRsn) {
70 SetEAPKeyManagement("WPA-PSK");
71 need_passphrase_ = true;
72 } else if (security_ == flimflam::kSecurityWpa) {
73 SetEAPKeyManagement("WPA-PSK");
74 need_passphrase_ = true;
75 } else if (security_ == flimflam::kSecurityWep) {
76 SetEAPKeyManagement("NONE");
77 need_passphrase_ = true;
78 } else if (security_ == flimflam::kSecurityNone) {
79 SetEAPKeyManagement("NONE");
80 need_passphrase_ = false;
81 } else {
82 LOG(ERROR) << "unsupported security method " << security_;
83 }
84
mukesh agrawal32399322011-09-01 10:53:43 -070085 // TODO(quiche): figure out when to set true
86 hidden_ssid_ = false;
mukesh agrawalb54601c2011-06-07 17:39:22 -070087}
88
89WiFiService::~WiFiService() {
90 LOG(INFO) << __func__;
91}
92
mukesh agrawal1830fa12011-09-26 14:31:40 -070093void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -070094 LOG(INFO) << __func__;
95
96 // NB(quiche) defer handling, since dbus-c++ does not permit us to
97 // send an outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -070098 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -070099 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700100}
101
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700102void WiFiService::Disconnect() {
103 // TODO(quiche) RemoveNetwork from supplicant
104 // XXX remove from favorite networks list?
105}
106
Paul Stewart22aa71b2011-09-16 12:15:11 -0700107bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
108 return wifi_->TechnologyIs(type);
109}
110
Chris Masone9d779932011-08-25 16:33:41 -0700111string WiFiService::GetStorageIdentifier() {
Chris Masone34af2182011-08-22 11:59:36 -0700112 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
mukesh agrawal32399322011-09-01 10:53:43 -0700113 flimflam::kTypeWifi,
Chris Masone9d779932011-08-25 16:33:41 -0700114 wifi_->address().c_str(),
mukesh agrawal32399322011-09-01 10:53:43 -0700115 hex_ssid_.c_str(),
Chris Masone34af2182011-08-22 11:59:36 -0700116 mode_.c_str(),
117 security_.c_str()));
118}
119
Chris Masone092df3e2011-08-22 09:41:39 -0700120const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700121 return mode_;
122}
123
Chris Masone092df3e2011-08-22 09:41:39 -0700124const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700125 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700126}
127
128const std::vector<uint8_t> &WiFiService::ssid() const {
129 return ssid_;
130}
131
mukesh agrawal1a056262011-10-05 14:36:54 -0700132void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
133 if (security_ == flimflam::kSecurityWep) {
134 passphrase_ = ParseWEPPassphrase(passphrase, error);
135 } else if (security_ == flimflam::kSecurityPsk ||
136 security_ == flimflam::kSecurityWpa ||
137 security_ == flimflam::kSecurityRsn) {
138 passphrase_ = ParseWPAPassphrase(passphrase, error);
139 }
140}
141
mukesh agrawal32399322011-09-01 10:53:43 -0700142// private methods
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700143void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700144 std::map<string, DBus::Variant> params;
145 DBus::MessageIter writer;
146
147 params[wpa_supplicant::kNetworkPropertyMode].writer().
148 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
149
150 if (security_ == flimflam::kSecurity8021x) {
151 NOTIMPLEMENTED();
152 } else if (security_ == flimflam::kSecurityPsk) {
153 NOTIMPLEMENTED();
154 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700155 params[wpa_supplicant::kPropertySecurityProtocol].writer().
156 append_string(wpa_supplicant::kSecurityModeRSN);
157 params[wpa_supplicant::kPropertyPreSharedKey].writer().
158 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700159 } else if (security_ == flimflam::kSecurityWpa) {
160 params[wpa_supplicant::kPropertySecurityProtocol].writer().
161 append_string(wpa_supplicant::kSecurityModeWPA);
162 params[wpa_supplicant::kPropertyPreSharedKey].writer().
163 append_string(passphrase_.c_str());
164 } else if (security_ == flimflam::kSecurityWep) {
165 NOTIMPLEMENTED();
166 } else if (security_ == flimflam::kSecurityNone) {
167 // nothing special to do here
168 } else {
169 LOG(ERROR) << "can't connect. unsupported security method " << security_;
170 }
171
172 params[wpa_supplicant::kPropertyKeyManagement].writer().
173 append_string(key_management().c_str());
174 // TODO(quiche): figure out why we can't use operator<< without the
175 // temporary variable.
176 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
177 writer << ssid_;
178
179 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700180}
181
Chris Masone95207da2011-06-29 16:50:49 -0700182string WiFiService::GetDeviceRpcId() {
183 return wifi_->GetRpcIdentifier();
184}
185
mukesh agrawal1a056262011-10-05 14:36:54 -0700186// static
187string WiFiService::ParseWEPPassphrase(const string &passphrase, Error *error) {
188 unsigned int length = passphrase.length();
189
190 switch (length) {
191 case IEEE_80211::kWEP40AsciiLen:
192 case IEEE_80211::kWEP104AsciiLen:
193 break;
194 case IEEE_80211::kWEP40AsciiLen + 2:
195 case IEEE_80211::kWEP104AsciiLen + 2:
196 CheckWEPKeyIndex(passphrase, error);
197 break;
198 case IEEE_80211::kWEP40HexLen:
199 case IEEE_80211::kWEP104HexLen:
200 CheckWEPIsHex(passphrase, error);
201 break;
202 case IEEE_80211::kWEP40HexLen + 2:
203 case IEEE_80211::kWEP104HexLen + 2:
204 (CheckWEPKeyIndex(passphrase, error) ||
205 CheckWEPPrefix(passphrase, error)) &&
206 CheckWEPIsHex(passphrase.substr(2), error);
207 break;
208 case IEEE_80211::kWEP40HexLen + 4:
209 case IEEE_80211::kWEP104HexLen + 4:
210 CheckWEPKeyIndex(passphrase, error) &&
211 CheckWEPPrefix(passphrase.substr(2), error) &&
212 CheckWEPIsHex(passphrase.substr(4), error);
213 break;
214 default:
215 error->Populate(Error::kInvalidPassphrase);
216 break;
217 }
218
219 // TODO(quiche): may need to normalize passphrase format
220 if (error->IsSuccess()) {
221 return passphrase;
222 } else {
223 return "";
224 }
225}
226
227// static
228string WiFiService::ParseWPAPassphrase(const string &passphrase, Error *error) {
229 unsigned int length = passphrase.length();
230 vector<uint8> passphrase_bytes;
231
232 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
233 if (length != IEEE_80211::kWPAHexLen &&
234 (length < IEEE_80211::kWPAAsciiMinLen ||
235 length > IEEE_80211::kWPAAsciiMaxLen)) {
236 error->Populate(Error::kInvalidPassphrase);
237 }
238 } else {
239 if (length < IEEE_80211::kWPAAsciiMinLen ||
240 length > IEEE_80211::kWPAAsciiMaxLen) {
241 error->Populate(Error::kInvalidPassphrase);
242 }
243 }
244
245 // TODO(quiche): may need to normalize passphrase format
246 if (error->IsSuccess()) {
247 return passphrase;
248 } else {
249 return "";
250 }
251}
252
253// static
254bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
255 vector<uint8> passphrase_bytes;
256 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
257 return true;
258 } else {
259 error->Populate(Error::kInvalidPassphrase);
260 return false;
261 }
262}
263
264// static
265bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
266 if (StartsWithASCII(passphrase, "0:", false) ||
267 StartsWithASCII(passphrase, "1:", false) ||
268 StartsWithASCII(passphrase, "2:", false) ||
269 StartsWithASCII(passphrase, "3:", false)) {
270 return true;
271 } else {
272 error->Populate(Error::kInvalidPassphrase);
273 return false;
274 }
275}
276
277// static
278bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
279 if (StartsWithASCII(passphrase, "0x", false)) {
280 return true;
281 } else {
282 error->Populate(Error::kInvalidPassphrase);
283 return false;
284 }
285}
286
mukesh agrawalb54601c2011-06-07 17:39:22 -0700287} // namespace shill