tethering: Fix bridge module loading problem
[platform/upstream/connman.git] / src / tethering.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  ProFUSION embedded systems
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <sys/ioctl.h>
33 #include <net/if.h>
34 #include <linux/sockios.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <linux/if_tun.h>
38 #include <linux/if_bridge.h>
39
40 #include "connman.h"
41
42 #include <gdhcp/gdhcp.h>
43
44 #include <gdbus.h>
45
46 #ifndef DBUS_TYPE_UNIX_FD
47 #define DBUS_TYPE_UNIX_FD -1
48 #endif
49
50 #define BRIDGE_NAME "tether"
51 #define BRIDGE_DNS "8.8.8.8"
52
53 #define DEFAULT_MTU     1500
54
55 #define PRIVATE_NETWORK_PRIMARY_DNS BRIDGE_DNS
56 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
57
58 static volatile int tethering_enabled;
59 static GDHCPServer *tethering_dhcp_server = NULL;
60 static struct connman_ippool *dhcp_ippool = NULL;
61 static DBusConnection *connection;
62 static GHashTable *pn_hash;
63
64 struct connman_private_network {
65         char *owner;
66         char *path;
67         guint watch;
68         DBusMessage *msg;
69         DBusMessage *reply;
70         int fd;
71         char *interface;
72         int index;
73         guint iface_watch;
74         struct connman_ippool *pool;
75         const char *primary_dns;
76         const char *secondary_dns;
77 };
78
79 const char *__connman_tethering_get_bridge(void)
80 {
81         int sk, err;
82         unsigned long args[3];
83
84         sk = socket(AF_INET, SOCK_STREAM, 0);
85         if (sk < 0)
86                 return NULL;
87
88         args[0] = BRCTL_GET_VERSION;
89         args[1] = args[2] = 0;
90         err = ioctl(sk, SIOCGIFBR, &args);
91         close(sk);
92         if (err == -1)
93                 return NULL;
94
95         return BRIDGE_NAME;
96 }
97
98 static void dhcp_server_debug(const char *str, void *data)
99 {
100         connman_info("%s: %s\n", (const char *) data, str);
101 }
102
103 static void dhcp_server_error(GDHCPServerError error)
104 {
105         switch (error) {
106         case G_DHCP_SERVER_ERROR_NONE:
107                 connman_error("OK");
108                 break;
109         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
110                 connman_error("Interface unavailable");
111                 break;
112         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
113                 connman_error("Interface in use");
114                 break;
115         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
116                 connman_error("Interface down");
117                 break;
118         case G_DHCP_SERVER_ERROR_NOMEM:
119                 connman_error("No memory");
120                 break;
121         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
122                 connman_error("Invalid index");
123                 break;
124         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
125                 connman_error("Invalid option");
126                 break;
127         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
128                 connman_error("Invalid address");
129                 break;
130         }
131 }
132
133 static GDHCPServer *dhcp_server_start(const char *bridge,
134                                 const char *router, const char* subnet,
135                                 const char *start_ip, const char *end_ip,
136                                 unsigned int lease_time, const char *dns)
137 {
138         GDHCPServerError error;
139         GDHCPServer *dhcp_server;
140         int index;
141
142         DBG("");
143
144         index = connman_inet_ifindex(bridge);
145         if (index < 0)
146                 return NULL;
147
148         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
149         if (dhcp_server == NULL) {
150                 dhcp_server_error(error);
151                 return NULL;
152         }
153
154         g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
155
156         g_dhcp_server_set_lease_time(dhcp_server, lease_time);
157         g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
158         g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
159         g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
160         g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
161
162         g_dhcp_server_start(dhcp_server);
163
164         return dhcp_server;
165 }
166
167 static void dhcp_server_stop(GDHCPServer *server)
168 {
169         if (server == NULL)
170                 return;
171
172         g_dhcp_server_unref(server);
173 }
174
175 static void tethering_restart(struct connman_ippool *pool, void *user_data)
176 {
177         __connman_tethering_set_disabled();
178         __connman_tethering_set_enabled();
179 }
180
181 void __connman_tethering_set_enabled(void)
182 {
183         int index;
184         int err;
185         const char *gateway;
186         const char *broadcast;
187         const char *subnet_mask;
188         const char *start_ip;
189         const char *end_ip;
190         const char *dns;
191         unsigned char prefixlen;
192
193         DBG("enabled %d", tethering_enabled + 1);
194
195         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
196                 return;
197
198         err = __connman_bridge_create(BRIDGE_NAME);
199         if (err < 0)
200                 return;
201
202         index = connman_inet_ifindex(BRIDGE_NAME);
203         dhcp_ippool = __connman_ippool_create(index, 2, 252,
204                                                 tethering_restart, NULL);
205         if (dhcp_ippool == NULL) {
206                 connman_error("Fail to create IP pool");
207                 return;
208         }
209
210         gateway = __connman_ippool_get_gateway(dhcp_ippool);
211         broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
212         subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
213         start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
214         end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
215
216         err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
217         if (err < 0 && err != -EALREADY) {
218                 __connman_bridge_remove(BRIDGE_NAME);
219                 return;
220         }
221
222         dns = gateway;
223         if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
224                 connman_error("Can't add listener %s to DNS proxy",
225                                                                 BRIDGE_NAME);
226                 dns = BRIDGE_DNS;
227         }
228
229         tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
230                                                 gateway, subnet_mask,
231                                                 start_ip, end_ip,
232                                                 24 * 3600, dns);
233         if (tethering_dhcp_server == NULL) {
234                 __connman_bridge_disable(BRIDGE_NAME);
235                 __connman_bridge_remove(BRIDGE_NAME);
236                 return;
237         }
238
239         prefixlen =
240                 __connman_ipconfig_netmask_prefix_len(subnet_mask);
241         __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
242
243         DBG("tethering started");
244 }
245
246 void __connman_tethering_set_disabled(void)
247 {
248         DBG("enabled %d", tethering_enabled - 1);
249
250         __connman_dnsproxy_remove_listener(BRIDGE_NAME);
251
252         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
253                 return;
254
255         __connman_nat_disable(BRIDGE_NAME);
256
257         dhcp_server_stop(tethering_dhcp_server);
258
259         tethering_dhcp_server = NULL;
260
261         __connman_bridge_disable(BRIDGE_NAME);
262
263         __connman_ippool_unref(dhcp_ippool);
264
265         __connman_bridge_remove(BRIDGE_NAME);
266
267         DBG("tethering stopped");
268 }
269
270 static void setup_tun_interface(unsigned int flags, unsigned change,
271                 void *data)
272 {
273         struct connman_private_network *pn = data;
274         unsigned char prefixlen;
275         DBusMessageIter array, dict;
276         const char *server_ip;
277         const char *peer_ip;
278         const char *subnet_mask;
279         int err;
280
281         DBG("index %d flags %d change %d", pn->index,  flags, change);
282
283         if (flags & IFF_UP)
284                 return;
285
286         subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
287         server_ip = __connman_ippool_get_start_ip(pn->pool);
288         peer_ip = __connman_ippool_get_end_ip(pn->pool);
289         prefixlen =
290                 __connman_ipconfig_netmask_prefix_len(subnet_mask);
291
292         if ((__connman_inet_modify_address(RTM_NEWADDR,
293                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
294                                 server_ip, peer_ip, prefixlen, NULL)) < 0) {
295                 DBG("address setting failed");
296                 return;
297         }
298
299         connman_inet_ifup(pn->index);
300
301         err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
302         if (err < 0) {
303                 connman_error("failed to enable NAT");
304                 goto error;
305         }
306
307         dbus_message_iter_init_append(pn->reply, &array);
308
309         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
310                                                 &pn->path);
311
312         connman_dbus_dict_open(&array, &dict);
313
314         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
315                                         DBUS_TYPE_STRING, &server_ip);
316         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
317                                         DBUS_TYPE_STRING, &peer_ip);
318         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
319                                         DBUS_TYPE_STRING, &pn->primary_dns);
320         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
321                                         DBUS_TYPE_STRING, &pn->secondary_dns);
322
323         connman_dbus_dict_close(&array, &dict);
324
325         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
326
327         g_dbus_send_message(connection, pn->reply);
328
329         return;
330
331 error:
332         pn->reply = __connman_error_failed(pn->msg, -err);
333         g_dbus_send_message(connection, pn->reply);
334
335         g_hash_table_remove(pn_hash, pn->path);
336 }
337
338 static void remove_private_network(gpointer user_data)
339 {
340         struct connman_private_network *pn = user_data;
341
342         __connman_nat_disable(BRIDGE_NAME);
343         connman_rtnl_remove_watch(pn->iface_watch);
344         __connman_ippool_unref(pn->pool);
345
346         if (pn->watch > 0) {
347                 g_dbus_remove_watch(connection, pn->watch);
348                 pn->watch = 0;
349         }
350
351         close(pn->fd);
352
353         g_free(pn->interface);
354         g_free(pn->owner);
355         g_free(pn->path);
356         g_free(pn);
357 }
358
359 static void owner_disconnect(DBusConnection *conn, void *user_data)
360 {
361         struct connman_private_network *pn = user_data;
362
363         DBG("%s died", pn->owner);
364
365         pn->watch = 0;
366
367         g_hash_table_remove(pn_hash, pn->path);
368 }
369
370 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
371 {
372         struct connman_private_network *pn = user_data;
373
374         DBG("block used externally");
375
376         g_hash_table_remove(pn_hash, pn->path);
377 }
378
379 int __connman_private_network_request(DBusMessage *msg, const char *owner)
380 {
381         struct connman_private_network *pn;
382         char *iface = NULL;
383         char *path = NULL;
384         int index, fd, err;
385
386         if (DBUS_TYPE_UNIX_FD < 0)
387                 return -EINVAL;
388
389         fd = connman_inet_create_tunnel(&iface);
390         if (fd < 0)
391                 return fd;
392
393         path = g_strdup_printf("/tethering/%s", iface);
394
395         pn = g_hash_table_lookup(pn_hash, path);
396         if (pn) {
397                 g_free(path);
398                 g_free(iface);
399                 close(fd);
400                 return -EEXIST;
401         }
402
403         index = connman_inet_ifindex(iface);
404         if (index < 0) {
405                 err = -ENODEV;
406                 goto error;
407         }
408         DBG("interface %s", iface);
409
410         err = connman_inet_set_mtu(index, DEFAULT_MTU);
411
412         pn = g_try_new0(struct connman_private_network, 1);
413         if (pn == NULL) {
414                 err = -ENOMEM;
415                 goto error;
416         }
417
418         pn->owner = g_strdup(owner);
419         pn->path = path;
420         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
421                                         owner_disconnect, pn, NULL);
422         pn->msg = msg;
423         pn->reply = dbus_message_new_method_return(pn->msg);
424         if (pn->reply == NULL)
425                 goto error;
426
427         pn->fd = fd;
428         pn->interface = iface;
429         pn->index = index;
430         pn->pool = __connman_ippool_create(pn->index, 1, 1, ippool_disconnect, pn);
431         if (pn->pool == NULL) {
432                 errno = -ENOMEM;
433                 goto error;
434         }
435
436         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
437         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
438
439         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
440                                                 setup_tun_interface, pn);
441
442         g_hash_table_insert(pn_hash, pn->path, pn);
443
444         return 0;
445
446 error:
447         close(fd);
448         g_free(iface);
449         g_free(path);
450         g_free(pn);
451         return err;
452 }
453
454 int __connman_private_network_release(const char *path)
455 {
456         struct connman_private_network *pn;
457
458         pn = g_hash_table_lookup(pn_hash, path);
459         if (pn == NULL)
460                 return -EACCES;
461
462         g_hash_table_remove(pn_hash, path);
463         return 0;
464 }
465
466 int __connman_tethering_init(void)
467 {
468         DBG("");
469
470         tethering_enabled = 0;
471
472         connection = connman_dbus_get_connection();
473         if (connection == NULL)
474                 return -EFAULT;
475
476         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
477                                                 NULL, remove_private_network);
478
479         return 0;
480 }
481
482 void __connman_tethering_cleanup(void)
483 {
484         DBG("");
485
486         __sync_synchronize();
487         if (tethering_enabled == 0) {
488                 if (tethering_dhcp_server)
489                         dhcp_server_stop(tethering_dhcp_server);
490                 __connman_bridge_disable(BRIDGE_NAME);
491                 __connman_bridge_remove(BRIDGE_NAME);
492                 __connman_nat_disable(BRIDGE_NAME);
493         }
494
495         if (connection == NULL)
496                 return;
497
498         g_hash_table_destroy(pn_hash);
499         dbus_connection_unref(connection);
500 }