Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 5 | #ifndef SHILL_L2TP_IPSEC_DRIVER_H_ |
| 6 | #define SHILL_L2TP_IPSEC_DRIVER_H_ |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 7 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include <base/file_path.h> |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 11 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 12 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 13 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 14 | #include "shill/glib.h" |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 15 | #include "shill/ipconfig.h" |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 16 | #include "shill/rpc_task.h" |
| 17 | #include "shill/service.h" |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 18 | #include "shill/vpn_driver.h" |
| 19 | |
| 20 | namespace shill { |
| 21 | |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 22 | // Declared in the header to avoid linking unused code into shims. |
| 23 | static const char kL2TPIPSecDNS1[] = "DNS1"; |
| 24 | static const char kL2TPIPSecDNS2[] = "DNS2"; |
| 25 | static const char kL2TPIPSecExternalIP4Address[] = "EXTERNAL_IP4_ADDRESS"; |
| 26 | static const char kL2TPIPSecGatewayAddress[] = "GATEWAY_ADDRESS"; |
| 27 | static const char kL2TPIPSecInterfaceName[] = "INTERNAL_IFNAME"; |
| 28 | static const char kL2TPIPSecInternalIP4Address[] = "INTERNAL_IP4_ADDRESS"; |
| 29 | static const char kL2TPIPSecLNSAddress[] = "LNS_ADDRESS"; |
| 30 | static const char kL2TPIPSecReasonConnect[] = "connect"; |
| 31 | static const char kL2TPIPSecReasonDisconnect[] = "disconnect"; |
| 32 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 33 | class CertificateFile; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 34 | class ControlInterface; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 35 | class DeviceInfo; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 36 | class GLib; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 37 | class Metrics; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 38 | class NSS; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 39 | class ProcessKiller; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 40 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 41 | class L2TPIPSecDriver : public VPNDriver, |
| 42 | public RPCTaskDelegate { |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 43 | public: |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 44 | L2TPIPSecDriver(ControlInterface *control, |
| 45 | EventDispatcher *dispatcher, |
| 46 | Metrics *metrics, |
| 47 | Manager *manager, |
| 48 | DeviceInfo *device_info, |
| 49 | GLib *glib); |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 50 | virtual ~L2TPIPSecDriver(); |
| 51 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 52 | protected: |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 53 | // Inherited from VPNDriver. |
| 54 | virtual bool ClaimInterface(const std::string &link_name, |
| 55 | int interface_index); |
| 56 | virtual void Connect(const VPNServiceRefPtr &service, Error *error); |
| 57 | virtual void Disconnect(); |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 58 | virtual std::string GetProviderType() const; |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 59 | virtual void OnConnectionDisconnected(); |
| 60 | virtual void OnConnectTimeout(); |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | friend class L2TPIPSecDriverTest; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 64 | FRIEND_TEST(L2TPIPSecDriverTest, AppendFlag); |
| 65 | FRIEND_TEST(L2TPIPSecDriverTest, AppendValueOption); |
| 66 | FRIEND_TEST(L2TPIPSecDriverTest, Cleanup); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 67 | FRIEND_TEST(L2TPIPSecDriverTest, Connect); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 68 | FRIEND_TEST(L2TPIPSecDriverTest, DeletePSKFile); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 69 | FRIEND_TEST(L2TPIPSecDriverTest, Disconnect); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 70 | FRIEND_TEST(L2TPIPSecDriverTest, GetLogin); |
| 71 | FRIEND_TEST(L2TPIPSecDriverTest, InitEnvironment); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 72 | FRIEND_TEST(L2TPIPSecDriverTest, InitNSSOptions); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 73 | FRIEND_TEST(L2TPIPSecDriverTest, InitPEMOptions); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 74 | FRIEND_TEST(L2TPIPSecDriverTest, InitOptions); |
| 75 | FRIEND_TEST(L2TPIPSecDriverTest, InitOptionsNoHost); |
| 76 | FRIEND_TEST(L2TPIPSecDriverTest, InitPSKOptions); |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 77 | FRIEND_TEST(L2TPIPSecDriverTest, NotifyDisconnected); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 78 | FRIEND_TEST(L2TPIPSecDriverTest, OnConnectionDisconnected); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 79 | FRIEND_TEST(L2TPIPSecDriverTest, OnL2TPIPSecVPNDied); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 80 | FRIEND_TEST(L2TPIPSecDriverTest, ParseIPConfiguration); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 81 | FRIEND_TEST(L2TPIPSecDriverTest, SpawnL2TPIPSecVPN); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 82 | FRIEND_TEST(L2TPIPSecDriverTest, VerifyPaths); |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 83 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 84 | static const char kPPPDPlugin[]; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 85 | static const char kL2TPIPSecVPNPath[]; |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 86 | static const Property kProperties[]; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 87 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 88 | bool SpawnL2TPIPSecVPN(Error *error); |
| 89 | |
| 90 | void InitEnvironment(std::vector<std::string> *environment); |
| 91 | |
| 92 | bool InitOptions(std::vector<std::string> *options, Error *error); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 93 | bool InitPSKOptions(std::vector<std::string> *options, Error *error); |
| 94 | void InitNSSOptions(std::vector<std::string> *options); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 95 | bool InitPEMOptions(std::vector<std::string> *options); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 96 | |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 97 | // Resets the VPN state and deallocates all resources. If there's a service |
| 98 | // associated through Connect, sets its state to Service::kStateIdle and |
| 99 | // disassociates from the service. |
| 100 | void IdleService(); |
| 101 | |
| 102 | // Resets the VPN state and deallocates all resources. If there's a service |
| 103 | // associated through Connect, sets its state to Service::kStateFailure with |
| 104 | // failure reason |failure| and disassociates from the service. |
| 105 | void FailService(Service::ConnectFailure failure); |
| 106 | |
| 107 | // Implements the IdleService and FailService methods. Resets the VPN state |
| 108 | // and deallocates all resources. If there's a service associated through |
| 109 | // Connect, sets its state |state|; if |state| is Service::kStateFailure, sets |
| 110 | // the failure reason to |failure|; disassociates from the service. |
| 111 | void Cleanup(Service::ConnectState state, Service::ConnectFailure failure); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 112 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 113 | void DeletePSKFile(); |
| 114 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 115 | // Returns true if an opton was appended. |
| 116 | bool AppendValueOption(const std::string &property, |
| 117 | const std::string &option, |
| 118 | std::vector<std::string> *options); |
| 119 | |
| 120 | // Returns true if a flag was appended. |
| 121 | bool AppendFlag(const std::string &property, |
| 122 | const std::string &true_option, |
| 123 | const std::string &false_option, |
| 124 | std::vector<std::string> *options); |
| 125 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 126 | static void ParseIPConfiguration( |
| 127 | const std::map<std::string, std::string> &configuration, |
| 128 | IPConfig::Properties *properties, |
| 129 | std::string *interface_name); |
| 130 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 131 | // Called when the l2tpipsec_vpn process exits. |
| 132 | static void OnL2TPIPSecVPNDied(GPid pid, gint status, gpointer data); |
| 133 | |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 134 | static Service::ConnectFailure TranslateExitStatusToFailure(int status); |
| 135 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 136 | // Inherit from VPNDriver to add custom properties. |
| 137 | virtual KeyValueStore GetProvider(Error *error); |
| 138 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 139 | // Implements RPCTaskDelegate. |
| 140 | virtual void GetLogin(std::string *user, std::string *password); |
| 141 | virtual void Notify(const std::string &reason, |
| 142 | const std::map<std::string, std::string> &dict); |
| 143 | |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 144 | static void DeleteRPCTask(RPCTask *rpc_task); |
| 145 | |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 146 | void ReportConnectionMetrics(); |
| 147 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 148 | ControlInterface *control_; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 149 | Metrics *metrics_; |
| 150 | DeviceInfo *device_info_; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 151 | GLib *glib_; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 152 | NSS *nss_; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 153 | ProcessKiller *process_killer_; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 154 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 155 | VPNServiceRefPtr service_; |
| 156 | scoped_ptr<RPCTask> rpc_task_; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 157 | base::FilePath psk_file_; |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 158 | scoped_ptr<CertificateFile> certificate_file_; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 159 | VPNRefPtr device_; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 160 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 161 | // The PID of the spawned l2tpipsec_vpn process. May be 0 if no process has |
| 162 | // been spawned yet or the process has died. |
| 163 | int pid_; |
| 164 | |
| 165 | // Child exit watch callback source tag. |
| 166 | unsigned int child_watch_tag_; |
| 167 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 168 | DISALLOW_COPY_AND_ASSIGN(L2TPIPSecDriver); |
| 169 | }; |
| 170 | |
| 171 | } // namespace shill |
| 172 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 173 | #endif // SHILL_L2TP_IPSEC_DRIVER_H_ |