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 | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 33 | class OpenVPNManagementServer; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 34 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 35 | class OpenVPNDriver : public VPNDriver, |
| 36 | public RPCTaskDelegate { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 37 | public: |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 38 | OpenVPNDriver(ControlInterface *control, |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 39 | EventDispatcher *dispatcher, |
| 40 | Metrics *metrics, |
| 41 | Manager *manager, |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 42 | DeviceInfo *device_info, |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 43 | GLib *glib, |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 44 | const KeyValueStore &args); |
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); |
| 64 | void ClearMappedProperty(const size_t &index, Error *error); |
| 65 | std::string GetMappedProperty(const size_t &index, Error *error); |
| 66 | void SetMappedProperty(const size_t &index, |
| 67 | const std::string &value, |
| 68 | Error *error); |
| 69 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 70 | KeyValueStore *args() { return &args_; } |
| 71 | |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 72 | // Returns true if an opton was appended. |
| 73 | bool AppendValueOption(const std::string &property, |
| 74 | const std::string &option, |
| 75 | std::vector<std::string> *options); |
| 76 | |
| 77 | // Returns true if a flag was appended. |
| 78 | bool AppendFlag(const std::string &property, |
| 79 | const std::string &option, |
| 80 | std::vector<std::string> *options); |
| 81 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 82 | private: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 83 | friend class OpenVPNDriverTest; |
| 84 | FRIEND_TEST(OpenVPNDriverTest, AppendFlag); |
| 85 | FRIEND_TEST(OpenVPNDriverTest, AppendValueOption); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 86 | FRIEND_TEST(OpenVPNDriverTest, ClaimInterface); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 87 | FRIEND_TEST(OpenVPNDriverTest, Cleanup); |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 88 | FRIEND_TEST(OpenVPNDriverTest, Connect); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 89 | FRIEND_TEST(OpenVPNDriverTest, ConnectTunnelFailure); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 90 | FRIEND_TEST(OpenVPNDriverTest, Disconnect); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 91 | FRIEND_TEST(OpenVPNDriverTest, GetRouteOptionEntry); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 92 | FRIEND_TEST(OpenVPNDriverTest, InitOptions); |
| 93 | FRIEND_TEST(OpenVPNDriverTest, InitOptionsNoHost); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 94 | FRIEND_TEST(OpenVPNDriverTest, Notify); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 95 | FRIEND_TEST(OpenVPNDriverTest, NotifyFail); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 96 | FRIEND_TEST(OpenVPNDriverTest, OnOpenVPNDied); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 97 | FRIEND_TEST(OpenVPNDriverTest, OnReconnecting); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 98 | FRIEND_TEST(OpenVPNDriverTest, ParseForeignOption); |
| 99 | FRIEND_TEST(OpenVPNDriverTest, ParseForeignOptions); |
| 100 | FRIEND_TEST(OpenVPNDriverTest, ParseIPConfiguration); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 101 | FRIEND_TEST(OpenVPNDriverTest, ParseRouteOption); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 102 | FRIEND_TEST(OpenVPNDriverTest, PinHostRoute); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 103 | FRIEND_TEST(OpenVPNDriverTest, SetRoutes); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 104 | FRIEND_TEST(OpenVPNDriverTest, SpawnOpenVPN); |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 105 | FRIEND_TEST(OpenVPNDriverTest, VerifyPaths); |
| 106 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 107 | struct Property { |
| 108 | const char *property; |
| 109 | bool crypted; |
| 110 | }; |
| 111 | |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 112 | static const char kOpenVPNPath[]; |
| 113 | static const char kOpenVPNScript[]; |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 114 | static const Property kProperties[]; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 115 | |
| 116 | // The map is a sorted container that allows us to iterate through the options |
| 117 | // in order. |
| 118 | typedef std::map<int, std::string> ForeignOptions; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 119 | typedef std::map<int, IPConfig::Route> RouteOptions; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 120 | |
| 121 | static void ParseIPConfiguration( |
| 122 | const std::map<std::string, std::string> &configuration, |
| 123 | IPConfig::Properties *properties); |
| 124 | static void ParseForeignOptions(const ForeignOptions &options, |
| 125 | IPConfig::Properties *properties); |
| 126 | static void ParseForeignOption(const std::string &option, |
| 127 | IPConfig::Properties *properties); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 128 | static IPConfig::Route *GetRouteOptionEntry(const std::string &prefix, |
| 129 | const std::string &key, |
| 130 | RouteOptions *routes); |
| 131 | static void ParseRouteOption(const std::string &key, |
| 132 | const std::string &value, |
| 133 | RouteOptions *routes); |
| 134 | static void SetRoutes(const RouteOptions &routes, |
| 135 | IPConfig::Properties *properties); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 136 | |
| 137 | void InitOptions(std::vector<std::string> *options, Error *error); |
| 138 | |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 139 | bool PinHostRoute(const IPConfig::Properties &properties); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 140 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 141 | bool SpawnOpenVPN(); |
Darin Petkov | 3f9131c | 2012-03-20 11:37:32 +0100 | [diff] [blame] | 142 | void Cleanup(Service::ConnectState state); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 143 | |
| 144 | // Called when the openpvn process exits. |
| 145 | static void OnOpenVPNDied(GPid pid, gint status, gpointer data); |
| 146 | |
| 147 | // Implements RPCTaskDelegate. |
| 148 | virtual void Notify(const std::string &reason, |
| 149 | const std::map<std::string, std::string> &dict); |
| 150 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 151 | ControlInterface *control_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 152 | EventDispatcher *dispatcher_; |
| 153 | Metrics *metrics_; |
| 154 | Manager *manager_; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 155 | DeviceInfo *device_info_; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 156 | GLib *glib_; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 157 | KeyValueStore args_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 158 | Sockets sockets_; |
| 159 | scoped_ptr<OpenVPNManagementServer> management_server_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 160 | |
| 161 | VPNServiceRefPtr service_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 162 | scoped_ptr<RPCTask> rpc_task_; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 163 | std::string tunnel_interface_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 164 | VPNRefPtr device_; |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame^] | 165 | FilePath tls_auth_file_; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 166 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 167 | // The PID of the spawned openvpn process. May be 0 if no process has been |
| 168 | // spawned yet or the process has died. |
| 169 | int pid_; |
| 170 | |
| 171 | // Child exit watch callback source tag. |
| 172 | unsigned int child_watch_tag_; |
| 173 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 174 | DISALLOW_COPY_AND_ASSIGN(OpenVPNDriver); |
| 175 | }; |
| 176 | |
| 177 | } // namespace shill |
| 178 | |
| 179 | #endif // SHILL_OPENVPN_DRIVER_ |