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