blob: ca28e10ea6e0be399bc18642092150d0f1cb1b87 [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;
Chris Masone6791a432011-07-12 13:23:19 -070024
mukesh agrawalb54601c2011-06-07 17:39:22 -070025class WiFiService : public Service {
26 public:
Paul Stewart0756db92012-01-27 08:34:47 -080027 // 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 agrawalb54601c2011-06-07 17:39:22 -070035 WiFiService(ControlInterface *control_interface,
36 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080037 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070038 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070039 const WiFiRefPtr &device,
mukesh agrawal7ec71312011-11-10 02:08:26 +000040 const std::vector<uint8_t> &ssid,
Chris Masone092df3e2011-08-22 09:41:39 -070041 const std::string &mode,
Paul Stewartced6a0b2011-11-08 15:32:04 -080042 const std::string &security,
43 bool hidden_ssid);
mukesh agrawalb54601c2011-06-07 17:39:22 -070044 ~WiFiService();
Darin Petkov4d6d9412011-08-24 13:19:54 -070045
46 // Inherited from Service.
mukesh agrawal8a3188d2011-12-01 20:56:44 +000047 virtual void AutoConnect();
Darin Petkov4d6d9412011-08-24 13:19:54 -070048 virtual void Connect(Error *error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +000049 virtual void Disconnect(Error *error);
Chris Masone34af2182011-08-22 11:59:36 -070050
Paul Stewart22aa71b2011-09-16 12:15:11 -070051 virtual bool TechnologyIs(const Technology::Identifier type) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +000052 virtual bool IsConnecting() const;
Paul Stewart22aa71b2011-09-16 12:15:11 -070053
mukesh agrawal261daca2011-12-02 18:56:56 +000054 virtual void AddEndpoint(WiFiEndpointConstRefPtr endpoint);
55 virtual void RemoveEndpoint(WiFiEndpointConstRefPtr endpoint);
mukesh agrawal4eb4d782011-12-05 17:34:37 +000056 bool NumEndpoints() const { return endpoints_.size(); }
mukesh agrawal261daca2011-12-02 18:56:56 +000057
Chris Masone34af2182011-08-22 11:59:36 -070058 // wifi_<MAC>_<BSSID>_<mode_string>_<security_string>
Chris Masone6515aab2011-10-12 16:19:09 -070059 std::string GetStorageIdentifier() const;
Paul Stewarta41e38d2011-11-11 07:47:29 -080060 static bool ParseStorageIdentifier(const std::string &storage_name,
61 std::string *address,
62 std::string *mode,
63 std::string *security);
Chris Masone34af2182011-08-22 11:59:36 -070064
Thieu Le48e6d6d2011-12-06 00:40:27 +000065 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 agrawalb54601c2011-06-07 17:39:22 -070068
mukesh agrawal1a056262011-10-05 14:36:54 -070069 void SetPassphrase(const std::string &passphrase, Error *error);
70
Paul Stewartd08f4432011-11-04 07:48:20 -070071 // 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 Stewartd8ad3c42012-01-09 12:39:38 -080076 virtual void Unload();
Paul Stewartd08f4432011-11-04 07:48:20 -070077
mukesh agrawal8a3188d2011-12-01 20:56:44 +000078 virtual bool HasEndpoints() const { return !endpoints_.empty(); }
Paul Stewarta41e38d2011-11-11 07:47:29 -080079 virtual bool IsVisible() const;
Paul Stewart6ab23a92011-11-09 17:17:47 -080080 bool IsSecurityMatch(const std::string &security) const;
Paul Stewartced6a0b2011-11-08 15:32:04 -080081 bool hidden_ssid() const { return hidden_ssid_; }
82
Thieu Le48e6d6d2011-12-06 00:40:27 +000083 virtual void InitializeCustomMetrics() const;
84 virtual void SendPostReadyStateMetrics() const;
85
Gaurav Shah10109f22011-11-11 20:16:22 -080086 // 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 agrawal8a3188d2011-12-01 20:56:44 +000090 protected:
91 virtual bool IsAutoConnectable() const;
92
mukesh agrawalb54601c2011-06-07 17:39:22 -070093 private:
Paul Stewartd08f4432011-11-04 07:48:20 -070094 friend class WiFiServiceSecurityTest;
Thieu Lead1ec2c2012-01-05 23:39:48 +000095 FRIEND_TEST(MetricsTest, WiFiServicePostReady);
mukesh agrawal8a3188d2011-12-01 20:56:44 +000096 FRIEND_TEST(WiFiServiceTest, AutoConnect);
Gaurav Shah10109f22011-11-11 20:16:22 -080097 FRIEND_TEST(WiFiServiceTest, ConnectTask8021x);
Gaurav Shah29d68882012-01-30 19:06:42 -080098 FRIEND_TEST(WiFiServiceTest, ConnectTaskDynamicWEP);
Gaurav Shahf8721ee2011-11-07 09:12:46 -080099 FRIEND_TEST(WiFiServiceTest, ConnectTaskPSK);
Gaurav Shah29d68882012-01-30 19:06:42 -0800100 FRIEND_TEST(WiFiServiceTest, ConnectTaskRSN);
Thieu Lef4cbda92011-11-10 23:41:24 +0000101 FRIEND_TEST(WiFiServiceTest, ConnectTaskWEP);
Gaurav Shah29d68882012-01-30 19:06:42 -0800102 FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000103 FRIEND_TEST(WiFiServiceTest, IsAutoConnectable);
Paul Stewartd08f4432011-11-04 07:48:20 -0700104 FRIEND_TEST(WiFiServiceTest, LoadHidden);
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800105 FRIEND_TEST(WiFiServiceTest, LoadAndUnloadPassphrase);
Gaurav Shah10109f22011-11-11 20:16:22 -0800106 FRIEND_TEST(WiFiServiceTest, Populate8021x);
Paul Stewartd08f4432011-11-04 07:48:20 -0700107
Thieu Lef7709452011-11-15 01:13:19 +0000108 void HelpRegisterDerivedString(
109 PropertyStore *store,
110 const std::string &name,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800111 std::string(WiFiService::*get)(Error *error),
112 void(WiFiService::*set)(const std::string &value, Error *error));
Thieu Lef7709452011-11-15 01:13:19 +0000113
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700114 void ConnectTask();
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000115 void DisconnectTask();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700116
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800117 std::string GetDeviceRpcId(Error *error);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000118 void UpdateConnectable();
Chris Masone95207da2011-06-29 16:50:49 -0700119
Thieu Lef4cbda92011-11-10 23:41:24 +0000120 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 agrawal1a056262011-10-05 14:36:54 -0700128 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 agrawald835b202011-10-07 15:26:47 -0700132 // replace non-ASCII characters with '?'. return true if one or more
133 // characters were changed
134 static bool SanitizeSSID(std::string *ssid);
135
Paul Stewart6ab23a92011-11-09 17:17:47 -0800136 // "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 Stewartd08f4432011-11-04 07:48:20 -0700140 // 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 Shah29d68882012-01-30 19:06:42 -0800149 // Returns true if the wireless service uses 802.1x for key management.
150 bool Is8021x() const;
151
Gaurav Shah10109f22011-11-11 20:16:22 -0800152 // Populate the |params| map with available 802.1x EAP properties.
153 void Populate8021xProperties(std::map<std::string, DBus::Variant> *params);
154
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700155 // Properties
156 std::string passphrase_;
157 bool need_passphrase_;
158 std::string security_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700159 // TODO(cmasone): see if the below can be pulled from the endpoint associated
160 // with this service instead.
Chris Masone092df3e2011-08-22 09:41:39 -0700161 const std::string mode_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700162 std::string auth_mode_;
163 bool hidden_ssid_;
164 uint16 frequency_;
165 uint16 physical_mode_;
mukesh agrawal32399322011-09-01 10:53:43 -0700166 std::string hex_ssid_;
Paul Stewartd08f4432011-11-04 07:48:20 -0700167 std::string storage_identifier_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700168
mukesh agrawalb54601c2011-06-07 17:39:22 -0700169 ScopedRunnableMethodFactory<WiFiService> task_factory_;
Chris Masone2b105542011-06-22 10:58:09 -0700170 WiFiRefPtr wifi_;
mukesh agrawal261daca2011-12-02 18:56:56 +0000171 std::set<WiFiEndpointConstRefPtr> endpoints_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700172 const std::vector<uint8_t> ssid_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700173 DISALLOW_COPY_AND_ASSIGN(WiFiService);
174};
175
176} // namespace shill
177
178#endif // SHILL_WIFI_SERVICE_