5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2011 ProFUSION embedded systems
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.
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.
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
28 #include <sys/types.h>
32 #include <sys/ioctl.h>
34 #include <linux/sockios.h>
37 #include <linux/if_tun.h>
41 #include <gdhcp/gdhcp.h>
45 #ifndef DBUS_TYPE_UNIX_FD
46 #define DBUS_TYPE_UNIX_FD -1
49 #define BRIDGE_PROC_DIR "/proc/sys/net/bridge"
51 #define BRIDGE_NAME "tether"
52 #define BRIDGE_DNS "8.8.8.8"
54 #define DEFAULT_MTU 1500
56 #define PRIVATE_NETWORK_PRIMARY_DNS BRIDGE_DNS
57 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
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;
66 struct connman_private_network {
76 struct connman_ippool *pool;
77 const char *primary_dns;
78 const char *secondary_dns;
81 const char *__connman_tethering_get_bridge(void)
85 if (stat(BRIDGE_PROC_DIR, &st) < 0) {
86 connman_error("Missing support for 802.1d ethernet bridging");
93 static void dhcp_server_debug(const char *str, void *data)
95 connman_info("%s: %s\n", (const char *) data, str);
98 static void dhcp_server_error(GDHCPServerError error)
101 case G_DHCP_SERVER_ERROR_NONE:
104 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
105 connman_error("Interface unavailable");
107 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
108 connman_error("Interface in use");
110 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
111 connman_error("Interface down");
113 case G_DHCP_SERVER_ERROR_NOMEM:
114 connman_error("No memory");
116 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
117 connman_error("Invalid index");
119 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
120 connman_error("Invalid option");
122 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
123 connman_error("Invalid address");
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)
133 GDHCPServerError error;
134 GDHCPServer *dhcp_server;
139 index = connman_inet_ifindex(bridge);
143 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
144 if (dhcp_server == NULL) {
145 dhcp_server_error(error);
149 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
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);
157 g_dhcp_server_start(dhcp_server);
162 static void dhcp_server_stop(GDHCPServer *server)
167 g_dhcp_server_unref(server);
170 static int set_forward_delay(const char *name, unsigned int delay)
173 char *forward_delay_path;
176 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
178 if (forward_delay_path == NULL)
181 f = fopen(forward_delay_path, "r+");
183 g_free(forward_delay_path);
188 fprintf(f, "%d", delay);
195 static int create_bridge(const char *name)
199 DBG("name %s", name);
201 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
205 if (ioctl(sk, SIOCBRADDBR, name) == -1) {
211 err = set_forward_delay(name, 0);
214 ioctl(sk, SIOCBRDELBR, name);
221 static int remove_bridge(const char *name)
225 DBG("name %s", name);
227 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
231 err = ioctl(sk, SIOCBRDELBR, name);
241 static int enable_bridge(const char *name, const char *broadcast,
246 index = connman_inet_ifindex(name);
250 err = __connman_inet_modify_address(RTM_NEWADDR,
251 NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
252 gateway, NULL, 24, broadcast);
256 return connman_inet_ifup(index);
259 static int disable_bridge(const char *name)
263 index = connman_inet_ifindex(name);
267 return connman_inet_ifdown(index);
270 static int enable_ip_forward(connman_bool_t enable)
275 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
289 static int enable_nat(const char *interface)
293 if (interface == NULL)
296 /* Enable IPv4 forwarding */
297 err = enable_ip_forward(TRUE);
301 /* POSTROUTING flush */
302 err = __connman_iptables_command("-t nat -F POSTROUTING");
306 /* Enable masquerading */
307 err = __connman_iptables_command("-t nat -A POSTROUTING "
308 "-o %s -j MASQUERADE", interface);
312 return __connman_iptables_commit("nat");
315 static void disable_nat(const char *interface)
319 /* Disable IPv4 forwarding */
320 enable_ip_forward(FALSE);
322 /* POSTROUTING flush */
323 err = __connman_iptables_command("-t nat -F POSTROUTING");
327 __connman_iptables_commit("nat");
330 void __connman_tethering_set_enabled(void)
334 const char *broadcast;
335 const char *subnet_mask;
336 const char *start_ip;
340 DBG("enabled %d", tethering_enabled + 1);
342 if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
345 err = create_bridge(BRIDGE_NAME);
349 dhcp_ippool = __connman_ippool_create(1, 253);
350 if (dhcp_ippool == NULL) {
351 connman_error("Fail to create IP pool");
355 gateway = __connman_ippool_get_gateway(dhcp_ippool);
356 broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
357 subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
358 start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
359 end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
361 err = enable_bridge(BRIDGE_NAME, gateway, broadcast);
362 if (err < 0 && err != -EALREADY) {
363 remove_bridge(BRIDGE_NAME);
368 if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
369 connman_error("Can't add listener %s to DNS proxy",
374 tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
375 gateway, subnet_mask,
378 if (tethering_dhcp_server == NULL) {
379 disable_bridge(BRIDGE_NAME);
380 remove_bridge(BRIDGE_NAME);
384 enable_nat(default_interface);
386 DBG("tethering started");
389 void __connman_tethering_set_disabled(void)
391 DBG("enabled %d", tethering_enabled - 1);
393 __connman_dnsproxy_remove_listener(BRIDGE_NAME);
395 if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
398 disable_nat(default_interface);
400 dhcp_server_stop(tethering_dhcp_server);
402 tethering_dhcp_server = NULL;
404 disable_bridge(BRIDGE_NAME);
406 __connman_ippool_unref(dhcp_ippool);
408 remove_bridge(BRIDGE_NAME);
410 DBG("tethering stopped");
413 void __connman_tethering_update_interface(const char *interface)
415 DBG("interface %s", interface);
417 g_free(default_interface);
419 if (interface == NULL) {
420 disable_nat(interface);
421 default_interface = NULL;
426 default_interface = g_strdup(interface);
428 __sync_synchronize();
429 if (tethering_enabled == 0)
432 enable_nat(interface);
435 static void setup_tun_interface(unsigned int flags, unsigned change,
438 struct connman_private_network *pn = data;
439 unsigned char prefixlen;
440 DBusMessageIter array, dict;
441 const char *server_ip;
443 const char *subnet_mask;
446 DBG("index %d flags %d change %d", pn->index, flags, change);
451 subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
452 server_ip = __connman_ippool_get_start_ip(pn->pool);
453 peer_ip = __connman_ippool_get_end_ip(pn->pool);
455 __connman_ipconfig_netmask_prefix_len(subnet_mask);
457 if ((__connman_inet_modify_address(RTM_NEWADDR,
458 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
459 server_ip, peer_ip, prefixlen, NULL)) < 0) {
460 DBG("address setting failed");
464 connman_inet_ifup(pn->index);
466 err = enable_nat(default_interface);
468 connman_error("failed to enable NAT on %s", default_interface);
472 dbus_message_iter_init_append(pn->reply, &array);
474 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
477 connman_dbus_dict_open(&array, &dict);
479 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
480 DBUS_TYPE_STRING, &server_ip);
481 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
482 DBUS_TYPE_STRING, &peer_ip);
483 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
484 DBUS_TYPE_STRING, &pn->primary_dns);
485 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
486 DBUS_TYPE_STRING, &pn->secondary_dns);
488 connman_dbus_dict_close(&array, &dict);
490 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
492 g_dbus_send_message(connection, pn->reply);
497 pn->reply = __connman_error_failed(pn->msg, -err);
498 g_dbus_send_message(connection, pn->reply);
500 g_hash_table_remove(pn_hash, pn->path);
503 static void remove_private_network(gpointer user_data)
505 struct connman_private_network *pn = user_data;
507 disable_nat(default_interface);
508 connman_rtnl_remove_watch(pn->iface_watch);
509 __connman_ippool_unref(pn->pool);
512 g_dbus_remove_watch(connection, pn->watch);
518 g_free(pn->interface);
524 static void owner_disconnect(DBusConnection *connection, void *user_data)
526 struct connman_private_network *pn = user_data;
528 DBG("%s died", pn->owner);
532 g_hash_table_remove(pn_hash, pn->path);
535 int __connman_private_network_request(DBusMessage *msg, const char *owner)
537 struct connman_private_network *pn;
542 if (DBUS_TYPE_UNIX_FD < 0)
545 fd = connman_inet_create_tunnel(&iface);
549 path = g_strdup_printf("/tethering/%s", iface);
551 pn = g_hash_table_lookup(pn_hash, path);
559 index = connman_inet_ifindex(iface);
564 DBG("interface %s", iface);
566 err = connman_inet_set_mtu(index, DEFAULT_MTU);
568 pn = g_try_new0(struct connman_private_network, 1);
574 pn->owner = g_strdup(owner);
576 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
577 owner_disconnect, pn, NULL);
579 pn->reply = dbus_message_new_method_return(pn->msg);
580 if (pn->reply == NULL)
584 pn->interface = iface;
586 pn->pool = __connman_ippool_create(1, 1);
587 if (pn->pool == NULL) {
592 pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
593 pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
595 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
596 setup_tun_interface, pn);
598 g_hash_table_insert(pn_hash, pn->path, pn);
610 int __connman_private_network_release(const char *path)
612 struct connman_private_network *pn;
614 pn = g_hash_table_lookup(pn_hash, path);
618 g_hash_table_remove(pn_hash, path);
622 int __connman_tethering_init(void)
626 tethering_enabled = 0;
628 connection = connman_dbus_get_connection();
629 if (connection == NULL)
632 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
633 NULL, remove_private_network);
638 void __connman_tethering_cleanup(void)
642 __sync_synchronize();
643 if (tethering_enabled == 0) {
644 if (tethering_dhcp_server)
645 dhcp_server_stop(tethering_dhcp_server);
646 disable_bridge(BRIDGE_NAME);
647 remove_bridge(BRIDGE_NAME);
650 if (connection == NULL)
653 g_hash_table_destroy(pn_hash);
654 dbus_connection_unref(connection);