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
27 #include <sys/types.h>
31 #include <sys/ioctl.h>
33 #include <linux/sockios.h>
36 #include <linux/if_tun.h>
40 #include <gdhcp/gdhcp.h>
44 #ifndef DBUS_TYPE_UNIX_FD
45 #define DBUS_TYPE_UNIX_FD -1
48 #define BRIDGE_PROC_DIR "/proc/sys/net/bridge"
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"
58 #define DEFAULT_MTU 1500
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"
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;
72 struct connman_private_network {
81 const char *server_ip;
83 const char *primary_dns;
84 const char *secondary_dns;
87 const char *__connman_tethering_get_bridge(void)
91 if (stat(BRIDGE_PROC_DIR, &st) < 0) {
92 connman_error("Missing support for 802.1d ethernet bridging");
99 static void dhcp_server_debug(const char *str, void *data)
101 connman_info("%s: %s\n", (const char *) data, str);
104 static void dhcp_server_error(GDHCPServerError error)
107 case G_DHCP_SERVER_ERROR_NONE:
110 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
111 connman_error("Interface unavailable");
113 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
114 connman_error("Interface in use");
116 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
117 connman_error("Interface down");
119 case G_DHCP_SERVER_ERROR_NOMEM:
120 connman_error("No memory");
122 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
123 connman_error("Invalid index");
125 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
126 connman_error("Invalid option");
128 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
129 connman_error("Invalid address");
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)
139 GDHCPServerError error;
140 GDHCPServer *dhcp_server;
145 index = connman_inet_ifindex(bridge);
149 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
150 if (dhcp_server == NULL) {
151 dhcp_server_error(error);
155 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
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);
163 g_dhcp_server_start(dhcp_server);
168 static void dhcp_server_stop(GDHCPServer *server)
173 g_dhcp_server_unref(server);
176 static int set_forward_delay(const char *name, unsigned int delay)
179 char *forward_delay_path;
182 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
184 if (forward_delay_path == NULL)
187 f = fopen(forward_delay_path, "r+");
189 g_free(forward_delay_path);
194 fprintf(f, "%d", delay);
201 static int create_bridge(const char *name)
205 DBG("name %s", name);
207 sk = socket(AF_INET, SOCK_STREAM, 0);
211 err = ioctl(sk, SIOCBRADDBR, name);
216 err = set_forward_delay(name, 0);
219 ioctl(sk, SIOCBRDELBR, name);
226 static int remove_bridge(const char *name)
230 DBG("name %s", name);
232 sk = socket(AF_INET, SOCK_STREAM, 0);
236 err = ioctl(sk, SIOCBRDELBR, name);
246 static int enable_bridge(const char *name)
250 index = connman_inet_ifindex(name);
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);
260 return connman_inet_ifup(index);
263 static int disable_bridge(const char *name)
267 index = connman_inet_ifindex(name);
271 return connman_inet_ifdown(index);
274 static int enable_ip_forward(connman_bool_t enable)
279 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
293 static int enable_nat(const char *interface)
297 if (interface == NULL)
300 /* Enable IPv4 forwarding */
301 err = enable_ip_forward(TRUE);
305 /* POSTROUTING flush */
306 err = __connman_iptables_command("-t nat -F POSTROUTING");
310 /* Enable masquerading */
311 err = __connman_iptables_command("-t nat -A POSTROUTING "
312 "-o %s -j MASQUERADE", interface);
316 return __connman_iptables_commit("nat");
319 static void disable_nat(const char *interface)
323 /* Disable IPv4 forwarding */
324 enable_ip_forward(FALSE);
326 /* POSTROUTING flush */
327 err = __connman_iptables_command("-t nat -F POSTROUTING");
331 __connman_iptables_commit("nat");
334 void __connman_tethering_set_enabled(void)
338 DBG("enabled %d", tethering_enabled + 1);
340 if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
343 err = create_bridge(BRIDGE_NAME);
347 err = enable_bridge(BRIDGE_NAME);
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");
377 void __connman_tethering_set_disabled(void)
379 DBG("enabled %d", tethering_enabled - 1);
381 __connman_dnsproxy_remove_listener(BRIDGE_NAME);
383 if (g_atomic_int_dec_and_test(&tethering_enabled) == TRUE) {
384 disable_nat(default_interface);
386 dhcp_server_stop(tethering_dhcp_server);
388 disable_bridge(BRIDGE_NAME);
390 remove_bridge(BRIDGE_NAME);
392 DBG("tethering stopped");
396 void __connman_tethering_update_interface(const char *interface)
398 DBG("interface %s", interface);
400 g_free(default_interface);
402 if (interface == NULL) {
403 disable_nat(interface);
404 default_interface = NULL;
409 default_interface = g_strdup(interface);
411 if (!g_atomic_int_get(&tethering_enabled))
414 enable_nat(interface);
417 static void setup_tun_interface(unsigned int flags, unsigned change,
420 struct connman_private_network *pn = data;
421 unsigned char prefixlen;
423 DBusMessageIter array, dict;
426 DBG("index %d flags %d change %d", pn->index, flags, change);
432 __connman_ipconfig_netmask_prefix_len(PRIVATE_NETWORK_NETMASK);
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");
442 connman_inet_ifup(pn->index);
444 err = enable_nat(default_interface);
446 connman_error("failed to enable NAT on %s", default_interface);
450 reply = dbus_message_new_method_return(pn->msg);
455 dbus_message_iter_init_append(reply, &array);
457 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
459 connman_dbus_dict_open(&array, &dict);
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);
470 connman_dbus_dict_close(&array, &dict);
472 g_dbus_send_message(connection, reply);
477 reply = __connman_error_failed(pn->msg, -err);
478 g_dbus_send_message(connection, reply);
480 g_hash_table_remove(pn_hash, pn->owner);
483 static void remove_private_network(gpointer user_data)
485 struct connman_private_network *pn = user_data;
489 connman_rtnl_remove_watch(pn->iface_watch);
491 disable_nat(default_interface);
494 g_dbus_remove_watch(connection, pn->watch);
498 g_free(pn->interface);
503 static void owner_disconnect(DBusConnection *connection, void *user_data)
505 struct connman_private_network *pn = user_data;
507 DBG("%s died", pn->owner);
511 g_hash_table_remove(pn_hash, pn->owner);
514 int __connman_private_network_request(DBusMessage *msg, const char *owner)
516 struct connman_private_network *pn;
520 if (DBUS_TYPE_UNIX_FD < 0)
523 pn = g_hash_table_lookup(pn_hash, owner);
527 fd = connman_inet_create_tunnel(&iface);
531 index = connman_inet_ifindex(iface);
536 DBG("inteface %s", iface);
538 err = connman_inet_set_mtu(index, DEFAULT_MTU);
540 pn = g_try_new0(struct connman_private_network, 1);
546 pn->owner = g_strdup(owner);
547 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
548 owner_disconnect, pn, NULL);
551 pn->interface = iface;
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;
558 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
559 setup_tun_interface, pn);
561 g_hash_table_insert(pn_hash, pn->owner, pn);
571 int __connman_private_network_release(const char *owner)
573 struct connman_private_network *pn;
575 pn = g_hash_table_lookup(pn_hash, owner);
579 g_hash_table_remove(pn_hash, owner);
583 int __connman_tethering_init(void)
587 tethering_enabled = 0;
589 connection = connman_dbus_get_connection();
590 if (connection == NULL)
593 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
594 NULL, remove_private_network);
599 void __connman_tethering_cleanup(void)
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);
610 if (connection == NULL)
613 g_hash_table_destroy(pn_hash);
614 dbus_connection_unref(connection);