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_rtnl_remove_watch(data->watch);
143 vpn_provider_unref(provider);
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_IDLE);
166 vpn_provider_set_index(provider, -1);
169 vpn_provider_unref(data->provider);
170 g_free(data->if_name);
174 connman_task_destroy(task);
177 int vpn_set_ifname(struct vpn_provider *provider, const char *ifname)
179 struct vpn_data *data = vpn_provider_get_data(provider);
182 if (!ifname || !data)
185 index = connman_inet_ifindex(ifname);
190 g_free(data->if_name);
192 data->if_name = (char *)g_strdup(ifname);
193 vpn_provider_set_index(provider, index);
198 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
200 struct vpn_provider *provider = user_data;
201 struct vpn_data *data = vpn_provider_get_data(provider);
203 if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
204 if (flags & IFF_UP) {
205 data->state = VPN_STATE_READY;
206 vpn_provider_set_state(provider,
207 VPN_PROVIDER_STATE_READY);
213 static DBusMessage *vpn_notify(struct connman_task *task,
214 DBusMessage *msg, void *user_data)
216 struct vpn_provider *provider = user_data;
217 struct vpn_data *data;
218 struct vpn_driver_data *vpn_driver_data;
220 int state, index, err;
222 data = vpn_provider_get_data(provider);
224 name = vpn_provider_get_driver_name(provider);
227 DBG("Cannot find VPN driver for provider %p", provider);
228 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
232 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
233 if (!vpn_driver_data) {
234 DBG("Cannot find VPN driver data for name %s", name);
235 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
239 state = vpn_driver_data->vpn_driver->notify(msg, provider);
241 DBG("provider %p driver %s state %d", provider, name, state);
244 case VPN_STATE_CONNECT:
245 case VPN_STATE_READY:
246 if (data->state == VPN_STATE_READY) {
248 * This is the restart case, in which case we must
249 * just set the IP address.
251 * We need to remove first the old address, just
252 * replacing the old address will not work as expected
253 * because the old address will linger in the interface
254 * and not disapper so the clearing is needed here.
256 * Also the state must change, otherwise the routes
257 * will not be set properly.
259 vpn_provider_set_state(provider,
260 VPN_PROVIDER_STATE_CONNECT);
262 vpn_provider_clear_address(provider, AF_INET);
263 vpn_provider_clear_address(provider, AF_INET6);
265 vpn_provider_change_address(provider);
266 vpn_provider_set_state(provider,
267 VPN_PROVIDER_STATE_READY);
271 index = vpn_provider_get_index(provider);
272 vpn_provider_ref(provider);
273 data->watch = vpn_rtnl_add_newlink_watch(index,
274 vpn_newlink, provider);
275 err = connman_inet_ifup(index);
277 if (err == -EALREADY)
279 * So the interface is up already, that is just
280 * great. Unfortunately in this case the
281 * newlink watch might not have been called at
282 * all. We must manually call it here so that
283 * the provider can go to ready state and the
284 * routes are setup properly.
286 vpn_newlink(IFF_UP, 0, provider);
288 DBG("Cannot take interface %d up err %d/%s",
289 index, -err, strerror(-err));
293 case VPN_STATE_UNKNOWN:
295 case VPN_STATE_DISCONNECT:
296 case VPN_STATE_FAILURE:
297 vpn_provider_set_state(provider,
298 VPN_PROVIDER_STATE_DISCONNECT);
301 case VPN_STATE_AUTH_FAILURE:
302 vpn_provider_indicate_error(provider,
303 VPN_PROVIDER_ERROR_AUTH_FAILED);
310 static void vpn_event(struct vpn_provider *provider, int state)
312 struct vpn_data *data;
313 struct vpn_driver_data *vpn_driver_data;
317 data = vpn_provider_get_data(provider);
319 name = vpn_provider_get_driver_name(provider);
322 DBG("Cannot find VPN driver for provider %p", provider);
323 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
327 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
328 if (!vpn_driver_data) {
329 DBG("Cannot find VPN driver data for name %s", name);
330 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
334 DBG("provider %p driver %s state %d", provider, name, state);
337 case VPN_STATE_CONNECT:
338 case VPN_STATE_READY:
339 if (data->state == VPN_STATE_READY) {
341 * This is the restart case, in which case we must
342 * just set the IP address.
344 * We need to remove first the old address, just
345 * replacing the old address will not work as expected
346 * because the old address will linger in the interface
347 * and not disapper so the clearing is needed here.
349 * Also the state must change, otherwise the routes
350 * will not be set properly.
352 vpn_provider_set_state(provider,
353 VPN_PROVIDER_STATE_CONNECT);
355 vpn_provider_clear_address(provider, AF_INET);
356 vpn_provider_clear_address(provider, AF_INET6);
358 vpn_provider_change_address(provider);
359 vpn_provider_set_state(provider,
360 VPN_PROVIDER_STATE_READY);
364 index = vpn_provider_get_index(provider);
365 vpn_provider_ref(provider);
366 data->watch = vpn_rtnl_add_newlink_watch(index,
367 vpn_newlink, provider);
368 err = connman_inet_ifup(index);
370 if (err == -EALREADY)
372 * So the interface is up already, that is just
373 * great. Unfortunately in this case the
374 * newlink watch might not have been called at
375 * all. We must manually call it here so that
376 * the provider can go to ready state and the
377 * routes are setup properly.
379 vpn_newlink(IFF_UP, 0, provider);
381 DBG("Cannot take interface %d up err %d/%s",
382 index, -err, strerror(-err));
386 case VPN_STATE_UNKNOWN:
388 case VPN_STATE_DISCONNECT:
389 case VPN_STATE_FAILURE:
390 vpn_provider_set_state(provider,
391 VPN_PROVIDER_STATE_DISCONNECT);
394 case VPN_STATE_AUTH_FAILURE:
395 vpn_provider_indicate_error(provider,
396 VPN_PROVIDER_ERROR_AUTH_FAILED);
403 static int vpn_create_tun(struct vpn_provider *provider)
405 struct vpn_data *data = vpn_provider_get_data(provider);
413 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
416 connman_error("Failed to open /dev/net/tun: %s",
422 memset(&ifr, 0, sizeof(ifr));
423 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
425 for (i = 0; i < 256; i++) {
426 sprintf(ifr.ifr_name, "vpn%d", i);
428 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
433 connman_error("Failed to find available tun device");
439 data->if_name = (char *)g_strdup(ifr.ifr_name);
440 if (!data->if_name) {
441 connman_error("Failed to allocate memory");
447 if (ioctl(fd, TUNSETPERSIST, 1)) {
449 connman_error("Failed to set tun persistent: %s",
458 index = connman_inet_ifindex(data->if_name);
460 connman_error("Failed to get tun ifindex");
465 vpn_provider_set_index(provider, index);
473 static int vpn_connect(struct vpn_provider *provider,
474 vpn_provider_connect_cb_t cb,
475 const char *dbus_sender, void *user_data)
477 struct vpn_data *data = vpn_provider_get_data(provider);
478 struct vpn_driver_data *vpn_driver_data;
481 enum vpn_state state = VPN_STATE_UNKNOWN;
486 DBG("data %p state %d", data, state);
489 case VPN_STATE_UNKNOWN:
490 data = g_try_new0(struct vpn_data, 1);
494 data->provider = vpn_provider_ref(provider);
499 vpn_provider_set_data(provider, data);
502 case VPN_STATE_DISCONNECT:
504 case VPN_STATE_FAILURE:
505 case VPN_STATE_AUTH_FAILURE:
506 data->state = VPN_STATE_IDLE;
509 case VPN_STATE_CONNECT:
512 case VPN_STATE_READY:
516 name = vpn_provider_get_driver_name(provider);
520 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
522 if (!vpn_driver_data || !vpn_driver_data->vpn_driver) {
527 if (vpn_driver_data->vpn_driver->flags != VPN_FLAG_NO_TUN) {
528 ret = vpn_create_tun(provider);
533 data->task = connman_task_create(vpn_driver_data->program);
541 if (connman_task_set_notify(data->task, "notify",
542 vpn_notify, provider)) {
545 connman_task_destroy(data->task);
551 if(vpn_driver_data->vpn_driver->set_event_cb)
552 vpn_driver_data->vpn_driver->set_event_cb(vpn_event, provider);
554 ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
555 data->if_name, cb, dbus_sender,
557 if (ret < 0 && ret != -EINPROGRESS) {
559 connman_task_destroy(data->task);
564 DBG("%s started with dev %s",
565 vpn_driver_data->provider_driver.name, data->if_name);
567 data->state = VPN_STATE_CONNECT;
572 vpn_provider_set_index(provider, -1);
573 vpn_provider_set_data(provider, NULL);
574 vpn_provider_unref(data->provider);
575 g_free(data->if_name);
581 static int vpn_probe(struct vpn_provider *provider)
586 static int vpn_disconnect(struct vpn_provider *provider)
588 struct vpn_data *data = vpn_provider_get_data(provider);
589 struct vpn_driver_data *vpn_driver_data;
592 DBG("disconnect provider %p:", provider);
597 name = vpn_provider_get_driver_name(provider);
601 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
602 if (vpn_driver_data->vpn_driver->disconnect)
603 vpn_driver_data->vpn_driver->disconnect(provider);
605 if (data->watch != 0) {
606 vpn_provider_unref(provider);
607 vpn_rtnl_remove_watch(data->watch);
611 data->state = VPN_STATE_DISCONNECT;
612 connman_task_stop(data->task);
617 static int vpn_remove(struct vpn_provider *provider)
619 struct vpn_data *data;
621 data = vpn_provider_get_data(provider);
625 if (data->watch != 0) {
626 vpn_provider_unref(provider);
627 vpn_rtnl_remove_watch(data->watch);
631 connman_task_stop(data->task);
633 g_usleep(G_USEC_PER_SEC);
638 static int vpn_save(struct vpn_provider *provider, GKeyFile *keyfile)
640 struct vpn_driver_data *vpn_driver_data;
643 name = vpn_provider_get_driver_name(provider);
644 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
645 if (vpn_driver_data &&
646 vpn_driver_data->vpn_driver->save)
647 return vpn_driver_data->vpn_driver->save(provider, keyfile);
652 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
655 struct vpn_driver_data *data;
657 data = g_try_new0(struct vpn_driver_data, 1);
662 data->program = program;
664 data->vpn_driver = vpn_driver;
666 data->provider_driver.name = name;
667 data->provider_driver.disconnect = vpn_disconnect;
668 data->provider_driver.connect = vpn_connect;
669 data->provider_driver.probe = vpn_probe;
670 data->provider_driver.remove = vpn_remove;
671 data->provider_driver.save = vpn_save;
674 driver_hash = g_hash_table_new_full(g_str_hash,
679 connman_error("driver_hash not initialized for %s", name);
684 g_hash_table_replace(driver_hash, (char *)name, data);
686 vpn_provider_driver_register(&data->provider_driver);
691 void vpn_unregister(const char *name)
693 struct vpn_driver_data *data;
695 data = g_hash_table_lookup(driver_hash, name);
699 vpn_provider_driver_unregister(&data->provider_driver);
701 g_hash_table_remove(driver_hash, name);
703 if (g_hash_table_size(driver_hash) == 0)
704 g_hash_table_destroy(driver_hash);