ba67238e5a6e571ca1078c92ab948229ecce729f
[strongswan.git] / src / libhydra / kernel / kernel_ipsec.h
1 /*
2 * Copyright (C) 2006-2012 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 reqid unique ID for this SA
62 * @param spi allocated spi
63 * @return SUCCESS if operation completed
64 */
65 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
66 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi);
67
68 /**
69 * Get a Compression Parameter Index (CPI) from the kernel.
70 *
71 * @param src source address of SA
72 * @param dst destination address of SA
73 * @param reqid unique ID for the corresponding SA
74 * @param cpi allocated cpi
75 * @return SUCCESS if operation completed
76 */
77 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
78 u_int32_t reqid, u_int16_t *cpi);
79
80 /**
81 * Add an SA to the SAD.
82 *
83 * add_sa() may update an already allocated
84 * SPI (via get_spi). In this case, the replace
85 * flag must be set.
86 * This function does install a single SA for a
87 * single protocol in one direction.
88 *
89 * @param src source address for this SA
90 * @param dst destination address for this SA
91 * @param spi SPI allocated by us or remote peer
92 * @param protocol protocol for this SA (ESP/AH)
93 * @param reqid unique ID for this SA
94 * @param mark mark for this SA
95 * @param tfc Traffic Flow Confidentiality padding for this SA
96 * @param lifetime lifetime_cfg_t for this SA
97 * @param enc_alg Algorithm to use for encryption (ESP only)
98 * @param enc_key key to use for encryption
99 * @param int_alg Algorithm to use for integrity protection
100 * @param int_key key to use for integrity protection
101 * @param mode mode of the SA (tunnel, transport)
102 * @param ipcomp IPComp transform to use
103 * @param cpi CPI for IPComp
104 * @param encap enable UDP encapsulation for NAT traversal
105 * @param esn TRUE to use Extended Sequence Numbers
106 * @param inbound TRUE if this is an inbound SA
107 * @param src_ts traffic selector with BEET source address
108 * @param dst_ts traffic selector with BEET destination address
109 * @return SUCCESS if operation completed
110 */
111 status_t (*add_sa) (kernel_ipsec_t *this,
112 host_t *src, host_t *dst, u_int32_t spi,
113 u_int8_t protocol, u_int32_t reqid,
114 mark_t mark, u_int32_t tfc, lifetime_cfg_t *lifetime,
115 u_int16_t enc_alg, chunk_t enc_key,
116 u_int16_t int_alg, chunk_t int_key,
117 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
118 bool encap, bool esn, bool inbound,
119 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
120
121 /**
122 * Update the hosts on an installed SA.
123 *
124 * We cannot directly update the destination address as the kernel
125 * requires the spi, the protocol AND the destination address (and family)
126 * to identify SAs. Therefore if the destination address changed we
127 * create a new SA and delete the old one.
128 *
129 * @param spi SPI of the SA
130 * @param protocol protocol for this SA (ESP/AH)
131 * @param cpi CPI for IPComp, 0 if no IPComp is used
132 * @param src current source address
133 * @param dst current destination address
134 * @param new_src new source address
135 * @param new_dst new destination address
136 * @param encap current use of UDP encapsulation
137 * @param new_encap new use of UDP encapsulation
138 * @param mark optional mark for this SA
139 * @return SUCCESS if operation completed, NOT_SUPPORTED if
140 * the kernel interface can't update the SA
141 */
142 status_t (*update_sa)(kernel_ipsec_t *this,
143 u_int32_t spi, u_int8_t protocol, u_int16_t cpi,
144 host_t *src, host_t *dst,
145 host_t *new_src, host_t *new_dst,
146 bool encap, bool new_encap, mark_t mark);
147
148 /**
149 * Query the number of bytes processed by an SA from the SAD.
150 *
151 * @param src source address for this SA
152 * @param dst destination address for this SA
153 * @param spi SPI allocated by us or remote peer
154 * @param protocol protocol for this SA (ESP/AH)
155 * @param mark optional mark for this SA
156 * @param[out] bytes the number of bytes processed by SA
157 * @param[out] packets number of packets processed by SA
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);
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 * A policy is always associated to an SA. Traffic which matches a
190 * policy is handled by the SA with the same reqid.
191 *
192 * @param src source address of SA
193 * @param dst dest address of SA
194 * @param src_ts traffic selector to match traffic source
195 * @param dst_ts traffic selector to match traffic dest
196 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
197 * @param type type of policy, POLICY_(IPSEC|PASS|DROP)
198 * @param sa details about the SA(s) tied to this policy
199 * @param mark mark for this policy
200 * @param priority priority of this policy
201 * @return SUCCESS if operation completed
202 */
203 status_t (*add_policy) (kernel_ipsec_t *this,
204 host_t *src, host_t *dst,
205 traffic_selector_t *src_ts,
206 traffic_selector_t *dst_ts,
207 policy_dir_t direction, policy_type_t type,
208 ipsec_sa_cfg_t *sa, mark_t mark,
209 policy_priority_t priority);
210
211 /**
212 * Query the use time of a policy.
213 *
214 * The use time of a policy is the time the policy was used for the last
215 * time. It is not the system time, but a monotonic timestamp as returned
216 * by time_monotonic.
217 *
218 * @param src_ts traffic selector to match traffic source
219 * @param dst_ts traffic selector to match traffic dest
220 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
221 * @param mark optional mark
222 * @param[out] use_time the monotonic timestamp of this SA's last use
223 * @return SUCCESS if operation completed
224 */
225 status_t (*query_policy) (kernel_ipsec_t *this,
226 traffic_selector_t *src_ts,
227 traffic_selector_t *dst_ts,
228 policy_dir_t direction, mark_t mark,
229 u_int32_t *use_time);
230
231 /**
232 * Remove a policy from the SPD.
233 *
234 * The kernel interface implements reference counting for policies.
235 * If the same policy is installed multiple times (in the case of rekeying),
236 * the reference counter is increased. del_policy() decreases the ref counter
237 * and removes the policy only when no more references are available.
238 *
239 * @param src_ts traffic selector to match traffic source
240 * @param dst_ts traffic selector to match traffic dest
241 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
242 * @param reqid unique ID of the associated SA
243 * @param mark optional mark
244 * @param priority priority of the policy
245 * @return SUCCESS if operation completed
246 */
247 status_t (*del_policy) (kernel_ipsec_t *this,
248 traffic_selector_t *src_ts,
249 traffic_selector_t *dst_ts,
250 policy_dir_t direction, u_int32_t reqid,
251 mark_t mark, policy_priority_t priority);
252
253 /**
254 * Flush all policies from the SPD.
255 *
256 * @return SUCCESS if operation completed
257 */
258 status_t (*flush_policies) (kernel_ipsec_t *this);
259
260 /**
261 * Install a bypass policy for the given socket.
262 *
263 * @param fd socket file descriptor to setup policy for
264 * @param family protocol family of the socket
265 * @return TRUE of policy set up successfully
266 */
267 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
268
269 /**
270 * Enable decapsulation of ESP-in-UDP packets for the given port/socket.
271 *
272 * @param fd socket file descriptor
273 * @param family protocol family of the socket
274 * @param port the UDP port
275 * @return TRUE if UDP decapsulation was enabled successfully
276 */
277 bool (*enable_udp_decap)(kernel_ipsec_t *this, int fd, int family,
278 u_int16_t port);
279
280 /**
281 * Destroy the implementation.
282 */
283 void (*destroy) (kernel_ipsec_t *this);
284 };
285
286 /**
287 * Helper function to (un-)register IPsec kernel interfaces from plugin features.
288 *
289 * This function is a plugin_feature_callback_t and can be used with the
290 * PLUGIN_CALLBACK macro to register an IPsec kernel interface constructor.
291 *
292 * @param plugin plugin registering the kernel interface
293 * @param feature associated plugin feature
294 * @param reg TRUE to register, FALSE to unregister
295 * @param data data passed to callback, an kernel_ipsec_constructor_t
296 */
297 bool kernel_ipsec_register(plugin_t *plugin, plugin_feature_t *feature,
298 bool reg, void *data);
299
300 #endif /** KERNEL_IPSEC_H_ @}*/