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