1 /* $Id: pfkey.c,v 1.31.2.10 2005/10/03 14:52:19 manubsd Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "racoon_types.h"
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <netinet/udp.h>
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/queue.h>
55 #include <sys/sysctl.h>
57 #include <net/route.h>
58 #include <net/pfkeyv2.h>
60 #include <netinet/in.h>
61 #ifndef HAVE_NETINET6_IPSEC
62 #include <netinet/ipsec.h>
64 #include <netinet6/ipsec.h>
76 #include "ike_session.h"
79 #include "localconf.h"
80 #include "remoteconf.h"
81 #include "isakmp_var.h"
83 #include "isakmp_inf.h"
84 #include "ipsec_doi.h"
89 #include "algorithm.h"
94 #include "nattraversal.h"
95 #include "crypto_openssl.h"
96 #include "grabmyaddr.h"
97 #include "vpn_control.h"
98 #include "vpn_control_var.h"
99 #include "ike_session.h"
100 #include "ipsecSessionTracer.h"
101 #include "ipsecMessageTracer.h"
102 #include "power_mgmt.h"
105 #if defined(SADB_X_EALG_RIJNDAELCBC) && !defined(SADB_X_EALG_AESCBC)
106 #define SADB_X_EALG_AESCBC SADB_X_EALG_RIJNDAELCBC
110 static u_int
ipsecdoi2pfkey_aalg (u_int
);
111 static u_int
ipsecdoi2pfkey_ealg (u_int
);
112 static u_int
ipsecdoi2pfkey_calg (u_int
);
113 static u_int
ipsecdoi2pfkey_alg (u_int
, u_int
);
114 static u_int
keylen_aalg (u_int
);
115 static u_int
keylen_ealg (u_int
, int);
117 static int pk_recvgetspi (caddr_t
*);
118 static int pk_recvupdate (caddr_t
*);
119 static int pk_recvadd (caddr_t
*);
120 static int pk_recvdelete (caddr_t
*);
121 static int pk_recvacquire (caddr_t
*);
122 static int pk_recvexpire (caddr_t
*);
123 static int pk_recvflush (caddr_t
*);
124 static int getsadbpolicy (caddr_t
*, int *, int, phase2_handle_t
*);
125 static int pk_recvspdupdate (caddr_t
*);
126 static int pk_recvspdadd (caddr_t
*);
127 static int pk_recvspddelete (caddr_t
*);
128 static int pk_recvspdexpire (caddr_t
*);
129 static int pk_recvspdget (caddr_t
*);
130 static int pk_recvspddump (caddr_t
*);
131 static int pk_recvspdflush (caddr_t
*);
132 static int pk_recvgetsastat (caddr_t
*);
133 static struct sadb_msg
*pk_recv (int, ssize_t
*);
135 static int (*pkrecvf
[]) (caddr_t
*) = {
143 NULL
, /* SABD_REGISTER */
146 NULL
, /* SADB_DUMP */
147 NULL
, /* SADB_X_PROMISC */
148 NULL
, /* SADB_X_PCHANGE */
153 NULL
, /* SADB_X_SPDACQUIRE */
156 NULL
, /* SADB_X_SPDSETIDX */
158 NULL
, /* SADB_X_SPDDELETE2 */
159 pk_recvgetsastat
, /* SADB_GETSASTAT */
160 NULL
, /* SADB_X_SPDENABLE */
161 NULL
, /* SADB_X_SPDDISNABLE */
162 NULL
, /* SADB_MIGRATE */
164 #warning "SADB extra message?"
168 static int addnewsp (caddr_t
*);
170 /* cope with old kame headers - ugly */
171 #ifndef SADB_X_AALG_MD5
172 #define SADB_X_AALG_MD5 SADB_AALG_MD5
174 #ifndef SADB_X_AALG_SHA
175 #define SADB_X_AALG_SHA SADB_AALG_SHA
177 #ifndef SADB_X_AALG_NULL
178 #define SADB_X_AALG_NULL SADB_AALG_NULL
181 #ifndef SADB_X_EALG_BLOWFISHCBC
182 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
184 #ifndef SADB_X_EALG_CAST128CBC
185 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC
187 #ifndef SADB_X_EALG_RC5CBC
188 #ifdef SADB_EALG_RC5CBC
189 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
195 struct sadb_msg
*msg
;
197 caddr_t mhp
[SADB_EXT_MAX
+ 1];
200 // Special debug use only - creates large logs
201 // plogdump(ASL_LEVEL_DEBUG, msg, msg->sadb_msg_len << 3, "get pfkey %s message\n",
202 // s_pfkey_type(msg->sadb_msg_type));
205 /* check pfkey message. */
206 if (pfkey_align(msg
, mhp
)) {
208 "libipsec failed pfkey align (%s)\n",
212 if (pfkey_check(mhp
)) {
214 "libipsec failed pfkey check (%s)\n",
218 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
220 if (msg
->sadb_msg_errno
) {
223 /* when SPD is empty, treat the state as no error. */
224 if (msg
->sadb_msg_type
== SADB_X_SPDDUMP
&&
225 msg
->sadb_msg_errno
== ENOENT
)
226 pri
= ASL_LEVEL_DEBUG
;
231 "pfkey %s failed: %s\n",
232 s_pfkey_type(msg
->sadb_msg_type
),
233 strerror(msg
->sadb_msg_errno
));
238 if (msg
->sadb_msg_type
>= ARRAYLEN(pkrecvf
)) {
240 "unknown PF_KEY message type=%u\n",
245 if (pkrecvf
[msg
->sadb_msg_type
] == NULL
) {
247 "unsupported PF_KEY message %s\n",
248 s_pfkey_type(msg
->sadb_msg_type
));
252 if ((pkrecvf
[msg
->sadb_msg_type
])(mhp
) < 0)
263 * PF_KEY packet handler
268 //%%%%%%%%%%%%%%%%%% need to handle errors encountered here - this no longer returns a result
270 pfkey_handler(void *unused
)
272 struct sadb_msg
*msg
;
275 if (slept_at
|| woke_at
) {
276 plog(ASL_LEVEL_DEBUG
,
277 "ignoring pfkey port until power-mgmt event is handled.\n");
281 /* receive pfkey message. */
283 msg
= (struct sadb_msg
*)pk_recv(lcconf
->sock_pfkey
, &len
);
288 "failed to recv from pfkey (%s)\n",
292 /* short message - msg not ready */
293 plog(ASL_LEVEL_DEBUG
, "recv short message from pfkey\n");
303 struct saved_msg_elem
*elem
;
304 struct saved_msg_elem
*elem_tmp
= NULL
;
306 if (slept_at
|| woke_at
) {
307 plog(ASL_LEVEL_DEBUG
,
308 "ignoring (saved) pfkey messages until power-mgmt event is handled.\n");
312 TAILQ_FOREACH_SAFE(elem
, &lcconf
->saved_msg_queue
, chain
, elem_tmp
) {
313 pfkey_process((struct sadb_msg
*)elem
->msg
);
314 TAILQ_REMOVE(&lcconf
->saved_msg_queue
, elem
, chain
);
322 struct sadb_msg
*msg
;
324 struct saved_msg_elem
*elem
;
326 elem
= (struct saved_msg_elem
*)racoon_calloc(sizeof(struct saved_msg_elem
), 1);
330 TAILQ_INSERT_TAIL(&lcconf
->saved_msg_queue
, elem
, chain
);
338 pfkey_dump_sadb(satype
)
343 pid_t pid
= getpid();
344 struct sadb_msg
*msg
= NULL
;
348 if ((s
= pfkey_open()) < 0) {
350 "libipsec failed pfkey open: %s\n",
355 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_dump\n");
356 if (pfkey_send_dump(s
, satype
) < 0) {
358 "libipsec failed dump: %s\n", ipsec_strerror());
365 msg
= pk_recv(s
, &len
);
374 * for multi-processor system this had to be added because the messages can
375 * be interleaved - they won't all be dump messages
377 if (msg
->sadb_msg_type
!= SADB_DUMP
) { /* save for later processing */
383 // ignore dump messages that aren't racoon's
384 if (msg
->sadb_msg_pid
!= pid
)
387 ml
= msg
->sadb_msg_len
<< 3;
388 bl
= buf
? buf
->l
: 0;
389 buf
= vrealloc(buf
, bl
+ ml
);
392 "failed to reallocate buffer to dump.\n");
395 memcpy(buf
->v
+ bl
, msg
, ml
);
397 if (msg
->sadb_msg_seq
== 0)
416 * These are the SATYPEs that we manage. We register to get
417 * PF_KEY messages related to these SATYPEs, and we also use
418 * this list to determine which SATYPEs to delete SAs for when
419 * we receive an INITIAL-CONTACT.
421 const struct pfkey_satype pfkey_satypes
[] = {
422 { SADB_SATYPE_AH
, "AH" },
423 { SADB_SATYPE_ESP
, "ESP" },
424 { SADB_X_SATYPE_IPCOMP
, "IPCOMP" },
426 const int pfkey_nsatypes
=
427 sizeof(pfkey_satypes
) / sizeof(pfkey_satypes
[0]);
430 * PF_KEY initialization
435 int i
, reg_fail
, sock
;
437 if ((lcconf
->sock_pfkey
= pfkey_open()) < 0) {
439 "libipsec failed pfkey open (%s)\n", ipsec_strerror());
443 for (i
= 0, reg_fail
= 0; i
< pfkey_nsatypes
; i
++) {
444 plog(ASL_LEVEL_DEBUG
,
445 "call pfkey_send_register for %s\n",
446 pfkey_satypes
[i
].ps_name
);
447 if (pfkey_send_register(lcconf
->sock_pfkey
,
448 pfkey_satypes
[i
].ps_satype
) < 0 ||
449 pfkey_recv_register(lcconf
->sock_pfkey
) < 0) {
450 plog(ASL_LEVEL_WARNING
,
451 "failed to register %s (%s)\n",
452 pfkey_satypes
[i
].ps_name
,
458 if (reg_fail
== pfkey_nsatypes
) {
460 "failed to regist any protocol.\n");
461 close(lcconf
->sock_pfkey
);
466 lcconf
->pfkey_source
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, lcconf
->sock_pfkey
, 0, dispatch_get_main_queue());
467 if (lcconf
->pfkey_source
== NULL
) {
468 plog(ASL_LEVEL_ERR
, "could not create pfkey socket source.");
471 dispatch_source_set_event_handler_f(lcconf
->pfkey_source
, pfkey_handler
);
472 sock
= lcconf
->sock_pfkey
;
473 dispatch_source_set_cancel_handler(lcconf
->pfkey_source
,
475 pfkey_close_sock(sock
);
477 dispatch_resume(lcconf
->pfkey_source
);
479 if (pfkey_send_spddump(lcconf
->sock_pfkey
) < 0) {
481 "libipsec sending spddump failed: %s\n",
487 if (pfkey_promisc_toggle(1) < 0) {
499 dispatch_source_cancel(lcconf
->pfkey_source
);
500 lcconf
->pfkey_source
= NULL
;
503 /* %%% for conversion */
504 /* IPSECDOI_ATTR_AUTH -> SADB_AALG */
506 ipsecdoi2pfkey_aalg(hashtype
)
510 case IPSECDOI_ATTR_AUTH_HMAC_MD5
:
511 case IPSECDOI_ATTR_AUTH_HMAC_MD5_96
:
512 return SADB_AALG_MD5HMAC
;
513 case IPSECDOI_ATTR_AUTH_HMAC_SHA1
:
514 case IPSECDOI_ATTR_AUTH_HMAC_SHA1_96
:
515 return SADB_AALG_SHA1HMAC
;
516 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_256
:
517 #if (defined SADB_X_AALG_SHA2_256) && !defined(SADB_X_AALG_SHA2_256HMAC)
518 return SADB_X_AALG_SHA2_256
;
520 return SADB_X_AALG_SHA2_256HMAC
;
522 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_384
:
523 #if (defined SADB_X_AALG_SHA2_384) && !defined(SADB_X_AALG_SHA2_384HMAC)
524 return SADB_X_AALG_SHA2_384
;
526 return SADB_X_AALG_SHA2_384HMAC
;
528 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_512
:
529 #if (defined SADB_X_AALG_SHA2_512) && !defined(SADB_X_AALG_SHA2_512HMAC)
530 return SADB_X_AALG_SHA2_512
;
532 return SADB_X_AALG_SHA2_512HMAC
;
534 case IPSECDOI_ATTR_AUTH_KPDK
: /* need special care */
535 return SADB_AALG_NONE
;
538 case IPSECDOI_ATTR_AUTH_DES_MAC
:
540 "Not supported hash type: %u\n", hashtype
);
543 case 0: /* reserved */
545 return SADB_AALG_NONE
;
548 "Invalid hash type: %u\n", hashtype
);
554 /* IPSECDOI_ESP -> SADB_EALG */
556 ipsecdoi2pfkey_ealg(t_id
)
560 case IPSECDOI_ESP_DES_IV64
: /* sa_flags |= SADB_X_EXT_OLD */
561 return SADB_EALG_DESCBC
;
562 case IPSECDOI_ESP_DES
:
563 return SADB_EALG_DESCBC
;
564 case IPSECDOI_ESP_3DES
:
565 return SADB_EALG_3DESCBC
;
566 #ifdef SADB_X_EALG_RC5CBC
567 case IPSECDOI_ESP_RC5
:
568 return SADB_X_EALG_RC5CBC
;
570 case IPSECDOI_ESP_CAST
:
571 return SADB_X_EALG_CAST128CBC
;
572 case IPSECDOI_ESP_BLOWFISH
:
573 return SADB_X_EALG_BLOWFISHCBC
;
574 case IPSECDOI_ESP_DES_IV32
: /* flags |= (SADB_X_EXT_OLD|
576 return SADB_EALG_DESCBC
;
577 case IPSECDOI_ESP_NULL
:
578 return SADB_EALG_NULL
;
579 #ifdef SADB_X_EALG_AESCBC
580 case IPSECDOI_ESP_AES
:
581 return SADB_X_EALG_AESCBC
;
583 #ifdef SADB_X_EALG_TWOFISHCBC
584 case IPSECDOI_ESP_TWOFISH
:
585 return SADB_X_EALG_TWOFISHCBC
;
589 case IPSECDOI_ESP_3IDEA
:
590 case IPSECDOI_ESP_IDEA
:
591 case IPSECDOI_ESP_RC4
:
593 "Not supported transform: %u\n", t_id
);
596 case 0: /* reserved */
599 "Invalid transform id: %u\n", t_id
);
605 /* IPCOMP -> SADB_CALG */
607 ipsecdoi2pfkey_calg(t_id
)
611 case IPSECDOI_IPCOMP_OUI
:
612 return SADB_X_CALG_OUI
;
613 case IPSECDOI_IPCOMP_DEFLATE
:
614 return SADB_X_CALG_DEFLATE
;
615 case IPSECDOI_IPCOMP_LZS
:
616 return SADB_X_CALG_LZS
;
618 case 0: /* reserved */
621 "Invalid transform id: %u\n", t_id
);
627 /* IPSECDOI_PROTO -> SADB_SATYPE */
629 ipsecdoi2pfkey_proto(proto
)
633 case IPSECDOI_PROTO_IPSEC_AH
:
634 return SADB_SATYPE_AH
;
635 case IPSECDOI_PROTO_IPSEC_ESP
:
636 return SADB_SATYPE_ESP
;
637 case IPSECDOI_PROTO_IPCOMP
:
638 return SADB_X_SATYPE_IPCOMP
;
642 "Invalid ipsec_doi proto: %u\n", proto
);
649 ipsecdoi2pfkey_alg(algclass
, type
)
650 u_int algclass
, type
;
653 case IPSECDOI_ATTR_AUTH
:
654 return ipsecdoi2pfkey_aalg(type
);
655 case IPSECDOI_PROTO_IPSEC_ESP
:
656 return ipsecdoi2pfkey_ealg(type
);
657 case IPSECDOI_PROTO_IPCOMP
:
658 return ipsecdoi2pfkey_calg(type
);
661 "Invalid ipsec_doi algclass: %u\n", algclass
);
667 /* SADB_SATYPE -> IPSECDOI_PROTO */
669 pfkey2ipsecdoi_proto(satype
)
674 return IPSECDOI_PROTO_IPSEC_AH
;
675 case SADB_SATYPE_ESP
:
676 return IPSECDOI_PROTO_IPSEC_ESP
;
677 case SADB_X_SATYPE_IPCOMP
:
678 return IPSECDOI_PROTO_IPCOMP
;
682 "Invalid pfkey proto: %u\n", satype
);
688 /* IPSECDOI_ATTR_ENC_MODE -> IPSEC_MODE */
690 ipsecdoi2pfkey_mode(mode
)
694 case IPSECDOI_ATTR_ENC_MODE_TUNNEL
:
696 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
:
697 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
:
699 return IPSEC_MODE_TUNNEL
;
700 case IPSECDOI_ATTR_ENC_MODE_TRNS
:
702 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
:
703 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
:
705 return IPSEC_MODE_TRANSPORT
;
707 plog(ASL_LEVEL_ERR
, "Invalid mode type: %u\n", mode
);
713 /* IPSECDOI_ATTR_ENC_MODE -> IPSEC_MODE */
715 pfkey2ipsecdoi_mode(mode
)
719 case IPSEC_MODE_TUNNEL
:
720 return IPSECDOI_ATTR_ENC_MODE_TUNNEL
;
721 case IPSEC_MODE_TRANSPORT
:
722 return IPSECDOI_ATTR_ENC_MODE_TRNS
;
724 return IPSECDOI_ATTR_ENC_MODE_ANY
;
726 plog(ASL_LEVEL_ERR
, "Invalid mode type: %u\n", mode
);
732 /* default key length for encryption algorithm */
734 keylen_aalg(hashtype
)
740 return SADB_AALG_NONE
;
742 res
= alg_ipsec_hmacdef_hashlen(hashtype
);
745 "invalid hmac algorithm %u.\n", hashtype
);
751 /* default key length for encryption algorithm */
753 keylen_ealg(enctype
, encklen
)
759 res
= alg_ipsec_encdef_keylen(enctype
, encklen
);
762 "invalid encryption algorithm %u.\n", enctype
);
769 pfkey_convertfromipsecdoi(iph2
, proto_id
, t_id
, hashtype
,
770 e_type
, e_keylen
, a_type
, a_keylen
, flags
)
771 phase2_handle_t
*iph2
;
783 case IPSECDOI_PROTO_IPSEC_ESP
:
784 if ((*e_type
= ipsecdoi2pfkey_ealg(t_id
)) == ~0)
786 if ((*e_keylen
= keylen_ealg(t_id
, *e_keylen
)) == ~0)
790 if ((*a_type
= ipsecdoi2pfkey_aalg(hashtype
)) == ~0)
792 if ((*a_keylen
= keylen_aalg(hashtype
)) == ~0)
796 if (*e_type
== SADB_EALG_NONE
) {
797 plog(ASL_LEVEL_ERR
, "no ESP algorithm.\n");
802 case IPSECDOI_PROTO_IPSEC_AH
:
803 if ((*a_type
= ipsecdoi2pfkey_aalg(hashtype
)) == ~0)
805 if ((*a_keylen
= keylen_aalg(hashtype
)) == ~0)
809 if (t_id
== IPSECDOI_ATTR_AUTH_HMAC_MD5
810 && hashtype
== IPSECDOI_ATTR_AUTH_KPDK
) {
811 /* AH_MD5 + Auth(KPDK) = RFC1826 keyed-MD5 */
812 *a_type
= SADB_X_AALG_MD5
;
813 *flags
|= SADB_X_EXT_OLD
;
815 *e_type
= SADB_EALG_NONE
;
817 if (*a_type
== SADB_AALG_NONE
) {
818 plog(ASL_LEVEL_ERR
, "no AH algorithm.\n");
823 case IPSECDOI_PROTO_IPCOMP
:
824 if ((*e_type
= ipsecdoi2pfkey_calg(t_id
)) == ~0)
828 *flags
= SADB_X_EXT_RAWCPI
;
830 *a_type
= SADB_AALG_NONE
;
832 if (*e_type
== SADB_X_CALG_NONE
) {
833 plog(ASL_LEVEL_ERR
, "no IPCOMP algorithm.\n");
839 plog(ASL_LEVEL_ERR
, "unknown IPsec protocol.\n");
850 /* called from scheduler */
852 pfkey_timeover_stub(p
)
856 pfkey_timeover((phase2_handle_t
*)p
);
861 phase2_handle_t
*iph2
;
864 "%s give up to get IPsec-SA due to time up to wait.\n",
865 saddrwop2str((struct sockaddr
*)iph2
->dst
));
866 SCHED_KILL(iph2
->sce
);
868 /* If initiator side, send error to kernel by SADB_ACQUIRE. */
869 if (iph2
->side
== INITIATOR
)
870 pk_sendeacquire(iph2
);
872 ike_session_unlink_phase2(iph2
);
878 /* send getspi message per ipsec protocol per remote address */
880 * the local address and remote address in ph1handle are dealed
881 * with destination address and source address respectively.
882 * Because SPI is decided by responder.
886 phase2_handle_t
*iph2
;
888 struct sockaddr_storage
*src
= NULL
, *dst
= NULL
;
892 u_int32_t minspi
, maxspi
;
895 if (iph2
->side
== INITIATOR
) {
897 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
900 if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
904 /* for mobile IPv6 */
905 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
906 ipsecdoi_transportmode(pp
)) {
914 for (pr
= pp
->head
; pr
!= NULL
; pr
= pr
->next
) {
917 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
920 "invalid proto_id %d\n", pr
->proto_id
);
923 /* this works around a bug in Linux kernel where it allocates 4 byte
925 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
933 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
936 "invalid encmode %d\n", pr
->encmode
);
940 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_getspi\n");
941 if (pfkey_send_getspi(
948 pr
->reqid_in
, 0, 0, iph2
->seq
, 0) < 0) {
950 "ipseclib failed send getspi (%s)\n",
955 plog(ASL_LEVEL_DEBUG
,
956 "pfkey GETSPI sent: %s\n",
957 sadbsecas2str(dst
, src
, satype
, 0, mode
));
964 * receive GETSPI from kernel.
970 struct sadb_msg
*msg
;
972 phase2_handle_t
*iph2
;
973 struct sockaddr_storage
*dst
;
975 int allspiok
, notfound
;
980 if (mhp
[SADB_EXT_SA
] == NULL
981 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
983 "Inappropriate sadb getspi message passed.\n");
986 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
987 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
];
988 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]); /* note SA dir */
990 /* the message has to be processed or not ? */
991 if (msg
->sadb_msg_pid
!= getpid()) {
992 plog(ASL_LEVEL_DEBUG
,
993 "%s message is not interesting "
994 "because pid %d is not mine.\n",
995 s_pfkey_type(msg
->sadb_msg_type
),
1000 iph2
= ike_session_getph2byseq(msg
->sadb_msg_seq
);
1002 plog(ASL_LEVEL_DEBUG
,
1003 "Seq %d of %s message not interesting.\n",
1005 s_pfkey_type(msg
->sadb_msg_type
));
1009 if (iph2
->is_dying
) {
1011 "Status mismatch Phase 2 dying (db:%d)\n",
1016 switch (iph2
->version
) {
1017 case ISAKMP_VERSION_NUMBER_IKEV1
:
1018 if (iph2
->status
!= IKEV1_STATE_QUICK_I_GETSPISENT
&&
1019 iph2
->status
!= IKEV1_STATE_QUICK_R_GETSPISENT
) {
1020 plog(ASL_LEVEL_ERR
, "Status mismatch (db:%d)\n", iph2
->status
);
1023 // check the underlying iph2->ph1
1025 if (!ike_session_update_ph2_ph1bind(iph2
)) {
1027 "Can't proceed with getspi for %s. no suitable ISAKMP-SA found \n",
1028 saddrwop2str((struct sockaddr
*)iph2
->dst
));
1029 ike_session_unlink_phase2(iph2
);
1035 plog(ASL_LEVEL_ERR
, "Internal error: invalid IKE major version %d\n", iph2
->version
);
1039 /* set SPI, and check to get all spi whether or not */
1042 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1043 pp
= iph2
->side
== INITIATOR
? iph2
->proposal
: iph2
->approval
;
1045 for (pr
= pp
->head
; pr
!= NULL
; pr
= pr
->next
) {
1046 if (pr
->proto_id
== proto_id
&& pr
->spi
== 0) {
1047 pr
->spi
= sa
->sadb_sa_spi
;
1049 plog(ASL_LEVEL_DEBUG
,
1050 "pfkey GETSPI succeeded: %s\n",
1051 sadbsecas2str(iph2
->dst
, iph2
->src
,
1052 msg
->sadb_msg_satype
,
1054 ipsecdoi2pfkey_mode(pr
->encmode
)));
1057 allspiok
= 0; /* not get all spi */
1062 "Get spi for unknown address %s\n",
1063 saddrwop2str((struct sockaddr
*)iph2
->dst
));
1064 ike_session_unlink_phase2(iph2
);
1069 switch (iph2
->version
) {
1070 case ISAKMP_VERSION_NUMBER_IKEV1
:
1071 if (isakmp_post_getspi(iph2
) < 0) {
1072 plog(ASL_LEVEL_ERR
, "IKEv1 post getspi failed.\n");
1073 ike_session_unlink_phase2(iph2
);
1088 phase2_handle_t
*iph2
;
1091 struct sockaddr_storage
*src
= NULL
, *dst
= NULL
;
1092 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1094 u_int64_t lifebyte
= 0;
1095 u_int wsize
= 4; /* XXX static size of window */
1097 struct ph2natt natt
;
1101 if (iph2
->approval
== NULL
) {
1103 "No approved SAs found.\n");
1106 if (iph2
->side
== INITIATOR
)
1107 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1108 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1111 /* for mobile IPv6 */
1112 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1113 ipsecdoi_transportmode(iph2
->approval
)) {
1121 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1122 /* validity check */
1123 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1126 "Invalid proto_id %d\n", pr
->proto_id
);
1129 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1130 /* IPCOMP has no replay window */
1133 #ifdef ENABLE_SAMODE_UNSPECIFIED
1134 mode
= IPSEC_MODE_ANY
;
1136 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1139 "Invalid encmode %d\n", pr
->encmode
);
1144 /* set algorithm type and key length */
1145 e_keylen
= pr
->head
->encklen
;
1146 authtype
= pr
->head
->authtype
;
1148 if (pfkey_convertfromipsecdoi(
1154 &a_type
, &a_keylen
, &flags
) < 0)
1158 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1164 //plog(ASL_LEVEL_DEBUG, "call pfkey_send_update\n");
1165 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_update: e_type %d, e_klen %d, a_type %d, a_klen %d\n",
1166 e_type
, e_keylen
, a_type
, a_keylen
);
1167 if (pr
->udp_encap
) {
1168 memset (&natt
, 0, sizeof (natt
));
1169 natt
.sport
= extract_port (iph2
->ph1
->remote
);
1170 flags
|= SADB_X_EXT_NATT
;
1171 if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1172 mode
== IPSEC_MODE_TRANSPORT
&&
1173 src
->ss_family
== AF_INET
) {
1174 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1175 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
) {
1176 // is mutually exclusive with SADB_X_EXT_NATT_KEEPALIVE
1177 flags
|= SADB_X_EXT_NATT_DETECTED_PEER
;
1179 } else if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1180 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1181 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1183 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
) {
1184 // is mutually exclusive with SADB_X_EXT_NATT_KEEPALIVE
1185 flags
|= SADB_X_EXT_NATT_DETECTED_PEER
;
1189 memset (&natt
, 0, sizeof (natt
));
1192 if (pfkey_send_update(
1202 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1203 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1204 iph2
->seq
, natt
.sport
, 0) < 0) {
1206 "libipsec failed send update (%s)\n",
1211 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_update\n");
1212 if (pfkey_send_update(
1222 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1223 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1224 iph2
->seq
, 0, 0) < 0) {
1226 "libipsec failed send update (%s)\n",
1230 #endif /* ENABLE_NATT */
1242 struct sadb_msg
*msg
;
1244 struct sockaddr_storage
*src
, *dst
;
1245 phase2_handle_t
*iph2
;
1246 u_int proto_id
, encmode
, sa_mode
;
1250 /* ignore this message because of local test mode. */
1256 || mhp
[SADB_EXT_SA
] == NULL
1257 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1258 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1260 "inappropriate sadb update message passed.\n");
1263 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
1264 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1265 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1266 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1268 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1270 : (ALIGNED_CAST(struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1272 /* the message has to be processed or not ? */
1273 if (msg
->sadb_msg_pid
!= getpid()) {
1274 plog(ASL_LEVEL_DEBUG
,
1275 "%s message is not interesting "
1276 "because pid %d is not mine.\n",
1277 s_pfkey_type(msg
->sadb_msg_type
),
1282 iph2
= ike_session_getph2byseq(msg
->sadb_msg_seq
);
1284 plog(ASL_LEVEL_DEBUG
,
1285 "Seq %d of %s message not interesting.\n",
1287 s_pfkey_type(msg
->sadb_msg_type
));
1291 if (iph2
->is_dying
) {
1293 "Status mismatch Phase 2 dying (db:%d)\n",
1297 if (iph2
->status
!= IKEV1_STATE_QUICK_I_ADDSA
&&
1298 iph2
->status
!= IKEV1_STATE_QUICK_R_ADDSA
) {
1300 "Status mismatch (db:%d)\n",
1305 /* check to complete all keys ? */
1306 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1307 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1308 if (proto_id
== ~0) {
1310 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1313 encmode
= pfkey2ipsecdoi_mode(sa_mode
);
1314 if (encmode
== ~0) {
1316 "invalid encmode %d\n", sa_mode
);
1320 if (pr
->proto_id
== proto_id
1321 && pr
->spi
== sa
->sadb_sa_spi
) {
1323 plog(ASL_LEVEL_DEBUG
,
1324 "pfkey UPDATE succeeded: %s\n",
1325 sadbsecas2str(iph2
->dst
, iph2
->src
,
1326 msg
->sadb_msg_satype
,
1330 plog(ASL_LEVEL_INFO
,
1331 "IPsec-SA established: %s\n",
1332 sadbsecas2str(iph2
->dst
, iph2
->src
,
1333 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1344 /* turn off the timer for calling pfkey_timeover() */
1345 SCHED_KILL(iph2
->sce
);
1348 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_ESTABLISHED
);
1350 if (iph2
->side
== INITIATOR
) {
1351 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1352 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_SUCC
,
1353 CONSTSTR("Initiator, Quick-Mode"),
1356 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1357 IPSECSESSIONEVENTCODE_IKEV1_PH2_RESP_SUCC
,
1358 CONSTSTR("Responder, Quick-Mode"),
1362 ike_session_ph2_established(iph2
);
1364 IPSECLOGASLMSG("IPSec Phase 2 established (Initiated by %s).\n",
1365 (iph2
->side
== INITIATOR
)? "me" : "peer");
1368 gettimeofday(&iph2
->end
, NULL
);
1369 plog(ASL_LEVEL_NOTICE
, "%s(%s): %8.6f",
1370 "Phase 2", "quick", timedelta(&iph2
->start
, &iph2
->end
));
1375 iph2
->ph1
->ph2cnt
++;
1377 /* turn off schedule */
1379 SCHED_KILL(iph2
->scr
);
1382 * since we are going to reuse the phase2 handler, we need to
1383 * remain it and refresh all the references between ph1 and ph2 to use.
1385 ike_session_unbindph12(iph2
); //%%%%% fix this
1387 iph2
->sce
= sched_new(iph2
->approval
->lifetime
,
1388 isakmp_ph2expire_stub
, iph2
);
1390 plog(ASL_LEVEL_DEBUG
, "===\n");
1399 phase2_handle_t
*iph2
;
1402 struct sockaddr_storage
*src
= NULL
, *dst
= NULL
;
1403 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1405 u_int64_t lifebyte
= 0;
1406 u_int wsize
= 4; /* XXX static size of window */
1408 struct ph2natt natt
;
1412 if (iph2
->approval
== NULL
) {
1414 "no approvaled SAs found.\n");
1417 if (iph2
->side
== INITIATOR
)
1418 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1419 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1422 /* for mobile IPv6 */
1423 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1424 ipsecdoi_transportmode(iph2
->approval
)) {
1432 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1433 /* validity check */
1434 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1437 "invalid proto_id %d\n", pr
->proto_id
);
1440 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1441 /* no replay window for IPCOMP */
1444 #ifdef ENABLE_SAMODE_UNSPECIFIED
1445 mode
= IPSEC_MODE_ANY
;
1447 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1450 "invalid encmode %d\n", pr
->encmode
);
1455 /* set algorithm type and key length */
1456 e_keylen
= pr
->head
->encklen
;
1457 authtype
= pr
->head
->authtype
;
1459 if (pfkey_convertfromipsecdoi(
1465 &a_type
, &a_keylen
, &flags
) < 0)
1469 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1475 //plog(ASL_LEVEL_DEBUG, "call pfkey_send_add\n");
1476 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_add: e_type %d, e_klen %d, a_type %d, a_klen %d\n",
1477 e_type
, e_keylen
, a_type
, a_keylen
);
1479 if (pr
->udp_encap
) {
1480 memset (&natt
, 0, sizeof (natt
));
1481 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1482 flags
|= SADB_X_EXT_NATT
;
1483 if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1484 mode
== IPSEC_MODE_TRANSPORT
&&
1485 src
->ss_family
== AF_INET
) {
1486 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1487 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
) {
1488 // is mutually exclusive with SADB_X_EXT_NATT_KEEPALIVE
1489 flags
|= SADB_X_EXT_NATT_DETECTED_PEER
;
1491 } else if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1492 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1493 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1495 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
) {
1496 // is mutually exclusive with SADB_X_EXT_NATT_KEEPALIVE
1497 flags
|= SADB_X_EXT_NATT_DETECTED_PEER
;
1501 memset (&natt
, 0, sizeof (natt
));
1503 /* Remove port information, that SA doesn't use it */
1518 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1519 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1520 iph2
->seq
,natt
.dport
, 0) < 0) {
1522 "libipsec failed send add (%s)\n",
1527 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_add\n");
1529 /* Remove port information, it is not used without NAT-T */
1543 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1544 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1545 iph2
->seq
, 0, 0) < 0) {
1547 "libipsec failed send add (%s)\n",
1551 #endif /* ENABLE_NATT */
1561 struct sadb_msg
*msg
;
1563 struct sockaddr_storage
*src
, *dst
;
1564 phase2_handle_t
*iph2
;
1567 /* ignore this message because of local test mode. */
1573 || mhp
[SADB_EXT_SA
] == NULL
1574 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1575 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1577 "inappropriate sadb add message passed.\n");
1580 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
1581 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1582 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1583 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1585 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1587 : (ALIGNED_CAST(struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1589 /* the message has to be processed or not ? */
1590 if (msg
->sadb_msg_pid
!= getpid()) {
1591 plog(ASL_LEVEL_DEBUG
,
1592 "%s message is not interesting "
1593 "because pid %d is not mine.\n",
1594 s_pfkey_type(msg
->sadb_msg_type
),
1599 iph2
= ike_session_getph2byseq(msg
->sadb_msg_seq
);
1601 plog(ASL_LEVEL_DEBUG
,
1602 "seq %d of %s message not interesting.\n",
1604 s_pfkey_type(msg
->sadb_msg_type
));
1608 * NOTE don't update any status of phase2 handle
1609 * because they must be updated by SADB_UPDATE message
1612 plog(ASL_LEVEL_INFO
,
1613 "IPsec-SA established: %s\n",
1614 sadbsecas2str(iph2
->src
, iph2
->dst
,
1615 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1617 ike_session_cleanup_other_established_ph2s(iph2
->parent_session
, iph2
);
1619 #ifdef ENABLE_VPNCONTROL_PORT
1621 vpncontrol_notify_phase_change(0, FROM_LOCAL
, NULL
, iph2
);
1625 plog(ASL_LEVEL_DEBUG
, "===\n");
1633 struct sadb_msg
*msg
;
1635 struct sockaddr_storage
*src
, *dst
;
1636 phase2_handle_t
*iph2
;
1637 u_int proto_id
, sa_mode
;
1641 || mhp
[SADB_EXT_SA
] == NULL
1642 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1643 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1644 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
1645 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
1647 "inappropriate sadb expire message passed.\n");
1650 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
1651 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1652 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1653 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1655 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1657 : (ALIGNED_CAST(struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1659 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1660 if (proto_id
== ~0) {
1662 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1666 plog(ASL_LEVEL_INFO
,
1667 "IPsec-SA expired: %s\n",
1668 sadbsecas2str(src
, dst
,
1669 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1671 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1674 * Ignore it because two expire messages are come up.
1675 * phase2 handler has been deleted already when 2nd message
1678 plog(ASL_LEVEL_DEBUG
,
1679 "no such a SA found: %s\n",
1680 sadbsecas2str(src
, dst
,
1681 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1685 if (iph2
->is_dying
|| !FSM_STATE_IS_ESTABLISHED(iph2
->status
)) {
1687 * If the status is not equal to PHASE2ST_ESTABLISHED,
1688 * racoon ignores this expire message. There are two reason.
1689 * One is that the phase 2 probably starts because there is
1690 * a potential that racoon receives the acquire message
1691 * without receiving a expire message. Another is that racoon
1692 * may receive the multiple expire messages from the kernel.
1694 plog(ASL_LEVEL_WARNING
,
1695 "The expire message is received but the handler %s (status = 0x%x).\n",
1696 iph2
->is_dying
? "is dying" : "has not been established", iph2
->status
);
1700 /* turn off the timer for calling isakmp_ph2expire() */
1701 SCHED_KILL(iph2
->sce
);
1703 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
1705 /* INITIATOR, begin phase 2 exchange only if there's no other established ph2. */
1706 /* allocate buffer for status management of pfkey message */
1707 if (iph2
->side
== INITIATOR
&&
1708 !ike_session_has_other_established_ph2(iph2
->parent_session
, iph2
) &&
1709 !ike_session_drop_rekey(iph2
->parent_session
, IKE_SESSION_REKEY_TYPE_PH2
)) {
1711 ike_session_initph2(iph2
);
1713 /* start isakmp initiation by using ident exchange */
1714 if (isakmp_post_acquire(iph2
) < 0) {
1716 "failed to begin ipsec sa "
1717 "re-negotiation.\n");
1718 ike_session_unlink_phase2(iph2
);
1727 /* If not received SADB_EXPIRE, INITIATOR delete ph2handle. */
1728 /* RESPONDER always delete ph2handle, keep silent. RESPONDER doesn't
1729 * manage IPsec SA, so delete the list */
1730 ike_session_unlink_phase2(iph2
);
1739 struct sadb_msg
*msg
;
1740 struct sadb_x_policy
*xpl
;
1741 struct secpolicy
*sp_out
= NULL
, *sp_in
= NULL
;
1742 phase2_handle_t
*iph2
;
1743 struct sockaddr_storage
*src
, *dst
;
1744 ike_session_t
*session
= NULL
;
1745 struct remoteconf
*rmconf
;
1747 /* ignore this message because of local test mode. */
1753 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1754 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1755 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1757 "inappropriate sadb acquire message passed.\n");
1760 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
1761 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
1762 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1763 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1765 /* ignore if type is not IPSEC_POLICY_IPSEC */
1766 if (xpl
->sadb_x_policy_type
!= IPSEC_POLICY_IPSEC
) {
1767 plog(ASL_LEVEL_DEBUG
,
1768 "ignore ACQUIRE message. type is not IPsec.\n");
1772 /* ignore it if src is multicast address */
1774 struct sockaddr_storage
*sa
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1776 if ((sa
->ss_family
== AF_INET
1777 && IN_MULTICAST(ntohl(((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
)))
1779 || (sa
->ss_family
== AF_INET6
1780 && IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)sa
)->sin6_addr
))
1783 plog(ASL_LEVEL_DEBUG
,
1784 "ignore due to multicast address: %s.\n",
1785 saddrwop2str((struct sockaddr
*)sa
));
1790 /* ignore, if we do not listen on source address */
1793 * - if we'll contact peer from address we do not listen -
1794 * we will be unable to complete negotiation;
1795 * - if we'll negotiate using address we're listening -
1796 * remote peer will send packets to address different
1797 * than one in the policy, so kernel will drop them;
1798 * => therefore this acquire is not for us! --Aidas
1800 // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
1801 struct sockaddr_storage
*sa
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1805 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1806 str
= saddr2str((struct sockaddr
*)p
->addr
);
1807 plog(ASL_LEVEL_DEBUG
,
1808 "checking listen addrs: %s", str
);
1810 if (!cmpsaddrwop(p
->addr
, sa
)) {
1817 plog(ASL_LEVEL_DEBUG
,
1818 "ignore because do not listen on source address : %s.\n",
1819 saddrwop2str((struct sockaddr
*)sa
));
1825 * If there is a phase 2 handler against the policy identifier in
1826 * the acquire message, and if
1827 * 1. its state is less than PHASE2ST_ESTABLISHED, then racoon
1828 * should ignore such a acquire message because the phase 2
1829 * is just negotiating.
1830 * 2. its state is equal to PHASE2ST_ESTABLISHED, then racoon
1831 * has to process such a acquire message because racoon may
1832 * have lost the expire message.
1834 iph2
= ike_session_getph2byid(src
, dst
, xpl
->sadb_x_policy_id
);
1836 session
= iph2
->parent_session
;
1837 if (!FSM_STATE_IS_ESTABLISHED(iph2
->status
)) {
1838 plog(ASL_LEVEL_DEBUG
,
1839 "ignore the acquire because ph2 found\n");
1842 if (FSM_STATE_IS_EXPIRED(iph2
->status
))
1847 /* search for proper policyindex */
1848 sp_out
= getspbyspid(xpl
->sadb_x_policy_id
);
1849 if (sp_out
== NULL
) {
1850 plog(ASL_LEVEL_ERR
, "no policy found: id:%d.\n",
1851 xpl
->sadb_x_policy_id
);
1854 plog(ASL_LEVEL_DEBUG
,
1855 "suitable outbound SP found: %s.\n", spidx2str(&sp_out
->spidx
));
1857 /* get inbound policy */
1859 struct policyindex spidx
;
1861 spidx
.dir
= IPSEC_DIR_INBOUND
;
1862 memcpy(&spidx
.src
, &sp_out
->spidx
.dst
, sizeof(spidx
.src
));
1863 memcpy(&spidx
.dst
, &sp_out
->spidx
.src
, sizeof(spidx
.dst
));
1864 spidx
.prefs
= sp_out
->spidx
.prefd
;
1865 spidx
.prefd
= sp_out
->spidx
.prefs
;
1866 spidx
.ul_proto
= sp_out
->spidx
.ul_proto
;
1868 sp_in
= getsp(&spidx
);
1870 plog(ASL_LEVEL_DEBUG
,
1871 "Suitable inbound SP found: %s.\n",
1872 spidx2str(&sp_in
->spidx
));
1874 plog(ASL_LEVEL_NOTICE
,
1875 "No in-bound policy found: %s\n",
1880 /* allocate a phase 2 */
1881 rmconf
= getrmconf(dst
);
1882 if (rmconf
== NULL
) {
1883 plog(ASL_LEVEL_ERR
, "No configuration found for %s.\n",
1884 saddrwop2str((struct sockaddr
*)dst
));
1888 iph2
= ike_session_newph2(rmconf
->ike_version
, PHASE2_TYPE_SA
);
1891 "Failed to allocate Phase 2 entry.\n");
1894 plog(ASL_LEVEL_DEBUG
, "Got new Phase 2 version %d\n", iph2
->version
);
1895 iph2
->version
= rmconf
->ike_version
;
1896 iph2
->side
= INITIATOR
;
1897 iph2
->spid
= xpl
->sadb_x_policy_id
;
1899 iph2
->satype
= msg
->sadb_msg_satype
;
1900 iph2
->seq
= msg
->sadb_msg_seq
;
1901 vpncontrol_set_nat64_prefix(&iph2
->nat64_prefix
);
1902 /* set end addresses of SA */
1903 // Wcast_align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
1904 iph2
->src
= dupsaddr(ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]));
1905 if (iph2
->src
== NULL
) {
1906 ike_session_delph2(iph2
);
1909 iph2
->dst
= dupsaddr(ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]));
1910 if (iph2
->dst
== NULL
) {
1911 ike_session_delph2(iph2
);
1915 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1916 fsm_set_state(&iph2
->status
, IKEV1_STATE_QUICK_I_START
);
1919 plog(ASL_LEVEL_DEBUG
,
1920 "new acquire %s\n", spidx2str(&sp_out
->spidx
));
1924 vchar_t
*idsrc
, *iddst
;
1926 idsrc
= ipsecdoi_sockaddr2id(&sp_out
->spidx
.src
,
1927 sp_out
->spidx
.prefs
, sp_out
->spidx
.ul_proto
);
1928 if (idsrc
== NULL
) {
1930 "failed to get ID for %s\n",
1931 spidx2str(&sp_out
->spidx
));
1932 ike_session_delph2(iph2
);
1935 iddst
= ipsecdoi_sockaddr2id(&sp_out
->spidx
.dst
,
1936 sp_out
->spidx
.prefd
, sp_out
->spidx
.ul_proto
);
1937 if (iddst
== NULL
) {
1939 "failed to get ID for %s\n",
1940 spidx2str(&sp_out
->spidx
));
1942 ike_session_delph2(iph2
);
1945 iph2
->sainfo
= getsainfo(idsrc
, iddst
, NULL
, 0);
1948 if (iph2
->sainfo
== NULL
) {
1950 "failed to get sainfo.\n");
1951 ike_session_delph2(iph2
);
1953 /* XXX should use the algorithm list from register message */
1956 retain_sainfo(iph2
->sainfo
);
1958 if (set_proposal_from_policy(iph2
, sp_out
, sp_in
) < 0) {
1960 "failed to create saprop.\n");
1961 ike_session_delph2(iph2
);
1965 if (session
== NULL
)
1966 session
= ike_session_get_session(iph2
->src
, iph2
->dst
, 1, NULL
);
1967 if (session
== NULL
)
1970 if (ike_session_link_phase2(session
, iph2
))
1971 fatal_error(-1); //????? fix ???
1973 /* start isakmp initiation by using ident exchange */
1974 /* XXX should be looped if there are multiple phase 2 handler. */
1975 if (isakmp_post_acquire(iph2
) < 0) {
1977 "failed to begin ipsec sa negotiation.\n");
1981 #if !TARGET_OS_EMBEDDED
1982 if ( lcconf
->vt
== NULL
){
1983 if (!(lcconf
->vt
= vproc_transaction_begin(NULL
)))
1985 "vproc_transaction_begin returns NULL.\n");
1993 ike_session_unlink_phase2(iph2
);
2001 struct sadb_msg
*msg
;
2003 struct sockaddr_storage
*src
, *dst
;
2004 phase2_handle_t
*iph2
= NULL
;
2007 /* ignore this message because of local test mode. */
2013 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2014 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
2016 "inappropriate sadb delete message passed.\n");
2019 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
2020 sa
= ALIGNED_CAST(struct sadb_sa
*)mhp
[SADB_EXT_SA
];
2021 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
2022 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
2024 /* the message has to be processed or not ? */
2025 if (msg
->sadb_msg_pid
== getpid()) {
2026 plog(ASL_LEVEL_DEBUG
,
2027 "%s message is not interesting "
2028 "because the message was originated by me.\n",
2029 s_pfkey_type(msg
->sadb_msg_type
));
2033 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
2034 if (proto_id
== ~0) {
2036 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
2040 plog(ASL_LEVEL_DEBUG
, "SADB delete message: proto-id %d\n", proto_id
);
2041 plog(ASL_LEVEL_DEBUG
, "src: %s\n", saddr2str((struct sockaddr
*)src
));
2042 plog(ASL_LEVEL_DEBUG
, "dst: %s\n", saddr2str((struct sockaddr
*)dst
));
2045 ike_session_deleteallph2(src
, dst
, proto_id
);
2046 ike_session_deleteallph1(src
, dst
);
2050 iph2
= ike_session_getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
2054 "no iph2 found: %s\n",
2055 sadbsecas2str(src
, dst
, msg
->sadb_msg_satype
,
2056 sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2061 "pfkey DELETE received: %s\n",
2062 sadbsecas2str(iph2
->src
, iph2
->dst
,
2063 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2065 /* send delete information */
2067 /* TODO: Look into handling this properly. Currently, if we get here, we can end up sending delete messages to the server for their own SAs, which is rejected. */
2068 /*if (FSM_STATE_IS_ESTABLISHED(iph2->status))
2069 isakmp_info_send_d2(iph2);
2071 ike_session_cleanup_ph1s_by_ph2(iph2);
2072 ike_session_unlink_phase2(iph2);*/
2082 if (mhp
[0] == NULL
) {
2084 "inappropriate sadb flush message passed.\n");
2088 ike_session_flush_all_phase2(false);
2089 ike_session_flush_all_phase1(false);
2095 getsadbpolicy(policy0
, policylen0
, type
, iph2
)
2097 int *policylen0
, type
;
2098 phase2_handle_t
*iph2
;
2100 struct policyindex
*spidx
= iph2
->spidx_gen
;
2101 struct sadb_x_policy
*xpl
;
2102 struct sadb_x_ipsecrequest
*xisr
;
2109 /* get policy buffer size */
2110 policylen
= sizeof(struct sadb_x_policy
);
2111 if (type
!= SADB_X_SPDDELETE
) {
2112 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2113 xisrlen
= sizeof(*xisr
);
2114 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2115 xisrlen
+= (sysdep_sa_len((struct sockaddr
*)iph2
->src
)
2116 + sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
2119 policylen
+= PFKEY_ALIGN8(xisrlen
);
2123 /* make policy structure */
2124 policy
= racoon_malloc(policylen
);
2127 "buffer allocation failed.\n");
2131 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)policy
;
2132 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(policylen
);
2133 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2134 xpl
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2135 xpl
->sadb_x_policy_dir
= spidx
->dir
;
2136 xpl
->sadb_x_policy_id
= 0;
2137 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2138 xpl
->sadb_x_policy_priority
= PRIORITY_DEFAULT
;
2141 /* no need to append policy information any more if type is SPDDELETE */
2142 if (type
== SADB_X_SPDDELETE
)
2145 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
2147 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2149 satype
= doi2ipproto(pr
->proto_id
);
2152 "invalid proto_id %d\n", pr
->proto_id
);
2155 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
2158 "invalid encmode %d\n", pr
->encmode
);
2163 * the policy level cannot be unique because the policy
2164 * is defined later than SA, so req_id cannot be bound to SA.
2166 xisr
->sadb_x_ipsecrequest_proto
= satype
;
2167 xisr
->sadb_x_ipsecrequest_mode
= mode
;
2168 xisr
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2169 xisr
->sadb_x_ipsecrequest_reqid
= 0;
2170 p
= (caddr_t
)(xisr
+ 1);
2172 xisrlen
= sizeof(*xisr
);
2174 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2175 int src_len
, dst_len
;
2177 src_len
= sysdep_sa_len((struct sockaddr
*)iph2
->src
);
2178 dst_len
= sysdep_sa_len((struct sockaddr
*)iph2
->dst
);
2179 xisrlen
+= src_len
+ dst_len
;
2181 memcpy(p
, iph2
->src
, src_len
);
2184 memcpy(p
, iph2
->dst
, dst_len
);
2188 xisr
->sadb_x_ipsecrequest_len
= PFKEY_ALIGN8(xisrlen
);
2193 *policylen0
= policylen
;
2199 racoon_free(policy
);
2205 pk_sendspdupdate2(iph2
)
2206 phase2_handle_t
*iph2
;
2208 struct policyindex
*spidx
= iph2
->spidx_gen
;
2209 caddr_t policy
= NULL
;
2211 u_int64_t ltime
, vtime
;
2213 ltime
= iph2
->approval
->lifetime
;
2216 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDUPDATE
, iph2
)) {
2218 "getting sadb policy failed.\n");
2222 if (pfkey_send_spdupdate2(
2230 policy
, policylen
, 0) < 0) {
2232 "libipsec failed send spdupdate2 (%s)\n",
2236 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_spdupdate2\n");
2240 racoon_free(policy
);
2246 pk_recvspdupdate(mhp
)
2249 struct sadb_address
*saddr
, *daddr
;
2250 struct sadb_x_policy
*xpl
;
2251 struct policyindex spidx
;
2252 struct secpolicy
*sp
;
2256 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2257 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2258 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2260 "inappropriate sadb spdupdate message passed.\n");
2263 saddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
2264 daddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2265 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2267 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2268 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2271 saddr
->sadb_address_prefixlen
,
2272 daddr
->sadb_address_prefixlen
,
2273 saddr
->sadb_address_proto
,
2274 xpl
->sadb_x_policy_priority
,
2277 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2280 saddr
->sadb_address_prefixlen
,
2281 daddr
->sadb_address_prefixlen
,
2282 saddr
->sadb_address_proto
,
2289 "such policy does not already exist: \"%s\"\n",
2296 if (addnewsp(mhp
) < 0)
2303 * this function has to be used by responder side.
2306 pk_sendspdadd2(iph2
)
2307 phase2_handle_t
*iph2
;
2309 struct policyindex
*spidx
= iph2
->spidx_gen
;
2310 caddr_t policy
= NULL
;
2312 u_int64_t ltime
, vtime
;
2314 ltime
= iph2
->approval
->lifetime
;
2317 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDADD
, iph2
)) {
2319 "getting sadb policy failed.\n");
2323 if (pfkey_send_spdadd2(
2331 policy
, policylen
, 0) < 0) {
2333 "libipsec failed send spdadd2 (%s)\n",
2337 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_spdadd2\n");
2341 racoon_free(policy
);
2350 struct sadb_address
*saddr
, *daddr
;
2351 struct sadb_x_policy
*xpl
;
2352 struct policyindex spidx
;
2353 struct secpolicy
*sp
;
2357 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2358 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2359 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2361 "inappropriate sadb spdadd message passed.\n");
2364 saddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
2365 daddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2366 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2368 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2369 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2372 saddr
->sadb_address_prefixlen
,
2373 daddr
->sadb_address_prefixlen
,
2374 saddr
->sadb_address_proto
,
2375 xpl
->sadb_x_policy_priority
,
2378 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2381 saddr
->sadb_address_prefixlen
,
2382 daddr
->sadb_address_prefixlen
,
2383 saddr
->sadb_address_proto
,
2390 "such policy already exists. "
2391 "anyway replace it: %s\n",
2397 if (addnewsp(mhp
) < 0)
2404 * this function has to be used by responder side.
2407 pk_sendspddelete(iph2
)
2408 phase2_handle_t
*iph2
;
2410 struct policyindex
*spidx
= iph2
->spidx_gen
;
2411 caddr_t policy
= NULL
;
2414 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDDELETE
, iph2
)) {
2416 "getting sadb policy failed.\n");
2420 if (pfkey_send_spddelete(
2427 policy
, policylen
, 0) < 0) {
2429 "libipsec failed send spddelete (%s)\n",
2433 plog(ASL_LEVEL_DEBUG
, "call pfkey_send_spddelete\n");
2437 racoon_free(policy
);
2443 pk_recvspddelete(mhp
)
2446 struct sadb_address
*saddr
, *daddr
;
2447 struct sadb_x_policy
*xpl
;
2448 struct policyindex spidx
;
2449 struct secpolicy
*sp
;
2453 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2454 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2455 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2457 "inappropriate sadb spddelete message passed.\n");
2460 saddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
2461 daddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2462 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2464 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2465 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2468 saddr
->sadb_address_prefixlen
,
2469 daddr
->sadb_address_prefixlen
,
2470 saddr
->sadb_address_proto
,
2471 xpl
->sadb_x_policy_priority
,
2474 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2477 saddr
->sadb_address_prefixlen
,
2478 daddr
->sadb_address_prefixlen
,
2479 saddr
->sadb_address_proto
,
2486 "no policy found: %s\n",
2491 ike_session_purgephXbyspid(xpl
->sadb_x_policy_id
, true);
2500 pk_recvspdexpire(mhp
)
2503 struct sadb_address
*saddr
, *daddr
;
2504 struct sadb_x_policy
*xpl
;
2505 struct policyindex spidx
;
2506 struct secpolicy
*sp
;
2510 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2511 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2512 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2514 "inappropriate sadb spdexpire message passed.\n");
2517 saddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
2518 daddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2519 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2521 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2522 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2525 saddr
->sadb_address_prefixlen
,
2526 daddr
->sadb_address_prefixlen
,
2527 saddr
->sadb_address_proto
,
2528 xpl
->sadb_x_policy_priority
,
2531 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2534 saddr
->sadb_address_prefixlen
,
2535 daddr
->sadb_address_prefixlen
,
2536 saddr
->sadb_address_proto
,
2543 "no policy found: %s\n",
2548 ike_session_purgephXbyspid(xpl
->sadb_x_policy_id
, false);
2561 if (mhp
[0] == NULL
) {
2563 "inappropriate sadb spdget message passed.\n");
2574 struct sadb_msg
*msg
;
2575 struct sadb_address
*saddr
, *daddr
;
2576 struct sadb_x_policy
*xpl
;
2577 struct policyindex spidx
;
2578 struct secpolicy
*sp
;
2581 if (mhp
[0] == NULL
) {
2583 "inappropriate sadb spddump message passed.\n");
2586 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
2588 saddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2589 daddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2590 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2592 if (saddr
== NULL
|| daddr
== NULL
|| xpl
== NULL
) {
2594 "inappropriate sadb spddump message passed.\n");
2598 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2599 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2602 saddr
->sadb_address_prefixlen
,
2603 daddr
->sadb_address_prefixlen
,
2604 saddr
->sadb_address_proto
,
2605 xpl
->sadb_x_policy_priority
,
2608 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2611 saddr
->sadb_address_prefixlen
,
2612 daddr
->sadb_address_prefixlen
,
2613 saddr
->sadb_address_proto
,
2620 "such policy already exists. "
2621 "anyway replace it: %s\n",
2627 if (addnewsp(mhp
) < 0)
2634 pk_recvspdflush(mhp
)
2638 if (mhp
[0] == NULL
) {
2640 "inappropriate sadb spdflush message passed.\n");
2644 ike_session_flush_all_phase2(false);
2645 ike_session_flush_all_phase1(false);
2652 * send error against acquire message to kenrel.
2655 pk_sendeacquire(iph2
)
2656 phase2_handle_t
*iph2
;
2658 struct sadb_msg
*newmsg
;
2661 len
= sizeof(struct sadb_msg
);
2662 newmsg
= racoon_calloc(1, len
);
2663 if (newmsg
== NULL
) {
2665 "failed to get buffer to send acquire.\n");
2669 memset(newmsg
, 0, len
);
2670 newmsg
->sadb_msg_version
= PF_KEY_V2
;
2671 newmsg
->sadb_msg_type
= SADB_ACQUIRE
;
2672 newmsg
->sadb_msg_errno
= ENOENT
; /* XXX */
2673 newmsg
->sadb_msg_satype
= iph2
->satype
;
2674 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
2675 newmsg
->sadb_msg_reserved
= 0;
2676 newmsg
->sadb_msg_seq
= iph2
->seq
;
2677 newmsg
->sadb_msg_pid
= (u_int32_t
)getpid();
2680 len
= pfkey_send(lcconf
->sock_pfkey
, newmsg
, len
);
2682 racoon_free(newmsg
);
2688 pk_sendget_inbound_sastats(ike_session_t
*session
)
2690 u_int32_t max_stats
;
2694 plog(ASL_LEVEL_DEBUG
, "invalid args in %s \n", __FUNCTION__
);
2698 session
->traffic_monitor
.num_in_curr_req
= 0;
2699 bzero(session
->traffic_monitor
.in_curr_req
, sizeof(session
->traffic_monitor
.in_curr_req
));
2700 max_stats
= (sizeof(session
->traffic_monitor
.in_curr_req
) / sizeof(session
->traffic_monitor
.in_curr_req
[0]));
2703 if ((session
->traffic_monitor
.num_in_curr_req
= ike_session_get_sas_for_stats(session
,
2706 session
->traffic_monitor
.in_curr_req
,
2708 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2710 //plog(ASL_LEVEL_DEBUG, "about to call %s\n", __FUNCTION__);
2712 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2717 session
->traffic_monitor
.in_curr_req
,
2718 session
->traffic_monitor
.num_in_curr_req
) < 0) {
2721 //plog(ASL_LEVEL_DEBUG, "%s successful\n", __FUNCTION__);
2723 return session
->traffic_monitor
.num_in_curr_req
;
2729 pk_sendget_outbound_sastats(ike_session_t
*session
)
2731 u_int32_t max_stats
;
2735 plog(ASL_LEVEL_DEBUG
, "invalid args in %s \n", __FUNCTION__
);
2739 session
->traffic_monitor
.num_out_curr_req
= 0;
2740 bzero(session
->traffic_monitor
.out_curr_req
, sizeof(session
->traffic_monitor
.out_curr_req
));
2741 max_stats
= (sizeof(session
->traffic_monitor
.out_curr_req
) / sizeof(session
->traffic_monitor
.out_curr_req
[0]));
2744 if ((session
->traffic_monitor
.num_out_curr_req
= ike_session_get_sas_for_stats(session
,
2747 session
->traffic_monitor
.out_curr_req
,
2749 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2751 //plog(ASL_LEVEL_DEBUG, "about to call %s\n", __FUNCTION__);
2753 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2758 session
->traffic_monitor
.out_curr_req
,
2759 session
->traffic_monitor
.num_out_curr_req
) < 0) {
2762 //plog(ASL_LEVEL_DEBUG, "%s successful\n", __FUNCTION__);
2764 return session
->traffic_monitor
.num_out_curr_req
;
2770 * receive GETSPDSTAT from kernel.
2773 pk_recvgetsastat(mhp
)
2776 struct sadb_msg
*msg
;
2777 struct sadb_session_id
*session_id
;
2778 struct sadb_sastat
*stat_resp
;
2779 ike_session_t
*session
;
2781 /* validity check */
2782 if (mhp
[0] == NULL
||
2783 mhp
[SADB_EXT_SESSION_ID
] == NULL
||
2784 mhp
[SADB_EXT_SASTAT
] == NULL
) {
2786 "inappropriate sadb getsastat response.\n");
2789 msg
= ALIGNED_CAST(struct sadb_msg
*)mhp
[0]; // Wcast-align fix (void*) - mhp contains pointers to structs in an aligned buffer
2790 session_id
= ALIGNED_CAST(struct sadb_session_id
*)mhp
[SADB_EXT_SESSION_ID
];
2791 stat_resp
= ALIGNED_CAST(struct sadb_sastat
*)mhp
[SADB_EXT_SASTAT
];
2793 /* the message has to be processed or not ? */
2794 if (msg
->sadb_msg_pid
!= getpid()) {
2795 plog(ASL_LEVEL_DEBUG
,
2796 "%s message is not interesting "
2797 "because pid %d is not mine.\n",
2798 s_pfkey_type(msg
->sadb_msg_type
),
2802 if (!session_id
->sadb_session_id_v
[0]) {
2803 plog(ASL_LEVEL_DEBUG
,
2804 "%s message is bad "
2805 "because session-id[0] is invalid.\n",
2806 s_pfkey_type(msg
->sadb_msg_type
));
2809 session
= ALIGNED_CAST(__typeof__(session
))session_id
->sadb_session_id_v
[0];
2811 if (!stat_resp
->sadb_sastat_list_len
) {
2812 plog(ASL_LEVEL_DEBUG
,
2813 "%s message is bad "
2814 "because it has no sastats.\n",
2815 s_pfkey_type(msg
->sadb_msg_type
));
2819 ike_session_update_traffic_idle_status(session
,
2820 stat_resp
->sadb_sastat_dir
,
2821 (struct sastat
*)(stat_resp
+ 1),
2822 stat_resp
->sadb_sastat_list_len
);
2827 * check if the algorithm is supported or not.
2832 pk_checkalg(class, calg
, keylen
)
2833 int class, calg
, keylen
;
2837 struct sadb_alg alg0
;
2839 switch (algclass2doi(class)) {
2840 case IPSECDOI_PROTO_IPSEC_ESP
:
2841 sup
= SADB_EXT_SUPPORTED_ENCRYPT
;
2843 case IPSECDOI_ATTR_AUTH
:
2844 sup
= SADB_EXT_SUPPORTED_AUTH
;
2846 case IPSECDOI_PROTO_IPCOMP
:
2850 "invalid algorithm class.\n");
2853 alg
= ipsecdoi2pfkey_alg(algclass2doi(class), algtype2doi(class, calg
));
2858 if (ipsec_get_keylen(sup
, alg
, &alg0
)) {
2860 "%s.\n", ipsec_strerror());
2863 keylen
= alg0
.sadb_alg_minbits
;
2866 error
= ipsec_check_keylen(sup
, alg
, keylen
);
2869 "%s.\n", ipsec_strerror());
2875 * differences with pfkey_recv() in libipsec/pfkey.c:
2876 * - never performs busy wait loop.
2877 * - returns NULL and set *lenp to negative on fatal failures
2878 * - returns NULL and set *lenp to non-negative on non-fatal failures
2879 * - returns non-NULL on success
2881 static struct sadb_msg
*
2886 struct sadb_msg
*newmsg
;
2888 socklen_t optlen
= sizeof(reallen
);
2890 if (getsockopt(so
, SOL_SOCKET
, SO_NREAD
, &reallen
, &optlen
) < 0)
2891 return NULL
; /*fatal*/
2896 if ((newmsg
= racoon_calloc(1, reallen
)) == NULL
)
2899 while ((*lenp
= recv(so
, (caddr_t
)newmsg
, reallen
, 0)) < 0) {
2902 plog(ASL_LEVEL_ERR
, "failed to recv pfkey message: %s\n", strerror(errno
));
2906 racoon_free(newmsg
);
2907 return NULL
; /*fatal*/
2908 } else if (*lenp
!= reallen
|| *lenp
< sizeof(struct sadb_msg
)) {
2909 racoon_free(newmsg
);
2920 return eay_random();
2927 struct secpolicy
*new;
2928 struct sadb_address
*saddr
, *daddr
;
2929 struct sadb_x_policy
*xpl
;
2932 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2933 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2934 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2936 "inappropriate sadb spd management message passed.\n");
2940 saddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
]; // Wcast-align fix (void*) - mhp contains pointers to aligned structs in malloc'd msg buffer
2941 daddr
= ALIGNED_CAST(struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2942 xpl
= ALIGNED_CAST(struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2947 "failed to allocate buffer\n");
2951 new->spidx
.dir
= xpl
->sadb_x_policy_dir
;
2952 new->id
= xpl
->sadb_x_policy_id
;
2953 new->policy
= xpl
->sadb_x_policy_type
;
2957 switch (xpl
->sadb_x_policy_type
) {
2958 case IPSEC_POLICY_DISCARD
:
2959 case IPSEC_POLICY_GENERATE
:
2960 case IPSEC_POLICY_NONE
:
2961 case IPSEC_POLICY_ENTRUST
:
2962 case IPSEC_POLICY_BYPASS
:
2965 case IPSEC_POLICY_IPSEC
:
2968 struct sadb_x_ipsecrequest
*xisr
;
2969 struct ipsecrequest
**p_isr
= &new->req
;
2971 /* validity check */
2972 if (PFKEY_EXTLEN(xpl
) < sizeof(*xpl
)) {
2974 "invalid msg length.\n");
2978 tlen
= PFKEY_EXTLEN(xpl
) - sizeof(*xpl
);
2979 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
2984 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
2986 "invalid msg length.\n");
2990 /* allocate request buffer */
2991 *p_isr
= newipsecreq();
2992 if (*p_isr
== NULL
) {
2994 "failed to get new ipsecreq.\n");
2999 (*p_isr
)->next
= NULL
;
3001 switch (xisr
->sadb_x_ipsecrequest_proto
) {
3004 case IPPROTO_IPCOMP
:
3008 "invalid proto type: %u\n",
3009 xisr
->sadb_x_ipsecrequest_proto
);
3012 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
3014 switch (xisr
->sadb_x_ipsecrequest_mode
) {
3015 case IPSEC_MODE_TRANSPORT
:
3016 case IPSEC_MODE_TUNNEL
:
3018 case IPSEC_MODE_ANY
:
3021 "invalid mode: %u\n",
3022 xisr
->sadb_x_ipsecrequest_mode
);
3025 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
3027 switch (xisr
->sadb_x_ipsecrequest_level
) {
3028 case IPSEC_LEVEL_DEFAULT
:
3029 case IPSEC_LEVEL_USE
:
3030 case IPSEC_LEVEL_REQUIRE
:
3032 case IPSEC_LEVEL_UNIQUE
:
3033 (*p_isr
)->saidx
.reqid
=
3034 xisr
->sadb_x_ipsecrequest_reqid
;
3039 "invalid level: %u\n",
3040 xisr
->sadb_x_ipsecrequest_level
);
3043 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
3045 /* set IP addresses if there */
3046 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
3047 struct sockaddr
*paddr
;
3049 paddr
= (struct sockaddr
*)(xisr
+ 1);
3050 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
3051 sysdep_sa_len(paddr
));
3053 paddr
= (struct sockaddr
*)((caddr_t
)paddr
3054 + sysdep_sa_len(paddr
));
3055 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
3056 sysdep_sa_len(paddr
));
3061 /* initialization for the next. */
3062 p_isr
= &(*p_isr
)->next
;
3063 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
3065 /* validity check */
3068 "becoming tlen < 0\n");
3071 xisr
= ALIGNED_CAST(struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
3072 + xisr
->sadb_x_ipsecrequest_len
);
3078 "invalid policy type.\n");
3082 #ifdef HAVE_PFKEY_POLICY_PRIORITY
3083 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3086 saddr
->sadb_address_prefixlen
,
3087 daddr
->sadb_address_prefixlen
,
3088 saddr
->sadb_address_proto
,
3089 xpl
->sadb_x_policy_priority
,
3092 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3095 saddr
->sadb_address_prefixlen
,
3096 daddr
->sadb_address_prefixlen
,
3097 saddr
->sadb_address_proto
,
3106 /* proto/mode/src->dst spi */
3108 sadbsecas2str(src
, dst
, proto
, spi
, mode
)
3109 struct sockaddr_storage
*src
, *dst
;
3114 static char buf
[256];
3115 u_int doi_proto
, doi_mode
= 0;
3119 doi_proto
= pfkey2ipsecdoi_proto(proto
);
3120 if (doi_proto
== ~0)
3123 doi_mode
= pfkey2ipsecdoi_mode(mode
);
3128 blen
= sizeof(buf
) - 1;
3131 i
= snprintf(p
, blen
, "%s%s%s ",
3132 s_ipsecdoi_proto(doi_proto
),
3134 mode
? s_ipsecdoi_encmode(doi_mode
) : "");
3135 if (i
< 0 || i
>= blen
)
3140 i
= snprintf(p
, blen
, "%s->", saddr2str((struct sockaddr
*)src
));
3141 if (i
< 0 || i
>= blen
)
3146 i
= snprintf(p
, blen
, "%s ", saddr2str((struct sockaddr
*)dst
));
3147 if (i
< 0 || i
>= blen
)
3153 snprintf(p
, blen
, "spi=%lu(0x%lx)", (unsigned long)ntohl(spi
),
3154 (unsigned long)ntohl(spi
));