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