5 * Copyright (C) 2007-2012 Intel Corporation. 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
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <linux/if_tun.h>
38 #include <dbus/dbus.h>
40 #include <glib/gprintf.h>
42 #include <connman/log.h>
43 #include <connman/rtnl.h>
44 #include <connman/task.h>
45 #include <connman/inet.h>
47 #include "../vpn-rtnl.h"
48 #include "../vpn-provider.h"
53 struct vpn_provider *provider;
58 struct connman_task *task;
61 struct vpn_driver_data {
64 struct vpn_driver *vpn_driver;
65 struct vpn_provider_driver provider_driver;
68 GHashTable *driver_hash = NULL;
70 static int stop_vpn(struct vpn_provider *provider)
72 struct vpn_data *data = vpn_provider_get_data(provider);
73 struct vpn_driver_data *vpn_driver_data;
81 name = vpn_provider_get_driver_name(provider);
85 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
87 if (vpn_driver_data != NULL && vpn_driver_data->vpn_driver != NULL &&
88 vpn_driver_data->vpn_driver->flags == VPN_FLAG_NO_TUN)
91 memset(&ifr, 0, sizeof(ifr));
92 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
93 sprintf(ifr.ifr_name, "%s", data->if_name);
95 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
98 connman_error("Failed to open /dev/net/tun to device %s: %s",
99 data->if_name, strerror(errno));
103 if (ioctl(fd, TUNSETIFF, (void *)&ifr)) {
105 connman_error("Failed to TUNSETIFF for device %s to it: %s",
106 data->if_name, strerror(errno));
111 if (ioctl(fd, TUNSETPERSIST, 0)) {
113 connman_error("Failed to set tun device %s nonpersistent: %s",
114 data->if_name, strerror(errno));
119 DBG("Killed tun device %s", data->if_name);
123 void vpn_died(struct connman_task *task, int exit_code, void *user_data)
125 struct vpn_provider *provider = user_data;
126 struct vpn_data *data = vpn_provider_get_data(provider);
127 int state = VPN_STATE_FAILURE;
128 enum vpn_provider_error ret;
130 DBG("provider %p data %p", provider, data);
138 vpn_provider_set_data(provider, NULL);
140 if (data->watch != 0) {
141 vpn_provider_unref(provider);
142 vpn_rtnl_remove_watch(data->watch);
147 if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT) {
149 struct vpn_driver_data *vpn_data = NULL;
151 name = vpn_provider_get_driver_name(provider);
153 vpn_data = g_hash_table_lookup(driver_hash, name);
155 if (vpn_data != NULL &&
156 vpn_data->vpn_driver->error_code != NULL)
157 ret = vpn_data->vpn_driver->error_code(exit_code);
159 ret = VPN_PROVIDER_ERROR_UNKNOWN;
161 vpn_provider_indicate_error(provider, ret);
163 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
165 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_IDLE);
167 vpn_provider_set_index(provider, -1);
170 vpn_provider_unref(data->provider);
171 g_free(data->if_name);
175 connman_task_destroy(task);
178 int vpn_set_ifname(struct vpn_provider *provider, const char *ifname)
180 struct vpn_data *data = vpn_provider_get_data(provider);
183 if (ifname == NULL || data == NULL)
186 index = connman_inet_ifindex(ifname);
190 if (data->if_name != NULL)
191 g_free(data->if_name);
193 data->if_name = (char *)g_strdup(ifname);
194 vpn_provider_set_index(provider, index);
199 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
201 struct vpn_provider *provider = user_data;
202 struct vpn_data *data = vpn_provider_get_data(provider);
204 if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
205 if (flags & IFF_UP) {
206 data->state = VPN_STATE_READY;
207 vpn_provider_set_state(provider,
208 VPN_PROVIDER_STATE_READY);
214 static DBusMessage *vpn_notify(struct connman_task *task,
215 DBusMessage *msg, void *user_data)
217 struct vpn_provider *provider = user_data;
218 struct vpn_data *data;
219 struct vpn_driver_data *vpn_driver_data;
221 int state, index, err;
223 data = vpn_provider_get_data(provider);
225 name = vpn_provider_get_driver_name(provider);
228 DBG("Cannot find VPN driver for provider %p", provider);
229 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
233 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
234 if (vpn_driver_data == NULL) {
235 DBG("Cannot find VPN driver data for name %s", name);
236 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
240 state = vpn_driver_data->vpn_driver->notify(msg, provider);
242 DBG("provider %p driver %s state %d", provider, name, state);
245 case VPN_STATE_CONNECT:
246 case VPN_STATE_READY:
247 index = vpn_provider_get_index(provider);
248 vpn_provider_ref(provider);
249 data->watch = vpn_rtnl_add_newlink_watch(index,
250 vpn_newlink, provider);
251 err = connman_inet_ifup(index);
253 if (err == -EALREADY)
255 * So the interface is up already, that is just
256 * great. Unfortunately in this case the
257 * newlink watch might not have been called at
258 * all. We must manually call it here so that
259 * the provider can go to ready state and the
260 * routes are setup properly.
262 vpn_newlink(IFF_UP, 0, provider);
264 DBG("Cannot take interface %d up err %d/%s",
265 index, -err, strerror(-err));
269 case VPN_STATE_UNKNOWN:
271 case VPN_STATE_DISCONNECT:
272 case VPN_STATE_FAILURE:
273 vpn_provider_set_state(provider,
274 VPN_PROVIDER_STATE_DISCONNECT);
277 case VPN_STATE_AUTH_FAILURE:
278 vpn_provider_indicate_error(provider,
279 VPN_PROVIDER_ERROR_AUTH_FAILED);
286 static int vpn_create_tun(struct vpn_provider *provider)
288 struct vpn_data *data = vpn_provider_get_data(provider);
296 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
299 connman_error("Failed to open /dev/net/tun: %s",
305 memset(&ifr, 0, sizeof(ifr));
306 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
308 for (i = 0; i < 256; i++) {
309 sprintf(ifr.ifr_name, "vpn%d", i);
311 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
316 connman_error("Failed to find available tun device");
322 data->if_name = (char *)g_strdup(ifr.ifr_name);
323 if (data->if_name == NULL) {
324 connman_error("Failed to allocate memory");
330 if (ioctl(fd, TUNSETPERSIST, 1)) {
332 connman_error("Failed to set tun persistent: %s",
341 index = connman_inet_ifindex(data->if_name);
343 connman_error("Failed to get tun ifindex");
348 vpn_provider_set_index(provider, index);
356 static int vpn_connect(struct vpn_provider *provider,
357 vpn_provider_connect_cb_t cb, void *user_data)
359 struct vpn_data *data = vpn_provider_get_data(provider);
360 struct vpn_driver_data *vpn_driver_data;
367 data = g_try_new0(struct vpn_data, 1);
371 data->provider = vpn_provider_ref(provider);
375 data->state = VPN_STATE_IDLE;
377 vpn_provider_set_data(provider, data);
379 name = vpn_provider_get_driver_name(provider);
383 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
385 if (vpn_driver_data == NULL || vpn_driver_data->vpn_driver == NULL) {
390 if (vpn_driver_data->vpn_driver->flags != VPN_FLAG_NO_TUN) {
391 ret = vpn_create_tun(provider);
396 data->task = connman_task_create(vpn_driver_data->program);
398 if (data->task == NULL) {
404 if (connman_task_set_notify(data->task, "notify",
405 vpn_notify, provider)) {
408 connman_task_destroy(data->task);
413 ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
414 data->if_name, cb, user_data);
417 connman_task_destroy(data->task);
422 DBG("%s started with dev %s",
423 vpn_driver_data->provider_driver.name, data->if_name);
425 data->state = VPN_STATE_CONNECT;
430 vpn_provider_set_index(provider, -1);
431 vpn_provider_set_data(provider, NULL);
432 vpn_provider_unref(data->provider);
433 g_free(data->if_name);
439 static int vpn_probe(struct vpn_provider *provider)
444 static int vpn_disconnect(struct vpn_provider *provider)
446 struct vpn_data *data = vpn_provider_get_data(provider);
447 struct vpn_driver_data *vpn_driver_data;
450 DBG("disconnect provider %p:", provider);
455 name = vpn_provider_get_driver_name(provider);
459 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
460 if (vpn_driver_data->vpn_driver->disconnect)
461 vpn_driver_data->vpn_driver->disconnect();
463 if (data->watch != 0) {
464 vpn_provider_unref(provider);
465 vpn_rtnl_remove_watch(data->watch);
469 data->state = VPN_STATE_DISCONNECT;
470 connman_task_stop(data->task);
475 static int vpn_remove(struct vpn_provider *provider)
477 struct vpn_data *data;
479 data = vpn_provider_get_data(provider);
483 if (data->watch != 0) {
484 vpn_provider_unref(provider);
485 vpn_rtnl_remove_watch(data->watch);
489 connman_task_stop(data->task);
491 g_usleep(G_USEC_PER_SEC);
496 static int vpn_save(struct vpn_provider *provider, GKeyFile *keyfile)
498 struct vpn_driver_data *vpn_driver_data;
501 name = vpn_provider_get_driver_name(provider);
502 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
503 if (vpn_driver_data != NULL &&
504 vpn_driver_data->vpn_driver->save != NULL)
505 return vpn_driver_data->vpn_driver->save(provider, keyfile);
510 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
513 struct vpn_driver_data *data;
515 data = g_try_new0(struct vpn_driver_data, 1);
520 data->program = program;
522 data->vpn_driver = vpn_driver;
524 data->provider_driver.name = name;
525 data->provider_driver.disconnect = vpn_disconnect;
526 data->provider_driver.connect = vpn_connect;
527 data->provider_driver.probe = vpn_probe;
528 data->provider_driver.remove = vpn_remove;
529 data->provider_driver.save = vpn_save;
531 if (driver_hash == NULL)
532 driver_hash = g_hash_table_new_full(g_str_hash,
536 if (driver_hash == NULL) {
537 connman_error("driver_hash not initialized for %s", name);
542 g_hash_table_replace(driver_hash, (char *)name, data);
544 vpn_provider_driver_register(&data->provider_driver);
549 void vpn_unregister(const char *name)
551 struct vpn_driver_data *data;
553 data = g_hash_table_lookup(driver_hash, name);
557 vpn_provider_driver_unregister(&data->provider_driver);
559 g_hash_table_remove(driver_hash, name);
561 if (g_hash_table_size(driver_hash) == 0)
562 g_hash_table_destroy(driver_hash);