libhydra: Move all kernel plugins to libcharon
[strongswan.git] / src / libhydra / kernel / kernel_ipsec.h
1 /*
2 * Copyright (C) 2006-2015 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /**
20 * @defgroup kernel_ipsec kernel_ipsec
21 * @{ @ingroup hkernel
22 */
23
24 #ifndef KERNEL_IPSEC_H_
25 #define KERNEL_IPSEC_H_
26
27 typedef struct kernel_ipsec_t kernel_ipsec_t;
28
29 #include <networking/host.h>
30 #include <ipsec/ipsec_types.h>
31 #include <selectors/traffic_selector.h>
32 #include <plugins/plugin.h>
33 #include <kernel/kernel_interface.h>
34
35 /**
36 * Interface to the ipsec subsystem of the kernel.
37 *
38 * The kernel ipsec interface handles the communication with the kernel
39 * for SA and policy management. It allows setup of these, and provides
40 * further the handling of kernel events.
41 * Policy information are cached in the interface. This is necessary to do
42 * reference counting. The Linux kernel does not allow the same policy
43 * installed twice, but we need this as CHILD_SA exist multiple times
44 * when rekeying. Thats why we do reference counting of policies.
45 */
46 struct kernel_ipsec_t {
47
48 /**
49 * Get the feature set supported by this kernel backend.
50 *
51 * @return ORed feature-set of backend
52 */
53 kernel_feature_t (*get_features)(kernel_ipsec_t *this);
54
55 /**
56 * Get a SPI from the kernel.
57 *
58 * @param src source address of SA
59 * @param dst destination address of SA
60 * @param protocol protocol for SA (ESP/AH)
61 * @param spi allocated spi
62 * @return SUCCESS if operation completed
63 */
64 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
65 u_int8_t protocol, u_int32_t *spi);
66
67 /**
68 * Get a Compression Parameter Index (CPI) from the kernel.
69 *
70 * @param src source address of SA
71 * @param dst destination address of SA
72 * @param cpi allocated cpi
73 * @return SUCCESS if operation completed
74 */
75 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
76 u_int16_t *cpi);
77
78 /**
79 * Add an SA to the SAD.
80 *
81 * This function does install a single SA for a single protocol in one
82 * direction.
83 *
84 * @param src source address for this SA
85 * @param dst destination address for this SA
86 * @param spi SPI allocated by us or remote peer
87 * @param protocol protocol for this SA (ESP/AH)
88 * @param reqid unique ID for this SA
89 * @param mark mark for this SA
90 * @param tfc Traffic Flow Confidentiality padding for this SA
91 * @param lifetime lifetime_cfg_t for this SA
92 * @param enc_alg Algorithm to use for encryption (ESP only)
93 * @param enc_key key to use for encryption
94 * @param int_alg Algorithm to use for integrity protection
95 * @param int_key key to use for integrity protection
96 * @param mode mode of the SA (tunnel, transport)
97 * @param ipcomp IPComp transform to use
98 * @param cpi CPI for IPComp
99 * @param replay_window anti-replay window size
100 * @param initiator TRUE if initiator of the exchange creating this SA
101 * @param encap enable UDP encapsulation for NAT traversal
102 * @param esn TRUE to use Extended Sequence Numbers
103 * @param inbound TRUE if this is an inbound SA
104 * @param update TRUE if an SPI has already been allocated for SA
105 * @param src_ts list of source traffic selectors
106 * @param dst_ts list of destination traffic selectors
107 * @return SUCCESS if operation completed
108 */
109 status_t (*add_sa) (kernel_ipsec_t *this,
110 host_t *src, host_t *dst, u_int32_t spi,
111 u_int8_t protocol, u_int32_t reqid,
112 mark_t mark, u_int32_t tfc, lifetime_cfg_t *lifetime,
113 u_int16_t enc_alg, chunk_t enc_key,
114 u_int16_t int_alg, chunk_t int_key,
115 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
116 u_int32_t replay_window, bool initiator, bool encap,
117 bool esn, bool inbound, bool update,
118 linked_list_t *src_ts, linked_list_t *dst_ts);
119
120 /**
121 * Update the hosts on an installed SA.
122 *
123 * We cannot directly update the destination address as the kernel
124 * requires the spi, the protocol AND the destination address (and family)
125 * to identify SAs. Therefore if the destination address changed we
126 * create a new SA and delete the old one.
127 *
128 * @param spi SPI of the SA
129 * @param protocol protocol for this SA (ESP/AH)
130 * @param cpi CPI for IPComp, 0 if no IPComp is used
131 * @param src current source address
132 * @param dst current destination address
133 * @param new_src new source address
134 * @param new_dst new destination address
135 * @param encap current use of UDP encapsulation
136 * @param new_encap new use of UDP encapsulation
137 * @param mark optional mark for this SA
138 * @return SUCCESS if operation completed, NOT_SUPPORTED if
139 * the kernel interface can't update the SA
140 */
141 status_t (*update_sa)(kernel_ipsec_t *this,
142 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
143 host_t *src, host_t *dst,
144 host_t *new_src, host_t *new_dst,
145 bool encap, bool new_encap, mark_t mark);
146
147 /**
148 * Query the number of bytes processed by an SA from the SAD.
149 *
150 * @param src source address for this SA
151 * @param dst destination address for this SA
152 * @param spi SPI allocated by us or remote peer
153 * @param protocol protocol for this SA (ESP/AH)
154 * @param mark optional mark for this SA
155 * @param[out] bytes the number of bytes processed by SA
156 * @param[out] packets number of packets processed by SA
157 * @param[out] time last (monotonic) time of SA use
158 * @return SUCCESS if operation completed
159 */
160 status_t (*query_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
161 u_int32_t spi, u_int8_t protocol, mark_t mark,
162 u_int64_t *bytes, u_int64_t *packets, time_t *time);
163
164 /**
165 * Delete a previusly installed SA from the SAD.
166 *
167 * @param src source address for this SA
168 * @param dst destination address for this SA
169 * @param spi SPI allocated by us or remote peer
170 * @param protocol protocol for this SA (ESP/AH)
171 * @param cpi CPI for IPComp or 0
172 * @param mark optional mark for this SA
173 * @return SUCCESS if operation completed
174 */
175 status_t (*del_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
176 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
177 mark_t mark);
178
179 /**
180 * Flush all SAs from the SAD.
181 *
182 * @return SUCCESS if operation completed
183 */
184 status_t (*flush_sas) (kernel_ipsec_t *this);
185
186 /**
187 * Add a policy to the SPD.
188 *
189 * @param src source address of SA
190 * @param dst dest address of SA
191 * @param src_ts traffic selector to match traffic source
192 * @param dst_ts traffic selector to match traffic dest
193 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
194 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
195 * @param sa details about the SA(s) tied to this policy
196 * @param mark mark for this policy
197 * @param priority priority of this policy
198 * @return SUCCESS if operation completed
199 */
200 status_t (*add_policy) (kernel_ipsec_t *this,
201 host_t *src, host_t *dst,
202 traffic_selector_t *src_ts,
203 traffic_selector_t *dst_ts,
204 policy_dir_t direction, policy_type_t type,
205 ipsec_sa_cfg_t *sa, mark_t mark,
206 policy_priority_t priority);
207
208 /**
209 * Query the use time of a policy.
210 *
211 * The use time of a policy is the time the policy was used for the last
212 * time. It is not the system time, but a monotonic timestamp as returned
213 * by time_monotonic.
214 *
215 * @param src_ts traffic selector to match traffic source
216 * @param dst_ts traffic selector to match traffic dest
217 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
218 * @param mark optional mark
219 * @param[out] use_time the monotonic timestamp of this SA's last use
220 * @return SUCCESS if operation completed
221 */
222 status_t (*query_policy) (kernel_ipsec_t *this,
223 traffic_selector_t *src_ts,
224 traffic_selector_t *dst_ts,
225 policy_dir_t direction, mark_t mark,
226 time_t *use_time);
227
228 /**
229 * Remove a policy from the SPD.
230 *
231 * @param src source address of SA
232 * @param dst dest address of SA
233 * @param src_ts traffic selector to match traffic source
234 * @param dst_ts traffic selector to match traffic dest
235 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
236 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
237 * @param sa details about the SA(s) tied to this policy
238 * @param mark mark for this policy
239 * @param priority priority of the policy
240 * @return SUCCESS if operation completed
241 */
242 status_t (*del_policy) (kernel_ipsec_t *this,
243 host_t *src, host_t *dst,
244 traffic_selector_t *src_ts,
245 traffic_selector_t *dst_ts,
246 policy_dir_t direction, policy_type_t type,
247 ipsec_sa_cfg_t *sa, mark_t mark,
248 policy_priority_t priority);
249
250 /**
251 * Flush all policies from the SPD.
252 *
253 * @return SUCCESS if operation completed
254 */
255 status_t (*flush_policies) (kernel_ipsec_t *this);
256
257 /**
258 * Install a bypass policy for the given socket.
259 *
260 * @param fd socket file descriptor to setup policy for
261 * @param family protocol family of the socket
262 * @return TRUE of policy set up successfully
263 */
264 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
265
266 /**
267 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
268 *
269 * @param fd socket file descriptor
270 * @param family protocol family of the socket
271 * @param port the UDP port
272 * @return TRUE if UDP decapsulation was enabled successfully
273 */
274 bool (*enable_udp_decap)(kernel_ipsec_t *this, int fd, int family,
275 u_int16_t port);
276
277 /**
278 * Destroy the implementation.
279 */
280 void (*destroy) (kernel_ipsec_t *this);
281 };
282
283 /**
284 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
285 *
286 * This function is a plugin_feature_callback_t and can be used with the
287 * PLUGIN_CALLBACK macro to register an IPsec kernel interface constructor.
288 *
289 * @param plugin plugin registering the kernel interface
290 * @param feature associated plugin feature
291 * @param reg TRUE to register, FALSE to unregister
292 * @param data data passed to callback, an kernel_ipsec_constructor_t
293 */
294 bool kernel_ipsec_register(plugin_t *plugin, plugin_feature_t *feature,
295 bool reg, void *data);
296
297 #endif /** KERNEL_IPSEC_H_ @}*/