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 static void update_tethering_interface(struct connman_service *service)
326 interface = connman_service_get_interface(service);
328 DBG("interface %s", interface);
330 g_free(default_interface);
332 if (interface == NULL) {
333 disable_nat(interface);
334 default_interface = NULL;
339 default_interface = g_strdup(interface);
341 __sync_synchronize();
342 if (tethering_enabled == 0)
345 enable_nat(interface);
351 static void setup_tun_interface(unsigned int flags, unsigned change,
354 struct connman_private_network *pn = data;
355 unsigned char prefixlen;
356 DBusMessageIter array, dict;
357 const char *server_ip;
359 const char *subnet_mask;
362 DBG("index %d flags %d change %d", pn->index, flags, change);
367 subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
368 server_ip = __connman_ippool_get_start_ip(pn->pool);
369 peer_ip = __connman_ippool_get_end_ip(pn->pool);
371 __connman_ipconfig_netmask_prefix_len(subnet_mask);
373 if ((__connman_inet_modify_address(RTM_NEWADDR,
374 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
375 server_ip, peer_ip, prefixlen, NULL)) < 0) {
376 DBG("address setting failed");
380 connman_inet_ifup(pn->index);
382 err = enable_nat(default_interface);
384 connman_error("failed to enable NAT on %s", default_interface);
388 dbus_message_iter_init_append(pn->reply, &array);
390 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
393 connman_dbus_dict_open(&array, &dict);
395 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
396 DBUS_TYPE_STRING, &server_ip);
397 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
398 DBUS_TYPE_STRING, &peer_ip);
399 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
400 DBUS_TYPE_STRING, &pn->primary_dns);
401 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
402 DBUS_TYPE_STRING, &pn->secondary_dns);
404 connman_dbus_dict_close(&array, &dict);
406 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
408 g_dbus_send_message(connection, pn->reply);
413 pn->reply = __connman_error_failed(pn->msg, -err);
414 g_dbus_send_message(connection, pn->reply);
416 g_hash_table_remove(pn_hash, pn->path);
419 static void remove_private_network(gpointer user_data)
421 struct connman_private_network *pn = user_data;
423 disable_nat(default_interface);
424 connman_rtnl_remove_watch(pn->iface_watch);
425 __connman_ippool_unref(pn->pool);
428 g_dbus_remove_watch(connection, pn->watch);
434 g_free(pn->interface);
440 static void owner_disconnect(DBusConnection *connection, void *user_data)
442 struct connman_private_network *pn = user_data;
444 DBG("%s died", pn->owner);
448 g_hash_table_remove(pn_hash, pn->path);
451 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
453 struct connman_private_network *pn = user_data;
455 DBG("block used externally");
457 g_hash_table_remove(pn_hash, pn->path);
460 int __connman_private_network_request(DBusMessage *msg, const char *owner)
462 struct connman_private_network *pn;
467 if (DBUS_TYPE_UNIX_FD < 0)
470 fd = connman_inet_create_tunnel(&iface);
474 path = g_strdup_printf("/tethering/%s", iface);
476 pn = g_hash_table_lookup(pn_hash, path);
484 index = connman_inet_ifindex(iface);
489 DBG("interface %s", iface);
491 err = connman_inet_set_mtu(index, DEFAULT_MTU);
493 pn = g_try_new0(struct connman_private_network, 1);
499 pn->owner = g_strdup(owner);
501 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
502 owner_disconnect, pn, NULL);
504 pn->reply = dbus_message_new_method_return(pn->msg);
505 if (pn->reply == NULL)
509 pn->interface = iface;
511 pn->pool = __connman_ippool_create(pn->fd, 1, 1, ippool_disconnect, pn);
512 if (pn->pool == NULL) {
517 pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
518 pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
520 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
521 setup_tun_interface, pn);
523 g_hash_table_insert(pn_hash, pn->path, pn);
535 int __connman_private_network_release(const char *path)
537 struct connman_private_network *pn;
539 pn = g_hash_table_lookup(pn_hash, path);
543 g_hash_table_remove(pn_hash, path);
547 static struct connman_notifier tethering_notifier = {
549 .default_changed = update_tethering_interface,
552 int __connman_tethering_init(void)
558 tethering_enabled = 0;
560 connection = connman_dbus_get_connection();
561 if (connection == NULL)
564 err = connman_notifier_register(&tethering_notifier);
566 dbus_connection_unref(connection);
570 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
571 NULL, remove_private_network);
576 void __connman_tethering_cleanup(void)
580 __sync_synchronize();
581 if (tethering_enabled == 0) {
582 if (tethering_dhcp_server)
583 dhcp_server_stop(tethering_dhcp_server);
584 __connman_bridge_disable(BRIDGE_NAME);
585 __connman_bridge_remove(BRIDGE_NAME);
588 connman_notifier_unregister(&tethering_notifier);
590 if (connection == NULL)
593 g_hash_table_destroy(pn_hash);
594 dbus_connection_unref(connection);