tethering: Add notification callback implementation
[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 set_forward_delay(const char *name, unsigned int delay)
171 {
172         FILE *f;
173         char *forward_delay_path;
174
175         forward_delay_path =
176                 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
177
178         if (forward_delay_path == NULL)
179                 return -ENOMEM;
180
181         f = fopen(forward_delay_path, "r+");
182
183         g_free(forward_delay_path);
184
185         if (f == NULL)
186                 return -errno;
187
188         fprintf(f, "%d", delay);
189
190         fclose(f);
191
192         return 0;
193 }
194
195 static int create_bridge(const char *name)
196 {
197         int sk, err;
198
199         DBG("name %s", name);
200
201         sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
202         if (sk < 0)
203                 return -EOPNOTSUPP;
204
205         if (ioctl(sk, SIOCBRADDBR, name) == -1) {
206                 err = -errno;
207                 if (err != -EEXIST)
208                         return -EOPNOTSUPP;
209         }
210
211         err = set_forward_delay(name, 0);
212
213         if (err < 0)
214                 ioctl(sk, SIOCBRDELBR, name);
215
216         close(sk);
217
218         return err;
219 }
220
221 static int remove_bridge(const char *name)
222 {
223         int sk, err;
224
225         DBG("name %s", name);
226
227         sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
228         if (sk < 0)
229                 return -EOPNOTSUPP;
230
231         err = ioctl(sk, SIOCBRDELBR, name);
232
233         close(sk);
234
235         if (err < 0)
236                 return -EOPNOTSUPP;
237
238         return 0;
239 }
240
241 static int enable_bridge(const char *name, const char *broadcast,
242                                 const char *gateway)
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                                         gateway, NULL, 24, broadcast);
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 static void tethering_restart(struct connman_ippool *pool, void *user_data)
331 {
332         __connman_tethering_set_disabled();
333         __connman_tethering_set_enabled();
334 }
335
336 void __connman_tethering_set_enabled(void)
337 {
338         int index;
339         int err;
340         const char *gateway;
341         const char *broadcast;
342         const char *subnet_mask;
343         const char *start_ip;
344         const char *end_ip;
345         const char *dns;
346
347         DBG("enabled %d", tethering_enabled + 1);
348
349         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
350                 return;
351
352         err = create_bridge(BRIDGE_NAME);
353         if (err < 0)
354                 return;
355
356         index = connman_inet_ifindex(BRIDGE_NAME);
357         dhcp_ippool = __connman_ippool_create(index, 1, 253,
358                                                 tethering_restart, NULL);
359         if (dhcp_ippool == NULL) {
360                 connman_error("Fail to create IP pool");
361                 return;
362         }
363
364         gateway = __connman_ippool_get_gateway(dhcp_ippool);
365         broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
366         subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
367         start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
368         end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
369
370         err = enable_bridge(BRIDGE_NAME, gateway, broadcast);
371         if (err < 0 && err != -EALREADY) {
372                 remove_bridge(BRIDGE_NAME);
373                 return;
374         }
375
376         dns = gateway;
377         if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
378                 connman_error("Can't add listener %s to DNS proxy",
379                                                                 BRIDGE_NAME);
380                 dns = BRIDGE_DNS;
381         }
382
383         tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
384                                                 gateway, subnet_mask,
385                                                 start_ip, end_ip,
386                                                 24 * 3600, dns);
387         if (tethering_dhcp_server == NULL) {
388                 disable_bridge(BRIDGE_NAME);
389                 remove_bridge(BRIDGE_NAME);
390                 return;
391         }
392
393         enable_nat(default_interface);
394
395         DBG("tethering started");
396 }
397
398 void __connman_tethering_set_disabled(void)
399 {
400         DBG("enabled %d", tethering_enabled - 1);
401
402         __connman_dnsproxy_remove_listener(BRIDGE_NAME);
403
404         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
405                 return;
406
407         disable_nat(default_interface);
408
409         dhcp_server_stop(tethering_dhcp_server);
410
411         tethering_dhcp_server = NULL;
412
413         disable_bridge(BRIDGE_NAME);
414
415         __connman_ippool_unref(dhcp_ippool);
416
417         remove_bridge(BRIDGE_NAME);
418
419         DBG("tethering stopped");
420 }
421
422 void __connman_tethering_update_interface(const char *interface)
423 {
424         DBG("interface %s", interface);
425
426         g_free(default_interface);
427
428         if (interface == NULL) {
429                 disable_nat(interface);
430                 default_interface = NULL;
431
432                 return;
433         }
434
435         default_interface = g_strdup(interface);
436
437         __sync_synchronize();
438         if (tethering_enabled == 0)
439                 return;
440
441         enable_nat(interface);
442 }
443
444 static void setup_tun_interface(unsigned int flags, unsigned change,
445                 void *data)
446 {
447         struct connman_private_network *pn = data;
448         unsigned char prefixlen;
449         DBusMessageIter array, dict;
450         const char *server_ip;
451         const char *peer_ip;
452         const char *subnet_mask;
453         int err;
454
455         DBG("index %d flags %d change %d", pn->index,  flags, change);
456
457         if (flags & IFF_UP)
458                 return;
459
460         subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
461         server_ip = __connman_ippool_get_start_ip(pn->pool);
462         peer_ip = __connman_ippool_get_end_ip(pn->pool);
463         prefixlen =
464                 __connman_ipconfig_netmask_prefix_len(subnet_mask);
465
466         if ((__connman_inet_modify_address(RTM_NEWADDR,
467                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
468                                 server_ip, peer_ip, prefixlen, NULL)) < 0) {
469                 DBG("address setting failed");
470                 return;
471         }
472
473         connman_inet_ifup(pn->index);
474
475         err = enable_nat(default_interface);
476         if (err < 0) {
477                 connman_error("failed to enable NAT on %s", default_interface);
478                 goto error;
479         }
480
481         dbus_message_iter_init_append(pn->reply, &array);
482
483         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
484                                                 &pn->path);
485
486         connman_dbus_dict_open(&array, &dict);
487
488         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
489                                         DBUS_TYPE_STRING, &server_ip);
490         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
491                                         DBUS_TYPE_STRING, &peer_ip);
492         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
493                                         DBUS_TYPE_STRING, &pn->primary_dns);
494         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
495                                         DBUS_TYPE_STRING, &pn->secondary_dns);
496
497         connman_dbus_dict_close(&array, &dict);
498
499         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
500
501         g_dbus_send_message(connection, pn->reply);
502
503         return;
504
505 error:
506         pn->reply = __connman_error_failed(pn->msg, -err);
507         g_dbus_send_message(connection, pn->reply);
508
509         g_hash_table_remove(pn_hash, pn->path);
510 }
511
512 static void remove_private_network(gpointer user_data)
513 {
514         struct connman_private_network *pn = user_data;
515
516         disable_nat(default_interface);
517         connman_rtnl_remove_watch(pn->iface_watch);
518         __connman_ippool_unref(pn->pool);
519
520         if (pn->watch > 0) {
521                 g_dbus_remove_watch(connection, pn->watch);
522                 pn->watch = 0;
523         }
524
525         close(pn->fd);
526
527         g_free(pn->interface);
528         g_free(pn->owner);
529         g_free(pn->path);
530         g_free(pn);
531 }
532
533 static void owner_disconnect(DBusConnection *connection, void *user_data)
534 {
535         struct connman_private_network *pn = user_data;
536
537         DBG("%s died", pn->owner);
538
539         pn->watch = 0;
540
541         g_hash_table_remove(pn_hash, pn->path);
542 }
543
544 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
545 {
546         struct connman_private_network *pn = user_data;
547
548         DBG("block used externally");
549
550         g_hash_table_remove(pn_hash, pn->path);
551 }
552
553 int __connman_private_network_request(DBusMessage *msg, const char *owner)
554 {
555         struct connman_private_network *pn;
556         char *iface = NULL;
557         char *path = NULL;
558         int index, fd, err;
559
560         if (DBUS_TYPE_UNIX_FD < 0)
561                 return -EINVAL;
562
563         fd = connman_inet_create_tunnel(&iface);
564         if (fd < 0)
565                 return fd;
566
567         path = g_strdup_printf("/tethering/%s", iface);
568
569         pn = g_hash_table_lookup(pn_hash, path);
570         if (pn) {
571                 g_free(path);
572                 g_free(iface);
573                 close(fd);
574                 return -EEXIST;
575         }
576
577         index = connman_inet_ifindex(iface);
578         if (index < 0) {
579                 err = -ENODEV;
580                 goto error;
581         }
582         DBG("interface %s", iface);
583
584         err = connman_inet_set_mtu(index, DEFAULT_MTU);
585
586         pn = g_try_new0(struct connman_private_network, 1);
587         if (pn == NULL) {
588                 err = -ENOMEM;
589                 goto error;
590         }
591
592         pn->owner = g_strdup(owner);
593         pn->path = path;
594         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
595                                         owner_disconnect, pn, NULL);
596         pn->msg = msg;
597         pn->reply = dbus_message_new_method_return(pn->msg);
598         if (pn->reply == NULL)
599                 goto error;
600
601         pn->fd = fd;
602         pn->interface = iface;
603         pn->index = index;
604         pn->pool = __connman_ippool_create(pn->fd, 1, 1, ippool_disconnect, pn);
605         if (pn->pool == NULL) {
606                 errno = -ENOMEM;
607                 goto error;
608         }
609
610         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
611         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
612
613         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
614                                                 setup_tun_interface, pn);
615
616         g_hash_table_insert(pn_hash, pn->path, pn);
617
618         return 0;
619
620 error:
621         close(fd);
622         g_free(iface);
623         g_free(path);
624         g_free(pn);
625         return err;
626 }
627
628 int __connman_private_network_release(const char *path)
629 {
630         struct connman_private_network *pn;
631
632         pn = g_hash_table_lookup(pn_hash, path);
633         if (pn == NULL)
634                 return -EACCES;
635
636         g_hash_table_remove(pn_hash, path);
637         return 0;
638 }
639
640 int __connman_tethering_init(void)
641 {
642         DBG("");
643
644         tethering_enabled = 0;
645
646         connection = connman_dbus_get_connection();
647         if (connection == NULL)
648                 return -EFAULT;
649
650         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
651                                                 NULL, remove_private_network);
652
653         return 0;
654 }
655
656 void __connman_tethering_cleanup(void)
657 {
658         DBG("");
659
660         __sync_synchronize();
661         if (tethering_enabled == 0) {
662                 if (tethering_dhcp_server)
663                         dhcp_server_stop(tethering_dhcp_server);
664                 disable_bridge(BRIDGE_NAME);
665                 remove_bridge(BRIDGE_NAME);
666         }
667
668         if (connection == NULL)
669                 return;
670
671         g_hash_table_destroy(pn_hash);
672         dbus_connection_unref(connection);
673 }