tethering: Use already existing 'tether' bridge 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_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         disable_bridge(BRIDGE_NAME);
394
395         remove_bridge(BRIDGE_NAME);
396
397         DBG("tethering stopped");
398 }
399
400 void __connman_tethering_update_interface(const char *interface)
401 {
402         DBG("interface %s", interface);
403
404         g_free(default_interface);
405
406         if (interface == NULL) {
407                 disable_nat(interface);
408                 default_interface = NULL;
409
410                 return;
411         }
412
413         default_interface = g_strdup(interface);
414
415         __sync_synchronize();
416         if (tethering_enabled == 0)
417                 return;
418
419         enable_nat(interface);
420 }
421
422 static void setup_tun_interface(unsigned int flags, unsigned change,
423                 void *data)
424 {
425         struct connman_private_network *pn = data;
426         unsigned char prefixlen;
427         DBusMessageIter array, dict;
428         int err;
429
430         DBG("index %d flags %d change %d", pn->index,  flags, change);
431
432         if (flags & IFF_UP)
433                 return;
434
435         prefixlen =
436                 __connman_ipconfig_netmask_prefix_len(PRIVATE_NETWORK_NETMASK);
437
438         if ((__connman_inet_modify_address(RTM_NEWADDR,
439                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
440                                 pn->server_ip, pn->peer_ip,
441                                 prefixlen, NULL)) < 0) {
442                 DBG("address setting failed");
443                 return;
444         }
445
446         connman_inet_ifup(pn->index);
447
448         err = enable_nat(default_interface);
449         if (err < 0) {
450                 connman_error("failed to enable NAT on %s", default_interface);
451                 goto error;
452         }
453
454         dbus_message_iter_init_append(pn->reply, &array);
455
456         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
457                                                 &pn->path);
458
459         connman_dbus_dict_open(&array, &dict);
460
461         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
462                                         DBUS_TYPE_STRING, &pn->server_ip);
463         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
464                                         DBUS_TYPE_STRING, &pn->peer_ip);
465         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
466                                         DBUS_TYPE_STRING, &pn->primary_dns);
467         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
468                                         DBUS_TYPE_STRING, &pn->secondary_dns);
469
470         connman_dbus_dict_close(&array, &dict);
471
472         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
473
474         g_dbus_send_message(connection, pn->reply);
475
476         return;
477
478 error:
479         pn->reply = __connman_error_failed(pn->msg, -err);
480         g_dbus_send_message(connection, pn->reply);
481
482         g_hash_table_remove(pn_hash, pn->path);
483 }
484
485 static void remove_private_network(gpointer user_data)
486 {
487         struct connman_private_network *pn = user_data;
488
489         disable_nat(default_interface);
490         connman_rtnl_remove_watch(pn->iface_watch);
491
492         if (pn->watch > 0) {
493                 g_dbus_remove_watch(connection, pn->watch);
494                 pn->watch = 0;
495         }
496
497         close(pn->fd);
498
499         g_free(pn->interface);
500         g_free(pn->owner);
501         g_free(pn->path);
502         g_free(pn);
503 }
504
505 static void owner_disconnect(DBusConnection *connection, void *user_data)
506 {
507         struct connman_private_network *pn = user_data;
508
509         DBG("%s died", pn->owner);
510
511         pn->watch = 0;
512
513         g_hash_table_remove(pn_hash, pn->path);
514 }
515
516 int __connman_private_network_request(DBusMessage *msg, const char *owner)
517 {
518         struct connman_private_network *pn;
519         char *iface = NULL;
520         char *path = NULL;
521         int index, fd, err;
522
523         if (DBUS_TYPE_UNIX_FD < 0)
524                 return -EINVAL;
525
526         fd = connman_inet_create_tunnel(&iface);
527         if (fd < 0)
528                 return fd;
529
530         path = g_strdup_printf("/tethering/%s", iface);
531
532         pn = g_hash_table_lookup(pn_hash, path);
533         if (pn) {
534                 g_free(path);
535                 g_free(iface);
536                 close(fd);
537                 return -EEXIST;
538         }
539
540         index = connman_inet_ifindex(iface);
541         if (index < 0) {
542                 err = -ENODEV;
543                 goto error;
544         }
545         DBG("interface %s", iface);
546
547         err = connman_inet_set_mtu(index, DEFAULT_MTU);
548
549         pn = g_try_new0(struct connman_private_network, 1);
550         if (pn == NULL) {
551                 err = -ENOMEM;
552                 goto error;
553         }
554
555         pn->owner = g_strdup(owner);
556         pn->path = path;
557         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
558                                         owner_disconnect, pn, NULL);
559         pn->msg = msg;
560         pn->reply = dbus_message_new_method_return(pn->msg);
561         if (pn->reply == NULL)
562                 goto error;
563
564         pn->fd = fd;
565         pn->interface = iface;
566         pn->index = index;
567         pn->server_ip = PRIVATE_NETWORK_IP;
568         pn->peer_ip = PRIVATE_NETWORK_PEER_IP;
569         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
570         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
571
572         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
573                                                 setup_tun_interface, pn);
574
575         g_hash_table_insert(pn_hash, pn->path, pn);
576
577         return 0;
578
579 error:
580         close(fd);
581         g_free(iface);
582         g_free(path);
583         g_free(pn);
584         return err;
585 }
586
587 int __connman_private_network_release(const char *path)
588 {
589         struct connman_private_network *pn;
590
591         pn = g_hash_table_lookup(pn_hash, path);
592         if (pn == NULL)
593                 return -EACCES;
594
595         g_hash_table_remove(pn_hash, path);
596         return 0;
597 }
598
599 int __connman_tethering_init(void)
600 {
601         DBG("");
602
603         tethering_enabled = 0;
604
605         connection = connman_dbus_get_connection();
606         if (connection == NULL)
607                 return -EFAULT;
608
609         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
610                                                 NULL, remove_private_network);
611
612         return 0;
613 }
614
615 void __connman_tethering_cleanup(void)
616 {
617         DBG("");
618
619         __sync_synchronize();
620         if (tethering_enabled == 0) {
621                 if (tethering_dhcp_server)
622                         dhcp_server_stop(tethering_dhcp_server);
623                 disable_bridge(BRIDGE_NAME);
624                 remove_bridge(BRIDGE_NAME);
625         }
626
627         if (connection == NULL)
628                 return;
629
630         g_hash_table_destroy(pn_hash);
631         dbus_connection_unref(connection);
632 }