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
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <linux/if_tun.h>
36 #include <sys/types.h>
40 #include <dbus/dbus.h>
42 #include <glib/gprintf.h>
44 #include <connman/log.h>
45 #include <connman/rtnl.h>
46 #include <connman/task.h>
47 #include <connman/inet.h>
49 #include "../vpn-rtnl.h"
50 #include "../vpn-provider.h"
56 struct vpn_provider *provider;
61 struct connman_task *task;
65 struct vpn_driver_data {
68 const struct vpn_driver *vpn_driver;
69 struct vpn_provider_driver provider_driver;
72 GHashTable *driver_hash = NULL;
74 static int stop_vpn(struct vpn_provider *provider)
76 struct vpn_data *data = vpn_provider_get_data(provider);
77 struct vpn_driver_data *vpn_driver_data;
85 name = vpn_provider_get_driver_name(provider);
89 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
91 if (vpn_driver_data && vpn_driver_data->vpn_driver &&
92 vpn_driver_data->vpn_driver->flags & VPN_FLAG_NO_TUN) {
93 vpn_driver_data->vpn_driver->disconnect(data->provider);
97 memset(&ifr, 0, sizeof(ifr));
98 ifr.ifr_flags = data->tun_flags | IFF_NO_PI;
99 sprintf(ifr.ifr_name, "%s", data->if_name);
101 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
104 connman_error("Failed to open /dev/net/tun to device %s: %s",
105 data->if_name, strerror(errno));
109 if (ioctl(fd, TUNSETIFF, (void *)&ifr)) {
111 connman_error("Failed to TUNSETIFF for device %s to it: %s",
112 data->if_name, strerror(errno));
117 if (ioctl(fd, TUNSETPERSIST, 0)) {
119 connman_error("Failed to set tun device %s nonpersistent: %s",
120 data->if_name, strerror(errno));
125 DBG("Killed tun device %s", data->if_name);
129 void vpn_died(struct connman_task *task, int exit_code, void *user_data)
131 struct vpn_provider *provider = user_data;
132 struct vpn_data *data = vpn_provider_get_data(provider);
133 int state = VPN_STATE_FAILURE;
134 enum vpn_provider_error ret;
136 DBG("provider %p data %p", provider, data);
141 /* The task may die after we have already started the new one */
142 if (data->task != task)
148 vpn_provider_set_data(provider, NULL);
150 if (data->watch != 0) {
151 vpn_rtnl_remove_watch(data->watch);
153 vpn_provider_unref(provider);
157 if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT) {
159 struct vpn_driver_data *vpn_data = NULL;
161 name = vpn_provider_get_driver_name(provider);
163 vpn_data = g_hash_table_lookup(driver_hash, name);
166 vpn_data->vpn_driver->error_code)
167 ret = vpn_data->vpn_driver->error_code(provider,
170 ret = VPN_PROVIDER_ERROR_UNKNOWN;
172 vpn_provider_indicate_error(provider, ret);
174 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_IDLE);
176 vpn_provider_set_index(provider, -1);
179 vpn_provider_unref(data->provider);
180 g_free(data->if_name);
185 connman_task_destroy(task);
188 int vpn_set_ifname(struct vpn_provider *provider, const char *ifname)
190 struct vpn_data *data = vpn_provider_get_data(provider);
193 if (!ifname || !data)
196 index = connman_inet_ifindex(ifname);
201 g_free(data->if_name);
203 data->if_name = (char *)g_strdup(ifname);
204 vpn_provider_set_index(provider, index);
209 static int vpn_set_state(struct vpn_provider *provider,
210 enum vpn_provider_state state)
212 struct vpn_data *data = vpn_provider_get_data(provider);
217 case VPN_PROVIDER_STATE_UNKNOWN:
219 case VPN_PROVIDER_STATE_IDLE:
220 data->state = VPN_STATE_IDLE;
222 case VPN_PROVIDER_STATE_CONNECT:
223 case VPN_PROVIDER_STATE_READY:
224 data->state = VPN_STATE_CONNECT;
226 case VPN_PROVIDER_STATE_DISCONNECT:
227 data->state = VPN_STATE_DISCONNECT;
229 case VPN_PROVIDER_STATE_FAILURE:
230 data->state = VPN_STATE_FAILURE;
237 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
239 struct vpn_provider *provider = user_data;
240 struct vpn_data *data = vpn_provider_get_data(provider);
242 if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
243 if (flags & IFF_UP) {
244 data->state = VPN_STATE_READY;
245 vpn_provider_set_state(provider,
246 VPN_PROVIDER_STATE_READY);
252 static DBusMessage *vpn_notify(struct connman_task *task,
253 DBusMessage *msg, void *user_data)
255 struct vpn_provider *provider = user_data;
256 struct vpn_data *data;
257 struct vpn_driver_data *vpn_driver_data;
259 int state, index, err;
261 data = vpn_provider_get_data(provider);
263 name = vpn_provider_get_driver_name(provider);
266 DBG("Cannot find VPN driver for provider %p", provider);
267 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
271 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
272 if (!vpn_driver_data) {
273 DBG("Cannot find VPN driver data for name %s", name);
274 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
278 state = vpn_driver_data->vpn_driver->notify(msg, provider);
280 DBG("provider %p driver %s state %d", provider, name, state);
283 case VPN_STATE_CONNECT:
284 case VPN_STATE_READY:
285 if (data->state == VPN_STATE_READY) {
287 * This is the restart case, in which case we must
288 * just set the IP address.
290 * We need to remove first the old address, just
291 * replacing the old address will not work as expected
292 * because the old address will linger in the interface
293 * and not disappear so the clearing is needed here.
295 * Also the state must change, otherwise the routes
296 * will not be set properly.
298 vpn_provider_set_state(provider,
299 VPN_PROVIDER_STATE_CONNECT);
301 vpn_provider_clear_address(provider, AF_INET);
302 vpn_provider_clear_address(provider, AF_INET6);
304 vpn_provider_change_address(provider);
305 vpn_provider_set_state(provider,
306 VPN_PROVIDER_STATE_READY);
310 index = vpn_provider_get_index(provider);
311 vpn_provider_ref(provider);
312 data->watch = vpn_rtnl_add_newlink_watch(index,
313 vpn_newlink, provider);
314 err = connman_inet_ifup(index);
316 if (err == -EALREADY) {
318 * So the interface is up already, that is just
319 * great. Unfortunately in this case the
320 * newlink watch might not have been called at
321 * all. We must manually call it here so that
322 * the provider can go to ready state and the
323 * routes are setup properly. Also reset flags
324 * so vpn_newlink() can handle the change.
327 vpn_newlink(IFF_UP, 0, provider);
329 DBG("Cannot take interface %d up err %d/%s",
330 index, -err, strerror(-err));
335 case VPN_STATE_UNKNOWN:
337 case VPN_STATE_DISCONNECT:
338 case VPN_STATE_FAILURE:
339 vpn_provider_set_state(provider,
340 VPN_PROVIDER_STATE_DISCONNECT);
343 case VPN_STATE_AUTH_FAILURE:
344 vpn_provider_indicate_error(provider,
345 VPN_PROVIDER_ERROR_AUTH_FAILED);
352 static int vpn_create_tun(struct vpn_provider *provider, int flags)
354 struct vpn_data *data = vpn_provider_get_data(provider);
362 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
365 connman_error("Failed to open /dev/net/tun: %s",
371 memset(&ifr, 0, sizeof(ifr));
372 ifr.ifr_flags = flags | IFF_NO_PI;
374 for (i = 0; i < 256; i++) {
375 sprintf(ifr.ifr_name, "vpn%d", i);
377 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
382 connman_error("Failed to find available tun device");
388 data->tun_flags = flags;
389 g_free(data->if_name);
390 data->if_name = (char *)g_strdup(ifr.ifr_name);
391 if (!data->if_name) {
392 connman_error("Failed to allocate memory");
398 if (ioctl(fd, TUNSETPERSIST, 1)) {
400 connman_error("Failed to set tun persistent: %s",
409 index = connman_inet_ifindex(data->if_name);
411 connman_error("Failed to get tun ifindex");
416 vpn_provider_set_index(provider, index);
424 static gid_t get_gid(const char *group_name)
428 grp = vpn_util_get_group(group_name);
435 static uid_t get_uid(const char *user_name)
439 pw = vpn_util_get_passwd(user_name);
446 static gint get_supplementary_gids(gchar **groups, gid_t **gid_list)
448 gint group_count = 0;
453 for(i = 0; groups[i]; i++) {
456 list = (gid_t*)g_try_realloc(list,
457 sizeof(gid_t) * group_count);
460 DBG("cannot allocate supplementary group list");
464 list[i] = get_gid(groups[i]);
473 static void vpn_task_setup(gpointer user_data)
475 struct vpn_plugin_data *data;
478 gid_t *gid_list = NULL;
479 size_t gid_list_size;
482 gchar **suppl_groups;
485 user = vpn_settings_get_binary_user(data);
486 group = vpn_settings_get_binary_group(data);
487 suppl_groups = vpn_settings_get_binary_supplementary_groups(data);
490 gid = get_gid(group);
491 gid_list_size = get_supplementary_gids(suppl_groups, &gid_list);
493 DBG("vpn_task_setup uid:%d gid:%d supplementary group list size:%zu",
494 uid, gid, gid_list_size);
497 /* Change group if proper group name was set, requires CAP_SETGID.*/
498 if (gid > 0 && setgid(gid))
499 connman_error("error setting gid %d %s", gid, strerror(errno));
501 /* Set the supplementary groups if list exists, requires CAP_SETGID. */
502 if (gid_list_size && gid_list && setgroups(gid_list_size, gid_list))
503 connman_error("error setting gid list %s", strerror(errno));
505 /* Change user for the task if set, requires CAP_SETUID */
506 if (uid > 0 && setuid(uid))
507 connman_error("error setting uid %d %s", uid, strerror(errno));
511 static gboolean update_provider_state(gpointer data)
513 struct vpn_provider *provider = data;
514 struct vpn_data *vpn_data;
519 vpn_data = vpn_provider_get_data(provider);
521 index = vpn_provider_get_index(provider);
522 DBG("index to watch %d", index);
523 vpn_provider_ref(provider);
524 vpn_data->watch = vpn_rtnl_add_newlink_watch(index,
525 vpn_newlink, provider);
526 connman_inet_ifup(index);
531 static int vpn_connect(struct vpn_provider *provider,
532 vpn_provider_connect_cb_t cb,
533 const char *dbus_sender, void *user_data)
535 struct vpn_data *data = vpn_provider_get_data(provider);
536 struct vpn_driver_data *vpn_driver_data;
537 struct vpn_plugin_data *vpn_plugin_data;
539 int ret = 0, tun_flags = IFF_TUN;
540 enum vpn_state state = VPN_STATE_UNKNOWN;
545 DBG("data %p state %d", data, state);
548 case VPN_STATE_UNKNOWN:
549 data = g_try_new0(struct vpn_data, 1);
553 data->provider = vpn_provider_ref(provider);
558 vpn_provider_set_data(provider, data);
561 case VPN_STATE_DISCONNECT:
563 case VPN_STATE_FAILURE:
564 case VPN_STATE_AUTH_FAILURE:
565 data->state = VPN_STATE_IDLE;
568 case VPN_STATE_CONNECT:
571 case VPN_STATE_READY:
575 name = vpn_provider_get_driver_name(provider);
579 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
581 if (!vpn_driver_data || !vpn_driver_data->vpn_driver) {
586 if (!(vpn_driver_data->vpn_driver->flags & VPN_FLAG_NO_TUN)) {
587 if (vpn_driver_data->vpn_driver->device_flags) {
588 tun_flags = vpn_driver_data->vpn_driver->device_flags(provider);
590 ret = vpn_create_tun(provider, tun_flags);
596 if (vpn_driver_data && vpn_driver_data->vpn_driver &&
597 vpn_driver_data->vpn_driver->flags & VPN_FLAG_NO_DAEMON) {
599 ret = vpn_driver_data->vpn_driver->connect(provider,
600 NULL, NULL, cb, dbus_sender, user_data);
606 DBG("%s started with dev %s",
607 vpn_driver_data->provider_driver.name, data->if_name);
609 data->state = VPN_STATE_CONNECT;
611 g_timeout_add(1, update_provider_state, provider);
616 vpn_settings_get_vpn_plugin_config(vpn_driver_data->name);
617 data->task = connman_task_create(vpn_driver_data->program,
618 vpn_task_setup, vpn_plugin_data);
626 if (connman_task_set_notify(data->task, "notify",
627 vpn_notify, provider)) {
630 connman_task_destroy(data->task);
635 ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
636 data->if_name, cb, dbus_sender,
638 if (ret < 0 && ret != -EINPROGRESS) {
640 connman_task_destroy(data->task);
645 DBG("%s started with dev %s",
646 vpn_driver_data->provider_driver.name, data->if_name);
648 data->state = VPN_STATE_CONNECT;
653 vpn_provider_set_index(provider, -1);
654 vpn_provider_set_data(provider, NULL);
655 vpn_provider_unref(data->provider);
656 g_free(data->if_name);
662 static int vpn_probe(struct vpn_provider *provider)
667 static int vpn_disconnect(struct vpn_provider *provider)
669 struct vpn_data *data = vpn_provider_get_data(provider);
670 struct vpn_driver_data *vpn_driver_data;
673 DBG("disconnect provider %p:", provider);
678 name = vpn_provider_get_driver_name(provider);
682 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
683 if (vpn_driver_data->vpn_driver->disconnect)
684 vpn_driver_data->vpn_driver->disconnect(provider);
686 if (data->watch != 0) {
687 vpn_provider_unref(provider);
688 vpn_rtnl_remove_watch(data->watch);
692 data->state = VPN_STATE_DISCONNECT;
694 if (!vpn_driver_data->vpn_driver->disconnect) {
695 DBG("Driver has no disconnect() implementation, set provider "
696 "state to disconnect.");
697 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_DISCONNECT);
701 connman_task_stop(data->task);
706 static int vpn_remove(struct vpn_provider *provider)
708 struct vpn_data *data;
709 struct vpn_driver_data *driver_data;
713 data = vpn_provider_get_data(provider);
714 name = vpn_provider_get_driver_name(provider);
719 if (data->watch != 0) {
720 vpn_provider_unref(provider);
721 vpn_rtnl_remove_watch(data->watch);
726 connman_task_stop(data->task);
728 g_usleep(G_USEC_PER_SEC);
735 driver_data = g_hash_table_lookup(driver_hash, name);
737 if (driver_data && driver_data->vpn_driver->remove)
738 err = driver_data->vpn_driver->remove(provider);
741 DBG("%p vpn_driver->remove() returned %d", provider, err);
746 static int vpn_save(struct vpn_provider *provider, GKeyFile *keyfile)
748 struct vpn_driver_data *vpn_driver_data;
751 name = vpn_provider_get_driver_name(provider);
752 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
753 if (vpn_driver_data &&
754 vpn_driver_data->vpn_driver->save)
755 return vpn_driver_data->vpn_driver->save(provider, keyfile);
760 static int vpn_route_env_parse(struct vpn_provider *provider, const char *key,
761 int *family, unsigned long *idx,
762 enum vpn_provider_route_type *type)
764 struct vpn_driver_data *vpn_driver_data = NULL;
765 const char *name = NULL;
770 name = vpn_provider_get_driver_name(provider);
771 vpn_driver_data = g_hash_table_lookup(driver_hash, name);
773 if (vpn_driver_data && vpn_driver_data->vpn_driver->route_env_parse)
774 return vpn_driver_data->vpn_driver->route_env_parse(provider, key,
780 int vpn_register(const char *name, const struct vpn_driver *vpn_driver,
783 struct vpn_driver_data *data;
785 data = g_try_new0(struct vpn_driver_data, 1);
790 data->program = program;
792 if (vpn_settings_parse_vpn_plugin_config(data->name) != 0)
793 DBG("No configuration provided for VPN plugin %s", data->name);
795 data->vpn_driver = vpn_driver;
797 data->provider_driver.name = name;
798 data->provider_driver.disconnect = vpn_disconnect;
799 data->provider_driver.connect = vpn_connect;
800 data->provider_driver.probe = vpn_probe;
801 data->provider_driver.remove = vpn_remove;
802 data->provider_driver.save = vpn_save;
803 data->provider_driver.set_state = vpn_set_state;
804 data->provider_driver.route_env_parse = vpn_route_env_parse;
807 driver_hash = g_hash_table_new_full(g_str_hash,
812 connman_error("driver_hash not initialized for %s", name);
817 g_hash_table_replace(driver_hash, (char *)name, data);
819 vpn_provider_driver_register(&data->provider_driver);
824 void vpn_unregister(const char *name)
826 struct vpn_driver_data *data;
828 data = g_hash_table_lookup(driver_hash, name);
832 vpn_provider_driver_unregister(&data->provider_driver);
833 vpn_settings_delete_vpn_plugin_config(name);
835 g_hash_table_remove(driver_hash, name);
837 if (g_hash_table_size(driver_hash) == 0)
838 g_hash_table_destroy(driver_hash);