tethering: Fix build with kernel headers 3.8+
[platform/upstream/connman.git] / src / tethering.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  ProFUSION embedded systems
7  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <sys/ioctl.h>
33 #include <net/if.h>
34 #include <linux/sockios.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <linux/if_tun.h>
38 #include <netinet/in.h>
39 #include <linux/if_bridge.h>
40
41 #include "connman.h"
42
43 #include <gdhcp/gdhcp.h>
44
45 #include <gdbus.h>
46
47 #ifndef DBUS_TYPE_UNIX_FD
48 #define DBUS_TYPE_UNIX_FD -1
49 #endif
50
51 #define BRIDGE_NAME "tether"
52 #define BRIDGE_DNS "8.8.8.8"
53
54 #define DEFAULT_MTU     1500
55
56 #define PRIVATE_NETWORK_PRIMARY_DNS BRIDGE_DNS
57 #define PRIVATE_NETWORK_SECONDARY_DNS "8.8.4.4"
58
59 static volatile int tethering_enabled;
60 static GDHCPServer *tethering_dhcp_server = NULL;
61 static struct connman_ippool *dhcp_ippool = NULL;
62 static DBusConnection *connection;
63 static GHashTable *pn_hash;
64
65 struct connman_private_network {
66         char *owner;
67         char *path;
68         guint watch;
69         DBusMessage *msg;
70         DBusMessage *reply;
71         int fd;
72         char *interface;
73         int index;
74         guint iface_watch;
75         struct connman_ippool *pool;
76         const char *primary_dns;
77         const char *secondary_dns;
78 };
79
80 const char *__connman_tethering_get_bridge(void)
81 {
82         int sk, err;
83         unsigned long args[3];
84
85         sk = socket(AF_INET, SOCK_STREAM, 0);
86         if (sk < 0)
87                 return NULL;
88
89         args[0] = BRCTL_GET_VERSION;
90         args[1] = args[2] = 0;
91         err = ioctl(sk, SIOCGIFBR, &args);
92         close(sk);
93         if (err == -1) {
94                 connman_error("Missing support for 802.1d ethernet bridging");
95                 return NULL;
96         }
97
98         return BRIDGE_NAME;
99 }
100
101 static void dhcp_server_debug(const char *str, void *data)
102 {
103         connman_info("%s: %s\n", (const char *) data, str);
104 }
105
106 static void dhcp_server_error(GDHCPServerError error)
107 {
108         switch (error) {
109         case G_DHCP_SERVER_ERROR_NONE:
110                 connman_error("OK");
111                 break;
112         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
113                 connman_error("Interface unavailable");
114                 break;
115         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
116                 connman_error("Interface in use");
117                 break;
118         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
119                 connman_error("Interface down");
120                 break;
121         case G_DHCP_SERVER_ERROR_NOMEM:
122                 connman_error("No memory");
123                 break;
124         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
125                 connman_error("Invalid index");
126                 break;
127         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
128                 connman_error("Invalid option");
129                 break;
130         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
131                 connman_error("Invalid address");
132                 break;
133         }
134 }
135
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)
140 {
141         GDHCPServerError error;
142         GDHCPServer *dhcp_server;
143         int index;
144
145         DBG("");
146
147         index = connman_inet_ifindex(bridge);
148         if (index < 0)
149                 return NULL;
150
151         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
152         if (dhcp_server == NULL) {
153                 dhcp_server_error(error);
154                 return NULL;
155         }
156
157         g_dhcp_server_set_debug(dhcp_server, dhcp_server_debug, "DHCP server");
158
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);
164
165         g_dhcp_server_start(dhcp_server);
166
167         return dhcp_server;
168 }
169
170 static void dhcp_server_stop(GDHCPServer *server)
171 {
172         if (server == NULL)
173                 return;
174
175         g_dhcp_server_unref(server);
176 }
177
178 static void tethering_restart(struct connman_ippool *pool, void *user_data)
179 {
180         __connman_tethering_set_disabled();
181         __connman_tethering_set_enabled();
182 }
183
184 void __connman_tethering_set_enabled(void)
185 {
186         int index;
187         int err;
188         const char *gateway;
189         const char *broadcast;
190         const char *subnet_mask;
191         const char *start_ip;
192         const char *end_ip;
193         const char *dns;
194         unsigned char prefixlen;
195
196         DBG("enabled %d", tethering_enabled + 1);
197
198         if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
199                 return;
200
201         err = __connman_bridge_create(BRIDGE_NAME);
202         if (err < 0) {
203                 __sync_fetch_and_sub(&tethering_enabled, 1);
204                 return;
205         }
206
207         index = connman_inet_ifindex(BRIDGE_NAME);
208         dhcp_ippool = __connman_ippool_create(index, 2, 252,
209                                                 tethering_restart, NULL);
210         if (dhcp_ippool == NULL) {
211                 connman_error("Fail to create IP pool");
212                 __connman_bridge_remove(BRIDGE_NAME);
213                 __sync_fetch_and_sub(&tethering_enabled, 1);
214                 return;
215         }
216
217         gateway = __connman_ippool_get_gateway(dhcp_ippool);
218         broadcast = __connman_ippool_get_broadcast(dhcp_ippool);
219         subnet_mask = __connman_ippool_get_subnet_mask(dhcp_ippool);
220         start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
221         end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
222
223         err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
224         if (err < 0 && err != -EALREADY) {
225                 __connman_ippool_unref(dhcp_ippool);
226                 __connman_bridge_remove(BRIDGE_NAME);
227                 __sync_fetch_and_sub(&tethering_enabled, 1);
228                 return;
229         }
230
231         dns = gateway;
232         if (__connman_dnsproxy_add_listener(index) < 0) {
233                 connman_error("Can't add listener %s to DNS proxy",
234                                                                 BRIDGE_NAME);
235                 dns = BRIDGE_DNS;
236         }
237
238         tethering_dhcp_server = dhcp_server_start(BRIDGE_NAME,
239                                                 gateway, subnet_mask,
240                                                 start_ip, end_ip,
241                                                 24 * 3600, dns);
242         if (tethering_dhcp_server == NULL) {
243                 __connman_bridge_disable(BRIDGE_NAME);
244                 __connman_ippool_unref(dhcp_ippool);
245                 __connman_bridge_remove(BRIDGE_NAME);
246                 __sync_fetch_and_sub(&tethering_enabled, 1);
247                 return;
248         }
249
250         prefixlen =
251                 __connman_ipaddress_netmask_prefix_len(subnet_mask);
252         __connman_nat_enable(BRIDGE_NAME, start_ip, prefixlen);
253
254         DBG("tethering started");
255 }
256
257 void __connman_tethering_set_disabled(void)
258 {
259         int index;
260
261         DBG("enabled %d", tethering_enabled - 1);
262
263         index = connman_inet_ifindex(BRIDGE_NAME);
264         __connman_dnsproxy_remove_listener(index);
265
266         if (__sync_fetch_and_sub(&tethering_enabled, 1) != 1)
267                 return;
268
269         __connman_nat_disable(BRIDGE_NAME);
270
271         dhcp_server_stop(tethering_dhcp_server);
272
273         tethering_dhcp_server = NULL;
274
275         __connman_bridge_disable(BRIDGE_NAME);
276
277         __connman_ippool_unref(dhcp_ippool);
278
279         __connman_bridge_remove(BRIDGE_NAME);
280
281         DBG("tethering stopped");
282 }
283
284 static void setup_tun_interface(unsigned int flags, unsigned change,
285                 void *data)
286 {
287         struct connman_private_network *pn = data;
288         unsigned char prefixlen;
289         DBusMessageIter array, dict;
290         const char *server_ip;
291         const char *peer_ip;
292         const char *subnet_mask;
293         int err;
294
295         DBG("index %d flags %d change %d", pn->index,  flags, change);
296
297         if (flags & IFF_UP)
298                 return;
299
300         subnet_mask = __connman_ippool_get_subnet_mask(pn->pool);
301         server_ip = __connman_ippool_get_start_ip(pn->pool);
302         peer_ip = __connman_ippool_get_end_ip(pn->pool);
303         prefixlen =
304                 __connman_ipaddress_netmask_prefix_len(subnet_mask);
305
306         if ((__connman_inet_modify_address(RTM_NEWADDR,
307                                 NLM_F_REPLACE | NLM_F_ACK, pn->index, AF_INET,
308                                 server_ip, peer_ip, prefixlen, NULL)) < 0) {
309                 DBG("address setting failed");
310                 return;
311         }
312
313         connman_inet_ifup(pn->index);
314
315         err = __connman_nat_enable(BRIDGE_NAME, server_ip, prefixlen);
316         if (err < 0) {
317                 connman_error("failed to enable NAT");
318                 goto error;
319         }
320
321         dbus_message_iter_init_append(pn->reply, &array);
322
323         dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
324                                                 &pn->path);
325
326         connman_dbus_dict_open(&array, &dict);
327
328         connman_dbus_dict_append_basic(&dict, "ServerIPv4",
329                                         DBUS_TYPE_STRING, &server_ip);
330         connman_dbus_dict_append_basic(&dict, "PeerIPv4",
331                                         DBUS_TYPE_STRING, &peer_ip);
332         connman_dbus_dict_append_basic(&dict, "PrimaryDNS",
333                                         DBUS_TYPE_STRING, &pn->primary_dns);
334         connman_dbus_dict_append_basic(&dict, "SecondaryDNS",
335                                         DBUS_TYPE_STRING, &pn->secondary_dns);
336
337         connman_dbus_dict_close(&array, &dict);
338
339         dbus_message_iter_append_basic(&array, DBUS_TYPE_UNIX_FD, &pn->fd);
340
341         g_dbus_send_message(connection, pn->reply);
342
343         return;
344
345 error:
346         pn->reply = __connman_error_failed(pn->msg, -err);
347         g_dbus_send_message(connection, pn->reply);
348
349         g_hash_table_remove(pn_hash, pn->path);
350 }
351
352 static void remove_private_network(gpointer user_data)
353 {
354         struct connman_private_network *pn = user_data;
355
356         __connman_nat_disable(BRIDGE_NAME);
357         connman_rtnl_remove_watch(pn->iface_watch);
358         __connman_ippool_unref(pn->pool);
359
360         if (pn->watch > 0) {
361                 g_dbus_remove_watch(connection, pn->watch);
362                 pn->watch = 0;
363         }
364
365         close(pn->fd);
366
367         g_free(pn->interface);
368         g_free(pn->owner);
369         g_free(pn->path);
370         g_free(pn);
371 }
372
373 static void owner_disconnect(DBusConnection *conn, void *user_data)
374 {
375         struct connman_private_network *pn = user_data;
376
377         DBG("%s died", pn->owner);
378
379         pn->watch = 0;
380
381         g_hash_table_remove(pn_hash, pn->path);
382 }
383
384 static void ippool_disconnect(struct connman_ippool *pool, void *user_data)
385 {
386         struct connman_private_network *pn = user_data;
387
388         DBG("block used externally");
389
390         g_hash_table_remove(pn_hash, pn->path);
391 }
392
393 int __connman_private_network_request(DBusMessage *msg, const char *owner)
394 {
395         struct connman_private_network *pn;
396         char *iface = NULL;
397         char *path = NULL;
398         int index, fd, err;
399
400         if (DBUS_TYPE_UNIX_FD < 0)
401                 return -EINVAL;
402
403         fd = connman_inet_create_tunnel(&iface);
404         if (fd < 0)
405                 return fd;
406
407         path = g_strdup_printf("/tethering/%s", iface);
408
409         pn = g_hash_table_lookup(pn_hash, path);
410         if (pn) {
411                 g_free(path);
412                 g_free(iface);
413                 close(fd);
414                 return -EEXIST;
415         }
416
417         index = connman_inet_ifindex(iface);
418         if (index < 0) {
419                 err = -ENODEV;
420                 goto error;
421         }
422         DBG("interface %s", iface);
423
424         err = connman_inet_set_mtu(index, DEFAULT_MTU);
425
426         pn = g_try_new0(struct connman_private_network, 1);
427         if (pn == NULL) {
428                 err = -ENOMEM;
429                 goto error;
430         }
431
432         pn->owner = g_strdup(owner);
433         pn->path = path;
434         pn->watch = g_dbus_add_disconnect_watch(connection, pn->owner,
435                                         owner_disconnect, pn, NULL);
436         pn->msg = msg;
437         pn->reply = dbus_message_new_method_return(pn->msg);
438         if (pn->reply == NULL)
439                 goto error;
440
441         pn->fd = fd;
442         pn->interface = iface;
443         pn->index = index;
444         pn->pool = __connman_ippool_create(pn->index, 1, 1, ippool_disconnect, pn);
445         if (pn->pool == NULL) {
446                 errno = -ENOMEM;
447                 goto error;
448         }
449
450         pn->primary_dns = PRIVATE_NETWORK_PRIMARY_DNS;
451         pn->secondary_dns = PRIVATE_NETWORK_SECONDARY_DNS;
452
453         pn->iface_watch = connman_rtnl_add_newlink_watch(index,
454                                                 setup_tun_interface, pn);
455
456         g_hash_table_insert(pn_hash, pn->path, pn);
457
458         return 0;
459
460 error:
461         close(fd);
462         g_free(iface);
463         g_free(path);
464         g_free(pn);
465         return err;
466 }
467
468 int __connman_private_network_release(const char *path)
469 {
470         struct connman_private_network *pn;
471
472         pn = g_hash_table_lookup(pn_hash, path);
473         if (pn == NULL)
474                 return -EACCES;
475
476         g_hash_table_remove(pn_hash, path);
477         return 0;
478 }
479
480 int __connman_tethering_init(void)
481 {
482         DBG("");
483
484         tethering_enabled = 0;
485
486         connection = connman_dbus_get_connection();
487         if (connection == NULL)
488                 return -EFAULT;
489
490         pn_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
491                                                 NULL, remove_private_network);
492
493         return 0;
494 }
495
496 void __connman_tethering_cleanup(void)
497 {
498         DBG("");
499
500         __sync_synchronize();
501         if (tethering_enabled == 0) {
502                 if (tethering_dhcp_server)
503                         dhcp_server_stop(tethering_dhcp_server);
504                 __connman_bridge_disable(BRIDGE_NAME);
505                 __connman_bridge_remove(BRIDGE_NAME);
506                 __connman_nat_disable(BRIDGE_NAME);
507         }
508
509         if (connection == NULL)
510                 return;
511
512         g_hash_table_destroy(pn_hash);
513         dbus_connection_unref(connection);
514 }