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;
53 { "Host", "--remote", 1 },
54 { "OpenVPN.CACert", "--ca", 1 },
55 { "OpenVPN.Cert", "--cert", 1 },
56 { "OpenVPN.Key", "--key", 1 },
57 { "OpenVPN.MTU", "--mtu", 1 },
58 { "OpenVPN.Proto", "--proto", 1 },
59 { "OpenVPN.Port", "--port", 1 },
60 { "OpenVPN.AuthUserPass", "--auth-user-pass", 1 },
61 { "OpenVPN.TLSRemote", "--tls-remote", 1 },
62 { "OpenVPN.Cipher", "--cipher", 1 },
63 { "OpenVPN.Auth", "--auth", 1 },
64 { "OpenVPN.CompLZO", "--comp-lzo", 0 },
65 { "OpenVPN.RemoteCertTls", "--remote-cert-tls", 1 },
68 static void ov_append_dns_entries(const char *key, const char *value,
73 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
76 options = g_strsplit(value, " ", 3);
77 if (options[0] != NULL &&
78 !strcmp(options[0], "dhcp-option") &&
80 !strcmp(options[1], "DNS") &&
83 if (*dns_entries != NULL) {
86 tmp = g_strjoin(" ", *dns_entries,
91 *dns_entries = g_strdup(options[2]);
98 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
100 DBusMessageIter iter, dict;
101 const char *reason, *key, *value;
102 char *nameservers = NULL;
103 char *address = NULL, *gateway = NULL, *peer = NULL;
104 struct connman_ipaddress *ipaddress;
106 dbus_message_iter_init(msg, &iter);
108 dbus_message_iter_get_basic(&iter, &reason);
109 dbus_message_iter_next(&iter);
112 connman_error("No provider found");
113 return VPN_STATE_FAILURE;
116 if (strcmp(reason, "up"))
117 return VPN_STATE_DISCONNECT;
119 dbus_message_iter_recurse(&iter, &dict);
121 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
122 DBusMessageIter entry;
124 dbus_message_iter_recurse(&dict, &entry);
125 dbus_message_iter_get_basic(&entry, &key);
126 dbus_message_iter_next(&entry);
127 dbus_message_iter_get_basic(&entry, &value);
129 DBG("%s = %s", key, value);
131 if (!strcmp(key, "trusted_ip")) {
132 connman_provider_set_string(provider, "Gateway", value);
133 gateway = g_strdup(value);
136 if (!strcmp(key, "ifconfig_local")) {
137 connman_provider_set_string(provider, "Address", value);
138 address = g_strdup(value);
141 if (!strcmp(key, "ifconfig_remote")) {
142 connman_provider_set_string(provider, "Peer", value);
143 peer = g_strdup(value);
146 if (g_str_has_prefix(key, "route_") == TRUE)
147 connman_provider_append_route(provider, key, value);
149 ov_append_dns_entries(key, value, &nameservers);
151 dbus_message_iter_next(&dict);
154 ipaddress = connman_ipaddress_alloc(AF_INET);
155 if (ipaddress == NULL) {
161 return VPN_STATE_FAILURE;
164 connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
165 connman_ipaddress_set_peer(ipaddress, peer);
166 connman_provider_set_ipaddress(provider, ipaddress);
168 connman_provider_set_nameservers(provider, nameservers);
174 connman_ipaddress_free(ipaddress);
176 return VPN_STATE_CONNECT;
179 static int ov_save(struct connman_provider *provider, GKeyFile *keyfile)
184 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
185 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
186 option = connman_provider_get_string(provider,
187 ov_options[i].cm_opt);
191 g_key_file_set_string(keyfile,
192 connman_provider_get_save_group(provider),
193 ov_options[i].cm_opt, option);
199 static int task_append_config_data(struct connman_provider *provider,
200 struct connman_task *task)
205 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
206 option = connman_provider_get_string(provider,
207 ov_options[i].cm_opt);
211 if (connman_task_add_argument(task,
212 ov_options[i].ov_opt,
213 ov_options[i].has_value ? option : NULL) < 0) {
221 static int ov_connect(struct connman_provider *provider,
222 struct connman_task *task, const char *if_name)
227 option = connman_provider_get_string(provider, "Host");
228 if (option == NULL) {
229 connman_error("Host not set; cannot enable VPN");
233 task_append_config_data(provider, task);
235 connman_task_add_argument(task, "--syslog", NULL);
237 connman_task_add_argument(task, "--script-security", "2");
239 connman_task_add_argument(task, "--up",
240 SCRIPTDIR "/openvpn-script");
241 connman_task_add_argument(task, "--up-restart", NULL);
243 connman_task_add_argument(task, "--setenv", NULL);
244 connman_task_add_argument(task, "CONNMAN_BUSNAME",
245 dbus_bus_get_unique_name(connection));
247 connman_task_add_argument(task, "--setenv", NULL);
248 connman_task_add_argument(task, "CONNMAN_INTERFACE",
249 CONNMAN_TASK_INTERFACE);
251 connman_task_add_argument(task, "--setenv", NULL);
252 connman_task_add_argument(task, "CONNMAN_PATH",
253 connman_task_get_path(task));
255 connman_task_add_argument(task, "--dev", if_name);
256 connman_task_add_argument(task, "--dev-type", "tun");
258 connman_task_add_argument(task, "--tls-client", NULL);
259 connman_task_add_argument(task, "--nobind", NULL);
260 connman_task_add_argument(task, "--persist-key", NULL);
261 connman_task_add_argument(task, "--persist-tun", NULL);
263 connman_task_add_argument(task, "--route-noexec", NULL);
264 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
267 * Disable client restarts because we can't handle this at the
268 * moment. The problem is that when OpenVPN decides to switch
269 * from CONNECTED state to RECONNECTING and then to RESOLVE,
270 * it is not possible to do a DNS lookup. The DNS server is
271 * not accessable through the tunnel anymore and so we end up
272 * trying to resolve the OpenVPN servers address.
274 connman_task_add_argument(task, "--ping-restart", "0");
276 connman_task_add_argument(task, "--client", NULL);
279 err = connman_task_run(task, vpn_died, provider,
282 connman_error("openvpn failed to start");
289 static struct vpn_driver vpn_driver = {
291 .connect = ov_connect,
295 static int openvpn_init(void)
297 connection = connman_dbus_get_connection();
299 return vpn_register("openvpn", &vpn_driver, OPENVPN);
302 static void openvpn_exit(void)
304 vpn_unregister("openvpn");
306 dbus_connection_unref(connection);
309 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
310 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)