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