mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1 | // 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 Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
| 11 | #include <base/string_number_conversions.h> |
| 12 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 14 | #include <dbus/dbus.h> |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 15 | #include <glib.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 16 | |
| 17 | #include "shill/control_interface.h" |
| 18 | #include "shill/device.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 19 | #include "shill/error.h" |
| 20 | #include "shill/ieee80211.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 21 | #include "shill/shill_event.h" |
| 22 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 23 | #include "shill/wifi_endpoint.h" |
| 24 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 25 | |
| 26 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 27 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 28 | |
| 29 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 30 | |
| 31 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 32 | EventDispatcher *dispatcher, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 33 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 34 | const WiFiRefPtr &device, |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 35 | const std::vector<uint8_t> ssid, |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 36 | const std::string &mode, |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 37 | const std::string &security) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 38 | : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame^] | 39 | need_passphrase_(false), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 40 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 41 | mode_(mode), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame^] | 42 | hidden_ssid_(false), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 43 | task_factory_(this), |
| 44 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 45 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 46 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 47 | 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 Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 52 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 53 | 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 57 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 58 | 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 Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 67 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 68 | // 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 92 | // TODO(quiche): figure out when to set true |
| 93 | hidden_ssid_ = false; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | WiFiService::~WiFiService() { |
| 97 | LOG(INFO) << __func__; |
| 98 | } |
| 99 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 100 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 101 | 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 Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 105 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 106 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 109 | void WiFiService::Disconnect() { |
| 110 | // TODO(quiche) RemoveNetwork from supplicant |
| 111 | // XXX remove from favorite networks list? |
| 112 | } |
| 113 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 114 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 115 | return wifi_->TechnologyIs(type); |
| 116 | } |
| 117 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 118 | string WiFiService::GetStorageIdentifier() { |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 119 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 120 | flimflam::kTypeWifi, |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 121 | wifi_->address().c_str(), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 122 | hex_ssid_.c_str(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 123 | mode_.c_str(), |
| 124 | security_.c_str())); |
| 125 | } |
| 126 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 127 | const string &WiFiService::mode() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 128 | return mode_; |
| 129 | } |
| 130 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 131 | const string &WiFiService::key_management() const { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 132 | return GetEAPKeyManagement(); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | const std::vector<uint8_t> &WiFiService::ssid() const { |
| 136 | return ssid_; |
| 137 | } |
| 138 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 139 | void 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 149 | // private methods |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 150 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 151 | 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 agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 162 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 163 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 164 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 165 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 166 | } 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 189 | string WiFiService::GetDeviceRpcId() { |
| 190 | return wifi_->GetRpcIdentifier(); |
| 191 | } |
| 192 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 193 | // static |
| 194 | string 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 |
| 235 | string 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 |
| 261 | bool 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 |
| 272 | bool 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 |
| 285 | bool 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 agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 294 | bool 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 311 | } // namespace shill |