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; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 24 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 25 | class WiFiService : public Service { |
| 26 | public: |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 27 | // TODO(pstew): Storage constants shouldn't need to be public |
| 28 | // crosbug.com/25813 |
| 29 | static const char kStorageHiddenSSID[]; |
| 30 | static const char kStorageMode[]; |
| 31 | static const char kStoragePassphrase[]; |
| 32 | static const char kStorageSecurity[]; |
| 33 | static const char kStorageSSID[]; |
| 34 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 35 | WiFiService(ControlInterface *control_interface, |
| 36 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 37 | Metrics *metrics, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 38 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 39 | const WiFiRefPtr &device, |
mukesh agrawal | 7ec7131 | 2011-11-10 02:08:26 +0000 | [diff] [blame] | 40 | const std::vector<uint8_t> &ssid, |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 41 | const std::string &mode, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 42 | const std::string &security, |
| 43 | bool hidden_ssid); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 44 | ~WiFiService(); |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 45 | |
| 46 | // Inherited from Service. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 47 | virtual void AutoConnect(); |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 48 | virtual void Connect(Error *error); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 49 | virtual void Disconnect(Error *error); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 50 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 51 | virtual bool TechnologyIs(const Technology::Identifier type) const; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 52 | virtual bool IsConnecting() const; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 53 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 54 | virtual void AddEndpoint(WiFiEndpointConstRefPtr endpoint); |
| 55 | virtual void RemoveEndpoint(WiFiEndpointConstRefPtr endpoint); |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 56 | bool NumEndpoints() const { return endpoints_.size(); } |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 57 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 58 | // wifi_<MAC>_<BSSID>_<mode_string>_<security_string> |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 59 | std::string GetStorageIdentifier() const; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 60 | static bool ParseStorageIdentifier(const std::string &storage_name, |
| 61 | std::string *address, |
| 62 | std::string *mode, |
| 63 | std::string *security); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 64 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 65 | const std::string &mode() const { return mode_; } |
| 66 | const std::string &key_management() const { return GetEAPKeyManagement(); } |
| 67 | const std::vector<uint8_t> &ssid() const { return ssid_; } |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 68 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 69 | void SetPassphrase(const std::string &passphrase, Error *error); |
| 70 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 71 | // Overrride Load and Save from parent Service class. We will call |
| 72 | // the parent method. |
| 73 | virtual bool IsLoadableFrom(StoreInterface *storage) const; |
| 74 | virtual bool Load(StoreInterface *storage); |
| 75 | virtual bool Save(StoreInterface *storage); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 76 | virtual void Unload(); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 77 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 78 | virtual bool HasEndpoints() const { return !endpoints_.empty(); } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 79 | virtual bool IsVisible() const; |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 80 | bool IsSecurityMatch(const std::string &security) const; |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 81 | bool hidden_ssid() const { return hidden_ssid_; } |
| 82 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 83 | virtual void InitializeCustomMetrics() const; |
| 84 | virtual void SendPostReadyStateMetrics() const; |
| 85 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 86 | // Override from parent Service class to correctly update connectability |
| 87 | // when the EAP credentials change for 802.1x networks. |
| 88 | void set_eap(const EapCredentials& eap); |
| 89 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 90 | protected: |
| 91 | virtual bool IsAutoConnectable() const; |
| 92 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 93 | private: |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 94 | friend class WiFiServiceSecurityTest; |
Thieu Le | ad1ec2c | 2012-01-05 23:39:48 +0000 | [diff] [blame] | 95 | FRIEND_TEST(MetricsTest, WiFiServicePostReady); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 96 | FRIEND_TEST(WiFiServiceTest, AutoConnect); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 97 | FRIEND_TEST(WiFiServiceTest, ConnectTask8021x); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 98 | FRIEND_TEST(WiFiServiceTest, ConnectTaskDynamicWEP); |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 99 | FRIEND_TEST(WiFiServiceTest, ConnectTaskPSK); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 100 | FRIEND_TEST(WiFiServiceTest, ConnectTaskRSN); |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 101 | FRIEND_TEST(WiFiServiceTest, ConnectTaskWEP); |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 102 | FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 103 | FRIEND_TEST(WiFiServiceTest, IsAutoConnectable); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 104 | FRIEND_TEST(WiFiServiceTest, LoadHidden); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 105 | FRIEND_TEST(WiFiServiceTest, LoadAndUnloadPassphrase); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 106 | FRIEND_TEST(WiFiServiceTest, Populate8021x); |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 107 | |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 108 | void HelpRegisterDerivedString( |
| 109 | PropertyStore *store, |
| 110 | const std::string &name, |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 111 | std::string(WiFiService::*get)(Error *error), |
| 112 | void(WiFiService::*set)(const std::string &value, Error *error)); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 113 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 114 | void ConnectTask(); |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 115 | void DisconnectTask(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 116 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 117 | std::string GetDeviceRpcId(Error *error); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 118 | void UpdateConnectable(); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 119 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 120 | static void ValidateWEPPassphrase(const std::string &passphrase, |
| 121 | Error *error); |
| 122 | static void ValidateWPAPassphrase(const std::string &passphrase, |
| 123 | Error *error); |
| 124 | static void ParseWEPPassphrase(const std::string &passphrase, |
| 125 | int *key_index, |
| 126 | std::vector<uint8> *password_bytes, |
| 127 | Error *error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 128 | static bool CheckWEPIsHex(const std::string &passphrase, Error *error); |
| 129 | static bool CheckWEPKeyIndex(const std::string &passphrase, Error *error); |
| 130 | static bool CheckWEPPrefix(const std::string &passphrase, Error *error); |
| 131 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 132 | // replace non-ASCII characters with '?'. return true if one or more |
| 133 | // characters were changed |
| 134 | static bool SanitizeSSID(std::string *ssid); |
| 135 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 136 | // "wpa", "rsn" and "psk" are equivalent from a configuration perspective. |
| 137 | // This function maps them all into "psk". |
| 138 | static std::string GetSecurityClass(const std::string &security); |
| 139 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 140 | // Profile data for a WPA/RSN service can be stored under a number of |
| 141 | // different names. These functions create different storage identifiers |
| 142 | // based on whether they are referred to by their generic "psk" name or |
| 143 | // if they use the (legacy) specific "wpa" or "rsn" names. |
| 144 | std::string GetGenericStorageIdentifier() const; |
| 145 | std::string GetSpecificStorageIdentifier() const; |
| 146 | std::string GetStorageIdentifierForSecurity( |
| 147 | const std::string &security) const; |
| 148 | |
Gaurav Shah | 29d6888 | 2012-01-30 19:06:42 -0800 | [diff] [blame] | 149 | // Returns true if the wireless service uses 802.1x for key management. |
| 150 | bool Is8021x() const; |
| 151 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 152 | // Populate the |params| map with available 802.1x EAP properties. |
| 153 | void Populate8021xProperties(std::map<std::string, DBus::Variant> *params); |
| 154 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 155 | // Properties |
| 156 | std::string passphrase_; |
| 157 | bool need_passphrase_; |
| 158 | std::string security_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 159 | // TODO(cmasone): see if the below can be pulled from the endpoint associated |
| 160 | // with this service instead. |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 161 | const std::string mode_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 162 | std::string auth_mode_; |
| 163 | bool hidden_ssid_; |
| 164 | uint16 frequency_; |
| 165 | uint16 physical_mode_; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 166 | std::string hex_ssid_; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 167 | std::string storage_identifier_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 168 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 169 | ScopedRunnableMethodFactory<WiFiService> task_factory_; |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 170 | WiFiRefPtr wifi_; |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame] | 171 | std::set<WiFiEndpointConstRefPtr> endpoints_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 172 | const std::vector<uint8_t> ssid_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 173 | DISALLOW_COPY_AND_ASSIGN(WiFiService); |
| 174 | }; |
| 175 | |
| 176 | } // namespace shill |
| 177 | |
| 178 | #endif // SHILL_WIFI_SERVICE_ |