mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/wifi.h" |
| 6 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 8 | #include <string.h> |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 9 | #include <netinet/ether.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <linux/if.h> // Needs definitions from netinet/ether.h |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 11 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 12 | #include <algorithm> |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 13 | #include <map> |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 14 | #include <set> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 15 | #include <string> |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 16 | #include <vector> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 17 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 18 | #include <base/bind.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 19 | #include <base/logging.h> |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 20 | #include <base/stringprintf.h> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 21 | #include <base/string_number_conversions.h> |
| 22 | #include <base/string_util.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 23 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 24 | #include <glib.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 25 | |
| 26 | #include "shill/control_interface.h" |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 27 | #include "shill/dbus_adaptor.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 28 | #include "shill/device.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 29 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 30 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 31 | #include "shill/key_value_store.h" |
| 32 | #include "shill/ieee80211.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 33 | #include "shill/manager.h" |
Thieu Le | 67370f6 | 2012-02-14 23:01:42 +0000 | [diff] [blame] | 34 | #include "shill/metrics.h" |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 35 | #include "shill/power_manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 36 | #include "shill/profile.h" |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 37 | #include "shill/property_accessor.h" |
Darin Petkov | d196726 | 2011-07-18 14:55:18 -0700 | [diff] [blame] | 38 | #include "shill/proxy_factory.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 39 | #include "shill/rtnl_handler.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 40 | #include "shill/scope_logger.h" |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 41 | #include "shill/shill_time.h" |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 42 | #include "shill/store_interface.h" |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 43 | #include "shill/supplicant_interface_proxy_interface.h" |
| 44 | #include "shill/supplicant_process_proxy_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 45 | #include "shill/technology.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 46 | #include "shill/wifi_endpoint.h" |
| 47 | #include "shill/wifi_service.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 48 | #include "shill/wpa_supplicant.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 49 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 50 | using base::Bind; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 51 | using base::StringPrintf; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 52 | using std::map; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 53 | using std::set; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 54 | using std::string; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 55 | using std::vector; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 56 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 57 | namespace shill { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 58 | |
| 59 | // statics |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 60 | const char *WiFi::kDefaultBgscanMethod = |
| 61 | wpa_supplicant::kNetworkBgscanMethodSimple; |
| 62 | const uint16 WiFi::kDefaultBgscanShortIntervalSeconds = 30; |
| 63 | const int32 WiFi::kDefaultBgscanSignalThresholdDbm = -50; |
| 64 | const uint16 WiFi::kDefaultScanIntervalSeconds = 180; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 65 | // Note that WiFi generates some manager-level errors, because it implements |
| 66 | // the Manager.GetWiFiService flimflam API. The API is implemented here, |
| 67 | // rather than in manager, to keep WiFi-specific logic in the right place. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 68 | const char WiFi::kManagerErrorSSIDRequired[] = "must specify SSID"; |
| 69 | const char WiFi::kManagerErrorSSIDTooLong[] = "SSID is too long"; |
| 70 | const char WiFi::kManagerErrorSSIDTooShort[] = "SSID is too short"; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 71 | const char WiFi::kManagerErrorUnsupportedSecurityMode[] = |
| 72 | "security mode is unsupported"; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 73 | const char WiFi::kManagerErrorUnsupportedServiceMode[] = |
| 74 | "service mode is unsupported"; |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 75 | // Age (in seconds) beyond which a BSS cache entry will not be preserved, |
| 76 | // across a suspend/resume. |
| 77 | const time_t WiFi::kMaxBSSResumeAgeSeconds = 10; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 78 | const char WiFi::kInterfaceStateUnknown[] = "shill-unknown"; |
mukesh agrawal | f202817 | 2012-03-13 14:20:22 -0700 | [diff] [blame] | 79 | const time_t WiFi::kRescanIntervalSeconds = 1; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 80 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 81 | WiFi::WiFi(ControlInterface *control_interface, |
| 82 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 83 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 84 | Manager *manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 85 | const string& link, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 86 | const string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 87 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 88 | : Device(control_interface, |
| 89 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 90 | metrics, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 91 | manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 92 | link, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 93 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 94 | interface_index, |
| 95 | Technology::kWifi), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 96 | weak_ptr_factory_(this), |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 97 | proxy_factory_(ProxyFactory::GetInstance()), |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 98 | time_(Time::GetInstance()), |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 99 | supplicant_state_(kInterfaceStateUnknown), |
| 100 | supplicant_bss_("(unknown)"), |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 101 | clear_cached_credentials_pending_(false), |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 102 | need_bss_flush_(false), |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 103 | bgscan_method_(kDefaultBgscanMethod), |
| 104 | bgscan_short_interval_seconds_(kDefaultBgscanShortIntervalSeconds), |
| 105 | bgscan_signal_threshold_dbm_(kDefaultBgscanSignalThresholdDbm), |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 106 | scan_pending_(false), |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 107 | scan_interval_seconds_(kDefaultScanIntervalSeconds) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 108 | PropertyStore *store = this->mutable_store(); |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 109 | resumed_at_.tv_sec = 0; |
| 110 | resumed_at_.tv_usec = 0; |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 111 | HelpRegisterDerivedString(store, |
| 112 | flimflam::kBgscanMethodProperty, |
| 113 | &WiFi::GetBgscanMethod, |
| 114 | &WiFi::SetBgscanMethod); |
| 115 | HelpRegisterDerivedUint16(store, |
| 116 | flimflam::kBgscanShortIntervalProperty, |
| 117 | &WiFi::GetBgscanShortInterval, |
| 118 | &WiFi::SetBgscanShortInterval); |
| 119 | HelpRegisterDerivedInt32(store, |
| 120 | flimflam::kBgscanSignalThresholdProperty, |
| 121 | &WiFi::GetBgscanSignalThreshold, |
| 122 | &WiFi::SetBgscanSignalThreshold); |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 123 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 124 | // TODO(quiche): Decide if scan_pending_ is close enough to |
| 125 | // "currently scanning" that we don't care, or if we want to track |
| 126 | // scan pending/currently scanning/no scan scheduled as a tri-state |
| 127 | // kind of thing. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 128 | store->RegisterConstBool(flimflam::kScanningProperty, &scan_pending_); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 129 | HelpRegisterDerivedUint16(store, |
| 130 | flimflam::kScanIntervalProperty, |
| 131 | &WiFi::GetScanInterval, |
| 132 | &WiFi::SetScanInterval); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 133 | SLOG(WiFi, 2) << "WiFi device " << link_name() << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 134 | } |
| 135 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 136 | WiFi::~WiFi() {} |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 137 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 138 | void WiFi::Start(Error *error, const EnabledStateChangedCallback &callback) { |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 139 | ::DBus::Path interface_path; |
| 140 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 141 | supplicant_process_proxy_.reset( |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 142 | proxy_factory_->CreateSupplicantProcessProxy( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 143 | wpa_supplicant::kDBusPath, wpa_supplicant::kDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 144 | try { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 145 | map<string, DBus::Variant> create_interface_args; |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 146 | create_interface_args[wpa_supplicant::kInterfacePropertyName].writer(). |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 147 | append_string(link_name().c_str()); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 148 | create_interface_args[wpa_supplicant::kInterfacePropertyDriver].writer(). |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 149 | append_string(wpa_supplicant::kDriverNL80211); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 150 | create_interface_args[ |
| 151 | wpa_supplicant::kInterfacePropertyConfigFile].writer(). |
| 152 | append_string(SCRIPTDIR "/wpa_supplicant.conf"); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 153 | interface_path = |
| 154 | supplicant_process_proxy_->CreateInterface(create_interface_args); |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 155 | } catch (const DBus::Error &e) { // NOLINT |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 156 | if (!strcmp(e.name(), wpa_supplicant::kErrorInterfaceExists)) { |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 157 | interface_path = |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 158 | supplicant_process_proxy_->GetInterface(link_name()); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 159 | // TODO(quiche): Is it okay to crash here, if device is missing? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 160 | } else { |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 161 | // TODO(quiche): Log error. |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 165 | supplicant_interface_proxy_.reset( |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 166 | proxy_factory_->CreateSupplicantInterfaceProxy( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 167 | this, interface_path, wpa_supplicant::kDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 168 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 169 | RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP, |
| 170 | IFF_UP); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 171 | // TODO(quiche) Set ApScan=1 and BSSExpireAge=190, like flimflam does? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 172 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 173 | // Clear out any networks that might previously have been configured |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 174 | // for this interface. |
| 175 | supplicant_interface_proxy_->RemoveAllNetworks(); |
| 176 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 177 | // Flush interface's BSS cache, so that we get BSSAdded signals for |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 178 | // all BSSes (not just new ones since the last scan). |
| 179 | supplicant_interface_proxy_->FlushBSS(0); |
| 180 | |
mukesh agrawal | 23e9f28 | 2012-02-17 15:36:36 -0800 | [diff] [blame] | 181 | try { |
| 182 | // TODO(pstew): Disable fast_reauth until supplicant can properly deal |
| 183 | // with RADIUS servers that respond strangely to such requests. |
| 184 | // crosbug.com/25630 |
| 185 | supplicant_interface_proxy_->SetFastReauth(false); |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 186 | } catch (const DBus::Error &e) { // NOLINT |
mukesh agrawal | f202817 | 2012-03-13 14:20:22 -0700 | [diff] [blame] | 187 | LOG(INFO) << "Failed to disable fast_reauth." |
| 188 | << "May be running an older version of wpa_supplicant."; |
| 189 | } |
| 190 | |
| 191 | try { |
| 192 | // Helps with passing WiFiRomaing.001SSIDSwitchBack. |
| 193 | supplicant_interface_proxy_->SetScanInterval(kRescanIntervalSeconds); |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 194 | } catch (const DBus::Error &e) { // NOLINT |
mukesh agrawal | f202817 | 2012-03-13 14:20:22 -0700 | [diff] [blame] | 195 | LOG(INFO) << "Failed to set scan_interval. " |
mukesh agrawal | 23e9f28 | 2012-02-17 15:36:36 -0800 | [diff] [blame] | 196 | << "May be running an older version of wpa_supplicant."; |
| 197 | } |
Paul Stewart | 2987dcf | 2012-01-30 15:47:42 -0800 | [diff] [blame] | 198 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 199 | // Register for power state changes. HandlePowerStateChange() will be called |
| 200 | // when the power state changes. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 201 | manager()->power_manager()->AddStateChangeCallback( |
| 202 | UniqueName(), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 203 | Bind(&WiFi::HandlePowerStateChange, weak_ptr_factory_.GetWeakPtr())); |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 204 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 205 | Scan(NULL); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 206 | StartScanTimer(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 207 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 208 | if (error) |
| 209 | error->Reset(); // indicate immediate completion |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 212 | void WiFi::Stop(Error *error, const EnabledStateChangedCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 213 | SLOG(WiFi, 2) << "WiFi " << link_name() << " stopping."; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 214 | // TODO(quiche): Remove interface from supplicant. |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 215 | StopScanTimer(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 216 | supplicant_interface_proxy_.reset(); // breaks a reference cycle |
| 217 | supplicant_process_proxy_.reset(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 218 | endpoint_by_rpcid_.clear(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 219 | rpcid_by_service_.clear(); |
Gary Morain | 9100112 | 2012-02-29 16:22:26 -0800 | [diff] [blame] | 220 | manager()->power_manager()->RemoveStateChangeCallback(UniqueName()); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 221 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 222 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 223 | it != services_.end(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 224 | ++it) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 225 | SLOG(WiFi, 3) << "WiFi " << link_name() << " deregistering service " |
| 226 | << (*it)->friendly_name(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 227 | manager()->DeregisterService(*it); |
| 228 | } |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 229 | services_.clear(); // breaks reference cycles |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 230 | current_service_ = NULL; // breaks a reference cycle |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 231 | pending_service_ = NULL; // breaks a reference cycle |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 232 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 233 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 234 | if (error) |
| 235 | error->Reset(); // indicate immediate completion |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 236 | // TODO(quiche): Anything else to do? |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 237 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 238 | SLOG(WiFi, 3) << "WiFi " << link_name() << " supplicant_process_proxy_ " |
| 239 | << (supplicant_process_proxy_.get() ? |
| 240 | "is set." : "is not set."); |
| 241 | SLOG(WiFi, 3) << "WiFi " << link_name() << " supplicant_interface_proxy_ " |
| 242 | << (supplicant_interface_proxy_.get() ? |
| 243 | "is set." : "is not set."); |
| 244 | SLOG(WiFi, 3) << "WiFi " << link_name() << " pending_service_ " |
| 245 | << (pending_service_.get() ? "is set." : "is not set."); |
| 246 | SLOG(WiFi, 3) << "WiFi " << link_name() << " has " |
| 247 | << endpoint_by_rpcid_.size() << " EndpointMap entries."; |
| 248 | SLOG(WiFi, 3) << "WiFi " << link_name() << " has " << services_.size() |
| 249 | << " Services."; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 252 | bool WiFi::Load(StoreInterface *storage) { |
| 253 | LoadHiddenServices(storage); |
| 254 | return Device::Load(storage); |
| 255 | } |
| 256 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 257 | void WiFi::Scan(Error */*error*/) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 258 | LOG(INFO) << __func__; |
| 259 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 260 | // Needs to send a D-Bus message, but may be called from D-Bus |
| 261 | // signal handler context (via Manager::RequestScan). So defer work |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 262 | // to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 263 | dispatcher()->PostTask(Bind(&WiFi::ScanTask, weak_ptr_factory_.GetWeakPtr())); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 266 | bool WiFi::TechnologyIs(const Technology::Identifier type) const { |
| 267 | return type == Technology::kWifi; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 268 | } |
| 269 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 270 | bool WiFi::IsConnectingTo(const WiFiService &service) const { |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 271 | return pending_service_ == &service || |
| 272 | (current_service_ == &service && |
| 273 | service.state() != Service::kStateConnected); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 276 | void WiFi::BSSAdded(const ::DBus::Path &path, |
| 277 | const map<string, ::DBus::Variant> &properties) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 278 | // Called from a D-Bus signal handler, and may need to send a D-Bus |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 279 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 280 | dispatcher()->PostTask(Bind(&WiFi::BSSAddedTask, |
| 281 | weak_ptr_factory_.GetWeakPtr(), |
| 282 | path, properties)); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void WiFi::BSSRemoved(const ::DBus::Path &path) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 286 | // Called from a D-Bus signal handler, and may need to send a D-Bus |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 287 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 288 | dispatcher()->PostTask(Bind(&WiFi::BSSRemovedTask, |
| 289 | weak_ptr_factory_.GetWeakPtr(), path)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 290 | } |
| 291 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 292 | void WiFi::PropertiesChanged(const map<string, ::DBus::Variant> &properties) { |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 293 | LOG(INFO) << "In " << __func__ << "(): called"; |
| 294 | // Called from D-Bus signal handler, but may need to send a D-Bus |
| 295 | // message. So defer work to event loop. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 296 | dispatcher()->PostTask(Bind(&WiFi::PropertiesChangedTask, |
| 297 | weak_ptr_factory_.GetWeakPtr(), properties)); |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 298 | } |
| 299 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 300 | void WiFi::ScanDone() { |
| 301 | LOG(INFO) << __func__; |
| 302 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 303 | // Defer handling of scan result processing, because that processing |
| 304 | // may require the the registration of new D-Bus objects. And such |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 305 | // registration can't be done in the context of a D-Bus signal |
| 306 | // handler. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 307 | dispatcher()->PostTask(Bind(&WiFi::ScanDoneTask, |
| 308 | weak_ptr_factory_.GetWeakPtr())); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 309 | } |
| 310 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 311 | void WiFi::ConnectTo(WiFiService *service, |
mukesh agrawal | 6489632 | 2011-12-01 01:13:10 +0000 | [diff] [blame] | 312 | map<string, DBus::Variant> service_params) { |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 313 | CHECK(service) << "Can't connect to NULL service."; |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 314 | DBus::Path network_path; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 315 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 316 | // TODO(quiche): Handle cases where already connected. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 317 | if (pending_service_ && pending_service_ == service) { |
| 318 | // TODO(quiche): Return an error to the caller. crosbug.com/23832 |
| 319 | LOG(INFO) << "WiFi " << link_name() << " ignoring ConnectTo " |
| 320 | << service->friendly_name() |
| 321 | << ", which is already pending."; |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | if (pending_service_ && pending_service_ != service) { |
| 326 | DisconnectFrom(pending_service_); |
| 327 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 328 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 329 | try { |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 330 | // TODO(quiche): Set a timeout here. In the normal case, we expect |
| 331 | // that, if wpa_supplicant fails to connect, it will eventually send |
| 332 | // a signal that its CurrentBSS has changed. But there may be cases |
| 333 | // where the signal is not sent. (crosbug.com/23206) |
mukesh agrawal | 6489632 | 2011-12-01 01:13:10 +0000 | [diff] [blame] | 334 | const uint32_t scan_ssid = 1; // "True": Use directed probe. |
| 335 | service_params[wpa_supplicant::kNetworkPropertyScanSSID].writer(). |
| 336 | append_uint32(scan_ssid); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 337 | service_params[wpa_supplicant::kNetworkPropertyBgscan].writer(). |
| 338 | append_string(CreateBgscanConfigString().c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 339 | network_path = |
| 340 | supplicant_interface_proxy_->AddNetwork(service_params); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 341 | rpcid_by_service_[service] = network_path; |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 342 | } catch (const DBus::Error &e) { // NOLINT |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 343 | LOG(ERROR) << "exception while adding network: " << e.what(); |
| 344 | return; |
| 345 | } |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 346 | |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 347 | supplicant_interface_proxy_->SelectNetwork(network_path); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 348 | pending_service_ = service; |
| 349 | CHECK(current_service_.get() != pending_service_.get()); |
| 350 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 351 | // SelectService here (instead of in LinkEvent, like Ethernet), so |
| 352 | // that, if we fail to bring up L2, we can attribute failure correctly. |
| 353 | // |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 354 | // TODO(quiche): When we add code for dealing with connection failures, |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 355 | // reconsider if this is the right place to change the selected service. |
| 356 | // see discussion in crosbug.com/20191. |
| 357 | SelectService(service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 358 | } |
| 359 | |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 360 | void WiFi::DisconnectFrom(WiFiService *service) { |
| 361 | if (service != current_service_ && service != pending_service_) { |
| 362 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 363 | // generate a D-Bus error here. (crosbug.com/23832) |
| 364 | LOG(WARNING) << "In " << __func__ << "(): " |
| 365 | << " ignoring request to disconnect from service " |
| 366 | << service->friendly_name() |
| 367 | << " which is neither current nor pending"; |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | if (pending_service_ && service != pending_service_) { |
| 372 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 373 | // generate a D-Bus error here. (crosbug.com/23832) |
| 374 | LOG(WARNING) << "In " << __func__ << "(): " |
| 375 | << " ignoring request to disconnect from service " |
| 376 | << service->friendly_name() |
| 377 | << " which is not the pending service."; |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | if (!pending_service_ && service != current_service_) { |
| 382 | // TODO(quiche): Once we have asynchronous reply support, we should |
| 383 | // generate a D-Bus error here. (crosbug.com/23832) |
| 384 | LOG(WARNING) << "In " << __func__ << "(): " |
| 385 | << " ignoring request to disconnect from service " |
| 386 | << service->friendly_name() |
| 387 | << " which is not the current service."; |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | pending_service_ = NULL; |
| 392 | try { |
| 393 | supplicant_interface_proxy_->Disconnect(); |
| 394 | // We'll call RemoveNetwork and reset |current_service_| after |
| 395 | // supplicant notifies us that the CurrentBSS has changed. |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 396 | } catch (const DBus::Error &e) { // NOLINT |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 397 | // Can't depend on getting a notification of CurrentBSS change. |
| 398 | // So effect changes immediately. |
| 399 | ReverseServiceMap::const_iterator rpcid_it = |
| 400 | rpcid_by_service_.find(service); |
| 401 | DCHECK(rpcid_it != rpcid_by_service_.end()); |
| 402 | if (rpcid_it == rpcid_by_service_.end()) { |
| 403 | LOG(WARNING) << "WiFi " << link_name() << " can not disconnect from " |
| 404 | << service->friendly_name() << ": " |
| 405 | << "could not find supplicant network to disable."; |
| 406 | } else { |
| 407 | supplicant_interface_proxy_->RemoveNetwork(rpcid_it->second); |
| 408 | } |
| 409 | current_service_ = NULL; |
| 410 | } |
| 411 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 412 | CHECK(current_service_ == NULL || |
| 413 | current_service_.get() != pending_service_.get()); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 414 | } |
| 415 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 416 | bool WiFi::IsIdle() const { |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 417 | return !current_service_ && !pending_service_; |
| 418 | } |
| 419 | |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 420 | void WiFi::ClearCachedCredentials() { |
| 421 | LOG(INFO) << __func__; |
| 422 | |
| 423 | // Needs to send a D-Bus message, but may be called from D-Bus |
| 424 | // caller context (via Manager::PopProfile). So defer work |
| 425 | // to event loop. |
| 426 | if (!clear_cached_credentials_pending_) { |
| 427 | clear_cached_credentials_pending_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 428 | dispatcher()->PostTask(Bind(&WiFi::ClearCachedCredentialsTask, |
| 429 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 433 | void WiFi::NotifyEndpointChanged(const WiFiEndpoint &endpoint) { |
| 434 | WiFiService *service = FindServiceForEndpoint(endpoint); |
| 435 | DCHECK(service); |
| 436 | if (service) { |
| 437 | service->NotifyEndpointUpdated(endpoint); |
| 438 | return; |
| 439 | } |
| 440 | } |
| 441 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 442 | string WiFi::CreateBgscanConfigString() { |
| 443 | return StringPrintf("%s:%d:%d:%d", |
| 444 | bgscan_method_.c_str(), |
| 445 | bgscan_short_interval_seconds_, |
| 446 | bgscan_signal_threshold_dbm_, |
| 447 | scan_interval_seconds_); |
| 448 | } |
| 449 | |
| 450 | void WiFi::SetBgscanMethod(const string &method, Error *error) { |
| 451 | if (method != wpa_supplicant::kNetworkBgscanMethodSimple && |
| 452 | method != wpa_supplicant::kNetworkBgscanMethodLearn) { |
| 453 | const string error_message = |
| 454 | StringPrintf("Unrecognized bgscan method %s", method.c_str()); |
| 455 | LOG(WARNING) << error_message; |
| 456 | error->Populate(Error::kInvalidArguments, error_message); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | bgscan_method_ = method; |
| 461 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 462 | // |current_service_|, because supplicant does not allow for |
| 463 | // reconfiguration without disconnect and reconnect. |
| 464 | } |
| 465 | |
| 466 | void WiFi::SetBgscanShortInterval(const uint16 &seconds, Error */*error*/) { |
| 467 | bgscan_short_interval_seconds_ = seconds; |
| 468 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 469 | // |current_service_|, because supplicant does not allow for |
| 470 | // reconfiguration without disconnect and reconnect. |
| 471 | } |
| 472 | |
| 473 | void WiFi::SetBgscanSignalThreshold(const int32 &dbm, Error */*error*/) { |
| 474 | bgscan_signal_threshold_dbm_ = dbm; |
| 475 | // We do not update kNetworkPropertyBgscan for |pending_service_| or |
| 476 | // |current_service_|, because supplicant does not allow for |
| 477 | // reconfiguration without disconnect and reconnect. |
| 478 | } |
| 479 | |
| 480 | void WiFi::SetScanInterval(const uint16 &seconds, Error */*error*/) { |
| 481 | scan_interval_seconds_ = seconds; |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 482 | if (running()) { |
| 483 | StartScanTimer(); |
| 484 | } |
| 485 | // The scan interval affects both foreground scans (handled by |
| 486 | // |scan_timer_callback_|), and background scans (handled by |
| 487 | // supplicant). However, we do not update |pending_service_| or |
| 488 | // |current_service_|, because supplicant does not allow for |
| 489 | // reconfiguration without disconnect and reconnect. |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 490 | } |
| 491 | |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 492 | // To avoid creating duplicate services, call FindServiceForEndpoint |
| 493 | // before calling this method. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 494 | WiFiServiceRefPtr WiFi::CreateServiceForEndpoint(const WiFiEndpoint &endpoint, |
| 495 | bool hidden_ssid) { |
| 496 | WiFiServiceRefPtr service = |
| 497 | new WiFiService(control_interface(), |
| 498 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 499 | metrics(), |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 500 | manager(), |
| 501 | this, |
| 502 | endpoint.ssid(), |
| 503 | endpoint.network_mode(), |
| 504 | endpoint.security_mode(), |
| 505 | hidden_ssid); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 506 | services_.push_back(service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 507 | return service; |
| 508 | } |
| 509 | |
| 510 | void WiFi::CurrentBSSChanged(const ::DBus::Path &new_bss) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 511 | SLOG(WiFi, 3) << "WiFi " << link_name() << " CurrentBSS " |
| 512 | << supplicant_bss_ << " -> " << new_bss; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 513 | supplicant_bss_ = new_bss; |
| 514 | if (new_bss == wpa_supplicant::kCurrentBSSNull) { |
| 515 | HandleDisconnect(); |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 516 | if (!GetHiddenSSIDList().empty()) { |
| 517 | // Before disconnecting, wpa_supplicant probably scanned for |
| 518 | // APs. So, in the normal case, we defer to the timer for the next scan. |
| 519 | // |
| 520 | // However, in the case of hidden SSIDs, supplicant knows about |
| 521 | // at most one of them. (That would be the hidden SSID we were |
| 522 | // connected to, if applicable.) |
| 523 | // |
| 524 | // So, in this case, we initiate an immediate scan. This scan |
| 525 | // will include the hidden SSIDs we know about (up to the limit of |
| 526 | // kScanMAxSSIDsPerScan). |
| 527 | // |
| 528 | // We may want to reconsider this immediate scan, if/when shill |
| 529 | // takes greater responsibility for scanning (vs. letting |
| 530 | // supplicant handle most of it). |
| 531 | Scan(NULL); |
| 532 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 533 | } else { |
| 534 | HandleRoam(new_bss); |
| 535 | } |
| 536 | |
| 537 | SelectService(current_service_); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 538 | // Invariant check: a Service can either be current, or pending, but |
| 539 | // not both. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 540 | CHECK(current_service_.get() != pending_service_.get() || |
| 541 | current_service_.get() == NULL); |
| 542 | |
| 543 | // TODO(quiche): Update BSSID property on the Service |
| 544 | // (crosbug.com/22377). |
| 545 | } |
| 546 | |
| 547 | void WiFi::HandleDisconnect() { |
| 548 | // Identify the affected service. We expect to get a disconnect |
| 549 | // event when we fall off a Service that we were connected |
| 550 | // to. However, we also allow for the case where we get a disconnect |
| 551 | // event while attempting to connect from a disconnected state. |
| 552 | WiFiService *affected_service = |
| 553 | current_service_.get() ? current_service_.get() : pending_service_.get(); |
| 554 | |
| 555 | current_service_ = NULL; |
| 556 | if (!affected_service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 557 | SLOG(WiFi, 2) << "WiFi " << link_name() |
| 558 | << " disconnected while not connected or connecting"; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 559 | return; |
| 560 | } |
| 561 | |
| 562 | ReverseServiceMap::const_iterator rpcid_it = |
| 563 | rpcid_by_service_.find(affected_service); |
| 564 | if (rpcid_it == rpcid_by_service_.end()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 565 | SLOG(WiFi, 2) << "WiFi " << link_name() << " disconnected from " |
| 566 | << " (or failed to connect to) " |
| 567 | << affected_service->friendly_name() << ", " |
| 568 | << "but could not find supplicant network to disable."; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 569 | } else { |
| 570 | // TODO(quiche): Reconsider giving up immediately. Maybe give |
| 571 | // wpa_supplicant some time to retry, first. |
| 572 | supplicant_interface_proxy_->RemoveNetwork(rpcid_it->second); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 575 | SLOG(WiFi, 2) << "WiFi " << link_name() << " disconnected from " |
| 576 | << " (or failed to connect to) " |
| 577 | << affected_service->friendly_name(); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 578 | if (SuspectCredentials(*affected_service)) { |
| 579 | // If we suspect bad credentials, set failure, to trigger an error |
| 580 | // mole in Chrome. Failure is a transient state, and we'll |
| 581 | // transition out of it immediately (after this if block). |
| 582 | affected_service->SetFailure(Service::kFailureBadCredentials); |
| 583 | LOG(ERROR) << "Connection failure during 4-Way Handshake. Bad passphrase?"; |
| 584 | } |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 585 | affected_service->SetFailureSilent(Service::kFailureUnknown); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 586 | affected_service->NotifyCurrentEndpoint(NULL); |
Thieu Le | 67370f6 | 2012-02-14 23:01:42 +0000 | [diff] [blame] | 587 | metrics()->NotifyServiceDisconnect(affected_service); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 588 | |
| 589 | if (affected_service == pending_service_.get()) { |
| 590 | // The attempt to connect to |pending_service_| failed. Clear |
| 591 | // |pending_service_|, to indicate we're no longer in the middle |
| 592 | // of a connect request. |
| 593 | pending_service_ = NULL; |
| 594 | } else if (pending_service_.get()) { |
| 595 | // We've attributed the disconnection to what was the |
| 596 | // |current_service_|, rather than the |pending_service_|. |
| 597 | // |
| 598 | // If we're wrong about that (i.e. supplicant reported this |
| 599 | // CurrentBSS change after attempting to connect to |
| 600 | // |pending_service_|), we're depending on supplicant to retry |
| 601 | // connecting to |pending_service_|, and delivering another |
| 602 | // CurrentBSS change signal in the future. |
| 603 | // |
| 604 | // Log this fact, to help us debug (in case our assumptions are |
| 605 | // wrong). |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 606 | SLOG(WiFi, 2) << "WiFi " << link_name() << " pending connection to " |
| 607 | << pending_service_->friendly_name() |
| 608 | << " after disconnect"; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | // We use the term "Roam" loosely. In particular, we include the case |
| 613 | // where we "Roam" to a BSS from the disconnected state. |
| 614 | void WiFi::HandleRoam(const ::DBus::Path &new_bss) { |
| 615 | EndpointMap::iterator endpoint_it = endpoint_by_rpcid_.find(new_bss); |
| 616 | if (endpoint_it == endpoint_by_rpcid_.end()) { |
| 617 | LOG(WARNING) << "WiFi " << link_name() << " connected to unknown BSS " |
| 618 | << new_bss; |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | const WiFiEndpoint &endpoint(*endpoint_it->second); |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 623 | WiFiServiceRefPtr service = FindServiceForEndpoint(endpoint); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 624 | if (!service.get()) { |
| 625 | LOG(WARNING) << "WiFi " << link_name() |
| 626 | << " could not find Service for Endpoint " |
| 627 | << endpoint.bssid_string() |
| 628 | << " (service will be unchanged)"; |
| 629 | return; |
| 630 | } |
| 631 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 632 | SLOG(WiFi, 2) << "WiFi " << link_name() |
| 633 | << " roamed to Endpoint " << endpoint.bssid_string() |
| 634 | << " (SSID " << endpoint.ssid_string() << ")"; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 635 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 636 | service->NotifyCurrentEndpoint(&endpoint); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 637 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 638 | if (pending_service_.get() && |
| 639 | service.get() != pending_service_.get()) { |
| 640 | // The Service we've roamed on to is not the one we asked for. |
| 641 | // We assume that this is transient, and that wpa_supplicant |
| 642 | // is trying / will try to connect to |pending_service_|. |
| 643 | // |
| 644 | // If it succeeds, we'll end up back here, but with |service| |
| 645 | // pointing at the same service as |pending_service_|. |
| 646 | // |
| 647 | // If it fails, we'll process things in HandleDisconnect. |
| 648 | // |
| 649 | // So we leave |pending_service_| untouched. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 650 | SLOG(WiFi, 2) << "WiFi " << link_name() |
| 651 | << " new current Endpoint " |
| 652 | << endpoint.bssid_string() |
| 653 | << " is not part of pending service " |
| 654 | << pending_service_->friendly_name(); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 655 | |
| 656 | // Sanity check: if we didn't roam onto |pending_service_|, we |
| 657 | // should still be on |current_service_|. |
| 658 | if (service.get() != current_service_.get()) { |
| 659 | LOG(WARNING) << "WiFi " << link_name() |
| 660 | << " new current Endpoint " |
| 661 | << endpoint.bssid_string() |
| 662 | << " is neither part of pending service " |
| 663 | << pending_service_->friendly_name() |
| 664 | << " nor part of current service " |
| 665 | << (current_service_.get() ? |
| 666 | current_service_->friendly_name() : |
| 667 | "(NULL)"); |
| 668 | // Although we didn't expect to get here, we should keep |
| 669 | // |current_service_| in sync with what supplicant has done. |
| 670 | current_service_ = service; |
| 671 | } |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | if (pending_service_.get()) { |
| 676 | // We assume service.get() == pending_service_.get() here, because |
| 677 | // of the return in the previous if clause. |
| 678 | // |
| 679 | // Boring case: we've connected to the service we asked |
| 680 | // for. Simply update |current_service_| and |pending_service_|. |
| 681 | current_service_ = service; |
| 682 | pending_service_ = NULL; |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | // |pending_service_| was NULL, so we weren't attempting to connect |
| 687 | // to a new Service. Sanity check that we're still on |
| 688 | // |current_service_|. |
| 689 | if (service.get() != current_service_.get()) { |
| 690 | LOG(WARNING) |
| 691 | << "WiFi " << link_name() |
| 692 | << " new current Endpoint " |
| 693 | << endpoint.bssid_string() |
| 694 | << (current_service_.get() ? |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 695 | StringPrintf(" is not part of current service %s", |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 696 | current_service_->friendly_name().c_str()) : |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 697 | " with no current service"); |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 698 | // We didn't expect to be here, but let's cope as well as we |
| 699 | // can. Update |current_service_| to keep it in sync with |
| 700 | // supplicant. |
| 701 | current_service_ = service; |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | // At this point, we know that |pending_service_| was NULL, and that |
| 706 | // we're still on |current_service_|. This is the most boring case |
| 707 | // of all, because there's no state to update here. |
| 708 | return; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 711 | WiFiServiceRefPtr WiFi::FindService(const vector<uint8_t> &ssid, |
| 712 | const string &mode, |
| 713 | const string &security) const { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 714 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 715 | it != services_.end(); |
| 716 | ++it) { |
| 717 | if ((*it)->ssid() == ssid && (*it)->mode() == mode && |
| 718 | (*it)->IsSecurityMatch(security)) { |
| 719 | return *it; |
| 720 | } |
| 721 | } |
| 722 | return NULL; |
| 723 | } |
| 724 | |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 725 | WiFiServiceRefPtr WiFi::FindServiceForEndpoint(const WiFiEndpoint &endpoint) { |
| 726 | return FindService(endpoint.ssid(), |
| 727 | endpoint.network_mode(), |
| 728 | endpoint.security_mode()); |
| 729 | } |
| 730 | |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 731 | ByteArrays WiFi::GetHiddenSSIDList() { |
| 732 | // Create a unique set of hidden SSIDs. |
| 733 | set<ByteArray> hidden_ssids_set; |
| 734 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 735 | it != services_.end(); |
| 736 | ++it) { |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 737 | if ((*it)->hidden_ssid() && (*it)->IsRemembered()) { |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 738 | hidden_ssids_set.insert((*it)->ssid()); |
| 739 | } |
| 740 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 741 | SLOG(WiFi, 2) << "Found " << hidden_ssids_set.size() << " hidden services"; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 742 | ByteArrays hidden_ssids(hidden_ssids_set.begin(), hidden_ssids_set.end()); |
| 743 | if (!hidden_ssids.empty()) { |
| 744 | // TODO(pstew): Devise a better method for time-sharing with SSIDs that do |
| 745 | // not fit in. |
| 746 | if (hidden_ssids.size() >= wpa_supplicant::kScanMaxSSIDsPerScan) { |
| 747 | hidden_ssids.erase( |
| 748 | hidden_ssids.begin() + wpa_supplicant::kScanMaxSSIDsPerScan - 1, |
| 749 | hidden_ssids.end()); |
| 750 | } |
| 751 | // Add Broadcast SSID, signified by an empty ByteArray. If we specify |
| 752 | // SSIDs to wpa_supplicant, we need to explicitly specify the default |
| 753 | // behavior of doing a broadcast probe. |
| 754 | hidden_ssids.push_back(ByteArray()); |
| 755 | } |
| 756 | return hidden_ssids; |
| 757 | } |
| 758 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 759 | bool WiFi::LoadHiddenServices(StoreInterface *storage) { |
| 760 | bool created_hidden_service = false; |
| 761 | set<string> groups = storage->GetGroupsWithKey(flimflam::kWifiHiddenSsid); |
| 762 | for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) { |
| 763 | bool is_hidden = false; |
| 764 | if (!storage->GetBool(*it, flimflam::kWifiHiddenSsid, &is_hidden)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 765 | SLOG(WiFi, 2) << "Storage group " << *it << " returned by " |
| 766 | << "GetGroupsWithKey failed for GetBool(" |
| 767 | << flimflam::kWifiHiddenSsid |
| 768 | << ") -- possible non-bool key"; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 769 | continue; |
| 770 | } |
| 771 | if (!is_hidden) { |
| 772 | continue; |
| 773 | } |
| 774 | string ssid_hex; |
| 775 | vector<uint8_t> ssid_bytes; |
| 776 | if (!storage->GetString(*it, flimflam::kSSIDProperty, &ssid_hex) || |
| 777 | !base::HexStringToBytes(ssid_hex, &ssid_bytes)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 778 | SLOG(WiFi, 2) << "Hidden network is missing/invalid \"" |
| 779 | << flimflam::kSSIDProperty << "\" property"; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 780 | continue; |
| 781 | } |
| 782 | string device_address; |
| 783 | string network_mode; |
| 784 | string security; |
| 785 | // It is gross that we have to do this, but the only place we can |
| 786 | // get information about the service is from its storage name. |
| 787 | if (!WiFiService::ParseStorageIdentifier(*it, &device_address, |
| 788 | &network_mode, &security) || |
| 789 | device_address != address()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 790 | SLOG(WiFi, 2) << "Hidden network has unparsable storage identifier \"" |
| 791 | << *it << "\""; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 792 | continue; |
| 793 | } |
| 794 | if (FindService(ssid_bytes, network_mode, security).get()) { |
| 795 | // If service already exists, we have nothing to do, since the |
| 796 | // service has already loaded its configuration from storage. |
| 797 | // This is guaranteed to happen in both cases where Load() is |
| 798 | // called on a device (via a ConfigureDevice() call on the |
| 799 | // profile): |
| 800 | // - In RegisterDevice() the Device hasn't been started yet, |
| 801 | // so it has no services registered, except for those |
| 802 | // created by previous iterations of LoadHiddenService(). |
| 803 | // The latter can happen if another profile in the Manager's |
| 804 | // stack defines the same ssid/mode/security. Even this |
| 805 | // case is okay, since even if the profiles differ |
| 806 | // materially on configuration and credentials, the "right" |
| 807 | // one will be configured in the course of the |
| 808 | // RegisterService() call below. |
| 809 | // - In PushProfile(), all registered services have been |
| 810 | // introduced to the profile via ConfigureService() prior |
| 811 | // to calling ConfigureDevice() on the profile. |
| 812 | continue; |
| 813 | } |
| 814 | WiFiServiceRefPtr service(new WiFiService(control_interface(), |
| 815 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 816 | metrics(), |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 817 | manager(), |
| 818 | this, |
| 819 | ssid_bytes, |
| 820 | network_mode, |
| 821 | security, |
| 822 | true)); |
| 823 | services_.push_back(service); |
| 824 | |
| 825 | // By registering the service, the rest of the configuration |
| 826 | // will be loaded from the profile into the service via ConfigureService(). |
| 827 | manager()->RegisterService(service); |
| 828 | |
| 829 | created_hidden_service = true; |
| 830 | } |
| 831 | |
| 832 | // If we are idle and we created a service as a result of opening the |
| 833 | // profile, we should initiate a scan for our new hidden SSID. |
| 834 | if (running() && created_hidden_service && |
| 835 | supplicant_state_ == wpa_supplicant::kInterfaceStateInactive) { |
| 836 | Scan(NULL); |
| 837 | } |
| 838 | |
| 839 | return created_hidden_service; |
| 840 | } |
| 841 | |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 842 | void WiFi::ClearCachedCredentialsTask() { |
Paul Stewart | 57f3d9d | 2012-04-11 18:21:41 -0700 | [diff] [blame] | 843 | try { |
| 844 | supplicant_interface_proxy_->ClearCachedCredentials(); |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 845 | } catch (const DBus::Error &e) { // NOLINT |
Paul Stewart | 57f3d9d | 2012-04-11 18:21:41 -0700 | [diff] [blame] | 846 | LOG(WARNING) << "Clear of cached credentials failed."; |
| 847 | } |
Paul Stewart | 66c8600 | 2012-01-30 18:00:52 -0800 | [diff] [blame] | 848 | clear_cached_credentials_pending_ = false; |
| 849 | } |
| 850 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 851 | void WiFi::BSSAddedTask( |
| 852 | const ::DBus::Path &path, |
| 853 | const map<string, ::DBus::Variant> &properties) { |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 854 | // Note: we assume that BSSIDs are unique across endpoints. This |
| 855 | // means that if an AP reuses the same BSSID for multiple SSIDs, we |
| 856 | // lose. |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 857 | WiFiEndpointRefPtr endpoint( |
| 858 | new WiFiEndpoint(proxy_factory_, this, path, properties)); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 859 | LOG(INFO) << "Found endpoint. " |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 860 | << "RPC path: " << path << ", " |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 861 | << "ssid: " << endpoint->ssid_string() << ", " |
| 862 | << "bssid: " << endpoint->bssid_string() << ", " |
| 863 | << "signal: " << endpoint->signal_strength() << ", " |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 864 | << "security: " << endpoint->security_mode() << ", " |
| 865 | << "frequency: " << endpoint->frequency(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 866 | |
| 867 | if (endpoint->ssid_string().empty()) { |
| 868 | // Don't bother trying to find or create a Service for an Endpoint |
| 869 | // without an SSID. We wouldn't be able to connect to it anyway. |
| 870 | return; |
| 871 | } |
| 872 | |
mukesh agrawal | b385761 | 2012-01-18 16:23:29 -0800 | [diff] [blame] | 873 | if (endpoint->ssid()[0] == 0) { |
| 874 | // Assume that an SSID starting with NULL is bogus/misconfigured, |
| 875 | // and filter it out. |
| 876 | return; |
| 877 | } |
| 878 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 879 | WiFiServiceRefPtr service = FindServiceForEndpoint(*endpoint); |
| 880 | if (service) { |
| 881 | LOG(INFO) << "Assigned endpoint " << endpoint->bssid_string() |
| 882 | << " to service " << service->friendly_name() << "."; |
| 883 | service->AddEndpoint(endpoint); |
| 884 | |
| 885 | if (manager()->HasService(service)) { |
| 886 | manager()->UpdateService(service); |
| 887 | } else { |
| 888 | DCHECK_EQ(1, service->NumEndpoints()); // Expect registered by now if >1. |
| 889 | manager()->RegisterService(service); |
| 890 | } |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 891 | } else { |
| 892 | const bool hidden_ssid = false; |
| 893 | service = CreateServiceForEndpoint(*endpoint, hidden_ssid); |
| 894 | LOG(INFO) << "New service " << service->GetRpcIdentifier() |
| 895 | << " (" << service->friendly_name() << ")"; |
| 896 | service->AddEndpoint(endpoint); |
| 897 | manager()->RegisterService(service); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 898 | } |
| 899 | |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 900 | // Do this last, to maintain the invariant that any Endpoint we |
| 901 | // know about has a corresponding Service. |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 902 | // |
| 903 | // TODO(quiche): Write test to verify correct behavior in the case |
| 904 | // where we get multiple BSSAdded events for a single endpoint. |
| 905 | // (Old Endpoint's refcount should fall to zero, and old Endpoint |
| 906 | // should be destroyed.) |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 907 | endpoint_by_rpcid_[path] = endpoint; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 908 | endpoint->Start(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | void WiFi::BSSRemovedTask(const ::DBus::Path &path) { |
| 912 | EndpointMap::iterator i = endpoint_by_rpcid_.find(path); |
| 913 | if (i == endpoint_by_rpcid_.end()) { |
| 914 | LOG(WARNING) << "WiFi " << link_name() |
| 915 | << " could not find BSS " << path |
| 916 | << " to remove."; |
| 917 | return; |
| 918 | } |
| 919 | |
| 920 | WiFiEndpointRefPtr endpoint = i->second; |
| 921 | CHECK(endpoint); |
| 922 | endpoint_by_rpcid_.erase(i); |
| 923 | |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 924 | WiFiServiceRefPtr service = FindServiceForEndpoint(*endpoint); |
mukesh agrawal | e9adda1 | 2012-02-09 18:33:48 -0800 | [diff] [blame] | 925 | CHECK(service) << "Can't find Service for Endpoint " |
| 926 | << path << " " |
| 927 | << "(with BSSID " << endpoint->bssid_string() << ")."; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 928 | SLOG(WiFi, 2) << "Removing Endpoint " << endpoint->bssid_string() |
| 929 | << " from Service " << service->friendly_name(); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 930 | service->RemoveEndpoint(endpoint); |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 931 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 932 | bool disconnect_service = !service->HasEndpoints() && |
| 933 | (service->IsConnecting() || service->IsConnected()); |
| 934 | bool forget_service = |
| 935 | // Forget Services without Endpoints, except that we always keep |
| 936 | // hidden services around. (We need them around to populate the |
| 937 | // hidden SSIDs list.) |
| 938 | !service->HasEndpoints() && !service->hidden_ssid(); |
| 939 | bool deregister_service = |
| 940 | // Only deregister a Service if we're forgetting it. Otherwise, |
| 941 | // Manager can't keep our configuration up-to-date (as Profiles |
| 942 | // change). |
| 943 | forget_service; |
| 944 | |
| 945 | if (disconnect_service) { |
| 946 | DisconnectFrom(service); |
| 947 | } |
| 948 | |
| 949 | if (deregister_service) { |
| 950 | manager()->DeregisterService(service); |
| 951 | } else { |
| 952 | manager()->UpdateService(service); |
| 953 | } |
| 954 | |
| 955 | if (forget_service) { |
| 956 | vector<WiFiServiceRefPtr>::iterator it; |
| 957 | it = std::find(services_.begin(), services_.end(), service); |
| 958 | if (it != services_.end()) { |
| 959 | services_.erase(it); |
| 960 | } |
| 961 | } |
mukesh agrawal | b4bc57d | 2011-12-07 01:07:47 +0000 | [diff] [blame] | 962 | } |
| 963 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 964 | void WiFi::PropertiesChangedTask( |
| 965 | const map<string, ::DBus::Variant> &properties) { |
| 966 | // TODO(quiche): Handle changes in other properties (e.g. signal |
| 967 | // strength). |
| 968 | |
| 969 | // Note that order matters here. In particular, we want to process |
| 970 | // changes in the current BSS before changes in state. This is so |
| 971 | // that we update the state of the correct Endpoint/Service. |
| 972 | |
| 973 | map<string, ::DBus::Variant>::const_iterator properties_it = |
| 974 | properties.find(wpa_supplicant::kInterfacePropertyCurrentBSS); |
| 975 | if (properties_it != properties.end()) { |
| 976 | CurrentBSSChanged(properties_it->second.reader().get_path()); |
| 977 | } |
| 978 | |
| 979 | properties_it = properties.find(wpa_supplicant::kInterfacePropertyState); |
| 980 | if (properties_it != properties.end()) { |
| 981 | StateChanged(properties_it->second.reader().get_string()); |
| 982 | } |
| 983 | } |
| 984 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 985 | void WiFi::ScanDoneTask() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 986 | SLOG(WiFi, 2) << __func__ << " need_bss_flush_ " << need_bss_flush_; |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 987 | if (need_bss_flush_) { |
| 988 | CHECK(supplicant_interface_proxy_ != NULL); |
| 989 | // Compute |max_age| relative to |resumed_at_|, to account for the |
| 990 | // time taken to scan. |
| 991 | struct timeval now; |
| 992 | uint32_t max_age; |
| 993 | time_->GetTimeMonotonic(&now); |
| 994 | max_age = kMaxBSSResumeAgeSeconds + (now.tv_sec - resumed_at_.tv_sec); |
| 995 | supplicant_interface_proxy_->FlushBSS(max_age); |
| 996 | need_bss_flush_ = false; |
| 997 | } |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 998 | scan_pending_ = false; |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 999 | StartScanTimer(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1002 | void WiFi::ScanTask() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1003 | SLOG(WiFi, 2) << "WiFi " << link_name() << " scan requested."; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 1004 | map<string, DBus::Variant> scan_args; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 1005 | scan_args[wpa_supplicant::kPropertyScanType].writer(). |
| 1006 | append_string(wpa_supplicant::kScanTypeActive); |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1007 | |
| 1008 | ByteArrays hidden_ssids = GetHiddenSSIDList(); |
| 1009 | if (!hidden_ssids.empty()) { |
| 1010 | scan_args[wpa_supplicant::kPropertyScanSSIDs] = |
| 1011 | DBusAdaptor::ByteArraysToVariant(hidden_ssids); |
| 1012 | } |
| 1013 | |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 1014 | // TODO(quiche): Indicate scanning in UI. crosbug.com/14887 |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1015 | // FIXME(gauravsh): A scan can fail if, for example, wpa_supplicant |
| 1016 | // was restarted. This is done by a few of the 802.1x tests. |
| 1017 | // crosbug.com/25657 |
| 1018 | try { |
| 1019 | supplicant_interface_proxy_->Scan(scan_args); |
| 1020 | scan_pending_ = true; |
Ben Chan | 80326f3 | 2012-05-04 17:51:32 -0700 | [diff] [blame] | 1021 | } catch (const DBus::Error &e) { // NOLINT |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1022 | LOG(WARNING) << "Scan failed. Attempting to re-connect to the supplicant."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1023 | EnabledStateChangedCallback null_callback; |
| 1024 | Stop(NULL, null_callback); |
| 1025 | Start(NULL, null_callback); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 1026 | } |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1029 | void WiFi::StateChanged(const string &new_state) { |
| 1030 | const string old_state = supplicant_state_; |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1031 | supplicant_state_ = new_state; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1032 | LOG(INFO) << "WiFi " << link_name() << " " << __func__ << " " |
| 1033 | << old_state << " -> " << new_state; |
| 1034 | |
| 1035 | WiFiService *affected_service; |
| 1036 | // Identify the service to which the state change applies. If |
| 1037 | // |pending_service_| is non-NULL, then the state change applies to |
| 1038 | // |pending_service_|. Otherwise, it applies to |current_service_|. |
| 1039 | // |
| 1040 | // This policy is driven by the fact that the |pending_service_| |
| 1041 | // doesn't become the |current_service_| until wpa_supplicant |
| 1042 | // reports a CurrentBSS change to the |pending_service_|. And the |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1043 | // CurrentBSS change won't be reported until the |pending_service_| |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1044 | // reaches the wpa_supplicant::kInterfaceStateCompleted state. |
| 1045 | affected_service = |
| 1046 | pending_service_.get() ? pending_service_.get() : current_service_.get(); |
| 1047 | if (!affected_service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1048 | SLOG(WiFi, 2) << "WiFi " << link_name() << " " << __func__ |
| 1049 | << " with no service"; |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1050 | return; |
| 1051 | } |
| 1052 | |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1053 | if (new_state == wpa_supplicant::kInterfaceStateCompleted && |
| 1054 | !affected_service->IsConnected()) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 1055 | if (AcquireIPConfigWithLeaseName( |
| 1056 | affected_service->GetStorageIdentifier())) { |
| 1057 | LOG(INFO) << link_name() << " is up; started L3 configuration."; |
mukesh agrawal | c01f398 | 2012-01-24 13:48:39 -0800 | [diff] [blame] | 1058 | affected_service->SetState(Service::kStateConfiguring); |
| 1059 | } else { |
| 1060 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 1061 | } |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1062 | } else if (new_state == wpa_supplicant::kInterfaceStateAssociated) { |
| 1063 | affected_service->SetState(Service::kStateAssociating); |
| 1064 | } else if (new_state == wpa_supplicant::kInterfaceStateAuthenticating || |
| 1065 | new_state == wpa_supplicant::kInterfaceStateAssociating || |
| 1066 | new_state == wpa_supplicant::kInterfaceState4WayHandshake || |
| 1067 | new_state == wpa_supplicant::kInterfaceStateGroupHandshake) { |
| 1068 | // Ignore transitions into these states from Completed, to avoid |
| 1069 | // bothering the user when roaming, or re-keying. |
| 1070 | if (old_state != wpa_supplicant::kInterfaceStateCompleted) |
| 1071 | affected_service->SetState(Service::kStateAssociating); |
| 1072 | // TOOD(quiche): On backwards transitions, we should probably set |
| 1073 | // a timeout for getting back into the completed state. At present, |
| 1074 | // we depend on wpa_supplicant eventually reporting that CurrentBSS |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1075 | // has changed. But there may be cases where that signal is not sent. |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 1076 | // (crosbug.com/23207) |
| 1077 | } else { |
| 1078 | // Other transitions do not affect Service state. |
| 1079 | // |
| 1080 | // Note in particular that we ignore a State change into |
| 1081 | // kInterfaceStateDisconnected, in favor of observing the corresponding |
| 1082 | // change in CurrentBSS. |
| 1083 | } |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame^] | 1086 | bool WiFi::SuspectCredentials(const WiFiService &service) const { |
| 1087 | if (!service.IsSecurityMatch(flimflam::kSecurityPsk)) { |
| 1088 | // We can only diagnose credentials for WPA/RSN networks. For |
| 1089 | // others, assume the failure was not credential related. |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | return supplicant_state_ == wpa_supplicant::kInterfaceState4WayHandshake && |
| 1094 | !service.has_ever_connected(); |
| 1095 | } |
| 1096 | |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 1097 | // Used by Manager. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1098 | WiFiServiceRefPtr WiFi::GetService(const KeyValueStore &args, Error *error) { |
Darin Petkov | b65c245 | 2012-02-23 15:17:06 +0100 | [diff] [blame] | 1099 | CHECK_EQ(args.GetString(flimflam::kTypeProperty), flimflam::kTypeWifi); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1100 | |
| 1101 | if (args.ContainsString(flimflam::kModeProperty) && |
| 1102 | args.GetString(flimflam::kModeProperty) != |
| 1103 | flimflam::kModeManaged) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1104 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 1105 | kManagerErrorUnsupportedServiceMode); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1106 | return NULL; |
| 1107 | } |
| 1108 | |
| 1109 | if (!args.ContainsString(flimflam::kSSIDProperty)) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1110 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 1111 | kManagerErrorSSIDRequired); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1112 | return NULL; |
| 1113 | } |
| 1114 | |
| 1115 | string ssid = args.GetString(flimflam::kSSIDProperty); |
| 1116 | if (ssid.length() < 1) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1117 | Error::PopulateAndLog(error, Error::kInvalidNetworkName, |
| 1118 | kManagerErrorSSIDTooShort); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1119 | return NULL; |
| 1120 | } |
| 1121 | |
| 1122 | if (ssid.length() > IEEE_80211::kMaxSSIDLen) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1123 | Error::PopulateAndLog(error, Error::kInvalidNetworkName, |
| 1124 | kManagerErrorSSIDTooLong); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1125 | return NULL; |
| 1126 | } |
| 1127 | |
| 1128 | string security_method; |
| 1129 | if (args.ContainsString(flimflam::kSecurityProperty)) { |
| 1130 | security_method = args.GetString(flimflam::kSecurityProperty); |
| 1131 | } else { |
| 1132 | security_method = flimflam::kSecurityNone; |
| 1133 | } |
| 1134 | |
| 1135 | if (security_method != flimflam::kSecurityNone && |
| 1136 | security_method != flimflam::kSecurityWep && |
| 1137 | security_method != flimflam::kSecurityPsk && |
| 1138 | security_method != flimflam::kSecurityWpa && |
| 1139 | security_method != flimflam::kSecurityRsn && |
| 1140 | security_method != flimflam::kSecurity8021x) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 1141 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 1142 | kManagerErrorUnsupportedSecurityMode); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1143 | return NULL; |
| 1144 | } |
| 1145 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1146 | bool hidden_ssid; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1147 | if (args.ContainsBool(flimflam::kWifiHiddenSsid)) { |
| 1148 | hidden_ssid = args.GetBool(flimflam::kWifiHiddenSsid); |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1149 | } else { |
| 1150 | // If the service is not found, and the caller hasn't specified otherwise, |
| 1151 | // we assume this is is a hidden network. |
| 1152 | hidden_ssid = true; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1155 | vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); |
| 1156 | WiFiServiceRefPtr service(FindService(ssid_bytes, flimflam::kModeManaged, |
| 1157 | security_method)); |
| 1158 | if (!service.get()) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1159 | service = new WiFiService(control_interface(), |
| 1160 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1161 | metrics(), |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1162 | manager(), |
| 1163 | this, |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 1164 | ssid_bytes, |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1165 | flimflam::kModeManaged, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 1166 | security_method, |
| 1167 | hidden_ssid); |
| 1168 | services_.push_back(service); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 1169 | // NB: We do not register the newly created Service with the Manager. |
| 1170 | // The Service will be registered if/when we find Endpoints for it. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
mukesh agrawal | b172e4a | 2012-04-25 18:15:30 -0700 | [diff] [blame] | 1173 | if ((security_method == flimflam::kSecurityWep || |
| 1174 | security_method == flimflam::kSecurityPsk || |
| 1175 | security_method == flimflam::kSecurityWpa || |
| 1176 | security_method == flimflam::kSecurityRsn) && |
| 1177 | args.ContainsString(flimflam::kPassphraseProperty)) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 1178 | service->SetPassphrase(args.GetString(flimflam::kPassphraseProperty), |
| 1179 | error); |
| 1180 | if (error->IsFailure()) { |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 1185 | service->Configure(args, error); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1186 | |
Paul Stewart | 7f61e52 | 2012-03-22 11:13:45 -0700 | [diff] [blame] | 1187 | // TODO(pstew): Schedule a task to forget up all non-hidden services that |
| 1188 | // have no endpoints like the one we may have just created. crosbug.com/28224 |
| 1189 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 1190 | return service; |
| 1191 | } |
| 1192 | |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 1193 | // static |
| 1194 | bool WiFi::SanitizeSSID(string *ssid) { |
| 1195 | CHECK(ssid); |
| 1196 | |
| 1197 | size_t ssid_len = ssid->length(); |
| 1198 | size_t i; |
| 1199 | bool changed = false; |
| 1200 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1201 | for (i = 0; i < ssid_len; ++i) { |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 1202 | if (!g_ascii_isprint((*ssid)[i])) { |
| 1203 | (*ssid)[i] = '?'; |
| 1204 | changed = true; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | return changed; |
| 1209 | } |
| 1210 | |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1211 | void WiFi::HelpRegisterDerivedInt32( |
| 1212 | PropertyStore *store, |
| 1213 | const string &name, |
| 1214 | int32(WiFi::*get)(Error *error), |
| 1215 | void(WiFi::*set)(const int32 &value, Error *error)) { |
| 1216 | store->RegisterDerivedInt32( |
| 1217 | name, |
| 1218 | Int32Accessor(new CustomAccessor<WiFi, int32>(this, get, set))); |
| 1219 | } |
| 1220 | |
| 1221 | void WiFi::HelpRegisterDerivedString( |
| 1222 | PropertyStore *store, |
| 1223 | const string &name, |
| 1224 | string(WiFi::*get)(Error *error), |
| 1225 | void(WiFi::*set)(const string &value, Error *error)) { |
| 1226 | store->RegisterDerivedString( |
| 1227 | name, |
| 1228 | StringAccessor(new CustomAccessor<WiFi, string>(this, get, set))); |
| 1229 | } |
| 1230 | |
| 1231 | void WiFi::HelpRegisterDerivedUint16( |
| 1232 | PropertyStore *store, |
| 1233 | const string &name, |
| 1234 | uint16(WiFi::*get)(Error *error), |
| 1235 | void(WiFi::*set)(const uint16 &value, Error *error)) { |
| 1236 | store->RegisterDerivedUint16( |
| 1237 | name, |
| 1238 | Uint16Accessor(new CustomAccessor<WiFi, uint16>(this, get, set))); |
| 1239 | } |
| 1240 | |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1241 | void WiFi::HandlePowerStateChange(PowerManager::SuspendState new_state) { |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 1242 | LOG(INFO) << __func__; |
| 1243 | if (new_state == PowerManagerProxyDelegate::kOn) { |
| 1244 | // We want to flush the BSS cache, but we don't want to conflict |
| 1245 | // with a running scan or an active connection attempt. So record |
| 1246 | // the need to flush, and take care of flushing when the next scan |
| 1247 | // completes. |
| 1248 | // |
| 1249 | // Note that supplicant will automatically expire old cache |
| 1250 | // entries (after, e.g., a BSS is not found in two consecutive |
| 1251 | // scans). However, our explicit flush accelerates re-association |
| 1252 | // in cases where a BSS disappeared while we were asleep. (See, |
| 1253 | // e.g. WiFiRoaming.005SuspendRoam.) |
| 1254 | time_->GetTimeMonotonic(&resumed_at_); |
| 1255 | need_bss_flush_ = true; |
| 1256 | |
| 1257 | if (!scan_pending_ && IsIdle()) { |
| 1258 | // Not scanning/connecting/connected, so let's get things rolling. |
| 1259 | Scan(NULL); |
| 1260 | } |
Gary Morain | ac1bdb4 | 2012-02-16 17:42:29 -0800 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
mukesh agrawal | b66c646 | 2012-05-07 11:45:25 -0700 | [diff] [blame] | 1264 | void WiFi::StartScanTimer() { |
| 1265 | if (scan_interval_seconds_ == 0) { |
| 1266 | StopScanTimer(); |
| 1267 | return; |
| 1268 | } |
| 1269 | scan_timer_callback_.Reset( |
| 1270 | Bind(&WiFi::ScanTimerHandler, weak_ptr_factory_.GetWeakPtr())); |
| 1271 | dispatcher()->PostDelayedTask( |
| 1272 | scan_timer_callback_.callback(), scan_interval_seconds_ * 1000); |
| 1273 | } |
| 1274 | |
| 1275 | void WiFi::StopScanTimer() { |
| 1276 | scan_timer_callback_.Cancel(); |
| 1277 | } |
| 1278 | |
| 1279 | void WiFi::ScanTimerHandler() { |
| 1280 | SLOG(WiFi, 2) << "WiFi Device " << link_name() << ": " << __func__; |
| 1281 | if (IsIdle() && !scan_pending_) { |
| 1282 | Scan(NULL); |
| 1283 | } |
| 1284 | StartScanTimer(); |
| 1285 | } |
| 1286 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 1287 | } // namespace shill |