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), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 39 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 40 | mode_(mode), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 41 | task_factory_(this), |
| 42 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 43 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 44 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 45 | 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 Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 50 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 51 | 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 55 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame^] | 56 | 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 Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 65 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 66 | // 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 90 | // TODO(quiche): figure out when to set true |
| 91 | hidden_ssid_ = false; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | WiFiService::~WiFiService() { |
| 95 | LOG(INFO) << __func__; |
| 96 | } |
| 97 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 98 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 99 | 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 Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 103 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 104 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 107 | void WiFiService::Disconnect() { |
| 108 | // TODO(quiche) RemoveNetwork from supplicant |
| 109 | // XXX remove from favorite networks list? |
| 110 | } |
| 111 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 112 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 113 | return wifi_->TechnologyIs(type); |
| 114 | } |
| 115 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 116 | string WiFiService::GetStorageIdentifier() { |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 117 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 118 | flimflam::kTypeWifi, |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 119 | wifi_->address().c_str(), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 120 | hex_ssid_.c_str(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 121 | mode_.c_str(), |
| 122 | security_.c_str())); |
| 123 | } |
| 124 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 125 | const string &WiFiService::mode() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 126 | return mode_; |
| 127 | } |
| 128 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 129 | const string &WiFiService::key_management() const { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 130 | return GetEAPKeyManagement(); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | const std::vector<uint8_t> &WiFiService::ssid() const { |
| 134 | return ssid_; |
| 135 | } |
| 136 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 137 | void 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 147 | // private methods |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 148 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 149 | 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 agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 160 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 161 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 162 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 163 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 164 | } 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 187 | string WiFiService::GetDeviceRpcId() { |
| 188 | return wifi_->GetRpcIdentifier(); |
| 189 | } |
| 190 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 191 | // static |
| 192 | string 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 |
| 233 | string 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 |
| 259 | bool 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 |
| 270 | bool 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 |
| 283 | bool 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 agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame^] | 292 | bool 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 309 | } // namespace shill |