mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -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 | |
| 5 | #ifndef SHILL_WIFI_SERVICE_ |
| 6 | #define SHILL_WIFI_SERVICE_ |
| 7 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 8 | #include <set> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Darin Petkov | 4a09b6b | 2011-07-19 12:52:06 -0700 | [diff] [blame] | 12 | #include "shill/dbus_bindings/supplicant-interface.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 13 | #include "shill/event_dispatcher.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 14 | #include "shill/refptr_types.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 15 | #include "shill/service.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 16 | |
| 17 | namespace shill { |
| 18 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 19 | class ControlInterface; |
| 20 | class EventDispatcher; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 21 | class Error; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 22 | class Manager; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 23 | class Metrics; |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 24 | class NSS; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 25 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 26 | class WiFiService : public Service { |
| 27 | public: |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 28 | // TODO(pstew): Storage constants shouldn't need to be public |
| 29 | // crosbug.com/25813 |
| 30 | static const char kStorageHiddenSSID[]; |
| 31 | static const char kStorageMode[]; |
| 32 | static const char kStoragePassphrase[]; |
| 33 | static const char kStorageSecurity[]; |
| 34 | static const char kStorageSSID[]; |
| 35 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 36 | WiFiService(ControlInterface *control_interface, |
| 37 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 38 | Metrics *metrics, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 39 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 40 | const WiFiRefPtr &device, |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 41 | const std::vector<uint8_t> &ssid, |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 42 | const std::string &mode, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 43 | const std::string &security, |
| 44 | bool hidden_ssid); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 45 | ~WiFiService(); |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 46 | |
| 47 | // Inherited from Service. |
| 48 | virtual void Connect(Error *error); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 49 | virtual void Disconnect(Error *error); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 50 | virtual bool Is8021x() const; |
| 51 | |
Darin Petkov | 4a66cc5 | 2012-06-15 10:08:29 +0200 | [diff] [blame] | 52 | void AddEndpoint(const WiFiEndpointConstRefPtr &endpoint); |
| 53 | void RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint); |
| 54 | int GetEndpointCount() const { return endpoints_.size(); } |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 55 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 56 | // Called to update the identity of the currently connected endpoint. |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 57 | // To indicate that there is no currently connect endpoint, call with |
| 58 | // |endpoint| set to NULL. |
| 59 | void NotifyCurrentEndpoint(const WiFiEndpoint *endpoint); |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 60 | // Called to inform of changes in the properties of an endpoint. |
| 61 | // (Not necessarily the currently connected endpoint.) |
| 62 | void NotifyEndpointUpdated(const WiFiEndpoint &endpoint); |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 63 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 64 | // wifi_<MAC>_<BSSID>_<mode_string>_<security_string> |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 65 | std::string GetStorageIdentifier() const; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 66 | static bool ParseStorageIdentifier(const std::string &storage_name, |
| 67 | std::string *address, |
| 68 | std::string *mode, |
| 69 | std::string *security); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 70 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 71 | const std::string &mode() const { return mode_; } |
| 72 | const std::string &key_management() const { return GetEAPKeyManagement(); } |
| 73 | const std::vector<uint8_t> &ssid() const { return ssid_; } |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 74 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 75 | // Overrride Load and Save from parent Service class. We will call |
| 76 | // the parent method. |
| 77 | virtual bool IsLoadableFrom(StoreInterface *storage) const; |
| 78 | virtual bool Load(StoreInterface *storage); |
| 79 | virtual bool Save(StoreInterface *storage); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 80 | virtual bool Unload(); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 81 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 82 | virtual bool HasEndpoints() const { return !endpoints_.empty(); } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 83 | virtual bool IsVisible() const; |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 84 | bool IsSecurityMatch(const std::string &security) const; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 85 | bool hidden_ssid() const { return hidden_ssid_; } |
| 86 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 87 | virtual void InitializeCustomMetrics() const; |
Thieu Le | b84ba34 | 2012-03-02 15:15:19 -0800 | [diff] [blame] | 88 | virtual void SendPostReadyStateMetrics( |
| 89 | int64 time_resume_to_ready_milliseconds) const; |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 90 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 91 | // Override from parent Service class to correctly update connectability |
| 92 | // when the EAP credentials change for 802.1x networks. |
| 93 | void set_eap(const EapCredentials& eap); |
| 94 | |
Paul Stewart | 4357f4e | 2012-04-26 17:39:26 -0700 | [diff] [blame] | 95 | // Override from parent Service class to register hidden services once they |
| 96 | // have been configured. |
| 97 | virtual void OnProfileConfigured(); |
| 98 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 99 | protected: |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 100 | virtual bool IsAutoConnectable(const char **reason) const; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 101 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 102 | private: |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 103 | friend class WiFiServiceSecurityTest; |
Paul Stewart | 0b95044 | 2012-09-11 13:10:08 -0700 | [diff] [blame] | 104 | friend class WiFiServiceTest; // SetPassphrase |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 105 | friend class WiFiServiceUpdateFromEndpointsTest; // SignalToStrength |
Thieu Le | ad1ec2c | 2012-01-05 23:39:48 +0000 | [diff] [blame] | 106 | FRIEND_TEST(MetricsTest, WiFiServicePostReady); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 107 | FRIEND_TEST(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 108 | FRIEND_TEST(WiFiServiceTest, AutoConnect); |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 109 | FRIEND_TEST(WiFiServiceTest, ClearWriteOnlyDerivedProperty); // passphrase_ |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 110 | FRIEND_TEST(WiFiServiceTest, ConnectTask8021x); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 111 | FRIEND_TEST(WiFiServiceTest, ConnectTaskDynamicWEP); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 112 | FRIEND_TEST(WiFiServiceTest, ConnectTaskPSK); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 113 | FRIEND_TEST(WiFiServiceTest, ConnectTaskRSN); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 114 | FRIEND_TEST(WiFiServiceTest, ConnectTaskWEP); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 115 | FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 116 | FRIEND_TEST(WiFiServiceTest, IsAutoConnectable); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 117 | FRIEND_TEST(WiFiServiceTest, LoadHidden); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 118 | FRIEND_TEST(WiFiServiceTest, LoadAndUnloadPassphrase); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 119 | FRIEND_TEST(WiFiServiceTest, Populate8021x); |
Paul Stewart | 2055098 | 2012-04-16 12:16:11 -0700 | [diff] [blame] | 120 | FRIEND_TEST(WiFiServiceTest, Populate8021xNoSystemCAs); |
| 121 | FRIEND_TEST(WiFiServiceTest, Populate8021xUsingHardwareAuth); |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 122 | FRIEND_TEST(WiFiServiceTest, Populate8021xNSS); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 123 | FRIEND_TEST(WiFiServiceTest, SignalToStrength); // SignalToStrength |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 124 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 125 | static const char kAutoConnNoEndpoint[]; |
| 126 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 127 | // Override the base clase implementation, because we need to allow |
| 128 | // arguments that aren't base class methods. |
| 129 | void HelpRegisterWriteOnlyDerivedString( |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 130 | const std::string &name, |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 131 | void(WiFiService::*set)(const std::string &value, Error *error), |
| 132 | void(WiFiService::*clear)(Error *error), |
| 133 | const std::string *default_value); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 134 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 135 | std::string GetDeviceRpcId(Error *error); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 136 | void ClearPassphrase(Error *error); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 137 | void UpdateConnectable(); |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 138 | void UpdateFromEndpoints(); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 139 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 140 | static void ValidateWEPPassphrase(const std::string &passphrase, |
| 141 | Error *error); |
| 142 | static void ValidateWPAPassphrase(const std::string &passphrase, |
| 143 | Error *error); |
| 144 | static void ParseWEPPassphrase(const std::string &passphrase, |
| 145 | int *key_index, |
| 146 | std::vector<uint8> *password_bytes, |
| 147 | Error *error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 148 | static bool CheckWEPIsHex(const std::string &passphrase, Error *error); |
| 149 | static bool CheckWEPKeyIndex(const std::string &passphrase, Error *error); |
| 150 | static bool CheckWEPPrefix(const std::string &passphrase, Error *error); |
| 151 | |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 152 | // Maps a signal value, in dBm, to a "strength" value, from |
| 153 | // |Service::kStrengthMin| to |Service:kStrengthMax|. |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 154 | static uint8 SignalToStrength(int16 signal_dbm); |
| 155 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 156 | // "wpa", "rsn" and "psk" are equivalent from a configuration perspective. |
| 157 | // This function maps them all into "psk". |
| 158 | static std::string GetSecurityClass(const std::string &security); |
| 159 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 160 | // Profile data for a WPA/RSN service can be stored under a number of |
| 161 | // different names. These functions create different storage identifiers |
| 162 | // based on whether they are referred to by their generic "psk" name or |
| 163 | // if they use the (legacy) specific "wpa" or "rsn" names. |
| 164 | std::string GetGenericStorageIdentifier() const; |
| 165 | std::string GetSpecificStorageIdentifier() const; |
| 166 | std::string GetStorageIdentifierForSecurity( |
| 167 | const std::string &security) const; |
| 168 | |
Paul Stewart | 0b95044 | 2012-09-11 13:10:08 -0700 | [diff] [blame] | 169 | // Validate then apply a passphrase for this service. |
| 170 | void SetPassphrase(const std::string &passphrase, Error *error); |
| 171 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 172 | // Populate the |params| map with available 802.1x EAP properties. |
| 173 | void Populate8021xProperties(std::map<std::string, DBus::Variant> *params); |
| 174 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 175 | // Properties |
| 176 | std::string passphrase_; |
| 177 | bool need_passphrase_; |
| 178 | std::string security_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 179 | // TODO(cmasone): see if the below can be pulled from the endpoint associated |
| 180 | // with this service instead. |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 181 | const std::string mode_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 182 | std::string auth_mode_; |
| 183 | bool hidden_ssid_; |
| 184 | uint16 frequency_; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 185 | // TODO(quiche): I noticed this is not hooked up to anything. In fact, it |
| 186 | // was undefined until now. crosbug.com/26490 |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 187 | uint16 physical_mode_; |
Paul Stewart | 23b393a | 2012-09-25 21:21:06 -0700 | [diff] [blame] | 188 | // The raw dBm signal strength from the associated endpoint. |
| 189 | int16 raw_signal_strength_; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 190 | std::string hex_ssid_; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 191 | std::string storage_identifier_; |
mukesh agrawal | 923f14f | 2012-06-04 16:46:08 -0700 | [diff] [blame] | 192 | std::string bssid_; |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 193 | Stringmap vendor_information_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 194 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 195 | // Track whether or not we've warned about large signal values. |
| 196 | // Used to avoid spamming the log. |
| 197 | static bool logged_signal_warning; |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 198 | WiFiRefPtr wifi_; |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 199 | std::set<WiFiEndpointConstRefPtr> endpoints_; |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 200 | WiFiEndpointConstRefPtr current_endpoint_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 201 | const std::vector<uint8_t> ssid_; |
Paul Stewart | ecf4cd1 | 2012-04-17 11:08:39 -0700 | [diff] [blame] | 202 | NSS *nss_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 203 | DISALLOW_COPY_AND_ASSIGN(WiFiService); |
| 204 | }; |
| 205 | |
| 206 | } // namespace shill |
| 207 | |
| 208 | #endif // SHILL_WIFI_SERVICE_ |