1 /* information about connections between hosts and clients
2 * Copyright (C) 1998-2002 D. Hugh Redelmeier.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * RCSID $Id: connections.c,v 1.43 2006/04/29 18:16:02 as Exp $
22 #include <netinet/in.h>
23 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
29 #include <sys/queue.h>
32 #include <ipsec_policy.h>
33 #include "kameipsec.h"
35 #include "constants.h"
44 #include "smartcard.h"
46 #include "connections.h"
47 #include "foodgroups.h"
51 #include "ipsec_doi.h" /* needs demux.h and state.h */
56 #include "adns.h" /* needs <resolv.h> */
57 #include "dnskey.h" /* needs keys.h and adns.h */
61 #include "nat_traversal.h"
64 static void flush_pending_by_connection(struct connection
*c
); /* forward */
66 static struct connection
*connections
= NULL
;
68 /* struct host_pair: a nexus of information about a pair of hosts.
69 * A host is an IP address, UDP port pair. This is a debatable choice:
70 * - should port be considered (no choice of port in standard)?
71 * - should ID be considered (hard because not always known)?
72 * - should IP address matter on our end (we don't know our end)?
73 * Only oriented connections are registered.
74 * Unoriented connections are kept on the unoriented_connections
75 * linked list (using hp_next). For them, host_pair is NULL.
81 u_int16_t port
; /* host order */
83 bool initial_connection_sent
;
84 struct connection
*connections
; /* connections with this pair */
85 struct pending
*pending
; /* awaiting Keying Channel */
86 struct host_pair
*next
;
89 static struct host_pair
*host_pairs
= NULL
;
91 static struct connection
*unoriented_connections
= NULL
;
93 /* check to see that Ids of peers match */
95 same_peer_ids(const struct connection
*c
, const struct connection
*d
96 , const struct id
*his_id
)
98 return same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
99 && same_id(his_id
== NULL?
&c
->spd
.that
.id
: his_id
, &d
->spd
.that
.id
);
102 static struct host_pair
*
103 find_host_pair(const ip_address
*myaddr
, u_int16_t myport
104 , const ip_address
*hisaddr
, u_int16_t hisport
)
106 struct host_pair
*p
, *prev
;
108 /* default hisaddr to an appropriate any */
110 hisaddr
= aftoinfo(addrtypeof(myaddr
))->any
;
112 if (nat_traversal_enabled
)
115 * port is not relevant in host_pair. with nat_traversal we
116 * always use pluto_port (500)
119 hisport
= pluto_port
;
122 for (prev
= NULL
, p
= host_pairs
; p
!= NULL
; prev
= p
, p
= p
->next
)
124 if (sameaddr(&p
->me
.addr
, myaddr
) && p
->me
.port
== myport
125 && sameaddr(&p
->him
.addr
, hisaddr
) && p
->him
.port
== hisport
)
129 prev
->next
= p
->next
; /* remove p from list */
130 p
->next
= host_pairs
; /* and stick it on front */
139 /* find head of list of connections with this pair of hosts */
140 static struct connection
*
141 find_host_pair_connections(const ip_address
*myaddr
, u_int16_t myport
142 , const ip_address
*hisaddr
, u_int16_t hisport
)
144 struct host_pair
*hp
= find_host_pair(myaddr
, myport
, hisaddr
, hisport
);
146 if (nat_traversal_enabled
&& hp
&& hisaddr
)
148 struct connection
*c
;
150 for (c
= hp
->connections
; c
!= NULL
; c
= c
->hp_next
)
152 if (c
->spd
.this.host_port
== myport
&& c
->spd
.that
.host_port
== hisport
)
157 return hp
== NULL? NULL
: hp
->connections
;
161 connect_to_host_pair(struct connection
*c
)
165 struct host_pair
*hp
= find_host_pair(&c
->spd
.this.host_addr
, c
->spd
.this.host_port
166 , &c
->spd
.that
.host_addr
, c
->spd
.that
.host_port
);
170 /* no suitable host_pair -- build one */
171 hp
= alloc_thing(struct host_pair
, "host_pair");
172 hp
->me
.addr
= c
->spd
.this.host_addr
;
173 hp
->him
.addr
= c
->spd
.that
.host_addr
;
174 hp
->me
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.this.host_port
;
175 hp
->him
.port
= nat_traversal_enabled ? pluto_port
: c
->spd
.that
.host_port
;
176 hp
->initial_connection_sent
= FALSE
;
177 hp
->connections
= NULL
;
179 hp
->next
= host_pairs
;
183 c
->hp_next
= hp
->connections
;
188 /* since this connection isn't oriented, we place it
189 * in the unoriented_connections list instead.
192 c
->hp_next
= unoriented_connections
;
193 unoriented_connections
= c
;
197 /* find a connection by name.
198 * If strict, don't accept a CK_INSTANCE.
199 * Move the winner (if any) to the front.
200 * If none is found, and strict, a diagnostic is logged to whack.
203 con_by_name(const char *nm
, bool strict
)
205 struct connection
*p
, *prev
;
207 for (prev
= NULL
, p
= connections
; ; prev
= p
, p
= p
->ac_next
)
212 whack_log(RC_UNKNOWN_NAME
213 , "no connection named \"%s\"", nm
);
216 if (streq(p
->name
, nm
)
217 && (!strict
|| p
->kind
!= CK_INSTANCE
))
221 prev
->ac_next
= p
->ac_next
; /* remove p from list */
222 p
->ac_next
= connections
; /* and stick it on front */
232 release_connection(struct connection
*c
, bool relations
)
234 if (c
->kind
== CK_INSTANCE
)
236 /* This does everything we need.
237 * Note that we will be called recursively by delete_connection,
238 * but kind will be CK_GOING_AWAY.
240 delete_connection(c
, relations
);
244 flush_pending_by_connection(c
);
245 delete_states_by_connection(c
, relations
);
246 unroute_connection(c
);
250 /* Delete a connection */
252 #define list_rm(etype, enext, e, ehead) { \
254 for (ep = &(ehead); *ep != (e); ep = &(*ep)->enext) \
255 passert(*ep != NULL); /* we must not come up empty-handed */ \
261 delete_connection(struct connection
*c
, bool relations
)
263 struct connection
*old_cur_connection
264 = cur_connection
== c? NULL
: cur_connection
;
266 lset_t old_cur_debugging
= cur_debugging
;
269 set_cur_connection(c
);
271 /* Must be careful to avoid circularity:
272 * we mark c as going away so it won't get deleted recursively.
274 passert(c
->kind
!= CK_GOING_AWAY
);
275 if (c
->kind
== CK_INSTANCE
)
277 plog("deleting connection \"%s\" instance with peer %s {isakmp=#%lu/ipsec=#%lu}"
279 , ip_str(&c
->spd
.that
.host_addr
)
280 , c
->newest_isakmp_sa
, c
->newest_ipsec_sa
);
281 c
->kind
= CK_GOING_AWAY
;
285 plog("deleting connection");
287 release_connection(c
, relations
); /* won't delete c */
289 if (c
->kind
== CK_GROUP
)
292 /* free up any logging resources */
295 /* find and delete c from connections list */
296 list_rm(struct connection
, ac_next
, c
, connections
);
297 cur_connection
= old_cur_connection
;
299 /* find and delete c from the host pair list */
300 if (c
->host_pair
== NULL
)
303 list_rm(struct connection
, hp_next
, c
, unoriented_connections
);
307 struct host_pair
*hp
= c
->host_pair
;
309 list_rm(struct connection
, hp_next
, c
, hp
->connections
);
310 c
->host_pair
= NULL
; /* redundant, but safe */
312 /* if there are no more connections with this host_pair
313 * and we haven't even made an initial contact, let's delete
314 * this guy in case we were created by an attempted DOS attack.
316 if (hp
->connections
== NULL
317 && !hp
->initial_connection_sent
)
319 passert(hp
->pending
== NULL
); /* ??? must deal with this! */
320 list_rm(struct host_pair
, next
, hp
, host_pairs
);
325 if (c
->kind
!= CK_GOING_AWAY
)
326 pfreeany(c
->spd
.that
.virt
);
329 cur_debugging
= old_cur_debugging
;
332 free_id_content(&c
->spd
.this.id
);
333 pfreeany(c
->spd
.this.updown
);
334 freeanychunk(c
->spd
.this.ca
);
335 free_ietfAttrList(c
->spd
.this.groups
);
336 free_id_content(&c
->spd
.that
.id
);
337 pfreeany(c
->spd
.that
.updown
);
338 freeanychunk(c
->spd
.that
.ca
);
339 free_ietfAttrList(c
->spd
.that
.groups
);
340 free_generalNames(c
->requested_ca
, TRUE
);
341 gw_delref(&c
->gw_info
);
343 lock_certs_and_keys("delete_connection");
344 release_cert(c
->spd
.this.cert
);
345 scx_release(c
->spd
.this.sc
);
346 release_cert(c
->spd
.that
.cert
);
347 scx_release(c
->spd
.that
.sc
);
348 unlock_certs_and_keys("delete_connection");
350 alg_info_delref((struct alg_info
**)&c
->alg_info_esp
);
351 alg_info_delref((struct alg_info
**)&c
->alg_info_ike
);
356 /* Delete connections with the specified name */
358 delete_connections_by_name(const char *name
, bool strict
)
360 struct connection
*c
= con_by_name(name
, strict
);
362 for (; c
!= NULL
; c
= con_by_name(name
, FALSE
))
363 delete_connection(c
, FALSE
);
367 delete_every_connection(void)
369 while (connections
!= NULL
)
370 delete_connection(connections
, TRUE
);
374 release_dead_interfaces(void)
376 struct host_pair
*hp
;
378 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
380 struct connection
**pp
383 for (pp
= &hp
->connections
; (p
= *pp
) != NULL
; )
385 if (p
->interface
->change
== IFN_DELETE
)
387 /* this connection's interface is going away */
388 enum connection_kind k
= p
->kind
;
390 release_connection(p
, TRUE
);
392 if (k
<= CK_PERMANENT
)
394 /* The connection should have survived release:
395 * move it to the unoriented_connections list.
401 *pp
= p
->hp_next
; /* advance *pp */
403 p
->hp_next
= unoriented_connections
;
404 unoriented_connections
= p
;
408 /* The connection should have vanished,
409 * but the previous connection remains.
416 pp
= &p
->hp_next
; /* advance pp */
422 /* adjust orientations of connections to reflect newly added interfaces */
424 check_orientations(void)
426 /* try to orient all the unoriented connections */
428 struct connection
*c
= unoriented_connections
;
430 unoriented_connections
= NULL
;
434 struct connection
*nxt
= c
->hp_next
;
437 connect_to_host_pair(c
);
442 /* Check that no oriented connection has become double-oriented.
443 * In other words, the far side must not match one of our new interfaces.
448 for (i
= interfaces
; i
!= NULL
; i
= i
->next
)
450 if (i
->change
== IFN_ADD
)
452 struct host_pair
*hp
;
454 for (hp
= host_pairs
; hp
!= NULL
; hp
= hp
->next
)
456 if (sameaddr(&hp
->him
.addr
, &i
->addr
)
457 && (!no_klips
|| hp
->him
.port
== pluto_port
))
459 /* bad news: the whole chain of connections
460 * hanging off this host pair has both sides
461 * matching an interface.
462 * We'll get rid of them, using orient and
463 * connect_to_host_pair. But we'll be lazy
464 * and not ditch the host_pair itself (the
465 * cost of leaving it is slight and cannot
466 * be induced by a foe).
468 struct connection
*c
= hp
->connections
;
470 hp
->connections
= NULL
;
473 struct connection
*nxt
= c
->hp_next
;
477 connect_to_host_pair(c
);
488 default_end(struct end
*e
, ip_address
*dflt_nexthop
)
491 const struct af_info
*afi
= aftoinfo(addrtypeof(&e
->host_addr
));
494 return "unknown address family in default_end";
496 /* default ID to IP (but only if not NO_IP -- WildCard) */
497 if (e
->id
.kind
== ID_NONE
&& !isanyaddr(&e
->host_addr
))
499 e
->id
.kind
= afi
->id_addr
;
500 e
->id
.ip_addr
= e
->host_addr
;
501 e
->has_id_wildcards
= FALSE
;
504 /* default nexthop to other side */
505 if (isanyaddr(&e
->host_nexthop
))
506 e
->host_nexthop
= *dflt_nexthop
;
508 /* default client to subnet containing only self
509 * XXX This may mean that the client's address family doesn't match
510 * tunnel_addr_family.
513 ugh
= addrtosubnet(&e
->host_addr
, &e
->client
);
518 /* Format the topology of a connection end, leaving out defaults.
519 * Largest left end looks like: client === host : port [ host_id ] --- hop
520 * Note: if that==NULL, skip nexthop
521 * Returns strlen of formated result (length excludes NUL at end).
526 , const struct end
*this
527 , const struct end
*that
531 char client
[SUBNETTOT_BUF
];
532 const char *client_sep
= "";
533 char protoport
[sizeof(":255/65535")];
534 const char *host
= NULL
;
535 char host_space
[ADDRTOT_BUF
];
536 char host_port
[sizeof(":65535")];
537 char host_id
[BUF_LEN
+ 2];
538 char hop
[ADDRTOT_BUF
];
539 const char *hop_sep
= "";
540 const char *open_brackets
= "";
541 const char *close_brackets
= "";
543 if (isanyaddr(&this->host_addr
))
545 switch (policy
& (POLICY_GROUP
| POLICY_OPPO
))
551 host
= "%opportunistic";
553 case POLICY_GROUP
| POLICY_OPPO
:
554 host
= "%opportunisticgroup";
564 if (is_virtual_end(this) && isanyaddr(&this->host_addr
))
570 if (this->has_client
)
572 ip_address client_net
, client_mask
;
574 networkof(&this->client
, &client_net
);
575 maskof(&this->client
, &client_mask
);
578 /* {client_subnet_wildcard} */
579 if (this->has_client_wildcard
)
582 close_brackets
= "}";
585 if (isanyaddr(&client_net
) && isanyaddr(&client_mask
)
586 && (policy
& (POLICY_GROUP
| POLICY_OPPO
)))
587 client_sep
= ""; /* boring case */
588 else if (subnetisnone(&this->client
))
591 subnettot(&this->client
, 0, client
, sizeof(client
));
593 else if (this->modecfg
&& isanyaddr(&this->host_srcip
))
595 /* we are mode config client */
597 strcpy(client
, "%modecfg");
603 addrtot(&this->host_addr
, 0, host_space
, sizeof(host_space
));
608 if (this->host_port
!= IKE_UDP_PORT
)
609 snprintf(host_port
, sizeof(host_port
), ":%u"
612 /* payload portocol and port */
614 if (this->has_port_wildcard
)
615 snprintf(protoport
, sizeof(protoport
), ":%u/%%any", this->protocol
);
616 else if (this->port
|| this->protocol
)
617 snprintf(protoport
, sizeof(protoport
), ":%u/%u", this->protocol
620 /* id, if different from host */
622 if (this->id
.kind
== ID_MYID
)
624 strcpy(host_id
, "[%myid]");
626 else if (!(this->id
.kind
== ID_NONE
627 || (id_is_ipaddr(&this->id
) && sameaddr(&this->id
.ip_addr
, &this->host_addr
))))
629 int len
= idtoa(&this->id
, host_id
+1, sizeof(host_id
)-2);
632 strcpy(&host_id
[len
< 0?
(ptrdiff_t)sizeof(host_id
)-2 : 1 + len
], "]");
638 if (that
!= NULL
&& !sameaddr(&this->host_nexthop
, &that
->host_addr
))
640 addrtot(&this->host_nexthop
, 0, hop
, sizeof(hop
));
645 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s"
646 , open_brackets
, client
, close_brackets
647 , client_sep
, host
, host_port
, host_id
648 , protoport
, hop_sep
, hop
);
650 snprintf(buf
, buf_len
, "%s%s%s%s%s%s%s%s%s%s"
651 , hop
, hop_sep
, host
, host_port
, host_id
652 , protoport
, client_sep
653 , open_brackets
, client
, close_brackets
);
657 /* format topology of a connection.
658 * Two symmetric ends separated by ...
660 #define CONNECTION_BUF (2 * (END_BUF - 1) + 4)
663 format_connection(char *buf
, size_t buf_len
664 , const struct connection
*c
665 , struct spd_route
*sr
)
667 size_t w
= format_end(buf
, buf_len
, &sr
->this, &sr
->that
, TRUE
, LEMPTY
);
669 w
+= snprintf(buf
+ w
, buf_len
- w
, "...");
670 return w
+ format_end(buf
+ w
, buf_len
- w
, &sr
->that
, &sr
->this, FALSE
, c
->policy
);
674 unshare_connection_strings(struct connection
*c
)
676 c
->name
= clone_str(c
->name
, "connection name");
678 unshare_id_content(&c
->spd
.this.id
);
679 c
->spd
.this.updown
= clone_str(c
->spd
.this.updown
, "updown");
680 scx_share(c
->spd
.this.sc
);
681 share_cert(c
->spd
.this.cert
);
682 if (c
->spd
.this.ca
.ptr
!= NULL
)
683 clonetochunk(c
->spd
.this.ca
, c
->spd
.this.ca
.ptr
, c
->spd
.this.ca
.len
, "ca string");
685 unshare_id_content(&c
->spd
.that
.id
);
686 c
->spd
.that
.updown
= clone_str(c
->spd
.that
.updown
, "updown");
687 scx_share(c
->spd
.that
.sc
);
688 share_cert(c
->spd
.that
.cert
);
689 if (c
->spd
.that
.ca
.ptr
!= NULL
)
690 clonetochunk(c
->spd
.that
.ca
, c
->spd
.that
.ca
.ptr
, c
->spd
.that
.ca
.len
, "ca string");
692 /* increment references to algo's */
693 alg_info_addref((struct alg_info
*)c
->alg_info_esp
);
694 alg_info_addref((struct alg_info
*)c
->alg_info_ike
);
698 load_end_certificate(const char *filename
, struct end
*dst
)
702 bool valid_cert
= FALSE
;
703 bool cached_cert
= FALSE
;
705 /* initialize end certificate */
706 dst
->cert
.type
= CERT_NONE
;
707 dst
->cert
.u
.x509
= NULL
;
709 /* initialize smartcard info record */
712 if (filename
!= NULL
)
714 if (scx_on_smartcard(filename
))
716 /* load cert from smartcard */
717 valid_cert
= scx_load_cert(filename
, &dst
->sc
, &cert
, &cached_cert
);
721 /* load cert from file */
722 valid_cert
= load_host_cert(filename
, &cert
);
733 select_pgpcert_id(cert
.u
.pgp
, &dst
->id
);
739 valid_until
= cert
.u
.pgp
->until
;
740 add_pgp_public_key(cert
.u
.pgp
, cert
.u
.pgp
->until
, DAL_LOCAL
);
741 dst
->cert
.type
= cert
.type
;
742 dst
->cert
.u
.pgp
= add_pgpcert(cert
.u
.pgp
);
745 case CERT_X509_SIGNATURE
:
746 select_x509cert_id(cert
.u
.x509
, &dst
->id
);
752 /* check validity of cert */
753 valid_until
= cert
.u
.x509
->notAfter
;
754 ugh
= check_validity(cert
.u
.x509
, &valid_until
);
758 free_x509cert(cert
.u
.x509
);
763 DBG_log("certificate is valid")
765 add_x509_public_key(cert
.u
.x509
, valid_until
, DAL_LOCAL
);
766 dst
->cert
.type
= cert
.type
;
767 dst
->cert
.u
.x509
= add_x509cert(cert
.u
.x509
);
769 /* if no CA is defined, use issuer as default */
770 if (dst
->ca
.ptr
== NULL
)
771 dst
->ca
= dst
->cert
.u
.x509
->issuer
;
777 /* cache the certificate that was last retrieved from the smartcard */
780 if (!same_cert(&dst
->sc
->last_cert
, &dst
->cert
))
782 lock_certs_and_keys("load_end_certificates");
783 release_cert(dst
->sc
->last_cert
);
784 dst
->sc
->last_cert
= dst
->cert
;
785 share_cert(dst
->cert
);
786 unlock_certs_and_keys("load_end_certificates");
788 time(&dst
->sc
->last_load
);
794 extract_end(struct end
*dst
, const whack_end_t
*src
, const char *which
)
796 bool same_ca
= FALSE
;
798 /* decode id, if any */
801 dst
->id
.kind
= ID_NONE
;
805 err_t ugh
= atoid(src
->id
, &dst
->id
, TRUE
);
809 loglog(RC_BADID
, "bad %s --id: %s (ignored)", which
, ugh
);
810 dst
->id
= empty_id
; /* ignore bad one */
814 dst
->ca
= empty_chunk
;
816 /* decode CA distinguished name, if any */
819 if streq(src
->ca
, "%same")
821 else if (!streq(src
->ca
, "%any"))
825 dst
->ca
.ptr
= temporary_cyclic_buffer();
826 ugh
= atodn(src
->ca
, &dst
->ca
);
829 plog("bad CA string '%s': %s (ignored)", src
->ca
, ugh
);
830 dst
->ca
= empty_chunk
;
835 /* load local end certificate and extract ID, if any */
836 load_end_certificate(src
->cert
, dst
);
838 /* does id has wildcards? */
839 dst
->has_id_wildcards
= id_count_wildcards(&dst
->id
) > 0;
841 /* decode group attributes, if any */
842 decode_groups(src
->groups
, &dst
->groups
);
844 /* the rest is simple copying of corresponding fields */
845 dst
->host_addr
= src
->host_addr
;
846 dst
->host_nexthop
= src
->host_nexthop
;
847 dst
->host_srcip
= src
->host_srcip
;
848 dst
->has_natip
= src
->has_natip
;
849 dst
->client
= src
->client
;
850 dst
->protocol
= src
->protocol
;
851 dst
->port
= src
->port
;
852 dst
->has_port_wildcard
= src
->has_port_wildcard
;
853 dst
->key_from_DNS_on_demand
= src
->key_from_DNS_on_demand
;
854 dst
->has_client
= src
->has_client
;
855 dst
->has_client_wildcard
= src
->has_client_wildcard
;
856 dst
->modecfg
= src
->modecfg
;
857 dst
->hostaccess
= src
->hostaccess
;
858 dst
->sendcert
= src
->sendcert
;
859 dst
->updown
= src
->updown
;
860 dst
->host_port
= src
->host_port
;
862 /* if host sourceip is defined but no client is present
863 * behind the host then set client to sourceip/32
865 if (addrbytesptr(&dst
->host_srcip
, NULL
)
866 && !isanyaddr(&dst
->host_srcip
)
870 err_t ugh
= addrtosubnet(&dst
->host_srcip
, &dst
->client
);
873 plog("could not assign host sourceip to client subnet");
875 dst
->has_client
= TRUE
;
881 check_connection_end(const whack_end_t
*this, const whack_end_t
*that
882 , const whack_message_t
*wm
)
884 if (wm
->addr_family
!= addrtypeof(&this->host_addr
)
885 || wm
->addr_family
!= addrtypeof(&this->host_nexthop
)
886 || (this->has_client? wm
->tunnel_addr_family
: wm
->addr_family
)
887 != subnettypeof(&this->client
)
888 || subnettypeof(&this->client
) != subnettypeof(&that
->client
))
890 /* this should have been diagnosed by whack, so we need not be clear
891 * !!! overloaded use of RC_CLASH
893 loglog(RC_CLASH
, "address family inconsistency in connection");
897 if (isanyaddr(&that
->host_addr
))
899 /* other side is wildcard: we must check if other conditions met */
900 if (isanyaddr(&this->host_addr
))
902 loglog(RC_ORIENT
, "connection must specify host IP address for our side");
907 if (this->virt
&& (!isanyaddr(&this->host_addr
) || this->has_client
))
910 "virtual IP must only be used with %%any and without client");
914 return TRUE
; /* happy */
918 find_connection_by_reqid(uint32_t reqid
)
920 struct connection
*c
;
923 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
925 if (c
->spd
.reqid
== reqid
)
936 static uint32_t reqid
= IPSEC_MANUAL_REQID_MAX
& ~3;
942 reqid
= (IPSEC_MANUAL_REQID_MAX
& ~3) + 4;
943 if (!find_connection_by_reqid(reqid
))
945 } while (reqid
!= start
);
947 exit_log("unable to allocate reqid");
951 add_connection(const whack_message_t
*wm
)
953 if (con_by_name(wm
->name
, FALSE
) != NULL
)
955 loglog(RC_DUPNAME
, "attempt to redefine connection \"%s\"", wm
->name
);
957 else if (wm
->right
.protocol
!= wm
->left
.protocol
)
959 /* this should haven been diagnosed by whack
960 * !!! overloaded use of RC_CLASH
962 loglog(RC_CLASH
, "the protocol must be the same for leftport and rightport");
964 else if (check_connection_end(&wm
->right
, &wm
->left
, wm
)
965 && check_connection_end(&wm
->left
, &wm
->right
, wm
))
967 bool same_rightca
, same_leftca
;
968 struct connection
*c
= alloc_thing(struct connection
, "struct connection");
971 c
->ikev1
= wm
->ikev1
;
972 c
->policy
= wm
->policy
;
974 if ((c
->policy
& POLICY_COMPRESS
) && !can_do_IPcomp
)
976 , "ignoring --compress in \"%s\" because KLIPS is not configured to do IPCOMP"
984 DBG_log("from whack: got --esp=%s", wm
->esp ? wm
->esp
: "NULL")
986 c
->alg_info_esp
= alg_info_esp_create_from_str(wm
->esp? wm
->esp
: "", &ugh
);
988 DBG(DBG_CRYPT
|DBG_CONTROL
,
989 static char buf
[256]="<NULL>";
992 alg_info_snprint(buf
, sizeof(buf
)
993 ,(struct alg_info
*)c
->alg_info_esp
);
994 DBG_log("esp string values: %s", buf
);
998 if (c
->alg_info_esp
->alg_info_cnt
==0)
999 loglog(RC_LOG_SERIOUS
1000 , "got 0 transforms for esp=\"%s\"", wm
->esp
);
1004 loglog(RC_LOG_SERIOUS
1005 , "esp string error: %s", ugh? ugh
: "Unknown");
1014 DBG_log("from whack: got --ike=%s", wm
->ike ? wm
->ike
: "NULL")
1016 c
->alg_info_ike
= alg_info_ike_create_from_str(wm
->ike? wm
->ike
: "", &ugh
);
1018 DBG(DBG_CRYPT
|DBG_CONTROL
,
1019 static char buf
[256]="<NULL>";
1021 if (c
->alg_info_ike
)
1022 alg_info_snprint(buf
, sizeof(buf
)
1023 , (struct alg_info
*)c
->alg_info_ike
);
1024 DBG_log("ike string values: %s", buf
);
1026 if (c
->alg_info_ike
)
1028 if (c
->alg_info_ike
->alg_info_cnt
==0)
1029 loglog(RC_LOG_SERIOUS
1030 , "got 0 transforms for ike=\"%s\"", wm
->ike
);
1034 loglog(RC_LOG_SERIOUS
1035 , "ike string error: %s", ugh? ugh
: "Unknown");
1039 c
->sa_ike_life_seconds
= wm
->sa_ike_life_seconds
;
1040 c
->sa_ipsec_life_seconds
= wm
->sa_ipsec_life_seconds
;
1041 c
->sa_rekey_margin
= wm
->sa_rekey_margin
;
1042 c
->sa_rekey_fuzz
= wm
->sa_rekey_fuzz
;
1043 c
->sa_keying_tries
= wm
->sa_keying_tries
;
1046 c
->dpd_delay
= wm
->dpd_delay
;
1047 c
->dpd_timeout
= wm
->dpd_timeout
;
1048 c
->dpd_action
= wm
->dpd_action
;
1050 c
->addr_family
= wm
->addr_family
;
1051 c
->tunnel_addr_family
= wm
->tunnel_addr_family
;
1053 c
->requested_ca
= NULL
;
1055 same_leftca
= extract_end(&c
->spd
.this, &wm
->left
, "left");
1056 same_rightca
= extract_end(&c
->spd
.that
, &wm
->right
, "right");
1059 c
->spd
.that
.ca
= c
->spd
.this.ca
;
1060 else if (same_leftca
)
1061 c
->spd
.this.ca
= c
->spd
.that
.ca
;
1063 default_end(&c
->spd
.this, &c
->spd
.that
.host_addr
);
1064 default_end(&c
->spd
.that
, &c
->spd
.this.host_addr
);
1066 /* force any wildcard host IP address, any wildcard subnet
1067 * or any wildcard ID to that end
1069 if (isanyaddr(&c
->spd
.this.host_addr
) || c
->spd
.this.has_client_wildcard
1070 || c
->spd
.this.has_port_wildcard
|| c
->spd
.this.has_id_wildcards
)
1072 struct end t
= c
->spd
.this;
1074 c
->spd
.this = c
->spd
.that
;
1079 c
->spd
.reqid
= gen_reqid();
1081 /* set internal fields */
1082 c
->instance_serial
= 0;
1083 c
->ac_next
= connections
;
1085 c
->interface
= NULL
;
1086 c
->spd
.routing
= RT_UNROUTED
;
1087 c
->newest_isakmp_sa
= SOS_NOBODY
;
1088 c
->newest_ipsec_sa
= SOS_NOBODY
;
1089 c
->spd
.eroute_owner
= SOS_NOBODY
;
1091 if (c
->policy
& POLICY_GROUP
)
1096 else if ((isanyaddr(&c
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(c
->policy
))
1097 || c
->spd
.that
.has_client_wildcard
|| c
->spd
.that
.has_port_wildcard
1098 || c
->spd
.that
.has_id_wildcards
)
1100 /* Opportunistic or Road Warrior or wildcard client subnet
1102 c
->kind
= CK_TEMPLATE
;
1106 c
->kind
= CK_PERMANENT
;
1108 set_policy_prio(c
); /* must be after kind is set */
1111 c
->extra_debugging
= wm
->debugging
;
1116 passert(!(wm
->left
.virt
&& wm
->right
.virt
));
1117 if (wm
->left
.virt
|| wm
->right
.virt
)
1119 passert(isanyaddr(&c
->spd
.that
.host_addr
));
1120 c
->spd
.that
.virt
= create_virtual(c
,
1121 wm
->left
.virt ? wm
->left
.virt
: wm
->right
.virt
);
1122 if (c
->spd
.that
.virt
)
1123 c
->spd
.that
.has_client
= TRUE
;
1126 unshare_connection_strings(c
);
1130 connect_to_host_pair(c
);
1132 /* log all about this connection */
1133 plog("added connection description \"%s\"", c
->name
);
1135 char topo
[CONNECTION_BUF
];
1137 (void) format_connection(topo
, sizeof(topo
), c
, &c
->spd
);
1139 DBG_log("%s", topo
);
1141 /* Make sure that address families can be correctly inferred
1142 * from printed ends.
1144 passert(c
->addr_family
== addrtypeof(&c
->spd
.this.host_addr
)
1145 && c
->addr_family
== addrtypeof(&c
->spd
.this.host_nexthop
)
1146 && (c
->spd
.this.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1147 == subnettypeof(&c
->spd
.this.client
)
1149 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_addr
)
1150 && c
->addr_family
== addrtypeof(&c
->spd
.that
.host_nexthop
)
1151 && (c
->spd
.that
.has_client? c
->tunnel_addr_family
: c
->addr_family
)
1152 == subnettypeof(&c
->spd
.that
.client
));
1154 DBG_log("ike_life: %lus; ipsec_life: %lus; rekey_margin: %lus;"
1155 " rekey_fuzz: %lu%%; keyingtries: %lu; policy: %s"
1156 , (unsigned long) c
->sa_ike_life_seconds
1157 , (unsigned long) c
->sa_ipsec_life_seconds
1158 , (unsigned long) c
->sa_rekey_margin
1159 , (unsigned long) c
->sa_rekey_fuzz
1160 , (unsigned long) c
->sa_keying_tries
1161 , prettypolicy(c
->policy
));
1166 /* Derive a template connection from a group connection and target.
1167 * Similar to instantiate(). Happens at whack --listen.
1168 * Returns name of new connection. May be NULL.
1169 * Caller is responsible for pfreeing.
1172 add_group_instance(struct connection
*group
, const ip_subnet
*target
)
1175 , targetbuf
[SUBNETTOT_BUF
];
1176 struct connection
*t
;
1179 passert(group
->kind
== CK_GROUP
);
1180 passert(oriented(*group
));
1182 /* manufacture a unique name for this template */
1183 subnettot(target
, 0, targetbuf
, sizeof(targetbuf
));
1184 snprintf(namebuf
, sizeof(namebuf
), "%s#%s", group
->name
, targetbuf
);
1186 if (con_by_name(namebuf
, FALSE
) != NULL
)
1188 loglog(RC_DUPNAME
, "group name + target yields duplicate name \"%s\""
1193 t
= clone_thing(*group
, "group instance");
1195 unshare_connection_strings(t
);
1196 name
= clone_str(t
->name
, "group instance name");
1197 t
->spd
.that
.client
= *target
;
1198 t
->policy
&= ~(POLICY_GROUP
| POLICY_GROUTED
);
1199 t
->kind
= isanyaddr(&t
->spd
.that
.host_addr
) && !NEVER_NEGOTIATE(t
->policy
)
1200 ? CK_TEMPLATE
: CK_INSTANCE
;
1202 /* reset log file info */
1203 t
->log_file_name
= NULL
;
1205 t
->log_file_err
= FALSE
;
1207 t
->spd
.reqid
= gen_reqid();
1209 if (t
->spd
.that
.virt
)
1211 DBG_log("virtual_ip not supported in group instance");
1212 t
->spd
.that
.virt
= NULL
;
1215 /* add to connections list */
1216 t
->ac_next
= connections
;
1219 /* same host_pair as parent: stick after parent on list */
1222 /* route if group is routed */
1223 if (group
->policy
& POLICY_GROUTED
)
1225 if (!trap_connection(t
))
1226 whack_log(RC_ROUTE
, "could not route");
1232 /* an old target has disappeared for a group: delete instance */
1234 remove_group_instance(const struct connection
*group USED_BY_DEBUG
1237 passert(group
->kind
== CK_GROUP
);
1238 passert(oriented(*group
));
1240 delete_connections_by_name(name
, FALSE
);
1243 /* Common part of instantiating a Road Warrior or Opportunistic connection.
1244 * his_id can be used to carry over an ID discovered in Phase 1.
1245 * It must not disagree with the one in c, but if that is unspecified,
1246 * the new connection will use his_id.
1247 * If his_id is NULL, and c.that.id is uninstantiated (ID_NONE), the
1248 * new connection will continue to have an uninstantiated that.id.
1249 * Note: instantiation does not affect port numbers.
1251 * Note that instantiate can only deal with a single SPD/eroute.
1253 static struct connection
*
1254 instantiate(struct connection
*c
, const ip_address
*him
1255 , u_int16_t his_port
1256 , const struct id
*his_id
)
1258 struct connection
*d
;
1261 passert(c
->kind
== CK_TEMPLATE
);
1262 passert(c
->spd
.next
== NULL
);
1264 c
->instance_serial
++;
1265 d
= clone_thing(*c
, "temporary connection");
1268 passert(match_id(his_id
, &d
->spd
.that
.id
, &wildcards
));
1269 d
->spd
.that
.id
= *his_id
;
1270 d
->spd
.that
.has_id_wildcards
= FALSE
;
1272 unshare_connection_strings(d
);
1273 unshare_ietfAttrList(&d
->spd
.this.groups
);
1274 unshare_ietfAttrList(&d
->spd
.that
.groups
);
1275 d
->kind
= CK_INSTANCE
;
1277 passert(oriented(*d
));
1278 d
->spd
.that
.host_addr
= *him
;
1279 setportof(htons(c
->spd
.that
.port
), &d
->spd
.that
.host_addr
);
1281 if (his_port
) d
->spd
.that
.host_port
= his_port
;
1283 default_end(&d
->spd
.that
, &d
->spd
.this.host_addr
);
1285 /* We cannot guess what our next_hop should be, but if it was
1286 * explicitly specified as 0.0.0.0, we set it to be him.
1287 * (whack will not allow nexthop to be elided in RW case.)
1289 default_end(&d
->spd
.this, &d
->spd
.that
.host_addr
);
1291 d
->spd
.reqid
= gen_reqid();
1293 /* set internal fields */
1294 d
->ac_next
= connections
;
1296 d
->spd
.routing
= RT_UNROUTED
;
1297 d
->newest_isakmp_sa
= SOS_NOBODY
;
1298 d
->newest_ipsec_sa
= SOS_NOBODY
;
1299 d
->spd
.eroute_owner
= SOS_NOBODY
;
1301 /* reset log file info */
1302 d
->log_file_name
= NULL
;
1304 d
->log_file_err
= FALSE
;
1306 connect_to_host_pair(d
);
1312 rw_instantiate(struct connection
*c
, const ip_address
*him
, u_int16_t his_port
1313 , const ip_subnet
*his_net
, const struct id
*his_id
)
1315 struct connection
*d
= instantiate(c
, him
, his_port
, his_id
);
1317 if (d
&& his_net
&& is_virtual_connection(c
))
1319 d
->spd
.that
.client
= *his_net
;
1320 d
->spd
.that
.virt
= NULL
;
1321 if (subnetishost(his_net
) && addrinsubnet(him
, his_net
))
1322 d
->spd
.that
.has_client
= FALSE
;
1325 if (d
->policy
& POLICY_OPPO
)
1327 /* This must be before we know the client addresses.
1328 * Fill in one that is impossible. This prevents anyone else from
1329 * trying to use this connection to get to a particular client
1331 d
->spd
.that
.client
= *aftoinfo(subnettypeof(&d
->spd
.that
.client
))->none
;
1334 , DBG_log("instantiated \"%s\" for %s" , d
->name
, ip_str(him
)));
1339 oppo_instantiate(struct connection
*c
1340 , const ip_address
*him
1341 , const struct id
*his_id
1342 , struct gw_info
*gw
1343 , const ip_address
*our_client USED_BY_DEBUG
1344 , const ip_address
*peer_client
)
1346 struct connection
*d
= instantiate(c
, him
, 0, his_id
);
1348 passert(d
->spd
.next
== NULL
);
1350 /* fill in our client side */
1351 if (d
->spd
.this.has_client
)
1353 /* there was a client in the abstract connection
1354 * so we demand that the required client is within that subnet.
1356 passert(addrinsubnet(our_client
, &d
->spd
.this.client
));
1357 happy(addrtosubnet(our_client
, &d
->spd
.this.client
));
1358 /* opportunistic connections do not use port selectors */
1359 setportof(0, &d
->spd
.this.client
.addr
);
1363 /* there was no client in the abstract connection
1364 * so we demand that the required client be the host
1366 passert(sameaddr(our_client
, &d
->spd
.this.host_addr
));
1369 /* fill in peer's client side.
1370 * If the client is the peer, excise the client from the connection.
1372 passert((d
->policy
& POLICY_OPPO
)
1373 && addrinsubnet(peer_client
, &d
->spd
.that
.client
));
1374 happy(addrtosubnet(peer_client
, &d
->spd
.that
.client
));
1375 /* opportunistic connections do not use port selectors */
1376 setportof(0, &d
->spd
.that
.client
.addr
);
1378 if (sameaddr(peer_client
, &d
->spd
.that
.host_addr
))
1379 d
->spd
.that
.has_client
= FALSE
;
1381 passert(d
->gw_info
== NULL
);
1385 /* Adjust routing if something is eclipsing c.
1386 * It must be a %hold for us (hard to passert this).
1387 * If there was another instance eclipsing, we'd be using it.
1389 if (c
->spd
.routing
== RT_ROUTED_ECLIPSED
)
1390 d
->spd
.routing
= RT_ROUTED_PROSPECTIVE
;
1392 /* Remember if the template is routed:
1393 * if so, this instance applies for initiation
1394 * even if it is created for responding.
1396 if (routed(c
->spd
.routing
))
1397 d
->instance_initiation_ok
= TRUE
;
1400 char topo
[CONNECTION_BUF
];
1402 (void) format_connection(topo
, sizeof(topo
), d
, &d
->spd
);
1403 DBG_log("instantiated \"%s\": %s", d
->name
, topo
);
1408 /* priority formatting */
1410 fmt_policy_prio(policy_prio_t pp
, char buf
[POLICY_PRIO_BUF
])
1412 if (pp
== BOTTOM_PRIO
)
1413 snprintf(buf
, POLICY_PRIO_BUF
, "0");
1415 snprintf(buf
, POLICY_PRIO_BUF
, "%lu,%lu"
1416 , pp
>>16, (pp
& ~(~(policy_prio_t
)0 << 16)) >> 8);
1419 /* Format any information needed to identify an instance of a connection.
1420 * Fills any needed information into buf which MUST be big enough.
1421 * Road Warrior: peer's IP address
1422 * Opportunistic: [" " myclient "==="] " ..." peer ["===" hisclient] '\0'
1425 fmt_client(const ip_subnet
*client
, const ip_address
*gw
, const char *prefix
, char buf
[ADDRTOT_BUF
])
1427 if (subnetisaddr(client
, gw
))
1429 buf
[0] = '\0'; /* compact denotation for "self" */
1435 strcpy(buf
, prefix
);
1436 ap
= buf
+ strlen(prefix
);
1437 if (subnetisnone(client
))
1438 strcpy(ap
, "?"); /* unknown */
1440 subnettot(client
, 0, ap
, SUBNETTOT_BUF
);
1446 fmt_conn_instance(const struct connection
*c
, char buf
[CONN_INST_BUF
])
1452 if (c
->kind
== CK_INSTANCE
)
1454 if (c
->instance_serial
!= 0)
1456 snprintf(p
, CONN_INST_BUF
, "[%lu]", c
->instance_serial
);
1460 if (c
->policy
& POLICY_OPPO
)
1462 size_t w
= fmt_client(&c
->spd
.this.client
, &c
->spd
.this.host_addr
, " ", p
);
1466 strcpy(p
, w
== 0?
" ..." : "=== ...");
1469 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1472 (void) fmt_client(&c
->spd
.that
.client
, &c
->spd
.that
.host_addr
, "===", p
);
1477 addrtot(&c
->spd
.that
.host_addr
, 0, p
, ADDRTOT_BUF
);
1479 if (c
->spd
.that
.host_port
!= pluto_port
)
1482 sprintf(p
, ":%d", c
->spd
.that
.host_port
);
1488 /* Find an existing connection for a trapped outbound packet.
1489 * This is attempted before we bother with gateway discovery.
1490 * + this connection is routed or instance_of_routed_template
1491 * (i.e. approved for on-demand)
1492 * + this subnet contains our_client (or we are our_client)
1493 * + that subnet contains peer_client (or peer is peer_client)
1494 * + don't care about Phase 1 IDs (we don't know)
1495 * Note: result may still need to be instantiated.
1496 * The winner has the highest policy priority.
1498 * If there are several with that priority, we give preference to
1499 * the first one that is an instance.
1501 * See also build_outgoing_opportunistic_connection.
1504 find_connection_for_clients(struct spd_route
**srp
,
1505 const ip_address
*our_client
,
1506 const ip_address
*peer_client
,
1507 int transport_proto
)
1509 struct connection
*c
= connections
, *best
= NULL
;
1510 policy_prio_t best_prio
= BOTTOM_PRIO
;
1511 struct spd_route
*sr
;
1512 struct spd_route
*best_sr
= NULL
;
1513 int our_port
= ntohs(portof(our_client
));
1514 int peer_port
= ntohs(portof(peer_client
));
1516 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1518 if (DBGP(DBG_CONTROL
))
1520 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1522 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1523 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1524 DBG_log("find_connection: "
1525 "looking for policy for connection: %s:%d/%d -> %s:%d/%d"
1526 , ocb
, transport_proto
, our_port
, pcb
, transport_proto
, peer_port
);
1530 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
1532 if (c
->kind
== CK_GROUP
)
1535 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1537 if ((routed(sr
->routing
) || c
->instance_initiation_ok
)
1538 && addrinsubnet(our_client
, &sr
->this.client
)
1539 && addrinsubnet(peer_client
, &sr
->that
.client
)
1540 && addrinsubnet(peer_client
, &sr
->that
.client
)
1541 && (!sr
->this.protocol
|| transport_proto
== sr
->this.protocol
)
1542 && (!sr
->this.port
|| our_port
== sr
->this.port
)
1543 && (!sr
->that
.port
|| peer_port
== sr
->that
.port
))
1545 char cib
[CONN_INST_BUF
];
1546 char cib2
[CONN_INST_BUF
];
1548 policy_prio_t prio
= 8 * (c
->prio
+ (c
->kind
== CK_INSTANCE
))
1549 + 2 * (sr
->this.port
== our_port
)
1550 + 2 * (sr
->that
.port
== peer_port
)
1551 + (sr
->this.protocol
== transport_proto
);
1554 if (DBGP(DBG_CONTROL
|DBG_CONTROLMORE
))
1556 char c_ocb
[SUBNETTOT_BUF
], c_pcb
[SUBNETTOT_BUF
];
1558 subnettot(&c
->spd
.this.client
, 0, c_ocb
, sizeof(c_ocb
));
1559 subnettot(&c
->spd
.that
.client
, 0, c_pcb
, sizeof(c_pcb
));
1560 DBG_log("find_connection: conn \"%s\"%s has compatible peers: %s->%s [pri: %ld]"
1562 , (fmt_conn_instance(c
, cib
), cib
)
1563 , c_ocb
, c_pcb
, prio
);
1574 DBG(DBG_CONTROLMORE
,
1575 DBG_log("find_connection: "
1576 "comparing best \"%s\"%s [pri:%ld]{%p} (child %s) to \"%s\"%s [pri:%ld]{%p} (child %s)"
1578 , (fmt_conn_instance(best
, cib
), cib
)
1581 , (best
->policy_next ? best
->policy_next
->name
: "none")
1583 , (fmt_conn_instance(c
, cib2
), cib2
)
1586 , (c
->policy_next ? c
->policy_next
->name
: "none")));
1588 if (prio
> best_prio
)
1598 if (best
!= NULL
&& NEVER_NEGOTIATE(best
->policy
))
1601 if (srp
!= NULL
&& best
!= NULL
)
1605 if (DBGP(DBG_CONTROL
))
1609 char cib
[CONN_INST_BUF
];
1610 DBG_log("find_connection: concluding with \"%s\"%s [pri:%ld]{%p} kind=%s"
1612 , (fmt_conn_instance(best
, cib
), cib
)
1615 , enum_name(&connection_kind_names
, best
->kind
));
1617 DBG_log("find_connection: concluding with empty");
1625 /* Find and instantiate a connection for an outgoing Opportunistic connection.
1626 * We've already discovered its gateway.
1627 * We look for a the connection such that:
1628 * + this is one of our interfaces
1629 * + this subnet contains our_client (or we are our_client)
1630 * (we will specialize the client). We prefer the smallest such subnet.
1631 * + that subnet contains peer_clent (we will specialize the client).
1632 * We prefer the smallest such subnet.
1633 * + is opportunistic
1634 * + that peer is NO_IP
1635 * + don't care about Phase 1 IDs (probably should be default)
1636 * We could look for a connection that already had the desired peer
1637 * (rather than NO_IP) specified, but it doesn't seem worth the
1640 * We look for the routed policy applying to the narrowest subnets.
1641 * We only succeed if we find such a policy AND it is satisfactory.
1643 * The body of the inner loop is a lot like that in
1644 * find_connection_for_clients. In this case, we know the gateways
1645 * that we need to instantiate an opportunistic connection.
1648 build_outgoing_opportunistic_connection(struct gw_info
*gw
1649 ,const ip_address
*our_client
1650 ,const ip_address
*peer_client
)
1653 struct connection
*best
= NULL
;
1654 struct spd_route
*sr
, *bestsr
;
1655 char ocb
[ADDRTOT_BUF
], pcb
[ADDRTOT_BUF
];
1657 addrtot(our_client
, 0, ocb
, sizeof(ocb
));
1658 addrtot(peer_client
, 0, pcb
, sizeof(pcb
));
1660 passert(!isanyaddr(our_client
) && !isanyaddr(peer_client
));
1662 /* We don't know his ID yet, so gw id must be an ipaddr */
1663 passert(gw
->key
!= NULL
);
1664 passert(id_is_ipaddr(&gw
->gw_id
));
1666 /* for each of our addresses... */
1667 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1669 /* go through those connections with our address and NO_IP as hosts
1670 * We cannot know what port the peer would use, so we assume
1671 * that it is pluto_port (makes debugging easier).
1673 struct connection
*c
= find_host_pair_connections(&p
->addr
1674 , pluto_port
, (ip_address
*)NULL
, pluto_port
);
1676 for (; c
!= NULL
; c
= c
->hp_next
)
1679 DBG_log("checking %s", c
->name
));
1680 if (c
->kind
== CK_GROUP
)
1685 for (sr
= &c
->spd
; best
!=c
&& sr
; sr
= sr
->next
)
1687 if (routed(sr
->routing
)
1688 && addrinsubnet(our_client
, &sr
->this.client
)
1689 && addrinsubnet(peer_client
, &sr
->that
.client
))
1698 DBG_log("comparing best %s to %s"
1699 , best
->name
, c
->name
));
1701 for (bestsr
= &best
->spd
; best
!=c
&& bestsr
; bestsr
=bestsr
->next
)
1703 if (!subnetinsubnet(&bestsr
->this.client
, &sr
->this.client
)
1704 || (samesubnet(&bestsr
->this.client
, &sr
->this.client
)
1705 && !subnetinsubnet(&bestsr
->that
.client
1706 , &sr
->that
.client
)))
1717 || NEVER_NEGOTIATE(best
->policy
)
1718 || (best
->policy
& POLICY_OPPO
) == LEMPTY
1719 || best
->kind
!= CK_TEMPLATE
)
1722 return oppo_instantiate(best
, &gw
->gw_id
.ip_addr
, NULL
, gw
1723 , our_client
, peer_client
);
1727 orient(struct connection
*c
)
1729 struct spd_route
*sr
;
1735 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
1737 /* Note: this loop does not stop when it finds a match:
1738 * it continues checking to catch any ambiguity.
1740 for (p
= interfaces
; p
!= NULL
; p
= p
->next
)
1747 /* check if this interface matches this end */
1748 if (sameaddr(&sr
->this.host_addr
, &p
->addr
)
1749 && (!no_klips
|| sr
->this.host_port
== pluto_port
))
1753 if (c
->interface
== p
)
1754 loglog(RC_LOG_SERIOUS
1755 , "both sides of \"%s\" are our interface %s!"
1756 , c
->name
, p
->rname
);
1758 loglog(RC_LOG_SERIOUS
, "two interfaces match \"%s\" (%s, %s)"
1759 , c
->name
, c
->interface
->rname
, p
->rname
);
1760 c
->interface
= NULL
; /* withdraw orientation */
1766 /* done with this interface if it doesn't match that end */
1767 if (!(sameaddr(&sr
->that
.host_addr
, &p
->addr
)
1768 && (!no_klips
|| sr
->that
.host_port
== pluto_port
)))
1771 /* swap ends and try again.
1772 * It is a little tricky to see that this loop will stop.
1773 * Only continue if the far side matches.
1774 * If both sides match, there is an error-out.
1777 struct end t
= sr
->this;
1779 sr
->this = sr
->that
;
1786 return oriented(*c
);
1790 initiate_connection(const char *name
, int whackfd
)
1792 struct connection
*c
= con_by_name(name
, TRUE
);
1794 if (c
!= NULL
&& c
->ikev1
)
1796 set_cur_connection(c
);
1799 loglog(RC_ORIENT
, "we have no ipsecN interface for either end of this connection");
1801 else if (NEVER_NEGOTIATE(c
->policy
))
1804 , "cannot initiate an authby=never connection");
1806 else if (c
->kind
!= CK_PERMANENT
)
1808 if (isanyaddr(&c
->spd
.that
.host_addr
))
1809 loglog(RC_NOPEERIP
, "cannot initiate connection without knowing peer IP address");
1811 loglog(RC_WILDCARD
, "cannot initiate connection with ID wildcards");
1815 /* We will only request an IPsec SA if policy isn't empty
1816 * (ignoring Main Mode items).
1817 * This is a fudge, but not yet important.
1818 * If we are to proceed asynchronously, whackfd will be NULL_FD.
1820 c
->policy
|= POLICY_UP
;
1821 /* do we have to prompt for a PIN code? */
1822 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
&& whackfd
!= NULL_FD
)
1823 scx_get_pin(c
->spd
.this.sc
, whackfd
);
1825 if (c
->spd
.this.sc
!= NULL
&& !c
->spd
.this.sc
->valid
)
1827 loglog(RC_NOVALIDPIN
, "cannot initiate connection without valid PIN");
1831 ipsecdoi_initiate(whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
1832 whackfd
= NULL_FD
; /* protect from close */
1835 reset_cur_connection();
1840 /* (Possibly) Opportunistic Initiation:
1841 * Knowing clients (single IP addresses), try to build an tunnel.
1842 * This may involve discovering a gateway and instantiating an
1843 * Opportunistic connection. Called when a packet is caught by
1844 * a %trap, or when whack --oppohere --oppothere is used.
1845 * It may turn out that an existing or non-opporunistic connnection
1846 * can handle the traffic.
1848 * Most of the code will be restarted if an ADNS request is made
1849 * to discover the gateway. The only difference between the first
1850 * and second entry is whether gateways_from_dns is NULL or not.
1851 * initiate_opportunistic: initial entrypoint
1852 * continue_oppo: where we pickup when ADNS result arrives
1853 * initiate_opportunistic_body: main body shared by above routines
1854 * cannot_oppo: a helper function to log a diagnostic
1855 * This structure repeats a lot of code when the ADNS result arrives.
1856 * This seems like a waste, but anything learned the first time through
1857 * may no longer be true!
1859 * After the first IKE message is sent, the regular state machinery
1860 * carries negotiation forward.
1863 enum find_oppo_step
{
1866 fos_myid_hostname_txt
,
1868 fos_myid_hostname_key
,
1873 #endif /* USE_KEYRR */
1879 static const char *const oppo_step_name
[] = {
1882 "fos_myid_hostname_txt",
1884 "fos_myid_hostname_key",
1889 #endif /* USE_KEYRR */
1895 struct find_oppo_bundle
{
1896 enum find_oppo_step step
;
1898 bool failure_ok
; /* if true, continue_oppo should not die on DNS failure */
1899 ip_address our_client
; /* not pointer! */
1900 ip_address peer_client
;
1901 int transport_proto
;
1903 policy_prio_t policy_prio
;
1904 ipsec_spi_t failure_shunt
; /* in host order! 0 for delete. */
1908 struct find_oppo_continuation
{
1909 struct adns_continuation ac
; /* common prefix */
1910 struct find_oppo_bundle b
;
1914 cannot_oppo(struct connection
*c
1915 , struct find_oppo_bundle
*b
1918 char pcb
[ADDRTOT_BUF
];
1919 char ocb
[ADDRTOT_BUF
];
1921 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
1922 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
1924 DBG(DBG_DNS
| DBG_OPPO
, DBG_log("Can't Opportunistically initiate for %s to %s: %s"
1927 whack_log(RC_OPPOFAILURE
1928 , "Can't Opportunistically initiate for %s to %s: %s"
1931 if (c
!= NULL
&& c
->policy_next
!= NULL
)
1933 /* there is some policy that comes afterwards */
1934 struct spd_route
*shunt_spd
;
1935 struct connection
*nc
= c
->policy_next
;
1938 passert(c
->kind
== CK_TEMPLATE
);
1939 passert(c
->policy_next
->kind
== CK_PERMANENT
);
1941 DBG(DBG_OPPO
, DBG_log("OE failed for %s to %s, but %s overrides shunt"
1942 , ocb
, pcb
, c
->policy_next
->name
));
1945 * okay, here we need add to the "next" policy, which is ought
1946 * to be an instance.
1947 * We will add another entry to the spd_route list for the specific
1948 * situation that we have.
1951 shunt_spd
= clone_thing(nc
->spd
, "shunt eroute policy");
1953 shunt_spd
->next
= nc
->spd
.next
;
1954 nc
->spd
.next
= shunt_spd
;
1956 happy(addrtosubnet(&b
->peer_client
, &shunt_spd
->that
.client
));
1958 if (sameaddr(&b
->peer_client
, &shunt_spd
->that
.host_addr
))
1959 shunt_spd
->that
.has_client
= FALSE
;
1962 * override the tunnel destination with the one from the secondaried
1965 shunt_spd
->that
.host_addr
= nc
->spd
.that
.host_addr
;
1967 /* now, lookup the state, and poke it up.
1970 st
= state_with_serialno(nc
->newest_ipsec_sa
);
1972 /* XXX what to do if the IPSEC SA has died? */
1973 passert(st
!= NULL
);
1975 /* link the new connection instance to the state's list of
1979 DBG(DBG_OPPO
, DBG_log("installing state: %ld for %s to %s"
1980 , nc
->newest_ipsec_sa
1984 if (DBGP(DBG_OPPO
| DBG_CONTROLMORE
))
1986 char state_buf
[LOG_WIDTH
];
1987 char state_buf2
[LOG_WIDTH
];
1990 fmt_state(FALSE
, st
, n
1991 , state_buf
, sizeof(state_buf
)
1992 , state_buf2
, sizeof(state_buf2
));
1993 DBG_log("cannot_oppo, failure SA1: %s", state_buf
);
1994 DBG_log("cannot_oppo, failure SA2: %s", state_buf2
);
1998 if (!route_and_eroute(c
, shunt_spd
, st
))
2000 whack_log(RC_OPPOFAILURE
2001 , "failed to instantiate shunt policy %s for %s to %s"
2011 /* Replace HOLD with b->failure_shunt.
2012 * If no b->failure_shunt specified, use SPI_PASS -- THIS MAY CHANGE.
2014 if (b
->failure_shunt
== 0)
2016 DBG(DBG_OPPO
, DBG_log("no explicit failure shunt for %s to %s; installing %%pass"
2020 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2023 , b
->failure_shunt
!= 0
2024 , b
->transport_proto
2030 static void initiate_opportunistic_body(struct find_oppo_bundle
*b
2031 , struct adns_continuation
*ac
, err_t ac_ugh
); /* forward */
2034 initiate_opportunistic(const ip_address
*our_client
2035 , const ip_address
*peer_client
2036 , int transport_proto
2040 struct find_oppo_bundle b
;
2042 b
.want
= (whackfd
== NULL_FD ?
"whack" : "acquire");
2043 b
.failure_ok
= FALSE
;
2044 b
.our_client
= *our_client
;
2045 b
.peer_client
= *peer_client
;
2046 b
.transport_proto
= transport_proto
;
2048 b
.policy_prio
= BOTTOM_PRIO
;
2049 b
.failure_shunt
= 0;
2050 b
.whackfd
= whackfd
;
2052 initiate_opportunistic_body(&b
, NULL
, NULL
);
2056 continue_oppo(struct adns_continuation
*acr
, err_t ugh
)
2058 struct find_oppo_continuation
*cr
= (void *)acr
; /* inherit, damn you! */
2059 struct connection
*c
;
2060 bool was_held
= cr
->b
.held
;
2061 int whackfd
= cr
->b
.whackfd
;
2063 /* note: cr->id has no resources; cr->sgw_id is id_none:
2064 * neither need freeing.
2066 whack_log_fd
= whackfd
;
2069 /* Discover and record whether %hold has gone away.
2070 * This could have happened while we were awaiting DNS.
2071 * We must check BEFORE any call to cannot_oppo.
2074 cr
->b
.held
= has_bare_hold(&cr
->b
.our_client
, &cr
->b
.peer_client
2075 , cr
->b
.transport_proto
);
2079 /* if we're going to ignore the error, at least note it in debugging log */
2080 if (cr
->b
.failure_ok
&& ugh
!= NULL
)
2082 DBG(DBG_CONTROL
| DBG_DNS
,
2084 char ocb
[ADDRTOT_BUF
];
2085 char pcb
[ADDRTOT_BUF
];
2087 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2088 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2089 DBG_log("continuing from failed DNS lookup for %s, %s to %s: %s"
2090 , cr
->b
.want
, ocb
, pcb
, ugh
);
2095 if (!cr
->b
.failure_ok
&& ugh
!= NULL
)
2097 c
= find_connection_for_clients(NULL
, &cr
->b
.our_client
, &cr
->b
.peer_client
2098 , cr
->b
.transport_proto
);
2099 cannot_oppo(c
, &cr
->b
2100 , builddiag("%s: %s", cr
->b
.want
, ugh
));
2102 else if (was_held
&& !cr
->b
.held
)
2104 /* was_held indicates we were started due to a %trap firing
2105 * (as opposed to a "whack --oppohere --oppothere").
2106 * Since the %hold has gone, we can assume that somebody else
2107 * has beaten us to the punch. We can go home. But lets log it.
2109 char ocb
[ADDRTOT_BUF
];
2110 char pcb
[ADDRTOT_BUF
];
2112 addrtot(&cr
->b
.our_client
, 0, ocb
, sizeof(ocb
));
2113 addrtot(&cr
->b
.peer_client
, 0, pcb
, sizeof(pcb
));
2116 , "%%hold otherwise handled during DNS lookup for Opportunistic Initiation for %s to %s"
2121 initiate_opportunistic_body(&cr
->b
, &cr
->ac
, ugh
);
2122 whackfd
= NULL_FD
; /* was handed off */
2125 whack_log_fd
= NULL_FD
;
2131 check_key_recs(enum myid_state try_state
2132 , const struct connection
*c
2133 , struct adns_continuation
*ac
)
2135 /* Check if KEY lookup yielded good results.
2136 * Looking up based on our ID. Used if
2137 * client is ourself, or if TXT had no public key.
2138 * Note: if c is different this time, there is
2139 * a chance that we did the wrong query.
2140 * If so, treat as a kind of failure.
2142 enum myid_state old_myid_state
= myid_state
;
2143 const struct RSA_private_key
*our_RSA_pri
;
2146 myid_state
= try_state
;
2148 if (old_myid_state
!= myid_state
2149 && old_myid_state
== MYID_SPECIFIED
)
2151 ugh
= "%myid was specified while we were guessing";
2153 else if ((our_RSA_pri
= get_RSA_private_key(c
)) == NULL
)
2155 ugh
= "we don't know our own RSA key";
2157 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2159 ugh
= "our ID changed underfoot";
2163 /* Similar to code in RSA_check_signature
2164 * for checking the other side.
2168 ugh
= "no KEY RR found for us";
2169 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2171 ugh
= "all our KEY RRs have the wrong public key";
2172 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2173 && same_RSA_public_key(&our_RSA_pri
->pub
, &kr
->key
->u
.rsa
))
2175 ugh
= NULL
; /* good! */
2181 myid_state
= old_myid_state
;
2184 #endif /* USE_KEYRR */
2187 check_txt_recs(enum myid_state try_state
2188 , const struct connection
*c
2189 , struct adns_continuation
*ac
)
2191 /* Check if TXT lookup yielded good results.
2192 * Looking up based on our ID. Used if
2193 * client is ourself, or if TXT had no public key.
2194 * Note: if c is different this time, there is
2195 * a chance that we did the wrong query.
2196 * If so, treat as a kind of failure.
2198 enum myid_state old_myid_state
= myid_state
;
2199 const struct RSA_private_key
*our_RSA_pri
;
2202 myid_state
= try_state
;
2204 if (old_myid_state
!= myid_state
2205 && old_myid_state
== MYID_SPECIFIED
)
2207 ugh
= "%myid was specified while we were guessing";
2209 else if ((our_RSA_pri
= get_RSA_private_key(c
)) == NULL
)
2211 ugh
= "we don't know our own RSA key";
2213 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2215 ugh
= "our ID changed underfoot";
2219 /* Similar to code in RSA_check_signature
2220 * for checking the other side.
2222 struct gw_info
*gwp
;
2224 ugh
= "no TXT RR found for us";
2225 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2227 ugh
= "all our TXT RRs have the wrong public key";
2228 if (gwp
->key
->alg
== PUBKEY_ALG_RSA
2229 && same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2231 ugh
= NULL
; /* good! */
2237 myid_state
= old_myid_state
;
2242 /* note: gateways_from_dns must be NULL iff this is the first call */
2244 initiate_opportunistic_body(struct find_oppo_bundle
*b
2245 , struct adns_continuation
*ac
2248 struct connection
*c
;
2249 struct spd_route
*sr
;
2251 /* What connection shall we use?
2252 * First try for one that explicitly handles the clients.
2256 char ours
[ADDRTOT_BUF
];
2257 char his
[ADDRTOT_BUF
];
2261 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2262 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2263 ourport
= ntohs(portof(&b
->our_client
));
2264 hisport
= ntohs(portof(&b
->peer_client
));
2265 DBG_log("initiate on demand from %s:%d to %s:%d proto=%d state: %s because: %s"
2266 , ours
, ourport
, his
, hisport
, b
->transport_proto
2267 , oppo_step_name
[b
->step
], b
->want
);
2269 if (isanyaddr(&b
->our_client
) || isanyaddr(&b
->peer_client
))
2271 cannot_oppo(NULL
, b
, "impossible IP address");
2273 else if ((c
= find_connection_for_clients(&sr
2276 , b
->transport_proto
)) == NULL
)
2278 /* No connection explicitly handles the clients and there
2279 * are no Opportunistic connections -- whine and give up.
2280 * The failure policy cannot be gotten from a connection; we pick %pass.
2282 cannot_oppo(NULL
, b
, "no routed Opportunistic template covers this pair");
2284 else if (c
->kind
!= CK_TEMPLATE
)
2286 /* We've found a connection that can serve.
2287 * Do we have to initiate it?
2288 * Not if there is currently an IPSEC SA.
2289 * But if there is an IPSEC SA, then KLIPS would not
2290 * have generated the acquire. So we assume that there isn't one.
2291 * This may be redundant if a non-opportunistic
2292 * negotiation is already being attempted.
2295 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2297 if(c
->kind
== CK_INSTANCE
)
2299 char cib
[CONN_INST_BUF
];
2300 /* there is already an instance being negotiated, no nothing */
2301 DBG(DBG_CONTROL
, DBG_log("found existing instance \"%s\"%s, rekeying it"
2303 , (fmt_conn_instance(c
, cib
), cib
)));
2304 /* XXX-mcr - return; */
2307 /* otherwise, there is some kind of static conn that can handle
2308 * this connection, so we initiate it */
2313 /* what should we do on failure? */
2314 (void) assign_hold(c
, sr
, b
->transport_proto
, &b
->our_client
, &b
->peer_client
);
2317 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2318 b
->whackfd
= NULL_FD
; /* protect from close */
2322 /* We are handling an opportunistic situation.
2323 * This involves several DNS lookup steps that require suspension.
2324 * Note: many facts might change while we're suspended.
2327 * The first chunk of code handles the result of the previous
2328 * DNS query (if any). It also selects the kind of the next step.
2329 * The second chunk initiates the next DNS query (if any).
2331 enum find_oppo_step next_step
;
2333 char mycredentialstr
[BUF_LEN
];
2334 char cib
[CONN_INST_BUF
];
2336 DBG(DBG_CONTROL
, DBG_log("creating new instance from \"%s\"%s"
2338 , (fmt_conn_instance(c
, cib
), cib
)));
2341 idtoa(&sr
->this.id
, mycredentialstr
, sizeof(mycredentialstr
));
2343 passert(c
->policy
& POLICY_OPPO
); /* can't initiate Road Warrior connections */
2345 /* handle any DNS answer; select next step */
2350 /* just starting out: select first query step */
2351 next_step
= fos_myid_ip_txt
;
2354 case fos_myid_ip_txt
: /* TXT for our default IP address as %myid */
2355 ugh
= check_txt_recs(MYID_IP
, c
, ac
);
2358 /* cannot use our IP as OE identitiy for initiation */
2359 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:TXT) as identity: %s"
2362 if (!logged_myid_ip_txt_warning
)
2364 loglog(RC_LOG_SERIOUS
2365 , "can not use our IP (%s:TXT) as identity: %s"
2368 logged_myid_ip_txt_warning
= TRUE
;
2371 next_step
= fos_myid_hostname_txt
;
2372 ugh
= NULL
; /* failure can be recovered from */
2376 /* we can use our IP as OE identity for initiation */
2377 if (!logged_myid_ip_txt_warning
)
2379 loglog(RC_LOG_SERIOUS
2380 , "using our IP (%s:TXT) as identity!"
2381 , myid_str
[MYID_IP
]);
2382 logged_myid_ip_txt_warning
= TRUE
;
2385 next_step
= fos_our_client
;
2389 case fos_myid_hostname_txt
: /* TXT for our hostname as %myid */
2390 ugh
= check_txt_recs(MYID_HOSTNAME
, c
, ac
);
2393 /* cannot use our hostname as OE identitiy for initiation */
2394 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:TXT) as identity: %s"
2395 , myid_str
[MYID_HOSTNAME
]
2397 if (!logged_myid_fqdn_txt_warning
)
2399 loglog(RC_LOG_SERIOUS
2400 , "can not use our hostname (%s:TXT) as identity: %s"
2401 , myid_str
[MYID_HOSTNAME
]
2403 logged_myid_fqdn_txt_warning
= TRUE
;
2406 next_step
= fos_myid_ip_key
;
2407 ugh
= NULL
; /* failure can be recovered from */
2412 /* we can use our hostname as OE identity for initiation */
2413 if (!logged_myid_fqdn_txt_warning
)
2415 loglog(RC_LOG_SERIOUS
2416 , "using our hostname (%s:TXT) as identity!"
2417 , myid_str
[MYID_HOSTNAME
]);
2418 logged_myid_fqdn_txt_warning
= TRUE
;
2420 next_step
= fos_our_client
;
2425 case fos_myid_ip_key
: /* KEY for our default IP address as %myid */
2426 ugh
= check_key_recs(MYID_IP
, c
, ac
);
2429 /* cannot use our IP as OE identitiy for initiation */
2430 DBG(DBG_OPPO
, DBG_log("can not use our IP (%s:KEY) as identity: %s"
2433 if (!logged_myid_ip_key_warning
)
2435 loglog(RC_LOG_SERIOUS
2436 , "can not use our IP (%s:KEY) as identity: %s"
2439 logged_myid_ip_key_warning
= TRUE
;
2442 next_step
= fos_myid_hostname_key
;
2443 ugh
= NULL
; /* failure can be recovered from */
2447 /* we can use our IP as OE identity for initiation */
2448 if (!logged_myid_ip_key_warning
)
2450 loglog(RC_LOG_SERIOUS
2451 , "using our IP (%s:KEY) as identity!"
2452 , myid_str
[MYID_IP
]);
2453 logged_myid_ip_key_warning
= TRUE
;
2455 next_step
= fos_our_client
;
2459 case fos_myid_hostname_key
: /* KEY for our hostname as %myid */
2460 ugh
= check_key_recs(MYID_HOSTNAME
, c
, ac
);
2463 /* cannot use our IP as OE identitiy for initiation */
2464 DBG(DBG_OPPO
, DBG_log("can not use our hostname (%s:KEY) as identity: %s"
2465 , myid_str
[MYID_HOSTNAME
]
2467 if (!logged_myid_fqdn_key_warning
)
2469 loglog(RC_LOG_SERIOUS
2470 , "can not use our hostname (%s:KEY) as identity: %s"
2471 , myid_str
[MYID_HOSTNAME
]
2473 logged_myid_fqdn_key_warning
= TRUE
;
2476 next_step
= fos_myid_hostname_key
;
2477 ugh
= NULL
; /* failure can be recovered from */
2481 /* we can use our IP as OE identity for initiation */
2482 if (!logged_myid_fqdn_key_warning
)
2484 loglog(RC_LOG_SERIOUS
2485 , "using our hostname (%s:KEY) as identity!"
2486 , myid_str
[MYID_HOSTNAME
]);
2487 logged_myid_fqdn_key_warning
= TRUE
;
2489 next_step
= fos_our_client
;
2494 case fos_our_client
: /* TXT for our client */
2496 /* Our client is not us: we must check the TXT records.
2497 * Note: if c is different this time, there is
2498 * a chance that we did the wrong query.
2499 * If so, treat as a kind of failure.
2501 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2503 next_step
= fos_his_client
; /* normal situation */
2505 passert(sr
!= NULL
);
2507 if (our_RSA_pri
== NULL
)
2509 ugh
= "we don't know our own RSA key";
2511 else if (sameaddr(&sr
->this.host_addr
, &b
->our_client
))
2513 /* this wasn't true when we started -- bail */
2514 ugh
= "our IP address changed underfoot";
2516 else if (!same_id(&ac
->sgw_id
, &sr
->this.id
))
2518 /* this wasn't true when we started -- bail */
2519 ugh
= "our ID changed underfoot";
2523 /* Similar to code in quick_inI1_outR1_tail
2524 * for checking the other side.
2526 struct gw_info
*gwp
;
2528 ugh
= "no TXT RR for our client delegates us";
2529 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2531 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2533 ugh
= "TXT RR for our client has wrong key";
2534 /* If there is a key from the TXT record,
2535 * we count it as a win if we match the key.
2536 * If there was no key, we have a tentative win:
2537 * we need to check our KEY record to be sure.
2539 if (!gwp
->gw_key_present
)
2541 /* Success, but the TXT had no key
2542 * so we must check our our own KEY records.
2544 next_step
= fos_our_txt
;
2545 ugh
= NULL
; /* good! */
2548 if (same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2550 ugh
= NULL
; /* good! */
2558 case fos_our_txt
: /* TXT for us */
2560 /* Check if TXT lookup yielded good results.
2561 * Looking up based on our ID. Used if
2562 * client is ourself, or if TXT had no public key.
2563 * Note: if c is different this time, there is
2564 * a chance that we did the wrong query.
2565 * If so, treat as a kind of failure.
2567 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2569 next_step
= fos_his_client
; /* unless we decide to look for KEY RR */
2571 if (our_RSA_pri
== NULL
)
2573 ugh
= "we don't know our own RSA key";
2575 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2577 ugh
= "our ID changed underfoot";
2581 /* Similar to code in RSA_check_signature
2582 * for checking the other side.
2584 struct gw_info
*gwp
;
2586 ugh
= "no TXT RR for us";
2587 for (gwp
= ac
->gateways_from_dns
; gwp
!= NULL
; gwp
= gwp
->next
)
2589 passert(same_id(&gwp
->gw_id
, &sr
->this.id
));
2591 ugh
= "TXT RR for us has wrong key";
2592 if (gwp
->gw_key_present
2593 && same_RSA_public_key(&our_RSA_pri
->pub
, &gwp
->key
->u
.rsa
))
2596 DBG_log("initiate on demand found TXT with right public key at: %s"
2597 , mycredentialstr
));
2605 /* if no TXT with right key, try KEY */
2607 DBG_log("will try for KEY RR since initiate on demand found %s: %s"
2608 , ugh
, mycredentialstr
));
2609 next_step
= fos_our_key
;
2618 case fos_our_key
: /* KEY for us */
2620 /* Check if KEY lookup yielded good results.
2621 * Looking up based on our ID. Used if
2622 * client is ourself, or if TXT had no public key.
2623 * Note: if c is different this time, there is
2624 * a chance that we did the wrong query.
2625 * If so, treat as a kind of failure.
2627 const struct RSA_private_key
*our_RSA_pri
= get_RSA_private_key(c
);
2629 next_step
= fos_his_client
; /* always */
2631 if (our_RSA_pri
== NULL
)
2633 ugh
= "we don't know our own RSA key";
2635 else if (!same_id(&ac
->id
, &c
->spd
.this.id
))
2637 ugh
= "our ID changed underfoot";
2641 /* Similar to code in RSA_check_signature
2642 * for checking the other side.
2646 ugh
= "no KEY RR found for us (and no good TXT RR)";
2647 for (kr
= ac
->keys_from_dns
; kr
!= NULL
; kr
= kr
->next
)
2649 ugh
= "all our KEY RRs have the wrong public key (and no good TXT RR)";
2650 if (kr
->key
->alg
== PUBKEY_ALG_RSA
2651 && same_RSA_public_key(&our_RSA_pri
->pub
, &kr
->key
->u
.rsa
))
2653 /* do this only once a day */
2654 if (!logged_txt_warning
)
2656 loglog(RC_LOG_SERIOUS
2657 , "found KEY RR but not TXT RR for %s. See http://www.freeswan.org/err/txt-change.html."
2659 logged_txt_warning
= TRUE
;
2661 ugh
= NULL
; /* good! */
2668 #endif /* USE_KEYRR */
2670 case fos_his_client
: /* TXT for his client */
2672 /* We've finished last DNS queries: TXT for his client.
2673 * Using the information, try to instantiate a connection
2674 * and start negotiating.
2675 * We now know the peer. The chosing of "c" ignored this,
2676 * so we will disregard its current value.
2677 * !!! We need to randomize the entry in gw that we choose.
2679 next_step
= fos_done
; /* no more queries */
2681 c
= build_outgoing_opportunistic_connection(ac
->gateways_from_dns
2687 /* We cannot seem to instantiate a suitable connection:
2690 char ocb
[ADDRTOT_BUF
]
2694 addrtot(&b
->our_client
, 0, ocb
, sizeof(ocb
));
2695 addrtot(&b
->peer_client
, 0, pcb
, sizeof(pcb
));
2696 passert(id_is_ipaddr(&ac
->gateways_from_dns
->gw_id
));
2697 addrtot(&ac
->gateways_from_dns
->gw_id
.ip_addr
, 0, pb
, sizeof(pb
));
2698 loglog(RC_OPPOFAILURE
2699 , "no suitable connection for opportunism"
2700 " between %s and %s with %s as peer"
2706 /* Replace HOLD with PASS.
2707 * The type of replacement *ought* to be
2708 * specified by policy.
2710 (void) replace_bare_shunt(&b
->our_client
, &b
->peer_client
2712 , SPI_PASS
/* fail into PASS */
2713 , TRUE
, b
->transport_proto
2714 , "no suitable connection");
2720 /* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
2721 passert(c
->kind
== CK_INSTANCE
);
2722 passert(c
->gw_info
!= NULL
);
2723 passert(HAS_IPSEC_POLICY(c
->policy
));
2724 passert(LHAS(LELEM(RT_UNROUTED
) | LELEM(RT_ROUTED_PROSPECTIVE
), c
->spd
.routing
));
2728 /* what should we do on failure? */
2729 (void) assign_hold(c
, &c
->spd
2730 , b
->transport_proto
2731 , &b
->our_client
, &b
->peer_client
);
2734 c
->gw_info
->key
->last_tried_time
= now();
2735 ipsecdoi_initiate(b
->whackfd
, c
, c
->policy
, 1, SOS_NOBODY
);
2736 b
->whackfd
= NULL_FD
; /* protect from close */
2745 /* the second chunk: initiate the next DNS query (if any) */
2748 char ours
[ADDRTOT_BUF
];
2749 char his
[ADDRTOT_BUF
];
2751 addrtot(&b
->our_client
, 0, ours
, sizeof(ours
));
2752 addrtot(&b
->peer_client
, 0, his
, sizeof(his
));
2753 DBG_log("initiate on demand from %s to %s new state: %s with ugh: %s"
2754 , ours
, his
, oppo_step_name
[b
->step
], ugh ? ugh
: "ok");
2759 b
->policy_prio
= c
->prio
;
2760 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2761 cannot_oppo(c
, b
, ugh
);
2763 else if (next_step
== fos_done
)
2769 /* set up the next query */
2770 struct find_oppo_continuation
*cr
= alloc_thing(struct find_oppo_continuation
2771 , "opportunistic continuation");
2774 b
->policy_prio
= c
->prio
;
2775 b
->failure_shunt
= shunt_policy_spi(c
, FALSE
);
2776 cr
->b
= *b
; /* copy; start hand off of whackfd */
2777 cr
->b
.failure_ok
= FALSE
;
2778 cr
->b
.step
= next_step
;
2781 ; sr
!=NULL
&& !sameaddr(&sr
->this.host_addr
, &b
->our_client
)
2788 /* If a %hold shunt has replaced the eroute for this template,
2792 && sr
->routing
== RT_ROUTED_PROSPECTIVE
&& eclipsable(sr
))
2794 sr
->routing
= RT_ROUTED_ECLIPSED
;
2798 /* Switch to issue next query.
2799 * A case may turn out to be unnecessary. If so, it falls
2800 * through to the next case.
2801 * Figuring out what %myid can stand for must be done before
2802 * our client credentials are looked up: we must know what
2803 * the client credentials may use to identify us.
2804 * On the other hand, our own credentials should be looked
2805 * up after our clients in case our credentials are not
2807 * XXX this is a wasted effort if we don't have credentials
2808 * BUT they are not needed.
2812 case fos_myid_ip_txt
:
2813 if (c
->spd
.this.id
.kind
== ID_MYID
2814 && myid_state
!= MYID_SPECIFIED
)
2816 cr
->b
.failure_ok
= TRUE
;
2817 cr
->b
.want
= b
->want
= "TXT record for IP address as %myid";
2818 ugh
= start_adns_query(&myids
[MYID_IP
]
2825 cr
->b
.step
= fos_myid_hostname_txt
;
2828 case fos_myid_hostname_txt
:
2829 if (c
->spd
.this.id
.kind
== ID_MYID
2830 && myid_state
!= MYID_SPECIFIED
)
2833 cr
->b
.failure_ok
= TRUE
;
2835 cr
->b
.failure_ok
= FALSE
;
2837 cr
->b
.want
= b
->want
= "TXT record for hostname as %myid";
2838 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2839 , &myids
[MYID_HOSTNAME
]
2847 cr
->b
.step
= fos_myid_ip_key
;
2850 case fos_myid_ip_key
:
2851 if (c
->spd
.this.id
.kind
== ID_MYID
2852 && myid_state
!= MYID_SPECIFIED
)
2854 cr
->b
.failure_ok
= TRUE
;
2855 cr
->b
.want
= b
->want
= "KEY record for IP address as %myid (no good TXT)";
2856 ugh
= start_adns_query(&myids
[MYID_IP
]
2857 , (const struct id
*) NULL
/* security gateway meaningless */
2863 cr
->b
.step
= fos_myid_hostname_key
;
2866 case fos_myid_hostname_key
:
2867 if (c
->spd
.this.id
.kind
== ID_MYID
2868 && myid_state
!= MYID_SPECIFIED
)
2870 cr
->b
.failure_ok
= FALSE
; /* last attempt! */
2871 cr
->b
.want
= b
->want
= "KEY record for hostname as %myid (no good TXT)";
2872 ugh
= start_adns_query(&myids
[MYID_HOSTNAME
]
2873 , (const struct id
*) NULL
/* security gateway meaningless */
2880 cr
->b
.step
= fos_our_client
;
2883 case fos_our_client
: /* TXT for our client */
2884 if (!sameaddr(&c
->spd
.this.host_addr
, &b
->our_client
))
2886 /* Check that at least one TXT(reverse(b->our_client)) is workable.
2887 * Note: {unshare|free}_id_content not needed for id: ephemeral.
2889 cr
->b
.want
= b
->want
= "our client's TXT record";
2890 iptoid(&b
->our_client
, &id
);
2891 ugh
= start_adns_query(&id
2892 , &c
->spd
.this.id
/* we are the security gateway */
2898 cr
->b
.step
= fos_our_txt
;
2901 case fos_our_txt
: /* TXT for us */
2902 cr
->b
.failure_ok
= b
->failure_ok
= TRUE
;
2903 cr
->b
.want
= b
->want
= "our TXT record";
2904 ugh
= start_adns_query(&sr
->this.id
2905 , &sr
->this.id
/* we are the security gateway XXX - maybe ignore? mcr */
2912 case fos_our_key
: /* KEY for us */
2913 cr
->b
.want
= b
->want
= "our KEY record";
2914 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2915 ugh
= start_adns_query(&sr
->this.id
2916 , (const struct id
*) NULL
/* security gateway meaningless */
2921 #endif /* USE_KEYRR */
2923 case fos_his_client
: /* TXT for his client */
2924 /* note: {unshare|free}_id_content not needed for id: ephemeral */
2925 cr
->b
.want
= b
->want
= "target's TXT record";
2926 cr
->b
.failure_ok
= b
->failure_ok
= FALSE
;
2927 iptoid(&b
->peer_client
, &id
);
2928 ugh
= start_adns_query(&id
2929 , (const struct id
*) NULL
/* security gateway unconstrained */
2936 bad_case(next_step
);
2940 b
->whackfd
= NULL_FD
; /* complete hand-off */
2942 cannot_oppo(c
, b
, ugh
);
2945 close_any(b
->whackfd
);
2949 terminate_connection(const char *nm
)
2951 /* Loop because more than one may match (master and instances)
2952 * But at least one is required (enforced by con_by_name).
2954 struct connection
*c
= con_by_name(nm
, TRUE
);
2956 if (c
== NULL
|| !c
->ikev1
)
2961 struct connection
*n
= c
->ac_next
; /* grab this before c might disappear */
2963 if (streq(c
->name
, nm
)
2964 && c
->kind
>= CK_PERMANENT
2965 && !NEVER_NEGOTIATE(c
->policy
))
2967 set_cur_connection(c
);
2968 plog("terminating SAs using this connection");
2969 c
->policy
&= ~POLICY_UP
;
2970 flush_pending_by_connection(c
);
2971 delete_states_by_connection(c
, FALSE
);
2972 reset_cur_connection();
2975 } while (c
!= NULL
);
2978 /* check nexthop safety
2979 * Our nexthop must not be within a routed client subnet, and vice versa.
2980 * Note: we don't think this is true. We think that KLIPS will
2981 * not process a packet output by an eroute.
2985 //check_nexthop(const struct connection *c)
2987 // struct connection *d;
2989 // if (addrinsubnet(&c->spd.this.host_nexthop, &c->spd.that.client))
2991 // loglog(RC_LOG_SERIOUS, "cannot perform routing for connection \"%s\""
2992 // " because nexthop is within peer's client network",
2997 // for (d = connections; d != NULL; d = d->next)
2999 // if (d->routing != RT_UNROUTED)
3001 // if (addrinsubnet(&c->spd.this.host_nexthop, &d->spd.that.client))
3003 // loglog(RC_LOG_SERIOUS, "cannot do routing for connection \"%s\"
3004 // " because nexthop is contained in"
3005 // " existing routing for connection \"%s\"",
3006 // c->name, d->name);
3009 // if (addrinsubnet(&d->spd.this.host_nexthop, &c->spd.that.client))
3011 // loglog(RC_LOG_SERIOUS, "cannot do routing for connection \"%s\"
3012 // " because it contains nexthop of"
3013 // " existing routing for connection \"%s\"",
3014 // c->name, d->name);
3023 /* an ISAKMP SA has been established.
3024 * Note the serial number, and release any connections with
3025 * the same peer ID but different peer IP address.
3027 bool uniqueIDs
= FALSE
; /* --uniqueids? */
3030 ISAKMP_SA_established(struct connection
*c
, so_serial_t serial
)
3032 c
->newest_isakmp_sa
= serial
;
3034 /* the connection is now oriented so that we are able to determine
3035 * whether we are a mode config server with a virtual IP to send.
3037 if (!isanyaddr(&c
->spd
.that
.host_srcip
) && !c
->spd
.that
.has_natip
)
3038 c
->spd
.that
.modecfg
= TRUE
;
3042 /* for all connections: if the same Phase 1 IDs are used
3043 * for a different IP address, unorient that connection.
3045 struct connection
*d
;
3047 for (d
= connections
; d
!= NULL
; )
3049 struct connection
*next
= d
->ac_next
; /* might move underneath us */
3051 if (d
->kind
>= CK_PERMANENT
3052 && same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
3053 && same_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
)
3054 && (!sameaddr(&c
->spd
.that
.host_addr
, &d
->spd
.that
.host_addr
) ||
3055 (c
->spd
.that
.host_port
!= d
->spd
.that
.host_port
)))
3057 release_connection(d
, FALSE
);
3064 /* Find the connection to connection c's peer's client with the
3065 * largest value of .routing. All other things being equal,
3066 * preference is given to c. If none is routed, return NULL.
3068 * If erop is non-null, set *erop to a connection sharing both
3069 * our client subnet and peer's client subnet with the largest value
3070 * of .routing. If none is erouted, set *erop to NULL.
3072 * The return value is used to find other connections sharing a route.
3073 * *erop is used to find other connections sharing an eroute.
3076 route_owner(struct connection
*c
3077 , struct spd_route
**srp
3078 , struct connection
**erop
3079 , struct spd_route
**esrp
)
3081 struct connection
*d
3084 struct spd_route
*srd
, *src
;
3085 struct spd_route
*best_sr
, *best_esr
;
3086 enum routing_t best_routing
, best_erouting
;
3088 passert(oriented(*c
));
3091 best_routing
= c
->spd
.routing
;
3092 best_erouting
= best_routing
;
3094 for (d
= connections
; d
!= NULL
; d
= d
->ac_next
)
3096 for (srd
= &d
->spd
; srd
; srd
= srd
->next
)
3098 if (srd
->routing
== RT_UNROUTED
)
3101 for (src
= &c
->spd
; src
; src
=src
->next
)
3103 if (!samesubnet(&src
->that
.client
, &srd
->that
.client
))
3105 if (src
->that
.protocol
!= srd
->that
.protocol
)
3107 if (src
->that
.port
!= srd
->that
.port
)
3109 passert(oriented(*d
));
3110 if (srd
->routing
> best_routing
)
3114 best_routing
= srd
->routing
;
3117 if (!samesubnet(&src
->this.client
, &srd
->this.client
))
3119 if (src
->this.protocol
!= srd
->this.protocol
)
3121 if (src
->this.port
!= srd
->this.port
)
3123 if (srd
->routing
> best_erouting
)
3127 best_erouting
= srd
->routing
;
3135 char cib
[CONN_INST_BUF
];
3136 err_t m
= builddiag("route owner of \"%s\"%s %s:"
3138 , (fmt_conn_instance(c
, cib
), cib
)
3139 , enum_name(&routing_story
, c
->spd
.routing
));
3141 if (!routed(best_ro
->spd
.routing
))
3142 m
= builddiag("%s NULL", m
);
3143 else if (best_ro
== c
)
3144 m
= builddiag("%s self", m
);
3146 m
= builddiag("%s \"%s\"%s %s", m
3148 , (fmt_conn_instance(best_ro
, cib
), cib
)
3149 , enum_name(&routing_story
, best_ro
->spd
.routing
));
3153 m
= builddiag("%s; eroute owner:", m
);
3154 if (!erouted(best_ero
->spd
.routing
))
3155 m
= builddiag("%s NULL", m
);
3156 else if (best_ero
== c
)
3157 m
= builddiag("%s self", m
);
3159 m
= builddiag("%s \"%s\"%s %s", m
3161 , (fmt_conn_instance(best_ero
, cib
), cib
)
3162 , enum_name(&routing_story
, best_ero
->spd
.routing
));
3169 *erop
= erouted(best_erouting
)? best_ero
: NULL
;
3178 return routed(best_routing
)? best_ro
: NULL
;
3181 /* Find a connection that owns the shunt eroute between subnets.
3182 * There ought to be only one.
3183 * This might get to be a bottleneck -- try hashing if it does.
3186 shunt_owner(const ip_subnet
*ours
, const ip_subnet
*his
)
3188 struct connection
*c
;
3189 struct spd_route
*sr
;
3191 for (c
= connections
; c
!= NULL
; c
= c
->ac_next
)
3193 for (sr
= &c
->spd
; sr
; sr
= sr
->next
)
3195 if (shunt_erouted(sr
->routing
)
3196 && samesubnet(ours
, &sr
->this.client
)
3197 && samesubnet(his
, &sr
->that
.client
))
3204 /* Find some connection with this pair of hosts.
3205 * We don't know enough to chose amongst those available.
3206 * ??? no longer usefully different from find_host_pair_connections
3209 find_host_connection(const ip_address
*me
, u_int16_t my_port
3210 , const ip_address
*him
, u_int16_t his_port
, lset_t policy
)
3212 struct connection
*c
= find_host_pair_connections(me
, my_port
, him
, his_port
);
3214 if (policy
!= LEMPTY
)
3216 lset_t auth_requested
= policy
& POLICY_ID_AUTH_MASK
;
3218 /* if we have requirements for the policy,
3219 * choose the first matching connection.
3223 if (c
->policy
& auth_requested
)
3233 /* given an up-until-now satisfactory connection, find the best connection
3234 * now that we just got the Phase 1 Id Payload from the peer.
3236 * Comments in the code describe the (tricky!) matching criteria.
3237 * Although this routine could handle the initiator case,
3238 * it isn't currently called in this case.
3239 * If it were, it could "upgrade" an Opportunistic Connection
3240 * to a Road Warrior Connection if a suitable Peer ID were found.
3242 * In RFC 2409 "The Internet Key Exchange (IKE)",
3243 * in 5.1 "IKE Phase 1 Authenticated With Signatures", describing Main
3246 * Initiator Responder
3247 * ----------- -----------
3252 * HDR*, IDii, [ CERT, ] SIG_I -->
3253 * <-- HDR*, IDir, [ CERT, ] SIG_R
3255 * In 5.4 "Phase 1 Authenticated With a Pre-Shared Key":
3261 * HDR*, IDii, HASH_I -->
3262 * <-- HDR*, IDir, HASH_R
3264 * refine_host_connection could be called in two case:
3266 * - the Responder receives the IDii payload:
3267 * + [PSK] after using PSK to decode this message
3268 * + before sending its IDir payload
3269 * + before using its ID in HASH_R computation
3270 * + [DSig] before using its private key to sign SIG_R
3271 * + before using the Initiator's ID in HASH_I calculation
3272 * + [DSig] before using the Initiator's public key to check SIG_I
3274 * - the Initiator receives the IDir payload:
3275 * + [PSK] after using PSK to encode previous message and decode this message
3276 * + after sending its IDii payload
3277 * + after using its ID in HASH_I computation
3278 * + [DSig] after using its private key to sign SIG_I
3279 * + before using the Responder's ID to compute HASH_R
3280 * + [DSig] before using Responder's public key to check SIG_R
3282 * refine_host_connection can choose a different connection, as long as
3283 * nothing already used is changed.
3285 * In the Initiator case, the particular connection might have been
3286 * specified by whatever provoked Pluto to initiate. For example:
3287 * whack --initiate connection-name
3288 * The advantages of switching connections when we're the Initiator seem
3289 * less important than the disadvantages, so after FreeS/WAN 1.9, we
3293 refine_host_connection(const struct state
*st
, const struct id
*peer_id
3296 struct connection
*c
= st
->st_connection
;
3297 u_int16_t auth
= st
->st_oakley
.auth
;
3298 struct connection
*d
;
3299 struct connection
*best_found
= NULL
;
3301 const chunk_t
*psk
= NULL
;
3302 bool wcpip
; /* wildcard Peer IP? */
3304 int wildcards
, our_pathlen
, peer_pathlen
;
3305 int best_wildcards
= MAX_WILDCARDS
;
3306 int best_our_pathlen
= MAX_CA_PATH_LEN
;
3307 int best_peer_pathlen
= MAX_CA_PATH_LEN
;
3309 if (same_id(&c
->spd
.that
.id
, peer_id
)
3310 && trusted_ca(peer_ca
, c
->spd
.that
.ca
, &peer_pathlen
)
3311 && peer_pathlen
== 0
3312 && match_requested_ca(c
->requested_ca
, c
->spd
.this.ca
, &our_pathlen
)
3313 && our_pathlen
== 0)
3316 DBG_log("current connection is a full match"
3317 " -- no need to look further");
3324 case OAKLEY_PRESHARED_KEY
:
3325 auth_policy
= POLICY_PSK
;
3326 psk
= get_preshared_secret(c
);
3327 /* It should be virtually impossible to fail to find PSK:
3328 * we just used it to decode the current message!
3331 return NULL
; /* cannot determine PSK! */
3333 case XAUTHInitPreShared
:
3334 case XAUTHRespPreShared
:
3335 auth_policy
= POLICY_XAUTH_PSK
;
3336 psk
= get_preshared_secret(c
);
3338 return NULL
; /* cannot determine PSK! */
3340 case OAKLEY_RSA_SIG
:
3341 auth_policy
= POLICY_RSASIG
;
3345 auth_policy
= POLICY_XAUTH_RSASIG
;
3351 /* The current connection won't do: search for one that will.
3352 * First search for one with the same pair of hosts.
3353 * If that fails, search for a suitable Road Warrior or Opportunistic
3354 * connection (i.e. wildcard peer IP).
3356 * - peer_id (slightly complicated by instantiation)
3357 * - if PSK auth, the key must not change (we used it to decode message)
3358 * - policy-as-used must be acceptable to new connection
3360 d
= c
->host_pair
->connections
;
3361 for (wcpip
= FALSE
; ; wcpip
= TRUE
)
3363 for (; d
!= NULL
; d
= d
->hp_next
)
3365 const char *match_name
[] = {"no", "ok"};
3367 bool matching_id
= match_id(peer_id
3368 , &d
->spd
.that
.id
, &wildcards
);
3369 bool matching_auth
= (d
->policy
& auth_policy
) != LEMPTY
;
3371 bool matching_trust
= trusted_ca(peer_ca
3372 , d
->spd
.that
.ca
, &peer_pathlen
);
3373 bool matching_request
= match_requested_ca(c
->requested_ca
3374 , d
->spd
.this.ca
, &our_pathlen
);
3375 bool match
= matching_id
&& matching_auth
&&
3376 matching_trust
&& matching_request
;
3378 DBG(DBG_CONTROLMORE
,
3379 DBG_log("%s: %s match (id: %s, auth: %s, trust: %s, request: %s)"
3381 , match ?
"full":" no"
3382 , match_name
[matching_id
]
3383 , match_name
[matching_auth
]
3384 , match_name
[matching_trust
]
3385 , match_name
[matching_request
])
3388 /* do we have a match? */
3392 /* ignore group connections */
3393 if (d
->policy
& POLICY_GROUP
)
3396 if (c
->spd
.that
.host_port
!= d
->spd
.that
.host_port
3397 && d
->kind
== CK_INSTANCE
)
3404 case OAKLEY_PRESHARED_KEY
:
3405 case XAUTHInitPreShared
:
3406 case XAUTHRespPreShared
:
3407 /* secret must match the one we already used */
3409 const chunk_t
*dpsk
= get_preshared_secret(d
);
3412 continue; /* no secret */
3415 if (psk
->len
!= dpsk
->len
3416 || memcmp(psk
->ptr
, dpsk
->ptr
, psk
->len
) != 0)
3417 continue; /* different secret */
3421 case OAKLEY_RSA_SIG
:
3425 * We must at least be able to find our private key
3427 if (d
->spd
.this.sc
== NULL
/* no smartcard */
3428 && get_RSA_private_key(d
) == NULL
) /* no private key */
3436 /* d has passed all the tests.
3437 * We'll go with it if the Peer ID was an exact match.
3439 if (match
&& wildcards
== 0 && peer_pathlen
== 0 && our_pathlen
== 0)
3442 /* We'll remember it as best_found in case an exact
3443 * match doesn't come along.
3445 if (best_found
== NULL
|| wildcards
< best_wildcards
3446 || ((wildcards
== best_wildcards
&& peer_pathlen
< best_peer_pathlen
)
3447 || (peer_pathlen
== best_peer_pathlen
&& our_pathlen
< best_our_pathlen
)))
3450 best_wildcards
= wildcards
;
3451 best_peer_pathlen
= peer_pathlen
;
3452 best_our_pathlen
= our_pathlen
;
3456 return best_found
; /* been around twice already */
3458 /* Starting second time around.
3459 * We're willing to settle for a connection that needs Peer IP
3460 * instantiated: Road Warrior or Opportunistic.
3461 * Look on list of connections for host pair with wildcard Peer IP
3463 d
= find_host_pair_connections(&c
->spd
.this.host_addr
, c
->spd
.this.host_port
3464 , (ip_address
*)NULL
, c
->spd
.that
.host_port
);
3469 * With virtual addressing, we must not allow someone to use an already
3470 * used (by another id) addr/net.
3473 is_virtual_net_used(const ip_subnet
*peer_net
, const struct id
*peer_id
)
3475 struct connection
*d
;
3477 for (d
= connections
; d
!= NULL
; d
= d
->ac_next
)
3483 if ((subnetinsubnet(peer_net
,&d
->spd
.that
.client
) ||
3484 subnetinsubnet(&d
->spd
.that
.client
,peer_net
))
3485 && !same_id(&d
->spd
.that
.id
, peer_id
))
3488 char client
[SUBNETTOT_BUF
];
3490 subnettot(peer_net
, 0, client
, sizeof(client
));
3491 idtoa(&d
->spd
.that
.id
, buf
, sizeof(buf
));
3492 plog("Virtual IP %s is already used by '%s'", client
, buf
);
3493 idtoa(peer_id
, buf
, sizeof(buf
));
3494 plog("Your ID is '%s'", buf
);
3495 return TRUE
; /* already used by another one */
3503 return FALSE
; /* you can safely use it */
3506 /* find_client_connection: given a connection suitable for ISAKMP
3507 * (i.e. the hosts match), find a one suitable for IPSEC
3508 * (i.e. with matching clients).
3510 * If we don't find an exact match (not even our current connection),
3511 * we try for one that still needs instantiation. Try Road Warrior
3512 * abstract connections and the Opportunistic abstract connections.
3513 * This requires inverse instantiation: abstraction.
3515 * After failing to find an exact match, we abstract the peer
3516 * to be NO_IP (the wildcard value). This enables matches with
3517 * Road Warrior and Opportunistic abstract connections.
3519 * After failing that search, we also abstract the Phase 1 peer ID
3520 * if possible. If the peer's ID was the peer's IP address, we make
3521 * it NO_ID; instantiation will make it the peer's IP address again.
3523 * If searching for a Road Warrior abstract connection fails,
3524 * and conditions are suitable, we search for the best Opportunistic
3525 * abstract connection.
3527 * Note: in the end, both Phase 1 IDs must be preserved, after any
3528 * instantiation. They are the IDs that have been authenticated.
3531 #define PATH_WEIGHT 1
3532 #define WILD_WEIGHT (MAX_CA_PATH_LEN+1)
3533 #define PRIO_WEIGHT (MAX_WILDCARDS+1)*WILD_WEIGHT
3535 /* fc_try: a helper function for find_client_connection */
3536 static struct connection
*
3537 fc_try(const struct connection
*c
3538 , struct host_pair
*hp
3539 , const struct id
*peer_id
3540 , const ip_subnet
*our_net
3541 , const ip_subnet
*peer_net
3542 , const u_int8_t our_protocol
3543 , const u_int16_t our_port
3544 , const u_int8_t peer_protocol
3545 , const u_int16_t peer_port
3547 , const ietfAttrList_t
*peer_list
)
3549 struct connection
*d
;
3550 struct connection
*best
= NULL
;
3551 policy_prio_t best_prio
= BOTTOM_PRIO
;
3552 int wildcards
, pathlen
;
3554 const bool peer_net_is_host
= subnetisaddr(peer_net
, &c
->spd
.that
.host_addr
);
3556 for (d
= hp
->connections
; d
!= NULL
; d
= d
->hp_next
)
3558 struct spd_route
*sr
;
3560 if (d
->policy
& POLICY_GROUP
)
3563 if (!(same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
3564 && match_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
, &wildcards
)
3565 && trusted_ca(peer_ca
, d
->spd
.that
.ca
, &pathlen
)
3566 && group_membership(peer_list
, d
->name
, d
->spd
.that
.groups
)))
3569 /* compare protocol and ports */
3570 if (d
->spd
.this.protocol
!= our_protocol
3571 || d
->spd
.this.port
!= our_port
3572 || d
->spd
.that
.protocol
!= peer_protocol
3573 || (d
->spd
.that
.port
!= peer_port
&& !d
->spd
.that
.has_port_wildcard
))
3576 /* non-Opportunistic case:
3577 * our_client must match.
3579 * So must peer_client, but the testing is complicated
3580 * by the fact that the peer might be a wildcard
3581 * and if so, the default value of that.client
3582 * won't match the default peer_net. The appropriate test:
3584 * If d has a peer client, it must match peer_net.
3585 * If d has no peer client, peer_net must just have peer itself.
3588 for (sr
= &d
->spd
; best
!= d
&& sr
!= NULL
; sr
= sr
->next
)
3592 if (DBGP(DBG_CONTROLMORE
))
3594 char s1
[SUBNETTOT_BUF
],d1
[SUBNETTOT_BUF
];
3595 char s3
[SUBNETTOT_BUF
],d3
[SUBNETTOT_BUF
];
3597 subnettot(our_net
, 0, s1
, sizeof(s1
));
3598 subnettot(peer_net
, 0, d1
, sizeof(d1
));
3599 subnettot(&sr
->this.client
, 0, s3
, sizeof(s3
));
3600 subnettot(&sr
->that
.client
, 0, d3
, sizeof(d3
));
3601 DBG_log(" fc_try trying "
3602 "%s:%s:%d/%d -> %s:%d/%d vs %s:%s:%d/%d -> %s:%d/%d"
3603 , c
->name
, s1
, c
->spd
.this.protocol
, c
->spd
.this.port
3604 , d1
, c
->spd
.that
.protocol
, c
->spd
.that
.port
3605 , d
->name
, s3
, sr
->this.protocol
, sr
->this.port
3606 , d3
, sr
->that
.protocol
, sr
->that
.port
);
3610 if (!samesubnet(&sr
->this.client
, our_net
))
3613 if (sr
->that
.has_client
)
3615 if (sr
->that
.has_client_wildcard
)
3617 if (!subnetinsubnet(peer_net
, &sr
->that
.client
))
3622 if (!samesubnet(&sr
->that
.client
, peer_net
) && !is_virtual_connection(d
))
3624 if (is_virtual_connection(d
)
3625 && (!is_virtual_net_allowed(d
, peer_net
, &c
->spd
.that
.host_addr
)
3626 || is_virtual_net_used(peer_net
, peer_id?peer_id
:&c
->spd
.that
.id
)))
3632 if (!peer_net_is_host
)
3636 /* We've run the gauntlet -- success:
3637 * We've got an exact match of subnets.
3638 * The connection is feasible, but we continue looking for the best.
3639 * The highest priority wins, implementing eroute-like rule.
3640 * - a routed connection is preferrred
3641 * - given that, the smallest number of ID wildcards are preferred
3642 * - given that, the shortest CA pathlength is preferred
3644 prio
= PRIO_WEIGHT
* routed(sr
->routing
)
3645 + WILD_WEIGHT
* (MAX_WILDCARDS
- wildcards
)
3646 + PATH_WEIGHT
* (MAX_CA_PATH_LEN
- pathlen
)
3648 if (prio
> best_prio
)
3656 if (best
!= NULL
&& NEVER_NEGOTIATE(best
->policy
))
3659 DBG(DBG_CONTROLMORE
,
3660 DBG_log(" fc_try concluding with %s [%ld]"
3661 , (best ? best
->name
: "none"), best_prio
)
3666 static struct connection
*
3667 fc_try_oppo(const struct connection
*c
3668 , struct host_pair
*hp
3669 , const ip_subnet
*our_net
3670 , const ip_subnet
*peer_net
3671 , const u_int8_t our_protocol
3672 , const u_int16_t our_port
3673 , const u_int8_t peer_protocol
3674 , const u_int16_t peer_port
3676 , const ietfAttrList_t
*peer_list
)
3678 struct connection
*d
;
3679 struct connection
*best
= NULL
;
3680 policy_prio_t best_prio
= BOTTOM_PRIO
;
3681 int wildcards
, pathlen
;
3683 for (d
= hp
->connections
; d
!= NULL
; d
= d
->hp_next
)
3685 struct spd_route
*sr
;
3688 if (d
->policy
& POLICY_GROUP
)
3691 if (!(same_id(&c
->spd
.this.id
, &d
->spd
.this.id
)
3692 && match_id(&c
->spd
.that
.id
, &d
->spd
.that
.id
, &wildcards
)
3693 && trusted_ca(peer_ca
, d
->spd
.that
.ca
, &pathlen
)
3694 && group_membership(peer_list
, d
->name
, d
->spd
.that
.groups
)))
3697 /* compare protocol and ports */
3698 if (d
->spd
.this.protocol
!= our_protocol
3699 || d
->spd
.this.port
!= our_port
3700 || d
->spd
.that
.protocol
!= peer_protocol
3701 || (d
->spd
.that
.port
!= peer_port
&& !d
->spd
.that
.has_port_wildcard
))
3704 /* Opportunistic case:
3705 * our_net must be inside d->spd.this.client
3706 * and peer_net must be inside d->spd.that.client
3707 * Note: this host_pair chain also has shunt
3708 * eroute conns (clear, drop), but they won't
3709 * be marked as opportunistic.
3711 for (sr
= &d
->spd
; sr
!= NULL
; sr
= sr
->next
)
3714 if (DBGP(DBG_CONTROLMORE
))
3716 char s1
[SUBNETTOT_BUF
],d1
[SUBNETTOT_BUF
];
3717 char s3
[SUBNETTOT_BUF
],d3
[SUBNETTOT_BUF
];
3719 subnettot(our_net
, 0, s1
, sizeof(s1
));
3720 subnettot(peer_net
, 0, d1
,