c240302a2a474f9fc1c77ac3011a922f8441044b
[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 enable_ip_forward(connman_bool_t enable)
171 {
172
173         FILE *f;
174
175         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
176         if (f == NULL)
177                 return -errno;
178
179         if (enable == TRUE)
180                 fprintf(f, "1");
181         else
182                 fprintf(f, "0");
183
184         fclose(f);
185
186         return 0;
187 }
188
189 static int enable_nat(const char *interface)
190 {
191         int err;
192
193         if (interface == NULL)
194                 return 0;
195
196         /* Enable IPv4 forwarding */
197         err = enable_ip_forward(TRUE);
198         if (err < 0)
199                 return err;
200
201         /* POSTROUTING flush */
202         err = __connman_iptables_command("-t nat -F POSTROUTING");
203         if (err < 0)
204                 return err;
205
206         /* Enable masquerading */
207         err = __connman_iptables_command("-t nat -A POSTROUTING "
208                                         "-o %s -j MASQUERADE", interface);
209         if (err < 0)
210                 return err;
211
212         return __connman_iptables_commit("nat");
213 }
214
215 static void disable_nat(const char *interface)
216 {
217         int err;
218
219         /* Disable IPv4 forwarding */
220         enable_ip_forward(FALSE);
221
222         /* POSTROUTING flush */
223         err = __connman_iptables_command("-t nat -F POSTROUTING");
224         if (err < 0)
225                 return;
226
227         __connman_iptables_commit("nat");
228 }
229
230 static void tethering_restart(struct connman_ippool *pool, void *user_data)
231 {
232         __connman_tethering_set_disabled();
233         __connman_tethering_set_enabled();
234 }
235
236 void __connman_tethering_set_enabled(void)
237 {
238         int index;
239         int err;
240         const char *gateway;
241         const char *broadcast;
242         const char *subnet_mask;
243         const char *start_ip;
244         const char *end_ip;
245         const char *dns;
246
247         DBG("enabled %d", tethering_enabled + 1);
248
249         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
250                 return;
251
252         err = __connman_bridge_create(BRIDGE_NAME);
253         if (err < 0)
254                 return;
255
256         index = connman_inet_ifindex(BRIDGE_NAME);
257         dhcp_ippool = __connman_ippool_create(index, 1, 253,
258                                                 tethering_restart, NULL);
259         if (dhcp_ippool == NULL) {
260                 connman_error("Fail to create IP pool");
261                 return;
262         }
263
264         gateway = __connman_ippool_get_gateway(dhcp_ippool);
265         broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
266         subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
267         start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
268         end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
269
270         err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
271         if (err < 0 && err != -EALREADY) {
272                 __connman_bridge_remove(BRIDGE_NAME);
273                 return;
274         }
275
276         dns = gateway;
277         if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
278                 connman_error("Can't add listener %s to DNS proxy",
279                                                                 BRIDGE_NAME);
280                 dns = BRIDGE_DNS;
281         }
282
283         tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
284                                                 gateway, subnet_mask,
285                                                 start_ip, end_ip,
286                                                 24 * 3600, dns);
287         if (tethering_dhcp_server == NULL) {
288                 __connman_bridge_disable(BRIDGE_NAME);
289                 __connman_bridge_remove(BRIDGE_NAME);
290                 return;
291         }
292
293         enable_nat(default_interface);
294
295         DBG("tethering started");
296 }
297
298 void __connman_tethering_set_disabled(void)
299 {
300         DBG("enabled %d", tethering_enabled - 1);
301
302         __connman_dnsproxy_remove_listener(BRIDGE_NAME);
303
304         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
305                 return;
306
307         disable_nat(default_interface);
308
309         dhcp_server_stop(tethering_dhcp_server);
310
311         tethering_dhcp_server = NULL;
312
313         __connman_bridge_disable(BRIDGE_NAME);
314
315         __connman_ippool_unref(dhcp_ippool);
316
317         __connman_bridge_remove(BRIDGE_NAME);
318
319         DBG("tethering stopped");
320 }
321
322 void __connman_tethering_update_interface(const char *interface)
323 {
324         DBG("interface %s", interface);
325
326         g_free(default_interface);
327
328         if (interface == NULL) {
329                 disable_nat(interface);
330                 default_interface = NULL;
331
332                 return;
333         }
334
335         default_interface = g_strdup(interface);
336
337         __sync_synchronize();
338         if (tethering_enabled == 0)
339                 return;
340
341         enable_nat(interface);
342 }
343
344 static void setup_tun_interface(unsigned int flags, unsigned change,
345                 void *data)
346 {
347         struct connman_private_network *pn = data;
348         unsigned char prefixlen;
349         DBusMessageIter array, dict;
350         const char *server_ip;
351         const char *peer_ip;
352         const char *subnet_mask;
353         int err;
354
355         DBG("index %d flags %d change %d", pn->index,  flags, change);
356
357         if (flags & IFF_UP)
358                 return;
359
360         subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
361         server_ip = __connman_ippool_get_start_ip(pn->pool);
362         peer_ip = __connman_ippool_get_end_ip(pn->pool);
363         prefixlen =
364                 __connman_ipconfig_netmask_prefix_len(subnet_mask);
365
366         if ((__connman_inet_modify_address(RTM_NEWADDR,
367                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
368                                 server_ip, peer_ip, prefixlen, NULL)) < 0) {
369                 DBG("address setting failed");
370                 return;
371         }
372
373         connman_inet_ifup(pn->index);
374
375         err = enable_nat(default_interface);
376         if (err < 0) {
377                 connman_error("failed to enable NAT on %s", default_interface);
378                 goto error;
379         }
380
381         dbus_message_iter_init_append(pn->reply, &array);
382
383         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
384                                                 &pn->path);
385
386         connman_dbus_dict_open(&array, &dict);
387
388         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
389                                         DBUS_TYPE_STRING, &server_ip);
390         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
391                                         DBUS_TYPE_STRING, &peer_ip);
392         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
393                                         DBUS_TYPE_STRING, &pn->primary_dns);
394         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
395                                         DBUS_TYPE_STRING, &pn->secondary_dns);
396
397         connman_dbus_dict_close(&array, &dict);
398
399         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
400
401         g_dbus_send_message(connection, pn->reply);
402
403         return;
404
405 error:
406         pn->reply = __connman_error_failed(pn->msg, -err);
407         g_dbus_send_message(connection, pn->reply);
408
409         g_hash_table_remove(pn_hash, pn->path);
410 }
411
412 static void remove_private_network(gpointer user_data)
413 {
414         struct connman_private_network *pn = user_data;
415
416         disable_nat(default_interface);
417         connman_rtnl_remove_watch(pn->iface_watch);
418         __connman_ippool_unref(pn->pool);
419
420         if (pn->watch > 0) {
421                 g_dbus_remove_watch(connection, pn->watch);
422                 pn->watch = 0;
423         }
424
425         close(pn->fd);
426
427         g_free(pn->interface);
428         g_free(pn->owner);
429         g_free(pn->path);
430         g_free(pn);
431 }
432
433 static void owner_disconnect(DBusConnection *connection, void *user_data)
434 {
435         struct connman_private_network *pn = user_data;
436
437         DBG("%s died", pn->owner);
438
439         pn->watch = 0;
440
441         g_hash_table_remove(pn_hash, pn->path);
442 }
443
444 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
445 {
446         struct connman_private_network *pn = user_data;
447
448         DBG("block used externally");
449
450         g_hash_table_remove(pn_hash, pn->path);
451 }
452
453 int __connman_private_network_request(DBusMessage *msg, const char *owner)
454 {
455         struct connman_private_network *pn;
456         char *iface = NULL;
457         char *path = NULL;
458         int index, fd, err;
459
460         if (DBUS_TYPE_UNIX_FD < 0)
461                 return -EINVAL;
462
463         fd = connman_inet_create_tunnel(&iface);
464         if (fd < 0)
465                 return fd;
466
467         path = g_strdup_printf("/tethering/%s", iface);
468
469         pn = g_hash_table_lookup(pn_hash, path);
470         if (pn) {
471                 g_free(path);
472                 g_free(iface);
473                 close(fd);
474                 return -EEXIST;
475         }
476
477         index = connman_inet_ifindex(iface);
478         if (index < 0) {
479                 err = -ENODEV;
480                 goto error;
481         }
482         DBG("interface %s", iface);
483
484         err = connman_inet_set_mtu(index, DEFAULT_MTU);
485
486         pn = g_try_new0(struct connman_private_network, 1);
487         if (pn == NULL) {
488                 err = -ENOMEM;
489                 goto error;
490         }
491
492         pn->owner = g_strdup(owner);
493         pn->path = path;
494         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
495                                         owner_disconnect, pn, NULL);
496         pn->msg = msg;
497         pn->reply = dbus_message_new_method_return(pn->msg);
498         if (pn->reply == NULL)
499                 goto error;
500
501         pn->fd = fd;
502         pn->interface = iface;
503         pn->index = index;
504         pn->pool = __connman_ippool_create(pn->fd, 1, 1, ippool_disconnect, pn);
505         if (pn->pool == NULL) {
506                 errno = -ENOMEM;
507                 goto error;
508         }
509
510         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
511         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
512
513         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
514                                                 setup_tun_interface, pn);
515
516         g_hash_table_insert(pn_hash, pn->path, pn);
517
518         return 0;
519
520 error:
521         close(fd);
522         g_free(iface);
523         g_free(path);
524         g_free(pn);
525         return err;
526 }
527
528 int __connman_private_network_release(const char *path)
529 {
530         struct connman_private_network *pn;
531
532         pn = g_hash_table_lookup(pn_hash, path);
533         if (pn == NULL)
534                 return -EACCES;
535
536         g_hash_table_remove(pn_hash, path);
537         return 0;
538 }
539
540 int __connman_tethering_init(void)
541 {
542         DBG("");
543
544         tethering_enabled = 0;
545
546         connection = connman_dbus_get_connection();
547         if (connection == NULL)
548                 return -EFAULT;
549
550         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
551                                                 NULL, remove_private_network);
552
553         return 0;
554 }
555
556 void __connman_tethering_cleanup(void)
557 {
558         DBG("");
559
560         __sync_synchronize();
561         if (tethering_enabled == 0) {
562                 if (tethering_dhcp_server)
563                         dhcp_server_stop(tethering_dhcp_server);
564                 __connman_bridge_disable(BRIDGE_NAME);
565                 __connman_bridge_remove(BRIDGE_NAME);
566         }
567
568         if (connection == NULL)
569                 return;
570
571         g_hash_table_destroy(pn_hash);
572         dbus_connection_unref(connection);
573 }