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