Constify GDBus signal tables
[framework/connectivity/connman.git] / src / tethering.c
index c240302..c67f200 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
  *  Copyright (C) 2011 ProFUSION embedded systems
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -56,7 +56,6 @@
 #define PRIVATE_NETWORK_PRIMARY_DNS BRIDGE_DNS
 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
 
-static char *default_interface = NULL;
 static volatile int tethering_enabled;
 static GDHCPServer *tethering_dhcp_server = NULL;
 static struct connman_ippool *dhcp_ippool = NULL;
@@ -167,66 +166,6 @@ static void dhcp_server_stop(GDHCPServer *server)
        g_dhcp_server_unref(server);
 }
 
-static int enable_ip_forward(connman_bool_t enable)
-{
-
-       FILE *f;
-
-       f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
-       if (f == NULL)
-               return -errno;
-
-       if (enable == TRUE)
-               fprintf(f, "1");
-       else
-               fprintf(f, "0");
-
-       fclose(f);
-
-       return 0;
-}
-
-static int enable_nat(const char *interface)
-{
-       int err;
-
-       if (interface == NULL)
-               return 0;
-
-       /* 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;
-
-       /* Enable masquerading */
-       err = __connman_iptables_command("-t nat -A POSTROUTING "
-                                       "-o %s -j MASQUERADE", interface);
-       if (err < 0)
-               return err;
-
-       return __connman_iptables_commit("nat");
-}
-
-static void disable_nat(const char *interface)
-{
-       int err;
-
-       /* Disable IPv4 forwarding */
-       enable_ip_forward(FALSE);
-
-       /* POSTROUTING flush */
-       err = __connman_iptables_command("-t nat -F POSTROUTING");
-       if (err < 0)
-               return;
-
-       __connman_iptables_commit("nat");
-}
-
 static void tethering_restart(struct connman_ippool *pool, void *user_data)
 {
        __connman_tethering_set_disabled();
@@ -243,6 +182,7 @@ void __connman_tethering_set_enabled(void)
        const char *start_ip;
        const char *end_ip;
        const char *dns;
+       unsigned char prefixlen;
 
        DBG("enabled %d", tethering_enabled + 1);
 
@@ -254,7 +194,7 @@ void __connman_tethering_set_enabled(void)
                return;
 
        index = connman_inet_ifindex(BRIDGE_NAME);
-       dhcp_ippool = __connman_ippool_create(index, 1, 253,
+       dhcp_ippool = __connman_ippool_create(index, 2, 252,
                                                tethering_restart, NULL);
        if (dhcp_ippool == NULL) {
                connman_error("Fail to create IP pool");
@@ -290,7 +230,9 @@ void __connman_tethering_set_enabled(void)
                return;
        }
 
-       enable_nat(default_interface);
+       prefixlen =
+               __connman_ipconfig_netmask_prefix_len(subnet_mask);
+       __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
 
        DBG("tethering started");
 }
@@ -304,7 +246,7 @@ void __connman_tethering_set_disabled(void)
        if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
                return;
 
-       disable_nat(default_interface);
+       __connman_nat_disable(BRIDGE_NAME);
 
        dhcp_server_stop(tethering_dhcp_server);
 
@@ -319,28 +261,6 @@ void __connman_tethering_set_disabled(void)
        DBG("tethering stopped");
 }
 
-void __connman_tethering_update_interface(const char *interface)
-{
-       DBG("interface %s", interface);
-
-       g_free(default_interface);
-
-       if (interface == NULL) {
-               disable_nat(interface);
-               default_interface = NULL;
-
-               return;
-       }
-
-       default_interface = g_strdup(interface);
-
-       __sync_synchronize();
-       if (tethering_enabled == 0)
-               return;
-
-       enable_nat(interface);
-}
-
 static void setup_tun_interface(unsigned int flags, unsigned change,
                void *data)
 {
@@ -372,9 +292,9 @@ static void setup_tun_interface(unsigned int flags, unsigned change,
 
        connman_inet_ifup(pn->index);
 
-       err = enable_nat(default_interface);
+       err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
        if (err < 0) {
-               connman_error("failed to enable NAT on %s", default_interface);
+               connman_error("failed to enable NAT");
                goto error;
        }
 
@@ -413,7 +333,7 @@ static void remove_private_network(gpointer user_data)
 {
        struct connman_private_network *pn = user_data;
 
-       disable_nat(default_interface);
+       __connman_nat_disable(BRIDGE_NAME);
        connman_rtnl_remove_watch(pn->iface_watch);
        __connman_ippool_unref(pn->pool);
 
@@ -563,6 +483,7 @@ void __connman_tethering_cleanup(void)
                        dhcp_server_stop(tethering_dhcp_server);
                __connman_bridge_disable(BRIDGE_NAME);
                __connman_bridge_remove(BRIDGE_NAME);
+               __connman_nat_disable(BRIDGE_NAME);
        }
 
        if (connection == NULL)