1 /* information about connections between hosts and clients
2 * Copyright (C) 1998-2002 D. Hugh Redelmeier.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * RCSID $Id: connections.c,v 1.43 2006/04/29 18:16:02 as Exp $
22 #include <netinet/in.h>
23 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
29 #include <sys/queue.h>
32 #include <ipsec_policy.h>
33 #include "kameipsec.h"
35 #include "constants.h"
44 #include "smartcard.h"
46 #include "connections.h"
47 #include "foodgroups.h"
51 #include "ipsec_doi.h" /* needs demux.h and state.h */
56 #include "adns.h" /* needs <resolv.h> */
57 #include "dnskey.h" /* needs keys.h and adns.h */
61 #include "nat_traversal.h"
64 static void flush_pending_by_connection(struct connection
*c
); /* forward */
66 static struct connection
*connections
= NULL
;
68 /* struct host_pair: a nexus of information about a pair of hosts.
69 * A host is an IP address, UDP port pair. This is a debatable choice:
70 * - should port be considered (no choice of port in standard)?
71 * - should ID be considered (hard because not always known)?
72 * - should IP address matter on our end (we don't know our end)?
73 * Only oriented connections are registered.
74 * Unoriented connections are kept on the unoriented_connections
75 * linked list (using hp_next). For them, host_pair is NULL.
81 u_int16_t port
; /* host order */
83 bool initial_connection_sent
;
84 struct connection
*connections
; /* connections with this pair */
85 struct pending
*pending
; /* awaiting Keying Channel */
86 struct host_pair
*next
;
89 static struct host_pair
*host_pairs
= NULL
;
91 static struct connection
*unoriented_connections
= NULL
;
93 /* check to see that Ids of peers match */
95 same_peer_ids(const struct connection
*c
, const struct connection
*d
96 , const struct id
*his_id
)
98 return same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
99 && same_id(his_id
== NULL?
&c
->spd
.that
.id
: his_id
, &d
->spd
.that
.id
);
102 static struct host_pair
*
103 find_host_pair(const ip_address
*myaddr
, u_int16_t myport
104 , const ip_address
*hisaddr
, u_int16_t hisport
)
106 struct host_pair
*p
, *prev
;
108 /* default hisaddr to an appropriate any */
110 hisaddr
= aftoinfo(addrtypeof(myaddr
))->any
;
112 if (nat_traversal_enabled
)
115 * port is not relevant in host_pair. with nat_traversal we
116 * always use pluto_port (500)
119 hisport
= pluto_port
;
122 for (prev
= NULL
, p
= host_pairs
; p
!= NULL
; prev
= p
, p
= p
->next
)
124 if (sameaddr(&p
->me
.addr
, myaddr
) && p
->me
.port
== myport
125 && sameaddr(&p
->him
.addr
, hisaddr
) && p
->him
.port
== hisport
)
129 prev
->next
= p
->next
; /* remove p from list */
130 p
->next
= host_pairs
; /* and stick it on front */
139 /* find head of list of connections with this pair of hosts */
140 static struct connection
*
141 find_host_pair_connections(const ip_address
*myaddr
, u_int16_t myport
142 , const ip_address
*hisaddr
, u_int16_t hisport
)
144 struct host_pair
*hp
= find_host_pair(myaddr
, myport
, hisaddr
, hisport
);
146 if (nat_traversal_enabled
&& hp
&& hisaddr
)
148 struct connection
*c
;
150 for (c
= hp
->connections
; c
!= NULL
; c
= c
->hp_next
)
152 if (c
->spd
.this.host_port
== myport
&& c
->spd
.that
.host_port
== hisport
)
157 return hp
== NULL? NULL
: hp
->connections
;
161 connect_to_host_pair(struct connection
*c
)
165 struct host_pair
*hp
= find_host_pair(&c
->spd
.this.host_addr
, c
->spd
.this.host_port
166 , &c
->spd
.that
.host_addr
, c
->spd
.that
.host_port
);
170 /* no suitable host_pair -- build one */
171 hp
= alloc_thing(struct host_pair
, "host_pair");
172 hp
->me
.addr
= c
->spd
.this.host_addr
;
173 hp
->him
.addr
= c
->spd
.that
.host_addr
;
174 hp
->me
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.this.host_port
;
175 hp
->him
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.that
.host_port
;
176 hp
->initial_connection_sent
= FALSE
;
177 hp
->connections
= NULL
;
179 hp
->next
= host_pairs
;
183 c
->hp_next
= hp
->connections
;
188 /* since this connection isn't oriented, we place it
189 * in the unoriented_connections list instead.
192 c
->hp_next
= unoriented_connections
;
193 unoriented_connections
= c
;
197 /* find a connection by name.
198 * If strict, don't accept a CK_INSTANCE.
199 * Move the winner (if any) to the front.
200 * If none is found, and strict, a diagnostic is logged to whack.
203 con_by_name(const char *nm
, bool strict
)
205 struct connection
*p
, *prev
;
207 for (prev
= NULL
, p
= connections
; ; prev
= p
, p
= p
->ac_next
)
212 whack_log(RC_UNKNOWN_NAME
213 , "no connection named \"%s\"", nm
);
216 if (streq(p
->name
, nm
)
217 && (!strict
|| p
->kind
!= CK_INSTANCE
))
221 prev
->ac_next
= p
->ac_next
; /* remove p from list */
222 p
->ac_next
= connections
; /* and stick it on front */
232 release_connection(struct connection
*c
, bool relations
)
234 if (c
->kind
== CK_INSTANCE
)
236 /* This does everything we need.
237 * Note that we will be called recursively by delete_connection,
238 * but kind will be CK_GOING_AWAY.
240 delete_connection(c
, relations
);
244 flush_pending_by_connection(c
);
245 delete_states_by_connection(c
, relations
);
246 unroute_connection(c
);
250 /* Delete a connection */
252 #define list_rm(etype, enext, e, ehead) { \
254 for (ep = &(ehead); *ep != (e); ep = &(*ep)->enext) \
255 passert(*ep != NULL); /* we must not come up empty-handed */ \
261 delete_connection(struct connection
*c
, bool relations
)
263 struct connection
*old_cur_connection
264 = cur_connection
== c? NULL
: cur_connection
;
266 lset_t old_cur_debugging
= cur_debugging
;
269 set_cur_connection(c
);
271 /* Must be careful to avoid circularity:
272 * we mark c as going away so it won't get deleted recursively.
274 passert(c
->kind
!= CK_GOING_AWAY
);
275 if (c
->kind
== CK_INSTANCE
)
277 plog("deleting connection \"%s\" instance with peer %s {isakmp=#%lu/ipsec=#%lu}"
279 , ip_str(&c
->spd
.that
.host_addr
)
280 , c
->newest_isakmp_sa
, c
->newest_ipsec_sa
);
281 c
->kind
= CK_GOING_AWAY
;
285 plog("deleting connection");
287 release_connection(c
, relations
); /* won't delete c */
289 if (c
->kind
== CK_GROUP
)
292 /* free up any logging resources */
295 /* find and delete c from connections list */
296 list_rm(struct connection
, ac_next
, c
, connections
);
297 cur_connection
= old_cur_connection
;
299 /* find and delete c from the host pair list */
300 if (c
->host_pair
== NULL
)
303 list_rm(struct connection
, hp_next
, c
, unoriented_connections
);
307 struct host_pair
*hp
= c
->host_pair
;
309 list_rm(struct connection
, hp_next
, c
, hp
->connections
);
310 c
->host_pair
= NULL
; /* redundant, but safe */
312 /* if there are no more connections with this host_pair
313 * and we haven't even made an initial contact, let's delete
314 * this guy in case we were created by an attempted DOS attack.
316 if (hp
->connections
== NULL
317 && !hp
->initial_connection_sent
)
319 passert(hp
->pending
== NULL
); /* ??? must deal with this! */
320 list_rm(struct host_pair
, next
, hp
, host_pairs
);
325 if (c
->kind
!= CK_GOING_AWAY
)
326 pfreeany(c
->spd
.that
.virt
);
329 cur_debugging
= old_cur_debugging
;
332 free_id_content(&c
->spd
.this.id
);
333 pfreeany(c
->spd
.this.updown
);
334 freeanychunk(c
->spd
.this.ca
);
335 free_ietfAttrList(c
->spd
.this.groups
);
336 free_id_content(&c
->spd
.that
.id
);
337 pfreeany(c
->spd
.that
.updown
);
338 freeanychunk(c
->spd
.that
.ca
);
339 free_ietfAttrList(c
->spd
.that
.groups
);
340 free_generalNames(c
->requested_ca
, TRUE
);
341 gw_delref(&c
->gw_info
);
343 lock_certs_and_keys("delete_connection");
344 release_cert(c
->spd
.this.cert
);
345 scx_release(c
->spd
.this.sc
);
346 release_cert(c
->spd
.that
.cert
);
347 scx_release(c
->spd
.that
.sc
);
348 unlock_certs_and_keys("delete_connection");
350 alg_info_delref((struct alg_info
**)&c
->alg_info_esp
);
351 alg_info_delref((struct alg_info
**)&c
->alg_info_ike
);
356 /* Delete connections with the specified name */
358 delete_connections_by_name(const char *name
, bool strict
)
360 struct connection
*c
= con_by_name(name
, strict
);
362 for (; c
!= NULL
; c
= con_by_name(name
, FALSE
))
363 delete_connection(c
, FALSE
);
367 delete_every_connection(void)
369 while (connections
!= NULL
)
370 delete_connection(connections
, TRUE
);
374 release_dead_interfaces(void)
376 struct host_pair
*hp
;
378 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
380 struct connection
**pp
383 for (pp
= &hp
->connections
; (p
= *pp
) != NULL
; )
385 if (p
->interface
->change
== IFN_DELETE
)
387 /* this connection's interface is going away */
388 enum connection_kind k
= p
->kind
;
390 release_connection(p
, TRUE
);
392 if (k
<= CK_PERMANENT
)
394 /* The connection should have survived release:
395 * move it to the unoriented_connections list.
401 *pp
= p
->hp_next
; /* advance *pp */
403 p
->hp_next
= unoriented_connections
;
404 unoriented_connections
= p
;
408 /* The connection should have vanished,
409 * but the previous connection remains.
416 pp
= &p
->hp_next
; /* advance pp */
422 /* adjust orientations of connections to reflect newly added interfaces */
424 check_orientations(void)
426 /* try to orient all the unoriented connections */
428 struct connection
*c
= unoriented_connections
;
430 unoriented_connections
= NULL
;
434 struct connection
*nxt
= c
->hp_next
;
437 connect_to_host_pair(c
);
442 /* Check that no oriented connection has become double-oriented.
443 * In other words, the far side must not match one of our new interfaces.
448 for (i
= interfaces
; i
!= NULL
; i
= i
->next
)
450 if (i
->change
== IFN_ADD
)
452 struct host_pair
*hp
;
454 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
456 if (sameaddr(&hp
->him
.addr
, &i
->addr
)
457 && (!no_klips
|| hp
->him
.port
== pluto_port
))
459 /* bad news: the whole chain of connections
460 * hanging off this host pair has both sides
461 * matching an interface.
462 * We'll get rid of them, using orient and
463 * connect_to_host_pair. But we'll be lazy
464 * and not ditch the host_pair itself (the
465 * cost of leaving it is slight and cannot
466 * be induced by a foe).
468 struct connection
*c
= hp
->connections
;
470 hp
->connections
= NULL
;
473 struct connection
*nxt
= c
->hp_next
;
477 connect_to_host_pair(c
);
488 default_end(struct end
*e
, ip_address
*dflt_nexthop
)
491 const struct af_info
*afi
= aftoinfo(addrtypeof(&e
->host_addr
));
494 return "unknown address family in default_end";
496 /* default ID to IP (but only if not NO_IP -- WildCard) */
497 if (e
->id
.kind
== ID_NONE
&& !isanyaddr(&e
->host_addr
))
499 e
->id
.kind
= afi
->id_addr
;
500 e
->id
.ip_addr
= e
->host_addr
;
501 e
->has_id_wildcards
= FALSE
;
504 /* default nexthop to other side */
505 if (isanyaddr(&e
->host_nexthop
))
506 e
->host_nexthop
= *dflt_nexthop
;
508 /* default client to subnet containing only self
509 * XXX This may mean that the client's address family doesn't match
510 * tunnel_addr_family.
513 ugh
= addrtosubnet(&e
->host_addr
, &e
->client
);
518 /* Format the topology of a connection end, leaving out defaults.
519 * Largest left end looks like: client === host : port [ host_id ] --- hop
520 * Note: if that==NULL, skip nexthop
521 * Returns strlen of formated result (length excludes NUL at end).
526 , const struct end
*this
527 , const struct end
*that
531 char client
[SUBNETTOT_BUF
];
532 const char *client_sep
= "";
533 char protoport
[sizeof(":255/65535")];
534 const char *host
= NULL
;
535 char host_space
[ADDRTOT_BUF
];
536 char host_port
[sizeof(":65535")];
537 char host_id
[BUF_LEN
+ 2];
538 char hop
[ADDRTOT_BUF
];
539 const char *hop_sep
= "";
540 const char *open_brackets
= "";
541 const char *close_brackets
= "";
543 if (isanyaddr(&this->host_addr
))
545 switch (policy
& (POLICY_GROUP
| POLICY_OPPO
))
551 host
= "%opportunistic";
553 case POLICY_GROUP
| POLICY_OPPO
:
554 host
= "%opportunisticgroup";
564 if (is_virtual_end(this) && isanyaddr(&this->host_addr
))
570 if (this->has_client
)
572 ip_address client_net
, client_mask
;
574 networkof(&this->client
, &client_net
);
575 maskof(&this->client
, &client_mask
);
578 /* {client_subnet_wildcard} */
579 if (this->has_client_wildcard
)
582 close_brackets
= "}";
585 if (isanyaddr(&client_net
) && isanyaddr(&client_mask
)
586 && (policy
& (POLICY_GROUP
| POLICY_OPPO
)))
587 client_sep
= ""; /* boring case */
588 else if (subnetisnone(&this->client
))
591 subnettot(&this->client
, 0, client
, sizeof(client
));
593 else if (this->modecfg
&& isanyaddr(&this->host_srcip
))
595 /* we are mode config client */
597 strcpy(client
, "%modecfg");
603 addrtot(&this->host_addr
, 0, host_space
, sizeof(host_space
));
608 if (this->host_port
!= IKE_UDP_PORT
)
609 snprintf(host_port
, sizeof(host_port
), ":%u"
612 /* payload portocol and port */
614 if (this->has_port_wildcard
)
615 snprintf(protoport
, sizeof(protoport
), ":%u/%%any", this->protocol
);
616 else if (this->port
|| this->protocol
)
617 snprintf(protoport
, sizeof(protoport
), ":%u/%u", this->protocol
620 /* id, if different from host */
622 if (this->id
.kind
== ID_MYID
)
624 strcpy(host_id
, "[%myid]");
626 else if (!(this->id
.kind
== ID_NONE
627 || (id_is_ipaddr(&this->id
) && sameaddr(&this->id
.ip_addr
, &this->host_addr
))))
629 int len
= idtoa(&this->id
, host_id
+1, sizeof(host_id
)-2);
632 strcpy(&host_id
[len
< 0?
(ptrdiff_t)sizeof(host_id
)-2 : 1 + len
], "]");
636 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s"
637 , open_brackets
, client
, close_brackets
, client_sep
638 , host
, host_port
, host_id
, protoport
);
640 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s"
641 , host
, host_port
, host_id
, protoport
, client_sep
642 , open_brackets
, client
, close_brackets
);
646 /* format topology of a connection.
647 * Two symmetric ends separated by ...
649 #define CONNECTION_BUF (2 * (END_BUF - 1) + 4)
652 format_connection(char *buf
, size_t buf_len
653 , const struct connection
*c
654 , struct spd_route
*sr
)
656 size_t w
= format_end(buf
, buf_len
, &sr
->this, &sr
->that
, TRUE
, LEMPTY
);
658 w
+= snprintf(buf
+ w
, buf_len
- w
, "...");
659 return w
+ format_end(buf
+ w
, buf_len
- w
, &sr
->that
, &sr
->this, FALSE
, c
->policy
);
663 unshare_connection_strings(struct connection
*c
)
665 c
->name
= clone_str(c
->name
, "connection name");
667 unshare_id_content(&c
->spd
.this.id
);
668 c
->spd
.this.updown
= clone_str(c
->spd
.this.updown
, "updown");
669 scx_share(c
->spd
.this.sc
);
670 share_cert(c
->spd
.this.cert
);
671 if (c
->spd
.this.ca
.ptr
!= NULL
)
672 clonetochunk(c
->spd
.this.ca
, c
->spd
.this.ca
.ptr
, c
->spd
.this.ca
.len
, "ca string");
674 unshare_id_content(&c
->spd
.that
.id
);
675 c
->spd
.that
.updown
= clone_str(c
->spd
.that
.updown
, "updown");
676 scx_share(c
->spd
.that
.sc
);
677 share_cert(c
->spd
.that
.cert
);
678 if (c
->spd
.that
.ca
.ptr
!= NULL
)
679 clonetochunk(c
->spd
.that
.ca
, c
->spd
.that
.ca
.ptr
, c
->spd
.that
.ca
.len
, "ca string");
681 /* increment references to algo's */
682 alg_info_addref((struct alg_info
*)c
->alg_info_esp
);
683 alg_info_addref((struct alg_info
*)c
->alg_info_ike
);
687 load_end_certificate(const char *filename
, struct end
*dst
)
691 bool valid_cert
= FALSE
;
692 bool cached_cert
= FALSE
;
694 /* initialize end certificate */
695 dst
->cert
.type
= CERT_NONE
;
696 dst
->cert
.u
.x509
= NULL
;
698 /* initialize smartcard info record */
701 if (filename
!= NULL
)
703 if (scx_on_smartcard(filename
))
705 /* load cert from smartcard */
706 valid_cert
= scx_load_cert(filename
, &dst
->sc
, &cert
, &cached_cert
);
710 /* load cert from file */
711 valid_cert
= load_host_cert(filename
, &cert
);
722 select_pgpcert_id(cert
.u
.pgp
, &dst
->id
);
728 valid_until
= cert
.u
.pgp
->until
;
729 add_pgp_public_key(cert
.u
.pgp
, cert
.u
.pgp
->until
, DAL_LOCAL
);
730 dst
->cert
.type
= cert
.type
;
731 dst
->cert
.u
.pgp
= add_pgpcert(cert
.u
.pgp
);
734 case CERT_X509_SIGNATURE
:
735 select_x509cert_id(cert
.u
.x509
, &dst
->id
);
741 /* check validity of cert */
742 valid_until
= cert
.u
.x509
->notAfter
;
743 ugh
= check_validity(cert
.u
.x509
, &valid_until
);
747 free_x509cert(cert
.u
.x509
);
752 DBG_log("certificate is valid")
754 add_x509_public_key(cert
.u
.x509
, valid_until
, DAL_LOCAL
);
755 dst
->cert
.type
= cert
.type
;
756 dst
->cert
.u
.x509
= add_x509cert(cert
.u
.x509
);
758 /* if no CA is defined, use issuer as default */
759 if (dst
->ca
.ptr
== NULL
)
760 dst
->ca
= dst
->cert
.u
.x509
->issuer
;
766 /* cache the certificate that was last retrieved from the smartcard */
769 if (!same_cert(&dst
->sc
->last_cert
, &dst
->cert
))
771 lock_certs_and_keys("load_end_certificates");
772 release_cert(dst
->sc
->last_cert
);
773 dst
->sc
->last_cert
= dst
->cert
;
774 share_cert(dst
->cert
);
775 unlock_certs_and_keys("load_end_certificates");
777 time(&dst
->sc
->last_load
);
783 extract_end(struct end
*dst
, const whack_end_t
*src
, const char *which
)
785 bool same_ca
= FALSE
;
787 /* decode id, if any */
790 dst
->id
.kind
= ID_NONE
;
794 err_t ugh
= atoid(src
->id
, &dst
->id
, TRUE
);
798 loglog(RC_BADID
, "bad %s --id: %s (ignored)", which
, ugh
);
799 dst
->id
= empty_id
; /* ignore bad one */
803 dst
->ca
= empty_chunk
;
805 /* decode CA distinguished name, if any */
808 if streq(src
->ca
, "%same")
810 else if (!streq(src
->ca
, "%any"))
814 dst
->ca
.ptr
= temporary_cyclic_buffer();
815 ugh
= atodn(src
->ca
, &dst
->ca
);
818 plog("bad CA string '%s': %s (ignored)", src
->ca
, ugh
);
819 dst
->ca
= empty_chunk
;
824 /* load local end certificate and extract ID, if any */
825 load_end_certificate(src
->cert
, dst
);
827 /* does id has wildcards? */
828 dst
->has_id_wildcards
= id_count_wildcards(&dst
->id
) > 0;
830 /* decode group attributes, if any */
831 decode_groups(src
->groups
, &dst
->groups
);
833 /* the rest is simple copying of corresponding fields */
834 dst
->host_addr
= src
->host_addr
;
835 dst
->host_nexthop
= src
->host_nexthop
;
836 dst
->host_srcip
= src
->host_srcip
;
837 dst
->has_natip
= src
->has_natip
;
838 dst
->client
= src
->client
;
839 dst
->protocol
= src
->protocol
;
840 dst
->port
= src
->port
;
841 dst
->has_port_wildcard
= src
->has_port_wildcard
;
842 dst
->key_from_DNS_on_demand
= src
->key_from_DNS_on_demand
;
843 dst
->has_client
= src
->has_client
;
844 dst
->has_client_wildcard
= src
->has_client_wildcard
;
845 dst
->modecfg
= src
->modecfg
;
846 dst
->hostaccess
= src
->hostaccess
;
847 dst
->sendcert
= src
->sendcert
;
848 dst
->updown
= src
->updown
;
849 dst
->host_port
= src
->host_port
;
851 /* if host sourceip is defined but no client is present
852 * behind the host then set client to sourceip/32
854 if (addrbytesptr(&dst
->host_srcip
, NULL
)
855 && !isanyaddr(&dst
->host_srcip
)
859 err_t ugh
= addrtosubnet(&dst
->host_srcip
, &dst
->client
);
862 plog("could not assign host sourceip to client subnet");
864 dst
->has_client
= TRUE
;
870 check_connection_end(const whack_end_t
*this, const whack_end_t
*that
871 , const whack_message_t
*wm
)
873 if (wm
->addr_family
!= addrtypeof(&this->host_addr
)
874 || wm
->addr_family
!= addrtypeof(&this->host_nexthop
)
875 || (this->has_client? wm
->tunnel_addr_family
: wm
->addr_family
)
876 != subnettypeof(&this->client
)
877 || subnettypeof(&this->client
) != subnettypeof(&that
->client
))
879 /* this should have been diagnosed by whack, so we need not be clear
880 * !!! overloaded use of RC_CLASH
882 loglog(RC_CLASH
, "address family inconsistency in connection");
886 if (isanyaddr(&that
->host_addr
))
888 /* other side is wildcard: we must check if other conditions met */
889 if (isanyaddr(&this->host_addr
))
891 loglog(RC_ORIENT
, "connection must specify host IP address for our side");
896 if (this->virt
&& (!isanyaddr(&this->host_addr
) || this->has_client
))
899 "virtual IP must only be used with %%any and without client");
903 return TRUE
; /* happy */
907 find_connection_by_reqid(uint32_t reqid
)
909 struct connection
*c
;
912 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
914 if (c
->spd
.reqid
== reqid
)
925 static uint32_t reqid
= IPSEC_MANUAL_REQID_MAX
& ~3;
931 reqid
= (IPSEC_MANUAL_REQID_MAX
& ~3) + 4;
932 if (!find_connection_by_reqid(reqid
))
934 } while (reqid
!= start
);
936 exit_log("unable to allocate reqid");
940 add_connection(const whack_message_t
*wm
)
942 if (con_by_name(wm
->name
, FALSE
) != NULL
)
944 loglog(RC_DUPNAME
, "attempt to redefine connection \"%s\"", wm
->name
);
946 else if (wm
->right
.protocol
!= wm
->left
.protocol
)
948 /* this should haven been diagnosed by whack
949 * !!! overloaded use of RC_CLASH
951 loglog(RC_CLASH
, "the protocol must be the same for leftport and rightport");
953 else if (check_connection_end(&wm
->right
, &wm
->left
, wm
)
954 && check_connection_end(&wm
->left
, &wm
->right
, wm
))
956 bool same_rightca
, same_leftca
;
957 struct connection
*c
= alloc_thing(struct connection
, "struct connection");
960 c
->ikev1
= wm
->ikev1
;
961 c
->policy
= wm
->policy
;
963 if ((c
->policy
& POLICY_COMPRESS
) && !can_do_IPcomp
)
965 , "ignoring --compress in \"%s\" because KLIPS is not configured to do IPCOMP"
973 DBG_log("from whack: got --esp=%s", wm
->esp ? wm
->esp
: "NULL")
975 c
->alg_info_esp
= alg_info_esp_create_from_str(wm
->esp? wm
->esp
: "", &ugh
);
977 DBG(DBG_CRYPT
|DBG_CONTROL
,
978 static char buf
[256]="<NULL>";
981 alg_info_snprint(buf
, sizeof(buf
)
982 ,(struct alg_info
*)c
->alg_info_esp
);
983 DBG_log("esp string values: %s", buf
);
987 if (c
->alg_info_esp
->alg_info_cnt
==0)
988 loglog(RC_LOG_SERIOUS
989 , "got 0 transforms for esp=\"%s\"", wm
->esp
);
993 loglog(RC_LOG_SERIOUS
994 , "esp string error: %s", ugh? ugh
: "Unknown");
1003 DBG_log("from whack: got --ike=%s", wm
->ike ? wm
->ike
: "NULL")
1005 c
->alg_info_ike
= alg_info_ike_create_from_str(wm
->ike? wm
->ike
: "", &ugh
);
1007 DBG(DBG_CRYPT
|DBG_CONTROL
,
1008 static char buf
[256]="<NULL>";
1010 if (c
->alg_info_ike
)
1011 alg_info_snprint(buf
, sizeof(buf
)
1012 , (struct alg_info
*)c
->alg_info_ike
);
1013 DBG_log("ike string values: %s", buf
);
1015 if (c
->alg_info_ike
)
1017 if (c
->alg_info_ike
->alg_info_cnt
==0)
1018 loglog(RC_LOG_SERIOUS
1019 , "got 0 transforms for ike=\"%s\"", wm
->ike
);
1023 loglog(RC_LOG_SERIOUS
1024 , "ike string error: %s", ugh? ugh
: "Unknown");
1028 c
->sa_ike_life_seconds
= wm
->sa_ike_life_seconds
;
1029 c
->sa_ipsec_life_seconds
= wm
->sa_ipsec_life_seconds
;
1030 c
->sa_rekey_margin
= wm
->sa_rekey_margin
;
1031 c
->sa_rekey_fuzz
= wm
->sa_rekey_fuzz
;
1032 c
->sa_keying_tries
= wm
->sa_keying_tries
;
1035 c
->dpd_delay
= wm
->dpd_delay
;
1036 c
->dpd_timeout
= wm
->dpd_timeout
;
1037 c
->dpd_action
= wm
->dpd_action
;
1039 c
->addr_family
= wm
->addr_family
;
1040 c
->tunnel_addr_family
= wm
->tunnel_addr_family
;
1042 c
->requested_ca
= NULL
;
1044 same_leftca
= extract_end(&c
->spd
.this, &wm
->left
, "left");
1045 same_rightca
= extract_end(&c
->spd
.that
, &wm
->right
, "right");
1048 c
->spd
.that
.ca
= c
->spd
.this.ca
;
1049 else if (same_leftca
)
1050 c
->spd
.this.ca
= c
->spd
.that
.ca
;
1052 default_end(&c
->spd
.this, &c
->spd
.that
.host_addr
);
1053 default_end(&c
->spd
.that
, &c
->spd
.this.host_addr
);
1055 /* force any wildcard host IP address, any wildcard subnet
1056 * or any wildcard ID to that end
1058 if (isanyaddr(&c
->spd
.this.host_addr
) || c
->spd
.this.has_client_wildcard
1059 || c
->spd
.this.has_port_wildcard
|| c
->spd
.this.has_id_wildcards
)
1061 struct end t
= c
->spd
.this;
1063 c
->spd
.this = c
->spd
.that
;
1068 c
->spd
.reqid
= gen_reqid();
1070 /* set internal fields */
1071 c
->instance_serial
= 0;
1072 c
->ac_next
= connections
;
1074 c
->interface
= NULL
;
1075 c
->spd
.routing
= RT_UNROUTED
;
1076 c
->newest_isakmp_sa
= SOS_NOBODY
;
1077 c
->newest_ipsec_sa
= SOS_NOBODY
;
1078 c
->spd
.eroute_owner
= SOS_NOBODY
;
1080 if (c
->policy
& POLICY_GROUP
)
1085 else if ((isanyaddr(&c
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(c
->policy
))
1086 || c
->spd
.that
.has_client_wildcard
|| c
->spd
.that
.has_port_wildcard
1087 || c
->spd
.that
.has_id_wildcards
)
1089 /* Opportunistic or Road Warrior or wildcard client subnet
1091 c
->kind
= CK_TEMPLATE
;
1095 c
->kind
= CK_PERMANENT
;
1097 set_policy_prio(c
); /* must be after kind is set */
1100 c
->extra_debugging
= wm
->debugging
;
1105 passert(!(wm
->left
.virt
&& wm
->right
.virt
));
1106 if (wm
->left
.virt
|| wm
->right
.virt
)
1108 passert(isanyaddr(&c
->spd
.that
.host_addr
));
1109 c
->spd
.that
.virt
= create_virtual(c
,
1110 wm
->left
.virt ? wm
->left
.virt
: wm
->right
.virt
);
1111 if (c
->spd
.that
.virt
)
1112 c
->spd
.that
.has_client
= TRUE
;
1115 unshare_connection_strings(c
);
1119 connect_to_host_pair(c
);
1121 /* log all about this connection */
1122 plog("added connection description \"%s\"", c
->name
);
1124 char topo
[CONNECTION_BUF
];
1126 (void) format_connection(topo
, sizeof(topo
), c
, &c
->spd
);
1128 DBG_log("%s", topo
);
1130 /* Make sure that address families can be correctly inferred
1131 * from printed ends.
1133 passert(c
->addr_family
== addrtypeof(&c
->spd
.this.host_addr
)
1134 && c
->addr_family
== addrtypeof(&c
->spd
.this.host_nexthop
)
1135 && (c
->spd
.this.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1136 == subnettypeof(&c
->spd
.this.client
)
1138 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_addr
)
1139 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_nexthop
)
1140 && (c
->spd
.that
.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1141 == subnettypeof(&c
->spd
.that
.client
));
1143 DBG_log("ike_life: %lus; ipsec_life: %lus; rekey_margin: %lus;"
1144 " rekey_fuzz: %lu%%; keyingtries: %lu; policy: %s"
1145 , (unsigned long) c
->sa_ike_life_seconds
1146 , (unsigned long) c
->sa_ipsec_life_seconds
1147 , (unsigned long) c
->sa_rekey_margin
1148 , (unsigned long) c
->sa_rekey_fuzz
1149 , (unsigned long) c
->sa_keying_tries
1150 , prettypolicy(c
->policy
));
1155 /* Derive a template connection from a group connection and target.
1156 * Similar to instantiate(). Happens at whack --listen.
1157 * Returns name of new connection. May be NULL.
1158 * Caller is responsible for pfreeing.
1161 add_group_instance(struct connection
*group
, const ip_subnet
*target
)
1164 , targetbuf
[SUBNETTOT_BUF
];
1165 struct connection
*t
;
1168 passert(group
->kind
== CK_GROUP
);
1169 passert(oriented(*group
));
1171 /* manufacture a unique name for this template */
1172 subnettot(target
, 0, targetbuf
, sizeof(targetbuf
));
1173 snprintf(namebuf
, sizeof(namebuf
), "%s#%s", group
->name
, targetbuf
);
1175 if (con_by_name(namebuf
, FALSE
) != NULL
)
1177 loglog(RC_DUPNAME
, "group name + target yields duplicate name \"%s\""
1182 t
= clone_thing(*group
, "group instance");
1184 unshare_connection_strings(t
);
1185 name
= clone_str(t
->name
, "group instance name");
1186 t
->spd
.that
.client
= *target
;
1187 t
->policy
&= ~(POLICY_GROUP
| POLICY_GROUTED
);
1188 t
->kind
= isanyaddr(&t
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(t
->policy
)
1189 ? CK_TEMPLATE
: CK_INSTANCE
;
1191 /* reset log file info */
1192 t
->log_file_name
= NULL
;
1194 t
->log_file_err
= FALSE
;
1196 t
->spd
.reqid
= gen_reqid();
1198 if (t
->spd
.that
.virt
)
1200 DBG_log("virtual_ip not supported in group instance");
1201 t
->spd
.that
.virt
= NULL
;
1204 /* add to connections list */
1205 t
->ac_next
= connections
;
1208 /* same host_pair as parent: stick after parent on list */
1211 /* route if group is routed */
1212 if (group
->policy
& POLICY_GROUTED
)
1214 if (!trap_connection(t
))
1215 whack_log(RC_ROUTE
, "could not route");
1221 /* an old target has disappeared for a group: delete instance */
1223 remove_group_instance(const struct connection
*group USED_BY_DEBUG
1226 passert(group
->kind
== CK_GROUP
);
1227 passert(oriented(*group
));
1229 delete_connections_by_name(name
, FALSE
);
1232 /* Common part of instantiating a Road Warrior or Opportunistic connection.
1233 * his_id can be used to carry over an ID discovered in Phase 1.
1234 * It must not disagree with the one in c, but if that is unspecified,
1235 * the new connection will use his_id.
1236 * If his_id is NULL, and c.that.id is uninstantiated (ID_NONE), the
1237 * new connection will continue to have an uninstantiated that.id.
1238 * Note: instantiation does not affect port numbers.
1240 * Note that instantiate can only deal with a single SPD/eroute.
1242 static struct connection
*
1243 instantiate(struct connection
*c
, const ip_address
*him
1244 , u_int16_t his_port
1245 , const struct id
*his_id
)
1247 struct connection
*d
;
1250 passert(c
->kind
== CK_TEMPLATE
);
1251 passert(c
->spd
.next
== NULL
);
1253 c
->instance_serial
++;
1254 d
= clone_thing(*c
, "temporary connection");
1257 passert(match_id(his_id
, &d
->spd
.that
.id
, &wildcards
));
1258 d
->spd
.that
.id
= *his_id
;
1259 d
->spd
.that
.has_id_wildcards
= FALSE
;
1261 unshare_connection_strings(d
);
1262 unshare_ietfAttrList(&d
->spd
.this.groups
);
1263 unshare_ietfAttrList(&d
->spd
.that
.groups
);
1264 d
->kind
= CK_INSTANCE
;
1266 passert(oriented(*d
));
1267 d
->spd
.that
.host_addr
= *him
;
1268 setportof(htons(c
->spd
.that
.port
), &d
->spd
.that
.host_addr
);
1270 if (his_port
) d
->spd
.that
.host_port
= his_port
;
1272 default_end(&d
->spd
.that
, &d
->spd
.this.host_addr
);
1274 /* We cannot guess what our next_hop should be, but if it was
1275 * explicitly specified as 0.0.0.0, we set it to be him.
1276 * (whack will not allow nexthop to be elided in RW case.)
1278 default_end(&d
->spd
.this, &d
->spd
.that
.host_addr
);
1280 d
->spd
.reqid
= gen_reqid();
1282 /* set internal fields */
1283 d
->ac_next
= connections
;
1285 d
->spd
.routing
= RT_UNROUTED
;
1286 d
->newest_isakmp_sa
= SOS_NOBODY
;
1287 d
->newest_ipsec_sa
= SOS_NOBODY
;
1288 d
->spd
.eroute_owner
= SOS_NOBODY
;
1290 /* reset log file info */
1291 d
->log_file_name
= NULL
;
1293 d
->log_file_err
= FALSE
;
1295 connect_to_host_pair(d
);
1301 rw_instantiate(struct connection
*c
, const ip_address
*him
, u_int16_t his_port
1302 , const ip_subnet
*his_net
, const struct id
*his_id
)
1304 struct connection
*d
= instantiate(c
, him
, his_port
, his_id
);
1306 if (d
&& his_net
&& is_virtual_connection(c
))
1308 d
->spd
.that
.client
= *his_net
;
1309 d
->spd
.that
.virt
= NULL
;
1310 if (subnetishost(his_net
) && addrinsubnet(him
, his_net
))
1311 d
->spd
.that
.has_client
= FALSE
;
1314 if (d
->policy
& POLICY_OPPO
)
1316 /* This must be before we know the client addresses.
1317 * Fill in one that is impossible. This prevents anyone else from
1318 * trying to use this connection to get to a particular client
1320 d
->spd
.that
.client
= *aftoinfo(subnettypeof(&d
->spd
.that
.client
))->none
;
1323 , DBG_log("instantiated \"%s\" for %s" , d
->name
, ip_str(him
)));
1328 oppo_instantiate(struct connection
*c
1329 , const ip_address
*him
1330 , const struct id
*his_id
1331 , struct gw_info
*gw
1332 , const ip_address
*our_client USED_BY_DEBUG
1333 , const ip_address
*peer_client
)
1335 struct connection
*d
= instantiate(c
, him
, 0, his_id
);
1337 passert(d
->spd
.next
== NULL
);
1339 /* fill in our client side */
1340 if (d
->spd
.this.has_client
)
1342 /* there was a client in the abstract connection
1343 * so we demand that the required client is within that subnet.
1345 passert(addrinsubnet(our_client
, &d
->spd
.this.client
));
1346 happy(addrtosubnet(our_client
, &d
->spd
.this.client
));
1347 /* opportunistic connections do not use port selectors */
1348 setportof(0, &d
->spd
.this.client
.addr
);
1352 /* there was no client in the abstract connection
1353 * so we demand that the required client be the host
1355 passert(sameaddr(our_client
, &d
->spd
.this.host_addr
));
1358 /* fill in peer's client side.
1359 * If the client is the peer, excise the client from the connection.
1361 passert((d
->policy
& POLICY_OPPO
)
1362 && addrinsubnet(peer_client
, &d
->spd
.that
.client
));
1363 happy(addrtosubnet(peer_client
, &d
->spd
.that
.client
));
1364 /* opportunistic connections do not use port selectors */
1365 setportof(0, &d
->spd
.that
.client
.addr
);
1367 if (sameaddr(peer_client
, &d
->spd
.that
.host_addr
))
1368 d
->spd
.that
.has_client
= FALSE
;
1370 passert(d
->gw_info
== NULL
);
1374 /* Adjust routing if something is eclipsing c.
1375 * It must be a %hold for us (hard to passert this).
1376 * If there was another instance eclipsing, we'd be using it.
1378 if (c
->spd
.routing
== RT_ROUTED_ECLIPSED
)
1379 d
->spd
.routing
= RT_ROUTED_PROSPECTIVE
;
1381 /* Remember if the template is routed:
1382 * if so, this instance applies for initiation
1383 * even if it is created for responding.
1385 if (routed(c
->spd
.routing
))
1386 d
->instance_initiation_ok
= TRUE
;
1389 char topo
[CONNECTION_BUF
];
1391 (void) format_connection(topo
, sizeof(topo
), d
, &d
->spd
);
1392 DBG_log("instantiated \"%s\": %s", d
->name
, topo
);
1397 /* priority formatting */
1399 fmt_policy_prio(policy_prio_t pp
, char buf
[POLICY_PRIO_BUF
])
1401 if (pp
== BOTTOM_PRIO
)
1402 snprintf(buf
, POLICY_PRIO_BUF
, "0");
1404 snprintf(buf
, POLICY_PRIO_BUF
, "%lu,%lu"
1405 , pp
>>16, (pp
& ~(~(policy_prio_t
)0 << 16)) >> 8);
1408 /* Format any information needed to identify an instance of a connection.
1409 * Fills any needed information into buf which MUST be big enough.
1410 * Road Warrior: peer's IP address
1411 * Opportunistic: [" " myclient "==="] " ..." peer ["===" hisclient] '\0'
1414 fmt_client(const ip_subnet
*client
, const ip_address
*gw
, const char *prefix
, char buf
[ADDRTOT_BUF
])
1416 if (subnetisaddr(client
, gw
))
1418 buf
[0] = '\0'; /* compact denotation for "self" */
1424 strcpy(buf
, prefix
);
1425 ap
= buf
+ strlen(prefix
);
1426 if (subnetisnone(client
))
1427 strcpy(ap
, "?"); /* unknown */
1429 subnettot(client
, 0, ap
, SUBNETTOT_BUF
);
1435 fmt_conn_instance(const struct connection
*c
, char buf
[CONN_INST_BUF
])
1441 if (c
->kind
== CK_INSTANCE
)
1443 if (c
->instance_serial
!= 0)
1445 snprintf(p
, CONN_INST_BUF
, "[%lu]", c
->instance_serial
);
1449 if (c
->policy
& POLICY_OPPO
)
1451 size_t w
= fmt_client(&c
->spd
.this.client
, &c
->spd
.this.host_addr
, " ", p
);
1455 strcpy(p
, w
== 0?
" ..." : "=== ...");
1458 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1461 (void) fmt_client(&c
->spd
.that
.client
, &c
->spd
.that
.host_addr
, "===", p
);
1466 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1468 if (c
->spd
.that
.host_port
!= pluto_port
)
1471 sprintf(p
, ":%d", c
->spd
.that
.host_port
);
1477 /* Find an existing connection for a trapped outbound packet.
1478 * This is attempted before we bother with gateway discovery.
1479 * + this connection is routed or instance_of_routed_template
1480 * (i.e. approved for on-demand)
1481 * + this subnet contains our_client (or we are our_client)
1482 * + that subnet contains peer_client (or peer is peer_client)
1483 * + don't care about Phase 1 IDs (we don't know)
1484 * Note: result may still need to be instantiated.
1485 * The winner has the highest policy priority.
1487 * If there are several with that priority, we give preference to
1488 * the first one that is an instance.
1490 * See also build_outgoing_opportunistic_connection.
1493 find_connection_for_clients(struct spd_route
**srp
,
1494 const ip_address
*our_client
,
1495 const ip_address
*peer_client
,
1496 int transport_proto
)
1498 struct connection
*c
= connections
, *best
= NULL
;
1499 policy_prio_t best_prio
= BOTTOM_PRIO
;
1500 struct spd_route
*sr
;
1501 struct spd_route
*best_sr
= NULL
;
1502 int our_port
= ntohs(portof(our_client
));
1503 int peer_port
= ntohs(portof(peer_client
));
1505 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1507 if (DBGP(DBG_CONTROL
))
1509 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1511 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1512 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1513 DBG_log("find_connection: "
1514 "looking for policy for connection: %s:%d/%d -> %s:%d/%d"
1515 , ocb
, transport_proto
, our_port
, pcb
, transport_proto
, peer_port
);
1519 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
1521 if (c
->kind
== CK_GROUP
)
1524 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1526 if ((routed(sr
->routing
) || c
->instance_initiation_ok
)
1527 && addrinsubnet(our_client
, &sr
->this.client
)
1528 && addrinsubnet(peer_client
, &sr
->that
.client
)
1529 && addrinsubnet(peer_client
, &sr
->that
.client
)
1530 && (!sr
->this.protocol
|| transport_proto
== sr
->this.protocol
)
1531 && (!sr
->this.port
|| our_port
== sr
->this.port
)
1532 && (!sr
->that
.port
|| peer_port
== sr
->that
.port
))
1534 char cib
[CONN_INST_BUF
];
1535 char cib2
[CONN_INST_BUF
];
1537 policy_prio_t prio
= 8 * (c
->prio
+ (c
->kind
== CK_INSTANCE
))
1538 + 2 * (sr
->this.port
== our_port
)
1539 + 2 * (sr
->that
.port
== peer_port
)
1540 + (sr
->this.protocol
== transport_proto
);
1543 if (DBGP(DBG_CONTROL
|DBG_CONTROLMORE
))
1545 char c_ocb
[SUBNETTOT_BUF
], c_pcb
[SUBNETTOT_BUF
];
1547 subnettot(&c
->spd
.this.client
, 0, c_ocb
, sizeof(c_ocb
));
1548 subnettot(&c
->spd
.that
.client
, 0, c_pcb
, sizeof(c_pcb
));
1549 DBG_log("find_connection: conn \"%s\"%s has compatible peers: %s->%s [pri: %ld]"
1551 , (fmt_conn_instance(c
, cib
), cib
)
1552 , c_ocb
, c_pcb
, prio
);
1563 DBG(DBG_CONTROLMORE
,
1564 DBG_log("find_connection: "
1565 "comparing best \"%s\"%s [pri:%ld]{%p} (child %s) to \"%s\"%s [pri:%ld]{%p} (child %s)"
1567 , (fmt_conn_instance(best
, cib
), cib
)
1570 , (best
->policy_next ? best
->policy_next
->name
: "none")
1572 , (fmt_conn_instance(c
, cib2
), cib2
)
1575 , (c
->policy_next ? c
->policy_next
->name
: "none")));
1577 if (prio
> best_prio
)
1587 if (best
!= NULL
&& NEVER_NEGOTIATE(best
->policy
))
1590 if (srp
!= NULL
&& best
!= NULL
)
1594 if (DBGP(DBG_CONTROL
))
1598 char cib
[CONN_INST_BUF
];
1599 DBG_log("find_connection: concluding with \"%s\"%s [pri:%ld]{%p} kind=%s"
1601 , (fmt_conn_instance(best
, cib
), cib
)
1604 , enum_name(&connection_kind_names
, best
->kind
));
1606 DBG_log("find_connection: concluding with empty");
1614 /* Find and instantiate a connection for an outgoing Opportunistic connection.
1615 * We've already discovered its gateway.
1616 * We look for a the connection such that:
1617 * + this is one of our interfaces
1618 * + this subnet contains our_client (or we are our_client)
1619 * (we will specialize the client). We prefer the smallest such subnet.
1620 * + that subnet contains peer_clent (we will specialize the client).
1621 * We prefer the smallest such subnet.
1622 * + is opportunistic
1623 * + that peer is NO_IP
1624 * + don't care about Phase 1 IDs (probably should be default)
1625 * We could look for a connection that already had the desired peer
1626 * (rather than NO_IP) specified, but it doesn't seem worth the
1629 * We look for the routed policy applying to the narrowest subnets.
1630 * We only succeed if we find such a policy AND it is satisfactory.
1632 * The body of the inner loop is a lot like that in
1633 * find_connection_for_clients. In this case, we know the gateways
1634 * that we need to instantiate an opportunistic connection.
1637 build_outgoing_opportunistic_connection(struct gw_info
*gw
1638 ,const ip_address
*our_client
1639 ,const ip_address
*peer_client
)
1642 struct connection
*best
= NULL
;
1643 struct spd_route
*sr
, *bestsr
;
1644 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1646 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1647 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1649 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1651 /* We don't know his ID yet, so gw id must be an ipaddr */
1652 passert(gw
->key
!= NULL
);
1653 passert(id_is_ipaddr(&gw
->gw_id
));
1655 /* for each of our addresses... */
1656 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1658 /* go through those connections with our address and NO_IP as hosts
1659 * We cannot know what port the peer would use, so we assume
1660 * that it is pluto_port (makes debugging easier).
1662 struct connection
*c
= find_host_pair_connections(&p
->addr
1663 , pluto_port
, (ip_address
*)NULL
, pluto_port
);
1665 for (; c
!= NULL
; c
= c
->hp_next
)
1668 DBG_log("checking %s", c
->name
));
1669 if (c
->kind
== CK_GROUP
)
1674 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1676 if (routed(sr
->routing
)
1677 && addrinsubnet(our_client
, &sr
->this.client
)
1678 && addrinsubnet(peer_client
, &sr
->that
.client
))
1687 DBG_log("comparing best %s to %s"
1688 , best
->name
, c
->name
));
1690 for (bestsr
= &best
->spd
; best
!=c
&& bestsr
; bestsr
=bestsr
->next
)
1692 if (!subnetinsubnet(&bestsr
->this.client
, &sr
->this.client
)
1693 || (samesubnet(&bestsr
->this.client
, &sr
->this.client
)
1694 && !subnetinsubnet(&bestsr
->that
.client
1695 , &sr
->that
.client
)))
1706 || NEVER_NEGOTIATE(best
->policy
)
1707 || (best
->policy
& POLICY_OPPO
) == LEMPTY
1708 || best
->kind
!= CK_TEMPLATE
)
1711 return oppo_instantiate(best
, &gw
->gw_id
.ip_addr
, NULL
, gw
1712 , our_client
, peer_client
);
1716 orient(struct connection
*c
)
1718 struct spd_route
*sr
;
1724 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
1726 /* Note: this loop does not stop when it finds a match:
1727 * it continues checking to catch any ambiguity.
1729 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1736 /* check if this interface matches this end */
1737 if (sameaddr(&sr
->this.host_addr
, &p
->addr
)
1738 && (!no_klips
|| sr
->this.host_port
== pluto_port
))
1742 if (c
->interface
== p
)
1743 loglog(RC_LOG_SERIOUS
1744 , "both sides of \"%s\" are our interface %s!"
1745 , c
->name
, p
->rname
);
1747 loglog(RC_LOG_SERIOUS
, "two interfaces match \"%s\" (%s, %s)"
1748 , c
->name
, c
->interface
->rname
, p
->rname
);
1749 c
->interface
= NULL
; /* withdraw orientation */
1755 /* done with this interface if it doesn't match that end */
1756 if (!(sameaddr(&sr
->that
.host_addr
, &p
->addr
)
1757 && (!no_klips
|| sr
->that
.host_port
== pluto_port
)))
1760 /* swap ends and try again.
1761 * It is a little tricky to see that this loop will stop.
1762 * Only continue if the far side matches.
1763 * If both sides match, there is an error-out.
1766 struct end t
= sr
->this;
1768 sr
->this = sr
->that
;
1775 return oriented(*c
);
1779 initiate_connection(const char *name
, int whackfd
)
1781 struct connection
*c
= con_by_name(name
, TRUE
);
1783 if (c
!= NULL
&& c
->ikev1
)
1785 set_cur_connection(c
);
1788 loglog(RC_ORIENT
, "we have no ipsecN interface for either end of this connection");
1790 else if (NEVER_NEGOTIATE(c
->policy
))
1793 , "cannot initiate an authby=never connection");
1795 else if (c
->kind
!= CK_PERMANENT
)
1797 if (isanyaddr(&c
->spd
.that
.host_addr
))
1798 loglog(RC_NOPEERIP
, "cannot initiate connection without knowing peer IP address");
1800 loglog(RC_WILDCARD
, "cannot initiate connection with ID wildcards");
1804 /* We will only request an IPsec SA if policy isn't empty
1805 * (ignoring Main Mode items).
1806 * This is a fudge, but not yet important.
1807 * If we are to proceed asynchronously, whackfd will be NULL_FD.
1809 c
->policy
|= POLICY_UP
;
1810 /* do we have to prompt for a PIN code? */
1811 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
&& whackfd
!= NULL_FD
)
1812 scx_get_pin(c
->spd
.this.sc
, whackfd
);
1814 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
)
1816 loglog(RC_NOVALIDPIN
, "cannot initiate connection without valid PIN");
1820 ipsecdoi_initiate(whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
1821 whackfd
= NULL_FD
; /* protect from close */
1824 reset_cur_connection();
1829 /* (Possibly) Opportunistic Initiation:
1830 * Knowing clients (single IP addresses), try to build an tunnel.
1831 * This may involve discovering a gateway and instantiating an
1832 * Opportunistic connection. Called when a packet is caught by
1833 * a %trap, or when whack --oppohere --oppothere is used.
1834 * It may turn out that an existing or non-opporunistic connnection
1835 * can handle the traffic.
1837 * Most of the code will be restarted if an ADNS request is made
1838 * to discover the gateway. The only difference between the first
1839 * and second entry is whether gateways_from_dns is NULL or not.
1840 * initiate_opportunistic: initial entrypoint
1841 * continue_oppo: where we pickup when ADNS result arrives
1842 * initiate_opportunistic_body: main body shared by above routines
1843 * cannot_oppo: a helper function to log a diagnostic
1844 * This structure repeats a lot of code when the ADNS result arrives.
1845 * This seems like a waste, but anything learned the first time through
1846 * may no longer be true!
1848 * After the first IKE message is sent, the regular state machinery
1849 * carries negotiation forward.
1852 enum find_oppo_step
{
1855 fos_myid_hostname_txt
,
1857 fos_myid_hostname_key
,
1862 #endif /* USE_KEYRR */
1868 static const char *const oppo_step_name
[] = {
1871 "fos_myid_hostname_txt",
1873 "fos_myid_hostname_key",
1878 #endif /* USE_KEYRR */
1884 struct find_oppo_bundle
{
1885 enum find_oppo_step step
;
1887 bool failure_ok
; /* if true, continue_oppo should not die on DNS failure */
1888 ip_address our_client
; /* not pointer! */
1889 ip_address peer_client
;
1890 int transport_proto
;
1892 policy_prio_t policy_prio
;
1893 ipsec_spi_t failure_shunt
; /* in host order! 0 for delete. */
1897 struct find_oppo_continuation
{
1898 struct adns_continuation ac
; /* common prefix */
1899 struct find_oppo_bundle b
;
1903 cannot_oppo(struct connection
*c
1904 , struct find_oppo_bundle
*b
1907 char pcb
[ADDRTOT_BUF
];
1908 char ocb
[ADDRTOT_BUF
];
1910 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
1911 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
1913 DBG(DBG_DNS
| DBG_OPPO
, DBG_log("Can't Opportunistically initiate for %s to %s: %s"
1916 whack_log(RC_OPPOFAILURE
1917 , "Can't Opportunistically initiate for %s to %s: %s"
1920 if (c
!= NULL
&& c
->policy_next
!= NULL
)
1922 /* there is some policy that comes afterwards */
1923 struct spd_route
*shunt_spd
;
1924 struct connection
*nc
= c
->policy_next
;
1927 passert(c
->kind
== CK_TEMPLATE
);
1928 passert(c
->policy_next
->kind
== CK_PERMANENT
);
1930 DBG(DBG_OPPO
, DBG_log("OE failed for %s to %s, but %s overrides shunt"
1931 , ocb
, pcb
, c
->policy_next
->name
));
1934 * okay, here we need add to the "next" policy, which is ought
1935 * to be an instance.
1936 * We will add another entry to the spd_route list for the specific
1937 * situation that we have.
1940 shunt_spd
= clone_thing(nc
->spd
, "shunt eroute policy");
1942 shunt_spd
->next
= nc
->spd
.next
;
1943 nc
->spd
.next
= shunt_spd
;
1945 happy(addrtosubnet(&b
->peer_client
, &shunt_spd
->that
.client
));
1947 if (sameaddr(&b
->peer_client
, &shunt_spd
->that
.host_addr
))
1948 shunt_spd
->that
.has_client
= FALSE
;
1951 * override the tunnel destination with the one from the secondaried
1954 shunt_spd
->that
.host_addr
= nc
->spd
.that
.host_addr
;
1956 /* now, lookup the state, and poke it up.
1959 st
= state_with_serialno(nc
->newest_ipsec_sa
);
1961 /* XXX what to do if the IPSEC SA has died? */
1962 passert(st
!= NULL
);
1964 /* link the new connection instance to the state's list of
1968 DBG(DBG_OPPO
, DBG_log("installing state: %ld for %s to %s"
1969 , nc
->newest_ipsec_sa
1973 if (DBGP(DBG_OPPO
| DBG_CONTROLMORE
))
1975 char state_buf
[LOG_WIDTH
];
1976 char state_buf2
[LOG_WIDTH
];
1979 fmt_state(FALSE
, st
, n
1980 , state_buf
, sizeof(state_buf
)
1981 , state_buf2
, sizeof(state_buf2
));
1982 DBG_log("cannot_oppo, failure SA1: %s", state_buf
);
1983 DBG_log("cannot_oppo, failure SA2: %s", state_buf2
);
1987 if (!route_and_eroute(c
, shunt_spd
, st
))
1989 whack_log(RC_OPPOFAILURE
1990 , "failed to instantiate shunt policy %s for %s to %s"
2000 /* Replace HOLD with b->failure_shunt.
2001 * If no b->failure_shunt specified, use SPI_PASS -- THIS MAY CHANGE.
2003 if (b
->failure_shunt
== 0)
2005 DBG(DBG_OPPO
, DBG_log("no explicit failure shunt for %s to %s; installing %%pass"
2009 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2012 , b
->failure_shunt
!= 0
2013 , b
->transport_proto
2019 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
2020 , struct adns_continuation
*ac
, err_t ac_ugh
); /* forward */
2023 initiate_opportunistic(const ip_address
*our_client
2024 , const ip_address
*peer_client
2025 , int transport_proto
2029 struct find_oppo_bundle b
;
2031 b
.want
= (whackfd
== NULL_FD ?
"whack" : "acquire");
2032 b
.failure_ok
= FALSE
;
2033 b
.our_client
= *our_client
;
2034 b
.peer_client
= *peer_client
;
2035 b
.transport_proto
= transport_proto
;
2037 b
.policy_prio
= BOTTOM_PRIO
;
2038 b
.failure_shunt
= 0;
2039 b
.whackfd
= whackfd
;
2041 initiate_opportunistic_body(&b
, NULL
, NULL
);
2045 continue_oppo(struct adns_continuation
*acr
, err_t ugh
)
2047 struct find_oppo_continuation
*cr
= (void *)acr
; /* inherit, damn you! */
2048 struct connection
*c
;
2049 bool was_held
= cr
->b
.held
;
2050 int whackfd
= cr
->b
.whackfd
;
2052 /* note: cr->id has no resources; cr->sgw_id is id_none:
2053 * neither need freeing.
2055 whack_log_fd
= whackfd
;
2058 /* Discover and record whether %hold has gone away.
2059 * This could have happened while we were awaiting DNS.
2060 * We must check BEFORE any call to cannot_oppo.
2063 cr
->b
.held
= has_bare_hold(&cr
->b
.our_client
, &cr
->b
.peer_client
2064 , cr
->b
.transport_proto
);
2068 /* if we're going to ignore the error, at least note it in debugging log */
2069 if (cr
->b
.failure_ok
&& ugh
!= NULL
)
2071 DBG(DBG_CONTROL
| DBG_DNS
,
2073 char ocb
[ADDRTOT_BUF
];
2074 char pcb
[ADDRTOT_BUF
];
2076 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2077 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2078 DBG_log("continuing from failed DNS lookup for %s, %s to %s: %s"
2079 , cr
->b
.want
, ocb
, pcb
, ugh
);
2084 if (!cr
->b
.failure_ok
&& ugh
!= NULL
)
2086 c
= find_connection_for_clients(NULL
, &cr
->b
.our_client
, &cr
->b
.peer_client
2087 , cr
->b
.transport_proto
);
2088 cannot_oppo(c
, &cr
->b
2089 , builddiag("%s: %s", cr
->b
.want
, ugh
));
2091 else if (was_held
&& !cr
->b
.held
)
2093 /* was_held indicates we were started due to a %trap firing
2094 * (as opposed to a "whack --oppohere --oppothere").
2095 * Since the %hold has gone, we can assume that somebody else
2096 * has beaten us to the punch. We can go home. But lets log it.
2098 char ocb
[ADDRTOT_BUF
];
2099 char pcb
[ADDRTOT_BUF
];
2101 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2102 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2105 , "%%hold otherwise handled during DNS lookup for Opportunistic Initiation for %s to %s"
2110 initiate_opportunistic_body(&cr
->b
, &cr
->ac
, ugh
);
2111 whackfd
= NULL_FD
; /* was handed off */
2114 whack_log_fd
= NULL_FD
;
2120 check_key_recs(enum myid_state try_state
2121 , const struct connection
*c
2122 , struct adns_continuation
*ac
)
2124 /* Check if KEY lookup yielded good results.
2125 * Looking up based on our ID. Used if
2126 * client is ourself, or if TXT had no public key.
2127 * Note: if c is different this time, there is
2128 * a chance that we did the wrong query.
2129 * If so, treat as a kind of failure.
2131 enum myid_state old_myid_state
= myid_state
;
2132 const struct RSA_private_key
*our_RSA_pri
;
2135 myid_state
= try_state
;
2137 if (old_myid_state
!= myid_state
2138 && old_myid_state
== MYID_SPECIFIED
)
2140 ugh
= "%myid was specified while we were guessing";
2142 else if ((our_RSA_pri
= get_RSA_private_key(c
)) == NULL
)
2144 ugh
= "we don't know our own RSA key";
2146 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2148 ugh
= "our ID changed underfoot";
2152 /* Similar to code in RSA_check_signature
2153 * for checking the other side.
2157 ugh
= "no KEY RR found for us";
2158 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2160 ugh
= "all our KEY RRs have the wrong public key";
2161 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2162 && same_RSA_public_key(&our_RSA_pri
->pub
, &kr
->key
->u
.rsa
))
2164 ugh
= NULL
; /* good! */
2170 myid_state
= old_myid_state
;
2173 #endif /* USE_KEYRR */
2176 check_txt_recs(enum myid_state try_state
2177 , const struct connection
*c
2178 , struct adns_continuation
*ac
)
2180 /* Check if TXT lookup yielded good results.
2181 * Looking up based on our ID. Used if
2182 * client is ourself, or if TXT had no public key.
2183 * Note: if c is different this time, there is
2184 * a chance that we did the wrong query.
2185 * If so, treat as a kind of failure.
2187 enum myid_state old_myid_state
= myid_state
;
2188 const struct RSA_private_key
*our_RSA_pri
;
2191 myid_state
= try_state
;
2193 if (old_myid_state
!= myid_state
2194 && old_myid_state
== MYID_SPECIFIED
)
2196 ugh
= "%myid was specified while we were guessing";
2198 else if ((our_RSA_pri
= get_RSA_private_key(c
)) == NULL
)
2200 ugh
= "we don't know our own RSA key";
2202 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2204 ugh
= "our ID changed underfoot";
2208 /* Similar to code in RSA_check_signature
2209 * for checking the other side.
2211 struct gw_info
*gwp
;
2213 ugh
= "no TXT RR found for us";
2214 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2216 ugh
= "all our TXT RRs have the wrong public key";
2217 if (gwp
->key
->alg
== PUBKEY_ALG_RSA
2218 && same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2220 ugh
= NULL
; /* good! */
2226 myid_state
= old_myid_state
;
2231 /* note: gateways_from_dns must be NULL iff this is the first call */
2233 initiate_opportunistic_body(struct find_oppo_bundle
*b
2234 , struct adns_continuation
*ac
2237 struct connection
*c
;
2238 struct spd_route
*sr
;
2240 /* What connection shall we use?
2241 * First try for one that explicitly handles the clients.
2245 char ours
[ADDRTOT_BUF
];
2246 char his
[ADDRTOT_BUF
];
2250 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2251 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2252 ourport
= ntohs(portof(&b
->our_client
));
2253 hisport
= ntohs(portof(&b
->peer_client
));
2254 DBG_log("initiate on demand from %s:%d to %s:%d proto=%d state: %s because: %s"
2255 , ours
, ourport
, his
, hisport
, b
->transport_proto
2256 , oppo_step_name
[b
->step
], b
->want
);
2258 if (isanyaddr(&b
->our_client
) || isanyaddr(&b
->peer_client
))
2260 cannot_oppo(NULL
, b
, "impossible IP address");
2262 else if ((c
= find_connection_for_clients(&sr
2265 , b
->transport_proto
)) == NULL
)
2267 /* No connection explicitly handles the clients and there
2268 * are no Opportunistic connections -- whine and give up.
2269 * The failure policy cannot be gotten from a connection; we pick %pass.
2271 cannot_oppo(NULL
, b
, "no routed Opportunistic template covers this pair");
2273 else if (c
->kind
!= CK_TEMPLATE
)
2275 /* We've found a connection that can serve.
2276 * Do we have to initiate it?
2277 * Not if there is currently an IPSEC SA.
2278 * But if there is an IPSEC SA, then KLIPS would not
2279 * have generated the acquire. So we assume that there isn't one.
2280 * This may be redundant if a non-opportunistic
2281 * negotiation is already being attempted.
2284 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2286 if(c
->kind
== CK_INSTANCE
)
2288 char cib
[CONN_INST_BUF
];
2289 /* there is already an instance being negotiated, no nothing */
2290 DBG(DBG_CONTROL
, DBG_log("found existing instance \"%s\"%s, rekeying it"
2292 , (fmt_conn_instance(c
, cib
), cib
)));
2293 /* XXX-mcr - return; */
2296 /* otherwise, there is some kind of static conn that can handle
2297 * this connection, so we initiate it */
2302 /* what should we do on failure? */
2303 (void) assign_hold(c
, sr
, b
->transport_proto
, &b
->our_client
, &b
->peer_client
);
2306 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2307 b
->whackfd
= NULL_FD
; /* protect from close */
2311 /* We are handling an opportunistic situation.
2312 * This involves several DNS lookup steps that require suspension.
2313 * Note: many facts might change while we're suspended.
2316 * The first chunk of code handles the result of the previous
2317 * DNS query (if any). It also selects the kind of the next step.
2318 * The second chunk initiates the next DNS query (if any).
2320 enum find_oppo_step next_step
;
2322 char mycredentialstr
[BUF_LEN
];
2323 char cib
[CONN_INST_BUF
];
2325 DBG(DBG_CONTROL
, DBG_log("creating new instance from \"%s\"%s"
2327 , (fmt_conn_instance(c
, cib
), cib
)));
2330 idtoa(&sr
->this.id
, mycredentialstr
, sizeof(mycredentialstr
));
2332 passert(c
->policy
& POLICY_OPPO
); /* can't initiate Road Warrior connections */
2334 /* handle any DNS answer; select next step */
2339 /* just starting out: select first query step */
2340 next_step
= fos_myid_ip_txt
;
2343 case fos_myid_ip_txt
: /* TXT for our default IP address as %myid */
2344 ugh
= check_txt_recs(MYID_IP
, c
, ac
);
2347 /* cannot use our IP as OE identitiy for initiation */
2348 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:TXT) as identity: %s"
2351 if (!logged_myid_ip_txt_warning
)
2353 loglog(RC_LOG_SERIOUS
2354 , "can not use our IP (%s:TXT) as identity: %s"
2357 logged_myid_ip_txt_warning
= TRUE
;
2360 next_step
= fos_myid_hostname_txt
;
2361 ugh
= NULL
; /* failure can be recovered from */
2365 /* we can use our IP as OE identity for initiation */
2366 if (!logged_myid_ip_txt_warning
)
2368 loglog(RC_LOG_SERIOUS
2369 , "using our IP (%s:TXT) as identity!"
2370 , myid_str
[MYID_IP
]);
2371 logged_myid_ip_txt_warning
= TRUE
;
2374 next_step
= fos_our_client
;
2378 case fos_myid_hostname_txt
: /* TXT for our hostname as %myid */
2379 ugh
= check_txt_recs(MYID_HOSTNAME
, c
, ac
);
2382 /* cannot use our hostname as OE identitiy for initiation */
2383 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:TXT) as identity: %s"
2384 , myid_str
[MYID_HOSTNAME
]
2386 if (!logged_myid_fqdn_txt_warning
)
2388 loglog(RC_LOG_SERIOUS
2389 , "can not use our hostname (%s:TXT) as identity: %s"
2390 , myid_str
[MYID_HOSTNAME
]
2392 logged_myid_fqdn_txt_warning
= TRUE
;
2395 next_step
= fos_myid_ip_key
;
2396 ugh
= NULL
; /* failure can be recovered from */
2401 /* we can use our hostname as OE identity for initiation */
2402 if (!logged_myid_fqdn_txt_warning
)
2404 loglog(RC_LOG_SERIOUS
2405 , "using our hostname (%s:TXT) as identity!"
2406 , myid_str
[MYID_HOSTNAME
]);
2407 logged_myid_fqdn_txt_warning
= TRUE
;
2409 next_step
= fos_our_client
;
2414 case fos_myid_ip_key
: /* KEY for our default IP address as %myid */
2415 ugh
= check_key_recs(MYID_IP
, c
, ac
);
2418 /* cannot use our IP as OE identitiy for initiation */
2419 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:KEY) as identity: %s"
2422 if (!logged_myid_ip_key_warning
)
2424 loglog(RC_LOG_SERIOUS
2425 , "can not use our IP (%s:KEY) as identity: %s"
2428 logged_myid_ip_key_warning
= TRUE
;
2431 next_step
= fos_myid_hostname_key
;
2432 ugh
= NULL
; /* failure can be recovered from */
2436 /* we can use our IP as OE identity for initiation */
2437 if (!logged_myid_ip_key_warning
)
2439 loglog(RC_LOG_SERIOUS
2440 , "using our IP (%s:KEY) as identity!"
2441 , myid_str
[MYID_IP
]);
2442 logged_myid_ip_key_warning
= TRUE
;
2444 next_step
= fos_our_client
;
2448 case fos_myid_hostname_key
: /* KEY for our hostname as %myid */
2449 ugh
= check_key_recs(MYID_HOSTNAME
, c
, ac
);
2452 /* cannot use our IP as OE identitiy for initiation */
2453 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:KEY) as identity: %s"
2454 , myid_str
[MYID_HOSTNAME
]
2456 if (!logged_myid_fqdn_key_warning
)
2458 loglog(RC_LOG_SERIOUS
2459 , "can not use our hostname (%s:KEY) as identity: %s"
2460 , myid_str
[MYID_HOSTNAME
]
2462 logged_myid_fqdn_key_warning
= TRUE
;
2465 next_step
= fos_myid_hostname_key
;
2466 ugh
= NULL
; /* failure can be recovered from */
2470 /* we can use our IP as OE identity for initiation */
2471 if (!logged_myid_fqdn_key_warning
)
2473 loglog(RC_LOG_SERIOUS
2474 , "using our hostname (%s:KEY) as identity!"
2475 , myid_str
[MYID_HOSTNAME
]);
2476 logged_myid_fqdn_key_warning
= TRUE
;
2478 next_step
= fos_our_client
;
2483 case fos_our_client
: /* TXT for our client */
2485 /* Our client is not us: we must check the TXT records.
2486 * Note: if c is different this time, there is
2487 * a chance that we did the wrong query.
2488 * If so, treat as a kind of failure.
2490 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2492 next_step
= fos_his_client
; /* normal situation */
2494 passert(sr
!= NULL
);
2496 if (our_RSA_pri
== NULL
)
2498 ugh
= "we don't know our own RSA key";
2500 else if (sameaddr(&sr
->this.host_addr
, &b
->our_client
))
2502 /* this wasn't true when we started -- bail */
2503 ugh
= "our IP address changed underfoot";
2505 else if (!same_id(&ac
->sgw_id
, &sr
->this.id
))
2507 /* this wasn't true when we started -- bail */
2508 ugh
= "our ID changed underfoot";
2512 /* Similar to code in quick_inI1_outR1_tail
2513 * for checking the other side.
2515 struct gw_info
*gwp
;
2517 ugh
= "no TXT RR for our client delegates us";
2518 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2520 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2522 ugh
= "TXT RR for our client has wrong key";
2523 /* If there is a key from the TXT record,
2524 * we count it as a win if we match the key.
2525 * If there was no key, we have a tentative win:
2526 * we need to check our KEY record to be sure.
2528 if (!gwp
->gw_key_present
)
2530 /* Success, but the TXT had no key
2531 * so we must check our our own KEY records.
2533 next_step
= fos_our_txt
;
2534 ugh
= NULL
; /* good! */
2537 if (same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2539 ugh
= NULL
; /* good! */
2547 case fos_our_txt
: /* TXT for us */
2549 /* Check if TXT lookup yielded good results.
2550 * Looking up based on our ID. Used if
2551 * client is ourself, or if TXT had no public key.
2552 * Note: if c is different this time, there is
2553 * a chance that we did the wrong query.
2554 * If so, treat as a kind of failure.
2556 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2558 next_step
= fos_his_client
; /* unless we decide to look for KEY RR */
2560 if (our_RSA_pri
== NULL
)
2562 ugh
= "we don't know our own RSA key";
2564 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2566 ugh
= "our ID changed underfoot";
2570 /* Similar to code in RSA_check_signature
2571 * for checking the other side.
2573 struct gw_info
*gwp
;
2575 ugh
= "no TXT RR for us";
2576 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2578 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2580 ugh
= "TXT RR for us has wrong key";
2581 if (gwp
->gw_key_present
2582 && same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2585 DBG_log("initiate on demand found TXT with right public key at: %s"
2586 , mycredentialstr
));
2594 /* if no TXT with right key, try KEY */
2596 DBG_log("will try for KEY RR since initiate on demand found %s: %s"
2597 , ugh
, mycredentialstr
));
2598 next_step
= fos_our_key
;
2607 case fos_our_key
: /* KEY for us */
2609 /* Check if KEY lookup yielded good results.
2610 * Looking up based on our ID. Used if
2611 * client is ourself, or if TXT had no public key.
2612 * Note: if c is different this time, there is
2613 * a chance that we did the wrong query.
2614 * If so, treat as a kind of failure.
2616 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2618 next_step
= fos_his_client
; /* always */
2620 if (our_RSA_pri
== NULL
)
2622 ugh
= "we don't know our own RSA key";
2624 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2626 ugh
= "our ID changed underfoot";
2630 /* Similar to code in RSA_check_signature
2631 * for checking the other side.
2635 ugh
= "no KEY RR found for us (and no good TXT RR)";
2636 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2638 ugh
= "all our KEY RRs have the wrong public key (and no good TXT RR)";
2639 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2640 && same_RSA_public_key(&our_RSA_pri
->pub
, &kr
->key
->u
.rsa
))
2642 /* do this only once a day */
2643 if (!logged_txt_warning
)
2645 loglog(RC_LOG_SERIOUS
2646 , "found KEY RR but not TXT RR for %s. See http://www.freeswan.org/err/txt-change.html."
2648 logged_txt_warning
= TRUE
;
2650 ugh
= NULL
; /* good! */
2657 #endif /* USE_KEYRR */
2659 case fos_his_client
: /* TXT for his client */
2661 /* We've finished last DNS queries: TXT for his client.
2662 * Using the information, try to instantiate a connection
2663 * and start negotiating.
2664 * We now know the peer. The chosing of "c" ignored this,
2665 * so we will disregard its current value.
2666 * !!! We need to randomize the entry in gw that we choose.
2668 next_step
= fos_done
; /* no more queries */
2670 c
= build_outgoing_opportunistic_connection(ac
->gateways_from_dns
2676 /* We cannot seem to instantiate a suitable connection:
2679 char ocb
[ADDRTOT_BUF
]
2683 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2684 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2685 passert(id_is_ipaddr(&ac
->gateways_from_dns
->gw_id
));
2686 addrtot(&ac
->gateways_from_dns
->gw_id
.ip_addr
, 0, pb
, sizeof(pb
));
2687 loglog(RC_OPPOFAILURE
2688 , "no suitable connection for opportunism"
2689 " between %s and %s with %s as peer"
2695 /* Replace HOLD with PASS.
2696 * The type of replacement *ought* to be
2697 * specified by policy.
2699 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2701 , SPI_PASS
/* fail into PASS */
2702 , TRUE
, b
->transport_proto
2703 , "no suitable connection");
2709 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2710 passert(c
->kind
== CK_INSTANCE
);
2711 passert(c
->gw_info
!= NULL
);
2712 passert(HAS_IPSEC_POLICY(c
->policy
));
2713 passert(LHAS(LELEM(RT_UNROUTED
) | LELEM(RT_ROUTED_PROSPECTIVE
), c
->spd
.routing
));
2717 /* what should we do on failure? */
2718 (void) assign_hold(c
, &c
->spd
2719 , b
->transport_proto
2720 , &b
->our_client
, &b
->peer_client
);
2723 c
->gw_info
->key
->last_tried_time
= now();
2724 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2725 b
->whackfd
= NULL_FD
; /* protect from close */
2734 /* the second chunk: initiate the next DNS query (if any) */
2737 char ours
[ADDRTOT_BUF
];
2738 char his
[ADDRTOT_BUF
];
2740 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2741 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2742 DBG_log("initiate on demand from %s to %s new state: %s with ugh: %s"
2743 , ours
, his
, oppo_step_name
[b
->step
], ugh ? ugh
: "ok");
2748 b
->policy_prio
= c
->prio
;
2749 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2750 cannot_oppo(c
, b
, ugh
);
2752 else if (next_step
== fos_done
)
2758 /* set up the next query */
2759 struct find_oppo_continuation
*cr
= alloc_thing(struct find_oppo_continuation
2760 , "opportunistic continuation");
2763 b
->policy_prio
= c
->prio
;
2764 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2765 cr
->b
= *b
; /* copy; start hand off of whackfd */
2766 cr
->b
.failure_ok
= FALSE
;
2767 cr
->b
.step
= next_step
;
2770 ; sr
!=NULL
&& !sameaddr(&sr
->this.host_addr
, &b
->our_client
)
2777 /* If a %hold shunt has replaced the eroute for this template,
2781 && sr
->routing
== RT_ROUTED_PROSPECTIVE
&& eclipsable(sr
))
2783 sr
->routing
= RT_ROUTED_ECLIPSED
;
2787 /* Switch to issue next query.
2788 * A case may turn out to be unnecessary. If so, it falls
2789 * through to the next case.
2790 * Figuring out what %myid can stand for must be done before
2791 * our client credentials are looked up: we must know what
2792 * the client credentials may use to identify us.
2793 * On the other hand, our own credentials should be looked
2794 * up after our clients in case our credentials are not
2796 * XXX this is a wasted effort if we don't have credentials
2797 * BUT they are not needed.
2801 case fos_myid_ip_txt
:
2802 if (c
->spd
.this.id
.kind
== ID_MYID
2803 && myid_state
!= MYID_SPECIFIED
)
2805 cr
->b
.failure_ok
= TRUE
;
2806 cr
->b
.want
= b
->want
= "TXT record for IP address as %myid";
2807 ugh
= start_adns_query(&myids
[MYID_IP
]
2814 cr
->b
.step
= fos_myid_hostname_txt
;
2817 case fos_myid_hostname_txt
:
2818 if (c
->spd
.this.id
.kind
== ID_MYID
2819 && myid_state
!= MYID_SPECIFIED
)
2822 cr
->b
.failure_ok
= TRUE
;
2824 cr
->b
.failure_ok
= FALSE
;
2826 cr
->b
.want
= b
->want
= "TXT record for hostname as %myid";
2827 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2828 , &myids
[MYID_HOSTNAME
]
2836 cr
->b
.step
= fos_myid_ip_key
;
2839 case fos_myid_ip_key
:
2840 if (c
->spd
.this.id
.kind
== ID_MYID
2841 && myid_state
!= MYID_SPECIFIED
)
2843 cr
->b
.failure_ok
= TRUE
;
2844 cr
->b
.want
= b
->want
= "KEY record for IP address as %myid (no good TXT)";
2845 ugh
= start_adns_query(&myids
[MYID_IP
]
2846 , (const struct id
*) NULL
/* security gateway meaningless */
2852 cr
->b
.step
= fos_myid_hostname_key
;
2855 case fos_myid_hostname_key
:
2856 if (c
->spd
.this.id
.kind
== ID_MYID
2857 && myid_state
!= MYID_SPECIFIED
)
2859 cr
->b
.failure_ok
= FALSE
; /* last attempt! */
2860 cr
->b
.want
= b
->want
= "KEY record for hostname as %myid (no good TXT)";
2861 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2862 , (const struct id
*) NULL
/* security gateway meaningless */
2869 cr
->b
.step
= fos_our_client
;
2872 case fos_our_client
: /* TXT for our client */
2873 if (!sameaddr(&c
->spd
.this.host_addr
, &b
->our_client
))
2875 /* Check that at least one TXT(reverse(b->our_client)) is workable.
2876 * Note: {unshare|free}_id_content not needed for id: ephemeral.
2878 cr
->b
.want
= b
->want
= "our client's TXT record";
2879 iptoid(&b
->our_client
, &id
);
2880 ugh
= start_adns_query(&id
2881 , &c
->spd
.this.id
/* we are the security gateway */
2887 cr
->b
.step
= fos_our_txt
;
2890 case fos_our_txt
: /* TXT for us */
2891 cr
->b
.failure_ok
= b
->failure_ok
= TRUE
;
2892 cr
->b
.want
= b
->want
= "our TXT record";
2893 ugh
= start_adns_query(&sr
->this.id
2894 , &sr
->this.id
/* we are the security gateway XXX - maybe ignore? mcr */
2901 case fos_our_key
: /* KEY for us */
2902 cr
->b
.want
= b
->want
= "our KEY record";
2903 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2904 ugh
= start_adns_query(&sr
->this.id
2905 , (const struct id
*) NULL
/* security gateway meaningless */
2910 #endif /* USE_KEYRR */
2912 case fos_his_client
: /* TXT for his client */
2913 /* note: {unshare|free}_id_content not needed for id: ephemeral */
2914 cr
->b
.want
= b
->want
= "target's TXT record";
2915 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2916 iptoid(&b
->peer_client
, &id
);
2917 ugh
= start_adns_query(&id
2918 , (const struct id
*) NULL
/* security gateway unconstrained */
2925 bad_case(next_step
);
2929 b
->whackfd
= NULL_FD
; /* complete hand-off */
2931 cannot_oppo(c
, b
, ugh
);
2934 close_any(b
->whackfd
);
2938 terminate_connection(const char *nm
)
2940 /* Loop because more than one may match (master and instances)
2941 * But at least one is required (enforced by con_by_name).
2943 struct connection
*c
= con_by_name(nm
, TRUE
);
2945 if (c
== NULL
|| !c
->ikev1
)
2950 struct connection
*n
= c
->ac_next
; /* grab this before c might disappear */
2952 if (streq(c
->name
, nm
)
2953 && c
->kind
>= CK_PERMANENT
2954 && !NEVER_NEGOTIATE(c
->policy
))
2956 set_cur_connection(c
);
2957 plog("terminating SAs using this connection");
2958 c
->policy
&= ~POLICY_UP
;
2959 flush_pending_by_connection(c
);
2960 delete_states_by_connection(c
, FALSE
);
2961 reset_cur_connection();
2964 } while (c
!= NULL
);
2967 /* an ISAKMP SA has been established.
2968 * Note the serial number, and release any connections with
2969 * the same peer ID but different peer IP address.
2971 bool uniqueIDs
= FALSE
; /* --uniqueids? */
2974 ISAKMP_SA_established(struct connection
*c
, so_serial_t serial
)
2976 c
->newest_isakmp_sa
= serial
;
2978 /* the connection is now oriented so that we are able to determine
2979 * whether we are a mode config server with a virtual IP to send.
2981 if (!isanyaddr(&c
->spd
.that
.host_srcip
) && !c
->spd
.that
.has_natip
)
2982 c
->spd
.that
.modecfg
= TRUE
;
2986 /* for all connections: if the same Phase 1 IDs are used
2987 * for a different IP address, unorient that connection.
2989 struct connection
*d
;
2991 for (d
= connections
; d
!= NULL
; )
2993 struct connection
*next
= d
->ac_next
; /* might move underneath us */
2995 if (d
->kind
>= CK_PERMANENT
2996 && same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
2997 && same_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
)
2998 && (!sameaddr(&c
->spd
.that
.host_addr
, &d
->spd
.that
.host_addr
) ||
2999 (c
->spd
.that
.host_port
!= d
->spd
.that
.host_port
)))
3001 release_connection(d
, FALSE
);
3008 /* Find the connection to connection c's peer's client with the
3009 * largest value of .routing. All other things being equal,
3010 * preference is given to c. If none is routed, return NULL.
3012 * If erop is non-null, set *erop to a connection sharing both
3013 * our client subnet and peer's client subnet with the largest value
3014 * of .routing. If none is erouted, set *erop to NULL.
3016 * The return value is used to find other connections sharing a route.
3017 * *erop is used to find other connections sharing an eroute.
3020 route_owner(struct connection
*c
3021 , struct spd_route
**srp
3022 , struct connection
**erop
3023 , struct spd_route
**esrp
)
3025 struct connection
*d
3028 struct spd_route
*srd
, *src
;
3029 struct spd_route
*best_sr
, *best_esr
;
3030 enum routing_t best_routing
, best_erouting
;
3032 passert(oriented(*c
));
3035 best_routing
= c
->spd
.routing
;
3036 best_erouting
= best_routing
;
3038 for (d
= connections
; d
!= NULL
; d
= d
->ac_next
)
3040 for (srd
= &d
->spd
; srd
; srd
= srd
->next
)
3042 if (srd
->routing
== RT_UNROUTED
)
3045 for (src
= &c
->spd
; src
; src
=src
->next
)
3047 if (!samesubnet(&src
->that
.client
, &srd
->that
.client
))
3049 if (src
->that
.protocol
!= srd
->that
.protocol
)
3051 if (src
->that
.port
!= srd
->that
.port
)
3053 passert(oriented(*d
));
3054 if (srd
->routing
> best_routing
)
3058 best_routing
= srd
->routing
;
3061 if (!samesubnet(&src
->this.client
, &srd
->this.client
))
3063 if (src
->this.protocol
!= srd
->this.protocol
)
3065 if (src
->this.port
!= srd
->this.port
)
3067 if (srd
->routing
> best_erouting
)
3071 best_erouting
= srd
->routing
;
3079 char cib
[CONN_INST_BUF
];
3080 err_t m
= builddiag("route owner of \"%s\"%s %s:"
3082 , (fmt_conn_instance(c
, cib
), cib
)
3083 , enum_name(&routing_story
, c
->spd
.routing
));
3085 if (!routed(best_ro
->spd
.routing
))
3086 m
= builddiag("%s NULL", m
);
3087 else if (best_ro
== c
)
3088 m
= builddiag("%s self", m
);
3090 m
= builddiag("%s \"%s\"%s %s", m
3092 , (fmt_conn_instance(best_ro
, cib
), cib
)
3093 , enum_name(&routing_story
, best_ro
->spd
.routing
));
3097 m
= builddiag("%s; eroute owner:", m
);
3098 if (!erouted(best_ero
->spd
.routing
))
3099 m
= builddiag("%s NULL", m
);
3100 else if (best_ero
== c
)
3101 m
= builddiag("%s self", m
);
3103 m
= builddiag("%s \"%s\"%s %s", m
3105 , (fmt_conn_instance(best_ero
, cib
), cib
)
3106 , enum_name(&routing_story
, best_ero
->spd
.routing
));
3113 *erop
= erouted(best_erouting
)? best_ero
: NULL
;
3122 return routed(best_routing
)? best_ro
: NULL
;
3125 /* Find a connection that owns the shunt eroute between subnets.
3126 * There ought to be only one.
3127 * This might get to be a bottleneck -- try hashing if it does.
3130 shunt_owner(const ip_subnet
*ours
, const ip_subnet
*his
)
3132 struct connection
*c
;
3133 struct spd_route
*sr
;
3135 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
3137 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
3139 if (shunt_erouted(sr
->routing
)
3140 && samesubnet(ours
, &sr
->this.client
)
3141 && samesubnet(his
, &sr
->that
.client
))
3148 /* Find some connection with this pair of hosts.
3149 * We don't know enough to chose amongst those available.
3150 * ??? no longer usefully different from find_host_pair_connections
3153 find_host_connection(const ip_address
*me
, u_int16_t my_port
3154 , const ip_address
*him
, u_int16_t his_port
, lset_t policy
)
3156 struct connection
*c
= find_host_pair_connections(me
, my_port
, him
, his_port
);
3158 if (policy
!= LEMPTY
)
3160 lset_t auth_requested
= policy
& POLICY_ID_AUTH_MASK
;
3162 /* if we have requirements for the policy,
3163 * choose the first matching connection.
3167 if (c
->policy
& auth_requested
)
3177 /* given an up-until-now satisfactory connection, find the best connection
3178 * now that we just got the Phase 1 Id Payload from the peer.
3180 * Comments in the code describe the (tricky!) matching criteria.
3181 * Although this routine could handle the initiator case,
3182 * it isn't currently called in this case.
3183 * If it were, it could "upgrade" an Opportunistic Connection
3184 * to a Road Warrior Connection if a suitable Peer ID were found.
3186 * In RFC 2409 "The Internet Key Exchange (IKE)",
3187 * in 5.1 "IKE Phase 1 Authenticated With Signatures", describing Main
3190 * Initiator Responder
3191 * ----------- -----------
3196 * HDR*, IDii, [ CERT, ] SIG_I -->
3197 * <-- HDR*, IDir, [ CERT, ] SIG_R
3199 * In 5.4 "Phase 1 Authenticated With a Pre-Shared Key":
3205 * HDR*, IDii, HASH_I -->
3206 * <-- HDR*, IDir, HASH_R
3208 * refine_host_connection could be called in two case:
3210 * - the Responder receives the IDii payload:
3211 * + [PSK] after using PSK to decode this message