kernel-libipsec: Add plugin that implements kernel_ipsec_t using libipsec
[strongswan.git] / src / libcharon / plugins / kernel_libipsec / kernel_libipsec_ipsec.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 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "kernel_libipsec_ipsec.h"
16
17 #include <library.h>
18 #include <ipsec.h>
19 #include <hydra.h>
20 #include <utils/debug.h>
21
22 typedef struct private_kernel_libipsec_ipsec_t private_kernel_libipsec_ipsec_t;
23
24 struct private_kernel_libipsec_ipsec_t {
25
26 /**
27 * Public libipsec_ipsec interface
28 */
29 kernel_libipsec_ipsec_t public;
30
31 /**
32 * Listener for lifetime expire events
33 */
34 ipsec_event_listener_t ipsec_listener;
35 };
36
37 /**
38 * Expiration callback
39 */
40 static void expire(u_int32_t reqid, u_int8_t protocol, u_int32_t spi, bool hard)
41 {
42 hydra->kernel_interface->expire(hydra->kernel_interface, reqid, protocol,
43 spi, hard);
44 }
45
46 METHOD(kernel_ipsec_t, get_spi, status_t,
47 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
48 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi)
49 {
50 return ipsec->sas->get_spi(ipsec->sas, src, dst, protocol, reqid, spi);
51 }
52
53 METHOD(kernel_ipsec_t, get_cpi, status_t,
54 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
55 u_int32_t reqid, u_int16_t *cpi)
56 {
57 return NOT_SUPPORTED;
58 }
59
60 METHOD(kernel_ipsec_t, add_sa, status_t,
61 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
62 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
63 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
64 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode, u_int16_t ipcomp,
65 u_int16_t cpi, bool initiator, bool encap, bool esn, bool inbound,
66 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
67 {
68 return ipsec->sas->add_sa(ipsec->sas, src, dst, spi, protocol, reqid, mark,
69 tfc, lifetime, enc_alg, enc_key, int_alg, int_key,
70 mode, ipcomp, cpi, initiator, encap, esn, inbound,
71 src_ts, dst_ts);
72 }
73
74 METHOD(kernel_ipsec_t, update_sa, status_t,
75 private_kernel_libipsec_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
76 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
77 bool encap, bool new_encap, mark_t mark)
78 {
79 return NOT_SUPPORTED;
80 }
81
82 METHOD(kernel_ipsec_t, query_sa, status_t,
83 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
84 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes,
85 u_int64_t *packets, u_int32_t *time)
86 {
87 return NOT_SUPPORTED;
88 }
89
90 METHOD(kernel_ipsec_t, del_sa, status_t,
91 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
92 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
93 {
94 return ipsec->sas->del_sa(ipsec->sas, src, dst, spi, protocol, cpi, mark);
95 }
96
97 METHOD(kernel_ipsec_t, flush_sas, status_t,
98 private_kernel_libipsec_ipsec_t *this)
99 {
100 return ipsec->sas->flush_sas(ipsec->sas);
101 }
102
103 METHOD(kernel_ipsec_t, add_policy, status_t,
104 private_kernel_libipsec_ipsec_t *this, host_t *src, host_t *dst,
105 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
106 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa, mark_t mark,
107 policy_priority_t priority)
108 {
109 return ipsec->policies->add_policy(ipsec->policies, src, dst, src_ts,
110 dst_ts, direction, type, sa, mark, priority);
111 }
112
113 METHOD(kernel_ipsec_t, query_policy, status_t,
114 private_kernel_libipsec_ipsec_t *this, traffic_selector_t *src_ts,
115 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
116 u_int32_t *use_time)
117 {
118 return NOT_SUPPORTED;
119 }
120
121 METHOD(kernel_ipsec_t, del_policy, status_t,
122 private_kernel_libipsec_ipsec_t *this, traffic_selector_t *src_ts,
123 traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
124 mark_t mark, policy_priority_t priority)
125 {
126 return ipsec->policies->del_policy(ipsec->policies, src_ts, dst_ts,
127 direction, reqid, mark, priority);
128 }
129
130 METHOD(kernel_ipsec_t, flush_policies, status_t,
131 private_kernel_libipsec_ipsec_t *this)
132 {
133 return ipsec->policies->flush_policies(ipsec->policies);
134 }
135
136 METHOD(kernel_ipsec_t, bypass_socket, bool,
137 private_kernel_libipsec_ipsec_t *this, int fd, int family)
138 {
139 return NOT_SUPPORTED;
140 }
141
142 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
143 private_kernel_libipsec_ipsec_t *this, int fd, int family, u_int16_t port)
144 {
145 return NOT_SUPPORTED;
146 }
147
148 METHOD(kernel_ipsec_t, destroy, void,
149 private_kernel_libipsec_ipsec_t *this)
150 {
151 ipsec->events->unregister_listener(ipsec->events, &this->ipsec_listener);
152 free(this);
153 }
154
155 /*
156 * Described in header.
157 */
158 kernel_libipsec_ipsec_t *kernel_libipsec_ipsec_create()
159 {
160 private_kernel_libipsec_ipsec_t *this;
161
162 INIT(this,
163 .public = {
164 .interface = {
165 .get_spi = _get_spi,
166 .get_cpi = _get_cpi,
167 .add_sa = _add_sa,
168 .update_sa = _update_sa,
169 .query_sa = _query_sa,
170 .del_sa = _del_sa,
171 .flush_sas = _flush_sas,
172 .add_policy = _add_policy,
173 .query_policy = _query_policy,
174 .del_policy = _del_policy,
175 .flush_policies = _flush_policies,
176 .bypass_socket = _bypass_socket,
177 .enable_udp_decap = _enable_udp_decap,
178 .destroy = _destroy,
179 },
180 },
181 .ipsec_listener = {
182 .expire = expire,
183 },
184 );
185
186 ipsec->events->register_listener(ipsec->events, &this->ipsec_listener);
187
188 return &this->public;
189 };