blob: a039f989fe8002ae057bd6adb301fe973ad235a6 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawalb54601c2011-06-07 17:39:22 -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_SERVICE_
6#define SHILL_WIFI_SERVICE_
7
mukesh agrawal261daca2011-12-02 18:56:56 +00008#include <set>
mukesh agrawalb54601c2011-06-07 17:39:22 -07009#include <string>
10#include <vector>
11
Darin Petkov4a09b6b2011-07-19 12:52:06 -070012#include "shill/dbus_bindings/supplicant-interface.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070013#include "shill/event_dispatcher.h"
Paul Stewart71a4d3b2013-01-18 18:12:56 -080014#include "shill/key_value_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070015#include "shill/refptr_types.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070016#include "shill/service.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070017
18namespace shill {
19
Chris Masone6791a432011-07-12 13:23:19 -070020class ControlInterface;
21class EventDispatcher;
mukesh agrawal1a056262011-10-05 14:36:54 -070022class Error;
Chris Masone6791a432011-07-12 13:23:19 -070023class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080024class Metrics;
Paul Stewartecf4cd12012-04-17 11:08:39 -070025class NSS;
Chris Masone6791a432011-07-12 13:23:19 -070026
mukesh agrawalb54601c2011-06-07 17:39:22 -070027class WiFiService : public Service {
28 public:
Paul Stewart0756db92012-01-27 08:34:47 -080029 // TODO(pstew): Storage constants shouldn't need to be public
30 // crosbug.com/25813
31 static const char kStorageHiddenSSID[];
32 static const char kStorageMode[];
33 static const char kStoragePassphrase[];
34 static const char kStorageSecurity[];
Paul Stewart71a4d3b2013-01-18 18:12:56 -080035 static const char kStorageSecurityClass[];
Paul Stewart0756db92012-01-27 08:34:47 -080036 static const char kStorageSSID[];
37
mukesh agrawalb54601c2011-06-07 17:39:22 -070038 WiFiService(ControlInterface *control_interface,
39 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080040 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070041 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070042 const WiFiRefPtr &device,
mukesh agrawal7ec71312011-11-10 02:08:26 +000043 const std::vector<uint8_t> &ssid,
Chris Masone092df3e2011-08-22 09:41:39 -070044 const std::string &mode,
Paul Stewartced6a0b2011-11-08 15:32:04 -080045 const std::string &security,
46 bool hidden_ssid);
mukesh agrawalb54601c2011-06-07 17:39:22 -070047 ~WiFiService();
Darin Petkov4d6d9412011-08-24 13:19:54 -070048
49 // Inherited from Service.
50 virtual void Connect(Error *error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +000051 virtual void Disconnect(Error *error);
Paul Stewart81426132012-05-16 10:05:10 -070052 virtual bool Is8021x() const;
53
Darin Petkov4a66cc52012-06-15 10:08:29 +020054 void AddEndpoint(const WiFiEndpointConstRefPtr &endpoint);
55 void RemoveEndpoint(const WiFiEndpointConstRefPtr &endpoint);
56 int GetEndpointCount() const { return endpoints_.size(); }
mukesh agrawale1d90e92012-02-15 17:36:08 -080057
mukesh agrawalb20776f2012-02-10 16:00:36 -080058 // Called to update the identity of the currently connected endpoint.
mukesh agrawale1d90e92012-02-15 17:36:08 -080059 // To indicate that there is no currently connect endpoint, call with
60 // |endpoint| set to NULL.
61 void NotifyCurrentEndpoint(const WiFiEndpoint *endpoint);
mukesh agrawalb20776f2012-02-10 16:00:36 -080062 // Called to inform of changes in the properties of an endpoint.
63 // (Not necessarily the currently connected endpoint.)
64 void NotifyEndpointUpdated(const WiFiEndpoint &endpoint);
mukesh agrawal261daca2011-12-02 18:56:56 +000065
Chris Masone34af2182011-08-22 11:59:36 -070066 // wifi_<MAC>_<BSSID>_<mode_string>_<security_string>
Chris Masone6515aab2011-10-12 16:19:09 -070067 std::string GetStorageIdentifier() const;
Paul Stewarta41e38d2011-11-11 07:47:29 -080068 static bool ParseStorageIdentifier(const std::string &storage_name,
69 std::string *address,
70 std::string *mode,
71 std::string *security);
Chris Masone34af2182011-08-22 11:59:36 -070072
Paul Stewart85aea152013-01-22 09:31:56 -080073 // Iterate over |storage| looking for WiFi servces with "old-style"
74 // properties that don't include explicit type/mode/security, and add
75 // these properties. Returns true if any entries were fixed.
76 static bool FixupServiceEntries(StoreInterface *storage);
77
Thieu Le48e6d6d2011-12-06 00:40:27 +000078 const std::string &mode() const { return mode_; }
79 const std::string &key_management() const { return GetEAPKeyManagement(); }
80 const std::vector<uint8_t> &ssid() const { return ssid_; }
mukesh agrawalb54601c2011-06-07 17:39:22 -070081
Paul Stewartd08f4432011-11-04 07:48:20 -070082 // Overrride Load and Save from parent Service class. We will call
83 // the parent method.
84 virtual bool IsLoadableFrom(StoreInterface *storage) const;
85 virtual bool Load(StoreInterface *storage);
86 virtual bool Save(StoreInterface *storage);
Paul Stewart65512e12012-03-26 18:01:08 -070087 virtual bool Unload();
Paul Stewartd08f4432011-11-04 07:48:20 -070088
mukesh agrawal8a3188d2011-12-01 20:56:44 +000089 virtual bool HasEndpoints() const { return !endpoints_.empty(); }
Paul Stewarta41e38d2011-11-11 07:47:29 -080090 virtual bool IsVisible() const;
Paul Stewart6ab23a92011-11-09 17:17:47 -080091 bool IsSecurityMatch(const std::string &security) const;
Paul Stewartced6a0b2011-11-08 15:32:04 -080092 bool hidden_ssid() const { return hidden_ssid_; }
Paul Stewarta5e7d5f2013-01-09 18:06:15 -080093 bool ieee80211w_required() const { return ieee80211w_required_; }
Paul Stewartced6a0b2011-11-08 15:32:04 -080094
Thieu Le48e6d6d2011-12-06 00:40:27 +000095 virtual void InitializeCustomMetrics() const;
Thieu Leb84ba342012-03-02 15:15:19 -080096 virtual void SendPostReadyStateMetrics(
97 int64 time_resume_to_ready_milliseconds) const;
Thieu Le48e6d6d2011-12-06 00:40:27 +000098
Paul Stewart835934a2012-12-06 19:27:09 -080099 // Clear any cached credentials stored in wpa_supplicant related to |this|.
100 // This will disconnect this service if it is currently connected.
101 void ClearCachedCredentials();
102
Gaurav Shah10109f22011-11-11 20:16:22 -0800103 // Override from parent Service class to correctly update connectability
104 // when the EAP credentials change for 802.1x networks.
105 void set_eap(const EapCredentials& eap);
106
Paul Stewart4357f4e2012-04-26 17:39:26 -0700107 // Override from parent Service class to register hidden services once they
108 // have been configured.
109 virtual void OnProfileConfigured();
110
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000111 protected:
mukesh agrawalbf14e942012-03-02 14:36:34 -0800112 virtual bool IsAutoConnectable(const char **reason) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000113
mukesh agrawalb54601c2011-06-07 17:39:22 -0700114 private:
Paul Stewartd08f4432011-11-04 07:48:20 -0700115 friend class WiFiServiceSecurityTest;
Paul Stewart0b950442012-09-11 13:10:08 -0700116 friend class WiFiServiceTest; // SetPassphrase
mukesh agrawale1d90e92012-02-15 17:36:08 -0800117 friend class WiFiServiceUpdateFromEndpointsTest; // SignalToStrength
Thieu Lead1ec2c2012-01-05 23:39:48 +0000118 FRIEND_TEST(MetricsTest, WiFiServicePostReady);
Thieu Lee41a72d2012-02-06 20:46:51 +0000119 FRIEND_TEST(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000120 FRIEND_TEST(WiFiServiceTest, AutoConnect);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800121 FRIEND_TEST(WiFiServiceTest, ClearWriteOnlyDerivedProperty); // passphrase_
Gaurav Shah10109f22011-11-11 20:16:22 -0800122 FRIEND_TEST(WiFiServiceTest, ConnectTask8021x);
Gaurav Shah29d68882012-01-30 19:06:42 -0800123 FRIEND_TEST(WiFiServiceTest, ConnectTaskDynamicWEP);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800124 FRIEND_TEST(WiFiServiceTest, ConnectTaskPSK);
Gaurav Shah29d68882012-01-30 19:06:42 -0800125 FRIEND_TEST(WiFiServiceTest, ConnectTaskRSN);
Thieu Lef4cbda92011-11-10 23:41:24 +0000126 FRIEND_TEST(WiFiServiceTest, ConnectTaskWEP);
Gaurav Shah29d68882012-01-30 19:06:42 -0800127 FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA);
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800128 FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA80211w);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000129 FRIEND_TEST(WiFiServiceTest, IsAutoConnectable);
Paul Stewartd08f4432011-11-04 07:48:20 -0700130 FRIEND_TEST(WiFiServiceTest, LoadHidden);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800131 FRIEND_TEST(WiFiServiceTest, LoadAndUnloadPassphrase);
Gaurav Shah10109f22011-11-11 20:16:22 -0800132 FRIEND_TEST(WiFiServiceTest, Populate8021x);
Paul Stewart20550982012-04-16 12:16:11 -0700133 FRIEND_TEST(WiFiServiceTest, Populate8021xNoSystemCAs);
134 FRIEND_TEST(WiFiServiceTest, Populate8021xUsingHardwareAuth);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700135 FRIEND_TEST(WiFiServiceTest, Populate8021xNSS);
Paul Stewart835934a2012-12-06 19:27:09 -0800136 FRIEND_TEST(WiFiServiceTest, SetPassphraseRemovesCachedCredentials);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800137 FRIEND_TEST(WiFiServiceTest, SignalToStrength); // SignalToStrength
Paul Stewartd08f4432011-11-04 07:48:20 -0700138
mukesh agrawalbf14e942012-03-02 14:36:34 -0800139 static const char kAutoConnNoEndpoint[];
140
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800141 // Override the base clase implementation, because we need to allow
142 // arguments that aren't base class methods.
143 void HelpRegisterWriteOnlyDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000144 const std::string &name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800145 void(WiFiService::*set)(const std::string &value, Error *error),
146 void(WiFiService::*clear)(Error *error),
147 const std::string *default_value);
Thieu Lef7709452011-11-15 01:13:19 +0000148
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800149 std::string GetDeviceRpcId(Error *error);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800150 void ClearPassphrase(Error *error);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000151 void UpdateConnectable();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800152 void UpdateFromEndpoints();
Chris Masone95207da2011-06-29 16:50:49 -0700153
Thieu Lef4cbda92011-11-10 23:41:24 +0000154 static void ValidateWEPPassphrase(const std::string &passphrase,
155 Error *error);
156 static void ValidateWPAPassphrase(const std::string &passphrase,
157 Error *error);
158 static void ParseWEPPassphrase(const std::string &passphrase,
159 int *key_index,
160 std::vector<uint8> *password_bytes,
161 Error *error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700162 static bool CheckWEPIsHex(const std::string &passphrase, Error *error);
163 static bool CheckWEPKeyIndex(const std::string &passphrase, Error *error);
164 static bool CheckWEPPrefix(const std::string &passphrase, Error *error);
165
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800166 // Maps a signal value, in dBm, to a "strength" value, from
167 // |Service::kStrengthMin| to |Service:kStrengthMax|.
mukesh agrawale1d90e92012-02-15 17:36:08 -0800168 static uint8 SignalToStrength(int16 signal_dbm);
169
Paul Stewart6ab23a92011-11-09 17:17:47 -0800170 // "wpa", "rsn" and "psk" are equivalent from a configuration perspective.
171 // This function maps them all into "psk".
172 static std::string GetSecurityClass(const std::string &security);
173
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800174 // Create a default group name for this WiFi service.
175 std::string GetDefaultStorageIdentifier() const;
176
Paul Stewartd08f4432011-11-04 07:48:20 -0700177 // Profile data for a WPA/RSN service can be stored under a number of
Paul Stewart71a4d3b2013-01-18 18:12:56 -0800178 // different security types. These functions create different storage
179 // property lists based on whether they are saved with their generic
180 // "psk" name or if they use the (legacy) specific "wpa" or "rsn" names.
181 KeyValueStore GetStorageProperties() const;
Paul Stewartd08f4432011-11-04 07:48:20 -0700182
Paul Stewart0b950442012-09-11 13:10:08 -0700183 // Validate then apply a passphrase for this service.
184 void SetPassphrase(const std::string &passphrase, Error *error);
185
Gaurav Shah10109f22011-11-11 20:16:22 -0800186 // Populate the |params| map with available 802.1x EAP properties.
187 void Populate8021xProperties(std::map<std::string, DBus::Variant> *params);
188
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700189 // Properties
190 std::string passphrase_;
191 bool need_passphrase_;
192 std::string security_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700193 // TODO(cmasone): see if the below can be pulled from the endpoint associated
194 // with this service instead.
Chris Masone092df3e2011-08-22 09:41:39 -0700195 const std::string mode_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700196 std::string auth_mode_;
197 bool hidden_ssid_;
198 uint16 frequency_;
Paul Stewart20088d82012-02-16 06:58:55 -0800199 // TODO(quiche): I noticed this is not hooked up to anything. In fact, it
200 // was undefined until now. crosbug.com/26490
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700201 uint16 physical_mode_;
Paul Stewart23b393a2012-09-25 21:21:06 -0700202 // The raw dBm signal strength from the associated endpoint.
203 int16 raw_signal_strength_;
mukesh agrawal32399322011-09-01 10:53:43 -0700204 std::string hex_ssid_;
Paul Stewartd08f4432011-11-04 07:48:20 -0700205 std::string storage_identifier_;
mukesh agrawal923f14f2012-06-04 16:46:08 -0700206 std::string bssid_;
Paul Stewart72b2fdc2012-06-02 08:58:51 -0700207 Stringmap vendor_information_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700208
mukesh agrawale1d90e92012-02-15 17:36:08 -0800209 // Track whether or not we've warned about large signal values.
210 // Used to avoid spamming the log.
211 static bool logged_signal_warning;
Chris Masone2b105542011-06-22 10:58:09 -0700212 WiFiRefPtr wifi_;
mukesh agrawal261daca2011-12-02 18:56:56 +0000213 std::set<WiFiEndpointConstRefPtr> endpoints_;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800214 WiFiEndpointConstRefPtr current_endpoint_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700215 const std::vector<uint8_t> ssid_;
Paul Stewarta5e7d5f2013-01-09 18:06:15 -0800216 // Track whether IEEE 802.11w (Protected Management Frame) support is
217 // mandated by one or more endpoints we have seen that provide this service.
218 bool ieee80211w_required_;
Paul Stewartecf4cd12012-04-17 11:08:39 -0700219 NSS *nss_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700220 DISALLOW_COPY_AND_ASSIGN(WiFiService);
221};
222
223} // namespace shill
224
225#endif // SHILL_WIFI_SERVICE_