Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [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 | |
| 5 | #ifndef SHILL_OPENVPN_DRIVER_ |
| 6 | #define SHILL_OPENVPN_DRIVER_ |
| 7 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 8 | #include <map> |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 12 | #include <base/file_path.h> |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 15 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 16 | #include "shill/glib.h" |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 17 | #include "shill/ipconfig.h" |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 18 | #include "shill/key_value_store.h" |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 19 | #include "shill/refptr_types.h" |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 20 | #include "shill/rpc_task.h" |
Darin Petkov | 3f9131c | 2012-03-20 11:37:32 +0100 | [diff] [blame] | 21 | #include "shill/service.h" |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 22 | #include "shill/sockets.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 23 | #include "shill/vpn_driver.h" |
| 24 | |
| 25 | namespace shill { |
| 26 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 27 | class ControlInterface; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 28 | class DeviceInfo; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 29 | class Error; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 30 | class EventDispatcher; |
| 31 | class Manager; |
| 32 | class Metrics; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 33 | class NSS; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 34 | class OpenVPNManagementServer; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 35 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 36 | class OpenVPNDriver : public VPNDriver, |
| 37 | public RPCTaskDelegate { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 38 | public: |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 39 | OpenVPNDriver(ControlInterface *control, |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 40 | EventDispatcher *dispatcher, |
| 41 | Metrics *metrics, |
| 42 | Manager *manager, |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 43 | DeviceInfo *device_info, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 44 | GLib *glib); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 45 | virtual ~OpenVPNDriver(); |
| 46 | |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 47 | // Inherited from VPNDriver. |Connect| initiates the VPN connection by |
| 48 | // creating a tunnel device. When the device index becomes available, this |
| 49 | // instance is notified through |ClaimInterface| and resumes the connection |
| 50 | // process by setting up and spawning an external 'openvpn' process. IP |
| 51 | // configuration settings are passed back from the external process through |
| 52 | // the |Notify| RPC service method. |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 53 | virtual void Connect(const VPNServiceRefPtr &service, Error *error); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 54 | virtual bool ClaimInterface(const std::string &link_name, |
| 55 | int interface_index); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 56 | virtual void Disconnect(); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 57 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 58 | virtual bool Load(StoreInterface *storage, const std::string &storage_id); |
| 59 | virtual bool Save(StoreInterface *storage, const std::string &storage_id); |
| 60 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 61 | virtual void OnReconnecting(); |
| 62 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 63 | virtual void InitPropertyStore(PropertyStore *store); |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 64 | |
| 65 | virtual std::string GetProviderType() const; |
| 66 | |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame^] | 67 | virtual void Cleanup(Service::ConnectState state); |
| 68 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 69 | void ClearMappedProperty(const size_t &index, Error *error); |
| 70 | std::string GetMappedProperty(const size_t &index, Error *error); |
| 71 | void SetMappedProperty(const size_t &index, |
| 72 | const std::string &value, |
| 73 | Error *error); |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 74 | Stringmap GetProvider(Error *error); |
| 75 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 76 | KeyValueStore *args() { return &args_; } |
| 77 | |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 78 | // Returns true if an opton was appended. |
| 79 | bool AppendValueOption(const std::string &property, |
| 80 | const std::string &option, |
| 81 | std::vector<std::string> *options); |
| 82 | |
| 83 | // Returns true if a flag was appended. |
| 84 | bool AppendFlag(const std::string &property, |
| 85 | const std::string &option, |
| 86 | std::vector<std::string> *options); |
| 87 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 88 | private: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 89 | friend class OpenVPNDriverTest; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 90 | FRIEND_TEST(OpenVPNDriverTest, ClaimInterface); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 91 | FRIEND_TEST(OpenVPNDriverTest, Cleanup); |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 92 | FRIEND_TEST(OpenVPNDriverTest, Connect); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 93 | FRIEND_TEST(OpenVPNDriverTest, ConnectTunnelFailure); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 94 | FRIEND_TEST(OpenVPNDriverTest, Disconnect); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 95 | FRIEND_TEST(OpenVPNDriverTest, GetRouteOptionEntry); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 96 | FRIEND_TEST(OpenVPNDriverTest, InitManagementChannelOptions); |
| 97 | FRIEND_TEST(OpenVPNDriverTest, InitNSSOptions); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 98 | FRIEND_TEST(OpenVPNDriverTest, InitOptions); |
| 99 | FRIEND_TEST(OpenVPNDriverTest, InitOptionsNoHost); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 100 | FRIEND_TEST(OpenVPNDriverTest, InitPKCS11Options); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 101 | FRIEND_TEST(OpenVPNDriverTest, Notify); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 102 | FRIEND_TEST(OpenVPNDriverTest, NotifyFail); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 103 | FRIEND_TEST(OpenVPNDriverTest, OnOpenVPNDied); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 104 | FRIEND_TEST(OpenVPNDriverTest, OnReconnecting); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 105 | FRIEND_TEST(OpenVPNDriverTest, ParseForeignOption); |
| 106 | FRIEND_TEST(OpenVPNDriverTest, ParseForeignOptions); |
| 107 | FRIEND_TEST(OpenVPNDriverTest, ParseIPConfiguration); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 108 | FRIEND_TEST(OpenVPNDriverTest, ParseRouteOption); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 109 | FRIEND_TEST(OpenVPNDriverTest, PinHostRoute); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 110 | FRIEND_TEST(OpenVPNDriverTest, SetRoutes); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 111 | FRIEND_TEST(OpenVPNDriverTest, SpawnOpenVPN); |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 112 | FRIEND_TEST(OpenVPNDriverTest, VerifyPaths); |
| 113 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 114 | struct Property { |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 115 | enum Flags { |
| 116 | kEphemeral = 1 << 0, |
| 117 | kCrypted = 1 << 1, |
| 118 | }; |
| 119 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 120 | const char *property; |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 121 | int flags; |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 124 | static const char kOpenVPNPath[]; |
| 125 | static const char kOpenVPNScript[]; |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 126 | static const Property kProperties[]; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 127 | |
| 128 | // The map is a sorted container that allows us to iterate through the options |
| 129 | // in order. |
| 130 | typedef std::map<int, std::string> ForeignOptions; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 131 | typedef std::map<int, IPConfig::Route> RouteOptions; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 132 | |
| 133 | static void ParseIPConfiguration( |
| 134 | const std::map<std::string, std::string> &configuration, |
| 135 | IPConfig::Properties *properties); |
| 136 | static void ParseForeignOptions(const ForeignOptions &options, |
| 137 | IPConfig::Properties *properties); |
| 138 | static void ParseForeignOption(const std::string &option, |
| 139 | IPConfig::Properties *properties); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 140 | static IPConfig::Route *GetRouteOptionEntry(const std::string &prefix, |
| 141 | const std::string &key, |
| 142 | RouteOptions *routes); |
| 143 | static void ParseRouteOption(const std::string &key, |
| 144 | const std::string &value, |
| 145 | RouteOptions *routes); |
| 146 | static void SetRoutes(const RouteOptions &routes, |
| 147 | IPConfig::Properties *properties); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 148 | |
| 149 | void InitOptions(std::vector<std::string> *options, Error *error); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 150 | bool InitNSSOptions(std::vector<std::string> *options, Error *error); |
| 151 | void InitPKCS11Options(std::vector<std::string> *options); |
| 152 | bool InitManagementChannelOptions( |
| 153 | std::vector<std::string> *options, Error *error); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 154 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 155 | bool PinHostRoute(const IPConfig::Properties &properties); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 156 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 157 | bool SpawnOpenVPN(); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 158 | |
| 159 | // Called when the openpvn process exits. |
| 160 | static void OnOpenVPNDied(GPid pid, gint status, gpointer data); |
| 161 | |
| 162 | // Implements RPCTaskDelegate. |
| 163 | virtual void Notify(const std::string &reason, |
| 164 | const std::map<std::string, std::string> &dict); |
| 165 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 166 | ControlInterface *control_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 167 | EventDispatcher *dispatcher_; |
| 168 | Metrics *metrics_; |
| 169 | Manager *manager_; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 170 | DeviceInfo *device_info_; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 171 | GLib *glib_; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 172 | KeyValueStore args_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 173 | Sockets sockets_; |
| 174 | scoped_ptr<OpenVPNManagementServer> management_server_; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 175 | NSS *nss_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 176 | |
| 177 | VPNServiceRefPtr service_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 178 | scoped_ptr<RPCTask> rpc_task_; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 179 | std::string tunnel_interface_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 180 | VPNRefPtr device_; |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 181 | FilePath tls_auth_file_; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 182 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 183 | // The PID of the spawned openvpn process. May be 0 if no process has been |
| 184 | // spawned yet or the process has died. |
| 185 | int pid_; |
| 186 | |
| 187 | // Child exit watch callback source tag. |
| 188 | unsigned int child_watch_tag_; |
| 189 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 190 | DISALLOW_COPY_AND_ASSIGN(OpenVPNDriver); |
| 191 | }; |
| 192 | |
| 193 | } // namespace shill |
| 194 | |
| 195 | #endif // SHILL_OPENVPN_DRIVER_ |