5 * Copyright (C) 2007-2010 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
28 #include <sys/ioctl.h>
30 #include <linux/sockios.h>
34 #define BRIDGE_NAME "tether"
36 static connman_bool_t tethering_status = FALSE;
37 static const char *default_interface = NULL;
38 static volatile gint tethering_enabled;
40 connman_bool_t __connman_tethering_get_status(void)
42 return tethering_status;
45 static int create_bridge(const char *name)
51 sk = socket(AF_INET, SOCK_STREAM, 0);
55 err = ioctl(sk, SIOCBRADDBR, name);
65 static int remove_bridge(const char *name)
71 sk = socket(AF_INET, SOCK_STREAM, 0);
75 err = ioctl(sk, SIOCBRDELBR, name);
85 static int enable_ip_forward(connman_bool_t enable)
90 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
104 static int enable_nat(const char *interface)
108 if (interface == NULL)
111 /* Enable IPv4 forwarding */
112 err = enable_ip_forward(TRUE);
116 /* POSTROUTING flush */
117 err = __connman_iptables_command("-t nat -F POSTROUTING");
121 /* Enable masquerading */
122 err = __connman_iptables_command("-t nat -A POSTROUTING "
123 "-o %s -j MASQUERADE", interface);
127 return __connman_iptables_commit("nat");
130 static void disable_nat(const char *interface)
134 /* Disable IPv4 forwarding */
135 enable_ip_forward(FALSE);
137 /* POSTROUTING flush */
138 err = __connman_iptables_command("-t nat -F POSTROUTING");
142 __connman_iptables_commit("nat");
145 void __connman_tethering_set_enabled(void)
147 if (tethering_status == FALSE)
150 DBG("enabled %d", tethering_enabled + 1);
152 if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
153 /* TODO Start DHCP server and DNS proxy on the bridge */
155 enable_nat(default_interface);
156 DBG("tethering started");
160 void __connman_tethering_set_disabled(void)
162 if (tethering_status == FALSE)
165 DBG("enabled %d", tethering_enabled - 1);
167 if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
168 /* TODO Stop DHCP server and DNS proxy on the bridge */
170 disable_nat(default_interface);
171 DBG("tethering stopped");
175 int __connman_tethering_set_status(connman_bool_t status)
177 if (status == tethering_status)
180 if (status == TRUE) {
181 create_bridge(BRIDGE_NAME);
182 __connman_technology_enable_tethering(BRIDGE_NAME);
184 __connman_technology_disable_tethering(BRIDGE_NAME);
185 remove_bridge(BRIDGE_NAME);
188 tethering_status = status;
193 void __connman_tethering_update_interface(const char *interface)
195 DBG("interface %s", interface);
197 default_interface = interface;
199 if (interface == NULL) {
200 disable_nat(interface);
205 if (tethering_status == FALSE ||
206 !g_atomic_int_get(&tethering_enabled))
209 enable_nat(interface);
212 int __connman_tethering_init(void)
216 tethering_enabled = 0;
221 void __connman_tethering_cleanup(void)
225 if (tethering_status == TRUE)
226 remove_bridge(BRIDGE_NAME);