1 /* $NetBSD: remoteconf.c,v 1.9.4.1 2007/08/01 11:52:22 vanhu Exp $ */
3 /* Id: remoteconf.c,v 1.38 2006/05/06 15:52:44 manubsd Exp */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
45 #ifndef HAVE_NETINET6_IPSEC
46 #include <netinet/ipsec.h>
48 #include <netinet6/ipsec.h>
64 #include "isakmp_var.h"
66 #include "isakmp_xauth.h"
69 #include "ipsec_doi.h"
71 #include "remoteconf.h"
72 #include "localconf.h"
73 #include "grabmyaddr.h"
79 #include "algorithm.h"
80 #include "nattraversal.h"
81 #include "isakmp_frag.h"
85 static TAILQ_HEAD(_rmtree
, remoteconf
) rmtree
;
88 * Script hook names and script hook paths
90 char *script_names
[SCRIPT_MAX
+ 1] = { "phase1_up", "phase1_down" };
94 * search remote configuration.
95 * don't use port number to search if its value is either IPSEC_PORT_ANY.
96 * If matching anonymous entry, then new entry is copied from anonymous entry.
97 * If no anonymous entry found, then return NULL.
99 * Other: remote configuration entry.
102 getrmconf_strict(remote
, allow_anon
)
103 struct sockaddr
*remote
;
106 struct remoteconf
*p
;
107 struct remoteconf
*p_withport_besteffort
= NULL
;
108 struct remoteconf
*anon
= NULL
;
110 char buf
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
111 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
116 * We never have ports set in our remote configurations, but when
117 * NAT-T is enabled, the kernel can have policies with ports and
118 * send us an acquire message for a destination that has a port set.
119 * If we do this port check here, we have to fallback to a best-effort result (without the port).
121 * In an ideal world, we would be able to have remote conf with
122 * port, and the port could be a wildcard. That test could be used.
124 switch (remote
->sa_family
) {
126 if (((struct sockaddr_in
*)remote
)->sin_port
!= IPSEC_PORT_ANY
)
131 if (((struct sockaddr_in6
*)remote
)->sin6_port
!= IPSEC_PORT_ANY
)
139 plog(LLV_ERROR2
, LOCATION
, NULL
,
140 "invalid ip address family: %d\n", remote
->sa_family
);
144 if (remote
->sa_family
== AF_UNSPEC
)
145 snprintf (buf
, sizeof(buf
), "%s", "anonymous");
147 GETNAMEINFO(remote
, addr
, port
);
148 snprintf(buf
, sizeof(buf
), "%s%s%s%s", addr
,
150 withport
? port
: "",
151 withport
? "]" : "");
154 TAILQ_FOREACH(p
, &rmtree
, chain
) {
156 if (p
->to_delete
|| p
->to_remove
) {
160 if ((remote
->sa_family
== AF_UNSPEC
161 && remote
->sa_family
== p
->remote
->sa_family
)
162 || (!withport
&& cmpsaddrwop(remote
, p
->remote
) == 0)
163 || (withport
&& cmpsaddrstrict(remote
, p
->remote
) == 0)) {
164 plog(LLV_DEBUG
, LOCATION
, NULL
,
165 "configuration found for %s.\n", buf
);
167 } else if (withport
&& cmpsaddrwop(remote
, p
->remote
) == 0) {
168 // for withport: save the pointer for the best-effort search
169 p_withport_besteffort
= p
;
172 /* save the pointer to the anonymous configuration */
173 if (p
->remote
->sa_family
== AF_UNSPEC
)
177 if (p_withport_besteffort
) {
178 plog(LLV_DEBUG
, LOCATION
, NULL
,
179 "configuration found for %s.\n", buf
);
180 return p_withport_besteffort
;
183 if (allow_anon
&& anon
!= NULL
) {
184 plog(LLV_DEBUG
, LOCATION
, NULL
,
185 "anonymous configuration selected for %s.\n", buf
);
189 plog(LLV_DEBUG
, LOCATION
, NULL
,
190 "no remote configuration found.\n");
199 struct remoteconf
*p
;
201 TAILQ_FOREACH(p
, &rmtree
, chain
) {
202 if (p
->remote
->sa_family
== AF_UNSPEC
) /* anonymous */
211 struct sockaddr
*remote
;
213 return getrmconf_strict(remote
, 1);
218 link_rmconf_to_ph1 (struct remoteconf
*new)
223 if (new->to_delete
||
227 new->linked_to_ph1
++;
232 unlink_rmconf_from_ph1 (struct remoteconf
*old
)
237 if (old
->linked_to_ph1
<= 0) {
240 old
->linked_to_ph1
--;
241 if (old
->linked_to_ph1
== 0) {
242 if (old
->to_remove
) {
245 if (old
->to_delete
) {
256 struct remoteconf
*new;
259 new = racoon_calloc(1, sizeof(*new));
263 new->proposal
= NULL
;
266 new->doitype
= IPSEC_DOI
;
267 new->sittype
= IPSECDOI_SIT_IDENTITY_ONLY
;
268 new->idvtype
= IDTYPE_UNDEFINED
;
269 new->idvl_p
= genlist_init();
270 new->nonce_size
= DEFAULT_NONCE_SIZE
;
271 new->passive
= FALSE
;
272 new->ike_frag
= FALSE
;
273 new->esp_frag
= IP_MAXPACKET
;
274 new->ini_contact
= TRUE
;
275 new->mode_cfg
= FALSE
;
276 new->pcheck_level
= PROP_CHECK_STRICT
;
277 new->verify_identifier
= FALSE
;
278 new->verify_cert
= TRUE
;
279 new->getcert_method
= ISAKMP_GETCERT_PAYLOAD
;
280 new->getcacert_method
= ISAKMP_GETCERT_LOCALFILE
;
281 new->cacerttype
= ISAKMP_CERT_X509SIGN
;
282 new->certtype
= ISAKMP_CERT_NONE
;
283 new->cacertfile
= NULL
;
284 new->send_cert
= TRUE
;
286 new->support_proxy
= FALSE
;
287 for (i
= 0; i
<= SCRIPT_MAX
; i
++)
288 new->script
[i
] = NULL
;
289 new->gen_policy
= FALSE
;
290 new->retry_counter
= lcconf
->retry_counter
;
291 new->retry_interval
= lcconf
->retry_interval
;
293 new->nat_traversal
= NATT_ON
;
294 new->natt_multiple_user
= FALSE
;
295 new->natt_keepalive
= TRUE
;
296 new->to_remove
= FALSE
;
297 new->to_delete
= FALSE
;
298 new->linked_to_ph1
= 0;
300 new->nat_traversal
= NATT_OFF
;
302 new->rsa_private
= genlist_init();
303 new->rsa_public
= genlist_init();
307 new->dpd
= TRUE
; /* Enable DPD support by default */
308 new->dpd_interval
= 0; /* Disable DPD checks by default */
310 new->dpd_maxfails
= 5;
311 new->dpd_algo
= DPD_ALGO_INBOUND_DETECT
;
312 new->idle_timeout
= 0;
314 new->weak_phase1_check
= 0;
319 new->initiate_ph1rekey
= TRUE
;
325 struct sockaddr
*remote
;
327 struct remoteconf
*new, *old
;
329 old
= getrmconf_strict (remote
, 0);
331 plog (LLV_ERROR
, LOCATION
, NULL
,
332 "Remote configuration for '%s' not found!\n",
337 new = duprmconf (old
);
348 struct idspec
*old
= (struct idspec
*) entry
;
350 if (!id
) return (void *) -1;
352 if (set_identifier(&id
->id
, old
->idtype
, old
->id
) != 0) {
357 id
->idtype
= old
->idtype
;
359 genlist_append(arg
, id
);
365 struct remoteconf
*rmconf
;
367 struct remoteconf
*new;
369 new = racoon_calloc(1, sizeof(*new));
372 memcpy (new, rmconf
, sizeof (*new));
373 // FIXME: We should duplicate the proposal as well.
374 // This is now handled in the cfparse.y
375 // new->proposal = ...;
377 /* duplicate dynamic structures */
379 new->etypes
=dupetypes(new->etypes
);
380 new->idvl_p
= genlist_init();
381 genlist_foreach(rmconf
->idvl_p
, dupidvl
, new->idvl_p
);
387 idspec_free(void *data
)
389 vfree (((struct idspec
*)data
)->id
);
394 proposalspec_free(struct proposalspec
*head
)
397 struct proposalspec
* next_propsp
= head
;
399 while (next_propsp
) {
400 struct proposalspec
* curr_propsp
;
401 struct secprotospec
* next_protosp
;
403 curr_propsp
= next_propsp
;
404 next_propsp
= next_propsp
->next
;
405 next_protosp
= curr_propsp
->spspec
;
406 while (next_protosp
) {
407 struct secprotospec
* curr_protosp
;
409 curr_protosp
= next_protosp
;
410 next_protosp
= next_protosp
->next
;
412 if (curr_protosp
->gssid
)
413 free(curr_protosp
->gssid
);
414 if (curr_protosp
->remote
)
415 free(curr_protosp
->remote
);
416 racoon_free(curr_protosp
);
418 racoon_free(curr_propsp
);
424 struct remoteconf
*rmconf
;
427 if (rmconf
->linked_to_ph1
) {
428 rmconf
->to_delete
= TRUE
;
433 racoon_free(rmconf
->remote
);
436 xauth_rmconf_delete(&rmconf
->xauth
);
438 if (rmconf
->etypes
) {
439 deletypes(rmconf
->etypes
);
445 genlist_free(rmconf
->idvl_p
, idspec_free
);
447 oakley_dhgrp_free(rmconf
->dhgrp
);
448 if (rmconf
->proposal
)
449 delisakmpsa(rmconf
->proposal
);
450 if (rmconf
->mycertfile
)
451 racoon_free(rmconf
->mycertfile
);
452 if (rmconf
->myprivfile
)
453 racoon_free(rmconf
->myprivfile
);
454 if (rmconf
->peerscertfile
)
455 racoon_free(rmconf
->peerscertfile
);
456 if (rmconf
->cacertfile
)
457 racoon_free(rmconf
->cacertfile
);
459 proposalspec_free(rmconf
->prhead
);
460 if (rmconf
->rsa_private
)
461 genlist_free(rmconf
->rsa_private
, rsa_key_free
);
462 if (rmconf
->rsa_public
)
463 genlist_free(rmconf
->rsa_public
, rsa_key_free
);
465 if (rmconf
->shared_secret
)
466 vfree(rmconf
->shared_secret
);
467 if (rmconf
->keychainCertRef
)
468 vfree(rmconf
->keychainCertRef
);
469 if (rmconf
->open_dir_auth_group
)
470 vfree(rmconf
->open_dir_auth_group
);
481 oakley_dhgrp_free(sa
->dhgrp
);
483 delisakmpsa(sa
->next
);
500 new = racoon_malloc(sizeof(struct etypes
));
504 new->type
= orig
->type
;
508 new->next
=dupetypes(orig
->next
);
523 * insert into head of list.
527 struct remoteconf
*new;
529 TAILQ_INSERT_HEAD(&rmtree
, new, chain
);
534 struct remoteconf
*rmconf
;
537 if (rmconf
->linked_to_ph1
) {
538 rmconf
->to_remove
= TRUE
;
542 TAILQ_REMOVE(&rmtree
, rmconf
, chain
);
548 struct remoteconf
*p
, *next
;
550 for (p
= TAILQ_FIRST(&rmtree
); p
; p
= next
) {
551 next
= TAILQ_NEXT(p
, chain
);
563 /* check exchange type to be acceptable */
565 check_etypeok(rmconf
, etype
)
566 struct remoteconf
*rmconf
;
571 for (e
= rmconf
->etypes
; e
!= NULL
; e
= e
->next
) {
572 if (e
->type
== etype
)
583 struct isakmpsa
*new;
585 new = racoon_calloc(1, sizeof(*new));
590 * Just for sanity, make sure this is initialized. This is
591 * filled in for real when the ISAKMP proposal is configured.
593 new->vendorid
= VENDORID_UNKNOWN
;
605 * insert into tail of list.
608 insisakmpsa(new, rmconf
)
609 struct isakmpsa
*new;
610 struct remoteconf
*rmconf
;
614 new->rmconf
= rmconf
;
616 if (rmconf
->proposal
== NULL
) {
617 rmconf
->proposal
= new;
621 for (p
= rmconf
->proposal
; p
->next
!= NULL
; p
= p
->next
)
629 foreachrmconf(rmconf_func_t rmconf_func
, void *data
)
631 struct remoteconf
*p
, *ret
= NULL
;
633 TAILQ_FOREACH_REVERSE(p
, &rmtree
, _rmtree
, chain
) {
634 ret
= (*rmconf_func
)(p
, data
);
643 dump_peers_identifiers (void *entry
, void *arg
)
645 struct idspec
*id
= (struct idspec
*) entry
;
646 char buf
[1024], *pbuf
;
648 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), "\tpeers_identifier %s",
649 s_idtype (id
->idtype
));
651 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), " \"%s\"", id
->id
->v
);
652 plog(LLV_INFO
, LOCATION
, NULL
, "%s;\n", buf
);
656 static struct remoteconf
*
657 dump_rmconf_single (struct remoteconf
*p
, void *data
)
659 struct etypes
*etype
= p
->etypes
;
660 struct isakmpsa
*prop
= p
->proposal
;
661 char buf
[1024], *pbuf
;
664 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), "remote %s", saddr2str(p
->remote
));
665 if (p
->inherited_from
)
666 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), " inherit %s",
667 saddr2str(p
->inherited_from
->remote
));
668 plog(LLV_INFO
, LOCATION
, NULL
, "%s {\n", buf
);
670 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), "\texchange_type ");
672 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), "%s%s", s_etype(etype
->type
),
673 etype
->next
!= NULL
? ", " : ";\n");
676 plog(LLV_INFO
, LOCATION
, NULL
, "%s", buf
);
677 plog(LLV_INFO
, LOCATION
, NULL
, "\tdoi %s;\n", s_doi(p
->doitype
));
679 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), "\tmy_identifier %s", s_idtype (p
->idvtype
));
680 if (p
->idvtype
== IDTYPE_ASN1DN
) {
681 plog(LLV_INFO
, LOCATION
, NULL
, "%s;\n", buf
);
682 plog(LLV_INFO
, LOCATION
, NULL
, "\tcertificate_type %s \"%s\" \"%s\";\n",
683 p
->certtype
== ISAKMP_CERT_X509SIGN
? "x509" : "*UNKNOWN*",
684 p
->mycertfile
, p
->myprivfile
);
685 switch (p
->getcert_method
) {
688 case ISAKMP_GETCERT_PAYLOAD
:
689 plog(LLV_INFO
, LOCATION
, NULL
, "\t/* peers certificate from payload */\n");
691 case ISAKMP_GETCERT_LOCALFILE
:
692 plog(LLV_INFO
, LOCATION
, NULL
, "\tpeers_certfile \"%s\";\n", p
->peerscertfile
);
694 case ISAKMP_GETCERT_DNS
:
695 plog(LLV_INFO
, LOCATION
, NULL
, "\tpeer_certfile dnssec;\n");
698 plog(LLV_INFO
, LOCATION
, NULL
, "\tpeers_certfile *UNKNOWN* (%d)\n", p
->getcert_method
);
703 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), " \"%s\"", p
->idv
->v
);
704 plog(LLV_INFO
, LOCATION
, NULL
, "%s;\n", buf
);
705 genlist_foreach(p
->idvl_p
, &dump_peers_identifiers
, NULL
);
708 plog(LLV_INFO
, LOCATION
, NULL
, "\tsend_cert %s;\n",
709 s_switch (p
->send_cert
));
710 plog(LLV_INFO
, LOCATION
, NULL
, "\tsend_cr %s;\n",
711 s_switch (p
->send_cr
));
712 plog(LLV_INFO
, LOCATION
, NULL
, "\tverify_cert %s;\n",
713 s_switch (p
->verify_cert
));
714 plog(LLV_INFO
, LOCATION
, NULL
, "\tverify_identifier %s;\n",
715 s_switch (p
->verify_identifier
));
716 plog(LLV_INFO
, LOCATION
, NULL
, "\tnat_traversal %s;\n",
717 p
->nat_traversal
== NATT_FORCE
?
718 "force" : s_switch (p
->nat_traversal
));
720 plog(LLV_INFO
, LOCATION
, NULL
, "\tnatt_multiple_user %s;\n",
721 s_switch (p
->natt_multiple_user
));
723 plog(LLV_INFO
, LOCATION
, NULL
, "\tnonce_size %d;\n",
725 plog(LLV_INFO
, LOCATION
, NULL
, "\tpassive %s;\n",
726 s_switch (p
->passive
));
727 plog(LLV_INFO
, LOCATION
, NULL
, "\tike_frag %s;\n",
728 p
->ike_frag
== ISAKMP_FRAG_FORCE
?
729 "force" : s_switch (p
->ike_frag
));
730 plog(LLV_INFO
, LOCATION
, NULL
, "\tesp_frag %d;\n", p
->esp_frag
);
731 plog(LLV_INFO
, LOCATION
, NULL
, "\tinitial_contact %s;\n",
732 s_switch (p
->ini_contact
));
733 plog(LLV_INFO
, LOCATION
, NULL
, "\tgenerate_policy %s;\n",
734 s_switch (p
->gen_policy
));
735 plog(LLV_INFO
, LOCATION
, NULL
, "\tsupport_proxy %s;\n",
736 s_switch (p
->support_proxy
));
739 plog(LLV_INFO
, LOCATION
, NULL
, "\n");
740 plog(LLV_INFO
, LOCATION
, NULL
,
741 "\t/* prop_no=%d, trns_no=%d, rmconf=%s */\n",
742 prop
->prop_no
, prop
->trns_no
,
743 saddr2str(prop
->rmconf
->remote
));
744 plog(LLV_INFO
, LOCATION
, NULL
, "\tproposal {\n");
745 plog(LLV_INFO
, LOCATION
, NULL
, "\t\tlifetime time %lu sec;\n",
746 (long)prop
->lifetime
);
747 plog(LLV_INFO
, LOCATION
, NULL
, "\t\tlifetime bytes %zd;\n",
749 plog(LLV_INFO
, LOCATION
, NULL
, "\t\tdh_group %s;\n",
750 alg_oakley_dhdef_name(prop
->dh_group
));
751 plog(LLV_INFO
, LOCATION
, NULL
, "\t\tencryption_algorithm %s;\n",
752 alg_oakley_encdef_name(prop
->enctype
));
753 plog(LLV_INFO
, LOCATION
, NULL
, "\t\thash_algorithm %s;\n",
754 alg_oakley_hashdef_name(prop
->hashtype
));
755 plog(LLV_INFO
, LOCATION
, NULL
, "\t\tauthentication_method %s;\n",
756 alg_oakley_authdef_name(prop
->authmethod
));
757 plog(LLV_INFO
, LOCATION
, NULL
, "\t}\n");
760 plog(LLV_INFO
, LOCATION
, NULL
, "}\n");
761 plog(LLV_INFO
, LOCATION
, NULL
, "\n");
769 foreachrmconf (dump_rmconf_single
, NULL
);
777 new = racoon_calloc(1, sizeof(*new));
780 new->idtype
= IDTYPE_ADDRESS
;
786 script_path_add(path
)
791 vchar_t
*new_storage
;
796 script_dir
= lcconf
->pathinfo
[LC_PATHTYPE_SCRIPT
];
798 /* Try to find the script in the script directory */
799 if ((path
->v
[0] != '/') && (script_dir
!= NULL
)) {
800 len
= strlen(script_dir
) + sizeof("/") + path
->l
+ 1;
802 if ((new_path
= vmalloc(len
)) == NULL
) {
803 plog(LLV_ERROR
, LOCATION
, NULL
,
804 "Cannot allocate memory: %s\n", strerror(errno
));
808 new_path
->v
[0] = '\0';
809 (void)strlcat(new_path
->v
, script_dir
, new_path
->l
);
810 (void)strlcat(new_path
->v
, "/", new_path
->l
);
811 (void)strlcat(new_path
->v
, path
->v
, new_path
->l
);
822 dupisakmpsa(struct isakmpsa
*sa
)
824 struct isakmpsa
*res
= NULL
;
835 res
->gssid
=vdup(sa
->gssid
);
839 if (sa
->dhgrp
!= NULL
)
840 oakley_setdhgroup(sa
->dh_group
, &(res
->dhgrp
));
847 rsa_key_free(void *entry
)
849 struct rsa_key
*key
= (struct rsa_key
*)entry
;