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 !isanyaddr(&c
->spd
.that
.host_srcip
))
376 vip
= host_create_from_sockaddr((sockaddr_t
*)&c
->spd
.that
.host_srcip
);
377 hydra
->attributes
->release_address(hydra
->attributes
, c
->spd
.that
.pool
,
378 vip
, c
->spd
.that
.id
);
382 /* release requested attributes if any */
385 c
->requested
->destroy_function(c
->requested
,
386 (void*)modecfg_attribute_destroy
);
389 /* release other attributes if any */
392 while (c
->attributes
->remove_last(c
->attributes
, (void **)&ca
) == SUCCESS
)
394 hydra
->attributes
->release(hydra
->attributes
, ca
->handler
,
395 c
->spd
.that
.id
, ca
->type
, ca
->value
);
396 modecfg_attribute_destroy(ca
);
398 c
->attributes
->destroy(c
->attributes
);
401 if (c
->kind
!= CK_GOING_AWAY
)
403 whack_attr
->del_pool(whack_attr
, c
->name
);
406 /* free internal data */
408 cur_debugging
= old_cur_debugging
;
411 DESTROY_IF(c
->spd
.this.id
);
412 DESTROY_IF(c
->spd
.this.ca
);
413 DESTROY_IF(c
->spd
.this.groups
);
414 free(c
->spd
.this.updown
);
415 free(c
->spd
.this.pool
);
416 DESTROY_IF(c
->spd
.that
.id
);
417 DESTROY_IF(c
->spd
.that
.ca
);
418 DESTROY_IF(c
->spd
.that
.groups
);
419 free(c
->spd
.that
.updown
);
420 free(c
->spd
.that
.pool
);
423 c
->requested_ca
->destroy_offset(c
->requested_ca
,
424 offsetof(identification_t
, destroy
));
426 gw_delref(&c
->gw_info
);
428 lock_certs_and_keys("delete_connection");
429 cert_release(c
->spd
.this.cert
);
430 scx_release(c
->spd
.this.sc
);
431 cert_release(c
->spd
.that
.cert
);
432 scx_release(c
->spd
.that
.sc
);
433 unlock_certs_and_keys("delete_connection");
435 alg_info_delref((struct alg_info
**)&c
->alg_info_esp
);
436 alg_info_delref((struct alg_info
**)&c
->alg_info_ike
);
441 /* Delete connections with the specified name */
442 void delete_connections_by_name(const char *name
, bool strict
)
444 connection_t
*c
= con_by_name(name
, strict
);
446 for (; c
!= NULL
; c
= con_by_name(name
, FALSE
))
447 delete_connection(c
, FALSE
);
450 void delete_every_connection(void)
454 delete_connection(connections
, TRUE
);
458 void release_dead_interfaces(void)
460 struct host_pair
*hp
;
462 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
467 for (pp
= &hp
->connections
; (p
= *pp
) != NULL
; )
469 if (p
->interface
->change
== IFN_DELETE
)
471 /* this connection's interface is going away */
472 enum connection_kind k
= p
->kind
;
474 release_connection(p
, TRUE
);
476 if (k
<= CK_PERMANENT
)
478 /* The connection should have survived release:
479 * move it to the unoriented_connections list.
485 *pp
= p
->hp_next
; /* advance *pp */
487 p
->hp_next
= unoriented_connections
;
488 unoriented_connections
= p
;
492 /* The connection should have vanished,
493 * but the previous connection remains.
500 pp
= &p
->hp_next
; /* advance pp */
506 /* adjust orientations of connections to reflect newly added interfaces */
507 void check_orientations(void)
509 /* try to orient all the unoriented connections */
511 connection_t
*c
= unoriented_connections
;
513 unoriented_connections
= NULL
;
517 connection_t
*nxt
= c
->hp_next
;
520 connect_to_host_pair(c
);
525 /* Check that no oriented connection has become double-oriented.
526 * In other words, the far side must not match one of our new interfaces.
531 for (i
= interfaces
; i
!= NULL
; i
= i
->next
)
533 if (i
->change
== IFN_ADD
)
535 struct host_pair
*hp
;
537 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
539 if (sameaddr(&hp
->him
.addr
, &i
->addr
)
540 && (!no_klips
|| hp
->him
.port
== pluto_port
))
542 /* bad news: the whole chain of connections
543 * hanging off this host pair has both sides
544 * matching an interface.
545 * We'll get rid of them, using orient and
546 * connect_to_host_pair. But we'll be lazy
547 * and not ditch the host_pair itself (the
548 * cost of leaving it is slight and cannot
549 * be induced by a foe).
551 connection_t
*c
= hp
->connections
;
553 hp
->connections
= NULL
;
556 connection_t
*nxt
= c
->hp_next
;
560 connect_to_host_pair(c
);
570 static err_t
default_end(struct end
*e
, ip_address
*dflt_nexthop
)
573 int af
= addrtypeof(&e
->host_addr
);
575 if (af
!= AF_INET
&& af
!= AF_INET6
)
577 return "unknown address family in default_end";
580 /* default ID to IP (but only if not NO_IP -- WildCard) */
581 if (e
->id
->get_type(e
->id
) == ID_ANY
&& !isanyaddr(&e
->host_addr
))
583 e
->id
->destroy(e
->id
);
584 e
->id
= identification_create_from_sockaddr((sockaddr_t
*)&e
->host_addr
);
585 e
->has_id_wildcards
= FALSE
;
588 /* default nexthop to other side */
589 if (isanyaddr(&e
->host_nexthop
))
591 e
->host_nexthop
= *dflt_nexthop
;
594 /* default client to subnet containing only self
595 * XXX This may mean that the client's address family doesn't match
596 * tunnel_addr_family.
600 ugh
= addrtosubnet(&e
->host_addr
, &e
->client
);
605 /* Format the topology of a connection end, leaving out defaults.
606 * Largest left end looks like: client === host : port [ host_id ] --- hop
607 * Note: if that==NULL, skip nexthop
608 * Returns strlen of formated result (length excludes NUL at end).
610 size_t format_end(char *buf
, size_t buf_len
, const struct end
*this,
611 const struct end
*that
, bool is_left
, lset_t policy
)
613 char client
[BUF_LEN
];
614 const char *client_sep
= "";
615 char protoport
[sizeof(":255/65535")];
616 const char *host
= NULL
;
617 char host_space
[ADDRTOT_BUF
];
618 char host_port
[sizeof(":65535")];
619 char host_id
[BUF_LEN
+ 2];
620 char hop
[ADDRTOT_BUF
];
621 const char *hop_sep
= "";
622 const char *open_brackets
= "";
623 const char *close_brackets
= "";
625 if (isanyaddr(&this->host_addr
))
627 switch (policy
& (POLICY_GROUP
| POLICY_OPPO
))
633 host
= "%opportunistic";
635 case POLICY_GROUP
| POLICY_OPPO
:
636 host
= "%opportunisticgroup";
646 if (is_virtual_end(this) && isanyaddr(&this->host_addr
))
652 if (this->has_client
)
654 ip_address client_net
, client_mask
;
656 networkof(&this->client
, &client_net
);
657 maskof(&this->client
, &client_mask
);
660 /* {client_subnet_wildcard} */
661 if (this->has_client_wildcard
)
664 close_brackets
= "}";
667 if (isanyaddr(&client_net
) && isanyaddr(&client_mask
)
668 && (policy
& (POLICY_GROUP
| POLICY_OPPO
)))
670 client_sep
= ""; /* boring case */
672 else if (subnetisnone(&this->client
))
678 subnettot(&this->client
, 0, client
, sizeof(client
));
681 else if (this->modecfg
&& isanyaddr(&this->host_srcip
))
683 /* we are mode config client, or a server with a pool */
686 strcpy(client
+1, this->pool ?
this->pool
: "modecfg");
692 addrtot(&this->host_addr
, 0, host_space
, sizeof(host_space
));
697 if (this->host_port
!= IKE_UDP_PORT
)
699 snprintf(host_port
, sizeof(host_port
), ":%u", this->host_port
);
702 /* payload portocol and port */
704 if (this->has_port_wildcard
)
706 snprintf(protoport
, sizeof(protoport
), ":%u/%%any", this->protocol
);
708 else if (this->port
|| this->protocol
)
710 snprintf(protoport
, sizeof(protoport
), ":%u/%u", this->protocol
715 snprintf(host_id
, sizeof(host_id
), "[%Y]", this->id
);
720 if (that
&& !sameaddr(&this->host_nexthop
, &that
->host_addr
))
722 addrtot(&this->host_nexthop
, 0, hop
, sizeof(hop
));
728 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
729 , open_brackets
, client
, close_brackets
, client_sep
730 , this->allow_any?
"%":""
731 , host
, host_port
, host_id
, protoport
736 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
738 , this->allow_any?
"%":""
739 , host
, host_port
, host_id
, protoport
, client_sep
740 , open_brackets
, client
, close_brackets
);
745 /* format topology of a connection.
746 * Two symmetric ends separated by ...
748 #define CONNECTION_BUF (2 * (END_BUF - 1) + 4)
750 static size_t format_connection(char *buf
, size_t buf_len
,
751 const connection_t
*c
,
752 struct spd_route
*sr
)
754 size_t w
= format_end(buf
, buf_len
, &sr
->this, &sr
->that
, TRUE
, LEMPTY
);
756 w
+= snprintf(buf
+ w
, buf_len
- w
, "...");
757 return w
+ format_end(buf
+ w
, buf_len
- w
, &sr
->that
, &sr
->this, FALSE
, c
->policy
);
760 static void unshare_connection_strings(connection_t
*c
)
762 c
->name
= clone_str(c
->name
);
763 c
->spd
.this.id
= c
->spd
.this.id
->clone(c
->spd
.this.id
);
764 c
->spd
.this.pool
= clone_str(c
->spd
.this.pool
);
765 c
->spd
.this.updown
= clone_str(c
->spd
.this.updown
);
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 scx_share(c
->spd
.that
.sc
);
780 cert_share(c
->spd
.that
.cert
);
783 c
->spd
.that
.ca
= c
->spd
.that
.ca
->clone(c
->spd
.that
.ca
);
785 if (c
->spd
.that
.groups
)
787 c
->spd
.that
.groups
= c
->spd
.that
.groups
->get_ref(c
->spd
.that
.groups
);
790 /* increment references to algo's */
791 alg_info_addref((struct alg_info
*)c
->alg_info_esp
);
792 alg_info_addref((struct alg_info
*)c
->alg_info_ike
);
795 static void load_end_certificate(char *filename
, struct end
*dst
)
797 time_t notBefore
, notAfter
;
799 certificate_t
*certificate
;
800 bool cached_cert
= FALSE
;
802 /* initialize end certificate */
805 /* initialize smartcard info record */
810 if (scx_on_smartcard(filename
))
812 /* load cert from smartcard */
813 cert
= scx_load_cert(filename
, &dst
->sc
, &cached_cert
);
817 /* load cert from file */
818 cert
= load_host_cert(filename
);
824 certificate
= cert
->cert
;
826 if (dst
->id
->get_type(dst
->id
) == ID_ANY
||
827 !certificate
->has_subject(certificate
, dst
->id
))
829 plog( " id '%Y' not confirmed by certificate, defaulting to '%Y'",
830 dst
->id
, certificate
->get_subject(certificate
));
831 dst
->id
->destroy(dst
->id
);
832 dst
->id
= certificate
->get_subject(certificate
);
833 dst
->id
= dst
->id
->clone(dst
->id
);
842 if (!certificate
->get_validity(certificate
, NULL
, ¬Before
, ¬After
))
844 plog("certificate is invalid (valid from %T to %T)",
845 ¬Before
, FALSE
, ¬After
, FALSE
);
850 DBG_log("certificate is valid")
852 add_public_key_from_cert(cert
, notAfter
, DAL_LOCAL
);
853 dst
->cert
= cert_add(cert
);
855 certificate
= dst
->cert
->cert
;
857 /* if no CA is defined, use issuer as default */
858 if (dst
->ca
== NULL
&& certificate
->get_type(certificate
) == CERT_X509
)
860 identification_t
*issuer
;
862 issuer
= certificate
->get_issuer(certificate
);
863 dst
->ca
= issuer
->clone(issuer
);
866 /* cache the certificate that was last retrieved from the smartcard */
869 if (!certificate
->equals(certificate
, dst
->sc
->last_cert
->cert
))
871 lock_certs_and_keys("load_end_certificates");
872 cert_release(dst
->sc
->last_cert
);
873 dst
->sc
->last_cert
= dst
->cert
;
874 cert_share(dst
->cert
);
875 unlock_certs_and_keys("load_end_certificates");
877 time(&dst
->sc
->last_load
);
881 cert_share(dst
->cert
);
884 static bool extract_end(struct end
*dst
, const whack_end_t
*src
,
885 const char *name
, bool is_left
)
887 bool same_ca
= FALSE
;
889 dst
->is_left
= is_left
;
890 dst
->id
= identification_create_from_string(src
->id
);
893 /* decode CA distinguished name, if any */
896 if streq(src
->ca
, "%same")
900 else if (!streq(src
->ca
, "%any"))
902 dst
->ca
= identification_create_from_string(src
->ca
);
903 if (dst
->ca
->get_type(dst
->ca
) != ID_DER_ASN1_DN
)
905 plog("bad CA string '%s', ignored", src
->ca
);
906 dst
->ca
->destroy(dst
->ca
);
912 /* load local end certificate and extract ID, if any */
913 load_end_certificate(src
->cert
, dst
);
915 /* does id has wildcards? */
916 dst
->has_id_wildcards
= dst
->id
->contains_wildcards(dst
->id
);
918 /* decode group attributes, if any */
921 dst
->groups
= ietf_attributes_create_from_string(src
->groups
);
924 /* the rest is simple copying of corresponding fields */
925 dst
->host_addr
= src
->host_addr
;
926 dst
->host_nexthop
= src
->host_nexthop
;
927 dst
->host_srcip
= src
->host_srcip
;
928 dst
->has_natip
= src
->has_natip
;
929 dst
->client
= src
->client
;
930 dst
->protocol
= src
->protocol
;
931 dst
->port
= src
->port
;
932 dst
->has_port_wildcard
= src
->has_port_wildcard
;
933 dst
->key_from_DNS_on_demand
= src
->key_from_DNS_on_demand
;
934 dst
->has_client
= src
->has_client
;
935 dst
->has_client_wildcard
= src
->has_client_wildcard
;
936 dst
->modecfg
= src
->modecfg
;
937 dst
->hostaccess
= src
->hostaccess
;
938 dst
->allow_any
= src
->allow_any
;
939 dst
->sendcert
= src
->sendcert
;
940 dst
->updown
= clone_str(src
->updown
);
941 dst
->host_port
= src
->host_port
;
943 /* if the sourceip netmask is zero a named pool exists */
944 if (src
->sourceip_mask
== 0)
946 dst
->pool
= clone_str(src
->sourceip
);
949 /* if host sourceip is defined but no client is present
950 * behind the host then set client to sourceip/32
952 if (addrbytesptr(&dst
->host_srcip
, NULL
) &&
953 !isanyaddr(&dst
->host_srcip
) && !dst
->has_natip
&& !dst
->has_client
)
955 err_t ugh
= addrtosubnet(&dst
->host_srcip
, &dst
->client
);
959 plog("could not assign host sourceip to client subnet");
963 dst
->has_client
= TRUE
;
969 static bool check_connection_end(const whack_end_t
*this,
970 const whack_end_t
*that
,
971 const whack_message_t
*wm
)
973 if (wm
->addr_family
!= addrtypeof(&this->host_addr
)
974 || wm
->addr_family
!= addrtypeof(&this->host_nexthop
)
975 || (this->has_client? wm
->tunnel_addr_family
: wm
->addr_family
)
976 != subnettypeof(&this->client
)
977 || subnettypeof(&this->client
) != subnettypeof(&that
->client
))
979 /* this should have been diagnosed by whack, so we need not be clear
980 * !!! overloaded use of RC_CLASH
982 loglog(RC_CLASH
, "address family inconsistency in connection");
986 if (isanyaddr(&that
->host_addr
))
988 /* other side is wildcard: we must check if other conditions met */
989 if (isanyaddr(&this->host_addr
))
991 loglog(RC_ORIENT
, "connection must specify host IP address for our side");
996 if (this->virt
&& (!isanyaddr(&this->host_addr
) || this->has_client
))
999 "virtual IP must only be used with %%any and without client");
1003 return TRUE
; /* happy */
1006 connection_t
*find_connection_by_reqid(uint32_t reqid
)
1011 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
1013 if (c
->spd
.reqid
== reqid
)
1022 static uint32_t gen_reqid(void)
1025 static uint32_t reqid
= IPSEC_MANUAL_REQID_MAX
& ~3;
1032 reqid
= (IPSEC_MANUAL_REQID_MAX
& ~3) + 4;
1034 if (!find_connection_by_reqid(reqid
))
1038 } while (reqid
!= start
);
1040 exit_log("unable to allocate reqid");
1041 return 0; /* never reached ... */
1044 void add_connection(const whack_message_t
*wm
)
1046 if (con_by_name(wm
->name
, FALSE
) != NULL
)
1048 loglog(RC_DUPNAME
, "attempt to redefine connection \"%s\"", wm
->name
);
1050 else if (wm
->right
.protocol
!= wm
->left
.protocol
)
1052 /* this should haven been diagnosed by whack
1053 * !!! overloaded use of RC_CLASH
1055 loglog(RC_CLASH
, "the protocol must be the same for leftport and rightport");
1057 else if (check_connection_end(&wm
->right
, &wm
->left
, wm
)
1058 && check_connection_end(&wm
->left
, &wm
->right
, wm
))
1060 bool same_rightca
, same_leftca
;
1061 connection_t
*c
= malloc_thing(connection_t
);
1064 c
->name
= clone_str(wm
->name
);
1065 c
->ikev1
= wm
->ikev1
;
1066 c
->policy
= wm
->policy
;
1068 if ((c
->policy
& POLICY_COMPRESS
) && !can_do_IPcomp
)
1071 , "ignoring --compress in \"%s\" because KLIPS is not configured to do IPCOMP"
1078 DBG_log("from whack: got --esp=%s", wm
->esp ? wm
->esp
: "NULL")
1080 c
->alg_info_esp
= alg_info_esp_create_from_str(wm
->esp? wm
->esp
: "");
1082 DBG(DBG_CRYPT
|DBG_CONTROL
,
1083 static char buf
[BUF_LEN
]="<NULL>";
1085 if (c
->alg_info_esp
)
1087 alg_info_snprint(buf
, sizeof(buf
)
1088 ,(struct alg_info
*)c
->alg_info_esp
);
1090 DBG_log("esp proposal: %s", buf
);
1092 if (c
->alg_info_esp
)
1094 if (c
->alg_info_esp
->alg_info_cnt
== 0)
1096 loglog(RC_LOG_SERIOUS
, "got 0 esp transforms");
1101 loglog(RC_LOG_SERIOUS
, "syntax error in esp string");
1108 DBG_log("from whack: got --ike=%s", wm
->ike ? wm
->ike
: "NULL")
1110 c
->alg_info_ike
= alg_info_ike_create_from_str(wm
->ike? wm
->ike
: "");
1112 DBG(DBG_CRYPT
|DBG_CONTROL
,
1113 static char buf
[BUF_LEN
]="<NULL>";
1115 if (c
->alg_info_ike
)
1117 alg_info_snprint(buf
, sizeof(buf
)
1118 , (struct alg_info
*)c
->alg_info_ike
);
1120 DBG_log("ike proposal: %s", buf
);
1122 if (c
->alg_info_ike
)
1124 if (c
->alg_info_ike
->alg_info_cnt
== 0)
1126 loglog(RC_LOG_SERIOUS
, "got 0 ike transforms");
1131 loglog(RC_LOG_SERIOUS
, "syntax error in ike string");
1135 c
->sa_ike_life_seconds
= wm
->sa_ike_life_seconds
;
1136 c
->sa_ipsec_life_seconds
= wm
->sa_ipsec_life_seconds
;
1137 c
->sa_rekey_margin
= wm
->sa_rekey_margin
;
1138 c
->sa_rekey_fuzz
= wm
->sa_rekey_fuzz
;
1139 c
->sa_keying_tries
= wm
->sa_keying_tries
;
1142 c
->dpd_delay
= wm
->dpd_delay
;
1143 c
->dpd_timeout
= wm
->dpd_timeout
;
1144 c
->dpd_action
= wm
->dpd_action
;
1146 c
->addr_family
= wm
->addr_family
;
1147 c
->tunnel_addr_family
= wm
->tunnel_addr_family
;
1149 c
->requested_ca
= NULL
;
1150 same_leftca
= extract_end(&c
->spd
.this, &wm
->left
, wm
->name
, TRUE
);
1151 same_rightca
= extract_end(&c
->spd
.that
, &wm
->right
, wm
->name
, FALSE
);
1153 if (same_rightca
&& c
->spd
.this.ca
)
1155 c
->spd
.that
.ca
= c
->spd
.this.ca
->clone(c
->spd
.this.ca
);
1157 else if (same_leftca
&& c
->spd
.that
.ca
)
1159 c
->spd
.this.ca
= c
->spd
.that
.ca
->clone(c
->spd
.that
.ca
);
1162 default_end(&c
->spd
.this, &c
->spd
.that
.host_addr
);
1163 default_end(&c
->spd
.that
, &c
->spd
.this.host_addr
);
1165 /* force any wildcard host IP address, any wildcard subnet
1166 * or any wildcard ID to that end
1168 if (isanyaddr(&c
->spd
.this.host_addr
) || c
->spd
.this.has_client_wildcard
1169 || c
->spd
.this.has_port_wildcard
|| c
->spd
.this.has_id_wildcards
1170 || c
->spd
.this.allow_any
)
1172 struct end t
= c
->spd
.this;
1174 c
->spd
.this = c
->spd
.that
;
1179 c
->spd
.reqid
= gen_reqid();
1181 /* set internal fields */
1182 c
->instance_serial
= 0;
1183 c
->ac_next
= connections
;
1185 c
->interface
= NULL
;
1186 c
->spd
.routing
= RT_UNROUTED
;
1187 c
->newest_isakmp_sa
= SOS_NOBODY
;
1188 c
->newest_ipsec_sa
= SOS_NOBODY
;
1189 c
->spd
.eroute_owner
= SOS_NOBODY
;
1191 if (c
->policy
& POLICY_GROUP
)
1196 else if ((isanyaddr(&c
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(c
->policy
))
1197 || c
->spd
.that
.has_client_wildcard
|| c
->spd
.that
.has_port_wildcard
1198 || c
->spd
.that
.has_id_wildcards
|| c
->spd
.that
.allow_any
)
1200 /* Opportunistic or Road Warrior or wildcard client subnet
1202 c
->kind
= CK_TEMPLATE
;
1206 c
->kind
= CK_PERMANENT
;
1208 set_policy_prio(c
); /* must be after kind is set */
1211 c
->extra_debugging
= wm
->debugging
;
1216 passert(!(wm
->left
.virt
&& wm
->right
.virt
));
1217 if (wm
->left
.virt
|| wm
->right
.virt
)
1219 passert(isanyaddr(&c
->spd
.that
.host_addr
));
1220 c
->spd
.that
.virt
= create_virtual(c
,
1221 wm
->left
.virt ? wm
->left
.virt
: wm
->right
.virt
);
1222 if (c
->spd
.that
.virt
)
1223 c
->spd
.that
.has_client
= TRUE
;
1228 /* if rightsourceip defines a subnet then create an in-memory pool */
1229 if (whack_attr
->add_pool(whack_attr
, c
->name
,
1230 c
->spd
.this.is_left ?
&wm
->right
: &wm
->left
))
1232 c
->spd
.that
.pool
= clone_str(c
->name
);
1233 c
->spd
.that
.modecfg
= TRUE
;
1234 c
->spd
.that
.has_client
= FALSE
;
1235 /* reset the host_srcip so that it gets assigned in modecfg */
1236 anyaddr(AF_INET
, &c
->spd
.that
.host_srcip
);
1241 connect_to_host_pair(c
);
1244 /* log all about this connection */
1245 plog("added connection description \"%s\"", c
->name
);
1249 (void) format_connection(topo
, sizeof(topo
), c
, &c
->spd
);
1251 DBG_log("%s", topo
);
1253 /* Make sure that address families can be correctly inferred
1254 * from printed ends.
1256 passert(c
->addr_family
== addrtypeof(&c
->spd
.this.host_addr
)
1257 && c
->addr_family
== addrtypeof(&c
->spd
.this.host_nexthop
)
1258 && (c
->spd
.this.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1259 == subnettypeof(&c
->spd
.this.client
)
1261 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_addr
)
1262 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_nexthop
)
1263 && (c
->spd
.that
.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1264 == subnettypeof(&c
->spd
.that
.client
));
1266 DBG_log("ike_life: %lus; ipsec_life: %lus; rekey_margin: %lus;"
1267 " rekey_fuzz: %lu%%; keyingtries: %lu; policy: %s"
1268 , (unsigned long) c
->sa_ike_life_seconds
1269 , (unsigned long) c
->sa_ipsec_life_seconds
1270 , (unsigned long) c
->sa_rekey_margin
1271 , (unsigned long) c
->sa_rekey_fuzz
1272 , (unsigned long) c
->sa_keying_tries
1273 , prettypolicy(c
->policy
));
1278 /* Derive a template connection from a group connection and target.
1279 * Similar to instantiate(). Happens at whack --listen.
1280 * Returns name of new connection. May be NULL.
1281 * Caller is responsible for freeing.
1283 char *add_group_instance(connection_t
*group
, const ip_subnet
*target
)
1285 char namebuf
[100], targetbuf
[SUBNETTOT_BUF
];
1289 passert(group
->kind
== CK_GROUP
);
1290 passert(oriented(*group
));
1292 /* manufacture a unique name for this template */
1293 subnettot(target
, 0, targetbuf
, sizeof(targetbuf
));
1294 snprintf(namebuf
, sizeof(namebuf
), "%s#%s", group
->name
, targetbuf
);
1296 if (con_by_name(namebuf
, FALSE
) != NULL
)
1298 loglog(RC_DUPNAME
, "group name + target yields duplicate name \"%s\""
1303 t
= clone_thing(*group
);
1305 unshare_connection_strings(t
);
1306 name
= clone_str(t
->name
);
1307 t
->spd
.that
.client
= *target
;
1308 t
->policy
&= ~(POLICY_GROUP
| POLICY_GROUTED
);
1309 t
->kind
= isanyaddr(&t
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(t
->policy
)
1310 ? CK_TEMPLATE
: CK_INSTANCE
;
1312 /* reset log file info */
1313 t
->log_file_name
= NULL
;
1315 t
->log_file_err
= FALSE
;
1317 t
->spd
.reqid
= gen_reqid();
1319 if (t
->spd
.that
.virt
)
1321 DBG_log("virtual_ip not supported in group instance");
1322 t
->spd
.that
.virt
= NULL
;
1325 /* add to connections list */
1326 t
->ac_next
= connections
;
1329 /* same host_pair as parent: stick after parent on list */
1332 /* route if group is routed */
1333 if (group
->policy
& POLICY_GROUTED
)
1335 if (!trap_connection(t
))
1336 whack_log(RC_ROUTE
, "could not route");
1342 /* an old target has disappeared for a group: delete instance */
1343 void remove_group_instance(const connection_t
*group USED_BY_DEBUG
,
1346 passert(group
->kind
== CK_GROUP
);
1347 passert(oriented(*group
));
1349 delete_connections_by_name(name
, FALSE
);
1352 /* Common part of instantiating a Road Warrior or Opportunistic connection.
1353 * his_id can be used to carry over an ID discovered in Phase 1.
1354 * It must not disagree with the one in c, but if that is unspecified,
1355 * the new connection will use his_id.
1356 * If his_id is NULL, and c.that.id is uninstantiated (ID_ANY), the
1357 * new connection will continue to have an uninstantiated that.id.
1358 * Note: instantiation does not affect port numbers.
1360 * Note that instantiate can only deal with a single SPD/eroute.
1362 static connection_t
*instantiate(connection_t
*c
, const ip_address
*him
,
1363 u_int16_t his_port
, identification_t
*his_id
)
1367 passert(c
->kind
== CK_TEMPLATE
);
1368 passert(c
->spd
.next
== NULL
);
1370 c
->instance_serial
++;
1371 d
= clone_thing(*c
);
1372 d
->spd
.that
.allow_any
= FALSE
;
1376 d
->spd
.that
.id
= his_id
;
1377 d
->spd
.that
.has_id_wildcards
= FALSE
;
1379 unshare_connection_strings(d
);
1380 if (d
->spd
.this.groups
)
1382 d
->spd
.this.groups
= d
->spd
.this.groups
->get_ref(d
->spd
.this.groups
);
1384 if (d
->spd
.that
.groups
)
1386 d
->spd
.that
.groups
= d
->spd
.that
.groups
->get_ref(d
->spd
.that
.groups
);
1388 d
->kind
= CK_INSTANCE
;
1390 passert(oriented(*d
));
1391 d
->spd
.that
.host_addr
= *him
;
1392 setportof(htons(c
->spd
.that
.port
), &d
->spd
.that
.host_addr
);
1394 if (his_port
) d
->spd
.that
.host_port
= his_port
;
1396 default_end(&d
->spd
.that
, &d
->spd
.this.host_addr
);
1398 /* We cannot guess what our next_hop should be, but if it was
1399 * explicitly specified as 0.0.0.0, we set it to be him.
1400 * (whack will not allow nexthop to be elided in RW case.)
1402 default_end(&d
->spd
.this, &d
->spd
.that
.host_addr
);
1404 d
->spd
.reqid
= gen_reqid();
1406 /* set internal fields */
1407 d
->ac_next
= connections
;
1409 d
->spd
.routing
= RT_UNROUTED
;
1410 d
->newest_isakmp_sa
= SOS_NOBODY
;
1411 d
->newest_ipsec_sa
= SOS_NOBODY
;
1412 d
->spd
.eroute_owner
= SOS_NOBODY
;
1414 /* reset log file info */
1415 d
->log_file_name
= NULL
;
1417 d
->log_file_err
= FALSE
;
1419 connect_to_host_pair(d
);
1422 if (sameaddr(&d
->spd
.that
.host_addr
, &d
->spd
.this.host_nexthop
))
1424 d
->spd
.this.host_nexthop
= *him
;
1428 connection_t
*rw_instantiate(connection_t
*c
, const ip_address
*him
,
1429 u_int16_t his_port
, const ip_subnet
*his_net
,
1430 identification_t
*his_id
)
1432 connection_t
*d
= instantiate(c
, him
, his_port
, his_id
);
1434 if (d
&& his_net
&& is_virtual_connection(c
))
1436 d
->spd
.that
.client
= *his_net
;
1437 d
->spd
.that
.virt
= NULL
;
1438 if (subnetishost(his_net
) && addrinsubnet(him
, his_net
))
1439 d
->spd
.that
.has_client
= FALSE
;
1442 if (d
->policy
& POLICY_OPPO
)
1444 /* This must be before we know the client addresses.
1445 * Fill in one that is impossible. This prevents anyone else from
1446 * trying to use this connection to get to a particular client
1448 d
->spd
.that
.client
= *aftoinfo(subnettypeof(&d
->spd
.that
.client
))->none
;
1451 , DBG_log("instantiated \"%s\" for %s" , d
->name
, ip_str(him
)));
1455 connection_t
*oppo_instantiate(connection_t
*c
, const ip_address
*him
,
1456 identification_t
*his_id
, struct gw_info
*gw
,
1457 const ip_address
*our_client USED_BY_DEBUG
,
1458 const ip_address
*peer_client
)
1460 connection_t
*d
= instantiate(c
, him
, 0, his_id
);
1462 passert(d
->spd
.next
== NULL
);
1464 /* fill in our client side */
1465 if (d
->spd
.this.has_client
)
1467 /* there was a client in the abstract connection
1468 * so we demand that the required client is within that subnet.
1470 passert(addrinsubnet(our_client
, &d
->spd
.this.client
));
1471 happy(addrtosubnet(our_client
, &d
->spd
.this.client
));
1472 /* opportunistic connections do not use port selectors */
1473 setportof(0, &d
->spd
.this.client
.addr
);
1477 /* there was no client in the abstract connection
1478 * so we demand that the required client be the host
1480 passert(sameaddr(our_client
, &d
->spd
.this.host_addr
));
1483 /* fill in peer's client side.
1484 * If the client is the peer, excise the client from the connection.
1486 passert((d
->policy
& POLICY_OPPO
)
1487 && addrinsubnet(peer_client
, &d
->spd
.that
.client
));
1488 happy(addrtosubnet(peer_client
, &d
->spd
.that
.client
));
1489 /* opportunistic connections do not use port selectors */
1490 setportof(0, &d
->spd
.that
.client
.addr
);
1492 if (sameaddr(peer_client
, &d
->spd
.that
.host_addr
))
1493 d
->spd
.that
.has_client
= FALSE
;
1495 passert(d
->gw_info
== NULL
);
1499 /* Adjust routing if something is eclipsing c.
1500 * It must be a %hold for us (hard to passert this).
1501 * If there was another instance eclipsing, we'd be using it.
1503 if (c
->spd
.routing
== RT_ROUTED_ECLIPSED
)
1504 d
->spd
.routing
= RT_ROUTED_PROSPECTIVE
;
1506 /* Remember if the template is routed:
1507 * if so, this instance applies for initiation
1508 * even if it is created for responding.
1510 if (routed(c
->spd
.routing
))
1511 d
->instance_initiation_ok
= TRUE
;
1516 (void) format_connection(topo
, sizeof(topo
), d
, &d
->spd
);
1517 DBG_log("instantiated \"%s\": %s", d
->name
, topo
);
1522 /* priority formatting */
1523 void fmt_policy_prio(policy_prio_t pp
, char buf
[POLICY_PRIO_BUF
])
1525 if (pp
== BOTTOM_PRIO
)
1527 snprintf(buf
, POLICY_PRIO_BUF
, "0");
1531 snprintf(buf
, POLICY_PRIO_BUF
, "%lu,%lu"
1532 , pp
>>16, (pp
& ~(~(policy_prio_t
)0 << 16)) >> 8);
1536 /* Format any information needed to identify an instance of a connection.
1537 * Fills any needed information into buf which MUST be big enough.
1538 * Road Warrior: peer's IP address
1539 * Opportunistic: [" " myclient "==="] " ..." peer ["===" hisclient] '\0'
1541 static size_t fmt_client(const ip_subnet
*client
, const ip_address
*gw
,
1542 const char *prefix
, char buf
[ADDRTOT_BUF
])
1544 if (subnetisaddr(client
, gw
))
1546 buf
[0] = '\0'; /* compact denotation for "self" */
1552 strcpy(buf
, prefix
);
1553 ap
= buf
+ strlen(prefix
);
1554 if (subnetisnone(client
))
1555 strcpy(ap
, "?"); /* unknown */
1557 subnettot(client
, 0, ap
, SUBNETTOT_BUF
);
1562 void fmt_conn_instance(const connection_t
*c
, char buf
[CONN_INST_BUF
])
1568 if (c
->kind
== CK_INSTANCE
)
1570 if (c
->instance_serial
!= 0)
1572 snprintf(p
, CONN_INST_BUF
, "[%lu]", c
->instance_serial
);
1576 if (c
->policy
& POLICY_OPPO
)
1578 size_t w
= fmt_client(&c
->spd
.this.client
, &c
->spd
.this.host_addr
, " ", p
);
1582 strcpy(p
, w
== 0?
" ..." : "=== ...");
1585 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1588 (void) fmt_client(&c
->spd
.that
.client
, &c
->spd
.that
.host_addr
, "===", p
);
1593 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1595 if (c
->spd
.that
.host_port
!= pluto_port
)
1598 sprintf(p
, ":%d", c
->spd
.that
.host_port
);
1604 /* Find an existing connection for a trapped outbound packet.
1605 * This is attempted before we bother with gateway discovery.
1606 * + this connection is routed or instance_of_routed_template
1607 * (i.e. approved for on-demand)
1608 * + this subnet contains our_client (or we are our_client)
1609 * + that subnet contains peer_client (or peer is peer_client)
1610 * + don't care about Phase 1 IDs (we don't know)
1611 * Note: result may still need to be instantiated.
1612 * The winner has the highest policy priority.
1614 * If there are several with that priority, we give preference to
1615 * the first one that is an instance.
1617 * See also build_outgoing_opportunistic_connection.
1619 connection_t
*find_connection_for_clients(struct spd_route
**srp
,
1620 const ip_address
*our_client
,
1621 const ip_address
*peer_client
,
1622 int transport_proto
)
1624 connection_t
*c
= connections
, *best
= NULL
;
1625 policy_prio_t best_prio
= BOTTOM_PRIO
;
1626 struct spd_route
*sr
;
1627 struct spd_route
*best_sr
= NULL
;
1628 int our_port
= ntohs(portof(our_client
));
1629 int peer_port
= ntohs(portof(peer_client
));
1631 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1633 if (DBGP(DBG_CONTROL
))
1635 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1637 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1638 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1639 DBG_log("find_connection: "
1640 "looking for policy for connection: %s:%d/%d -> %s:%d/%d"
1641 , ocb
, transport_proto
, our_port
, pcb
, transport_proto
, peer_port
);
1645 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
1647 if (c
->kind
== CK_GROUP
)
1652 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1654 if ((routed(sr
->routing
) || c
->instance_initiation_ok
)
1655 && addrinsubnet(our_client
, &sr
->this.client
)
1656 && addrinsubnet(peer_client
, &sr
->that
.client
)
1657 && addrinsubnet(peer_client
, &sr
->that
.client
)
1658 && (!sr
->this.protocol
|| transport_proto
== sr
->this.protocol
)
1659 && (!sr
->this.port
|| our_port
== sr
->this.port
)
1660 && (!sr
->that
.port
|| peer_port
== sr
->that
.port
))
1662 char cib
[CONN_INST_BUF
];
1663 char cib2
[CONN_INST_BUF
];
1665 policy_prio_t prio
= 8 * (c
->prio
+ (c
->kind
== CK_INSTANCE
))
1666 + 2 * (sr
->this.port
== our_port
)
1667 + 2 * (sr
->that
.port
== peer_port
)
1668 + (sr
->this.protocol
== transport_proto
);
1671 if (DBGP(DBG_CONTROL
|DBG_CONTROLMORE
))
1673 char c_ocb
[SUBNETTOT_BUF
], c_pcb
[SUBNETTOT_BUF
];
1675 subnettot(&c
->spd
.this.client
, 0, c_ocb
, sizeof(c_ocb
));
1676 subnettot(&c
->spd
.that
.client
, 0, c_pcb
, sizeof(c_pcb
));
1677 DBG_log("find_connection: conn \"%s\"%s has compatible peers: %s->%s [pri: %ld]"
1679 , (fmt_conn_instance(c
, cib
), cib
)
1680 , c_ocb
, c_pcb
, prio
);
1691 DBG(DBG_CONTROLMORE
,
1692 DBG_log("find_connection: "
1693 "comparing best \"%s\"%s [pri:%ld]{%p} (child %s) to \"%s\"%s [pri:%ld]{%p} (child %s)"
1695 , (fmt_conn_instance(best
, cib
), cib
)
1698 , (best
->policy_next ? best
->policy_next
->name
: "none")
1700 , (fmt_conn_instance(c
, cib2
), cib2
)
1703 , (c
->policy_next ? c
->policy_next
->name
: "none")));
1705 if (prio
> best_prio
)
1715 if (best
&& NEVER_NEGOTIATE(best
->policy
))
1725 if (DBGP(DBG_CONTROL
))
1729 char cib
[CONN_INST_BUF
];
1730 DBG_log("find_connection: concluding with \"%s\"%s [pri:%ld]{%p} kind=%s"
1732 , (fmt_conn_instance(best
, cib
), cib
)
1735 , enum_name(&connection_kind_names
, best
->kind
));
1737 DBG_log("find_connection: concluding with empty");
1745 /* Find and instantiate a connection for an outgoing Opportunistic connection.
1746 * We've already discovered its gateway.
1747 * We look for a the connection such that:
1748 * + this is one of our interfaces
1749 * + this subnet contains our_client (or we are our_client)
1750 * (we will specialize the client). We prefer the smallest such subnet.
1751 * + that subnet contains peer_clent (we will specialize the client).
1752 * We prefer the smallest such subnet.
1753 * + is opportunistic
1754 * + that peer is NO_IP
1755 * + don't care about Phase 1 IDs (probably should be default)
1756 * We could look for a connection that already had the desired peer
1757 * (rather than NO_IP) specified, but it doesn't seem worth the
1760 * We look for the routed policy applying to the narrowest subnets.
1761 * We only succeed if we find such a policy AND it is satisfactory.
1763 * The body of the inner loop is a lot like that in
1764 * find_connection_for_clients. In this case, we know the gateways
1765 * that we need to instantiate an opportunistic connection.
1767 connection_t
*build_outgoing_opportunistic_connection(struct gw_info
*gw
,
1768 const ip_address
*our_client
,
1769 const ip_address
*peer_client
)
1772 connection_t
*best
= NULL
;
1773 struct spd_route
*sr
, *bestsr
;
1774 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1776 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1777 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1779 /* for each of our addresses... */
1780 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1782 /* go through those connections with our address and NO_IP as hosts
1783 * We cannot know what port the peer would use, so we assume
1784 * that it is pluto_port (makes debugging easier).
1786 connection_t
*c
= find_host_pair_connections(&p
->addr
, pluto_port
,
1787 (ip_address
*)NULL
, pluto_port
);
1789 for (; c
!= NULL
; c
= c
->hp_next
)
1792 DBG_log("checking %s", c
->name
));
1793 if (c
->kind
== CK_GROUP
)
1798 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1800 if (routed(sr
->routing
)
1801 && addrinsubnet(our_client
, &sr
->this.client
)
1802 && addrinsubnet(peer_client
, &sr
->that
.client
))
1811 DBG_log("comparing best %s to %s"
1812 , best
->name
, c
->name
));
1814 for (bestsr
= &best
->spd
; best
!=c
&& bestsr
; bestsr
=bestsr
->next
)
1816 if (!subnetinsubnet(&bestsr
->this.client
, &sr
->this.client
)
1817 || (samesubnet(&bestsr
->this.client
, &sr
->this.client
)
1818 && !subnetinsubnet(&bestsr
->that
.client
1819 , &sr
->that
.client
)))
1829 if (best
== NULL
|| NEVER_NEGOTIATE(best
->policy
) ||
1830 (best
->policy
& POLICY_OPPO
) == LEMPTY
|| best
->kind
!= CK_TEMPLATE
)
1836 chunk_t encoding
= gw
->gw_id
->get_encoding(gw
->gw_id
);
1837 id_type_t type
= gw
->gw_id
->get_type(gw
->gw_id
);
1840 initaddr(encoding
.ptr
, encoding
.len
,
1841 (type
== ID_IPV4_ADDR
) ? AF_INET
: AF_INET6
, &ip_addr
);
1843 return oppo_instantiate(best
, &ip_addr
, NULL
, gw
, our_client
, peer_client
);
1847 bool orient(connection_t
*c
)
1849 struct spd_route
*sr
;
1855 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
1857 /* Note: this loop does not stop when it finds a match:
1858 * it continues checking to catch any ambiguity.
1860 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1869 /* check if this interface matches this end */
1870 if (sameaddr(&sr
->this.host_addr
, &p
->addr
)
1871 && (!no_klips
|| sr
->this.host_port
== pluto_port
))
1875 if (c
->interface
== p
)
1876 loglog(RC_LOG_SERIOUS
1877 , "both sides of \"%s\" are our interface %s!"
1878 , c
->name
, p
->rname
);
1880 loglog(RC_LOG_SERIOUS
, "two interfaces match \"%s\" (%s, %s)"
1881 , c
->name
, c
->interface
->rname
, p
->rname
);
1882 c
->interface
= NULL
; /* withdraw orientation */
1888 /* done with this interface if it doesn't match that end */
1889 if (!(sameaddr(&sr
->that
.host_addr
, &p
->addr
)
1890 && (!no_klips
|| sr
->that
.host_port
== pluto_port
)))
1893 /* swap ends and try again.
1894 * It is a little tricky to see that this loop will stop.
1895 * Only continue if the far side matches.
1896 * If both sides match, there is an error-out.
1899 struct end t
= sr
->this;
1901 sr
->this = sr
->that
;
1908 return oriented(*c
);
1911 void initiate_connection(const char *name
, int whackfd
)
1913 connection_t
*c
= con_by_name(name
, TRUE
);
1917 set_cur_connection(c
);
1920 loglog(RC_ORIENT
, "we have no ipsecN interface for either end of this connection");
1922 else if (NEVER_NEGOTIATE(c
->policy
))
1925 , "cannot initiate an authby=never connection");
1927 else if (c
->kind
!= CK_PERMANENT
&& !c
->spd
.that
.allow_any
)
1929 if (isanyaddr(&c
->spd
.that
.host_addr
))
1930 loglog(RC_NOPEERIP
, "cannot initiate connection without knowing peer IP address");
1932 loglog(RC_WILDCARD
, "cannot initiate connection with ID wildcards");
1936 /* do we have to prompt for a PIN code? */
1937 if (c
->spd
.this.sc
&& !c
->spd
.this.sc
->valid
&& whackfd
!= NULL_FD
)
1939 scx_get_pin(c
->spd
.this.sc
, whackfd
);
1941 if (c
->spd
.this.sc
&& !c
->spd
.this.sc
->valid
)
1943 loglog(RC_NOVALIDPIN
, "cannot initiate connection without valid PIN");
1948 if (c
->spd
.that
.allow_any
)
1950 c
= instantiate(c
, &c
->spd
.that
.host_addr
,
1951 c
->spd
.that
.host_port
, c
->spd
.that
.id
);
1954 /* We will only request an IPsec SA if policy isn't empty
1955 * (ignoring Main Mode items).
1956 * This is a fudge, but not yet important.
1957 * If we are to proceed asynchronously, whackfd will be NULL_FD.
1959 c
->policy
|= POLICY_UP
;
1960 ipsecdoi_initiate(whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
1961 whackfd
= NULL_FD
; /* protect from close */
1964 reset_cur_connection();
1969 /* (Possibly) Opportunistic Initiation:
1970 * Knowing clients (single IP addresses), try to build an tunnel.
1971 * This may involve discovering a gateway and instantiating an
1972 * Opportunistic connection. Called when a packet is caught by
1973 * a %trap, or when whack --oppohere --oppothere is used.
1974 * It may turn out that an existing or non-opporunistic connnection
1975 * can handle the traffic.
1977 * Most of the code will be restarted if an ADNS request is made
1978 * to discover the gateway. The only difference between the first
1979 * and second entry is whether gateways_from_dns is NULL or not.
1980 * initiate_opportunistic: initial entrypoint
1981 * continue_oppo: where we pickup when ADNS result arrives
1982 * initiate_opportunistic_body: main body shared by above routines
1983 * cannot_oppo: a helper function to log a diagnostic
1984 * This structure repeats a lot of code when the ADNS result arrives.
1985 * This seems like a waste, but anything learned the first time through
1986 * may no longer be true!
1988 * After the first IKE message is sent, the regular state machinery
1989 * carries negotiation forward.
1992 enum find_oppo_step
{
1995 fos_myid_hostname_txt
,
1997 fos_myid_hostname_key
,
2002 #endif /* USE_KEYRR */
2008 static const char *const oppo_step_name
[] = {
2011 "fos_myid_hostname_txt",
2013 "fos_myid_hostname_key",
2018 #endif /* USE_KEYRR */
2024 struct find_oppo_bundle
{
2025 enum find_oppo_step step
;
2027 bool failure_ok
; /* if true, continue_oppo should not die on DNS failure */
2028 ip_address our_client
; /* not pointer! */
2029 ip_address peer_client
;
2030 int transport_proto
;
2032 policy_prio_t policy_prio
;
2033 ipsec_spi_t failure_shunt
; /* in host order! 0 for delete. */
2037 struct find_oppo_continuation
{
2038 struct adns_continuation ac
; /* common prefix */
2039 struct find_oppo_bundle b
;
2042 static void cannot_oppo(connection_t
*c
, struct find_oppo_bundle
*b
, err_t ugh
)
2044 char pcb
[ADDRTOT_BUF
];
2045 char ocb
[ADDRTOT_BUF
];
2047 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2048 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2050 DBG(DBG_DNS
| DBG_OPPO
, DBG_log("Can't Opportunistically initiate for %s to %s: %s"
2053 whack_log(RC_OPPOFAILURE
2054 , "Can't Opportunistically initiate for %s to %s: %s"
2057 if (c
&& c
->policy_next
)
2059 /* there is some policy that comes afterwards */
2060 struct spd_route
*shunt_spd
;
2061 connection_t
*nc
= c
->policy_next
;
2064 passert(c
->kind
== CK_TEMPLATE
);
2065 passert(c
->policy_next
->kind
== CK_PERMANENT
);
2067 DBG(DBG_OPPO
, DBG_log("OE failed for %s to %s, but %s overrides shunt"
2068 , ocb
, pcb
, c
->policy_next
->name
));
2071 * okay, here we need add to the "next" policy, which is ought
2072 * to be an instance.
2073 * We will add another entry to the spd_route list for the specific
2074 * situation that we have.
2077 shunt_spd
= clone_thing(nc
->spd
);
2079 shunt_spd
->next
= nc
->spd
.next
;
2080 nc
->spd
.next
= shunt_spd
;
2082 happy(addrtosubnet(&b
->peer_client
, &shunt_spd
->that
.client
));
2084 if (sameaddr(&b
->peer_client
, &shunt_spd
->that
.host_addr
))
2085 shunt_spd
->that
.has_client
= FALSE
;
2088 * override the tunnel destination with the one from the secondaried
2091 shunt_spd
->that
.host_addr
= nc
->spd
.that
.host_addr
;
2093 /* now, lookup the state, and poke it up.
2096 st
= state_with_serialno(nc
->newest_ipsec_sa
);
2098 /* XXX what to do if the IPSEC SA has died? */
2099 passert(st
!= NULL
);
2101 /* link the new connection instance to the state's list of
2105 DBG(DBG_OPPO
, DBG_log("installing state: %ld for %s to %s"
2106 , nc
->newest_ipsec_sa
2110 if (DBGP(DBG_OPPO
| DBG_CONTROLMORE
))
2112 char state_buf
[LOG_WIDTH
];
2113 char state_buf2
[LOG_WIDTH
];
2116 fmt_state(FALSE
, st
, n
2117 , state_buf
, sizeof(state_buf
)
2118 , state_buf2
, sizeof(state_buf2
));
2119 DBG_log("cannot_oppo, failure SA1: %s", state_buf
);
2120 DBG_log("cannot_oppo, failure SA2: %s", state_buf2
);
2124 if (!route_and_eroute(c
, shunt_spd
, st
))
2126 whack_log(RC_OPPOFAILURE
2127 , "failed to instantiate shunt policy %s for %s to %s"
2137 /* Replace HOLD with b->failure_shunt.
2138 * If no b->failure_shunt specified, use SPI_PASS -- THIS MAY CHANGE.
2140 if (b
->failure_shunt
== 0)
2142 DBG(DBG_OPPO
, DBG_log("no explicit failure shunt for %s to %s; installing %%pass"
2146 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2149 , b
->failure_shunt
!= 0
2150 , b
->transport_proto
2156 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
2157 , struct adns_continuation
*ac
, err_t ac_ugh
); /* forward */
2159 void initiate_opportunistic(const ip_address
*our_client
,
2160 const ip_address
*peer_client
, int transport_proto
,
2161 bool held
, int whackfd
)
2163 struct find_oppo_bundle b
;
2165 b
.want
= (whackfd
== NULL_FD ?
"whack" : "acquire");
2166 b
.failure_ok
= FALSE
;
2167 b
.our_client
= *our_client
;
2168 b
.peer_client
= *peer_client
;
2169 b
.transport_proto
= transport_proto
;
2171 b
.policy_prio
= BOTTOM_PRIO
;
2172 b
.failure_shunt
= 0;
2173 b
.whackfd
= whackfd
;
2175 initiate_opportunistic_body(&b
, NULL
, NULL
);
2178 static void continue_oppo(struct adns_continuation
*acr
, err_t ugh
)
2180 struct find_oppo_continuation
*cr
= (void *)acr
; /* inherit, damn you! */
2182 bool was_held
= cr
->b
.held
;
2183 int whackfd
= cr
->b
.whackfd
;
2185 /* note: cr->id has no resources; cr->sgw_id is ID_ANY:
2186 * neither need freeing.
2188 whack_log_fd
= whackfd
;
2191 /* Discover and record whether %hold has gone away.
2192 * This could have happened while we were awaiting DNS.
2193 * We must check BEFORE any call to cannot_oppo.
2196 cr
->b
.held
= has_bare_hold(&cr
->b
.our_client
, &cr
->b
.peer_client
2197 , cr
->b
.transport_proto
);
2201 /* if we're going to ignore the error, at least note it in debugging log */
2202 if (cr
->b
.failure_ok
&& ugh
)
2204 DBG(DBG_CONTROL
| DBG_DNS
,
2206 char ocb
[ADDRTOT_BUF
];
2207 char pcb
[ADDRTOT_BUF
];
2209 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2210 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2211 DBG_log("continuing from failed DNS lookup for %s, %s to %s: %s"
2212 , cr
->b
.want
, ocb
, pcb
, ugh
);
2217 if (!cr
->b
.failure_ok
&& ugh
)
2219 c
= find_connection_for_clients(NULL
, &cr
->b
.our_client
, &cr
->b
.peer_client
2220 , cr
->b
.transport_proto
);
2221 cannot_oppo(c
, &cr
->b
2222 , builddiag("%s: %s", cr
->b
.want
, ugh
));
2224 else if (was_held
&& !cr
->b
.held
)
2226 /* was_held indicates we were started due to a %trap firing
2227 * (as opposed to a "whack --oppohere --oppothere").
2228 * Since the %hold has gone, we can assume that somebody else
2229 * has beaten us to the punch. We can go home. But lets log it.
2231 char ocb
[ADDRTOT_BUF
];
2232 char pcb
[ADDRTOT_BUF
];
2234 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2235 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2238 , "%%hold otherwise handled during DNS lookup for Opportunistic Initiation for %s to %s"
2243 initiate_opportunistic_body(&cr
->b
, &cr
->ac
, ugh
);
2244 whackfd
= NULL_FD
; /* was handed off */
2247 whack_log_fd
= NULL_FD
;
2252 static err_t
check_key_recs(enum myid_state try_state
, const connection_t
*c
,
2253 struct adns_continuation
*ac
)
2255 /* Check if KEY lookup yielded good results.
2256 * Looking up based on our ID. Used if
2257 * client is ourself, or if TXT had no public key.
2258 * Note: if c is different this time, there is
2259 * a chance that we did the wrong query.
2260 * If so, treat as a kind of failure.
2262 enum myid_state old_myid_state
= myid_state
;
2263 private_key_t
*private;
2266 myid_state
= try_state
;
2268 if (old_myid_state
!= myid_state
&& old_myid_state
== MYID_SPECIFIED
)
2270 ugh
= "%myid was specified while we were guessing";
2272 else if ((private = get_private_key(c
)) == NULL
)
2274 ugh
= "we don't know our own RSA key";
2276 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2278 ugh
= "our ID changed underfoot";
2282 /* Similar to code in RSA_check_signature
2283 * for checking the other side.
2287 ugh
= "no KEY RR found for us";
2288 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2290 ugh
= "all our KEY RRs have the wrong public key";
2291 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2292 && private->belongs_to(private, &kr
->key
->public_key
))
2294 ugh
= NULL
; /* good! */
2301 myid_state
= old_myid_state
;
2305 #endif /* USE_KEYRR */
2307 static err_t
check_txt_recs(enum myid_state try_state
, const connection_t
*c
,
2308 struct adns_continuation
*ac
)
2310 /* Check if TXT lookup yielded good results.
2311 * Looking up based on our ID. Used if
2312 * client is ourself, or if TXT had no public key.
2313 * Note: if c is different this time, there is
2314 * a chance that we did the wrong query.
2315 * If so, treat as a kind of failure.
2317 enum myid_state old_myid_state
= myid_state
;
2318 private_key_t
*private;
2321 myid_state
= try_state
;
2323 if (old_myid_state
!= myid_state
2324 && old_myid_state
== MYID_SPECIFIED
)
2326 ugh
= "%myid was specified while we were guessing";
2328 else if ((private = get_private_key(c
)) == NULL
)
2330 ugh
= "we don't know our own RSA key";
2332 else if (!ac
->id
->equals(ac
->id
, c
->spd
.this.id
))
2334 ugh
= "our ID changed underfoot";
2338 /* Similar to code in RSA_check_signature
2339 * for checking the other side.
2341 struct gw_info
*gwp
;
2343 ugh
= "no TXT RR found for us";
2344 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2346 public_key_t
*pub_key
= gwp
->key
->public_key
;
2348 ugh
= "all our TXT RRs have the wrong public key";
2349 if (pub_key
->get_type(pub_key
) == KEY_RSA
&&
2350 private->belongs_to(private, pub_key
))
2352 ugh
= NULL
; /* good! */
2359 myid_state
= old_myid_state
;
2365 /* note: gateways_from_dns must be NULL iff this is the first call */
2366 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
,
2367 struct adns_continuation
*ac
,
2371 struct spd_route
*sr
;
2373 /* What connection shall we use?
2374 * First try for one that explicitly handles the clients.
2378 char ours
[ADDRTOT_BUF
];
2379 char his
[ADDRTOT_BUF
];
2383 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2384 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2385 ourport
= ntohs(portof(&b
->our_client
));
2386 hisport
= ntohs(portof(&b
->peer_client
));
2387 DBG_log("initiate on demand from %s:%d to %s:%d proto=%d state: %s because: %s"
2388 , ours
, ourport
, his
, hisport
, b
->transport_proto
2389 , oppo_step_name
[b
->step
], b
->want
);
2391 if (isanyaddr(&b
->our_client
) || isanyaddr(&b
->peer_client
))
2393 cannot_oppo(NULL
, b
, "impossible IP address");
2395 else if ((c
= find_connection_for_clients(&sr
2398 , b
->transport_proto
)) == NULL
)
2400 /* No connection explicitly handles the clients and there
2401 * are no Opportunistic connections -- whine and give up.
2402 * The failure policy cannot be gotten from a connection; we pick %pass.
2404 cannot_oppo(NULL
, b
, "no routed Opportunistic template covers this pair");
2406 else if (c
->kind
!= CK_TEMPLATE
)
2408 /* We've found a connection that can serve.
2409 * Do we have to initiate it?
2410 * Not if there is currently an IPSEC SA.
2411 * But if there is an IPSEC SA, then KLIPS would not
2412 * have generated the acquire. So we assume that there isn't one.
2413 * This may be redundant if a non-opportunistic
2414 * negotiation is already being attempted.
2417 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2419 if(c
->kind
== CK_INSTANCE
)
2421 char cib
[CONN_INST_BUF
];
2422 /* there is already an instance being negotiated, no nothing */
2423 DBG(DBG_CONTROL
, DBG_log("found existing instance \"%s\"%s, rekeying it"
2425 , (fmt_conn_instance(c
, cib
), cib
)));
2426 /* XXX-mcr - return; */
2429 /* otherwise, there is some kind of static conn that can handle
2430 * this connection, so we initiate it */
2435 /* what should we do on failure? */
2436 (void) assign_hold(c
, sr
, b
->transport_proto
, &b
->our_client
, &b
->peer_client
);
2439 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2440 b
->whackfd
= NULL_FD
; /* protect from close */
2444 /* We are handling an opportunistic situation.
2445 * This involves several DNS lookup steps that require suspension.
2446 * Note: many facts might change while we're suspended.
2449 * The first chunk of code handles the result of the previous
2450 * DNS query (if any). It also selects the kind of the next step.
2451 * The second chunk initiates the next DNS query (if any).
2453 enum find_oppo_step next_step
= fos_myid_ip_txt
;
2455 char mycredentialstr
[BUF_LEN
];
2456 char cib
[CONN_INST_BUF
];
2458 DBG(DBG_CONTROL
, DBG_log("creating new instance from \"%s\"%s",
2459 c
->name
, (fmt_conn_instance(c
, cib
), cib
)));
2460 snprintf(mycredentialstr
, BUF_LEN
, "%Y", sr
->this.id
);
2462 /* handle any DNS answer; select next step */
2466 /* just starting out: select first query step */
2467 next_step
= fos_myid_ip_txt
;
2470 case fos_myid_ip_txt
: /* TXT for our default IP address as %myid */
2471 ugh
= check_txt_recs(MYID_IP
, c
, ac
);
2474 /* cannot use our IP as OE identitiy for initiation */
2476 DBG_log("can not use our IP (%Y:TXT) as identity: %s",
2477 myids
[MYID_IP
], ugh
));
2478 if (!logged_myid_ip_txt_warning
)
2480 loglog(RC_LOG_SERIOUS
,
2481 "can not use our IP (%Y:TXT) as identity: %s",
2482 myids
[MYID_IP
], ugh
);
2483 logged_myid_ip_txt_warning
= TRUE
;
2486 next_step
= fos_myid_hostname_txt
;
2487 ugh
= NULL
; /* failure can be recovered from */
2491 /* we can use our IP as OE identity for initiation */
2492 if (!logged_myid_ip_txt_warning
)
2494 loglog(RC_LOG_SERIOUS
,
2495 "using our IP (%Y:TXT) as identity!",
2497 logged_myid_ip_txt_warning
= TRUE
;
2500 next_step
= fos_our_client
;
2504 case fos_myid_hostname_txt
: /* TXT for our hostname as %myid */
2505 ugh
= check_txt_recs(MYID_HOSTNAME
, c
, ac
);
2508 /* cannot use our hostname as OE identitiy for initiation */
2510 DBG_log("can not use our hostname (%Y:TXT) as identity: %s",
2511 myids
[MYID_HOSTNAME
], ugh
));
2512 if (!logged_myid_fqdn_txt_warning
)
2514 loglog(RC_LOG_SERIOUS
,
2515 "can not use our hostname (%Y:TXT) as identity: %s",
2516 myids
[MYID_HOSTNAME
], ugh
);
2517 logged_myid_fqdn_txt_warning
= TRUE
;
2520 next_step
= fos_myid_ip_key
;
2521 ugh
= NULL
; /* failure can be recovered from */
2526 /* we can use our hostname as OE identity for initiation */
2527 if (!logged_myid_fqdn_txt_warning
)
2529 loglog(RC_LOG_SERIOUS
,
2530 "using our hostname (%Y:TXT) as identity!",
2531 myids
[MYID_HOSTNAME
]);
2532 logged_myid_fqdn_txt_warning
= TRUE
;
2534 next_step
= fos_our_client
;
2539 case fos_myid_ip_key
: /* KEY for our default IP address as %myid */
2540 ugh
= check_key_recs(MYID_IP
, c
, ac
);
2543 /* cannot use our IP as OE identitiy for initiation */
2545 DBG_log("can not use our IP (%Y:KEY) as identity: %s",
2546 myids
[MYID_IP
], ugh
));
2547 if (!logged_myid_ip_key_warning
)
2549 loglog(RC_LOG_SERIOUS
,
2550 "can not use our IP (%Y:KEY) as identity: %s",
2551 myids
[MYID_IP
], ugh
);
2552 logged_myid_ip_key_warning
= TRUE
;
2555 next_step
= fos_myid_hostname_key
;
2556 ugh
= NULL
; /* failure can be recovered from */
2560 /* we can use our IP as OE identity for initiation */
2561 if (!logged_myid_ip_key_warning
)
2563 loglog(RC_LOG_SERIOUS
,
2564 "using our IP (%Y:KEY) as identity!",
2566 logged_myid_ip_key_warning
= TRUE
;
2568 next_step
= fos_our_client
;
2572 case fos_myid_hostname_key
: /* KEY for our hostname as %myid */
2573 ugh
= check_key_recs(MYID_HOSTNAME
, c
, ac
);
2576 /* cannot use our IP as OE identitiy for initiation */
2578 DBG_log("can not use our hostname (%Y:KEY) as identity: %s",
2579 myids
[MYID_HOSTNAME
], ugh
));
2580 if (!logged_myid_fqdn_key_warning
)
2582 loglog(RC_LOG_SERIOUS
,
2583 "can not use our hostname (%Y:KEY) as identity: %s",
2584 myids
[MYID_HOSTNAME
], ugh
);
2585 logged_myid_fqdn_key_warning
= TRUE
;
2587 next_step
= fos_myid_hostname_key
;
2588 ugh
= NULL
; /* failure can be recovered from */
2592 /* we can use our IP as OE identity for initiation */
2593 if (!logged_myid_fqdn_key_warning
)
2595 loglog(RC_LOG_SERIOUS
,
2596 "using our hostname (%Y:KEY) as identity!",
2597 myids
[MYID_HOSTNAME
]);
2598 logged_myid_fqdn_key_warning
= TRUE
;
2600 next_step
= fos_our_client
;
2605 case fos_our_client
: /* TXT for our client */
2607 /* Our client is not us: we must check the TXT records.
2608 * Note: if c is different this time, there is
2609 * a chance that we did the wrong query.
2610 * If so, treat as a kind of failure.
2612 private_key_t
*private = get_private_key(c
);
2614 next_step
= fos_his_client
; /* normal situation */
2616 if (private == NULL
)
2618 ugh
= "we don't know our own RSA key";
2620 else if (sameaddr(&sr
->this.host_addr
, &b
->our_client
))
2622 /* this wasn't true when we started -- bail */
2623 ugh
= "our IP address changed underfoot";
2625 else if (!ac
->sgw_id
->equals(ac
->sgw_id
, sr
->this.id
))
2627 /* this wasn't true when we started -- bail */
2628 ugh
= "our ID changed underfoot";
2632 /* Similar to code in quick_inI1_outR1_tail
2633 * for checking the other side.
2635 struct gw_info
*gwp
;
2637 ugh
= "no TXT RR for our client delegates us";
2638 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2640 ugh
= "TXT RR for our client has wrong key";
2641 /* If there is a key from the TXT record,
2642 * we count it as a win if we match the key.
2643 * If there was no key, we have a tentative win:
2644 * we need to check our KEY record to be sure.
2646 if (!gwp
->gw_key_present
)
2648 /* Success, but the TXT had no key
2649 * so we must check our our own KEY records.
2651 next_step
= fos_our_txt
;
2652 ugh
= NULL
; /* good! */
2655 if (private->belongs_to(private, gwp
->key
->public_key
))
2657 ugh
= NULL
; /* good! */
2665 case fos_our_txt
: /* TXT for us */
2667 /* Check if TXT lookup yielded good results.
2668 * Looking up based on our ID. Used if
2669 * client is ourself, or if TXT had no public key.
2670 * Note: if c is different this time, there is
2671 * a chance that we did the wrong query.
2672 * If so, treat as a kind of failure.
2674 private_key_t
*private = get_private_key(c
);
2676 next_step
= fos_his_client
; /* unless we decide to look for KEY RR */
2678 if (private == NULL
)
2680 ugh
= "we don't know our own RSA key";
2682 else if (!ac
->id
->equals(ac
->id
, c
->spd
.this.id
))
2684 ugh
= "our ID changed underfoot";
2688 /* Similar to code in RSA_check_signature
2689 * for checking the other side.
2691 struct gw_info
*gwp
;
2693 ugh
= "no TXT RR for us";
2694 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2696 ugh
= "TXT RR for us has wrong key";
2697 if (gwp
->gw_key_present
&&
2698 private->belongs_to(private, gwp
->key
->public_key
))
2701 DBG_log("initiate on demand found TXT with right public key at: %s"
2702 , mycredentialstr
));
2710 /* if no TXT with right key, try KEY */
2712 DBG_log("will try for KEY RR since initiate on demand found %s: %s"
2713 , ugh
, mycredentialstr
));
2714 next_step
= fos_our_key
;
2723 case fos_our_key
: /* KEY for us */
2725 /* Check if KEY lookup yielded good results.
2726 * Looking up based on our ID. Used if
2727 * client is ourself, or if TXT had no public key.
2728 * Note: if c is different this time, there is
2729 * a chance that we did the wrong query.
2730 * If so, treat as a kind of failure.
2732 private_key_t
*private = get_private_key(c
);
2734 next_step
= fos_his_client
; /* always */
2736 if (private == NULL
)
2738 ugh
= "we don't know our own RSA key";
2740 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2742 ugh
= "our ID changed underfoot";
2746 /* Similar to code in RSA_check_signature
2747 * for checking the other side.
2751 ugh
= "no KEY RR found for us (and no good TXT RR)";
2752 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2754 ugh
= "all our KEY RRs have the wrong public key (and no good TXT RR)";
2755 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2756 && private->belongs_to(private, kr
->key
->public_key
))
2758 /* do this only once a day */
2759 if (!logged_txt_warning
)
2761 loglog(RC_LOG_SERIOUS
2762 , "found KEY RR but not TXT RR for %s. See http://www.freeswan.org/err/txt-change.html."
2764 logged_txt_warning
= TRUE
;
2766 ugh
= NULL
; /* good! */
2773 #endif /* USE_KEYRR */
2775 case fos_his_client
: /* TXT for his client */
2777 /* We've finished last DNS queries: TXT for his client.
2778 * Using the information, try to instantiate a connection
2779 * and start negotiating.
2780 * We now know the peer. The chosing of "c" ignored this,
2781 * so we will disregard its current value.
2782 * !!! We need to randomize the entry in gw that we choose.
2784 next_step
= fos_done
; /* no more queries */
2786 c
= build_outgoing_opportunistic_connection(ac
->gateways_from_dns
2792 /* We cannot seem to instantiate a suitable connection:
2795 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
2797 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2798 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2799 loglog(RC_OPPOFAILURE
,
2800 "no suitable connection for opportunism "
2801 "between %s and %s with %Y as peer",
2802 ocb
, pcb
, ac
->gateways_from_dns
->gw_id
);
2807 /* Replace HOLD with PASS.
2808 * The type of replacement *ought* to be
2809 * specified by policy.
2811 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2813 , SPI_PASS
/* fail into PASS */
2814 , TRUE
, b
->transport_proto
2815 , "no suitable connection");
2821 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2822 passert(c
->kind
== CK_INSTANCE
);
2823 passert(c
->gw_info
!= NULL
);
2824 passert(HAS_IPSEC_POLICY(c
->policy
));
2825 passert(LHAS(LELEM(RT_UNROUTED
) | LELEM(RT_ROUTED_PROSPECTIVE
), c
->spd
.routing
));
2829 /* what should we do on failure? */
2830 (void) assign_hold(c
, &c
->spd
2831 , b
->transport_proto
2832 , &b
->our_client
, &b
->peer_client
);
2835 c
->gw_info
->key
->last_tried_time
= now();
2836 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2837 b
->whackfd
= NULL_FD
; /* protect from close */
2846 /* the second chunk: initiate the next DNS query (if any) */
2849 char ours
[ADDRTOT_BUF
];
2850 char his
[ADDRTOT_BUF
];
2852 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2853 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2854 DBG_log("initiate on demand from %s to %s new state: %s with ugh: %s"
2855 , ours
, his
, oppo_step_name
[b
->step
], ugh ? ugh
: "ok");
2860 b
->policy_prio
= c
->prio
;
2861 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2862 cannot_oppo(c
, b
, ugh
);
2864 else if (next_step
== fos_done
)
2870 /* set up the next query */
2871 struct find_oppo_continuation
*cr
= malloc_thing(struct find_oppo_continuation
);
2872 identification_t
*id
;
2874 b
->policy_prio
= c
->prio
;
2875 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2876 cr
->b
= *b
; /* copy; start hand off of whackfd */
2877 cr
->b
.failure_ok
= FALSE
;
2878 cr
->b
.step
= next_step
;
2881 ; sr
!=NULL
&& !sameaddr(&sr
->this.host_addr
, &b
->our_client
)
2888 /* If a %hold shunt has replaced the eroute for this template,
2892 && sr
->routing
== RT_ROUTED_PROSPECTIVE
&& eclipsable(sr
))
2894 sr
->routing
= RT_ROUTED_ECLIPSED
;
2898 /* Switch to issue next query.
2899 * A case may turn out to be unnecessary. If so, it falls
2900 * through to the next case.
2901 * Figuring out what %myid can stand for must be done before
2902 * our client credentials are looked up: we must know what
2903 * the client credentials may use to identify us.
2904 * On the other hand, our own credentials should be looked
2905 * up after our clients in case our credentials are not
2907 * XXX this is a wasted effort if we don't have credentials
2908 * BUT they are not needed.
2912 case fos_myid_ip_txt
:
2913 if (c
->spd
.this.id
->get_type(c
->spd
.this.id
) == ID_MYID
2914 && myid_state
!= MYID_SPECIFIED
)
2916 cr
->b
.failure_ok
= TRUE
;
2917 cr
->b
.want
= b
->want
= "TXT record for IP address as %myid";
2918 ugh
= start_adns_query(myids
[MYID_IP
], myids
[MYID_IP
],
2919 T_TXT
, continue_oppo
, &cr
->ac
);
2922 cr
->b
.step
= fos_myid_hostname_txt
;
2925 case fos_myid_hostname_txt
:
2926 if (c
->spd
.this.id
->get_type(c
->spd
.this.id
) == ID_MYID
2927 && myid_state
!= MYID_SPECIFIED
)
2930 cr
->b
.failure_ok
= TRUE
;
2932 cr
->b
.failure_ok
= FALSE
;
2934 cr
->b
.want
= b
->want
= "TXT record for hostname as %myid";
2935 ugh
= start_adns_query(myids
[MYID_HOSTNAME
],
2936 myids
[MYID_HOSTNAME
],
2937 T_TXT
, continue_oppo
, &cr
->ac
);
2942 cr
->b
.step
= fos_myid_ip_key
;
2945 case fos_myid_ip_key
:
2946 if (c
->spd
.this.id
.kind
== ID_MYID
2947 && myid_state
!= MYID_SPECIFIED
)
2949 cr
->b
.failure_ok
= TRUE
;
2950 cr
->b
.want
= b
->want
= "KEY record for IP address as %myid (no good TXT)";
2951 ugh
= start_adns_query(myids
[MYID_IP
], NULL
, /* security gateway meaningless */
2952 T_KEY
, continue_oppo
, &cr
->ac
);
2955 cr
->b
.step
= fos_myid_hostname_key
;
2958 case fos_myid_hostname_key
:
2959 if (c
->spd
.this.id
.kind
== ID_MYID
2960 && myid_state
!= MYID_SPECIFIED
)
2962 cr
->b
.failure_ok
= FALSE
; /* last attempt! */
2963 cr
->b
.want
= b
->want
= "KEY record for hostname as %myid (no good TXT)";
2964 ugh
= start_adns_query(myids
[MYID_HOSTNAME
], NULL
, /* security gateway meaningless */
2965 T_KEY
, continue_oppo
, &cr
->ac
);
2969 cr
->b
.step
= fos_our_client
;
2972 case fos_our_client
: /* TXT for our client */
2973 if (!sameaddr(&c
->spd
.this.host_addr
, &b
->our_client
))
2975 /* Check that at least one TXT(reverse(b->our_client)) is workable.
2976 * Note: {unshare|free}_id_content not needed for id: ephemeral.
2978 cr
->b
.want
= b
->want
= "our client's TXT record";
2979 id
= identification_create_from_sockaddr((sockaddr_t
*)&b
->our_client
);
2980 ugh
= start_adns_query(id
, c
->spd
.this.id
, /* we are the security gateway */
2981 T_TXT
, continue_oppo
, &cr
->ac
);
2985 cr
->b
.step
= fos_our_txt
;
2988 case fos_our_txt
: /* TXT for us */
2989 cr
->b
.failure_ok
= b
->failure_ok
= TRUE
;
2990 cr
->b
.want
= b
->want
= "our TXT record";
2991 ugh
= start_adns_query(sr
->this.id
, sr
->this.id
, /* we are the security gateway */
2992 T_TXT
, continue_oppo
, &cr
->ac
);
2996 case fos_our_key
: /* KEY for us */
2997 cr
->b
.want
= b
->want
= "our KEY record";
2998 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2999 ugh
= start_adns_query(sr
->this.id
, NULL
, /* security gateway meaningless */
3000 T_KEY
, continue_oppo
, &cr
->ac
);
3002 #endif /* USE_KEYRR */
3004 case fos_his_client
: /* TXT for his client */
3005 /* note: {unshare|free}_id_content not needed for id: ephemeral */
3006 cr
->b
.want
= b
->want
= "target's TXT record";
3007 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
3008 id
= identification_create_from_sockaddr((sockaddr_t
*)&b
->peer_client
);
3009 ugh
= start_adns_query(id
, NULL
, /* security gateway unconstrained */
3010 T_TXT
, continue_oppo
, &cr
->ac
);
3015 bad_case(next_step
);
3019 b
->whackfd
= NULL_FD
; /* complete hand-off */
3021 cannot_oppo(c
, b
, ugh
);
3024 close_any(b
->whackfd
);
3027 void terminate_connection(const char *nm
)
3029 /* Loop because more than one may match (master and instances)
3030 * But at least one is required (enforced by con_by_name).
3032 connection_t
*c
= con_by_name(nm
, TRUE
);
3034 if (c
== NULL
|| !c
->ikev1
)
3039 connection_t
*n
= c
->ac_next
; /* grab this before c might disappear */
3041 if (streq(c
->name
, nm
)
3042 && c
->kind
>= CK_PERMANENT
3043 && !NEVER_NEGOTIATE(c
->policy
))
3045 set_cur_connection(c
);
3046 plog("terminating SAs using this connection");
3047 c
->policy
&= ~POLICY_UP
;
3048 flush_pending_by_connection(c
);
3049 delete_states_by_connection(c
, FALSE
);
3050 if (c
->kind
== CK_INSTANCE
)
3051 delete_connection(c
, FALSE
);
3052 reset_cur_connection();
3058 /* an ISAKMP SA has been established.
3059 * Note the serial number, and release any connections with
3060 * the same peer ID but different peer IP address.
3062 bool uniqueIDs
= FALSE
; /* --uniqueids? */
3064 void ISAKMP_SA_established(connection_t
*c
, so_serial_t serial
)
3066 c
->newest_isakmp_sa
= serial
;
3068 /* the connection is now oriented so that we are able to determine
3069 * whether we are a mode config server with a virtual IP to send.
3071 if (!isanyaddr(&c
->spd
.that
.host_srcip
) && !c
->spd
.that
.has_natip
)
3073 c
->spd
.that
.modecfg
= TRUE
;
3078 /* for all connections: if the same Phase 1 IDs are used
3079 * for a different IP address, unorient that connection.
3083 for (d
= connections
; d
!= NULL
; )
3085 connection_t
*next
= d
->ac_next
; /* might move underneath us */
3087 if (d
->kind
>= CK_PERMANENT
&&
3088 c
->spd
.this.id
->equals(c
->spd
.this.id
, d
->spd
.this.id
) &&
3089 c
->spd
.that
.id
->equals(c
->spd
.that
.id
, d
->spd
.that
.id
) &&
3090 !sameaddr(&c
->spd
.that
.host_addr
, &d
->spd
.that
.host_addr
))
3092 release_connection(d
, FALSE
);
3099 /* Find the connection to connection c's peer's client with the
3100 * largest value of .routing. All other things being equal,
3101 * preference is given to c. If none is routed, return NULL.
3103 * If erop is non-null, set *erop to a connection sharing both
3104 * our client subnet and peer's client subnet with the largest value
3105 * of .routing. If none is erouted, set *erop to NULL.
3107 * The return value is used to find other connections sharing a route.
3108 * *erop is used to find other connections sharing an eroute.
3110 connection_t
*route_owner(connection_t
*c
, struct spd_route
**srp
,
3111 connection_t
**erop
, struct spd_route
**esrp
)