task: Pass exit code to the exit callback
[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 = data->state;
110
111         DBG("provider %p data %p", provider, data);
112
113         if (!data)
114                 goto vpn_exit;
115
116         kill_tun(data->if_name);
117         connman_provider_set_data(provider, NULL);
118         connman_rtnl_remove_watch(data->watch);
119
120 vpn_exit:
121         if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT)
122                 connman_provider_set_state(provider,
123                                                 CONNMAN_PROVIDER_STATE_FAILURE);
124         else
125                 connman_provider_set_state(provider,
126                                                 CONNMAN_PROVIDER_STATE_IDLE);
127
128         connman_provider_set_index(provider, -1);
129         connman_provider_unref(data->provider);
130         g_free(data);
131
132         connman_task_destroy(task);
133 }
134
135 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
136 {
137         struct connman_provider *provider = user_data;
138         struct vpn_data *data = connman_provider_get_data(provider);
139
140         if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
141                 if (flags & IFF_UP) {
142                         data->state = VPN_STATE_READY;
143                         connman_provider_set_state(provider,
144                                         CONNMAN_PROVIDER_STATE_READY);
145                 }
146         }
147         data->flags = flags;
148 }
149
150 static void vpn_notify(struct connman_task *task,
151                         DBusMessage *msg, void *user_data)
152 {
153         struct connman_provider *provider = user_data;
154         struct vpn_data *data;
155         struct vpn_driver_data *vpn_driver_data;
156         const char *name;
157         int state, index;
158
159         data = connman_provider_get_data(provider);
160
161         name = connman_provider_get_driver_name(provider);
162         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
163         if (vpn_driver_data == NULL)
164                 return;
165
166         state = vpn_driver_data->vpn_driver->notify(msg, provider);
167         switch (state) {
168         case VPN_STATE_CONNECT:
169         case VPN_STATE_READY:
170                 index = connman_provider_get_index(provider);
171                 data->watch = connman_rtnl_add_newlink_watch(index,
172                                                      vpn_newlink, provider);
173                 connman_inet_ifup(index);
174                 break;
175
176         case VPN_STATE_UNKNOWN:
177         case VPN_STATE_IDLE:
178         case VPN_STATE_DISCONNECT:
179         case VPN_STATE_FAILURE:
180                 connman_provider_set_state(provider,
181                                         CONNMAN_PROVIDER_STATE_DISCONNECT);
182                 break;
183         }
184 }
185
186 static int vpn_connect(struct connman_provider *provider)
187 {
188         struct vpn_data *data = connman_provider_get_data(provider);
189         struct vpn_driver_data *vpn_driver_data;
190         struct ifreq ifr;
191         const char *name;
192         int i, fd, index;
193         int ret = 0;
194
195         if (data != NULL)
196                 return -EISCONN;
197
198         data = g_try_new0(struct vpn_data, 1);
199         if (data == NULL)
200                 return -ENOMEM;
201
202         data->provider = connman_provider_ref(provider);
203         data->watch = 0;
204         data->flags = 0;
205         data->task = NULL;
206         data->state = VPN_STATE_IDLE;
207
208         connman_provider_set_data(provider, data);
209
210         name = connman_provider_get_driver_name(provider);
211         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
212
213         fd = open("/dev/net/tun", O_RDWR);
214         if (fd < 0) {
215                 i = -errno;
216                 connman_error("Failed to open /dev/net/tun: %s",
217                               strerror(errno));
218                 ret = i;
219                 goto exist_err;
220         }
221
222         memset(&ifr, 0, sizeof(ifr));
223         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
224
225         for (i = 0; i < 256; i++) {
226                 sprintf(ifr.ifr_name, "vpn%d", i);
227
228                 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
229                         break;
230         }
231
232         if (i == 256) {
233                 connman_error("Failed to find available tun device");
234                 close(fd);
235                 ret = -ENODEV;
236                 goto exist_err;
237         }
238
239         data->if_name = (char *)g_strdup(ifr.ifr_name);
240         if (!data->if_name) {
241                 ret = -ENOMEM;
242                 goto exist_err;
243         }
244
245         if (ioctl(fd, TUNSETPERSIST, 1)) {
246                 i = -errno;
247                 connman_error("Failed to set tun persistent: %s",
248                               strerror(errno));
249                 close(fd);
250                 ret = i;
251                 goto exist_err;
252         }
253
254         close(fd);
255
256         index = connman_inet_ifindex(data->if_name);
257         if (index < 0) {
258                 connman_error("Failed to get tun ifindex");
259                 kill_tun(data->if_name);
260                 ret = -EIO;
261                 goto exist_err;
262         }
263         connman_provider_set_index(provider, index);
264
265         data->task = connman_task_create(vpn_driver_data->program);
266
267         if (data->task == NULL) {
268                 ret = -ENOMEM;
269                 kill_tun(data->if_name);
270                 goto exist_err;
271         }
272
273         if (connman_task_set_notify(data->task, "notify",
274                                         vpn_notify, provider)) {
275                 ret = -ENOMEM;
276                 kill_tun(data->if_name);
277                 connman_task_destroy(data->task);
278                 data->task = NULL;
279                 goto exist_err;
280         }
281
282         ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
283                                                         data->if_name);
284         if (ret < 0) {
285                 kill_tun(data->if_name);
286                 connman_task_destroy(data->task);
287                 data->task = NULL;
288                 goto exist_err;
289         }
290
291         DBG("%s started with dev %s",
292                 vpn_driver_data->provider_driver.name, data->if_name);
293
294         data->state = VPN_STATE_CONNECT;
295
296         return -EINPROGRESS;
297
298 exist_err:
299         connman_provider_set_index(provider, -1);
300         connman_provider_set_data(provider, NULL);
301         connman_provider_unref(data->provider);
302         g_free(data);
303
304         return ret;
305 }
306
307 static int vpn_probe(struct connman_provider *provider)
308 {
309         return 0;
310 }
311
312 static int vpn_disconnect(struct connman_provider *provider)
313 {
314         struct vpn_data *data = connman_provider_get_data(provider);
315         struct vpn_driver_data *vpn_driver_data;
316         const char *name;
317
318         DBG("disconnect provider %p:", provider);
319
320         if (data == NULL)
321                 return 0;
322
323         name = connman_provider_get_driver_name(provider);
324         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
325         if (vpn_driver_data->vpn_driver->disconnect)
326                 vpn_driver_data->vpn_driver->disconnect();
327
328         if (data->watch != 0)
329                 connman_rtnl_remove_watch(data->watch);
330
331         data->watch = 0;
332         data->state = VPN_STATE_DISCONNECT;
333         connman_task_stop(data->task);
334
335         return 0;
336 }
337
338 static int vpn_remove(struct connman_provider *provider)
339 {
340         struct vpn_data *data;
341
342         data = connman_provider_get_data(provider);
343         connman_provider_set_data(provider, NULL);
344         if (data == NULL)
345                 return 0;
346
347         if (data->watch != 0)
348                 connman_rtnl_remove_watch(data->watch);
349         data->watch = 0;
350         connman_task_stop(data->task);
351
352         g_usleep(G_USEC_PER_SEC);
353         kill_tun(data->if_name);
354         return 0;
355 }
356
357 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
358                         const char *program)
359 {
360         struct vpn_driver_data *data;
361
362         data = g_try_new0(struct vpn_driver_data, 1);
363         if (data == NULL)
364                 return -ENOMEM;
365
366         data->name = name;
367         data->program = program;
368
369         data->vpn_driver = vpn_driver;
370
371         data->provider_driver.name = name;
372         data->provider_driver.disconnect = vpn_disconnect;
373         data->provider_driver.connect = vpn_connect;
374         data->provider_driver.probe = vpn_probe;
375         data->provider_driver.remove = vpn_remove;
376
377         if (driver_hash == NULL) {
378                 driver_hash = g_hash_table_new_full(g_str_hash,
379                                                         g_str_equal,
380                                                         NULL, g_free);
381         }
382
383         g_hash_table_insert(driver_hash, (char *)name, data);
384
385         connman_provider_driver_register(&data->provider_driver);
386
387         return 0;
388 }
389
390 void vpn_unregister(const char *name)
391 {
392         struct vpn_driver_data *data;
393
394         data = g_hash_table_lookup(driver_hash, name);
395         if (data == NULL)
396                 return;
397
398         connman_provider_driver_unregister(&data->provider_driver);
399
400         g_hash_table_remove(driver_hash, name);
401
402         if (g_hash_table_size(driver_hash) == 0)
403                 g_hash_table_destroy(driver_hash);
404 }