5 * Copyright (C) 2007-2013 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 && vpn_driver_data->vpn_driver &&
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);
156 vpn_data->vpn_driver->error_code)
157 ret = vpn_data->vpn_driver->error_code(provider,
160 ret = VPN_PROVIDER_ERROR_UNKNOWN;
162 vpn_provider_indicate_error(provider, ret);
164 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
166 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_IDLE);
168 vpn_provider_set_index(provider, -1);
171 vpn_provider_unref(data->provider);
172 g_free(data->if_name);
176 connman_task_destroy(task);
179 int vpn_set_ifname(struct vpn_provider *provider, const char *ifname)
181 struct vpn_data *data = vpn_provider_get_data(provider);
184 if (!ifname || !data)
187 index = connman_inet_ifindex(ifname);
192 g_free(data->if_name);
194 data->if_name = (char *)g_strdup(ifname);
195 vpn_provider_set_index(provider, index);
200 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
202 struct vpn_provider *provider = user_data;
203 struct vpn_data *data = vpn_provider_get_data(provider);
205 if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
206 if (flags & IFF_UP) {
207 data->state = VPN_STATE_READY;
208 vpn_provider_set_state(provider,
209 VPN_PROVIDER_STATE_READY);
215 static DBusMessage *vpn_notify(struct connman_task *task,
216 DBusMessage *msg, void *user_data)
218 struct vpn_provider *provider = user_data;
219 struct vpn_data *data;
220 struct vpn_driver_data *vpn_driver_data;
222 int state, index, err;
224 data = vpn_provider_get_data(provider);
226 name = vpn_provider_get_driver_name(provider);
229 DBG("Cannot find VPN driver for provider %p", provider);
230 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
234 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
235 if (!vpn_driver_data) {
236 DBG("Cannot find VPN driver data for name %s", name);
237 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
241 state = vpn_driver_data->vpn_driver->notify(msg, provider);
243 DBG("provider %p driver %s state %d", provider, name, state);
246 case VPN_STATE_CONNECT:
247 case VPN_STATE_READY:
248 index = vpn_provider_get_index(provider);
249 vpn_provider_ref(provider);
250 data->watch = vpn_rtnl_add_newlink_watch(index,
251 vpn_newlink, provider);
252 err = connman_inet_ifup(index);
254 if (err == -EALREADY)
256 * So the interface is up already, that is just
257 * great. Unfortunately in this case the
258 * newlink watch might not have been called at
259 * all. We must manually call it here so that
260 * the provider can go to ready state and the
261 * routes are setup properly.
263 vpn_newlink(IFF_UP, 0, provider);
265 DBG("Cannot take interface %d up err %d/%s",
266 index, -err, strerror(-err));
270 case VPN_STATE_UNKNOWN:
272 case VPN_STATE_DISCONNECT:
273 case VPN_STATE_FAILURE:
274 vpn_provider_set_state(provider,
275 VPN_PROVIDER_STATE_DISCONNECT);
278 case VPN_STATE_AUTH_FAILURE:
279 vpn_provider_indicate_error(provider,
280 VPN_PROVIDER_ERROR_AUTH_FAILED);
287 static int vpn_create_tun(struct vpn_provider *provider)
289 struct vpn_data *data = vpn_provider_get_data(provider);
297 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
300 connman_error("Failed to open /dev/net/tun: %s",
306 memset(&ifr, 0, sizeof(ifr));
307 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
309 for (i = 0; i < 256; i++) {
310 sprintf(ifr.ifr_name, "vpn%d", i);
312 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
317 connman_error("Failed to find available tun device");
323 data->if_name = (char *)g_strdup(ifr.ifr_name);
324 if (!data->if_name) {
325 connman_error("Failed to allocate memory");
331 if (ioctl(fd, TUNSETPERSIST, 1)) {
333 connman_error("Failed to set tun persistent: %s",
342 index = connman_inet_ifindex(data->if_name);
344 connman_error("Failed to get tun ifindex");
349 vpn_provider_set_index(provider, index);
357 static int vpn_connect(struct vpn_provider *provider,
358 vpn_provider_connect_cb_t cb,
359 const char *dbus_sender, void *user_data)
361 struct vpn_data *data = vpn_provider_get_data(provider);
362 struct vpn_driver_data *vpn_driver_data;
365 enum vpn_state state = VPN_STATE_UNKNOWN;
370 DBG("data %p state %d", data, state);
373 case VPN_STATE_UNKNOWN:
374 data = g_try_new0(struct vpn_data, 1);
378 data->provider = vpn_provider_ref(provider);
383 vpn_provider_set_data(provider, data);
386 case VPN_STATE_DISCONNECT:
388 case VPN_STATE_FAILURE:
389 case VPN_STATE_AUTH_FAILURE:
390 data->state = VPN_STATE_IDLE;
393 case VPN_STATE_CONNECT:
396 case VPN_STATE_READY:
400 name = vpn_provider_get_driver_name(provider);
404 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
406 if (!vpn_driver_data || !vpn_driver_data->vpn_driver) {
411 if (vpn_driver_data->vpn_driver->flags != VPN_FLAG_NO_TUN) {
412 ret = vpn_create_tun(provider);
417 data->task = connman_task_create(vpn_driver_data->program);
425 if (connman_task_set_notify(data->task, "notify",
426 vpn_notify, provider)) {
429 connman_task_destroy(data->task);
434 ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
435 data->if_name, cb, dbus_sender,
437 if (ret < 0 && ret != -EINPROGRESS) {
439 connman_task_destroy(data->task);
444 DBG("%s started with dev %s",
445 vpn_driver_data->provider_driver.name, data->if_name);
447 data->state = VPN_STATE_CONNECT;
452 vpn_provider_set_index(provider, -1);
453 vpn_provider_set_data(provider, NULL);
454 vpn_provider_unref(data->provider);
455 g_free(data->if_name);
461 static int vpn_probe(struct vpn_provider *provider)
466 static int vpn_disconnect(struct vpn_provider *provider)
468 struct vpn_data *data = vpn_provider_get_data(provider);
469 struct vpn_driver_data *vpn_driver_data;
472 DBG("disconnect provider %p:", provider);
477 name = vpn_provider_get_driver_name(provider);
481 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
482 if (vpn_driver_data->vpn_driver->disconnect)
483 vpn_driver_data->vpn_driver->disconnect(provider);
485 if (data->watch != 0) {
486 vpn_provider_unref(provider);
487 vpn_rtnl_remove_watch(data->watch);
491 data->state = VPN_STATE_DISCONNECT;
492 connman_task_stop(data->task);
497 static int vpn_remove(struct vpn_provider *provider)
499 struct vpn_data *data;
501 data = vpn_provider_get_data(provider);
505 if (data->watch != 0) {
506 vpn_provider_unref(provider);
507 vpn_rtnl_remove_watch(data->watch);
511 connman_task_stop(data->task);
513 g_usleep(G_USEC_PER_SEC);
518 static int vpn_save(struct vpn_provider *provider, GKeyFile *keyfile)
520 struct vpn_driver_data *vpn_driver_data;
523 name = vpn_provider_get_driver_name(provider);
524 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
525 if (vpn_driver_data &&
526 vpn_driver_data->vpn_driver->save)
527 return vpn_driver_data->vpn_driver->save(provider, keyfile);
532 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
535 struct vpn_driver_data *data;
537 data = g_try_new0(struct vpn_driver_data, 1);
542 data->program = program;
544 data->vpn_driver = vpn_driver;
546 data->provider_driver.name = name;
547 data->provider_driver.disconnect = vpn_disconnect;
548 data->provider_driver.connect = vpn_connect;
549 data->provider_driver.probe = vpn_probe;
550 data->provider_driver.remove = vpn_remove;
551 data->provider_driver.save = vpn_save;
554 driver_hash = g_hash_table_new_full(g_str_hash,
559 connman_error("driver_hash not initialized for %s", name);
564 g_hash_table_replace(driver_hash, (char *)name, data);
566 vpn_provider_driver_register(&data->provider_driver);
571 void vpn_unregister(const char *name)
573 struct vpn_driver_data *data;
575 data = g_hash_table_lookup(driver_hash, name);
579 vpn_provider_driver_unregister(&data->provider_driver);
581 g_hash_table_remove(driver_hash, name);
583 if (g_hash_table_size(driver_hash) == 0)
584 g_hash_table_destroy(driver_hash);