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" },
66 static void ov_append_dns_entries(const char *key, const char *value,
71 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
74 options = g_strsplit(value, " ", 3);
75 if (options[0] != NULL &&
76 !strcmp(options[0], "dhcp-option") &&
78 !strcmp(options[1], "DNS") &&
81 if (*dns_entries != NULL) {
84 tmp = g_strjoin(" ", *dns_entries,
89 *dns_entries = g_strdup(options[2]);
96 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
98 DBusMessageIter iter, dict;
99 const char *reason, *key, *value;
100 const char *domain = NULL;
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 domain = connman_provider_get_string(provider, "VPN.Domain");
120 dbus_message_iter_recurse(&iter, &dict);
122 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
123 DBusMessageIter entry;
125 dbus_message_iter_recurse(&dict, &entry);
126 dbus_message_iter_get_basic(&entry, &key);
127 dbus_message_iter_next(&entry);
128 dbus_message_iter_get_basic(&entry, &value);
130 DBG("%s = %s", key, value);
132 if (!strcmp(key, "trusted_ip")) {
133 connman_provider_set_string(provider, "Gateway", value);
134 gateway = g_strdup(value);
137 if (!strcmp(key, "ifconfig_local")) {
138 connman_provider_set_string(provider, "Address", value);
139 address = g_strdup(value);
142 if (!strcmp(key, "ifconfig_remote")) {
143 connman_provider_set_string(provider, "Peer", value);
144 peer = g_strdup(value);
147 if (g_str_has_prefix(key, "route_") == TRUE)
148 connman_provider_append_route(provider, key, value);
150 ov_append_dns_entries(key, value, &nameservers);
152 dbus_message_iter_next(&dict);
155 ipaddress = connman_ipaddress_alloc(AF_INET);
156 if (ipaddress == NULL) {
162 return VPN_STATE_FAILURE;
165 connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
166 connman_ipaddress_set_peer(ipaddress, peer);
167 connman_provider_set_ipaddress(provider, ipaddress);
169 connman_provider_set_nameservers(provider, nameservers);
175 connman_ipaddress_free(ipaddress);
177 return VPN_STATE_CONNECT;
180 static int task_append_config_data(struct connman_provider *provider,
181 struct connman_task *task)
186 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
187 option = connman_provider_get_string(provider,
188 ov_options[i].cm_opt);
192 if (connman_task_add_argument(task,
193 ov_options[i].ov_opt, option) < 0) {
201 static int ov_connect(struct connman_provider *provider,
202 struct connman_task *task, const char *if_name)
207 option = connman_provider_get_string(provider, "Host");
208 if (option == NULL) {
209 connman_error("Host not set; cannot enable VPN");
213 task_append_config_data(provider, task);
215 connman_task_add_argument(task, "--syslog", NULL);
217 connman_task_add_argument(task, "--script-security", "2");
219 connman_task_add_argument(task, "--up",
220 SCRIPTDIR "/openvpn-script");
221 connman_task_add_argument(task, "--up-restart", NULL);
223 connman_task_add_argument(task, "--setenv", NULL);
224 connman_task_add_argument(task, "CONNMAN_BUSNAME",
225 dbus_bus_get_unique_name(connection));
227 connman_task_add_argument(task, "--setenv", NULL);
228 connman_task_add_argument(task, "CONNMAN_INTERFACE",
229 CONNMAN_TASK_INTERFACE);
231 connman_task_add_argument(task, "--setenv", NULL);
232 connman_task_add_argument(task, "CONNMAN_PATH",
233 connman_task_get_path(task));
235 connman_task_add_argument(task, "--dev", if_name);
236 connman_task_add_argument(task, "--dev-type", "tun");
238 connman_task_add_argument(task, "--tls-client", NULL);
239 connman_task_add_argument(task, "--nobind", NULL);
240 connman_task_add_argument(task, "--persist-key", NULL);
241 connman_task_add_argument(task, "--persist-tun", NULL);
243 connman_task_add_argument(task, "--route-noexec", NULL);
244 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
247 * Disable client restarts because we can't handle this at the
248 * moment. The problem is that when OpenVPN decides to switch
249 * from CONNECTED state to RECONNECTING and then to RESOLVE,
250 * it is not possible to do a DNS lookup. The DNS server is
251 * not accessable through the tunnel anymore and so we end up
252 * trying to resolve the OpenVPN servers address.
254 connman_task_add_argument(task, "--ping-restart", "0");
256 connman_task_add_argument(task, "--client", NULL);
259 err = connman_task_run(task, vpn_died, provider,
262 connman_error("openvpn failed to start");
269 static struct vpn_driver vpn_driver = {
271 .connect = ov_connect,
274 static int openvpn_init(void)
276 connection = connman_dbus_get_connection();
278 return vpn_register("openvpn", &vpn_driver, OPENVPN);
281 static void openvpn_exit(void)
283 vpn_unregister("openvpn");
285 dbus_connection_unref(connection);
288 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
289 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)