manager: Add hooks for the PrivateNetwork API
[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 <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <sys/ioctl.h>
31 #include <net/if.h>
32 #include <linux/sockios.h>
33
34 #include "connman.h"
35
36 #include <gdhcp/gdhcp.h>
37
38 #include <gdbus.h>
39
40 #define BRIDGE_PROC_DIR "/proc/sys/net/bridge"
41
42 #define BRIDGE_NAME "tether"
43 #define BRIDGE_IP "192.168.218.1"
44 #define BRIDGE_BCAST "192.168.218.255"
45 #define BRIDGE_SUBNET "255.255.255.0"
46 #define BRIDGE_IP_START "192.168.218.100"
47 #define BRIDGE_IP_END "192.168.218.200"
48 #define BRIDGE_DNS "8.8.8.8"
49
50 static char *default_interface = NULL;
51 static volatile gint tethering_enabled;
52 static GDHCPServer *tethering_dhcp_server = NULL;
53 static DBusConnection *connection;
54 static GHashTable *pn_hash;
55
56 struct connman_private_network {
57         char *owner;
58         guint watch;
59 };
60
61 const char *__connman_tethering_get_bridge(void)
62 {
63         struct stat st;
64
65         if (stat(BRIDGE_PROC_DIR, &st) < 0) {
66                 connman_error("Missing support for 802.1d ethernet bridging");
67                 return NULL;
68         }
69
70         return BRIDGE_NAME;
71 }
72
73 static void dhcp_server_debug(const char *str, void *data)
74 {
75         connman_info("%s: %s\n", (const char *) data, str);
76 }
77
78 static void dhcp_server_error(GDHCPServerError error)
79 {
80         switch (error) {
81         case G_DHCP_SERVER_ERROR_NONE:
82                 connman_error("OK");
83                 break;
84         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
85                 connman_error("Interface unavailable");
86                 break;
87         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
88                 connman_error("Interface in use");
89                 break;
90         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
91                 connman_error("Interface down");
92                 break;
93         case G_DHCP_SERVER_ERROR_NOMEM:
94                 connman_error("No memory");
95                 break;
96         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
97                 connman_error("Invalid index");
98                 break;
99         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
100                 connman_error("Invalid option");
101                 break;
102         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
103                 connman_error("Invalid address");
104                 break;
105         }
106 }
107
108 static GDHCPServer *dhcp_server_start(const char *bridge,
109                                 const char *router, const char* subnet,
110                                 const char *start_ip, const char *end_ip,
111                                 unsigned int lease_time, const char *dns)
112 {
113         GDHCPServerError error;
114         GDHCPServer *dhcp_server;
115         int index;
116
117         DBG("");
118
119         index = connman_inet_ifindex(bridge);
120         if (index < 0)
121                 return NULL;
122
123         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
124         if (dhcp_server == NULL) {
125                 dhcp_server_error(error);
126                 return NULL;
127         }
128
129         g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
130
131         g_dhcp_server_set_lease_time(dhcp_server, lease_time);
132         g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, subnet);
133         g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, router);
134         g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, dns);
135         g_dhcp_server_set_ip_range(dhcp_server, start_ip, end_ip);
136
137         g_dhcp_server_start(dhcp_server);
138
139         return dhcp_server;
140 }
141
142 static void dhcp_server_stop(GDHCPServer *server)
143 {
144         if (server == NULL)
145                 return;
146
147         g_dhcp_server_unref(server);
148 }
149
150 static int set_forward_delay(const char *name, unsigned int delay)
151 {
152         FILE *f;
153         char *forward_delay_path;
154
155         forward_delay_path =
156                 g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
157
158         if (forward_delay_path == NULL)
159                 return -ENOMEM;
160
161         f = fopen(forward_delay_path, "r+");
162
163         g_free(forward_delay_path);
164
165         if (f == NULL)
166                 return -errno;
167
168         fprintf(f, "%d", delay);
169
170         fclose(f);
171
172         return 0;
173 }
174
175 static int create_bridge(const char *name)
176 {
177         int sk, err;
178
179         DBG("name %s", name);
180
181         sk = socket(AF_INET, SOCK_STREAM, 0);
182         if (sk < 0)
183                 return -EOPNOTSUPP;
184
185         err = ioctl(sk, SIOCBRADDBR, name);
186
187         if (err < 0)
188                 return -EOPNOTSUPP;
189
190         err = set_forward_delay(name, 0);
191
192         if (err < 0)
193                 ioctl(sk, SIOCBRDELBR, name);
194
195         close(sk);
196
197         return err;
198 }
199
200 static int remove_bridge(const char *name)
201 {
202         int sk, err;
203
204         DBG("name %s", name);
205
206         sk = socket(AF_INET, SOCK_STREAM, 0);
207         if (sk < 0)
208                 return -EOPNOTSUPP;
209
210         err = ioctl(sk, SIOCBRDELBR, name);
211
212         close(sk);
213
214         if (err < 0)
215                 return -EOPNOTSUPP;
216
217         return 0;
218 }
219
220 static int enable_bridge(const char *name)
221 {
222         int err, index;
223
224         index = connman_inet_ifindex(name);
225         if (index < 0)
226                 return index;
227
228         err = __connman_inet_modify_address(RTM_NEWADDR,
229                         NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
230                                         BRIDGE_IP, NULL, 24, BRIDGE_BCAST);
231         if (err < 0)
232                 return err;
233
234         return connman_inet_ifup(index);
235 }
236
237 static int disable_bridge(const char *name)
238 {
239         int index;
240
241         index = connman_inet_ifindex(name);
242         if (index < 0)
243                 return index;
244
245         return connman_inet_ifdown(index);
246 }
247
248 static int enable_ip_forward(connman_bool_t enable)
249 {
250
251         FILE *f;
252
253         f = fopen("/proc/sys/net/ipv4/ip_forward", "r+");
254         if (f == NULL)
255                 return -errno;
256
257         if (enable == TRUE)
258                 fprintf(f, "1");
259         else
260                 fprintf(f, "0");
261
262         fclose(f);
263
264         return 0;
265 }
266
267 static int enable_nat(const char *interface)
268 {
269         int err;
270
271         if (interface == NULL)
272                 return 0;
273
274         /* Enable IPv4 forwarding */
275         err = enable_ip_forward(TRUE);
276         if (err < 0)
277                 return err;
278
279         /* POSTROUTING flush */
280         err = __connman_iptables_command("-t nat -F POSTROUTING");
281         if (err < 0)
282                 return err;
283
284         /* Enable masquerading */
285         err = __connman_iptables_command("-t nat -A POSTROUTING "
286                                         "-o %s -j MASQUERADE", interface);
287         if (err < 0)
288                 return err;
289
290         return __connman_iptables_commit("nat");
291 }
292
293 static void disable_nat(const char *interface)
294 {
295         int err;
296
297         /* Disable IPv4 forwarding */
298         enable_ip_forward(FALSE);
299
300         /* POSTROUTING flush */
301         err = __connman_iptables_command("-t nat -F POSTROUTING");
302         if (err < 0)
303                 return;
304
305         __connman_iptables_commit("nat");
306 }
307
308 void __connman_tethering_set_enabled(void)
309 {
310         int err;
311
312         DBG("enabled %d", tethering_enabled + 1);
313
314         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
315                 err = create_bridge(BRIDGE_NAME);
316                 if (err < 0)
317                         return;
318
319                 err = enable_bridge(BRIDGE_NAME);
320                 if (err < 0) {
321                         remove_bridge(BRIDGE_NAME);
322                         return;
323                 }
324
325                 tethering_dhcp_server =
326                         dhcp_server_start(BRIDGE_NAME,
327                                                 BRIDGE_IP, BRIDGE_SUBNET,
328                                                 BRIDGE_IP_START, BRIDGE_IP_END,
329                                                         24 * 3600, BRIDGE_DNS);
330                 if (tethering_dhcp_server == NULL) {
331                         disable_bridge(BRIDGE_NAME);
332                         remove_bridge(BRIDGE_NAME);
333                         return;
334                 }
335
336                 enable_nat(default_interface);
337
338                 DBG("tethering started");
339         }
340 }
341
342 void __connman_tethering_set_disabled(void)
343 {
344         DBG("enabled %d", tethering_enabled - 1);
345
346         if (g_atomic_int_dec_and_test(&tethering_enabled) == TRUE) {
347                 disable_nat(default_interface);
348
349                 dhcp_server_stop(tethering_dhcp_server);
350
351                 disable_bridge(BRIDGE_NAME);
352
353                 remove_bridge(BRIDGE_NAME);
354
355                 DBG("tethering stopped");
356         }
357 }
358
359 void __connman_tethering_update_interface(const char *interface)
360 {
361         DBG("interface %s", interface);
362
363         g_free(default_interface);
364
365         if (interface == NULL) {
366                 disable_nat(interface);
367                 default_interface = NULL;
368
369                 return;
370         }
371
372         default_interface = g_strdup(interface);
373
374         if (!g_atomic_int_get(&tethering_enabled))
375                 return;
376
377         enable_nat(interface);
378 }
379
380 static void remove_private_network(gpointer user_data)
381 {
382         struct connman_private_network *pn = user_data;
383
384         if (pn->watch > 0) {
385                 g_dbus_remove_watch(connection, pn->watch);
386                 pn->watch = 0;
387         }
388
389         g_free(pn->owner);
390         g_free(pn);
391 }
392
393 static void owner_disconnect(DBusConnection *connection, void *user_data)
394 {
395         struct connman_private_network *pn = user_data;
396
397         DBG("%s died", pn->owner);
398
399         pn->watch = 0;
400
401         g_hash_table_remove(pn_hash, pn->owner);
402 }
403
404 int __connman_private_network_request(const char *owner)
405 {
406         struct connman_private_network *pn;
407
408         pn = g_hash_table_lookup(pn_hash, owner);
409         if (pn != NULL)
410                 return -EEXIST;
411
412         pn = g_try_new0(struct connman_private_network, 1);
413         if (pn == NULL)
414                 return -ENOMEM;
415
416         pn->owner = g_strdup(owner);
417         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
418                                         owner_disconnect, pn, NULL);
419
420         g_hash_table_insert(pn_hash, pn->owner, pn);
421
422         return 0;
423 }
424
425 int __connman_private_network_release(const char *owner)
426 {
427         struct connman_private_network *pn;
428
429         pn = g_hash_table_lookup(pn_hash, owner);
430         if (pn == NULL)
431                 return -EACCES;
432
433         g_hash_table_remove(pn_hash, owner);
434         return 0;
435 }
436
437 int __connman_tethering_init(void)
438 {
439         DBG("");
440
441         tethering_enabled = 0;
442
443         connection = connman_dbus_get_connection();
444         if (connection == NULL)
445                 return -EFAULT;
446
447         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
448                                                 NULL, remove_private_network);
449
450         return 0;
451 }
452
453 void __connman_tethering_cleanup(void)
454 {
455         DBG("");
456
457         if (g_atomic_int_get(&tethering_enabled)) {
458                 if (tethering_dhcp_server)
459                         dhcp_server_stop(tethering_dhcp_server);
460                 disable_bridge(BRIDGE_NAME);
461                 remove_bridge(BRIDGE_NAME);
462         }
463
464         if (connection == NULL)
465                 return;
466
467         g_hash_table_destroy(pn_hash);
468         dbus_connection_unref(connection);
469 }