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)
89 int ip_forward = enable ? 1 : 0;
91 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
93 fprintf(f, "%d", ip_forward);
100 static int enable_nat(const char *interface)
104 if (interface == NULL)
107 /* Enable IPv4 forwarding */
108 ret = enable_ip_forward(TRUE);
112 /* TODO: Flush nat POSTROUTING chain */
113 /* Enable masquerading */
114 ret = __connman_iptables_command("-t nat -A POSTROUTING -o %s -j MASQUERADE", interface);
118 return __connman_iptables_commit("nat");
121 static void disable_nat(const char *interface)
123 /* Disable IPv4 forwarding */
124 enable_ip_forward(FALSE);
126 /* TODO: Flush nat POSTROUTING chain */
129 void __connman_tethering_set_enabled(void)
131 if (tethering_status == FALSE)
134 DBG("enabled %d", tethering_enabled + 1);
136 if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
137 /* TODO Start DHCP server and DNS proxy on the bridge */
139 enable_nat(default_interface);
140 DBG("tethering started");
144 void __connman_tethering_set_disabled(void)
146 if (tethering_status == FALSE)
149 DBG("enabled %d", tethering_enabled - 1);
151 if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
152 /* TODO Stop DHCP server and DNS proxy on the bridge */
154 disable_nat(default_interface);
155 DBG("tethering stopped");
159 int __connman_tethering_set_status(connman_bool_t status)
161 if (status == tethering_status)
164 if (status == TRUE) {
165 create_bridge(BRIDGE_NAME);
166 __connman_technology_enable_tethering(BRIDGE_NAME);
168 __connman_technology_disable_tethering(BRIDGE_NAME);
169 remove_bridge(BRIDGE_NAME);
172 tethering_status = status;
177 void __connman_tethering_update_interface(const char *interface)
179 DBG("interface %s", interface);
181 default_interface = interface;
183 if (interface == NULL) {
184 disable_nat(interface);
189 if (tethering_status == FALSE ||
190 !g_atomic_int_get(&tethering_enabled))
193 enable_nat(interface);
196 int __connman_tethering_init(void)
200 tethering_enabled = 0;
205 void __connman_tethering_cleanup(void)
209 if (tethering_status == TRUE)
210 remove_bridge(BRIDGE_NAME);