fixed compile warnings when using -Wall
[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 encryption_algorithm_t enc_algo;
168 integrity_algorithm_t int_algo;
169 chunk_t enc_key, int_key;
170 algorithm_t *algo;
171 crypter_t *crypter;
172 signer_t *signer;
173 size_t key_size;
174 host_t *src;
175 host_t *dst;
176 status_t status;
177
178 /* we must assign the roles to correctly set up the SAs */
179 if (mine)
180 {
181 dst = this->me.addr;
182 src = this->other.addr;
183 }
184 else
185 {
186 src = this->me.addr;
187 dst = this->other.addr;
188 }
189
190 this->protocol = proposal->get_protocol(proposal);
191
192 /* now we have to decide which spi to use. Use self allocated, if "mine",
193 * or the one in the proposal, if not "mine" (others). */
194 if (mine)
195 {
196 spi = this->me.spi;
197 }
198 else
199 {
200 spi = proposal->get_spi(proposal);
201 this->other.spi = spi;
202 }
203
204 /* derive encryption key first */
205 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &algo))
206 {
207 enc_algo = algo->algorithm;
208 this->logger->log(this->logger, CONTROL|LEVEL1, "%s for %s: using %s %s, ",
209 mapping_find(protocol_id_m, this->protocol),
210 mine ? "me" : "other",
211 mapping_find(transform_type_m, ENCRYPTION_ALGORITHM),
212 mapping_find(encryption_algorithm_m, enc_algo));
213
214 /* we must create a (unused) crypter, since its the only way to get the size
215 * of the key. This is not so nice, since charon must support all algorithms
216 * the kernel supports...
217 * TODO: build something of a encryption algorithm lookup function
218 */
219 crypter = crypter_create(enc_algo, algo->key_size);
220 key_size = crypter->get_key_size(crypter);
221 crypter->destroy(crypter);
222 prf_plus->allocate_bytes(prf_plus, key_size, &enc_key);
223 this->logger->log_chunk(this->logger, PRIVATE, "key:", enc_key);
224 }
225 else
226 {
227 enc_algo = ENCR_UNDEFINED;
228 }
229
230 /* derive integrity key */
231 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &algo))
232 {
233 int_algo = algo->algorithm;
234 this->logger->log(this->logger, CONTROL|LEVEL1, "%s for %s: using %s %s,",
235 mapping_find(protocol_id_m, this->protocol),
236 mine ? "me" : "other",
237 mapping_find(transform_type_m, INTEGRITY_ALGORITHM),
238 mapping_find(integrity_algorithm_m, algo->algorithm));
239
240 signer = signer_create(int_algo);
241 key_size = signer->get_key_size(signer);
242 signer->destroy(signer);
243 prf_plus->allocate_bytes(prf_plus, key_size, &int_key);
244 this->logger->log_chunk(this->logger, PRIVATE, "key:", int_key);
245 }
246 else
247 {
248 int_algo = AUTH_UNDEFINED;
249 }
250
251 /* send keys down to kernel */
252 this->logger->log(this->logger, CONTROL|LEVEL1,
253 "installing 0x%.8x for %s, src %s dst %s",
254 ntohl(spi), mapping_find(protocol_id_m, this->protocol),
255 src->get_address(src), dst->get_address(dst));
256 status = charon->kernel_interface->add_sa(charon->kernel_interface,
257 src, dst,
258 spi, this->protocol,
259 this->reqid,
260 mine ? 0 : this->soft_lifetime,
261 this->hard_lifetime,
262 enc_algo, enc_key,
263 int_algo, int_key, mine);
264 /* clean up */
265 if (enc_algo != ENCR_UNDEFINED)
266 {
267 chunk_free(&enc_key);
268 }
269 if (int_algo != AUTH_UNDEFINED)
270 {
271 chunk_free(&int_key);
272 }
273 return status;
274 }
275
276 static status_t add(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
277 {
278 linked_list_t *list;
279
280 /* install others (initiators) SAs*/
281 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
282 {
283 return FAILED;
284 }
285
286 /* get SPIs for our SAs */
287 list = linked_list_create();
288 list->insert_last(list, proposal);
289 if (alloc(this, list) != SUCCESS)
290 {
291 list->destroy(list);
292 return FAILED;
293 }
294 list->destroy(list);
295
296 /* install our (responders) SAs */
297 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
298 {
299 return FAILED;
300 }
301
302 return SUCCESS;
303 }
304
305 static status_t update(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
306 {
307 /* install our (initator) SAs */
308 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
309 {
310 return FAILED;
311 }
312 /* install his (responder) SAs */
313 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
314 {
315 return FAILED;
316 }
317
318 return SUCCESS;
319 }
320
321 static status_t add_policies(private_child_sa_t *this, linked_list_t *my_ts_list, linked_list_t *other_ts_list)
322 {
323 iterator_t *my_iter, *other_iter;
324 traffic_selector_t *my_ts, *other_ts;
325
326 /* iterate over both lists */
327 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
328 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
329 while (my_iter->has_next(my_iter))
330 {
331 my_iter->current(my_iter, (void**)&my_ts);
332 other_iter->reset(other_iter);
333 while (other_iter->has_next(other_iter))
334 {
335 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
336 int family;
337 chunk_t from_addr;
338 u_int16_t from_port, to_port;
339 sa_policy_t *policy;
340 status_t status;
341
342 other_iter->current(other_iter, (void**)&other_ts);
343
344 /* only set up policies if protocol matches */
345 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts))
346 {
347 continue;
348 }
349 policy = malloc_thing(sa_policy_t);
350 policy->upper_proto = my_ts->get_protocol(my_ts);
351
352 /* calculate net and ports for local side */
353 family = my_ts->get_type(my_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
354 from_addr = my_ts->get_from_address(my_ts);
355 from_port = my_ts->get_from_port(my_ts);
356 to_port = my_ts->get_to_port(my_ts);
357 from_port = (from_port != to_port) ? 0 : from_port;
358 policy->me.net = host_create_from_chunk(family, from_addr, from_port);
359 policy->me.net_mask = my_ts->get_netmask(my_ts);
360 chunk_free(&from_addr);
361
362 /* calculate net and ports for remote side */
363 family = other_ts->get_type(other_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
364 from_addr = other_ts->get_from_address(other_ts);
365 from_port = other_ts->get_from_port(other_ts);
366 to_port = other_ts->get_to_port(other_ts);
367 from_port = (from_port != to_port) ? 0 : from_port;
368 policy->other.net = host_create_from_chunk(family, from_addr, from_port);
369 policy->other.net_mask = other_ts->get_netmask(other_ts);
370 chunk_free(&from_addr);
371
372 /* install 3 policies: out, in and forward */
373 status = charon->kernel_interface->add_policy(charon->kernel_interface,
374 this->me.addr, this->other.addr,
375 policy->me.net, policy->other.net,
376 policy->me.net_mask, policy->other.net_mask,
377 XFRM_POLICY_OUT, policy->upper_proto,
378 this->protocol == PROTO_AH,
379 this->protocol == PROTO_ESP,
380 this->reqid);
381
382 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
383 this->other.addr, this->me.addr,
384 policy->other.net, policy->me.net,
385 policy->other.net_mask, policy->me.net_mask,
386 XFRM_POLICY_IN, policy->upper_proto,
387 this->protocol == PROTO_AH,
388 this->protocol == PROTO_ESP,
389 this->reqid);
390
391 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
392 this->other.addr, this->me.addr,
393 policy->other.net, policy->me.net,
394 policy->other.net_mask, policy->me.net_mask,
395 XFRM_POLICY_FWD, policy->upper_proto,
396 this->protocol == PROTO_AH,
397 this->protocol == PROTO_ESP,
398 this->reqid);
399
400 if (status != SUCCESS)
401 {
402 my_iter->destroy(my_iter);
403 other_iter->destroy(other_iter);
404 policy->me.net->destroy(policy->me.net);
405 policy->other.net->destroy(policy->other.net);
406 free(policy);
407 return status;
408 }
409
410 /* add it to the policy list, since we want to know which policies we own */
411 this->policies->insert_last(this->policies, policy);
412 }
413 }
414 my_iter->destroy(my_iter);
415 other_iter->destroy(other_iter);
416 return SUCCESS;
417 }
418
419 /**
420 * Implementation of child_sa_t.log_status.
421 */
422 static void log_status(private_child_sa_t *this, logger_t *logger, char* name)
423 {
424 iterator_t *iterator;
425 sa_policy_t *policy;
426 struct protoent *proto;
427 char proto_buf[8] = "";
428 char *proto_name = proto_buf;
429
430 if (logger == NULL)
431 {
432 logger = this->logger;
433 }
434 logger->log(logger, CONTROL|LEVEL1, " \"%s\": protected with %s (0x%x/0x%x), reqid %d:",
435 name,
436 this->protocol == PROTO_ESP ? "ESP" : "AH",
437 htonl(this->me.spi), htonl(this->other.spi),
438 this->reqid);
439 iterator = this->policies->create_iterator(this->policies, TRUE);
440 while (iterator->has_next(iterator))
441 {
442 iterator->current(iterator, (void**)&policy);
443 if (policy->upper_proto)
444 {
445 proto = getprotobynumber(policy->upper_proto);
446 if (proto)
447 {
448 proto_name = proto->p_name;
449 }
450 else
451 {
452 snprintf(proto_buf, sizeof(proto_buf), "<%d>", policy->upper_proto);
453 }
454 }
455 logger->log(logger, CONTROL, " \"%s\": %s/%d==%s==%s/%d",
456 name,
457 policy->me.net->get_address(policy->me.net), policy->me.net_mask,
458 proto_name,
459 policy->other.net->get_address(policy->other.net), policy->other.net_mask);
460 }
461 iterator->destroy(iterator);
462 }
463
464 /**
465 * Implementation of child_sa_t.destroy.
466 */
467 static void destroy(private_child_sa_t *this)
468 {
469 /* delete all policies in the kernel */
470 sa_policy_t *policy;
471 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
472 {
473 charon->kernel_interface->del_policy(charon->kernel_interface,
474 this->me.addr, this->other.addr,
475 policy->me.net, policy->other.net,
476 policy->me.net_mask, policy->other.net_mask,
477 XFRM_POLICY_OUT, policy->upper_proto);
478
479 charon->kernel_interface->del_policy(charon->kernel_interface,
480 this->other.addr, this->me.addr,
481 policy->other.net, policy->me.net,
482 policy->other.net_mask, policy->me.net_mask,
483 XFRM_POLICY_IN, policy->upper_proto);
484
485 charon->kernel_interface->del_policy(charon->kernel_interface,
486 this->other.addr, this->me.addr,
487 policy->other.net, policy->me.net,
488 policy->other.net_mask, policy->me.net_mask,
489 XFRM_POLICY_FWD, policy->upper_proto);
490
491 policy->me.net->destroy(policy->me.net);
492 policy->other.net->destroy(policy->other.net);
493 free(policy);
494 }
495 this->policies->destroy(this->policies);
496
497 /* delete SAs in the kernel, if they are set up */
498 if (this->protocol != PROTO_NONE)
499 {
500 charon->kernel_interface->del_sa(charon->kernel_interface,
501 this->other.addr, this->me.spi, this->protocol);
502 charon->kernel_interface->del_sa(charon->kernel_interface,
503 this->me.addr, this->other.spi, this->protocol);
504 }
505 free(this);
506 }
507
508 /*
509 * Described in header.
510 */
511 child_sa_t * child_sa_create(host_t *me, host_t* other, u_int32_t soft_lifetime, u_int32_t hard_lifetime)
512 {
513 static u_int32_t reqid = 2000000000;
514 private_child_sa_t *this = malloc_thing(private_child_sa_t);
515
516 /* public functions */
517 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
518 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
519 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
520 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
521 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))add;
522 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))update;
523 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*))add_policies;
524 this->public.log_status = (void (*)(child_sa_t*, logger_t*, char*))log_status;
525 this->public.destroy = (void(*)(child_sa_t*))destroy;
526
527 /* private data */
528 this->logger = logger_manager->get_logger(logger_manager, CHILD_SA);
529 this->me.addr = me;
530 this->other.addr = other;
531 this->me.spi = 0;
532 this->other.spi = 0;
533 this->soft_lifetime = soft_lifetime;
534 this->hard_lifetime = hard_lifetime;
535 this->reqid = ++reqid;
536 this->policies = linked_list_create();
537 this->protocol = PROTO_NONE;
538
539 return (&this->public);
540 }