tethering: Configure bridge interface when starting tethering
[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 #define BRIDGE_IP "192.168.218.1"
36 #define BRIDGE_BCAST "192.168.218.255"
37 #define BRIDGE_SUBNET "255.255.255.0"
38
39 static connman_bool_t tethering_status = FALSE;
40 static const char *default_interface = NULL;
41 static volatile gint tethering_enabled;
42
43 connman_bool_t __connman_tethering_get_status(void)
44 {
45         return tethering_status;
46 }
47
48 static int create_bridge(const char *name)
49 {
50         int sk, err;
51
52         DBG("name %s", name);
53
54         sk = socket(AF_INET, SOCK_STREAM, 0);
55         if (sk < 0)
56                 return -EOPNOTSUPP;
57
58         err = ioctl(sk, SIOCBRADDBR, name);
59
60         close(sk);
61
62         if (err < 0)
63                 return -EOPNOTSUPP;
64
65         return 0;
66 }
67
68 static int remove_bridge(const char *name)
69 {
70         int sk, err;
71
72         DBG("name %s", name);
73
74         sk = socket(AF_INET, SOCK_STREAM, 0);
75         if (sk < 0)
76                 return -EOPNOTSUPP;
77
78         err = ioctl(sk, SIOCBRDELBR, name);
79
80         close(sk);
81
82         if (err < 0)
83                 return -EOPNOTSUPP;
84
85         return 0;
86 }
87
88 static int enable_bridge(const char *name)
89 {
90         int err, index;
91
92         index = connman_inet_ifindex(name);
93         if (index < 0)
94                 return index;
95
96         err = __connman_inet_modify_address(RTM_NEWADDR,
97                         NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
98                                             BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
99         if (err < 0)
100                 return err;
101
102         return connman_inet_ifup(index);
103 }
104
105 static int disable_bridge(const char *name)
106 {
107         int index;
108
109         index = connman_inet_ifindex(name);
110         if (index < 0)
111                 return index;
112
113         return connman_inet_ifdown(index);
114 }
115
116 static int enable_ip_forward(connman_bool_t enable)
117 {
118
119         FILE *f;
120
121         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
122         if (f == NULL)
123                 return -errno;
124
125         if (enable == TRUE)
126                 fprintf(f, "1");
127         else
128                 fprintf(f, "0");
129
130         fclose(f);
131
132         return 0;
133 }
134
135 static int enable_nat(const char *interface)
136 {
137         int err;
138
139         if (interface == NULL)
140                 return 0;
141
142         /* Enable IPv4 forwarding */
143         err = enable_ip_forward(TRUE);
144         if (err < 0)
145                 return err;
146
147         /* POSTROUTING flush */
148         err = __connman_iptables_command("-t nat -F POSTROUTING");
149         if (err < 0)
150                 return err;
151
152         /* Enable masquerading */
153         err = __connman_iptables_command("-t nat -A POSTROUTING "
154                                         "-o %s -j MASQUERADE", interface);
155         if (err < 0)
156                 return err;
157
158         return __connman_iptables_commit("nat");
159 }
160
161 static void disable_nat(const char *interface)
162 {
163         int err;
164
165         /* Disable IPv4 forwarding */
166         enable_ip_forward(FALSE);
167
168         /* POSTROUTING flush */
169         err = __connman_iptables_command("-t nat -F POSTROUTING");
170         if (err < 0)
171                 return;
172
173         __connman_iptables_commit("nat");
174 }
175
176 void __connman_tethering_set_enabled(void)
177 {
178         int err;
179
180         if (tethering_status == FALSE)
181                 return;
182
183         DBG("enabled %d", tethering_enabled + 1);
184
185         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
186                 err = enable_bridge(BRIDGE_NAME);
187                 if (err < 0)
188                         return;
189
190                 /* TODO Start DHCP server and DNS proxy on the bridge */
191
192                 enable_nat(default_interface);
193
194                 DBG("tethering started");
195         }
196 }
197
198 void __connman_tethering_set_disabled(void)
199 {
200         if (tethering_status == FALSE)
201                 return;
202
203         DBG("enabled %d", tethering_enabled - 1);
204
205         if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
206                 /* TODO Stop DHCP server and DNS proxy on the bridge */
207
208                 disable_nat(default_interface);
209
210                 disable_bridge(BRIDGE_NAME);
211
212                 DBG("tethering stopped");
213         }
214 }
215
216 int __connman_tethering_set_status(connman_bool_t status)
217 {
218         if (status == tethering_status)
219                 return -EALREADY;
220
221         if (status == TRUE) {
222                 create_bridge(BRIDGE_NAME);
223                 __connman_technology_enable_tethering(BRIDGE_NAME);
224         } else {
225                 __connman_technology_disable_tethering(BRIDGE_NAME);
226                 remove_bridge(BRIDGE_NAME);
227         }
228
229         tethering_status = status;
230
231         return 0;
232 }
233
234 void __connman_tethering_update_interface(const char *interface)
235 {
236         DBG("interface %s", interface);
237
238         default_interface = interface;
239
240         if (interface == NULL) {
241                 disable_nat(interface);
242
243                 return;
244         }
245
246         if (tethering_status == FALSE ||
247                         !g_atomic_int_get(&tethering_enabled))
248                 return;
249
250         enable_nat(interface);
251 }
252
253 int __connman_tethering_init(void)
254 {
255         DBG("");
256
257         tethering_enabled = 0;
258
259         return 0;
260 }
261
262 void __connman_tethering_cleanup(void)
263 {
264         DBG("");
265
266         if (tethering_status == TRUE) {
267                 disable_bridge(BRIDGE_NAME);
268                 remove_bridge(BRIDGE_NAME);
269         }
270 }