bafb1a6d16cffdbbf604e0af292cabb077092899
[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 #include <utils/host.h>
29 #include <crypto/prf_plus.h>
30 #include <encoding/payloads/proposal_substructure.h>
31
32 typedef struct natt_conf_t natt_conf_t;
33
34 /**
35 * Configuration for NAT-T
36 */
37 struct natt_conf_t {
38 /** source port to use for UDP-encapsulated packets */
39 u_int16_t sport;
40 /** dest port to use for UDP-encapsulated packets */
41 u_int16_t dport;
42 };
43
44 typedef enum policy_dir_t policy_dir_t;
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 typedef struct kernel_interface_t kernel_interface_t;
61
62 /**
63 * @brief Interface to the kernel.
64 *
65 * The kernel interface handles the communication with the kernel
66 * for SA and policy management. It allows setup of these, and provides
67 * further the handling of kernel events.
68 * Policy information are cached in the interface. This is necessary to do
69 * reference counting. The Linux kernel does not allow the same policy
70 * installed twice, but we need this as CHILD_SA exist multiple times
71 * when rekeying. Thats why we do reference counting of policies.
72 *
73 * @b Constructors:
74 * - kernel_interface_create()
75 *
76 * @ingroup threads
77 */
78 struct kernel_interface_t {
79
80 /**
81 * @brief Get a SPI from the kernel.
82 *
83 * @warning get_spi() implicitely creates an SA with
84 * the allocated SPI, therefore the replace flag
85 * in add_sa() must be set when installing this SA.
86 *
87 * @param this calling object
88 * @param src source address of SA
89 * @param dst destination address of SA
90 * @param protocol protocol for SA (ESP/AH)
91 * @param reqid unique ID for this SA
92 * @param[out] spi allocated spi
93 * @return
94 * - SUCCESS
95 * - FAILED if kernel comm failed
96 */
97 status_t (*get_spi)(kernel_interface_t *this, host_t *src, host_t *dst,
98 protocol_id_t protocol, u_int32_t reqid, u_int32_t *spi);
99
100 /**
101 * @brief Add an SA to the SAD.
102 *
103 * add_sa() may update an already allocated
104 * SPI (via get_spi). In this case, the replace
105 * flag must be set.
106 * This function does install a single SA for a
107 * single protocol in one direction. The kernel-interface
108 * gets the keys itself from the PRF, as we don't know
109 * his algorithms and key sizes.
110 *
111 * @param this calling object
112 * @param src source address for this SA
113 * @param dst destination address for this SA
114 * @param spi SPI allocated by us or remote peer
115 * @param protocol protocol for this SA (ESP/AH)
116 * @param reqid unique ID for this SA
117 * @param expire_soft lifetime in seconds before rekeying
118 * @param expire_hard lieftime in seconds before delete
119 * @param enc_alg Algorithm to use for encryption (ESP only)
120 * @param int_alg Algorithm to use for integrity protection
121 * @param prf_plus PRF to derive keys from
122 * @param natt NAT-T Configuration, or NULL of no NAT-T used
123 * @param replace Should an already installed SA be updated?
124 * @return
125 * - SUCCESS
126 * - FAILED if kernel comm failed
127 */
128 status_t (*add_sa) (kernel_interface_t *this,
129 host_t *src, host_t *dst, u_int32_t spi,
130 protocol_id_t protocol, u_int32_t reqid,
131 u_int64_t expire_soft, u_int64_t expire_hard,
132 algorithm_t *enc_alg, algorithm_t *int_alg,
133 prf_plus_t *prf_plus, natt_conf_t *natt, 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 update update an existing policy, if TRUE
211 * @return
212 * - SUCCESS
213 * - FAILED if kernel comm failed
214 */
215 status_t (*add_policy) (kernel_interface_t *this,
216 host_t *src, host_t *dst,
217 traffic_selector_t *src_ts,
218 traffic_selector_t *dst_ts,
219 policy_dir_t direction, protocol_id_t protocol,
220 u_int32_t reqid, bool update);
221
222 /**
223 * @brief Query the use time of a policy.
224 *
225 * The use time of a policy is the time the policy was used
226 * for the last time.
227 *
228 * @param this calling object
229 * @param src_ts traffic selector to match traffic source
230 * @param dst_ts traffic selector to match traffic dest
231 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
232 * @param[out] use_time the time of this SA's last use
233 * @return
234 * - SUCCESS
235 * - FAILED if kernel comm failed
236 */
237 status_t (*query_policy) (kernel_interface_t *this,
238 traffic_selector_t *src_ts,
239 traffic_selector_t *dst_ts,
240 policy_dir_t direction, u_int32_t *use_time);
241
242 /**
243 * @brief Remove a policy from the SPD.
244 *
245 * The kernel interface implements reference counting for policies.
246 * If the same policy is installed multiple times (in the case of rekeying),
247 * the reference counter is increased. del_policy() decreases the ref counter
248 * and removes the policy only when no more references are available.
249 *
250 * @param this calling object
251 * @param src_ts traffic selector to match traffic source
252 * @param dst_ts traffic selector to match traffic dest
253 * @param direction direction of traffic, POLICY_IN, POLICY_OUT, POLICY_FWD
254 * @return
255 * - SUCCESS
256 * - FAILED if kernel comm failed
257 */
258 status_t (*del_policy) (kernel_interface_t *this,
259 traffic_selector_t *src_ts,
260 traffic_selector_t *dst_ts,
261 policy_dir_t direction);
262
263 /**
264 * @brief Destroys a kernel_interface object.
265 *
266 * @param kernel_interface_t calling object
267 */
268 void (*destroy) (kernel_interface_t *kernel_interface);
269 };
270
271 /**
272 * @brief Creates an object of type kernel_interface_t.
273 *
274 * @ingroup threads
275 */
276 kernel_interface_t *kernel_interface_create(void);
277
278 #endif /*KERNEL_INTERFACE_H_*/