5 * Copyright (C) 2007-2013 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>
38 #include <netinet/in.h>
39 #include <linux/if_bridge.h>
43 #include <gdhcp/gdhcp.h>
47 #ifndef DBUS_TYPE_UNIX_FD
48 #define DBUS_TYPE_UNIX_FD -1
51 #define BRIDGE_NAME "tether"
53 #define DEFAULT_MTU 1500
55 static char *private_network_primary_dns = NULL;
56 static char *private_network_secondary_dns = NULL;
58 static volatile int tethering_enabled;
59 static GDHCPServer *tethering_dhcp_server = NULL;
60 static struct connman_ippool *dhcp_ippool = NULL;
61 static DBusConnection *connection;
62 static GHashTable *pn_hash;
64 struct connman_private_network {
74 struct connman_ippool *pool;
79 const char *__connman_tethering_get_bridge(void)
82 unsigned long args[3];
84 sk = socket(AF_INET, SOCK_STREAM, 0);
88 args[0] = BRCTL_GET_VERSION;
89 args[1] = args[2] = 0;
90 err = ioctl(sk, SIOCGIFBR, &args);
93 connman_error("Missing support for 802.1d ethernet bridging");
100 static void dhcp_server_debug(const char *str, void *data)
102 connman_info("%s: %s\n", (const char *) data, str);
105 static void dhcp_server_error(GDHCPServerError error)
108 case G_DHCP_SERVER_ERROR_NONE:
111 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
112 connman_error("Interface unavailable");
114 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
115 connman_error("Interface in use");
117 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
118 connman_error("Interface down");
120 case G_DHCP_SERVER_ERROR_NOMEM:
121 connman_error("No memory");
123 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
124 connman_error("Invalid index");
126 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
127 connman_error("Invalid option");
129 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
130 connman_error("Invalid address");
135 static GDHCPServer *dhcp_server_start(const char *bridge,
136 const char *router, const char *subnet,
137 const char *start_ip, const char *end_ip,
138 unsigned int lease_time, const char *dns)
140 GDHCPServerError error;
141 GDHCPServer *dhcp_server;
146 index = connman_inet_ifindex(bridge);
150 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
152 dhcp_server_error(error);
156 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
158 g_dhcp_server_set_lease_time(dhcp_server, lease_time);
159 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
160 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
161 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
162 g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
164 g_dhcp_server_start(dhcp_server);
169 static void dhcp_server_stop(GDHCPServer *server)
174 g_dhcp_server_unref(server);
177 static void tethering_restart(struct connman_ippool *pool, void *user_data)
179 DBG("pool %p", pool);
180 __connman_tethering_set_disabled();
181 __connman_tethering_set_enabled();
184 int __connman_tethering_set_enabled(void)
189 const char *broadcast;
190 const char *subnet_mask;
191 const char *start_ip;
194 unsigned char prefixlen;
197 DBG("enabled %d", tethering_enabled + 1);
199 if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
202 err = __connman_bridge_create(BRIDGE_NAME);
204 __sync_fetch_and_sub(&tethering_enabled, 1);
208 index = connman_inet_ifindex(BRIDGE_NAME);
209 dhcp_ippool = __connman_ippool_create(index, 2, 252,
210 tethering_restart, NULL);
212 connman_error("Fail to create IP pool");
213 __connman_bridge_remove(BRIDGE_NAME);
214 __sync_fetch_and_sub(&tethering_enabled, 1);
215 return -EADDRNOTAVAIL;
218 gateway = __connman_ippool_get_gateway(dhcp_ippool);
219 broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
220 subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
221 start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
222 end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
224 err = __connman_bridge_enable(BRIDGE_NAME, gateway,
225 connman_ipaddress_calc_netmask_len(subnet_mask),
227 if (err < 0 && err != -EALREADY) {
228 __connman_ippool_unref(dhcp_ippool);
229 __connman_bridge_remove(BRIDGE_NAME);
230 __sync_fetch_and_sub(&tethering_enabled, 1);
231 return -EADDRNOTAVAIL;
234 ns = connman_setting_get_string_list("FallbackNameservers");
237 g_free(private_network_primary_dns);
238 private_network_primary_dns = g_strdup(ns[0]);
241 g_free(private_network_secondary_dns);
242 private_network_secondary_dns = g_strdup(ns[1]);
245 DBG("Fallback ns primary %s secondary %s",
246 private_network_primary_dns,
247 private_network_secondary_dns);
251 if (__connman_dnsproxy_add_listener(index) < 0) {
252 connman_error("Can't add listener %s to DNS proxy",
254 dns = private_network_primary_dns;
255 DBG("Serving %s nameserver to clients", dns);
258 tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
259 gateway, subnet_mask,
262 if (!tethering_dhcp_server) {
263 __connman_bridge_disable(BRIDGE_NAME);
264 __connman_ippool_unref(dhcp_ippool);
265 __connman_bridge_remove(BRIDGE_NAME);
266 __sync_fetch_and_sub(&tethering_enabled, 1);
270 prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
271 err = __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
273 connman_error("Cannot enable NAT %d/%s", err, strerror(-err));
274 dhcp_server_stop(tethering_dhcp_server);
275 __connman_bridge_disable(BRIDGE_NAME);
276 __connman_ippool_unref(dhcp_ippool);
277 __connman_bridge_remove(BRIDGE_NAME);
278 __sync_fetch_and_sub(&tethering_enabled, 1);
282 err = __connman_ipv6pd_setup(BRIDGE_NAME);
283 if (err < 0 && err != -EINPROGRESS)
284 DBG("Cannot setup IPv6 prefix delegation %d/%s", err,
287 DBG("tethering started");
292 void __connman_tethering_set_disabled(void)
296 DBG("enabled %d", tethering_enabled - 1);
298 if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
301 __connman_ipv6pd_cleanup();
303 index = connman_inet_ifindex(BRIDGE_NAME);
304 __connman_dnsproxy_remove_listener(index);
306 __connman_nat_disable(BRIDGE_NAME);
308 dhcp_server_stop(tethering_dhcp_server);
310 tethering_dhcp_server = NULL;
312 __connman_bridge_disable(BRIDGE_NAME);
314 __connman_ippool_unref(dhcp_ippool);
316 __connman_bridge_remove(BRIDGE_NAME);
318 g_free(private_network_primary_dns);
319 private_network_primary_dns = NULL;
320 g_free(private_network_secondary_dns);
321 private_network_secondary_dns = NULL;
323 DBG("tethering stopped");
326 static void setup_tun_interface(unsigned int flags, unsigned change,
329 struct connman_private_network *pn = data;
330 unsigned char prefixlen;
331 DBusMessageIter array, dict;
332 const char *server_ip;
334 const char *subnet_mask;
337 DBG("index %d flags %d change %d", pn->index, flags, change);
342 subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
343 server_ip = __connman_ippool_get_start_ip(pn->pool);
344 peer_ip = __connman_ippool_get_end_ip(pn->pool);
345 prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
347 if ((__connman_inet_modify_address(RTM_NEWADDR,
348 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
349 server_ip, peer_ip, prefixlen, NULL)) < 0) {
350 DBG("address setting failed");
354 connman_inet_ifup(pn->index);
356 err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
358 connman_error("failed to enable NAT");
362 dbus_message_iter_init_append(pn->reply, &array);
364 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
367 connman_dbus_dict_open(&array, &dict);
369 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
370 DBUS_TYPE_STRING, &server_ip);
371 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
372 DBUS_TYPE_STRING, &peer_ip);
374 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
375 DBUS_TYPE_STRING, &pn->primary_dns);
377 if (pn->secondary_dns)
378 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
379 DBUS_TYPE_STRING, &pn->secondary_dns);
381 connman_dbus_dict_close(&array, &dict);
383 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
385 g_dbus_send_message(connection, pn->reply);
390 pn->reply = __connman_error_failed(pn->msg, -err);
391 g_dbus_send_message(connection, pn->reply);
393 g_hash_table_remove(pn_hash, pn->path);
396 static void remove_private_network(gpointer user_data)
398 struct connman_private_network *pn = user_data;
400 __connman_nat_disable(BRIDGE_NAME);
401 connman_rtnl_remove_watch(pn->iface_watch);
402 __connman_ippool_unref(pn->pool);
405 g_dbus_remove_watch(connection, pn->watch);
411 g_free(pn->interface);
414 g_free(pn->primary_dns);
415 g_free(pn->secondary_dns);
419 static void owner_disconnect(DBusConnection *conn, void *user_data)
421 struct connman_private_network *pn = user_data;
423 DBG("%s died", pn->owner);
427 g_hash_table_remove(pn_hash, pn->path);
430 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
432 struct connman_private_network *pn = user_data;
434 DBG("block used externally");
436 g_hash_table_remove(pn_hash, pn->path);
439 int __connman_private_network_request(DBusMessage *msg, const char *owner)
441 struct connman_private_network *pn;
446 if (DBUS_TYPE_UNIX_FD < 0)
449 fd = connman_inet_create_tunnel(&iface);
453 path = g_strdup_printf("/tethering/%s", iface);
455 pn = g_hash_table_lookup(pn_hash, path);
463 index = connman_inet_ifindex(iface);
468 DBG("interface %s", iface);
470 err = connman_inet_set_mtu(index, DEFAULT_MTU);
472 pn = g_try_new0(struct connman_private_network, 1);
478 pn->owner = g_strdup(owner);
480 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
481 owner_disconnect, pn, NULL);
483 pn->reply = dbus_message_new_method_return(pn->msg);
488 pn->interface = iface;
490 pn->pool = __connman_ippool_create(pn->index, 1, 1, ippool_disconnect, pn);
496 pn->primary_dns = g_strdup(private_network_primary_dns);
497 pn->secondary_dns = g_strdup(private_network_secondary_dns);
499 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
500 setup_tun_interface, pn);
502 g_hash_table_insert(pn_hash, pn->path, pn);
516 int __connman_private_network_release(const char *path)
518 struct connman_private_network *pn;
520 pn = g_hash_table_lookup(pn_hash, path);
524 g_hash_table_remove(pn_hash, path);
528 int __connman_tethering_init(void)
532 tethering_enabled = 0;
534 connection = connman_dbus_get_connection();
538 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
539 NULL, remove_private_network);
544 void __connman_tethering_cleanup(void)
546 DBG("enabled %d", tethering_enabled);
548 __sync_synchronize();
549 if (tethering_enabled > 0) {
550 if (tethering_dhcp_server)
551 dhcp_server_stop(tethering_dhcp_server);
552 __connman_bridge_disable(BRIDGE_NAME);
553 __connman_bridge_remove(BRIDGE_NAME);
554 __connman_nat_disable(BRIDGE_NAME);
560 g_hash_table_destroy(pn_hash);
561 dbus_connection_unref(connection);