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