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