Some whitespace and code style fixes.
[strongswan.git] / src / libcharon / kernel / kernel_ipsec.h
1 /*
2 * Copyright (C) 2006-2009 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 kernel
22 */
23
24 #ifndef KERNEL_IPSEC_H_
25 #define KERNEL_IPSEC_H_
26
27 typedef enum ipsec_mode_t ipsec_mode_t;
28 typedef enum policy_dir_t policy_dir_t;
29 typedef struct kernel_ipsec_t kernel_ipsec_t;
30
31 #include <utils/host.h>
32 #include <crypto/prf_plus.h>
33 #include <config/proposal.h>
34 #include <config/child_cfg.h>
35
36 /**
37 * Mode of a CHILD_SA.
38 */
39 enum ipsec_mode_t {
40 /** transport mode, no inner address */
41 MODE_TRANSPORT = 1,
42 /** tunnel mode, inner and outer addresses */
43 MODE_TUNNEL,
44 /** BEET mode, tunnel mode but fixed, bound inner addresses */
45 MODE_BEET,
46 };
47
48 /**
49 * enum names for ipsec_mode_t.
50 */
51 extern enum_name_t *ipsec_mode_names;
52
53 /**
54 * Direction of a policy. These are equal to those
55 * defined in xfrm.h, but we want to stay implementation
56 * neutral here.
57 */
58 enum policy_dir_t {
59 /** Policy for inbound traffic */
60 POLICY_IN = 0,
61 /** Policy for outbound traffic */
62 POLICY_OUT = 1,
63 /** Policy for forwarded traffic */
64 POLICY_FWD = 2,
65 };
66
67 /**
68 * enum names for policy_dir_t.
69 */
70 extern enum_name_t *policy_dir_names;
71
72 /**
73 * Interface to the ipsec subsystem of the kernel.
74 *
75 * The kernel ipsec interface handles the communication with the kernel
76 * for SA and policy management. It allows setup of these, and provides
77 * further the handling of kernel events.
78 * Policy information are cached in the interface. This is necessary to do
79 * reference counting. The Linux kernel does not allow the same policy
80 * installed twice, but we need this as CHILD_SA exist multiple times
81 * when rekeying. Thats why we do reference counting of policies.
82 */
83 struct kernel_ipsec_t {
84
85 /**
86 * Get a SPI from the kernel.
87 *
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 spi allocated spi
93 * @return SUCCESS if operation completed
94 */
95 status_t (*get_spi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
96 protocol_id_t protocol, u_int32_t reqid, u_int32_t *spi);
97
98 /**
99 * Get a Compression Parameter Index (CPI) from the kernel.
100 *
101 * @param src source address of SA
102 * @param dst destination address of SA
103 * @param reqid unique ID for the corresponding SA
104 * @param cpi allocated cpi
105 * @return SUCCESS if operation completed
106 */
107 status_t (*get_cpi)(kernel_ipsec_t *this, host_t *src, host_t *dst,
108 u_int32_t reqid, u_int16_t *cpi);
109
110 /**
111 * Add an SA to the SAD.
112 *
113 * add_sa() may update an already allocated
114 * SPI (via get_spi). In this case, the replace
115 * flag must be set.
116 * This function does install a single SA for a
117 * single protocol in one direction.
118 *
119 * @param src source address for this SA
120 * @param dst destination address for this SA
121 * @param spi SPI allocated by us or remote peer
122 * @param protocol protocol for this SA (ESP/AH)
123 * @param reqid unique ID for this SA
124 * @param mark mark for this SA
125 * @param lifetime lifetime_cfg_t for this SA
126 * @param enc_alg Algorithm to use for encryption (ESP only)
127 * @param enc_key key to use for encryption
128 * @param int_alg Algorithm to use for integrity protection
129 * @param int_key key to use for integrity protection
130 * @param mode mode of the SA (tunnel, transport)
131 * @param ipcomp IPComp transform to use
132 * @param cpi CPI for IPComp
133 * @param encap enable UDP encapsulation for NAT traversal
134 * @param inbound TRUE if this is an inbound SA
135 * @param src_ts traffic selector with BEET source address
136 * @param dst_ts traffic selector with BEET destination address
137 * @return SUCCESS if operation completed
138 */
139 status_t (*add_sa) (kernel_ipsec_t *this,
140 host_t *src, host_t *dst, u_int32_t spi,
141 protocol_id_t protocol, u_int32_t reqid,
142 mark_t mark, lifetime_cfg_t *lifetime,
143 u_int16_t enc_alg, chunk_t enc_key,
144 u_int16_t int_alg, chunk_t int_key,
145 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
146 bool encap, bool inbound,
147 traffic_selector_t *src_ts, traffic_selector_t *dst_ts);
148
149 /**
150 * Update the hosts on an installed SA.
151 *
152 * We cannot directly update the destination address as the kernel
153 * requires the spi, the protocol AND the destination address (and family)
154 * to identify SAs. Therefore if the destination address changed we
155 * create a new SA and delete the old one.
156 *
157 * @param spi SPI of the SA
158 * @param protocol protocol for this SA (ESP/AH)
159 * @param cpi CPI for IPComp, 0 if no IPComp is used
160 * @param src current source address
161 * @param dst current destination address
162 * @param new_src new source address
163 * @param new_dst new destination address
164 * @param encap current use of UDP encapsulation
165 * @param new_encap new use of UDP encapsulation
166 * @param mark optional mark for this SA
167 * @return SUCCESS if operation completed, NOT_SUPPORTED if
168 * the kernel interface can't update the SA
169 */
170 status_t (*update_sa)(kernel_ipsec_t *this,
171 u_int32_t spi, protocol_id_t protocol, u_int16_t cpi,
172 host_t *src, host_t *dst,
173 host_t *new_src, host_t *new_dst,
174 bool encap, bool new_encap, mark_t mark);
175
176 /**
177 * Query the number of bytes processed by an SA from the SAD.
178 *
179 * @param src source address for this SA
180 * @param dst destination address for this SA
181 * @param spi SPI allocated by us or remote peer
182 * @param protocol protocol for this SA (ESP/AH)
183 * @param mark optional mark for this SA
184 * @param[out] bytes the number of bytes processed by SA
185 * @return SUCCESS if operation completed
186 */
187 status_t (*query_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
188 u_int32_t spi, protocol_id_t protocol, mark_t mark,
189 u_int64_t *bytes);
190
191 /**
192 * Delete a previusly installed SA from the SAD.
193 *
194 * @param src source address for this SA
195 * @param dst destination address for this SA
196 * @param spi SPI allocated by us or remote peer
197 * @param protocol protocol for this SA (ESP/AH)
198 * @param cpi CPI for IPComp or 0
199 * @param mark optional mark for this SA
200 * @return SUCCESS if operation completed
201 */
202 status_t (*del_sa) (kernel_ipsec_t *this, host_t *src, host_t *dst,
203 u_int32_t spi, protocol_id_t protocol, u_int16_t cpi,
204 mark_t mark);
205
206 /**
207 * Add a policy to the SPD.
208 *
209 * A policy is always associated to an SA. Traffic which matches a
210 * policy is handled by the SA with the same reqid.
211 *
212 * @param src source address of SA
213 * @param dst dest address of SA
214 * @param src_ts traffic selector to match traffic source
215 * @param dst_ts traffic selector to match traffic dest
216 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
217 * @param spi SPI of SA
218 * @param protocol protocol to use to protect traffic (AH/ESP)
219 * @param reqid unique ID of an SA to use to enforce policy
220 * @param mark mark for this policy
221 * @param mode mode of SA (tunnel, transport)
222 * @param ipcomp the IPComp transform used
223 * @param cpi CPI for IPComp
224 * @param routed TRUE, if this policy is routed in the kernel
225 * @return SUCCESS if operation completed
226 */
227 status_t (*add_policy) (kernel_ipsec_t *this,
228 host_t *src, host_t *dst,
229 traffic_selector_t *src_ts,
230 traffic_selector_t *dst_ts,
231 policy_dir_t direction, u_int32_t spi,
232 protocol_id_t protocol, u_int32_t reqid,
233 mark_t mark, ipsec_mode_t mode,
234 u_int16_t ipcomp, u_int16_t cpi, bool routed);
235
236 /**
237 * Query the use time of a policy.
238 *
239 * The use time of a policy is the time the policy was used for the last
240 * time. It is not the system time, but a monotonic timestamp as returned
241 * by time_monotonic.
242 *
243 * @param src_ts traffic selector to match traffic source
244 * @param dst_ts traffic selector to match traffic dest
245 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
246 * @param mark optional mark
247 * @param[out] use_time the monotonic timestamp of this SA's last use
248 * @return SUCCESS if operation completed
249 */
250 status_t (*query_policy) (kernel_ipsec_t *this,
251 traffic_selector_t *src_ts,
252 traffic_selector_t *dst_ts,
253 policy_dir_t direction, mark_t mark,
254 u_int32_t *use_time);
255
256 /**
257 * Remove a policy from the SPD.
258 *
259 * The kernel interface implements reference counting for policies.
260 * If the same policy is installed multiple times (in the case of rekeying),
261 * the reference counter is increased. del_policy() decreases the ref counter
262 * and removes the policy only when no more references are available.
263 *
264 * @param src_ts traffic selector to match traffic source
265 * @param dst_ts traffic selector to match traffic dest
266 * @param direction direction of traffic, POLICY_(IN|OUT|FWD)
267 * @param mark optional mark
268 * @param unrouted TRUE, if this policy is unrouted from the kernel
269 * @return SUCCESS if operation completed
270 */
271 status_t (*del_policy) (kernel_ipsec_t *this,
272 traffic_selector_t *src_ts,
273 traffic_selector_t *dst_ts,
274 policy_dir_t direction, mark_t mark,
275 bool unrouted);
276
277 /**
278 * Install a bypass policy for the given socket.
279 *
280 * @param fd socket file descriptor to setup policy for
281 * @param family protocol family of the socket
282 * @return TRUE of policy set up successfully
283 */
284 bool (*bypass_socket)(kernel_ipsec_t *this, int fd, int family);
285
286 /**
287 * Destroy the implementation.
288 */
289 void (*destroy) (kernel_ipsec_t *this);
290 };
291
292 #endif /** KERNEL_IPSEC_H_ @}*/