Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / vpn / plugins / vpn.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2007-2013  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/log.h>
43 #include <connman/rtnl.h>
44 #include <connman/task.h>
45 #include <connman/inet.h>
46
47 #include "../vpn-rtnl.h"
48 #include "../vpn-provider.h"
49
50 #include "vpn.h"
51
52 struct vpn_data {
53         struct vpn_provider *provider;
54         char *if_name;
55         unsigned flags;
56         unsigned int watch;
57         enum vpn_state state;
58         struct connman_task *task;
59 };
60
61 struct vpn_driver_data {
62         const char *name;
63         const char *program;
64         struct vpn_driver *vpn_driver;
65         struct vpn_provider_driver provider_driver;
66 };
67
68 GHashTable *driver_hash = NULL;
69
70 static int stop_vpn(struct vpn_provider *provider)
71 {
72         struct vpn_data *data = vpn_provider_get_data(provider);
73         struct vpn_driver_data *vpn_driver_data;
74         const char *name;
75         struct ifreq ifr;
76         int fd, err;
77
78         if (!data)
79                 return -EINVAL;
80
81         name = vpn_provider_get_driver_name(provider);
82         if (!name)
83                 return -EINVAL;
84
85         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
86
87         if (vpn_driver_data && vpn_driver_data->vpn_driver &&
88                         vpn_driver_data->vpn_driver->flags == VPN_FLAG_NO_TUN)
89                 return 0;
90
91         memset(&ifr, 0, sizeof(ifr));
92         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
93         sprintf(ifr.ifr_name, "%s", data->if_name);
94
95         fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
96         if (fd < 0) {
97                 err = -errno;
98                 connman_error("Failed to open /dev/net/tun to device %s: %s",
99                               data->if_name, strerror(errno));
100                 return err;
101         }
102
103         if (ioctl(fd, TUNSETIFF, (void *)&ifr)) {
104                 err = -errno;
105                 connman_error("Failed to TUNSETIFF for device %s to it: %s",
106                               data->if_name, strerror(errno));
107                 close(fd);
108                 return err;
109         }
110
111         if (ioctl(fd, TUNSETPERSIST, 0)) {
112                 err = -errno;
113                 connman_error("Failed to set tun device %s nonpersistent: %s",
114                               data->if_name, strerror(errno));
115                 close(fd);
116                 return err;
117         }
118         close(fd);
119         DBG("Killed tun device %s", data->if_name);
120         return 0;
121 }
122
123 void vpn_died(struct connman_task *task, int exit_code, void *user_data)
124 {
125         struct vpn_provider *provider = user_data;
126         struct vpn_data *data = vpn_provider_get_data(provider);
127         int state = VPN_STATE_FAILURE;
128         enum vpn_provider_error ret;
129
130         DBG("provider %p data %p", provider, data);
131
132         if (!data)
133                 goto vpn_exit;
134
135         state = data->state;
136
137         stop_vpn(provider);
138         vpn_provider_set_data(provider, NULL);
139
140         if (data->watch != 0) {
141                 vpn_rtnl_remove_watch(data->watch);
142                 data->watch = 0;
143                 vpn_provider_unref(provider);
144         }
145
146 vpn_exit:
147         if (state != VPN_STATE_READY && state != VPN_STATE_DISCONNECT) {
148                 const char *name;
149                 struct vpn_driver_data *vpn_data = NULL;
150
151                 name = vpn_provider_get_driver_name(provider);
152                 if (name)
153                         vpn_data = g_hash_table_lookup(driver_hash, name);
154
155                 if (vpn_data &&
156                                 vpn_data->vpn_driver->error_code)
157                         ret = vpn_data->vpn_driver->error_code(provider,
158                                         exit_code);
159                 else
160                         ret = VPN_PROVIDER_ERROR_UNKNOWN;
161
162                 vpn_provider_indicate_error(provider, ret);
163         } else
164                 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_IDLE);
165
166         vpn_provider_set_index(provider, -1);
167
168         if (data) {
169                 vpn_provider_unref(data->provider);
170                 g_free(data->if_name);
171                 g_free(data);
172         }
173
174         connman_task_destroy(task);
175 }
176
177 int vpn_set_ifname(struct vpn_provider *provider, const char *ifname)
178 {
179         struct vpn_data *data = vpn_provider_get_data(provider);
180         int index;
181
182         if (!ifname || !data)
183                 return  -EIO;
184
185         index = connman_inet_ifindex(ifname);
186         if (index < 0)
187                 return  -EIO;
188
189         if (data->if_name)
190                 g_free(data->if_name);
191
192         data->if_name = (char *)g_strdup(ifname);
193         vpn_provider_set_index(provider, index);
194
195         return 0;
196 }
197
198 static void vpn_newlink(unsigned flags, unsigned change, void *user_data)
199 {
200         struct vpn_provider *provider = user_data;
201         struct vpn_data *data = vpn_provider_get_data(provider);
202
203         if ((data->flags & IFF_UP) != (flags & IFF_UP)) {
204                 if (flags & IFF_UP) {
205                         data->state = VPN_STATE_READY;
206                         vpn_provider_set_state(provider,
207                                         VPN_PROVIDER_STATE_READY);
208                 }
209         }
210         data->flags = flags;
211 }
212
213 static DBusMessage *vpn_notify(struct connman_task *task,
214                         DBusMessage *msg, void *user_data)
215 {
216         struct vpn_provider *provider = user_data;
217         struct vpn_data *data;
218         struct vpn_driver_data *vpn_driver_data;
219         const char *name;
220         int state, index, err;
221
222         data = vpn_provider_get_data(provider);
223
224         name = vpn_provider_get_driver_name(provider);
225
226         if (!name) {
227                 DBG("Cannot find VPN driver for provider %p", provider);
228                 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
229                 return NULL;
230         }
231
232         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
233         if (!vpn_driver_data) {
234                 DBG("Cannot find VPN driver data for name %s", name);
235                 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
236                 return NULL;
237         }
238
239         state = vpn_driver_data->vpn_driver->notify(msg, provider);
240
241         DBG("provider %p driver %s state %d", provider, name, state);
242
243         switch (state) {
244         case VPN_STATE_CONNECT:
245         case VPN_STATE_READY:
246                 if (data->state == VPN_STATE_READY) {
247                         /*
248                          * This is the restart case, in which case we must
249                          * just set the IP address.
250                          *
251                          * We need to remove first the old address, just
252                          * replacing the old address will not work as expected
253                          * because the old address will linger in the interface
254                          * and not disapper so the clearing is needed here.
255                          *
256                          * Also the state must change, otherwise the routes
257                          * will not be set properly.
258                          */
259                         vpn_provider_set_state(provider,
260                                                 VPN_PROVIDER_STATE_CONNECT);
261
262                         vpn_provider_clear_address(provider, AF_INET);
263                         vpn_provider_clear_address(provider, AF_INET6);
264
265                         vpn_provider_change_address(provider);
266                         vpn_provider_set_state(provider,
267                                                 VPN_PROVIDER_STATE_READY);
268                         break;
269                 }
270
271                 index = vpn_provider_get_index(provider);
272                 vpn_provider_ref(provider);
273                 data->watch = vpn_rtnl_add_newlink_watch(index,
274                                                      vpn_newlink, provider);
275                 err = connman_inet_ifup(index);
276                 if (err < 0) {
277                         if (err == -EALREADY)
278                                 /*
279                                  * So the interface is up already, that is just
280                                  * great. Unfortunately in this case the
281                                  * newlink watch might not have been called at
282                                  * all. We must manually call it here so that
283                                  * the provider can go to ready state and the
284                                  * routes are setup properly.
285                                  */
286                                 vpn_newlink(IFF_UP, 0, provider);
287                         else
288                                 DBG("Cannot take interface %d up err %d/%s",
289                                         index, -err, strerror(-err));
290                 }
291                 break;
292
293         case VPN_STATE_UNKNOWN:
294         case VPN_STATE_IDLE:
295         case VPN_STATE_DISCONNECT:
296         case VPN_STATE_FAILURE:
297                 vpn_provider_set_state(provider,
298                                         VPN_PROVIDER_STATE_DISCONNECT);
299                 break;
300
301         case VPN_STATE_AUTH_FAILURE:
302                 vpn_provider_indicate_error(provider,
303                                         VPN_PROVIDER_ERROR_AUTH_FAILED);
304                 break;
305         }
306
307         return NULL;
308 }
309
310 static int vpn_create_tun(struct vpn_provider *provider)
311 {
312         struct vpn_data *data = vpn_provider_get_data(provider);
313         struct ifreq ifr;
314         int i, fd, index;
315         int ret = 0;
316
317         if (!data)
318                 return -EISCONN;
319
320         fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
321         if (fd < 0) {
322                 i = -errno;
323                 connman_error("Failed to open /dev/net/tun: %s",
324                               strerror(errno));
325                 ret = i;
326                 goto exist_err;
327         }
328
329         memset(&ifr, 0, sizeof(ifr));
330         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
331
332         for (i = 0; i < 256; i++) {
333                 sprintf(ifr.ifr_name, "vpn%d", i);
334
335                 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
336                         break;
337         }
338
339         if (i == 256) {
340                 connman_error("Failed to find available tun device");
341                 close(fd);
342                 ret = -ENODEV;
343                 goto exist_err;
344         }
345
346         data->if_name = (char *)g_strdup(ifr.ifr_name);
347         if (!data->if_name) {
348                 connman_error("Failed to allocate memory");
349                 close(fd);
350                 ret = -ENOMEM;
351                 goto exist_err;
352         }
353
354         if (ioctl(fd, TUNSETPERSIST, 1)) {
355                 i = -errno;
356                 connman_error("Failed to set tun persistent: %s",
357                               strerror(errno));
358                 close(fd);
359                 ret = i;
360                 goto exist_err;
361         }
362
363         close(fd);
364
365         index = connman_inet_ifindex(data->if_name);
366         if (index < 0) {
367                 connman_error("Failed to get tun ifindex");
368                 stop_vpn(provider);
369                 ret = -EIO;
370                 goto exist_err;
371         }
372         vpn_provider_set_index(provider, index);
373
374         return 0;
375
376 exist_err:
377         return ret;
378 }
379
380 static int vpn_connect(struct vpn_provider *provider,
381                         vpn_provider_connect_cb_t cb,
382                         const char *dbus_sender, void *user_data)
383 {
384         struct vpn_data *data = vpn_provider_get_data(provider);
385         struct vpn_driver_data *vpn_driver_data;
386         const char *name;
387         int ret = 0;
388         enum vpn_state state = VPN_STATE_UNKNOWN;
389
390         if (data)
391                 state = data->state;
392
393         DBG("data %p state %d", data, state);
394
395         switch (state) {
396         case VPN_STATE_UNKNOWN:
397                 data = g_try_new0(struct vpn_data, 1);
398                 if (!data)
399                         return -ENOMEM;
400
401                 data->provider = vpn_provider_ref(provider);
402                 data->watch = 0;
403                 data->flags = 0;
404                 data->task = NULL;
405
406                 vpn_provider_set_data(provider, data);
407                 /* fall through */
408
409         case VPN_STATE_DISCONNECT:
410         case VPN_STATE_IDLE:
411         case VPN_STATE_FAILURE:
412         case VPN_STATE_AUTH_FAILURE:
413                 data->state = VPN_STATE_IDLE;
414                 break;
415
416         case VPN_STATE_CONNECT:
417                 return -EINPROGRESS;
418
419         case VPN_STATE_READY:
420                 return -EISCONN;
421         }
422
423         name = vpn_provider_get_driver_name(provider);
424         if (!name)
425                 return -EINVAL;
426
427         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
428
429         if (!vpn_driver_data || !vpn_driver_data->vpn_driver) {
430                 ret = -EINVAL;
431                 goto exist_err;
432         }
433
434         if (vpn_driver_data->vpn_driver->flags != VPN_FLAG_NO_TUN) {
435                 ret = vpn_create_tun(provider);
436                 if (ret < 0)
437                         goto exist_err;
438         }
439
440         data->task = connman_task_create(vpn_driver_data->program);
441
442         if (!data->task) {
443                 ret = -ENOMEM;
444                 stop_vpn(provider);
445                 goto exist_err;
446         }
447
448         if (connman_task_set_notify(data->task, "notify",
449                                         vpn_notify, provider)) {
450                 ret = -ENOMEM;
451                 stop_vpn(provider);
452                 connman_task_destroy(data->task);
453                 data->task = NULL;
454                 goto exist_err;
455         }
456
457         ret = vpn_driver_data->vpn_driver->connect(provider, data->task,
458                                                 data->if_name, cb, dbus_sender,
459                                                 user_data);
460         if (ret < 0 && ret != -EINPROGRESS) {
461                 stop_vpn(provider);
462                 connman_task_destroy(data->task);
463                 data->task = NULL;
464                 goto exist_err;
465         }
466
467         DBG("%s started with dev %s",
468                 vpn_driver_data->provider_driver.name, data->if_name);
469
470         data->state = VPN_STATE_CONNECT;
471
472         return -EINPROGRESS;
473
474 exist_err:
475         vpn_provider_set_index(provider, -1);
476         vpn_provider_set_data(provider, NULL);
477         vpn_provider_unref(data->provider);
478         g_free(data->if_name);
479         g_free(data);
480
481         return ret;
482 }
483
484 static int vpn_probe(struct vpn_provider *provider)
485 {
486         return 0;
487 }
488
489 static int vpn_disconnect(struct vpn_provider *provider)
490 {
491         struct vpn_data *data = vpn_provider_get_data(provider);
492         struct vpn_driver_data *vpn_driver_data;
493         const char *name;
494
495         DBG("disconnect provider %p:", provider);
496
497         if (!data)
498                 return 0;
499
500         name = vpn_provider_get_driver_name(provider);
501         if (!name)
502                 return 0;
503
504         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
505         if (vpn_driver_data->vpn_driver->disconnect)
506                 vpn_driver_data->vpn_driver->disconnect(provider);
507
508         if (data->watch != 0) {
509                 vpn_provider_unref(provider);
510                 vpn_rtnl_remove_watch(data->watch);
511                 data->watch = 0;
512         }
513
514         data->state = VPN_STATE_DISCONNECT;
515         connman_task_stop(data->task);
516
517         return 0;
518 }
519
520 static int vpn_remove(struct vpn_provider *provider)
521 {
522         struct vpn_data *data;
523
524         data = vpn_provider_get_data(provider);
525         if (!data)
526                 return 0;
527
528         if (data->watch != 0) {
529                 vpn_provider_unref(provider);
530                 vpn_rtnl_remove_watch(data->watch);
531                 data->watch = 0;
532         }
533
534         connman_task_stop(data->task);
535
536         g_usleep(G_USEC_PER_SEC);
537         stop_vpn(provider);
538         return 0;
539 }
540
541 static int vpn_save(struct vpn_provider *provider, GKeyFile *keyfile)
542 {
543         struct vpn_driver_data *vpn_driver_data;
544         const char *name;
545
546         name = vpn_provider_get_driver_name(provider);
547         vpn_driver_data = g_hash_table_lookup(driver_hash, name);
548         if (vpn_driver_data &&
549                         vpn_driver_data->vpn_driver->save)
550                 return vpn_driver_data->vpn_driver->save(provider, keyfile);
551
552         return 0;
553 }
554
555 int vpn_register(const char *name, struct vpn_driver *vpn_driver,
556                         const char *program)
557 {
558         struct vpn_driver_data *data;
559
560         data = g_try_new0(struct vpn_driver_data, 1);
561         if (!data)
562                 return -ENOMEM;
563
564         data->name = name;
565         data->program = program;
566
567         data->vpn_driver = vpn_driver;
568
569         data->provider_driver.name = name;
570         data->provider_driver.disconnect = vpn_disconnect;
571         data->provider_driver.connect = vpn_connect;
572         data->provider_driver.probe = vpn_probe;
573         data->provider_driver.remove = vpn_remove;
574         data->provider_driver.save = vpn_save;
575
576         if (!driver_hash)
577                 driver_hash = g_hash_table_new_full(g_str_hash,
578                                                         g_str_equal,
579                                                         NULL, g_free);
580
581         if (!driver_hash) {
582                 connman_error("driver_hash not initialized for %s", name);
583                 g_free(data);
584                 return -ENOMEM;
585         }
586
587         g_hash_table_replace(driver_hash, (char *)name, data);
588
589         vpn_provider_driver_register(&data->provider_driver);
590
591         return 0;
592 }
593
594 void vpn_unregister(const char *name)
595 {
596         struct vpn_driver_data *data;
597
598         data = g_hash_table_lookup(driver_hash, name);
599         if (!data)
600                 return;
601
602         vpn_provider_driver_unregister(&data->provider_driver);
603
604         g_hash_table_remove(driver_hash, name);
605
606         if (g_hash_table_size(driver_hash) == 0)
607                 g_hash_table_destroy(driver_hash);
608 }