tethering: Reset the tethering refcount on error
[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                 connman_error("Missing support for 802.1d ethernet bridging");
94                 return NULL;
95         }
96
97         return BRIDGE_NAME;
98 }
99
100 static void dhcp_server_debug(const char *str, void *data)
101 {
102         connman_info("%s: %s\n", (const char *) data, str);
103 }
104
105 static void dhcp_server_error(GDHCPServerError error)
106 {
107         switch (error) {
108         case G_DHCP_SERVER_ERROR_NONE:
109                 connman_error("OK");
110                 break;
111         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
112                 connman_error("Interface unavailable");
113                 break;
114         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
115                 connman_error("Interface in use");
116                 break;
117         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
118                 connman_error("Interface down");
119                 break;
120         case G_DHCP_SERVER_ERROR_NOMEM:
121                 connman_error("No memory");
122                 break;
123         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
124                 connman_error("Invalid index");
125                 break;
126         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
127                 connman_error("Invalid option");
128                 break;
129         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
130                 connman_error("Invalid address");
131                 break;
132         }
133 }
134
135 static GDHCPServer *dhcp_server_start(const char *bridge,
136                                 const char *router, const char* subnet,
137                                 const char *start_ip, const char *end_ip,
138                                 unsigned int lease_time, const char *dns)
139 {
140         GDHCPServerError error;
141         GDHCPServer *dhcp_server;
142         int index;
143
144         DBG("");
145
146         index = connman_inet_ifindex(bridge);
147         if (index < 0)
148                 return NULL;
149
150         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
151         if (dhcp_server == NULL) {
152                 dhcp_server_error(error);
153                 return NULL;
154         }
155
156         g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
157
158         g_dhcp_server_set_lease_time(dhcp_server, lease_time);
159         g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
160         g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
161         g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
162         g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
163
164         g_dhcp_server_start(dhcp_server);
165
166         return dhcp_server;
167 }
168
169 static void dhcp_server_stop(GDHCPServer *server)
170 {
171         if (server == NULL)
172                 return;
173
174         g_dhcp_server_unref(server);
175 }
176
177 static void tethering_restart(struct connman_ippool *pool, void *user_data)
178 {
179         __connman_tethering_set_disabled();
180         __connman_tethering_set_enabled();
181 }
182
183 void __connman_tethering_set_enabled(void)
184 {
185         int index;
186         int err;
187         const char *gateway;
188         const char *broadcast;
189         const char *subnet_mask;
190         const char *start_ip;
191         const char *end_ip;
192         const char *dns;
193         unsigned char prefixlen;
194
195         DBG("enabled %d", tethering_enabled + 1);
196
197         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
198                 return;
199
200         err = __connman_bridge_create(BRIDGE_NAME);
201         if (err < 0) {
202                 __sync_fetch_and_sub(&tethering_enabled, 1);
203                 return;
204         }
205
206         index = connman_inet_ifindex(BRIDGE_NAME);
207         dhcp_ippool = __connman_ippool_create(index, 2, 252,
208                                                 tethering_restart, NULL);
209         if (dhcp_ippool == NULL) {
210                 connman_error("Fail to create IP pool");
211                 __connman_bridge_remove(BRIDGE_NAME);
212                 __sync_fetch_and_sub(&tethering_enabled, 1);
213                 return;
214         }
215
216         gateway = __connman_ippool_get_gateway(dhcp_ippool);
217         broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
218         subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
219         start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
220         end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
221
222         err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
223         if (err < 0 && err != -EALREADY) {
224                 __connman_ippool_unref(dhcp_ippool);
225                 __connman_bridge_remove(BRIDGE_NAME);
226                 __sync_fetch_and_sub(&tethering_enabled, 1);
227                 return;
228         }
229
230         dns = gateway;
231         if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
232                 connman_error("Can't add listener %s to DNS proxy",
233                                                                 BRIDGE_NAME);
234                 dns = BRIDGE_DNS;
235         }
236
237         tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
238                                                 gateway, subnet_mask,
239                                                 start_ip, end_ip,
240                                                 24 * 3600, dns);
241         if (tethering_dhcp_server == NULL) {
242                 __connman_bridge_disable(BRIDGE_NAME);
243                 __connman_ippool_unref(dhcp_ippool);
244                 __connman_bridge_remove(BRIDGE_NAME);
245                 __sync_fetch_and_sub(&tethering_enabled, 1);
246                 return;
247         }
248
249         prefixlen =
250                 __connman_ipconfig_netmask_prefix_len(subnet_mask);
251         __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
252
253         DBG("tethering started");
254 }
255
256 void __connman_tethering_set_disabled(void)
257 {
258         DBG("enabled %d", tethering_enabled - 1);
259
260         __connman_dnsproxy_remove_listener(BRIDGE_NAME);
261
262         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
263                 return;
264
265         __connman_nat_disable(BRIDGE_NAME);
266
267         dhcp_server_stop(tethering_dhcp_server);
268
269         tethering_dhcp_server = NULL;
270
271         __connman_bridge_disable(BRIDGE_NAME);
272
273         __connman_ippool_unref(dhcp_ippool);
274
275         __connman_bridge_remove(BRIDGE_NAME);
276
277         DBG("tethering stopped");
278 }
279
280 static void setup_tun_interface(unsigned int flags, unsigned change,
281                 void *data)
282 {
283         struct connman_private_network *pn = data;
284         unsigned char prefixlen;
285         DBusMessageIter array, dict;
286         const char *server_ip;
287         const char *peer_ip;
288         const char *subnet_mask;
289         int err;
290
291         DBG("index %d flags %d change %d", pn->index,  flags, change);
292
293         if (flags & IFF_UP)
294                 return;
295
296         subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
297         server_ip = __connman_ippool_get_start_ip(pn->pool);
298         peer_ip = __connman_ippool_get_end_ip(pn->pool);
299         prefixlen =
300                 __connman_ipconfig_netmask_prefix_len(subnet_mask);
301
302         if ((__connman_inet_modify_address(RTM_NEWADDR,
303                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
304                                 server_ip, peer_ip, prefixlen, NULL)) < 0) {
305                 DBG("address setting failed");
306                 return;
307         }
308
309         connman_inet_ifup(pn->index);
310
311         err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
312         if (err < 0) {
313                 connman_error("failed to enable NAT");
314                 goto error;
315         }
316
317         dbus_message_iter_init_append(pn->reply, &array);
318
319         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
320                                                 &pn->path);
321
322         connman_dbus_dict_open(&array, &dict);
323
324         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
325                                         DBUS_TYPE_STRING, &server_ip);
326         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
327                                         DBUS_TYPE_STRING, &peer_ip);
328         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
329                                         DBUS_TYPE_STRING, &pn->primary_dns);
330         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
331                                         DBUS_TYPE_STRING, &pn->secondary_dns);
332
333         connman_dbus_dict_close(&array, &dict);
334
335         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
336
337         g_dbus_send_message(connection, pn->reply);
338
339         return;
340
341 error:
342         pn->reply = __connman_error_failed(pn->msg, -err);
343         g_dbus_send_message(connection, pn->reply);
344
345         g_hash_table_remove(pn_hash, pn->path);
346 }
347
348 static void remove_private_network(gpointer user_data)
349 {
350         struct connman_private_network *pn = user_data;
351
352         __connman_nat_disable(BRIDGE_NAME);
353         connman_rtnl_remove_watch(pn->iface_watch);
354         __connman_ippool_unref(pn->pool);
355
356         if (pn->watch > 0) {
357                 g_dbus_remove_watch(connection, pn->watch);
358                 pn->watch = 0;
359         }
360
361         close(pn->fd);
362
363         g_free(pn->interface);
364         g_free(pn->owner);
365         g_free(pn->path);
366         g_free(pn);
367 }
368
369 static void owner_disconnect(DBusConnection *conn, void *user_data)
370 {
371         struct connman_private_network *pn = user_data;
372
373         DBG("%s died", pn->owner);
374
375         pn->watch = 0;
376
377         g_hash_table_remove(pn_hash, pn->path);
378 }
379
380 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
381 {
382         struct connman_private_network *pn = user_data;
383
384         DBG("block used externally");
385
386         g_hash_table_remove(pn_hash, pn->path);
387 }
388
389 int __connman_private_network_request(DBusMessage *msg, const char *owner)
390 {
391         struct connman_private_network *pn;
392         char *iface = NULL;
393         char *path = NULL;
394         int index, fd, err;
395
396         if (DBUS_TYPE_UNIX_FD < 0)
397                 return -EINVAL;
398
399         fd = connman_inet_create_tunnel(&iface);
400         if (fd < 0)
401                 return fd;
402
403         path = g_strdup_printf("/tethering/%s", iface);
404
405         pn = g_hash_table_lookup(pn_hash, path);
406         if (pn) {
407                 g_free(path);
408                 g_free(iface);
409                 close(fd);
410                 return -EEXIST;
411         }
412
413         index = connman_inet_ifindex(iface);
414         if (index < 0) {
415                 err = -ENODEV;
416                 goto error;
417         }
418         DBG("interface %s", iface);
419
420         err = connman_inet_set_mtu(index, DEFAULT_MTU);
421
422         pn = g_try_new0(struct connman_private_network, 1);
423         if (pn == NULL) {
424                 err = -ENOMEM;
425                 goto error;
426         }
427
428         pn->owner = g_strdup(owner);
429         pn->path = path;
430         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
431                                         owner_disconnect, pn, NULL);
432         pn->msg = msg;
433         pn->reply = dbus_message_new_method_return(pn->msg);
434         if (pn->reply == NULL)
435                 goto error;
436
437         pn->fd = fd;
438         pn->interface = iface;
439         pn->index = index;
440         pn->pool = __connman_ippool_create(pn->index, 1, 1, ippool_disconnect, pn);
441         if (pn->pool == NULL) {
442                 errno = -ENOMEM;
443                 goto error;
444         }
445
446         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
447         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
448
449         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
450                                                 setup_tun_interface, pn);
451
452         g_hash_table_insert(pn_hash, pn->path, pn);
453
454         return 0;
455
456 error:
457         close(fd);
458         g_free(iface);
459         g_free(path);
460         g_free(pn);
461         return err;
462 }
463
464 int __connman_private_network_release(const char *path)
465 {
466         struct connman_private_network *pn;
467
468         pn = g_hash_table_lookup(pn_hash, path);
469         if (pn == NULL)
470                 return -EACCES;
471
472         g_hash_table_remove(pn_hash, path);
473         return 0;
474 }
475
476 int __connman_tethering_init(void)
477 {
478         DBG("");
479
480         tethering_enabled = 0;
481
482         connection = connman_dbus_get_connection();
483         if (connection == NULL)
484                 return -EFAULT;
485
486         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
487                                                 NULL, remove_private_network);
488
489         return 0;
490 }
491
492 void __connman_tethering_cleanup(void)
493 {
494         DBG("");
495
496         __sync_synchronize();
497         if (tethering_enabled == 0) {
498                 if (tethering_dhcp_server)
499                         dhcp_server_stop(tethering_dhcp_server);
500                 __connman_bridge_disable(BRIDGE_NAME);
501                 __connman_bridge_remove(BRIDGE_NAME);
502                 __connman_nat_disable(BRIDGE_NAME);
503         }
504
505         if (connection == NULL)
506                 return;
507
508         g_hash_table_destroy(pn_hash);
509         dbus_connection_unref(connection);
510 }