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