tethering: Update APIs to be able to use multiple private networks
[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         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, 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, 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
340         DBG("enabled %d", tethering_enabled + 1);
341
342         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
343                 const char *dns;
344
345                 err = create_bridge(BRIDGE_NAME);
346                 if (err < 0)
347                         return;
348
349                 err = enable_bridge(BRIDGE_NAME);
350                 if (err < 0) {
351                         remove_bridge(BRIDGE_NAME);
352                         return;
353                 }
354
355                 dns = BRIDGE_IP;
356                 if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
357                         connman_error("Can't add listener %s to DNS proxy",
358                                                                 BRIDGE_NAME);
359                         dns = BRIDGE_DNS;
360                 }
361
362                 tethering_dhcp_server =
363                         dhcp_server_start(BRIDGE_NAME,
364                                                 BRIDGE_IP, BRIDGE_SUBNET,
365                                                 BRIDGE_IP_START, BRIDGE_IP_END,
366                                                         24 * 3600, dns);
367                 if (tethering_dhcp_server == NULL) {
368                         disable_bridge(BRIDGE_NAME);
369                         remove_bridge(BRIDGE_NAME);
370                         return;
371                 }
372
373                 enable_nat(default_interface);
374
375                 DBG("tethering started");
376         }
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 (g_atomic_int_dec_and_test(&tethering_enabled) == TRUE) {
386                 disable_nat(default_interface);
387
388                 dhcp_server_stop(tethering_dhcp_server);
389
390                 disable_bridge(BRIDGE_NAME);
391
392                 remove_bridge(BRIDGE_NAME);
393
394                 DBG("tethering stopped");
395         }
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         if (!g_atomic_int_get(&tethering_enabled))
414                 return;
415
416         enable_nat(interface);
417 }
418
419 static void setup_tun_interface(unsigned int flags, unsigned change,
420                 void *data)
421 {
422         struct connman_private_network *pn = data;
423         unsigned char prefixlen;
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         dbus_message_iter_init_append(pn->reply, &array);
452
453         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
454                                                 &pn->path);
455
456         connman_dbus_dict_open(&array, &dict);
457
458         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
459                                         DBUS_TYPE_STRING, &pn->server_ip);
460         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
461                                         DBUS_TYPE_STRING, &pn->peer_ip);
462         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
463                                         DBUS_TYPE_STRING, &pn->primary_dns);
464         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
465                                         DBUS_TYPE_STRING, &pn->secondary_dns);
466
467         connman_dbus_dict_close(&array, &dict);
468
469         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
470
471         g_dbus_send_message(connection, pn->reply);
472
473         return;
474
475 error:
476         pn->reply = __connman_error_failed(pn->msg, -err);
477         g_dbus_send_message(connection, pn->reply);
478
479         g_hash_table_remove(pn_hash, pn->path);
480 }
481
482 static void remove_private_network(gpointer user_data)
483 {
484         struct connman_private_network *pn = user_data;
485
486         disable_nat(default_interface);
487         connman_rtnl_remove_watch(pn->iface_watch);
488
489         if (pn->watch > 0) {
490                 g_dbus_remove_watch(connection, pn->watch);
491                 pn->watch = 0;
492         }
493
494         close(pn->fd);
495
496         g_free(pn->interface);
497         g_free(pn->owner);
498         g_free(pn->path);
499         g_free(pn);
500 }
501
502 static void owner_disconnect(DBusConnection *connection, void *user_data)
503 {
504         struct connman_private_network *pn = user_data;
505
506         DBG("%s died", pn->owner);
507
508         pn->watch = 0;
509
510         g_hash_table_remove(pn_hash, pn->path);
511 }
512
513 int __connman_private_network_request(DBusMessage *msg, const char *owner)
514 {
515         struct connman_private_network *pn;
516         char *iface = NULL;
517         char *path = NULL;
518         int index, fd, err;
519
520         if (DBUS_TYPE_UNIX_FD < 0)
521                 return -EINVAL;
522
523         fd = connman_inet_create_tunnel(&iface);
524         if (fd < 0)
525                 return fd;
526
527         path = g_strdup_printf("/tethering/%s", iface);
528
529         pn = g_hash_table_lookup(pn_hash, path);
530         if (pn) {
531                 g_free(path);
532                 g_free(iface);
533                 close(fd);
534                 return -EEXIST;
535         }
536
537         index = connman_inet_ifindex(iface);
538         if (index < 0) {
539                 err = -ENODEV;
540                 goto error;
541         }
542         DBG("interface %s", iface);
543
544         err = connman_inet_set_mtu(index, DEFAULT_MTU);
545
546         pn = g_try_new0(struct connman_private_network, 1);
547         if (pn == NULL) {
548                 err = -ENOMEM;
549                 goto error;
550         }
551
552         pn->owner = g_strdup(owner);
553         pn->path = path;
554         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
555                                         owner_disconnect, pn, NULL);
556         pn->msg = msg;
557         pn->reply = dbus_message_new_method_return(pn->msg);
558         if (pn->reply == NULL)
559                 goto error;
560
561         pn->fd = fd;
562         pn->interface = iface;
563         pn->index = index;
564         pn->server_ip = PRIVATE_NETWORK_IP;
565         pn->peer_ip = PRIVATE_NETWORK_PEER_IP;
566         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
567         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
568
569         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
570                                                 setup_tun_interface, pn);
571
572         g_hash_table_insert(pn_hash, pn->path, pn);
573
574         return 0;
575
576 error:
577         close(fd);
578         g_free(iface);
579         g_free(path);
580         g_free(pn);
581         return err;
582 }
583
584 int __connman_private_network_release(const char *path)
585 {
586         struct connman_private_network *pn;
587
588         pn = g_hash_table_lookup(pn_hash, path);
589         if (pn == NULL)
590                 return -EACCES;
591
592         g_hash_table_remove(pn_hash, path);
593         return 0;
594 }
595
596 int __connman_tethering_init(void)
597 {
598         DBG("");
599
600         tethering_enabled = 0;
601
602         connection = connman_dbus_get_connection();
603         if (connection == NULL)
604                 return -EFAULT;
605
606         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
607                                                 NULL, remove_private_network);
608
609         return 0;
610 }
611
612 void __connman_tethering_cleanup(void)
613 {
614         DBG("");
615
616         if (g_atomic_int_get(&tethering_enabled)) {
617                 if (tethering_dhcp_server)
618                         dhcp_server_stop(tethering_dhcp_server);
619                 disable_bridge(BRIDGE_NAME);
620                 remove_bridge(BRIDGE_NAME);
621         }
622
623         if (connection == NULL)
624                 return;
625
626         g_hash_table_destroy(pn_hash);
627         dbus_connection_unref(connection);
628 }