Make sure IP_XFRM_POLICY is defined
[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 DBG1(DBG_KNL, "getting SPI for reqid {%u}", reqid);
66 const bool result = this->rng->get_bytes(this->rng, sizeof(u_int32_t),
67 (u_int8_t *)spi);
68 return result ? SUCCESS : FAILED;
69 }
70
71 METHOD(kernel_ipsec_t, get_cpi, status_t,
72 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
73 u_int32_t reqid, u_int16_t *cpi)
74 {
75 return NOT_SUPPORTED;
76 }
77
78 METHOD(kernel_ipsec_t, add_sa, status_t,
79 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
80 u_int32_t spi, u_int8_t protocol, u_int32_t reqid, mark_t mark,
81 u_int32_t tfc, lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
82 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode, u_int16_t ipcomp,
83 u_int16_t cpi, bool encap, bool esn, bool inbound,
84 traffic_selector_t* src_ts, traffic_selector_t* dst_ts)
85 {
86 if (enc_key.ptr == NULL)
87 {
88 DBG1(DBG_KNL, "Unable to get ESA information");
89 return FAILED;
90 }
91 esa_info_t esa = *(esa_info_t *)(enc_key.ptr);
92
93 /* only handle the case where we have both distinct ESP spi's available */
94 if (esa.spi_r == spi)
95 {
96 chunk_free(&esa.nonce_i);
97 chunk_free(&esa.nonce_r);
98 return SUCCESS;
99 }
100
101 /* Initiator if encr_r is passed as enc_key to the inbound add_sa call */
102 const bool initiator = esa.is_encr_r && inbound;
103
104 esp_spi_type spi_loc, spi_rem;
105 host_t *local, *peer;
106 chunk_t *nonce_loc, *nonce_rem;
107 if (initiator)
108 {
109 spi_loc = spi;
110 spi_rem = esa.spi_r;
111 local = dst;
112 peer = src;
113 nonce_loc = &esa.nonce_i;
114 nonce_rem = &esa.nonce_r;
115 }
116 else
117 {
118 spi_loc = esa.spi_r;
119 spi_rem = spi;
120 local = src;
121 peer = dst;
122 nonce_loc = &esa.nonce_r;
123 nonce_rem = &esa.nonce_i;
124 }
125
126 const nc_id_type nonce_loc_id = tkm->chunk_map->get_id(tkm->chunk_map,
127 nonce_loc);
128
129 const esa_id_type esa_id = tkm->idmgr->acquire_id(tkm->idmgr, TKM_CTX_ESA);
130 if (!this->sad->insert(this->sad, esa_id, peer, local, spi_loc, protocol))
131 {
132 DBG1(DBG_KNL, "unable to add entry (%llu) to SAD", esa_id);
133 goto sad_failure;
134 }
135
136 /*
137 * creation of first CHILD SA:
138 * no nonce and no dh contexts because the ones from the IKE SA are re-used
139 */
140 if (nonce_loc_id == 0 && esa.dh_id == 0)
141 {
142 if (ike_esa_create_first(esa_id, esa.isa_id, reqid, 1, ntohl(spi_loc),
143 ntohl(spi_rem)) != TKM_OK)
144 {
145 DBG1(DBG_KNL, "child SA (%llu, first) creation failed", esa_id);
146 goto failure;
147 }
148 }
149 /* creation of child SA without PFS: no dh context */
150 else if (nonce_loc_id != 0 && esa.dh_id == 0)
151 {
152 nonce_type nc_rem;
153 chunk_to_sequence(nonce_rem, &nc_rem, sizeof(nonce_type));
154 if (ike_esa_create_no_pfs(esa_id, esa.isa_id, reqid, 1, nonce_loc_id,
155 nc_rem, initiator, ntohl(spi_loc),
156 ntohl(spi_rem)) != TKM_OK)
157 {
158 DBG1(DBG_KNL, "child SA (%llu, no PFS) creation failed", esa_id);
159 goto failure;
160 }
161 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nonce_loc_id);
162 }
163 /* creation of subsequent child SA with PFS: nonce and dh context are set */
164 else
165 {
166 nonce_type nc_rem;
167 chunk_to_sequence(nonce_rem, &nc_rem, sizeof(nonce_type));
168 if (ike_esa_create(esa_id, esa.isa_id, reqid, 1, esa.dh_id, nonce_loc_id,
169 nc_rem, initiator, ntohl(spi_loc),
170 ntohl(spi_rem)) != TKM_OK)
171 {
172 DBG1(DBG_KNL, "child SA (%llu) creation failed", esa_id);
173 goto failure;
174 }
175 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_NONCE, nonce_loc_id);
176 }
177 if (ike_esa_select(esa_id) != TKM_OK)
178 {
179 DBG1(DBG_KNL, "error selecting new child SA (%llu)", esa_id);
180 if (ike_esa_reset(esa_id) != TKM_OK)
181 {
182 DBG1(DBG_KNL, "child SA (%llu) deletion failed", esa_id);
183 }
184 goto failure;
185 }
186
187 DBG1(DBG_KNL, "added child SA (esa: %llu, isa: %llu, esp_spi_loc: %x, "
188 "esp_spi_rem: %x, role: %s)", esa_id, esa.isa_id, ntohl(spi_loc),
189 ntohl(spi_rem), initiator ? "initiator" : "responder");
190 chunk_free(&esa.nonce_i);
191 chunk_free(&esa.nonce_r);
192
193 return SUCCESS;
194
195 failure:
196 this->sad->remove(this->sad, esa_id);
197 sad_failure:
198 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_ESA, esa_id);
199 chunk_free(&esa.nonce_i);
200 chunk_free(&esa.nonce_r);
201 return FAILED;
202 }
203
204 METHOD(kernel_ipsec_t, query_sa, status_t,
205 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
206 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes,
207 u_int64_t *packets)
208 {
209 return NOT_SUPPORTED;
210 }
211
212 METHOD(kernel_ipsec_t, del_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, u_int16_t cpi, mark_t mark)
215 {
216 const esa_id_type esa_id = this->sad->get_esa_id(this->sad, src, dst, spi,
217 protocol);
218 if (esa_id)
219 {
220 DBG1(DBG_KNL, "deleting child SA (esa: %llu, spi: %x)", esa_id,
221 ntohl(spi));
222 if (ike_esa_reset(esa_id) != TKM_OK)
223 {
224 DBG1(DBG_KNL, "child SA (%llu) deletion failed", esa_id);
225 return FAILED;
226 }
227 this->sad->remove(this->sad, esa_id);
228 tkm->idmgr->release_id(tkm->idmgr, TKM_CTX_ESA, esa_id);
229 }
230 return SUCCESS;
231 }
232
233 METHOD(kernel_ipsec_t, update_sa, status_t,
234 private_tkm_kernel_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
235 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
236 bool old_encap, bool new_encap, mark_t mark)
237 {
238 return NOT_SUPPORTED;
239 }
240
241 METHOD(kernel_ipsec_t, flush_sas, status_t,
242 private_tkm_kernel_ipsec_t *this)
243 {
244 DBG1(DBG_KNL, "flushing child SA entries");
245 return SUCCESS;
246 }
247
248 METHOD(kernel_ipsec_t, add_policy, status_t,
249 private_tkm_kernel_ipsec_t *this, host_t *src, host_t *dst,
250 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
251 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
252 mark_t mark, policy_priority_t priority)
253 {
254 return SUCCESS;
255 }
256
257 METHOD(kernel_ipsec_t, query_policy, status_t,
258 private_tkm_kernel_ipsec_t *this, traffic_selector_t *src_ts,
259 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
260 u_int32_t *use_time)
261 {
262 return NOT_SUPPORTED;
263 }
264
265 METHOD(kernel_ipsec_t, del_policy, status_t,
266 private_tkm_kernel_ipsec_t *this, traffic_selector_t *src_ts,
267 traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
268 mark_t mark, policy_priority_t prio)
269 {
270 return SUCCESS;
271 }
272
273 METHOD(kernel_ipsec_t, flush_policies, status_t,
274 private_tkm_kernel_ipsec_t *this)
275 {
276 return SUCCESS;
277 }
278
279
280 METHOD(kernel_ipsec_t, bypass_socket, bool,
281 private_tkm_kernel_ipsec_t *this, int fd, int family)
282 {
283 struct xfrm_userpolicy_info policy;
284 u_int sol, ipsec_policy;
285
286 switch (family)
287 {
288 case AF_INET:
289 sol = SOL_IP;
290 ipsec_policy = IP_XFRM_POLICY;
291 break;
292 case AF_INET6:
293 sol = SOL_IPV6;
294 ipsec_policy = IPV6_XFRM_POLICY;
295 break;
296 default:
297 return FALSE;
298 }
299
300 memset(&policy, 0, sizeof(policy));
301 policy.action = XFRM_POLICY_ALLOW;
302 policy.sel.family = family;
303
304 policy.dir = XFRM_POLICY_OUT;
305 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
306 {
307 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
308 strerror(errno));
309 return FALSE;
310 }
311 policy.dir = XFRM_POLICY_IN;
312 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
313 {
314 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
315 strerror(errno));
316 return FALSE;
317 }
318 return TRUE;
319 }
320
321 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
322 private_tkm_kernel_ipsec_t *this, int fd, int family, u_int16_t port)
323 {
324 int type = UDP_ENCAP_ESPINUDP;
325
326 if (setsockopt(fd, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
327 {
328 DBG1(DBG_KNL, "unable to set UDP_ENCAP: %s", strerror(errno));
329 return FALSE;
330 }
331 return TRUE;
332 }
333
334 METHOD(kernel_ipsec_t, destroy, void,
335 private_tkm_kernel_ipsec_t *this)
336 {
337 DESTROY_IF(this->rng);
338 DESTROY_IF(this->sad);
339 free(this);
340 }
341
342 /*
343 * Described in header.
344 */
345 tkm_kernel_ipsec_t *tkm_kernel_ipsec_create()
346 {
347 private_tkm_kernel_ipsec_t *this;
348
349 INIT(this,
350 .public = {
351 .interface = {
352 .get_spi = _get_spi,
353 .get_cpi = _get_cpi,
354 .add_sa = _add_sa,
355 .update_sa = _update_sa,
356 .query_sa = _query_sa,
357 .del_sa = _del_sa,
358 .flush_sas = _flush_sas,
359 .add_policy = _add_policy,
360 .query_policy = _query_policy,
361 .del_policy = _del_policy,
362 .flush_policies = _flush_policies,
363 .bypass_socket = _bypass_socket,
364 .enable_udp_decap = _enable_udp_decap,
365 .destroy = _destroy,
366 },
367 },
368 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
369 .sad = tkm_kernel_sad_create(),
370 );
371
372 if (!this->rng)
373 {
374 DBG1(DBG_KNL, "unable to create RNG");
375 destroy(this);
376 return NULL;
377 }
378 if (!this->sad)
379 {
380 DBG1(DBG_KNL, "unable to create SAD");
381 destroy(this);
382 return NULL;
383 }
384
385 return &this->public;
386 }