Imported Upstream version 1.24
[platform/upstream/connman.git] / src / tethering.c
index 7042040..c7e17f5 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
@@ -133,7 +133,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 +148,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 +168,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,6 +176,7 @@ 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();
 }
@@ -207,7 +208,7 @@ void __connman_tethering_set_enabled(void)
        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);
@@ -220,7 +221,9 @@ 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_netmask_prefix_len(subnet_mask),
+                       broadcast);
        if (err < 0 && err != -EALREADY) {
                __connman_ippool_unref(dhcp_ippool);
                __connman_bridge_remove(BRIDGE_NAME);
@@ -229,12 +232,12 @@ void __connman_tethering_set_enabled(void)
        }
 
        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,7 +259,7 @@ 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_bridge_remove(BRIDGE_NAME);
@@ -264,9 +267,22 @@ void __connman_tethering_set_enabled(void)
                return;
        }
 
-       prefixlen =
-               __connman_ipaddress_netmask_prefix_len(subnet_mask);
-       __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
+       prefixlen = __connman_ipaddress_netmask_prefix_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_unref(dhcp_ippool);
+               __connman_bridge_remove(BRIDGE_NAME);
+               __sync_fetch_and_sub(&tethering_enabled, 1);
+               return;
+       }
+
+       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");
 }
@@ -277,12 +293,14 @@ 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;
 
+       __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);
@@ -351,11 +369,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);
 
@@ -451,7 +469,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 +480,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;
        }
@@ -497,7 +515,7 @@ 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);
@@ -511,7 +529,7 @@ 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,
@@ -522,10 +540,10 @@ int __connman_tethering_init(void)
 
 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,7 +551,7 @@ void __connman_tethering_cleanup(void)
                __connman_nat_disable(BRIDGE_NAME);
        }
 
-       if (connection == NULL)
+       if (!connection)
                return;
 
        g_hash_table_destroy(pn_hash);