1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_ETHERNET_ETHERNET_H_
18 #define SHILL_ETHERNET_ETHERNET_H_
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include <base/cancelable_callback.h>
25 #include <base/memory/weak_ptr.h>
26 
27 #include "shill/certificate_file.h"
28 #include "shill/device.h"
29 #include "shill/event_dispatcher.h"
30 #include "shill/refptr_types.h"
31 
32 #if !defined(DISABLE_WIRED_8021X)
33 #include "shill/key_value_store.h"
34 #include "shill/supplicant/supplicant_eap_state_handler.h"
35 #include "shill/supplicant/supplicant_event_delegate_interface.h"
36 #endif  // DISABLE_WIRED_8021X
37 
38 namespace shill {
39 
40 class Sockets;
41 class StoreInterface;
42 
43 #if !defined(DISABLE_WIRED_8021X)
44 class CertificateFile;
45 class EapListener;
46 class EthernetEapProvider;
47 class SupplicantEAPStateHandler;
48 class SupplicantInterfaceProxyInterface;
49 class SupplicantProcessProxyInterface;
50 #endif  // DISABLE_WIRED_8021X
51 
52 class Ethernet
53 #if !defined(DISABLE_WIRED_8021X)
54     : public Device, public SupplicantEventDelegateInterface {
55 #else
56     : public Device {
57 #endif  // DISABLE_WIRED_8021X
58  public:
59   Ethernet(ControlInterface* control_interface,
60            EventDispatcher* dispatcher,
61            Metrics* metrics,
62            Manager* manager,
63            const std::string& link_name,
64            const std::string& address,
65            int interface_index);
66   ~Ethernet() override;
67 
68   void Start(Error* error,
69              const EnabledStateChangedCallback& callback) override;
70   void Stop(Error* error, const EnabledStateChangedCallback& callback) override;
71   void LinkEvent(unsigned int flags, unsigned int change) override;
72   bool Load(StoreInterface* storage) override;
73   bool Save(StoreInterface* storage) override;
74 
75   virtual void ConnectTo(EthernetService* service);
76   virtual void DisconnectFrom(EthernetService* service);
77 
78 #if !defined(DISABLE_WIRED_8021X)
79   // Test to see if conditions are correct for EAP authentication (both
80   // credentials and a remote EAP authenticator is present) and initiate
81   // an authentication if possible.
82   virtual void TryEapAuthentication();
83 
84   // Implementation of SupplicantEventDelegateInterface.  These methods
85   // are called by SupplicantInterfaceProxy, in response to events from
86   // wpa_supplicant.
87   void BSSAdded(
88       const std::string& BSS,
89       const KeyValueStore& properties) override;
90   void BSSRemoved(const std::string& BSS) override;
91   void Certification(const KeyValueStore& properties) override;
92   void EAPEvent(const std::string& status,
93                 const std::string& parameter) override;
94   void PropertiesChanged(const KeyValueStore& properties) override;
95   void ScanDone(const bool& /*success*/) override;
96   void TDLSDiscoverResponse(const std::string& peer_address) override;
97 #endif  // DISABLE_WIRED_8021X
98 
link_up()99   virtual bool link_up() const { return link_up_; }
100 
101  private:
102   friend class EthernetTest;
103   friend class EthernetServiceTest;  // For weak_ptr_factory_.
104   friend class PPPoEServiceTest;     // For weak_ptr_factory_.
105 
106 #if !defined(DISABLE_WIRED_8021X)
107   // Return a pointer to the EAP provider for Ethernet devices.
108   EthernetEapProvider* GetEapProvider();
109 
110   // Return a reference to the shared service that contains EAP credentials
111   // for Ethernet.
112   ServiceConstRefPtr GetEapService();
113 
114   // Invoked by |eap_listener_| when an EAP authenticator is detected.
115   void OnEapDetected();
116 
117   // Start and stop a supplicant instance on this link.
118   bool StartSupplicant();
119   void StopSupplicant();
120 
121   // Start the EAP authentication process.
122   bool StartEapAuthentication();
123 
124   // Change our EAP authentication state.
125   void SetIsEapAuthenticated(bool is_eap_authenticated);
126 
127   // Callback tasks run as a result of event delegate methods.
128   void CertificationTask(const std::string& subject, uint32_t depth);
129   void EAPEventTask(const std::string& status, const std::string& parameter);
130   void SupplicantStateChangedTask(const std::string& state);
131 
132   // Callback task run as a result of TryEapAuthentication().
133   void TryEapAuthenticationTask();
134 #endif  // DISABLE_WIRED_8021X
135 
136   // Accessors for the PPoE property.
137   bool GetPPPoEMode(Error* error);
138   bool ConfigurePPPoEMode(const bool& mode, Error* error);
139   void ClearPPPoEMode(Error* error);
140 
141   // Helpers for creating services with |this| as their device.
142   EthernetServiceRefPtr CreateEthernetService();
143   EthernetServiceRefPtr CreatePPPoEService();
144 
145   void SetupWakeOnLan();
146 
147   ControlInterface* control_interface_;
148 
149   EthernetServiceRefPtr service_;
150   bool link_up_;
151 
152 #if !defined(DISABLE_WIRED_8021X)
153   // Track whether we have completed EAP authentication successfully.
154   bool is_eap_authenticated_;
155 
156   // Track whether an EAP authenticator has been detected on this link.
157   bool is_eap_detected_;
158   std::unique_ptr<EapListener> eap_listener_;
159 
160   // Track the progress of EAP authentication.
161   SupplicantEAPStateHandler eap_state_handler_;
162 
163   // Proxy instances used to talk to wpa_supplicant.
164   std::unique_ptr<SupplicantProcessProxyInterface> supplicant_process_proxy_;
165   std::unique_ptr<SupplicantInterfaceProxyInterface>
166       supplicant_interface_proxy_;
167   std::string supplicant_interface_path_;
168   std::string supplicant_network_path_;
169 
170   // Certificate file instance to generate public key data for remote
171   // authentication.
172   CertificateFile certificate_file_;
173 
174   // Make sure TryEapAuthenticationTask is only queued for execution once
175   // at a time.
176   base::CancelableClosure try_eap_authentication_callback_;
177 #endif  // DISABLE_WIRED_8021X
178 
179   std::unique_ptr<Sockets> sockets_;
180 
181   base::WeakPtrFactory<Ethernet> weak_ptr_factory_;
182 
183   DISALLOW_COPY_AND_ASSIGN(Ethernet);
184 };
185 
186 }  // namespace shill
187 
188 #endif  // SHILL_ETHERNET_ETHERNET_H_
189