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 | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 15 | |
| 16 | #include "shill/control_interface.h" |
| 17 | #include "shill/device.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame^] | 18 | #include "shill/error.h" |
| 19 | #include "shill/ieee80211.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 20 | #include "shill/shill_event.h" |
| 21 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 22 | #include "shill/wifi_endpoint.h" |
| 23 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 24 | |
| 25 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame^] | 26 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 27 | |
| 28 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 29 | |
| 30 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 31 | EventDispatcher *dispatcher, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 32 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 33 | const WiFiRefPtr &device, |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 34 | const std::vector<uint8_t> ssid, |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 35 | const std::string &mode, |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 36 | const std::string &security) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 37 | : Service(control_interface, dispatcher, manager, flimflam::kTypeWifi), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 38 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 39 | mode_(mode), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 40 | task_factory_(this), |
| 41 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 42 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 43 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 44 | 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 Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 49 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 50 | 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 54 | store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_); |
| 55 | |
| 56 | hex_ssid_ = base::HexEncode(&(*ssid_.begin()), ssid_.size()); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 57 | set_name(name() + |
| 58 | "_" + |
| 59 | string(reinterpret_cast<const char*>(ssid_.data()), ssid_.size())); |
| 60 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 61 | // 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 85 | // TODO(quiche): figure out when to set true |
| 86 | hidden_ssid_ = false; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | WiFiService::~WiFiService() { |
| 90 | LOG(INFO) << __func__; |
| 91 | } |
| 92 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 93 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 94 | 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 Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 98 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 99 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 102 | void WiFiService::Disconnect() { |
| 103 | // TODO(quiche) RemoveNetwork from supplicant |
| 104 | // XXX remove from favorite networks list? |
| 105 | } |
| 106 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 107 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 108 | return wifi_->TechnologyIs(type); |
| 109 | } |
| 110 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 111 | string WiFiService::GetStorageIdentifier() { |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 112 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 113 | flimflam::kTypeWifi, |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 114 | wifi_->address().c_str(), |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 115 | hex_ssid_.c_str(), |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 116 | mode_.c_str(), |
| 117 | security_.c_str())); |
| 118 | } |
| 119 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 120 | const string &WiFiService::mode() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 121 | return mode_; |
| 122 | } |
| 123 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 124 | const string &WiFiService::key_management() const { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 125 | return GetEAPKeyManagement(); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | const std::vector<uint8_t> &WiFiService::ssid() const { |
| 129 | return ssid_; |
| 130 | } |
| 131 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame^] | 132 | void 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 142 | // private methods |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 143 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 144 | 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 agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 155 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 156 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 157 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 158 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 159 | } 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 182 | string WiFiService::GetDeviceRpcId() { |
| 183 | return wifi_->GetRpcIdentifier(); |
| 184 | } |
| 185 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame^] | 186 | // static |
| 187 | string 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 |
| 228 | string 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 |
| 254 | bool 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 |
| 265 | bool 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 |
| 278 | bool 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 287 | } // namespace shill |