tethering: Avoid removing dhcp server twice.
[framework/connectivity/connman.git] / src / tethering.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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
39 #include "connman.h"
40
41 #include <gdhcp/gdhcp.h>
42
43 #include <gdbus.h>
44
45 #ifndef DBUS_TYPE_UNIX_FD
46 #define DBUS_TYPE_UNIX_FD -1
47 #endif
48
49 #define BRIDGE_PROC_DIR "/proc/sys/net/bridge"
50
51 #define BRIDGE_NAME "tether"
52 #define BRIDGE_IP "192.168.218.1"
53 #define BRIDGE_BCAST "192.168.218.255"
54 #define BRIDGE_SUBNET "255.255.255.0"
55 #define BRIDGE_IP_START "192.168.218.100"
56 #define BRIDGE_IP_END "192.168.218.200"
57 #define BRIDGE_DNS "8.8.8.8"
58
59 #define DEFAULT_MTU     1500
60
61 #define PRIVATE_NETWORK_IP "192.168.219.1"
62 #define PRIVATE_NETWORK_PEER_IP "192.168.219.2"
63 #define PRIVATE_NETWORK_NETMASK "255.255.255.0"
64 #define PRIVATE_NETWORK_PRIMARY_DNS BRIDGE_DNS
65 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
66
67 static char *default_interface = NULL;
68 static volatile int tethering_enabled;
69 static GDHCPServer *tethering_dhcp_server = NULL;
70 static DBusConnection *connection;
71 static GHashTable *pn_hash;
72
73 struct connman_private_network {
74         char *owner;
75         char *path;
76         guint watch;
77         DBusMessage *msg;
78         DBusMessage *reply;
79         int fd;
80         char *interface;
81         int index;
82         guint iface_watch;
83         const char *server_ip;
84         const char *peer_ip;
85         const char *primary_dns;
86         const char *secondary_dns;
87 };
88
89 const char *__connman_tethering_get_bridge(void)
90 {
91         struct stat st;
92
93         if (stat(BRIDGE_PROC_DIR, &st) < 0) {
94                 connman_error("Missing support for 802.1d ethernet bridging");
95                 return NULL;
96         }
97
98         return BRIDGE_NAME;
99 }
100
101 static void dhcp_server_debug(const char *str, void *data)
102 {
103         connman_info("%s: %s\n", (const char *) data, str);
104 }
105
106 static void dhcp_server_error(GDHCPServerError error)
107 {
108         switch (error) {
109         case G_DHCP_SERVER_ERROR_NONE:
110                 connman_error("OK");
111                 break;
112         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
113                 connman_error("Interface unavailable");
114                 break;
115         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
116                 connman_error("Interface in use");
117                 break;
118         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
119                 connman_error("Interface down");
120                 break;
121         case G_DHCP_SERVER_ERROR_NOMEM:
122                 connman_error("No memory");
123                 break;
124         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
125                 connman_error("Invalid index");
126                 break;
127         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
128                 connman_error("Invalid option");
129                 break;
130         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
131                 connman_error("Invalid address");
132                 break;
133         }
134 }
135
136 static GDHCPServer *dhcp_server_start(const char *bridge,
137                                 const char *router, const char* subnet,
138                                 const char *start_ip, const char *end_ip,
139                                 unsigned int lease_time, const char *dns)
140 {
141         GDHCPServerError error;
142         GDHCPServer *dhcp_server;
143         int index;
144
145         DBG("");
146
147         index = connman_inet_ifindex(bridge);
148         if (index < 0)
149                 return NULL;
150
151         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
152         if (dhcp_server == NULL) {
153                 dhcp_server_error(error);
154                 return NULL;
155         }
156
157         g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
158
159         g_dhcp_server_set_lease_time(dhcp_server, lease_time);
160         g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
161         g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
162         g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
163         g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
164
165         g_dhcp_server_start(dhcp_server);
166
167         return dhcp_server;
168 }
169
170 static void dhcp_server_stop(GDHCPServer *server)
171 {
172         if (server == NULL)
173                 return;
174
175         g_dhcp_server_unref(server);
176 }
177
178 static int set_forward_delay(const char *name, unsigned int delay)
179 {
180         FILE *f;
181         char *forward_delay_path;
182
183         forward_delay_path =
184                 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
185
186         if (forward_delay_path == NULL)
187                 return -ENOMEM;
188
189         f = fopen(forward_delay_path, "r+");
190
191         g_free(forward_delay_path);
192
193         if (f == NULL)
194                 return -errno;
195
196         fprintf(f, "%d", delay);
197
198         fclose(f);
199
200         return 0;
201 }
202
203 static int create_bridge(const char *name)
204 {
205         int sk, err;
206
207         DBG("name %s", name);
208
209         sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
210         if (sk < 0)
211                 return -EOPNOTSUPP;
212
213         if (ioctl(sk, SIOCBRADDBR, name) == -1) {
214                 err = -errno;
215                 if (err != -EEXIST)
216                         return -EOPNOTSUPP;
217         }
218
219         err = set_forward_delay(name, 0);
220
221         if (err < 0)
222                 ioctl(sk, SIOCBRDELBR, name);
223
224         close(sk);
225
226         return err;
227 }
228
229 static int remove_bridge(const char *name)
230 {
231         int sk, err;
232
233         DBG("name %s", name);
234
235         sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
236         if (sk < 0)
237                 return -EOPNOTSUPP;
238
239         err = ioctl(sk, SIOCBRDELBR, name);
240
241         close(sk);
242
243         if (err < 0)
244                 return -EOPNOTSUPP;
245
246         return 0;
247 }
248
249 static int enable_bridge(const char *name)
250 {
251         int err, index;
252
253         index = connman_inet_ifindex(name);
254         if (index < 0)
255                 return index;
256
257         err = __connman_inet_modify_address(RTM_NEWADDR,
258                         NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
259                                         BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
260         if (err < 0)
261                 return err;
262
263         return connman_inet_ifup(index);
264 }
265
266 static int disable_bridge(const char *name)
267 {
268         int index;
269
270         index = connman_inet_ifindex(name);
271         if (index < 0)
272                 return index;
273
274         return connman_inet_ifdown(index);
275 }
276
277 static int enable_ip_forward(connman_bool_t enable)
278 {
279
280         FILE *f;
281
282         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
283         if (f == NULL)
284                 return -errno;
285
286         if (enable == TRUE)
287                 fprintf(f, "1");
288         else
289                 fprintf(f, "0");
290
291         fclose(f);
292
293         return 0;
294 }
295
296 static int enable_nat(const char *interface)
297 {
298         int err;
299
300         if (interface == NULL)
301                 return 0;
302
303         /* Enable IPv4 forwarding */
304         err = enable_ip_forward(TRUE);
305         if (err < 0)
306                 return err;
307
308         /* POSTROUTING flush */
309         err = __connman_iptables_command("-t nat -F POSTROUTING");
310         if (err < 0)
311                 return err;
312
313         /* Enable masquerading */
314         err = __connman_iptables_command("-t nat -A POSTROUTING "
315                                         "-o %s -j MASQUERADE", interface);
316         if (err < 0)
317                 return err;
318
319         return __connman_iptables_commit("nat");
320 }
321
322 static void disable_nat(const char *interface)
323 {
324         int err;
325
326         /* Disable IPv4 forwarding */
327         enable_ip_forward(FALSE);
328
329         /* POSTROUTING flush */
330         err = __connman_iptables_command("-t nat -F POSTROUTING");
331         if (err < 0)
332                 return;
333
334         __connman_iptables_commit("nat");
335 }
336
337 void __connman_tethering_set_enabled(void)
338 {
339         int err;
340         const char *dns;
341
342         DBG("enabled %d", tethering_enabled + 1);
343
344         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
345                 return;
346
347         err = create_bridge(BRIDGE_NAME);
348         if (err < 0)
349                 return;
350
351         err = enable_bridge(BRIDGE_NAME);
352         if (err < 0 && err != -EALREADY) {
353                 remove_bridge(BRIDGE_NAME);
354                 return;
355         }
356
357         dns = BRIDGE_IP;
358         if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
359                 connman_error("Can't add listener %s to DNS proxy",
360                                                                 BRIDGE_NAME);
361                 dns = BRIDGE_DNS;
362         }
363
364         tethering_dhcp_server =
365                 dhcp_server_start(BRIDGE_NAME,
366                                         BRIDGE_IP, BRIDGE_SUBNET,
367                                         BRIDGE_IP_START, BRIDGE_IP_END,
368                                         24 * 3600, dns);
369         if (tethering_dhcp_server == NULL) {
370                 disable_bridge(BRIDGE_NAME);
371                 remove_bridge(BRIDGE_NAME);
372                 return;
373         }
374
375         enable_nat(default_interface);
376
377         DBG("tethering started");
378 }
379
380 void __connman_tethering_set_disabled(void)
381 {
382         DBG("enabled %d", tethering_enabled - 1);
383
384         __connman_dnsproxy_remove_listener(BRIDGE_NAME);
385
386         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
387                 return;
388
389         disable_nat(default_interface);
390
391         dhcp_server_stop(tethering_dhcp_server);
392
393         tethering_dhcp_server = NULL;
394
395         disable_bridge(BRIDGE_NAME);
396
397         remove_bridge(BRIDGE_NAME);
398
399         DBG("tethering stopped");
400 }
401
402 void __connman_tethering_update_interface(const char *interface)
403 {
404         DBG("interface %s", interface);
405
406         g_free(default_interface);
407
408         if (interface == NULL) {
409                 disable_nat(interface);
410                 default_interface = NULL;
411
412                 return;
413         }
414
415         default_interface = g_strdup(interface);
416
417         __sync_synchronize();
418         if (tethering_enabled == 0)
419                 return;
420
421         enable_nat(interface);
422 }
423
424 static void setup_tun_interface(unsigned int flags, unsigned change,
425                 void *data)
426 {
427         struct connman_private_network *pn = data;
428         unsigned char prefixlen;
429         DBusMessageIter array, dict;
430         int err;
431
432         DBG("index %d flags %d change %d", pn->index,  flags, change);
433
434         if (flags & IFF_UP)
435                 return;
436
437         prefixlen =
438                 __connman_ipconfig_netmask_prefix_len(PRIVATE_NETWORK_NETMASK);
439
440         if ((__connman_inet_modify_address(RTM_NEWADDR,
441                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
442                                 pn->server_ip, pn->peer_ip,
443                                 prefixlen, NULL)) < 0) {
444                 DBG("address setting failed");
445                 return;
446         }
447
448         connman_inet_ifup(pn->index);
449
450         err = enable_nat(default_interface);
451         if (err < 0) {
452                 connman_error("failed to enable NAT on %s", default_interface);
453                 goto error;
454         }
455
456         dbus_message_iter_init_append(pn->reply, &array);
457
458         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
459                                                 &pn->path);
460
461         connman_dbus_dict_open(&array, &dict);
462
463         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
464                                         DBUS_TYPE_STRING, &pn->server_ip);
465         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
466                                         DBUS_TYPE_STRING, &pn->peer_ip);
467         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
468                                         DBUS_TYPE_STRING, &pn->primary_dns);
469         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
470                                         DBUS_TYPE_STRING, &pn->secondary_dns);
471
472         connman_dbus_dict_close(&array, &dict);
473
474         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
475
476         g_dbus_send_message(connection, pn->reply);
477
478         return;
479
480 error:
481         pn->reply = __connman_error_failed(pn->msg, -err);
482         g_dbus_send_message(connection, pn->reply);
483
484         g_hash_table_remove(pn_hash, pn->path);
485 }
486
487 static void remove_private_network(gpointer user_data)
488 {
489         struct connman_private_network *pn = user_data;
490
491         disable_nat(default_interface);
492         connman_rtnl_remove_watch(pn->iface_watch);
493
494         if (pn->watch > 0) {
495                 g_dbus_remove_watch(connection, pn->watch);
496                 pn->watch = 0;
497         }
498
499         close(pn->fd);
500
501         g_free(pn->interface);
502         g_free(pn->owner);
503         g_free(pn->path);
504         g_free(pn);
505 }
506
507 static void owner_disconnect(DBusConnection *connection, void *user_data)
508 {
509         struct connman_private_network *pn = user_data;
510
511         DBG("%s died", pn->owner);
512
513         pn->watch = 0;
514
515         g_hash_table_remove(pn_hash, pn->path);
516 }
517
518 int __connman_private_network_request(DBusMessage *msg, const char *owner)
519 {
520         struct connman_private_network *pn;
521         char *iface = NULL;
522         char *path = NULL;
523         int index, fd, err;
524
525         if (DBUS_TYPE_UNIX_FD < 0)
526                 return -EINVAL;
527
528         fd = connman_inet_create_tunnel(&iface);
529         if (fd < 0)
530                 return fd;
531
532         path = g_strdup_printf("/tethering/%s", iface);
533
534         pn = g_hash_table_lookup(pn_hash, path);
535         if (pn) {
536                 g_free(path);
537                 g_free(iface);
538                 close(fd);
539                 return -EEXIST;
540         }
541
542         index = connman_inet_ifindex(iface);
543         if (index < 0) {
544                 err = -ENODEV;
545                 goto error;
546         }
547         DBG("interface %s", iface);
548
549         err = connman_inet_set_mtu(index, DEFAULT_MTU);
550
551         pn = g_try_new0(struct connman_private_network, 1);
552         if (pn == NULL) {
553                 err = -ENOMEM;
554                 goto error;
555         }
556
557         pn->owner = g_strdup(owner);
558         pn->path = path;
559         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
560                                         owner_disconnect, pn, NULL);
561         pn->msg = msg;
562         pn->reply = dbus_message_new_method_return(pn->msg);
563         if (pn->reply == NULL)
564                 goto error;
565
566         pn->fd = fd;
567         pn->interface = iface;
568         pn->index = index;
569         pn->server_ip = PRIVATE_NETWORK_IP;
570         pn->peer_ip = PRIVATE_NETWORK_PEER_IP;
571         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
572         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
573
574         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
575                                                 setup_tun_interface, pn);
576
577         g_hash_table_insert(pn_hash, pn->path, pn);
578
579         return 0;
580
581 error:
582         close(fd);
583         g_free(iface);
584         g_free(path);
585         g_free(pn);
586         return err;
587 }
588
589 int __connman_private_network_release(const char *path)
590 {
591         struct connman_private_network *pn;
592
593         pn = g_hash_table_lookup(pn_hash, path);
594         if (pn == NULL)
595                 return -EACCES;
596
597         g_hash_table_remove(pn_hash, path);
598         return 0;
599 }
600
601 int __connman_tethering_init(void)
602 {
603         DBG("");
604
605         tethering_enabled = 0;
606
607         connection = connman_dbus_get_connection();
608         if (connection == NULL)
609                 return -EFAULT;
610
611         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
612                                                 NULL, remove_private_network);
613
614         return 0;
615 }
616
617 void __connman_tethering_cleanup(void)
618 {
619         DBG("");
620
621         __sync_synchronize();
622         if (tethering_enabled == 0) {
623                 if (tethering_dhcp_server)
624                         dhcp_server_stop(tethering_dhcp_server);
625                 disable_bridge(BRIDGE_NAME);
626                 remove_bridge(BRIDGE_NAME);
627         }
628
629         if (connection == NULL)
630                 return;
631
632         g_hash_table_destroy(pn_hash);
633         dbus_connection_unref(connection);
634 }