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