kernel-libipsec: Create a TUN device and use it to install virtual IPs
[strongswan.git] / src / libcharon / plugins / kernel_libipsec / kernel_libipsec_plugin.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "kernel_libipsec_plugin.h"
17 #include "kernel_libipsec_ipsec.h"
18
19 #include <ipsec.h>
20 #include <networking/tun_device.h>
21 #include <utils/debug.h>
22
23 #define TUN_DEFAULT_MTU 1400
24
25 typedef struct private_kernel_libipsec_plugin_t private_kernel_libipsec_plugin_t;
26
27 /**
28 * private data of "kernel" libipsec plugin
29 */
30 struct private_kernel_libipsec_plugin_t {
31
32 /**
33 * implements plugin interface
34 */
35 kernel_libipsec_plugin_t public;
36
37 /**
38 * TUN device created by this plugin
39 */
40 tun_device_t *tun;
41
42 };
43
44 METHOD(plugin_t, get_name, char*,
45 private_kernel_libipsec_plugin_t *this)
46 {
47 return "kernel-libipsec";
48 }
49
50 METHOD(plugin_t, get_features, int,
51 private_kernel_libipsec_plugin_t *this, plugin_feature_t *features[])
52 {
53 static plugin_feature_t f[] = {
54 PLUGIN_CALLBACK(kernel_ipsec_register, kernel_libipsec_ipsec_create),
55 PLUGIN_PROVIDE(CUSTOM, "kernel-ipsec"),
56 };
57 *features = f;
58 return countof(f);
59 }
60
61 METHOD(plugin_t, destroy, void,
62 private_kernel_libipsec_plugin_t *this)
63 {
64 if (this->tun)
65 {
66 lib->set(lib, "kernel-libipsec-tun", NULL);
67 this->tun->destroy(this->tun);
68 }
69 libipsec_deinit();
70 free(this);
71 }
72
73 /*
74 * see header file
75 */
76 plugin_t *kernel_libipsec_plugin_create()
77 {
78 private_kernel_libipsec_plugin_t *this;
79
80 INIT(this,
81 .public = {
82 .plugin = {
83 .get_name = _get_name,
84 .get_features = _get_features,
85 .destroy = _destroy,
86 },
87 },
88 );
89
90 if (!libipsec_init())
91 {
92 DBG1(DBG_LIB, "initialization of libipsec failed");
93 destroy(this);
94 return NULL;
95 }
96
97 this->tun = tun_device_create("ipsec%d");
98 if (!this->tun)
99 {
100 DBG1(DBG_KNL, "failed to create TUN device");
101 destroy(this);
102 return NULL;
103 }
104 if (!this->tun->set_mtu(this->tun, TUN_DEFAULT_MTU) ||
105 !this->tun->up(this->tun))
106 {
107 DBG1(DBG_KNL, "failed to configure TUN device");
108 destroy(this);
109 return NULL;
110 }
111 lib->set(lib, "kernel-libipsec-tun", this->tun);
112
113 /* set TUN device as default to install VIPs */
114 lib->settings->set_str(lib->settings, "%s.install_virtual_ip_on",
115 this->tun->get_name(this->tun), charon->name);
116 return &this->public.plugin;
117 }