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_IP "192.168.218.1"
53 #define BRIDGE_BCAST "192.168.218.255"
54 #define BRIDGE_SUBNET "255.255.255.0"
55 #define BRIDGE_IP_START "192.168.218.100"
56 #define BRIDGE_IP_END "192.168.218.200"
57 #define BRIDGE_DNS "8.8.8.8"
59 #define DEFAULT_MTU 1500
61 #define PRIVATE_NETWORK_IP "192.168.219.1"
62 #define PRIVATE_NETWORK_PEER_IP "192.168.219.2"
63 #define PRIVATE_NETWORK_NETMASK "255.255.255.0"
64 #define PRIVATE_NETWORK_PRIMARY_DNS BRIDGE_DNS
65 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
67 static char *default_interface = NULL;
68 static volatile int tethering_enabled;
69 static GDHCPServer *tethering_dhcp_server = NULL;
70 static DBusConnection *connection;
71 static GHashTable *pn_hash;
73 struct connman_private_network {
83 const char *server_ip;
85 const char *primary_dns;
86 const char *secondary_dns;
89 const char *__connman_tethering_get_bridge(void)
93 if (stat(BRIDGE_PROC_DIR, &st) < 0) {
94 connman_error("Missing support for 802.1d ethernet bridging");
101 static void dhcp_server_debug(const char *str, void *data)
103 connman_info("%s: %s\n", (const char *) data, str);
106 static void dhcp_server_error(GDHCPServerError error)
109 case G_DHCP_SERVER_ERROR_NONE:
112 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
113 connman_error("Interface unavailable");
115 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
116 connman_error("Interface in use");
118 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
119 connman_error("Interface down");
121 case G_DHCP_SERVER_ERROR_NOMEM:
122 connman_error("No memory");
124 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
125 connman_error("Invalid index");
127 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
128 connman_error("Invalid option");
130 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
131 connman_error("Invalid address");
136 static GDHCPServer *dhcp_server_start(const char *bridge,
137 const char *router, const char* subnet,
138 const char *start_ip, const char *end_ip,
139 unsigned int lease_time, const char *dns)
141 GDHCPServerError error;
142 GDHCPServer *dhcp_server;
147 index = connman_inet_ifindex(bridge);
151 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
152 if (dhcp_server == NULL) {
153 dhcp_server_error(error);
157 g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
159 g_dhcp_server_set_lease_time(dhcp_server, lease_time);
160 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
161 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
162 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
163 g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
165 g_dhcp_server_start(dhcp_server);
170 static void dhcp_server_stop(GDHCPServer *server)
175 g_dhcp_server_unref(server);
178 static int set_forward_delay(const char *name, unsigned int delay)
181 char *forward_delay_path;
184 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
186 if (forward_delay_path == NULL)
189 f = fopen(forward_delay_path, "r+");
191 g_free(forward_delay_path);
196 fprintf(f, "%d", delay);
203 static int create_bridge(const char *name)
207 DBG("name %s", name);
209 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
213 if (ioctl(sk, SIOCBRADDBR, name) == -1) {
219 err = set_forward_delay(name, 0);
222 ioctl(sk, SIOCBRDELBR, name);
229 static int remove_bridge(const char *name)
233 DBG("name %s", name);
235 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
239 err = ioctl(sk, SIOCBRDELBR, name);
249 static int enable_bridge(const char *name)
253 index = connman_inet_ifindex(name);
257 err = __connman_inet_modify_address(RTM_NEWADDR,
258 NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
259 BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
263 return connman_inet_ifup(index);
266 static int disable_bridge(const char *name)
270 index = connman_inet_ifindex(name);
274 return connman_inet_ifdown(index);
277 static int enable_ip_forward(connman_bool_t enable)
282 f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
296 static int enable_nat(const char *interface)
300 if (interface == NULL)
303 /* Enable IPv4 forwarding */
304 err = enable_ip_forward(TRUE);
308 /* POSTROUTING flush */
309 err = __connman_iptables_command("-t nat -F POSTROUTING");
313 /* Enable masquerading */
314 err = __connman_iptables_command("-t nat -A POSTROUTING "
315 "-o %s -j MASQUERADE", interface);
319 return __connman_iptables_commit("nat");
322 static void disable_nat(const char *interface)
326 /* Disable IPv4 forwarding */
327 enable_ip_forward(FALSE);
329 /* POSTROUTING flush */
330 err = __connman_iptables_command("-t nat -F POSTROUTING");
334 __connman_iptables_commit("nat");
337 void __connman_tethering_set_enabled(void)
342 DBG("enabled %d", tethering_enabled + 1);
344 if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
347 err = create_bridge(BRIDGE_NAME);
351 err = enable_bridge(BRIDGE_NAME);
352 if (err < 0 && err != -EALREADY) {
353 remove_bridge(BRIDGE_NAME);
358 if (__connman_dnsproxy_add_listener(BRIDGE_NAME) < 0) {
359 connman_error("Can't add listener %s to DNS proxy",
364 tethering_dhcp_server =
365 dhcp_server_start(BRIDGE_NAME,
366 BRIDGE_IP, BRIDGE_SUBNET,
367 BRIDGE_IP_START, BRIDGE_IP_END,
369 if (tethering_dhcp_server == NULL) {
370 disable_bridge(BRIDGE_NAME);
371 remove_bridge(BRIDGE_NAME);
375 enable_nat(default_interface);
377 DBG("tethering started");
380 void __connman_tethering_set_disabled(void)
382 DBG("enabled %d", tethering_enabled - 1);
384 __connman_dnsproxy_remove_listener(BRIDGE_NAME);
386 if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
389 disable_nat(default_interface);
391 dhcp_server_stop(tethering_dhcp_server);
393 tethering_dhcp_server = NULL;
395 disable_bridge(BRIDGE_NAME);
397 remove_bridge(BRIDGE_NAME);
399 DBG("tethering stopped");
402 void __connman_tethering_update_interface(const char *interface)
404 DBG("interface %s", interface);
406 g_free(default_interface);
408 if (interface == NULL) {
409 disable_nat(interface);
410 default_interface = NULL;
415 default_interface = g_strdup(interface);
417 __sync_synchronize();
418 if (tethering_enabled == 0)
421 enable_nat(interface);
424 static void setup_tun_interface(unsigned int flags, unsigned change,
427 struct connman_private_network *pn = data;
428 unsigned char prefixlen;
429 DBusMessageIter array, dict;
432 DBG("index %d flags %d change %d", pn->index, flags, change);
438 __connman_ipconfig_netmask_prefix_len(PRIVATE_NETWORK_NETMASK);
440 if ((__connman_inet_modify_address(RTM_NEWADDR,
441 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
442 pn->server_ip, pn->peer_ip,
443 prefixlen, NULL)) < 0) {
444 DBG("address setting failed");
448 connman_inet_ifup(pn->index);
450 err = enable_nat(default_interface);
452 connman_error("failed to enable NAT on %s", default_interface);
456 dbus_message_iter_init_append(pn->reply, &array);
458 dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
461 connman_dbus_dict_open(&array, &dict);
463 connman_dbus_dict_append_basic(&dict, "ServerIPv4",
464 DBUS_TYPE_STRING, &pn->server_ip);
465 connman_dbus_dict_append_basic(&dict, "PeerIPv4",
466 DBUS_TYPE_STRING, &pn->peer_ip);
467 connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
468 DBUS_TYPE_STRING, &pn->primary_dns);
469 connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
470 DBUS_TYPE_STRING, &pn->secondary_dns);
472 connman_dbus_dict_close(&array, &dict);
474 dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
476 g_dbus_send_message(connection, pn->reply);
481 pn->reply = __connman_error_failed(pn->msg, -err);
482 g_dbus_send_message(connection, pn->reply);
484 g_hash_table_remove(pn_hash, pn->path);
487 static void remove_private_network(gpointer user_data)
489 struct connman_private_network *pn = user_data;
491 disable_nat(default_interface);
492 connman_rtnl_remove_watch(pn->iface_watch);
495 g_dbus_remove_watch(connection, pn->watch);
501 g_free(pn->interface);
507 static void owner_disconnect(DBusConnection *connection, void *user_data)
509 struct connman_private_network *pn = user_data;
511 DBG("%s died", pn->owner);
515 g_hash_table_remove(pn_hash, pn->path);
518 int __connman_private_network_request(DBusMessage *msg, const char *owner)
520 struct connman_private_network *pn;
525 if (DBUS_TYPE_UNIX_FD < 0)
528 fd = connman_inet_create_tunnel(&iface);
532 path = g_strdup_printf("/tethering/%s", iface);
534 pn = g_hash_table_lookup(pn_hash, path);
542 index = connman_inet_ifindex(iface);
547 DBG("interface %s", iface);
549 err = connman_inet_set_mtu(index, DEFAULT_MTU);
551 pn = g_try_new0(struct connman_private_network, 1);
557 pn->owner = g_strdup(owner);
559 pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
560 owner_disconnect, pn, NULL);
562 pn->reply = dbus_message_new_method_return(pn->msg);
563 if (pn->reply == NULL)
567 pn->interface = iface;
569 pn->server_ip = PRIVATE_NETWORK_IP;
570 pn->peer_ip = PRIVATE_NETWORK_PEER_IP;
571 pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
572 pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
574 pn->iface_watch = connman_rtnl_add_newlink_watch(index,
575 setup_tun_interface, pn);
577 g_hash_table_insert(pn_hash, pn->path, pn);
589 int __connman_private_network_release(const char *path)
591 struct connman_private_network *pn;
593 pn = g_hash_table_lookup(pn_hash, path);
597 g_hash_table_remove(pn_hash, path);
601 int __connman_tethering_init(void)
605 tethering_enabled = 0;
607 connection = connman_dbus_get_connection();
608 if (connection == NULL)
611 pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
612 NULL, remove_private_network);
617 void __connman_tethering_cleanup(void)
621 __sync_synchronize();
622 if (tethering_enabled == 0) {
623 if (tethering_dhcp_server)
624 dhcp_server_stop(tethering_dhcp_server);
625 disable_bridge(BRIDGE_NAME);
626 remove_bridge(BRIDGE_NAME);
629 if (connection == NULL)
632 g_hash_table_destroy(pn_hash);
633 dbus_connection_unref(connection);