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"
33 #include <credentials/keys/private_key.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 "kernel_alg.h"
62 #include "nat_traversal.h"
65 static void flush_pending_by_connection(connection_t
*c
); /* forward */
67 static connection_t
*connections
= NULL
;
69 /* struct host_pair: a nexus of information about a pair of hosts.
70 * A host is an IP address, UDP port pair. This is a debatable choice:
71 * - should port be considered (no choice of port in standard)?
72 * - should ID be considered (hard because not always known)?
73 * - should IP address matter on our end (we don't know our end)?
74 * Only oriented connections are registered.
75 * Unoriented connections are kept on the unoriented_connections
76 * linked list (using hp_next). For them, host_pair is NULL.
82 u_int16_t port
; /* host order */
84 bool initial_connection_sent
;
85 connection_t
*connections
; /* connections with this pair */
86 struct pending
*pending
; /* awaiting Keying Channel */
87 struct host_pair
*next
;
90 static struct host_pair
*host_pairs
= NULL
;
92 static connection_t
*unoriented_connections
= NULL
;
94 /* check to see that Ids of peers match */
95 bool same_peer_ids(const connection_t
*c
, const connection_t
*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
*find_host_pair(const ip_address
*myaddr
,
104 const ip_address
*hisaddr
,
107 struct host_pair
*p
, *prev
;
109 /* default hisaddr to an appropriate any */
111 hisaddr
= aftoinfo(addrtypeof(myaddr
))->any
;
113 if (nat_traversal_enabled
)
116 * port is not relevant in host_pair. with nat_traversal we
117 * always use pluto_port (500)
120 hisport
= pluto_port
;
123 for (prev
= NULL
, p
= host_pairs
; p
!= NULL
; prev
= p
, p
= p
->next
)
125 if (sameaddr(&p
->me
.addr
, myaddr
) && p
->me
.port
== myport
126 && sameaddr(&p
->him
.addr
, hisaddr
) && p
->him
.port
== hisport
)
130 prev
->next
= p
->next
; /* remove p from list */
131 p
->next
= host_pairs
; /* and stick it on front */
140 /* find head of list of connections with this pair of hosts */
141 static connection_t
*find_host_pair_connections(const ip_address
*myaddr
,
143 const ip_address
*hisaddr
,
146 struct host_pair
*hp
= find_host_pair(myaddr
, myport
, hisaddr
, hisport
);
148 if (nat_traversal_enabled
&& hp
&& hisaddr
)
152 for (c
= hp
->connections
; c
!= NULL
; c
= c
->hp_next
)
154 if (c
->spd
.this.host_port
== myport
&& c
->spd
.that
.host_port
== hisport
)
159 return hp
== NULL? NULL
: hp
->connections
;
162 static void connect_to_host_pair(connection_t
*c
)
166 struct host_pair
*hp
;
168 ip_address his_addr
= (c
->spd
.that
.allow_any
)
169 ?
*aftoinfo(addrtypeof(&c
->spd
.that
.host_addr
))->any
170 : c
->spd
.that
.host_addr
;
172 hp
= find_host_pair(&c
->spd
.this.host_addr
, c
->spd
.this.host_port
173 , &his_addr
, c
->spd
.that
.host_port
);
177 /* no suitable host_pair -- build one */
178 hp
= malloc_thing(struct host_pair
);
179 hp
->me
.addr
= c
->spd
.this.host_addr
;
180 hp
->him
.addr
= his_addr
;
181 hp
->me
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.this.host_port
;
182 hp
->him
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.that
.host_port
;
183 hp
->initial_connection_sent
= FALSE
;
184 hp
->connections
= NULL
;
186 hp
->next
= host_pairs
;
190 c
->hp_next
= hp
->connections
;
195 /* since this connection isn't oriented, we place it
196 * in the unoriented_connections list instead.
199 c
->hp_next
= unoriented_connections
;
200 unoriented_connections
= c
;
204 /* find a connection by name.
205 * If strict, don't accept a CK_INSTANCE.
206 * Move the winner (if any) to the front.
207 * If none is found, and strict, a diagnostic is logged to whack.
209 connection_t
*con_by_name(const char *nm
, bool strict
)
211 connection_t
*p
, *prev
;
213 for (prev
= NULL
, p
= connections
; ; prev
= p
, p
= p
->ac_next
)
218 whack_log(RC_UNKNOWN_NAME
219 , "no connection named \"%s\"", nm
);
222 if (streq(p
->name
, nm
)
223 && (!strict
|| p
->kind
!= CK_INSTANCE
))
227 prev
->ac_next
= p
->ac_next
; /* remove p from list */
228 p
->ac_next
= connections
; /* and stick it on front */
237 void release_connection(connection_t
*c
, bool relations
)
239 if (c
->kind
== CK_INSTANCE
)
241 /* This does everything we need.
242 * Note that we will be called recursively by delete_connection,
243 * but kind will be CK_GOING_AWAY.
245 delete_connection(c
, relations
);
249 flush_pending_by_connection(c
);
250 delete_states_by_connection(c
, relations
);
251 unroute_connection(c
);
255 /* Delete a connection */
257 #define list_rm(etype, enext, e, ehead) { \
259 for (ep = &(ehead); *ep != (e); ep = &(*ep)->enext) \
260 passert(*ep != NULL); /* we must not come up empty-handed */ \
265 void delete_connection(connection_t
*c
, bool relations
)
267 connection_t
*old_cur_connection
268 = cur_connection
== c? NULL
: cur_connection
;
270 lset_t old_cur_debugging
= cur_debugging
;
273 set_cur_connection(c
);
275 /* Must be careful to avoid circularity:
276 * we mark c as going away so it won't get deleted recursively.
278 passert(c
->kind
!= CK_GOING_AWAY
);
279 if (c
->kind
== CK_INSTANCE
)
281 plog("deleting connection \"%s\" instance with peer %s {isakmp=#%lu/ipsec=#%lu}"
283 , ip_str(&c
->spd
.that
.host_addr
)
284 , c
->newest_isakmp_sa
, c
->newest_ipsec_sa
);
285 c
->kind
= CK_GOING_AWAY
;
289 plog("deleting connection");
291 release_connection(c
, relations
); /* won't delete c */
293 if (c
->kind
== CK_GROUP
)
296 /* free up any logging resources */
299 /* find and delete c from connections list */
300 list_rm(connection_t
, ac_next
, c
, connections
);
301 cur_connection
= old_cur_connection
;
303 /* find and delete c from the host pair list */
304 if (c
->host_pair
== NULL
)
307 list_rm(connection_t
, hp_next
, c
, unoriented_connections
);
311 struct host_pair
*hp
= c
->host_pair
;
313 list_rm(connection_t
, hp_next
, c
, hp
->connections
);
314 c
->host_pair
= NULL
; /* redundant, but safe */
316 /* if there are no more connections with this host_pair
317 * and we haven't even made an initial contact, let's delete
318 * this guy in case we were created by an attempted DOS attack.
320 if (hp
->connections
== NULL
321 && !hp
->initial_connection_sent
)
323 passert(hp
->pending
== NULL
); /* ??? must deal with this! */
324 list_rm(struct host_pair
, next
, hp
, host_pairs
);
328 if (c
->kind
!= CK_GOING_AWAY
)
330 free(c
->spd
.that
.virt
);
333 cur_debugging
= old_cur_debugging
;
336 free_id_content(&c
->spd
.this.id
);
337 free(c
->spd
.this.updown
);
338 free(c
->spd
.this.ca
.ptr
);
339 free_ietfAttrList(c
->spd
.this.groups
);
340 free_id_content(&c
->spd
.that
.id
);
341 free(c
->spd
.that
.updown
);
342 free(c
->spd
.that
.ca
.ptr
);
343 free_ietfAttrList(c
->spd
.that
.groups
);
344 free_generalNames(c
->requested_ca
, TRUE
);
345 gw_delref(&c
->gw_info
);
347 lock_certs_and_keys("delete_connection");
348 release_cert(c
->spd
.this.cert
);
349 scx_release(c
->spd
.this.sc
);
350 release_cert(c
->spd
.that
.cert
);
351 scx_release(c
->spd
.that
.sc
);
352 unlock_certs_and_keys("delete_connection");
354 alg_info_delref((struct alg_info
**)&c
->alg_info_esp
);
355 alg_info_delref((struct alg_info
**)&c
->alg_info_ike
);
360 /* Delete connections with the specified name */
361 void delete_connections_by_name(const char *name
, bool strict
)
363 connection_t
*c
= con_by_name(name
, strict
);
365 for (; c
!= NULL
; c
= con_by_name(name
, FALSE
))
366 delete_connection(c
, FALSE
);
369 void delete_every_connection(void)
371 while (connections
!= NULL
)
372 delete_connection(connections
, TRUE
);
375 void release_dead_interfaces(void)
377 struct host_pair
*hp
;
379 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
384 for (pp
= &hp
->connections
; (p
= *pp
) != NULL
; )
386 if (p
->interface
->change
== IFN_DELETE
)
388 /* this connection's interface is going away */
389 enum connection_kind k
= p
->kind
;
391 release_connection(p
, TRUE
);
393 if (k
<= CK_PERMANENT
)
395 /* The connection should have survived release:
396 * move it to the unoriented_connections list.
402 *pp
= p
->hp_next
; /* advance *pp */
404 p
->hp_next
= unoriented_connections
;
405 unoriented_connections
= p
;
409 /* The connection should have vanished,
410 * but the previous connection remains.
417 pp
= &p
->hp_next
; /* advance pp */
423 /* adjust orientations of connections to reflect newly added interfaces */
424 void check_orientations(void)
426 /* try to orient all the unoriented connections */
428 connection_t
*c
= unoriented_connections
;
430 unoriented_connections
= NULL
;
434 connection_t
*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 connection_t
*c
= hp
->connections
;
470 hp
->connections
= NULL
;
473 connection_t
*nxt
= c
->hp_next
;
477 connect_to_host_pair(c
);
487 static err_t
default_end(struct end
*e
, ip_address
*dflt_nexthop
)
490 const struct af_info
*afi
= aftoinfo(addrtypeof(&e
->host_addr
));
493 return "unknown address family in default_end";
495 /* default ID to IP (but only if not NO_IP -- WildCard) */
496 if (e
->id
.kind
== ID_ANY
&& !isanyaddr(&e
->host_addr
))
498 e
->id
.kind
= afi
->id_addr
;
499 e
->id
.ip_addr
= e
->host_addr
;
500 e
->has_id_wildcards
= FALSE
;
503 /* default nexthop to other side */
504 if (isanyaddr(&e
->host_nexthop
))
505 e
->host_nexthop
= *dflt_nexthop
;
507 /* default client to subnet containing only self
508 * XXX This may mean that the client's address family doesn't match
509 * tunnel_addr_family.
512 ugh
= addrtosubnet(&e
->host_addr
, &e
->client
);
517 /* Format the topology of a connection end, leaving out defaults.
518 * Largest left end looks like: client === host : port [ host_id ] --- hop
519 * Note: if that==NULL, skip nexthop
520 * Returns strlen of formated result (length excludes NUL at end).
522 size_t format_end(char *buf
, size_t buf_len
, const struct end
*this,
523 const struct end
*that
, bool is_left
, lset_t policy
)
525 char client
[SUBNETTOT_BUF
];
526 const char *client_sep
= "";
527 char protoport
[sizeof(":255/65535")];
528 const char *host
= NULL
;
529 char host_space
[ADDRTOT_BUF
];
530 char host_port
[sizeof(":65535")];
531 char host_id
[BUF_LEN
+ 2];
532 char hop
[ADDRTOT_BUF
];
533 const char *hop_sep
= "";
534 const char *open_brackets
= "";
535 const char *close_brackets
= "";
537 if (isanyaddr(&this->host_addr
))
539 switch (policy
& (POLICY_GROUP
| POLICY_OPPO
))
545 host
= "%opportunistic";
547 case POLICY_GROUP
| POLICY_OPPO
:
548 host
= "%opportunisticgroup";
558 if (is_virtual_end(this) && isanyaddr(&this->host_addr
))
564 if (this->has_client
)
566 ip_address client_net
, client_mask
;
568 networkof(&this->client
, &client_net
);
569 maskof(&this->client
, &client_mask
);
572 /* {client_subnet_wildcard} */
573 if (this->has_client_wildcard
)
576 close_brackets
= "}";
579 if (isanyaddr(&client_net
) && isanyaddr(&client_mask
)
580 && (policy
& (POLICY_GROUP
| POLICY_OPPO
)))
581 client_sep
= ""; /* boring case */
582 else if (subnetisnone(&this->client
))
585 subnettot(&this->client
, 0, client
, sizeof(client
));
587 else if (this->modecfg
&& isanyaddr(&this->host_srcip
))
589 /* we are mode config client */
591 strcpy(client
, "%modecfg");
597 addrtot(&this->host_addr
, 0, host_space
, sizeof(host_space
));
602 if (this->host_port
!= IKE_UDP_PORT
)
603 snprintf(host_port
, sizeof(host_port
), ":%u"
606 /* payload portocol and port */
608 if (this->has_port_wildcard
)
609 snprintf(protoport
, sizeof(protoport
), ":%u/%%any", this->protocol
);
610 else if (this->port
|| this->protocol
)
611 snprintf(protoport
, sizeof(protoport
), ":%u/%u", this->protocol
614 /* id, if different from host */
616 if (this->id
.kind
== ID_MYID
)
618 strcpy(host_id
, "[%myid]");
620 else if (!(this->id
.kind
== ID_ANY
621 || (id_is_ipaddr(&this->id
) && sameaddr(&this->id
.ip_addr
, &this->host_addr
))))
623 int len
= idtoa(&this->id
, host_id
+1, sizeof(host_id
)-2);
626 strcpy(&host_id
[len
< 0?
(ptrdiff_t)sizeof(host_id
)-2 : 1 + len
], "]");
632 if (that
!= NULL
&& !sameaddr(&this->host_nexthop
, &that
->host_addr
))
634 addrtot(&this->host_nexthop
, 0, hop
, sizeof(hop
));
639 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
640 , open_brackets
, client
, close_brackets
, client_sep
641 , this->allow_any?
"%":""
642 , host
, host_port
, host_id
, protoport
645 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
647 , this->allow_any?
"%":""
648 , host
, host_port
, host_id
, protoport
, client_sep
649 , open_brackets
, client
, close_brackets
);
653 /* format topology of a connection.
654 * Two symmetric ends separated by ...
656 #define CONNECTION_BUF (2 * (END_BUF - 1) + 4)
658 static size_t format_connection(char *buf
, size_t buf_len
,
659 const connection_t
*c
,
660 struct spd_route
*sr
)
662 size_t w
= format_end(buf
, buf_len
, &sr
->this, &sr
->that
, TRUE
, LEMPTY
);
664 w
+= snprintf(buf
+ w
, buf_len
- w
, "...");
665 return w
+ format_end(buf
+ w
, buf_len
- w
, &sr
->that
, &sr
->this, FALSE
, c
->policy
);
668 static void unshare_connection_strings(connection_t
*c
)
670 c
->name
= clone_str(c
->name
);
672 unshare_id_content(&c
->spd
.this.id
);
673 c
->spd
.this.updown
= clone_str(c
->spd
.this.updown
);
674 scx_share(c
->spd
.this.sc
);
675 share_cert(c
->spd
.this.cert
);
676 c
->spd
.this.ca
= chunk_clone(c
->spd
.this.ca
);
678 unshare_id_content(&c
->spd
.that
.id
);
679 c
->spd
.that
.updown
= clone_str(c
->spd
.that
.updown
);
680 scx_share(c
->spd
.that
.sc
);
681 share_cert(c
->spd
.that
.cert
);
682 c
->spd
.that
.ca
= chunk_clone(c
->spd
.that
.ca
);
684 /* increment references to algo's */
685 alg_info_addref((struct alg_info
*)c
->alg_info_esp
);
686 alg_info_addref((struct alg_info
*)c
->alg_info_ike
);
689 static void load_end_certificate(char *filename
, struct end
*dst
)
693 bool valid_cert
= FALSE
;
694 bool cached_cert
= FALSE
;
696 /* initialize end certificate */
697 dst
->cert
.type
= CERT_NONE
;
698 dst
->cert
.u
.x509
= NULL
;
700 /* initialize smartcard info record */
703 if (filename
!= NULL
)
705 if (scx_on_smartcard(filename
))
707 /* load cert from smartcard */
708 valid_cert
= scx_load_cert(filename
, &dst
->sc
, &cert
, &cached_cert
);
712 /* load cert from file */
713 valid_cert
= load_host_cert(filename
, &cert
);
724 select_pgpcert_id(cert
.u
.pgp
, &dst
->id
);
730 valid_until
= cert
.u
.pgp
->until
;
731 add_pgp_public_key(cert
.u
.pgp
, cert
.u
.pgp
->until
, DAL_LOCAL
);
732 dst
->cert
.type
= cert
.type
;
733 dst
->cert
.u
.pgp
= add_pgpcert(cert
.u
.pgp
);
736 case CERT_X509_SIGNATURE
:
737 select_x509cert_id(cert
.u
.x509
, &dst
->id
);
743 time_t valid_until
= 0;
745 /* check validity of cert */
746 ugh
= check_validity(cert
.u
.x509
, &valid_until
);
750 free_x509cert(cert
.u
.x509
);
755 DBG_log("certificate is valid")
757 add_x509_public_key(cert
.u
.x509
, valid_until
, DAL_LOCAL
);
758 dst
->cert
.type
= cert
.type
;
759 dst
->cert
.u
.x509
= add_x509cert(cert
.u
.x509
);
762 /* if no CA is defined, use issuer as default */
763 if (dst
->ca
.ptr
== NULL
)
765 certificate_t
*certificate
= dst
->cert
.u
.x509
->cert
;
766 identification_t
*issuer
= certificate
->get_issuer(certificate
);
768 dst
->ca
= issuer
->get_encoding(issuer
);
775 /* cache the certificate that was last retrieved from the smartcard */
778 if (!same_cert(&dst
->sc
->last_cert
, &dst
->cert
))
780 lock_certs_and_keys("load_end_certificates");
781 release_cert(dst
->sc
->last_cert
);
782 dst
->sc
->last_cert
= dst
->cert
;
783 share_cert(dst
->cert
);
784 unlock_certs_and_keys("load_end_certificates");
786 time(&dst
->sc
->last_load
);
791 static bool extract_end(struct end
*dst
, const whack_end_t
*src
,
794 bool same_ca
= FALSE
;
796 /* decode id, if any */
799 dst
->id
.kind
= ID_ANY
;
803 err_t ugh
= atoid(src
->id
, &dst
->id
, TRUE
);
807 loglog(RC_BADID
, "bad %s --id: %s (ignored)", which
, ugh
);
808 dst
->id
= empty_id
; /* ignore bad one */
812 dst
->ca
= chunk_empty
;
814 /* decode CA distinguished name, if any */
817 if streq(src
->ca
, "%same")
819 else if (!streq(src
->ca
, "%any"))
823 dst
->ca
.ptr
= temporary_cyclic_buffer();
824 ugh
= atodn(src
->ca
, &dst
->ca
);
827 plog("bad CA string '%s': %s (ignored)", src
->ca
, ugh
);
828 dst
->ca
= chunk_empty
;
833 /* load local end certificate and extract ID, if any */
834 load_end_certificate(src
->cert
, dst
);
836 /* does id has wildcards? */
837 dst
->has_id_wildcards
= id_count_wildcards(&dst
->id
) > 0;
839 /* decode group attributes, if any */
840 decode_groups(src
->groups
, &dst
->groups
);
842 /* the rest is simple copying of corresponding fields */
843 dst
->host_addr
= src
->host_addr
;
844 dst
->host_nexthop
= src
->host_nexthop
;
845 dst
->host_srcip
= src
->host_srcip
;
846 dst
->has_natip
= src
->has_natip
;
847 dst
->client
= src
->client
;
848 dst
->protocol
= src
->protocol
;
849 dst
->port
= src
->port
;
850 dst
->has_port_wildcard
= src
->has_port_wildcard
;
851 dst
->key_from_DNS_on_demand
= src
->key_from_DNS_on_demand
;
852 dst
->has_client
= src
->has_client
;
853 dst
->has_client_wildcard
= src
->has_client_wildcard
;
854 dst
->modecfg
= src
->modecfg
;
855 dst
->hostaccess
= src
->hostaccess
;
856 dst
->allow_any
= src
->allow_any
;
857 dst
->sendcert
= src
->sendcert
;
858 dst
->updown
= src
->updown
;
859 dst
->host_port
= src
->host_port
;
861 /* if host sourceip is defined but no client is present
862 * behind the host then set client to sourceip/32
864 if (addrbytesptr(&dst
->host_srcip
, NULL
)
865 && !isanyaddr(&dst
->host_srcip
)
869 err_t ugh
= addrtosubnet(&dst
->host_srcip
, &dst
->client
);
872 plog("could not assign host sourceip to client subnet");
874 dst
->has_client
= TRUE
;
879 static bool check_connection_end(const whack_end_t
*this,
880 const whack_end_t
*that
,
881 const whack_message_t
*wm
)
883 if (wm
->addr_family
!= addrtypeof(&this->host_addr
)
884 || wm
->addr_family
!= addrtypeof(&this->host_nexthop
)
885 || (this->has_client? wm
->tunnel_addr_family
: wm
->addr_family
)
886 != subnettypeof(&this->client
)
887 || subnettypeof(&this->client
) != subnettypeof(&that
->client
))
889 /* this should have been diagnosed by whack, so we need not be clear
890 * !!! overloaded use of RC_CLASH
892 loglog(RC_CLASH
, "address family inconsistency in connection");
896 if (isanyaddr(&that
->host_addr
))
898 /* other side is wildcard: we must check if other conditions met */
899 if (isanyaddr(&this->host_addr
))
901 loglog(RC_ORIENT
, "connection must specify host IP address for our side");
906 if (this->virt
&& (!isanyaddr(&this->host_addr
) || this->has_client
))
909 "virtual IP must only be used with %%any and without client");
913 return TRUE
; /* happy */
916 connection_t
*find_connection_by_reqid(uint32_t reqid
)
921 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
923 if (c
->spd
.reqid
== reqid
)
930 static uint32_t gen_reqid(void)
933 static uint32_t reqid
= IPSEC_MANUAL_REQID_MAX
& ~3;
939 reqid
= (IPSEC_MANUAL_REQID_MAX
& ~3) + 4;
940 if (!find_connection_by_reqid(reqid
))
942 } while (reqid
!= start
);
944 exit_log("unable to allocate reqid");
945 return 0; /* never reached ... */
948 void add_connection(const whack_message_t
*wm
)
950 if (con_by_name(wm
->name
, FALSE
) != NULL
)
952 loglog(RC_DUPNAME
, "attempt to redefine connection \"%s\"", wm
->name
);
954 else if (wm
->right
.protocol
!= wm
->left
.protocol
)
956 /* this should haven been diagnosed by whack
957 * !!! overloaded use of RC_CLASH
959 loglog(RC_CLASH
, "the protocol must be the same for leftport and rightport");
961 else if (check_connection_end(&wm
->right
, &wm
->left
, wm
)
962 && check_connection_end(&wm
->left
, &wm
->right
, wm
))
964 bool same_rightca
, same_leftca
;
965 connection_t
*c
= malloc_thing(connection_t
);
969 c
->ikev1
= wm
->ikev1
;
970 c
->policy
= wm
->policy
;
972 if ((c
->policy
& POLICY_COMPRESS
) && !can_do_IPcomp
)
974 , "ignoring --compress in \"%s\" because KLIPS is not configured to do IPCOMP"
980 DBG_log("from whack: got --esp=%s", wm
->esp ? wm
->esp
: "NULL")
982 c
->alg_info_esp
= alg_info_esp_create_from_str(wm
->esp? wm
->esp
: "");
984 DBG(DBG_CRYPT
|DBG_CONTROL
,
985 static char buf
[BUF_LEN
]="<NULL>";
988 alg_info_snprint(buf
, sizeof(buf
)
989 ,(struct alg_info
*)c
->alg_info_esp
);
990 DBG_log("esp proposal: %s", buf
);
994 if (c
->alg_info_esp
->alg_info_cnt
==0)
995 loglog(RC_LOG_SERIOUS
996 , "got 0 transforms for esp=\"%s\"", wm
->esp
);
1000 loglog(RC_LOG_SERIOUS
, "esp string error");
1007 DBG_log("from whack: got --ike=%s", wm
->ike ? wm
->ike
: "NULL")
1009 c
->alg_info_ike
= alg_info_ike_create_from_str(wm
->ike? wm
->ike
: "");
1011 DBG(DBG_CRYPT
|DBG_CONTROL
,
1012 static char buf
[BUF_LEN
]="<NULL>";
1014 if (c
->alg_info_ike
)
1015 alg_info_snprint(buf
, sizeof(buf
)
1016 , (struct alg_info
*)c
->alg_info_ike
);
1017 DBG_log("ike proposal: %s", buf
);
1019 if (c
->alg_info_ike
)
1021 if (c
->alg_info_ike
->alg_info_cnt
==0)
1022 loglog(RC_LOG_SERIOUS
1023 , "got 0 transforms for ike=\"%s\"", wm
->ike
);
1027 loglog(RC_LOG_SERIOUS
, "ike string error:");
1031 c
->sa_ike_life_seconds
= wm
->sa_ike_life_seconds
;
1032 c
->sa_ipsec_life_seconds
= wm
->sa_ipsec_life_seconds
;
1033 c
->sa_rekey_margin
= wm
->sa_rekey_margin
;
1034 c
->sa_rekey_fuzz
= wm
->sa_rekey_fuzz
;
1035 c
->sa_keying_tries
= wm
->sa_keying_tries
;
1038 c
->dpd_delay
= wm
->dpd_delay
;
1039 c
->dpd_timeout
= wm
->dpd_timeout
;
1040 c
->dpd_action
= wm
->dpd_action
;
1042 c
->addr_family
= wm
->addr_family
;
1043 c
->tunnel_addr_family
= wm
->tunnel_addr_family
;
1045 c
->requested_ca
= NULL
;
1047 same_leftca
= extract_end(&c
->spd
.this, &wm
->left
, "left");
1048 same_rightca
= extract_end(&c
->spd
.that
, &wm
->right
, "right");
1051 c
->spd
.that
.ca
= c
->spd
.this.ca
;
1052 else if (same_leftca
)
1053 c
->spd
.this.ca
= c
->spd
.that
.ca
;
1055 default_end(&c
->spd
.this, &c
->spd
.that
.host_addr
);
1056 default_end(&c
->spd
.that
, &c
->spd
.this.host_addr
);
1058 /* force any wildcard host IP address, any wildcard subnet
1059 * or any wildcard ID to that end
1061 if (isanyaddr(&c
->spd
.this.host_addr
) || c
->spd
.this.has_client_wildcard
1062 || c
->spd
.this.has_port_wildcard
|| c
->spd
.this.has_id_wildcards
1063 || c
->spd
.this.allow_any
)
1065 struct end t
= c
->spd
.this;
1067 c
->spd
.this = c
->spd
.that
;
1072 c
->spd
.reqid
= gen_reqid();
1074 /* set internal fields */
1075 c
->instance_serial
= 0;
1076 c
->ac_next
= connections
;
1078 c
->interface
= NULL
;
1079 c
->spd
.routing
= RT_UNROUTED
;
1080 c
->newest_isakmp_sa
= SOS_NOBODY
;
1081 c
->newest_ipsec_sa
= SOS_NOBODY
;
1082 c
->spd
.eroute_owner
= SOS_NOBODY
;
1084 if (c
->policy
& POLICY_GROUP
)
1089 else if ((isanyaddr(&c
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(c
->policy
))
1090 || c
->spd
.that
.has_client_wildcard
|| c
->spd
.that
.has_port_wildcard
1091 || c
->spd
.that
.has_id_wildcards
|| c
->spd
.that
.allow_any
)
1093 /* Opportunistic or Road Warrior or wildcard client subnet
1095 c
->kind
= CK_TEMPLATE
;
1099 c
->kind
= CK_PERMANENT
;
1101 set_policy_prio(c
); /* must be after kind is set */
1104 c
->extra_debugging
= wm
->debugging
;
1109 passert(!(wm
->left
.virt
&& wm
->right
.virt
));
1110 if (wm
->left
.virt
|| wm
->right
.virt
)
1112 passert(isanyaddr(&c
->spd
.that
.host_addr
));
1113 c
->spd
.that
.virt
= create_virtual(c
,
1114 wm
->left
.virt ? wm
->left
.virt
: wm
->right
.virt
);
1115 if (c
->spd
.that
.virt
)
1116 c
->spd
.that
.has_client
= TRUE
;
1119 unshare_connection_strings(c
);
1123 connect_to_host_pair(c
);
1125 /* log all about this connection */
1126 plog("added connection description \"%s\"", c
->name
);
1128 char topo
[CONNECTION_BUF
];
1130 (void) format_connection(topo
, sizeof(topo
), c
, &c
->spd
);
1132 DBG_log("%s", topo
);
1134 /* Make sure that address families can be correctly inferred
1135 * from printed ends.
1137 passert(c
->addr_family
== addrtypeof(&c
->spd
.this.host_addr
)
1138 && c
->addr_family
== addrtypeof(&c
->spd
.this.host_nexthop
)
1139 && (c
->spd
.this.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1140 == subnettypeof(&c
->spd
.this.client
)
1142 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_addr
)
1143 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_nexthop
)
1144 && (c
->spd
.that
.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1145 == subnettypeof(&c
->spd
.that
.client
));
1147 DBG_log("ike_life: %lus; ipsec_life: %lus; rekey_margin: %lus;"
1148 " rekey_fuzz: %lu%%; keyingtries: %lu; policy: %s"
1149 , (unsigned long) c
->sa_ike_life_seconds
1150 , (unsigned long) c
->sa_ipsec_life_seconds
1151 , (unsigned long) c
->sa_rekey_margin
1152 , (unsigned long) c
->sa_rekey_fuzz
1153 , (unsigned long) c
->sa_keying_tries
1154 , prettypolicy(c
->policy
));
1159 /* Derive a template connection from a group connection and target.
1160 * Similar to instantiate(). Happens at whack --listen.
1161 * Returns name of new connection. May be NULL.
1162 * Caller is responsible for freeing.
1164 char *add_group_instance(connection_t
*group
, const ip_subnet
*target
)
1166 char namebuf
[100], targetbuf
[SUBNETTOT_BUF
];
1170 passert(group
->kind
== CK_GROUP
);
1171 passert(oriented(*group
));
1173 /* manufacture a unique name for this template */
1174 subnettot(target
, 0, targetbuf
, sizeof(targetbuf
));
1175 snprintf(namebuf
, sizeof(namebuf
), "%s#%s", group
->name
, targetbuf
);
1177 if (con_by_name(namebuf
, FALSE
) != NULL
)
1179 loglog(RC_DUPNAME
, "group name + target yields duplicate name \"%s\""
1184 t
= clone_thing(*group
);
1186 unshare_connection_strings(t
);
1187 name
= clone_str(t
->name
);
1188 t
->spd
.that
.client
= *target
;
1189 t
->policy
&= ~(POLICY_GROUP
| POLICY_GROUTED
);
1190 t
->kind
= isanyaddr(&t
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(t
->policy
)
1191 ? CK_TEMPLATE
: CK_INSTANCE
;
1193 /* reset log file info */
1194 t
->log_file_name
= NULL
;
1196 t
->log_file_err
= FALSE
;
1198 t
->spd
.reqid
= gen_reqid();
1200 if (t
->spd
.that
.virt
)
1202 DBG_log("virtual_ip not supported in group instance");
1203 t
->spd
.that
.virt
= NULL
;
1206 /* add to connections list */
1207 t
->ac_next
= connections
;
1210 /* same host_pair as parent: stick after parent on list */
1213 /* route if group is routed */
1214 if (group
->policy
& POLICY_GROUTED
)
1216 if (!trap_connection(t
))
1217 whack_log(RC_ROUTE
, "could not route");
1223 /* an old target has disappeared for a group: delete instance */
1224 void remove_group_instance(const connection_t
*group USED_BY_DEBUG
,
1227 passert(group
->kind
== CK_GROUP
);
1228 passert(oriented(*group
));
1230 delete_connections_by_name(name
, FALSE
);
1233 /* Common part of instantiating a Road Warrior or Opportunistic connection.
1234 * his_id can be used to carry over an ID discovered in Phase 1.
1235 * It must not disagree with the one in c, but if that is unspecified,
1236 * the new connection will use his_id.
1237 * If his_id is NULL, and c.that.id is uninstantiated (ID_ANY), the
1238 * new connection will continue to have an uninstantiated that.id.
1239 * Note: instantiation does not affect port numbers.
1241 * Note that instantiate can only deal with a single SPD/eroute.
1243 static connection_t
*instantiate(connection_t
*c
,
1244 const ip_address
*him
, u_int16_t his_port
,
1245 const struct id
*his_id
)
1250 passert(c
->kind
== CK_TEMPLATE
);
1251 passert(c
->spd
.next
== NULL
);
1253 c
->instance_serial
++;
1254 d
= clone_thing(*c
);
1255 d
->spd
.that
.allow_any
= FALSE
;
1259 passert(match_id(his_id
, &d
->spd
.that
.id
, &wildcards
));
1260 d
->spd
.that
.id
= *his_id
;
1261 d
->spd
.that
.has_id_wildcards
= FALSE
;
1263 unshare_connection_strings(d
);
1264 unshare_ietfAttrList(&d
->spd
.this.groups
);
1265 unshare_ietfAttrList(&d
->spd
.that
.groups
);
1266 d
->kind
= CK_INSTANCE
;
1268 passert(oriented(*d
));
1269 d
->spd
.that
.host_addr
= *him
;
1270 setportof(htons(c
->spd
.that
.port
), &d
->spd
.that
.host_addr
);
1272 if (his_port
) d
->spd
.that
.host_port
= his_port
;
1274 default_end(&d
->spd
.that
, &d
->spd
.this.host_addr
);
1276 /* We cannot guess what our next_hop should be, but if it was
1277 * explicitly specified as 0.0.0.0, we set it to be him.
1278 * (whack will not allow nexthop to be elided in RW case.)
1280 default_end(&d
->spd
.this, &d
->spd
.that
.host_addr
);
1282 d
->spd
.reqid
= gen_reqid();
1284 /* set internal fields */
1285 d
->ac_next
= connections
;
1287 d
->spd
.routing
= RT_UNROUTED
;
1288 d
->newest_isakmp_sa
= SOS_NOBODY
;
1289 d
->newest_ipsec_sa
= SOS_NOBODY
;
1290 d
->spd
.eroute_owner
= SOS_NOBODY
;
1292 /* reset log file info */
1293 d
->log_file_name
= NULL
;
1295 d
->log_file_err
= FALSE
;
1297 connect_to_host_pair(d
);
1300 if (sameaddr(&d
->spd
.that
.host_addr
, &d
->spd
.this.host_nexthop
))
1302 d
->spd
.this.host_nexthop
= *him
;
1306 connection_t
*rw_instantiate(connection_t
*c
, const ip_address
*him
,
1307 u_int16_t his_port
, const ip_subnet
*his_net
,
1308 const struct id
*his_id
)
1310 connection_t
*d
= instantiate(c
, him
, his_port
, his_id
);
1312 if (d
&& his_net
&& is_virtual_connection(c
))
1314 d
->spd
.that
.client
= *his_net
;
1315 d
->spd
.that
.virt
= NULL
;
1316 if (subnetishost(his_net
) && addrinsubnet(him
, his_net
))
1317 d
->spd
.that
.has_client
= FALSE
;
1320 if (d
->policy
& POLICY_OPPO
)
1322 /* This must be before we know the client addresses.
1323 * Fill in one that is impossible. This prevents anyone else from
1324 * trying to use this connection to get to a particular client
1326 d
->spd
.that
.client
= *aftoinfo(subnettypeof(&d
->spd
.that
.client
))->none
;
1329 , DBG_log("instantiated \"%s\" for %s" , d
->name
, ip_str(him
)));
1333 connection_t
*oppo_instantiate(connection_t
*c
, const ip_address
*him
,
1334 const struct id
*his_id
, struct gw_info
*gw
,
1335 const ip_address
*our_client USED_BY_DEBUG
,
1336 const ip_address
*peer_client
)
1338 connection_t
*d
= instantiate(c
, him
, 0, his_id
);
1340 passert(d
->spd
.next
== NULL
);
1342 /* fill in our client side */
1343 if (d
->spd
.this.has_client
)
1345 /* there was a client in the abstract connection
1346 * so we demand that the required client is within that subnet.
1348 passert(addrinsubnet(our_client
, &d
->spd
.this.client
));
1349 happy(addrtosubnet(our_client
, &d
->spd
.this.client
));
1350 /* opportunistic connections do not use port selectors */
1351 setportof(0, &d
->spd
.this.client
.addr
);
1355 /* there was no client in the abstract connection
1356 * so we demand that the required client be the host
1358 passert(sameaddr(our_client
, &d
->spd
.this.host_addr
));
1361 /* fill in peer's client side.
1362 * If the client is the peer, excise the client from the connection.
1364 passert((d
->policy
& POLICY_OPPO
)
1365 && addrinsubnet(peer_client
, &d
->spd
.that
.client
));
1366 happy(addrtosubnet(peer_client
, &d
->spd
.that
.client
));
1367 /* opportunistic connections do not use port selectors */
1368 setportof(0, &d
->spd
.that
.client
.addr
);
1370 if (sameaddr(peer_client
, &d
->spd
.that
.host_addr
))
1371 d
->spd
.that
.has_client
= FALSE
;
1373 passert(d
->gw_info
== NULL
);
1377 /* Adjust routing if something is eclipsing c.
1378 * It must be a %hold for us (hard to passert this).
1379 * If there was another instance eclipsing, we'd be using it.
1381 if (c
->spd
.routing
== RT_ROUTED_ECLIPSED
)
1382 d
->spd
.routing
= RT_ROUTED_PROSPECTIVE
;
1384 /* Remember if the template is routed:
1385 * if so, this instance applies for initiation
1386 * even if it is created for responding.
1388 if (routed(c
->spd
.routing
))
1389 d
->instance_initiation_ok
= TRUE
;
1392 char topo
[CONNECTION_BUF
];
1394 (void) format_connection(topo
, sizeof(topo
), d
, &d
->spd
);
1395 DBG_log("instantiated \"%s\": %s", d
->name
, topo
);
1400 /* priority formatting */
1401 void fmt_policy_prio(policy_prio_t pp
, char buf
[POLICY_PRIO_BUF
])
1403 if (pp
== BOTTOM_PRIO
)
1404 snprintf(buf
, POLICY_PRIO_BUF
, "0");
1406 snprintf(buf
, POLICY_PRIO_BUF
, "%lu,%lu"
1407 , pp
>>16, (pp
& ~(~(policy_prio_t
)0 << 16)) >> 8);
1410 /* Format any information needed to identify an instance of a connection.
1411 * Fills any needed information into buf which MUST be big enough.
1412 * Road Warrior: peer's IP address
1413 * Opportunistic: [" " myclient "==="] " ..." peer ["===" hisclient] '\0'
1415 static size_t fmt_client(const ip_subnet
*client
, const ip_address
*gw
,
1416 const char *prefix
, char buf
[ADDRTOT_BUF
])
1418 if (subnetisaddr(client
, gw
))
1420 buf
[0] = '\0'; /* compact denotation for "self" */
1426 strcpy(buf
, prefix
);
1427 ap
= buf
+ strlen(prefix
);
1428 if (subnetisnone(client
))
1429 strcpy(ap
, "?"); /* unknown */
1431 subnettot(client
, 0, ap
, SUBNETTOT_BUF
);
1436 void fmt_conn_instance(const connection_t
*c
, char buf
[CONN_INST_BUF
])
1442 if (c
->kind
== CK_INSTANCE
)
1444 if (c
->instance_serial
!= 0)
1446 snprintf(p
, CONN_INST_BUF
, "[%lu]", c
->instance_serial
);
1450 if (c
->policy
& POLICY_OPPO
)
1452 size_t w
= fmt_client(&c
->spd
.this.client
, &c
->spd
.this.host_addr
, " ", p
);
1456 strcpy(p
, w
== 0?
" ..." : "=== ...");
1459 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1462 (void) fmt_client(&c
->spd
.that
.client
, &c
->spd
.that
.host_addr
, "===", p
);
1467 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1469 if (c
->spd
.that
.host_port
!= pluto_port
)
1472 sprintf(p
, ":%d", c
->spd
.that
.host_port
);
1478 /* Find an existing connection for a trapped outbound packet.
1479 * This is attempted before we bother with gateway discovery.
1480 * + this connection is routed or instance_of_routed_template
1481 * (i.e. approved for on-demand)
1482 * + this subnet contains our_client (or we are our_client)
1483 * + that subnet contains peer_client (or peer is peer_client)
1484 * + don't care about Phase 1 IDs (we don't know)
1485 * Note: result may still need to be instantiated.
1486 * The winner has the highest policy priority.
1488 * If there are several with that priority, we give preference to
1489 * the first one that is an instance.
1491 * See also build_outgoing_opportunistic_connection.
1493 connection_t
*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 connection_t
*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.
1636 connection_t
*build_outgoing_opportunistic_connection(struct gw_info
*gw
,
1637 const ip_address
*our_client
,
1638 const ip_address
*peer_client
)
1641 connection_t
*best
= NULL
;
1642 struct spd_route
*sr
, *bestsr
;
1643 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1645 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1646 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1648 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1650 /* We don't know his ID yet, so gw id must be an ipaddr */
1651 passert(gw
->key
!= NULL
);
1652 passert(id_is_ipaddr(&gw
->gw_id
));
1654 /* for each of our addresses... */
1655 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1657 /* go through those connections with our address and NO_IP as hosts
1658 * We cannot know what port the peer would use, so we assume
1659 * that it is pluto_port (makes debugging easier).
1661 connection_t
*c
= find_host_pair_connections(&p
->addr
, pluto_port
,
1662 (ip_address
*)NULL
, pluto_port
);
1664 for (; c
!= NULL
; c
= c
->hp_next
)
1667 DBG_log("checking %s", c
->name
));
1668 if (c
->kind
== CK_GROUP
)
1673 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1675 if (routed(sr
->routing
)
1676 && addrinsubnet(our_client
, &sr
->this.client
)
1677 && addrinsubnet(peer_client
, &sr
->that
.client
))
1686 DBG_log("comparing best %s to %s"
1687 , best
->name
, c
->name
));
1689 for (bestsr
= &best
->spd
; best
!=c
&& bestsr
; bestsr
=bestsr
->next
)
1691 if (!subnetinsubnet(&bestsr
->this.client
, &sr
->this.client
)
1692 || (samesubnet(&bestsr
->this.client
, &sr
->this.client
)
1693 && !subnetinsubnet(&bestsr
->that
.client
1694 , &sr
->that
.client
)))
1705 || NEVER_NEGOTIATE(best
->policy
)
1706 || (best
->policy
& POLICY_OPPO
) == LEMPTY
1707 || best
->kind
!= CK_TEMPLATE
)
1710 return oppo_instantiate(best
, &gw
->gw_id
.ip_addr
, NULL
, gw
1711 , our_client
, peer_client
);
1714 bool orient(connection_t
*c
)
1716 struct spd_route
*sr
;
1722 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
1724 /* Note: this loop does not stop when it finds a match:
1725 * it continues checking to catch any ambiguity.
1727 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1734 /* check if this interface matches this end */
1735 if (sameaddr(&sr
->this.host_addr
, &p
->addr
)
1736 && (!no_klips
|| sr
->this.host_port
== pluto_port
))
1740 if (c
->interface
== p
)
1741 loglog(RC_LOG_SERIOUS
1742 , "both sides of \"%s\" are our interface %s!"
1743 , c
->name
, p
->rname
);
1745 loglog(RC_LOG_SERIOUS
, "two interfaces match \"%s\" (%s, %s)"
1746 , c
->name
, c
->interface
->rname
, p
->rname
);
1747 c
->interface
= NULL
; /* withdraw orientation */
1753 /* done with this interface if it doesn't match that end */
1754 if (!(sameaddr(&sr
->that
.host_addr
, &p
->addr
)
1755 && (!no_klips
|| sr
->that
.host_port
== pluto_port
)))
1758 /* swap ends and try again.
1759 * It is a little tricky to see that this loop will stop.
1760 * Only continue if the far side matches.
1761 * If both sides match, there is an error-out.
1764 struct end t
= sr
->this;
1766 sr
->this = sr
->that
;
1773 return oriented(*c
);
1776 void initiate_connection(const char *name
, int whackfd
)
1778 connection_t
*c
= con_by_name(name
, TRUE
);
1780 if (c
!= NULL
&& c
->ikev1
)
1782 set_cur_connection(c
);
1785 loglog(RC_ORIENT
, "we have no ipsecN interface for either end of this connection");
1787 else if (NEVER_NEGOTIATE(c
->policy
))
1790 , "cannot initiate an authby=never connection");
1792 else if (c
->kind
!= CK_PERMANENT
&& !c
->spd
.that
.allow_any
)
1794 if (isanyaddr(&c
->spd
.that
.host_addr
))
1795 loglog(RC_NOPEERIP
, "cannot initiate connection without knowing peer IP address");
1797 loglog(RC_WILDCARD
, "cannot initiate connection with ID wildcards");
1801 /* do we have to prompt for a PIN code? */
1802 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
&& whackfd
!= NULL_FD
)
1804 scx_get_pin(c
->spd
.this.sc
, whackfd
);
1806 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
)
1808 loglog(RC_NOVALIDPIN
, "cannot initiate connection without valid PIN");
1813 if (c
->spd
.that
.allow_any
)
1815 c
= instantiate(c
, &c
->spd
.that
.host_addr
, c
->spd
.that
.host_port
1819 /* We will only request an IPsec SA if policy isn't empty
1820 * (ignoring Main Mode items).
1821 * This is a fudge, but not yet important.
1822 * If we are to proceed asynchronously, whackfd will be NULL_FD.
1824 c
->policy
|= POLICY_UP
;
1825 ipsecdoi_initiate(whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
1826 whackfd
= NULL_FD
; /* protect from close */
1829 reset_cur_connection();
1834 /* (Possibly) Opportunistic Initiation:
1835 * Knowing clients (single IP addresses), try to build an tunnel.
1836 * This may involve discovering a gateway and instantiating an
1837 * Opportunistic connection. Called when a packet is caught by
1838 * a %trap, or when whack --oppohere --oppothere is used.
1839 * It may turn out that an existing or non-opporunistic connnection
1840 * can handle the traffic.
1842 * Most of the code will be restarted if an ADNS request is made
1843 * to discover the gateway. The only difference between the first
1844 * and second entry is whether gateways_from_dns is NULL or not.
1845 * initiate_opportunistic: initial entrypoint
1846 * continue_oppo: where we pickup when ADNS result arrives
1847 * initiate_opportunistic_body: main body shared by above routines
1848 * cannot_oppo: a helper function to log a diagnostic
1849 * This structure repeats a lot of code when the ADNS result arrives.
1850 * This seems like a waste, but anything learned the first time through
1851 * may no longer be true!
1853 * After the first IKE message is sent, the regular state machinery
1854 * carries negotiation forward.
1857 enum find_oppo_step
{
1860 fos_myid_hostname_txt
,
1862 fos_myid_hostname_key
,
1867 #endif /* USE_KEYRR */
1873 static const char *const oppo_step_name
[] = {
1876 "fos_myid_hostname_txt",
1878 "fos_myid_hostname_key",
1883 #endif /* USE_KEYRR */
1889 struct find_oppo_bundle
{
1890 enum find_oppo_step step
;
1892 bool failure_ok
; /* if true, continue_oppo should not die on DNS failure */
1893 ip_address our_client
; /* not pointer! */
1894 ip_address peer_client
;
1895 int transport_proto
;
1897 policy_prio_t policy_prio
;
1898 ipsec_spi_t failure_shunt
; /* in host order! 0 for delete. */
1902 struct find_oppo_continuation
{
1903 struct adns_continuation ac
; /* common prefix */
1904 struct find_oppo_bundle b
;
1907 static void cannot_oppo(connection_t
*c
, struct find_oppo_bundle
*b
, err_t ugh
)
1909 char pcb
[ADDRTOT_BUF
];
1910 char ocb
[ADDRTOT_BUF
];
1912 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
1913 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
1915 DBG(DBG_DNS
| DBG_OPPO
, DBG_log("Can't Opportunistically initiate for %s to %s: %s"
1918 whack_log(RC_OPPOFAILURE
1919 , "Can't Opportunistically initiate for %s to %s: %s"
1922 if (c
!= NULL
&& c
->policy_next
!= NULL
)
1924 /* there is some policy that comes afterwards */
1925 struct spd_route
*shunt_spd
;
1926 connection_t
*nc
= c
->policy_next
;
1929 passert(c
->kind
== CK_TEMPLATE
);
1930 passert(c
->policy_next
->kind
== CK_PERMANENT
);
1932 DBG(DBG_OPPO
, DBG_log("OE failed for %s to %s, but %s overrides shunt"
1933 , ocb
, pcb
, c
->policy_next
->name
));
1936 * okay, here we need add to the "next" policy, which is ought
1937 * to be an instance.
1938 * We will add another entry to the spd_route list for the specific
1939 * situation that we have.
1942 shunt_spd
= clone_thing(nc
->spd
);
1944 shunt_spd
->next
= nc
->spd
.next
;
1945 nc
->spd
.next
= shunt_spd
;
1947 happy(addrtosubnet(&b
->peer_client
, &shunt_spd
->that
.client
));
1949 if (sameaddr(&b
->peer_client
, &shunt_spd
->that
.host_addr
))
1950 shunt_spd
->that
.has_client
= FALSE
;
1953 * override the tunnel destination with the one from the secondaried
1956 shunt_spd
->that
.host_addr
= nc
->spd
.that
.host_addr
;
1958 /* now, lookup the state, and poke it up.
1961 st
= state_with_serialno(nc
->newest_ipsec_sa
);
1963 /* XXX what to do if the IPSEC SA has died? */
1964 passert(st
!= NULL
);
1966 /* link the new connection instance to the state's list of
1970 DBG(DBG_OPPO
, DBG_log("installing state: %ld for %s to %s"
1971 , nc
->newest_ipsec_sa
1975 if (DBGP(DBG_OPPO
| DBG_CONTROLMORE
))
1977 char state_buf
[LOG_WIDTH
];
1978 char state_buf2
[LOG_WIDTH
];
1981 fmt_state(FALSE
, st
, n
1982 , state_buf
, sizeof(state_buf
)
1983 , state_buf2
, sizeof(state_buf2
));
1984 DBG_log("cannot_oppo, failure SA1: %s", state_buf
);
1985 DBG_log("cannot_oppo, failure SA2: %s", state_buf2
);
1989 if (!route_and_eroute(c
, shunt_spd
, st
))
1991 whack_log(RC_OPPOFAILURE
1992 , "failed to instantiate shunt policy %s for %s to %s"
2002 /* Replace HOLD with b->failure_shunt.
2003 * If no b->failure_shunt specified, use SPI_PASS -- THIS MAY CHANGE.
2005 if (b
->failure_shunt
== 0)
2007 DBG(DBG_OPPO
, DBG_log("no explicit failure shunt for %s to %s; installing %%pass"
2011 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2014 , b
->failure_shunt
!= 0
2015 , b
->transport_proto
2021 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
2022 , struct adns_continuation
*ac
, err_t ac_ugh
); /* forward */
2024 void initiate_opportunistic(const ip_address
*our_client
,
2025 const ip_address
*peer_client
, int transport_proto
,
2026 bool held
, int whackfd
)
2028 struct find_oppo_bundle b
;
2030 b
.want
= (whackfd
== NULL_FD ?
"whack" : "acquire");
2031 b
.failure_ok
= FALSE
;
2032 b
.our_client
= *our_client
;
2033 b
.peer_client
= *peer_client
;
2034 b
.transport_proto
= transport_proto
;
2036 b
.policy_prio
= BOTTOM_PRIO
;
2037 b
.failure_shunt
= 0;
2038 b
.whackfd
= whackfd
;
2040 initiate_opportunistic_body(&b
, NULL
, NULL
);
2043 static void continue_oppo(struct adns_continuation
*acr
, err_t ugh
)
2045 struct find_oppo_continuation
*cr
= (void *)acr
; /* inherit, damn you! */
2047 bool was_held
= cr
->b
.held
;
2048 int whackfd
= cr
->b
.whackfd
;
2050 /* note: cr->id has no resources; cr->sgw_id is ID_ANY:
2051 * neither need freeing.
2053 whack_log_fd
= whackfd
;
2056 /* Discover and record whether %hold has gone away.
2057 * This could have happened while we were awaiting DNS.
2058 * We must check BEFORE any call to cannot_oppo.
2061 cr
->b
.held
= has_bare_hold(&cr
->b
.our_client
, &cr
->b
.peer_client
2062 , cr
->b
.transport_proto
);
2066 /* if we're going to ignore the error, at least note it in debugging log */
2067 if (cr
->b
.failure_ok
&& ugh
!= NULL
)
2069 DBG(DBG_CONTROL
| DBG_DNS
,
2071 char ocb
[ADDRTOT_BUF
];
2072 char pcb
[ADDRTOT_BUF
];
2074 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2075 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2076 DBG_log("continuing from failed DNS lookup for %s, %s to %s: %s"
2077 , cr
->b
.want
, ocb
, pcb
, ugh
);
2082 if (!cr
->b
.failure_ok
&& ugh
!= NULL
)
2084 c
= find_connection_for_clients(NULL
, &cr
->b
.our_client
, &cr
->b
.peer_client
2085 , cr
->b
.transport_proto
);
2086 cannot_oppo(c
, &cr
->b
2087 , builddiag("%s: %s", cr
->b
.want
, ugh
));
2089 else if (was_held
&& !cr
->b
.held
)
2091 /* was_held indicates we were started due to a %trap firing
2092 * (as opposed to a "whack --oppohere --oppothere").
2093 * Since the %hold has gone, we can assume that somebody else
2094 * has beaten us to the punch. We can go home. But lets log it.
2096 char ocb
[ADDRTOT_BUF
];
2097 char pcb
[ADDRTOT_BUF
];
2099 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2100 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2103 , "%%hold otherwise handled during DNS lookup for Opportunistic Initiation for %s to %s"
2108 initiate_opportunistic_body(&cr
->b
, &cr
->ac
, ugh
);
2109 whackfd
= NULL_FD
; /* was handed off */
2112 whack_log_fd
= NULL_FD
;
2117 static err_t
check_key_recs(enum myid_state try_state
, const connection_t
*c
,
2118 struct adns_continuation
*ac
)
2120 /* Check if KEY lookup yielded good results.
2121 * Looking up based on our ID. Used if
2122 * client is ourself, or if TXT had no public key.
2123 * Note: if c is different this time, there is
2124 * a chance that we did the wrong query.
2125 * If so, treat as a kind of failure.
2127 enum myid_state old_myid_state
= myid_state
;
2128 private_key_t
*private;
2131 myid_state
= try_state
;
2133 if (old_myid_state
!= myid_state
&& old_myid_state
== MYID_SPECIFIED
)
2135 ugh
= "%myid was specified while we were guessing";
2137 else if ((private = get_private_key(c
)) == NULL
)
2139 ugh
= "we don't know our own RSA key";
2141 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2143 ugh
= "our ID changed underfoot";
2147 /* Similar to code in RSA_check_signature
2148 * for checking the other side.
2152 ugh
= "no KEY RR found for us";
2153 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2155 ugh
= "all our KEY RRs have the wrong public key";
2156 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2157 && private->belongs_to(private, &kr
->key
->public_key
))
2159 ugh
= NULL
; /* good! */
2165 myid_state
= old_myid_state
;
2168 #endif /* USE_KEYRR */
2170 static err_t
check_txt_recs(enum myid_state try_state
, const connection_t
*c
,
2171 struct adns_continuation
*ac
)
2173 /* Check if TXT lookup yielded good results.
2174 * Looking up based on our ID. Used if
2175 * client is ourself, or if TXT had no public key.
2176 * Note: if c is different this time, there is
2177 * a chance that we did the wrong query.
2178 * If so, treat as a kind of failure.
2180 enum myid_state old_myid_state
= myid_state
;
2181 private_key_t
*private;
2184 myid_state
= try_state
;
2186 if (old_myid_state
!= myid_state
2187 && old_myid_state
== MYID_SPECIFIED
)
2189 ugh
= "%myid was specified while we were guessing";
2191 else if ((private = get_private_key(c
)) == NULL
)
2193 ugh
= "we don't know our own RSA key";
2195 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2197 ugh
= "our ID changed underfoot";
2201 /* Similar to code in RSA_check_signature
2202 * for checking the other side.
2204 struct gw_info
*gwp
;
2206 ugh
= "no TXT RR found for us";
2207 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2209 public_key_t
*pub_key
= gwp
->key
->public_key
;
2211 ugh
= "all our TXT RRs have the wrong public key";
2212 if (pub_key
->get_type(pub_key
) == KEY_RSA
&&
2213 private->belongs_to(private, pub_key
))
2215 ugh
= NULL
; /* good! */
2222 myid_state
= old_myid_state
;
2228 /* note: gateways_from_dns must be NULL iff this is the first call */
2229 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
,
2230 struct adns_continuation
*ac
,
2234 struct spd_route
*sr
;
2236 /* What connection shall we use?
2237 * First try for one that explicitly handles the clients.
2241 char ours
[ADDRTOT_BUF
];
2242 char his
[ADDRTOT_BUF
];
2246 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2247 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2248 ourport
= ntohs(portof(&b
->our_client
));
2249 hisport
= ntohs(portof(&b
->peer_client
));
2250 DBG_log("initiate on demand from %s:%d to %s:%d proto=%d state: %s because: %s"
2251 , ours
, ourport
, his
, hisport
, b
->transport_proto
2252 , oppo_step_name
[b
->step
], b
->want
);
2254 if (isanyaddr(&b
->our_client
) || isanyaddr(&b
->peer_client
))
2256 cannot_oppo(NULL
, b
, "impossible IP address");
2258 else if ((c
= find_connection_for_clients(&sr
2261 , b
->transport_proto
)) == NULL
)
2263 /* No connection explicitly handles the clients and there
2264 * are no Opportunistic connections -- whine and give up.
2265 * The failure policy cannot be gotten from a connection; we pick %pass.
2267 cannot_oppo(NULL
, b
, "no routed Opportunistic template covers this pair");
2269 else if (c
->kind
!= CK_TEMPLATE
)
2271 /* We've found a connection that can serve.
2272 * Do we have to initiate it?
2273 * Not if there is currently an IPSEC SA.
2274 * But if there is an IPSEC SA, then KLIPS would not
2275 * have generated the acquire. So we assume that there isn't one.
2276 * This may be redundant if a non-opportunistic
2277 * negotiation is already being attempted.
2280 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2282 if(c
->kind
== CK_INSTANCE
)
2284 char cib
[CONN_INST_BUF
];
2285 /* there is already an instance being negotiated, no nothing */
2286 DBG(DBG_CONTROL
, DBG_log("found existing instance \"%s\"%s, rekeying it"
2288 , (fmt_conn_instance(c
, cib
), cib
)));
2289 /* XXX-mcr - return; */
2292 /* otherwise, there is some kind of static conn that can handle
2293 * this connection, so we initiate it */
2298 /* what should we do on failure? */
2299 (void) assign_hold(c
, sr
, b
->transport_proto
, &b
->our_client
, &b
->peer_client
);
2302 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2303 b
->whackfd
= NULL_FD
; /* protect from close */
2307 /* We are handling an opportunistic situation.
2308 * This involves several DNS lookup steps that require suspension.
2309 * Note: many facts might change while we're suspended.
2312 * The first chunk of code handles the result of the previous
2313 * DNS query (if any). It also selects the kind of the next step.
2314 * The second chunk initiates the next DNS query (if any).
2316 enum find_oppo_step next_step
= fos_myid_ip_txt
;
2318 char mycredentialstr
[BUF_LEN
];
2319 char cib
[CONN_INST_BUF
];
2321 DBG(DBG_CONTROL
, DBG_log("creating new instance from \"%s\"%s"
2323 , (fmt_conn_instance(c
, cib
), cib
)));
2326 idtoa(&sr
->this.id
, mycredentialstr
, sizeof(mycredentialstr
));
2328 passert(c
->policy
& POLICY_OPPO
); /* can't initiate Road Warrior connections */
2330 /* handle any DNS answer; select next step */
2335 /* just starting out: select first query step */
2336 next_step
= fos_myid_ip_txt
;
2339 case fos_myid_ip_txt
: /* TXT for our default IP address as %myid */
2340 ugh
= check_txt_recs(MYID_IP
, c
, ac
);
2343 /* cannot use our IP as OE identitiy for initiation */
2344 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:TXT) as identity: %s"
2347 if (!logged_myid_ip_txt_warning
)
2349 loglog(RC_LOG_SERIOUS
2350 , "can not use our IP (%s:TXT) as identity: %s"
2353 logged_myid_ip_txt_warning
= TRUE
;
2356 next_step
= fos_myid_hostname_txt
;
2357 ugh
= NULL
; /* failure can be recovered from */
2361 /* we can use our IP as OE identity for initiation */
2362 if (!logged_myid_ip_txt_warning
)
2364 loglog(RC_LOG_SERIOUS
2365 , "using our IP (%s:TXT) as identity!"
2366 , myid_str
[MYID_IP
]);
2367 logged_myid_ip_txt_warning
= TRUE
;
2370 next_step
= fos_our_client
;
2374 case fos_myid_hostname_txt
: /* TXT for our hostname as %myid */
2375 ugh
= check_txt_recs(MYID_HOSTNAME
, c
, ac
);
2378 /* cannot use our hostname as OE identitiy for initiation */
2379 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:TXT) as identity: %s"
2380 , myid_str
[MYID_HOSTNAME
]
2382 if (!logged_myid_fqdn_txt_warning
)
2384 loglog(RC_LOG_SERIOUS
2385 , "can not use our hostname (%s:TXT) as identity: %s"
2386 , myid_str
[MYID_HOSTNAME
]
2388 logged_myid_fqdn_txt_warning
= TRUE
;
2391 next_step
= fos_myid_ip_key
;
2392 ugh
= NULL
; /* failure can be recovered from */
2397 /* we can use our hostname as OE identity for initiation */
2398 if (!logged_myid_fqdn_txt_warning
)
2400 loglog(RC_LOG_SERIOUS
2401 , "using our hostname (%s:TXT) as identity!"
2402 , myid_str
[MYID_HOSTNAME
]);
2403 logged_myid_fqdn_txt_warning
= TRUE
;
2405 next_step
= fos_our_client
;
2410 case fos_myid_ip_key
: /* KEY for our default IP address as %myid */
2411 ugh
= check_key_recs(MYID_IP
, c
, ac
);
2414 /* cannot use our IP as OE identitiy for initiation */
2415 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:KEY) as identity: %s"
2418 if (!logged_myid_ip_key_warning
)
2420 loglog(RC_LOG_SERIOUS
2421 , "can not use our IP (%s:KEY) as identity: %s"
2424 logged_myid_ip_key_warning
= TRUE
;
2427 next_step
= fos_myid_hostname_key
;
2428 ugh
= NULL
; /* failure can be recovered from */
2432 /* we can use our IP as OE identity for initiation */
2433 if (!logged_myid_ip_key_warning
)
2435 loglog(RC_LOG_SERIOUS
2436 , "using our IP (%s:KEY) as identity!"
2437 , myid_str
[MYID_IP
]);
2438 logged_myid_ip_key_warning
= TRUE
;
2440 next_step
= fos_our_client
;
2444 case fos_myid_hostname_key
: /* KEY for our hostname as %myid */
2445 ugh
= check_key_recs(MYID_HOSTNAME
, c
, ac
);
2448 /* cannot use our IP as OE identitiy for initiation */
2449 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:KEY) as identity: %s"
2450 , myid_str
[MYID_HOSTNAME
]
2452 if (!logged_myid_fqdn_key_warning
)
2454 loglog(RC_LOG_SERIOUS
2455 , "can not use our hostname (%s:KEY) as identity: %s"
2456 , myid_str
[MYID_HOSTNAME
]
2458 logged_myid_fqdn_key_warning
= TRUE
;
2461 next_step
= fos_myid_hostname_key
;
2462 ugh
= NULL
; /* failure can be recovered from */
2466 /* we can use our IP as OE identity for initiation */
2467 if (!logged_myid_fqdn_key_warning
)
2469 loglog(RC_LOG_SERIOUS
2470 , "using our hostname (%s:KEY) as identity!"
2471 , myid_str
[MYID_HOSTNAME
]);
2472 logged_myid_fqdn_key_warning
= TRUE
;
2474 next_step
= fos_our_client
;
2479 case fos_our_client
: /* TXT for our client */
2481 /* Our client is not us: we must check the TXT records.
2482 * Note: if c is different this time, there is
2483 * a chance that we did the wrong query.
2484 * If so, treat as a kind of failure.
2486 private_key_t
*private = get_private_key(c
);
2488 next_step
= fos_his_client
; /* normal situation */
2490 passert(sr
!= NULL
);
2492 if (private == NULL
)
2494 ugh
= "we don't know our own RSA key";
2496 else if (sameaddr(&sr
->this.host_addr
, &b
->our_client
))
2498 /* this wasn't true when we started -- bail */
2499 ugh
= "our IP address changed underfoot";
2501 else if (!same_id(&ac
->sgw_id
, &sr
->this.id
))
2503 /* this wasn't true when we started -- bail */
2504 ugh
= "our ID changed underfoot";
2508 /* Similar to code in quick_inI1_outR1_tail
2509 * for checking the other side.
2511 struct gw_info
*gwp
;
2513 ugh
= "no TXT RR for our client delegates us";
2514 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2516 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2518 ugh
= "TXT RR for our client has wrong key";
2519 /* If there is a key from the TXT record,
2520 * we count it as a win if we match the key.
2521 * If there was no key, we have a tentative win:
2522 * we need to check our KEY record to be sure.
2524 if (!gwp
->gw_key_present
)
2526 /* Success, but the TXT had no key
2527 * so we must check our our own KEY records.
2529 next_step
= fos_our_txt
;
2530 ugh
= NULL
; /* good! */
2533 if (private->belongs_to(private, gwp
->key
->public_key
))
2535 ugh
= NULL
; /* good! */
2543 case fos_our_txt
: /* TXT for us */
2545 /* Check if TXT lookup yielded good results.
2546 * Looking up based on our ID. Used if
2547 * client is ourself, or if TXT had no public key.
2548 * Note: if c is different this time, there is
2549 * a chance that we did the wrong query.
2550 * If so, treat as a kind of failure.
2552 private_key_t
*private = get_private_key(c
);
2554 next_step
= fos_his_client
; /* unless we decide to look for KEY RR */
2556 if (private == NULL
)
2558 ugh
= "we don't know our own RSA key";
2560 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2562 ugh
= "our ID changed underfoot";
2566 /* Similar to code in RSA_check_signature
2567 * for checking the other side.
2569 struct gw_info
*gwp
;
2571 ugh
= "no TXT RR for us";
2572 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2574 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2576 ugh
= "TXT RR for us has wrong key";
2577 if (gwp
->gw_key_present
&&
2578 private->belongs_to(private, gwp
->key
->public_key
))
2581 DBG_log("initiate on demand found TXT with right public key at: %s"
2582 , mycredentialstr
));
2590 /* if no TXT with right key, try KEY */
2592 DBG_log("will try for KEY RR since initiate on demand found %s: %s"
2593 , ugh
, mycredentialstr
));
2594 next_step
= fos_our_key
;
2603 case fos_our_key
: /* KEY for us */
2605 /* Check if KEY lookup yielded good results.
2606 * Looking up based on our ID. Used if
2607 * client is ourself, or if TXT had no public key.
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
; /* always */
2616 if (private == NULL
)
2618 ugh
= "we don't know our own RSA key";
2620 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2622 ugh
= "our ID changed underfoot";
2626 /* Similar to code in RSA_check_signature
2627 * for checking the other side.
2631 ugh
= "no KEY RR found for us (and no good TXT RR)";
2632 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2634 ugh
= "all our KEY RRs have the wrong public key (and no good TXT RR)";
2635 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2636 && private->belongs_to(private, kr
->key
->public_key
))
2638 /* do this only once a day */
2639 if (!logged_txt_warning
)
2641 loglog(RC_LOG_SERIOUS
2642 , "found KEY RR but not TXT RR for %s. See http://www.freeswan.org/err/txt-change.html."
2644 logged_txt_warning
= TRUE
;
2646 ugh
= NULL
; /* good! */
2653 #endif /* USE_KEYRR */
2655 case fos_his_client
: /* TXT for his client */
2657 /* We've finished last DNS queries: TXT for his client.
2658 * Using the information, try to instantiate a connection
2659 * and start negotiating.
2660 * We now know the peer. The chosing of "c" ignored this,
2661 * so we will disregard its current value.
2662 * !!! We need to randomize the entry in gw that we choose.
2664 next_step
= fos_done
; /* no more queries */
2666 c
= build_outgoing_opportunistic_connection(ac
->gateways_from_dns
2672 /* We cannot seem to instantiate a suitable connection:
2675 char ocb
[ADDRTOT_BUF
]
2679 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2680 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2681 passert(id_is_ipaddr(&ac
->gateways_from_dns
->gw_id
));
2682 addrtot(&ac
->gateways_from_dns
->gw_id
.ip_addr
, 0, pb
, sizeof(pb
));
2683 loglog(RC_OPPOFAILURE
2684 , "no suitable connection for opportunism"
2685 " between %s and %s with %s as peer"
2691 /* Replace HOLD with PASS.
2692 * The type of replacement *ought* to be
2693 * specified by policy.
2695 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2697 , SPI_PASS
/* fail into PASS */
2698 , TRUE
, b
->transport_proto
2699 , "no suitable connection");
2705 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2706 passert(c
->kind
== CK_INSTANCE
);
2707 passert(c
->gw_info
!= NULL
);
2708 passert(HAS_IPSEC_POLICY(c
->policy
));
2709 passert(LHAS(LELEM(RT_UNROUTED
) | LELEM(RT_ROUTED_PROSPECTIVE
), c
->spd
.routing
));
2713 /* what should we do on failure? */
2714 (void) assign_hold(c
, &c
->spd
2715 , b
->transport_proto
2716 , &b
->our_client
, &b
->peer_client
);
2719 c
->gw_info
->key
->last_tried_time
= now();
2720 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2721 b
->whackfd
= NULL_FD
; /* protect from close */
2730 /* the second chunk: initiate the next DNS query (if any) */
2733 char ours
[ADDRTOT_BUF
];
2734 char his
[ADDRTOT_BUF
];
2736 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2737 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2738 DBG_log("initiate on demand from %s to %s new state: %s with ugh: %s"
2739 , ours
, his
, oppo_step_name
[b
->step
], ugh ? ugh
: "ok");
2744 b
->policy_prio
= c
->prio
;
2745 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2746 cannot_oppo(c
, b
, ugh
);
2748 else if (next_step
== fos_done
)
2754 /* set up the next query */
2755 struct find_oppo_continuation
*cr
= malloc_thing(struct find_oppo_continuation
);
2758 b
->policy_prio
= c
->prio
;
2759 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2760 cr
->b
= *b
; /* copy; start hand off of whackfd */
2761 cr
->b
.failure_ok
= FALSE
;
2762 cr
->b
.step
= next_step
;
2765 ; sr
!=NULL
&& !sameaddr(&sr
->this.host_addr
, &b
->our_client
)
2772 /* If a %hold shunt has replaced the eroute for this template,
2776 && sr
->routing
== RT_ROUTED_PROSPECTIVE
&& eclipsable(sr
))
2778 sr
->routing
= RT_ROUTED_ECLIPSED
;
2782 /* Switch to issue next query.
2783 * A case may turn out to be unnecessary. If so, it falls
2784 * through to the next case.
2785 * Figuring out what %myid can stand for must be done before
2786 * our client credentials are looked up: we must know what
2787 * the client credentials may use to identify us.
2788 * On the other hand, our own credentials should be looked
2789 * up after our clients in case our credentials are not
2791 * XXX this is a wasted effort if we don't have credentials
2792 * BUT they are not needed.
2796 case fos_myid_ip_txt
:
2797 if (c
->spd
.this.id
.kind
== ID_MYID
2798 && myid_state
!= MYID_SPECIFIED
)
2800 cr
->b
.failure_ok
= TRUE
;
2801 cr
->b
.want
= b
->want
= "TXT record for IP address as %myid";
2802 ugh
= start_adns_query(&myids
[MYID_IP
]
2809 cr
->b
.step
= fos_myid_hostname_txt
;
2812 case fos_myid_hostname_txt
:
2813 if (c
->spd
.this.id
.kind
== ID_MYID
2814 && myid_state
!= MYID_SPECIFIED
)
2817 cr
->b
.failure_ok
= TRUE
;
2819 cr
->b
.failure_ok
= FALSE
;
2821 cr
->b
.want
= b
->want
= "TXT record for hostname as %myid";
2822 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2823 , &myids
[MYID_HOSTNAME
]
2831 cr
->b
.step
= fos_myid_ip_key
;
2834 case fos_myid_ip_key
:
2835 if (c
->spd
.this.id
.kind
== ID_MYID
2836 && myid_state
!= MYID_SPECIFIED
)
2838 cr
->b
.failure_ok
= TRUE
;
2839 cr
->b
.want
= b
->want
= "KEY record for IP address as %myid (no good TXT)";
2840 ugh
= start_adns_query(&myids
[MYID_IP
]
2841 , (const struct id
*) NULL
/* security gateway meaningless */
2847 cr
->b
.step
= fos_myid_hostname_key
;
2850 case fos_myid_hostname_key
:
2851 if (c
->spd
.this.id
.kind
== ID_MYID
2852 && myid_state
!= MYID_SPECIFIED
)
2854 cr
->b
.failure_ok
= FALSE
; /* last attempt! */
2855 cr
->b
.want
= b
->want
= "KEY record for hostname as %myid (no good TXT)";
2856 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2857 , (const struct id
*) NULL
/* security gateway meaningless */
2864 cr
->b
.step
= fos_our_client
;
2867 case fos_our_client
: /* TXT for our client */
2868 if (!sameaddr(&c
->spd
.this.host_addr
, &b
->our_client
))
2870 /* Check that at least one TXT(reverse(b->our_client)) is workable.
2871 * Note: {unshare|free}_id_content not needed for id: ephemeral.
2873 cr
->b
.want
= b
->want
= "our client's TXT record";
2874 iptoid(&b
->our_client
, &id
);
2875 ugh
= start_adns_query(&id
2876 , &c
->spd
.this.id
/* we are the security gateway */
2882 cr
->b
.step
= fos_our_txt
;
2885 case fos_our_txt
: /* TXT for us */
2886 cr
->b
.failure_ok
= b
->failure_ok
= TRUE
;
2887 cr
->b
.want
= b
->want
= "our TXT record";
2888 ugh
= start_adns_query(&sr
->this.id
2889 , &sr
->this.id
/* we are the security gateway XXX - maybe ignore? mcr */
2896 case fos_our_key
: /* KEY for us */
2897 cr
->b
.want
= b
->want
= "our KEY record";
2898 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2899 ugh
= start_adns_query(&sr
->this.id
2900 , (const struct id
*) NULL
/* security gateway meaningless */
2905 #endif /* USE_KEYRR */
2907 case fos_his_client
: /* TXT for his client */
2908 /* note: {unshare|free}_id_content not needed for id: ephemeral */
2909 cr
->b
.want
= b
->want
= "target's TXT record";
2910 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2911 iptoid(&b
->peer_client
, &id
);
2912 ugh
= start_adns_query(&id
2913 , (const struct id
*) NULL
/* security gateway unconstrained */
2920 bad_case(next_step
);
2924 b
->whackfd
= NULL_FD
; /* complete hand-off */
2926 cannot_oppo(c
, b
, ugh
);
2929 close_any(b
->whackfd
);
2932 void terminate_connection(const char *nm
)
2934 /* Loop because more than one may match (master and instances)
2935 * But at least one is required (enforced by con_by_name).
2937 connection_t
*c
= con_by_name(nm
, TRUE
);
2939 if (c
== NULL
|| !c
->ikev1
)
2944 connection_t
*n
= c
->ac_next
; /* grab this before c might disappear */
2946 if (streq(c
->name
, nm
)
2947 && c
->kind
>= CK_PERMANENT
2948 && !NEVER_NEGOTIATE(c
->policy
))
2950 set_cur_connection(c
);
2951 plog("terminating SAs using this connection");
2952 c
->policy
&= ~POLICY_UP
;
2953 flush_pending_by_connection(c
);
2954 delete_states_by_connection(c
, FALSE
);
2955 if (c
->kind
== CK_INSTANCE
)
2956 delete_connection(c
, FALSE
);
2957 reset_cur_connection();
2960 } while (c
!= NULL
);
2963 /* an ISAKMP SA has been established.
2964 * Note the serial number, and release any connections with
2965 * the same peer ID but different peer IP address.
2967 bool uniqueIDs
= FALSE
; /* --uniqueids? */
2969 void ISAKMP_SA_established(connection_t
*c
, so_serial_t serial
)
2971 c
->newest_isakmp_sa
= serial
;
2973 /* the connection is now oriented so that we are able to determine
2974 * whether we are a mode config server with a virtual IP to send.
2976 if (!isanyaddr(&c
->spd
.that
.host_srcip
) && !c
->spd
.that
.has_natip
)
2977 c
->spd
.that
.modecfg
= TRUE
;
2981 /* for all connections: if the same Phase 1 IDs are used
2982 * for a different IP address, unorient that connection.
2986 for (d
= connections
; d
!= NULL
; )
2988 connection_t
*next
= d
->ac_next
; /* might move underneath us */
2990 if (d
->kind
>= CK_PERMANENT
2991 && same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
2992 && same_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
)
2993 && !sameaddr(&c
->spd
.that
.host_addr
, &d
->spd
.that
.host_addr
))
2995 release_connection(d
, FALSE
);
3002 /* Find the connection to connection c's peer's client with the
3003 * largest value of .routing. All other things being equal,
3004 * preference is given to c. If none is routed, return NULL.
3006 * If erop is non-null, set *erop to a connection sharing both
3007 * our client subnet and peer's client subnet with the largest value
3008 * of .routing. If none is erouted, set *erop to NULL.
3010 * The return value is used to find other connections sharing a route.
3011 * *erop is used to find other connections sharing an eroute.
3013 connection_t
*route_owner(connection_t
*c
, struct spd_route
**srp
,
3014 connection_t
**erop
, struct spd_route
**esrp
)
3019 struct spd_route
*srd
, *src
;
3020 struct spd_route
*best_sr
, *best_esr
;
3021 enum routing_t best_routing
, best_erouting
;
3023 passert(oriented(*c
));
3026 best_routing
= c
->spd
.routing
;
3027 best_erouting
= best_routing
;
3029 for (d
= connections
; d
!= NULL
; d
= d
->ac_next
)
3031 for (srd
= &d
->spd
; srd
; srd
= srd
->next
)
3033 if (srd
->routing
== RT_UNROUTED
)
3036 for (src
= &c
->spd
; src
; src
=src
->next
)
3038 if (!samesubnet(&src
->that
.client
, &srd
->that
.client
))
3040 if (src
->that
.protocol
!= srd
->that
.protocol
)
3042 if (src
->that
.port
!= srd
->that
.port
)
3044 passert(oriented(*d
));
3045 if (srd
->routing
> best_routing
)
3049 best_routing
= srd
->routing
;
3052 if (!samesubnet(&src
->this.client
, &srd
->this.client
))
3054 if (src
->this.protocol
!= srd
->this.protocol
)
3056 if (src
->this.port
!= srd
->this.port
)
3058 if (srd
->routing
> best_erouting
)
3062 best_erouting
= srd
->routing
;
3070 char cib
[CONN_INST_BUF
];
3071 err_t m
= builddiag("route owner of \"%s\"%s %s:"
3073 , (fmt_conn_instance(c
, cib
), cib
)
3074 , enum_name(&routing_story
, c
->spd
.routing
));
3076 if (!routed(best_ro
->spd
.routing
))
3077 m
= builddiag("%s NULL", m
);
3078 else if (best_ro
== c
)
3079 m
= builddiag("%s self", m
);
3081 m
= builddiag("%s \"%s\"%s %s", m
3083 , (fmt_conn_instance(best_ro
, cib
), cib
)
3084 , enum_name(&routing_story
, best_ro
->spd
.routing
));
3088 m
= builddiag("%s; eroute owner:", m
);
3089 if (!erouted(best_ero
->spd
.routing
))
3090 m
= builddiag("%s NULL", m
);
3091 else if (best_ero
== c
)
3092 m
= builddiag("%s self", m
);
3094 m
= builddiag("%s \"%s\"%s %s", m
3096 , (fmt_conn_instance(best_ero
, cib
), cib
)
3097 , enum_name(&routing_story
, best_ero
->spd
.routing
));
3104 *erop
= erouted(best_erouting
)? best_ero
: NULL
;
3113 return routed(best_routing
)? best_ro
: NULL
;
3116 /* Find a connection that owns the shunt eroute between subnets.
3117 * There ought to be only one.
3118 * This might get to be a bottleneck -- try hashing if it does.
3120 connection_t
*shunt_owner(const ip_subnet
*ours
, const ip_subnet
*his
)
3123 struct spd_route
*sr
;
3125 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
3127 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
3129 if (shunt_erouted(sr
->routing
)
3130 && samesubnet(ours
, &sr
->this.client
)
3131 && samesubnet(his
, &sr
->that
.client
))
3138 /* Find some connection with this pair of hosts.
3139 * We don't know enough to chose amongst those available.
3140 * ??? no longer usefully different from find_host_pair_connections
3142 connection_t
*find_host_connection(const ip_address
*me
, u_int16_t my_port
,
3143 const ip_address
*him
, u_int16_t his_port
,
3146 connection_t
*c
= find_host_pair_connections(me
, my_port
, him
, his_port
);
3148 if (policy
!= LEMPTY
)
3150 lset_t auth_requested
= policy
& POLICY_ID_AUTH_MASK
;
3152 /* if we have requirements for the policy,
3153 * choose the first matching connection.
3157 if (c
->policy
& auth_requested
)
3167 /* given an up-until-now satisfactory connection, find the best connection
3168 * now that we just got the Phase 1 Id Payload from the peer.
3170 * Comments in the code describe the (tricky!) matching criteria.
3171 * Although this routine could handle the initiator case,
3172 * it isn't currently called in this case.
3173 * If it were, it could "upgrade" an Opportunistic Connection
3174 * to a Road Warrior Connection if a suitable Peer ID were found.
3176 * In RFC 2409 "The Internet Key Exchange (IKE)",
3177 * in 5.1 "IKE Phase 1 Authenticated With Signatures", describing Main
3180 * Initiator Responder
3181 * ----------- -----------
3186 * HDR*, IDii, [ CERT, ] SIG_I -->
3187 * <-- HDR*, IDir, [ CERT, ] SIG_R
3189 * In 5.4 "Phase 1 Authenticated With a Pre-Shared Key":
3195 * HDR*, IDii, HASH_I -->
3196 * <-- HDR*, IDir, HASH_R
3198 * refine_host_connection could be called in two case:
3200 * - the Responder receives the IDii payload:
3201 * + [PSK] after using PSK to decode this message
3202 * + before sending its IDir payload
3203 * + before using its ID in HASH_R computation
3204 * + [DSig] before using its private key to sign SIG_R
3205 * + before using the Initiator's ID in HASH_I calculation
3206 * + [DSig] before using the Initiator's public key to check SIG_I
3208 * - the Initiator receives the IDir payload:
3209 * + [PSK] after using PSK to encode previous message and decode this message
3210 * + after sending its IDii payload
3211 * + after using its ID in HASH_I computation
3212 * + [DSig] after using its private key to sign SIG_I
3213 * + before using the Responder's ID to compute HASH_R
3214 * + [DSig] before using Responder's public key to check SIG_R
3216 * refine_host_connection can choose a different connection, as long as
3217 * nothing already used is changed.
3219 * In the Initiator case, the particular connection might have been
3220 * specified by whatever provoked Pluto to initiate. For example:
3221 * whack --initiate connection-name
3222 * The advantages of switching connections when we're the Initiator seem
3223 * less important than the disadvantages, so after FreeS/WAN 1.9, we
3226 #define PRIO_NO_MATCH_FOUND 2048
3228 connection_t
*refine_host_connection(const struct state
*st
,
3229 const struct id
*peer_id
,
3232 connection_t
*c
= st
->st_connection
;
3234 connection_t
*best_found
= NULL
;
3235 u_int16_t auth
= st
->st_oakley
.auth
;
3236 lset_t auth_policy
= POLICY_PSK
;
3237 const chunk_t
*psk
= NULL
;
3238 bool wcpip
; /* wildcard Peer IP? */
3239 int best_prio
= PRIO_NO_MATCH_FOUND
;
3240 int wildcards
, our_pathlen
, peer_pathlen
;
3242 if (same_id(&c
->spd
.that
.id
, peer_id
)
3243 && trusted_ca(peer_ca
, c
->spd
.that
.ca
, &peer_pathlen
)
3244 && peer_pathlen
== 0
3245 && match_requested_ca(c
->requested_ca
, c
->spd
.this.ca
, &our_pathlen
)
3246 && our_pathlen
== 0)
3249 DBG_log("current connection is a full match"
3250 " -- no need to look further");
3257 case OAKLEY_PRESHARED_KEY
:
3258 auth_policy
= POLICY_PSK
;
3259 psk
= get_preshared_secret(c
);
3260 /* It should be virtually impossible to fail to find PSK:
3261 * we just used it to decode the current message!
3265 return NULL
; /* cannot determine PSK! */
3268 case XAUTHInitPreShared
:
3269 case XAUTHRespPreShared
:
3270 auth_policy
= POLICY_XAUTH_PSK
;
3271 psk
= get_preshared_secret(c
);
3274 return NULL
; /* cannot determine PSK! */
3277 case OAKLEY_RSA_SIG
:
3278 case OAKLEY_ECDSA_256
:
3279 case OAKLEY_ECDSA_384
:
3280 case OAKLEY_ECDSA_521
:
3281 auth_policy
= POLICY_PUBKEY
;
3285 auth_policy
= POLICY_XAUTH_RSASIG
;
3291 /* The current connection won't do: search for one that will.
3292 * First search for one with the same pair of hosts.
3293 * If that fails, search for a suitable Road Warrior or Opportunistic
3294 * connection (i.e. wildcard peer IP).
3296 * - peer_id (slightly complicated by instantiation)
3297 * - if PSK auth, the key must not change (we used it to decode message)
3298 * - policy-as-used must be acceptable to new connection
3300 d
= c
->host_pair
->connections
;
3301 for (wcpip
= FALSE
; ; wcpip
= TRUE
)
3303 for (; d
!= NULL
; d
= d
->hp_next
)
3305 const char *match_name
[] = {"no", "ok"};
3307 bool matching_id
= match_id(peer_id
3308 , &d
->spd
.that
.id
, &wildcards
);
3309 bool matching_auth
= (d
->policy
& auth_policy
) != LEMPTY
;
3311 bool matching_trust
= trusted_ca(peer_ca
3312 , d
->spd
.that
.ca
, &peer_pathlen
);
3313 bool matching_request
= match_requested_ca(c
->requested_ca
3314 , d
->spd
.this.ca
, &our_pathlen
);
3315 bool match
= matching_id
&& matching_auth
&& matching_trust
;
3317 int prio
= (MAX_WILDCARDS
+ 1) * !matching_request
+ wildcards
;
3319 prio
= (MAX_CA_PATH_LEN
+ 1) * prio
+ peer_pathlen
;
3320 prio
= (MAX_CA_PATH_LEN
+ 1) * prio
+ our_pathlen
;
3322 DBG(DBG_CONTROLMORE
,
3323 DBG_log("%s: %s match (id: %s, auth: %s, trust: %s, request: %s, prio: %4d)"
3325 , match ?
"full":" no"
3326 , match_name
[matching_id
]
3327 , match_name
[matching_auth
]
3328 , match_name
[matching_trust
]
3329 , match_name
[matching_request
]
3330 , match ? prio
:PRIO_NO_MATCH_FOUND
)
3333 /* do we have a match? */
3337 /* ignore group connections */
3338 if (d
->policy
& POLICY_GROUP
)
3341 if (c
->spd
.that
.host_port
!= d
->spd
.that
.host_port
3342 && d
->kind
== CK_INSTANCE
)
3349 case OAKLEY_PRESHARED_KEY
:
3350 case XAUTHInitPreShared
:
3351 case XAUTHRespPreShared
:
3352 /* secret must match the one we already used */
3354 const chunk_t
*dpsk
= get_preshared_secret(d
);
3357 continue; /* no secret */
3360 if (psk
->len
!= dpsk
->len
3361 || memcmp(psk
->ptr
, dpsk
->ptr
, psk
->len
) != 0)
3362 continue; /* different secret */
3366 case OAKLEY_RSA_SIG
:
3367 case OAKLEY_ECDSA_256
:
3368 case OAKLEY_ECDSA_384
:
3369 case OAKLEY_ECDSA_521
:
3373 * We must at least be able to find our private key
3375 if (d
->spd
.this.sc
== NULL
/* no smartcard */
3376 && get_private_key(d
) == NULL
) /* no private key */
3384 /* d has passed all the tests.
3385 * We'll go with it if the Peer ID was an exact match.
3392 /* We'll remember it as best_found in case an exact
3393 * match doesn't come along.
3395 if (prio
< best_prio
)
3402 return best_found
; /* been around twice already */
3404 /* Starting second time around.
3405 * We're willing to settle for a connection that needs Peer IP
3406 * instantiated: Road Warrior or Opportunistic.
3407 * Look on list of connections for host pair with wildcard Peer IP
3409 d
= find_host_pair_connections(&c
->spd
.this.host_addr
, c
->spd
.this.host_port
3410 , (ip_address
*)NULL
, c
->spd
.that
.host_port
);
3415 * With virtual addressing, we must not allow someone to use an already
3416 * used (by another id) addr/net.
3418 static bool is_virtual_net_used(const ip_subnet
*peer_net
,
3419 const struct id
*peer_id
)
3423 for (d
= connections
; d
!= NULL
; d
= d
->ac_next
)
3429 if ((subnetinsubnet(peer_net
,&d
->spd
.that
.client
) ||
3430 subnetinsubnet(&d
->spd
.that
.client
,peer_net
))
3431 && !same_id(&d
->spd
.that
.id
, peer_id
))
3434 char client
[SUBNETTOT_BUF
];
3436 subnettot(peer_net
, 0, client
, sizeof(client
));
3437 idtoa(&d
->spd
.that
.id
, buf
, sizeof(buf
));
3438 plog("Virtual IP %s is already used by '%s'", client
, buf
);
3439 idtoa(peer_id
, buf
, sizeof(buf
));
3440 plog("Your ID is '%s'", buf
);
3441 return TRUE
; /* already used by another one */
3449 return FALSE
; /* you can safely use it */
3452 /* find_client_connection: given a connection suitable for ISAKMP
3453 * (i.e. the hosts match), find a one suitable for IPSEC
3454 * (i.e. with matching clients).
3456 * If we don't find an exact match (not even our current connection),
3457 * we try for one that still needs instantiation. Try Road Warrior
3458 * abstract connections and the Opportunistic abstract connections.
3459 * This requires inverse instantiation: abstraction.
3461 * After failing to find an exact match, we abstract the peer
3462 * to be NO_IP (the wildcard value). This enables matches with
3463 * Road Warrior and Opportunistic abstract connections.
3465 * After failing that search, we also abstract the Phase 1 peer ID
3466 * if possible. If the peer's ID was the peer's IP address, we make
3467 * it NO_ID; instantiation will make it the peer's IP address again.
3469 * If searching for a Road Warrior abstract connection fails,
3470 * and conditions are suitable, we search for the best Opportunistic
3471 * abstract connection.
3473 * Note: in the end, both Phase 1 IDs must be preserved, after any
3474 * instantiation. They are the IDs that have been authenticated.
3477 #define PATH_WEIGHT 1
3478 #define WILD_WEIGHT (MAX_CA_PATH_LEN+1)
3479 #define PRIO_WEIGHT (MAX_WILDCARDS+1)*WILD_WEIGHT
3481 /* fc_try: a helper function for find_client_connection */
3482 static connection_t
*fc_try(const connection_t
*c
, struct host_pair
*hp
,
3483 const struct id
*peer_id
,
3484 const ip_subnet
*our_net
,
3485 const ip_subnet
*peer_net
,
3486 const u_int8_t our_protocol
,
3487 const u_int16_t our_port
,
3488 const u_int8_t peer_protocol
,
3489 const u_int16_t peer_port
,
3491 const ietfAttrList_t
*peer_list
)
3494 connection_t
*best
= NULL
;
3495 policy_prio_t best_prio
= BOTTOM_PRIO
;
3496 int wildcards
, pathlen
;
3498 const bool peer_net_is_host
= subnetisaddr(peer_net
, &c
->spd
.that
.host_addr
);
3500 for (d
= hp
->connections
; d
!= NULL
; d
= d
->hp_next
)
3502 struct spd_route
*sr
;
3504 if (d
->policy
& POLICY_GROUP
)
3507 if (!(same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
3508 && match_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
, &wildcards
)
3509 && trusted_ca(peer_ca
, d
->spd
.that
.ca
, &pathlen
)
3510 && group_membership(peer_list
, d
->name
, d
->spd
.that
.groups
)))
3513 /* compare protocol and ports */
3514 if (d
->spd
.this.protocol
!= our_protocol
3515 || d
->spd
.this.port
!= our_port
3516 || d
->spd
.that
.protocol
!= peer_protocol
3517 || (d
->spd
.that
.port
!= peer_port
&& !d
->spd
.that
.has_port_wildcard
))
3520 /* non-Opportunistic case:
3521 * our_client must match.
3523 * So must peer_client, but the testing is complicated
3524 * by the fact that the peer might be a wildcard
3525 * and if so, the default value of that.client
3526 * won't match the default peer_net. The appropriate test:
3528 * If d has a peer client, it must match peer_net.
3529 * If d has no peer client, peer_net must just have peer itself.
3532 for (sr
= &d
->spd
; best
!= d
&& sr
!= NULL
; sr
= sr
->next
)
3536 if (DBGP(DBG_CONTROLMORE
))
3538 char s1
[SUBNETTOT_BUF
],d1
[SUBNETTOT_BUF
];
3539 char s3
[SUBNETTOT_BUF
],d3
[SUBNETTOT_BUF
];
3541 subnettot(our_net
, 0, s1
, sizeof(s1
));
3542 subnettot(peer_net
, 0, d1
, sizeof(d1
));
3543 subnettot(&sr
->this.client
, 0, s3
, sizeof(s3
));
3544 subnettot(&sr
->that
.client
, 0, d3
, sizeof(d3
));
3545 DBG_log(" fc_try trying "
3546 "%s:%s:%d/%d -> %s:%d/%d vs %s:%s:%d/%d -> %s:%d/%d"
3547 , c
->name
, s1
, c
->spd
.this.protocol
, c
->spd
.this.port
3548 , d1
, c
->spd
.that
.protocol
, c
->spd
.that
.port
3549 , d
->name
, s3
, sr
->this.protocol
, sr
->this.port
3550 , d3
, sr
->that
.protocol
, sr
->that
.port
);
3554 if (!samesubnet(&sr
->this.client
, our_net
))
3557 if (sr
->that
.has_client
)
3559 if (sr
->that
.has_client_wildcard
)
3561 if (!subnetinsubnet(peer_net
, &sr
->that
.client
))
3566 if (!samesubnet(&sr
->that
.client
, peer_net
) && !is_virtual_connection(d
))
3568 if (is_virtual_connection(d
)
3569 && (!is_virtual_net_allowed(d
, peer_net
, &c
->spd
.that
.host_addr
)
3570 || is_virtual_net_used(peer_net
, peer_id?peer_id
:&c
->spd
.that
.id
)))
3576 if (!peer_net_is_host
)
3580 /* We've run the gauntlet -- success:
3581 * We've got an exact match of subnets.
3582 * The connection is feasible, but we continue looking for the best.
3583 * The highest priority wins, implementing eroute-like rule.
3584 * - a routed connection is preferrred
3585 * - given that, the smallest number of ID wildcards are preferred
3586 * - given that, the shortest CA pathlength is preferred
3588 prio
= PRIO_WEIGHT
* routed(sr
->routing
)
3589 + WILD_WEIGHT
* (MAX_WILDCARDS
- wildcards
)
3590 + PATH_WEIGHT
* (MAX_CA_PATH_LEN
- pathlen
)