7a35f7a428ab27ee429b0fb4c53f811f3bf3f94f
[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 #define _GNU_SOURCE
27 #include <string.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <linux/if_tun.h>
36 #include <net/if.h>
37
38 #include <dbus/dbus.h>
39
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 stop_vpn(struct connman_provider *provider)
69 {
70         struct vpn_data *data = connman_provider_get_data(provider);
71         struct vpn_driver_data *vpn_driver_data;
72         const char *name;
73         struct ifreq ifr;
74         int fd, err;
75
76         if (data == NULL)
77                 return -EINVAL;
78
79         name = connman_provider_get_driver_name(provider);
80         if (name == NULL)
81                 return -EINVAL;
82
83         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
84
85         if (vpn_driver_data != NULL && vpn_driver_data->vpn_driver != NULL &&
86                         vpn_driver_data->vpn_driver->flags == VPN_FLAG_NO_TUN)
87                 return 0;
88
89         memset(&ifr, 0, sizeof(ifr));
90         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
91         sprintf(ifr.ifr_name, "%s", data->if_name);
92
93         fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
94         if (fd < 0) {
95                 err = -errno;
96                 connman_error("Failed to open /dev/net/tun to device %s: %s",
97                               data->if_name, strerror(errno));
98                 return err;
99         }
100
101         if (ioctl(fd, TUNSETIFF, (void *)&ifr)) {
102                 err = -errno;
103                 connman_error("Failed to TUNSETIFF for device %s to it: %s",
104                               data->if_name, strerror(errno));
105                 close(fd);
106                 return err;
107         }
108
109         if (ioctl(fd, TUNSETPERSIST, 0)) {
110                 err = -errno;
111                 connman_error("Failed to set tun device %s nonpersistent: %s",
112                               data->if_name, strerror(errno));
113                 close(fd);
114                 return err;
115         }
116         close(fd);
117         DBG("Killed tun device %s", data->if_name);
118         return 0;
119 }
120
121 void vpn_died(struct connman_task *task, int exit_code, void *user_data)
122 {
123         struct connman_provider *provider = user_data;
124         struct vpn_data *data = connman_provider_get_data(provider);
125         int state = VPN_STATE_FAILURE;
126         enum connman_provider_error ret;
127
128         DBG("provider %p data %p", provider, data);
129
130         if (data == NULL)
131                 goto vpn_exit;
132
133         state = data->state;
134
135         stop_vpn(provider);
136         connman_provider_set_data(provider, NULL);
137         connman_provider_unref(provider);
138         connman_rtnl_remove_watch(data->watch);
139
140 vpn_exit:
141         if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT) {
142                 const char *name;
143                 struct vpn_driver_data *vpn_data = NULL;
144
145                 name = connman_provider_get_driver_name(provider);
146                 if (name != NULL)
147                         vpn_data = g_hash_table_lookup(driver_hash, name);
148
149                 if (vpn_data != NULL &&
150                                 vpn_data->vpn_driver->error_code != NULL)
151                         ret = vpn_data->vpn_driver->error_code(exit_code);
152                 else
153                         ret = CONNMAN_PROVIDER_ERROR_UNKNOWN;
154
155                 connman_provider_indicate_error(provider, ret);
156         } else
157                 connman_provider_set_state(provider,
158                                                 CONNMAN_PROVIDER_STATE_IDLE);
159
160         connman_provider_set_index(provider, -1);
161         connman_provider_unref(data->provider);
162
163         g_free(data->if_name);
164         g_free(data);
165
166         connman_task_destroy(task);
167 }
168
169 int vpn_set_ifname(struct connman_provider *provider, const char *ifname)
170 {
171         struct vpn_data *data = connman_provider_get_data(provider);
172         int index;
173
174         if (ifname == NULL || data == NULL)
175                 return  -EIO;
176
177         index = connman_inet_ifindex(ifname);
178         if (index < 0)
179                 return  -EIO;
180
181         if (data->if_name != NULL)
182                 g_free(data->if_name);
183
184         data->if_name = (char *)g_strdup(ifname);
185         connman_provider_set_index(provider, index);
186
187         return 0;
188 }
189
190 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
191 {
192         struct connman_provider *provider = user_data;
193         struct vpn_data *data = connman_provider_get_data(provider);
194
195         if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
196                 if (flags & IFF_UP) {
197                         data->state = VPN_STATE_READY;
198                         connman_provider_set_state(provider,
199                                         CONNMAN_PROVIDER_STATE_READY);
200                 }
201         }
202         data->flags = flags;
203 }
204
205 static DBusMessage *vpn_notify(struct connman_task *task,
206                         DBusMessage *msg, void *user_data)
207 {
208         struct connman_provider *provider = user_data;
209         struct vpn_data *data;
210         struct vpn_driver_data *vpn_driver_data;
211         const char *name;
212         int state, index;
213
214         data = connman_provider_get_data(provider);
215
216         name = connman_provider_get_driver_name(provider);
217         if (name == NULL)
218                 return NULL;
219
220         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
221         if (vpn_driver_data == NULL)
222                 return NULL;
223
224         state = vpn_driver_data->vpn_driver->notify(msg, provider);
225         switch (state) {
226         case VPN_STATE_CONNECT:
227         case VPN_STATE_READY:
228                 index = connman_provider_get_index(provider);
229                 connman_provider_ref(provider);
230                 data->watch = connman_rtnl_add_newlink_watch(index,
231                                                      vpn_newlink, provider);
232                 connman_inet_ifup(index);
233                 break;
234
235         case VPN_STATE_UNKNOWN:
236         case VPN_STATE_IDLE:
237         case VPN_STATE_DISCONNECT:
238         case VPN_STATE_FAILURE:
239                 connman_provider_set_state(provider,
240                                         CONNMAN_PROVIDER_STATE_DISCONNECT);
241                 break;
242
243         case VPN_STATE_AUTH_FAILURE:
244                 connman_provider_indicate_error(provider,
245                                         CONNMAN_PROVIDER_ERROR_AUTH_FAILED);
246                 break;
247         }
248
249         return NULL;
250 }
251
252 static int vpn_create_tun(struct connman_provider *provider)
253 {
254         struct vpn_data *data = connman_provider_get_data(provider);
255         struct ifreq ifr;
256         int i, fd, index;
257         int ret = 0;
258
259         if (data == NULL)
260                 return -EISCONN;
261
262         fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
263         if (fd < 0) {
264                 i = -errno;
265                 connman_error("Failed to open /dev/net/tun: %s",
266                               strerror(errno));
267                 ret = i;
268                 goto exist_err;
269         }
270
271         memset(&ifr, 0, sizeof(ifr));
272         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
273
274         for (i = 0; i < 256; i++) {
275                 sprintf(ifr.ifr_name, "vpn%d", i);
276
277                 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
278                         break;
279         }
280
281         if (i == 256) {
282                 connman_error("Failed to find available tun device");
283                 close(fd);
284                 ret = -ENODEV;
285                 goto exist_err;
286         }
287
288         data->if_name = (char *)g_strdup(ifr.ifr_name);
289         if (data->if_name == NULL) {
290                 connman_error("Failed to allocate memory");
291                 close(fd);
292                 ret = -ENOMEM;
293                 goto exist_err;
294         }
295
296         if (ioctl(fd, TUNSETPERSIST, 1)) {
297                 i = -errno;
298                 connman_error("Failed to set tun persistent: %s",
299                               strerror(errno));
300                 close(fd);
301                 ret = i;
302                 goto exist_err;
303         }
304
305         close(fd);
306
307         index = connman_inet_ifindex(data->if_name);
308         if (index < 0) {
309                 connman_error("Failed to get tun ifindex");
310                 stop_vpn(provider);
311                 ret = -EIO;
312                 goto exist_err;
313         }
314         connman_provider_set_index(provider, index);
315
316         return 0;
317
318 exist_err:
319         return ret;
320 }
321
322 static int vpn_connect(struct connman_provider *provider)
323 {
324         struct vpn_data *data = connman_provider_get_data(provider);
325         struct vpn_driver_data *vpn_driver_data;
326         const char *name;
327         int ret = 0;
328
329         if (data != NULL)
330                 return -EISCONN;
331
332         data = g_try_new0(struct vpn_data, 1);
333         if (data == NULL)
334                 return -ENOMEM;
335
336         data->provider = connman_provider_ref(provider);
337         data->watch = 0;
338         data->flags = 0;
339         data->task = NULL;
340         data->state = VPN_STATE_IDLE;
341
342         connman_provider_set_data(provider, data);
343
344         name = connman_provider_get_driver_name(provider);
345         if (name == NULL)
346                 return -EINVAL;
347
348         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
349
350         if (vpn_driver_data != NULL && vpn_driver_data->vpn_driver != NULL &&
351                 vpn_driver_data->vpn_driver->flags != VPN_FLAG_NO_TUN) {
352
353                 ret = vpn_create_tun(provider);
354                 if (ret < 0)
355                         goto exist_err;
356         }
357
358         data->task = connman_task_create(vpn_driver_data->program);
359
360         if (data->task == NULL) {
361                 ret = -ENOMEM;
362                 stop_vpn(provider);
363                 goto exist_err;
364         }
365
366         if (connman_task_set_notify(data->task, "notify",
367                                         vpn_notify, provider)) {
368                 ret = -ENOMEM;
369                 stop_vpn(provider);
370                 connman_task_destroy(data->task);
371                 data->task = NULL;
372                 goto exist_err;
373         }
374
375         ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
376                                                         data->if_name);
377         if (ret < 0) {
378                 stop_vpn(provider);
379                 connman_task_destroy(data->task);
380                 data->task = NULL;
381                 goto exist_err;
382         }
383
384         DBG("%s started with dev %s",
385                 vpn_driver_data->provider_driver.name, data->if_name);
386
387         data->state = VPN_STATE_CONNECT;
388
389         return -EINPROGRESS;
390
391 exist_err:
392         connman_provider_set_index(provider, -1);
393         connman_provider_set_data(provider, NULL);
394         connman_provider_unref(data->provider);
395         g_free(data->if_name);
396         g_free(data);
397
398         return ret;
399 }
400
401 static int vpn_probe(struct connman_provider *provider)
402 {
403         return 0;
404 }
405
406 static int vpn_disconnect(struct connman_provider *provider)
407 {
408         struct vpn_data *data = connman_provider_get_data(provider);
409         struct vpn_driver_data *vpn_driver_data;
410         const char *name;
411
412         DBG("disconnect provider %p:", provider);
413
414         if (data == NULL)
415                 return 0;
416
417         name = connman_provider_get_driver_name(provider);
418         if (name == NULL)
419                 return 0;
420
421         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
422         if (vpn_driver_data->vpn_driver->disconnect)
423                 vpn_driver_data->vpn_driver->disconnect();
424
425         if (data->watch != 0) {
426                 connman_provider_unref(provider);
427                 connman_rtnl_remove_watch(data->watch);
428                 data->watch = 0;
429         }
430
431         data->state = VPN_STATE_DISCONNECT;
432         connman_task_stop(data->task);
433
434         return 0;
435 }
436
437 static int vpn_remove(struct connman_provider *provider)
438 {
439         struct vpn_data *data;
440
441         data = connman_provider_get_data(provider);
442         if (data == NULL)
443                 return 0;
444
445         if (data->watch != 0) {
446                 connman_provider_unref(provider);
447                 connman_rtnl_remove_watch(data->watch);
448                 data->watch = 0;
449         }
450
451         connman_task_stop(data->task);
452
453         g_usleep(G_USEC_PER_SEC);
454         stop_vpn(provider);
455         return 0;
456 }
457
458 static int vpn_save (struct connman_provider *provider, GKeyFile *keyfile)
459 {
460         struct vpn_driver_data *vpn_driver_data;
461         const char *name;
462
463         name = connman_provider_get_driver_name(provider);
464         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
465         if (vpn_driver_data != NULL &&
466                         vpn_driver_data->vpn_driver->save != NULL)
467                 return vpn_driver_data->vpn_driver->save(provider, keyfile);
468
469         return 0;
470 }
471
472 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
473                         const char *program)
474 {
475         struct vpn_driver_data *data;
476
477         data = g_try_new0(struct vpn_driver_data, 1);
478         if (data == NULL)
479                 return -ENOMEM;
480
481         data->name = name;
482         data->program = program;
483
484         data->vpn_driver = vpn_driver;
485
486         data->provider_driver.name = name;
487         data->provider_driver.disconnect = vpn_disconnect;
488         data->provider_driver.connect = vpn_connect;
489         data->provider_driver.probe = vpn_probe;
490         data->provider_driver.remove = vpn_remove;
491         data->provider_driver.save = vpn_save;
492
493         if (driver_hash == NULL)
494                 driver_hash = g_hash_table_new_full(g_str_hash,
495                                                         g_str_equal,
496                                                         NULL, g_free);
497
498         if (driver_hash == NULL) {
499                 connman_error("driver_hash not initialized for %s", name);
500                 g_free(data);
501                 return -ENOMEM;
502         }
503
504         g_hash_table_insert(driver_hash, (char *)name, data);
505
506         connman_provider_driver_register(&data->provider_driver);
507
508         return 0;
509 }
510
511 void vpn_unregister(const char *name)
512 {
513         struct vpn_driver_data *data;
514
515         data = g_hash_table_lookup(driver_hash, name);
516         if (data == NULL)
517                 return;
518
519         connman_provider_driver_unregister(&data->provider_driver);
520
521         g_hash_table_remove(driver_hash, name);
522
523         if (g_hash_table_size(driver_hash) == 0)
524                 g_hash_table_destroy(driver_hash);
525 }