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
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/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 stop_vpn(struct connman_provider *provider)
70 struct vpn_data *data = connman_provider_get_data(provider);
71 struct vpn_driver_data *vpn_driver_data;
79 name = connman_provider_get_driver_name(provider);
83 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
85 if (vpn_driver_data != NULL && vpn_driver_data->vpn_driver != NULL &&
86 vpn_driver_data->vpn_driver->flags == VPN_FLAG_NO_TUN)
89 memset(&ifr, 0, sizeof(ifr));
90 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
91 sprintf(ifr.ifr_name, "%s", data->if_name);
93 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
96 connman_error("Failed to open /dev/net/tun to device %s: %s",
97 data->if_name, strerror(errno));
101 if (ioctl(fd, TUNSETIFF, (void *)&ifr)) {
103 connman_error("Failed to TUNSETIFF for device %s to it: %s",
104 data->if_name, strerror(errno));
109 if (ioctl(fd, TUNSETPERSIST, 0)) {
111 connman_error("Failed to set tun device %s nonpersistent: %s",
112 data->if_name, strerror(errno));
117 DBG("Killed tun device %s", data->if_name);
121 void vpn_died(struct connman_task *task, int exit_code, void *user_data)
123 struct connman_provider *provider = user_data;
124 struct vpn_data *data = connman_provider_get_data(provider);
125 int state = VPN_STATE_FAILURE;
126 enum connman_provider_error ret;
128 DBG("provider %p data %p", provider, data);
136 connman_provider_set_data(provider, NULL);
137 connman_provider_unref(provider);
138 connman_rtnl_remove_watch(data->watch);
141 if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT) {
143 struct vpn_driver_data *vpn_data = NULL;
145 name = connman_provider_get_driver_name(provider);
147 vpn_data = g_hash_table_lookup(driver_hash, name);
149 if (vpn_data != NULL &&
150 vpn_data->vpn_driver->error_code != NULL)
151 ret = vpn_data->vpn_driver->error_code(exit_code);
153 ret = CONNMAN_PROVIDER_ERROR_UNKNOWN;
155 connman_provider_indicate_error(provider, ret);
157 connman_provider_set_state(provider,
158 CONNMAN_PROVIDER_STATE_IDLE);
160 connman_provider_set_index(provider, -1);
161 connman_provider_unref(data->provider);
163 g_free(data->if_name);
166 connman_task_destroy(task);
169 int vpn_set_ifname(struct connman_provider *provider, const char *ifname)
171 struct vpn_data *data = connman_provider_get_data(provider);
174 if (ifname == NULL || data == NULL)
177 index = connman_inet_ifindex(ifname);
181 if (data->if_name != NULL)
182 g_free(data->if_name);
184 data->if_name = (char *)g_strdup(ifname);
185 connman_provider_set_index(provider, index);
190 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
192 struct connman_provider *provider = user_data;
193 struct vpn_data *data = connman_provider_get_data(provider);
195 if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
196 if (flags & IFF_UP) {
197 data->state = VPN_STATE_READY;
198 connman_provider_set_state(provider,
199 CONNMAN_PROVIDER_STATE_READY);
205 static DBusMessage *vpn_notify(struct connman_task *task,
206 DBusMessage *msg, void *user_data)
208 struct connman_provider *provider = user_data;
209 struct vpn_data *data;
210 struct vpn_driver_data *vpn_driver_data;
214 data = connman_provider_get_data(provider);
216 name = connman_provider_get_driver_name(provider);
220 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
221 if (vpn_driver_data == NULL)
224 state = vpn_driver_data->vpn_driver->notify(msg, provider);
226 case VPN_STATE_CONNECT:
227 case VPN_STATE_READY:
228 index = connman_provider_get_index(provider);
229 connman_provider_ref(provider);
230 data->watch = connman_rtnl_add_newlink_watch(index,
231 vpn_newlink, provider);
232 connman_inet_ifup(index);
235 case VPN_STATE_UNKNOWN:
237 case VPN_STATE_DISCONNECT:
238 case VPN_STATE_FAILURE:
239 connman_provider_set_state(provider,
240 CONNMAN_PROVIDER_STATE_DISCONNECT);
243 case VPN_STATE_AUTH_FAILURE:
244 connman_provider_indicate_error(provider,
245 CONNMAN_PROVIDER_ERROR_AUTH_FAILED);
252 static int vpn_create_tun(struct connman_provider *provider)
254 struct vpn_data *data = connman_provider_get_data(provider);
262 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
265 connman_error("Failed to open /dev/net/tun: %s",
271 memset(&ifr, 0, sizeof(ifr));
272 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
274 for (i = 0; i < 256; i++) {
275 sprintf(ifr.ifr_name, "vpn%d", i);
277 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
282 connman_error("Failed to find available tun device");
288 data->if_name = (char *)g_strdup(ifr.ifr_name);
289 if (data->if_name == NULL) {
290 connman_error("Failed to allocate memory");
296 if (ioctl(fd, TUNSETPERSIST, 1)) {
298 connman_error("Failed to set tun persistent: %s",
307 index = connman_inet_ifindex(data->if_name);
309 connman_error("Failed to get tun ifindex");
314 connman_provider_set_index(provider, index);
322 static int vpn_connect(struct connman_provider *provider)
324 struct vpn_data *data = connman_provider_get_data(provider);
325 struct vpn_driver_data *vpn_driver_data;
332 data = g_try_new0(struct vpn_data, 1);
336 data->provider = connman_provider_ref(provider);
340 data->state = VPN_STATE_IDLE;
342 connman_provider_set_data(provider, data);
344 name = connman_provider_get_driver_name(provider);
348 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
350 if (vpn_driver_data != NULL && vpn_driver_data->vpn_driver != NULL &&
351 vpn_driver_data->vpn_driver->flags != VPN_FLAG_NO_TUN) {
353 ret = vpn_create_tun(provider);
358 data->task = connman_task_create(vpn_driver_data->program);
360 if (data->task == NULL) {
366 if (connman_task_set_notify(data->task, "notify",
367 vpn_notify, provider)) {
370 connman_task_destroy(data->task);
375 ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
379 connman_task_destroy(data->task);
384 DBG("%s started with dev %s",
385 vpn_driver_data->provider_driver.name, data->if_name);
387 data->state = VPN_STATE_CONNECT;
392 connman_provider_set_index(provider, -1);
393 connman_provider_set_data(provider, NULL);
394 connman_provider_unref(data->provider);
395 g_free(data->if_name);
401 static int vpn_probe(struct connman_provider *provider)
406 static int vpn_disconnect(struct connman_provider *provider)
408 struct vpn_data *data = connman_provider_get_data(provider);
409 struct vpn_driver_data *vpn_driver_data;
412 DBG("disconnect provider %p:", provider);
417 name = connman_provider_get_driver_name(provider);
421 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
422 if (vpn_driver_data->vpn_driver->disconnect)
423 vpn_driver_data->vpn_driver->disconnect();
425 if (data->watch != 0) {
426 connman_provider_unref(provider);
427 connman_rtnl_remove_watch(data->watch);
431 data->state = VPN_STATE_DISCONNECT;
432 connman_task_stop(data->task);
437 static int vpn_remove(struct connman_provider *provider)
439 struct vpn_data *data;
441 data = connman_provider_get_data(provider);
445 if (data->watch != 0) {
446 connman_provider_unref(provider);
447 connman_rtnl_remove_watch(data->watch);
451 connman_task_stop(data->task);
453 g_usleep(G_USEC_PER_SEC);
458 static int vpn_save (struct connman_provider *provider, GKeyFile *keyfile)
460 struct vpn_driver_data *vpn_driver_data;
463 name = connman_provider_get_driver_name(provider);
464 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
465 if (vpn_driver_data != NULL &&
466 vpn_driver_data->vpn_driver->save != NULL)
467 return vpn_driver_data->vpn_driver->save(provider, keyfile);
472 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
475 struct vpn_driver_data *data;
477 data = g_try_new0(struct vpn_driver_data, 1);
482 data->program = program;
484 data->vpn_driver = vpn_driver;
486 data->provider_driver.name = name;
487 data->provider_driver.disconnect = vpn_disconnect;
488 data->provider_driver.connect = vpn_connect;
489 data->provider_driver.probe = vpn_probe;
490 data->provider_driver.remove = vpn_remove;
491 data->provider_driver.save = vpn_save;
493 if (driver_hash == NULL)
494 driver_hash = g_hash_table_new_full(g_str_hash,
498 if (driver_hash == NULL) {
499 connman_error("driver_hash not initialized for %s", name);
504 g_hash_table_insert(driver_hash, (char *)name, data);
506 connman_provider_driver_register(&data->provider_driver);
511 void vpn_unregister(const char *name)
513 struct vpn_driver_data *data;
515 data = g_hash_table_lookup(driver_hash, name);
519 connman_provider_driver_unregister(&data->provider_driver);
521 g_hash_table_remove(driver_hash, name);
523 if (g_hash_table_size(driver_hash) == 0)
524 g_hash_table_destroy(driver_hash);