Imported Upstream version 1.38
[platform/upstream/connman.git] / src / tethering.c
index 7042040..e2687b6 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2012  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
 #include <stdio.h>
 #include <sys/ioctl.h>
 #include <net/if.h>
-#include <linux/sockios.h>
 #include <string.h>
 #include <fcntl.h>
-#include <linux/if_tun.h>
 #include <netinet/in.h>
+#include <linux/sockios.h>
+#include <linux/if_tun.h>
 #include <linux/if_bridge.h>
 
 #include "connman.h"
@@ -61,6 +61,13 @@ 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;
@@ -133,7 +140,7 @@ static void dhcp_server_error(GDHCPServerError error)
 }
 
 static GDHCPServer *dhcp_server_start(const char *bridge,
-                               const char *router, const charsubnet,
+                               const char *router, const char *subnet,
                                const char *start_ip, const char *end_ip,
                                unsigned int lease_time, const char *dns)
 {
@@ -148,7 +155,7 @@ static GDHCPServer *dhcp_server_start(const char *bridge,
                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;
        }
@@ -168,7 +175,7 @@ static GDHCPServer *dhcp_server_start(const char *bridge,
 
 static void dhcp_server_stop(GDHCPServer *server)
 {
-       if (server == NULL)
+       if (!server)
                return;
 
        g_dhcp_server_unref(server);
@@ -176,11 +183,24 @@ static void dhcp_server_stop(GDHCPServer *server)
 
 static void tethering_restart(struct connman_ippool *pool, void *user_data)
 {
+       DBG("pool %p", pool);
        __connman_tethering_set_disabled();
        __connman_tethering_set_enabled();
 }
 
-void __connman_tethering_set_enabled(void)
+static void unregister_client(gpointer key,
+                                       gpointer value, gpointer user_data)
+{
+       const char *addr = key;
+       __connman_tethering_client_unregister(addr);
+}
+
+static void unregister_all_clients(void)
+{
+       g_hash_table_foreach(clients_table, unregister_client, NULL);
+}
+
+int __connman_tethering_set_enabled(void)
 {
        int index;
        int err;
@@ -196,22 +216,22 @@ void __connman_tethering_set_enabled(void)
        DBG("enabled %d", tethering_enabled + 1);
 
        if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
-               return;
+               return 0;
 
        err = __connman_bridge_create(BRIDGE_NAME);
        if (err < 0) {
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EOPNOTSUPP;
        }
 
        index = connman_inet_ifindex(BRIDGE_NAME);
        dhcp_ippool = __connman_ippool_create(index, 2, 252,
                                                tethering_restart, NULL);
-       if (dhcp_ippool == NULL) {
+       if (!dhcp_ippool) {
                connman_error("Fail to create IP pool");
                __connman_bridge_remove(BRIDGE_NAME);
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EADDRNOTAVAIL;
        }
 
        gateway = __connman_ippool_get_gateway(dhcp_ippool);
@@ -220,21 +240,24 @@ void __connman_tethering_set_enabled(void)
        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, broadcast);
+       err = __connman_bridge_enable(BRIDGE_NAME, gateway,
+                       connman_ipaddress_calc_netmask_len(subnet_mask),
+                       broadcast);
        if (err < 0 && err != -EALREADY) {
-               __connman_ippool_unref(dhcp_ippool);
+               __connman_ippool_free(dhcp_ippool);
+               dhcp_ippool = NULL;
                __connman_bridge_remove(BRIDGE_NAME);
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EADDRNOTAVAIL;
        }
 
        ns = connman_setting_get_string_list("FallbackNameservers");
-       if (ns != NULL) {
-               if (ns[0] != NULL) {
+       if (ns) {
+               if (ns[0]) {
                        g_free(private_network_primary_dns);
                        private_network_primary_dns = g_strdup(ns[0]);
                }
-               if (ns[1] != NULL) {
+               if (ns[1]) {
                        g_free(private_network_secondary_dns);
                        private_network_secondary_dns = g_strdup(ns[1]);
                }
@@ -256,19 +279,36 @@ void __connman_tethering_set_enabled(void)
                                                gateway, subnet_mask,
                                                start_ip, end_ip,
                                                24 * 3600, dns);
-       if (tethering_dhcp_server == NULL) {
+       if (!tethering_dhcp_server) {
                __connman_bridge_disable(BRIDGE_NAME);
-               __connman_ippool_unref(dhcp_ippool);
+               __connman_ippool_free(dhcp_ippool);
+               dhcp_ippool = NULL;
                __connman_bridge_remove(BRIDGE_NAME);
                __sync_fetch_and_sub(&tethering_enabled, 1);
-               return;
+               return -EOPNOTSUPP;
        }
 
-       prefixlen =
-               __connman_ipaddress_netmask_prefix_len(subnet_mask);
-       __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
+       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;
+       }
+
+       err = __connman_ipv6pd_setup(BRIDGE_NAME);
+       if (err < 0 && err != -EINPROGRESS)
+               DBG("Cannot setup IPv6 prefix delegation %d/%s", err,
+                       strerror(-err));
 
        DBG("tethering started");
+
+       return 0;
 }
 
 void __connman_tethering_set_disabled(void)
@@ -277,12 +317,16 @@ void __connman_tethering_set_disabled(void)
 
        DBG("enabled %d", tethering_enabled - 1);
 
-       index = connman_inet_ifindex(BRIDGE_NAME);
-       __connman_dnsproxy_remove_listener(index);
-
        if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
                return;
 
+       unregister_all_clients();
+
+       __connman_ipv6pd_cleanup();
+
+       index = connman_inet_ifindex(BRIDGE_NAME);
+       __connman_dnsproxy_remove_listener(index);
+
        __connman_nat_disable(BRIDGE_NAME);
 
        dhcp_server_stop(tethering_dhcp_server);
@@ -291,7 +335,8 @@ void __connman_tethering_set_disabled(void)
 
        __connman_bridge_disable(BRIDGE_NAME);
 
-       __connman_ippool_unref(dhcp_ippool);
+       __connman_ippool_free(dhcp_ippool);
+       dhcp_ippool = NULL;
 
        __connman_bridge_remove(BRIDGE_NAME);
 
@@ -303,6 +348,21 @@ void __connman_tethering_set_disabled(void)
        DBG("tethering stopped");
 }
 
+static void append_client(gpointer key, gpointer value,
+                                               gpointer user_data)
+{
+       const char *addr = key;
+       DBusMessageIter *array = user_data;
+
+       dbus_message_iter_append_basic(array, DBUS_TYPE_STRING,
+                                                       &addr);
+}
+
+void __connman_tethering_list_clients(DBusMessageIter *array)
+{
+       g_hash_table_foreach(clients_table, append_client, array);
+}
+
 static void setup_tun_interface(unsigned int flags, unsigned change,
                void *data)
 {
@@ -322,8 +382,7 @@ static void setup_tun_interface(unsigned int flags, unsigned change,
        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_netmask_prefix_len(subnet_mask);
+       prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
 
        if ((__connman_inet_modify_address(RTM_NEWADDR,
                                NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
@@ -351,11 +410,11 @@ static void setup_tun_interface(unsigned int flags, unsigned change,
                                        DBUS_TYPE_STRING, &server_ip);
        connman_dbus_dict_append_basic(&dict, "PeerIPv4",
                                        DBUS_TYPE_STRING, &peer_ip);
-       if (pn->primary_dns != NULL)
+       if (pn->primary_dns)
                connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
                                        DBUS_TYPE_STRING, &pn->primary_dns);
 
-       if (pn->secondary_dns != NULL)
+       if (pn->secondary_dns)
                connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
                                        DBUS_TYPE_STRING, &pn->secondary_dns);
 
@@ -380,7 +439,7 @@ static void remove_private_network(gpointer user_data)
 
        __connman_nat_disable(BRIDGE_NAME);
        connman_rtnl_remove_watch(pn->iface_watch);
-       __connman_ippool_unref(pn->pool);
+       __connman_ippool_free(pn->pool);
 
        if (pn->watch > 0) {
                g_dbus_remove_watch(connection, pn->watch);
@@ -417,6 +476,70 @@ static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
        g_hash_table_remove(pn_hash, pn->path);
 }
 
+static gboolean client_send_changed(gpointer data)
+{
+       DBusMessage *signal;
+       DBusMessageIter iter, array;
+
+       DBG("");
+
+       clients_notify->id = 0;
+
+       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;
+}
+
+static void client_schedule_changed(void)
+{
+       if (clients_notify->id != 0)
+               return;
+
+       clients_notify->id = g_timeout_add(100, client_send_changed, NULL);
+}
+
+static void client_added(const char *addr)
+{
+       DBG("client %s", addr);
+
+       g_hash_table_remove(clients_notify->remove, addr);
+
+       client_schedule_changed();
+}
+
+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();
+}
+
 int __connman_private_network_request(DBusMessage *msg, const char *owner)
 {
        struct connman_private_network *pn;
@@ -451,7 +574,7 @@ int __connman_private_network_request(DBusMessage *msg, const char *owner)
        err = connman_inet_set_mtu(index, DEFAULT_MTU);
 
        pn = g_try_new0(struct connman_private_network, 1);
-       if (pn == NULL) {
+       if (!pn) {
                err = -ENOMEM;
                goto error;
        }
@@ -462,14 +585,14 @@ int __connman_private_network_request(DBusMessage *msg, const char *owner)
                                        owner_disconnect, pn, NULL);
        pn->msg = msg;
        pn->reply = dbus_message_new_method_return(pn->msg);
-       if (pn->reply == NULL)
+       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 == NULL) {
+       if (!pn->pool) {
                errno = -ENOMEM;
                goto error;
        }
@@ -488,6 +611,8 @@ error:
        close(fd);
        g_free(iface);
        g_free(path);
+       if (pn)
+               g_free(pn->owner);
        g_free(pn);
        return err;
 }
@@ -497,13 +622,25 @@ int __connman_private_network_release(const char *path)
        struct connman_private_network *pn;
 
        pn = g_hash_table_lookup(pn_hash, path);
-       if (pn == NULL)
+       if (!pn)
                return -EACCES;
 
        g_hash_table_remove(pn_hash, path);
        return 0;
 }
 
+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)
+{
+       client_removed(addr);
+       g_hash_table_remove(clients_table, addr);
+}
+
 int __connman_tethering_init(void)
 {
        DBG("");
@@ -511,21 +648,27 @@ int __connman_tethering_init(void)
        tethering_enabled = 0;
 
        connection = connman_dbus_get_connection();
-       if (connection == NULL)
+       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);
 
        __sync_synchronize();
-       if (tethering_enabled == 0) {
+       if (tethering_enabled > 0) {
                if (tethering_dhcp_server)
                        dhcp_server_stop(tethering_dhcp_server);
                __connman_bridge_disable(BRIDGE_NAME);
@@ -533,9 +676,17 @@ void __connman_tethering_cleanup(void)
                __connman_nat_disable(BRIDGE_NAME);
        }
 
-       if (connection == NULL)
+       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);
 }