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