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