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_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 char *default_interface = NULL;
60 static volatile int tethering_enabled;
61 static GDHCPServer *tethering_dhcp_server = NULL;
62 static struct connman_ippool *dhcp_ippool = NULL;
63 static DBusConnection *connection;
64 static GHashTable *pn_hash;
66 struct connman_private_network {
76 struct connman_ippool *pool;
77 const char *primary_dns;
78 const char *secondary_dns;
81 const char *__connman_tethering_get_bridge(void)
85 if (stat(BRIDGE_PROC_DIR, &st) < 0) {
86 connman_error("Missing support for 802.1d ethernet bridging");
93 static void dhcp_server_debug(const char *str, void *data)
95 connman_info("%s: %s\n", (const char *) data, str);
98 static void dhcp_server_error(GDHCPServerError error)
101 case G_DHCP_SERVER_ERROR_NONE:
104 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
105 connman_error("Interface unavailable");
107 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
108 connman_error("Interface in use");
110 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
111 connman_error("Interface down");
113 case G_DHCP_SERVER_ERROR_NOMEM:
114 connman_error("No memory");
116 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
117 connman_error("Invalid index");
119 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
120 connman_error("Invalid option");
122 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
123 connman_error("Invalid address");
128 static GDHCPServer *dhcp_server_start(const char *bridge,
129 const char *router, const char* subnet,
130 const char *start_ip, const char *end_ip,
131 unsigned int lease_time, const char *dns)
133 GDHCPServerError error;
134 GDHCPServer *dhcp_server;
139 index = connman_inet_ifindex(bridge);
143 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
144 if (dhcp_server == NULL) {
145 dhcp_server_error(error);
149 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
151 g_dhcp_server_set_lease_time(dhcp_server, lease_time);
152 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
153 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
154 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
155 g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
157 g_dhcp_server_start(dhcp_server);
162 static void dhcp_server_stop(GDHCPServer *server)
167 g_dhcp_server_unref(server);
170 static int enable_ip_forward(connman_bool_t enable)
175 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
189 static int enable_nat(const char *interface)
193 if (interface == NULL)
196 /* Enable IPv4 forwarding */
197 err = enable_ip_forward(TRUE);
201 /* POSTROUTING flush */
202 err = __connman_iptables_command("-t nat -F POSTROUTING");
206 /* Enable masquerading */
207 err = __connman_iptables_command("-t nat -A POSTROUTING "
208 "-o %s -j MASQUERADE", interface);
212 return __connman_iptables_commit("nat");
215 static void disable_nat(const char *interface)
219 /* Disable IPv4 forwarding */
220 enable_ip_forward(FALSE);
222 /* POSTROUTING flush */
223 err = __connman_iptables_command("-t nat -F POSTROUTING");
227 __connman_iptables_commit("nat");
230 static void tethering_restart(struct connman_ippool *pool, void *user_data)
232 __connman_tethering_set_disabled();
233 __connman_tethering_set_enabled();
236 void __connman_tethering_set_enabled(void)
241 const char *broadcast;
242 const char *subnet_mask;
243 const char *start_ip;
247 DBG("enabled %d", tethering_enabled + 1);
249 if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
252 err = __connman_bridge_create(BRIDGE_NAME);
256 index = connman_inet_ifindex(BRIDGE_NAME);
257 dhcp_ippool = __connman_ippool_create(index, 1, 253,
258 tethering_restart, NULL);
259 if (dhcp_ippool == NULL) {
260 connman_error("Fail to create IP pool");
264 gateway = __connman_ippool_get_gateway(dhcp_ippool);
265 broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
266 subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
267 start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
268 end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
270 err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
271 if (err < 0 && err != -EALREADY) {
272 __connman_bridge_remove(BRIDGE_NAME);
277 if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
278 connman_error("Can't add listener %s to DNS proxy",
283 tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
284 gateway, subnet_mask,
287 if (tethering_dhcp_server == NULL) {
288 __connman_bridge_disable(BRIDGE_NAME);
289 __connman_bridge_remove(BRIDGE_NAME);
293 enable_nat(default_interface);
295 DBG("tethering started");
298 void __connman_tethering_set_disabled(void)
300 DBG("enabled %d", tethering_enabled - 1);
302 __connman_dnsproxy_remove_listener(BRIDGE_NAME);
304 if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
307 disable_nat(default_interface);
309 dhcp_server_stop(tethering_dhcp_server);
311 tethering_dhcp_server = NULL;
313 __connman_bridge_disable(BRIDGE_NAME);
315 __connman_ippool_unref(dhcp_ippool);
317 __connman_bridge_remove(BRIDGE_NAME);
319 DBG("tethering stopped");
322 void __connman_tethering_update_interface(const char *interface)
324 DBG("interface %s", interface);
326 g_free(default_interface);
328 if (interface == NULL) {
329 disable_nat(interface);
330 default_interface = NULL;
335 default_interface = g_strdup(interface);
337 __sync_synchronize();
338 if (tethering_enabled == 0)
341 enable_nat(interface);
344 static void setup_tun_interface(unsigned int flags, unsigned change,
347 struct connman_private_network *pn = data;
348 unsigned char prefixlen;
349 DBusMessageIter array, dict;
350 const char *server_ip;
352 const char *subnet_mask;
355 DBG("index %d flags %d change %d", pn->index, flags, change);
360 subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
361 server_ip = __connman_ippool_get_start_ip(pn->pool);
362 peer_ip = __connman_ippool_get_end_ip(pn->pool);
364 __connman_ipconfig_netmask_prefix_len(subnet_mask);
366 if ((__connman_inet_modify_address(RTM_NEWADDR,
367 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
368 server_ip, peer_ip, prefixlen, NULL)) < 0) {
369 DBG("address setting failed");
373 connman_inet_ifup(pn->index);
375 err = enable_nat(default_interface);
377 connman_error("failed to enable NAT on %s", default_interface);
381 dbus_message_iter_init_append(pn->reply, &array);
383 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
386 connman_dbus_dict_open(&array, &dict);
388 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
389 DBUS_TYPE_STRING, &server_ip);
390 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
391 DBUS_TYPE_STRING, &peer_ip);
392 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
393 DBUS_TYPE_STRING, &pn->primary_dns);
394 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
395 DBUS_TYPE_STRING, &pn->secondary_dns);
397 connman_dbus_dict_close(&array, &dict);
399 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
401 g_dbus_send_message(connection, pn->reply);
406 pn->reply = __connman_error_failed(pn->msg, -err);
407 g_dbus_send_message(connection, pn->reply);
409 g_hash_table_remove(pn_hash, pn->path);
412 static void remove_private_network(gpointer user_data)
414 struct connman_private_network *pn = user_data;
416 disable_nat(default_interface);
417 connman_rtnl_remove_watch(pn->iface_watch);
418 __connman_ippool_unref(pn->pool);
421 g_dbus_remove_watch(connection, pn->watch);
427 g_free(pn->interface);
433 static void owner_disconnect(DBusConnection *connection, void *user_data)
435 struct connman_private_network *pn = user_data;
437 DBG("%s died", pn->owner);
441 g_hash_table_remove(pn_hash, pn->path);
444 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
446 struct connman_private_network *pn = user_data;
448 DBG("block used externally");
450 g_hash_table_remove(pn_hash, pn->path);
453 int __connman_private_network_request(DBusMessage *msg, const char *owner)
455 struct connman_private_network *pn;
460 if (DBUS_TYPE_UNIX_FD < 0)
463 fd = connman_inet_create_tunnel(&iface);
467 path = g_strdup_printf("/tethering/%s", iface);
469 pn = g_hash_table_lookup(pn_hash, path);
477 index = connman_inet_ifindex(iface);
482 DBG("interface %s", iface);
484 err = connman_inet_set_mtu(index, DEFAULT_MTU);
486 pn = g_try_new0(struct connman_private_network, 1);
492 pn->owner = g_strdup(owner);
494 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
495 owner_disconnect, pn, NULL);
497 pn->reply = dbus_message_new_method_return(pn->msg);
498 if (pn->reply == NULL)
502 pn->interface = iface;
504 pn->pool = __connman_ippool_create(pn->fd, 1, 1, ippool_disconnect, pn);
505 if (pn->pool == NULL) {
510 pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
511 pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
513 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
514 setup_tun_interface, pn);
516 g_hash_table_insert(pn_hash, pn->path, pn);
528 int __connman_private_network_release(const char *path)
530 struct connman_private_network *pn;
532 pn = g_hash_table_lookup(pn_hash, path);
536 g_hash_table_remove(pn_hash, path);
540 int __connman_tethering_init(void)
544 tethering_enabled = 0;
546 connection = connman_dbus_get_connection();
547 if (connection == NULL)
550 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
551 NULL, remove_private_network);
556 void __connman_tethering_cleanup(void)
560 __sync_synchronize();
561 if (tethering_enabled == 0) {
562 if (tethering_dhcp_server)
563 dhcp_server_stop(tethering_dhcp_server);
564 __connman_bridge_disable(BRIDGE_NAME);
565 __connman_bridge_remove(BRIDGE_NAME);
568 if (connection == NULL)
571 g_hash_table_destroy(pn_hash);
572 dbus_connection_unref(connection);