5 * Copyright (C) 2007-2010 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
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <linux/if_tun.h>
37 #include <dbus/dbus.h>
39 #include <glib/ghash.h>
40 #include <glib/gprintf.h>
42 #include <connman/provider.h>
43 #include <connman/log.h>
44 #include <connman/rtnl.h>
45 #include <connman/task.h>
46 #include <connman/inet.h>
51 struct connman_provider *provider;
56 struct connman_task *task;
59 struct vpn_driver_data {
62 struct vpn_driver *vpn_driver;
63 struct connman_provider_driver provider_driver;
66 GHashTable *driver_hash = NULL;
68 static int kill_tun(char *tun_name)
73 memset(&ifr, 0, sizeof(ifr));
74 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
75 sprintf(ifr.ifr_name, "%s", tun_name);
77 fd = open("/dev/net/tun", O_RDWR);
80 connman_error("Failed to open /dev/net/tun to device %s: %s",
81 tun_name, strerror(errno));
85 if (ioctl(fd, TUNSETIFF, (void *)&ifr)) {
87 connman_error("Failed to TUNSETIFF for device %s to it: %s",
88 tun_name, strerror(errno));
93 if (ioctl(fd, TUNSETPERSIST, 0)) {
95 connman_error("Failed to set tun device %s nonpersistent: %s",
96 tun_name, strerror(errno));
101 DBG("Killed tun device %s", tun_name);
105 void vpn_died(struct connman_task *task, int exit_code, void *user_data)
107 struct connman_provider *provider = user_data;
108 struct vpn_data *data = connman_provider_get_data(provider);
109 int state = VPN_STATE_FAILURE;
110 enum connman_provider_error ret;
112 DBG("provider %p data %p", provider, data);
119 kill_tun(data->if_name);
120 connman_provider_set_data(provider, NULL);
121 connman_rtnl_remove_watch(data->watch);
124 if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT) {
126 struct vpn_driver_data *vpn_data;
128 name = connman_provider_get_driver_name(provider);
129 vpn_data = g_hash_table_lookup(driver_hash, name);
130 if (vpn_data != NULL &&
131 vpn_data->vpn_driver->error_code != NULL)
132 ret = vpn_data->vpn_driver->error_code(exit_code);
134 ret = CONNMAN_PROVIDER_ERROR_UNKNOWN;
136 connman_provider_indicate_error(provider, ret);
138 connman_provider_set_state(provider,
139 CONNMAN_PROVIDER_STATE_IDLE);
141 connman_provider_set_index(provider, -1);
142 connman_provider_unref(data->provider);
145 connman_task_destroy(task);
148 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
150 struct connman_provider *provider = user_data;
151 struct vpn_data *data = connman_provider_get_data(provider);
153 if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
154 if (flags & IFF_UP) {
155 data->state = VPN_STATE_READY;
156 connman_provider_set_state(provider,
157 CONNMAN_PROVIDER_STATE_READY);
163 static void vpn_notify(struct connman_task *task,
164 DBusMessage *msg, void *user_data)
166 struct connman_provider *provider = user_data;
167 struct vpn_data *data;
168 struct vpn_driver_data *vpn_driver_data;
172 data = connman_provider_get_data(provider);
174 name = connman_provider_get_driver_name(provider);
175 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
176 if (vpn_driver_data == NULL)
179 state = vpn_driver_data->vpn_driver->notify(msg, provider);
181 case VPN_STATE_CONNECT:
182 case VPN_STATE_READY:
183 index = connman_provider_get_index(provider);
184 data->watch = connman_rtnl_add_newlink_watch(index,
185 vpn_newlink, provider);
186 connman_inet_ifup(index);
189 case VPN_STATE_UNKNOWN:
191 case VPN_STATE_DISCONNECT:
192 case VPN_STATE_FAILURE:
193 connman_provider_set_state(provider,
194 CONNMAN_PROVIDER_STATE_DISCONNECT);
199 static int vpn_connect(struct connman_provider *provider)
201 struct vpn_data *data = connman_provider_get_data(provider);
202 struct vpn_driver_data *vpn_driver_data;
211 data = g_try_new0(struct vpn_data, 1);
215 data->provider = connman_provider_ref(provider);
219 data->state = VPN_STATE_IDLE;
221 connman_provider_set_data(provider, data);
223 name = connman_provider_get_driver_name(provider);
224 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
226 fd = open("/dev/net/tun", O_RDWR);
229 connman_error("Failed to open /dev/net/tun: %s",
235 memset(&ifr, 0, sizeof(ifr));
236 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
238 for (i = 0; i < 256; i++) {
239 sprintf(ifr.ifr_name, "vpn%d", i);
241 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
246 connman_error("Failed to find available tun device");
252 data->if_name = (char *)g_strdup(ifr.ifr_name);
253 if (data->if_name == NULL) {
254 connman_error("Failed to allocate memory");
260 if (ioctl(fd, TUNSETPERSIST, 1)) {
262 connman_error("Failed to set tun persistent: %s",
271 index = connman_inet_ifindex(data->if_name);
273 connman_error("Failed to get tun ifindex");
274 kill_tun(data->if_name);
278 connman_provider_set_index(provider, index);
280 data->task = connman_task_create(vpn_driver_data->program);
282 if (data->task == NULL) {
284 kill_tun(data->if_name);
288 if (connman_task_set_notify(data->task, "notify",
289 vpn_notify, provider)) {
291 kill_tun(data->if_name);
292 connman_task_destroy(data->task);
297 ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
300 kill_tun(data->if_name);
301 connman_task_destroy(data->task);
306 DBG("%s started with dev %s",
307 vpn_driver_data->provider_driver.name, data->if_name);
309 data->state = VPN_STATE_CONNECT;
314 connman_provider_set_index(provider, -1);
315 connman_provider_set_data(provider, NULL);
316 connman_provider_unref(data->provider);
322 static int vpn_probe(struct connman_provider *provider)
327 static int vpn_disconnect(struct connman_provider *provider)
329 struct vpn_data *data = connman_provider_get_data(provider);
330 struct vpn_driver_data *vpn_driver_data;
333 DBG("disconnect provider %p:", provider);
338 name = connman_provider_get_driver_name(provider);
339 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
340 if (vpn_driver_data->vpn_driver->disconnect)
341 vpn_driver_data->vpn_driver->disconnect();
343 if (data->watch != 0)
344 connman_rtnl_remove_watch(data->watch);
347 data->state = VPN_STATE_DISCONNECT;
348 connman_task_stop(data->task);
353 static int vpn_remove(struct connman_provider *provider)
355 struct vpn_data *data;
357 data = connman_provider_get_data(provider);
358 connman_provider_set_data(provider, NULL);
362 if (data->watch != 0)
363 connman_rtnl_remove_watch(data->watch);
365 connman_task_stop(data->task);
367 g_usleep(G_USEC_PER_SEC);
368 kill_tun(data->if_name);
372 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
375 struct vpn_driver_data *data;
377 data = g_try_new0(struct vpn_driver_data, 1);
382 data->program = program;
384 data->vpn_driver = vpn_driver;
386 data->provider_driver.name = name;
387 data->provider_driver.disconnect = vpn_disconnect;
388 data->provider_driver.connect = vpn_connect;
389 data->provider_driver.probe = vpn_probe;
390 data->provider_driver.remove = vpn_remove;
392 if (driver_hash == NULL) {
393 driver_hash = g_hash_table_new_full(g_str_hash,
398 g_hash_table_insert(driver_hash, (char *)name, data);
400 connman_provider_driver_register(&data->provider_driver);
405 void vpn_unregister(const char *name)
407 struct vpn_driver_data *data;
409 data = g_hash_table_lookup(driver_hash, name);
413 connman_provider_driver_unregister(&data->provider_driver);
415 g_hash_table_remove(driver_hash, name);
417 if (g_hash_table_size(driver_hash) == 0)
418 g_hash_table_destroy(driver_hash);