applied new changes from NATT team
[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 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <netdb.h>
25
26 #include "child_sa.h"
27
28 #include <daemon.h>
29
30
31 typedef struct sa_policy_t sa_policy_t;
32
33 /**
34 * Struct used to store information for a policy. This
35 * is needed since we must provide all this information
36 * for deleting a policy...
37 */
38 struct sa_policy_t {
39
40 struct {
41 /** subnet address behind peer peer */
42 host_t *net;
43 /** netmask used for net */
44 u_int8_t net_mask;
45 } me, other;
46
47 /**
48 * Protocol for this policy, such as TCP/UDP/ICMP...
49 */
50 int upper_proto;
51 };
52
53 typedef struct private_child_sa_t private_child_sa_t;
54
55 /**
56 * Private data of a child_sa_t object.
57 */
58 struct private_child_sa_t {
59 /**
60 * Public interface of child_sa_t.
61 */
62 child_sa_t public;
63
64 struct {
65 /** address of peer */
66 host_t *addr;
67 /** actual used SPI, 0 if unused */
68 u_int32_t spi;
69 } me, other;
70
71 /**
72 * Allocated SPI for a ESP proposal candidates
73 */
74 u_int32_t alloc_esp_spi;
75
76 /**
77 * Allocated SPI for a AH proposal candidates
78 */
79 u_int32_t alloc_ah_spi;
80
81 /**
82 * Protocol used to protect this SA, ESP|AH
83 */
84 protocol_id_t protocol;
85
86 /**
87 * List containing sa_policy_t objects
88 */
89 linked_list_t *policies;
90
91 /**
92 * reqid used for this child_sa
93 */
94 u_int32_t reqid;
95
96 /**
97 * time, on which SA was installed
98 */
99 time_t install_time;
100
101 /**
102 * Lifetime before rekeying
103 */
104 u_int32_t soft_lifetime;
105
106 /**
107 * Lifetime before delete
108 */
109 u_int32_t hard_lifetime;
110
111 /**
112 * has this CHILD_SA been rekeyed?
113 */
114 bool rekeyed;
115
116 /**
117 * Specifies if NAT traversal is used
118 */
119 bool use_natt;
120
121 /**
122 * CHILD_SAs own logger
123 */
124 logger_t *logger;
125 };
126
127 /**
128 * Implements child_sa_t.get_reqid
129 */
130 static u_int32_t get_reqid(private_child_sa_t *this)
131 {
132 return this->reqid;
133 }
134
135 /**
136 * Implements child_sa_t.get_spi
137 */
138 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
139 {
140 if (inbound)
141 {
142 return this->me.spi;
143 }
144 return this->other.spi;
145 }
146
147 /**
148 * Implements child_sa_t.get_protocol
149 */
150 protocol_id_t get_protocol(private_child_sa_t *this)
151 {
152 return this->protocol;
153 }
154
155 /**
156 * Allocate SPI for a single proposal
157 */
158 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
159 {
160 protocol_id_t protocol = proposal->get_protocol(proposal);
161
162 if (protocol == PROTO_AH)
163 {
164 /* get a new spi for AH, if not already done */
165 if (this->alloc_ah_spi == 0)
166 {
167 if (charon->kernel_interface->get_spi(
168 charon->kernel_interface,
169 this->other.addr, this->me.addr,
170 PROTO_AH, this->reqid,
171 &this->alloc_ah_spi) != SUCCESS)
172 {
173 return FAILED;
174 }
175 }
176 proposal->set_spi(proposal, this->alloc_ah_spi);
177 }
178 if (protocol == PROTO_ESP)
179 {
180 /* get a new spi for ESP, if not already done */
181 if (this->alloc_esp_spi == 0)
182 {
183 if (charon->kernel_interface->get_spi(
184 charon->kernel_interface,
185 this->other.addr, this->me.addr,
186 PROTO_ESP, this->reqid,
187 &this->alloc_esp_spi) != SUCCESS)
188 {
189 return FAILED;
190 }
191 }
192 proposal->set_spi(proposal, this->alloc_esp_spi);
193 }
194 return SUCCESS;
195 }
196
197
198 /**
199 * Implements child_sa_t.alloc
200 */
201 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
202 {
203 iterator_t *iterator;
204 proposal_t *proposal;
205
206 /* iterator through proposals to update spis */
207 iterator = proposals->create_iterator(proposals, TRUE);
208 while(iterator->has_next(iterator))
209 {
210 iterator->current(iterator, (void**)&proposal);
211 if (alloc_proposal(this, proposal) != SUCCESS)
212 {
213 iterator->destroy(iterator);
214 return FAILED;
215 }
216 }
217 iterator->destroy(iterator);
218 return SUCCESS;
219 }
220
221 static status_t install(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus, bool mine)
222 {
223 u_int32_t spi;
224 algorithm_t *enc_algo, *int_algo;
225 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
226 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
227 host_t *src;
228 host_t *dst;
229 natt_conf_t *natt;
230 status_t status;
231
232 this->protocol = proposal->get_protocol(proposal);
233
234 /* now we have to decide which spi to use. Use self allocated, if "mine",
235 * or the one in the proposal, if not "mine" (others). Additionally,
236 * source and dest host switch depending on the role */
237 if (mine)
238 {
239 /* if we have allocated SPIs for AH and ESP, we must delete the unused
240 * one. */
241 if (this->protocol == PROTO_ESP)
242 {
243 this->me.spi = this->alloc_esp_spi;
244 if (this->alloc_ah_spi)
245 {
246 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
247 this->alloc_ah_spi, PROTO_AH);
248 }
249 }
250 else
251 {
252 this->me.spi = this->alloc_ah_spi;
253 if (this->alloc_esp_spi)
254 {
255 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
256 this->alloc_esp_spi, PROTO_ESP);
257 }
258 }
259 spi = this->me.spi;
260 dst = this->me.addr;
261 src = this->other.addr;
262 }
263 else
264 {
265 this->other.spi = proposal->get_spi(proposal);
266 spi = this->other.spi;
267 src = this->me.addr;
268 dst = this->other.addr;
269 }
270
271 this->logger->log(this->logger, CONTROL|LEVEL1, "Adding %s %s SA",
272 mine ? "inbound" : "outbound",
273 mapping_find(protocol_id_m, this->protocol));
274
275 /* select encryption algo */
276 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
277 {
278 this->logger->log(this->logger, CONTROL|LEVEL2, " using %s for encryption",
279 mapping_find(encryption_algorithm_m, enc_algo->algorithm));
280 }
281 else
282 {
283 enc_algo = &enc_algo_none;
284 }
285
286 /* select integrity algo */
287 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
288 {
289 this->logger->log(this->logger, CONTROL|LEVEL2, " using %s for integrity",
290 mapping_find(integrity_algorithm_m, int_algo->algorithm));
291 }
292 else
293 {
294 int_algo = &int_algo_none;
295 }
296
297 /* setup nat-t */
298 if (this->use_natt)
299 {
300 natt = alloca(sizeof(natt_conf_t));
301 natt->sport = src->get_port(src);
302 natt->dport = dst->get_port(dst);
303 }
304 else
305 {
306 natt = NULL;
307 }
308
309
310 /* send SA down to the kernel */
311 this->logger->log(this->logger, CONTROL|LEVEL2,
312 " SPI 0x%.8x, src %s dst %s",
313 ntohl(spi), src->get_address(src), dst->get_address(dst));
314 status = charon->kernel_interface->add_sa(charon->kernel_interface,
315 src, dst,
316 spi, this->protocol,
317 this->reqid,
318 mine ? 0 : this->soft_lifetime,
319 this->hard_lifetime,
320 enc_algo, int_algo,
321 prf_plus, natt, mine);
322
323 this->install_time = time(NULL);
324
325 return status;
326 }
327
328 static status_t add(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
329 {
330 u_int32_t outbound_spi, inbound_spi;
331
332 /* backup outbound spi, as alloc overwrites it */
333 outbound_spi = proposal->get_spi(proposal);
334
335 /* get SPIs inbound SAs */
336 if (alloc_proposal(this, proposal) != SUCCESS)
337 {
338 return FAILED;
339 }
340 inbound_spi = proposal->get_spi(proposal);
341
342 /* install inbound SAs */
343 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
344 {
345 return FAILED;
346 }
347
348 /* install outbound SAs, restore spi*/
349 proposal->set_spi(proposal, outbound_spi);
350 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
351 {
352 return FAILED;
353 }
354 proposal->set_spi(proposal, inbound_spi);
355
356 return SUCCESS;
357 }
358
359 static status_t update(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
360 {
361 u_int32_t inbound_spi;
362
363 /* backup received spi, as install() overwrites it */
364 inbound_spi = proposal->get_spi(proposal);
365
366 /* install outbound SAs */
367 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
368 {
369 return FAILED;
370 }
371
372 /* restore spi */
373 proposal->set_spi(proposal, inbound_spi);
374 /* install inbound SAs */
375 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
376 {
377 return FAILED;
378 }
379
380 return SUCCESS;
381 }
382
383 static status_t add_policies(private_child_sa_t *this, linked_list_t *my_ts_list, linked_list_t *other_ts_list)
384 {
385 iterator_t *my_iter, *other_iter;
386 traffic_selector_t *my_ts, *other_ts;
387
388 /* iterate over both lists */
389 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
390 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
391 while (my_iter->has_next(my_iter))
392 {
393 my_iter->current(my_iter, (void**)&my_ts);
394 other_iter->reset(other_iter);
395 while (other_iter->has_next(other_iter))
396 {
397 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
398 int family;
399 chunk_t from_addr;
400 u_int16_t from_port, to_port;
401 sa_policy_t *policy;
402 status_t status;
403
404 other_iter->current(other_iter, (void**)&other_ts);
405
406 /* only set up policies if protocol matches */
407 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts))
408 {
409 continue;
410 }
411 policy = malloc_thing(sa_policy_t);
412 policy->upper_proto = my_ts->get_protocol(my_ts);
413
414 /* calculate net and ports for local side */
415 family = my_ts->get_type(my_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
416 from_addr = my_ts->get_from_address(my_ts);
417 from_port = my_ts->get_from_port(my_ts);
418 to_port = my_ts->get_to_port(my_ts);
419 from_port = (from_port != to_port) ? 0 : from_port;
420 policy->me.net = host_create_from_chunk(family, from_addr, from_port);
421 policy->me.net_mask = my_ts->get_netmask(my_ts);
422 chunk_free(&from_addr);
423
424 /* calculate net and ports for remote side */
425 family = other_ts->get_type(other_ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
426 from_addr = other_ts->get_from_address(other_ts);
427 from_port = other_ts->get_from_port(other_ts);
428 to_port = other_ts->get_to_port(other_ts);
429 from_port = (from_port != to_port) ? 0 : from_port;
430 policy->other.net = host_create_from_chunk(family, from_addr, from_port);
431 policy->other.net_mask = other_ts->get_netmask(other_ts);
432 chunk_free(&from_addr);
433
434 /* install 3 policies: out, in and forward */
435 status = charon->kernel_interface->add_policy(charon->kernel_interface,
436 this->me.addr, this->other.addr,
437 policy->me.net, policy->other.net,
438 policy->me.net_mask, policy->other.net_mask,
439 XFRM_POLICY_OUT, policy->upper_proto,
440 this->protocol, this->reqid);
441
442 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
443 this->other.addr, this->me.addr,
444 policy->other.net, policy->me.net,
445 policy->other.net_mask, policy->me.net_mask,
446 XFRM_POLICY_IN, policy->upper_proto,
447 this->protocol, this->reqid);
448
449 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
450 this->other.addr, this->me.addr,
451 policy->other.net, policy->me.net,
452 policy->other.net_mask, policy->me.net_mask,
453 XFRM_POLICY_FWD, policy->upper_proto,
454 this->protocol, this->reqid);
455
456 if (status != SUCCESS)
457 {
458 my_iter->destroy(my_iter);
459 other_iter->destroy(other_iter);
460 policy->me.net->destroy(policy->me.net);
461 policy->other.net->destroy(policy->other.net);
462 free(policy);
463 return status;
464 }
465
466 /* add it to the policy list, since we want to know which policies we own */
467 this->policies->insert_last(this->policies, policy);
468 }
469 }
470 my_iter->destroy(my_iter);
471 other_iter->destroy(other_iter);
472 return SUCCESS;
473 }
474
475 /**
476 * Implementation of child_sa_t.set_rekeyed.
477 */
478 static void set_rekeyed(private_child_sa_t *this)
479 {
480 this->rekeyed = TRUE;
481 }
482
483 /**
484 * Implementation of child_sa_t.log_status.
485 */
486 static void log_status(private_child_sa_t *this, logger_t *logger, char* name)
487 {
488 iterator_t *iterator;
489 sa_policy_t *policy;
490 struct protoent *proto;
491 char proto_buf[8] = "";
492 char *proto_name = proto_buf;
493
494 if (logger == NULL)
495 {
496 logger = this->logger;
497 }
498 if (this->soft_lifetime)
499 {
500 logger->log(logger, CONTROL|LEVEL1, " \"%s\": protected with %s (0x%x/0x%x), reqid %d, rekeying in %ds:",
501 name,
502 this->protocol == PROTO_ESP ? "ESP" : "AH",
503 htonl(this->me.spi), htonl(this->other.spi),
504 this->reqid,
505 this->soft_lifetime - (time(NULL) - this->install_time));
506 }
507 else
508 {
509
510 logger->log(logger, CONTROL|LEVEL1, " \"%s\": protected with %s (0x%x/0x%x), reqid %d, no rekeying:",
511 name,
512 this->protocol == PROTO_ESP ? "ESP" : "AH",
513 htonl(this->me.spi), htonl(this->other.spi),
514 this->reqid);
515 }
516 iterator = this->policies->create_iterator(this->policies, TRUE);
517 while (iterator->has_next(iterator))
518 {
519 iterator->current(iterator, (void**)&policy);
520 if (policy->upper_proto)
521 {
522 proto = getprotobynumber(policy->upper_proto);
523 if (proto)
524 {
525 proto_name = proto->p_name;
526 }
527 else
528 {
529 snprintf(proto_buf, sizeof(proto_buf), "<%d>", policy->upper_proto);
530 }
531 }
532 logger->log(logger, CONTROL, " \"%s\": %s/%d==%s==%s/%d",
533 name,
534 policy->me.net->get_address(policy->me.net), policy->me.net_mask,
535 proto_name,
536 policy->other.net->get_address(policy->other.net), policy->other.net_mask);
537 }
538 iterator->destroy(iterator);
539 }
540
541 /**
542 * Implementation of child_sa_t.get_use_time
543 */
544 static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
545 {
546 iterator_t *iterator;
547 sa_policy_t *policy;
548 struct protoent *proto;
549 char proto_buf[8] = "";
550 char *proto_name = proto_buf;
551 status_t status;
552
553 *use_time = UNDEFINED_TIME;
554
555 iterator = this->policies->create_iterator(this->policies, TRUE);
556 while (iterator->iterate(iterator, (void**)&policy))
557 {
558 time_t ut;
559
560 if (policy->upper_proto)
561 {
562 proto = getprotobynumber(policy->upper_proto);
563 if (proto)
564 {
565 proto_name = proto->p_name;
566 }
567 else
568 {
569 snprintf(proto_buf, sizeof(proto_buf), "<%d>", policy->upper_proto);
570 }
571 }
572
573 this->logger->log(this->logger, CONTROL|LEVEL1,
574 "quering policy: %s/%d==%s==%s/%d",
575 policy->me.net->get_address(policy->me.net), policy->me.net_mask,
576 proto_name,
577 policy->other.net->get_address(policy->other.net), policy->other.net_mask);
578
579 if (inbound)
580 {
581 status = charon->kernel_interface->query_policy(charon->kernel_interface,
582 this->other.addr, this->me.addr,
583 policy->other.net, policy->me.net,
584 policy->other.net_mask, policy->me.net_mask,
585 XFRM_POLICY_IN, policy->upper_proto,
586 &ut);
587
588 /* also check forward policy in tunnel mode */
589 if (status == SUCCESS /*&& mode == TUNNEL XXX */)
590 {
591 time_t fwd;
592
593 status = charon->kernel_interface->query_policy(charon->kernel_interface,
594 this->other.addr, this->me.addr,
595 policy->other.net, policy->me.net,
596 policy->other.net_mask, policy->me.net_mask,
597 XFRM_POLICY_FWD, policy->upper_proto,
598 &fwd);
599
600 if (status == SUCCESS)
601 {
602 ut = max(ut, fwd);
603 }
604 }
605 }
606 else
607 {
608 status = charon->kernel_interface->query_policy(charon->kernel_interface,
609 this->me.addr, this->other.addr,
610 policy->me.net, policy->other.net,
611 policy->me.net_mask, policy->other.net_mask,
612 XFRM_POLICY_OUT, policy->upper_proto,
613 &ut);
614 }
615
616 if (status != SUCCESS)
617 {
618 iterator->destroy(iterator);
619 return FAILED;
620 }
621
622 *use_time = max(*use_time, ut);
623 }
624 iterator->destroy(iterator);
625
626 return SUCCESS;
627 }
628
629 /**
630 * Update the host adress/port of a SA
631 */
632 static status_t update_sa_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
633 int my_changes, int other_changes, bool mine)
634 {
635 host_t *src, *dst, *new_src, *new_dst;
636 int src_changes, dst_changes;
637 status_t status;
638 u_int32_t spi;
639
640 if (mine)
641 {
642 src = this->me.addr;
643 dst = this->other.addr;
644 new_src = new_me;
645 new_dst = new_other;
646 src_changes = my_changes;
647 dst_changes = other_changes;
648 spi = this->me.spi;
649 }
650 else
651 {
652 src = this->other.addr;
653 dst = this->me.addr;
654 new_src = new_other;
655 new_dst = new_me;
656 src_changes = other_changes;
657 dst_changes = my_changes;
658 spi = this->other.spi;
659 }
660
661 this->logger->log(this->logger, CONTROL|LEVEL1,
662 "updating %s SA 0x%x, from %s:%d..%s:%d to %s:%d..%s:%d",
663 mapping_find(protocol_id_m, this->protocol), ntohl(spi),
664 src->get_address(src), src->get_port(src),
665 dst->get_address(dst), dst->get_port(dst),
666 new_src->get_address(new_src), new_src->get_port(new_src),
667 new_dst->get_address(new_dst), new_dst->get_port(new_dst));
668
669 status = charon->kernel_interface->update_sa_hosts(
670 charon->kernel_interface,
671 src, dst, new_src, new_dst,
672 src_changes, dst_changes,
673 spi, this->protocol);
674
675 if (status != SUCCESS)
676 {
677 return FAILED;
678 }
679 return SUCCESS;
680 }
681
682 /**
683 * Update the host adress/port of a policy
684 */
685 static status_t update_policy_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other)
686 {
687 iterator_t *iterator;
688 sa_policy_t *policy;
689 status_t status;
690
691 iterator = this->policies->create_iterator(this->policies, TRUE);
692 while (iterator->iterate(iterator, (void**)&policy))
693 {
694 this->logger->log(this->logger, CONTROL|LEVEL1,
695 "updating policy: %s/%d====%s/%d",
696 policy->me.net->get_address(policy->me.net), policy->me.net_mask,
697 policy->other.net->get_address(policy->other.net), policy->other.net_mask);
698
699 status = charon->kernel_interface->add_policy(
700 charon->kernel_interface,
701 new_me, new_other,
702 policy->me.net, policy->other.net,
703 policy->me.net_mask, policy->other.net_mask,
704 XFRM_POLICY_OUT, policy->upper_proto,
705 this->protocol, this->reqid);
706
707 status |= charon->kernel_interface->add_policy(
708 charon->kernel_interface,
709 new_other, new_me,
710 policy->other.net, policy->me.net,
711 policy->other.net_mask, policy->me.net_mask,
712 XFRM_POLICY_IN, policy->upper_proto,
713 this->protocol, this->reqid);
714
715 status |= charon->kernel_interface->add_policy(
716 charon->kernel_interface,
717 new_other, new_me,
718 policy->other.net, policy->me.net,
719 policy->other.net_mask, policy->me.net_mask,
720 XFRM_POLICY_FWD, policy->upper_proto,
721 this->protocol, this->reqid);
722
723 if (status != SUCCESS)
724 {
725 iterator->destroy(iterator);
726 return FAILED;
727 }
728 }
729 iterator->destroy(iterator);
730
731 return SUCCESS;
732 }
733
734 /**
735 * Implementation of child_sa_t.update_hosts.
736 */
737 static status_t update_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
738 int my_changes, int other_changes)
739 {
740 if (!my_changes || !other_changes)
741 {
742 return SUCCESS;
743 }
744
745 /* update our (initator) SAs */
746 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, TRUE) != SUCCESS)
747 {
748 return FAILED;
749 }
750
751 /* update his (responder) SAs */
752 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, FALSE) != SUCCESS)
753 {
754 return FAILED;
755 }
756
757 /* update policies */
758 if (my_changes & HOST_DIFF_ADDR || other_changes & HOST_DIFF_ADDR)
759 {
760 if (update_policy_hosts(this, new_me, new_other) != SUCCESS)
761 {
762 return FAILED;
763 }
764 }
765
766 /* update hosts */
767 if (my_changes)
768 {
769 this->me.addr->destroy(this->me.addr);
770 this->me.addr = new_me->clone(new_me);
771 }
772
773 if (other_changes)
774 {
775 this->other.addr->destroy(this->other.addr);
776 this->other.addr = new_other->clone(new_other);
777 }
778
779 return SUCCESS;
780 }
781
782 /**
783 * Implementation of child_sa_t.destroy.
784 */
785 static void destroy(private_child_sa_t *this)
786 {
787 sa_policy_t *policy;
788
789 /* delete SAs in the kernel, if they are set up */
790 if (this->me.spi)
791 {
792 charon->kernel_interface->del_sa(charon->kernel_interface,
793 this->me.addr, this->me.spi, this->protocol);
794 }
795 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
796 {
797 charon->kernel_interface->del_sa(charon->kernel_interface,
798 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
799 }
800 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
801 {
802 charon->kernel_interface->del_sa(charon->kernel_interface,
803 this->me.addr, this->alloc_ah_spi, PROTO_AH);
804 }
805 if (this->other.spi)
806 {
807 charon->kernel_interface->del_sa(charon->kernel_interface,
808 this->other.addr, this->other.spi, this->protocol);
809 }
810
811 /* delete all policies in the kernel */
812 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
813 {
814 if (!this->rekeyed)
815 {
816 /* let rekeyed policies, as they are used by another child_sa */
817 charon->kernel_interface->del_policy(charon->kernel_interface,
818 this->me.addr, this->other.addr,
819 policy->me.net, policy->other.net,
820 policy->me.net_mask, policy->other.net_mask,
821 XFRM_POLICY_OUT, policy->upper_proto);
822
823 charon->kernel_interface->del_policy(charon->kernel_interface,
824 this->other.addr, this->me.addr,
825 policy->other.net, policy->me.net,
826 policy->other.net_mask, policy->me.net_mask,
827 XFRM_POLICY_IN, policy->upper_proto);
828
829 charon->kernel_interface->del_policy(charon->kernel_interface,
830 this->other.addr, this->me.addr,
831 policy->other.net, policy->me.net,
832 policy->other.net_mask, policy->me.net_mask,
833 XFRM_POLICY_FWD, policy->upper_proto);
834 }
835 policy->me.net->destroy(policy->me.net);
836 policy->other.net->destroy(policy->other.net);
837 free(policy);
838 }
839 this->policies->destroy(this->policies);
840
841 free(this);
842 }
843
844 /*
845 * Described in header.
846 */
847 child_sa_t * child_sa_create(u_int32_t rekey, host_t *me, host_t* other,
848 u_int32_t soft_lifetime, u_int32_t hard_lifetime,
849 bool use_natt)
850 {
851 static u_int32_t reqid = REQID_START;
852 private_child_sa_t *this = malloc_thing(private_child_sa_t);
853
854 /* public functions */
855 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
856 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
857 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
858 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
859 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))add;
860 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,prf_plus_t*))update;
861 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*))add_policies;
862 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
863 this->public.set_rekeyed = (void (*)(child_sa_t*))set_rekeyed;
864 this->public.log_status = (void (*)(child_sa_t*, logger_t*, char*))log_status;
865 this->public.destroy = (void(*)(child_sa_t*))destroy;
866
867 /* private data */
868 this->logger = logger_manager->get_logger(logger_manager, CHILD_SA);
869 this->me.addr = me;
870 this->other.addr = other;
871 this->me.spi = 0;
872 this->other.spi = 0;
873 this->alloc_ah_spi = 0;
874 this->alloc_esp_spi = 0;
875 this->use_natt = use_natt;
876 this->soft_lifetime = soft_lifetime;
877 this->hard_lifetime = hard_lifetime;
878 /* reuse old reqid if we are rekeying an existing CHILD_SA */
879 this->reqid = rekey ? rekey : ++reqid;
880 this->policies = linked_list_create();
881 this->protocol = PROTO_NONE;
882 this->rekeyed = FALSE;
883
884 return &this->public;
885 }