5 * Copyright (C) 2010,2013 BMW Car IT GmbH.
6 * Copyright (C) 2010,2012-2013 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <linux/if_tun.h>
36 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/log.h>
39 #include <connman/task.h>
40 #include <connman/ipconfig.h>
41 #include <connman/dbus.h>
43 #include "../vpn-provider.h"
47 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
49 static DBusConnection *connection;
59 const char *vpnc_default;
63 { "Host", "IPSec gateway", NULL, OPT_STRING, true },
64 { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING, true },
65 { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING, false },
66 { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING, false },
67 { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING, false },
68 { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING, true },
69 { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING, true },
70 { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING, true },
71 { "VPNC.Domain", "Domain", NULL, OPT_STRING, true },
72 { "VPNC.Vendor", "Vendor", NULL, OPT_STRING, true },
73 { "VPNC.LocalPort", "Local Port", "0", OPT_STRING, true, },
74 { "VPNC.CiscoPort", "Cisco UDP Encapsulation Port", "0", OPT_STRING,
76 { "VPNC.AppVersion", "Application version", NULL, OPT_STRING, true },
77 { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING,
79 { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING,
81 { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN, true },
82 { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN,
86 static int vc_notify(DBusMessage *msg, struct vpn_provider *provider)
88 DBusMessageIter iter, dict;
89 char *address = NULL, *netmask = NULL, *gateway = NULL;
90 struct connman_ipaddress *ipaddress;
91 const char *reason, *key, *value;
93 dbus_message_iter_init(msg, &iter);
95 dbus_message_iter_get_basic(&iter, &reason);
96 dbus_message_iter_next(&iter);
99 connman_error("No provider found");
100 return VPN_STATE_FAILURE;
103 if (strcmp(reason, "connect"))
104 return VPN_STATE_DISCONNECT;
106 dbus_message_iter_recurse(&iter, &dict);
108 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
109 DBusMessageIter entry;
111 dbus_message_iter_recurse(&dict, &entry);
112 dbus_message_iter_get_basic(&entry, &key);
113 dbus_message_iter_next(&entry);
114 dbus_message_iter_get_basic(&entry, &value);
116 DBG("%s = %s", key, value);
118 if (!strcmp(key, "VPNGATEWAY"))
119 gateway = g_strdup(value);
121 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
122 address = g_strdup(value);
124 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
125 netmask = g_strdup(value);
127 if (!strcmp(key, "INTERNAL_IP4_DNS"))
128 vpn_provider_set_nameservers(provider, value);
130 if (!strcmp(key, "CISCO_DEF_DOMAIN"))
131 vpn_provider_set_domain(provider, value);
133 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") ||
134 g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC"))
135 vpn_provider_append_route(provider, key, value);
137 dbus_message_iter_next(&dict);
141 ipaddress = connman_ipaddress_alloc(AF_INET);
147 return VPN_STATE_FAILURE;
150 connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
151 vpn_provider_set_ipaddress(provider, ipaddress);
156 connman_ipaddress_free(ipaddress);
158 return VPN_STATE_CONNECT;
161 static ssize_t full_write(int fd, const void *buf, size_t len)
166 byte_write = write(fd, buf, len);
167 if (byte_write < 0) {
168 connman_error("failed to write config to vpnc: %s\n",
179 static ssize_t write_option(int fd, const char *key, const char *value)
185 buf = g_strdup_printf("%s %s\n", key, value);
186 ret = full_write(fd, buf, strlen(buf));
194 static ssize_t write_bool_option(int fd, const char *key, const char *value)
200 if (strcasecmp(value, "yes") == 0 ||
201 strcasecmp(value, "true") == 0 ||
202 strcmp(value, "1") == 0) {
203 buf = g_strdup_printf("%s\n", key);
204 ret = full_write(fd, buf, strlen(buf));
213 static int vc_write_config_data(struct vpn_provider *provider, int fd)
218 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
219 opt_s = vpn_provider_get_string(provider,
220 vpnc_options[i].cm_opt);
222 opt_s = vpnc_options[i].vpnc_default;
227 if (vpnc_options[i].type == OPT_STRING) {
229 vpnc_options[i].vpnc_opt, opt_s) < 0)
231 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
232 if (write_bool_option(fd,
233 vpnc_options[i].vpnc_opt, opt_s) < 0)
242 static int vc_save(struct vpn_provider *provider, GKeyFile *keyfile)
247 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
248 if (strncmp(vpnc_options[i].cm_opt, "VPNC.", 5) == 0) {
250 if (!vpnc_options[i].cm_save)
253 option = vpn_provider_get_string(provider,
254 vpnc_options[i].cm_opt);
258 g_key_file_set_string(keyfile,
259 vpn_provider_get_save_group(provider),
260 vpnc_options[i].cm_opt, option);
266 static int vc_connect(struct vpn_provider *provider,
267 struct connman_task *task, const char *if_name,
268 vpn_provider_connect_cb_t cb, const char *dbus_sender,
274 option = vpn_provider_get_string(provider, "Host");
276 connman_error("Host not set; cannot enable VPN");
280 option = vpn_provider_get_string(provider, "VPNC.IPSec.ID");
282 connman_error("Group not set; cannot enable VPN");
287 connman_task_add_argument(task, "--non-inter", NULL);
288 connman_task_add_argument(task, "--no-detach", NULL);
290 connman_task_add_argument(task, "--ifname", if_name);
291 option = vpn_provider_get_string(provider, "VPNC.DeviceType");
293 connman_task_add_argument(task, "--ifmode", option);
296 * Default to tun for backwards compatibility.
298 connman_task_add_argument(task, "--ifmode", "tun");
301 connman_task_add_argument(task, "--script",
302 SCRIPTDIR "/openconnect-script");
304 option = vpn_provider_get_string(provider, "VPNC.Debug");
306 connman_task_add_argument(task, "--debug", option);
308 connman_task_add_argument(task, "-", NULL);
310 err = connman_task_run(task, vpn_died, provider,
313 connman_error("vpnc failed to start");
318 err = vc_write_config_data(provider, fd);
324 cb(provider, user_data, err);
329 static int vc_error_code(struct vpn_provider *provider, int exit_code)
333 return VPN_PROVIDER_ERROR_CONNECT_FAILED;
335 return VPN_PROVIDER_ERROR_LOGIN_FAILED;
337 return VPN_PROVIDER_ERROR_UNKNOWN;
341 static int vc_device_flags(struct vpn_provider *provider)
345 option = vpn_provider_get_string(provider, "VPNC.DeviceType");
350 if (g_str_equal(option, "tap")) {
354 if (!g_str_equal(option, "tun")) {
355 connman_warn("bad VPNC.DeviceType value, falling back to tun");
361 static struct vpn_driver vpn_driver = {
363 .connect = vc_connect,
364 .error_code = vc_error_code,
366 .device_flags = vc_device_flags,
369 static int vpnc_init(void)
371 connection = connman_dbus_get_connection();
373 return vpn_register("vpnc", &vpn_driver, VPNC);
376 static void vpnc_exit(void)
378 vpn_unregister("vpnc");
380 dbus_connection_unref(connection);
383 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
384 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)