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 void ov_append_dns_entries(const char *key, const char *value,
50 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
53 options = g_strsplit(value, " ", 3);
54 if (options[0] != NULL &&
55 !strcmp(options[0], "dhcp-option") &&
57 !strcmp(options[1], "DNS") &&
60 if (*dns_entries != NULL) {
63 tmp = g_strjoin(" ", *dns_entries,
68 *dns_entries = g_strdup(options[2]);
75 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
77 DBusMessageIter iter, dict;
78 const char *reason, *key, *value;
79 const char *domain = NULL;
80 char *dns_entries = NULL;
82 dbus_message_iter_init(msg, &iter);
84 dbus_message_iter_get_basic(&iter, &reason);
85 dbus_message_iter_next(&iter);
88 connman_error("No provider found");
89 return VPN_STATE_FAILURE;
92 if (strcmp(reason, "up"))
93 return VPN_STATE_DISCONNECT;
95 domain = connman_provider_get_string(provider, "VPN.Domain");
97 dbus_message_iter_recurse(&iter, &dict);
99 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
100 DBusMessageIter entry;
102 dbus_message_iter_recurse(&dict, &entry);
103 dbus_message_iter_get_basic(&entry, &key);
104 dbus_message_iter_next(&entry);
105 dbus_message_iter_get_basic(&entry, &value);
107 DBG("%s = %s", key, value);
109 if (!strcmp(key, "trusted_ip"))
110 connman_provider_set_string(provider, "Gateway", value);
112 if (!strcmp(key, "ifconfig_local"))
113 connman_provider_set_string(provider, "Address", value);
115 if (!strcmp(key, "ifconfig_remote"))
116 connman_provider_set_string(provider, "Peer", value);
118 if (g_str_has_prefix(key, "network_") == TRUE)
119 connman_provider_append_route(provider, key, value);
121 ov_append_dns_entries(key, value, &dns_entries);
123 dbus_message_iter_next(&dict);
126 if (dns_entries != NULL) {
127 connman_provider_set_string(provider, "DNS", dns_entries);
131 return VPN_STATE_CONNECT;
134 static int ov_connect(struct connman_provider *provider,
135 struct connman_task *task, const char *if_name)
137 const char *vpnhost, *cafile, *mtu, *certfile, *keyfile;
138 const char *proto, *port, *auth_user_pass;
139 const char *tls_remote, *cipher, *auth, *comp_lzo;
142 vpnhost = connman_provider_get_string(provider, "Host");
144 connman_error("Host not set; cannot enable VPN");
148 cafile = connman_provider_get_string(provider, "OpenVPN.CACert");
149 certfile = connman_provider_get_string(provider, "OpenVPN.Cert");
150 keyfile = connman_provider_get_string(provider, "OpenVPN.Key");
151 mtu = connman_provider_get_string(provider, "VPN.MTU");
152 proto = connman_provider_get_string(provider, "OpenVPN.Proto");
153 port = connman_provider_get_string(provider, "OpenVPN.Port");
154 auth_user_pass = connman_provider_get_string(provider,
155 "OpenVPN.AuthUserPass");
156 tls_remote = connman_provider_get_string(provider, "OpenVPN.TLSRemote");
157 cipher = connman_provider_get_string(provider, "OpenVPN.Cipher");
158 auth = connman_provider_get_string(provider, "OpenVPN.Auth");
159 comp_lzo = connman_provider_get_string(provider, "OpenVPN.CompLZO");
162 connman_task_add_argument(task, "--mtu", (char *)mtu);
165 connman_task_add_argument(task, "--proto", (char *)proto);
168 connman_task_add_argument(task, "--port", (char *)port);
170 if (auth_user_pass != NULL) {
171 connman_task_add_argument(task, "--auth-user-pass",
172 (char *)auth_user_pass);
175 if (tls_remote != NULL) {
176 connman_task_add_argument(task, "--tls-remote",
181 connman_task_add_argument(task, "--cipher", (char *)cipher);
184 connman_task_add_argument(task, "--auth", (char *)auth);
187 connman_task_add_argument(task, "--comp-lzo", (char *)comp_lzo);
189 connman_task_add_argument(task, "--syslog", NULL);
191 connman_task_add_argument(task, "--script-security", "2");
193 connman_task_add_argument(task, "--up",
194 SCRIPTDIR "/openvpn-script");
195 connman_task_add_argument(task, "--up-restart", NULL);
197 connman_task_add_argument(task, "--setenv", NULL);
198 connman_task_add_argument(task, "CONNMAN_BUSNAME",
199 dbus_bus_get_unique_name(connection));
201 connman_task_add_argument(task, "--setenv", NULL);
202 connman_task_add_argument(task, "CONNMAN_INTERFACE",
203 CONNMAN_TASK_INTERFACE);
205 connman_task_add_argument(task, "--setenv", NULL);
206 connman_task_add_argument(task, "CONNMAN_PATH",
207 connman_task_get_path(task));
209 connman_task_add_argument(task, "--dev", if_name);
210 connman_task_add_argument(task, "--dev-type", "tun");
212 connman_task_add_argument(task, "--tls-client", NULL);
213 connman_task_add_argument(task, "--remote", (char *)vpnhost);
214 connman_task_add_argument(task, "--nobind", NULL);
215 connman_task_add_argument(task, "--persist-key", NULL);
216 connman_task_add_argument(task, "--persist-tun", NULL);
218 connman_task_add_argument(task, "--route-noexec", NULL);
219 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
222 * Disable client restarts because we can't handle this at the
223 * moment. The problem is that when OpenVPN decides to switch
224 * from CONNECTED state to RECONNECTING and then to RESOLVE,
225 * it is not possible to do a DNS lookup. The DNS server is
226 * not accessable through the tunnel anymore and so we end up
227 * trying to resolve the OpenVPN servers address.
229 connman_task_add_argument(task, "--ping-restart", "0");
231 connman_task_add_argument(task, "--client", NULL);
234 connman_task_add_argument(task, "--ca",
239 connman_task_add_argument(task, "--cert",
244 connman_task_add_argument(task, "--key",
249 err = connman_task_run(task, vpn_died, provider,
252 connman_error("openvpn failed to start");
259 static struct vpn_driver vpn_driver = {
261 .connect = ov_connect,
264 static int openvpn_init(void)
266 connection = connman_dbus_get_connection();
268 return vpn_register("openvpn", &vpn_driver, OPENVPN);
271 static void openvpn_exit(void)
273 vpn_unregister("openvpn");
275 dbus_connection_unref(connection);
278 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
279 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)