tethering: Use IP pool API with private network
[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_PRIMARY_DNS BRIDGE_DNS
62 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
63
64 static char *default_interface = NULL;
65 static volatile int tethering_enabled;
66 static GDHCPServer *tethering_dhcp_server = NULL;
67 static DBusConnection *connection;
68 static GHashTable *pn_hash;
69
70 struct connman_private_network {
71         char *owner;
72         char *path;
73         guint watch;
74         DBusMessage *msg;
75         DBusMessage *reply;
76         int fd;
77         char *interface;
78         int index;
79         guint iface_watch;
80         struct connman_ippool *pool;
81         const char *primary_dns;
82         const char *secondary_dns;
83 };
84
85 const char *__connman_tethering_get_bridge(void)
86 {
87         struct stat st;
88
89         if (stat(BRIDGE_PROC_DIR, &st) < 0) {
90                 connman_error("Missing support for 802.1d ethernet bridging");
91                 return NULL;
92         }
93
94         return BRIDGE_NAME;
95 }
96
97 static void dhcp_server_debug(const char *str, void *data)
98 {
99         connman_info("%s: %s\n", (const char *) data, str);
100 }
101
102 static void dhcp_server_error(GDHCPServerError error)
103 {
104         switch (error) {
105         case G_DHCP_SERVER_ERROR_NONE:
106                 connman_error("OK");
107                 break;
108         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
109                 connman_error("Interface unavailable");
110                 break;
111         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
112                 connman_error("Interface in use");
113                 break;
114         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
115                 connman_error("Interface down");
116                 break;
117         case G_DHCP_SERVER_ERROR_NOMEM:
118                 connman_error("No memory");
119                 break;
120         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
121                 connman_error("Invalid index");
122                 break;
123         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
124                 connman_error("Invalid option");
125                 break;
126         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
127                 connman_error("Invalid address");
128                 break;
129         }
130 }
131
132 static GDHCPServer *dhcp_server_start(const char *bridge,
133                                 const char *router, const char* subnet,
134                                 const char *start_ip, const char *end_ip,
135                                 unsigned int lease_time, const char *dns)
136 {
137         GDHCPServerError error;
138         GDHCPServer *dhcp_server;
139         int index;
140
141         DBG("");
142
143         index = connman_inet_ifindex(bridge);
144         if (index < 0)
145                 return NULL;
146
147         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
148         if (dhcp_server == NULL) {
149                 dhcp_server_error(error);
150                 return NULL;
151         }
152
153         g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
154
155         g_dhcp_server_set_lease_time(dhcp_server, lease_time);
156         g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
157         g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
158         g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
159         g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
160
161         g_dhcp_server_start(dhcp_server);
162
163         return dhcp_server;
164 }
165
166 static void dhcp_server_stop(GDHCPServer *server)
167 {
168         if (server == NULL)
169                 return;
170
171         g_dhcp_server_unref(server);
172 }
173
174 static int set_forward_delay(const char *name, unsigned int delay)
175 {
176         FILE *f;
177         char *forward_delay_path;
178
179         forward_delay_path =
180                 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
181
182         if (forward_delay_path == NULL)
183                 return -ENOMEM;
184
185         f = fopen(forward_delay_path, "r+");
186
187         g_free(forward_delay_path);
188
189         if (f == NULL)
190                 return -errno;
191
192         fprintf(f, "%d", delay);
193
194         fclose(f);
195
196         return 0;
197 }
198
199 static int create_bridge(const char *name)
200 {
201         int sk, err;
202
203         DBG("name %s", name);
204
205         sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
206         if (sk < 0)
207                 return -EOPNOTSUPP;
208
209         if (ioctl(sk, SIOCBRADDBR, name) == -1) {
210                 err = -errno;
211                 if (err != -EEXIST)
212                         return -EOPNOTSUPP;
213         }
214
215         err = set_forward_delay(name, 0);
216
217         if (err < 0)
218                 ioctl(sk, SIOCBRDELBR, name);
219
220         close(sk);
221
222         return err;
223 }
224
225 static int remove_bridge(const char *name)
226 {
227         int sk, err;
228
229         DBG("name %s", name);
230
231         sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
232         if (sk < 0)
233                 return -EOPNOTSUPP;
234
235         err = ioctl(sk, SIOCBRDELBR, name);
236
237         close(sk);
238
239         if (err < 0)
240                 return -EOPNOTSUPP;
241
242         return 0;
243 }
244
245 static int enable_bridge(const char *name)
246 {
247         int err, index;
248
249         index = connman_inet_ifindex(name);
250         if (index < 0)
251                 return index;
252
253         err = __connman_inet_modify_address(RTM_NEWADDR,
254                         NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
255                                         BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
256         if (err < 0)
257                 return err;
258
259         return connman_inet_ifup(index);
260 }
261
262 static int disable_bridge(const char *name)
263 {
264         int index;
265
266         index = connman_inet_ifindex(name);
267         if (index < 0)
268                 return index;
269
270         return connman_inet_ifdown(index);
271 }
272
273 static int enable_ip_forward(connman_bool_t enable)
274 {
275
276         FILE *f;
277
278         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
279         if (f == NULL)
280                 return -errno;
281
282         if (enable == TRUE)
283                 fprintf(f, "1");
284         else
285                 fprintf(f, "0");
286
287         fclose(f);
288
289         return 0;
290 }
291
292 static int enable_nat(const char *interface)
293 {
294         int err;
295
296         if (interface == NULL)
297                 return 0;
298
299         /* Enable IPv4 forwarding */
300         err = enable_ip_forward(TRUE);
301         if (err < 0)
302                 return err;
303
304         /* POSTROUTING flush */
305         err = __connman_iptables_command("-t nat -F POSTROUTING");
306         if (err < 0)
307                 return err;
308
309         /* Enable masquerading */
310         err = __connman_iptables_command("-t nat -A POSTROUTING "
311                                         "-o %s -j MASQUERADE", interface);
312         if (err < 0)
313                 return err;
314
315         return __connman_iptables_commit("nat");
316 }
317
318 static void disable_nat(const char *interface)
319 {
320         int err;
321
322         /* Disable IPv4 forwarding */
323         enable_ip_forward(FALSE);
324
325         /* POSTROUTING flush */
326         err = __connman_iptables_command("-t nat -F POSTROUTING");
327         if (err < 0)
328                 return;
329
330         __connman_iptables_commit("nat");
331 }
332
333 void __connman_tethering_set_enabled(void)
334 {
335         int err;
336         const char *dns;
337
338         DBG("enabled %d", tethering_enabled + 1);
339
340         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
341                 return;
342
343         err = create_bridge(BRIDGE_NAME);
344         if (err < 0)
345                 return;
346
347         err = enable_bridge(BRIDGE_NAME);
348         if (err < 0 && err != -EALREADY) {
349                 remove_bridge(BRIDGE_NAME);
350                 return;
351         }
352
353         dns = BRIDGE_IP;
354         if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
355                 connman_error("Can't add listener %s to DNS proxy",
356                                                                 BRIDGE_NAME);
357                 dns = BRIDGE_DNS;
358         }
359
360         tethering_dhcp_server =
361                 dhcp_server_start(BRIDGE_NAME,
362                                         BRIDGE_IP, BRIDGE_SUBNET,
363                                         BRIDGE_IP_START, BRIDGE_IP_END,
364                                         24 * 3600, dns);
365         if (tethering_dhcp_server == NULL) {
366                 disable_bridge(BRIDGE_NAME);
367                 remove_bridge(BRIDGE_NAME);
368                 return;
369         }
370
371         enable_nat(default_interface);
372
373         DBG("tethering started");
374 }
375
376 void __connman_tethering_set_disabled(void)
377 {
378         DBG("enabled %d", tethering_enabled - 1);
379
380         __connman_dnsproxy_remove_listener(BRIDGE_NAME);
381
382         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
383                 return;
384
385         disable_nat(default_interface);
386
387         dhcp_server_stop(tethering_dhcp_server);
388
389         tethering_dhcp_server = NULL;
390
391         disable_bridge(BRIDGE_NAME);
392
393         remove_bridge(BRIDGE_NAME);
394
395         DBG("tethering stopped");
396 }
397
398 void __connman_tethering_update_interface(const char *interface)
399 {
400         DBG("interface %s", interface);
401
402         g_free(default_interface);
403
404         if (interface == NULL) {
405                 disable_nat(interface);
406                 default_interface = NULL;
407
408                 return;
409         }
410
411         default_interface = g_strdup(interface);
412
413         __sync_synchronize();
414         if (tethering_enabled == 0)
415                 return;
416
417         enable_nat(interface);
418 }
419
420 static void setup_tun_interface(unsigned int flags, unsigned change,
421                 void *data)
422 {
423         struct connman_private_network *pn = data;
424         unsigned char prefixlen;
425         DBusMessageIter array, dict;
426         const char *server_ip;
427         const char *peer_ip;
428         const char *subnet_mask;
429         int err;
430
431         DBG("index %d flags %d change %d", pn->index,  flags, change);
432
433         if (flags & IFF_UP)
434                 return;
435
436         subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
437         server_ip = __connman_ippool_get_start_ip(pn->pool);
438         peer_ip = __connman_ippool_get_end_ip(pn->pool);
439         prefixlen =
440                 __connman_ipconfig_netmask_prefix_len(subnet_mask);
441
442         if ((__connman_inet_modify_address(RTM_NEWADDR,
443                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
444                                 server_ip, peer_ip, prefixlen, NULL)) < 0) {
445                 DBG("address setting failed");
446                 return;
447         }
448
449         connman_inet_ifup(pn->index);
450
451         err = enable_nat(default_interface);
452         if (err < 0) {
453                 connman_error("failed to enable NAT on %s", default_interface);
454                 goto error;
455         }
456
457         dbus_message_iter_init_append(pn->reply, &array);
458
459         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
460                                                 &pn->path);
461
462         connman_dbus_dict_open(&array, &dict);
463
464         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
465                                         DBUS_TYPE_STRING, &server_ip);
466         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
467                                         DBUS_TYPE_STRING, &peer_ip);
468         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
469                                         DBUS_TYPE_STRING, &pn->primary_dns);
470         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
471                                         DBUS_TYPE_STRING, &pn->secondary_dns);
472
473         connman_dbus_dict_close(&array, &dict);
474
475         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
476
477         g_dbus_send_message(connection, pn->reply);
478
479         return;
480
481 error:
482         pn->reply = __connman_error_failed(pn->msg, -err);
483         g_dbus_send_message(connection, pn->reply);
484
485         g_hash_table_remove(pn_hash, pn->path);
486 }
487
488 static void remove_private_network(gpointer user_data)
489 {
490         struct connman_private_network *pn = user_data;
491
492         disable_nat(default_interface);
493         connman_rtnl_remove_watch(pn->iface_watch);
494         __connman_ippool_unref(pn->pool);
495
496         if (pn->watch > 0) {
497                 g_dbus_remove_watch(connection, pn->watch);
498                 pn->watch = 0;
499         }
500
501         close(pn->fd);
502
503         g_free(pn->interface);
504         g_free(pn->owner);
505         g_free(pn->path);
506         g_free(pn);
507 }
508
509 static void owner_disconnect(DBusConnection *connection, void *user_data)
510 {
511         struct connman_private_network *pn = user_data;
512
513         DBG("%s died", pn->owner);
514
515         pn->watch = 0;
516
517         g_hash_table_remove(pn_hash, pn->path);
518 }
519
520 int __connman_private_network_request(DBusMessage *msg, const char *owner)
521 {
522         struct connman_private_network *pn;
523         char *iface = NULL;
524         char *path = NULL;
525         int index, fd, err;
526
527         if (DBUS_TYPE_UNIX_FD < 0)
528                 return -EINVAL;
529
530         fd = connman_inet_create_tunnel(&iface);
531         if (fd < 0)
532                 return fd;
533
534         path = g_strdup_printf("/tethering/%s", iface);
535
536         pn = g_hash_table_lookup(pn_hash, path);
537         if (pn) {
538                 g_free(path);
539                 g_free(iface);
540                 close(fd);
541                 return -EEXIST;
542         }
543
544         index = connman_inet_ifindex(iface);
545         if (index < 0) {
546                 err = -ENODEV;
547                 goto error;
548         }
549         DBG("interface %s", iface);
550
551         err = connman_inet_set_mtu(index, DEFAULT_MTU);
552
553         pn = g_try_new0(struct connman_private_network, 1);
554         if (pn == NULL) {
555                 err = -ENOMEM;
556                 goto error;
557         }
558
559         pn->owner = g_strdup(owner);
560         pn->path = path;
561         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
562                                         owner_disconnect, pn, NULL);
563         pn->msg = msg;
564         pn->reply = dbus_message_new_method_return(pn->msg);
565         if (pn->reply == NULL)
566                 goto error;
567
568         pn->fd = fd;
569         pn->interface = iface;
570         pn->index = index;
571         pn->pool = __connman_ippool_create(1, 1);
572         if (pn->pool == NULL) {
573                 errno = -ENOMEM;
574                 goto error;
575         }
576
577         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
578         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
579
580         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
581                                                 setup_tun_interface, pn);
582
583         g_hash_table_insert(pn_hash, pn->path, pn);
584
585         return 0;
586
587 error:
588         close(fd);
589         g_free(iface);
590         g_free(path);
591         g_free(pn);
592         return err;
593 }
594
595 int __connman_private_network_release(const char *path)
596 {
597         struct connman_private_network *pn;
598
599         pn = g_hash_table_lookup(pn_hash, path);
600         if (pn == NULL)
601                 return -EACCES;
602
603         g_hash_table_remove(pn_hash, path);
604         return 0;
605 }
606
607 int __connman_tethering_init(void)
608 {
609         DBG("");
610
611         tethering_enabled = 0;
612
613         connection = connman_dbus_get_connection();
614         if (connection == NULL)
615                 return -EFAULT;
616
617         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
618                                                 NULL, remove_private_network);
619
620         return 0;
621 }
622
623 void __connman_tethering_cleanup(void)
624 {
625         DBG("");
626
627         __sync_synchronize();
628         if (tethering_enabled == 0) {
629                 if (tethering_dhcp_server)
630                         dhcp_server_stop(tethering_dhcp_server);
631                 disable_bridge(BRIDGE_NAME);
632                 remove_bridge(BRIDGE_NAME);
633         }
634
635         if (connection == NULL)
636                 return;
637
638         g_hash_table_destroy(pn_hash);
639         dbus_connection_unref(connection);
640 }