some cleanups
[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-2007 Martin Willi
10 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #define _GNU_SOURCE
26 #include "child_sa.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <printf.h>
31
32 #include <daemon.h>
33
34 ENUM(child_sa_state_names, CHILD_CREATED, CHILD_DELETING,
35 "CREATED",
36 "ROUTED",
37 "INSTALLED",
38 "REKEYING",
39 "DELETING",
40 );
41
42 typedef struct sa_policy_t sa_policy_t;
43
44 /**
45 * Struct used to store information for a policy. This
46 * is needed since we must provide all this information
47 * for deleting a policy...
48 */
49 struct sa_policy_t {
50 /**
51 * Traffic selector for us
52 */
53 traffic_selector_t *my_ts;
54
55 /**
56 * Traffic selector for other
57 */
58 traffic_selector_t *other_ts;
59 };
60
61 typedef struct private_child_sa_t private_child_sa_t;
62
63 /**
64 * Private data of a child_sa_t \ 1bject.
65 */
66 struct private_child_sa_t {
67 /**
68 * Public interface of child_sa_t.
69 */
70 child_sa_t public;
71
72 struct {
73 /** address of peer */
74 host_t *addr;
75 /** id of peer */
76 identification_t *id;
77 /** actual used SPI, 0 if unused */
78 u_int32_t spi;
79 } me, other;
80
81 /**
82 * Allocated SPI for a ESP proposal candidates
83 */
84 u_int32_t alloc_esp_spi;
85
86 /**
87 * Allocated SPI for a AH proposal candidates
88 */
89 u_int32_t alloc_ah_spi;
90
91 /**
92 * Protocol used to protect this SA, ESP|AH
93 */
94 protocol_id_t protocol;
95
96 /**
97 * List containing sa_policy_t objects
98 */
99 linked_list_t *policies;
100
101 /**
102 * Seperate list for local traffic selectors
103 */
104 linked_list_t *my_ts;
105
106 /**
107 * Seperate list for remote traffic selectors
108 */
109 linked_list_t *other_ts;
110
111 /**
112 * reqid used for this child_sa
113 */
114 u_int32_t reqid;
115
116 /**
117 * encryption algorithm used for this SA
118 */
119 algorithm_t encryption;
120
121 /**
122 * integrity protection algorithm used for this SA
123 */
124 algorithm_t integrity;
125
126 /**
127 * time, on which SA was installed
128 */
129 time_t install_time;
130
131 /**
132 * state of the CHILD_SA
133 */
134 child_sa_state_t state;
135
136 /**
137 * Specifies if NAT traversal is used
138 */
139 bool use_natt;
140
141 /**
142 * mode this SA uses, tunnel/transport
143 */
144 mode_t mode;
145
146 /**
147 * virtual IP assinged to local host
148 */
149 host_t *virtual_ip;
150
151 /**
152 * policy used to create this child
153 */
154 policy_t *policy;
155 };
156
157 /**
158 * Implementation of child_sa_t.get_name.
159 */
160 static char *get_name(private_child_sa_t *this)
161 {
162 return this->policy->get_name(this->policy);;
163 }
164
165 /**
166 * Implements child_sa_t.get_reqid
167 */
168 static u_int32_t get_reqid(private_child_sa_t *this)
169 {
170 return this->reqid;
171 }
172
173 /**
174 * Implements child_sa_t.get_spi
175 */
176 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
177 {
178 if (inbound)
179 {
180 return this->me.spi;
181 }
182 return this->other.spi;
183 }
184
185 /**
186 * Implements child_sa_t.get_protocol
187 */
188 protocol_id_t get_protocol(private_child_sa_t *this)
189 {
190 return this->protocol;
191 }
192
193 /**
194 * Implements child_sa_t.get_state
195 */
196 static child_sa_state_t get_state(private_child_sa_t *this)
197 {
198 return this->state;
199 }
200
201 /**
202 * Implements child_sa_t.get_policy
203 */
204 static policy_t* get_policy(private_child_sa_t *this)
205 {
206 return this->policy;
207 }
208
209 /**
210 * Run the up/down script
211 */
212 static void updown(private_child_sa_t *this, bool up)
213 {
214 sa_policy_t *policy;
215 iterator_t *iterator;
216 char *script;
217
218 script = this->policy->get_updown(this->policy);
219
220 if (script == NULL)
221 {
222 return;
223 }
224
225 iterator = this->policies->create_iterator(this->policies, TRUE);
226 while (iterator->iterate(iterator, (void**)&policy))
227 {
228 char command[1024];
229 char *ifname = NULL;
230 char *my_client, *other_client, *my_client_mask, *other_client_mask;
231 char *pos, *virtual_ip;
232 FILE *shell;
233
234 /* get subnet/bits from string */
235 asprintf(&my_client, "%R", policy->my_ts);
236 pos = strchr(my_client, '/');
237 *pos = '\0';
238 my_client_mask = pos + 1;
239 pos = strchr(my_client_mask, '[');
240 if (pos)
241 {
242 *pos = '\0';
243 }
244 asprintf(&other_client, "%R", policy->other_ts);
245 pos = strchr(other_client, '/');
246 *pos = '\0';
247 other_client_mask = pos + 1;
248 pos = strchr(other_client_mask, '[');
249 if (pos)
250 {
251 *pos = '\0';
252 }
253
254 if (this->virtual_ip)
255 {
256 asprintf(&virtual_ip, "PLUTO_MY_SOURCEIP='%H' ",
257 this->virtual_ip);
258 }
259 else
260 {
261 asprintf(&virtual_ip, "");
262 }
263
264 ifname = charon->kernel_interface->get_interface(charon->kernel_interface,
265 this->me.addr);
266
267 /* build the command with all env variables.
268 * TODO: PLUTO_PEER_CA and PLUTO_NEXT_HOP are currently missing
269 */
270 snprintf(command, sizeof(command),
271 "2>&1 "
272 "PLUTO_VERSION='1.1' "
273 "PLUTO_VERB='%s%s%s' "
274 "PLUTO_CONNECTION='%s' "
275 "PLUTO_INTERFACE='%s' "
276 "PLUTO_REQID='%u' "
277 "PLUTO_ME='%H' "
278 "PLUTO_MY_ID='%D' "
279 "PLUTO_MY_CLIENT='%s/%s' "
280 "PLUTO_MY_CLIENT_NET='%s' "
281 "PLUTO_MY_CLIENT_MASK='%s' "
282 "PLUTO_MY_PORT='%u' "
283 "PLUTO_MY_PROTOCOL='%u' "
284 "PLUTO_PEER='%H' "
285 "PLUTO_PEER_ID='%D' "
286 "PLUTO_PEER_CLIENT='%s/%s' "
287 "PLUTO_PEER_CLIENT_NET='%s' "
288 "PLUTO_PEER_CLIENT_MASK='%s' "
289 "PLUTO_PEER_PORT='%u' "
290 "PLUTO_PEER_PROTOCOL='%u' "
291 "%s"
292 "%s"
293 "%s",
294 up ? "up" : "down",
295 policy->my_ts->is_host(policy->my_ts,
296 this->me.addr) ? "-host" : "-client",
297 this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-ipv6",
298 this->policy->get_name(this->policy),
299 ifname ? ifname : "(unknown)",
300 this->reqid,
301 this->me.addr,
302 this->me.id,
303 my_client, my_client_mask,
304 my_client, my_client_mask,
305 policy->my_ts->get_from_port(policy->my_ts),
306 policy->my_ts->get_protocol(policy->my_ts),
307 this->other.addr,
308 this->other.id,
309 other_client, other_client_mask,
310 other_client, other_client_mask,
311 policy->other_ts->get_from_port(policy->other_ts),
312 policy->other_ts->get_protocol(policy->other_ts),
313 virtual_ip,
314 this->policy->get_hostaccess(this->policy) ?
315 "PLUTO_HOST_ACCESS='1' " : "",
316 script);
317 free(ifname);
318 free(my_client);
319 free(other_client);
320 free(virtual_ip);
321
322 shell = popen(command, "r");
323
324 if (shell == NULL)
325 {
326 DBG1(DBG_CHD, "could not execute updown script '%s'", script);
327 return;
328 }
329
330 while (TRUE)
331 {
332 char resp[128];
333
334 if (fgets(resp, sizeof(resp), shell) == NULL)
335 {
336 if (ferror(shell))
337 {
338 DBG1(DBG_CHD, "error reading output from updown script");
339 return;
340 }
341 else
342 {
343 break;
344 }
345 }
346 else
347 {
348 char *e = resp + strlen(resp);
349 if (e > resp && e[-1] == '\n')
350 { /* trim trailing '\n' */
351 e[-1] = '\0';
352 }
353 DBG1(DBG_CHD, "updown: %s", resp);
354 }
355 }
356 pclose(shell);
357 }
358 iterator->destroy(iterator);
359 }
360
361 /**
362 * Implements child_sa_t.set_state
363 */
364 static void set_state(private_child_sa_t *this, child_sa_state_t state)
365 {
366 this->state = state;
367 if (state == CHILD_INSTALLED)
368 {
369 updown(this, TRUE);
370 }
371 }
372
373 /**
374 * Allocate SPI for a single proposal
375 */
376 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
377 {
378 protocol_id_t protocol = proposal->get_protocol(proposal);
379
380 if (protocol == PROTO_AH)
381 {
382 /* get a new spi for AH, if not already done */
383 if (this->alloc_ah_spi == 0)
384 {
385 if (charon->kernel_interface->get_spi(
386 charon->kernel_interface,
387 this->other.addr, this->me.addr,
388 PROTO_AH, this->reqid,
389 &this->alloc_ah_spi) != SUCCESS)
390 {
391 return FAILED;
392 }
393 }
394 proposal->set_spi(proposal, this->alloc_ah_spi);
395 }
396 if (protocol == PROTO_ESP)
397 {
398 /* get a new spi for ESP, if not already done */
399 if (this->alloc_esp_spi == 0)
400 {
401 if (charon->kernel_interface->get_spi(
402 charon->kernel_interface,
403 this->other.addr, this->me.addr,
404 PROTO_ESP, this->reqid,
405 &this->alloc_esp_spi) != SUCCESS)
406 {
407 return FAILED;
408 }
409 }
410 proposal->set_spi(proposal, this->alloc_esp_spi);
411 }
412 return SUCCESS;
413 }
414
415
416 /**
417 * Implements child_sa_t.alloc
418 */
419 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
420 {
421 iterator_t *iterator;
422 proposal_t *proposal;
423
424 /* iterator through proposals to update spis */
425 iterator = proposals->create_iterator(proposals, TRUE);
426 while(iterator->iterate(iterator, (void**)&proposal))
427 {
428 if (alloc_proposal(this, proposal) != SUCCESS)
429 {
430 iterator->destroy(iterator);
431 return FAILED;
432 }
433 }
434 iterator->destroy(iterator);
435 return SUCCESS;
436 }
437
438 static status_t install(private_child_sa_t *this, proposal_t *proposal,
439 mode_t mode, prf_plus_t *prf_plus, bool mine)
440 {
441 u_int32_t spi, soft, hard;;
442 algorithm_t *enc_algo, *int_algo;
443 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
444 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
445 host_t *src;
446 host_t *dst;
447 natt_conf_t *natt;
448 status_t status;
449
450 this->protocol = proposal->get_protocol(proposal);
451
452 /* now we have to decide which spi to use. Use self allocated, if "mine",
453 * or the one in the proposal, if not "mine" (others). Additionally,
454 * source and dest host switch depending on the role */
455 if (mine)
456 {
457 /* if we have allocated SPIs for AH and ESP, we must delete the unused
458 * one. */
459 if (this->protocol == PROTO_ESP)
460 {
461 this->me.spi = this->alloc_esp_spi;
462 if (this->alloc_ah_spi)
463 {
464 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
465 this->alloc_ah_spi, PROTO_AH);
466 }
467 }
468 else
469 {
470 this->me.spi = this->alloc_ah_spi;
471 if (this->alloc_esp_spi)
472 {
473 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
474 this->alloc_esp_spi, PROTO_ESP);
475 }
476 }
477 spi = this->me.spi;
478 dst = this->me.addr;
479 src = this->other.addr;
480 }
481 else
482 {
483 this->other.spi = proposal->get_spi(proposal);
484 spi = this->other.spi;
485 src = this->me.addr;
486 dst = this->other.addr;
487 }
488
489 DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
490 protocol_id_names, this->protocol);
491
492 /* select encryption algo */
493 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
494 {
495 DBG2(DBG_CHD, " using %N for encryption",
496 encryption_algorithm_names, enc_algo->algorithm);
497 }
498 else
499 {
500 enc_algo = &enc_algo_none;
501 }
502
503 /* select integrity algo */
504 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
505 {
506 DBG2(DBG_CHD, " using %N for integrity",
507 integrity_algorithm_names, int_algo->algorithm);
508 }
509 else
510 {
511 int_algo = &int_algo_none;
512 }
513
514 /* setup nat-t */
515 if (this->use_natt)
516 {
517 natt = alloca(sizeof(natt_conf_t));
518 natt->sport = src->get_port(src);
519 natt->dport = dst->get_port(dst);
520 }
521 else
522 {
523 natt = NULL;
524 }
525
526 soft = this->policy->get_soft_lifetime(this->policy);
527 hard = this->policy->get_hard_lifetime(this->policy);
528
529 /* send SA down to the kernel */
530 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
531 status = charon->kernel_interface->add_sa(charon->kernel_interface,
532 src, dst, spi, this->protocol,
533 this->reqid, mine ? soft : 0,
534 hard, enc_algo, int_algo,
535 prf_plus, natt, mode, mine);
536
537 this->encryption = *enc_algo;
538 this->integrity = *int_algo;
539 this->install_time = time(NULL);
540
541 return status;
542 }
543
544 static status_t add(private_child_sa_t *this, proposal_t *proposal,
545 mode_t mode, prf_plus_t *prf_plus)
546 {
547 u_int32_t outbound_spi, inbound_spi;
548
549 /* backup outbound spi, as alloc overwrites it */
550 outbound_spi = proposal->get_spi(proposal);
551
552 /* get SPIs inbound SAs */
553 if (alloc_proposal(this, proposal) != SUCCESS)
554 {
555 return FAILED;
556 }
557 inbound_spi = proposal->get_spi(proposal);
558
559 /* install inbound SAs */
560 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
561 {
562 return FAILED;
563 }
564
565 /* install outbound SAs, restore spi*/
566 proposal->set_spi(proposal, outbound_spi);
567 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
568 {
569 return FAILED;
570 }
571 proposal->set_spi(proposal, inbound_spi);
572
573 return SUCCESS;
574 }
575
576 static status_t update(private_child_sa_t *this, proposal_t *proposal,
577 mode_t mode, prf_plus_t *prf_plus)
578 {
579 u_int32_t inbound_spi;
580
581 /* backup received spi, as install() overwrites it */
582 inbound_spi = proposal->get_spi(proposal);
583
584 /* install outbound SAs */
585 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
586 {
587 return FAILED;
588 }
589
590 /* restore spi */
591 proposal->set_spi(proposal, inbound_spi);
592 /* install inbound SAs */
593 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
594 {
595 return FAILED;
596 }
597
598 return SUCCESS;
599 }
600
601 static status_t add_policies(private_child_sa_t *this,
602 linked_list_t *my_ts_list,
603 linked_list_t *other_ts_list, mode_t mode)
604 {
605 iterator_t *my_iter, *other_iter;
606 traffic_selector_t *my_ts, *other_ts;
607 /* use low prio for ROUTED policies */
608 bool high_prio = (this->state != CHILD_CREATED);
609
610 /* iterate over both lists */
611 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
612 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
613 while (my_iter->iterate(my_iter, (void**)&my_ts))
614 {
615 other_iter->reset(other_iter);
616 while (other_iter->iterate(other_iter, (void**)&other_ts))
617 {
618 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
619 status_t status;
620 sa_policy_t *policy;
621
622 if (my_ts->get_type(my_ts) != other_ts->get_type(other_ts))
623 {
624 DBG2(DBG_CHD,
625 "CHILD_SA policy uses two different IP families, ignored");
626 continue;
627 }
628
629 /* only set up policies if protocol matches, or if one is zero (any) */
630 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts) &&
631 my_ts->get_protocol(my_ts) && other_ts->get_protocol(other_ts))
632 {
633 DBG2(DBG_CHD,
634 "CHILD_SA policy uses two different protocols, ignored");
635 continue;
636 }
637
638 /* install 3 policies: out, in and forward */
639 status = charon->kernel_interface->add_policy(charon->kernel_interface,
640 this->me.addr, this->other.addr, my_ts, other_ts, POLICY_OUT,
641 this->protocol, this->reqid, high_prio, mode, FALSE);
642
643 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
644 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_IN,
645 this->protocol, this->reqid, high_prio, mode, FALSE);
646
647 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
648 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_FWD,
649 this->protocol, this->reqid, high_prio, mode, FALSE);
650
651 if (status != SUCCESS)
652 {
653 my_iter->destroy(my_iter);
654 other_iter->destroy(other_iter);
655 return status;
656 }
657
658 /* store policy to delete/update them later */
659 policy = malloc_thing(sa_policy_t);
660 policy->my_ts = my_ts->clone(my_ts);
661 policy->other_ts = other_ts->clone(other_ts);
662 this->policies->insert_last(this->policies, (void*)policy);
663 /* add to separate list to query them via get_*_traffic_selectors() */
664 this->my_ts->insert_last(this->my_ts, (void*)policy->my_ts);
665 this->other_ts->insert_last(this->other_ts, (void*)policy->other_ts);
666 }
667 }
668 my_iter->destroy(my_iter);
669 other_iter->destroy(other_iter);
670
671 /* switch to routed state if no SAD entry set up */
672 if (this->state == CHILD_CREATED)
673 {
674 this->state = CHILD_ROUTED;
675 }
676 /* needed to update hosts */
677 this->mode = mode;
678 return SUCCESS;
679 }
680
681 /**
682 * Implementation of child_sa_t.get_my_traffic_selectors.
683 */
684 static linked_list_t *get_my_traffic_selectors(private_child_sa_t *this)
685 {
686 return this->my_ts;
687 }
688
689 /**
690 * Implementation of child_sa_t.get_my_traffic_selectors.
691 */
692 static linked_list_t *get_other_traffic_selectors(private_child_sa_t *this)
693 {
694 return this->other_ts;
695 }
696
697 /**
698 * Implementation of child_sa_t.get_use_time
699 */
700 static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
701 {
702 iterator_t *iterator;
703 sa_policy_t *policy;
704 status_t status = FAILED;
705
706 *use_time = UNDEFINED_TIME;
707
708 iterator = this->policies->create_iterator(this->policies, TRUE);
709 while (iterator->iterate(iterator, (void**)&policy))
710 {
711 if (inbound)
712 {
713 time_t in = UNDEFINED_TIME, fwd = UNDEFINED_TIME;
714
715 status = charon->kernel_interface->query_policy(
716 charon->kernel_interface,
717 policy->other_ts, policy->my_ts,
718 POLICY_IN, (u_int32_t*)&in);
719 status |= charon->kernel_interface->query_policy(
720 charon->kernel_interface,
721 policy->other_ts, policy->my_ts,
722 POLICY_FWD, (u_int32_t*)&fwd);
723 *use_time = max(in, fwd);
724 }
725 else
726 {
727 status = charon->kernel_interface->query_policy(
728 charon->kernel_interface,
729 policy->my_ts, policy->other_ts,
730 POLICY_OUT, (u_int32_t*)use_time);
731 }
732 }
733 iterator->destroy(iterator);
734 return status;
735 }
736
737 /**
738 * output handler in printf()
739 */
740 static int print(FILE *stream, const struct printf_info *info,
741 const void *const *args)
742 {
743 private_child_sa_t *this = *((private_child_sa_t**)(args[0]));
744 iterator_t *iterator;
745 sa_policy_t *policy;
746 u_int32_t now, rekeying, soft;
747 u_int32_t use, use_in, use_fwd;
748 status_t status;
749 size_t written = 0;
750
751 if (this == NULL)
752 {
753 return fprintf(stream, "(null)");
754 }
755
756 now = (u_int32_t)time(NULL);
757
758 written += fprintf(stream, "%12s{%d}: %N, %N",
759 this->policy->get_name(this->policy), this->reqid,
760 child_sa_state_names, this->state,
761 mode_names, this->mode);
762
763 if (this->state == CHILD_INSTALLED)
764 {
765 written += fprintf(stream, ", %N SPIs: 0x%0x_i 0x%0x_o",
766 protocol_id_names, this->protocol,
767 htonl(this->me.spi), htonl(this->other.spi));
768
769 if (info->alt)
770 {
771 written += fprintf(stream, "\n%12s{%d}: ",
772 this->policy->get_name(this->policy),
773 this->reqid);
774
775 if (this->protocol == PROTO_ESP)
776 {
777 written += fprintf(stream, "%N", encryption_algorithm_names,
778 this->encryption.algorithm);
779
780 if (this->encryption.key_size)
781 {
782 written += fprintf(stream, "-%d", this->encryption.key_size);
783 }
784 written += fprintf(stream, "/");
785 }
786
787 written += fprintf(stream, "%N", integrity_algorithm_names,
788 this->integrity.algorithm);
789 if (this->integrity.key_size)
790 {
791 written += fprintf(stream, "-%d", this->integrity.key_size);
792 }
793 written += fprintf(stream, ", rekeying ");
794
795 soft = this->policy->get_soft_lifetime(this->policy);
796 /* calculate rekey times */
797 if (soft)
798 {
799 rekeying = soft - (now - this->install_time);
800 written += fprintf(stream, "in %ds", rekeying);
801 }
802 else
803 {
804 written += fprintf(stream, "disabled");
805 }
806 }
807 }
808 iterator = this->policies->create_iterator(this->policies, TRUE);
809 while (iterator->iterate(iterator, (void**)&policy))
810 {
811 written += fprintf(stream, "\n%12s{%d}: %R===%R, last use: ",
812 this->policy->get_name(this->policy), this->reqid,
813 policy->my_ts, policy->other_ts);
814
815 /* query time of last policy use */
816
817 /* inbound: POLICY_IN or POLICY_FWD */
818 status = charon->kernel_interface->query_policy(charon->kernel_interface,
819 policy->other_ts, policy->my_ts, POLICY_IN, &use_in);
820 use_in = (status == SUCCESS)? use_in : 0;
821 status = charon->kernel_interface->query_policy(charon->kernel_interface,
822 policy->other_ts, policy->my_ts, POLICY_FWD, &use_fwd);
823 use_fwd = (status == SUCCESS)? use_fwd : 0;
824 use = max(use_in, use_fwd);
825 if (use)
826 {
827 written += fprintf(stream, "%ds_i ", now - use);
828 }
829 else
830 {
831 written += fprintf(stream, "no_i ");
832 }
833
834 /* outbound: POLICY_OUT */
835 status = charon->kernel_interface->query_policy(charon->kernel_interface,
836 policy->my_ts, policy->other_ts, POLICY_OUT, &use);
837 if (status == SUCCESS && use)
838 {
839 written += fprintf(stream, "%ds_o ", now - use);
840 }
841 else
842 {
843 written += fprintf(stream, "no_o ");
844 }
845 }
846 iterator->destroy(iterator);
847 return written;
848 }
849
850 /**
851 * register printf() handlers
852 */
853 static void __attribute__ ((constructor))print_register()
854 {
855 register_printf_function(PRINTF_CHILD_SA, print, arginfo_ptr);
856 }
857
858 /**
859 * Update the host adress/port of a SA
860 */
861 static status_t update_sa_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
862 int my_changes, int other_changes, bool mine)
863 {
864 host_t *src, *dst, *new_src, *new_dst;
865 int src_changes, dst_changes;
866 status_t status;
867 u_int32_t spi;
868
869 if (mine)
870 {
871 src = this->other.addr;
872 dst = this->me.addr;
873 new_src = new_other;
874 new_dst = new_me;
875 src_changes = other_changes;
876 dst_changes = my_changes;
877 spi = this->other.spi;
878 }
879 else
880 {
881 src = this->me.addr;
882 dst = this->other.addr;
883 new_src = new_me;
884 new_dst = new_other;
885 src_changes = my_changes;
886 dst_changes = other_changes;
887 spi = this->me.spi;
888 }
889
890 DBG2(DBG_CHD, "updating %N SA 0x%x, from %#H..#H to %#H..%#H",
891 protocol_id_names, this->protocol, ntohl(spi), src, dst, new_src, new_dst);
892
893 status = charon->kernel_interface->update_sa(charon->kernel_interface,
894 dst, spi, this->protocol,
895 new_src, new_dst,
896 src_changes, dst_changes);
897
898 if (status != SUCCESS)
899 {
900 return FAILED;
901 }
902 return SUCCESS;
903 }
904
905 /**
906 * Update the host adress/port of a policy
907 */
908 static status_t update_policy_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other)
909 {
910 iterator_t *iterator;
911 sa_policy_t *policy;
912 status_t status;
913 /* we always use high priorities, as hosts getting updated are INSTALLED */
914
915 iterator = this->policies->create_iterator(this->policies, TRUE);
916 while (iterator->iterate(iterator, (void**)&policy))
917 {
918 status = charon->kernel_interface->add_policy(
919 charon->kernel_interface,
920 new_me, new_other,
921 policy->my_ts, policy->other_ts,
922 POLICY_OUT, this->protocol, this->reqid, TRUE, this->mode, TRUE);
923
924 status |= charon->kernel_interface->add_policy(
925 charon->kernel_interface,
926 new_other, new_me,
927 policy->other_ts, policy->my_ts,
928 POLICY_IN, this->protocol, this->reqid, TRUE, this->mode, TRUE);
929
930 status |= charon->kernel_interface->add_policy(
931 charon->kernel_interface,
932 new_other, new_me,
933 policy->other_ts, policy->my_ts,
934 POLICY_FWD, this->protocol, this->reqid, TRUE, this->mode, TRUE);
935
936 if (status != SUCCESS)
937 {
938 iterator->destroy(iterator);
939 return FAILED;
940 }
941 }
942 iterator->destroy(iterator);
943
944 return SUCCESS;
945 }
946
947 /**
948 * Implementation of child_sa_t.update_hosts.
949 */
950 static status_t update_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
951 host_diff_t my_changes, host_diff_t other_changes)
952 {
953 if (!my_changes && !other_changes)
954 {
955 return SUCCESS;
956 }
957
958 /* update our (initator) SAs */
959 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, TRUE) != SUCCESS)
960 {
961 return FAILED;
962 }
963
964 /* update his (responder) SAs */
965 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, FALSE) != SUCCESS)
966 {
967 return FAILED;
968 }
969
970 /* update policies */
971 if (my_changes & HOST_DIFF_ADDR || other_changes & HOST_DIFF_ADDR)
972 {
973 if (update_policy_hosts(this, new_me, new_other) != SUCCESS)
974 {
975 return FAILED;
976 }
977 }
978
979 /* update hosts */
980 if (my_changes)
981 {
982 this->me.addr->destroy(this->me.addr);
983 this->me.addr = new_me->clone(new_me);
984 }
985
986 if (other_changes)
987 {
988 this->other.addr->destroy(this->other.addr);
989 this->other.addr = new_other->clone(new_other);
990 }
991
992 return SUCCESS;
993 }
994
995 /**
996 * Implementation of child_sa_t.set_virtual_ip.
997 */
998 static void set_virtual_ip(private_child_sa_t *this, host_t *ip)
999 {
1000 this->virtual_ip = ip->clone(ip);
1001 }
1002
1003 /**
1004 * Implementation of child_sa_t.destroy.
1005 */
1006 static void destroy(private_child_sa_t *this)
1007 {
1008 sa_policy_t *policy;
1009
1010 if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
1011 {
1012 updown(this, FALSE);
1013 }
1014
1015 /* delete SAs in the kernel, if they are set up */
1016 if (this->me.spi)
1017 {
1018 charon->kernel_interface->del_sa(charon->kernel_interface,
1019 this->me.addr, this->me.spi, this->protocol);
1020 }
1021 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
1022 {
1023 charon->kernel_interface->del_sa(charon->kernel_interface,
1024 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
1025 }
1026 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
1027 {
1028 charon->kernel_interface->del_sa(charon->kernel_interface,
1029 this->me.addr, this->alloc_ah_spi, PROTO_AH);
1030 }
1031 if (this->other.spi)
1032 {
1033 charon->kernel_interface->del_sa(charon->kernel_interface,
1034 this->other.addr, this->other.spi, this->protocol);
1035 }
1036
1037 /* delete all policies in the kernel */
1038 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
1039 {
1040 /* let rekeyed policies, as they are used by another child_sa */
1041 charon->kernel_interface->del_policy(charon->kernel_interface,
1042 policy->my_ts, policy->other_ts,
1043 POLICY_OUT);
1044
1045 charon->kernel_interface->del_policy(charon->kernel_interface,
1046 policy->other_ts, policy->my_ts,
1047 POLICY_IN);
1048
1049 charon->kernel_interface->del_policy(charon->kernel_interface,
1050 policy->other_ts, policy->my_ts,
1051 POLICY_FWD);
1052 policy->my_ts->destroy(policy->my_ts);
1053 policy->other_ts->destroy(policy->other_ts);
1054 free(policy);
1055 }
1056 this->policies->destroy(this->policies);
1057
1058 this->my_ts->destroy(this->my_ts);
1059 this->other_ts->destroy(this->other_ts);
1060 this->me.addr->destroy(this->me.addr);
1061 this->other.addr->destroy(this->other.addr);
1062 this->me.id->destroy(this->me.id);
1063 this->other.id->destroy(this->other.id);
1064 this->policy->destroy(this->policy);
1065 DESTROY_IF(this->virtual_ip);
1066 free(this);
1067 }
1068
1069 /*
1070 * Described in header.
1071 */
1072 child_sa_t * child_sa_create(host_t *me, host_t* other,
1073 identification_t *my_id, identification_t *other_id,
1074 policy_t *policy, u_int32_t rekey, bool use_natt)
1075 {
1076 static u_int32_t reqid = 0;
1077 private_child_sa_t *this = malloc_thing(private_child_sa_t);
1078
1079 /* public functions */
1080 this->public.get_name = (char*(*)(child_sa_t*))get_name;
1081 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
1082 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
1083 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
1084 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
1085 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
1086 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
1087 this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,host_diff_t,host_diff_t))update_hosts;
1088 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
1089 this->public.get_my_traffic_selectors = (linked_list_t*(*)(child_sa_t*))get_my_traffic_selectors;
1090 this->public.get_other_traffic_selectors = (linked_list_t*(*)(child_sa_t*))get_other_traffic_selectors;
1091 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
1092 this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
1093 this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
1094 this->public.get_policy = (policy_t*(*)(child_sa_t*))get_policy;
1095 this->public.set_virtual_ip = (void(*)(child_sa_t*,host_t*))set_virtual_ip;
1096 this->public.destroy = (void(*)(child_sa_t*))destroy;
1097
1098 /* private data */
1099 this->me.addr = me->clone(me);
1100 this->other.addr = other->clone(other);
1101 this->me.id = my_id->clone(my_id);
1102 this->other.id = other_id->clone(other_id);
1103 this->me.spi = 0;
1104 this->other.spi = 0;
1105 this->alloc_ah_spi = 0;
1106 this->alloc_esp_spi = 0;
1107 this->use_natt = use_natt;
1108 this->state = CHILD_CREATED;
1109 /* reuse old reqid if we are rekeying an existing CHILD_SA */
1110 this->reqid = rekey ? rekey : ++reqid;
1111 this->encryption.algorithm = ENCR_UNDEFINED;
1112 this->encryption.key_size = 0;
1113 this->integrity.algorithm = AUTH_UNDEFINED;
1114 this->encryption.key_size = 0;
1115 this->policies = linked_list_create();
1116 this->my_ts = linked_list_create();
1117 this->other_ts = linked_list_create();
1118 this->protocol = PROTO_NONE;
1119 this->mode = MODE_TUNNEL;
1120 this->virtual_ip = NULL;
1121 this->policy = policy;
1122 policy->get_ref(policy);
1123
1124 return &this->public;
1125 }