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 int vpn_create_tun(struct vpn_provider *provider)
312 struct vpn_data *data = vpn_provider_get_data(provider);
320 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
323 connman_error("Failed to open /dev/net/tun: %s",
329 memset(&ifr, 0, sizeof(ifr));
330 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
332 for (i = 0; i < 256; i++) {
333 sprintf(ifr.ifr_name, "vpn%d", i);
335 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
340 connman_error("Failed to find available tun device");
346 data->if_name = (char *)g_strdup(ifr.ifr_name);
347 if (!data->if_name) {
348 connman_error("Failed to allocate memory");
354 if (ioctl(fd, TUNSETPERSIST, 1)) {
356 connman_error("Failed to set tun persistent: %s",
365 index = connman_inet_ifindex(data->if_name);
367 connman_error("Failed to get tun ifindex");
372 vpn_provider_set_index(provider, index);
380 static int vpn_connect(struct vpn_provider *provider,
381 vpn_provider_connect_cb_t cb,
382 const char *dbus_sender, void *user_data)
384 struct vpn_data *data = vpn_provider_get_data(provider);
385 struct vpn_driver_data *vpn_driver_data;
388 enum vpn_state state = VPN_STATE_UNKNOWN;
393 DBG("data %p state %d", data, state);
396 case VPN_STATE_UNKNOWN:
397 data = g_try_new0(struct vpn_data, 1);
401 data->provider = vpn_provider_ref(provider);
406 vpn_provider_set_data(provider, data);
409 case VPN_STATE_DISCONNECT:
411 case VPN_STATE_FAILURE:
412 case VPN_STATE_AUTH_FAILURE:
413 data->state = VPN_STATE_IDLE;
416 case VPN_STATE_CONNECT:
419 case VPN_STATE_READY:
423 name = vpn_provider_get_driver_name(provider);
427 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
429 if (!vpn_driver_data || !vpn_driver_data->vpn_driver) {
434 if (vpn_driver_data->vpn_driver->flags != VPN_FLAG_NO_TUN) {
435 ret = vpn_create_tun(provider);
440 data->task = connman_task_create(vpn_driver_data->program);
448 if (connman_task_set_notify(data->task, "notify",
449 vpn_notify, provider)) {
452 connman_task_destroy(data->task);
457 ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
458 data->if_name, cb, dbus_sender,
460 if (ret < 0 && ret != -EINPROGRESS) {
462 connman_task_destroy(data->task);
467 DBG("%s started with dev %s",
468 vpn_driver_data->provider_driver.name, data->if_name);
470 data->state = VPN_STATE_CONNECT;
475 vpn_provider_set_index(provider, -1);
476 vpn_provider_set_data(provider, NULL);
477 vpn_provider_unref(data->provider);
478 g_free(data->if_name);
484 static int vpn_probe(struct vpn_provider *provider)
489 static int vpn_disconnect(struct vpn_provider *provider)
491 struct vpn_data *data = vpn_provider_get_data(provider);
492 struct vpn_driver_data *vpn_driver_data;
495 DBG("disconnect provider %p:", provider);
500 name = vpn_provider_get_driver_name(provider);
504 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
505 if (vpn_driver_data->vpn_driver->disconnect)
506 vpn_driver_data->vpn_driver->disconnect(provider);
508 if (data->watch != 0) {
509 vpn_provider_unref(provider);
510 vpn_rtnl_remove_watch(data->watch);
514 data->state = VPN_STATE_DISCONNECT;
515 connman_task_stop(data->task);
520 static int vpn_remove(struct vpn_provider *provider)
522 struct vpn_data *data;
524 data = vpn_provider_get_data(provider);
528 if (data->watch != 0) {
529 vpn_provider_unref(provider);
530 vpn_rtnl_remove_watch(data->watch);
534 connman_task_stop(data->task);
536 g_usleep(G_USEC_PER_SEC);
541 static int vpn_save(struct vpn_provider *provider, GKeyFile *keyfile)
543 struct vpn_driver_data *vpn_driver_data;
546 name = vpn_provider_get_driver_name(provider);
547 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
548 if (vpn_driver_data &&
549 vpn_driver_data->vpn_driver->save)
550 return vpn_driver_data->vpn_driver->save(provider, keyfile);
555 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
558 struct vpn_driver_data *data;
560 data = g_try_new0(struct vpn_driver_data, 1);
565 data->program = program;
567 data->vpn_driver = vpn_driver;
569 data->provider_driver.name = name;
570 data->provider_driver.disconnect = vpn_disconnect;
571 data->provider_driver.connect = vpn_connect;
572 data->provider_driver.probe = vpn_probe;
573 data->provider_driver.remove = vpn_remove;
574 data->provider_driver.save = vpn_save;
577 driver_hash = g_hash_table_new_full(g_str_hash,
582 connman_error("driver_hash not initialized for %s", name);
587 g_hash_table_replace(driver_hash, (char *)name, data);
589 vpn_provider_driver_register(&data->provider_driver);
594 void vpn_unregister(const char *name)
596 struct vpn_driver_data *data;
598 data = g_hash_table_lookup(driver_hash, name);
602 vpn_provider_driver_unregister(&data->provider_driver);
604 g_hash_table_remove(driver_hash, name);
606 if (g_hash_table_size(driver_hash) == 0)
607 g_hash_table_destroy(driver_hash);