Use a proper technology callback to report Tethering status
[platform/upstream/connman.git] / src / tethering.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <sys/ioctl.h>
29 #include <net/if.h>
30 #include <linux/sockios.h>
31
32 #include "connman.h"
33
34 #define BRIDGE_NAME "tether"
35
36 static connman_bool_t tethering_status = FALSE;
37 static const char *default_interface = NULL;
38 static volatile gint tethering_enabled;
39
40 connman_bool_t __connman_tethering_get_status(void)
41 {
42         return tethering_status;
43 }
44
45 static int create_bridge(const char *name)
46 {
47         int sk, err;
48
49         DBG("name %s", name);
50
51         sk = socket(AF_INET, SOCK_STREAM, 0);
52         if (sk < 0)
53                 return -EOPNOTSUPP;
54
55         err = ioctl(sk, SIOCBRADDBR, name);
56
57         close(sk);
58
59         if (err < 0)
60                 return -EOPNOTSUPP;
61
62         return 0;
63 }
64
65 static int remove_bridge(const char *name)
66 {
67         int sk, err;
68
69         DBG("name %s", name);
70
71         sk = socket(AF_INET, SOCK_STREAM, 0);
72         if (sk < 0)
73                 return -EOPNOTSUPP;
74
75         err = ioctl(sk, SIOCBRDELBR, name);
76
77         close(sk);
78
79         if (err < 0)
80                 return -EOPNOTSUPP;
81
82         return 0;
83 }
84
85 static int enable_ip_forward(connman_bool_t enable)
86 {
87
88         FILE *f;
89         int ip_forward = enable ? 1 : 0;
90
91         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
92
93         fprintf(f, "%d", ip_forward);
94
95         fclose(f);
96
97         return 0;
98 }
99
100 static int enable_nat(const char *interface)
101 {
102         int ret;
103
104         if (interface == NULL)
105                 return 0;
106
107         /* Enable IPv4 forwarding */
108         ret = enable_ip_forward(TRUE);
109         if (ret < 0)
110                 return ret;
111
112         /* TODO: Flush nat POSTROUTING chain */
113         /* Enable masquerading */
114         ret = __connman_iptables_command("-t nat -A POSTROUTING -o %s -j MASQUERADE", interface);
115         if (ret < 0)
116                 return ret;
117
118         return __connman_iptables_commit("nat");
119 }
120
121 static void disable_nat(const char *interface)
122 {
123         /* Disable IPv4 forwarding */
124         enable_ip_forward(FALSE);
125
126         /* TODO: Flush nat POSTROUTING chain */
127 }
128
129 void __connman_tethering_set_enabled(void)
130 {
131         if (tethering_status == FALSE)
132                 return;
133
134         DBG("enabled %d", tethering_enabled + 1);
135
136         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
137                 /* TODO Start DHCP server and DNS proxy on the bridge */
138
139                 enable_nat(default_interface);
140                 DBG("tethering started");
141         }
142 }
143
144 void __connman_tethering_set_disabled(void)
145 {
146         if (tethering_status == FALSE)
147                 return;
148
149         DBG("enabled %d", tethering_enabled - 1);
150
151         if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
152                 /* TODO Stop DHCP server and DNS proxy on the bridge */
153
154                 disable_nat(default_interface);
155                 DBG("tethering stopped");
156         }
157 }
158
159 int __connman_tethering_set_status(connman_bool_t status)
160 {
161         if (status == tethering_status)
162                 return -EALREADY;
163
164         if (status == TRUE) {
165                 create_bridge(BRIDGE_NAME);
166                 __connman_technology_enable_tethering(BRIDGE_NAME);
167         } else {
168                 __connman_technology_disable_tethering(BRIDGE_NAME);
169                 remove_bridge(BRIDGE_NAME);
170         }
171
172         tethering_status = status;
173
174         return 0;
175 }
176
177 void __connman_tethering_update_interface(const char *interface)
178 {
179         DBG("interface %s", interface);
180
181         default_interface = interface;
182
183         if (interface == NULL) {
184                 disable_nat(interface);
185
186                 return;
187         }
188
189         if (tethering_status == FALSE ||
190                         !g_atomic_int_get(&tethering_enabled))
191                 return;
192
193         enable_nat(interface);
194 }
195
196 int __connman_tethering_init(void)
197 {
198         DBG("");
199
200         tethering_enabled = 0;
201
202         return 0;
203 }
204
205 void __connman_tethering_cleanup(void)
206 {
207         DBG("");
208
209         if (tethering_status == TRUE)
210                 remove_bridge(BRIDGE_NAME);
211 }