blob: 402be961b167e49584b5d983a6248598c2105050 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartb50f0b92011-05-16 16:31:42 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_WIFI_
6#define SHILL_WIFI_
7
Gary Morain22601da2012-03-16 10:48:39 -07008// A WiFi device represents a wireless network interface implemented as an IEEE
9// 802.11 station. An Access Point (AP) (or, more correctly, a Basic Service
10// Set(BSS)) is represented by a WiFiEndpoint. An AP provides a WiFiService,
11// which is the same concept as Extended Service Set (ESS) in 802.11,
12// identified by an SSID. A WiFiService includes zero or more WiFiEndpoints
13// that provide that service.
14//
15// A WiFi device interacts with a real device through WPA Supplicant.
16// Wifi::Start() creates a connection to WPA Supplicant, represented by
17// |supplicant_interface_proxy_|. [1]
18//
19// A WiFi device becomes aware of WiFiEndpoints through BSSAdded signals from
20// WPA Supplicant, which identifies them by a "path". The WiFi object maintains
21// an EndpointMap in |endpoint_by_rpcid_|, in which the key is the "path" and
22// the value is a pointer to a WiFiEndpoint object. When a WiFiEndpoint is
23// added, it is associated with a WiFiService.
24//
25// A WiFi device becomes aware of a WiFiService in three different ways. 1)
26// When a WiFiEndpoint is added through the BSSAdded signal, the WiFiEndpoint is
27// providing a service, and if that service is unknown to the WiFi device, it is
28// added at that point. 2) The Manager can add a WiFiService by calling
29// WiFi::GetService(). 3) Services are loaded from the profile through a call
30// to WiFi::Load().
31//
32// The WiFi device connects to a WiFiService, not a WiFiEndpoint, through WPA
33// Supplicant. It is the job of WPA Supplicant to select a BSS (aka
34// WiFiEndpoint) to connect to. The protocol for establishing a connection is
35// as follows:
36//
37// 1. The WiFi device sends AddNetwork to WPA Supplicant, which returns a
38// "network path" when done.
39//
40// 2. The WiFi device sends SelectNetwork, indicating the network path
41// received in 1, to WPA Supplicant, which begins the process of associating
42// with an AP in the ESS. At this point the WiFiService which is being
43// connected is called the |pending_service_|.
44//
Paul Stewartbc6e7392012-05-24 07:07:48 -070045// 3. During association to an EAP-TLS network, WPA Supplicant can send
46// multiple "Certification" events, which provide information about the
47// identity of the remote entity.
48//
49// 4. When association is complete, WPA Supplicant sends a PropertiesChanged
50// signal to the WiFi device, indicating a change in the CurrentBSS. The
51// WiFiService indicated by the new value of CurrentBSS is set as the
52// |current_service_|, and |pending_service_| is (normally) cleared.
Gary Morain22601da2012-03-16 10:48:39 -070053//
54// Some key things to notice are 1) WPA Supplicant does the work of selecting
55// the AP (aka WiFiEndpoint) and it tells the WiFi device which AP it selected.
56// 2) The process of connecting is asynchronous. There is a |current_service_|
57// to which the WiFi device is presently using and a |pending_service_| to which
58// the WiFi device has initiated a connection.
59//
60// A WiFi device is notified that an AP has gone away via the BSSRemoved signal.
61// When the last WiFiEndpoint of a WiFiService is removed, the WiFiService
62// itself is deleted.
63//
64// TODO(gmorain): Add explanation of hidden SSIDs.
65//
66// WPA Supplicant's PropertiesChanged signal communicates changes in the state
67// of WPA Supplicant's current service. This state is stored in
68// |supplicant_state_| and reflects WPA Supplicant's view of the state of the
69// connection to an AP. Changes in this state sometimes cause state changes in
70// the WiFiService to which a WiFi device is connected. For example, when WPA
71// Supplicant signals the new state to be "completed", then the WiFiService
72// state gets changed to "configuring". State change notifications are not
73// reliable because WPA Supplicant may coalesce state changes in quick
74// succession so that only the last of the changes is signaled.
75//
76// Notes:
77//
78// 1. Shill's definition of the interface is described in
79// shill/dbus_bindings/supplicant-interface.xml, and the WPA Supplicant's
80// description of the same interface is in
81// third_party/wpa_supplicant/doc/dbus.doxygen.
82
mukesh agrawal5c05b292012-03-07 10:12:52 -080083#include <time.h>
84
mukesh agrawalab87ea42011-05-18 11:44:49 -070085#include <map>
Chris Masone46eaaf52011-05-24 13:08:30 -070086#include <string>
mukesh agrawalab87ea42011-05-18 11:44:49 -070087#include <vector>
Chris Masone46eaaf52011-05-24 13:08:30 -070088
Eric Shienbrood9a245532012-03-07 14:20:39 -050089#include <base/callback_forward.h>
mukesh agrawalb66c6462012-05-07 11:45:25 -070090#include <base/cancelable_callback.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050091#include <base/memory/weak_ptr.h>
mukesh agrawalaf571952011-07-14 14:31:12 -070092#include <dbus-c++/dbus.h>
Paul Stewart6ab23a92011-11-09 17:17:47 -080093#include <gtest/gtest_prod.h> // for FRIEND_TEST
mukesh agrawalaf571952011-07-14 14:31:12 -070094
Paul Stewartb50f0b92011-05-16 16:31:42 -070095#include "shill/device.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070096#include "shill/event_dispatcher.h"
Gary Morainac1bdb42012-02-16 17:42:29 -080097#include "shill/power_manager.h"
Chris Masone2b105542011-06-22 10:58:09 -070098#include "shill/refptr_types.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070099
100namespace shill {
101
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700102class Error;
103class KeyValueStore;
Darin Petkovab565bb2011-10-06 02:55:51 -0700104class ProxyFactory;
mukesh agrawalaf571952011-07-14 14:31:12 -0700105class SupplicantInterfaceProxyInterface;
106class SupplicantProcessProxyInterface;
mukesh agrawal445e72c2011-06-22 11:13:50 -0700107class WiFiService;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700108
mukesh agrawalab87ea42011-05-18 11:44:49 -0700109// WiFi class. Specialization of Device for WiFi.
Paul Stewartb50f0b92011-05-16 16:31:42 -0700110class WiFi : public Device {
111 public:
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700112 WiFi(ControlInterface *control_interface,
113 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800114 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700115 Manager *manager,
Chris Masone626719f2011-08-18 16:58:48 -0700116 const std::string &link,
117 const std::string &address,
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700118 int interface_index);
mukesh agrawalab87ea42011-05-18 11:44:49 -0700119 virtual ~WiFi();
Darin Petkovc0865312011-09-16 15:31:20 -0700120
Eric Shienbrood9a245532012-03-07 14:20:39 -0500121 virtual void Start(Error *error, const EnabledStateChangedCallback &callback);
122 virtual void Stop(Error *error, const EnabledStateChangedCallback &callback);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800123 virtual bool Load(StoreInterface *storage);
Darin Petkovc0865312011-09-16 15:31:20 -0700124 virtual void Scan(Error *error);
Paul Stewartfdd16072011-09-16 12:41:35 -0700125 virtual bool TechnologyIs(const Technology::Identifier type) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000126 virtual bool IsConnectingTo(const WiFiService &service) const;
mukesh agrawalab87ea42011-05-18 11:44:49 -0700127
mukesh agrawal15908392011-11-16 18:29:25 +0000128 // Called by SupplicantInterfaceProxy, in response to events from
mukesh agrawalab87ea42011-05-18 11:44:49 -0700129 // wpa_supplicant.
130 void BSSAdded(const ::DBus::Path &BSS,
Chris Masonea82b7112011-05-25 15:16:29 -0700131 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawal261daca2011-12-02 18:56:56 +0000132 void BSSRemoved(const ::DBus::Path &BSS);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700133 void Certification(const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000134 void PropertiesChanged(
135 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawalab87ea42011-05-18 11:44:49 -0700136 void ScanDone();
137
mukesh agrawal15908392011-11-16 18:29:25 +0000138 // Called by WiFiService.
mukesh agrawal6e277772011-09-29 15:04:23 -0700139 virtual void ConnectTo(
140 WiFiService *service,
mukesh agrawal64896322011-12-01 01:13:10 +0000141 std::map<std::string, ::DBus::Variant> service_params);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000142 virtual void DisconnectFrom(WiFiService *service);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000143 virtual bool IsIdle() const;
Paul Stewart66c86002012-01-30 18:00:52 -0800144 virtual void ClearCachedCredentials();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700145
mukesh agrawalb20776f2012-02-10 16:00:36 -0800146 // Called by WiFiEndpoint.
147 virtual void NotifyEndpointChanged(const WiFiEndpoint &endpoint);
148
mukesh agrawal15908392011-11-16 18:29:25 +0000149 // Called by Manager.
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700150 virtual WiFiServiceRefPtr GetService(const KeyValueStore &args, Error *error);
151
mukesh agrawal16bc1b82012-02-09 18:38:26 -0800152 // Utility, used by WiFiService and WiFiEndpoint.
153 // Replace non-ASCII characters with '?'. Return true if one or more
154 // characters were changed.
155 static bool SanitizeSSID(std::string *ssid);
156
Paul Stewartb50f0b92011-05-16 16:31:42 -0700157 private:
Paul Stewarte369ece2012-05-22 09:11:03 -0700158 friend class WiFiObjectTest; // access to supplicant_*_proxy_, link_up_
159 friend class WiFiScanTest; // kNumFastScanAttempts, kFastScanIntervalSeconds
160 FRIEND_TEST(WiFiMainTest, FlushBSSOnResume); // kMaxBSSResumeAgeSeconds
mukesh agrawal7ec71312011-11-10 02:08:26 +0000161 FRIEND_TEST(WiFiMainTest, InitialSupplicantState); // kInterfaceStateUnknown
Thieu Lee41a72d2012-02-06 20:46:51 +0000162 FRIEND_TEST(WiFiMainTest, ScanResults); // EndpointMap
mukesh agrawal165e6142011-11-22 02:22:56 +0000163 FRIEND_TEST(WiFiMainTest, ScanResultsWithUpdates); // EndpointMap
Paul Stewarte369ece2012-05-22 09:11:03 -0700164 FRIEND_TEST(WiFiMainTest, SuspectCredentialsOpen); // SuspectCredentials
165 FRIEND_TEST(WiFiMainTest, SuspectCredentialsWPANeverConnected); // as above
166 FRIEND_TEST(WiFiMainTest, SuspectCredentialsWPAPreviouslyConnected); // ""
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800167 FRIEND_TEST(WiFiPropertyTest, ClearDerivedProperty); // bgscan_method_
Paul Stewarte369ece2012-05-22 09:11:03 -0700168 FRIEND_TEST(WiFiScanTest, FastRescan); // kFastScanIntervalSeconds
Paul Stewart6ab23a92011-11-09 17:17:47 -0800169
mukesh agrawalb54601c2011-06-07 17:39:22 -0700170 typedef std::map<const std::string, WiFiEndpointRefPtr> EndpointMap;
mukesh agrawal15908392011-11-16 18:29:25 +0000171 typedef std::map<WiFiService *, std::string> ReverseServiceMap;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700172
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800173 static const char *kDefaultBgscanMethod;
174 static const uint16 kDefaultBgscanShortIntervalSeconds;
175 static const int32 kDefaultBgscanSignalThresholdDbm;
176 static const uint16 kDefaultScanIntervalSeconds;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700177 static const char kManagerErrorSSIDTooLong[];
178 static const char kManagerErrorSSIDTooShort[];
179 static const char kManagerErrorSSIDRequired[];
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700180 static const char kManagerErrorUnsupportedSecurityMode[];
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700181 static const char kManagerErrorUnsupportedServiceMode[];
mukesh agrawal5c05b292012-03-07 10:12:52 -0800182 static const time_t kMaxBSSResumeAgeSeconds;
mukesh agrawal7ec71312011-11-10 02:08:26 +0000183 static const char kInterfaceStateUnknown[];
mukesh agrawalf2028172012-03-13 14:20:22 -0700184 // Delay between scans when supplicant finds "No suitable network".
185 static const time_t kRescanIntervalSeconds;
Paul Stewarte369ece2012-05-22 09:11:03 -0700186 // Number of times to quickly attempt a scan after startup / disconnect.
187 static const int kNumFastScanAttempts;
188 static const int kFastScanIntervalSeconds;
mukesh agrawalab87ea42011-05-18 11:44:49 -0700189
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800190 std::string CreateBgscanConfigString();
191 std::string GetBgscanMethod(Error */* error */) { return bgscan_method_; }
192 uint16 GetBgscanShortInterval(Error */* error */) {
193 return bgscan_short_interval_seconds_;
194 }
195 int32 GetBgscanSignalThreshold(Error */* error */) {
196 return bgscan_signal_threshold_dbm_;
197 }
198 uint16 GetScanInterval(Error */* error */) { return scan_interval_seconds_; }
199 void SetBgscanMethod(const std::string &method, Error *error);
200 void SetBgscanShortInterval(const uint16 &seconds, Error *error);
201 void SetBgscanSignalThreshold(const int32 &dbm, Error *error);
202 void SetScanInterval(const uint16 &seconds, Error *error);
203
mukesh agrawal15908392011-11-16 18:29:25 +0000204 WiFiServiceRefPtr CreateServiceForEndpoint(
205 const WiFiEndpoint &endpoint, bool hidden_ssid);
206 void CurrentBSSChanged(const ::DBus::Path &new_bss);
Paul Stewart6ab23a92011-11-09 17:17:47 -0800207 WiFiServiceRefPtr FindService(const std::vector<uint8_t> &ssid,
208 const std::string &mode,
209 const std::string &security) const;
mukesh agrawal165e6142011-11-22 02:22:56 +0000210 WiFiServiceRefPtr FindServiceForEndpoint(const WiFiEndpoint &endpoint);
Paul Stewartced6a0b2011-11-08 15:32:04 -0800211 ByteArrays GetHiddenSSIDList();
mukesh agrawal15908392011-11-16 18:29:25 +0000212 void HandleDisconnect();
213 void HandleRoam(const ::DBus::Path &new_bssid);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800214 // Create services for hidden networks stored in |storage|. Returns true
215 // if any were found, otherwise returns false.
216 bool LoadHiddenServices(StoreInterface *storage);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000217 void BSSAddedTask(const ::DBus::Path &BSS,
218 const std::map<std::string, ::DBus::Variant> &properties);
219 void BSSRemovedTask(const ::DBus::Path &BSS);
Paul Stewart66c86002012-01-30 18:00:52 -0800220 void ClearCachedCredentialsTask();
Paul Stewartbc6e7392012-05-24 07:07:48 -0700221 void CertificationTask(
222 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawal15908392011-11-16 18:29:25 +0000223 void PropertiesChangedTask(
224 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700225 void ScanDoneTask();
mukesh agrawal32399322011-09-01 10:53:43 -0700226 void ScanTask();
mukesh agrawal7ec71312011-11-10 02:08:26 +0000227 void StateChanged(const std::string &new_state);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700228 // Heuristic check if a connection failure was due to bad credentials.
229 bool SuspectCredentials(const WiFiService &service) const;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800230 void HelpRegisterDerivedInt32(
231 PropertyStore *store,
232 const std::string &name,
233 int32(WiFi::*get)(Error *error),
234 void(WiFi::*set)(const int32 &value, Error *error));
235 void HelpRegisterDerivedString(
236 PropertyStore *store,
237 const std::string &name,
238 std::string(WiFi::*get)(Error *error),
239 void(WiFi::*set)(const std::string &value, Error *error));
240 void HelpRegisterDerivedUint16(
241 PropertyStore *store,
242 const std::string &name,
243 uint16(WiFi::*get)(Error *error),
244 void(WiFi::*set)(const uint16 &value, Error *error));
245
Gary Morainac1bdb42012-02-16 17:42:29 -0800246 // If this WiFi device is idle and |new_state| indicates a resume event, a
247 // scan is initiated.
248 void HandlePowerStateChange(PowerManager::SuspendState new_state);
Paul Stewarte369ece2012-05-22 09:11:03 -0700249 // Restart fast scanning after disconnection.
250 void RestartFastScanAttempts();
mukesh agrawalb66c6462012-05-07 11:45:25 -0700251 // Schedules a scan attempt at time |scan_interval_seconds_| in the
252 // future. Cancels any currently pending scan timer.
253 void StartScanTimer();
254 // Cancels any currently pending scan timer.
255 void StopScanTimer();
256 // Initiates a scan, if idle. Reschedules the scan timer regardless.
257 void ScanTimerHandler();
Gary Morainac1bdb42012-02-16 17:42:29 -0800258
Eric Shienbrood9a245532012-03-07 14:20:39 -0500259 base::WeakPtrFactory<WiFi> weak_ptr_factory_;
260
Darin Petkovab565bb2011-10-06 02:55:51 -0700261 // Store cached copies of singletons for speed/ease of testing.
262 ProxyFactory *proxy_factory_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800263 Time *time_;
Darin Petkovab565bb2011-10-06 02:55:51 -0700264
mukesh agrawalaf571952011-07-14 14:31:12 -0700265 scoped_ptr<SupplicantProcessProxyInterface> supplicant_process_proxy_;
266 scoped_ptr<SupplicantInterfaceProxyInterface> supplicant_interface_proxy_;
mukesh agrawal15908392011-11-16 18:29:25 +0000267 // The rpcid used as the key is wpa_supplicant's D-Bus path for the
268 // Endpoint (BSS, in supplicant parlance).
269 EndpointMap endpoint_by_rpcid_;
mukesh agrawal15908392011-11-16 18:29:25 +0000270 // Map from Services to the D-Bus path for the corresponding wpa_supplicant
271 // Network.
272 ReverseServiceMap rpcid_by_service_;
mukesh agrawal15908392011-11-16 18:29:25 +0000273 std::vector<WiFiServiceRefPtr> services_;
274 // The Service we are presently connected to. May be NULL is we're not
275 // not connected to any Service.
276 WiFiServiceRefPtr current_service_;
277 // The Service we're attempting to connect to. May be NULL if we're
278 // not attempting to connect to a new Service. If non-NULL, should
279 // be distinct from |current_service_|. (A service should not
280 // simultaneously be both pending, and current.)
281 WiFiServiceRefPtr pending_service_;
282 std::string supplicant_state_;
283 std::string supplicant_bss_;
Paul Stewart66c86002012-01-30 18:00:52 -0800284 // Signifies that ClearCachedCredentialsTask() is pending.
285 bool clear_cached_credentials_pending_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800286 // Indicates that we should flush supplicant's BSS cache after the
287 // next scan completes.
288 bool need_bss_flush_;
289 struct timeval resumed_at_;
mukesh agrawalb66c6462012-05-07 11:45:25 -0700290 // Executes when the (foreground) scan timer expires. Calls ScanTimerHandler.
291 base::CancelableClosure scan_timer_callback_;
Paul Stewarte369ece2012-05-22 09:11:03 -0700292 // Number of remaining fast scans to be done during startup and disconnect.
293 int fast_scans_remaining_;
mukesh agrawalab87ea42011-05-18 11:44:49 -0700294
Chris Masone853b81b2011-06-24 14:11:41 -0700295 // Properties
296 std::string bgscan_method_;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800297 uint16 bgscan_short_interval_seconds_;
298 int32 bgscan_signal_threshold_dbm_;
Chris Masone853b81b2011-06-24 14:11:41 -0700299 bool scan_pending_;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800300 uint16 scan_interval_seconds_;
Chris Masone853b81b2011-06-24 14:11:41 -0700301
Paul Stewartb50f0b92011-05-16 16:31:42 -0700302 DISALLOW_COPY_AND_ASSIGN(WiFi);
303};
304
305} // namespace shill
306
307#endif // SHILL_WIFI_