Report error when enabling IP forwarding
[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
90         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
91         if (f == NULL)
92                 return -errno;
93
94         if (enable == TRUE)
95                 fprintf(f, "1");
96         else
97                 fprintf(f, "0");
98
99         fclose(f);
100
101         return 0;
102 }
103
104 static int enable_nat(const char *interface)
105 {
106         int ret;
107
108         if (interface == NULL)
109                 return 0;
110
111         /* Enable IPv4 forwarding */
112         ret = enable_ip_forward(TRUE);
113         if (ret < 0)
114                 return ret;
115
116         /* TODO: Flush nat POSTROUTING chain */
117         /* Enable masquerading */
118         ret = __connman_iptables_command("-t nat -A POSTROUTING -o %s -j MASQUERADE", interface);
119         if (ret < 0)
120                 return ret;
121
122         return __connman_iptables_commit("nat");
123 }
124
125 static void disable_nat(const char *interface)
126 {
127         /* Disable IPv4 forwarding */
128         enable_ip_forward(FALSE);
129
130         /* TODO: Flush nat POSTROUTING chain */
131 }
132
133 void __connman_tethering_set_enabled(void)
134 {
135         if (tethering_status == FALSE)
136                 return;
137
138         DBG("enabled %d", tethering_enabled + 1);
139
140         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
141                 /* TODO Start DHCP server and DNS proxy on the bridge */
142
143                 enable_nat(default_interface);
144                 DBG("tethering started");
145         }
146 }
147
148 void __connman_tethering_set_disabled(void)
149 {
150         if (tethering_status == FALSE)
151                 return;
152
153         DBG("enabled %d", tethering_enabled - 1);
154
155         if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
156                 /* TODO Stop DHCP server and DNS proxy on the bridge */
157
158                 disable_nat(default_interface);
159                 DBG("tethering stopped");
160         }
161 }
162
163 int __connman_tethering_set_status(connman_bool_t status)
164 {
165         if (status == tethering_status)
166                 return -EALREADY;
167
168         if (status == TRUE) {
169                 create_bridge(BRIDGE_NAME);
170                 __connman_technology_enable_tethering(BRIDGE_NAME);
171         } else {
172                 __connman_technology_disable_tethering(BRIDGE_NAME);
173                 remove_bridge(BRIDGE_NAME);
174         }
175
176         tethering_status = status;
177
178         return 0;
179 }
180
181 void __connman_tethering_update_interface(const char *interface)
182 {
183         DBG("interface %s", interface);
184
185         default_interface = interface;
186
187         if (interface == NULL) {
188                 disable_nat(interface);
189
190                 return;
191         }
192
193         if (tethering_status == FALSE ||
194                         !g_atomic_int_get(&tethering_enabled))
195                 return;
196
197         enable_nat(interface);
198 }
199
200 int __connman_tethering_init(void)
201 {
202         DBG("");
203
204         tethering_enabled = 0;
205
206         return 0;
207 }
208
209 void __connman_tethering_cleanup(void)
210 {
211         DBG("");
212
213         if (tethering_status == TRUE)
214                 remove_bridge(BRIDGE_NAME);
215 }