added support for transport mode and (experimental!) BEET mode
[strongswan.git] / src / charon / threads / kernel_interface.h
1 /**
2 * @file kernel_interface.h
3 *
4 * @brief Interface of kernel_interface_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #ifndef KERNEL_INTERFACE_H_
26 #define KERNEL_INTERFACE_H_
27
28 typedef struct natt_conf_t natt_conf_t;
29 typedef enum policy_dir_t policy_dir_t;
30 typedef struct kernel_interface_t kernel_interface_t;
31
32 #include <utils/host.h>
33 #include <crypto/prf_plus.h>
34 #include <encoding/payloads/proposal_substructure.h>
35
36 /**
37 * Configuration for NAT-T
38 */
39 struct natt_conf_t {
40 /** source port to use for UDP-encapsulated packets */
41 u_int16_t sport;
42 /** dest port to use for UDP-encapsulated packets */
43 u_int16_t dport;
44 };
45
46 /**
47 * Direction of a policy. These are equal to those
48 * defined in xfrm.h, but we want to stay implementation
49 * neutral here.
50 */
51 enum policy_dir_t {
52 /** Policy for inbound traffic */
53 POLICY_IN = 0,
54 /** Policy for outbound traffic */
55 POLICY_OUT = 1,
56 /** Policy for forwarded traffic */
57 POLICY_FWD = 2,
58 };
59
60 /**
61 * @brief Interface to the kernel.
62 *
63 * The kernel interface handles the communication with the kernel
64 * for SA and policy management. It allows setup of these, and provides
65 * further the handling of kernel events.
66 * Policy information are cached in the interface. This is necessary to do
67 * reference counting. The Linux kernel does not allow the same policy
68 * installed twice, but we need this as CHILD_SA exist multiple times
69 * when rekeying. Thats why we do reference counting of policies.
70 *
71 * @b Constructors:
72 * - kernel_interface_create()
73 *
74 * @ingroup threads
75 */
76 struct kernel_interface_t {
77
78 /**
79 * @brief Get a SPI from the kernel.
80 *
81 * @warning get_spi() implicitely creates an SA with
82 * the allocated SPI, therefore the replace flag
83 * in add_sa() must be set when installing this SA.
84 *
85 * @param this calling object
86 * @param src source address of SA
87 * @param dst destination address of SA
88 * @param protocol protocol for SA (ESP/AH)
89 * @param reqid unique ID for this SA
90 * @param[out] spi allocated spi
91 * @return
92 * - SUCCESS
93 * - FAILED if kernel comm failed
94 */
95 status_t (*get_spi)(kernel_interface_t *this, host_t *src, host_t *dst,
96 protocol_id_t protocol, u_int32_t reqid, u_int32_t *spi);
97
98 /**
99 * @brief Add an SA to the SAD.
100 *
101 * add_sa() may update an already allocated
102 * SPI (via get_spi). In this case, the replace
103 * flag must be set.
104 * This function does install a single SA for a
105 * single protocol in one direction. The kernel-interface
106 * gets the keys itself from the PRF, as we don't know
107 * his algorithms and key sizes.
108 *
109 * @param this calling object
110 * @param src source address for this SA
111 * @param dst destination address for this SA
112 * @param spi SPI allocated by us or remote peer
113 * @param protocol protocol for this SA (ESP/AH)
114 * @param reqid unique ID for this SA
115 * @param expire_soft lifetime in seconds before rekeying
116 * @param expire_hard lieftime in seconds before delete
117 * @param enc_alg Algorithm to use for encryption (ESP only)
118 * @param int_alg Algorithm to use for integrity protection
119 * @param prf_plus PRF to derive keys from
120 * @param natt NAT-T Configuration, or NULL of no NAT-T used
121 * @param mode mode of the SA (tunnel, transport)
122 * @param replace Should an already installed SA be updated?
123 * @return
124 * - SUCCESS
125 * - FAILED if kernel comm failed
126 */
127 status_t (*add_sa) (kernel_interface_t *this,
128 host_t *src, host_t *dst, u_int32_t spi,
129 protocol_id_t protocol, u_int32_t reqid,
130 u_int64_t expire_soft, u_int64_t expire_hard,
131 algorithm_t *enc_alg, algorithm_t *int_alg,
132 prf_plus_t *prf_plus, natt_conf_t *natt,
133 mode_t mode, bool update);
134
135 /**
136 * @brief Update the hosts on an installed SA.
137 *
138 * We cannot directly update the destination address as the kernel
139 * requires the spi, the protocol AND the destination address (and family)
140 * to identify SAs. Therefore if the destination address changed we
141 * create a new SA and delete the old one.
142 *
143 * @param this calling object
144 * @param dst destination address for this SA
145 * @param spi SPI of the SA
146 * @param protocol protocol for this SA (ESP/AH)
147 * @param new_src new source address for this SA
148 * @param new_dst new destination address for this SA
149 * @param src_changes changes in src
150 * @param dst_changes changes in dst
151 * @return
152 * - SUCCESS
153 * - FAILED if kernel comm failed
154 */
155 status_t (*update_sa)(kernel_interface_t *this, host_t *dst, u_int32_t spi,
156 protocol_id_t protocol,
157 host_t *new_src, host_t *new_dst,
158 host_diff_t src_changes, host_diff_t dst_changes);
159
160 /**
161 * @brief Query the use time of an SA.
162 *
163 * The use time of an SA is not the time of the last usage, but
164 * the time of the first usage of the SA.
165 *
166 * @param this calling object
167 * @param dst destination address for this SA
168 * @param spi SPI allocated by us or remote peer
169 * @param protocol protocol for this SA (ESP/AH)
170 * @param[out] use_time the time of this SA's last use
171 * @return
172 * - SUCCESS
173 * - FAILED if kernel comm failed
174 */
175 status_t (*query_sa) (kernel_interface_t *this, host_t *dst, u_int32_t spi,
176 protocol_id_t protocol, u_int32_t *use_time);
177
178 /**
179 * @brief Delete a previusly installed SA from the SAD.
180 *
181 * @param this calling object
182 * @param dst destination address for this SA
183 * @param spi SPI allocated by us or remote peer
184 * @param protocol protocol for this SA (ESP/AH)
185 * @return
186 * - SUCCESS
187 * - FAILED if kernel comm failed
188 */
189 status_t (*del_sa) (kernel_interface_t *this, host_t *dst, u_int32_t spi,
190 protocol_id_t protocol);
191
192 /**
193 * @brief Add a policy to the SPD.
194 *
195 * A policy is always associated to an SA. Traffic which matches a
196 * policy is handled by the SA with the same reqid.
197 * If the update flag is set, the policy is updated with the new
198 * src/dst addresses.
199 * If the update flag is not set, but a such policy is already in the
200 * kernel, the reference count to this policy is increased.
201 *
202 * @param this calling object
203 * @param src source address of SA
204 * @param dst dest address of SA
205 * @param src_ts traffic selector to match traffic source
206 * @param dst_ts traffic selector to match traffic dest
207 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
208 * @param protocol protocol to use to protect traffic (AH/ESP)
209 * @param reqid uniqe ID of an SA to use to enforce policy
210 * @param high_prio if TRUE, uses a higher priority than any with FALSE
211 * @param mode mode of SA (tunnel, transport)
212 * @param update update an existing policy, if TRUE
213 * @return
214 * - SUCCESS
215 * - FAILED if kernel comm failed
216 */
217 status_t (*add_policy) (kernel_interface_t *this,
218 host_t *src, host_t *dst,
219 traffic_selector_t *src_ts,
220 traffic_selector_t *dst_ts,
221 policy_dir_t direction, protocol_id_t protocol,
222 u_int32_t reqid, bool high_prio,
223 mode_t mode, bool update);
224
225 /**
226 * @brief Query the use time of a policy.
227 *
228 * The use time of a policy is the time the policy was used
229 * for the last time.
230 *
231 * @param this calling object
232 * @param src_ts traffic selector to match traffic source
233 * @param dst_ts traffic selector to match traffic dest
234 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
235 * @param[out] use_time the time of this SA's last use
236 * @return
237 * - SUCCESS
238 * - FAILED if kernel comm failed
239 */
240 status_t (*query_policy) (kernel_interface_t *this,
241 traffic_selector_t *src_ts,
242 traffic_selector_t *dst_ts,
243 policy_dir_t direction, u_int32_t *use_time);
244
245 /**
246 * @brief Remove a policy from the SPD.
247 *
248 * The kernel interface implements reference counting for policies.
249 * If the same policy is installed multiple times (in the case of rekeying),
250 * the reference counter is increased. del_policy() decreases the ref counter
251 * and removes the policy only when no more references are available.
252 *
253 * @param this calling object
254 * @param src_ts traffic selector to match traffic source
255 * @param dst_ts traffic selector to match traffic dest
256 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
257 * @return
258 * - SUCCESS
259 * - FAILED if kernel comm failed
260 */
261 status_t (*del_policy) (kernel_interface_t *this,
262 traffic_selector_t *src_ts,
263 traffic_selector_t *dst_ts,
264 policy_dir_t direction);
265
266 /**
267 * @brief Destroys a kernel_interface object.
268 *
269 * @param kernel_interface_t calling object
270 */
271 void (*destroy) (kernel_interface_t *kernel_interface);
272 };
273
274 /**
275 * @brief Creates an object of type kernel_interface_t.
276 *
277 * @ingroup threads
278 */
279 kernel_interface_t *kernel_interface_create(void);
280
281 #endif /*KERNEL_INTERFACE_H_*/