e028b63a994ce28b09929a24485e69d0e3eb47b6
[platform/upstream/connman.git] / plugins / vpn.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 <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <linux/if_tun.h>
35 #include <net/if.h>
36
37 #include <dbus/dbus.h>
38
39 #include <glib/ghash.h>
40 #include <glib/gprintf.h>
41
42 #include <connman/provider.h>
43 #include <connman/log.h>
44 #include <connman/rtnl.h>
45 #include <connman/task.h>
46 #include <connman/inet.h>
47
48 #include "vpn.h"
49
50 struct vpn_data {
51         struct connman_provider *provider;
52         char *if_name;
53         unsigned flags;
54         unsigned int watch;
55         unsigned int state;
56         struct connman_task *task;
57 };
58
59 struct vpn_driver_data {
60         const char *name;
61         const char *program;
62         struct vpn_driver *vpn_driver;
63         struct connman_provider_driver provider_driver;
64 };
65
66 GHashTable *driver_hash = NULL;
67
68 static int kill_tun(char *tun_name)
69 {
70         struct ifreq ifr;
71         int fd, err;
72
73         memset(&ifr, 0, sizeof(ifr));
74         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
75         sprintf(ifr.ifr_name, "%s", tun_name);
76
77         fd = open("/dev/net/tun", O_RDWR);
78         if (fd < 0) {
79                 err = -errno;
80                 connman_error("Failed to open /dev/net/tun to device %s: %s",
81                               tun_name, strerror(errno));
82                 return err;
83         }
84
85         if (ioctl(fd, TUNSETIFF, (void *)&ifr)) {
86                 err = -errno;
87                 connman_error("Failed to TUNSETIFF for device %s to it: %s",
88                               tun_name, strerror(errno));
89                 close(fd);
90                 return err;
91         }
92
93         if (ioctl(fd, TUNSETPERSIST, 0)) {
94                 err = -errno;
95                 connman_error("Failed to set tun device %s nonpersistent: %s",
96                               tun_name, strerror(errno));
97                 close(fd);
98                 return err;
99         }
100         close(fd);
101         DBG("Killed tun device %s", tun_name);
102         return 0;
103 }
104
105 void vpn_died(struct connman_task *task, int exit_code, void *user_data)
106 {
107         struct connman_provider *provider = user_data;
108         struct vpn_data *data = connman_provider_get_data(provider);
109         int state = VPN_STATE_FAILURE;
110         enum connman_provider_error ret;
111
112         DBG("provider %p data %p", provider, data);
113
114         if (data == NULL)
115                 goto vpn_exit;
116
117         state = data->state;
118
119         kill_tun(data->if_name);
120         connman_provider_set_data(provider, NULL);
121         connman_rtnl_remove_watch(data->watch);
122
123 vpn_exit:
124         if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT) {
125                 const char *name;
126                 struct vpn_driver_data *vpn_data;
127
128                 name = connman_provider_get_driver_name(provider);
129                 vpn_data = g_hash_table_lookup(driver_hash, name);
130                 if (vpn_data != NULL &&
131                                 vpn_data->vpn_driver->error_code != NULL)
132                         ret = vpn_data->vpn_driver->error_code(exit_code);
133                 else
134                         ret = CONNMAN_PROVIDER_ERROR_UNKNOWN;
135
136                 connman_provider_indicate_error(provider, ret);
137         } else
138                 connman_provider_set_state(provider,
139                                                 CONNMAN_PROVIDER_STATE_IDLE);
140
141         connman_provider_set_index(provider, -1);
142         connman_provider_unref(data->provider);
143         g_free(data);
144
145         connman_task_destroy(task);
146 }
147
148 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
149 {
150         struct connman_provider *provider = user_data;
151         struct vpn_data *data = connman_provider_get_data(provider);
152
153         if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
154                 if (flags & IFF_UP) {
155                         data->state = VPN_STATE_READY;
156                         connman_provider_set_state(provider,
157                                         CONNMAN_PROVIDER_STATE_READY);
158                 }
159         }
160         data->flags = flags;
161 }
162
163 static void vpn_notify(struct connman_task *task,
164                         DBusMessage *msg, void *user_data)
165 {
166         struct connman_provider *provider = user_data;
167         struct vpn_data *data;
168         struct vpn_driver_data *vpn_driver_data;
169         const char *name;
170         int state, index;
171
172         data = connman_provider_get_data(provider);
173
174         name = connman_provider_get_driver_name(provider);
175         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
176         if (vpn_driver_data == NULL)
177                 return;
178
179         state = vpn_driver_data->vpn_driver->notify(msg, provider);
180         switch (state) {
181         case VPN_STATE_CONNECT:
182         case VPN_STATE_READY:
183                 index = connman_provider_get_index(provider);
184                 data->watch = connman_rtnl_add_newlink_watch(index,
185                                                      vpn_newlink, provider);
186                 connman_inet_ifup(index);
187                 break;
188
189         case VPN_STATE_UNKNOWN:
190         case VPN_STATE_IDLE:
191         case VPN_STATE_DISCONNECT:
192         case VPN_STATE_FAILURE:
193                 connman_provider_set_state(provider,
194                                         CONNMAN_PROVIDER_STATE_DISCONNECT);
195                 break;
196         }
197 }
198
199 static int vpn_connect(struct connman_provider *provider)
200 {
201         struct vpn_data *data = connman_provider_get_data(provider);
202         struct vpn_driver_data *vpn_driver_data;
203         struct ifreq ifr;
204         const char *name;
205         int i, fd, index;
206         int ret = 0;
207
208         if (data != NULL)
209                 return -EISCONN;
210
211         data = g_try_new0(struct vpn_data, 1);
212         if (data == NULL)
213                 return -ENOMEM;
214
215         data->provider = connman_provider_ref(provider);
216         data->watch = 0;
217         data->flags = 0;
218         data->task = NULL;
219         data->state = VPN_STATE_IDLE;
220
221         connman_provider_set_data(provider, data);
222
223         name = connman_provider_get_driver_name(provider);
224         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
225
226         fd = open("/dev/net/tun", O_RDWR);
227         if (fd < 0) {
228                 i = -errno;
229                 connman_error("Failed to open /dev/net/tun: %s",
230                               strerror(errno));
231                 ret = i;
232                 goto exist_err;
233         }
234
235         memset(&ifr, 0, sizeof(ifr));
236         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
237
238         for (i = 0; i < 256; i++) {
239                 sprintf(ifr.ifr_name, "vpn%d", i);
240
241                 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
242                         break;
243         }
244
245         if (i == 256) {
246                 connman_error("Failed to find available tun device");
247                 close(fd);
248                 ret = -ENODEV;
249                 goto exist_err;
250         }
251
252         data->if_name = (char *)g_strdup(ifr.ifr_name);
253         if (data->if_name == NULL) {
254                 connman_error("Failed to allocate memory");
255                 close(fd);
256                 ret = -ENOMEM;
257                 goto exist_err;
258         }
259
260         if (ioctl(fd, TUNSETPERSIST, 1)) {
261                 i = -errno;
262                 connman_error("Failed to set tun persistent: %s",
263                               strerror(errno));
264                 close(fd);
265                 ret = i;
266                 goto exist_err;
267         }
268
269         close(fd);
270
271         index = connman_inet_ifindex(data->if_name);
272         if (index < 0) {
273                 connman_error("Failed to get tun ifindex");
274                 kill_tun(data->if_name);
275                 ret = -EIO;
276                 goto exist_err;
277         }
278         connman_provider_set_index(provider, index);
279
280         data->task = connman_task_create(vpn_driver_data->program);
281
282         if (data->task == NULL) {
283                 ret = -ENOMEM;
284                 kill_tun(data->if_name);
285                 goto exist_err;
286         }
287
288         if (connman_task_set_notify(data->task, "notify",
289                                         vpn_notify, provider)) {
290                 ret = -ENOMEM;
291                 kill_tun(data->if_name);
292                 connman_task_destroy(data->task);
293                 data->task = NULL;
294                 goto exist_err;
295         }
296
297         ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
298                                                         data->if_name);
299         if (ret < 0) {
300                 kill_tun(data->if_name);
301                 connman_task_destroy(data->task);
302                 data->task = NULL;
303                 goto exist_err;
304         }
305
306         DBG("%s started with dev %s",
307                 vpn_driver_data->provider_driver.name, data->if_name);
308
309         data->state = VPN_STATE_CONNECT;
310
311         return -EINPROGRESS;
312
313 exist_err:
314         connman_provider_set_index(provider, -1);
315         connman_provider_set_data(provider, NULL);
316         connman_provider_unref(data->provider);
317         g_free(data);
318
319         return ret;
320 }
321
322 static int vpn_probe(struct connman_provider *provider)
323 {
324         return 0;
325 }
326
327 static int vpn_disconnect(struct connman_provider *provider)
328 {
329         struct vpn_data *data = connman_provider_get_data(provider);
330         struct vpn_driver_data *vpn_driver_data;
331         const char *name;
332
333         DBG("disconnect provider %p:", provider);
334
335         if (data == NULL)
336                 return 0;
337
338         name = connman_provider_get_driver_name(provider);
339         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
340         if (vpn_driver_data->vpn_driver->disconnect)
341                 vpn_driver_data->vpn_driver->disconnect();
342
343         if (data->watch != 0)
344                 connman_rtnl_remove_watch(data->watch);
345
346         data->watch = 0;
347         data->state = VPN_STATE_DISCONNECT;
348         connman_task_stop(data->task);
349
350         return 0;
351 }
352
353 static int vpn_remove(struct connman_provider *provider)
354 {
355         struct vpn_data *data;
356
357         data = connman_provider_get_data(provider);
358         connman_provider_set_data(provider, NULL);
359         if (data == NULL)
360                 return 0;
361
362         if (data->watch != 0)
363                 connman_rtnl_remove_watch(data->watch);
364         data->watch = 0;
365         connman_task_stop(data->task);
366
367         g_usleep(G_USEC_PER_SEC);
368         kill_tun(data->if_name);
369         return 0;
370 }
371
372 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
373                         const char *program)
374 {
375         struct vpn_driver_data *data;
376
377         data = g_try_new0(struct vpn_driver_data, 1);
378         if (data == NULL)
379                 return -ENOMEM;
380
381         data->name = name;
382         data->program = program;
383
384         data->vpn_driver = vpn_driver;
385
386         data->provider_driver.name = name;
387         data->provider_driver.disconnect = vpn_disconnect;
388         data->provider_driver.connect = vpn_connect;
389         data->provider_driver.probe = vpn_probe;
390         data->provider_driver.remove = vpn_remove;
391
392         if (driver_hash == NULL) {
393                 driver_hash = g_hash_table_new_full(g_str_hash,
394                                                         g_str_equal,
395                                                         NULL, g_free);
396         }
397
398         g_hash_table_insert(driver_hash, (char *)name, data);
399
400         connman_provider_driver_register(&data->provider_driver);
401
402         return 0;
403 }
404
405 void vpn_unregister(const char *name)
406 {
407         struct vpn_driver_data *data;
408
409         data = g_hash_table_lookup(driver_hash, name);
410         if (data == NULL)
411                 return;
412
413         connman_provider_driver_unregister(&data->provider_driver);
414
415         g_hash_table_remove(driver_hash, name);
416
417         if (g_hash_table_size(driver_hash) == 0)
418                 g_hash_table_destroy(driver_hash);
419 }