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>
43 static DBusConnection *connection;
45 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
47 DBusMessageIter iter, dict;
48 const char *reason, *key, *value;
49 const char *domain = NULL;
50 char *dns_entries = NULL;
52 dbus_message_iter_init(msg, &iter);
54 dbus_message_iter_get_basic(&iter, &reason);
55 dbus_message_iter_next(&iter);
57 dbus_message_iter_init(msg, &iter);
59 dbus_message_iter_get_basic(&iter, &reason);
60 dbus_message_iter_next(&iter);
63 connman_error("No provider found");
64 return VPN_STATE_FAILURE;
67 if (strcmp(reason, "up"))
68 return VPN_STATE_DISCONNECT;
70 domain = connman_provider_get_string(provider, "VPN.Domain");
72 dbus_message_iter_recurse(&iter, &dict);
74 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
75 DBusMessageIter entry;
77 dbus_message_iter_recurse(&dict, &entry);
78 dbus_message_iter_get_basic(&entry, &key);
79 dbus_message_iter_next(&entry);
80 dbus_message_iter_get_basic(&entry, &value);
82 DBG("%s = %s", key, value);
84 if (!strcmp(key, "trusted_ip"))
85 connman_provider_set_string(provider, "Gateway", value);
87 if (!strcmp(key, "ifconfig_local"))
88 connman_provider_set_string(provider, "Address", value);
90 if (!strcmp(key, "route_vpn_gateway"))
91 connman_provider_set_string(provider, "Peer", value);
93 if (g_str_has_prefix(key, "foreign_option_")) {
96 options = g_strsplit(value, " ", 3);
97 if (options[0] != NULL &&
98 !strcmp(options[0], "dhcp-option") &&
100 !strcmp(options[1], "DNS") &&
101 options[2] != NULL) {
103 if (dns_entries != NULL) {
106 tmp = g_strjoin(" ", dns_entries,
111 dns_entries = g_strdup(options[2]);
118 dbus_message_iter_next(&dict);
121 if (dns_entries != NULL) {
122 connman_provider_set_string(provider, "DNS", dns_entries);
126 return VPN_STATE_CONNECT;
129 static int ov_connect(struct connman_provider *provider,
130 struct connman_task *task, const char *if_name)
132 const char *vpnhost, *cafile, *mtu, *certfile, *keyfile;
135 vpnhost = connman_provider_get_string(provider, "Host");
137 connman_error("Host not set; cannot enable VPN");
141 cafile = connman_provider_get_string(provider, "OpenVPN.CACert");
142 certfile = connman_provider_get_string(provider, "OpenVPN.Cert");
143 keyfile = connman_provider_get_string(provider, "OpenVPN.Key");
144 mtu = connman_provider_get_string(provider, "VPN.MTU");
147 connman_task_add_argument(task, "--mtu", (char *)mtu);
149 connman_task_add_argument(task, "--syslog", NULL);
151 connman_task_add_argument(task, "--script-security", "2");
153 connman_task_add_argument(task, "--up",
154 SCRIPTDIR "/openvpn-script");
155 connman_task_add_argument(task, "--up-restart", NULL);
157 connman_task_add_argument(task, "--setenv", NULL);
158 connman_task_add_argument(task, "CONNMAN_BUSNAME",
159 dbus_bus_get_unique_name(connection));
161 connman_task_add_argument(task, "--setenv", NULL);
162 connman_task_add_argument(task, "CONNMAN_INTERFACE",
163 CONNMAN_TASK_INTERFACE);
165 connman_task_add_argument(task, "--setenv", NULL);
166 connman_task_add_argument(task, "CONNMAN_PATH",
167 connman_task_get_path(task));
169 connman_task_add_argument(task, "--dev", if_name);
170 connman_task_add_argument(task, "--dev-type", "tun");
172 connman_task_add_argument(task, "--tls-client", NULL);
173 connman_task_add_argument(task, "--remote", (char *)vpnhost);
174 connman_task_add_argument(task, "--nobind", NULL);
175 connman_task_add_argument(task, "--persist-key", NULL);
176 connman_task_add_argument(task, "--persist-tun", NULL);
178 connman_task_add_argument(task, "--route-noexec", NULL);
179 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
182 * Disable client restarts because we can't handle this at the
183 * moment. The problem is that when OpenVPN decides to switch
184 * from CONNECTED state to RECONNECTING and then to RESOLVE,
185 * it is not possible to do a DNS lookup. The DNS server is
186 * not accessable through the tunnel anymore and so we end up
187 * trying to resolve the OpenVPN servers address.
189 connman_task_add_argument(task, "--ping-restart", "0");
191 connman_task_add_argument(task, "--client", NULL);
194 connman_task_add_argument(task, "--ca",
199 connman_task_add_argument(task, "--cert",
204 connman_task_add_argument(task, "--key",
209 err = connman_task_run(task, vpn_died, provider,
212 connman_error("openvpn failed to start");
219 static struct vpn_driver vpn_driver = {
221 .connect = ov_connect,
224 static int openvpn_init(void)
226 connection = connman_dbus_get_connection();
228 return vpn_register("openvpn", &vpn_driver, OPENVPN);
231 static void openvpn_exit(void)
233 vpn_unregister("openvpn");
235 dbus_connection_unref(connection);
238 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
239 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)