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> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 12 | #include <base/string_split.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 13 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 15 | #include <dbus/dbus.h> |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 16 | #include <glib.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/control_interface.h" |
| 19 | #include "shill/device.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 20 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 21 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 22 | #include "shill/ieee80211.h" |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame^] | 23 | #include "shill/metrics.h" |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 24 | #include "shill/property_accessor.h" |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 25 | #include "shill/store_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 26 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 27 | #include "shill/wifi_endpoint.h" |
| 28 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 29 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 30 | using std::set; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 31 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 32 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 33 | |
| 34 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 35 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 36 | const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID"; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 37 | const char WiFiService::kStorageMode[] = "WiFi.Mode"; |
| 38 | const char WiFiService::kStoragePassphrase[] = "Passphrase"; |
| 39 | const char WiFiService::kStorageSecurity[] = "WiFi.Security"; |
| 40 | const char WiFiService::kStorageSSID[] = "SSID"; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 41 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 42 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 43 | EventDispatcher *dispatcher, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 44 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 45 | const WiFiRefPtr &device, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 46 | const vector<uint8_t> &ssid, |
| 47 | const string &mode, |
| 48 | const string &security, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 49 | bool hidden_ssid) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 50 | : Service(control_interface, dispatcher, manager, Technology::kWifi), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame] | 51 | need_passphrase_(false), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 52 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 53 | mode_(mode), |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 54 | hidden_ssid_(hidden_ssid), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 55 | task_factory_(this), |
| 56 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 57 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 58 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 59 | store->RegisterConstString(flimflam::kModeProperty, &mode_); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 60 | HelpRegisterDerivedString(store, |
| 61 | flimflam::kPassphraseProperty, |
| 62 | NULL, |
| 63 | &WiFiService::SetPassphrase); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 64 | store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_); |
| 65 | store->RegisterConstString(flimflam::kSecurityProperty, &security_); |
| 66 | store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 67 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 68 | store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_); |
| 69 | store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_); |
| 70 | store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_); |
| 71 | store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 72 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 73 | hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size()); |
| 74 | string ssid_string( |
| 75 | reinterpret_cast<const char *>(ssid_.data()), ssid_.size()); |
| 76 | if (SanitizeSSID(&ssid_string)) { |
| 77 | // WifiHexSsid property should only be present if Name property |
| 78 | // has been munged. |
| 79 | store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_); |
| 80 | } |
| 81 | set_friendly_name(ssid_string); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 82 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 83 | // TODO(quiche): determine if it is okay to set EAP.KeyManagement for |
| 84 | // a service that is not 802.1x. |
| 85 | if (security_ == flimflam::kSecurity8021x) { |
| 86 | NOTIMPLEMENTED(); |
| 87 | // XXX needs_passpharse_ = false ? |
| 88 | } else if (security_ == flimflam::kSecurityPsk) { |
| 89 | SetEAPKeyManagement("WPA-PSK"); |
| 90 | need_passphrase_ = true; |
| 91 | } else if (security_ == flimflam::kSecurityRsn) { |
| 92 | SetEAPKeyManagement("WPA-PSK"); |
| 93 | need_passphrase_ = true; |
| 94 | } else if (security_ == flimflam::kSecurityWpa) { |
| 95 | SetEAPKeyManagement("WPA-PSK"); |
| 96 | need_passphrase_ = true; |
| 97 | } else if (security_ == flimflam::kSecurityWep) { |
| 98 | SetEAPKeyManagement("NONE"); |
| 99 | need_passphrase_ = true; |
| 100 | } else if (security_ == flimflam::kSecurityNone) { |
| 101 | SetEAPKeyManagement("NONE"); |
| 102 | need_passphrase_ = false; |
| 103 | } else { |
| 104 | LOG(ERROR) << "unsupported security method " << security_; |
| 105 | } |
| 106 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 107 | // Until we know better (at Profile load time), use the generic name. |
| 108 | storage_identifier_ = GetGenericStorageIdentifier(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 109 | UpdateConnectable(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | WiFiService::~WiFiService() { |
| 113 | LOG(INFO) << __func__; |
| 114 | } |
| 115 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 116 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 117 | LOG(INFO) << __func__; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 118 | // Defer handling, since dbus-c++ does not permit us to send an |
| 119 | // outbound request while processing an inbound one. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 120 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 121 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 122 | } |
| 123 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 124 | void WiFiService::Disconnect(Error */*error*/) { |
| 125 | LOG(INFO) << __func__; |
| 126 | // Defer handling, since dbus-c++ does not permit us to send an |
| 127 | // outbound request while processing an inbound one. |
| 128 | dispatcher()->PostTask( |
| 129 | task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 132 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 133 | return wifi_->TechnologyIs(type); |
| 134 | } |
| 135 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 136 | bool WiFiService::IsAutoConnectable() { |
| 137 | // TODO(quiche): Need to also handle the case where there might be |
| 138 | // another service that has posted a Connect task. crosbug.com/24276 |
| 139 | return connectable() && wifi_->IsIdle(); |
| 140 | } |
| 141 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 142 | void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) { |
| 143 | DCHECK(endpoint->ssid() == ssid()); |
| 144 | endpoints_.insert(endpoint); |
| 145 | } |
| 146 | |
| 147 | void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) { |
| 148 | set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint); |
| 149 | DCHECK(i != endpoints_.end()); |
| 150 | if (i == endpoints_.end()) { |
| 151 | LOG(WARNING) << "In " << __func__ << "(): " |
| 152 | << "ignorning non-existent endpoint " |
| 153 | << endpoint->bssid_string(); |
| 154 | return; |
| 155 | } |
| 156 | endpoints_.erase(i); |
| 157 | } |
| 158 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 159 | string WiFiService::GetStorageIdentifier() const { |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 160 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 161 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 162 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 163 | void WiFiService::SetPassphrase(const string &passphrase, Error *error) { |
| 164 | if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 165 | ValidateWEPPassphrase(passphrase, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 166 | } else if (security_ == flimflam::kSecurityPsk || |
| 167 | security_ == flimflam::kSecurityWpa || |
| 168 | security_ == flimflam::kSecurityRsn) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 169 | ValidateWPAPassphrase(passphrase, error); |
| 170 | } else { |
| 171 | error->Populate(Error::kNotSupported); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 172 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 173 | |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 174 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 175 | passphrase_ = passphrase; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 176 | need_passphrase_ = false; |
| 177 | } |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 178 | |
| 179 | UpdateConnectable(); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 182 | bool WiFiService::IsLoadableFrom(StoreInterface *storage) const { |
| 183 | return storage->ContainsGroup(GetGenericStorageIdentifier()) || |
| 184 | storage->ContainsGroup(GetSpecificStorageIdentifier()); |
| 185 | } |
| 186 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 187 | bool WiFiService::IsVisible() const { |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 188 | const bool is_visible_in_scan = !endpoints_.empty(); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 189 | |
| 190 | // WiFi Services should be displayed only if they are in range (have |
| 191 | // endpoints that have shown up in a scan) or if the service is actively |
| 192 | // being connected. |
| 193 | return is_visible_in_scan || IsConnected() || IsConnecting(); |
| 194 | } |
| 195 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 196 | bool WiFiService::Load(StoreInterface *storage) { |
| 197 | // First find out which storage identifier is available in priority order |
| 198 | // of specific, generic. |
| 199 | string id = GetSpecificStorageIdentifier(); |
| 200 | if (!storage->ContainsGroup(id)) { |
| 201 | id = GetGenericStorageIdentifier(); |
| 202 | if (!storage->ContainsGroup(id)) { |
| 203 | LOG(WARNING) << "Service is not available in the persistent store: " |
| 204 | << id; |
| 205 | return false; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Set our storage identifier to match the storage name in the Profile. |
| 210 | storage_identifier_ = id; |
| 211 | |
| 212 | // Load properties common to all Services. |
| 213 | if (!Service::Load(storage)) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | // Load properties specific to WiFi services. |
| 218 | storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 219 | |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 220 | // NB: mode, security and ssid parameters are never read in from |
| 221 | // Load() as they are provided from the scan. |
| 222 | |
| 223 | string passphrase; |
| 224 | if (storage->GetCryptedString(id, kStoragePassphrase, &passphrase)) { |
| 225 | Error error; |
| 226 | SetPassphrase(passphrase, &error); |
| 227 | if (!error.IsSuccess()) { |
| 228 | LOG(ERROR) << "Passphrase could not be set: " |
| 229 | << Error::GetName(error.type()); |
| 230 | } |
| 231 | } |
| 232 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool WiFiService::Save(StoreInterface *storage) { |
| 237 | // Save properties common to all Services. |
| 238 | if (!Service::Save(storage)) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // Save properties specific to WiFi services. |
| 243 | const string id = GetStorageIdentifier(); |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 244 | storage->SetBool(id, kStorageHiddenSSID, hidden_ssid_); |
| 245 | storage->SetString(id, kStorageMode, mode_); |
| 246 | storage->SetCryptedString(id, kStoragePassphrase, passphrase_); |
| 247 | storage->SetString(id, kStorageSecurity, security_); |
| 248 | storage->SetString(id, kStorageSSID, hex_ssid_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 249 | |
| 250 | // TODO(quiche): Save Passphrase property. (crosbug.com/23467) |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 254 | bool WiFiService::IsSecurityMatch(const string &security) const { |
| 255 | return GetSecurityClass(security) == GetSecurityClass(security_); |
| 256 | } |
| 257 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame^] | 258 | void WiFiService::InitializeCustomMetrics() const { |
| 259 | string histogram = metrics()->GetFullMetricName( |
| 260 | Metrics::kMetricTimeToJoinMilliseconds, |
| 261 | technology()); |
| 262 | metrics()->AddServiceStateTransitionTimer(this, |
| 263 | histogram, |
| 264 | Service::kStateAssociating, |
| 265 | Service::kStateConfiguring); |
| 266 | } |
| 267 | |
| 268 | void WiFiService::SendPostReadyStateMetrics() const { |
| 269 | // TODO(thieule): Send physical mode and security metrics. |
| 270 | // crosbug.com/24441 |
| 271 | metrics()->SendEnumToUMA( |
| 272 | metrics()->GetFullMetricName(Metrics::kMetricNetworkChannel, |
| 273 | technology()), |
| 274 | Metrics::WiFiFrequencyToChannel(frequency_), |
| 275 | Metrics::kMetricNetworkChannelMax); |
| 276 | } |
| 277 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 278 | // private methods |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 279 | void WiFiService::HelpRegisterDerivedString( |
| 280 | PropertyStore *store, |
| 281 | const std::string &name, |
| 282 | std::string(WiFiService::*get)(Error *), |
| 283 | void(WiFiService::*set)(const std::string&, Error *)) { |
| 284 | store->RegisterDerivedString( |
| 285 | name, |
| 286 | StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set))); |
| 287 | } |
| 288 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 289 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 290 | std::map<string, DBus::Variant> params; |
| 291 | DBus::MessageIter writer; |
| 292 | |
| 293 | params[wpa_supplicant::kNetworkPropertyMode].writer(). |
| 294 | append_uint32(WiFiEndpoint::ModeStringToUint(mode_)); |
| 295 | |
| 296 | if (security_ == flimflam::kSecurity8021x) { |
| 297 | NOTIMPLEMENTED(); |
| 298 | } else if (security_ == flimflam::kSecurityPsk) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 299 | const string psk_proto = StringPrintf("%s %s", |
| 300 | wpa_supplicant::kSecurityModeWPA, |
| 301 | wpa_supplicant::kSecurityModeRSN); |
| 302 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 303 | append_string(psk_proto.c_str()); |
| 304 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 305 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 306 | } else if (security_ == flimflam::kSecurityRsn) { |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 307 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 308 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 309 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 310 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 311 | } else if (security_ == flimflam::kSecurityWpa) { |
| 312 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 313 | append_string(wpa_supplicant::kSecurityModeWPA); |
| 314 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 315 | append_string(passphrase_.c_str()); |
| 316 | } else if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 317 | params[wpa_supplicant::kPropertyAuthAlg].writer(). |
| 318 | append_string(wpa_supplicant::kSecurityAuthAlg); |
| 319 | Error error; |
| 320 | int key_index; |
| 321 | std::vector<uint8> password_bytes; |
| 322 | ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error); |
| 323 | writer = params[wpa_supplicant::kPropertyWEPKey + |
| 324 | base::IntToString(key_index)].writer(); |
| 325 | writer << password_bytes; |
| 326 | params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer(). |
| 327 | append_uint32(key_index); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 328 | } else if (security_ == flimflam::kSecurityNone) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 329 | // Nothing special to do here. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 330 | } else { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 331 | LOG(ERROR) << "Can't connect. Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | params[wpa_supplicant::kPropertyKeyManagement].writer(). |
| 335 | append_string(key_management().c_str()); |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 336 | |
| 337 | // See note in dbus_adaptor.cc on why we need to use a local. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 338 | writer = params[wpa_supplicant::kNetworkPropertySSID].writer(); |
| 339 | writer << ssid_; |
| 340 | |
| 341 | wifi_->ConnectTo(this, params); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 342 | } |
| 343 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 344 | void WiFiService::DisconnectTask() { |
| 345 | wifi_->DisconnectFrom(this); |
| 346 | } |
| 347 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 348 | string WiFiService::GetDeviceRpcId(Error */*error*/) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 349 | return wifi_->GetRpcIdentifier(); |
| 350 | } |
| 351 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 352 | void WiFiService::UpdateConnectable() { |
| 353 | if (security_ == flimflam::kSecurityNone) { |
| 354 | DCHECK(passphrase_.empty()); |
| 355 | set_connectable(true); |
| 356 | } else if (security_ == flimflam::kSecurityWep || |
| 357 | security_ == flimflam::kSecurityWpa || |
| 358 | security_ == flimflam::kSecurityPsk || |
| 359 | security_ == flimflam::kSecurityRsn) { |
| 360 | set_connectable(!passphrase_.empty()); |
| 361 | } else { |
| 362 | // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466) |
| 363 | set_connectable(false); |
| 364 | } |
| 365 | } |
| 366 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 367 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 368 | void WiFiService::ValidateWEPPassphrase(const std::string &passphrase, |
| 369 | Error *error) { |
| 370 | ParseWEPPassphrase(passphrase, NULL, NULL, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 374 | void WiFiService::ValidateWPAPassphrase(const std::string &passphrase, |
| 375 | Error *error) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 376 | unsigned int length = passphrase.length(); |
| 377 | vector<uint8> passphrase_bytes; |
| 378 | |
| 379 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 380 | if (length != IEEE_80211::kWPAHexLen && |
| 381 | (length < IEEE_80211::kWPAAsciiMinLen || |
| 382 | length > IEEE_80211::kWPAAsciiMaxLen)) { |
| 383 | error->Populate(Error::kInvalidPassphrase); |
| 384 | } |
| 385 | } else { |
| 386 | if (length < IEEE_80211::kWPAAsciiMinLen || |
| 387 | length > IEEE_80211::kWPAAsciiMaxLen) { |
| 388 | error->Populate(Error::kInvalidPassphrase); |
| 389 | } |
| 390 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 391 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 392 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 393 | // static |
| 394 | void WiFiService::ParseWEPPassphrase(const string &passphrase, |
| 395 | int *key_index, |
| 396 | std::vector<uint8> *password_bytes, |
| 397 | Error *error) { |
| 398 | unsigned int length = passphrase.length(); |
| 399 | int key_index_local; |
| 400 | std::string password_text; |
| 401 | bool is_hex = false; |
| 402 | |
| 403 | switch (length) { |
| 404 | case IEEE_80211::kWEP40AsciiLen: |
| 405 | case IEEE_80211::kWEP104AsciiLen: |
| 406 | key_index_local = 0; |
| 407 | password_text = passphrase; |
| 408 | break; |
| 409 | case IEEE_80211::kWEP40AsciiLen + 2: |
| 410 | case IEEE_80211::kWEP104AsciiLen + 2: |
| 411 | if (CheckWEPKeyIndex(passphrase, error)) { |
| 412 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 413 | password_text = passphrase.substr(2); |
| 414 | } |
| 415 | break; |
| 416 | case IEEE_80211::kWEP40HexLen: |
| 417 | case IEEE_80211::kWEP104HexLen: |
| 418 | if (CheckWEPIsHex(passphrase, error)) { |
| 419 | key_index_local = 0; |
| 420 | password_text = passphrase; |
| 421 | is_hex = true; |
| 422 | } |
| 423 | break; |
| 424 | case IEEE_80211::kWEP40HexLen + 2: |
| 425 | case IEEE_80211::kWEP104HexLen + 2: |
| 426 | if(CheckWEPKeyIndex(passphrase, error) && |
| 427 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 428 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 429 | password_text = passphrase.substr(2); |
| 430 | is_hex = true; |
| 431 | } else if (CheckWEPPrefix(passphrase, error) && |
| 432 | CheckWEPIsHex(passphrase.substr(2), error)) { |
| 433 | key_index_local = 0; |
| 434 | password_text = passphrase.substr(2); |
| 435 | is_hex = true; |
| 436 | } |
| 437 | break; |
| 438 | case IEEE_80211::kWEP40HexLen + 4: |
| 439 | case IEEE_80211::kWEP104HexLen + 4: |
| 440 | if (CheckWEPKeyIndex(passphrase, error) && |
| 441 | CheckWEPPrefix(passphrase.substr(2), error) && |
| 442 | CheckWEPIsHex(passphrase.substr(4), error)) { |
| 443 | base::StringToInt(passphrase.substr(0,1), &key_index_local); |
| 444 | password_text = passphrase.substr(4); |
| 445 | is_hex = true; |
| 446 | } |
| 447 | break; |
| 448 | default: |
| 449 | error->Populate(Error::kInvalidPassphrase); |
| 450 | break; |
| 451 | } |
| 452 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 453 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 454 | if (key_index) |
| 455 | *key_index = key_index_local; |
| 456 | if (password_bytes) { |
| 457 | if (is_hex) |
| 458 | base::HexStringToBytes(password_text, password_bytes); |
| 459 | else |
| 460 | password_bytes->insert(password_bytes->end(), |
| 461 | password_text.begin(), |
| 462 | password_text.end()); |
| 463 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | // static |
| 468 | bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) { |
| 469 | vector<uint8> passphrase_bytes; |
| 470 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 471 | return true; |
| 472 | } else { |
| 473 | error->Populate(Error::kInvalidPassphrase); |
| 474 | return false; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // static |
| 479 | bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) { |
| 480 | if (StartsWithASCII(passphrase, "0:", false) || |
| 481 | StartsWithASCII(passphrase, "1:", false) || |
| 482 | StartsWithASCII(passphrase, "2:", false) || |
| 483 | StartsWithASCII(passphrase, "3:", false)) { |
| 484 | return true; |
| 485 | } else { |
| 486 | error->Populate(Error::kInvalidPassphrase); |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // static |
| 492 | bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) { |
| 493 | if (StartsWithASCII(passphrase, "0x", false)) { |
| 494 | return true; |
| 495 | } else { |
| 496 | error->Populate(Error::kInvalidPassphrase); |
| 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 501 | // static |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 502 | bool WiFiService::SanitizeSSID(string *ssid) { |
| 503 | CHECK(ssid); |
| 504 | |
| 505 | size_t ssid_len = ssid->length(); |
| 506 | size_t i; |
| 507 | bool changed = false; |
| 508 | |
| 509 | for (i=0; i < ssid_len; ++i) { |
| 510 | if (!g_ascii_isprint((*ssid)[i])) { |
| 511 | (*ssid)[i] = '?'; |
| 512 | changed = true; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return changed; |
| 517 | } |
| 518 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 519 | // static |
| 520 | string WiFiService::GetSecurityClass(const string &security) { |
| 521 | if (security == flimflam::kSecurityRsn || |
| 522 | security == flimflam::kSecurityWpa) { |
| 523 | return flimflam::kSecurityPsk; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 524 | } else { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 525 | return security; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 529 | // static |
| 530 | bool WiFiService::ParseStorageIdentifier(const string &storage_name, |
| 531 | string *address, |
| 532 | string *mode, |
| 533 | string *security) { |
| 534 | vector<string> wifi_parts; |
| 535 | base::SplitString(storage_name, '_', &wifi_parts); |
| 536 | if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) { |
| 537 | return false; |
| 538 | } |
| 539 | *address = wifi_parts[1]; |
| 540 | *mode = wifi_parts[3]; |
| 541 | *security = wifi_parts[4]; |
| 542 | return true; |
| 543 | } |
| 544 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 545 | string WiFiService::GetGenericStorageIdentifier() const { |
| 546 | return GetStorageIdentifierForSecurity(GetSecurityClass(security_)); |
| 547 | } |
| 548 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 549 | string WiFiService::GetSpecificStorageIdentifier() const { |
| 550 | return GetStorageIdentifierForSecurity(security_); |
| 551 | } |
| 552 | |
| 553 | string WiFiService::GetStorageIdentifierForSecurity( |
| 554 | const string &security) const { |
| 555 | return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s", |
| 556 | flimflam::kTypeWifi, |
| 557 | wifi_->address().c_str(), |
| 558 | hex_ssid_.c_str(), |
| 559 | mode_.c_str(), |
| 560 | security.c_str())); |
| 561 | } |
| 562 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 563 | } // namespace shill |