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