blob: 83fcbdad9d5ea21e36ae30ee357b733d92ede142 [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"
Chris Masone2b105542011-06-22 10:58:09 -070014#include "shill/refptr_types.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070015#include "shill/service.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070016
17namespace shill {
18
Chris Masone6791a432011-07-12 13:23:19 -070019class ControlInterface;
20class EventDispatcher;
mukesh agrawal1a056262011-10-05 14:36:54 -070021class Error;
Chris Masone6791a432011-07-12 13:23:19 -070022class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080023class Metrics;
Paul Stewartecf4cd12012-04-17 11:08:39 -070024class NSS;
Chris Masone6791a432011-07-12 13:23:19 -070025
mukesh agrawalb54601c2011-06-07 17:39:22 -070026class WiFiService : public Service {
27 public:
Paul Stewart0756db92012-01-27 08:34:47 -080028 // 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 agrawalb54601c2011-06-07 17:39:22 -070036 WiFiService(ControlInterface *control_interface,
37 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080038 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070039 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070040 const WiFiRefPtr &device,
mukesh agrawal7ec71312011-11-10 02:08:26 +000041 const std::vector<uint8_t> &ssid,
Chris Masone092df3e2011-08-22 09:41:39 -070042 const std::string &mode,
Paul Stewartced6a0b2011-11-08 15:32:04 -080043 const std::string &security,
44 bool hidden_ssid);
mukesh agrawalb54601c2011-06-07 17:39:22 -070045 ~WiFiService();
Darin Petkov4d6d9412011-08-24 13:19:54 -070046
47 // Inherited from Service.
mukesh agrawal8a3188d2011-12-01 20:56:44 +000048 virtual void AutoConnect();
Darin Petkov4d6d9412011-08-24 13:19:54 -070049 virtual void Connect(Error *error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +000050 virtual void Disconnect(Error *error);
Chris Masone34af2182011-08-22 11:59:36 -070051
Paul Stewart22aa71b2011-09-16 12:15:11 -070052 virtual bool TechnologyIs(const Technology::Identifier type) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +000053 virtual bool IsConnecting() const;
Paul Stewart22aa71b2011-09-16 12:15:11 -070054
mukesh agrawale1d90e92012-02-15 17:36:08 -080055 virtual void AddEndpoint(const WiFiEndpointConstRefPtr endpoint);
56 virtual void RemoveEndpoint(const WiFiEndpointConstRefPtr endpoint);
mukesh agrawal4eb4d782011-12-05 17:34:37 +000057 bool NumEndpoints() const { return endpoints_.size(); }
mukesh agrawale1d90e92012-02-15 17:36:08 -080058
mukesh agrawalb20776f2012-02-10 16:00:36 -080059 // Called to update the identity of the currently connected endpoint.
mukesh agrawale1d90e92012-02-15 17:36:08 -080060 // To indicate that there is no currently connect endpoint, call with
61 // |endpoint| set to NULL.
62 void NotifyCurrentEndpoint(const WiFiEndpoint *endpoint);
mukesh agrawalb20776f2012-02-10 16:00:36 -080063 // Called to inform of changes in the properties of an endpoint.
64 // (Not necessarily the currently connected endpoint.)
65 void NotifyEndpointUpdated(const WiFiEndpoint &endpoint);
mukesh agrawal261daca2011-12-02 18:56:56 +000066
Chris Masone34af2182011-08-22 11:59:36 -070067 // wifi_<MAC>_<BSSID>_<mode_string>_<security_string>
Chris Masone6515aab2011-10-12 16:19:09 -070068 std::string GetStorageIdentifier() const;
Paul Stewarta41e38d2011-11-11 07:47:29 -080069 static bool ParseStorageIdentifier(const std::string &storage_name,
70 std::string *address,
71 std::string *mode,
72 std::string *security);
Chris Masone34af2182011-08-22 11:59:36 -070073
Thieu Le48e6d6d2011-12-06 00:40:27 +000074 const std::string &mode() const { return mode_; }
75 const std::string &key_management() const { return GetEAPKeyManagement(); }
76 const std::vector<uint8_t> &ssid() const { return ssid_; }
mukesh agrawalb54601c2011-06-07 17:39:22 -070077
mukesh agrawal1a056262011-10-05 14:36:54 -070078 void SetPassphrase(const std::string &passphrase, Error *error);
79
Paul Stewartd08f4432011-11-04 07:48:20 -070080 // Overrride Load and Save from parent Service class. We will call
81 // the parent method.
82 virtual bool IsLoadableFrom(StoreInterface *storage) const;
83 virtual bool Load(StoreInterface *storage);
84 virtual bool Save(StoreInterface *storage);
Paul Stewart65512e12012-03-26 18:01:08 -070085 virtual bool Unload();
Paul Stewartd08f4432011-11-04 07:48:20 -070086
mukesh agrawal8a3188d2011-12-01 20:56:44 +000087 virtual bool HasEndpoints() const { return !endpoints_.empty(); }
Paul Stewarta41e38d2011-11-11 07:47:29 -080088 virtual bool IsVisible() const;
Paul Stewart6ab23a92011-11-09 17:17:47 -080089 bool IsSecurityMatch(const std::string &security) const;
Paul Stewartced6a0b2011-11-08 15:32:04 -080090 bool hidden_ssid() const { return hidden_ssid_; }
91
Thieu Le48e6d6d2011-12-06 00:40:27 +000092 virtual void InitializeCustomMetrics() const;
Thieu Leb84ba342012-03-02 15:15:19 -080093 virtual void SendPostReadyStateMetrics(
94 int64 time_resume_to_ready_milliseconds) const;
Thieu Le48e6d6d2011-12-06 00:40:27 +000095
Gaurav Shah10109f22011-11-11 20:16:22 -080096 // Override from parent Service class to correctly update connectability
97 // when the EAP credentials change for 802.1x networks.
98 void set_eap(const EapCredentials& eap);
99
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000100 protected:
mukesh agrawalbf14e942012-03-02 14:36:34 -0800101 virtual bool IsAutoConnectable(const char **reason) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000102
mukesh agrawalb54601c2011-06-07 17:39:22 -0700103 private:
Paul Stewartd08f4432011-11-04 07:48:20 -0700104 friend class WiFiServiceSecurityTest;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800105 friend class WiFiServiceUpdateFromEndpointsTest; // SignalToStrength
Thieu Lead1ec2c2012-01-05 23:39:48 +0000106 FRIEND_TEST(MetricsTest, WiFiServicePostReady);
Thieu Lee41a72d2012-02-06 20:46:51 +0000107 FRIEND_TEST(WiFiMainTest, CurrentBSSChangedUpdateServiceEndpoint);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000108 FRIEND_TEST(WiFiServiceTest, AutoConnect);
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800109 FRIEND_TEST(WiFiServiceTest, ClearWriteOnlyDerivedProperty); // passphrase_
Gaurav Shah10109f22011-11-11 20:16:22 -0800110 FRIEND_TEST(WiFiServiceTest, ConnectTask8021x);
Gaurav Shah29d68882012-01-30 19:06:42 -0800111 FRIEND_TEST(WiFiServiceTest, ConnectTaskDynamicWEP);
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800112 FRIEND_TEST(WiFiServiceTest, ConnectTaskPSK);
Gaurav Shah29d68882012-01-30 19:06:42 -0800113 FRIEND_TEST(WiFiServiceTest, ConnectTaskRSN);
Thieu Lef4cbda92011-11-10 23:41:24 +0000114 FRIEND_TEST(WiFiServiceTest, ConnectTaskWEP);
Gaurav Shah29d68882012-01-30 19:06:42 -0800115 FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000116 FRIEND_TEST(WiFiServiceTest, IsAutoConnectable);
Paul Stewartd08f4432011-11-04 07:48:20 -0700117 FRIEND_TEST(WiFiServiceTest, LoadHidden);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800118 FRIEND_TEST(WiFiServiceTest, LoadAndUnloadPassphrase);
Gaurav Shah10109f22011-11-11 20:16:22 -0800119 FRIEND_TEST(WiFiServiceTest, Populate8021x);
Paul Stewart20550982012-04-16 12:16:11 -0700120 FRIEND_TEST(WiFiServiceTest, Populate8021xNoSystemCAs);
121 FRIEND_TEST(WiFiServiceTest, Populate8021xUsingHardwareAuth);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700122 FRIEND_TEST(WiFiServiceTest, Populate8021xNSS);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800123 FRIEND_TEST(WiFiServiceTest, SignalToStrength); // SignalToStrength
Paul Stewartd08f4432011-11-04 07:48:20 -0700124
mukesh agrawalbf14e942012-03-02 14:36:34 -0800125 static const char kAutoConnBusy[];
126 static const char kAutoConnNoEndpoint[];
127
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800128 // Override the base clase implementation, because we need to allow
129 // arguments that aren't base class methods.
130 void HelpRegisterWriteOnlyDerivedString(
Thieu Lef7709452011-11-15 01:13:19 +0000131 const std::string &name,
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800132 void(WiFiService::*set)(const std::string &value, Error *error),
133 void(WiFiService::*clear)(Error *error),
134 const std::string *default_value);
Thieu Lef7709452011-11-15 01:13:19 +0000135
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800136 std::string GetDeviceRpcId(Error *error);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800137 void ClearPassphrase(Error *error);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000138 void UpdateConnectable();
mukesh agrawale1d90e92012-02-15 17:36:08 -0800139 void UpdateFromEndpoints();
Chris Masone95207da2011-06-29 16:50:49 -0700140
Thieu Lef4cbda92011-11-10 23:41:24 +0000141 static void ValidateWEPPassphrase(const std::string &passphrase,
142 Error *error);
143 static void ValidateWPAPassphrase(const std::string &passphrase,
144 Error *error);
145 static void ParseWEPPassphrase(const std::string &passphrase,
146 int *key_index,
147 std::vector<uint8> *password_bytes,
148 Error *error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700149 static bool CheckWEPIsHex(const std::string &passphrase, Error *error);
150 static bool CheckWEPKeyIndex(const std::string &passphrase, Error *error);
151 static bool CheckWEPPrefix(const std::string &passphrase, Error *error);
152
mukesh agrawal8f3f7752012-02-17 19:42:09 -0800153 // Maps a signal value, in dBm, to a "strength" value, from
154 // |Service::kStrengthMin| to |Service:kStrengthMax|.
mukesh agrawale1d90e92012-02-15 17:36:08 -0800155 static uint8 SignalToStrength(int16 signal_dbm);
156
Paul Stewart6ab23a92011-11-09 17:17:47 -0800157 // "wpa", "rsn" and "psk" are equivalent from a configuration perspective.
158 // This function maps them all into "psk".
159 static std::string GetSecurityClass(const std::string &security);
160
Paul Stewartd08f4432011-11-04 07:48:20 -0700161 // Profile data for a WPA/RSN service can be stored under a number of
162 // different names. These functions create different storage identifiers
163 // based on whether they are referred to by their generic "psk" name or
164 // if they use the (legacy) specific "wpa" or "rsn" names.
165 std::string GetGenericStorageIdentifier() const;
166 std::string GetSpecificStorageIdentifier() const;
167 std::string GetStorageIdentifierForSecurity(
168 const std::string &security) const;
169
Gaurav Shah29d68882012-01-30 19:06:42 -0800170 // Returns true if the wireless service uses 802.1x for key management.
171 bool Is8021x() const;
172
Gaurav Shah10109f22011-11-11 20:16:22 -0800173 // Populate the |params| map with available 802.1x EAP properties.
174 void Populate8021xProperties(std::map<std::string, DBus::Variant> *params);
175
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700176 // Properties
177 std::string passphrase_;
178 bool need_passphrase_;
179 std::string security_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700180 // TODO(cmasone): see if the below can be pulled from the endpoint associated
181 // with this service instead.
Chris Masone092df3e2011-08-22 09:41:39 -0700182 const std::string mode_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700183 std::string auth_mode_;
184 bool hidden_ssid_;
185 uint16 frequency_;
Paul Stewart20088d82012-02-16 06:58:55 -0800186 // TODO(quiche): I noticed this is not hooked up to anything. In fact, it
187 // was undefined until now. crosbug.com/26490
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700188 uint16 physical_mode_;
mukesh agrawal32399322011-09-01 10:53:43 -0700189 std::string hex_ssid_;
Paul Stewartd08f4432011-11-04 07:48:20 -0700190 std::string storage_identifier_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700191
mukesh agrawale1d90e92012-02-15 17:36:08 -0800192 // Track whether or not we've warned about large signal values.
193 // Used to avoid spamming the log.
194 static bool logged_signal_warning;
Chris Masone2b105542011-06-22 10:58:09 -0700195 WiFiRefPtr wifi_;
mukesh agrawal261daca2011-12-02 18:56:56 +0000196 std::set<WiFiEndpointConstRefPtr> endpoints_;
mukesh agrawale1d90e92012-02-15 17:36:08 -0800197 WiFiEndpointConstRefPtr current_endpoint_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700198 const std::vector<uint8_t> ssid_;
Paul Stewartecf4cd12012-04-17 11:08:39 -0700199 NSS *nss_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700200 DISALLOW_COPY_AND_ASSIGN(WiFiService);
201};
202
203} // namespace shill
204
205#endif // SHILL_WIFI_SERVICE_