a62e2b1505f17419a280c5912794c9b9da779abd
[strongswan.git] / src / charon-tkm / src / tkm / tkm_kernel_ipsec.c
1 /*
2 * Copyright (C) 2012-2014 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_ipsec.h"
30
31 /** From linux/in.h */
32 #ifndef IP_XFRM_POLICY
33 #define IP_XFRM_POLICY 17
34 #endif
35
36 typedef struct private_tkm_kernel_ipsec_t private_tkm_kernel_ipsec_t;
37
38 /**
39 * Private variables and functions of TKM kernel ipsec instance.
40 */
41 struct private_tkm_kernel_ipsec_t {
42
43 /**
44 * Public tkm_kernel_ipsec interface.
45 */
46 tkm_kernel_ipsec_t public;
47
48 /**
49 * RNG used for SPI generation.
50 */
51 rng_t *rng;
52
53 };
54
55 METHOD(kernel_ipsec_t, get_spi, status_t,
56 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
57 u_int8_t protocol, u_int32_t *spi)
58 {
59 bool result;
60
61 if (!this->rng)
62 {
63 this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
64 if (!this->rng)
65 {
66 DBG1(DBG_KNL, "unable to create RNG");
67 return FAILED;
68 }
69 }
70
71 result = this->rng->get_bytes(this->rng, sizeof(u_int32_t),
72 (u_int8_t *)spi);
73 return result ? SUCCESS : FAILED;
74 }
75
76 METHOD(kernel_ipsec_t, get_cpi, status_t,
77 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
78 u_int16_t *cpi)
79 {
80 return NOT_SUPPORTED;
81 }
82
83 METHOD(kernel_ipsec_t, add_sa, status_t,
84 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
85 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
86 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
87 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
88 u_int16_t ipcomp, u_int16_t cpi, u_int32_t replay_window,
89 bool _initiator, bool encap, bool esn, bool inbound,
90 linked_list_t* src_ts, linked_list_t* dst_ts)
91 {
92 esa_info_t esa;
93 bool initiator;
94 esp_spi_type spi_loc, spi_rem;
95 host_t *local, *peer;
96 chunk_t *nonce_loc, *nonce_rem;
97 nc_id_type nonce_loc_id;
98 esa_id_type esa_id;
99 nonce_type nc_rem;
100
101 if (enc_key.ptr == NULL)
102 {
103 DBG1(DBG_KNL, "Unable to get ESA information");
104 return FAILED;
105 }
106 esa = *(esa_info_t *)(enc_key.ptr);
107
108 /* only handle the case where we have both distinct ESP spi's available */
109 if (esa.spi_r == spi)
110 {
111 chunk_free(&esa.nonce_i);
112 chunk_free(&esa.nonce_r);
113 return SUCCESS;
114 }
115
116 /* Initiator if encr_r is passed as enc_key to the inbound add_sa call */
117 /* TODO: does the new _initiator parameter have the same meaning? */
118 initiator = esa.is_encr_r && inbound;
119 if (initiator)
120 {
121 spi_loc = spi;
122 spi_rem = esa.spi_r;
123 local = dst;
124 peer = src;
125 nonce_loc = &esa.nonce_i;
126 nonce_rem = &esa.nonce_r;
127 }
128 else
129 {
130 spi_loc = esa.spi_r;
131 spi_rem = spi;
132 local = src;
133 peer = dst;
134 nonce_loc = &esa.nonce_r;
135 nonce_rem = &esa.nonce_i;
136 }
137
138 esa_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_ESA);
139 if (!tkm->sad->insert(tkm->sad, reqid, esa_id, local, peer, spi_rem,
140 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 nonce_loc_id = tkm->chunk_map->get_id(tkm->chunk_map, nonce_loc);
151 if (nonce_loc_id == 0 && esa.dh_id == 0)
152 {
153 if (ike_esa_create_first(esa_id, esa.isa_id, reqid, 1, spi_loc, spi_rem)
154 != TKM_OK)
155 {
156 DBG1(DBG_KNL, "child SA (%llu, first) creation failed", esa_id);
157 goto failure;
158 }
159 }
160 /* creation of child SA without PFS: no dh context */
161 else if (nonce_loc_id != 0 && esa.dh_id == 0)
162 {
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 chunk_to_sequence(nonce_rem, &nc_rem, sizeof(nonce_type));
177 if (ike_esa_create(esa_id, esa.isa_id, reqid, 1, esa.dh_id, nonce_loc_id,
178 nc_rem, initiator, spi_loc, spi_rem) != TKM_OK)
179 {
180 DBG1(DBG_KNL, "child SA (%llu) creation failed", esa_id);
181 goto failure;
182 }
183 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nonce_loc_id);
184 }
185 if (ike_esa_select(esa_id) != TKM_OK)
186 {
187 DBG1(DBG_KNL, "error selecting new child SA (%llu)", esa_id);
188 if (ike_esa_reset(esa_id) != TKM_OK)
189 {
190 DBG1(DBG_KNL, "child SA (%llu) deletion failed", esa_id);
191 }
192 goto failure;
193 }
194
195 DBG1(DBG_KNL, "added child SA (esa: %llu, isa: %llu, esp_spi_loc: %x, "
196 "esp_spi_rem: %x, role: %s)", esa_id, esa.isa_id, ntohl(spi_loc),
197 ntohl(spi_rem), initiator ? "initiator" : "responder");
198 chunk_free(&esa.nonce_i);
199 chunk_free(&esa.nonce_r);
200
201 return SUCCESS;
202
203 failure:
204 tkm->sad->remove(tkm->sad, esa_id);
205 sad_failure:
206 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_ESA, esa_id);
207 chunk_free(&esa.nonce_i);
208 chunk_free(&esa.nonce_r);
209 return FAILED;
210 }
211
212 METHOD(kernel_ipsec_t, query_sa, status_t,
213 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
214 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes,
215 u_int64_t *packets, time_t *time)
216 {
217 return NOT_SUPPORTED;
218 }
219
220 METHOD(kernel_ipsec_t, del_sa, status_t,
221 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
222 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
223 {
224 esa_id_type esa_id;
225
226 esa_id = tkm->sad->get_esa_id(tkm->sad, src, dst, spi, 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 tkm->sad->remove(tkm->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 time_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 free(this);
348 }
349
350 /*
351 * Described in header.
352 */
353 tkm_kernel_ipsec_t *tkm_kernel_ipsec_create()
354 {
355 private_tkm_kernel_ipsec_t *this;
356
357 INIT(this,
358 .public = {
359 .interface = {
360 .get_spi = _get_spi,
361 .get_cpi = _get_cpi,
362 .add_sa = _add_sa,
363 .update_sa = _update_sa,
364 .query_sa = _query_sa,
365 .del_sa = _del_sa,
366 .flush_sas = _flush_sas,
367 .add_policy = _add_policy,
368 .query_policy = _query_policy,
369 .del_policy = _del_policy,
370 .flush_policies = _flush_policies,
371 .bypass_socket = _bypass_socket,
372 .enable_udp_decap = _enable_udp_decap,
373 .destroy = _destroy,
374 },
375 },
376 );
377
378 return &this->public;
379 }