2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "kernel_libipsec_plugin.h"
17 #include "kernel_libipsec_ipsec.h"
18 #include "kernel_libipsec_router.h"
22 #include <networking/tun_device.h>
24 #define TUN_DEFAULT_MTU 1400
26 typedef struct private_kernel_libipsec_plugin_t private_kernel_libipsec_plugin_t
;
29 * private data of "kernel" libipsec plugin
31 struct private_kernel_libipsec_plugin_t
{
34 * implements plugin interface
36 kernel_libipsec_plugin_t
public;
39 * TUN device created by this plugin
46 kernel_libipsec_router_t
*router
;
49 METHOD(plugin_t
, get_name
, char*,
50 private_kernel_libipsec_plugin_t
*this)
52 return "kernel-libipsec";
56 * Create the kernel_libipsec_router_t instance
58 static bool create_router(private_kernel_libipsec_plugin_t
*this,
59 plugin_feature_t
*feature
, bool reg
, void *arg
)
62 { /* registers as packet handler etc. */
63 this->router
= kernel_libipsec_router_create();
67 DESTROY_IF(this->router
);
72 METHOD(plugin_t
, get_features
, int,
73 private_kernel_libipsec_plugin_t
*this, plugin_feature_t
*features
[])
75 static plugin_feature_t f
[] = {
76 PLUGIN_CALLBACK(kernel_ipsec_register
, kernel_libipsec_ipsec_create
),
77 PLUGIN_PROVIDE(CUSTOM
, "kernel-ipsec"),
78 PLUGIN_CALLBACK((plugin_feature_callback_t
)create_router
, NULL
),
79 PLUGIN_PROVIDE(CUSTOM
, "kernel-libipsec-router"),
80 PLUGIN_DEPENDS(CUSTOM
, "libcharon-receiver"),
86 METHOD(plugin_t
, destroy
, void,
87 private_kernel_libipsec_plugin_t
*this)
91 lib
->set(lib
, "kernel-libipsec-tun", NULL
);
92 this->tun
->destroy(this->tun
);
101 plugin_t
*kernel_libipsec_plugin_create()
103 private_kernel_libipsec_plugin_t
*this;
105 if (!lib
->caps
->check(lib
->caps
, CAP_NET_ADMIN
))
106 { /* required to create TUN devices */
107 DBG1(DBG_KNL
, "kernel-libipsec plugin requires CAP_NET_ADMIN "
115 .get_name
= _get_name
,
116 .get_features
= _get_features
,
122 if (!libipsec_init())
124 DBG1(DBG_LIB
, "initialization of libipsec failed");
129 this->tun
= tun_device_create("ipsec%d");
132 DBG1(DBG_KNL
, "failed to create TUN device");
136 if (!this->tun
->set_mtu(this->tun
, TUN_DEFAULT_MTU
) ||
137 !this->tun
->up(this->tun
))
139 DBG1(DBG_KNL
, "failed to configure TUN device");
143 lib
->set(lib
, "kernel-libipsec-tun", this->tun
);
145 /* set TUN device as default to install VIPs */
146 lib
->settings
->set_str(lib
->settings
, "%s.install_virtual_ip_on",
147 this->tun
->get_name(this->tun
), charon
->name
);
148 return &this->public.plugin
;