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
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 "kernel_alg.h"
62 #include "nat_traversal.h"
65 static void flush_pending_by_connection(struct connection
*c
); /* forward */
67 static struct connection
*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 struct connection
*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 struct connection
*unoriented_connections
= NULL
;
94 /* check to see that Ids of peers match */
96 same_peer_ids(const struct connection
*c
, const struct connection
*d
97 , const struct id
*his_id
)
99 return same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
100 && same_id(his_id
== NULL?
&c
->spd
.that
.id
: his_id
, &d
->spd
.that
.id
);
103 static struct host_pair
*
104 find_host_pair(const ip_address
*myaddr
, u_int16_t myport
105 , const ip_address
*hisaddr
, u_int16_t hisport
)
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 struct connection
*
142 find_host_pair_connections(const ip_address
*myaddr
, u_int16_t myport
143 , const ip_address
*hisaddr
, u_int16_t hisport
)
145 struct host_pair
*hp
= find_host_pair(myaddr
, myport
, hisaddr
, hisport
);
147 if (nat_traversal_enabled
&& hp
&& hisaddr
)
149 struct connection
*c
;
151 for (c
= hp
->connections
; c
!= NULL
; c
= c
->hp_next
)
153 if (c
->spd
.this.host_port
== myport
&& c
->spd
.that
.host_port
== hisport
)
158 return hp
== NULL? NULL
: hp
->connections
;
162 connect_to_host_pair(struct connection
*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
= alloc_thing(struct host_pair
, "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.
210 con_by_name(const char *nm
, bool strict
)
212 struct connection
*p
, *prev
;
214 for (prev
= NULL
, p
= connections
; ; prev
= p
, p
= p
->ac_next
)
219 whack_log(RC_UNKNOWN_NAME
220 , "no connection named \"%s\"", nm
);
223 if (streq(p
->name
, nm
)
224 && (!strict
|| p
->kind
!= CK_INSTANCE
))
228 prev
->ac_next
= p
->ac_next
; /* remove p from list */
229 p
->ac_next
= connections
; /* and stick it on front */
239 release_connection(struct connection
*c
, bool relations
)
241 if (c
->kind
== CK_INSTANCE
)
243 /* This does everything we need.
244 * Note that we will be called recursively by delete_connection,
245 * but kind will be CK_GOING_AWAY.
247 delete_connection(c
, relations
);
251 flush_pending_by_connection(c
);
252 delete_states_by_connection(c
, relations
);
253 unroute_connection(c
);
257 /* Delete a connection */
259 #define list_rm(etype, enext, e, ehead) { \
261 for (ep = &(ehead); *ep != (e); ep = &(*ep)->enext) \
262 passert(*ep != NULL); /* we must not come up empty-handed */ \
268 delete_connection(struct connection
*c
, bool relations
)
270 struct connection
*old_cur_connection
271 = cur_connection
== c? NULL
: cur_connection
;
273 lset_t old_cur_debugging
= cur_debugging
;
276 set_cur_connection(c
);
278 /* Must be careful to avoid circularity:
279 * we mark c as going away so it won't get deleted recursively.
281 passert(c
->kind
!= CK_GOING_AWAY
);
282 if (c
->kind
== CK_INSTANCE
)
284 plog("deleting connection \"%s\" instance with peer %s {isakmp=#%lu/ipsec=#%lu}"
286 , ip_str(&c
->spd
.that
.host_addr
)
287 , c
->newest_isakmp_sa
, c
->newest_ipsec_sa
);
288 c
->kind
= CK_GOING_AWAY
;
292 plog("deleting connection");
294 release_connection(c
, relations
); /* won't delete c */
296 if (c
->kind
== CK_GROUP
)
299 /* free up any logging resources */
302 /* find and delete c from connections list */
303 list_rm(struct connection
, ac_next
, c
, connections
);
304 cur_connection
= old_cur_connection
;
306 /* find and delete c from the host pair list */
307 if (c
->host_pair
== NULL
)
310 list_rm(struct connection
, hp_next
, c
, unoriented_connections
);
314 struct host_pair
*hp
= c
->host_pair
;
316 list_rm(struct connection
, hp_next
, c
, hp
->connections
);
317 c
->host_pair
= NULL
; /* redundant, but safe */
319 /* if there are no more connections with this host_pair
320 * and we haven't even made an initial contact, let's delete
321 * this guy in case we were created by an attempted DOS attack.
323 if (hp
->connections
== NULL
324 && !hp
->initial_connection_sent
)
326 passert(hp
->pending
== NULL
); /* ??? must deal with this! */
327 list_rm(struct host_pair
, next
, hp
, host_pairs
);
332 if (c
->kind
!= CK_GOING_AWAY
)
333 pfreeany(c
->spd
.that
.virt
);
336 cur_debugging
= old_cur_debugging
;
339 free_id_content(&c
->spd
.this.id
);
340 pfreeany(c
->spd
.this.updown
);
341 freeanychunk(c
->spd
.this.ca
);
342 free_ietfAttrList(c
->spd
.this.groups
);
343 free_id_content(&c
->spd
.that
.id
);
344 pfreeany(c
->spd
.that
.updown
);
345 freeanychunk(c
->spd
.that
.ca
);
346 free_ietfAttrList(c
->spd
.that
.groups
);
347 free_generalNames(c
->requested_ca
, TRUE
);
348 gw_delref(&c
->gw_info
);
350 lock_certs_and_keys("delete_connection");
351 release_cert(c
->spd
.this.cert
);
352 scx_release(c
->spd
.this.sc
);
353 release_cert(c
->spd
.that
.cert
);
354 scx_release(c
->spd
.that
.sc
);
355 unlock_certs_and_keys("delete_connection");
357 alg_info_delref((struct alg_info
**)&c
->alg_info_esp
);
358 alg_info_delref((struct alg_info
**)&c
->alg_info_ike
);
363 /* Delete connections with the specified name */
365 delete_connections_by_name(const char *name
, bool strict
)
367 struct connection
*c
= con_by_name(name
, strict
);
369 for (; c
!= NULL
; c
= con_by_name(name
, FALSE
))
370 delete_connection(c
, FALSE
);
374 delete_every_connection(void)
376 while (connections
!= NULL
)
377 delete_connection(connections
, TRUE
);
381 release_dead_interfaces(void)
383 struct host_pair
*hp
;
385 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
387 struct connection
**pp
390 for (pp
= &hp
->connections
; (p
= *pp
) != NULL
; )
392 if (p
->interface
->change
== IFN_DELETE
)
394 /* this connection's interface is going away */
395 enum connection_kind k
= p
->kind
;
397 release_connection(p
, TRUE
);
399 if (k
<= CK_PERMANENT
)
401 /* The connection should have survived release:
402 * move it to the unoriented_connections list.
408 *pp
= p
->hp_next
; /* advance *pp */
410 p
->hp_next
= unoriented_connections
;
411 unoriented_connections
= p
;
415 /* The connection should have vanished,
416 * but the previous connection remains.
423 pp
= &p
->hp_next
; /* advance pp */
429 /* adjust orientations of connections to reflect newly added interfaces */
431 check_orientations(void)
433 /* try to orient all the unoriented connections */
435 struct connection
*c
= unoriented_connections
;
437 unoriented_connections
= NULL
;
441 struct connection
*nxt
= c
->hp_next
;
444 connect_to_host_pair(c
);
449 /* Check that no oriented connection has become double-oriented.
450 * In other words, the far side must not match one of our new interfaces.
455 for (i
= interfaces
; i
!= NULL
; i
= i
->next
)
457 if (i
->change
== IFN_ADD
)
459 struct host_pair
*hp
;
461 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
463 if (sameaddr(&hp
->him
.addr
, &i
->addr
)
464 && (!no_klips
|| hp
->him
.port
== pluto_port
))
466 /* bad news: the whole chain of connections
467 * hanging off this host pair has both sides
468 * matching an interface.
469 * We'll get rid of them, using orient and
470 * connect_to_host_pair. But we'll be lazy
471 * and not ditch the host_pair itself (the
472 * cost of leaving it is slight and cannot
473 * be induced by a foe).
475 struct connection
*c
= hp
->connections
;
477 hp
->connections
= NULL
;
480 struct connection
*nxt
= c
->hp_next
;
484 connect_to_host_pair(c
);
495 default_end(struct end
*e
, ip_address
*dflt_nexthop
)
498 const struct af_info
*afi
= aftoinfo(addrtypeof(&e
->host_addr
));
501 return "unknown address family in default_end";
503 /* default ID to IP (but only if not NO_IP -- WildCard) */
504 if (e
->id
.kind
== ID_NONE
&& !isanyaddr(&e
->host_addr
))
506 e
->id
.kind
= afi
->id_addr
;
507 e
->id
.ip_addr
= e
->host_addr
;
508 e
->has_id_wildcards
= FALSE
;
511 /* default nexthop to other side */
512 if (isanyaddr(&e
->host_nexthop
))
513 e
->host_nexthop
= *dflt_nexthop
;
515 /* default client to subnet containing only self
516 * XXX This may mean that the client's address family doesn't match
517 * tunnel_addr_family.
520 ugh
= addrtosubnet(&e
->host_addr
, &e
->client
);
525 /* Format the topology of a connection end, leaving out defaults.
526 * Largest left end looks like: client === host : port [ host_id ] --- hop
527 * Note: if that==NULL, skip nexthop
528 * Returns strlen of formated result (length excludes NUL at end).
533 , const struct end
*this
534 , const struct end
*that
538 char client
[SUBNETTOT_BUF
];
539 const char *client_sep
= "";
540 char protoport
[sizeof(":255/65535")];
541 const char *host
= NULL
;
542 char host_space
[ADDRTOT_BUF
];
543 char host_port
[sizeof(":65535")];
544 char host_id
[BUF_LEN
+ 2];
545 char hop
[ADDRTOT_BUF
];
546 const char *hop_sep
= "";
547 const char *open_brackets
= "";
548 const char *close_brackets
= "";
550 if (isanyaddr(&this->host_addr
))
552 switch (policy
& (POLICY_GROUP
| POLICY_OPPO
))
558 host
= "%opportunistic";
560 case POLICY_GROUP
| POLICY_OPPO
:
561 host
= "%opportunisticgroup";
571 if (is_virtual_end(this) && isanyaddr(&this->host_addr
))
577 if (this->has_client
)
579 ip_address client_net
, client_mask
;
581 networkof(&this->client
, &client_net
);
582 maskof(&this->client
, &client_mask
);
585 /* {client_subnet_wildcard} */
586 if (this->has_client_wildcard
)
589 close_brackets
= "}";
592 if (isanyaddr(&client_net
) && isanyaddr(&client_mask
)
593 && (policy
& (POLICY_GROUP
| POLICY_OPPO
)))
594 client_sep
= ""; /* boring case */
595 else if (subnetisnone(&this->client
))
598 subnettot(&this->client
, 0, client
, sizeof(client
));
600 else if (this->modecfg
&& isanyaddr(&this->host_srcip
))
602 /* we are mode config client */
604 strcpy(client
, "%modecfg");
610 addrtot(&this->host_addr
, 0, host_space
, sizeof(host_space
));
615 if (this->host_port
!= IKE_UDP_PORT
)
616 snprintf(host_port
, sizeof(host_port
), ":%u"
619 /* payload portocol and port */
621 if (this->has_port_wildcard
)
622 snprintf(protoport
, sizeof(protoport
), ":%u/%%any", this->protocol
);
623 else if (this->port
|| this->protocol
)
624 snprintf(protoport
, sizeof(protoport
), ":%u/%u", this->protocol
627 /* id, if different from host */
629 if (this->id
.kind
== ID_MYID
)
631 strcpy(host_id
, "[%myid]");
633 else if (!(this->id
.kind
== ID_NONE
634 || (id_is_ipaddr(&this->id
) && sameaddr(&this->id
.ip_addr
, &this->host_addr
))))
636 int len
= idtoa(&this->id
, host_id
+1, sizeof(host_id
)-2);
639 strcpy(&host_id
[len
< 0?
(ptrdiff_t)sizeof(host_id
)-2 : 1 + len
], "]");
645 if (that
!= NULL
&& !sameaddr(&this->host_nexthop
, &that
->host_addr
))
647 addrtot(&this->host_nexthop
, 0, hop
, sizeof(hop
));
652 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
653 , open_brackets
, client
, close_brackets
, client_sep
654 , this->allow_any?
"%":""
655 , host
, host_port
, host_id
, protoport
658 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s%s"
660 , this->allow_any?
"%":""
661 , host
, host_port
, host_id
, protoport
, client_sep
662 , open_brackets
, client
, close_brackets
);
666 /* format topology of a connection.
667 * Two symmetric ends separated by ...
669 #define CONNECTION_BUF (2 * (END_BUF - 1) + 4)
672 format_connection(char *buf
, size_t buf_len
673 , const struct connection
*c
674 , struct spd_route
*sr
)
676 size_t w
= format_end(buf
, buf_len
, &sr
->this, &sr
->that
, TRUE
, LEMPTY
);
678 w
+= snprintf(buf
+ w
, buf_len
- w
, "...");
679 return w
+ format_end(buf
+ w
, buf_len
- w
, &sr
->that
, &sr
->this, FALSE
, c
->policy
);
683 unshare_connection_strings(struct connection
*c
)
685 c
->name
= clone_str(c
->name
, "connection name");
687 unshare_id_content(&c
->spd
.this.id
);
688 c
->spd
.this.updown
= clone_str(c
->spd
.this.updown
, "updown");
689 scx_share(c
->spd
.this.sc
);
690 share_cert(c
->spd
.this.cert
);
691 if (c
->spd
.this.ca
.ptr
!= NULL
)
692 clonetochunk(c
->spd
.this.ca
, c
->spd
.this.ca
.ptr
, c
->spd
.this.ca
.len
, "ca string");
694 unshare_id_content(&c
->spd
.that
.id
);
695 c
->spd
.that
.updown
= clone_str(c
->spd
.that
.updown
, "updown");
696 scx_share(c
->spd
.that
.sc
);
697 share_cert(c
->spd
.that
.cert
);
698 if (c
->spd
.that
.ca
.ptr
!= NULL
)
699 clonetochunk(c
->spd
.that
.ca
, c
->spd
.that
.ca
.ptr
, c
->spd
.that
.ca
.len
, "ca string");
701 /* increment references to algo's */
702 alg_info_addref((struct alg_info
*)c
->alg_info_esp
);
703 alg_info_addref((struct alg_info
*)c
->alg_info_ike
);
707 load_end_certificate(const char *filename
, struct end
*dst
)
711 bool valid_cert
= FALSE
;
712 bool cached_cert
= FALSE
;
714 /* initialize end certificate */
715 dst
->cert
.type
= CERT_NONE
;
716 dst
->cert
.u
.x509
= NULL
;
718 /* initialize smartcard info record */
721 if (filename
!= NULL
)
723 if (scx_on_smartcard(filename
))
725 /* load cert from smartcard */
726 valid_cert
= scx_load_cert(filename
, &dst
->sc
, &cert
, &cached_cert
);
730 /* load cert from file */
731 valid_cert
= load_host_cert(filename
, &cert
);
742 select_pgpcert_id(cert
.u
.pgp
, &dst
->id
);
748 valid_until
= cert
.u
.pgp
->until
;
749 add_pgp_public_key(cert
.u
.pgp
, cert
.u
.pgp
->until
, DAL_LOCAL
);
750 dst
->cert
.type
= cert
.type
;
751 dst
->cert
.u
.pgp
= add_pgpcert(cert
.u
.pgp
);
754 case CERT_X509_SIGNATURE
:
755 select_x509cert_id(cert
.u
.x509
, &dst
->id
);
761 /* check validity of cert */
762 valid_until
= cert
.u
.x509
->notAfter
;
763 ugh
= check_validity(cert
.u
.x509
, &valid_until
);
767 free_x509cert(cert
.u
.x509
);
772 DBG_log("certificate is valid")
774 add_x509_public_key(cert
.u
.x509
, valid_until
, DAL_LOCAL
);
775 dst
->cert
.type
= cert
.type
;
776 dst
->cert
.u
.x509
= add_x509cert(cert
.u
.x509
);
778 /* if no CA is defined, use issuer as default */
779 if (dst
->ca
.ptr
== NULL
)
780 dst
->ca
= dst
->cert
.u
.x509
->issuer
;
786 /* cache the certificate that was last retrieved from the smartcard */
789 if (!same_cert(&dst
->sc
->last_cert
, &dst
->cert
))
791 lock_certs_and_keys("load_end_certificates");
792 release_cert(dst
->sc
->last_cert
);
793 dst
->sc
->last_cert
= dst
->cert
;
794 share_cert(dst
->cert
);
795 unlock_certs_and_keys("load_end_certificates");
797 time(&dst
->sc
->last_load
);
803 extract_end(struct end
*dst
, const whack_end_t
*src
, const char *which
)
805 bool same_ca
= FALSE
;
807 /* decode id, if any */
810 dst
->id
.kind
= ID_NONE
;
814 err_t ugh
= atoid(src
->id
, &dst
->id
, TRUE
);
818 loglog(RC_BADID
, "bad %s --id: %s (ignored)", which
, ugh
);
819 dst
->id
= empty_id
; /* ignore bad one */
823 dst
->ca
= empty_chunk
;
825 /* decode CA distinguished name, if any */
828 if streq(src
->ca
, "%same")
830 else if (!streq(src
->ca
, "%any"))
834 dst
->ca
.ptr
= temporary_cyclic_buffer();
835 ugh
= atodn(src
->ca
, &dst
->ca
);
838 plog("bad CA string '%s': %s (ignored)", src
->ca
, ugh
);
839 dst
->ca
= empty_chunk
;
844 /* load local end certificate and extract ID, if any */
845 load_end_certificate(src
->cert
, dst
);
847 /* does id has wildcards? */
848 dst
->has_id_wildcards
= id_count_wildcards(&dst
->id
) > 0;
850 /* decode group attributes, if any */
851 decode_groups(src
->groups
, &dst
->groups
);
853 /* the rest is simple copying of corresponding fields */
854 dst
->host_addr
= src
->host_addr
;
855 dst
->host_nexthop
= src
->host_nexthop
;
856 dst
->host_srcip
= src
->host_srcip
;
857 dst
->has_natip
= src
->has_natip
;
858 dst
->client
= src
->client
;
859 dst
->protocol
= src
->protocol
;
860 dst
->port
= src
->port
;
861 dst
->has_port_wildcard
= src
->has_port_wildcard
;
862 dst
->key_from_DNS_on_demand
= src
->key_from_DNS_on_demand
;
863 dst
->has_client
= src
->has_client
;
864 dst
->has_client_wildcard
= src
->has_client_wildcard
;
865 dst
->modecfg
= src
->modecfg
;
866 dst
->hostaccess
= src
->hostaccess
;
867 dst
->allow_any
= src
->allow_any
;
868 dst
->sendcert
= src
->sendcert
;
869 dst
->updown
= src
->updown
;
870 dst
->host_port
= src
->host_port
;
872 /* if host sourceip is defined but no client is present
873 * behind the host then set client to sourceip/32
875 if (addrbytesptr(&dst
->host_srcip
, NULL
)
876 && !isanyaddr(&dst
->host_srcip
)
880 err_t ugh
= addrtosubnet(&dst
->host_srcip
, &dst
->client
);
883 plog("could not assign host sourceip to client subnet");
885 dst
->has_client
= TRUE
;
891 check_connection_end(const whack_end_t
*this, const whack_end_t
*that
892 , const whack_message_t
*wm
)
894 if (wm
->addr_family
!= addrtypeof(&this->host_addr
)
895 || wm
->addr_family
!= addrtypeof(&this->host_nexthop
)
896 || (this->has_client? wm
->tunnel_addr_family
: wm
->addr_family
)
897 != subnettypeof(&this->client
)
898 || subnettypeof(&this->client
) != subnettypeof(&that
->client
))
900 /* this should have been diagnosed by whack, so we need not be clear
901 * !!! overloaded use of RC_CLASH
903 loglog(RC_CLASH
, "address family inconsistency in connection");
907 if (isanyaddr(&that
->host_addr
))
909 /* other side is wildcard: we must check if other conditions met */
910 if (isanyaddr(&this->host_addr
))
912 loglog(RC_ORIENT
, "connection must specify host IP address for our side");
917 if (this->virt
&& (!isanyaddr(&this->host_addr
) || this->has_client
))
920 "virtual IP must only be used with %%any and without client");
924 return TRUE
; /* happy */
928 find_connection_by_reqid(uint32_t reqid
)
930 struct connection
*c
;
933 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
935 if (c
->spd
.reqid
== reqid
)
946 static uint32_t reqid
= IPSEC_MANUAL_REQID_MAX
& ~3;
952 reqid
= (IPSEC_MANUAL_REQID_MAX
& ~3) + 4;
953 if (!find_connection_by_reqid(reqid
))
955 } while (reqid
!= start
);
957 exit_log("unable to allocate reqid");
958 return 0; /* never reached ... */
962 add_connection(const whack_message_t
*wm
)
964 if (con_by_name(wm
->name
, FALSE
) != NULL
)
966 loglog(RC_DUPNAME
, "attempt to redefine connection \"%s\"", wm
->name
);
968 else if (wm
->right
.protocol
!= wm
->left
.protocol
)
970 /* this should haven been diagnosed by whack
971 * !!! overloaded use of RC_CLASH
973 loglog(RC_CLASH
, "the protocol must be the same for leftport and rightport");
975 else if (check_connection_end(&wm
->right
, &wm
->left
, wm
)
976 && check_connection_end(&wm
->left
, &wm
->right
, wm
))
978 bool same_rightca
, same_leftca
;
979 struct connection
*c
= alloc_thing(struct connection
, "struct connection");
982 c
->ikev1
= wm
->ikev1
;
983 c
->policy
= wm
->policy
;
985 if ((c
->policy
& POLICY_COMPRESS
) && !can_do_IPcomp
)
987 , "ignoring --compress in \"%s\" because KLIPS is not configured to do IPCOMP"
995 DBG_log("from whack: got --esp=%s", wm
->esp ? wm
->esp
: "NULL")
997 c
->alg_info_esp
= alg_info_esp_create_from_str(wm
->esp? wm
->esp
: "", &ugh
);
999 DBG(DBG_CRYPT
|DBG_CONTROL
,
1000 static char buf
[256]="<NULL>";
1002 if (c
->alg_info_esp
)
1003 alg_info_snprint(buf
, sizeof(buf
)
1004 ,(struct alg_info
*)c
->alg_info_esp
);
1005 DBG_log("esp string values: %s", buf
);
1007 if (c
->alg_info_esp
)
1009 if (c
->alg_info_esp
->alg_info_cnt
==0)
1010 loglog(RC_LOG_SERIOUS
1011 , "got 0 transforms for esp=\"%s\"", wm
->esp
);
1015 loglog(RC_LOG_SERIOUS
1016 , "esp string error: %s", ugh? ugh
: "Unknown");
1025 DBG_log("from whack: got --ike=%s", wm
->ike ? wm
->ike
: "NULL")
1027 c
->alg_info_ike
= alg_info_ike_create_from_str(wm
->ike? wm
->ike
: "", &ugh
);
1029 DBG(DBG_CRYPT
|DBG_CONTROL
,
1030 static char buf
[256]="<NULL>";
1032 if (c
->alg_info_ike
)
1033 alg_info_snprint(buf
, sizeof(buf
)
1034 , (struct alg_info
*)c
->alg_info_ike
);
1035 DBG_log("ike string values: %s", buf
);
1037 if (c
->alg_info_ike
)
1039 if (c
->alg_info_ike
->alg_info_cnt
==0)
1040 loglog(RC_LOG_SERIOUS
1041 , "got 0 transforms for ike=\"%s\"", wm
->ike
);
1045 loglog(RC_LOG_SERIOUS
1046 , "ike string error: %s", ugh? ugh
: "Unknown");
1050 c
->sa_ike_life_seconds
= wm
->sa_ike_life_seconds
;
1051 c
->sa_ipsec_life_seconds
= wm
->sa_ipsec_life_seconds
;
1052 c
->sa_rekey_margin
= wm
->sa_rekey_margin
;
1053 c
->sa_rekey_fuzz
= wm
->sa_rekey_fuzz
;
1054 c
->sa_keying_tries
= wm
->sa_keying_tries
;
1057 c
->dpd_delay
= wm
->dpd_delay
;
1058 c
->dpd_timeout
= wm
->dpd_timeout
;
1059 c
->dpd_action
= wm
->dpd_action
;
1061 c
->addr_family
= wm
->addr_family
;
1062 c
->tunnel_addr_family
= wm
->tunnel_addr_family
;
1064 c
->requested_ca
= NULL
;
1066 same_leftca
= extract_end(&c
->spd
.this, &wm
->left
, "left");
1067 same_rightca
= extract_end(&c
->spd
.that
, &wm
->right
, "right");
1070 c
->spd
.that
.ca
= c
->spd
.this.ca
;
1071 else if (same_leftca
)
1072 c
->spd
.this.ca
= c
->spd
.that
.ca
;
1074 default_end(&c
->spd
.this, &c
->spd
.that
.host_addr
);
1075 default_end(&c
->spd
.that
, &c
->spd
.this.host_addr
);
1077 /* force any wildcard host IP address, any wildcard subnet
1078 * or any wildcard ID to that end
1080 if (isanyaddr(&c
->spd
.this.host_addr
) || c
->spd
.this.has_client_wildcard
1081 || c
->spd
.this.has_port_wildcard
|| c
->spd
.this.has_id_wildcards
1082 || c
->spd
.this.allow_any
)
1084 struct end t
= c
->spd
.this;
1086 c
->spd
.this = c
->spd
.that
;
1091 c
->spd
.reqid
= gen_reqid();
1093 /* set internal fields */
1094 c
->instance_serial
= 0;
1095 c
->ac_next
= connections
;
1097 c
->interface
= NULL
;
1098 c
->spd
.routing
= RT_UNROUTED
;
1099 c
->newest_isakmp_sa
= SOS_NOBODY
;
1100 c
->newest_ipsec_sa
= SOS_NOBODY
;
1101 c
->spd
.eroute_owner
= SOS_NOBODY
;
1103 if (c
->policy
& POLICY_GROUP
)
1108 else if ((isanyaddr(&c
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(c
->policy
))
1109 || c
->spd
.that
.has_client_wildcard
|| c
->spd
.that
.has_port_wildcard
1110 || c
->spd
.that
.has_id_wildcards
|| c
->spd
.that
.allow_any
)
1112 /* Opportunistic or Road Warrior or wildcard client subnet
1114 c
->kind
= CK_TEMPLATE
;
1118 c
->kind
= CK_PERMANENT
;
1120 set_policy_prio(c
); /* must be after kind is set */
1123 c
->extra_debugging
= wm
->debugging
;
1128 passert(!(wm
->left
.virt
&& wm
->right
.virt
));
1129 if (wm
->left
.virt
|| wm
->right
.virt
)
1131 passert(isanyaddr(&c
->spd
.that
.host_addr
));
1132 c
->spd
.that
.virt
= create_virtual(c
,
1133 wm
->left
.virt ? wm
->left
.virt
: wm
->right
.virt
);
1134 if (c
->spd
.that
.virt
)
1135 c
->spd
.that
.has_client
= TRUE
;
1138 unshare_connection_strings(c
);
1142 connect_to_host_pair(c
);
1144 /* log all about this connection */
1145 plog("added connection description \"%s\"", c
->name
);
1147 char topo
[CONNECTION_BUF
];
1149 (void) format_connection(topo
, sizeof(topo
), c
, &c
->spd
);
1151 DBG_log("%s", topo
);
1153 /* Make sure that address families can be correctly inferred
1154 * from printed ends.
1156 passert(c
->addr_family
== addrtypeof(&c
->spd
.this.host_addr
)
1157 && c
->addr_family
== addrtypeof(&c
->spd
.this.host_nexthop
)
1158 && (c
->spd
.this.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1159 == subnettypeof(&c
->spd
.this.client
)
1161 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_addr
)
1162 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_nexthop
)
1163 && (c
->spd
.that
.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1164 == subnettypeof(&c
->spd
.that
.client
));
1166 DBG_log("ike_life: %lus; ipsec_life: %lus; rekey_margin: %lus;"
1167 " rekey_fuzz: %lu%%; keyingtries: %lu; policy: %s"
1168 , (unsigned long) c
->sa_ike_life_seconds
1169 , (unsigned long) c
->sa_ipsec_life_seconds
1170 , (unsigned long) c
->sa_rekey_margin
1171 , (unsigned long) c
->sa_rekey_fuzz
1172 , (unsigned long) c
->sa_keying_tries
1173 , prettypolicy(c
->policy
));
1178 /* Derive a template connection from a group connection and target.
1179 * Similar to instantiate(). Happens at whack --listen.
1180 * Returns name of new connection. May be NULL.
1181 * Caller is responsible for pfreeing.
1184 add_group_instance(struct connection
*group
, const ip_subnet
*target
)
1187 , targetbuf
[SUBNETTOT_BUF
];
1188 struct connection
*t
;
1191 passert(group
->kind
== CK_GROUP
);
1192 passert(oriented(*group
));
1194 /* manufacture a unique name for this template */
1195 subnettot(target
, 0, targetbuf
, sizeof(targetbuf
));
1196 snprintf(namebuf
, sizeof(namebuf
), "%s#%s", group
->name
, targetbuf
);
1198 if (con_by_name(namebuf
, FALSE
) != NULL
)
1200 loglog(RC_DUPNAME
, "group name + target yields duplicate name \"%s\""
1205 t
= clone_thing(*group
, "group instance");
1207 unshare_connection_strings(t
);
1208 name
= clone_str(t
->name
, "group instance name");
1209 t
->spd
.that
.client
= *target
;
1210 t
->policy
&= ~(POLICY_GROUP
| POLICY_GROUTED
);
1211 t
->kind
= isanyaddr(&t
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(t
->policy
)
1212 ? CK_TEMPLATE
: CK_INSTANCE
;
1214 /* reset log file info */
1215 t
->log_file_name
= NULL
;
1217 t
->log_file_err
= FALSE
;
1219 t
->spd
.reqid
= gen_reqid();
1221 if (t
->spd
.that
.virt
)
1223 DBG_log("virtual_ip not supported in group instance");
1224 t
->spd
.that
.virt
= NULL
;
1227 /* add to connections list */
1228 t
->ac_next
= connections
;
1231 /* same host_pair as parent: stick after parent on list */
1234 /* route if group is routed */
1235 if (group
->policy
& POLICY_GROUTED
)
1237 if (!trap_connection(t
))
1238 whack_log(RC_ROUTE
, "could not route");
1244 /* an old target has disappeared for a group: delete instance */
1246 remove_group_instance(const struct connection
*group USED_BY_DEBUG
1249 passert(group
->kind
== CK_GROUP
);
1250 passert(oriented(*group
));
1252 delete_connections_by_name(name
, FALSE
);
1255 /* Common part of instantiating a Road Warrior or Opportunistic connection.
1256 * his_id can be used to carry over an ID discovered in Phase 1.
1257 * It must not disagree with the one in c, but if that is unspecified,
1258 * the new connection will use his_id.
1259 * If his_id is NULL, and c.that.id is uninstantiated (ID_NONE), the
1260 * new connection will continue to have an uninstantiated that.id.
1261 * Note: instantiation does not affect port numbers.
1263 * Note that instantiate can only deal with a single SPD/eroute.
1265 static struct connection
*
1266 instantiate(struct connection
*c
, const ip_address
*him
1267 , u_int16_t his_port
1268 , const struct id
*his_id
)
1270 struct connection
*d
;
1273 passert(c
->kind
== CK_TEMPLATE
);
1274 passert(c
->spd
.next
== NULL
);
1276 c
->instance_serial
++;
1277 d
= clone_thing(*c
, "temporary connection");
1278 d
->spd
.that
.allow_any
= FALSE
;
1282 passert(match_id(his_id
, &d
->spd
.that
.id
, &wildcards
));
1283 d
->spd
.that
.id
= *his_id
;
1284 d
->spd
.that
.has_id_wildcards
= FALSE
;
1286 unshare_connection_strings(d
);
1287 unshare_ietfAttrList(&d
->spd
.this.groups
);
1288 unshare_ietfAttrList(&d
->spd
.that
.groups
);
1289 d
->kind
= CK_INSTANCE
;
1291 passert(oriented(*d
));
1292 d
->spd
.that
.host_addr
= *him
;
1293 setportof(htons(c
->spd
.that
.port
), &d
->spd
.that
.host_addr
);
1295 if (his_port
) d
->spd
.that
.host_port
= his_port
;
1297 default_end(&d
->spd
.that
, &d
->spd
.this.host_addr
);
1299 /* We cannot guess what our next_hop should be, but if it was
1300 * explicitly specified as 0.0.0.0, we set it to be him.
1301 * (whack will not allow nexthop to be elided in RW case.)
1303 default_end(&d
->spd
.this, &d
->spd
.that
.host_addr
);
1305 d
->spd
.reqid
= gen_reqid();
1307 /* set internal fields */
1308 d
->ac_next
= connections
;
1310 d
->spd
.routing
= RT_UNROUTED
;
1311 d
->newest_isakmp_sa
= SOS_NOBODY
;
1312 d
->newest_ipsec_sa
= SOS_NOBODY
;
1313 d
->spd
.eroute_owner
= SOS_NOBODY
;
1315 /* reset log file info */
1316 d
->log_file_name
= NULL
;
1318 d
->log_file_err
= FALSE
;
1320 connect_to_host_pair(d
);
1323 if (sameaddr(&d
->spd
.that
.host_addr
, &d
->spd
.this.host_nexthop
))
1325 d
->spd
.this.host_nexthop
= *him
;
1330 rw_instantiate(struct connection
*c
, const ip_address
*him
, u_int16_t his_port
1331 , const ip_subnet
*his_net
, const struct id
*his_id
)
1333 struct connection
*d
= instantiate(c
, him
, his_port
, his_id
);
1335 if (d
&& his_net
&& is_virtual_connection(c
))
1337 d
->spd
.that
.client
= *his_net
;
1338 d
->spd
.that
.virt
= NULL
;
1339 if (subnetishost(his_net
) && addrinsubnet(him
, his_net
))
1340 d
->spd
.that
.has_client
= FALSE
;
1343 if (d
->policy
& POLICY_OPPO
)
1345 /* This must be before we know the client addresses.
1346 * Fill in one that is impossible. This prevents anyone else from
1347 * trying to use this connection to get to a particular client
1349 d
->spd
.that
.client
= *aftoinfo(subnettypeof(&d
->spd
.that
.client
))->none
;
1352 , DBG_log("instantiated \"%s\" for %s" , d
->name
, ip_str(him
)));
1357 oppo_instantiate(struct connection
*c
1358 , const ip_address
*him
1359 , const struct id
*his_id
1360 , struct gw_info
*gw
1361 , const ip_address
*our_client USED_BY_DEBUG
1362 , const ip_address
*peer_client
)
1364 struct connection
*d
= instantiate(c
, him
, 0, his_id
);
1366 passert(d
->spd
.next
== NULL
);
1368 /* fill in our client side */
1369 if (d
->spd
.this.has_client
)
1371 /* there was a client in the abstract connection
1372 * so we demand that the required client is within that subnet.
1374 passert(addrinsubnet(our_client
, &d
->spd
.this.client
));
1375 happy(addrtosubnet(our_client
, &d
->spd
.this.client
));
1376 /* opportunistic connections do not use port selectors */
1377 setportof(0, &d
->spd
.this.client
.addr
);
1381 /* there was no client in the abstract connection
1382 * so we demand that the required client be the host
1384 passert(sameaddr(our_client
, &d
->spd
.this.host_addr
));
1387 /* fill in peer's client side.
1388 * If the client is the peer, excise the client from the connection.
1390 passert((d
->policy
& POLICY_OPPO
)
1391 && addrinsubnet(peer_client
, &d
->spd
.that
.client
));
1392 happy(addrtosubnet(peer_client
, &d
->spd
.that
.client
));
1393 /* opportunistic connections do not use port selectors */
1394 setportof(0, &d
->spd
.that
.client
.addr
);
1396 if (sameaddr(peer_client
, &d
->spd
.that
.host_addr
))
1397 d
->spd
.that
.has_client
= FALSE
;
1399 passert(d
->gw_info
== NULL
);
1403 /* Adjust routing if something is eclipsing c.
1404 * It must be a %hold for us (hard to passert this).
1405 * If there was another instance eclipsing, we'd be using it.
1407 if (c
->spd
.routing
== RT_ROUTED_ECLIPSED
)
1408 d
->spd
.routing
= RT_ROUTED_PROSPECTIVE
;
1410 /* Remember if the template is routed:
1411 * if so, this instance applies for initiation
1412 * even if it is created for responding.
1414 if (routed(c
->spd
.routing
))
1415 d
->instance_initiation_ok
= TRUE
;
1418 char topo
[CONNECTION_BUF
];
1420 (void) format_connection(topo
, sizeof(topo
), d
, &d
->spd
);
1421 DBG_log("instantiated \"%s\": %s", d
->name
, topo
);
1426 /* priority formatting */
1428 fmt_policy_prio(policy_prio_t pp
, char buf
[POLICY_PRIO_BUF
])
1430 if (pp
== BOTTOM_PRIO
)
1431 snprintf(buf
, POLICY_PRIO_BUF
, "0");
1433 snprintf(buf
, POLICY_PRIO_BUF
, "%lu,%lu"
1434 , pp
>>16, (pp
& ~(~(policy_prio_t
)0 << 16)) >> 8);
1437 /* Format any information needed to identify an instance of a connection.
1438 * Fills any needed information into buf which MUST be big enough.
1439 * Road Warrior: peer's IP address
1440 * Opportunistic: [" " myclient "==="] " ..." peer ["===" hisclient] '\0'
1443 fmt_client(const ip_subnet
*client
, const ip_address
*gw
, const char *prefix
, char buf
[ADDRTOT_BUF
])
1445 if (subnetisaddr(client
, gw
))
1447 buf
[0] = '\0'; /* compact denotation for "self" */
1453 strcpy(buf
, prefix
);
1454 ap
= buf
+ strlen(prefix
);
1455 if (subnetisnone(client
))
1456 strcpy(ap
, "?"); /* unknown */
1458 subnettot(client
, 0, ap
, SUBNETTOT_BUF
);
1464 fmt_conn_instance(const struct connection
*c
, char buf
[CONN_INST_BUF
])
1470 if (c
->kind
== CK_INSTANCE
)
1472 if (c
->instance_serial
!= 0)
1474 snprintf(p
, CONN_INST_BUF
, "[%lu]", c
->instance_serial
);
1478 if (c
->policy
& POLICY_OPPO
)
1480 size_t w
= fmt_client(&c
->spd
.this.client
, &c
->spd
.this.host_addr
, " ", p
);
1484 strcpy(p
, w
== 0?
" ..." : "=== ...");
1487 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1490 (void) fmt_client(&c
->spd
.that
.client
, &c
->spd
.that
.host_addr
, "===", p
);
1495 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1497 if (c
->spd
.that
.host_port
!= pluto_port
)
1500 sprintf(p
, ":%d", c
->spd
.that
.host_port
);
1506 /* Find an existing connection for a trapped outbound packet.
1507 * This is attempted before we bother with gateway discovery.
1508 * + this connection is routed or instance_of_routed_template
1509 * (i.e. approved for on-demand)
1510 * + this subnet contains our_client (or we are our_client)
1511 * + that subnet contains peer_client (or peer is peer_client)
1512 * + don't care about Phase 1 IDs (we don't know)
1513 * Note: result may still need to be instantiated.
1514 * The winner has the highest policy priority.
1516 * If there are several with that priority, we give preference to
1517 * the first one that is an instance.
1519 * See also build_outgoing_opportunistic_connection.
1522 find_connection_for_clients(struct spd_route
**srp
,
1523 const ip_address
*our_client
,
1524 const ip_address
*peer_client
,
1525 int transport_proto
)
1527 struct connection
*c
= connections
, *best
= NULL
;
1528 policy_prio_t best_prio
= BOTTOM_PRIO
;
1529 struct spd_route
*sr
;
1530 struct spd_route
*best_sr
= NULL
;
1531 int our_port
= ntohs(portof(our_client
));
1532 int peer_port
= ntohs(portof(peer_client
));
1534 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1536 if (DBGP(DBG_CONTROL
))
1538 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1540 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1541 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1542 DBG_log("find_connection: "
1543 "looking for policy for connection: %s:%d/%d -> %s:%d/%d"
1544 , ocb
, transport_proto
, our_port
, pcb
, transport_proto
, peer_port
);
1548 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
1550 if (c
->kind
== CK_GROUP
)
1553 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1555 if ((routed(sr
->routing
) || c
->instance_initiation_ok
)
1556 && addrinsubnet(our_client
, &sr
->this.client
)
1557 && addrinsubnet(peer_client
, &sr
->that
.client
)
1558 && addrinsubnet(peer_client
, &sr
->that
.client
)
1559 && (!sr
->this.protocol
|| transport_proto
== sr
->this.protocol
)
1560 && (!sr
->this.port
|| our_port
== sr
->this.port
)
1561 && (!sr
->that
.port
|| peer_port
== sr
->that
.port
))
1563 char cib
[CONN_INST_BUF
];
1564 char cib2
[CONN_INST_BUF
];
1566 policy_prio_t prio
= 8 * (c
->prio
+ (c
->kind
== CK_INSTANCE
))
1567 + 2 * (sr
->this.port
== our_port
)
1568 + 2 * (sr
->that
.port
== peer_port
)
1569 + (sr
->this.protocol
== transport_proto
);
1572 if (DBGP(DBG_CONTROL
|DBG_CONTROLMORE
))
1574 char c_ocb
[SUBNETTOT_BUF
], c_pcb
[SUBNETTOT_BUF
];
1576 subnettot(&c
->spd
.this.client
, 0, c_ocb
, sizeof(c_ocb
));
1577 subnettot(&c
->spd
.that
.client
, 0, c_pcb
, sizeof(c_pcb
));
1578 DBG_log("find_connection: conn \"%s\"%s has compatible peers: %s->%s [pri: %ld]"
1580 , (fmt_conn_instance(c
, cib
), cib
)
1581 , c_ocb
, c_pcb
, prio
);
1592 DBG(DBG_CONTROLMORE
,
1593 DBG_log("find_connection: "
1594 "comparing best \"%s\"%s [pri:%ld]{%p} (child %s) to \"%s\"%s [pri:%ld]{%p} (child %s)"
1596 , (fmt_conn_instance(best
, cib
), cib
)
1599 , (best
->policy_next ? best
->policy_next
->name
: "none")
1601 , (fmt_conn_instance(c
, cib2
), cib2
)
1604 , (c
->policy_next ? c
->policy_next
->name
: "none")));
1606 if (prio
> best_prio
)
1616 if (best
!= NULL
&& NEVER_NEGOTIATE(best
->policy
))
1619 if (srp
!= NULL
&& best
!= NULL
)
1623 if (DBGP(DBG_CONTROL
))
1627 char cib
[CONN_INST_BUF
];
1628 DBG_log("find_connection: concluding with \"%s\"%s [pri:%ld]{%p} kind=%s"
1630 , (fmt_conn_instance(best
, cib
), cib
)
1633 , enum_name(&connection_kind_names
, best
->kind
));
1635 DBG_log("find_connection: concluding with empty");
1643 /* Find and instantiate a connection for an outgoing Opportunistic connection.
1644 * We've already discovered its gateway.
1645 * We look for a the connection such that:
1646 * + this is one of our interfaces
1647 * + this subnet contains our_client (or we are our_client)
1648 * (we will specialize the client). We prefer the smallest such subnet.
1649 * + that subnet contains peer_clent (we will specialize the client).
1650 * We prefer the smallest such subnet.
1651 * + is opportunistic
1652 * + that peer is NO_IP
1653 * + don't care about Phase 1 IDs (probably should be default)
1654 * We could look for a connection that already had the desired peer
1655 * (rather than NO_IP) specified, but it doesn't seem worth the
1658 * We look for the routed policy applying to the narrowest subnets.
1659 * We only succeed if we find such a policy AND it is satisfactory.
1661 * The body of the inner loop is a lot like that in
1662 * find_connection_for_clients. In this case, we know the gateways
1663 * that we need to instantiate an opportunistic connection.
1666 build_outgoing_opportunistic_connection(struct gw_info
*gw
1667 ,const ip_address
*our_client
1668 ,const ip_address
*peer_client
)
1671 struct connection
*best
= NULL
;
1672 struct spd_route
*sr
, *bestsr
;
1673 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1675 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1676 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1678 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1680 /* We don't know his ID yet, so gw id must be an ipaddr */
1681 passert(gw
->key
!= NULL
);
1682 passert(id_is_ipaddr(&gw
->gw_id
));
1684 /* for each of our addresses... */
1685 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1687 /* go through those connections with our address and NO_IP as hosts
1688 * We cannot know what port the peer would use, so we assume
1689 * that it is pluto_port (makes debugging easier).
1691 struct connection
*c
= find_host_pair_connections(&p
->addr
1692 , pluto_port
, (ip_address
*)NULL
, pluto_port
);
1694 for (; c
!= NULL
; c
= c
->hp_next
)
1697 DBG_log("checking %s", c
->name
));
1698 if (c
->kind
== CK_GROUP
)
1703 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1705 if (routed(sr
->routing
)
1706 && addrinsubnet(our_client
, &sr
->this.client
)
1707 && addrinsubnet(peer_client
, &sr
->that
.client
))
1716 DBG_log("comparing best %s to %s"
1717 , best
->name
, c
->name
));
1719 for (bestsr
= &best
->spd
; best
!=c
&& bestsr
; bestsr
=bestsr
->next
)
1721 if (!subnetinsubnet(&bestsr
->this.client
, &sr
->this.client
)
1722 || (samesubnet(&bestsr
->this.client
, &sr
->this.client
)
1723 && !subnetinsubnet(&bestsr
->that
.client
1724 , &sr
->that
.client
)))
1735 || NEVER_NEGOTIATE(best
->policy
)
1736 || (best
->policy
& POLICY_OPPO
) == LEMPTY
1737 || best
->kind
!= CK_TEMPLATE
)
1740 return oppo_instantiate(best
, &gw
->gw_id
.ip_addr
, NULL
, gw
1741 , our_client
, peer_client
);
1745 orient(struct connection
*c
)
1747 struct spd_route
*sr
;
1753 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
1755 /* Note: this loop does not stop when it finds a match:
1756 * it continues checking to catch any ambiguity.
1758 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1765 /* check if this interface matches this end */
1766 if (sameaddr(&sr
->this.host_addr
, &p
->addr
)
1767 && (!no_klips
|| sr
->this.host_port
== pluto_port
))
1771 if (c
->interface
== p
)
1772 loglog(RC_LOG_SERIOUS
1773 , "both sides of \"%s\" are our interface %s!"
1774 , c
->name
, p
->rname
);
1776 loglog(RC_LOG_SERIOUS
, "two interfaces match \"%s\" (%s, %s)"
1777 , c
->name
, c
->interface
->rname
, p
->rname
);
1778 c
->interface
= NULL
; /* withdraw orientation */
1784 /* done with this interface if it doesn't match that end */
1785 if (!(sameaddr(&sr
->that
.host_addr
, &p
->addr
)
1786 && (!no_klips
|| sr
->that
.host_port
== pluto_port
)))
1789 /* swap ends and try again.
1790 * It is a little tricky to see that this loop will stop.
1791 * Only continue if the far side matches.
1792 * If both sides match, there is an error-out.
1795 struct end t
= sr
->this;
1797 sr
->this = sr
->that
;
1804 return oriented(*c
);
1808 initiate_connection(const char *name
, int whackfd
)
1810 struct connection
*c
= con_by_name(name
, TRUE
);
1812 if (c
!= NULL
&& c
->ikev1
)
1814 set_cur_connection(c
);
1817 loglog(RC_ORIENT
, "we have no ipsecN interface for either end of this connection");
1819 else if (NEVER_NEGOTIATE(c
->policy
))
1822 , "cannot initiate an authby=never connection");
1824 else if (c
->kind
!= CK_PERMANENT
&& !c
->spd
.that
.allow_any
)
1826 if (isanyaddr(&c
->spd
.that
.host_addr
))
1827 loglog(RC_NOPEERIP
, "cannot initiate connection without knowing peer IP address");
1829 loglog(RC_WILDCARD
, "cannot initiate connection with ID wildcards");
1833 /* do we have to prompt for a PIN code? */
1834 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
&& whackfd
!= NULL_FD
)
1836 scx_get_pin(c
->spd
.this.sc
, whackfd
);
1838 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
)
1840 loglog(RC_NOVALIDPIN
, "cannot initiate connection without valid PIN");
1845 if (c
->spd
.that
.allow_any
)
1847 c
= instantiate(c
, &c
->spd
.that
.host_addr
, c
->spd
.that
.host_port
1851 /* We will only request an IPsec SA if policy isn't empty
1852 * (ignoring Main Mode items).
1853 * This is a fudge, but not yet important.
1854 * If we are to proceed asynchronously, whackfd will be NULL_FD.
1856 c
->policy
|= POLICY_UP
;
1857 ipsecdoi_initiate(whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
1858 whackfd
= NULL_FD
; /* protect from close */
1861 reset_cur_connection();
1866 /* (Possibly) Opportunistic Initiation:
1867 * Knowing clients (single IP addresses), try to build an tunnel.
1868 * This may involve discovering a gateway and instantiating an
1869 * Opportunistic connection. Called when a packet is caught by
1870 * a %trap, or when whack --oppohere --oppothere is used.
1871 * It may turn out that an existing or non-opporunistic connnection
1872 * can handle the traffic.
1874 * Most of the code will be restarted if an ADNS request is made
1875 * to discover the gateway. The only difference between the first
1876 * and second entry is whether gateways_from_dns is NULL or not.
1877 * initiate_opportunistic: initial entrypoint
1878 * continue_oppo: where we pickup when ADNS result arrives
1879 * initiate_opportunistic_body: main body shared by above routines
1880 * cannot_oppo: a helper function to log a diagnostic
1881 * This structure repeats a lot of code when the ADNS result arrives.
1882 * This seems like a waste, but anything learned the first time through
1883 * may no longer be true!
1885 * After the first IKE message is sent, the regular state machinery
1886 * carries negotiation forward.
1889 enum find_oppo_step
{
1892 fos_myid_hostname_txt
,
1894 fos_myid_hostname_key
,
1899 #endif /* USE_KEYRR */
1905 static const char *const oppo_step_name
[] = {
1908 "fos_myid_hostname_txt",
1910 "fos_myid_hostname_key",
1915 #endif /* USE_KEYRR */
1921 struct find_oppo_bundle
{
1922 enum find_oppo_step step
;
1924 bool failure_ok
; /* if true, continue_oppo should not die on DNS failure */
1925 ip_address our_client
; /* not pointer! */
1926 ip_address peer_client
;
1927 int transport_proto
;
1929 policy_prio_t policy_prio
;
1930 ipsec_spi_t failure_shunt
; /* in host order! 0 for delete. */
1934 struct find_oppo_continuation
{
1935 struct adns_continuation ac
; /* common prefix */
1936 struct find_oppo_bundle b
;
1940 cannot_oppo(struct connection
*c
1941 , struct find_oppo_bundle
*b
1944 char pcb
[ADDRTOT_BUF
];
1945 char ocb
[ADDRTOT_BUF
];
1947 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
1948 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
1950 DBG(DBG_DNS
| DBG_OPPO
, DBG_log("Can't Opportunistically initiate for %s to %s: %s"
1953 whack_log(RC_OPPOFAILURE
1954 , "Can't Opportunistically initiate for %s to %s: %s"
1957 if (c
!= NULL
&& c
->policy_next
!= NULL
)
1959 /* there is some policy that comes afterwards */
1960 struct spd_route
*shunt_spd
;
1961 struct connection
*nc
= c
->policy_next
;
1964 passert(c
->kind
== CK_TEMPLATE
);
1965 passert(c
->policy_next
->kind
== CK_PERMANENT
);
1967 DBG(DBG_OPPO
, DBG_log("OE failed for %s to %s, but %s overrides shunt"
1968 , ocb
, pcb
, c
->policy_next
->name
));
1971 * okay, here we need add to the "next" policy, which is ought
1972 * to be an instance.
1973 * We will add another entry to the spd_route list for the specific
1974 * situation that we have.
1977 shunt_spd
= clone_thing(nc
->spd
, "shunt eroute policy");
1979 shunt_spd
->next
= nc
->spd
.next
;
1980 nc
->spd
.next
= shunt_spd
;
1982 happy(addrtosubnet(&b
->peer_client
, &shunt_spd
->that
.client
));
1984 if (sameaddr(&b
->peer_client
, &shunt_spd
->that
.host_addr
))
1985 shunt_spd
->that
.has_client
= FALSE
;
1988 * override the tunnel destination with the one from the secondaried
1991 shunt_spd
->that
.host_addr
= nc
->spd
.that
.host_addr
;
1993 /* now, lookup the state, and poke it up.
1996 st
= state_with_serialno(nc
->newest_ipsec_sa
);
1998 /* XXX what to do if the IPSEC SA has died? */
1999 passert(st
!= NULL
);
2001 /* link the new connection instance to the state's list of
2005 DBG(DBG_OPPO
, DBG_log("installing state: %ld for %s to %s"
2006 , nc
->newest_ipsec_sa
2010 if (DBGP(DBG_OPPO
| DBG_CONTROLMORE
))
2012 char state_buf
[LOG_WIDTH
];
2013 char state_buf2
[LOG_WIDTH
];
2016 fmt_state(FALSE
, st
, n
2017 , state_buf
, sizeof(state_buf
)
2018 , state_buf2
, sizeof(state_buf2
));
2019 DBG_log("cannot_oppo, failure SA1: %s", state_buf
);
2020 DBG_log("cannot_oppo, failure SA2: %s", state_buf2
);
2024 if (!route_and_eroute(c
, shunt_spd
, st
))
2026 whack_log(RC_OPPOFAILURE
2027 , "failed to instantiate shunt policy %s for %s to %s"
2037 /* Replace HOLD with b->failure_shunt.
2038 * If no b->failure_shunt specified, use SPI_PASS -- THIS MAY CHANGE.
2040 if (b
->failure_shunt
== 0)
2042 DBG(DBG_OPPO
, DBG_log("no explicit failure shunt for %s to %s; installing %%pass"
2046 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2049 , b
->failure_shunt
!= 0
2050 , b
->transport_proto
2056 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
2057 , struct adns_continuation
*ac
, err_t ac_ugh
); /* forward */
2060 initiate_opportunistic(const ip_address
*our_client
2061 , const ip_address
*peer_client
2062 , int transport_proto
2066 struct find_oppo_bundle b
;
2068 b
.want
= (whackfd
== NULL_FD ?
"whack" : "acquire");
2069 b
.failure_ok
= FALSE
;
2070 b
.our_client
= *our_client
;
2071 b
.peer_client
= *peer_client
;
2072 b
.transport_proto
= transport_proto
;
2074 b
.policy_prio
= BOTTOM_PRIO
;
2075 b
.failure_shunt
= 0;
2076 b
.whackfd
= whackfd
;
2078 initiate_opportunistic_body(&b
, NULL
, NULL
);
2082 continue_oppo(struct adns_continuation
*acr
, err_t ugh
)
2084 struct find_oppo_continuation
*cr
= (void *)acr
; /* inherit, damn you! */
2085 struct connection
*c
;
2086 bool was_held
= cr
->b
.held
;
2087 int whackfd
= cr
->b
.whackfd
;
2089 /* note: cr->id has no resources; cr->sgw_id is id_none:
2090 * neither need freeing.
2092 whack_log_fd
= whackfd
;
2095 /* Discover and record whether %hold has gone away.
2096 * This could have happened while we were awaiting DNS.
2097 * We must check BEFORE any call to cannot_oppo.
2100 cr
->b
.held
= has_bare_hold(&cr
->b
.our_client
, &cr
->b
.peer_client
2101 , cr
->b
.transport_proto
);
2105 /* if we're going to ignore the error, at least note it in debugging log */
2106 if (cr
->b
.failure_ok
&& ugh
!= NULL
)
2108 DBG(DBG_CONTROL
| DBG_DNS
,
2110 char ocb
[ADDRTOT_BUF
];
2111 char pcb
[ADDRTOT_BUF
];
2113 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2114 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2115 DBG_log("continuing from failed DNS lookup for %s, %s to %s: %s"
2116 , cr
->b
.want
, ocb
, pcb
, ugh
);
2121 if (!cr
->b
.failure_ok
&& ugh
!= NULL
)
2123 c
= find_connection_for_clients(NULL
, &cr
->b
.our_client
, &cr
->b
.peer_client
2124 , cr
->b
.transport_proto
);
2125 cannot_oppo(c
, &cr
->b
2126 , builddiag("%s: %s", cr
->b
.want
, ugh
));
2128 else if (was_held
&& !cr
->b
.held
)
2130 /* was_held indicates we were started due to a %trap firing
2131 * (as opposed to a "whack --oppohere --oppothere").
2132 * Since the %hold has gone, we can assume that somebody else
2133 * has beaten us to the punch. We can go home. But lets log it.
2135 char ocb
[ADDRTOT_BUF
];
2136 char pcb
[ADDRTOT_BUF
];
2138 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2139 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2142 , "%%hold otherwise handled during DNS lookup for Opportunistic Initiation for %s to %s"
2147 initiate_opportunistic_body(&cr
->b
, &cr
->ac
, ugh
);
2148 whackfd
= NULL_FD
; /* was handed off */
2151 whack_log_fd
= NULL_FD
;
2157 check_key_recs(enum myid_state try_state
2158 , const struct connection
*c
2159 , struct adns_continuation
*ac
)
2161 /* Check if KEY lookup yielded good results.
2162 * Looking up based on our ID. Used if
2163 * client is ourself, or if TXT had no public key.
2164 * Note: if c is different this time, there is
2165 * a chance that we did the wrong query.
2166 * If so, treat as a kind of failure.
2168 enum myid_state old_myid_state
= myid_state
;
2169 const struct RSA_private_key
*our_RSA_pri
;
2172 myid_state
= try_state
;
2174 if (old_myid_state
!= myid_state
2175 && old_myid_state
== MYID_SPECIFIED
)
2177 ugh
= "%myid was specified while we were guessing";
2179 else if ((our_RSA_pri
= get_RSA_private_key(c
)) == NULL
)
2181 ugh
= "we don't know our own RSA key";
2183 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2185 ugh
= "our ID changed underfoot";
2189 /* Similar to code in RSA_check_signature
2190 * for checking the other side.
2194 ugh
= "no KEY RR found for us";
2195 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2197 ugh
= "all our KEY RRs have the wrong public key";
2198 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2199 && same_RSA_public_key(&our_RSA_pri
->pub
, &kr
->key
->u
.rsa
))
2201 ugh
= NULL
; /* good! */
2207 myid_state
= old_myid_state
;
2210 #endif /* USE_KEYRR */
2213 check_txt_recs(enum myid_state try_state
2214 , const struct connection
*c
2215 , struct adns_continuation
*ac
)
2217 /* Check if TXT lookup yielded good results.
2218 * Looking up based on our ID. Used if
2219 * client is ourself, or if TXT had no public key.
2220 * Note: if c is different this time, there is
2221 * a chance that we did the wrong query.
2222 * If so, treat as a kind of failure.
2224 enum myid_state old_myid_state
= myid_state
;
2225 const struct RSA_private_key
*our_RSA_pri
;
2228 myid_state
= try_state
;
2230 if (old_myid_state
!= myid_state
2231 && old_myid_state
== MYID_SPECIFIED
)
2233 ugh
= "%myid was specified while we were guessing";
2235 else if ((our_RSA_pri
= get_RSA_private_key(c
)) == NULL
)
2237 ugh
= "we don't know our own RSA key";
2239 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2241 ugh
= "our ID changed underfoot";
2245 /* Similar to code in RSA_check_signature
2246 * for checking the other side.
2248 struct gw_info
*gwp
;
2250 ugh
= "no TXT RR found for us";
2251 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2253 ugh
= "all our TXT RRs have the wrong public key";
2254 if (gwp
->key
->alg
== PUBKEY_ALG_RSA
2255 && same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2257 ugh
= NULL
; /* good! */
2263 myid_state
= old_myid_state
;
2268 /* note: gateways_from_dns must be NULL iff this is the first call */
2270 initiate_opportunistic_body(struct find_oppo_bundle
*b
2271 , struct adns_continuation
*ac
2274 struct connection
*c
;
2275 struct spd_route
*sr
;
2277 /* What connection shall we use?
2278 * First try for one that explicitly handles the clients.
2282 char ours
[ADDRTOT_BUF
];
2283 char his
[ADDRTOT_BUF
];
2287 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2288 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2289 ourport
= ntohs(portof(&b
->our_client
));
2290 hisport
= ntohs(portof(&b
->peer_client
));
2291 DBG_log("initiate on demand from %s:%d to %s:%d proto=%d state: %s because: %s"
2292 , ours
, ourport
, his
, hisport
, b
->transport_proto
2293 , oppo_step_name
[b
->step
], b
->want
);
2295 if (isanyaddr(&b
->our_client
) || isanyaddr(&b
->peer_client
))
2297 cannot_oppo(NULL
, b
, "impossible IP address");
2299 else if ((c
= find_connection_for_clients(&sr
2302 , b
->transport_proto
)) == NULL
)
2304 /* No connection explicitly handles the clients and there
2305 * are no Opportunistic connections -- whine and give up.
2306 * The failure policy cannot be gotten from a connection; we pick %pass.
2308 cannot_oppo(NULL
, b
, "no routed Opportunistic template covers this pair");
2310 else if (c
->kind
!= CK_TEMPLATE
)
2312 /* We've found a connection that can serve.
2313 * Do we have to initiate it?
2314 * Not if there is currently an IPSEC SA.
2315 * But if there is an IPSEC SA, then KLIPS would not
2316 * have generated the acquire. So we assume that there isn't one.
2317 * This may be redundant if a non-opportunistic
2318 * negotiation is already being attempted.
2321 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2323 if(c
->kind
== CK_INSTANCE
)
2325 char cib
[CONN_INST_BUF
];
2326 /* there is already an instance being negotiated, no nothing */
2327 DBG(DBG_CONTROL
, DBG_log("found existing instance \"%s\"%s, rekeying it"
2329 , (fmt_conn_instance(c
, cib
), cib
)));
2330 /* XXX-mcr - return; */
2333 /* otherwise, there is some kind of static conn that can handle
2334 * this connection, so we initiate it */
2339 /* what should we do on failure? */
2340 (void) assign_hold(c
, sr
, b
->transport_proto
, &b
->our_client
, &b
->peer_client
);
2343 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2344 b
->whackfd
= NULL_FD
; /* protect from close */
2348 /* We are handling an opportunistic situation.
2349 * This involves several DNS lookup steps that require suspension.
2350 * Note: many facts might change while we're suspended.
2353 * The first chunk of code handles the result of the previous
2354 * DNS query (if any). It also selects the kind of the next step.
2355 * The second chunk initiates the next DNS query (if any).
2357 enum find_oppo_step next_step
;
2359 char mycredentialstr
[BUF_LEN
];
2360 char cib
[CONN_INST_BUF
];
2362 DBG(DBG_CONTROL
, DBG_log("creating new instance from \"%s\"%s"
2364 , (fmt_conn_instance(c
, cib
), cib
)));
2367 idtoa(&sr
->this.id
, mycredentialstr
, sizeof(mycredentialstr
));
2369 passert(c
->policy
& POLICY_OPPO
); /* can't initiate Road Warrior connections */
2371 /* handle any DNS answer; select next step */
2376 /* just starting out: select first query step */
2377 next_step
= fos_myid_ip_txt
;
2380 case fos_myid_ip_txt
: /* TXT for our default IP address as %myid */
2381 ugh
= check_txt_recs(MYID_IP
, c
, ac
);
2384 /* cannot use our IP as OE identitiy for initiation */
2385 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:TXT) as identity: %s"
2388 if (!logged_myid_ip_txt_warning
)
2390 loglog(RC_LOG_SERIOUS
2391 , "can not use our IP (%s:TXT) as identity: %s"
2394 logged_myid_ip_txt_warning
= TRUE
;
2397 next_step
= fos_myid_hostname_txt
;
2398 ugh
= NULL
; /* failure can be recovered from */
2402 /* we can use our IP as OE identity for initiation */
2403 if (!logged_myid_ip_txt_warning
)
2405 loglog(RC_LOG_SERIOUS
2406 , "using our IP (%s:TXT) as identity!"
2407 , myid_str
[MYID_IP
]);
2408 logged_myid_ip_txt_warning
= TRUE
;
2411 next_step
= fos_our_client
;
2415 case fos_myid_hostname_txt
: /* TXT for our hostname as %myid */
2416 ugh
= check_txt_recs(MYID_HOSTNAME
, c
, ac
);
2419 /* cannot use our hostname as OE identitiy for initiation */
2420 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:TXT) as identity: %s"
2421 , myid_str
[MYID_HOSTNAME
]
2423 if (!logged_myid_fqdn_txt_warning
)
2425 loglog(RC_LOG_SERIOUS
2426 , "can not use our hostname (%s:TXT) as identity: %s"
2427 , myid_str
[MYID_HOSTNAME
]
2429 logged_myid_fqdn_txt_warning
= TRUE
;
2432 next_step
= fos_myid_ip_key
;
2433 ugh
= NULL
; /* failure can be recovered from */
2438 /* we can use our hostname as OE identity for initiation */
2439 if (!logged_myid_fqdn_txt_warning
)
2441 loglog(RC_LOG_SERIOUS
2442 , "using our hostname (%s:TXT) as identity!"
2443 , myid_str
[MYID_HOSTNAME
]);
2444 logged_myid_fqdn_txt_warning
= TRUE
;
2446 next_step
= fos_our_client
;
2451 case fos_myid_ip_key
: /* KEY for our default IP address as %myid */
2452 ugh
= check_key_recs(MYID_IP
, c
, ac
);
2455 /* cannot use our IP as OE identitiy for initiation */
2456 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:KEY) as identity: %s"
2459 if (!logged_myid_ip_key_warning
)
2461 loglog(RC_LOG_SERIOUS
2462 , "can not use our IP (%s:KEY) as identity: %s"
2465 logged_myid_ip_key_warning
= TRUE
;
2468 next_step
= fos_myid_hostname_key
;
2469 ugh
= NULL
; /* failure can be recovered from */
2473 /* we can use our IP as OE identity for initiation */
2474 if (!logged_myid_ip_key_warning
)
2476 loglog(RC_LOG_SERIOUS
2477 , "using our IP (%s:KEY) as identity!"
2478 , myid_str
[MYID_IP
]);
2479 logged_myid_ip_key_warning
= TRUE
;
2481 next_step
= fos_our_client
;
2485 case fos_myid_hostname_key
: /* KEY for our hostname as %myid */
2486 ugh
= check_key_recs(MYID_HOSTNAME
, c
, ac
);
2489 /* cannot use our IP as OE identitiy for initiation */
2490 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:KEY) as identity: %s"
2491 , myid_str
[MYID_HOSTNAME
]
2493 if (!logged_myid_fqdn_key_warning
)
2495 loglog(RC_LOG_SERIOUS
2496 , "can not use our hostname (%s:KEY) as identity: %s"
2497 , myid_str
[MYID_HOSTNAME
]
2499 logged_myid_fqdn_key_warning
= TRUE
;
2502 next_step
= fos_myid_hostname_key
;
2503 ugh
= NULL
; /* failure can be recovered from */
2507 /* we can use our IP as OE identity for initiation */
2508 if (!logged_myid_fqdn_key_warning
)
2510 loglog(RC_LOG_SERIOUS
2511 , "using our hostname (%s:KEY) as identity!"
2512 , myid_str
[MYID_HOSTNAME
]);
2513 logged_myid_fqdn_key_warning
= TRUE
;
2515 next_step
= fos_our_client
;
2520 case fos_our_client
: /* TXT for our client */
2522 /* Our client is not us: we must check the TXT records.
2523 * Note: if c is different this time, there is
2524 * a chance that we did the wrong query.
2525 * If so, treat as a kind of failure.
2527 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2529 next_step
= fos_his_client
; /* normal situation */
2531 passert(sr
!= NULL
);
2533 if (our_RSA_pri
== NULL
)
2535 ugh
= "we don't know our own RSA key";
2537 else if (sameaddr(&sr
->this.host_addr
, &b
->our_client
))
2539 /* this wasn't true when we started -- bail */
2540 ugh
= "our IP address changed underfoot";
2542 else if (!same_id(&ac
->sgw_id
, &sr
->this.id
))
2544 /* this wasn't true when we started -- bail */
2545 ugh
= "our ID changed underfoot";
2549 /* Similar to code in quick_inI1_outR1_tail
2550 * for checking the other side.
2552 struct gw_info
*gwp
;
2554 ugh
= "no TXT RR for our client delegates us";
2555 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2557 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2559 ugh
= "TXT RR for our client has wrong key";
2560 /* If there is a key from the TXT record,
2561 * we count it as a win if we match the key.
2562 * If there was no key, we have a tentative win:
2563 * we need to check our KEY record to be sure.
2565 if (!gwp
->gw_key_present
)
2567 /* Success, but the TXT had no key
2568 * so we must check our our own KEY records.
2570 next_step
= fos_our_txt
;
2571 ugh
= NULL
; /* good! */
2574 if (same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2576 ugh
= NULL
; /* good! */
2584 case fos_our_txt
: /* TXT for us */
2586 /* Check if TXT lookup yielded good results.
2587 * Looking up based on our ID. Used if
2588 * client is ourself, or if TXT had no public key.
2589 * Note: if c is different this time, there is
2590 * a chance that we did the wrong query.
2591 * If so, treat as a kind of failure.
2593 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2595 next_step
= fos_his_client
; /* unless we decide to look for KEY RR */
2597 if (our_RSA_pri
== NULL
)
2599 ugh
= "we don't know our own RSA key";
2601 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2603 ugh
= "our ID changed underfoot";
2607 /* Similar to code in RSA_check_signature
2608 * for checking the other side.
2610 struct gw_info
*gwp
;
2612 ugh
= "no TXT RR for us";
2613 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2615 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2617 ugh
= "TXT RR for us has wrong key";
2618 if (gwp
->gw_key_present
2619 && same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2622 DBG_log("initiate on demand found TXT with right public key at: %s"
2623 , mycredentialstr
));
2631 /* if no TXT with right key, try KEY */
2633 DBG_log("will try for KEY RR since initiate on demand found %s: %s"
2634 , ugh
, mycredentialstr
));
2635 next_step
= fos_our_key
;
2644 case fos_our_key
: /* KEY for us */
2646 /* Check if KEY lookup yielded good results.
2647 * Looking up based on our ID. Used if
2648 * client is ourself, or if TXT had no public key.
2649 * Note: if c is different this time, there is
2650 * a chance that we did the wrong query.
2651 * If so, treat as a kind of failure.
2653 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2655 next_step
= fos_his_client
; /* always */
2657 if (our_RSA_pri
== NULL
)
2659 ugh
= "we don't know our own RSA key";
2661 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2663 ugh
= "our ID changed underfoot";
2667 /* Similar to code in RSA_check_signature
2668 * for checking the other side.
2672 ugh
= "no KEY RR found for us (and no good TXT RR)";
2673 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2675 ugh
= "all our KEY RRs have the wrong public key (and no good TXT RR)";
2676 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2677 && same_RSA_public_key(&our_RSA_pri
->pub
, &kr
->key
->u
.rsa
))
2679 /* do this only once a day */
2680 if (!logged_txt_warning
)
2682 loglog(RC_LOG_SERIOUS
2683 , "found KEY RR but not TXT RR for %s. See http://www.freeswan.org/err/txt-change.html."
2685 logged_txt_warning
= TRUE
;
2687 ugh
= NULL
; /* good! */
2694 #endif /* USE_KEYRR */
2696 case fos_his_client
: /* TXT for his client */
2698 /* We've finished last DNS queries: TXT for his client.
2699 * Using the information, try to instantiate a connection
2700 * and start negotiating.
2701 * We now know the peer. The chosing of "c" ignored this,
2702 * so we will disregard its current value.
2703 * !!! We need to randomize the entry in gw that we choose.
2705 next_step
= fos_done
; /* no more queries */
2707 c
= build_outgoing_opportunistic_connection(ac
->gateways_from_dns
2713 /* We cannot seem to instantiate a suitable connection:
2716 char ocb
[ADDRTOT_BUF
]
2720 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2721 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2722 passert(id_is_ipaddr(&ac
->gateways_from_dns
->gw_id
));
2723 addrtot(&ac
->gateways_from_dns
->gw_id
.ip_addr
, 0, pb
, sizeof(pb
));
2724 loglog(RC_OPPOFAILURE
2725 , "no suitable connection for opportunism"
2726 " between %s and %s with %s as peer"
2732 /* Replace HOLD with PASS.
2733 * The type of replacement *ought* to be
2734 * specified by policy.
2736 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2738 , SPI_PASS
/* fail into PASS */
2739 , TRUE
, b
->transport_proto
2740 , "no suitable connection");
2746 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2747 passert(c
->kind
== CK_INSTANCE
);
2748 passert(c
->gw_info
!= NULL
);
2749 passert(HAS_IPSEC_POLICY(c
->policy
));
2750 passert(LHAS(LELEM(RT_UNROUTED
) | LELEM(RT_ROUTED_PROSPECTIVE
), c
->spd
.routing
));
2754 /* what should we do on failure? */
2755 (void) assign_hold(c
, &c
->spd
2756 , b
->transport_proto
2757 , &b
->our_client
, &b
->peer_client
);
2760 c
->gw_info
->key
->last_tried_time
= now();
2761 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2762 b
->whackfd
= NULL_FD
; /* protect from close */
2771 /* the second chunk: initiate the next DNS query (if any) */
2774 char ours
[ADDRTOT_BUF
];
2775 char his
[ADDRTOT_BUF
];
2777 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2778 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2779 DBG_log("initiate on demand from %s to %s new state: %s with ugh: %s"
2780 , ours
, his
, oppo_step_name
[b
->step
], ugh ? ugh
: "ok");
2785 b
->policy_prio
= c
->prio
;
2786 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2787 cannot_oppo(c
, b
, ugh
);
2789 else if (next_step
== fos_done
)
2795 /* set up the next query */
2796 struct find_oppo_continuation
*cr
= alloc_thing(struct find_oppo_continuation
2797 , "opportunistic continuation");
2800 b
->policy_prio
= c
->prio
;
2801 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2802 cr
->b
= *b
; /* copy; start hand off of whackfd */
2803 cr
->b
.failure_ok
= FALSE
;
2804 cr
->b
.step
= next_step
;
2807 ; sr
!=NULL
&& !sameaddr(&sr
->this.host_addr
, &b
->our_client
)
2814 /* If a %hold shunt has replaced the eroute for this template,
2818 && sr
->routing
== RT_ROUTED_PROSPECTIVE
&& eclipsable(sr
))
2820 sr
->routing
= RT_ROUTED_ECLIPSED
;
2824 /* Switch to issue next query.
2825 * A case may turn out to be unnecessary. If so, it falls
2826 * through to the next case.
2827 * Figuring out what %myid can stand for must be done before
2828 * our client credentials are looked up: we must know what
2829 * the client credentials may use to identify us.
2830 * On the other hand, our own credentials should be looked
2831 * up after our clients in case our credentials are not
2833 * XXX this is a wasted effort if we don't have credentials
2834 * BUT they are not needed.
2838 case fos_myid_ip_txt
:
2839 if (c
->spd
.this.id
.kind
== ID_MYID
2840 && myid_state
!= MYID_SPECIFIED
)
2842 cr
->b
.failure_ok
= TRUE
;
2843 cr
->b
.want
= b
->want
= "TXT record for IP address as %myid";
2844 ugh
= start_adns_query(&myids
[MYID_IP
]
2851 cr
->b
.step
= fos_myid_hostname_txt
;
2854 case fos_myid_hostname_txt
:
2855 if (c
->spd
.this.id
.kind
== ID_MYID
2856 && myid_state
!= MYID_SPECIFIED
)
2859 cr
->b
.failure_ok
= TRUE
;
2861 cr
->b
.failure_ok
= FALSE
;
2863 cr
->b
.want
= b
->want
= "TXT record for hostname as %myid";
2864 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2865 , &myids
[MYID_HOSTNAME
]
2873 cr
->b
.step
= fos_myid_ip_key
;
2876 case fos_myid_ip_key
:
2877 if (c
->spd
.this.id
.kind
== ID_MYID
2878 && myid_state
!= MYID_SPECIFIED
)
2880 cr
->b
.failure_ok
= TRUE
;
2881 cr
->b
.want
= b
->want
= "KEY record for IP address as %myid (no good TXT)";
2882 ugh
= start_adns_query(&myids
[MYID_IP
]
2883 , (const struct id
*) NULL
/* security gateway meaningless */
2889 cr
->b
.step
= fos_myid_hostname_key
;
2892 case fos_myid_hostname_key
:
2893 if (c
->spd
.this.id
.kind
== ID_MYID
2894 && myid_state
!= MYID_SPECIFIED
)
2896 cr
->b
.failure_ok
= FALSE
; /* last attempt! */
2897 cr
->b
.want
= b
->want
= "KEY record for hostname as %myid (no good TXT)";
2898 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2899 , (const struct id
*) NULL
/* security gateway meaningless */
2906 cr
->b
.step
= fos_our_client
;
2909 case fos_our_client
: /* TXT for our client */
2910 if (!sameaddr(&c
->spd
.this.host_addr
, &b
->our_client
))
2912 /* Check that at least one TXT(reverse(b->our_client)) is workable.
2913 * Note: {unshare|free}_id_content not needed for id: ephemeral.
2915 cr
->b
.want
= b
->want
= "our client's TXT record";
2916 iptoid(&b
->our_client
, &id
);
2917 ugh
= start_adns_query(&id
2918 , &c
->spd
.this.id
/* we are the security gateway */
2924 cr
->b
.step
= fos_our_txt
;
2927 case fos_our_txt
: /* TXT for us */
2928 cr
->b
.failure_ok
= b
->failure_ok
= TRUE
;
2929 cr
->b
.want
= b
->want
= "our TXT record";
2930 ugh
= start_adns_query(&sr
->this.id
2931 , &sr
->this.id
/* we are the security gateway XXX - maybe ignore? mcr */
2938 case fos_our_key
: /* KEY for us */
2939 cr
->b
.want
= b
->want
= "our KEY record";
2940 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2941 ugh
= start_adns_query(&sr
->this.id
2942 , (const struct id
*) NULL
/* security gateway meaningless */
2947 #endif /* USE_KEYRR */
2949 case fos_his_client
: /* TXT for his client */
2950 /* note: {unshare|free}_id_content not needed for id: ephemeral */
2951 cr
->b
.want
= b
->want
= "target's TXT record";
2952 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2953 iptoid(&b
->peer_client
, &id
);
2954 ugh
= start_adns_query(&id
2955 , (const struct id
*) NULL
/* security gateway unconstrained */
2962 bad_case(next_step
);
2966 b
->whackfd
= NULL_FD
; /* complete hand-off */
2968 cannot_oppo(c
, b
, ugh
);
2971 close_any(b
->whackfd
);
2975 terminate_connection(const char *nm
)
2977 /* Loop because more than one may match (master and instances)
2978 * But at least one is required (enforced by con_by_name).
2980 struct connection
*c
= con_by_name(nm
, TRUE
);
2982 if (c
== NULL
|| !c
->ikev1
)
2987 struct connection
*n
= c
->ac_next
; /* grab this before c might disappear */
2989 if (streq(c
->name
, nm
)
2990 && c
->kind
>= CK_PERMANENT
2991 && !NEVER_NEGOTIATE(c
->policy
))
2993 set_cur_connection(c
);
2994 plog("terminating SAs using this connection");
2995 c
->policy
&= ~POLICY_UP
;
2996 flush_pending_by_connection(c
);
2997 delete_states_by_connection(c
, FALSE
);
2998 reset_cur_connection();
3001 } while (c
!= NULL
);
3004 /* an ISAKMP SA has been established.
3005 * Note the serial number, and release any connections with
3006 * the same peer ID but different peer IP address.
3008 bool uniqueIDs
= FALSE
; /* --uniqueids? */
3011 ISAKMP_SA_established(struct connection
*c
, so_serial_t serial
)
3013 c
->newest_isakmp_sa
= serial
;
3015 /* the connection is now oriented so that we are able to determine
3016 * whether we are a mode config server with a virtual IP to send.
3018 if (!isanyaddr(&c
->spd
.that
.host_srcip
) && !c
->spd
.that
.has_natip
)
3019 c
->spd
.that
.modecfg
= TRUE
;
3023 /* for all connections: if the same Phase 1 IDs are used
3024 * for a different IP address, unorient that connection.
3026 struct connection
*d
;
3028 for (d
= connections
; d
!= NULL
; )
3030 struct connection
*next
= d
->ac_next
; /* might move underneath us */
3032 if (d
->kind
>= CK_PERMANENT
3033 && same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
3034 && same_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
)
3035 && !sameaddr(&c
->spd
.that
.host_addr
, &d
->spd
.that
.host_addr
))
3037 release_connection(d
, FALSE
);
3044 /* Find the connection to connection c's peer's client with the
3045 * largest value of .routing. All other things being equal,
3046 * preference is given to c. If none is routed, return NULL.
3048 * If erop is non-null, set *erop to a connection sharing both
3049 * our client subnet and peer's client subnet with the largest value
3050 * of .routing. If none is erouted, set *erop to NULL.
3052 * The return value is used to find other connections sharing a route.
3053 * *erop is used to find other connections sharing an eroute.
3056 route_owner(struct connection
*c
3057 , struct spd_route
**srp
3058 , struct connection
**erop
3059 , struct spd_route
**esrp
)
3061 struct connection
*d
3064 struct spd_route
*srd
, *src
;
3065 struct spd_route
*best_sr
, *best_esr
;
3066 enum routing_t best_routing
, best_erouting
;
3068 passert(oriented(*c
));
3071 best_routing
= c
->spd
.routing
;
3072 best_erouting
= best_routing
;
3074 for (d
= connections
; d
!= NULL
; d
= d
->ac_next
)
3076 for (srd
= &d
->spd
; srd
; srd
= srd
->next
)
3078 if (srd
->routing
== RT_UNROUTED
)
3081 for (src
= &c
->spd
; src
; src
=src
->next
)
3083 if (!samesubnet(&src
->that
.client
, &srd
->that
.client
))
3085 if (src
->that
.protocol
!= srd
->that
.protocol
)
3087 if (src
->that
.port
!= srd
->that
.port
)
3089 passert(oriented(*d
));
3090 if (srd
->routing
> best_routing
)
3094 best_routing
= srd
->routing
;
3097 if (!samesubnet(&src
->this.client
, &srd
->this.client
))
3099 if (src
->this.protocol
!= srd
->this.protocol
)
3101 if (src
->this.port
!= srd
->this.port
)
3103 if (srd
->routing
> best_erouting
)
3107 best_erouting
= srd
->routing
;
3115 char cib
[CONN_INST_BUF
];
3116 err_t m
= builddiag("route owner of \"%s\"%s %s:"
3118 , (fmt_conn_instance(c
, cib
), cib
)
3119 , enum_name(&routing_story
, c
->spd
.routing
));
3121 if (!routed(best_ro
->spd
.routing
))
3122 m
= builddiag("%s NULL", m
);
3123 else if (best_ro
== c
)
3124 m
= builddiag("%s self", m
);
3126 m
= builddiag("%s \"%s\"%s %s", m
3128 , (fmt_conn_instance(best_ro
, cib
), cib
)
3129 , enum_name(&routing_story
, best_ro
->spd
.routing
));
3133 m
= builddiag("%s; eroute owner:", m
);
3134 if (!erouted(best_ero
->spd
.routing
))
3135 m
= builddiag("%s NULL", m
);
3136 else if (best_ero
== c
)
3137 m
= builddiag("%s self", m
);
3139 m
= builddiag("%s \"%s\"%s %s", m
3141 , (fmt_conn_instance(best_ero
, cib
), cib
)
3142 , enum_name(&routing_story
, best_ero
->spd
.routing
));
3149 *erop
= erouted(best_erouting
)? best_ero
: NULL
;
3158 return routed(best_routing
)? best_ro
: NULL
;
3161 /* Find a connection that owns the shunt eroute between subnets.
3162 * There ought to be only one.
3163 * This might get to be a bottleneck -- try hashing if it does.
3166 shunt_owner(const ip_subnet
*ours
, const ip_subnet
*his
)
3168 struct connection
*c
;
3169 struct spd_route
*sr
;
3171 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
3173 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
3175 if (shunt_erouted(sr
->routing
)
3176 && samesubnet(ours
, &sr
->this.client
)
3177 && samesubnet(his
, &sr
->that
.client
))
3184 /* Find some connection with this pair of hosts.
3185 * We don't know enough to chose amongst those available.
3186 * ??? no longer usefully different from find_host_pair_connections
3189 find_host_connection(const ip_address
*me
, u_int16_t my_port
3190 , const ip_address
*him
, u_int16_t his_port
, lset_t policy
)
3192 struct connection
*c
= find_host_pair_connections(me
, my_port
, him
, his_port
);
3194 if (policy
!= LEMPTY
)
3196 lset_t auth_requested
= policy
& POLICY_ID_AUTH_MASK
;
3198 /* if we have requirements for the policy,
3199 * choose the first matching connection.
3203 if (c
->policy
& auth_requested
)
3213 /* given an up-until-now satisfactory connection, find the best connection
3214 * now that we just got the Phase 1 Id Payload from the peer.
3216 * Comments in the code describe the (tricky!) matching criteria.
3217 * Although this routine could handle the initiator case,
3218 * it isn't currently called in this case.
3219 * If it were, it could "upgrade" an Opportunistic Connection
3220 * to a Road Warrior Connection if a suitable Peer ID were found.
3222 * In RFC 2409 "The Internet Key Exchange (IKE)",
3223 * in 5.1 "IKE Phase 1 Authenticated With Signatures", describing Main
3226 * Initiator Responder
3227 * ----------- -----------
3232 * HDR*, IDii, [ CERT, ] SIG_I -->
3233 * <-- HDR*, IDir, [ CERT, ] SIG_R
3235 * In 5.4 "Phase 1 Authenticated With a Pre-Shared Key":
3241 * HDR*, IDii, HASH_I -->
3242 * <-- HDR*, IDir, HASH_R
3244 * refine_host_connection could be called in two case:
3246 * - the Responder receives the IDii payload:
3247 * + [PSK] after using PSK to decode this message
3248 * + before sending its IDir payload
3249 * + before using its ID in HASH_R computation
3250 * + [DSig] before using its private key to sign SIG_R
3251 * + before using the Initiator's ID in HASH_I calculation
3252 * + [DSig] before using the Initiator's public key to check SIG_I
3254 * - the Initiator receives the IDir payload:
3255 * + [PSK] after using PSK to encode previous message and decode this message
3256 * + after sending its IDii payload
3257 * + after using its ID in HASH_I computation
3258 * + [DSig] after using its private key to sign SIG_I
3259 * + before using the Responder's ID to compute HASH_R
3260 * + [DSig] before using Responder's public key to check SIG_R
3262 * refine_host_connection can choose a different connection, as long as
3263 * nothing already used is changed.
3265 * In the Initiator case, the particular connection might have been
3266 * specified by whatever provoked Pluto to initiate. For example:
3267 * whack --initiate connection-name
3268 * The advantages of switching connections when we're the Initiator seem
3269 * less important than the disadvantages, so after FreeS/WAN 1.9, we
3272 #define PRIO_NO_MATCH_FOUND 2048
3275 refine_host_connection(const struct state
*st
, const struct id
*peer_id
3278 struct connection
*c
= st
->st_connection
;
3279 struct connection
*d
;
3280 struct connection
*best_found
= NULL
;
3281 u_int16_t auth
= st
->st_oakley
.auth
;
3283 const chunk_t
*psk
= NULL
;
3284 bool wcpip
; /* wildcard Peer IP? */
3285 int best_prio
= PRIO_NO_MATCH_FOUND
;
3286 int wildcards
, our_pathlen
, peer_pathlen
;
3288 if (same_id(&c
->spd
.that
.id
, peer_id
)
3289 && trusted_ca(peer_ca
, c
->spd
.that
.ca
, &peer_pathlen
)
3290 && peer_pathlen
== 0
3291 && match_requested_ca(c
->requested_ca
, c
->spd
.this.ca
, &our_pathlen
)
3292 && our_pathlen
== 0)
3295 DBG_log("current connection is a full match"
3296 " -- no need to look further");
3303 case OAKLEY_PRESHARED_KEY
:
3304 auth_policy
= POLICY_PSK
;
3305 psk
= get_preshared_secret(c
);
3306 /* It should be virtually impossible to fail to find PSK:
3307 * we just used it to decode the current message!
3310 return NULL
; /* cannot determine PSK! */
3312 case XAUTHInitPreShared
:
3313 case XAUTHRespPreShared
:
3314 auth_policy
= POLICY_XAUTH_PSK
;
3315 psk
= get_preshared_secret(c
);
3317 return NULL
; /* cannot determine PSK! */
3319 case OAKLEY_RSA_SIG
:
3320 auth_policy
= POLICY_RSASIG
;
3324 auth_policy
= POLICY_XAUTH_RSASIG
;
3330 /* The current connection won't do: search for one that will.
3331 * First search for one with the same pair of hosts.
3332 * If that fails, search for a suitable Road Warrior or Opportunistic
3333 * connection (i.e. wildcard peer IP).
3335 * - peer_id (slightly complicated by instantiation)
3336 * - if PSK auth, the key must not change (we used it to decode message)
3337 * - policy-as-used must be acceptable to new connection
3339 d
= c
->host_pair
->connections
;
3340 for (wcpip
= FALSE
; ; wcpip
= TRUE
)
3342 for (; d
!= NULL
; d
= d
->hp_next
)
3344 const char *match_name
[] = {"no", "ok"};
3346 bool matching_id
= match_id(peer_id
3347 , &d
->spd
.that
.id
, &wildcards
);
3348 bool matching_auth
= (d
->policy
& auth_policy
) != LEMPTY
;
3350 bool matching_trust
= trusted_ca(peer_ca
3351 , d
->spd
.that
.ca
, &peer_pathlen
);
3352 bool matching_request
= match_requested_ca(c
->requested_ca
3353 , d
->spd
.this.ca
, &our_pathlen
);
3354 bool match
= matching_id
&& matching_auth
&& matching_trust
;
3356 int prio
= (MAX_WILDCARDS
+ 1) * !matching_request
+ wildcards
;
3358 prio
= (MAX_CA_PATH_LEN
+ 1) * prio
+ peer_pathlen
;
3359 prio
= (MAX_CA_PATH_LEN
+ 1) * prio
+ our_pathlen
;
3361 DBG(DBG_CONTROLMORE
,
3362 DBG_log("%s: %s match (id: %s, auth: %s, trust: %s, request: %s, prio: %4d)"
3364 , match ?
"full":" no"
3365 , match_name
[matching_id
]
3366 , match_name
[matching_auth
]
3367 , match_name
[matching_trust
]
3368 , match_name
[matching_request
]
3369 , match ? prio
:PRIO_NO_MATCH_FOUND
)
3372 /* do we have a match? */
3376 /* ignore group connections */
3377 if (d
->policy
& POLICY_GROUP
)
3380 if (c
->spd
.that
.host_port
!= d
->spd
.that
.host_port
3381 && d
->kind
== CK_INSTANCE
)
3388 case OAKLEY_PRESHARED_KEY
:
3389 case XAUTHInitPreShared
:
3390 case XAUTHRespPreShared
:
3391 /* secret must match the one we already used */
3393 const chunk_t
*dpsk
= get_preshared_secret(d
);
3396 continue; /* no secret */
3399 if (psk
->len
!= dpsk
->len
3400 || memcmp(psk
->ptr
, dpsk
->ptr
, psk
->len
) != 0)
3401 continue; /* different secret */
3405 case OAKLEY_RSA_SIG
:
3409 * We must at least be able to find our private key
3411 if (d
->spd
.this.sc
== NULL
/* no smartcard */
3412 && get_RSA_private_key(d
) == NULL
) /* no private key */
3420 /* d has passed all the tests.
3421 * We'll go with it if the Peer ID was an exact match.
3428 /* We'll remember it as best_found in case an exact
3429 * match doesn't come along.
3431 if (prio
< best_prio
)
3438 return best_found
; /* been around twice already */
3440 /* Starting second time around.
3441 * We're willing to settle for a connection that needs Peer IP
3442 * instantiated: Road Warrior or Opportunistic.
3443 * Look on list of connections for host pair with wildcard Peer IP
3445 d
= find_host_pair_connections(&c
->spd
.this.host_addr
, c
->spd
.this.host_port
3446 , (ip_address
*)NULL
, c
->spd
.that
.host_port
);
3451 * With virtual addressing, we must not allow someone to use an already
3452 * used (by another id) addr/net.
3455 is_virtual_net_used(const ip_subnet
*peer_net
, const struct id
*peer_id
)
3457 struct connection
*d
;
3459 for (d
= connections
; d
!= NULL
; d
= d
->ac_next
)
3465 if ((subnetinsubnet(peer_net
,&d
->spd
.that
.client
) ||
3466 subnetinsubnet(&d
->spd
.that
.client
,peer_net
))
3467 && !same_id(&d
->spd
.that
.id
, peer_id
))
3470 char client
[SUBNETTOT_BUF
];
3472 subnettot(peer_net
, 0, client
, sizeof(client
));
3473 idtoa(&d
->spd
.that
.id
, buf
, sizeof(buf
));
3474 plog("Virtual IP %s is already used by '%s'", client
, buf
);
3475 idtoa(peer_id
, buf
, sizeof(buf
));
3476 plog("Your ID is '%s'", buf
);
3477 return TRUE
; /* already used by another one */
3485 return FALSE
; /* you can safely use it */
3488 /* find_client_connection: given a connection suitable for ISAKMP
3489 * (i.e. the hosts match), find a one suitable for IPSEC
3490 * (i.e. with matching clients).
3492 * If we don't find an exact match (not even our current connection),
3493 * we try for one that still needs instantiation. Try Road Warrior
3494 * abstract connections and the Opportunistic abstract connections.
3495 * This requires inverse instantiation: abstraction.
3497 * After failing to find an exact match, we abstract the peer
3498 * to be NO_IP (the wildcard value). This enables matches with
3499 * Road Warrior and Opportunistic abstract connections.
3501 * After failing that search, we also abstract the Phase 1 peer ID
3502 * if possible. If the peer's ID was the peer's IP address, we make
3503 * it NO_ID; instantiation will make it the peer's IP address again.
3505 * If searching for a Road Warrior abstract connection fails,
3506 * and conditions are suitable, we search for the best Opportunistic
3507 * abstract connection.
3509 * Note: in the end, both Phase 1 IDs must be preserved, after any
3510 * instantiation. They are the IDs that have been authenticated.
3513 #define PATH_WEIGHT 1
3514 #define WILD_WEIGHT (MAX_CA_PATH_LEN+1)
3515 #define PRIO_WEIGHT (MAX_WILDCARDS+1)*WILD_WEIGHT
3517 /* fc_try: a helper function for find_client_connection */
3518 static struct connection
*
3519 fc_try(const struct connection
*c
3520 , struct host_pair
*hp
3521 , const struct id
*peer_id
3522 , const ip_subnet
*our_net
3523 , const ip_subnet
*peer_net
3524 , const u_int8_t our_protocol
3525 , const u_int16_t our_port
3526 , const u_int8_t peer_protocol
3527 , const u_int16_t peer_port
3529 , const ietfAttrList_t
*peer_list
)
3531 struct connection
*d
;
3532 struct connection
*best
= NULL
;
3533 policy_prio_t best_prio
= BOTTOM_PRIO
;
3534 int wildcards
, pathlen
;
3536 const bool peer_net_is_host
= subnetisaddr(peer_net
, &c
->spd
.that
.host_addr
);
3538 for (d
= hp
->connections
; d
!= NULL
; d
= d
->hp_next
)
3540 struct spd_route
*sr
;
3542 if (d
->policy
& POLICY_GROUP
)
3545 if (!(same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
3546 && match_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
, &wildcards
)
3547 && trusted_ca(peer_ca
, d
->spd
.that
.ca
, &pathlen
)
3548 && group_membership(peer_list
, d
->name
, d
->spd
.that
.groups
)))
3551 /* compare protocol and ports */
3552 if (d
->spd
.this.protocol
!= our_protocol
3553 || d
->spd
.this.port
!= our_port
3554 || d
->spd
.that
.protocol
!= peer_protocol
3555 || (d
->spd
.that
.port
!= peer_port
&& !d
->spd
.that
.has_port_wildcard
))
3558 /* non-Opportunistic case:
3559 * our_client must match.
3561 * So must peer_client, but the testing is complicated
3562 * by the fact that the peer might be a wildcard
3563 * and if so, the default value of that.client
3564 * won't match the default peer_net. The appropriate test:
3566 * If d has a peer client, it must match peer_net.
3567 * If d has no peer client, peer_net must just have peer itself.
3570 for (sr
= &d
->spd
; best
!= d
&& sr
!= NULL
; sr
= sr
->next
)
3574 if (DBGP(DBG_CONTROLMORE
))
3576 char s1
[SUBNETTOT_BUF
],d1
[SUBNETTOT_BUF
];
3577 char s3
[SUBNETTOT_BUF
],d3
[SUBNETTOT_BUF
];
3579 subnettot(our_net
, 0, s1
, sizeof(s1
));
3580 subnettot(peer_net
, 0, d1
, sizeof(d1
));
3581 subnettot(&sr
->this.client
, 0, s3
, sizeof(s3
));
3582 subnettot(&sr
->that
.client
, 0, d3
, sizeof(d3
));
3583 DBG_log(" fc_try trying "
3584 "%s:%s:%d/%d -> %s:%d/%d vs %s:%s:%d/%d -> %s:%d/%d"
3585 , c
->name
, s1
, c
->spd
.this.protocol
, c
->spd
.this.port
3586 , d1
, c
->spd
.that
.protocol
, c
->spd
.that
.port
3587 , d
->name
, s3
, sr
->this.protocol
, sr
->this.port
3588 , d3
, sr
->that
.protocol
, sr
->that
.port
);
3592 if (!samesubnet(&sr
->this.client
, our_net
))
3595 if (sr
->that
.has_client
)
3597 if (sr
->that
.has_client_wildcard
)
3599 if (!subnetinsubnet(peer_net
, &sr
->that
.client
))
3604 if (!samesubnet(&sr
->that
.client
, peer_net
) && !is_virtual_connection(d
))
3606 if (is_virtual_connection(d
)
3607 && (!is_virtual_net_allowed(d
, peer_net
, &c
->spd
.that
.host_addr
)
3608 || is_virtual_net_used(peer_net
, peer_id?peer_id
:&c
->spd
.that
.id
)))
3614 if (!peer_net_is_host
)
3618 /* We've run the gauntlet -- success:
3619 * We've got an exact match of subnets.
3620 * The connection is feasible, but we continue looking for the best.
3621 * The highest priority wins, implementing eroute-like rule.
3622 * - a routed connection is preferrred
3623 * - given that, the smallest number of ID wildcards are preferred
3624 * - given that, the shortest CA pathlength is preferred
3626 prio
= PRIO_WEIGHT
* routed(sr
->routing
)
3627 + WILD_WEIGHT
* (MAX_WILDCARDS
- wildcards
)
3628 + PATH_WEIGHT
* (MAX_CA_PATH_LEN
- pathlen
)
3630 if (prio
> best_prio
)
3638 if (best
!= NULL
&& NEVER_NEGOTIATE(best
->policy
))
3641 DBG(DBG_CONTROLMORE
,
3642 DBG_log(" fc_try concluding with %s [%ld]"
3643 , (best ? best
->name
: "none"), best_prio
)
3648 static struct connection
*
3649 fc_try_oppo(const struct connection
*c
3650 , struct host_pair
*hp
3651 , const ip_subnet
*our_net
3652 , const ip_subnet
*peer_net
3653 , const u_int8_t our_protocol
3654 , const u_int16_t our_port
3655 , const u_int8_t peer_protocol
3656 , const u_int16_t peer_port
3658 , const ietfAttrList_t
*peer_list
)
3660 struct connection
*d
;
3661 struct connection
*best
= NULL
;
3662 policy_prio_t best_prio
= BOTTOM_PRIO
;
3663 int wildcards
, pathlen
;
3665 for (d
= hp
->connections
; d
!= NULL
; d
= d
->hp_next
)
3667 struct spd_route
*sr
;
3670 if (d
->policy
& POLICY_GROUP
)
3673 if (!(same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
3674 && match_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
, &wildcards
)
3675 && trusted_ca(peer_ca
, d
->spd
.that
.ca
, &pathlen
)
3676 && group_membership(peer_list
, d
->name
, d
->spd
.that
.groups
)))
3679 /* compare protocol and ports */
3680 if (d
->spd
.this.protocol
!= our_protocol
3681 || d
->spd
.this.port
!= our_port
3682 || d
->spd
.that
.protocol
!= peer_protocol
3683 || (d
->spd
.that
.port
!= peer_port
&& !d
->spd
.that
.has_port_wildcard
))
3686 /* Opportunistic case:
3687 * our_net must be inside d->spd.this.client
3688 * and peer_net must be inside d->spd.that.client
3689 * Note: this host_pair chain also has shunt
3690 * eroute conns (clear, drop), but they won't
3691 * be marked as opportunistic.
3693 for (sr
= &d
->spd
; sr
!= NULL
; sr
= sr
->next
)
3696 if (DBGP(DBG_CONTROLMORE
))
3698 char s1
[SUBNETTOT_BUF
],d1
[SUBNETTOT_BUF
];
3699 char s3
[SUBNETTOT_BUF
],d3
[SUBNETTOT_BUF
];
3701 subnettot(our_net
, 0, s1
, sizeof(s1
));
3702 subnettot(peer_net
, 0, d1
, sizeof(d1
));
3703 subnettot(&sr
->this.client
, 0, s3
, sizeof(s3
));
3704 subnettot(&sr
->that
.client
, 0, d3
, sizeof(d3
));
3705 DBG_log(" fc_try_oppo trying %s:%s -> %s vs %s:%s -> %s"
3706 , c
->name
, s1
, d1
, d
->name
, s3
, d3
);