2 * Copyright (c) 2007 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of the project nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/socket.h>
57 #include <sys/signal.h>
61 #include <System/net/pfkeyv2.h>
63 #include <netinet/in.h>
64 #ifndef HAVE_NETINET6_IPSEC
65 #include <netinet/ipsec.h>
67 #include <netinet6/ipsec.h>
91 #include "localconf.h"
92 #include "remoteconf.h"
93 #include "grabmyaddr.h"
94 #include "isakmp_var.h"
99 #include "ipsec_doi.h"
101 #include "admin_var.h"
102 #include "isakmp_inf.h"
104 #include "isakmp_cfg.h"
105 #include "isakmp_unity.h"
108 #include "gcmalloc.h"
110 #include "ipsec_doi.h"
111 #include "nattraversal.h"
113 #include "vpn_control.h"
114 #include "vpn_control_var.h"
115 #include "strnames.h"
116 #include "ike_session.h"
117 #include "ipsecMessageTracer.h"
120 static int vpn_get_ph2pfs(struct ph1handle
*);
123 vpn_connect(struct bound_addr
*srv
, int oper
)
126 struct sockaddr_storage
*dst
;
127 struct remoteconf
*rmconf
;
128 struct sockaddr_storage
*remote
= NULL
;
129 struct sockaddr_storage
*local
= NULL
;
132 dst
= racoon_calloc(1, sizeof(struct sockaddr_storage
)); // this should come from the bound_addr parameter
135 ((struct sockaddr_in
*)(dst
))->sin_len
= sizeof(struct sockaddr_in
);
136 ((struct sockaddr_in
*)(dst
))->sin_family
= AF_INET
;
137 ((struct sockaddr_in
*)(dst
))->sin_port
= 500;
138 ((struct sockaddr_in
*)(dst
))->sin_addr
.s_addr
= srv
->address
;
141 * Find the source address
143 if ((local
= getlocaladdr((struct sockaddr
*)dst
)) == NULL
) {
144 plog(LLV_ERROR
, LOCATION
, NULL
,
145 "cannot get local address\n");
149 /* find appropreate configuration */
150 rmconf
= getrmconf(dst
);
151 if (rmconf
== NULL
) {
152 plog(LLV_ERROR
, LOCATION
, NULL
,
153 "no configuration found "
154 "for %s\n", saddrwop2str((struct sockaddr
*)dst
));
158 /* get remote IP address and port number. */
159 if ((remote
= dupsaddr((struct sockaddr
*)dst
)) == NULL
) {
160 plog(LLV_ERROR
, LOCATION
, NULL
,
161 "failed to duplicate address\n");
165 switch (remote
->ss_family
) {
167 ((struct sockaddr_in
*)remote
)->sin_port
=
168 ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
172 ((struct sockaddr_in6
*)remote
)->sin6_port
=
173 ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
177 plog(LLV_ERROR
, LOCATION
, NULL
,
178 "invalid family: %d\n",
184 port
= ntohs(getmyaddrsport(local
));
185 if (set_port(local
, port
) == NULL
)
188 plog(LLV_INFO
, LOCATION
, NULL
,
189 "accept a request to establish IKE-SA: "
190 "%s\n", saddrwop2str((struct sockaddr
*)remote
));
192 IPSECLOGASLMSG("IPSec connecting to server %s\n",
193 saddrwop2str((struct sockaddr
*)remote
));
195 /* begin ident mode */
196 if (isakmp_ph1begin_i(rmconf
, remote
, local
, oper
) < 0)
214 vpn_disconnect(struct bound_addr
*srv
, const char *reason
)
216 union { // Wcast-align fix - force alignment
217 struct sockaddr_storage ss
;
218 struct sockaddr_in saddr
;
221 bzero(&u
.saddr
, sizeof(u
.saddr
));
222 u
.saddr
.sin_len
= sizeof(u
.saddr
);
223 u
.saddr
.sin_addr
.s_addr
= srv
->address
;
224 u
.saddr
.sin_port
= 0;
225 u
.saddr
.sin_family
= AF_INET
;
227 IPSECLOGASLMSG("IPSec disconnecting from server %s\n",
228 saddrwop2str((struct sockaddr
*)&u
.ss
));
230 ike_sessions_stopped_by_controller(&u
.ss
,
233 if (purgephXbydstaddrwop(&u
.ss
) > 0) {
241 vpn_start_ph2(struct bound_addr
*addr
, struct vpnctl_cmd_start_ph2
*pkt
)
243 struct vpnctl_sa_selector
*selector_ptr
;
244 struct vpnctl_algo
*algo_ptr
, *next_algo
;
246 struct sainfoalg
*new_algo
;
247 struct sainfo
*new_sainfo
= NULL
, *check
;
248 u_int16_t
class, algorithm
, keylen
;
249 struct ph1handle
*ph1
;
250 struct sockaddr_in saddr
;
253 u_int8_t type
; /* ID Type */
254 u_int8_t proto_id
; /* Protocol ID */
255 u_int16_t port
; /* Port */
256 u_int32_t addr
; /* IPv4 address */
260 /* verify ph1 exists */
261 bzero(&saddr
, sizeof(saddr
));
262 saddr
.sin_len
= sizeof(saddr
);
263 saddr
.sin_addr
.s_addr
= addr
->address
;
265 saddr
.sin_family
= AF_INET
;
266 ph1
= getph1bydstaddrwop((struct sockaddr_storage
*)(&saddr
));
268 plog(LLV_ERROR
, LOCATION
, NULL
,
269 "cannot start phase2 - no phase1 found.\n");
272 if (ph1
->status
!= PHASE1ST_ESTABLISHED
) {
273 plog(LLV_ERROR
, LOCATION
, NULL
,
274 "cannot start phase2 - phase1 not established.\n");
278 selector_ptr
= (struct vpnctl_sa_selector
*)(pkt
+ 1);
279 algo_ptr
= (struct vpnctl_algo
*)(selector_ptr
+ ntohs(pkt
->selector_count
));
281 for (i
= 0; i
< ntohs(pkt
->selector_count
); i
++, selector_ptr
++) {
282 new_sainfo
= newsainfo();
283 if (new_sainfo
== NULL
) {
284 plog(LLV_ERROR
, LOCATION
, NULL
,
285 "unable to allocate sainfo struct.\n");
289 if (ntohl(selector_ptr
->src_tunnel_mask
) == 0xFFFFFFFF)
290 new_sainfo
->idsrc
= vmalloc(sizeof(struct id
) - sizeof(u_int32_t
));
292 new_sainfo
->idsrc
= vmalloc(sizeof(struct id
));
293 if (new_sainfo
->idsrc
== NULL
) {
294 plog(LLV_ERROR
, LOCATION
, NULL
,
295 "unable to allocate id struct.\n");
298 if (selector_ptr
->dst_tunnel_mask
== 0xFFFFFFFF)
299 new_sainfo
->iddst
= vmalloc(sizeof(struct id
) - sizeof(u_int32_t
));
301 new_sainfo
->iddst
= vmalloc(sizeof(struct id
));
302 if (new_sainfo
->iddst
== NULL
) {
303 plog(LLV_ERROR
, LOCATION
, NULL
,
304 "unable to allocate id struct.\n");
308 id_ptr
= ALIGNED_CAST(struct id
*)new_sainfo
->idsrc
->v
;
309 if (ntohl(selector_ptr
->src_tunnel_mask
) == 0xFFFFFFFF)
310 id_ptr
->type
= IPSECDOI_ID_IPV4_ADDR
;
312 id_ptr
->type
= IPSECDOI_ID_IPV4_ADDR_SUBNET
;
313 id_ptr
->mask
= selector_ptr
->src_tunnel_mask
;
315 id_ptr
->addr
= selector_ptr
->src_tunnel_address
;
316 id_ptr
->port
= selector_ptr
->src_tunnel_port
;
317 id_ptr
->proto_id
= selector_ptr
->ul_protocol
;
319 id_ptr
= ALIGNED_CAST(struct id
*)new_sainfo
->iddst
->v
;
320 if (selector_ptr
->dst_tunnel_mask
== 0xFFFFFFFF)
321 id_ptr
->type
= IPSECDOI_ID_IPV4_ADDR
;
323 id_ptr
->type
= IPSECDOI_ID_IPV4_ADDR_SUBNET
;
324 id_ptr
->mask
= selector_ptr
->dst_tunnel_mask
;
326 id_ptr
->addr
= selector_ptr
->dst_tunnel_address
;
327 id_ptr
->port
= selector_ptr
->dst_tunnel_port
;
328 id_ptr
->proto_id
= selector_ptr
->ul_protocol
;
330 new_sainfo
->dynamic
= addr
->address
;
331 new_sainfo
->lifetime
= ntohl(pkt
->lifetime
);
333 if (ntohs(pkt
->pfs_group
) != 0) {
334 new_sainfo
->pfs_group
= algtype2doi(algclass_isakmp_dh
, ntohs(pkt
->pfs_group
));
335 if (new_sainfo
->pfs_group
== -1) {
336 plog(LLV_ERROR
, LOCATION
, NULL
, "invalid dh group specified\n");
340 for (j
= 0, next_algo
= algo_ptr
; j
< ntohs(pkt
->algo_count
); j
++, next_algo
++) {
342 new_algo
= newsainfoalg();
343 if (new_algo
== NULL
) {
344 plog(LLV_ERROR
, LOCATION
, NULL
,
345 "failed to allocate algorithm structure\n");
349 class = ntohs(next_algo
->algo_class
);
350 algorithm
= ntohs(next_algo
->algo
);
351 keylen
= ntohs(next_algo
->key_len
);
353 new_algo
->alg
= algtype2doi(class, algorithm
);
354 if (new_algo
->alg
== -1) {
355 plog(LLV_ERROR
, LOCATION
, NULL
, "algorithm mismatched\n");
356 racoon_free(new_algo
);
360 defklen
= default_keylen(class, algorithm
);
363 plog(LLV_ERROR
, LOCATION
, NULL
, "keylen not allowed\n");
364 racoon_free(new_algo
);
368 if (keylen
&& check_keylen(class, algorithm
, keylen
) < 0) {
369 plog(LLV_ERROR
, LOCATION
, NULL
, "invalid keylen %d\n", keylen
);
370 racoon_free(new_algo
);
376 new_algo
->encklen
= keylen
;
378 new_algo
->encklen
= defklen
;
380 /* check if it's supported algorithm by kernel */
381 if (!(class == algclass_ipsec_auth
&& algorithm
== algtype_non_auth
)
382 && pk_checkalg(class, algorithm
, new_algo
->encklen
)) {
383 int a
= algclass2doi(class);
384 int b
= new_algo
->alg
;
385 if (a
== IPSECDOI_ATTR_AUTH
)
386 a
= IPSECDOI_PROTO_IPSEC_AH
;
387 plog(LLV_ERROR
, LOCATION
, NULL
,
388 "algorithm %s not supported by the kernel (missing module?)\n", s_ipsecdoi_trns(a
, b
));
389 racoon_free(new_algo
);
392 inssainfoalg(&new_sainfo
->algs
[class], new_algo
);
395 if (new_sainfo
->algs
[algclass_ipsec_enc
] == 0) {
396 plog(LLV_ERROR
, LOCATION
, NULL
,
397 "no encryption algorithm at %s\n", sainfo2str(new_sainfo
));
400 if (new_sainfo
->algs
[algclass_ipsec_auth
] == 0) {
401 plog(LLV_ERROR
, LOCATION
, NULL
,
402 "no authentication algorithm at %s\n", sainfo2str(new_sainfo
));
405 if (new_sainfo
->algs
[algclass_ipsec_comp
] == 0) {
406 plog(LLV_ERROR
, LOCATION
, NULL
,
407 "no compression algorithm at %s\n", sainfo2str(new_sainfo
));
411 /* duplicate check */
412 check
= getsainfo(new_sainfo
->idsrc
, new_sainfo
->iddst
, new_sainfo
->id_i
, 0);
413 if (check
&& (!check
->idsrc
&& !new_sainfo
->idsrc
)) {
414 plog(LLV_ERROR
, LOCATION
, NULL
,"duplicated sainfo: %s\n", sainfo2str(new_sainfo
));
417 //plog(LLV_DEBUG2, LOCATION, NULL, "create sainfo: %s\n", sainfo2str(new_sainfo));
418 inssainfo(new_sainfo
);
426 delsainfo(new_sainfo
);
427 flushsainfo_dynamic((u_int32_t
)addr
->address
);
432 vpn_get_ph2pfs(struct ph1handle
*ph1
)
438 vpn_get_config(struct ph1handle
*iph1
, struct vpnctl_status_phase_change
**msg
, size_t *msg_size
)
441 struct vpnctl_modecfg_params
*params
;
442 struct myaddrs
*myaddr
;
443 u_int16_t ifname_len
, msize
;
449 if (((struct sockaddr_in
*)iph1
->local
)->sin_family
!= AF_INET
) {
450 plog(LLV_ERROR
, LOCATION
, NULL
,
451 "IPv6 not supported for mode config.\n");
455 if (iph1
->mode_cfg
->attr_list
== NULL
)
456 return 1; /* haven't received configuration yet */
458 myaddr
= find_myaddr((struct sockaddr
*)iph1
->local
, 0);
459 if (myaddr
== NULL
) {
460 plog(LLV_ERROR
, LOCATION
, NULL
,
461 "unable to find address structure.\n");
465 msize
= sizeof(struct vpnctl_status_phase_change
)
466 + sizeof(struct vpnctl_modecfg_params
);
467 msize
+= iph1
->mode_cfg
->attr_list
->l
;
469 *msg
= racoon_calloc(1, msize
);
471 plog(LLV_ERROR
, LOCATION
, NULL
,
472 "faled to allocate space for message.\n");
476 (*msg
)->hdr
.flags
= htons(VPNCTL_FLAG_MODECFG_USED
);
477 params
= (struct vpnctl_modecfg_params
*)(*msg
+ 1);
478 params
->outer_local_addr
= ((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
;
479 params
->outer_remote_port
= htons(0);
480 params
->outer_local_port
= htons(0);
481 ifname_len
= strlen(myaddr
->ifname
);
482 memset(¶ms
->ifname
, 0, IFNAMSIZ
);
483 memcpy(¶ms
->ifname
, myaddr
->ifname
, ifname_len
< IFNAMSIZ
? ifname_len
: IFNAMSIZ
-1);
484 cptr
= (u_int8_t
*)(params
+ 1);
485 memcpy(cptr
, iph1
->mode_cfg
->attr_list
->v
, iph1
->mode_cfg
->attr_list
->l
);
488 IPSECLOGASLMSG("IPSec Network Configuration established.\n");
495 vpn_xauth_reply(u_int32_t address
, void *attr_list
, size_t attr_len
)
498 struct isakmp_pl_attr
*reply
;
500 vchar_t
*payload
= NULL
;
501 struct ph1handle
*iph1
;
502 struct sockaddr_in saddr
;
505 struct isakmp_data
*attr
;
506 char *dataptr
= (char *)attr_list
;
509 bzero(&saddr
, sizeof(saddr
));
510 saddr
.sin_len
= sizeof(saddr
);
511 saddr
.sin_addr
.s_addr
= address
;
513 saddr
.sin_family
= AF_INET
;
514 iph1
= getph1bydstaddrwop((struct sockaddr_storage
*)(&saddr
));
516 plog(LLV_ERROR
, LOCATION
, NULL
,
517 "cannot reply to xauth request - no ph1 found.\n");
521 if (iph1
->xauth_awaiting_userinput
== 0) {
522 plog(LLV_ERROR
, LOCATION
, NULL
, "Huh? recvd xauth reply data with no xauth reply pending \n");
526 /* validate attr lengths */
531 attr
= (struct isakmp_data
*)dataptr
;
532 tlv
= (attr
->type
& htons(0x8000)) == 0;
535 tlen
-= ntohs(attr
->lorv
);
536 dataptr
+= ntohs(attr
->lorv
);
538 tlen
-= sizeof(u_int32_t
);
539 dataptr
+= sizeof(u_int32_t
);
542 plog(LLV_ERROR
, LOCATION
, NULL
, "invalid auth info received from VPN Control socket.\n");
546 payload
= vmalloc(sizeof(struct isakmp_pl_attr
) + attr_len
);
547 if (payload
== NULL
) {
548 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot allocate memory for xauth reply\n");
551 memset(payload
->v
, 0, sizeof(reply
));
553 reply
= (struct isakmp_pl_attr
*)payload
->v
;
554 reply
->h
.len
= htons(payload
->l
);
555 reply
->type
= ISAKMP_CFG_REPLY
;
556 reply
->id
= iph1
->pended_xauth_id
; /* network byte order */
557 iph1
->xauth_awaiting_userinput
= 0; /* no longer waiting */
558 attr_ptr
= reply
+ 1;
559 memcpy(attr_ptr
, attr_list
, attr_len
);
561 plog(LLV_DEBUG
, LOCATION
, NULL
,
562 "Sending MODE_CFG REPLY\n");
563 error
= isakmp_cfg_send(iph1
, payload
,
564 ISAKMP_NPTYPE_ATTR
, ISAKMP_FLAG_E
, 0, 0, iph1
->xauth_awaiting_userinput_msg
);
565 VPTRINIT(iph1
->xauth_awaiting_userinput_msg
);
566 ike_session_stop_xauth_timer(iph1
);
568 IPSECLOGASLMSG("IPSec Extended Authentication sent.\n");
577 vpn_assert(struct sockaddr_storage
*src_addr
, struct sockaddr_storage
*dst_addr
)
579 if (ike_session_assert(src_addr
, dst_addr
)) {
580 plog(LLV_ERROR
, LOCATION
, NULL
,
581 "cannot assert - no matching session.\n");