tethering: Use tether device IP as tethering DNS server
[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                 err = create_bridge(BRIDGE_NAME);
338                 if (err < 0)
339                         return;
340
341                 err = enable_bridge(BRIDGE_NAME);
342                 if (err < 0) {
343                         remove_bridge(BRIDGE_NAME);
344                         return;
345                 }
346
347                 if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0)
348                         connman_error("Can't add listener %s to DNS proxy",
349                                                                 BRIDGE_NAME);
350
351                 tethering_dhcp_server =
352                         dhcp_server_start(BRIDGE_NAME,
353                                                 BRIDGE_IP, BRIDGE_SUBNET,
354                                                 BRIDGE_IP_START, BRIDGE_IP_END,
355                                                         24 * 3600, BRIDGE_IP);
356                 if (tethering_dhcp_server == NULL) {
357                         disable_bridge(BRIDGE_NAME);
358                         remove_bridge(BRIDGE_NAME);
359                         return;
360                 }
361
362                 enable_nat(default_interface);
363
364                 DBG("tethering started");
365         }
366 }
367
368 void __connman_tethering_set_disabled(void)
369 {
370         DBG("enabled %d", tethering_enabled - 1);
371
372         __connman_dnsproxy_remove_listener(BRIDGE_NAME);
373
374         if (g_atomic_int_dec_and_test(&tethering_enabled) == TRUE) {
375                 disable_nat(default_interface);
376
377                 dhcp_server_stop(tethering_dhcp_server);
378
379                 disable_bridge(BRIDGE_NAME);
380
381                 remove_bridge(BRIDGE_NAME);
382
383                 DBG("tethering stopped");
384         }
385 }
386
387 void __connman_tethering_update_interface(const char *interface)
388 {
389         DBG("interface %s", interface);
390
391         g_free(default_interface);
392
393         if (interface == NULL) {
394                 disable_nat(interface);
395                 default_interface = NULL;
396
397                 return;
398         }
399
400         default_interface = g_strdup(interface);
401
402         if (!g_atomic_int_get(&tethering_enabled))
403                 return;
404
405         enable_nat(interface);
406 }
407
408 static void setup_tun_interface(unsigned int flags, unsigned change,
409                 void *data)
410 {
411         struct connman_private_network *pn = data;
412         unsigned char prefixlen;
413         DBusMessage *reply;
414         DBusMessageIter array, dict;
415         int err;
416
417         DBG("index %d flags %d change %d", pn->index,  flags, change);
418
419         if (flags & IFF_UP)
420                 return;
421
422         prefixlen =
423                 __connman_ipconfig_netmask_prefix_len(PRIVATE_NETWORK_NETMASK);
424
425         if ((__connman_inet_modify_address(RTM_NEWADDR,
426                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
427                                 pn->server_ip, pn->peer_ip,
428                                 prefixlen, NULL)) < 0) {
429                 DBG("address setting failed");
430                 return;
431         }
432
433         connman_inet_ifup(pn->index);
434
435         err = enable_nat(default_interface);
436         if (err < 0) {
437                 connman_error("failed to enable NAT on %s", default_interface);
438                 goto error;
439         }
440
441         reply = dbus_message_new_method_return(pn->msg);
442
443         if (reply == NULL)
444                 goto error;
445
446         dbus_message_iter_init_append(reply, &array);
447
448         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
449
450         connman_dbus_dict_open(&array, &dict);
451
452         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
453                                                 DBUS_TYPE_STRING, &pn->server_ip);
454         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
455                                                 DBUS_TYPE_STRING, &pn->peer_ip);
456         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
457                                                 DBUS_TYPE_STRING, &pn->primary_dns);
458         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
459                                                 DBUS_TYPE_STRING, &pn->secondary_dns);
460
461         connman_dbus_dict_close(&array, &dict);
462
463         g_dbus_send_message(connection, reply);
464
465         return;
466
467 error:
468         reply = __connman_error_failed(pn->msg, -err);
469         g_dbus_send_message(connection, reply);
470
471         g_hash_table_remove(pn_hash, pn->owner);
472 }
473
474 static void remove_private_network(gpointer user_data)
475 {
476         struct connman_private_network *pn = user_data;
477
478         close(pn->fd);
479
480         connman_rtnl_remove_watch(pn->iface_watch);
481
482         disable_nat(default_interface);
483
484         if (pn->watch > 0) {
485                 g_dbus_remove_watch(connection, pn->watch);
486                 pn->watch = 0;
487         }
488
489         g_free(pn->interface);
490         g_free(pn->owner);
491         g_free(pn);
492 }
493
494 static void owner_disconnect(DBusConnection *connection, void *user_data)
495 {
496         struct connman_private_network *pn = user_data;
497
498         DBG("%s died", pn->owner);
499
500         pn->watch = 0;
501
502         g_hash_table_remove(pn_hash, pn->owner);
503 }
504
505 int __connman_private_network_request(DBusMessage *msg, const char *owner)
506 {
507         struct connman_private_network *pn;
508         char *iface = NULL;
509         int index, fd, err;
510
511         pn = g_hash_table_lookup(pn_hash, owner);
512         if (pn != NULL)
513                 return -EEXIST;
514
515         fd = connman_inet_create_tunnel(&iface);
516         if (fd < 0)
517                 return fd;
518
519         index = connman_inet_ifindex(iface);
520         if (index < 0) {
521                 err = -ENODEV;
522                 goto error;
523         }
524         DBG("inteface %s", iface);
525
526         err = connman_inet_set_mtu(index, DEFAULT_MTU);
527
528         pn = g_try_new0(struct connman_private_network, 1);
529         if (pn == NULL) {
530                 err = -ENOMEM;
531                 goto error;
532         }
533
534         pn->owner = g_strdup(owner);
535         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
536                                         owner_disconnect, pn, NULL);
537         pn->msg = msg;
538         pn->fd = fd;
539         pn->interface = iface;
540         pn->index = index;
541         pn->server_ip = PRIVATE_NETWORK_IP;
542         pn->peer_ip = PRIVATE_NETWORK_PEER_IP;
543         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
544         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
545
546         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
547                                                 setup_tun_interface, pn);
548
549         g_hash_table_insert(pn_hash, pn->owner, pn);
550
551         return 0;
552
553 error:
554         close(fd);
555         g_free(iface);
556         return err;
557 }
558
559 int __connman_private_network_release(const char *owner)
560 {
561         struct connman_private_network *pn;
562
563         pn = g_hash_table_lookup(pn_hash, owner);
564         if (pn == NULL)
565                 return -EACCES;
566
567         g_hash_table_remove(pn_hash, owner);
568         return 0;
569 }
570
571 int __connman_tethering_init(void)
572 {
573         DBG("");
574
575         tethering_enabled = 0;
576
577         connection = connman_dbus_get_connection();
578         if (connection == NULL)
579                 return -EFAULT;
580
581         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
582                                                 NULL, remove_private_network);
583
584         return 0;
585 }
586
587 void __connman_tethering_cleanup(void)
588 {
589         DBG("");
590
591         if (g_atomic_int_get(&tethering_enabled)) {
592                 if (tethering_dhcp_server)
593                         dhcp_server_stop(tethering_dhcp_server);
594                 disable_bridge(BRIDGE_NAME);
595                 remove_bridge(BRIDGE_NAME);
596         }
597
598         if (connection == NULL)
599                 return;
600
601         g_hash_table_destroy(pn_hash);
602         dbus_connection_unref(connection);
603 }