1 /* information about connections between hosts and clients
2 * Copyright (C) 1998-2002 D. Hugh Redelmeier.
3 * Copyright (C) 2009 Andreas Steffen - Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 #include <netinet/in.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
27 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
28 #include <sys/queue.h>
31 #include "kameipsec.h"
34 #include <credentials/certificates/ac.h>
35 #include <credentials/keys/private_key.h>
37 #include "constants.h"
45 #include "smartcard.h"
47 #include "connections.h"
48 #include "foodgroups.h"
52 #include "ipsec_doi.h" /* needs demux.h and state.h */
57 #include "adns.h" /* needs <resolv.h> */
58 #include "dnskey.h" /* needs keys.h and adns.h */
62 #include "kernel_alg.h"
63 #include "nat_traversal.h"
65 #include "whack_attribute.h"
68 static void flush_pending_by_connection(connection_t
*c
); /* forward */
70 static connection_t
*connections
= NULL
;
72 /* struct host_pair: a nexus of information about a pair of hosts.
73 * A host is an IP address, UDP port pair. This is a debatable choice:
74 * - should port be considered (no choice of port in standard)?
75 * - should ID be considered (hard because not always known)?
76 * - should IP address matter on our end (we don't know our end)?
77 * Only oriented connections are registered.
78 * Unoriented connections are kept on the unoriented_connections
79 * linked list (using hp_next). For them, host_pair is NULL.
85 u_int16_t port
; /* host order */
87 bool initial_connection_sent
;
88 connection_t
*connections
; /* connections with this pair */
89 struct pending
*pending
; /* awaiting Keying Channel */
90 struct host_pair
*next
;
93 static struct host_pair
*host_pairs
= NULL
;
95 static connection_t
*unoriented_connections
= NULL
;
98 * Check if an id was instantiated by assigning to it the current IP address
100 bool his_id_was_instantiated(const connection_t
*c
)
102 if (c
->kind
!= CK_INSTANCE
)
106 if (id_is_ipaddr(c
->spd
.that
.id
))
108 identification_t
*host
;
111 host
= identification_create_from_sockaddr((sockaddr_t
*)&c
->spd
.that
.host_addr
);
112 equal
= host
->equals(host
, c
->spd
.that
.id
);
123 * Check to see that IDs of peers match
125 bool same_peer_ids(const connection_t
*c
, const connection_t
*d
,
126 identification_t
*his_id
)
128 return d
->spd
.this.id
->equals(d
->spd
.this.id
, c
->spd
.this.id
) &&
129 d
->spd
.that
.id
->equals(d
->spd
.that
.id
,
130 his_id ? his_id
: c
->spd
.that
.id
);
133 static struct host_pair
*find_host_pair(const ip_address
*myaddr
,
135 const ip_address
*hisaddr
,
138 struct host_pair
*p
, *prev
;
140 /* default hisaddr to an appropriate any */
142 hisaddr
= aftoinfo(addrtypeof(myaddr
))->any
;
144 if (nat_traversal_enabled
)
147 * port is not relevant in host_pair. with nat_traversal we
148 * always use pluto_port (500)
151 hisport
= pluto_port
;
154 for (prev
= NULL
, p
= host_pairs
; p
!= NULL
; prev
= p
, p
= p
->next
)
156 if (sameaddr(&p
->me
.addr
, myaddr
) && p
->me
.port
== myport
157 && sameaddr(&p
->him
.addr
, hisaddr
) && p
->him
.port
== hisport
)
161 prev
->next
= p
->next
; /* remove p from list */
162 p
->next
= host_pairs
; /* and stick it on front */
171 /* find head of list of connections with this pair of hosts */
172 static connection_t
*find_host_pair_connections(const ip_address
*myaddr
,
174 const ip_address
*hisaddr
,
177 struct host_pair
*hp
= find_host_pair(myaddr
, myport
, hisaddr
, hisport
);
179 if (nat_traversal_enabled
&& hp
&& hisaddr
)
183 for (c
= hp
->connections
; c
!= NULL
; c
= c
->hp_next
)
185 if (c
->spd
.this.host_port
== myport
&& c
->spd
.that
.host_port
== hisport
)
190 return hp
== NULL? NULL
: hp
->connections
;
193 static void connect_to_host_pair(connection_t
*c
)
197 struct host_pair
*hp
;
199 ip_address his_addr
= (c
->spd
.that
.allow_any
)
200 ?
*aftoinfo(addrtypeof(&c
->spd
.that
.host_addr
))->any
201 : c
->spd
.that
.host_addr
;
203 hp
= find_host_pair(&c
->spd
.this.host_addr
, c
->spd
.this.host_port
204 , &his_addr
, c
->spd
.that
.host_port
);
208 /* no suitable host_pair -- build one */
209 hp
= malloc_thing(struct host_pair
);
210 hp
->me
.addr
= c
->spd
.this.host_addr
;
211 hp
->him
.addr
= his_addr
;
212 hp
->me
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.this.host_port
;
213 hp
->him
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.that
.host_port
;
214 hp
->initial_connection_sent
= FALSE
;
215 hp
->connections
= NULL
;
217 hp
->next
= host_pairs
;
221 c
->hp_next
= hp
->connections
;
226 /* since this connection isn't oriented, we place it
227 * in the unoriented_connections list instead.
230 c
->hp_next
= unoriented_connections
;
231 unoriented_connections
= c
;
235 /* find a connection by name.
236 * If strict, don't accept a CK_INSTANCE.
237 * Move the winner (if any) to the front.
238 * If none is found, and strict, a diagnostic is logged to whack.
240 connection_t
*con_by_name(const char *nm
, bool strict
)
242 connection_t
*p
, *prev
;
244 for (prev
= NULL
, p
= connections
; ; prev
= p
, p
= p
->ac_next
)
249 whack_log(RC_UNKNOWN_NAME
250 , "no connection named \"%s\"", nm
);
253 if (streq(p
->name
, nm
)
254 && (!strict
|| p
->kind
!= CK_INSTANCE
))
258 prev
->ac_next
= p
->ac_next
; /* remove p from list */
259 p
->ac_next
= connections
; /* and stick it on front */
268 void release_connection(connection_t
*c
, bool relations
)
270 if (c
->kind
== CK_INSTANCE
)
272 /* This does everything we need.
273 * Note that we will be called recursively by delete_connection,
274 * but kind will be CK_GOING_AWAY.
276 delete_connection(c
, relations
);
280 flush_pending_by_connection(c
);
281 delete_states_by_connection(c
, relations
);
282 unroute_connection(c
);
286 /* Delete a connection */
288 #define list_rm(etype, enext, e, ehead) { \
290 for (ep = &(ehead); *ep != (e); ep = &(*ep)->enext) \
291 passert(*ep != NULL); /* we must not come up empty-handed */ \
296 void delete_connection(connection_t
*c
, bool relations
)
298 modecfg_attribute_t
*ca
;
299 connection_t
*old_cur_connection
;
301 old_cur_connection
= cur_connection
== c? NULL
: cur_connection
;
303 lset_t old_cur_debugging
= cur_debugging
;
306 set_cur_connection(c
);
308 /* Must be careful to avoid circularity:
309 * we mark c as going away so it won't get deleted recursively.
311 passert(c
->kind
!= CK_GOING_AWAY
);
312 if (c
->kind
== CK_INSTANCE
)
314 plog("deleting connection \"%s\" instance with peer %s {isakmp=#%lu/ipsec=#%lu}"
316 , ip_str(&c
->spd
.that
.host_addr
)
317 , c
->newest_isakmp_sa
, c
->newest_ipsec_sa
);
318 c
->kind
= CK_GOING_AWAY
;
322 plog("deleting connection");
324 release_connection(c
, relations
); /* won't delete c */
326 if (c
->kind
== CK_GROUP
)
331 /* free up any logging resources */
334 /* find and delete c from connections list */
335 list_rm(connection_t
, ac_next
, c
, connections
);
336 cur_connection
= old_cur_connection
;
338 /* find and delete c from the host pair list */
339 if (c
->host_pair
== NULL
)
343 list_rm(connection_t
, hp_next
, c
, unoriented_connections
);
348 struct host_pair
*hp
= c
->host_pair
;
350 list_rm(connection_t
, hp_next
, c
, hp
->connections
);
351 c
->host_pair
= NULL
; /* redundant, but safe */
353 /* if there are no more connections with this host_pair
354 * and we haven't even made an initial contact, let's delete
355 * this guy in case we were created by an attempted DOS attack.
357 if (hp
->connections
== NULL
358 && !hp
->initial_connection_sent
)
360 passert(hp
->pending
== NULL
); /* ??? must deal with this! */
361 list_rm(struct host_pair
, next
, hp
, host_pairs
);
365 if (c
->kind
!= CK_GOING_AWAY
)
367 free(c
->spd
.that
.virt
);
370 /* release virtual IP address lease if any */
371 if (c
->spd
.that
.modecfg
&& c
->spd
.that
.pool
&&
372 !c
->spd
.that
.host_srcip
->is_anyaddr(c
->spd
.that
.host_srcip
))
374 hydra
->attributes
->release_address(hydra
->attributes
, c
->spd
.that
.pool
,
375 c
->spd
.that
.host_srcip
, c
->spd
.that
.id
);
378 /* release requested attributes if any */
381 c
->requested
->destroy_function(c
->requested
,
382 (void*)modecfg_attribute_destroy
);
385 /* release other attributes if any */
388 while (c
->attributes
->remove_last(c
->attributes
, (void **)&ca
) == SUCCESS
)
390 hydra
->attributes
->release(hydra
->attributes
, ca
->handler
,
391 c
->spd
.that
.id
, ca
->type
, ca
->value
);
392 modecfg_attribute_destroy(ca
);
394 c
->attributes
->destroy(c
->attributes
);
397 if (c
->kind
!= CK_GOING_AWAY
)
399 whack_attr
->del_pool(whack_attr
, c
->name
);
402 /* free internal data */
404 cur_debugging
= old_cur_debugging
;
407 DESTROY_IF(c
->spd
.this.id
);
408 DESTROY_IF(c
->spd
.this.ca
);
409 DESTROY_IF(c
->spd
.this.groups
);
410 DESTROY_IF(c
->spd
.this.host_srcip
);
412 free(c
->spd
.this.updown
);
413 free(c
->spd
.this.pool
);
414 DESTROY_IF(c
->spd
.that
.id
);
415 DESTROY_IF(c
->spd
.that
.ca
);
416 DESTROY_IF(c
->spd
.that
.groups
);
417 DESTROY_IF(c
->spd
.that
.host_srcip
);
418 free(c
->spd
.that
.updown
);
419 free(c
->spd
.that
.pool
);
422 c
->requested_ca
->destroy_offset(c
->requested_ca
,
423 offsetof(identification_t
, destroy
));
425 gw_delref(&c
->gw_info
);
427 lock_certs_and_keys("delete_connection");
428 cert_release(c
->spd
.this.cert
);
429 scx_release(c
->spd
.this.sc
);
430 cert_release(c
->spd
.that
.cert
);
431 scx_release(c
->spd
.that
.sc
);
432 unlock_certs_and_keys("delete_connection");
434 alg_info_delref((struct alg_info
**)&c
->alg_info_esp
);
435 alg_info_delref((struct alg_info
**)&c
->alg_info_ike
);
440 /* Delete connections with the specified name */
441 void delete_connections_by_name(const char *name
, bool strict
)
443 connection_t
*c
= con_by_name(name
, strict
);
445 for (; c
!= NULL
; c
= con_by_name(name
, FALSE
))
446 delete_connection(c
, FALSE
);
449 void delete_every_connection(void)
453 delete_connection(connections
, TRUE
);
457 void release_dead_interfaces(void)
459 struct host_pair
*hp
;
461 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
466 for (pp
= &hp
->connections
; (p
= *pp
) != NULL
; )
468 if (p
->interface
->change
== IFN_DELETE
)
470 /* this connection's interface is going away */
471 enum connection_kind k
= p
->kind
;
473 release_connection(p
, TRUE
);
475 if (k
<= CK_PERMANENT
)
477 /* The connection should have survived release:
478 * move it to the unoriented_connections list.
484 *pp
= p
->hp_next
; /* advance *pp */
486 p
->hp_next
= unoriented_connections
;
487 unoriented_connections
= p
;
491 /* The connection should have vanished,
492 * but the previous connection remains.
499 pp
= &p
->hp_next
; /* advance pp */
505 /* adjust orientations of connections to reflect newly added interfaces */
506 void check_orientations(void)
508 /* try to orient all the unoriented connections */
510 connection_t
*c
= unoriented_connections
;
512 unoriented_connections
= NULL
;
516 connection_t
*nxt
= c
->hp_next
;
519 connect_to_host_pair(c
);
524 /* Check that no oriented connection has become double-oriented.
525 * In other words, the far side must not match one of our new interfaces.
530 for (i
= interfaces
; i
!= NULL
; i
= i
->next
)
532 if (i
->change
== IFN_ADD
)
534 struct host_pair
*hp
;
536 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
538 if (sameaddr(&hp
->him
.addr
, &i
->addr
)
539 && (!no_klips
|| hp
->him
.port
== pluto_port
))
541 /* bad news: the whole chain of connections
542 * hanging off this host pair has both sides
543 * matching an interface.
544 * We'll get rid of them, using orient and
545 * connect_to_host_pair. But we'll be lazy
546 * and not ditch the host_pair itself (the
547 * cost of leaving it is slight and cannot
548 * be induced by a foe).
550 connection_t
*c
= hp
->connections
;
552 hp
->connections
= NULL
;
555 connection_t
*nxt
= c
->hp_next
;
559 connect_to_host_pair(c
);
569 static err_t
default_end(struct end
*e
, ip_address
*dflt_nexthop
)
572 int af
= addrtypeof(&e
->host_addr
);
574 if (af
!= AF_INET
&& af
!= AF_INET6
)
576 return "unknown address family in default_end";
579 /* default ID to IP (but only if not NO_IP -- WildCard) */
580 if (e
->id
->get_type(e
->id
) == ID_ANY
&& !isanyaddr(&e
->host_addr
))
582 e
->id
->destroy(e
->id
);
583 e
->id
= identification_create_from_sockaddr((sockaddr_t
*)&e
->host_addr
);
584 e
->has_id_wildcards
= FALSE
;
587 /* default nexthop to other side */
588 if (isanyaddr(&e
->host_nexthop
))
590 e
->host_nexthop
= *dflt_nexthop
;
593 /* default client to subnet containing only self
594 * XXX This may mean that the client's address family doesn't match
595 * tunnel_addr_family.
599 ugh
= addrtosubnet(&e
->host_addr
, &e
->client
);
604 /* Format the topology of a connection end, leaving out defaults.
605 * Largest left end looks like: client === host : port [ host_id ] --- hop
606 * Note: if that==NULL, skip nexthop
607 * Returns strlen of formated result (length excludes NUL at end).
609 size_t format_end(char *buf
, size_t buf_len
, const struct end
*this,
610 const struct end
*that
, bool is_left
, lset_t policy
)
612 char client
[BUF_LEN
];
613 const char *client_sep
= "";
614 char protoport
[sizeof(":255/65535")];
615 const char *host
= NULL
;
616 char host_space
[ADDRTOT_BUF
];
617 char host_port
[sizeof(":65535")];
618 char host_id
[BUF_LEN
+ 2];
619 char hop
[ADDRTOT_BUF
];
620 const char *hop_sep
= "";
621 const char *open_brackets
= "";
622 const char *close_brackets
= "";
624 if (isanyaddr(&this->host_addr
))
626 switch (policy
& (POLICY_GROUP
| POLICY_OPPO
))
632 host
= "%opportunistic";
634 case POLICY_GROUP
| POLICY_OPPO
:
635 host
= "%opportunisticgroup";
645 if (is_virtual_end(this) && isanyaddr(&this->host_addr
))
651 if (this->has_client
)
653 ip_address client_net
, client_mask
;
655 networkof(&this->client
, &client_net
);
656 maskof(&this->client
, &client_mask
);
659 /* {client_subnet_wildcard} */
660 if (this->has_client_wildcard
)
663 close_brackets
= "}";
666 if (isanyaddr(&client_net
) && isanyaddr(&client_mask
)
667 && (policy
& (POLICY_GROUP
| POLICY_OPPO
)))
669 client_sep
= ""; /* boring case */
671 else if (subnetisnone(&this->client
))
677 subnettot(&this->client
, 0, client
, sizeof(client
));
680 else if (this->modecfg
&& this->host_srcip
->is_anyaddr(this->host_srcip
))
682 /* we are mode config client, or a server with a pool */
685 strcpy(client
+1, this->pool ?
this->pool
: "modecfg");
691 addrtot(&this->host_addr
, 0, host_space
, sizeof(host_space
));
696 if (this->host_port
!= IKE_UDP_PORT
)
698 snprintf(host_port
, sizeof(host_port
), ":%u", this->host_port
);
701 /* payload portocol and port */
703 if (this->has_port_wildcard
)
705 snprintf(protoport
, sizeof(protoport
), ":%u/%%any", this->protocol
);
707 else if (this->port
|| this->protocol
)
709 snprintf(protoport
, sizeof(protoport
), ":%u/%u", this->protocol
714 snprintf(host_id
, sizeof(host_id
), "[%Y]", this->id
);
719 if (that
&& !sameaddr(&this->host_nexthop
, &that
->host_addr
))
721 addrtot(&this->host_nexthop
, 0, hop
, sizeof(hop
));
727 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
728 , open_brackets
, client
, close_brackets
, client_sep
729 , this->allow_any?
"%":""
730 , host
, host_port
, host_id
, protoport
735 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
737 , this->allow_any?
"%":""
738 , host
, host_port
, host_id
, protoport
, client_sep
739 , open_brackets
, client
, close_brackets
);
744 /* format topology of a connection.
745 * Two symmetric ends separated by ...
747 #define CONNECTION_BUF (2 * (END_BUF - 1) + 4)
749 static size_t format_connection(char *buf
, size_t buf_len
,
750 const connection_t
*c
,
751 struct spd_route
*sr
)
753 size_t w
= format_end(buf
, buf_len
, &sr
->this, &sr
->that
, TRUE
, LEMPTY
);
755 w
+= snprintf(buf
+ w
, buf_len
- w
, "...");
756 return w
+ format_end(buf
+ w
, buf_len
- w
, &sr
->that
, &sr
->this, FALSE
, c
->policy
);
759 static void unshare_connection_strings(connection_t
*c
)
761 c
->name
= clone_str(c
->name
);
762 c
->spd
.this.id
= c
->spd
.this.id
->clone(c
->spd
.this.id
);
763 c
->spd
.this.pool
= clone_str(c
->spd
.this.pool
);
764 c
->spd
.this.updown
= clone_str(c
->spd
.this.updown
);
765 c
->spd
.this.host_srcip
= c
->spd
.this.host_srcip
->clone(c
->spd
.this.host_srcip
);
766 scx_share(c
->spd
.this.sc
);
767 cert_share(c
->spd
.this.cert
);
770 c
->spd
.this.ca
= c
->spd
.this.ca
->clone(c
->spd
.this.ca
);
772 if (c
->spd
.this.groups
)
774 c
->spd
.this.groups
= c
->spd
.this.groups
->get_ref(c
->spd
.this.groups
);
776 c
->spd
.that
.id
= c
->spd
.that
.id
->clone(c
->spd
.that
.id
);
777 c
->spd
.that
.pool
= clone_str(c
->spd
.that
.pool
);
778 c
->spd
.that
.updown
= clone_str(c
->spd
.that
.updown
);
779 c
->spd
.that
.host_srcip
= c
->spd
.that
.host_srcip
->clone(c
->spd
.that
.host_srcip
);
780 scx_share(c
->spd
.that
.sc
);
781 cert_share(c
->spd
.that
.cert
);
784 c
->spd
.that
.ca
= c
->spd
.that
.ca
->clone(c
->spd
.that
.ca
);
786 if (c
->spd
.that
.groups
)
788 c
->spd
.that
.groups
= c
->spd
.that
.groups
->get_ref(c
->spd
.that
.groups
);
791 /* increment references to algo's */
792 alg_info_addref((struct alg_info
*)c
->alg_info_esp
);
793 alg_info_addref((struct alg_info
*)c
->alg_info_ike
);
796 static void load_end_certificate(char *filename
, struct end
*dst
)
798 time_t notBefore
, notAfter
;
800 certificate_t
*certificate
;
801 bool cached_cert
= FALSE
;
803 /* initialize end certificate */
806 /* initialize smartcard info record */
811 if (scx_on_smartcard(filename
))
813 /* load cert from smartcard */
814 cert
= scx_load_cert(filename
, &dst
->sc
, &cached_cert
);
818 /* load cert from file */
819 cert
= load_host_cert(filename
);
825 certificate
= cert
->cert
;
827 if (dst
->id
->get_type(dst
->id
) == ID_ANY
||
828 !certificate
->has_subject(certificate
, dst
->id
))
830 plog( " id '%Y' not confirmed by certificate, defaulting to '%Y'",
831 dst
->id
, certificate
->get_subject(certificate
));
832 dst
->id
->destroy(dst
->id
);
833 dst
->id
= certificate
->get_subject(certificate
);
834 dst
->id
= dst
->id
->clone(dst
->id
);
843 if (!certificate
->get_validity(certificate
, NULL
, ¬Before
, ¬After
))
845 plog("certificate is invalid (valid from %T to %T)",
846 ¬Before
, FALSE
, ¬After
, FALSE
);
851 DBG_log("certificate is valid")
853 add_public_key_from_cert(cert
, notAfter
, DAL_LOCAL
);
854 dst
->cert
= cert_add(cert
);
856 certificate
= dst
->cert
->cert
;
858 /* if no CA is defined, use issuer as default */
859 if (dst
->ca
== NULL
&& certificate
->get_type(certificate
) == CERT_X509
)
861 identification_t
*issuer
;
863 issuer
= certificate
->get_issuer(certificate
);
864 dst
->ca
= issuer
->clone(issuer
);
867 /* cache the certificate that was last retrieved from the smartcard */
870 if (!certificate
->equals(certificate
, dst
->sc
->last_cert
->cert
))
872 lock_certs_and_keys("load_end_certificates");
873 cert_release(dst
->sc
->last_cert
);
874 dst
->sc
->last_cert
= dst
->cert
;
875 cert_share(dst
->cert
);
876 unlock_certs_and_keys("load_end_certificates");
878 time(&dst
->sc
->last_load
);
882 cert_share(dst
->cert
);
885 static bool extract_end(struct end
*dst
, const whack_end_t
*src
,
886 const char *name
, bool is_left
)
888 bool same_ca
= FALSE
;
890 dst
->is_left
= is_left
;
891 dst
->id
= identification_create_from_string(src
->id
);
894 /* decode CA distinguished name, if any */
897 if streq(src
->ca
, "%same")
901 else if (!streq(src
->ca
, "%any"))
903 dst
->ca
= identification_create_from_string(src
->ca
);
904 if (dst
->ca
->get_type(dst
->ca
) != ID_DER_ASN1_DN
)
906 plog("bad CA string '%s', ignored", src
->ca
);
907 dst
->ca
->destroy(dst
->ca
);
913 /* load local end certificate and extract ID, if any */
914 load_end_certificate(src
->cert
, dst
);
916 /* does id has wildcards? */
917 dst
->has_id_wildcards
= dst
->id
->contains_wildcards(dst
->id
);
919 /* decode group attributes, if any */
922 dst
->groups
= ietf_attributes_create_from_string(src
->groups
);
925 /* the rest is simple copying of corresponding fields */
926 dst
->host_addr
= src
->host_addr
;
927 dst
->host_nexthop
= src
->host_nexthop
;
928 dst
->host_srcip
= host_create_from_sockaddr((sockaddr_t
*)&src
->host_srcip
);
929 dst
->has_natip
= src
->has_natip
;
930 dst
->client
= src
->client
;
931 dst
->protocol
= src
->protocol
;
932 dst
->port
= src
->port
;
933 dst
->has_port_wildcard
= src
->has_port_wildcard
;
934 dst
->key_from_DNS_on_demand
= src
->key_from_DNS_on_demand
;
935 dst
->has_client
= src
->has_client
;
936 dst
->has_client_wildcard
= src
->has_client_wildcard
;
937 dst
->modecfg
= src
->modecfg
;
938 dst
->hostaccess
= src
->hostaccess
;
939 dst
->allow_any
= src
->allow_any
;
940 dst
->sendcert
= src
->sendcert
;
941 dst
->updown
= clone_str(src
->updown
);
942 dst
->host_port
= src
->host_port
;
944 /* if the sourceip netmask is zero a named pool exists */
945 if (src
->sourceip_mask
== 0)
947 dst
->pool
= clone_str(src
->sourceip
);
950 /* if host sourceip is defined but no client is present
951 * behind the host then set client to sourceip/32
953 if (!dst
->host_srcip
->is_anyaddr(dst
->host_srcip
) &&
954 !dst
->has_natip
&& !dst
->has_client
)
959 addr
= *(ip_address
*)dst
->host_srcip
->get_sockaddr(dst
->host_srcip
);
960 ugh
= addrtosubnet(&addr
, &dst
->client
);
964 plog("could not assign host sourceip to client subnet");
968 dst
->has_client
= TRUE
;
974 static bool check_connection_end(const whack_end_t
*this,
975 const whack_end_t
*that
,
976 const whack_message_t
*wm
)
978 if (wm
->addr_family
!= addrtypeof(&this->host_addr
)
979 || wm
->addr_family
!= addrtypeof(&this->host_nexthop
)
980 || (this->has_client? wm
->tunnel_addr_family
: wm
->addr_family
)
981 != subnettypeof(&this->client
)
982 || subnettypeof(&this->client
) != subnettypeof(&that
->client
))
984 /* this should have been diagnosed by whack, so we need not be clear
985 * !!! overloaded use of RC_CLASH
987 loglog(RC_CLASH
, "address family inconsistency in connection");
991 if (isanyaddr(&that
->host_addr
))
993 /* other side is wildcard: we must check if other conditions met */
994 if (isanyaddr(&this->host_addr
))
996 loglog(RC_ORIENT
, "connection must specify host IP address for our side");
1001 if (this->virt
&& (!isanyaddr(&this->host_addr
) || this->has_client
))
1004 "virtual IP must only be used with %%any and without client");
1008 return TRUE
; /* happy */
1011 connection_t
*find_connection_by_reqid(uint32_t reqid
)
1016 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
1018 if (c
->spd
.reqid
== reqid
)
1027 static uint32_t gen_reqid(void)
1030 static uint32_t reqid
= IPSEC_MANUAL_REQID_MAX
& ~3;
1037 reqid
= (IPSEC_MANUAL_REQID_MAX
& ~3) + 4;
1039 if (!find_connection_by_reqid(reqid
))
1043 } while (reqid
!= start
);
1045 exit_log("unable to allocate reqid");
1046 return 0; /* never reached ... */
1049 void add_connection(const whack_message_t
*wm
)
1051 if (con_by_name(wm
->name
, FALSE
) != NULL
)
1053 loglog(RC_DUPNAME
, "attempt to redefine connection \"%s\"", wm
->name
);
1055 else if (wm
->right
.protocol
!= wm
->left
.protocol
)
1057 /* this should haven been diagnosed by whack
1058 * !!! overloaded use of RC_CLASH
1060 loglog(RC_CLASH
, "the protocol must be the same for leftport and rightport");
1062 else if (check_connection_end(&wm
->right
, &wm
->left
, wm
)
1063 && check_connection_end(&wm
->left
, &wm
->right
, wm
))
1065 bool same_rightca
, same_leftca
;
1066 connection_t
*c
= malloc_thing(connection_t
);
1069 c
->name
= clone_str(wm
->name
);
1070 c
->ikev1
= wm
->ikev1
;
1071 c
->policy
= wm
->policy
;
1073 if ((c
->policy
& POLICY_COMPRESS
) && !can_do_IPcomp
)
1076 , "ignoring --compress in \"%s\" because KLIPS is not configured to do IPCOMP"
1083 DBG_log("from whack: got --esp=%s", wm
->esp ? wm
->esp
: "NULL")
1085 c
->alg_info_esp
= alg_info_esp_create_from_str(wm
->esp? wm
->esp
: "");
1087 DBG(DBG_CRYPT
|DBG_CONTROL
,
1088 static char buf
[BUF_LEN
]="<NULL>";
1090 if (c
->alg_info_esp
)
1092 alg_info_snprint(buf
, sizeof(buf
)
1093 ,(struct alg_info
*)c
->alg_info_esp
);
1095 DBG_log("esp proposal: %s", buf
);
1097 if (c
->alg_info_esp
)
1099 if (c
->alg_info_esp
->alg_info_cnt
== 0)
1101 loglog(RC_LOG_SERIOUS
, "got 0 esp transforms");
1106 loglog(RC_LOG_SERIOUS
, "syntax error in esp string");
1113 DBG_log("from whack: got --ike=%s", wm
->ike ? wm
->ike
: "NULL")
1115 c
->alg_info_ike
= alg_info_ike_create_from_str(wm
->ike? wm
->ike
: "");
1117 DBG(DBG_CRYPT
|DBG_CONTROL
,
1118 static char buf
[BUF_LEN
]="<NULL>";
1120 if (c
->alg_info_ike
)
1122 alg_info_snprint(buf
, sizeof(buf
)
1123 , (struct alg_info
*)c
->alg_info_ike
);
1125 DBG_log("ike proposal: %s", buf
);
1127 if (c
->alg_info_ike
)
1129 if (c
->alg_info_ike
->alg_info_cnt
== 0)
1131 loglog(RC_LOG_SERIOUS
, "got 0 ike transforms");
1136 loglog(RC_LOG_SERIOUS
, "syntax error in ike string");
1140 c
->sa_ike_life_seconds
= wm
->sa_ike_life_seconds
;
1141 c
->sa_ipsec_life_seconds
= wm
->sa_ipsec_life_seconds
;
1142 c
->sa_rekey_margin
= wm
->sa_rekey_margin
;
1143 c
->sa_rekey_fuzz
= wm
->sa_rekey_fuzz
;
1144 c
->sa_keying_tries
= wm
->sa_keying_tries
;
1147 c
->dpd_delay
= wm
->dpd_delay
;
1148 c
->dpd_timeout
= wm
->dpd_timeout
;
1149 c
->dpd_action
= wm
->dpd_action
;
1151 c
->addr_family
= wm
->addr_family
;
1152 c
->tunnel_addr_family
= wm
->tunnel_addr_family
;
1154 c
->requested_ca
= NULL
;
1155 same_leftca
= extract_end(&c
->spd
.this, &wm
->left
, wm
->name
, TRUE
);
1156 same_rightca
= extract_end(&c
->spd
.that
, &wm
->right
, wm
->name
, FALSE
);
1158 if (same_rightca
&& c
->spd
.this.ca
)
1160 c
->spd
.that
.ca
= c
->spd
.this.ca
->clone(c
->spd
.this.ca
);
1162 else if (same_leftca
&& c
->spd
.that
.ca
)
1164 c
->spd
.this.ca
= c
->spd
.that
.ca
->clone(c
->spd
.that
.ca
);
1167 default_end(&c
->spd
.this, &c
->spd
.that
.host_addr
);
1168 default_end(&c
->spd
.that
, &c
->spd
.this.host_addr
);
1170 /* force any wildcard host IP address, any wildcard subnet
1171 * or any wildcard ID to that end
1173 if (isanyaddr(&c
->spd
.this.host_addr
) || c
->spd
.this.has_client_wildcard
1174 || c
->spd
.this.has_port_wildcard
|| c
->spd
.this.has_id_wildcards
1175 || c
->spd
.this.allow_any
)
1177 struct end t
= c
->spd
.this;
1179 c
->spd
.this = c
->spd
.that
;
1184 c
->spd
.reqid
= gen_reqid();
1186 /* set internal fields */
1187 c
->instance_serial
= 0;
1188 c
->ac_next
= connections
;
1190 c
->interface
= NULL
;
1191 c
->spd
.routing
= RT_UNROUTED
;
1192 c
->newest_isakmp_sa
= SOS_NOBODY
;
1193 c
->newest_ipsec_sa
= SOS_NOBODY
;
1194 c
->spd
.eroute_owner
= SOS_NOBODY
;
1196 if (c
->policy
& POLICY_GROUP
)
1201 else if ((isanyaddr(&c
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(c
->policy
))
1202 || c
->spd
.that
.has_client_wildcard
|| c
->spd
.that
.has_port_wildcard
1203 || c
->spd
.that
.has_id_wildcards
|| c
->spd
.that
.allow_any
)
1205 /* Opportunistic or Road Warrior or wildcard client subnet
1207 c
->kind
= CK_TEMPLATE
;
1211 c
->kind
= CK_PERMANENT
;
1213 set_policy_prio(c
); /* must be after kind is set */
1216 c
->extra_debugging
= wm
->debugging
;
1221 passert(!(wm
->left
.virt
&& wm
->right
.virt
));
1222 if (wm
->left
.virt
|| wm
->right
.virt
)
1224 passert(isanyaddr(&c
->spd
.that
.host_addr
));
1225 c
->spd
.that
.virt
= create_virtual(c
,
1226 wm
->left
.virt ? wm
->left
.virt
: wm
->right
.virt
);
1227 if (c
->spd
.that
.virt
)
1228 c
->spd
.that
.has_client
= TRUE
;
1233 /* if rightsourceip defines a subnet then create an in-memory pool */
1234 if (whack_attr
->add_pool(whack_attr
, c
->name
,
1235 c
->spd
.this.is_left ?
&wm
->right
: &wm
->left
))
1237 c
->spd
.that
.pool
= clone_str(c
->name
);
1238 c
->spd
.that
.modecfg
= TRUE
;
1239 c
->spd
.that
.has_client
= FALSE
;
1240 /* reset the host_srcip so that it gets assigned in modecfg */
1241 DESTROY_IF(c
->spd
.that
.host_srcip
);
1242 c
->spd
.that
.host_srcip
= host_create_any(AF_INET
);
1247 connect_to_host_pair(c
);
1250 /* log all about this connection */
1251 plog("added connection description \"%s\"", c
->name
);
1255 (void) format_connection(topo
, sizeof(topo
), c
, &c
->spd
);
1257 DBG_log("%s", topo
);
1259 /* Make sure that address families can be correctly inferred
1260 * from printed ends.
1262 passert(c
->addr_family
== addrtypeof(&c
->spd
.this.host_addr
)
1263 && c
->addr_family
== addrtypeof(&c
->spd
.this.host_nexthop
)
1264 && (c
->spd
.this.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1265 == subnettypeof(&c
->spd
.this.client
)
1267 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_addr
)
1268 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_nexthop
)
1269 && (c
->spd
.that
.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1270 == subnettypeof(&c
->spd
.that
.client
));
1272 DBG_log("ike_life: %lus; ipsec_life: %lus; rekey_margin: %lus;"
1273 " rekey_fuzz: %lu%%; keyingtries: %lu; policy: %s"
1274 , (unsigned long) c
->sa_ike_life_seconds
1275 , (unsigned long) c
->sa_ipsec_life_seconds
1276 , (unsigned long) c
->sa_rekey_margin
1277 , (unsigned long) c
->sa_rekey_fuzz
1278 , (unsigned long) c
->sa_keying_tries
1279 , prettypolicy(c
->policy
));
1284 /* Derive a template connection from a group connection and target.
1285 * Similar to instantiate(). Happens at whack --listen.
1286 * Returns name of new connection. May be NULL.
1287 * Caller is responsible for freeing.
1289 char *add_group_instance(connection_t
*group
, const ip_subnet
*target
)
1291 char namebuf
[100], targetbuf
[SUBNETTOT_BUF
];
1295 passert(group
->kind
== CK_GROUP
);
1296 passert(oriented(*group
));
1298 /* manufacture a unique name for this template */
1299 subnettot(target
, 0, targetbuf
, sizeof(targetbuf
));
1300 snprintf(namebuf
, sizeof(namebuf
), "%s#%s", group
->name
, targetbuf
);
1302 if (con_by_name(namebuf
, FALSE
) != NULL
)
1304 loglog(RC_DUPNAME
, "group name + target yields duplicate name \"%s\""
1309 t
= clone_thing(*group
);
1311 unshare_connection_strings(t
);
1312 name
= clone_str(t
->name
);
1313 t
->spd
.that
.client
= *target
;
1314 t
->policy
&= ~(POLICY_GROUP
| POLICY_GROUTED
);
1315 t
->kind
= isanyaddr(&t
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(t
->policy
)
1316 ? CK_TEMPLATE
: CK_INSTANCE
;
1318 /* reset log file info */
1319 t
->log_file_name
= NULL
;
1321 t
->log_file_err
= FALSE
;
1323 t
->spd
.reqid
= gen_reqid();
1325 if (t
->spd
.that
.virt
)
1327 DBG_log("virtual_ip not supported in group instance");
1328 t
->spd
.that
.virt
= NULL
;
1331 /* add to connections list */
1332 t
->ac_next
= connections
;
1335 /* same host_pair as parent: stick after parent on list */
1338 /* route if group is routed */
1339 if (group
->policy
& POLICY_GROUTED
)
1341 if (!trap_connection(t
))
1342 whack_log(RC_ROUTE
, "could not route");
1348 /* an old target has disappeared for a group: delete instance */
1349 void remove_group_instance(const connection_t
*group USED_BY_DEBUG
,
1352 passert(group
->kind
== CK_GROUP
);
1353 passert(oriented(*group
));
1355 delete_connections_by_name(name
, FALSE
);
1358 /* Common part of instantiating a Road Warrior or Opportunistic connection.
1359 * his_id can be used to carry over an ID discovered in Phase 1.
1360 * It must not disagree with the one in c, but if that is unspecified,
1361 * the new connection will use his_id.
1362 * If his_id is NULL, and c.that.id is uninstantiated (ID_ANY), the
1363 * new connection will continue to have an uninstantiated that.id.
1364 * Note: instantiation does not affect port numbers.
1366 * Note that instantiate can only deal with a single SPD/eroute.
1368 static connection_t
*instantiate(connection_t
*c
, const ip_address
*him
,
1369 u_int16_t his_port
, identification_t
*his_id
)
1373 passert(c
->kind
== CK_TEMPLATE
);
1374 passert(c
->spd
.next
== NULL
);
1376 c
->instance_serial
++;
1377 d
= clone_thing(*c
);
1378 d
->spd
.that
.allow_any
= FALSE
;
1382 d
->spd
.that
.id
= his_id
;
1383 d
->spd
.that
.has_id_wildcards
= FALSE
;
1385 unshare_connection_strings(d
);
1386 if (d
->spd
.this.groups
)
1388 d
->spd
.this.groups
= d
->spd
.this.groups
->get_ref(d
->spd
.this.groups
);
1390 if (d
->spd
.that
.groups
)
1392 d
->spd
.that
.groups
= d
->spd
.that
.groups
->get_ref(d
->spd
.that
.groups
);
1394 d
->kind
= CK_INSTANCE
;
1396 passert(oriented(*d
));
1397 d
->spd
.that
.host_addr
= *him
;
1398 setportof(htons(c
->spd
.that
.port
), &d
->spd
.that
.host_addr
);
1400 if (his_port
) d
->spd
.that
.host_port
= his_port
;
1402 default_end(&d
->spd
.that
, &d
->spd
.this.host_addr
);
1404 /* We cannot guess what our next_hop should be, but if it was
1405 * explicitly specified as 0.0.0.0, we set it to be him.
1406 * (whack will not allow nexthop to be elided in RW case.)
1408 default_end(&d
->spd
.this, &d
->spd
.that
.host_addr
);
1410 d
->spd
.reqid
= gen_reqid();
1412 /* set internal fields */
1413 d
->ac_next
= connections
;
1415 d
->spd
.routing
= RT_UNROUTED
;
1416 d
->newest_isakmp_sa
= SOS_NOBODY
;
1417 d
->newest_ipsec_sa
= SOS_NOBODY
;
1418 d
->spd
.eroute_owner
= SOS_NOBODY
;
1420 /* reset log file info */
1421 d
->log_file_name
= NULL
;
1423 d
->log_file_err
= FALSE
;
1425 connect_to_host_pair(d
);
1428 if (sameaddr(&d
->spd
.that
.host_addr
, &d
->spd
.this.host_nexthop
))
1430 d
->spd
.this.host_nexthop
= *him
;
1434 connection_t
*rw_instantiate(connection_t
*c
, const ip_address
*him
,
1435 u_int16_t his_port
, const ip_subnet
*his_net
,
1436 identification_t
*his_id
)
1438 connection_t
*d
= instantiate(c
, him
, his_port
, his_id
);
1440 if (d
&& his_net
&& is_virtual_connection(c
))
1442 d
->spd
.that
.client
= *his_net
;
1443 d
->spd
.that
.virt
= NULL
;
1444 if (subnetishost(his_net
) && addrinsubnet(him
, his_net
))
1445 d
->spd
.that
.has_client
= FALSE
;
1448 if (d
->policy
& POLICY_OPPO
)
1450 /* This must be before we know the client addresses.
1451 * Fill in one that is impossible. This prevents anyone else from
1452 * trying to use this connection to get to a particular client
1454 d
->spd
.that
.client
= *aftoinfo(subnettypeof(&d
->spd
.that
.client
))->none
;
1457 , DBG_log("instantiated \"%s\" for %s" , d
->name
, ip_str(him
)));
1461 connection_t
*oppo_instantiate(connection_t
*c
, const ip_address
*him
,
1462 identification_t
*his_id
, struct gw_info
*gw
,
1463 const ip_address
*our_client USED_BY_DEBUG
,
1464 const ip_address
*peer_client
)
1466 connection_t
*d
= instantiate(c
, him
, 0, his_id
);
1468 passert(d
->spd
.next
== NULL
);
1470 /* fill in our client side */
1471 if (d
->spd
.this.has_client
)
1473 /* there was a client in the abstract connection
1474 * so we demand that the required client is within that subnet.
1476 passert(addrinsubnet(our_client
, &d
->spd
.this.client
));
1477 happy(addrtosubnet(our_client
, &d
->spd
.this.client
));
1478 /* opportunistic connections do not use port selectors */
1479 setportof(0, &d
->spd
.this.client
.addr
);
1483 /* there was no client in the abstract connection
1484 * so we demand that the required client be the host
1486 passert(sameaddr(our_client
, &d
->spd
.this.host_addr
));
1489 /* fill in peer's client side.
1490 * If the client is the peer, excise the client from the connection.
1492 passert((d
->policy
& POLICY_OPPO
)
1493 && addrinsubnet(peer_client
, &d
->spd
.that
.client
));
1494 happy(addrtosubnet(peer_client
, &d
->spd
.that
.client
));
1495 /* opportunistic connections do not use port selectors */
1496 setportof(0, &d
->spd
.that
.client
.addr
);
1498 if (sameaddr(peer_client
, &d
->spd
.that
.host_addr
))
1499 d
->spd
.that
.has_client
= FALSE
;
1501 passert(d
->gw_info
== NULL
);
1505 /* Adjust routing if something is eclipsing c.
1506 * It must be a %hold for us (hard to passert this).
1507 * If there was another instance eclipsing, we'd be using it.
1509 if (c
->spd
.routing
== RT_ROUTED_ECLIPSED
)
1510 d
->spd
.routing
= RT_ROUTED_PROSPECTIVE
;
1512 /* Remember if the template is routed:
1513 * if so, this instance applies for initiation
1514 * even if it is created for responding.
1516 if (routed(c
->spd
.routing
))
1517 d
->instance_initiation_ok
= TRUE
;
1522 (void) format_connection(topo
, sizeof(topo
), d
, &d
->spd
);
1523 DBG_log("instantiated \"%s\": %s", d
->name
, topo
);
1528 /* priority formatting */
1529 void fmt_policy_prio(policy_prio_t pp
, char buf
[POLICY_PRIO_BUF
])
1531 if (pp
== BOTTOM_PRIO
)
1533 snprintf(buf
, POLICY_PRIO_BUF
, "0");
1537 snprintf(buf
, POLICY_PRIO_BUF
, "%lu,%lu"
1538 , pp
>>16, (pp
& ~(~(policy_prio_t
)0 << 16)) >> 8);
1542 /* Format any information needed to identify an instance of a connection.
1543 * Fills any needed information into buf which MUST be big enough.
1544 * Road Warrior: peer's IP address
1545 * Opportunistic: [" " myclient "==="] " ..." peer ["===" hisclient] '\0'
1547 static size_t fmt_client(const ip_subnet
*client
, const ip_address
*gw
,
1548 const char *prefix
, char buf
[ADDRTOT_BUF
])
1550 if (subnetisaddr(client
, gw
))
1552 buf
[0] = '\0'; /* compact denotation for "self" */
1558 strcpy(buf
, prefix
);
1559 ap
= buf
+ strlen(prefix
);
1560 if (subnetisnone(client
))
1561 strcpy(ap
, "?"); /* unknown */
1563 subnettot(client
, 0, ap
, SUBNETTOT_BUF
);
1568 void fmt_conn_instance(const connection_t
*c
, char buf
[CONN_INST_BUF
])
1574 if (c
->kind
== CK_INSTANCE
)
1576 if (c
->instance_serial
!= 0)
1578 snprintf(p
, CONN_INST_BUF
, "[%lu]", c
->instance_serial
);
1582 if (c
->policy
& POLICY_OPPO
)
1584 size_t w
= fmt_client(&c
->spd
.this.client
, &c
->spd
.this.host_addr
, " ", p
);
1588 strcpy(p
, w
== 0?
" ..." : "=== ...");
1591 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1594 (void) fmt_client(&c
->spd
.that
.client
, &c
->spd
.that
.host_addr
, "===", p
);
1599 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1601 if (c
->spd
.that
.host_port
!= pluto_port
)
1604 sprintf(p
, ":%d", c
->spd
.that
.host_port
);
1610 /* Find an existing connection for a trapped outbound packet.
1611 * This is attempted before we bother with gateway discovery.
1612 * + this connection is routed or instance_of_routed_template
1613 * (i.e. approved for on-demand)
1614 * + this subnet contains our_client (or we are our_client)
1615 * + that subnet contains peer_client (or peer is peer_client)
1616 * + don't care about Phase 1 IDs (we don't know)
1617 * Note: result may still need to be instantiated.
1618 * The winner has the highest policy priority.
1620 * If there are several with that priority, we give preference to
1621 * the first one that is an instance.
1623 * See also build_outgoing_opportunistic_connection.
1625 connection_t
*find_connection_for_clients(struct spd_route
**srp
,
1626 const ip_address
*our_client
,
1627 const ip_address
*peer_client
,
1628 int transport_proto
)
1630 connection_t
*c
= connections
, *best
= NULL
;
1631 policy_prio_t best_prio
= BOTTOM_PRIO
;
1632 struct spd_route
*sr
;
1633 struct spd_route
*best_sr
= NULL
;
1634 int our_port
= ntohs(portof(our_client
));
1635 int peer_port
= ntohs(portof(peer_client
));
1637 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1639 if (DBGP(DBG_CONTROL
))
1641 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1643 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1644 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1645 DBG_log("find_connection: "
1646 "looking for policy for connection: %s:%d/%d -> %s:%d/%d"
1647 , ocb
, transport_proto
, our_port
, pcb
, transport_proto
, peer_port
);
1651 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
1653 if (c
->kind
== CK_GROUP
)
1658 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1660 if ((routed(sr
->routing
) || c
->instance_initiation_ok
)
1661 && addrinsubnet(our_client
, &sr
->this.client
)
1662 && addrinsubnet(peer_client
, &sr
->that
.client
)
1663 && addrinsubnet(peer_client
, &sr
->that
.client
)
1664 && (!sr
->this.protocol
|| transport_proto
== sr
->this.protocol
)
1665 && (!sr
->this.port
|| our_port
== sr
->this.port
)
1666 && (!sr
->that
.port
|| peer_port
== sr
->that
.port
))
1668 char cib
[CONN_INST_BUF
];
1669 char cib2
[CONN_INST_BUF
];
1671 policy_prio_t prio
= 8 * (c
->prio
+ (c
->kind
== CK_INSTANCE
))
1672 + 2 * (sr
->this.port
== our_port
)
1673 + 2 * (sr
->that
.port
== peer_port
)
1674 + (sr
->this.protocol
== transport_proto
);
1677 if (DBGP(DBG_CONTROL
|DBG_CONTROLMORE
))
1679 char c_ocb
[SUBNETTOT_BUF
], c_pcb
[SUBNETTOT_BUF
];
1681 subnettot(&c
->spd
.this.client
, 0, c_ocb
, sizeof(c_ocb
));
1682 subnettot(&c
->spd
.that
.client
, 0, c_pcb
, sizeof(c_pcb
));
1683 DBG_log("find_connection: conn \"%s\"%s has compatible peers: %s->%s [pri: %ld]"
1685 , (fmt_conn_instance(c
, cib
), cib
)
1686 , c_ocb
, c_pcb
, prio
);
1697 DBG(DBG_CONTROLMORE
,
1698 DBG_log("find_connection: "
1699 "comparing best \"%s\"%s [pri:%ld]{%p} (child %s) to \"%s\"%s [pri:%ld]{%p} (child %s)"
1701 , (fmt_conn_instance(best
, cib
), cib
)
1704 , (best
->policy_next ? best
->policy_next
->name
: "none")
1706 , (fmt_conn_instance(c
, cib2
), cib2
)
1709 , (c
->policy_next ? c
->policy_next
->name
: "none")));
1711 if (prio
> best_prio
)
1721 if (best
&& NEVER_NEGOTIATE(best
->policy
))
1731 if (DBGP(DBG_CONTROL
))
1735 char cib
[CONN_INST_BUF
];
1736 DBG_log("find_connection: concluding with \"%s\"%s [pri:%ld]{%p} kind=%s"
1738 , (fmt_conn_instance(best
, cib
), cib
)
1741 , enum_name(&connection_kind_names
, best
->kind
));
1743 DBG_log("find_connection: concluding with empty");
1751 /* Find and instantiate a connection for an outgoing Opportunistic connection.
1752 * We've already discovered its gateway.
1753 * We look for a the connection such that:
1754 * + this is one of our interfaces
1755 * + this subnet contains our_client (or we are our_client)
1756 * (we will specialize the client). We prefer the smallest such subnet.
1757 * + that subnet contains peer_clent (we will specialize the client).
1758 * We prefer the smallest such subnet.
1759 * + is opportunistic
1760 * + that peer is NO_IP
1761 * + don't care about Phase 1 IDs (probably should be default)
1762 * We could look for a connection that already had the desired peer
1763 * (rather than NO_IP) specified, but it doesn't seem worth the
1766 * We look for the routed policy applying to the narrowest subnets.
1767 * We only succeed if we find such a policy AND it is satisfactory.
1769 * The body of the inner loop is a lot like that in
1770 * find_connection_for_clients. In this case, we know the gateways
1771 * that we need to instantiate an opportunistic connection.
1773 connection_t
*build_outgoing_opportunistic_connection(struct gw_info
*gw
,
1774 const ip_address
*our_client
,
1775 const ip_address
*peer_client
)
1778 connection_t
*best
= NULL
;
1779 struct spd_route
*sr
, *bestsr
;
1780 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1782 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1783 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1785 /* for each of our addresses... */
1786 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1788 /* go through those connections with our address and NO_IP as hosts
1789 * We cannot know what port the peer would use, so we assume
1790 * that it is pluto_port (makes debugging easier).
1792 connection_t
*c
= find_host_pair_connections(&p
->addr
, pluto_port
,
1793 (ip_address
*)NULL
, pluto_port
);
1795 for (; c
!= NULL
; c
= c
->hp_next
)
1798 DBG_log("checking %s", c
->name
));
1799 if (c
->kind
== CK_GROUP
)
1804 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1806 if (routed(sr
->routing
)
1807 && addrinsubnet(our_client
, &sr
->this.client
)
1808 && addrinsubnet(peer_client
, &sr
->that
.client
))
1817 DBG_log("comparing best %s to %s"
1818 , best
->name
, c
->name
));
1820 for (bestsr
= &best
->spd
; best
!=c
&& bestsr
; bestsr
=bestsr
->next
)
1822 if (!subnetinsubnet(&bestsr
->this.client
, &sr
->this.client
)
1823 || (samesubnet(&bestsr
->this.client
, &sr
->this.client
)
1824 && !subnetinsubnet(&bestsr
->that
.client
1825 , &sr
->that
.client
)))
1835 if (best
== NULL
|| NEVER_NEGOTIATE(best
->policy
) ||
1836 (best
->policy
& POLICY_OPPO
) == LEMPTY
|| best
->kind
!= CK_TEMPLATE
)
1842 chunk_t encoding
= gw
->gw_id
->get_encoding(gw
->gw_id
);
1843 id_type_t type
= gw
->gw_id
->get_type(gw
->gw_id
);
1846 initaddr(encoding
.ptr
, encoding
.len
,
1847 (type
== ID_IPV4_ADDR
) ? AF_INET
: AF_INET6
, &ip_addr
);
1849 return oppo_instantiate(best
, &ip_addr
, NULL
, gw
, our_client
, peer_client
);
1853 bool orient(connection_t
*c
)
1855 struct spd_route
*sr
;
1861 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
1863 /* Note: this loop does not stop when it finds a match:
1864 * it continues checking to catch any ambiguity.
1866 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1875 /* check if this interface matches this end */
1876 if (sameaddr(&sr
->this.host_addr
, &p
->addr
)
1877 && (!no_klips
|| sr
->this.host_port
== pluto_port
))
1881 if (c
->interface
== p
)
1882 loglog(RC_LOG_SERIOUS
1883 , "both sides of \"%s\" are our interface %s!"
1884 , c
->name
, p
->rname
);
1886 loglog(RC_LOG_SERIOUS
, "two interfaces match \"%s\" (%s, %s)"
1887 , c
->name
, c
->interface
->rname
, p
->rname
);
1888 c
->interface
= NULL
; /* withdraw orientation */
1894 /* done with this interface if it doesn't match that end */
1895 if (!(sameaddr(&sr
->that
.host_addr
, &p
->addr
)
1896 && (!no_klips
|| sr
->that
.host_port
== pluto_port
)))
1899 /* swap ends and try again.
1900 * It is a little tricky to see that this loop will stop.
1901 * Only continue if the far side matches.
1902 * If both sides match, there is an error-out.
1905 struct end t
= sr
->this;
1907 sr
->this = sr
->that
;
1914 return oriented(*c
);
1917 void initiate_connection(const char *name
, int whackfd
)
1919 connection_t
*c
= con_by_name(name
, TRUE
);
1923 set_cur_connection(c
);
1926 loglog(RC_ORIENT
, "we have no ipsecN interface for either end of this connection");
1928 else if (NEVER_NEGOTIATE(c
->policy
))
1931 , "cannot initiate an authby=never connection");
1933 else if (c
->kind
!= CK_PERMANENT
&& !c
->spd
.that
.allow_any
)
1935 if (isanyaddr(&c
->spd
.that
.host_addr
))
1936 loglog(RC_NOPEERIP
, "cannot initiate connection without knowing peer IP address");
1938 loglog(RC_WILDCARD
, "cannot initiate connection with ID wildcards");
1942 /* do we have to prompt for a PIN code? */
1943 if (c
->spd
.this.sc
&& !c
->spd
.this.sc
->valid
&& whackfd
!= NULL_FD
)
1945 scx_get_pin(c
->spd
.this.sc
, whackfd
);
1947 if (c
->spd
.this.sc
&& !c
->spd
.this.sc
->valid
)
1949 loglog(RC_NOVALIDPIN
, "cannot initiate connection without valid PIN");
1954 if (c
->spd
.that
.allow_any
)
1956 c
= instantiate(c
, &c
->spd
.that
.host_addr
,
1957 c
->spd
.that
.host_port
, c
->spd
.that
.id
);
1960 /* We will only request an IPsec SA if policy isn't empty
1961 * (ignoring Main Mode items).
1962 * This is a fudge, but not yet important.
1963 * If we are to proceed asynchronously, whackfd will be NULL_FD.
1965 c
->policy
|= POLICY_UP
;
1966 ipsecdoi_initiate(whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
1967 whackfd
= NULL_FD
; /* protect from close */
1970 reset_cur_connection();
1975 /* (Possibly) Opportunistic Initiation:
1976 * Knowing clients (single IP addresses), try to build an tunnel.
1977 * This may involve discovering a gateway and instantiating an
1978 * Opportunistic connection. Called when a packet is caught by
1979 * a %trap, or when whack --oppohere --oppothere is used.
1980 * It may turn out that an existing or non-opporunistic connnection
1981 * can handle the traffic.
1983 * Most of the code will be restarted if an ADNS request is made
1984 * to discover the gateway. The only difference between the first
1985 * and second entry is whether gateways_from_dns is NULL or not.
1986 * initiate_opportunistic: initial entrypoint
1987 * continue_oppo: where we pickup when ADNS result arrives
1988 * initiate_opportunistic_body: main body shared by above routines
1989 * cannot_oppo: a helper function to log a diagnostic
1990 * This structure repeats a lot of code when the ADNS result arrives.
1991 * This seems like a waste, but anything learned the first time through
1992 * may no longer be true!
1994 * After the first IKE message is sent, the regular state machinery
1995 * carries negotiation forward.
1998 enum find_oppo_step
{
2001 fos_myid_hostname_txt
,
2003 fos_myid_hostname_key
,
2008 #endif /* USE_KEYRR */
2014 static const char *const oppo_step_name
[] = {
2017 "fos_myid_hostname_txt",
2019 "fos_myid_hostname_key",
2024 #endif /* USE_KEYRR */
2030 struct find_oppo_bundle
{
2031 enum find_oppo_step step
;
2033 bool failure_ok
; /* if true, continue_oppo should not die on DNS failure */
2034 ip_address our_client
; /* not pointer! */
2035 ip_address peer_client
;
2036 int transport_proto
;
2038 policy_prio_t policy_prio
;
2039 ipsec_spi_t failure_shunt
; /* in host order! 0 for delete. */
2043 struct find_oppo_continuation
{
2044 struct adns_continuation ac
; /* common prefix */
2045 struct find_oppo_bundle b
;
2048 static void cannot_oppo(connection_t
*c
, struct find_oppo_bundle
*b
, err_t ugh
)
2050 char pcb
[ADDRTOT_BUF
];
2051 char ocb
[ADDRTOT_BUF
];
2053 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2054 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2056 DBG(DBG_DNS
| DBG_OPPO
, DBG_log("Can't Opportunistically initiate for %s to %s: %s"
2059 whack_log(RC_OPPOFAILURE
2060 , "Can't Opportunistically initiate for %s to %s: %s"
2063 if (c
&& c
->policy_next
)
2065 /* there is some policy that comes afterwards */
2066 struct spd_route
*shunt_spd
;
2067 connection_t
*nc
= c
->policy_next
;
2070 passert(c
->kind
== CK_TEMPLATE
);
2071 passert(c
->policy_next
->kind
== CK_PERMANENT
);
2073 DBG(DBG_OPPO
, DBG_log("OE failed for %s to %s, but %s overrides shunt"
2074 , ocb
, pcb
, c
->policy_next
->name
));
2077 * okay, here we need add to the "next" policy, which is ought
2078 * to be an instance.
2079 * We will add another entry to the spd_route list for the specific
2080 * situation that we have.
2083 shunt_spd
= clone_thing(nc
->spd
);
2085 shunt_spd
->next
= nc
->spd
.next
;
2086 nc
->spd
.next
= shunt_spd
;
2088 happy(addrtosubnet(&b
->peer_client
, &shunt_spd
->that
.client
));
2090 if (sameaddr(&b
->peer_client
, &shunt_spd
->that
.host_addr
))
2091 shunt_spd
->that
.has_client
= FALSE
;
2094 * override the tunnel destination with the one from the secondaried
2097 shunt_spd
->that
.host_addr
= nc
->spd
.that
.host_addr
;
2099 /* now, lookup the state, and poke it up.
2102 st
= state_with_serialno(nc
->newest_ipsec_sa
);
2104 /* XXX what to do if the IPSEC SA has died? */
2105 passert(st
!= NULL
);
2107 /* link the new connection instance to the state's list of
2111 DBG(DBG_OPPO
, DBG_log("installing state: %ld for %s to %s"
2112 , nc
->newest_ipsec_sa
2116 if (DBGP(DBG_OPPO
| DBG_CONTROLMORE
))
2118 char state_buf
[LOG_WIDTH
];
2119 char state_buf2
[LOG_WIDTH
];
2122 fmt_state(FALSE
, st
, n
2123 , state_buf
, sizeof(state_buf
)
2124 , state_buf2
, sizeof(state_buf2
));
2125 DBG_log("cannot_oppo, failure SA1: %s", state_buf
);
2126 DBG_log("cannot_oppo, failure SA2: %s", state_buf2
);
2130 if (!route_and_eroute(c
, shunt_spd
, st
))
2132 whack_log(RC_OPPOFAILURE
2133 , "failed to instantiate shunt policy %s for %s to %s"
2143 /* Replace HOLD with b->failure_shunt.
2144 * If no b->failure_shunt specified, use SPI_PASS -- THIS MAY CHANGE.
2146 if (b
->failure_shunt
== 0)
2148 DBG(DBG_OPPO
, DBG_log("no explicit failure shunt for %s to %s; installing %%pass"
2152 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2155 , b
->failure_shunt
!= 0
2156 , b
->transport_proto
2162 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
2163 , struct adns_continuation
*ac
, err_t ac_ugh
); /* forward */
2165 void initiate_opportunistic(const ip_address
*our_client
,
2166 const ip_address
*peer_client
, int transport_proto
,
2167 bool held
, int whackfd
)
2169 struct find_oppo_bundle b
;
2171 b
.want
= (whackfd
== NULL_FD ?
"whack" : "acquire");
2172 b
.failure_ok
= FALSE
;
2173 b
.our_client
= *our_client
;
2174 b
.peer_client
= *peer_client
;
2175 b
.transport_proto
= transport_proto
;
2177 b
.policy_prio
= BOTTOM_PRIO
;
2178 b
.failure_shunt
= 0;
2179 b
.whackfd
= whackfd
;
2181 initiate_opportunistic_body(&b
, NULL
, NULL
);
2184 static void continue_oppo(struct adns_continuation
*acr
, err_t ugh
)
2186 struct find_oppo_continuation
*cr
= (void *)acr
; /* inherit, damn you! */
2188 bool was_held
= cr
->b
.held
;
2189 int whackfd
= cr
->b
.whackfd
;
2191 /* note: cr->id has no resources; cr->sgw_id is ID_ANY:
2192 * neither need freeing.
2194 whack_log_fd
= whackfd
;
2197 /* Discover and record whether %hold has gone away.
2198 * This could have happened while we were awaiting DNS.
2199 * We must check BEFORE any call to cannot_oppo.
2202 cr
->b
.held
= has_bare_hold(&cr
->b
.our_client
, &cr
->b
.peer_client
2203 , cr
->b
.transport_proto
);
2207 /* if we're going to ignore the error, at least note it in debugging log */
2208 if (cr
->b
.failure_ok
&& ugh
)
2210 DBG(DBG_CONTROL
| DBG_DNS
,
2212 char ocb
[ADDRTOT_BUF
];
2213 char pcb
[ADDRTOT_BUF
];
2215 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2216 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2217 DBG_log("continuing from failed DNS lookup for %s, %s to %s: %s"
2218 , cr
->b
.want
, ocb
, pcb
, ugh
);
2223 if (!cr
->b
.failure_ok
&& ugh
)
2225 c
= find_connection_for_clients(NULL
, &cr
->b
.our_client
, &cr
->b
.peer_client
2226 , cr
->b
.transport_proto
);
2227 cannot_oppo(c
, &cr
->b
2228 , builddiag("%s: %s", cr
->b
.want
, ugh
));
2230 else if (was_held
&& !cr
->b
.held
)
2232 /* was_held indicates we were started due to a %trap firing
2233 * (as opposed to a "whack --oppohere --oppothere").
2234 * Since the %hold has gone, we can assume that somebody else
2235 * has beaten us to the punch. We can go home. But lets log it.
2237 char ocb
[ADDRTOT_BUF
];
2238 char pcb
[ADDRTOT_BUF
];
2240 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2241 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2244 , "%%hold otherwise handled during DNS lookup for Opportunistic Initiation for %s to %s"
2249 initiate_opportunistic_body(&cr
->b
, &cr
->ac
, ugh
);
2250 whackfd
= NULL_FD
; /* was handed off */
2253 whack_log_fd
= NULL_FD
;
2258 static err_t
check_key_recs(enum myid_state try_state
, const connection_t
*c
,
2259 struct adns_continuation
*ac
)
2261 /* Check if KEY lookup yielded good results.
2262 * Looking up based on our ID. Used if
2263 * client is ourself, or if TXT had no public key.
2264 * Note: if c is different this time, there is
2265 * a chance that we did the wrong query.
2266 * If so, treat as a kind of failure.
2268 enum myid_state old_myid_state
= myid_state
;
2269 private_key_t
*private;
2272 myid_state
= try_state
;
2274 if (old_myid_state
!= myid_state
&& old_myid_state
== MYID_SPECIFIED
)
2276 ugh
= "%myid was specified while we were guessing";
2278 else if ((private = get_private_key(c
)) == NULL
)
2280 ugh
= "we don't know our own RSA key";
2282 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2284 ugh
= "our ID changed underfoot";
2288 /* Similar to code in RSA_check_signature
2289 * for checking the other side.
2293 ugh
= "no KEY RR found for us";
2294 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2296 ugh
= "all our KEY RRs have the wrong public key";
2297 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2298 && private->belongs_to(private, &kr
->key
->public_key
))
2300 ugh
= NULL
; /* good! */
2307 myid_state
= old_myid_state
;
2311 #endif /* USE_KEYRR */
2313 static err_t
check_txt_recs(enum myid_state try_state
, const connection_t
*c
,
2314 struct adns_continuation
*ac
)
2316 /* Check if TXT lookup yielded good results.
2317 * Looking up based on our ID. Used if
2318 * client is ourself, or if TXT had no public key.
2319 * Note: if c is different this time, there is
2320 * a chance that we did the wrong query.
2321 * If so, treat as a kind of failure.
2323 enum myid_state old_myid_state
= myid_state
;
2324 private_key_t
*private;
2327 myid_state
= try_state
;
2329 if (old_myid_state
!= myid_state
2330 && old_myid_state
== MYID_SPECIFIED
)
2332 ugh
= "%myid was specified while we were guessing";
2334 else if ((private = get_private_key(c
)) == NULL
)
2336 ugh
= "we don't know our own RSA key";
2338 else if (!ac
->id
->equals(ac
->id
, c
->spd
.this.id
))
2340 ugh
= "our ID changed underfoot";
2344 /* Similar to code in RSA_check_signature
2345 * for checking the other side.
2347 struct gw_info
*gwp
;
2349 ugh
= "no TXT RR found for us";
2350 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2352 public_key_t
*pub_key
= gwp
->key
->public_key
;
2354 ugh
= "all our TXT RRs have the wrong public key";
2355 if (pub_key
->get_type(pub_key
) == KEY_RSA
&&
2356 private->belongs_to(private, pub_key
))
2358 ugh
= NULL
; /* good! */
2365 myid_state
= old_myid_state
;
2371 /* note: gateways_from_dns must be NULL iff this is the first call */
2372 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
,
2373 struct adns_continuation
*ac
,
2377 struct spd_route
*sr
;
2379 /* What connection shall we use?
2380 * First try for one that explicitly handles the clients.
2384 char ours
[ADDRTOT_BUF
];
2385 char his
[ADDRTOT_BUF
];
2389 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2390 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2391 ourport
= ntohs(portof(&b
->our_client
));
2392 hisport
= ntohs(portof(&b
->peer_client
));
2393 DBG_log("initiate on demand from %s:%d to %s:%d proto=%d state: %s because: %s"
2394 , ours
, ourport
, his
, hisport
, b
->transport_proto
2395 , oppo_step_name
[b
->step
], b
->want
);
2397 if (isanyaddr(&b
->our_client
) || isanyaddr(&b
->peer_client
))
2399 cannot_oppo(NULL
, b
, "impossible IP address");
2401 else if ((c
= find_connection_for_clients(&sr
2404 , b
->transport_proto
)) == NULL
)
2406 /* No connection explicitly handles the clients and there
2407 * are no Opportunistic connections -- whine and give up.
2408 * The failure policy cannot be gotten from a connection; we pick %pass.
2410 cannot_oppo(NULL
, b
, "no routed Opportunistic template covers this pair");
2412 else if (c
->kind
!= CK_TEMPLATE
)
2414 /* We've found a connection that can serve.
2415 * Do we have to initiate it?
2416 * Not if there is currently an IPSEC SA.
2417 * But if there is an IPSEC SA, then KLIPS would not
2418 * have generated the acquire. So we assume that there isn't one.
2419 * This may be redundant if a non-opportunistic
2420 * negotiation is already being attempted.
2423 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2425 if(c
->kind
== CK_INSTANCE
)
2427 char cib
[CONN_INST_BUF
];
2428 /* there is already an instance being negotiated, no nothing */
2429 DBG(DBG_CONTROL
, DBG_log("found existing instance \"%s\"%s, rekeying it"
2431 , (fmt_conn_instance(c
, cib
), cib
)));
2432 /* XXX-mcr - return; */
2435 /* otherwise, there is some kind of static conn that can handle
2436 * this connection, so we initiate it */
2441 /* what should we do on failure? */
2442 (void) assign_hold(c
, sr
, b
->transport_proto
, &b
->our_client
, &b
->peer_client
);
2445 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2446 b
->whackfd
= NULL_FD
; /* protect from close */
2450 /* We are handling an opportunistic situation.
2451 * This involves several DNS lookup steps that require suspension.
2452 * Note: many facts might change while we're suspended.
2455 * The first chunk of code handles the result of the previous
2456 * DNS query (if any). It also selects the kind of the next step.
2457 * The second chunk initiates the next DNS query (if any).
2459 enum find_oppo_step next_step
= fos_myid_ip_txt
;
2461 char mycredentialstr
[BUF_LEN
];
2462 char cib
[CONN_INST_BUF
];
2464 DBG(DBG_CONTROL
, DBG_log("creating new instance from \"%s\"%s",
2465 c
->name
, (fmt_conn_instance(c
, cib
), cib
)));
2466 snprintf(mycredentialstr
, BUF_LEN
, "%Y", sr
->this.id
);
2468 /* handle any DNS answer; select next step */
2472 /* just starting out: select first query step */
2473 next_step
= fos_myid_ip_txt
;
2476 case fos_myid_ip_txt
: /* TXT for our default IP address as %myid */
2477 ugh
= check_txt_recs(MYID_IP
, c
, ac
);
2480 /* cannot use our IP as OE identitiy for initiation */
2482 DBG_log("can not use our IP (%Y:TXT) as identity: %s",
2483 myids
[MYID_IP
], ugh
));
2484 if (!logged_myid_ip_txt_warning
)
2486 loglog(RC_LOG_SERIOUS
,
2487 "can not use our IP (%Y:TXT) as identity: %s",
2488 myids
[MYID_IP
], ugh
);
2489 logged_myid_ip_txt_warning
= TRUE
;
2492 next_step
= fos_myid_hostname_txt
;
2493 ugh
= NULL
; /* failure can be recovered from */
2497 /* we can use our IP as OE identity for initiation */
2498 if (!logged_myid_ip_txt_warning
)
2500 loglog(RC_LOG_SERIOUS
,
2501 "using our IP (%Y:TXT) as identity!",
2503 logged_myid_ip_txt_warning
= TRUE
;
2506 next_step
= fos_our_client
;
2510 case fos_myid_hostname_txt
: /* TXT for our hostname as %myid */
2511 ugh
= check_txt_recs(MYID_HOSTNAME
, c
, ac
);
2514 /* cannot use our hostname as OE identitiy for initiation */
2516 DBG_log("can not use our hostname (%Y:TXT) as identity: %s",
2517 myids
[MYID_HOSTNAME
], ugh
));
2518 if (!logged_myid_fqdn_txt_warning
)
2520 loglog(RC_LOG_SERIOUS
,
2521 "can not use our hostname (%Y:TXT) as identity: %s",
2522 myids
[MYID_HOSTNAME
], ugh
);
2523 logged_myid_fqdn_txt_warning
= TRUE
;
2526 next_step
= fos_myid_ip_key
;
2527 ugh
= NULL
; /* failure can be recovered from */
2532 /* we can use our hostname as OE identity for initiation */
2533 if (!logged_myid_fqdn_txt_warning
)
2535 loglog(RC_LOG_SERIOUS
,
2536 "using our hostname (%Y:TXT) as identity!",
2537 myids
[MYID_HOSTNAME
]);
2538 logged_myid_fqdn_txt_warning
= TRUE
;
2540 next_step
= fos_our_client
;
2545 case fos_myid_ip_key
: /* KEY for our default IP address as %myid */
2546 ugh
= check_key_recs(MYID_IP
, c
, ac
);
2549 /* cannot use our IP as OE identitiy for initiation */
2551 DBG_log("can not use our IP (%Y:KEY) as identity: %s",
2552 myids
[MYID_IP
], ugh
));
2553 if (!logged_myid_ip_key_warning
)
2555 loglog(RC_LOG_SERIOUS
,
2556 "can not use our IP (%Y:KEY) as identity: %s",
2557 myids
[MYID_IP
], ugh
);
2558 logged_myid_ip_key_warning
= TRUE
;
2561 next_step
= fos_myid_hostname_key
;
2562 ugh
= NULL
; /* failure can be recovered from */
2566 /* we can use our IP as OE identity for initiation */
2567 if (!logged_myid_ip_key_warning
)
2569 loglog(RC_LOG_SERIOUS
,
2570 "using our IP (%Y:KEY) as identity!",
2572 logged_myid_ip_key_warning
= TRUE
;
2574 next_step
= fos_our_client
;
2578 case fos_myid_hostname_key
: /* KEY for our hostname as %myid */
2579 ugh
= check_key_recs(MYID_HOSTNAME
, c
, ac
);
2582 /* cannot use our IP as OE identitiy for initiation */
2584 DBG_log("can not use our hostname (%Y:KEY) as identity: %s",
2585 myids
[MYID_HOSTNAME
], ugh
));
2586 if (!logged_myid_fqdn_key_warning
)
2588 loglog(RC_LOG_SERIOUS
,
2589 "can not use our hostname (%Y:KEY) as identity: %s",
2590 myids
[MYID_HOSTNAME
], ugh
);
2591 logged_myid_fqdn_key_warning
= TRUE
;
2593 next_step
= fos_myid_hostname_key
;
2594 ugh
= NULL
; /* failure can be recovered from */
2598 /* we can use our IP as OE identity for initiation */
2599 if (!logged_myid_fqdn_key_warning
)
2601 loglog(RC_LOG_SERIOUS
,
2602 "using our hostname (%Y:KEY) as identity!",
2603 myids
[MYID_HOSTNAME
]);
2604 logged_myid_fqdn_key_warning
= TRUE
;
2606 next_step
= fos_our_client
;
2611 case fos_our_client
: /* TXT for our client */
2613 /* Our client is not us: we must check the TXT records.
2614 * Note: if c is different this time, there is
2615 * a chance that we did the wrong query.
2616 * If so, treat as a kind of failure.
2618 private_key_t
*private = get_private_key(c
);
2620 next_step
= fos_his_client
; /* normal situation */
2622 if (private == NULL
)
2624 ugh
= "we don't know our own RSA key";
2626 else if (sameaddr(&sr
->this.host_addr
, &b
->our_client
))
2628 /* this wasn't true when we started -- bail */
2629 ugh
= "our IP address changed underfoot";
2631 else if (!ac
->sgw_id
->equals(ac
->sgw_id
, sr
->this.id
))
2633 /* this wasn't true when we started -- bail */
2634 ugh
= "our ID changed underfoot";
2638 /* Similar to code in quick_inI1_outR1_tail
2639 * for checking the other side.
2641 struct gw_info
*gwp
;
2643 ugh
= "no TXT RR for our client delegates us";
2644 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2646 ugh
= "TXT RR for our client has wrong key";
2647 /* If there is a key from the TXT record,
2648 * we count it as a win if we match the key.
2649 * If there was no key, we have a tentative win:
2650 * we need to check our KEY record to be sure.
2652 if (!gwp
->gw_key_present
)
2654 /* Success, but the TXT had no key
2655 * so we must check our our own KEY records.
2657 next_step
= fos_our_txt
;
2658 ugh
= NULL
; /* good! */
2661 if (private->belongs_to(private, gwp
->key
->public_key
))
2663 ugh
= NULL
; /* good! */
2671 case fos_our_txt
: /* TXT for us */
2673 /* Check if TXT lookup yielded good results.
2674 * Looking up based on our ID. Used if
2675 * client is ourself, or if TXT had no public key.
2676 * Note: if c is different this time, there is
2677 * a chance that we did the wrong query.
2678 * If so, treat as a kind of failure.
2680 private_key_t
*private = get_private_key(c
);
2682 next_step
= fos_his_client
; /* unless we decide to look for KEY RR */
2684 if (private == NULL
)
2686 ugh
= "we don't know our own RSA key";
2688 else if (!ac
->id
->equals(ac
->id
, c
->spd
.this.id
))
2690 ugh
= "our ID changed underfoot";
2694 /* Similar to code in RSA_check_signature
2695 * for checking the other side.
2697 struct gw_info
*gwp
;
2699 ugh
= "no TXT RR for us";
2700 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2702 ugh
= "TXT RR for us has wrong key";
2703 if (gwp
->gw_key_present
&&
2704 private->belongs_to(private, gwp
->key
->public_key
))
2707 DBG_log("initiate on demand found TXT with right public key at: %s"
2708 , mycredentialstr
));
2716 /* if no TXT with right key, try KEY */
2718 DBG_log("will try for KEY RR since initiate on demand found %s: %s"
2719 , ugh
, mycredentialstr
));
2720 next_step
= fos_our_key
;
2729 case fos_our_key
: /* KEY for us */
2731 /* Check if KEY lookup yielded good results.
2732 * Looking up based on our ID. Used if
2733 * client is ourself, or if TXT had no public key.
2734 * Note: if c is different this time, there is
2735 * a chance that we did the wrong query.
2736 * If so, treat as a kind of failure.
2738 private_key_t
*private = get_private_key(c
);
2740 next_step
= fos_his_client
; /* always */
2742 if (private == NULL
)
2744 ugh
= "we don't know our own RSA key";
2746 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2748 ugh
= "our ID changed underfoot";
2752 /* Similar to code in RSA_check_signature
2753 * for checking the other side.
2757 ugh
= "no KEY RR found for us (and no good TXT RR)";
2758 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2760 ugh
= "all our KEY RRs have the wrong public key (and no good TXT RR)";
2761 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2762 && private->belongs_to(private, kr
->key
->public_key
))
2764 /* do this only once a day */
2765 if (!logged_txt_warning
)
2767 loglog(RC_LOG_SERIOUS
2768 , "found KEY RR but not TXT RR for %s. See http://www.freeswan.org/err/txt-change.html."
2770 logged_txt_warning
= TRUE
;
2772 ugh
= NULL
; /* good! */
2779 #endif /* USE_KEYRR */
2781 case fos_his_client
: /* TXT for his client */
2783 /* We've finished last DNS queries: TXT for his client.
2784 * Using the information, try to instantiate a connection
2785 * and start negotiating.
2786 * We now know the peer. The chosing of "c" ignored this,
2787 * so we will disregard its current value.
2788 * !!! We need to randomize the entry in gw that we choose.
2790 next_step
= fos_done
; /* no more queries */
2792 c
= build_outgoing_opportunistic_connection(ac
->gateways_from_dns
2798 /* We cannot seem to instantiate a suitable connection:
2801 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
2803 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2804 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2805 loglog(RC_OPPOFAILURE
,
2806 "no suitable connection for opportunism "
2807 "between %s and %s with %Y as peer",
2808 ocb
, pcb
, ac
->gateways_from_dns
->gw_id
);
2813 /* Replace HOLD with PASS.
2814 * The type of replacement *ought* to be
2815 * specified by policy.
2817 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2819 , SPI_PASS
/* fail into PASS */
2820 , TRUE
, b
->transport_proto
2821 , "no suitable connection");
2827 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2828 passert(c
->kind
== CK_INSTANCE
);
2829 passert(c
->gw_info
!= NULL
);
2830 passert(HAS_IPSEC_POLICY(c
->policy
));
2831 passert(LHAS(LELEM(RT_UNROUTED
) | LELEM(RT_ROUTED_PROSPECTIVE
), c
->spd
.routing
));
2835 /* what should we do on failure? */
2836 (void) assign_hold(c
, &c
->spd
2837 , b
->transport_proto
2838 , &b
->our_client
, &b
->peer_client
);
2841 c
->gw_info
->key
->last_tried_time
= now();
2842 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2843 b
->whackfd
= NULL_FD
; /* protect from close */
2852 /* the second chunk: initiate the next DNS query (if any) */
2855 char ours
[ADDRTOT_BUF
];
2856 char his
[ADDRTOT_BUF
];
2858 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2859 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2860 DBG_log("initiate on demand from %s to %s new state: %s with ugh: %s"
2861 , ours
, his
, oppo_step_name
[b
->step
], ugh ? ugh
: "ok");
2866 b
->policy_prio
= c
->prio
;
2867 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2868 cannot_oppo(c
, b
, ugh
);
2870 else if (next_step
== fos_done
)
2876 /* set up the next query */
2877 struct find_oppo_continuation
*cr
= malloc_thing(struct find_oppo_continuation
);
2878 identification_t
*id
;
2880 b
->policy_prio
= c
->prio
;
2881 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2882 cr
->b
= *b
; /* copy; start hand off of whackfd */
2883 cr
->b
.failure_ok
= FALSE
;
2884 cr
->b
.step
= next_step
;
2887 ; sr
!=NULL
&& !sameaddr(&sr
->this.host_addr
, &b
->our_client
)
2894 /* If a %hold shunt has replaced the eroute for this template,
2898 && sr
->routing
== RT_ROUTED_PROSPECTIVE
&& eclipsable(sr
))
2900 sr
->routing
= RT_ROUTED_ECLIPSED
;
2904 /* Switch to issue next query.
2905 * A case may turn out to be unnecessary. If so, it falls
2906 * through to the next case.
2907 * Figuring out what %myid can stand for must be done before
2908 * our client credentials are looked up: we must know what
2909 * the client credentials may use to identify us.
2910 * On the other hand, our own credentials should be looked
2911 * up after our clients in case our credentials are not
2913 * XXX this is a wasted effort if we don't have credentials
2914 * BUT they are not needed.
2918 case fos_myid_ip_txt
:
2919 if (c
->spd
.this.id
->get_type(c
->spd
.this.id
) == ID_MYID
2920 && myid_state
!= MYID_SPECIFIED
)
2922 cr
->b
.failure_ok
= TRUE
;
2923 cr
->b
.want
= b
->want
= "TXT record for IP address as %myid";
2924 ugh
= start_adns_query(myids
[MYID_IP
], myids
[MYID_IP
],
2925 T_TXT
, continue_oppo
, &cr
->ac
);
2928 cr
->b
.step
= fos_myid_hostname_txt
;
2931 case fos_myid_hostname_txt
:
2932 if (c
->spd
.this.id
->get_type(c
->spd
.this.id
) == ID_MYID
2933 && myid_state
!= MYID_SPECIFIED
)
2936 cr
->b
.failure_ok
= TRUE
;
2938 cr
->b
.failure_ok
= FALSE
;
2940 cr
->b
.want
= b
->want
= "TXT record for hostname as %myid";
2941 ugh
= start_adns_query(myids
[MYID_HOSTNAME
],
2942 myids
[MYID_HOSTNAME
],
2943 T_TXT
, continue_oppo
, &cr
->ac
);
2948 cr
->b
.step
= fos_myid_ip_key
;
2951 case fos_myid_ip_key
:
2952 if (c
->spd
.this.id
.kind
== ID_MYID
2953 && myid_state
!= MYID_SPECIFIED
)
2955 cr
->b
.failure_ok
= TRUE
;
2956 cr
->b
.want
= b
->want
= "KEY record for IP address as %myid (no good TXT)";
2957 ugh
= start_adns_query(myids
[MYID_IP
], NULL
, /* security gateway meaningless */
2958 T_KEY
, continue_oppo
, &cr
->ac
);
2961 cr
->b
.step
= fos_myid_hostname_key
;
2964 case fos_myid_hostname_key
:
2965 if (c
->spd
.this.id
.kind
== ID_MYID
2966 && myid_state
!= MYID_SPECIFIED
)
2968 cr
->b
.failure_ok
= FALSE
; /* last attempt! */
2969 cr
->b
.want
= b
->want
= "KEY record for hostname as %myid (no good TXT)";
2970 ugh
= start_adns_query(myids
[MYID_HOSTNAME
], NULL
, /* security gateway meaningless */
2971 T_KEY
, continue_oppo
, &cr
->ac
);
2975 cr
->b
.step
= fos_our_client
;
2978 case fos_our_client
: /* TXT for our client */
2979 if (!sameaddr(&c
->spd
.this.host_addr
, &b
->our_client
))
2981 /* Check that at least one TXT(reverse(b->our_client)) is workable.
2982 * Note: {unshare|free}_id_content not needed for id: ephemeral.
2984 cr
->b
.want
= b
->want
= "our client's TXT record";
2985 id
= identification_create_from_sockaddr((sockaddr_t
*)&b
->our_client
);
2986 ugh
= start_adns_query(id
, c
->spd
.this.id
, /* we are the security gateway */
2987 T_TXT
, continue_oppo
, &cr
->ac
);
2991 cr
->b
.step
= fos_our_txt
;
2994 case fos_our_txt
: /* TXT for us */
2995 cr
->b
.failure_ok
= b
->failure_ok
= TRUE
;
2996 cr
->b
.want
= b
->want
= "our TXT record";
2997 ugh
= start_adns_query(sr
->this.id
, sr
->this.id
, /* we are the security gateway */
2998 T_TXT
, continue_oppo
, &cr
->ac
);
3002 case fos_our_key
: /* KEY for us */
3003 cr
->b
.want
= b
->want
= "our KEY record";
3004 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
3005 ugh
= start_adns_query(sr
->this.id
, NULL
, /* security gateway meaningless */
3006 T_KEY
, continue_oppo
, &cr
->ac
);
3008 #endif /* USE_KEYRR */
3010 case fos_his_client
: /* TXT for his client */
3011 /* note: {unshare|free}_id_content not needed for id: ephemeral */
3012 cr
->b
.want
= b
->want
= "target's TXT record";
3013 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
3014 id
= identification_create_from_sockaddr((sockaddr_t
*)&b
->peer_client
);
3015 ugh
= start_adns_query(id
, NULL
, /* security gateway unconstrained */
3016 T_TXT
, continue_oppo
, &cr
->ac
);
3021 bad_case(next_step
);
3025 b
->whackfd
= NULL_FD
; /* complete hand-off */
3027 cannot_oppo(c
, b
, ugh
);
3030 close_any(b
->whackfd
);
3033 void terminate_connection(const char *nm
)
3035 /* Loop because more than one may match (master and instances)
3036 * But at least one is required (enforced by con_by_name).
3038 connection_t
*c
= con_by_name(nm
, TRUE
);
3040 if (c
== NULL
|| !c
->ikev1
)
3045 connection_t
*n
= c
->ac_next
; /* grab this before c might disappear */
3047 if (streq(c
->name
, nm
)
3048 && c
->kind
>= CK_PERMANENT
3049 && !NEVER_NEGOTIATE(c
->policy
))
3051 set_cur_connection(c
);
3052 plog("terminating SAs using this connection");
3053 c
->policy
&= ~POLICY_UP
;
3054 flush_pending_by_connection(c
);
3055 delete_states_by_connection(c
, FALSE
);
3056 if (c
->kind
== CK_INSTANCE
)
3057 delete_connection(c
, FALSE
);
3058 reset_cur_connection();
3064 /* an ISAKMP SA has been established.
3065 * Note the serial number, and release any connections with
3066 * the same peer ID but different peer IP address.
3068 bool uniqueIDs
= FALSE
; /* --uniqueids? */
3070 void ISAKMP_SA_established(connection_t
*c
, so_serial_t serial
)
3072 c
->newest_isakmp_sa
= serial
;
3074 /* the connection is now oriented so that we are able to determine
3075 * whether we are a mode config server with a virtual IP to send.
3077 if (!c
->spd
.that
.host_srcip
->is_anyaddr(c
->spd
.that
.host_srcip
) &&
3078 !c
->spd
.that
.has_natip
)
3080 c
->spd
.that
.modecfg
= TRUE
;
3085 /* for all connections: if the same Phase 1 IDs are used
3086 * for a different IP address, unorient that connection.
3090 for (d
= connections
; d
!= NULL
; )
3092 connection_t
*next
= d
->ac_next
; /* might move underneath us */
3094 if (d
->kind
>= CK_PERMANENT
&&
3095 c
->spd
.this.id
->equals(c
->spd
.this.id
, d
->spd
.this.id
) &&
3096 c
->spd
.that
.id
->equals(c
->spd
.that
.id
, d
->spd
.that
.id
) &&
3097 !sameaddr(&c
->spd
.that
.host_addr
, &d
->spd
.that
.host_addr
))
3099 release_connection(d
, FALSE
);
3106 /* Find the connection to connection c's peer's client with the
3107 * largest value of .routing. All other things being equal,
3108 * preference is given to c. If none is routed, return NULL.
3110 * If erop is non-null, set *erop to a connection sha