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