7042040c6701f8ef83a57e71e4cf3d9018b9ab2d
[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 <netinet/in.h>
39 #include <linux/if_bridge.h>
40
41 #include "connman.h"
42
43 #include <gdhcp/gdhcp.h>
44
45 #include <gdbus.h>
46
47 #ifndef DBUS_TYPE_UNIX_FD
48 #define DBUS_TYPE_UNIX_FD -1
49 #endif
50
51 #define BRIDGE_NAME "tether"
52
53 #define DEFAULT_MTU     1500
54
55 static char *private_network_primary_dns = NULL;
56 static char *private_network_secondary_dns = NULL;
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         char *primary_dns;
76         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         char **ns;
195
196         DBG("enabled %d", tethering_enabled + 1);
197
198         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
199                 return;
200
201         err = __connman_bridge_create(BRIDGE_NAME);
202         if (err < 0) {
203                 __sync_fetch_and_sub(&tethering_enabled, 1);
204                 return;
205         }
206
207         index = connman_inet_ifindex(BRIDGE_NAME);
208         dhcp_ippool = __connman_ippool_create(index, 2, 252,
209                                                 tethering_restart, NULL);
210         if (dhcp_ippool == NULL) {
211                 connman_error("Fail to create IP pool");
212                 __connman_bridge_remove(BRIDGE_NAME);
213                 __sync_fetch_and_sub(&tethering_enabled, 1);
214                 return;
215         }
216
217         gateway = __connman_ippool_get_gateway(dhcp_ippool);
218         broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
219         subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
220         start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
221         end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
222
223         err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
224         if (err < 0 && err != -EALREADY) {
225                 __connman_ippool_unref(dhcp_ippool);
226                 __connman_bridge_remove(BRIDGE_NAME);
227                 __sync_fetch_and_sub(&tethering_enabled, 1);
228                 return;
229         }
230
231         ns = connman_setting_get_string_list("FallbackNameservers");
232         if (ns != NULL) {
233                 if (ns[0] != NULL) {
234                         g_free(private_network_primary_dns);
235                         private_network_primary_dns = g_strdup(ns[0]);
236                 }
237                 if (ns[1] != NULL) {
238                         g_free(private_network_secondary_dns);
239                         private_network_secondary_dns = g_strdup(ns[1]);
240                 }
241
242                 DBG("Fallback ns primary %s secondary %s",
243                         private_network_primary_dns,
244                         private_network_secondary_dns);
245         }
246
247         dns = gateway;
248         if (__connman_dnsproxy_add_listener(index) < 0) {
249                 connman_error("Can't add listener %s to DNS proxy",
250                                                                 BRIDGE_NAME);
251                 dns = private_network_primary_dns;
252                 DBG("Serving %s nameserver to clients", dns);
253         }
254
255         tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
256                                                 gateway, subnet_mask,
257                                                 start_ip, end_ip,
258                                                 24 * 3600, dns);
259         if (tethering_dhcp_server == NULL) {
260                 __connman_bridge_disable(BRIDGE_NAME);
261                 __connman_ippool_unref(dhcp_ippool);
262                 __connman_bridge_remove(BRIDGE_NAME);
263                 __sync_fetch_and_sub(&tethering_enabled, 1);
264                 return;
265         }
266
267         prefixlen =
268                 __connman_ipaddress_netmask_prefix_len(subnet_mask);
269         __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
270
271         DBG("tethering started");
272 }
273
274 void __connman_tethering_set_disabled(void)
275 {
276         int index;
277
278         DBG("enabled %d", tethering_enabled - 1);
279
280         index = connman_inet_ifindex(BRIDGE_NAME);
281         __connman_dnsproxy_remove_listener(index);
282
283         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
284                 return;
285
286         __connman_nat_disable(BRIDGE_NAME);
287
288         dhcp_server_stop(tethering_dhcp_server);
289
290         tethering_dhcp_server = NULL;
291
292         __connman_bridge_disable(BRIDGE_NAME);
293
294         __connman_ippool_unref(dhcp_ippool);
295
296         __connman_bridge_remove(BRIDGE_NAME);
297
298         g_free(private_network_primary_dns);
299         private_network_primary_dns = NULL;
300         g_free(private_network_secondary_dns);
301         private_network_secondary_dns = NULL;
302
303         DBG("tethering stopped");
304 }
305
306 static void setup_tun_interface(unsigned int flags, unsigned change,
307                 void *data)
308 {
309         struct connman_private_network *pn = data;
310         unsigned char prefixlen;
311         DBusMessageIter array, dict;
312         const char *server_ip;
313         const char *peer_ip;
314         const char *subnet_mask;
315         int err;
316
317         DBG("index %d flags %d change %d", pn->index,  flags, change);
318
319         if (flags & IFF_UP)
320                 return;
321
322         subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
323         server_ip = __connman_ippool_get_start_ip(pn->pool);
324         peer_ip = __connman_ippool_get_end_ip(pn->pool);
325         prefixlen =
326                 __connman_ipaddress_netmask_prefix_len(subnet_mask);
327
328         if ((__connman_inet_modify_address(RTM_NEWADDR,
329                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
330                                 server_ip, peer_ip, prefixlen, NULL)) < 0) {
331                 DBG("address setting failed");
332                 return;
333         }
334
335         connman_inet_ifup(pn->index);
336
337         err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
338         if (err < 0) {
339                 connman_error("failed to enable NAT");
340                 goto error;
341         }
342
343         dbus_message_iter_init_append(pn->reply, &array);
344
345         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
346                                                 &pn->path);
347
348         connman_dbus_dict_open(&array, &dict);
349
350         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
351                                         DBUS_TYPE_STRING, &server_ip);
352         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
353                                         DBUS_TYPE_STRING, &peer_ip);
354         if (pn->primary_dns != NULL)
355                 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
356                                         DBUS_TYPE_STRING, &pn->primary_dns);
357
358         if (pn->secondary_dns != NULL)
359                 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
360                                         DBUS_TYPE_STRING, &pn->secondary_dns);
361
362         connman_dbus_dict_close(&array, &dict);
363
364         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
365
366         g_dbus_send_message(connection, pn->reply);
367
368         return;
369
370 error:
371         pn->reply = __connman_error_failed(pn->msg, -err);
372         g_dbus_send_message(connection, pn->reply);
373
374         g_hash_table_remove(pn_hash, pn->path);
375 }
376
377 static void remove_private_network(gpointer user_data)
378 {
379         struct connman_private_network *pn = user_data;
380
381         __connman_nat_disable(BRIDGE_NAME);
382         connman_rtnl_remove_watch(pn->iface_watch);
383         __connman_ippool_unref(pn->pool);
384
385         if (pn->watch > 0) {
386                 g_dbus_remove_watch(connection, pn->watch);
387                 pn->watch = 0;
388         }
389
390         close(pn->fd);
391
392         g_free(pn->interface);
393         g_free(pn->owner);
394         g_free(pn->path);
395         g_free(pn->primary_dns);
396         g_free(pn->secondary_dns);
397         g_free(pn);
398 }
399
400 static void owner_disconnect(DBusConnection *conn, void *user_data)
401 {
402         struct connman_private_network *pn = user_data;
403
404         DBG("%s died", pn->owner);
405
406         pn->watch = 0;
407
408         g_hash_table_remove(pn_hash, pn->path);
409 }
410
411 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
412 {
413         struct connman_private_network *pn = user_data;
414
415         DBG("block used externally");
416
417         g_hash_table_remove(pn_hash, pn->path);
418 }
419
420 int __connman_private_network_request(DBusMessage *msg, const char *owner)
421 {
422         struct connman_private_network *pn;
423         char *iface = NULL;
424         char *path = NULL;
425         int index, fd, err;
426
427         if (DBUS_TYPE_UNIX_FD < 0)
428                 return -EINVAL;
429
430         fd = connman_inet_create_tunnel(&iface);
431         if (fd < 0)
432                 return fd;
433
434         path = g_strdup_printf("/tethering/%s", iface);
435
436         pn = g_hash_table_lookup(pn_hash, path);
437         if (pn) {
438                 g_free(path);
439                 g_free(iface);
440                 close(fd);
441                 return -EEXIST;
442         }
443
444         index = connman_inet_ifindex(iface);
445         if (index < 0) {
446                 err = -ENODEV;
447                 goto error;
448         }
449         DBG("interface %s", iface);
450
451         err = connman_inet_set_mtu(index, DEFAULT_MTU);
452
453         pn = g_try_new0(struct connman_private_network, 1);
454         if (pn == NULL) {
455                 err = -ENOMEM;
456                 goto error;
457         }
458
459         pn->owner = g_strdup(owner);
460         pn->path = path;
461         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
462                                         owner_disconnect, pn, NULL);
463         pn->msg = msg;
464         pn->reply = dbus_message_new_method_return(pn->msg);
465         if (pn->reply == NULL)
466                 goto error;
467
468         pn->fd = fd;
469         pn->interface = iface;
470         pn->index = index;
471         pn->pool = __connman_ippool_create(pn->index, 1, 1, ippool_disconnect, pn);
472         if (pn->pool == NULL) {
473                 errno = -ENOMEM;
474                 goto error;
475         }
476
477         pn->primary_dns = g_strdup(private_network_primary_dns);
478         pn->secondary_dns = g_strdup(private_network_secondary_dns);
479
480         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
481                                                 setup_tun_interface, pn);
482
483         g_hash_table_insert(pn_hash, pn->path, pn);
484
485         return 0;
486
487 error:
488         close(fd);
489         g_free(iface);
490         g_free(path);
491         g_free(pn);
492         return err;
493 }
494
495 int __connman_private_network_release(const char *path)
496 {
497         struct connman_private_network *pn;
498
499         pn = g_hash_table_lookup(pn_hash, path);
500         if (pn == NULL)
501                 return -EACCES;
502
503         g_hash_table_remove(pn_hash, path);
504         return 0;
505 }
506
507 int __connman_tethering_init(void)
508 {
509         DBG("");
510
511         tethering_enabled = 0;
512
513         connection = connman_dbus_get_connection();
514         if (connection == NULL)
515                 return -EFAULT;
516
517         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
518                                                 NULL, remove_private_network);
519
520         return 0;
521 }
522
523 void __connman_tethering_cleanup(void)
524 {
525         DBG("");
526
527         __sync_synchronize();
528         if (tethering_enabled == 0) {
529                 if (tethering_dhcp_server)
530                         dhcp_server_stop(tethering_dhcp_server);
531                 __connman_bridge_disable(BRIDGE_NAME);
532                 __connman_bridge_remove(BRIDGE_NAME);
533                 __connman_nat_disable(BRIDGE_NAME);
534         }
535
536         if (connection == NULL)
537                 return;
538
539         g_hash_table_destroy(pn_hash);
540         dbus_connection_unref(connection);
541 }