5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/provider.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/dbus.h>
40 #include <connman/ipconfig.h>
44 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
46 static DBusConnection *connection;
52 { "Host", "--remote" },
53 { "OpenVPN.CACert", "--ca" },
54 { "OpenVPN.Cert", "--cert" },
55 { "OpenVPN.Key", "--key" },
56 { "OpenVPN.MTU", "--mtu" },
57 { "OpenVPN.Proto", "--proto" },
58 { "OpenVPN.Port", "--port" },
59 { "OpenVPN.AuthUserPass", "--auth-user-pass" },
60 { "OpenVPN.TLSRemote", "--tls-remote" },
61 { "OpenVPN.Cipher", "--cipher" },
62 { "OpenVPN.Auth", "--auth" },
63 { "OpenVPN.CompLZO", "--comp-lzo" },
64 { "OpenVPN.RemoteCertTls", "--remote-cert-tls" },
67 static void ov_append_dns_entries(const char *key, const char *value,
72 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
75 options = g_strsplit(value, " ", 3);
76 if (options[0] != NULL &&
77 !strcmp(options[0], "dhcp-option") &&
79 !strcmp(options[1], "DNS") &&
82 if (*dns_entries != NULL) {
85 tmp = g_strjoin(" ", *dns_entries,
90 *dns_entries = g_strdup(options[2]);
97 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
99 DBusMessageIter iter, dict;
100 const char *reason, *key, *value;
101 char *nameservers = NULL;
102 char *address = NULL, *gateway = NULL, *peer = NULL;
103 struct connman_ipaddress *ipaddress;
105 dbus_message_iter_init(msg, &iter);
107 dbus_message_iter_get_basic(&iter, &reason);
108 dbus_message_iter_next(&iter);
111 connman_error("No provider found");
112 return VPN_STATE_FAILURE;
115 if (strcmp(reason, "up"))
116 return VPN_STATE_DISCONNECT;
118 dbus_message_iter_recurse(&iter, &dict);
120 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
121 DBusMessageIter entry;
123 dbus_message_iter_recurse(&dict, &entry);
124 dbus_message_iter_get_basic(&entry, &key);
125 dbus_message_iter_next(&entry);
126 dbus_message_iter_get_basic(&entry, &value);
128 DBG("%s = %s", key, value);
130 if (!strcmp(key, "trusted_ip")) {
131 connman_provider_set_string(provider, "Gateway", value);
132 gateway = g_strdup(value);
135 if (!strcmp(key, "ifconfig_local")) {
136 connman_provider_set_string(provider, "Address", value);
137 address = g_strdup(value);
140 if (!strcmp(key, "ifconfig_remote")) {
141 connman_provider_set_string(provider, "Peer", value);
142 peer = g_strdup(value);
145 if (g_str_has_prefix(key, "route_") == TRUE)
146 connman_provider_append_route(provider, key, value);
148 ov_append_dns_entries(key, value, &nameservers);
150 dbus_message_iter_next(&dict);
153 ipaddress = connman_ipaddress_alloc(AF_INET);
154 if (ipaddress == NULL) {
160 return VPN_STATE_FAILURE;
163 connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
164 connman_ipaddress_set_peer(ipaddress, peer);
165 connman_provider_set_ipaddress(provider, ipaddress);
167 connman_provider_set_nameservers(provider, nameservers);
173 connman_ipaddress_free(ipaddress);
175 return VPN_STATE_CONNECT;
178 static int ov_save(struct connman_provider *provider, GKeyFile *keyfile)
183 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
184 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
185 option = connman_provider_get_string(provider,
186 ov_options[i].cm_opt);
190 g_key_file_set_string(keyfile,
191 connman_provider_get_save_group(provider),
192 ov_options[i].cm_opt, option);
198 static int task_append_config_data(struct connman_provider *provider,
199 struct connman_task *task)
204 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
205 option = connman_provider_get_string(provider,
206 ov_options[i].cm_opt);
210 if (connman_task_add_argument(task,
211 ov_options[i].ov_opt, option) < 0) {
219 static int ov_connect(struct connman_provider *provider,
220 struct connman_task *task, const char *if_name)
225 option = connman_provider_get_string(provider, "Host");
226 if (option == NULL) {
227 connman_error("Host not set; cannot enable VPN");
231 task_append_config_data(provider, task);
233 connman_task_add_argument(task, "--syslog", NULL);
235 connman_task_add_argument(task, "--script-security", "2");
237 connman_task_add_argument(task, "--up",
238 SCRIPTDIR "/openvpn-script");
239 connman_task_add_argument(task, "--up-restart", NULL);
241 connman_task_add_argument(task, "--setenv", NULL);
242 connman_task_add_argument(task, "CONNMAN_BUSNAME",
243 dbus_bus_get_unique_name(connection));
245 connman_task_add_argument(task, "--setenv", NULL);
246 connman_task_add_argument(task, "CONNMAN_INTERFACE",
247 CONNMAN_TASK_INTERFACE);
249 connman_task_add_argument(task, "--setenv", NULL);
250 connman_task_add_argument(task, "CONNMAN_PATH",
251 connman_task_get_path(task));
253 connman_task_add_argument(task, "--dev", if_name);
254 connman_task_add_argument(task, "--dev-type", "tun");
256 connman_task_add_argument(task, "--tls-client", NULL);
257 connman_task_add_argument(task, "--nobind", NULL);
258 connman_task_add_argument(task, "--persist-key", NULL);
259 connman_task_add_argument(task, "--persist-tun", NULL);
261 connman_task_add_argument(task, "--route-noexec", NULL);
262 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
265 * Disable client restarts because we can't handle this at the
266 * moment. The problem is that when OpenVPN decides to switch
267 * from CONNECTED state to RECONNECTING and then to RESOLVE,
268 * it is not possible to do a DNS lookup. The DNS server is
269 * not accessable through the tunnel anymore and so we end up
270 * trying to resolve the OpenVPN servers address.
272 connman_task_add_argument(task, "--ping-restart", "0");
274 connman_task_add_argument(task, "--client", NULL);
277 err = connman_task_run(task, vpn_died, provider,
280 connman_error("openvpn failed to start");
287 static struct vpn_driver vpn_driver = {
289 .connect = ov_connect,
293 static int openvpn_init(void)
295 connection = connman_dbus_get_connection();
297 return vpn_register("openvpn", &vpn_driver, OPENVPN);
300 static void openvpn_exit(void)
302 vpn_unregister("openvpn");
304 dbus_connection_unref(connection);
307 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
308 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)