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 #include <connman/tethering.h>
36 #define BRIDGE_NAME "tether"
38 static connman_bool_t tethering_status = FALSE;
39 static const char *default_interface = NULL;
40 gint tethering_enabled;
42 connman_bool_t __connman_tethering_get_status(void)
44 return tethering_status;
47 static int create_bridge(const char *name)
53 sk = socket(AF_INET, SOCK_STREAM, 0);
57 err = ioctl(sk, SIOCBRADDBR, name);
67 static int remove_bridge(const char *name)
73 sk = socket(AF_INET, SOCK_STREAM, 0);
77 err = ioctl(sk, SIOCBRDELBR, name);
87 static int enable_ip_forward(connman_bool_t enable)
91 int ip_forward = enable ? 1 : 0;
93 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
95 fprintf(f, "%d", ip_forward);
102 static int enable_nat(const char *interface)
106 if (interface == NULL)
109 /* Enable IPv4 forwarding */
110 ret = enable_ip_forward(TRUE);
114 /* TODO: Flush nat POSTROUTING chain */
115 /* Enable masquerading */
116 ret = __connman_iptables_command("-t nat -A POSTROUTING -o %s -j MASQUERADE", interface);
120 return __connman_iptables_commit("nat");
123 static void disable_nat(const char *interface)
125 /* Disable IPv4 forwarding */
126 enable_ip_forward(FALSE);
128 /* TODO: Flush nat POSTROUTING chain */
131 void connman_tethering_enabled(void)
133 if (tethering_status == FALSE)
136 DBG("enabled %d", tethering_enabled + 1);
138 if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
139 /* TODO Start DHCP server and DNS proxy on the bridge */
141 enable_nat(default_interface);
142 DBG("tethering started");
146 void connman_tethering_disabled(void)
148 if (tethering_status == FALSE)
151 DBG("enabled %d", tethering_enabled - 1);
153 if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
154 /* TODO Stop DHCP server and DNS proxy on the bridge */
156 disable_nat(default_interface);
157 DBG("tethering stopped");
161 int __connman_tethering_set_status(connman_bool_t status)
163 if (status == tethering_status)
166 if (status == TRUE) {
167 create_bridge(BRIDGE_NAME);
168 __connman_technology_enable_tethering(BRIDGE_NAME);
170 __connman_technology_disable_tethering(BRIDGE_NAME);
171 remove_bridge(BRIDGE_NAME);
174 tethering_status = status;
179 void __connman_tethering_update_interface(const char *interface)
181 DBG("interface %s", interface);
183 default_interface = interface;
185 if (interface == NULL) {
186 disable_nat(interface);
191 if (tethering_status == FALSE ||
192 !g_atomic_int_get(&tethering_enabled))
195 enable_nat(interface);
198 int __connman_tethering_init(void)
202 tethering_enabled = 0;
207 void __connman_tethering_cleanup(void)
211 if (tethering_status == TRUE)
212 remove_bridge(BRIDGE_NAME);