tethering: Check for kernel bridge support
[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 <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <sys/ioctl.h>
31 #include <net/if.h>
32 #include <linux/sockios.h>
33
34 #include "connman.h"
35
36 #include <gdhcp/gdhcp.h>
37
38 #define BRIDGE_SYSFS_DIR "/sys/module/bridge"
39
40 #define BRIDGE_NAME "tether"
41 #define BRIDGE_IP "192.168.218.1"
42 #define BRIDGE_BCAST "192.168.218.255"
43 #define BRIDGE_SUBNET "255.255.255.0"
44 #define BRIDGE_IP_START "192.168.218.100"
45 #define BRIDGE_IP_END "192.168.218.200"
46 #define BRIDGE_DNS "8.8.8.8"
47
48 static char *default_interface = NULL;
49 static volatile gint tethering_enabled;
50 static GDHCPServer *tethering_dhcp_server = NULL;
51
52 const char *__connman_tethering_get_bridge(void)
53 {
54         struct stat st;
55
56         if (stat(BRIDGE_SYSFS_DIR, &st) < 0) {
57                 connman_error("Missing support for 802.1d ethernet bridging");
58                 return NULL;
59         }
60
61         return BRIDGE_NAME;
62 }
63
64 static void dhcp_server_debug(const char *str, void *data)
65 {
66         connman_info("%s: %s\n", (const char *) data, str);
67 }
68
69 static void dhcp_server_error(GDHCPServerError error)
70 {
71         switch (error) {
72         case G_DHCP_SERVER_ERROR_NONE:
73                 connman_error("OK");
74                 break;
75         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
76                 connman_error("Interface unavailable");
77                 break;
78         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
79                 connman_error("Interface in use");
80                 break;
81         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
82                 connman_error("Interface down");
83                 break;
84         case G_DHCP_SERVER_ERROR_NOMEM:
85                 connman_error("No memory");
86                 break;
87         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
88                 connman_error("Invalid index");
89                 break;
90         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
91                 connman_error("Invalid option");
92                 break;
93         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
94                 connman_error("Invalid address");
95                 break;
96         }
97 }
98
99 static GDHCPServer *dhcp_server_start(const char *bridge,
100                                 const char *router, const char* subnet,
101                                 const char *start_ip, const char *end_ip,
102                                 unsigned int lease_time, const char *dns)
103 {
104         GDHCPServerError error;
105         GDHCPServer *dhcp_server;
106         int index;
107
108         DBG("");
109
110         index = connman_inet_ifindex(bridge);
111         if (index < 0)
112                 return NULL;
113
114         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
115         if (dhcp_server == NULL) {
116                 dhcp_server_error(error);
117                 return NULL;
118         }
119
120         g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
121
122         g_dhcp_server_set_lease_time(dhcp_server, lease_time);
123         g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
124         g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
125         g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
126         g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
127
128         g_dhcp_server_start(dhcp_server);
129
130         return dhcp_server;
131 }
132
133 static void dhcp_server_stop(GDHCPServer *server)
134 {
135         if (server == NULL)
136                 return;
137
138         g_dhcp_server_unref(server);
139 }
140
141 static int set_forward_delay(const char *name, unsigned int delay)
142 {
143         FILE *f;
144         char *forward_delay_path;
145
146         forward_delay_path =
147                 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
148
149         if (forward_delay_path == NULL)
150                 return -ENOMEM;
151
152         f = fopen(forward_delay_path, "r+");
153
154         g_free(forward_delay_path);
155
156         if (f == NULL)
157                 return -errno;
158
159         fprintf(f, "%d", delay);
160
161         fclose(f);
162
163         return 0;
164 }
165
166 static int create_bridge(const char *name)
167 {
168         int sk, err;
169
170         DBG("name %s", name);
171
172         sk = socket(AF_INET, SOCK_STREAM, 0);
173         if (sk < 0)
174                 return -EOPNOTSUPP;
175
176         err = ioctl(sk, SIOCBRADDBR, name);
177
178         if (err < 0)
179                 return -EOPNOTSUPP;
180
181         err = set_forward_delay(name, 0);
182
183         if (err < 0)
184                 ioctl(sk, SIOCBRDELBR, name);
185
186         close(sk);
187
188         return err;
189 }
190
191 static int remove_bridge(const char *name)
192 {
193         int sk, err;
194
195         DBG("name %s", name);
196
197         sk = socket(AF_INET, SOCK_STREAM, 0);
198         if (sk < 0)
199                 return -EOPNOTSUPP;
200
201         err = ioctl(sk, SIOCBRDELBR, name);
202
203         close(sk);
204
205         if (err < 0)
206                 return -EOPNOTSUPP;
207
208         return 0;
209 }
210
211 static int enable_bridge(const char *name)
212 {
213         int err, index;
214
215         index = connman_inet_ifindex(name);
216         if (index < 0)
217                 return index;
218
219         err = __connman_inet_modify_address(RTM_NEWADDR,
220                         NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
221                                         BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
222         if (err < 0)
223                 return err;
224
225         return connman_inet_ifup(index);
226 }
227
228 static int disable_bridge(const char *name)
229 {
230         int index;
231
232         index = connman_inet_ifindex(name);
233         if (index < 0)
234                 return index;
235
236         return connman_inet_ifdown(index);
237 }
238
239 static int enable_ip_forward(connman_bool_t enable)
240 {
241
242         FILE *f;
243
244         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
245         if (f == NULL)
246                 return -errno;
247
248         if (enable == TRUE)
249                 fprintf(f, "1");
250         else
251                 fprintf(f, "0");
252
253         fclose(f);
254
255         return 0;
256 }
257
258 static int enable_nat(const char *interface)
259 {
260         int err;
261
262         if (interface == NULL)
263                 return 0;
264
265         /* Enable IPv4 forwarding */
266         err = enable_ip_forward(TRUE);
267         if (err < 0)
268                 return err;
269
270         /* POSTROUTING flush */
271         err = __connman_iptables_command("-t nat -F POSTROUTING");
272         if (err < 0)
273                 return err;
274
275         /* Enable masquerading */
276         err = __connman_iptables_command("-t nat -A POSTROUTING "
277                                         "-o %s -j MASQUERADE", interface);
278         if (err < 0)
279                 return err;
280
281         return __connman_iptables_commit("nat");
282 }
283
284 static void disable_nat(const char *interface)
285 {
286         int err;
287
288         /* Disable IPv4 forwarding */
289         enable_ip_forward(FALSE);
290
291         /* POSTROUTING flush */
292         err = __connman_iptables_command("-t nat -F POSTROUTING");
293         if (err < 0)
294                 return;
295
296         __connman_iptables_commit("nat");
297 }
298
299 void __connman_tethering_set_enabled(void)
300 {
301         int err;
302
303         DBG("enabled %d", tethering_enabled + 1);
304
305         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
306                 err = create_bridge(BRIDGE_NAME);
307                 if (err < 0)
308                         return;
309
310                 err = enable_bridge(BRIDGE_NAME);
311                 if (err < 0) {
312                         remove_bridge(BRIDGE_NAME);
313                         return;
314                 }
315
316                 tethering_dhcp_server =
317                         dhcp_server_start(BRIDGE_NAME,
318                                                 BRIDGE_IP, BRIDGE_SUBNET,
319                                                 BRIDGE_IP_START, BRIDGE_IP_END,
320                                                         24 * 3600, BRIDGE_DNS);
321                 if (tethering_dhcp_server == NULL) {
322                         disable_bridge(BRIDGE_NAME);
323                         remove_bridge(BRIDGE_NAME);
324                         return;
325                 }
326
327                 enable_nat(default_interface);
328
329                 DBG("tethering started");
330         }
331 }
332
333 void __connman_tethering_set_disabled(void)
334 {
335         DBG("enabled %d", tethering_enabled - 1);
336
337         if (g_atomic_int_dec_and_test(&tethering_enabled) == TRUE) {
338                 disable_nat(default_interface);
339
340                 dhcp_server_stop(tethering_dhcp_server);
341
342                 disable_bridge(BRIDGE_NAME);
343
344                 remove_bridge(BRIDGE_NAME);
345
346                 DBG("tethering stopped");
347         }
348 }
349
350 void __connman_tethering_update_interface(const char *interface)
351 {
352         DBG("interface %s", interface);
353
354         g_free(default_interface);
355
356         if (interface == NULL) {
357                 disable_nat(interface);
358                 default_interface = NULL;
359
360                 return;
361         }
362
363         default_interface = g_strdup(interface);
364
365         if (!g_atomic_int_get(&tethering_enabled))
366                 return;
367
368         enable_nat(interface);
369 }
370
371 int __connman_tethering_init(void)
372 {
373         DBG("");
374
375         tethering_enabled = 0;
376
377         return 0;
378 }
379
380 void __connman_tethering_cleanup(void)
381 {
382         DBG("");
383
384         if (g_atomic_int_get(&tethering_enabled)) {
385                 if (tethering_dhcp_server)
386                         dhcp_server_stop(tethering_dhcp_server);
387                 disable_bridge(BRIDGE_NAME);
388                 remove_bridge(BRIDGE_NAME);
389         }
390 }