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"
84 static TAILQ_HEAD(_rmtree
, remoteconf
) rmtree
;
89 * search remote configuration.
90 * don't use port number to search if its value is either IPSEC_PORT_ANY.
91 * If matching anonymous entry, then new entry is copied from anonymous entry.
92 * If no anonymous entry found, then return NULL.
94 * Other: remote configuration entry.
97 getrmconf_strict(remote
, allow_anon
)
98 struct sockaddr_storage
*remote
;
101 struct remoteconf
*p
;
102 struct remoteconf
*p_withport_besteffort
= NULL
;
103 struct remoteconf
*p_with_prefix
= NULL
;
104 struct remoteconf
*p_with_prefix_besteffort
= NULL
;
106 struct remoteconf
*anon
= NULL
;
109 char buf
[NI_MAXHOST
+ NI_MAXSERV
+ 10];
110 char addr
[NI_MAXHOST
], port
[NI_MAXSERV
];
115 * We never have ports set in our remote configurations, but when
116 * NAT-T is enabled, the kernel can have policies with ports and
117 * send us an acquire message for a destination that has a port set.
118 * If we do this port check here, we have to fallback to a best-effort result (without the port).
120 * In an ideal world, we would be able to have remote conf with
121 * port, and the port could be a wildcard. That test could be used.
123 switch (remote
->ss_family
) {
125 if (((struct sockaddr_in
*)remote
)->sin_port
!= IPSEC_PORT_ANY
)
130 if (((struct sockaddr_in6
*)remote
)->sin6_port
!= IPSEC_PORT_ANY
)
139 "invalid ip address family: %d\n", remote
->ss_family
);
143 if (remote
->ss_family
== AF_UNSPEC
)
144 snprintf (buf
, sizeof(buf
), "%s", "anonymous");
146 GETNAMEINFO((struct sockaddr
*)remote
, addr
, port
);
147 snprintf(buf
, sizeof(buf
), "%s%s%s%s", addr
,
149 withport
? port
: "",
150 withport
? "]" : "");
153 TAILQ_FOREACH(p
, &rmtree
, chain
) {
154 if (remote
->ss_family
== AF_UNSPEC
155 && remote
->ss_family
== p
->remote
->ss_family
) {
156 plog(ASL_LEVEL_DEBUG
, "configuration found for %s.\n", buf
);
159 if (p
->remote_prefix
== 0) {
160 if ((!withport
&& cmpsaddrwop(remote
, p
->remote
) == 0)
161 || (withport
&& cmpsaddrstrict(remote
, p
->remote
) == 0)) {
162 plog(ASL_LEVEL_DEBUG
, "configuration found for %s.\n", buf
);
164 } else if (withport
&& cmpsaddrwop(remote
, p
->remote
) == 0) {
165 // for withport: save the pointer for the best-effort search
166 p_withport_besteffort
= p
;
169 if ((!withport
&& cmpsaddrwop_withprefix(remote
, p
->remote
, p
->remote_prefix
) == 0)
170 || (withport
&& cmpsaddrstrict_withprefix(remote
, p
->remote
, p
->remote_prefix
) == 0)) {
171 if (p
->remote_prefix
>= last_prefix
) {
173 last_prefix
= p
->remote_prefix
;
175 } else if (withport
&& cmpsaddrwop_withprefix(remote
, p
->remote
, p
->remote_prefix
) == 0) {
176 if (p
->remote_prefix
>= last_prefix
) {
177 p_with_prefix_besteffort
= p
;
178 last_prefix
= p
->remote_prefix
;
183 /* save the pointer to the anonymous configuration */
184 if (p
->remote
->ss_family
== AF_UNSPEC
)
188 if (p_withport_besteffort
) {
189 plog(ASL_LEVEL_DEBUG
, "configuration found for %s.\n", buf
);
190 return p_withport_besteffort
;
193 plog(ASL_LEVEL_DEBUG
, "configuration found for %s.\n", buf
);
194 return p_with_prefix
;
196 if (p_with_prefix_besteffort
) {
197 plog(ASL_LEVEL_DEBUG
, "configuration found for %s.\n", buf
);
198 return p_with_prefix_besteffort
;
200 if (allow_anon
&& anon
!= NULL
) {
201 plog(ASL_LEVEL_DEBUG
,
202 "anonymous configuration selected for %s.\n", buf
);
206 plog(ASL_LEVEL_DEBUG
,
207 "no remote configuration found.\n");
213 no_remote_configs(ignore_anonymous
)
214 int ignore_anonymous
;
217 struct remoteconf
*p
;
218 #if !TARGET_OS_EMBEDDED
219 static const char default_idv
[] = "macuser@localhost";
220 static const int default_idv_len
= sizeof(default_idv
) - 1;
223 TAILQ_FOREACH(p
, &rmtree
, chain
) {
224 if (ignore_anonymous
) {
225 if (p
->remote
->ss_family
== AF_UNSPEC
) /* anonymous */
228 #if !TARGET_OS_EMBEDDED
229 // ignore the default btmm ipv6 config thats always present in racoon.conf
230 if (p
->remote
->ss_family
== AF_INET6
&&
231 p
->idvtype
== IDTYPE_USERFQDN
&&
233 p
->idv
->l
== default_idv_len
&&
234 strncmp(p
->idv
->v
, default_idv
, p
->idv
->l
) == 0) {
245 struct sockaddr_storage
*remote
;
247 return getrmconf_strict(remote
, 1);
253 struct remoteconf
*new;
255 new = racoon_calloc(1, sizeof(*new));
261 new->proposal
= NULL
;
264 new->doitype
= IPSEC_DOI
;
265 new->sittype
= IPSECDOI_SIT_IDENTITY_ONLY
;
266 new->ike_version
= ISAKMP_VERSION_NUMBER_IKEV1
;
267 new->idvtype
= IDTYPE_UNDEFINED
;
268 new->idvl_p
= genlist_init();
269 new->nonce_size
= DEFAULT_NONCE_SIZE
;
270 new->passive
= FALSE
;
271 new->ike_frag
= ISAKMP_FRAG_ON
;
272 new->esp_frag
= IP_MAXPACKET
;
273 new->ini_contact
= TRUE
;
274 new->mode_cfg
= FALSE
;
275 new->pcheck_level
= PROP_CHECK_STRICT
;
276 new->verify_identifier
= FALSE
;
277 new->verify_cert
= TRUE
;
278 new->getcert_method
= ISAKMP_GETCERT_PAYLOAD
;
279 new->cacerttype
= ISAKMP_CERT_X509SIGN
;
280 new->certtype
= ISAKMP_CERT_NONE
;
281 new->send_cert
= TRUE
;
283 new->support_proxy
= FALSE
;
284 new->gen_policy
= FALSE
;
285 new->retry_counter
= lcconf
->retry_counter
;
286 new->retry_interval
= lcconf
->retry_interval
;
287 new->nat_traversal
= NATT_ON
;
288 new->natt_multiple_user
= FALSE
;
289 new->natt_keepalive
= TRUE
;
293 new->dpd
= TRUE
; /* Enable DPD support by default */
294 new->dpd_interval
= 0; /* Disable DPD checks by default */
296 new->dpd_maxfails
= 5;
297 new->dpd_algo
= DPD_ALGO_INBOUND_DETECT
;
298 new->idle_timeout
= 0;
300 new->weak_phase1_check
= 0;
305 new->initiate_ph1rekey
= TRUE
;
310 copyrmconf(struct sockaddr_storage
*remote
)
312 struct remoteconf
*new, *old
;
314 old
= getrmconf_strict (remote
, 0);
317 "Remote configuration for '%s' not found!\n",
318 saddr2str((struct sockaddr
*)remote
));
322 new = duprmconf (old
);
328 dupidvl(void *entry
, void *arg
)
331 struct idspec
*old
= (struct idspec
*) entry
;
333 if (!id
) return (void *) -1;
335 if (set_identifier(&id
->id
, old
->idtype
, old
->id
) != 0) {
340 id
->idtype
= old
->idtype
;
342 genlist_append(arg
, id
);
347 duprmconf (struct remoteconf
*rmconf
)
349 struct remoteconf
*new;
351 new = racoon_calloc(1, sizeof(*new));
354 memcpy (new, rmconf
, sizeof (*new));
355 // FIXME: We should duplicate remote, proposal, etc.
356 // This is now handled in the cfparse.y
357 // new->proposal = ...;
361 new->forced_local
= NULL
;
362 new->keychainCertRef
= NULL
; /* peristant keychain ref for cert */
363 new->shared_secret
= NULL
; /* shared secret */
364 new->open_dir_auth_group
= NULL
; /* group to be used to authorize user */
365 new->proposal
= NULL
;
374 /* duplicate dynamic structures */
376 new->etypes
=dupetypes(new->etypes
);
377 new->idvl_p
= genlist_init();
378 genlist_foreach(rmconf
->idvl_p
, dupidvl
, new->idvl_p
);
384 idspec_free(void *data
)
386 vfree (((struct idspec
*)data
)->id
);
391 proposalspec_free(struct proposalspec
*head
)
394 struct proposalspec
* next_propsp
= head
;
396 while (next_propsp
) {
397 struct proposalspec
* curr_propsp
;
398 struct secprotospec
* next_protosp
;
400 curr_propsp
= next_propsp
;
401 next_propsp
= next_propsp
->next
;
402 next_protosp
= curr_propsp
->spspec
;
403 while (next_protosp
) {
404 struct secprotospec
* curr_protosp
;
406 curr_protosp
= next_protosp
;
407 next_protosp
= next_protosp
->next
;
409 if (curr_protosp
->gssid
)
410 free(curr_protosp
->gssid
);
411 if (curr_protosp
->remote
)
412 free(curr_protosp
->remote
);
413 racoon_free(curr_protosp
);
415 racoon_free(curr_propsp
);
420 delrmconf(struct remoteconf
*rmconf
)
423 racoon_free(rmconf
->remote
);
424 if (rmconf
->forced_local
)
425 racoon_free(rmconf
->forced_local
);
428 xauth_rmconf_delete(&rmconf
->xauth
);
430 if (rmconf
->etypes
) {
431 deletypes(rmconf
->etypes
);
437 genlist_free(rmconf
->idvl_p
, idspec_free
);
439 oakley_dhgrp_free(rmconf
->dhgrp
);
440 if (rmconf
->proposal
)
441 delisakmpsa(rmconf
->proposal
);
443 proposalspec_free(rmconf
->prhead
);
444 if (rmconf
->shared_secret
)
445 vfree(rmconf
->shared_secret
);
446 if (rmconf
->keychainCertRef
)
447 vfree(rmconf
->keychainCertRef
);
448 if (rmconf
->open_dir_auth_group
)
449 vfree(rmconf
->open_dir_auth_group
);
455 delisakmpsa(struct isakmpsa
*sa
)
458 oakley_dhgrp_free(sa
->dhgrp
);
460 delisakmpsa(sa
->next
);
465 dupetypes(struct etypes
*orig
)
472 new = racoon_malloc(sizeof(struct etypes
));
476 new->type
= orig
->type
;
480 new->next
=dupetypes(orig
->next
);
486 deletypes(struct etypes
*e
)
494 * insert into head of list.
497 insrmconf(struct remoteconf
*new)
499 TAILQ_INSERT_HEAD(&rmtree
, new, chain
);
504 remrmconf(struct remoteconf
*rmconf
)
507 TAILQ_REMOVE(&rmtree
, rmconf
, chain
);
512 retain_rmconf(struct remoteconf
*rmconf
)
514 (rmconf
->refcount
)++;
518 release_rmconf(struct remoteconf
*rmconf
)
520 if (--(rmconf
->refcount
) <= 0) {
529 struct remoteconf
*p
, *next
;
531 for (p
= TAILQ_FIRST(&rmtree
); p
; p
= next
) {
532 next
= TAILQ_NEXT(p
, chain
);
534 if (--(p
->refcount
) <= 0)
545 /* check exchange type to be acceptable */
547 check_etypeok(struct remoteconf
*rmconf
, u_int8_t etype
)
551 for (e
= rmconf
->etypes
; e
!= NULL
; e
= e
->next
) {
552 if (e
->type
== etype
)
563 struct isakmpsa
*new;
565 new = racoon_calloc(1, sizeof(*new));
570 * Just for sanity, make sure this is initialized. This is
571 * filled in for real when the ISAKMP proposal is configured.
573 new->vendorid
= VENDORID_UNKNOWN
;
582 * insert into tail of list.
585 insisakmpsa(struct isakmpsa
*new, struct remoteconf
*rmconf
)
589 new->rmconf
= rmconf
;
591 if (rmconf
->proposal
== NULL
) {
592 rmconf
->proposal
= new;
596 for (p
= rmconf
->proposal
; p
->next
!= NULL
; p
= p
->next
)
604 foreachrmconf(rmconf_func_t rmconf_func
, void *data
)
606 struct remoteconf
*p
, *ret
= NULL
;
608 TAILQ_FOREACH_REVERSE(p
, &rmtree
, _rmtree
, chain
) {
609 ret
= (*rmconf_func
)(p
, data
);
618 dump_peers_identifiers (void *entry
, void *arg
)
620 struct idspec
*id
= (struct idspec
*) entry
;
621 char buf
[1024], *pbuf
;
623 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), "\tpeers_identifier %s",
624 s_idtype (id
->idtype
));
626 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), " \"%s\"", id
->id
->v
);
627 plog(ASL_LEVEL_INFO
, "%s;\n", buf
);
631 static struct remoteconf
*
632 dump_rmconf_single (struct remoteconf
*p
, void *data
)
634 struct etypes
*etype
= p
->etypes
;
635 struct isakmpsa
*prop
= p
->proposal
;
636 char buf
[1024], *pbuf
;
639 if (p
->remote_prefix
)
640 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), "remote %s",
641 saddr2str_with_prefix((struct sockaddr
*)p
->remote
, p
->remote_prefix
));
643 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), "remote %s", saddr2str((struct sockaddr
*)p
->remote
));
644 if (p
->inherited_from
)
645 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), " inherit %s",
646 saddr2str((struct sockaddr
*)p
->inherited_from
->remote
));
647 plog(ASL_LEVEL_INFO
, "%s {\n", buf
);
649 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), "\texchange_type ");
651 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), "%s%s", s_etype(etype
->type
),
652 etype
->next
!= NULL
? ", " : ";\n");
655 plog(ASL_LEVEL_INFO
, "%s", buf
);
656 plog(ASL_LEVEL_INFO
, "\tdoi %s;\n", s_doi(p
->doitype
));
658 pbuf
+= snprintf(pbuf
, sizeof(buf
) - (pbuf
- buf
), "\tmy_identifier %s", s_idtype (p
->idvtype
));
659 if (p
->idvtype
== IDTYPE_ASN1DN
) {
660 plog(ASL_LEVEL_INFO
, "%s;\n", buf
);
661 switch (p
->getcert_method
) {
664 case ISAKMP_GETCERT_PAYLOAD
:
665 plog(ASL_LEVEL_INFO
, "\t/* peers certificate from payload */\n");
668 plog(ASL_LEVEL_INFO
, "\tpeers_certfile *UNKNOWN* (%d)\n", p
->getcert_method
);
673 pbuf
+= snprintf (pbuf
, sizeof(buf
) - (pbuf
- buf
), " \"%s\"", p
->idv
->v
);
674 plog(ASL_LEVEL_INFO
, "%s;\n", buf
);
675 genlist_foreach(p
->idvl_p
, &dump_peers_identifiers
, NULL
);
678 plog(ASL_LEVEL_INFO
, "\tsend_cert %s;\n",
679 s_switch (p
->send_cert
));
680 plog(ASL_LEVEL_INFO
, "\tsend_cr %s;\n",
681 s_switch (p
->send_cr
));
682 plog(ASL_LEVEL_INFO
, "\tverify_cert %s;\n",
683 s_switch (p
->verify_cert
));
684 plog(ASL_LEVEL_INFO
, "\tverify_identifier %s;\n",
685 s_switch (p
->verify_identifier
));
686 plog(ASL_LEVEL_INFO
, "\tnat_traversal %s;\n",
687 p
->nat_traversal
== NATT_FORCE
?
688 "force" : s_switch (p
->nat_traversal
));
689 plog(ASL_LEVEL_INFO
, "\tnatt_multiple_user %s;\n",
690 s_switch (p
->natt_multiple_user
));
691 plog(ASL_LEVEL_INFO
, "\tnonce_size %d;\n",
693 plog(ASL_LEVEL_INFO
, "\tpassive %s;\n",
694 s_switch (p
->passive
));
695 plog(ASL_LEVEL_INFO
, "\tike_frag %s;\n",
696 p
->ike_frag
== ISAKMP_FRAG_FORCE
?
697 "force" : s_switch (p
->ike_frag
));
698 plog(ASL_LEVEL_INFO
, "\tesp_frag %d;\n", p
->esp_frag
);
699 plog(ASL_LEVEL_INFO
, "\tinitial_contact %s;\n",
700 s_switch (p
->ini_contact
));
701 plog(ASL_LEVEL_INFO
, "\tgenerate_policy %s;\n",
702 s_switch (p
->gen_policy
));
703 plog(ASL_LEVEL_INFO
, "\tsupport_proxy %s;\n",
704 s_switch (p
->support_proxy
));
707 plog(ASL_LEVEL_INFO
, "\n");
709 "\t/* prop_no=%d, trns_no=%d, rmconf=%s */\n",
710 prop
->prop_no
, prop
->trns_no
,
711 saddr2str((struct sockaddr
*)prop
->rmconf
->remote
));
712 plog(ASL_LEVEL_INFO
, "\tproposal {\n");
713 plog(ASL_LEVEL_INFO
, "\t\tlifetime time %lu sec;\n",
714 (long)prop
->lifetime
);
715 plog(ASL_LEVEL_INFO
, "\t\tlifetime bytes %zd;\n",
717 plog(ASL_LEVEL_INFO
, "\t\tdh_group %s;\n",
718 alg_oakley_dhdef_name(prop
->dh_group
));
719 plog(ASL_LEVEL_INFO
, "\t\tencryption_algorithm %s;\n",
720 alg_oakley_encdef_name(prop
->enctype
));
721 plog(ASL_LEVEL_INFO
, "\t\thash_algorithm %s;\n",
722 alg_oakley_hashdef_name(prop
->hashtype
));
723 plog(ASL_LEVEL_INFO
, "\t\tprf_algorithm %s;\n",
724 alg_oakley_hashdef_name(prop
->prf
));
725 plog(ASL_LEVEL_INFO
, "\t\tauthentication_method %s;\n",
726 alg_oakley_authdef_name(prop
->authmethod
));
727 plog(ASL_LEVEL_INFO
, "\t}\n");
730 plog(ASL_LEVEL_INFO
, "}\n");
731 plog(ASL_LEVEL_INFO
, "\n");
739 foreachrmconf (dump_rmconf_single
, NULL
);
747 new = racoon_calloc(1, sizeof(*new));
750 new->idtype
= IDTYPE_ADDRESS
;
757 dupisakmpsa(struct isakmpsa
*sa
)
759 struct isakmpsa
*res
= NULL
;
771 if (sa
->dhgrp
!= NULL
)
772 oakley_setdhgroup(sa
->dh_group
, &(res
->dhgrp
));