7d14ba702f393a57e030bb951de23e95923e78fb
[strongswan.git] / src / charon-tkm / src / tkm / tkm_kernel_ipsec.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <errno.h>
18 #include <netinet/udp.h>
19 #include <linux/xfrm.h>
20 #include <utils/debug.h>
21 #include <utils/chunk.h>
22 #include <tkm/constants.h>
23 #include <tkm/client.h>
24
25 #include "tkm.h"
26 #include "tkm_utils.h"
27 #include "tkm_types.h"
28 #include "tkm_keymat.h"
29 #include "tkm_kernel_sad.h"
30 #include "tkm_kernel_ipsec.h"
31
32 /** From linux/in.h */
33 #ifndef IP_XFRM_POLICY
34 #define IP_XFRM_POLICY 17
35 #endif
36
37 typedef struct private_tkm_kernel_ipsec_t private_tkm_kernel_ipsec_t;
38
39 /**
40 * Private variables and functions of TKM kernel ipsec instance.
41 */
42 struct private_tkm_kernel_ipsec_t {
43
44 /**
45 * Public tkm_kernel_ipsec interface.
46 */
47 tkm_kernel_ipsec_t public;
48
49 /**
50 * RNG used for SPI generation.
51 */
52 rng_t *rng;
53
54 /**
55 * CHILD/ESP SA database.
56 */
57 tkm_kernel_sad_t *sad;
58
59 };
60
61 METHOD(kernel_ipsec_t, get_spi, status_t,
62 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
63 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi)
64 {
65 if (!this->rng)
66 {
67 this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
68 if (!this->rng)
69 {
70 DBG1(DBG_KNL, "unable to create RNG");
71 return FAILED;
72 }
73 }
74
75 DBG1(DBG_KNL, "getting SPI for reqid {%u}", reqid);
76 const bool result = this->rng->get_bytes(this->rng, sizeof(u_int32_t),
77 (u_int8_t *)spi);
78 return result ? SUCCESS : FAILED;
79 }
80
81 METHOD(kernel_ipsec_t, get_cpi, status_t,
82 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
83 u_int32_t reqid, u_int16_t *cpi)
84 {
85 return NOT_SUPPORTED;
86 }
87
88 METHOD(kernel_ipsec_t, add_sa, status_t,
89 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
90 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
91 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
92 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode, u_int16_t ipcomp,
93 u_int16_t cpi, bool encap, bool esn, bool inbound,
94 traffic_selector_t* src_ts, traffic_selector_t* dst_ts)
95 {
96 if (enc_key.ptr == NULL)
97 {
98 DBG1(DBG_KNL, "Unable to get ESA information");
99 return FAILED;
100 }
101 esa_info_t esa = *(esa_info_t *)(enc_key.ptr);
102
103 /* only handle the case where we have both distinct ESP spi's available */
104 if (esa.spi_r == spi)
105 {
106 chunk_free(&esa.nonce_i);
107 chunk_free(&esa.nonce_r);
108 return SUCCESS;
109 }
110
111 /* Initiator if encr_r is passed as enc_key to the inbound add_sa call */
112 const bool initiator = esa.is_encr_r && inbound;
113
114 esp_spi_type spi_loc, spi_rem;
115 host_t *local, *peer;
116 chunk_t *nonce_loc, *nonce_rem;
117 if (initiator)
118 {
119 spi_loc = spi;
120 spi_rem = esa.spi_r;
121 local = dst;
122 peer = src;
123 nonce_loc = &esa.nonce_i;
124 nonce_rem = &esa.nonce_r;
125 }
126 else
127 {
128 spi_loc = esa.spi_r;
129 spi_rem = spi;
130 local = src;
131 peer = dst;
132 nonce_loc = &esa.nonce_r;
133 nonce_rem = &esa.nonce_i;
134 }
135
136 const nc_id_type nonce_loc_id = tkm->chunk_map->get_id(tkm->chunk_map,
137 nonce_loc);
138
139 const esa_id_type esa_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_ESA);
140 if (!this->sad->insert(this->sad, esa_id, peer, local, spi_loc, protocol))
141 {
142 DBG1(DBG_KNL, "unable to add entry (%llu) to SAD", esa_id);
143 goto sad_failure;
144 }
145
146 /*
147 * creation of first CHILD SA:
148 * no nonce and no dh contexts because the ones from the IKE SA are re-used
149 */
150 if (nonce_loc_id == 0 && esa.dh_id == 0)
151 {
152 if (ike_esa_create_first(esa_id, esa.isa_id, reqid, 1, spi_loc, spi_rem)
153 != TKM_OK)
154 {
155 DBG1(DBG_KNL, "child SA (%llu, first) creation failed", esa_id);
156 goto failure;
157 }
158 }
159 /* creation of child SA without PFS: no dh context */
160 else if (nonce_loc_id != 0 && esa.dh_id == 0)
161 {
162 nonce_type nc_rem;
163 chunk_to_sequence(nonce_rem, &nc_rem, sizeof(nonce_type));
164 if (ike_esa_create_no_pfs(esa_id, esa.isa_id, reqid, 1, nonce_loc_id,
165 nc_rem, initiator, spi_loc, spi_rem)
166 != TKM_OK)
167 {
168 DBG1(DBG_KNL, "child SA (%llu, no PFS) creation failed", esa_id);
169 goto failure;
170 }
171 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nonce_loc_id);
172 }
173 /* creation of subsequent child SA with PFS: nonce and dh context are set */
174 else
175 {
176 nonce_type nc_rem;
177 chunk_to_sequence(nonce_rem, &nc_rem, sizeof(nonce_type));
178 if (ike_esa_create(esa_id, esa.isa_id, reqid, 1, esa.dh_id, nonce_loc_id,
179 nc_rem, initiator, spi_loc, spi_rem) != TKM_OK)
180 {
181 DBG1(DBG_KNL, "child SA (%llu) creation failed", esa_id);
182 goto failure;
183 }
184 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nonce_loc_id);
185 }
186 if (ike_esa_select(esa_id) != TKM_OK)
187 {
188 DBG1(DBG_KNL, "error selecting new child SA (%llu)", esa_id);
189 if (ike_esa_reset(esa_id) != TKM_OK)
190 {
191 DBG1(DBG_KNL, "child SA (%llu) deletion failed", esa_id);
192 }
193 goto failure;
194 }
195
196 DBG1(DBG_KNL, "added child SA (esa: %llu, isa: %llu, esp_spi_loc: %x, "
197 "esp_spi_rem: %x, role: %s)", esa_id, esa.isa_id, ntohl(spi_loc),
198 ntohl(spi_rem), initiator ? "initiator" : "responder");
199 chunk_free(&esa.nonce_i);
200 chunk_free(&esa.nonce_r);
201
202 return SUCCESS;
203
204 failure:
205 this->sad->remove(this->sad, esa_id);
206 sad_failure:
207 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_ESA, esa_id);
208 chunk_free(&esa.nonce_i);
209 chunk_free(&esa.nonce_r);
210 return FAILED;
211 }
212
213 METHOD(kernel_ipsec_t, query_sa, status_t,
214 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
215 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes,
216 u_int64_t *packets)
217 {
218 return NOT_SUPPORTED;
219 }
220
221 METHOD(kernel_ipsec_t, del_sa, status_t,
222 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
223 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
224 {
225 const esa_id_type esa_id = this->sad->get_esa_id(this->sad, src, dst, spi,
226 protocol);
227 if (esa_id)
228 {
229 DBG1(DBG_KNL, "deleting child SA (esa: %llu, spi: %x)", esa_id,
230 ntohl(spi));
231 if (ike_esa_reset(esa_id) != TKM_OK)
232 {
233 DBG1(DBG_KNL, "child SA (%llu) deletion failed", esa_id);
234 return FAILED;
235 }
236 this->sad->remove(this->sad, esa_id);
237 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_ESA, esa_id);
238 }
239 return SUCCESS;
240 }
241
242 METHOD(kernel_ipsec_t, update_sa, status_t,
243 private_tkm_kernel_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
244 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
245 bool old_encap, bool new_encap, mark_t mark)
246 {
247 return NOT_SUPPORTED;
248 }
249
250 METHOD(kernel_ipsec_t, flush_sas, status_t,
251 private_tkm_kernel_ipsec_t *this)
252 {
253 DBG1(DBG_KNL, "flushing child SA entries");
254 return SUCCESS;
255 }
256
257 METHOD(kernel_ipsec_t, add_policy, status_t,
258 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
259 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
260 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
261 mark_t mark, policy_priority_t priority)
262 {
263 return SUCCESS;
264 }
265
266 METHOD(kernel_ipsec_t, query_policy, status_t,
267 private_tkm_kernel_ipsec_t *this, traffic_selector_t *src_ts,
268 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
269 u_int32_t *use_time)
270 {
271 return NOT_SUPPORTED;
272 }
273
274 METHOD(kernel_ipsec_t, del_policy, status_t,
275 private_tkm_kernel_ipsec_t *this, traffic_selector_t *src_ts,
276 traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
277 mark_t mark, policy_priority_t prio)
278 {
279 return SUCCESS;
280 }
281
282 METHOD(kernel_ipsec_t, flush_policies, status_t,
283 private_tkm_kernel_ipsec_t *this)
284 {
285 return SUCCESS;
286 }
287
288
289 METHOD(kernel_ipsec_t, bypass_socket, bool,
290 private_tkm_kernel_ipsec_t *this, int fd, int family)
291 {
292 struct xfrm_userpolicy_info policy;
293 u_int sol, ipsec_policy;
294
295 switch (family)
296 {
297 case AF_INET:
298 sol = SOL_IP;
299 ipsec_policy = IP_XFRM_POLICY;
300 break;
301 case AF_INET6:
302 sol = SOL_IPV6;
303 ipsec_policy = IPV6_XFRM_POLICY;
304 break;
305 default:
306 return FALSE;
307 }
308
309 memset(&policy, 0, sizeof(policy));
310 policy.action = XFRM_POLICY_ALLOW;
311 policy.sel.family = family;
312
313 policy.dir = XFRM_POLICY_OUT;
314 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
315 {
316 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
317 strerror(errno));
318 return FALSE;
319 }
320 policy.dir = XFRM_POLICY_IN;
321 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
322 {
323 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
324 strerror(errno));
325 return FALSE;
326 }
327 return TRUE;
328 }
329
330 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
331 private_tkm_kernel_ipsec_t *this, int fd, int family, u_int16_t port)
332 {
333 int type = UDP_ENCAP_ESPINUDP;
334
335 if (setsockopt(fd, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
336 {
337 DBG1(DBG_KNL, "unable to set UDP_ENCAP: %s", strerror(errno));
338 return FALSE;
339 }
340 return TRUE;
341 }
342
343 METHOD(kernel_ipsec_t, destroy, void,
344 private_tkm_kernel_ipsec_t *this)
345 {
346 DESTROY_IF(this->rng);
347 DESTROY_IF(this->sad);
348 free(this);
349 }
350
351 /*
352 * Described in header.
353 */
354 tkm_kernel_ipsec_t *tkm_kernel_ipsec_create()
355 {
356 private_tkm_kernel_ipsec_t *this;
357
358 INIT(this,
359 .public = {
360 .interface = {
361 .get_spi = _get_spi,
362 .get_cpi = _get_cpi,
363 .add_sa = _add_sa,
364 .update_sa = _update_sa,
365 .query_sa = _query_sa,
366 .del_sa = _del_sa,
367 .flush_sas = _flush_sas,
368 .add_policy = _add_policy,
369 .query_policy = _query_policy,
370 .del_policy = _del_policy,
371 .flush_policies = _flush_policies,
372 .bypass_socket = _bypass_socket,
373 .enable_udp_decap = _enable_udp_decap,
374 .destroy = _destroy,
375 },
376 },
377 .sad = tkm_kernel_sad_create(),
378 );
379
380 if (!this->sad)
381 {
382 DBG1(DBG_KNL, "unable to create SAD");
383 destroy(this);
384 return NULL;
385 }
386
387 return &this->public;
388 }