tethering: Flush POSTROUTING table when needed
[platform/upstream/connman.git] / src / tethering.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <sys/ioctl.h>
29 #include <net/if.h>
30 #include <linux/sockios.h>
31
32 #include "connman.h"
33
34 #define BRIDGE_NAME "tether"
35
36 static connman_bool_t tethering_status = FALSE;
37 static const char *default_interface = NULL;
38 static volatile gint tethering_enabled;
39
40 connman_bool_t __connman_tethering_get_status(void)
41 {
42         return tethering_status;
43 }
44
45 static int create_bridge(const char *name)
46 {
47         int sk, err;
48
49         DBG("name %s", name);
50
51         sk = socket(AF_INET, SOCK_STREAM, 0);
52         if (sk < 0)
53                 return -EOPNOTSUPP;
54
55         err = ioctl(sk, SIOCBRADDBR, name);
56
57         close(sk);
58
59         if (err < 0)
60                 return -EOPNOTSUPP;
61
62         return 0;
63 }
64
65 static int remove_bridge(const char *name)
66 {
67         int sk, err;
68
69         DBG("name %s", name);
70
71         sk = socket(AF_INET, SOCK_STREAM, 0);
72         if (sk < 0)
73                 return -EOPNOTSUPP;
74
75         err = ioctl(sk, SIOCBRDELBR, name);
76
77         close(sk);
78
79         if (err < 0)
80                 return -EOPNOTSUPP;
81
82         return 0;
83 }
84
85 static int enable_ip_forward(connman_bool_t enable)
86 {
87
88         FILE *f;
89
90         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
91         if (f == NULL)
92                 return -errno;
93
94         if (enable == TRUE)
95                 fprintf(f, "1");
96         else
97                 fprintf(f, "0");
98
99         fclose(f);
100
101         return 0;
102 }
103
104 static int enable_nat(const char *interface)
105 {
106         int err;
107
108         if (interface == NULL)
109                 return 0;
110
111         /* Enable IPv4 forwarding */
112         err = enable_ip_forward(TRUE);
113         if (err < 0)
114                 return err;
115
116         /* POSTROUTING flush */
117         err = __connman_iptables_command("-t nat -F POSTROUTING");
118         if (err < 0)
119                 return err;
120
121         /* Enable masquerading */
122         err = __connman_iptables_command("-t nat -A POSTROUTING "
123                                         "-o %s -j MASQUERADE", interface);
124         if (err < 0)
125                 return err;
126
127         return __connman_iptables_commit("nat");
128 }
129
130 static void disable_nat(const char *interface)
131 {
132         int err;
133
134         /* Disable IPv4 forwarding */
135         enable_ip_forward(FALSE);
136
137         /* POSTROUTING flush */
138         err = __connman_iptables_command("-t nat -F POSTROUTING");
139         if (err < 0)
140                 return;
141
142         __connman_iptables_commit("nat");
143 }
144
145 void __connman_tethering_set_enabled(void)
146 {
147         if (tethering_status == FALSE)
148                 return;
149
150         DBG("enabled %d", tethering_enabled + 1);
151
152         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
153                 /* TODO Start DHCP server and DNS proxy on the bridge */
154
155                 enable_nat(default_interface);
156                 DBG("tethering started");
157         }
158 }
159
160 void __connman_tethering_set_disabled(void)
161 {
162         if (tethering_status == FALSE)
163                 return;
164
165         DBG("enabled %d", tethering_enabled - 1);
166
167         if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
168                 /* TODO Stop DHCP server and DNS proxy on the bridge */
169
170                 disable_nat(default_interface);
171                 DBG("tethering stopped");
172         }
173 }
174
175 int __connman_tethering_set_status(connman_bool_t status)
176 {
177         if (status == tethering_status)
178                 return -EALREADY;
179
180         if (status == TRUE) {
181                 create_bridge(BRIDGE_NAME);
182                 __connman_technology_enable_tethering(BRIDGE_NAME);
183         } else {
184                 __connman_technology_disable_tethering(BRIDGE_NAME);
185                 remove_bridge(BRIDGE_NAME);
186         }
187
188         tethering_status = status;
189
190         return 0;
191 }
192
193 void __connman_tethering_update_interface(const char *interface)
194 {
195         DBG("interface %s", interface);
196
197         default_interface = interface;
198
199         if (interface == NULL) {
200                 disable_nat(interface);
201
202                 return;
203         }
204
205         if (tethering_status == FALSE ||
206                         !g_atomic_int_get(&tethering_enabled))
207                 return;
208
209         enable_nat(interface);
210 }
211
212 int __connman_tethering_init(void)
213 {
214         DBG("");
215
216         tethering_enabled = 0;
217
218         return 0;
219 }
220
221 void __connman_tethering_cleanup(void)
222 {
223         DBG("");
224
225         if (tethering_status == TRUE)
226                 remove_bridge(BRIDGE_NAME);
227 }