specifying keysize in bits, as it is required in IKEv2
[strongswan.git] / src / charon / sa / child_sa.c
1 /**
2 * @file child_sa.c
3 *
4 * @brief Implementation of child_sa_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <netdb.h>
24
25 #include "child_sa.h"
26
27 #include <daemon.h>
28
29
30 typedef struct sa_policy_t sa_policy_t;
31
32 /**
33 * Struct used to store information for a policy. This
34 * is needed since we must provide all this information
35 * for deleting a policy...
36 */
37 struct sa_policy_t {
38
39 struct {
40 /** subnet address behind peer peer */
41 host_t *net;
42 /** netmask used for net */
43 u_int8_t net_mask;
44 } me, other;
45
46 /**
47 * Protocol for this policy, such as TCP/UDP/ICMP...
48 */
49 int upper_proto;
50 };
51
52 typedef struct private_child_sa_t private_child_sa_t;
53
54 /**
55 * Private data of a child_sa_t object.
56 */
57 struct private_child_sa_t {
58 /**
59 * Public interface of child_sa_t.
60 */
61 child_sa_t public;
62
63 struct {
64 /** address of peer */
65 host_t *addr;
66 /** actual used SPI, 0 if unused */
67 u_int32_t spi;
68 } me, other;
69
70 /**
71 * Protocol used to protect this SA, ESP|AH
72 */
73 protocol_id_t protocol;
74
75 /**
76 * List containing sa_policy_t objects
77 */
78 linked_list_t *policies;
79
80 /**
81 * reqid used for this child_sa
82 */
83 u_int32_t reqid;
84
85 /**
86 * Lifetime before rekeying
87 */
88 u_int32_t soft_lifetime;
89
90 /**
91 * Lifetime before delete
92 */
93 u_int32_t hard_lifetime;
94
95 /**
96 * CHILD_SAs own logger
97 */
98 logger_t *logger;
99 };
100
101 /**
102 * Implements child_sa_t.get_reqid
103 */
104 static u_int32_t get_reqid(private_child_sa_t *this)
105 {
106 return this->reqid;
107 }
108
109 /**
110 * Implements child_sa_t.get_spi
111 */
112 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
113 {
114 if (inbound)
115 {
116 return this->me.spi;
117 }
118 return this->other.spi;
119 }
120
121 /**
122 * Implements child_sa_t.get_protocol
123 */
124 protocol_id_t get_protocol(private_child_sa_t *this)
125 {
126 return this->protocol;
127 }
128
129 /**
130 * Implements child_sa_t.alloc
131 */
132 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
133 {
134 protocol_id_t protocol;
135 iterator_t *iterator;
136 proposal_t *proposal;
137 status_t status;
138
139 /* iterator through proposals */
140 iterator = proposals->create_iterator(proposals, TRUE);
141 while(iterator->has_next(iterator))
142 {
143 iterator->current(iterator, (void**)&proposal);
144 protocol = proposal->get_protocol(proposal);
145
146 status = charon->kernel_interface->get_spi(
147 charon->kernel_interface,
148 this->other.addr, this->me.addr,
149 protocol, FALSE,
150 &this->me.spi);
151
152 if (status != SUCCESS)
153 {
154 iterator->destroy(iterator);
155 return FAILED;
156 }
157 /* update proposal */
158 proposal->set_spi(proposal, (u_int64_t)this->me.spi);
159 }
160 iterator->destroy(iterator);
161 return SUCCESS;
162 }
163
164 static status_t install(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus, bool mine)
165 {
166 u_int32_t spi;
167 algorithm_t *enc_algo, *int_algo;
168 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
169 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
170 host_t *src;
171 host_t *dst;
172 status_t status;
173
174 /* we must assign the roles to correctly set up the SAs */
175 if (mine)
176 {
177 dst = this->me.addr;
178 src = this->other.addr;
179 }
180 else
181 {
182 src = this->me.addr;
183 dst = this->other.addr;
184 }
185
186 this->protocol = proposal->get_protocol(proposal);
187
188 /* now we have to decide which spi to use. Use self allocated, if "mine",
189 * or the one in the proposal, if not "mine" (others). */
190 if (mine)
191 {
192 spi = this->me.spi;
193 }
194 else
195 {
196 spi = proposal->get_spi(proposal);
197 this->other.spi = spi;
198 }
199
200 this->logger->log(this->logger, CONTROL|LEVEL1, "Adding %s %s SA",
201 mine ? "inbound" : "outbound",
202 mapping_find(protocol_id_m, this->protocol));
203
204 /* select encryption algo */
205 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
206 {
207 this->logger->log(this->logger, CONTROL|LEVEL2, " using %s for encryption",
208 mapping_find(encryption_algorithm_m, enc_algo->algorithm));
209 }
210 else
211 {
212 enc_algo = &enc_algo_none;
213 }
214
215 /* select integrity algo */
216 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
217 {
218 this->logger->log(this->logger, CONTROL|LEVEL2, " using %s for integrity",
219 mapping_find(integrity_algorithm_m, int_algo->algorithm));
220 }
221 else
222 {
223 int_algo = &int_algo_none;
224 }
225
226 /* send SA down to the kernel */
227 this->logger->log(this->logger, CONTROL|LEVEL2,
228 " SPI 0x%.8x, src %s dst %s",
229 ntohl(spi), src->get_address(src), dst->get_address(dst));
230 status = charon->kernel_interface->add_sa(charon->kernel_interface,
231 src, dst,
232 spi, this->protocol,
233 this->reqid,
234 mine ? 0 : this->soft_lifetime,
235 this->hard_lifetime,
236 enc_algo, int_algo, prf_plus, mine);
237
238 return status;
239 }
240
241 static status_t add(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
242 {
243 linked_list_t *list;
244
245 /* install others (initiators) SAs*/
246 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
247 {
248 return FAILED;
249 }
250
251 /* get SPIs for our SAs */
252 list = linked_list_create();
253 list->insert_last(list, proposal);
254 if (alloc(this, list) != SUCCESS)
255 {
256 list->destroy(list);
257 return FAILED;
258 }
259 list->destroy(list);
260
261 /* install our (responders) SAs */
262 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
263 {
264 return FAILED;
265 }
266
267 return SUCCESS;
268 }
269
270 static status_t update(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
271 {
272 /* install our (initator) SAs */
273 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
274 {
275 return FAILED;
276 }
277 /* install his (responder) SAs */
278 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
279 {
280 return FAILED;
281 }
282
283 return SUCCESS;
284 }
285
286 static status_t add_policies(private_child_sa_t *this, linked_list_t *my_ts_list, linked_list_t *other_ts_list)
287 {
288 iterator_t *my_iter, *other_iter;
289 traffic_selector_t *my_ts, *other_ts;
290
291 /* iterate over both lists */
292 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
293 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
294 while (my_iter->has_next(my_iter))
295 {
296 my_iter->current(my_iter, (void**)&my_ts);
297 other_iter->reset(other_iter);
298 while (other_iter->has_next(other_iter))
299 {
300 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
301 int family;
302 chunk_t from_addr;
303 u_int16_t from_port, to_port;
304 sa_policy_t *policy;
305 status_t status;
306
307 other_iter->current(other_iter, (void**)&other_ts);
308
309 /* only set up policies if protocol matches */
310 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts))
311 {
312 continue;
313 }
314 policy = malloc_thing(sa_policy_t);
315 policy->upper_proto = my_ts->get_protocol(my_ts);
316
317 /* calculate net and ports for local side */
318 family = my_ts->get_type(my_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
319 from_addr = my_ts->get_from_address(my_ts);
320 from_port = my_ts->get_from_port(my_ts);
321 to_port = my_ts->get_to_port(my_ts);
322 from_port = (from_port != to_port) ? 0 : from_port;
323 policy->me.net = host_create_from_chunk(family, from_addr, from_port);
324 policy->me.net_mask = my_ts->get_netmask(my_ts);
325 chunk_free(&from_addr);
326
327 /* calculate net and ports for remote side */
328 family = other_ts->get_type(other_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
329 from_addr = other_ts->get_from_address(other_ts);
330 from_port = other_ts->get_from_port(other_ts);
331 to_port = other_ts->get_to_port(other_ts);
332 from_port = (from_port != to_port) ? 0 : from_port;
333 policy->other.net = host_create_from_chunk(family, from_addr, from_port);
334 policy->other.net_mask = other_ts->get_netmask(other_ts);
335 chunk_free(&from_addr);
336
337 /* install 3 policies: out, in and forward */
338 status = charon->kernel_interface->add_policy(charon->kernel_interface,
339 this->me.addr, this->other.addr,
340 policy->me.net, policy->other.net,
341 policy->me.net_mask, policy->other.net_mask,
342 XFRM_POLICY_OUT, policy->upper_proto,
343 this->protocol == PROTO_AH,
344 this->protocol == PROTO_ESP,
345 this->reqid);
346
347 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
348 this->other.addr, this->me.addr,
349 policy->other.net, policy->me.net,
350 policy->other.net_mask, policy->me.net_mask,
351 XFRM_POLICY_IN, policy->upper_proto,
352 this->protocol == PROTO_AH,
353 this->protocol == PROTO_ESP,
354 this->reqid);
355
356 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
357 this->other.addr, this->me.addr,
358 policy->other.net, policy->me.net,
359 policy->other.net_mask, policy->me.net_mask,
360 XFRM_POLICY_FWD, policy->upper_proto,
361 this->protocol == PROTO_AH,
362 this->protocol == PROTO_ESP,
363 this->reqid);
364
365 if (status != SUCCESS)
366 {
367 my_iter->destroy(my_iter);
368 other_iter->destroy(other_iter);
369 policy->me.net->destroy(policy->me.net);
370 policy->other.net->destroy(policy->other.net);
371 free(policy);
372 return status;
373 }
374
375 /* add it to the policy list, since we want to know which policies we own */
376 this->policies->insert_last(this->policies, policy);
377 }
378 }
379 my_iter->destroy(my_iter);
380 other_iter->destroy(other_iter);
381 return SUCCESS;
382 }
383
384 /**
385 * Implementation of child_sa_t.log_status.
386 */
387 static void log_status(private_child_sa_t *this, logger_t *logger, char* name)
388 {
389 iterator_t *iterator;
390 sa_policy_t *policy;
391 struct protoent *proto;
392 char proto_buf[8] = "";
393 char *proto_name = proto_buf;
394
395 if (logger == NULL)
396 {
397 logger = this->logger;
398 }
399 logger->log(logger, CONTROL|LEVEL1, " \"%s\": protected with %s (0x%x/0x%x), reqid %d:",
400 name,
401 this->protocol == PROTO_ESP ? "ESP" : "AH",
402 htonl(this->me.spi), htonl(this->other.spi),
403 this->reqid);
404 iterator = this->policies->create_iterator(this->policies, TRUE);
405 while (iterator->has_next(iterator))
406 {
407 iterator->current(iterator, (void**)&policy);
408 if (policy->upper_proto)
409 {
410 proto = getprotobynumber(policy->upper_proto);
411 if (proto)
412 {
413 proto_name = proto->p_name;
414 }
415 else
416 {
417 snprintf(proto_buf, sizeof(proto_buf), "<%d>", policy->upper_proto);
418 }
419 }
420 logger->log(logger, CONTROL, " \"%s\": %s/%d==%s==%s/%d",
421 name,
422 policy->me.net->get_address(policy->me.net), policy->me.net_mask,
423 proto_name,
424 policy->other.net->get_address(policy->other.net), policy->other.net_mask);
425 }
426 iterator->destroy(iterator);
427 }
428
429 /**
430 * Implementation of child_sa_t.destroy.
431 */
432 static void destroy(private_child_sa_t *this)
433 {
434 /* delete all policies in the kernel */
435 sa_policy_t *policy;
436 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
437 {
438 charon->kernel_interface->del_policy(charon->kernel_interface,
439 this->me.addr, this->other.addr,
440 policy->me.net, policy->other.net,
441 policy->me.net_mask, policy->other.net_mask,
442 XFRM_POLICY_OUT, policy->upper_proto);
443
444 charon->kernel_interface->del_policy(charon->kernel_interface,
445 this->other.addr, this->me.addr,
446 policy->other.net, policy->me.net,
447 policy->other.net_mask, policy->me.net_mask,
448 XFRM_POLICY_IN, policy->upper_proto);
449
450 charon->kernel_interface->del_policy(charon->kernel_interface,
451 this->other.addr, this->me.addr,
452 policy->other.net, policy->me.net,
453 policy->other.net_mask, policy->me.net_mask,
454 XFRM_POLICY_FWD, policy->upper_proto);
455
456 policy->me.net->destroy(policy->me.net);
457 policy->other.net->destroy(policy->other.net);
458 free(policy);
459 }
460 this->policies->destroy(this->policies);
461
462 /* delete SAs in the kernel, if they are set up */
463 if (this->protocol != PROTO_NONE)
464 {
465 charon->kernel_interface->del_sa(charon->kernel_interface,
466 this->other.addr, this->me.spi, this->protocol);
467 charon->kernel_interface->del_sa(charon->kernel_interface,
468 this->me.addr, this->other.spi, this->protocol);
469 }
470 free(this);
471 }
472
473 /*
474 * Described in header.
475 */
476 child_sa_t * child_sa_create(host_t *me, host_t* other, u_int32_t soft_lifetime, u_int32_t hard_lifetime)
477 {
478 static u_int32_t reqid = 2000000000;
479 private_child_sa_t *this = malloc_thing(private_child_sa_t);
480
481 /* public functions */
482 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
483 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
484 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
485 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
486 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))add;
487 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))update;
488 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*))add_policies;
489 this->public.log_status = (void (*)(child_sa_t*, logger_t*, char*))log_status;
490 this->public.destroy = (void(*)(child_sa_t*))destroy;
491
492 /* private data */
493 this->logger = logger_manager->get_logger(logger_manager, CHILD_SA);
494 this->me.addr = me;
495 this->other.addr = other;
496 this->me.spi = 0;
497 this->other.spi = 0;
498 this->soft_lifetime = soft_lifetime;
499 this->hard_lifetime = hard_lifetime;
500 this->reqid = ++reqid;
501 this->policies = linked_list_create();
502 this->protocol = PROTO_NONE;
503
504 return (&this->public);
505 }