*
* Connection Manager
*
- * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
+ * Copyright (C) 2011 ProFUSION embedded systems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#include <config.h>
#endif
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
+#include <string.h>
+#include <fcntl.h>
+#include <netinet/in.h>
#include <linux/sockios.h>
+#include <linux/if_tun.h>
+#include <linux/if_bridge.h>
#include "connman.h"
#include <gdhcp/gdhcp.h>
+#include <gdbus.h>
+
+#ifndef DBUS_TYPE_UNIX_FD
+#define DBUS_TYPE_UNIX_FD -1
+#endif
+
#define BRIDGE_NAME "tether"
-#define BRIDGE_IP "192.168.218.1"
-#define BRIDGE_BCAST "192.168.218.255"
-#define BRIDGE_SUBNET "255.255.255.0"
-#define BRIDGE_IP_START "192.168.218.100"
-#define BRIDGE_IP_END "192.168.218.200"
-#define BRIDGE_DNS "8.8.8.8"
-
-static char *default_interface = NULL;
-static volatile gint tethering_enabled;
+
+#define DEFAULT_MTU 1500
+
+static char *private_network_primary_dns = NULL;
+static char *private_network_secondary_dns = NULL;
+
+static volatile int tethering_enabled;
static GDHCPServer *tethering_dhcp_server = NULL;
+static struct connman_ippool *dhcp_ippool = NULL;
+static DBusConnection *connection;
+static GHashTable *pn_hash;
+
+static GHashTable *clients_table;
+
+struct _clients_notify {
+ int id;
+ GHashTable *remove;
+} *clients_notify;
+
+struct connman_private_network {
+ char *owner;
+ char *path;
+ guint watch;
+ DBusMessage *msg;
+ DBusMessage *reply;
+ int fd;
+ char *interface;
+ int index;
+ guint iface_watch;
+ struct connman_ippool *pool;
+ char *primary_dns;
+ char *secondary_dns;
+};
const char *__connman_tethering_get_bridge(void)
{
+ int sk, err;
+ unsigned long args[3];
+
+ sk = socket(AF_INET, SOCK_STREAM, 0);
+ if (sk < 0)
+ return NULL;
+
+ args[0] = BRCTL_GET_VERSION;
+ args[1] = args[2] = 0;
+ err = ioctl(sk, SIOCGIFBR, &args);
+ close(sk);
+ if (err == -1) {
+ connman_error("Missing support for 802.1d ethernet bridging");
+ return NULL;
+ }
+
return BRIDGE_NAME;
}
}
static GDHCPServer *dhcp_server_start(const char *bridge,
- const char *router, const char* subnet,
+ const char *router, const char *subnet,
const char *start_ip, const char *end_ip,
unsigned int lease_time, const char *dns)
{
return NULL;
dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
- if (dhcp_server == NULL) {
+ if (!dhcp_server) {
dhcp_server_error(error);
return NULL;
}
static void dhcp_server_stop(GDHCPServer *server)
{
- if (server == NULL)
+ if (!server)
return;
g_dhcp_server_unref(server);
}
-static int set_forward_delay(const char *name, unsigned int delay)
+static void tethering_restart(struct connman_ippool *pool, void *user_data)
{
- FILE *f;
- char *forward_delay_path;
-
- forward_delay_path =
- g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
+ DBG("pool %p", pool);
+ __connman_tethering_set_disabled();
+ __connman_tethering_set_enabled();
+}
- if (forward_delay_path == NULL)
- return -ENOMEM;
+static void unregister_client(gpointer key,
+ gpointer value, gpointer user_data)
+{
+ const char *addr = key;
+ __connman_tethering_client_unregister(addr);
+}
- f = fopen(forward_delay_path, "r+");
+static void unregister_all_clients(void)
+{
+ g_hash_table_foreach(clients_table, unregister_client, NULL);
+}
- g_free(forward_delay_path);
+int __connman_tethering_set_enabled(void)
+{
+ int index;
+ int err;
+ const char *gateway;
+ const char *broadcast;
+ const char *subnet_mask;
+ const char *start_ip;
+ const char *end_ip;
+ const char *dns;
+ unsigned char prefixlen;
+ char **ns;
- if (f == NULL)
- return -errno;
+ DBG("enabled %d", tethering_enabled + 1);
- fprintf(f, "%d", delay);
+ if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
+ return 0;
- fclose(f);
+ err = __connman_bridge_create(BRIDGE_NAME);
+ if (err < 0) {
+ __sync_fetch_and_sub(&tethering_enabled, 1);
+ return -EOPNOTSUPP;
+ }
- return 0;
-}
+ index = connman_inet_ifindex(BRIDGE_NAME);
+ dhcp_ippool = __connman_ippool_create(index, 2, 252,
+ tethering_restart, NULL);
+ if (!dhcp_ippool) {
+ connman_error("Fail to create IP pool");
+ __connman_bridge_remove(BRIDGE_NAME);
+ __sync_fetch_and_sub(&tethering_enabled, 1);
+ return -EADDRNOTAVAIL;
+ }
-static int create_bridge(const char *name)
-{
- int sk, err;
+ gateway = __connman_ippool_get_gateway(dhcp_ippool);
+ broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
+ subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
+ start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
+ end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
+
+ err = __connman_bridge_enable(BRIDGE_NAME, gateway,
+ connman_ipaddress_calc_netmask_len(subnet_mask),
+ broadcast);
+ if (err < 0 && err != -EALREADY) {
+ __connman_ippool_free(dhcp_ippool);
+ dhcp_ippool = NULL;
+ __connman_bridge_remove(BRIDGE_NAME);
+ __sync_fetch_and_sub(&tethering_enabled, 1);
+ return -EADDRNOTAVAIL;
+ }
- DBG("name %s", name);
+ ns = connman_setting_get_string_list("FallbackNameservers");
+ if (ns) {
+ if (ns[0]) {
+ g_free(private_network_primary_dns);
+ private_network_primary_dns = g_strdup(ns[0]);
+ }
+ if (ns[1]) {
+ g_free(private_network_secondary_dns);
+ private_network_secondary_dns = g_strdup(ns[1]);
+ }
- sk = socket(AF_INET, SOCK_STREAM, 0);
- if (sk < 0)
- return -EOPNOTSUPP;
+ DBG("Fallback ns primary %s secondary %s",
+ private_network_primary_dns,
+ private_network_secondary_dns);
+ }
- err = ioctl(sk, SIOCBRADDBR, name);
+ dns = gateway;
+ if (__connman_dnsproxy_add_listener(index) < 0) {
+ connman_error("Can't add listener %s to DNS proxy",
+ BRIDGE_NAME);
+ dns = private_network_primary_dns;
+ DBG("Serving %s nameserver to clients", dns);
+ }
- if (err < 0)
+ tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
+ gateway, subnet_mask,
+ start_ip, end_ip,
+ 24 * 3600, dns);
+ if (!tethering_dhcp_server) {
+ __connman_bridge_disable(BRIDGE_NAME);
+ __connman_ippool_free(dhcp_ippool);
+ dhcp_ippool = NULL;
+ __connman_bridge_remove(BRIDGE_NAME);
+ __sync_fetch_and_sub(&tethering_enabled, 1);
return -EOPNOTSUPP;
+ }
- err = set_forward_delay(name, 0);
+ prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
+ err = __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
+ if (err < 0) {
+ connman_error("Cannot enable NAT %d/%s", err, strerror(-err));
+ dhcp_server_stop(tethering_dhcp_server);
+ __connman_bridge_disable(BRIDGE_NAME);
+ __connman_ippool_free(dhcp_ippool);
+ dhcp_ippool = NULL;
+ __connman_bridge_remove(BRIDGE_NAME);
+ __sync_fetch_and_sub(&tethering_enabled, 1);
+ return -EOPNOTSUPP;
+ }
- if (err < 0)
- ioctl(sk, SIOCBRDELBR, name);
+ err = __connman_ipv6pd_setup(BRIDGE_NAME);
+ if (err < 0 && err != -EINPROGRESS)
+ DBG("Cannot setup IPv6 prefix delegation %d/%s", err,
+ strerror(-err));
- close(sk);
+ DBG("tethering started");
- return err;
+ return 0;
}
-static int remove_bridge(const char *name)
+void __connman_tethering_set_disabled(void)
{
- int sk, err;
+ int index;
- DBG("name %s", name);
+ DBG("enabled %d", tethering_enabled - 1);
- sk = socket(AF_INET, SOCK_STREAM, 0);
- if (sk < 0)
- return -EOPNOTSUPP;
+ if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
+ return;
- err = ioctl(sk, SIOCBRDELBR, name);
+ unregister_all_clients();
- close(sk);
+ __connman_ipv6pd_cleanup();
- if (err < 0)
- return -EOPNOTSUPP;
+ index = connman_inet_ifindex(BRIDGE_NAME);
+ __connman_dnsproxy_remove_listener(index);
- return 0;
-}
+ __connman_nat_disable(BRIDGE_NAME);
-static int enable_bridge(const char *name)
-{
- int err, index;
+ dhcp_server_stop(tethering_dhcp_server);
- index = connman_inet_ifindex(name);
- if (index < 0)
- return index;
+ tethering_dhcp_server = NULL;
- err = __connman_inet_modify_address(RTM_NEWADDR,
- NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
- BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
- if (err < 0)
- return err;
+ __connman_bridge_disable(BRIDGE_NAME);
- return connman_inet_ifup(index);
+ __connman_ippool_free(dhcp_ippool);
+ dhcp_ippool = NULL;
+
+ __connman_bridge_remove(BRIDGE_NAME);
+
+ g_free(private_network_primary_dns);
+ private_network_primary_dns = NULL;
+ g_free(private_network_secondary_dns);
+ private_network_secondary_dns = NULL;
+
+ DBG("tethering stopped");
}
-static int disable_bridge(const char *name)
+static void append_client(gpointer key, gpointer value,
+ gpointer user_data)
{
- int index;
+ const char *addr = key;
+ DBusMessageIter *array = user_data;
- index = connman_inet_ifindex(name);
- if (index < 0)
- return index;
+ dbus_message_iter_append_basic(array, DBUS_TYPE_STRING,
+ &addr);
+}
- return connman_inet_ifdown(index);
+void __connman_tethering_list_clients(DBusMessageIter *array)
+{
+ g_hash_table_foreach(clients_table, append_client, array);
}
-static int enable_ip_forward(connman_bool_t enable)
+static void setup_tun_interface(unsigned int flags, unsigned change,
+ void *data)
{
+ struct connman_private_network *pn = data;
+ unsigned char prefixlen;
+ DBusMessageIter array, dict;
+ const char *server_ip;
+ const char *peer_ip;
+ const char *subnet_mask;
+ int err;
- FILE *f;
+ DBG("index %d flags %d change %d", pn->index, flags, change);
- f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
- if (f == NULL)
- return -errno;
+ if (flags & IFF_UP)
+ return;
- if (enable == TRUE)
- fprintf(f, "1");
- else
- fprintf(f, "0");
+ subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
+ server_ip = __connman_ippool_get_start_ip(pn->pool);
+ peer_ip = __connman_ippool_get_end_ip(pn->pool);
+ prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
- fclose(f);
+ if ((__connman_inet_modify_address(RTM_NEWADDR,
+ NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
+ server_ip, peer_ip, prefixlen, NULL)) < 0) {
+ DBG("address setting failed");
+ return;
+ }
- return 0;
+ connman_inet_ifup(pn->index);
+
+ err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
+ if (err < 0) {
+ connman_error("failed to enable NAT");
+ goto error;
+ }
+
+ dbus_message_iter_init_append(pn->reply, &array);
+
+ dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
+ &pn->path);
+
+ connman_dbus_dict_open(&array, &dict);
+
+ connman_dbus_dict_append_basic(&dict, "ServerIPv4",
+ DBUS_TYPE_STRING, &server_ip);
+ connman_dbus_dict_append_basic(&dict, "PeerIPv4",
+ DBUS_TYPE_STRING, &peer_ip);
+ if (pn->primary_dns)
+ connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
+ DBUS_TYPE_STRING, &pn->primary_dns);
+
+ if (pn->secondary_dns)
+ connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
+ DBUS_TYPE_STRING, &pn->secondary_dns);
+
+ connman_dbus_dict_close(&array, &dict);
+
+ dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
+
+ g_dbus_send_message(connection, pn->reply);
+
+ return;
+
+error:
+ pn->reply = __connman_error_failed(pn->msg, -err);
+ g_dbus_send_message(connection, pn->reply);
+
+ g_hash_table_remove(pn_hash, pn->path);
}
-static int enable_nat(const char *interface)
+static void remove_private_network(gpointer user_data)
{
- int err;
+ struct connman_private_network *pn = user_data;
- if (interface == NULL)
- return 0;
+ __connman_nat_disable(BRIDGE_NAME);
+ connman_rtnl_remove_watch(pn->iface_watch);
+ __connman_ippool_free(pn->pool);
- /* Enable IPv4 forwarding */
- err = enable_ip_forward(TRUE);
- if (err < 0)
- return err;
-
- /* POSTROUTING flush */
- err = __connman_iptables_command("-t nat -F POSTROUTING");
- if (err < 0)
- return err;
+ if (pn->watch > 0) {
+ g_dbus_remove_watch(connection, pn->watch);
+ pn->watch = 0;
+ }
- /* Enable masquerading */
- err = __connman_iptables_command("-t nat -A POSTROUTING "
- "-o %s -j MASQUERADE", interface);
- if (err < 0)
- return err;
+ close(pn->fd);
- return __connman_iptables_commit("nat");
+ g_free(pn->interface);
+ g_free(pn->owner);
+ g_free(pn->path);
+ g_free(pn->primary_dns);
+ g_free(pn->secondary_dns);
+ g_free(pn);
}
-static void disable_nat(const char *interface)
+static void owner_disconnect(DBusConnection *conn, void *user_data)
{
- int err;
+ struct connman_private_network *pn = user_data;
- /* Disable IPv4 forwarding */
- enable_ip_forward(FALSE);
+ DBG("%s died", pn->owner);
- /* POSTROUTING flush */
- err = __connman_iptables_command("-t nat -F POSTROUTING");
- if (err < 0)
- return;
+ pn->watch = 0;
- __connman_iptables_commit("nat");
+ g_hash_table_remove(pn_hash, pn->path);
}
-void __connman_tethering_set_enabled(void)
+static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
{
- int err;
+ struct connman_private_network *pn = user_data;
- DBG("enabled %d", tethering_enabled + 1);
+ DBG("block used externally");
- if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
- err = create_bridge(BRIDGE_NAME);
- if (err < 0)
- return;
+ g_hash_table_remove(pn_hash, pn->path);
+}
- err = enable_bridge(BRIDGE_NAME);
- if (err < 0) {
- remove_bridge(BRIDGE_NAME);
- return;
- }
+static gboolean client_send_changed(gpointer data)
+{
+ DBusMessage *signal;
+ DBusMessageIter iter, array;
- tethering_dhcp_server =
- dhcp_server_start(BRIDGE_NAME,
- BRIDGE_IP, BRIDGE_SUBNET,
- BRIDGE_IP_START, BRIDGE_IP_END,
- 24 * 3600, BRIDGE_DNS);
- if (tethering_dhcp_server == NULL) {
- disable_bridge(BRIDGE_NAME);
- remove_bridge(BRIDGE_NAME);
- return;
- }
+ DBG("");
- enable_nat(default_interface);
+ clients_notify->id = 0;
- DBG("tethering started");
- }
+ signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
+ CONNMAN_MANAGER_INTERFACE, "TetheringClientsChanged");
+ if (!signal)
+ return FALSE;
+
+ dbus_message_iter_init_append(signal, &iter);
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ DBUS_TYPE_STRING_AS_STRING, &array);
+
+ g_hash_table_foreach(clients_table, append_client, &array);
+
+ dbus_message_iter_close_container(&iter, &array);
+
+ dbus_message_iter_init_append(signal, &iter);
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ DBUS_TYPE_STRING_AS_STRING, &array);
+
+ g_hash_table_foreach(clients_notify->remove, append_client, &array);
+
+ dbus_message_iter_close_container(&iter, &array);
+
+ dbus_connection_send(connection, signal, NULL);
+ dbus_message_unref(signal);
+
+ g_hash_table_remove_all(clients_notify->remove);
+
+ return FALSE;
}
-void __connman_tethering_set_disabled(void)
+static void client_schedule_changed(void)
{
- DBG("enabled %d", tethering_enabled - 1);
+ if (clients_notify->id != 0)
+ return;
- if (g_atomic_int_dec_and_test(&tethering_enabled) == TRUE) {
- disable_nat(default_interface);
+ clients_notify->id = g_timeout_add(100, client_send_changed, NULL);
+}
- dhcp_server_stop(tethering_dhcp_server);
+static void client_added(const char *addr)
+{
+ DBG("client %s", addr);
- disable_bridge(BRIDGE_NAME);
+ g_hash_table_remove(clients_notify->remove, addr);
- remove_bridge(BRIDGE_NAME);
+ client_schedule_changed();
+}
- DBG("tethering stopped");
- }
+static void client_removed(const char *addr)
+{
+ DBG("client %s", addr);
+
+ g_hash_table_replace(clients_notify->remove, g_strdup(addr), NULL);
+
+ client_schedule_changed();
}
-void __connman_tethering_update_interface(const char *interface)
+int __connman_private_network_request(DBusMessage *msg, const char *owner)
{
- DBG("interface %s", interface);
+ struct connman_private_network *pn;
+ char *iface = NULL;
+ char *path = NULL;
+ int index, fd, err;
+
+ if (DBUS_TYPE_UNIX_FD < 0)
+ return -EINVAL;
+
+ fd = connman_inet_create_tunnel(&iface);
+ if (fd < 0)
+ return fd;
+
+ path = g_strdup_printf("/tethering/%s", iface);
+
+ pn = g_hash_table_lookup(pn_hash, path);
+ if (pn) {
+ g_free(path);
+ g_free(iface);
+ close(fd);
+ return -EEXIST;
+ }
- g_free(default_interface);
+ index = connman_inet_ifindex(iface);
+ if (index < 0) {
+ err = -ENODEV;
+ goto error;
+ }
+ DBG("interface %s", iface);
- if (interface == NULL) {
- disable_nat(interface);
- default_interface = NULL;
+ err = connman_inet_set_mtu(index, DEFAULT_MTU);
- return;
+ pn = g_try_new0(struct connman_private_network, 1);
+ if (!pn) {
+ err = -ENOMEM;
+ goto error;
}
- default_interface = g_strdup(interface);
+ pn->owner = g_strdup(owner);
+ pn->path = path;
+ pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
+ owner_disconnect, pn, NULL);
+ pn->msg = msg;
+ pn->reply = dbus_message_new_method_return(pn->msg);
+ if (!pn->reply)
+ goto error;
+
+ pn->fd = fd;
+ pn->interface = iface;
+ pn->index = index;
+ pn->pool = __connman_ippool_create(pn->index, 1, 1, ippool_disconnect, pn);
+ if (!pn->pool) {
+ errno = -ENOMEM;
+ goto error;
+ }
- if (!g_atomic_int_get(&tethering_enabled))
- return;
+ pn->primary_dns = g_strdup(private_network_primary_dns);
+ pn->secondary_dns = g_strdup(private_network_secondary_dns);
+
+ pn->iface_watch = connman_rtnl_add_newlink_watch(index,
+ setup_tun_interface, pn);
+
+ g_hash_table_insert(pn_hash, pn->path, pn);
+
+ return 0;
+
+error:
+ close(fd);
+ g_free(iface);
+ g_free(path);
+ if (pn)
+ g_free(pn->owner);
+ g_free(pn);
+ return err;
+}
+
+int __connman_private_network_release(const char *path)
+{
+ struct connman_private_network *pn;
+
+ pn = g_hash_table_lookup(pn_hash, path);
+ if (!pn)
+ return -EACCES;
+
+ g_hash_table_remove(pn_hash, path);
+ return 0;
+}
- enable_nat(interface);
+void __connman_tethering_client_register(const char *addr)
+{
+ g_hash_table_insert(clients_table, g_strdup(addr), NULL);
+ client_added(addr);
+}
+
+void __connman_tethering_client_unregister(const char *addr)
+{
+ g_hash_table_remove(clients_table, addr);
+ client_removed(addr);
}
int __connman_tethering_init(void)
tethering_enabled = 0;
+ connection = connman_dbus_get_connection();
+ if (!connection)
+ return -EFAULT;
+
+ pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+ NULL, remove_private_network);
+
+ clients_table = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, NULL);
+
+ clients_notify = g_new0(struct _clients_notify, 1);
+ clients_notify->remove = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, NULL);
return 0;
}
void __connman_tethering_cleanup(void)
{
- DBG("");
+ DBG("enabled %d", tethering_enabled);
- if (g_atomic_int_get(&tethering_enabled)) {
+ __sync_synchronize();
+ if (tethering_enabled > 0) {
if (tethering_dhcp_server)
dhcp_server_stop(tethering_dhcp_server);
- disable_bridge(BRIDGE_NAME);
- remove_bridge(BRIDGE_NAME);
+ __connman_bridge_disable(BRIDGE_NAME);
+ __connman_bridge_remove(BRIDGE_NAME);
+ __connman_nat_disable(BRIDGE_NAME);
}
+
+ if (!connection)
+ return;
+
+ g_hash_table_destroy(pn_hash);
+
+ g_hash_table_destroy(clients_notify->remove);
+ g_free(clients_notify);
+ clients_notify = NULL;
+
+ g_hash_table_destroy(clients_table);
+ clients_table = NULL;
+
+ dbus_connection_unref(connection);
}