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_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"
59 #define DEFAULT_MTU 1500
61 #define PRIVATE_NETWORK_PRIMARY_DNS BRIDGE_DNS
62 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
64 static char *default_interface = NULL;
65 static volatile int tethering_enabled;
66 static GDHCPServer *tethering_dhcp_server = NULL;
67 static DBusConnection *connection;
68 static GHashTable *pn_hash;
70 struct connman_private_network {
80 struct connman_ippool *pool;
81 const char *primary_dns;
82 const char *secondary_dns;
85 const char *__connman_tethering_get_bridge(void)
89 if (stat(BRIDGE_PROC_DIR, &st) < 0) {
90 connman_error("Missing support for 802.1d ethernet bridging");
97 static void dhcp_server_debug(const char *str, void *data)
99 connman_info("%s: %s\n", (const char *) data, str);
102 static void dhcp_server_error(GDHCPServerError error)
105 case G_DHCP_SERVER_ERROR_NONE:
108 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
109 connman_error("Interface unavailable");
111 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
112 connman_error("Interface in use");
114 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
115 connman_error("Interface down");
117 case G_DHCP_SERVER_ERROR_NOMEM:
118 connman_error("No memory");
120 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
121 connman_error("Invalid index");
123 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
124 connman_error("Invalid option");
126 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
127 connman_error("Invalid address");
132 static GDHCPServer *dhcp_server_start(const char *bridge,
133 const char *router, const char* subnet,
134 const char *start_ip, const char *end_ip,
135 unsigned int lease_time, const char *dns)
137 GDHCPServerError error;
138 GDHCPServer *dhcp_server;
143 index = connman_inet_ifindex(bridge);
147 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
148 if (dhcp_server == NULL) {
149 dhcp_server_error(error);
153 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
155 g_dhcp_server_set_lease_time(dhcp_server, lease_time);
156 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
157 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
158 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
159 g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
161 g_dhcp_server_start(dhcp_server);
166 static void dhcp_server_stop(GDHCPServer *server)
171 g_dhcp_server_unref(server);
174 static int set_forward_delay(const char *name, unsigned int delay)
177 char *forward_delay_path;
180 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
182 if (forward_delay_path == NULL)
185 f = fopen(forward_delay_path, "r+");
187 g_free(forward_delay_path);
192 fprintf(f, "%d", delay);
199 static int create_bridge(const char *name)
203 DBG("name %s", name);
205 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
209 if (ioctl(sk, SIOCBRADDBR, name) == -1) {
215 err = set_forward_delay(name, 0);
218 ioctl(sk, SIOCBRDELBR, name);
225 static int remove_bridge(const char *name)
229 DBG("name %s", name);
231 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
235 err = ioctl(sk, SIOCBRDELBR, name);
245 static int enable_bridge(const char *name)
249 index = connman_inet_ifindex(name);
253 err = __connman_inet_modify_address(RTM_NEWADDR,
254 NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
255 BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
259 return connman_inet_ifup(index);
262 static int disable_bridge(const char *name)
266 index = connman_inet_ifindex(name);
270 return connman_inet_ifdown(index);
273 static int enable_ip_forward(connman_bool_t enable)
278 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
292 static int enable_nat(const char *interface)
296 if (interface == NULL)
299 /* Enable IPv4 forwarding */
300 err = enable_ip_forward(TRUE);
304 /* POSTROUTING flush */
305 err = __connman_iptables_command("-t nat -F POSTROUTING");
309 /* Enable masquerading */
310 err = __connman_iptables_command("-t nat -A POSTROUTING "
311 "-o %s -j MASQUERADE", interface);
315 return __connman_iptables_commit("nat");
318 static void disable_nat(const char *interface)
322 /* Disable IPv4 forwarding */
323 enable_ip_forward(FALSE);
325 /* POSTROUTING flush */
326 err = __connman_iptables_command("-t nat -F POSTROUTING");
330 __connman_iptables_commit("nat");
333 void __connman_tethering_set_enabled(void)
338 DBG("enabled %d", tethering_enabled + 1);
340 if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
343 err = create_bridge(BRIDGE_NAME);
347 err = enable_bridge(BRIDGE_NAME);
348 if (err < 0 && err != -EALREADY) {
349 remove_bridge(BRIDGE_NAME);
354 if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
355 connman_error("Can't add listener %s to DNS proxy",
360 tethering_dhcp_server =
361 dhcp_server_start(BRIDGE_NAME,
362 BRIDGE_IP, BRIDGE_SUBNET,
363 BRIDGE_IP_START, BRIDGE_IP_END,
365 if (tethering_dhcp_server == NULL) {
366 disable_bridge(BRIDGE_NAME);
367 remove_bridge(BRIDGE_NAME);
371 enable_nat(default_interface);
373 DBG("tethering started");
376 void __connman_tethering_set_disabled(void)
378 DBG("enabled %d", tethering_enabled - 1);
380 __connman_dnsproxy_remove_listener(BRIDGE_NAME);
382 if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
385 disable_nat(default_interface);
387 dhcp_server_stop(tethering_dhcp_server);
389 tethering_dhcp_server = NULL;
391 disable_bridge(BRIDGE_NAME);
393 remove_bridge(BRIDGE_NAME);
395 DBG("tethering stopped");
398 void __connman_tethering_update_interface(const char *interface)
400 DBG("interface %s", interface);
402 g_free(default_interface);
404 if (interface == NULL) {
405 disable_nat(interface);
406 default_interface = NULL;
411 default_interface = g_strdup(interface);
413 __sync_synchronize();
414 if (tethering_enabled == 0)
417 enable_nat(interface);
420 static void setup_tun_interface(unsigned int flags, unsigned change,
423 struct connman_private_network *pn = data;
424 unsigned char prefixlen;
425 DBusMessageIter array, dict;
426 const char *server_ip;
428 const char *subnet_mask;
431 DBG("index %d flags %d change %d", pn->index, flags, change);
436 subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
437 server_ip = __connman_ippool_get_start_ip(pn->pool);
438 peer_ip = __connman_ippool_get_end_ip(pn->pool);
440 __connman_ipconfig_netmask_prefix_len(subnet_mask);
442 if ((__connman_inet_modify_address(RTM_NEWADDR,
443 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
444 server_ip, peer_ip, prefixlen, NULL)) < 0) {
445 DBG("address setting failed");
449 connman_inet_ifup(pn->index);
451 err = enable_nat(default_interface);
453 connman_error("failed to enable NAT on %s", default_interface);
457 dbus_message_iter_init_append(pn->reply, &array);
459 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
462 connman_dbus_dict_open(&array, &dict);
464 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
465 DBUS_TYPE_STRING, &server_ip);
466 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
467 DBUS_TYPE_STRING, &peer_ip);
468 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
469 DBUS_TYPE_STRING, &pn->primary_dns);
470 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
471 DBUS_TYPE_STRING, &pn->secondary_dns);
473 connman_dbus_dict_close(&array, &dict);
475 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
477 g_dbus_send_message(connection, pn->reply);
482 pn->reply = __connman_error_failed(pn->msg, -err);
483 g_dbus_send_message(connection, pn->reply);
485 g_hash_table_remove(pn_hash, pn->path);
488 static void remove_private_network(gpointer user_data)
490 struct connman_private_network *pn = user_data;
492 disable_nat(default_interface);
493 connman_rtnl_remove_watch(pn->iface_watch);
494 __connman_ippool_unref(pn->pool);
497 g_dbus_remove_watch(connection, pn->watch);
503 g_free(pn->interface);
509 static void owner_disconnect(DBusConnection *connection, void *user_data)
511 struct connman_private_network *pn = user_data;
513 DBG("%s died", pn->owner);
517 g_hash_table_remove(pn_hash, pn->path);
520 int __connman_private_network_request(DBusMessage *msg, const char *owner)
522 struct connman_private_network *pn;
527 if (DBUS_TYPE_UNIX_FD < 0)
530 fd = connman_inet_create_tunnel(&iface);
534 path = g_strdup_printf("/tethering/%s", iface);
536 pn = g_hash_table_lookup(pn_hash, path);
544 index = connman_inet_ifindex(iface);
549 DBG("interface %s", iface);
551 err = connman_inet_set_mtu(index, DEFAULT_MTU);
553 pn = g_try_new0(struct connman_private_network, 1);
559 pn->owner = g_strdup(owner);
561 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
562 owner_disconnect, pn, NULL);
564 pn->reply = dbus_message_new_method_return(pn->msg);
565 if (pn->reply == NULL)
569 pn->interface = iface;
571 pn->pool = __connman_ippool_create(1, 1);
572 if (pn->pool == NULL) {
577 pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
578 pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
580 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
581 setup_tun_interface, pn);
583 g_hash_table_insert(pn_hash, pn->path, pn);
595 int __connman_private_network_release(const char *path)
597 struct connman_private_network *pn;
599 pn = g_hash_table_lookup(pn_hash, path);
603 g_hash_table_remove(pn_hash, path);
607 int __connman_tethering_init(void)
611 tethering_enabled = 0;
613 connection = connman_dbus_get_connection();
614 if (connection == NULL)
617 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
618 NULL, remove_private_network);
623 void __connman_tethering_cleanup(void)
627 __sync_synchronize();
628 if (tethering_enabled == 0) {
629 if (tethering_dhcp_server)
630 dhcp_server_stop(tethering_dhcp_server);
631 disable_bridge(BRIDGE_NAME);
632 remove_bridge(BRIDGE_NAME);
635 if (connection == NULL)
638 g_hash_table_destroy(pn_hash);
639 dbus_connection_unref(connection);