5 * Copyright (C) 2007-2012 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 volatile int tethering_enabled;
60 static GDHCPServer *tethering_dhcp_server = NULL;
61 static struct connman_ippool *dhcp_ippool = NULL;
62 static DBusConnection *connection;
63 static GHashTable *pn_hash;
65 struct connman_private_network {
75 struct connman_ippool *pool;
76 const char *primary_dns;
77 const char *secondary_dns;
80 const char *__connman_tethering_get_bridge(void)
84 if (stat(BRIDGE_PROC_DIR, &st) < 0) {
85 connman_error("Missing support for 802.1d ethernet bridging");
92 static void dhcp_server_debug(const char *str, void *data)
94 connman_info("%s: %s\n", (const char *) data, str);
97 static void dhcp_server_error(GDHCPServerError error)
100 case G_DHCP_SERVER_ERROR_NONE:
103 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
104 connman_error("Interface unavailable");
106 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
107 connman_error("Interface in use");
109 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
110 connman_error("Interface down");
112 case G_DHCP_SERVER_ERROR_NOMEM:
113 connman_error("No memory");
115 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
116 connman_error("Invalid index");
118 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
119 connman_error("Invalid option");
121 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
122 connman_error("Invalid address");
127 static GDHCPServer *dhcp_server_start(const char *bridge,
128 const char *router, const char* subnet,
129 const char *start_ip, const char *end_ip,
130 unsigned int lease_time, const char *dns)
132 GDHCPServerError error;
133 GDHCPServer *dhcp_server;
138 index = connman_inet_ifindex(bridge);
142 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
143 if (dhcp_server == NULL) {
144 dhcp_server_error(error);
148 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
150 g_dhcp_server_set_lease_time(dhcp_server, lease_time);
151 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
152 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
153 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
154 g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
156 g_dhcp_server_start(dhcp_server);
161 static void dhcp_server_stop(GDHCPServer *server)
166 g_dhcp_server_unref(server);
169 static void tethering_restart(struct connman_ippool *pool, void *user_data)
171 __connman_tethering_set_disabled();
172 __connman_tethering_set_enabled();
175 void __connman_tethering_set_enabled(void)
180 const char *broadcast;
181 const char *subnet_mask;
182 const char *start_ip;
185 unsigned char prefixlen;
187 DBG("enabled %d", tethering_enabled + 1);
189 if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
192 err = __connman_bridge_create(BRIDGE_NAME);
196 index = connman_inet_ifindex(BRIDGE_NAME);
197 dhcp_ippool = __connman_ippool_create(index, 2, 252,
198 tethering_restart, NULL);
199 if (dhcp_ippool == NULL) {
200 connman_error("Fail to create IP pool");
204 gateway = __connman_ippool_get_gateway(dhcp_ippool);
205 broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
206 subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
207 start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
208 end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
210 err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
211 if (err < 0 && err != -EALREADY) {
212 __connman_bridge_remove(BRIDGE_NAME);
217 if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
218 connman_error("Can't add listener %s to DNS proxy",
223 tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
224 gateway, subnet_mask,
227 if (tethering_dhcp_server == NULL) {
228 __connman_bridge_disable(BRIDGE_NAME);
229 __connman_bridge_remove(BRIDGE_NAME);
234 __connman_ipconfig_netmask_prefix_len(subnet_mask);
235 __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
237 DBG("tethering started");
240 void __connman_tethering_set_disabled(void)
242 DBG("enabled %d", tethering_enabled - 1);
244 __connman_dnsproxy_remove_listener(BRIDGE_NAME);
246 if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
249 __connman_nat_disable(BRIDGE_NAME);
251 dhcp_server_stop(tethering_dhcp_server);
253 tethering_dhcp_server = NULL;
255 __connman_bridge_disable(BRIDGE_NAME);
257 __connman_ippool_unref(dhcp_ippool);
259 __connman_bridge_remove(BRIDGE_NAME);
261 DBG("tethering stopped");
264 static void setup_tun_interface(unsigned int flags, unsigned change,
267 struct connman_private_network *pn = data;
268 unsigned char prefixlen;
269 DBusMessageIter array, dict;
270 const char *server_ip;
272 const char *subnet_mask;
275 DBG("index %d flags %d change %d", pn->index, flags, change);
280 subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
281 server_ip = __connman_ippool_get_start_ip(pn->pool);
282 peer_ip = __connman_ippool_get_end_ip(pn->pool);
284 __connman_ipconfig_netmask_prefix_len(subnet_mask);
286 if ((__connman_inet_modify_address(RTM_NEWADDR,
287 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
288 server_ip, peer_ip, prefixlen, NULL)) < 0) {
289 DBG("address setting failed");
293 connman_inet_ifup(pn->index);
295 err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
297 connman_error("failed to enable NAT");
301 dbus_message_iter_init_append(pn->reply, &array);
303 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
306 connman_dbus_dict_open(&array, &dict);
308 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
309 DBUS_TYPE_STRING, &server_ip);
310 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
311 DBUS_TYPE_STRING, &peer_ip);
312 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
313 DBUS_TYPE_STRING, &pn->primary_dns);
314 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
315 DBUS_TYPE_STRING, &pn->secondary_dns);
317 connman_dbus_dict_close(&array, &dict);
319 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
321 g_dbus_send_message(connection, pn->reply);
326 pn->reply = __connman_error_failed(pn->msg, -err);
327 g_dbus_send_message(connection, pn->reply);
329 g_hash_table_remove(pn_hash, pn->path);
332 static void remove_private_network(gpointer user_data)
334 struct connman_private_network *pn = user_data;
336 __connman_nat_disable(BRIDGE_NAME);
337 connman_rtnl_remove_watch(pn->iface_watch);
338 __connman_ippool_unref(pn->pool);
341 g_dbus_remove_watch(connection, pn->watch);
347 g_free(pn->interface);
353 static void owner_disconnect(DBusConnection *connection, void *user_data)
355 struct connman_private_network *pn = user_data;
357 DBG("%s died", pn->owner);
361 g_hash_table_remove(pn_hash, pn->path);
364 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
366 struct connman_private_network *pn = user_data;
368 DBG("block used externally");
370 g_hash_table_remove(pn_hash, pn->path);
373 int __connman_private_network_request(DBusMessage *msg, const char *owner)
375 struct connman_private_network *pn;
380 if (DBUS_TYPE_UNIX_FD < 0)
383 fd = connman_inet_create_tunnel(&iface);
387 path = g_strdup_printf("/tethering/%s", iface);
389 pn = g_hash_table_lookup(pn_hash, path);
397 index = connman_inet_ifindex(iface);
402 DBG("interface %s", iface);
404 err = connman_inet_set_mtu(index, DEFAULT_MTU);
406 pn = g_try_new0(struct connman_private_network, 1);
412 pn->owner = g_strdup(owner);
414 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
415 owner_disconnect, pn, NULL);
417 pn->reply = dbus_message_new_method_return(pn->msg);
418 if (pn->reply == NULL)
422 pn->interface = iface;
424 pn->pool = __connman_ippool_create(pn->fd, 1, 1, ippool_disconnect, pn);
425 if (pn->pool == NULL) {
430 pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
431 pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
433 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
434 setup_tun_interface, pn);
436 g_hash_table_insert(pn_hash, pn->path, pn);
448 int __connman_private_network_release(const char *path)
450 struct connman_private_network *pn;
452 pn = g_hash_table_lookup(pn_hash, path);
456 g_hash_table_remove(pn_hash, path);
460 int __connman_tethering_init(void)
464 tethering_enabled = 0;
466 connection = connman_dbus_get_connection();
467 if (connection == NULL)
470 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
471 NULL, remove_private_network);
476 void __connman_tethering_cleanup(void)
480 __sync_synchronize();
481 if (tethering_enabled == 0) {
482 if (tethering_dhcp_server)
483 dhcp_server_stop(tethering_dhcp_server);
484 __connman_bridge_disable(BRIDGE_NAME);
485 __connman_bridge_remove(BRIDGE_NAME);
486 __connman_nat_disable(BRIDGE_NAME);
489 if (connection == NULL)
492 g_hash_table_destroy(pn_hash);
493 dbus_connection_unref(connection);