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>
36 #include <netinet/in.h>
37 #include <linux/sockios.h>
38 #include <linux/if_tun.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 static GHashTable *clients_table;
66 struct _clients_notify {
71 struct connman_private_network {
81 struct connman_ippool *pool;
86 const char *__connman_tethering_get_bridge(void)
89 unsigned long args[3];
91 sk = socket(AF_INET, SOCK_STREAM, 0);
95 args[0] = BRCTL_GET_VERSION;
96 args[1] = args[2] = 0;
97 err = ioctl(sk, SIOCGIFBR, &args);
100 connman_error("Missing support for 802.1d ethernet bridging");
107 static void dhcp_server_debug(const char *str, void *data)
109 connman_info("%s: %s\n", (const char *) data, str);
112 static void dhcp_server_error(GDHCPServerError error)
115 case G_DHCP_SERVER_ERROR_NONE:
118 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
119 connman_error("Interface unavailable");
121 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
122 connman_error("Interface in use");
124 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
125 connman_error("Interface down");
127 case G_DHCP_SERVER_ERROR_NOMEM:
128 connman_error("No memory");
130 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
131 connman_error("Invalid index");
133 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
134 connman_error("Invalid option");
136 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
137 connman_error("Invalid address");
142 static GDHCPServer *dhcp_server_start(const char *bridge,
143 const char *router, const char *subnet,
144 const char *start_ip, const char *end_ip,
145 unsigned int lease_time, const char *dns)
147 GDHCPServerError error;
148 GDHCPServer *dhcp_server;
153 index = connman_inet_ifindex(bridge);
157 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
159 dhcp_server_error(error);
163 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
165 g_dhcp_server_set_lease_time(dhcp_server, lease_time);
166 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
167 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
168 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
169 g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
171 g_dhcp_server_start(dhcp_server);
176 static void dhcp_server_stop(GDHCPServer *server)
181 g_dhcp_server_unref(server);
184 static void tethering_restart(struct connman_ippool *pool, void *user_data)
186 DBG("pool %p", pool);
187 __connman_tethering_set_disabled();
188 __connman_tethering_set_enabled();
191 static void unregister_client(gpointer key,
192 gpointer value, gpointer user_data)
194 const char *addr = key;
195 __connman_tethering_client_unregister(addr);
198 static void unregister_all_clients(void)
200 g_hash_table_foreach(clients_table, unregister_client, NULL);
203 int __connman_tethering_set_enabled(void)
208 const char *broadcast;
209 const char *subnet_mask;
210 const char *start_ip;
213 unsigned char prefixlen;
216 DBG("enabled %d", tethering_enabled + 1);
218 if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
221 err = __connman_bridge_create(BRIDGE_NAME);
223 __sync_fetch_and_sub(&tethering_enabled, 1);
227 index = connman_inet_ifindex(BRIDGE_NAME);
228 dhcp_ippool = __connman_ippool_create(index, 2, 252,
229 tethering_restart, NULL);
231 connman_error("Fail to create IP pool");
232 __connman_bridge_remove(BRIDGE_NAME);
233 __sync_fetch_and_sub(&tethering_enabled, 1);
234 return -EADDRNOTAVAIL;
237 gateway = __connman_ippool_get_gateway(dhcp_ippool);
238 broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
239 subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
240 start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
241 end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
243 err = __connman_bridge_enable(BRIDGE_NAME, gateway,
244 connman_ipaddress_calc_netmask_len(subnet_mask),
246 if (err < 0 && err != -EALREADY) {
247 __connman_ippool_free(dhcp_ippool);
249 __connman_bridge_remove(BRIDGE_NAME);
250 __sync_fetch_and_sub(&tethering_enabled, 1);
251 return -EADDRNOTAVAIL;
254 ns = connman_setting_get_string_list("FallbackNameservers");
257 g_free(private_network_primary_dns);
258 private_network_primary_dns = g_strdup(ns[0]);
261 g_free(private_network_secondary_dns);
262 private_network_secondary_dns = g_strdup(ns[1]);
265 DBG("Fallback ns primary %s secondary %s",
266 private_network_primary_dns,
267 private_network_secondary_dns);
271 if (__connman_dnsproxy_add_listener(index) < 0) {
272 connman_error("Can't add listener %s to DNS proxy",
274 dns = private_network_primary_dns;
275 DBG("Serving %s nameserver to clients", dns);
278 tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
279 gateway, subnet_mask,
282 if (!tethering_dhcp_server) {
283 __connman_bridge_disable(BRIDGE_NAME);
284 __connman_ippool_free(dhcp_ippool);
286 __connman_bridge_remove(BRIDGE_NAME);
287 __sync_fetch_and_sub(&tethering_enabled, 1);
291 prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
292 err = __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
294 connman_error("Cannot enable NAT %d/%s", err, strerror(-err));
295 dhcp_server_stop(tethering_dhcp_server);
296 __connman_bridge_disable(BRIDGE_NAME);
297 __connman_ippool_free(dhcp_ippool);
299 __connman_bridge_remove(BRIDGE_NAME);
300 __sync_fetch_and_sub(&tethering_enabled, 1);
304 err = __connman_ipv6pd_setup(BRIDGE_NAME);
305 if (err < 0 && err != -EINPROGRESS)
306 DBG("Cannot setup IPv6 prefix delegation %d/%s", err,
309 DBG("tethering started");
314 void __connman_tethering_set_disabled(void)
318 DBG("enabled %d", tethering_enabled - 1);
320 if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
323 unregister_all_clients();
325 __connman_ipv6pd_cleanup();
327 index = connman_inet_ifindex(BRIDGE_NAME);
328 __connman_dnsproxy_remove_listener(index);
330 __connman_nat_disable(BRIDGE_NAME);
332 dhcp_server_stop(tethering_dhcp_server);
334 tethering_dhcp_server = NULL;
336 __connman_bridge_disable(BRIDGE_NAME);
338 __connman_ippool_free(dhcp_ippool);
341 __connman_bridge_remove(BRIDGE_NAME);
343 g_free(private_network_primary_dns);
344 private_network_primary_dns = NULL;
345 g_free(private_network_secondary_dns);
346 private_network_secondary_dns = NULL;
348 DBG("tethering stopped");
351 static void append_client(gpointer key, gpointer value,
354 const char *addr = key;
355 DBusMessageIter *array = user_data;
357 dbus_message_iter_append_basic(array, DBUS_TYPE_STRING,
361 void __connman_tethering_list_clients(DBusMessageIter *array)
363 g_hash_table_foreach(clients_table, append_client, array);
366 static void setup_tun_interface(unsigned int flags, unsigned change,
369 struct connman_private_network *pn = data;
370 unsigned char prefixlen;
371 DBusMessageIter array, dict;
372 const char *server_ip;
374 const char *subnet_mask;
377 DBG("index %d flags %d change %d", pn->index, flags, change);
382 subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
383 server_ip = __connman_ippool_get_start_ip(pn->pool);
384 peer_ip = __connman_ippool_get_end_ip(pn->pool);
385 prefixlen = connman_ipaddress_calc_netmask_len(subnet_mask);
387 if ((__connman_inet_modify_address(RTM_NEWADDR,
388 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
389 server_ip, peer_ip, prefixlen, NULL, true))
391 DBG("address setting failed");
395 connman_inet_ifup(pn->index);
397 err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
399 connman_error("failed to enable NAT");
403 dbus_message_iter_init_append(pn->reply, &array);
405 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
408 connman_dbus_dict_open(&array, &dict);
410 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
411 DBUS_TYPE_STRING, &server_ip);
412 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
413 DBUS_TYPE_STRING, &peer_ip);
415 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
416 DBUS_TYPE_STRING, &pn->primary_dns);
418 if (pn->secondary_dns)
419 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
420 DBUS_TYPE_STRING, &pn->secondary_dns);
422 connman_dbus_dict_close(&array, &dict);
424 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
426 g_dbus_send_message(connection, pn->reply);
431 pn->reply = __connman_error_failed(pn->msg, -err);
432 g_dbus_send_message(connection, pn->reply);
434 g_hash_table_remove(pn_hash, pn->path);
437 static void remove_private_network(gpointer user_data)
439 struct connman_private_network *pn = user_data;
441 __connman_nat_disable(BRIDGE_NAME);
442 connman_rtnl_remove_watch(pn->iface_watch);
443 __connman_ippool_free(pn->pool);
446 g_dbus_remove_watch(connection, pn->watch);
452 g_free(pn->interface);
455 g_free(pn->primary_dns);
456 g_free(pn->secondary_dns);
460 static void owner_disconnect(DBusConnection *conn, void *user_data)
462 struct connman_private_network *pn = user_data;
464 DBG("%s died", pn->owner);
468 g_hash_table_remove(pn_hash, pn->path);
471 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
473 struct connman_private_network *pn = user_data;
475 DBG("block used externally");
477 g_hash_table_remove(pn_hash, pn->path);
480 static gboolean client_send_changed(gpointer data)
483 DBusMessageIter iter, array;
487 clients_notify->id = 0;
489 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
490 CONNMAN_MANAGER_INTERFACE, "TetheringClientsChanged");
494 dbus_message_iter_init_append(signal, &iter);
495 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
496 DBUS_TYPE_STRING_AS_STRING, &array);
498 g_hash_table_foreach(clients_table, append_client, &array);
500 dbus_message_iter_close_container(&iter, &array);
502 dbus_message_iter_init_append(signal, &iter);
503 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
504 DBUS_TYPE_STRING_AS_STRING, &array);
506 g_hash_table_foreach(clients_notify->remove, append_client, &array);
508 dbus_message_iter_close_container(&iter, &array);
510 dbus_connection_send(connection, signal, NULL);
511 dbus_message_unref(signal);
513 g_hash_table_remove_all(clients_notify->remove);
518 static void client_schedule_changed(void)
520 if (clients_notify->id != 0)
523 clients_notify->id = g_timeout_add(100, client_send_changed, NULL);
526 static void client_added(const char *addr)
528 DBG("client %s", addr);
530 g_hash_table_remove(clients_notify->remove, addr);
532 client_schedule_changed();
535 static void client_removed(const char *addr)
537 DBG("client %s", addr);
539 g_hash_table_replace(clients_notify->remove, g_strdup(addr), NULL);
541 client_schedule_changed();
544 int __connman_private_network_request(DBusMessage *msg, const char *owner)
546 struct connman_private_network *pn;
551 if (DBUS_TYPE_UNIX_FD < 0)
554 fd = connman_inet_create_tunnel(&iface);
558 path = g_strdup_printf("/tethering/%s", iface);
560 pn = g_hash_table_lookup(pn_hash, path);
568 index = connman_inet_ifindex(iface);
573 DBG("interface %s", iface);
575 err = connman_inet_set_mtu(index, DEFAULT_MTU);
577 pn = g_try_new0(struct connman_private_network, 1);
583 pn->owner = g_strdup(owner);
585 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
586 owner_disconnect, pn, NULL);
588 pn->reply = dbus_message_new_method_return(pn->msg);
593 pn->interface = iface;
595 pn->pool = __connman_ippool_create(pn->index, 1, 1, ippool_disconnect, pn);
601 pn->primary_dns = g_strdup(private_network_primary_dns);
602 pn->secondary_dns = g_strdup(private_network_secondary_dns);
604 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
605 setup_tun_interface, pn);
607 g_hash_table_insert(pn_hash, pn->path, pn);
621 int __connman_private_network_release(const char *path)
623 struct connman_private_network *pn;
625 pn = g_hash_table_lookup(pn_hash, path);
629 g_hash_table_remove(pn_hash, path);
633 void __connman_tethering_client_register(const char *addr)
635 g_hash_table_insert(clients_table, g_strdup(addr), NULL);
639 void __connman_tethering_client_unregister(const char *addr)
641 client_removed(addr);
642 g_hash_table_remove(clients_table, addr);
645 int __connman_tethering_init(void)
649 tethering_enabled = 0;
651 connection = connman_dbus_get_connection();
655 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
656 NULL, remove_private_network);
658 clients_table = g_hash_table_new_full(g_str_hash, g_str_equal,
661 clients_notify = g_new0(struct _clients_notify, 1);
662 clients_notify->remove = g_hash_table_new_full(g_str_hash, g_str_equal,
667 void __connman_tethering_cleanup(void)
669 DBG("enabled %d", tethering_enabled);
671 __sync_synchronize();
672 if (tethering_enabled > 0) {
673 if (tethering_dhcp_server)
674 dhcp_server_stop(tethering_dhcp_server);
675 __connman_bridge_disable(BRIDGE_NAME);
676 __connman_bridge_remove(BRIDGE_NAME);
677 __connman_nat_disable(BRIDGE_NAME);
683 g_hash_table_destroy(pn_hash);
685 g_hash_table_destroy(clients_notify->remove);
686 g_free(clients_notify);
687 clients_notify = NULL;
689 g_hash_table_destroy(clients_table);
690 clients_table = NULL;
692 dbus_connection_unref(connection);