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
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <netinet/udp.h>
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/queue.h>
54 #include <sys/sysctl.h>
56 #include <net/route.h>
57 #include <System/net/pfkeyv2.h>
59 #include <netinet/in.h>
60 #ifndef HAVE_NETINET6_IPSEC
61 #include <netinet/ipsec.h>
63 #include <netinet6/ipsec.h>
76 #include "localconf.h"
77 #include "remoteconf.h"
78 #include "isakmp_var.h"
80 #include "isakmp_inf.h"
81 #include "ipsec_doi.h"
86 #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"
104 #if defined(SADB_X_EALG_RIJNDAELCBC) && !defined(SADB_X_EALG_AESCBC)
105 #define SADB_X_EALG_AESCBC SADB_X_EALG_RIJNDAELCBC
109 static u_int ipsecdoi2pfkey_aalg
__P((u_int
));
110 static u_int ipsecdoi2pfkey_ealg
__P((u_int
));
111 static u_int ipsecdoi2pfkey_calg
__P((u_int
));
112 static u_int ipsecdoi2pfkey_alg
__P((u_int
, u_int
));
113 static u_int keylen_aalg
__P((u_int
));
114 static u_int keylen_ealg
__P((u_int
, int));
116 static int pk_recvgetspi
__P((caddr_t
*));
117 static int pk_recvupdate
__P((caddr_t
*));
118 static int pk_recvadd
__P((caddr_t
*));
119 static int pk_recvdelete
__P((caddr_t
*));
120 static int pk_recvacquire
__P((caddr_t
*));
121 static int pk_recvexpire
__P((caddr_t
*));
122 static int pk_recvflush
__P((caddr_t
*));
123 static int getsadbpolicy
__P((caddr_t
*, int *, int, struct ph2handle
*));
124 static int pk_recvspdupdate
__P((caddr_t
*));
125 static int pk_recvspdadd
__P((caddr_t
*));
126 static int pk_recvspddelete
__P((caddr_t
*));
127 static int pk_recvspdexpire
__P((caddr_t
*));
128 static int pk_recvspdget
__P((caddr_t
*));
129 static int pk_recvspddump
__P((caddr_t
*));
130 static int pk_recvspdflush
__P((caddr_t
*));
131 static int pk_recvgetsastat
__P((caddr_t
*));
132 static struct sadb_msg
*pk_recv
__P((int, int *));
134 static int (*pkrecvf
[]) __P((caddr_t
*)) = {
142 NULL
, /* SABD_REGISTER */
145 NULL
, /* SADB_DUMP */
146 NULL
, /* SADB_X_PROMISC */
147 NULL
, /* SADB_X_PCHANGE */
152 NULL
, /* SADB_X_SPDACQUIRE */
155 NULL
, /* SADB_X_SPDSETIDX */
157 NULL
, /* SADB_X_SPDDELETE2 */
158 pk_recvgetsastat
, /* SADB_GETSASTAT */
159 NULL
, /* SADB_X_NAT_T_NEW_MAPPING */
160 NULL
, /* SADB_X_MIGRATE */
162 #error "SADB extra message?"
166 static int addnewsp
__P((caddr_t
*));
168 /* cope with old kame headers - ugly */
169 #ifndef SADB_X_AALG_MD5
170 #define SADB_X_AALG_MD5 SADB_AALG_MD5
172 #ifndef SADB_X_AALG_SHA
173 #define SADB_X_AALG_SHA SADB_AALG_SHA
175 #ifndef SADB_X_AALG_NULL
176 #define SADB_X_AALG_NULL SADB_AALG_NULL
179 #ifndef SADB_X_EALG_BLOWFISHCBC
180 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
182 #ifndef SADB_X_EALG_CAST128CBC
183 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC
185 #ifndef SADB_X_EALG_RC5CBC
186 #ifdef SADB_EALG_RC5CBC
187 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
193 struct sadb_msg
*msg
;
195 caddr_t mhp
[SADB_EXT_MAX
+ 1];
198 plog(LLV_DEBUG
, LOCATION
, NULL
, "get pfkey %s message\n",
199 s_pfkey_type(msg
->sadb_msg_type
));
200 plogdump(LLV_DEBUG2
, msg
, msg
->sadb_msg_len
<< 3);
203 if (msg
->sadb_msg_errno
) {
206 /* when SPD is empty, treat the state as no error. */
207 if (msg
->sadb_msg_type
== SADB_X_SPDDUMP
&&
208 msg
->sadb_msg_errno
== ENOENT
)
213 plog(pri
, LOCATION
, NULL
,
214 "pfkey %s failed: %s\n",
215 s_pfkey_type(msg
->sadb_msg_type
),
216 strerror(msg
->sadb_msg_errno
));
221 /* check pfkey message. */
222 if (pfkey_align(msg
, mhp
)) {
223 plog(LLV_ERROR
, LOCATION
, NULL
,
224 "libipsec failed pfkey align (%s)\n",
228 if (pfkey_check(mhp
)) {
229 plog(LLV_ERROR
, LOCATION
, NULL
,
230 "libipsec failed pfkey check (%s)\n",
234 msg
= (struct sadb_msg
*)mhp
[0];
237 if (msg
->sadb_msg_type
>= ARRAYLEN(pkrecvf
)) {
238 plog(LLV_ERROR
, LOCATION
, NULL
,
239 "unknown PF_KEY message type=%u\n",
244 if (pkrecvf
[msg
->sadb_msg_type
] == NULL
) {
245 plog(LLV_INFO
, LOCATION
, NULL
,
246 "unsupported PF_KEY message %s\n",
247 s_pfkey_type(msg
->sadb_msg_type
));
251 if ((pkrecvf
[msg
->sadb_msg_type
])(mhp
) < 0)
262 * PF_KEY packet handler
269 struct sadb_msg
*msg
;
272 if (slept_at
|| woke_at
) {
273 plog(LLV_DEBUG
, LOCATION
, NULL
,
274 "ignoring pfkey port until power-mgmt event is handled.\n");
278 /* receive pfkey message. */
280 msg
= (struct sadb_msg
*)pk_recv(lcconf
->sock_pfkey
, &len
);
283 plog(LLV_ERROR
, LOCATION
, NULL
,
284 "failed to recv from pfkey (%s)\n",
288 /* short message - msg not ready */
292 return pfkey_process(msg
);
298 struct saved_msg_elem
*elem
;
299 struct saved_msg_elem
*elem_tmp
= NULL
;
301 if (slept_at
|| woke_at
) {
302 plog(LLV_DEBUG
, LOCATION
, NULL
,
303 "ignoring (saved) pfkey messages until power-mgmt event is handled.\n");
307 TAILQ_FOREACH_SAFE(elem
, &lcconf
->saved_msg_queue
, chain
, elem_tmp
) {
308 pfkey_process((struct sadb_msg
*)elem
->msg
);
309 TAILQ_REMOVE(&lcconf
->saved_msg_queue
, elem
, chain
);
317 struct sadb_msg
*msg
;
319 struct saved_msg_elem
*elem
;
321 elem
= (struct saved_msg_elem
*)racoon_calloc(sizeof(struct saved_msg_elem
), 1);
325 TAILQ_INSERT_TAIL(&lcconf
->saved_msg_queue
, elem
, chain
);
333 pfkey_dump_sadb(satype
)
338 pid_t pid
= getpid();
339 struct sadb_msg
*msg
= NULL
;
343 if ((s
= privsep_pfkey_open()) < 0) {
344 plog(LLV_ERROR
, LOCATION
, NULL
,
345 "libipsec failed pfkey open: %s\n",
350 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_dump\n");
351 if (pfkey_send_dump(s
, satype
) < 0) {
352 plog(LLV_ERROR
, LOCATION
, NULL
,
353 "libipsec failed dump: %s\n", ipsec_strerror());
360 msg
= pk_recv(s
, &len
);
369 * for multi-processor system this had to be added because the messages can
370 * be interleaved - they won't all be dump messages
372 if (msg
->sadb_msg_type
!= SADB_DUMP
) { /* save for later processing */
378 // ignore dump messages that aren't racoon's
379 if (msg
->sadb_msg_pid
!= pid
)
382 ml
= msg
->sadb_msg_len
<< 3;
383 bl
= buf
? buf
->l
: 0;
384 buf
= vrealloc(buf
, bl
+ ml
);
386 plog(LLV_ERROR
, LOCATION
, NULL
,
387 "failed to reallocate buffer to dump.\n");
390 memcpy(buf
->v
+ bl
, msg
, ml
);
392 if (msg
->sadb_msg_seq
== 0)
405 privsep_pfkey_close(s
);
409 #ifdef ENABLE_ADMINPORT
414 pfkey_flush_sadb(proto
)
419 /* convert to SADB_SATYPE */
420 if ((satype
= admin2pfkey_proto(proto
)) < 0)
423 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_flush\n");
424 if (pfkey_send_flush(lcconf
->sock_pfkey
, satype
) < 0) {
425 plog(LLV_ERROR
, LOCATION
, NULL
,
426 "libipsec failed send flush (%s)\n", ipsec_strerror());
435 * These are the SATYPEs that we manage. We register to get
436 * PF_KEY messages related to these SATYPEs, and we also use
437 * this list to determine which SATYPEs to delete SAs for when
438 * we receive an INITIAL-CONTACT.
440 const struct pfkey_satype pfkey_satypes
[] = {
441 { SADB_SATYPE_AH
, "AH" },
442 { SADB_SATYPE_ESP
, "ESP" },
443 { SADB_X_SATYPE_IPCOMP
, "IPCOMP" },
445 const int pfkey_nsatypes
=
446 sizeof(pfkey_satypes
) / sizeof(pfkey_satypes
[0]);
449 * PF_KEY initialization
456 if ((lcconf
->sock_pfkey
= privsep_pfkey_open()) < 0) {
457 plog(LLV_ERROR
, LOCATION
, NULL
,
458 "libipsec failed pfkey open (%s)\n", ipsec_strerror());
462 for (i
= 0, reg_fail
= 0; i
< pfkey_nsatypes
; i
++) {
463 plog(LLV_DEBUG
, LOCATION
, NULL
,
464 "call pfkey_send_register for %s\n",
465 pfkey_satypes
[i
].ps_name
);
466 if (pfkey_send_register(lcconf
->sock_pfkey
,
467 pfkey_satypes
[i
].ps_satype
) < 0 ||
468 pfkey_recv_register(lcconf
->sock_pfkey
) < 0) {
469 plog(LLV_WARNING
, LOCATION
, NULL
,
470 "failed to register %s (%s)\n",
471 pfkey_satypes
[i
].ps_name
,
477 if (reg_fail
== pfkey_nsatypes
) {
478 plog(LLV_ERROR
, LOCATION
, NULL
,
479 "failed to regist any protocol.\n");
480 pfkey_close(lcconf
->sock_pfkey
);
486 if (pfkey_send_spddump(lcconf
->sock_pfkey
) < 0) {
487 plog(LLV_ERROR
, LOCATION
, NULL
,
488 "libipsec sending spddump failed: %s\n",
490 pfkey_close(lcconf
->sock_pfkey
);
494 if (pfkey_promisc_toggle(1) < 0) {
495 pfkey_close(lcconf
->sock_pfkey
);
502 /* %%% for conversion */
503 /* IPSECDOI_ATTR_AUTH -> SADB_AALG */
505 ipsecdoi2pfkey_aalg(hashtype
)
509 case IPSECDOI_ATTR_AUTH_HMAC_MD5
:
510 return SADB_AALG_MD5HMAC
;
511 case IPSECDOI_ATTR_AUTH_HMAC_SHA1
:
512 return SADB_AALG_SHA1HMAC
;
513 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_256
:
514 #if (defined SADB_X_AALG_SHA2_256) && !defined(SADB_X_AALG_SHA2_256HMAC)
515 return SADB_X_AALG_SHA2_256
;
517 return SADB_X_AALG_SHA2_256HMAC
;
519 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_384
:
520 #if (defined SADB_X_AALG_SHA2_384) && !defined(SADB_X_AALG_SHA2_384HMAC)
521 return SADB_X_AALG_SHA2_384
;
523 return SADB_X_AALG_SHA2_384HMAC
;
525 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_512
:
526 #if (defined SADB_X_AALG_SHA2_512) && !defined(SADB_X_AALG_SHA2_512HMAC)
527 return SADB_X_AALG_SHA2_512
;
529 return SADB_X_AALG_SHA2_512HMAC
;
531 case IPSECDOI_ATTR_AUTH_KPDK
: /* need special care */
532 return SADB_AALG_NONE
;
535 case IPSECDOI_ATTR_AUTH_DES_MAC
:
536 plog(LLV_ERROR
, LOCATION
, NULL
,
537 "Not supported hash type: %u\n", hashtype
);
540 case 0: /* reserved */
542 return SADB_AALG_NONE
;
544 plog(LLV_ERROR
, LOCATION
, NULL
,
545 "Invalid hash type: %u\n", hashtype
);
551 /* IPSECDOI_ESP -> SADB_EALG */
553 ipsecdoi2pfkey_ealg(t_id
)
557 case IPSECDOI_ESP_DES_IV64
: /* sa_flags |= SADB_X_EXT_OLD */
558 return SADB_EALG_DESCBC
;
559 case IPSECDOI_ESP_DES
:
560 return SADB_EALG_DESCBC
;
561 case IPSECDOI_ESP_3DES
:
562 return SADB_EALG_3DESCBC
;
563 #ifdef SADB_X_EALG_RC5CBC
564 case IPSECDOI_ESP_RC5
:
565 return SADB_X_EALG_RC5CBC
;
567 case IPSECDOI_ESP_CAST
:
568 return SADB_X_EALG_CAST128CBC
;
569 case IPSECDOI_ESP_BLOWFISH
:
570 return SADB_X_EALG_BLOWFISHCBC
;
571 case IPSECDOI_ESP_DES_IV32
: /* flags |= (SADB_X_EXT_OLD|
573 return SADB_EALG_DESCBC
;
574 case IPSECDOI_ESP_NULL
:
575 return SADB_EALG_NULL
;
576 #ifdef SADB_X_EALG_AESCBC
577 case IPSECDOI_ESP_AES
:
578 return SADB_X_EALG_AESCBC
;
580 #ifdef SADB_X_EALG_TWOFISHCBC
581 case IPSECDOI_ESP_TWOFISH
:
582 return SADB_X_EALG_TWOFISHCBC
;
586 case IPSECDOI_ESP_3IDEA
:
587 case IPSECDOI_ESP_IDEA
:
588 case IPSECDOI_ESP_RC4
:
589 plog(LLV_ERROR
, LOCATION
, NULL
,
590 "Not supported transform: %u\n", t_id
);
593 case 0: /* reserved */
595 plog(LLV_ERROR
, LOCATION
, NULL
,
596 "Invalid transform id: %u\n", t_id
);
602 /* IPCOMP -> SADB_CALG */
604 ipsecdoi2pfkey_calg(t_id
)
608 case IPSECDOI_IPCOMP_OUI
:
609 return SADB_X_CALG_OUI
;
610 case IPSECDOI_IPCOMP_DEFLATE
:
611 return SADB_X_CALG_DEFLATE
;
612 case IPSECDOI_IPCOMP_LZS
:
613 return SADB_X_CALG_LZS
;
615 case 0: /* reserved */
617 plog(LLV_ERROR
, LOCATION
, NULL
,
618 "Invalid transform id: %u\n", t_id
);
624 /* IPSECDOI_PROTO -> SADB_SATYPE */
626 ipsecdoi2pfkey_proto(proto
)
630 case IPSECDOI_PROTO_IPSEC_AH
:
631 return SADB_SATYPE_AH
;
632 case IPSECDOI_PROTO_IPSEC_ESP
:
633 return SADB_SATYPE_ESP
;
634 case IPSECDOI_PROTO_IPCOMP
:
635 return SADB_X_SATYPE_IPCOMP
;
638 plog(LLV_ERROR
, LOCATION
, NULL
,
639 "Invalid ipsec_doi proto: %u\n", proto
);
646 ipsecdoi2pfkey_alg(algclass
, type
)
647 u_int algclass
, type
;
650 case IPSECDOI_ATTR_AUTH
:
651 return ipsecdoi2pfkey_aalg(type
);
652 case IPSECDOI_PROTO_IPSEC_ESP
:
653 return ipsecdoi2pfkey_ealg(type
);
654 case IPSECDOI_PROTO_IPCOMP
:
655 return ipsecdoi2pfkey_calg(type
);
657 plog(LLV_ERROR
, LOCATION
, NULL
,
658 "Invalid ipsec_doi algclass: %u\n", algclass
);
664 /* SADB_SATYPE -> IPSECDOI_PROTO */
666 pfkey2ipsecdoi_proto(satype
)
671 return IPSECDOI_PROTO_IPSEC_AH
;
672 case SADB_SATYPE_ESP
:
673 return IPSECDOI_PROTO_IPSEC_ESP
;
674 case SADB_X_SATYPE_IPCOMP
:
675 return IPSECDOI_PROTO_IPCOMP
;
678 plog(LLV_ERROR
, LOCATION
, NULL
,
679 "Invalid pfkey proto: %u\n", satype
);
685 /* IPSECDOI_ATTR_ENC_MODE -> IPSEC_MODE */
687 ipsecdoi2pfkey_mode(mode
)
691 case IPSECDOI_ATTR_ENC_MODE_TUNNEL
:
693 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
:
694 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
:
696 return IPSEC_MODE_TUNNEL
;
697 case IPSECDOI_ATTR_ENC_MODE_TRNS
:
699 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
:
700 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
:
702 return IPSEC_MODE_TRANSPORT
;
704 plog(LLV_ERROR
, LOCATION
, NULL
, "Invalid mode type: %u\n", mode
);
710 /* IPSECDOI_ATTR_ENC_MODE -> IPSEC_MODE */
712 pfkey2ipsecdoi_mode(mode
)
716 case IPSEC_MODE_TUNNEL
:
717 return IPSECDOI_ATTR_ENC_MODE_TUNNEL
;
718 case IPSEC_MODE_TRANSPORT
:
719 return IPSECDOI_ATTR_ENC_MODE_TRNS
;
721 return IPSECDOI_ATTR_ENC_MODE_ANY
;
723 plog(LLV_ERROR
, LOCATION
, NULL
, "Invalid mode type: %u\n", mode
);
729 /* default key length for encryption algorithm */
731 keylen_aalg(hashtype
)
737 return SADB_AALG_NONE
;
739 res
= alg_ipsec_hmacdef_hashlen(hashtype
);
741 plog(LLV_ERROR
, LOCATION
, NULL
,
742 "invalid hmac algorithm %u.\n", hashtype
);
748 /* default key length for encryption algorithm */
750 keylen_ealg(enctype
, encklen
)
756 res
= alg_ipsec_encdef_keylen(enctype
, encklen
);
758 plog(LLV_ERROR
, LOCATION
, NULL
,
759 "invalid encryption algorithm %u.\n", enctype
);
766 pfkey_convertfromipsecdoi(proto_id
, t_id
, hashtype
,
767 e_type
, e_keylen
, a_type
, a_keylen
, flags
)
779 case IPSECDOI_PROTO_IPSEC_ESP
:
780 if ((*e_type
= ipsecdoi2pfkey_ealg(t_id
)) == ~0)
782 if ((*e_keylen
= keylen_ealg(t_id
, *e_keylen
)) == ~0)
786 if ((*a_type
= ipsecdoi2pfkey_aalg(hashtype
)) == ~0)
788 if ((*a_keylen
= keylen_aalg(hashtype
)) == ~0)
792 if (*e_type
== SADB_EALG_NONE
) {
793 plog(LLV_ERROR
, LOCATION
, NULL
, "no ESP algorithm.\n");
798 case IPSECDOI_PROTO_IPSEC_AH
:
799 if ((*a_type
= ipsecdoi2pfkey_aalg(hashtype
)) == ~0)
801 if ((*a_keylen
= keylen_aalg(hashtype
)) == ~0)
805 if (t_id
== IPSECDOI_ATTR_AUTH_HMAC_MD5
806 && hashtype
== IPSECDOI_ATTR_AUTH_KPDK
) {
807 /* AH_MD5 + Auth(KPDK) = RFC1826 keyed-MD5 */
808 *a_type
= SADB_X_AALG_MD5
;
809 *flags
|= SADB_X_EXT_OLD
;
811 *e_type
= SADB_EALG_NONE
;
813 if (*a_type
== SADB_AALG_NONE
) {
814 plog(LLV_ERROR
, LOCATION
, NULL
, "no AH algorithm.\n");
819 case IPSECDOI_PROTO_IPCOMP
:
820 if ((*e_type
= ipsecdoi2pfkey_calg(t_id
)) == ~0)
824 *flags
= SADB_X_EXT_RAWCPI
;
826 *a_type
= SADB_AALG_NONE
;
828 if (*e_type
== SADB_X_CALG_NONE
) {
829 plog(LLV_ERROR
, LOCATION
, NULL
, "no IPCOMP algorithm.\n");
835 plog(LLV_ERROR
, LOCATION
, NULL
, "unknown IPsec protocol.\n");
846 /* called from scheduler */
848 pfkey_timeover_stub(p
)
852 pfkey_timeover((struct ph2handle
*)p
);
857 struct ph2handle
*iph2
;
859 plog(LLV_ERROR
, LOCATION
, NULL
,
860 "%s give up to get IPsec-SA due to time up to wait.\n",
861 saddrwop2str(iph2
->dst
));
862 SCHED_KILL(iph2
->sce
);
864 /* If initiator side, send error to kernel by SADB_ACQUIRE. */
865 if (iph2
->side
== INITIATOR
)
866 pk_sendeacquire(iph2
);
876 /* send getspi message per ipsec protocol per remote address */
878 * the local address and remote address in ph1handle are dealed
879 * with destination address and source address respectively.
880 * Because SPI is decided by responder.
884 struct ph2handle
*iph2
;
886 struct sockaddr
*src
= NULL
, *dst
= NULL
;
890 u_int32_t minspi
, maxspi
;
893 if (iph2
->side
== INITIATOR
) {
895 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
898 if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
902 /* for mobile IPv6 */
903 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
904 ipsecdoi_transportmode(pp
)) {
912 for (pr
= pp
->head
; pr
!= NULL
; pr
= pr
->next
) {
915 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
917 plog(LLV_ERROR
, LOCATION
, NULL
,
918 "invalid proto_id %d\n", pr
->proto_id
);
921 /* this works around a bug in Linux kernel where it allocates 4 byte
923 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
931 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
933 plog(LLV_ERROR
, LOCATION
, NULL
,
934 "invalid encmode %d\n", pr
->encmode
);
938 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_getspi\n");
939 if (pfkey_send_getspi(
946 pr
->reqid_in
, iph2
->seq
) < 0) {
947 plog(LLV_ERROR
, LOCATION
, NULL
,
948 "ipseclib failed send getspi (%s)\n",
952 plog(LLV_DEBUG
, LOCATION
, NULL
,
953 "pfkey GETSPI sent: %s\n",
954 sadbsecas2str(dst
, src
, satype
, 0, mode
));
961 * receive GETSPI from kernel.
967 struct sadb_msg
*msg
;
969 struct ph2handle
*iph2
;
970 struct sockaddr
*dst
;
972 int allspiok
, notfound
;
977 if (mhp
[SADB_EXT_SA
] == NULL
978 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
979 plog(LLV_ERROR
, LOCATION
, NULL
,
980 "inappropriate sadb getspi message passed.\n");
983 msg
= (struct sadb_msg
*)mhp
[0];
984 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
985 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]); /* note SA dir */
987 /* the message has to be processed or not ? */
988 if (msg
->sadb_msg_pid
!= getpid()) {
989 plog(LLV_DEBUG
, LOCATION
, NULL
,
990 "%s message is not interesting "
991 "because pid %d is not mine.\n",
992 s_pfkey_type(msg
->sadb_msg_type
),
997 iph2
= getph2byseq(msg
->sadb_msg_seq
);
999 plog(LLV_DEBUG
, LOCATION
, NULL
,
1000 "seq %d of %s message not interesting.\n",
1002 s_pfkey_type(msg
->sadb_msg_type
));
1006 if (iph2
->is_dying
) {
1007 plog(LLV_ERROR
, LOCATION
, NULL
,
1008 "status mismatch phase2 dying (db:%d msg:%d)\n",
1009 iph2
->status
, PHASE2ST_GETSPISENT
);
1013 if (iph2
->status
!= PHASE2ST_GETSPISENT
) {
1014 plog(LLV_ERROR
, LOCATION
, NULL
,
1015 "status mismatch (db:%d msg:%d)\n",
1016 iph2
->status
, PHASE2ST_GETSPISENT
);
1020 // check the underlying iph2->ph1
1022 if (!ike_session_update_ph2_ph1bind(iph2
)) {
1023 plog(LLV_ERROR
, LOCATION
, NULL
,
1024 "can't proceed with getspi for %s. no suitable ISAKMP-SA found \n",
1025 saddrwop2str(iph2
->dst
));
1033 /* set SPI, and check to get all spi whether or not */
1036 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1037 pp
= iph2
->side
== INITIATOR
? iph2
->proposal
: iph2
->approval
;
1039 for (pr
= pp
->head
; pr
!= NULL
; pr
= pr
->next
) {
1040 if (pr
->proto_id
== proto_id
&& pr
->spi
== 0) {
1041 pr
->spi
= sa
->sadb_sa_spi
;
1043 plog(LLV_DEBUG
, LOCATION
, NULL
,
1044 "pfkey GETSPI succeeded: %s\n",
1045 sadbsecas2str(iph2
->dst
, iph2
->src
,
1046 msg
->sadb_msg_satype
,
1048 ipsecdoi2pfkey_mode(pr
->encmode
)));
1051 allspiok
= 0; /* not get all spi */
1055 plog(LLV_ERROR
, LOCATION
, NULL
,
1056 "get spi for unknown address %s\n",
1057 saddrwop2str(iph2
->dst
));
1066 iph2
->status
= PHASE2ST_GETSPIDONE
;
1067 if (isakmp_post_getspi(iph2
) < 0) {
1068 plog(LLV_ERROR
, LOCATION
, NULL
,
1069 "failed to start post getspi.\n");
1086 struct ph2handle
*iph2
;
1089 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1090 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1092 u_int64_t lifebyte
= 0;
1093 u_int wsize
= 4; /* XXX static size of window */
1095 struct ph2natt natt
;
1098 if (iph2
->approval
== NULL
) {
1099 plog(LLV_ERROR
, LOCATION
, NULL
,
1100 "no approvaled SAs found.\n");
1103 if (iph2
->side
== INITIATOR
)
1104 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1105 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1108 /* for mobile IPv6 */
1109 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1110 ipsecdoi_transportmode(iph2
->approval
)) {
1118 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1119 /* validity check */
1120 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1122 plog(LLV_ERROR
, LOCATION
, NULL
,
1123 "invalid proto_id %d\n", pr
->proto_id
);
1126 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1127 /* IPCOMP has no replay window */
1130 #ifdef ENABLE_SAMODE_UNSPECIFIED
1131 mode
= IPSEC_MODE_ANY
;
1133 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1135 plog(LLV_ERROR
, LOCATION
, NULL
,
1136 "invalid encmode %d\n", pr
->encmode
);
1141 /* set algorithm type and key length */
1142 e_keylen
= pr
->head
->encklen
;
1143 if (pfkey_convertfromipsecdoi(
1148 &a_type
, &a_keylen
, &flags
) < 0)
1152 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1158 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1159 if (pr
->udp_encap
) {
1160 memset (&natt
, 0, sizeof (natt
));
1161 natt
.sport
= extract_port (iph2
->ph1
->remote
);
1162 flags
|= SADB_X_EXT_NATT
;
1163 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1164 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1165 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1167 if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1168 mode
== IPSEC_MODE_TRANSPORT
&&
1169 src
->sa_family
== AF_INET
) {
1170 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1172 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
) {
1173 // is mutually exclusive with SADB_X_EXT_NATT_KEEPALIVE
1174 flags
|= SADB_X_EXT_NATT_DETECTED_PEER
;
1178 memset (&natt
, 0, sizeof (natt
));
1181 if (pfkey_send_update(
1191 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1192 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1193 iph2
->seq
, natt
.sport
) < 0) {
1194 plog(LLV_ERROR
, LOCATION
, NULL
,
1195 "libipsec failed send update (%s)\n",
1200 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1201 if (pfkey_send_update(
1211 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1212 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1213 iph2
->seq
, 0) < 0) {
1214 plog(LLV_ERROR
, LOCATION
, NULL
,
1215 "libipsec failed send update (%s)\n",
1219 #endif /* ENABLE_NATT */
1221 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
1225 * It maybe good idea to call backupsa_to_file() after
1226 * racoon will receive the sadb_update messages.
1227 * But it is impossible because there is not key in the
1228 * information from the kernel.
1230 if (backupsa_to_file(satype
, mode
, dst
, src
,
1231 pr
->spi
, pr
->reqid_in
, 4,
1233 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1234 0, iph2
->approval
->lifebyte
* 1024,
1235 iph2
->approval
->lifetime
, 0,
1237 plog(LLV_ERROR
, LOCATION
, NULL
,
1238 "backuped SA failed: %s\n",
1239 sadbsecas2str(dst
, src
,
1240 satype
, pr
->spi
, mode
));
1242 plog(LLV_DEBUG
, LOCATION
, NULL
,
1243 "backuped SA: %s\n",
1244 sadbsecas2str(dst
, src
,
1245 satype
, pr
->spi
, mode
));
1255 struct sadb_msg
*msg
;
1257 struct sockaddr
*src
, *dst
;
1258 struct ph2handle
*iph2
;
1259 u_int proto_id
, encmode
, sa_mode
;
1263 /* ignore this message because of local test mode. */
1269 || mhp
[SADB_EXT_SA
] == NULL
1270 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1271 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1272 plog(LLV_ERROR
, LOCATION
, NULL
,
1273 "inappropriate sadb update message passed.\n");
1276 msg
= (struct sadb_msg
*)mhp
[0];
1277 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1278 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1279 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1281 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1283 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1285 /* the message has to be processed or not ? */
1286 if (msg
->sadb_msg_pid
!= getpid()) {
1287 plog(LLV_DEBUG
, LOCATION
, NULL
,
1288 "%s message is not interesting "
1289 "because pid %d is not mine.\n",
1290 s_pfkey_type(msg
->sadb_msg_type
),
1295 iph2
= getph2byseq(msg
->sadb_msg_seq
);
1297 plog(LLV_DEBUG
, LOCATION
, NULL
,
1298 "seq %d of %s message not interesting.\n",
1300 s_pfkey_type(msg
->sadb_msg_type
));
1304 if (iph2
->is_dying
) {
1305 plog(LLV_ERROR
, LOCATION
, NULL
,
1306 "status mismatch phase2 dying (db:%d msg:%d)\n",
1307 iph2
->status
, PHASE2ST_ADDSA
);
1311 if (iph2
->status
!= PHASE2ST_ADDSA
) {
1312 plog(LLV_ERROR
, LOCATION
, NULL
,
1313 "status mismatch (db:%d msg:%d)\n",
1314 iph2
->status
, PHASE2ST_ADDSA
);
1318 /* check to complete all keys ? */
1319 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1320 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1321 if (proto_id
== ~0) {
1322 plog(LLV_ERROR
, LOCATION
, NULL
,
1323 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1326 encmode
= pfkey2ipsecdoi_mode(sa_mode
);
1327 if (encmode
== ~0) {
1328 plog(LLV_ERROR
, LOCATION
, NULL
,
1329 "invalid encmode %d\n", sa_mode
);
1333 if (pr
->proto_id
== proto_id
1334 && pr
->spi
== sa
->sadb_sa_spi
) {
1336 plog(LLV_DEBUG
, LOCATION
, NULL
,
1337 "pfkey UPDATE succeeded: %s\n",
1338 sadbsecas2str(iph2
->dst
, iph2
->src
,
1339 msg
->sadb_msg_satype
,
1343 plog(LLV_INFO
, LOCATION
, NULL
,
1344 "IPsec-SA established: %s\n",
1345 sadbsecas2str(iph2
->dst
, iph2
->src
,
1346 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1357 /* turn off the timer for calling pfkey_timeover() */
1358 SCHED_KILL(iph2
->sce
);
1361 iph2
->status
= PHASE2ST_ESTABLISHED
;
1363 if (iph2
->side
== INITIATOR
) {
1364 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1365 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_SUCC
,
1366 CONSTSTR("Initiator, Quick-Mode"),
1369 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1370 IPSECSESSIONEVENTCODE_IKEV1_PH2_RESP_SUCC
,
1371 CONSTSTR("Responder, Quick-Mode"),
1375 ike_session_ph2_established(iph2
);
1377 IPSECLOGASLMSG("IPSec Phase2 established (Initiated by %s).\n",
1378 (iph2
->side
== INITIATOR
)? "me" : "peer");
1381 gettimeofday(&iph2
->end
, NULL
);
1382 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1383 "phase2", "quick", timedelta(&iph2
->start
, &iph2
->end
));
1388 iph2
->ph1
->ph2cnt
++;
1390 /* turn off schedule */
1392 SCHED_KILL(iph2
->scr
);
1395 * since we are going to reuse the phase2 handler, we need to
1396 * remain it and refresh all the references between ph1 and ph2 to use.
1400 iph2
->sce
= sched_new(iph2
->approval
->lifetime
,
1401 isakmp_ph2expire_stub
, iph2
);
1403 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1412 struct ph2handle
*iph2
;
1415 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1416 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1418 u_int64_t lifebyte
= 0;
1419 u_int wsize
= 4; /* XXX static size of window */
1421 struct ph2natt natt
;
1424 if (iph2
->approval
== NULL
) {
1425 plog(LLV_ERROR
, LOCATION
, NULL
,
1426 "no approvaled SAs found.\n");
1429 if (iph2
->side
== INITIATOR
)
1430 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1431 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1434 /* for mobile IPv6 */
1435 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1436 ipsecdoi_transportmode(iph2
->approval
)) {
1444 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1445 /* validity check */
1446 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1448 plog(LLV_ERROR
, LOCATION
, NULL
,
1449 "invalid proto_id %d\n", pr
->proto_id
);
1452 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1453 /* no replay window for IPCOMP */
1456 #ifdef ENABLE_SAMODE_UNSPECIFIED
1457 mode
= IPSEC_MODE_ANY
;
1459 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1461 plog(LLV_ERROR
, LOCATION
, NULL
,
1462 "invalid encmode %d\n", pr
->encmode
);
1467 /* set algorithm type and key length */
1468 e_keylen
= pr
->head
->encklen
;
1469 if (pfkey_convertfromipsecdoi(
1474 &a_type
, &a_keylen
, &flags
) < 0)
1478 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1484 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1486 if (pr
->udp_encap
) {
1487 memset (&natt
, 0, sizeof (natt
));
1488 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1489 flags
|= SADB_X_EXT_NATT
;
1490 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1491 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1492 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1494 if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1495 mode
== IPSEC_MODE_TRANSPORT
&&
1496 dst
->sa_family
== AF_INET
) {
1497 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1499 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_PEER
) {
1500 // is mutually exclusive with SADB_X_EXT_NATT_KEEPALIVE
1501 flags
|= SADB_X_EXT_NATT_DETECTED_PEER
;
1505 memset (&natt
, 0, sizeof (natt
));
1507 /* Remove port information, that SA doesn't use it */
1522 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1523 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1524 iph2
->seq
,natt
.dport
) < 0) {
1525 plog(LLV_ERROR
, LOCATION
, NULL
,
1526 "libipsec failed send add (%s)\n",
1531 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1533 /* Remove port information, it is not used without NAT-T */
1547 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1548 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1549 iph2
->seq
, 0) < 0) {
1550 plog(LLV_ERROR
, LOCATION
, NULL
,
1551 "libipsec failed send add (%s)\n",
1555 #endif /* ENABLE_NATT */
1556 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
1560 * It maybe good idea to call backupsa_to_file() after
1561 * racoon will receive the sadb_update messages.
1562 * But it is impossible because there is not key in the
1563 * information from the kernel.
1565 if (backupsa_to_file(satype
, mode
, src
, dst
,
1566 pr
->spi_p
, pr
->reqid_out
, 4,
1568 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1569 0, iph2
->approval
->lifebyte
* 1024,
1570 iph2
->approval
->lifetime
, 0,
1572 plog(LLV_ERROR
, LOCATION
, NULL
,
1573 "backuped SA failed: %s\n",
1574 sadbsecas2str(src
, dst
,
1575 satype
, pr
->spi_p
, mode
));
1577 plog(LLV_DEBUG
, LOCATION
, NULL
,
1578 "backuped SA: %s\n",
1579 sadbsecas2str(src
, dst
,
1580 satype
, pr
->spi_p
, mode
));
1590 struct sadb_msg
*msg
;
1592 struct sockaddr
*src
, *dst
;
1593 struct ph2handle
*iph2
;
1596 /* ignore this message because of local test mode. */
1602 || mhp
[SADB_EXT_SA
] == NULL
1603 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1604 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1605 plog(LLV_ERROR
, LOCATION
, NULL
,
1606 "inappropriate sadb add message passed.\n");
1609 msg
= (struct sadb_msg
*)mhp
[0];
1610 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1611 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1612 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1614 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1616 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1618 /* the message has to be processed or not ? */
1619 if (msg
->sadb_msg_pid
!= getpid()) {
1620 plog(LLV_DEBUG
, LOCATION
, NULL
,
1621 "%s message is not interesting "
1622 "because pid %d is not mine.\n",
1623 s_pfkey_type(msg
->sadb_msg_type
),
1628 iph2
= getph2byseq(msg
->sadb_msg_seq
);
1630 plog(LLV_DEBUG
, LOCATION
, NULL
,
1631 "seq %d of %s message not interesting.\n",
1633 s_pfkey_type(msg
->sadb_msg_type
));
1638 * NOTE don't update any status of phase2 handle
1639 * because they must be updated by SADB_UPDATE message
1642 plog(LLV_INFO
, LOCATION
, NULL
,
1643 "IPsec-SA established: %s\n",
1644 sadbsecas2str(iph2
->src
, iph2
->dst
,
1645 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1647 ike_session_cleanup_other_established_ph2s(iph2
->parent_session
, iph2
);
1649 #ifdef ENABLE_VPNCONTROL_PORT
1653 if (iph2
->dst
->sa_family
== AF_INET
)
1654 address
= ((struct sockaddr_in
*)iph2
->dst
)->sin_addr
.s_addr
;
1657 vpncontrol_notify_phase_change(0, FROM_LOCAL
, NULL
, iph2
);
1661 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1669 struct sadb_msg
*msg
;
1671 struct sockaddr
*src
, *dst
;
1672 struct ph2handle
*iph2
;
1673 u_int proto_id
, sa_mode
;
1677 || mhp
[SADB_EXT_SA
] == NULL
1678 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1679 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1680 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
1681 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
1682 plog(LLV_ERROR
, LOCATION
, NULL
,
1683 "inappropriate sadb expire message passed.\n");
1686 msg
= (struct sadb_msg
*)mhp
[0];
1687 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1688 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1689 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1691 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1693 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1695 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1696 if (proto_id
== ~0) {
1697 plog(LLV_ERROR
, LOCATION
, NULL
,
1698 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1702 plog(LLV_INFO
, LOCATION
, NULL
,
1703 "IPsec-SA expired: %s\n",
1704 sadbsecas2str(src
, dst
,
1705 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1707 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1710 * Ignore it because two expire messages are come up.
1711 * phase2 handler has been deleted already when 2nd message
1714 plog(LLV_DEBUG
, LOCATION
, NULL
,
1715 "no such a SA found: %s\n",
1716 sadbsecas2str(src
, dst
,
1717 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1721 if (iph2
->is_dying
|| iph2
->status
!= PHASE2ST_ESTABLISHED
) {
1723 * If the status is not equal to PHASE2ST_ESTABLISHED,
1724 * racoon ignores this expire message. There are two reason.
1725 * One is that the phase 2 probably starts because there is
1726 * a potential that racoon receives the acquire message
1727 * without receiving a expire message. Another is that racoon
1728 * may receive the multiple expire messages from the kernel.
1730 plog(LLV_WARNING
, LOCATION
, NULL
,
1731 "the expire message is received "
1732 "but the handler is dying or has not been established.\n");
1736 /* turn off the timer for calling isakmp_ph2expire() */
1737 SCHED_KILL(iph2
->sce
);
1739 iph2
->status
= PHASE2ST_EXPIRED
;
1741 /* INITIATOR, begin phase 2 exchange only if there's no other established ph2. */
1742 /* allocate buffer for status management of pfkey message */
1743 if (iph2
->side
== INITIATOR
&&
1744 !ike_session_has_other_established_ph2(iph2
->parent_session
, iph2
) &&
1745 !ike_session_drop_rekey(iph2
->parent_session
, IKE_SESSION_REKEY_TYPE_PH2
)) {
1749 /* update status for re-use */
1750 iph2
->status
= PHASE2ST_STATUS2
;
1752 /* start isakmp initiation by using ident exchange */
1753 if (isakmp_post_acquire(iph2
) < 0) {
1754 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
1755 "failed to begin ipsec sa "
1756 "re-negotiation.\n");
1767 /* If not received SADB_EXPIRE, INITIATOR delete ph2handle. */
1768 /* RESPONDER always delete ph2handle, keep silent. RESPONDER doesn't
1769 * manage IPsec SA, so delete the list */
1781 struct sadb_msg
*msg
;
1782 struct sadb_x_policy
*xpl
;
1783 struct secpolicy
*sp_out
= NULL
, *sp_in
= NULL
;
1784 #define MAXNESTEDSA 5 /* XXX */
1785 struct ph2handle
*iph2
[MAXNESTEDSA
];
1786 struct sockaddr
*src
, *dst
;
1787 int n
; /* # of phase 2 handler */
1789 /* ignore this message because of local test mode. */
1795 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1796 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1797 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1798 plog(LLV_ERROR
, LOCATION
, NULL
,
1799 "inappropriate sadb acquire message passed.\n");
1802 msg
= (struct sadb_msg
*)mhp
[0];
1803 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
1804 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1805 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1807 /* ignore if type is not IPSEC_POLICY_IPSEC */
1808 if (xpl
->sadb_x_policy_type
!= IPSEC_POLICY_IPSEC
) {
1809 plog(LLV_DEBUG
, LOCATION
, NULL
,
1810 "ignore ACQUIRE message. type is not IPsec.\n");
1814 /* ignore it if src is multicast address */
1816 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1818 if ((sa
->sa_family
== AF_INET
1819 && IN_MULTICAST(ntohl(((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
)))
1821 || (sa
->sa_family
== AF_INET6
1822 && IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)sa
)->sin6_addr
))
1825 plog(LLV_DEBUG
, LOCATION
, NULL
,
1826 "ignore due to multicast address: %s.\n",
1832 /* ignore, if we do not listen on source address */
1835 * - if we'll contact peer from address we do not listen -
1836 * we will be unable to complete negotiation;
1837 * - if we'll negotiate using address we're listening -
1838 * remote peer will send packets to address different
1839 * than one in the policy, so kernel will drop them;
1840 * => therefore this acquire is not for us! --Aidas
1842 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1845 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1846 if (!cmpsaddrwop(p
->addr
, sa
)) {
1853 plog(LLV_DEBUG
, LOCATION
, NULL
,
1854 "ignore because do not listen on source address : %s.\n",
1861 * If there is a phase 2 handler against the policy identifier in
1862 * the acquire message, and if
1863 * 1. its state is less than PHASE2ST_ESTABLISHED, then racoon
1864 * should ignore such a acquire message because the phase 2
1865 * is just negotiating.
1866 * 2. its state is equal to PHASE2ST_ESTABLISHED, then racoon
1867 * has to process such a acquire message because racoon may
1868 * have lost the expire message.
1870 iph2
[0] = getph2byid(src
, dst
, xpl
->sadb_x_policy_id
);
1871 if (iph2
[0] != NULL
) {
1872 if (iph2
[0]->status
< PHASE2ST_ESTABLISHED
) {
1873 plog(LLV_DEBUG
, LOCATION
, NULL
,
1874 "ignore the acquire because ph2 found\n");
1877 if (iph2
[0]->status
== PHASE2ST_EXPIRED
)
1882 /* search for proper policyindex */
1883 sp_out
= getspbyspid(xpl
->sadb_x_policy_id
);
1884 if (sp_out
== NULL
) {
1885 plog(LLV_ERROR
, LOCATION
, NULL
, "no policy found: id:%d.\n",
1886 xpl
->sadb_x_policy_id
);
1889 plog(LLV_DEBUG
, LOCATION
, NULL
,
1890 "suitable outbound SP found: %s.\n", spidx2str(&sp_out
->spidx
));
1892 /* get inbound policy */
1894 struct policyindex spidx
;
1896 spidx
.dir
= IPSEC_DIR_INBOUND
;
1897 memcpy(&spidx
.src
, &sp_out
->spidx
.dst
, sizeof(spidx
.src
));
1898 memcpy(&spidx
.dst
, &sp_out
->spidx
.src
, sizeof(spidx
.dst
));
1899 spidx
.prefs
= sp_out
->spidx
.prefd
;
1900 spidx
.prefd
= sp_out
->spidx
.prefs
;
1901 spidx
.ul_proto
= sp_out
->spidx
.ul_proto
;
1903 sp_in
= getsp(&spidx
);
1905 plog(LLV_DEBUG
, LOCATION
, NULL
,
1906 "suitable inbound SP found: %s.\n",
1907 spidx2str(&sp_in
->spidx
));
1909 plog(LLV_NOTIFY
, LOCATION
, NULL
,
1910 "no in-bound policy found: %s\n",
1915 memset(iph2
, 0, MAXNESTEDSA
);
1919 /* allocate a phase 2 */
1921 if (iph2
[n
] == NULL
) {
1922 plog(LLV_ERROR
, LOCATION
, NULL
,
1923 "failed to allocate phase2 entry.\n");
1926 iph2
[n
]->side
= INITIATOR
;
1927 iph2
[n
]->spid
= xpl
->sadb_x_policy_id
;
1928 iph2
[n
]->satype
= msg
->sadb_msg_satype
;
1929 iph2
[n
]->seq
= msg
->sadb_msg_seq
;
1930 iph2
[n
]->status
= PHASE2ST_STATUS2
;
1932 /* set end addresses of SA */
1933 iph2
[n
]->dst
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]));
1934 if (iph2
[n
]->dst
== NULL
) {
1938 iph2
[n
]->src
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]));
1939 if (iph2
[n
]->src
== NULL
) {
1944 plog(LLV_DEBUG
, LOCATION
, NULL
,
1945 "new acquire %s\n", spidx2str(&sp_out
->spidx
));
1949 vchar_t
*idsrc
, *iddst
;
1951 idsrc
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.src
,
1952 sp_out
->spidx
.prefs
, sp_out
->spidx
.ul_proto
);
1953 if (idsrc
== NULL
) {
1954 plog(LLV_ERROR
, LOCATION
, NULL
,
1955 "failed to get ID for %s\n",
1956 spidx2str(&sp_out
->spidx
));
1960 iddst
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.dst
,
1961 sp_out
->spidx
.prefd
, sp_out
->spidx
.ul_proto
);
1962 if (iddst
== NULL
) {
1963 plog(LLV_ERROR
, LOCATION
, NULL
,
1964 "failed to get ID for %s\n",
1965 spidx2str(&sp_out
->spidx
));
1970 iph2
[n
]->sainfo
= getsainfo(idsrc
, iddst
, NULL
, 0);
1973 if (iph2
[n
]->sainfo
== NULL
) {
1974 plog(LLV_ERROR
, LOCATION
, NULL
,
1975 "failed to get sainfo.\n");
1978 /* XXX should use the algorithm list from register message */
1980 if (link_sainfo_to_ph2(iph2
[n
]->sainfo
) != 0) {
1981 plog(LLV_ERROR
, LOCATION
, NULL
,
1982 "failed to link sainfo\n");
1983 iph2
[n
]->sainfo
= NULL
;
1989 if (set_proposal_from_policy(iph2
[n
], sp_out
, sp_in
) < 0) {
1990 plog(LLV_ERROR
, LOCATION
, NULL
,
1991 "failed to create saprop.\n");
1997 /* start isakmp initiation by using ident exchange */
1998 /* XXX should be looped if there are multiple phase 2 handler. */
1999 if (isakmp_post_acquire(iph2
[n
]) < 0) {
2000 plog(LLV_ERROR
, LOCATION
, NULL
,
2001 "failed to begin ipsec sa negotiation.\n");
2005 #if !TARGET_OS_EMBEDDED
2006 if ( lcconf
->vt
== NULL
){
2007 if (!(lcconf
->vt
= vproc_transaction_begin(NULL
)))
2008 plog(LLV_ERROR
, LOCATION
, NULL
,
2009 "vproc_transaction_begin returns NULL.\n");
2018 unbindph12(iph2
[n
]);
2031 struct sadb_msg
*msg
;
2033 struct sockaddr
*src
, *dst
;
2034 struct ph2handle
*iph2
= NULL
;
2037 /* ignore this message because of local test mode. */
2043 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2044 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
2045 plog(LLV_ERROR
, LOCATION
, NULL
,
2046 "inappropriate sadb delete message passed.\n");
2049 msg
= (struct sadb_msg
*)mhp
[0];
2050 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
2051 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
2052 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
2054 /* the message has to be processed or not ? */
2055 if (msg
->sadb_msg_pid
== getpid()) {
2056 plog(LLV_DEBUG
, LOCATION
, NULL
,
2057 "%s message is not interesting "
2058 "because the message was originated by me.\n",
2059 s_pfkey_type(msg
->sadb_msg_type
));
2063 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
2064 if (proto_id
== ~0) {
2065 plog(LLV_ERROR
, LOCATION
, NULL
,
2066 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
2070 plog(LLV_DEBUG2
, LOCATION
, NULL
, "SADB delete message: proto-id %d\n", proto_id
);
2071 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str(src
));
2072 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str(dst
));
2075 deleteallph2(src
, dst
, proto_id
);
2076 deleteallph1(src
, dst
);
2080 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
2083 plog(LLV_ERROR
, LOCATION
, NULL
,
2084 "no iph2 found: %s\n",
2085 sadbsecas2str(src
, dst
, msg
->sadb_msg_satype
,
2086 sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2090 plog(LLV_ERROR
, LOCATION
, NULL
,
2091 "pfkey DELETE received: %s\n",
2092 sadbsecas2str(iph2
->src
, iph2
->dst
,
2093 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2095 /* send delete information */
2096 if (iph2
->status
== PHASE2ST_ESTABLISHED
)
2097 isakmp_info_send_d2(iph2
);
2099 ike_session_cleanup_ph1s_by_ph2(iph2
);
2111 /* ignore this message because of local test mode. */
2116 if (mhp
[0] == NULL
) {
2117 plog(LLV_ERROR
, LOCATION
, NULL
,
2118 "inappropriate sadb flush message passed.\n");
2129 getsadbpolicy(policy0
, policylen0
, type
, iph2
)
2131 int *policylen0
, type
;
2132 struct ph2handle
*iph2
;
2134 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2135 struct sadb_x_policy
*xpl
;
2136 struct sadb_x_ipsecrequest
*xisr
;
2143 /* get policy buffer size */
2144 policylen
= sizeof(struct sadb_x_policy
);
2145 if (type
!= SADB_X_SPDDELETE
) {
2146 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2147 xisrlen
= sizeof(*xisr
);
2148 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2149 xisrlen
+= (sysdep_sa_len(iph2
->src
)
2150 + sysdep_sa_len(iph2
->dst
));
2153 policylen
+= PFKEY_ALIGN8(xisrlen
);
2157 /* make policy structure */
2158 policy
= racoon_malloc(policylen
);
2160 plog(LLV_ERROR
, LOCATION
, NULL
,
2161 "buffer allocation failed.\n");
2165 xpl
= (struct sadb_x_policy
*)policy
;
2166 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(policylen
);
2167 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2168 xpl
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2169 xpl
->sadb_x_policy_dir
= spidx
->dir
;
2170 xpl
->sadb_x_policy_id
= 0;
2171 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2172 xpl
->sadb_x_policy_priority
= PRIORITY_DEFAULT
;
2175 /* no need to append policy information any more if type is SPDDELETE */
2176 if (type
== SADB_X_SPDDELETE
)
2179 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
2181 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2183 satype
= doi2ipproto(pr
->proto_id
);
2185 plog(LLV_ERROR
, LOCATION
, NULL
,
2186 "invalid proto_id %d\n", pr
->proto_id
);
2189 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
2191 plog(LLV_ERROR
, LOCATION
, NULL
,
2192 "invalid encmode %d\n", pr
->encmode
);
2197 * the policy level cannot be unique because the policy
2198 * is defined later than SA, so req_id cannot be bound to SA.
2200 xisr
->sadb_x_ipsecrequest_proto
= satype
;
2201 xisr
->sadb_x_ipsecrequest_mode
= mode
;
2202 xisr
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2203 xisr
->sadb_x_ipsecrequest_reqid
= 0;
2204 p
= (caddr_t
)(xisr
+ 1);
2206 xisrlen
= sizeof(*xisr
);
2208 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2209 int src_len
, dst_len
;
2211 src_len
= sysdep_sa_len(iph2
->src
);
2212 dst_len
= sysdep_sa_len(iph2
->dst
);
2213 xisrlen
+= src_len
+ dst_len
;
2215 memcpy(p
, iph2
->src
, src_len
);
2218 memcpy(p
, iph2
->dst
, dst_len
);
2222 xisr
->sadb_x_ipsecrequest_len
= PFKEY_ALIGN8(xisrlen
);
2227 *policylen0
= policylen
;
2233 racoon_free(policy
);
2239 pk_sendspdupdate2(iph2
)
2240 struct ph2handle
*iph2
;
2242 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2243 caddr_t policy
= NULL
;
2245 u_int64_t ltime
, vtime
;
2247 ltime
= iph2
->approval
->lifetime
;
2250 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDUPDATE
, iph2
)) {
2251 plog(LLV_ERROR
, LOCATION
, NULL
,
2252 "getting sadb policy failed.\n");
2256 if (pfkey_send_spdupdate2(
2258 (struct sockaddr
*)&spidx
->src
,
2260 (struct sockaddr
*)&spidx
->dst
,
2264 policy
, policylen
, 0) < 0) {
2265 plog(LLV_ERROR
, LOCATION
, NULL
,
2266 "libipsec failed send spdupdate2 (%s)\n",
2270 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdupdate2\n");
2274 racoon_free(policy
);
2280 pk_recvspdupdate(mhp
)
2283 struct sadb_address
*saddr
, *daddr
;
2284 struct sadb_x_policy
*xpl
;
2285 struct policyindex spidx
;
2286 struct secpolicy
*sp
;
2290 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2291 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2292 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2293 plog(LLV_ERROR
, LOCATION
, NULL
,
2294 "inappropriate sadb spdupdate message passed.\n");
2297 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2298 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2299 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2301 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2302 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2305 saddr
->sadb_address_prefixlen
,
2306 daddr
->sadb_address_prefixlen
,
2307 saddr
->sadb_address_proto
,
2308 xpl
->sadb_x_policy_priority
,
2311 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2314 saddr
->sadb_address_prefixlen
,
2315 daddr
->sadb_address_prefixlen
,
2316 saddr
->sadb_address_proto
,
2322 plog(LLV_ERROR
, LOCATION
, NULL
,
2323 "such policy does not already exist: \"%s\"\n",
2330 if (addnewsp(mhp
) < 0)
2337 * this function has to be used by responder side.
2340 pk_sendspdadd2(iph2
)
2341 struct ph2handle
*iph2
;
2343 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2344 caddr_t policy
= NULL
;
2346 u_int64_t ltime
, vtime
;
2348 ltime
= iph2
->approval
->lifetime
;
2351 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDADD
, iph2
)) {
2352 plog(LLV_ERROR
, LOCATION
, NULL
,
2353 "getting sadb policy failed.\n");
2357 if (pfkey_send_spdadd2(
2359 (struct sockaddr
*)&spidx
->src
,
2361 (struct sockaddr
*)&spidx
->dst
,
2365 policy
, policylen
, 0) < 0) {
2366 plog(LLV_ERROR
, LOCATION
, NULL
,
2367 "libipsec failed send spdadd2 (%s)\n",
2371 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdadd2\n");
2375 racoon_free(policy
);
2384 struct sadb_address
*saddr
, *daddr
;
2385 struct sadb_x_policy
*xpl
;
2386 struct policyindex spidx
;
2387 struct secpolicy
*sp
;
2391 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2392 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2393 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2394 plog(LLV_ERROR
, LOCATION
, NULL
,
2395 "inappropriate sadb spdadd message passed.\n");
2398 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2399 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2400 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2402 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2403 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2406 saddr
->sadb_address_prefixlen
,
2407 daddr
->sadb_address_prefixlen
,
2408 saddr
->sadb_address_proto
,
2409 xpl
->sadb_x_policy_priority
,
2412 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2415 saddr
->sadb_address_prefixlen
,
2416 daddr
->sadb_address_prefixlen
,
2417 saddr
->sadb_address_proto
,
2423 plog(LLV_ERROR
, LOCATION
, NULL
,
2424 "such policy already exists. "
2425 "anyway replace it: %s\n",
2431 if (addnewsp(mhp
) < 0)
2438 * this function has to be used by responder side.
2441 pk_sendspddelete(iph2
)
2442 struct ph2handle
*iph2
;
2444 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2445 caddr_t policy
= NULL
;
2448 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDDELETE
, iph2
)) {
2449 plog(LLV_ERROR
, LOCATION
, NULL
,
2450 "getting sadb policy failed.\n");
2454 if (pfkey_send_spddelete(
2456 (struct sockaddr
*)&spidx
->src
,
2458 (struct sockaddr
*)&spidx
->dst
,
2461 policy
, policylen
, 0) < 0) {
2462 plog(LLV_ERROR
, LOCATION
, NULL
,
2463 "libipsec failed send spddelete (%s)\n",
2467 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spddelete\n");
2471 racoon_free(policy
);
2477 pk_recvspddelete(mhp
)
2480 struct sadb_address
*saddr
, *daddr
;
2481 struct sadb_x_policy
*xpl
;
2482 struct policyindex spidx
;
2483 struct secpolicy
*sp
;
2487 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2488 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2489 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2490 plog(LLV_ERROR
, LOCATION
, NULL
,
2491 "inappropriate sadb spddelete message passed.\n");
2494 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2495 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2496 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2498 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2499 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2502 saddr
->sadb_address_prefixlen
,
2503 daddr
->sadb_address_prefixlen
,
2504 saddr
->sadb_address_proto
,
2505 xpl
->sadb_x_policy_priority
,
2508 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2511 saddr
->sadb_address_prefixlen
,
2512 daddr
->sadb_address_prefixlen
,
2513 saddr
->sadb_address_proto
,
2519 plog(LLV_ERROR
, LOCATION
, NULL
,
2520 "no policy found: %s\n",
2525 purgephXbyspid(xpl
->sadb_x_policy_id
, true);
2534 pk_recvspdexpire(mhp
)
2537 struct sadb_address
*saddr
, *daddr
;
2538 struct sadb_x_policy
*xpl
;
2539 struct policyindex spidx
;
2540 struct secpolicy
*sp
;
2544 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2545 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2546 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2547 plog(LLV_ERROR
, LOCATION
, NULL
,
2548 "inappropriate sadb spdexpire message passed.\n");
2551 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2552 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2553 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2555 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2556 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2559 saddr
->sadb_address_prefixlen
,
2560 daddr
->sadb_address_prefixlen
,
2561 saddr
->sadb_address_proto
,
2562 xpl
->sadb_x_policy_priority
,
2565 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2568 saddr
->sadb_address_prefixlen
,
2569 daddr
->sadb_address_prefixlen
,
2570 saddr
->sadb_address_proto
,
2576 plog(LLV_ERROR
, LOCATION
, NULL
,
2577 "no policy found: %s\n",
2582 purgephXbyspid(xpl
->sadb_x_policy_id
, false);
2595 if (mhp
[0] == NULL
) {
2596 plog(LLV_ERROR
, LOCATION
, NULL
,
2597 "inappropriate sadb spdget message passed.\n");
2608 struct sadb_msg
*msg
;
2609 struct sadb_address
*saddr
, *daddr
;
2610 struct sadb_x_policy
*xpl
;
2611 struct policyindex spidx
;
2612 struct secpolicy
*sp
;
2615 if (mhp
[0] == NULL
) {
2616 plog(LLV_ERROR
, LOCATION
, NULL
,
2617 "inappropriate sadb spddump message passed.\n");
2620 msg
= (struct sadb_msg
*)mhp
[0];
2622 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2623 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2624 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2626 if (saddr
== NULL
|| daddr
== NULL
|| xpl
== NULL
) {
2627 plog(LLV_ERROR
, LOCATION
, NULL
,
2628 "inappropriate sadb spddump message passed.\n");
2632 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2633 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2636 saddr
->sadb_address_prefixlen
,
2637 daddr
->sadb_address_prefixlen
,
2638 saddr
->sadb_address_proto
,
2639 xpl
->sadb_x_policy_priority
,
2642 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2645 saddr
->sadb_address_prefixlen
,
2646 daddr
->sadb_address_prefixlen
,
2647 saddr
->sadb_address_proto
,
2653 plog(LLV_ERROR
, LOCATION
, NULL
,
2654 "such policy already exists. "
2655 "anyway replace it: %s\n",
2661 if (addnewsp(mhp
) < 0)
2668 pk_recvspdflush(mhp
)
2672 if (mhp
[0] == NULL
) {
2673 plog(LLV_ERROR
, LOCATION
, NULL
,
2674 "inappropriate sadb spdflush message passed.\n");
2686 * send error against acquire message to kenrel.
2689 pk_sendeacquire(iph2
)
2690 struct ph2handle
*iph2
;
2692 struct sadb_msg
*newmsg
;
2695 len
= sizeof(struct sadb_msg
);
2696 newmsg
= racoon_calloc(1, len
);
2697 if (newmsg
== NULL
) {
2698 plog(LLV_ERROR
, LOCATION
, NULL
,
2699 "failed to get buffer to send acquire.\n");
2703 memset(newmsg
, 0, len
);
2704 newmsg
->sadb_msg_version
= PF_KEY_V2
;
2705 newmsg
->sadb_msg_type
= SADB_ACQUIRE
;
2706 newmsg
->sadb_msg_errno
= ENOENT
; /* XXX */
2707 newmsg
->sadb_msg_satype
= iph2
->satype
;
2708 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
2709 newmsg
->sadb_msg_reserved
= 0;
2710 newmsg
->sadb_msg_seq
= iph2
->seq
;
2711 newmsg
->sadb_msg_pid
= (u_int32_t
)getpid();
2714 len
= pfkey_send(lcconf
->sock_pfkey
, newmsg
, len
);
2716 racoon_free(newmsg
);
2722 pk_sendget_inbound_sastats(ike_session_t
*session
)
2724 u_int32_t max_stats
;
2728 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2732 session
->traffic_monitor
.num_in_curr_req
= 0;
2733 bzero(session
->traffic_monitor
.in_curr_req
, sizeof(session
->traffic_monitor
.in_curr_req
));
2734 max_stats
= (sizeof(session
->traffic_monitor
.in_curr_req
) / sizeof(session
->traffic_monitor
.in_curr_req
[0]));
2737 if ((session
->traffic_monitor
.num_in_curr_req
= ike_session_get_sas_for_stats(session
,
2740 session
->traffic_monitor
.in_curr_req
,
2742 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2744 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2746 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2751 session
->traffic_monitor
.in_curr_req
,
2752 session
->traffic_monitor
.num_in_curr_req
) < 0) {
2755 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2757 return session
->traffic_monitor
.num_in_curr_req
;
2763 pk_sendget_outbound_sastats(ike_session_t
*session
)
2765 u_int32_t max_stats
;
2769 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2773 session
->traffic_monitor
.num_out_curr_req
= 0;
2774 bzero(session
->traffic_monitor
.out_curr_req
, sizeof(session
->traffic_monitor
.out_curr_req
));
2775 max_stats
= (sizeof(session
->traffic_monitor
.out_curr_req
) / sizeof(session
->traffic_monitor
.out_curr_req
[0]));
2778 if ((session
->traffic_monitor
.num_out_curr_req
= ike_session_get_sas_for_stats(session
,
2781 session
->traffic_monitor
.out_curr_req
,
2783 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2785 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2787 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2792 session
->traffic_monitor
.out_curr_req
,
2793 session
->traffic_monitor
.num_out_curr_req
) < 0) {
2796 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2798 return session
->traffic_monitor
.num_out_curr_req
;
2804 * receive GETSPDSTAT from kernel.
2807 pk_recvgetsastat(mhp
)
2810 struct sadb_msg
*msg
;
2811 struct sadb_session_id
*session_id
;
2812 struct sadb_sastat
*stat_resp
;
2813 ike_session_t
*session
;
2815 /* validity check */
2816 if (mhp
[0] == NULL
||
2817 mhp
[SADB_EXT_SESSION_ID
] == NULL
||
2818 mhp
[SADB_EXT_SASTAT
] == NULL
) {
2819 plog(LLV_ERROR
, LOCATION
, NULL
,
2820 "inappropriate sadb getsastat response.\n");
2823 msg
= (struct sadb_msg
*)mhp
[0];
2824 session_id
= (ike_session_t
*)mhp
[SADB_EXT_SESSION_ID
];
2825 stat_resp
= (struct sadb_sastat
*)mhp
[SADB_EXT_SASTAT
];
2827 /* the message has to be processed or not ? */
2828 if (msg
->sadb_msg_pid
!= getpid()) {
2829 plog(LLV_DEBUG
, LOCATION
, NULL
,
2830 "%s message is not interesting "
2831 "because pid %d is not mine.\n",
2832 s_pfkey_type(msg
->sadb_msg_type
),
2836 if (!session_id
->sadb_session_id_v
[0]) {
2837 plog(LLV_DEBUG
, LOCATION
, NULL
,
2838 "%s message is bad "
2839 "because session-id[0] is invalid.\n",
2840 s_pfkey_type(msg
->sadb_msg_type
));
2843 session
= (__typeof__(session
))session_id
->sadb_session_id_v
[0];
2845 if (!stat_resp
->sadb_sastat_list_len
) {
2846 plog(LLV_DEBUG
, LOCATION
, NULL
,
2847 "%s message is bad "
2848 "because it has no sastats.\n",
2849 s_pfkey_type(msg
->sadb_msg_type
));
2853 ike_session_update_traffic_idle_status(session
,
2854 stat_resp
->sadb_sastat_dir
,
2855 (struct sastat
*)(stat_resp
+ 1),
2856 stat_resp
->sadb_sastat_list_len
);
2861 * check if the algorithm is supported or not.
2866 pk_checkalg(class, calg
, keylen
)
2867 int class, calg
, keylen
;
2871 struct sadb_alg alg0
;
2873 switch (algclass2doi(class)) {
2874 case IPSECDOI_PROTO_IPSEC_ESP
:
2875 sup
= SADB_EXT_SUPPORTED_ENCRYPT
;
2877 case IPSECDOI_ATTR_AUTH
:
2878 sup
= SADB_EXT_SUPPORTED_AUTH
;
2880 case IPSECDOI_PROTO_IPCOMP
:
2881 plog(LLV_DEBUG
, LOCATION
, NULL
,
2882 "compression algorithm can not be checked "
2883 "because sadb message doesn't support it.\n");
2886 plog(LLV_ERROR
, LOCATION
, NULL
,
2887 "invalid algorithm class.\n");
2890 alg
= ipsecdoi2pfkey_alg(algclass2doi(class), algtype2doi(class, calg
));
2895 if (ipsec_get_keylen(sup
, alg
, &alg0
)) {
2896 plog(LLV_ERROR
, LOCATION
, NULL
,
2897 "%s.\n", ipsec_strerror());
2900 keylen
= alg0
.sadb_alg_minbits
;
2903 error
= ipsec_check_keylen(sup
, alg
, keylen
);
2905 plog(LLV_ERROR
, LOCATION
, NULL
,
2906 "%s.\n", ipsec_strerror());
2912 * differences with pfkey_recv() in libipsec/pfkey.c:
2913 * - never performs busy wait loop.
2914 * - returns NULL and set *lenp to negative on fatal failures
2915 * - returns NULL and set *lenp to non-negative on non-fatal failures
2916 * - returns non-NULL on success
2918 static struct sadb_msg
*
2923 struct sadb_msg
*newmsg
;
2925 socklen_t optlen
= sizeof(reallen
);
2927 if (getsockopt(so
, SOL_SOCKET
, SO_NREAD
, &reallen
, &optlen
) < 0)
2928 return NULL
; /*fatal*/
2933 if ((newmsg
= racoon_calloc(1, reallen
)) == NULL
)
2936 *lenp
= recv(so
, (caddr_t
)newmsg
, reallen
, 0);
2938 racoon_free(newmsg
);
2939 return NULL
; /*fatal*/
2940 } else if (*lenp
!= reallen
|| *lenp
< sizeof(struct sadb_msg
)) {
2941 racoon_free(newmsg
);
2952 return eay_random();
2959 struct secpolicy
*new;
2960 struct sadb_address
*saddr
, *daddr
;
2961 struct sadb_x_policy
*xpl
;
2964 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2965 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2966 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2967 plog(LLV_ERROR
, LOCATION
, NULL
,
2968 "inappropriate sadb spd management message passed.\n");
2972 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2973 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2974 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2978 plog(LLV_ERROR
, LOCATION
, NULL
,
2979 "failed to allocate buffer\n");
2983 new->spidx
.dir
= xpl
->sadb_x_policy_dir
;
2984 new->id
= xpl
->sadb_x_policy_id
;
2985 new->policy
= xpl
->sadb_x_policy_type
;
2989 switch (xpl
->sadb_x_policy_type
) {
2990 case IPSEC_POLICY_DISCARD
:
2991 case IPSEC_POLICY_GENERATE
:
2992 case IPSEC_POLICY_NONE
:
2993 case IPSEC_POLICY_ENTRUST
:
2994 case IPSEC_POLICY_BYPASS
:
2997 case IPSEC_POLICY_IPSEC
:
3000 struct sadb_x_ipsecrequest
*xisr
;
3001 struct ipsecrequest
**p_isr
= &new->req
;
3003 /* validity check */
3004 if (PFKEY_EXTLEN(xpl
) < sizeof(*xpl
)) {
3005 plog(LLV_ERROR
, LOCATION
, NULL
,
3006 "invalid msg length.\n");
3010 tlen
= PFKEY_EXTLEN(xpl
) - sizeof(*xpl
);
3011 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
3016 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
3017 plog(LLV_ERROR
, LOCATION
, NULL
,
3018 "invalid msg length.\n");
3022 /* allocate request buffer */
3023 *p_isr
= newipsecreq();
3024 if (*p_isr
== NULL
) {
3025 plog(LLV_ERROR
, LOCATION
, NULL
,
3026 "failed to get new ipsecreq.\n");
3031 (*p_isr
)->next
= NULL
;
3033 switch (xisr
->sadb_x_ipsecrequest_proto
) {
3036 case IPPROTO_IPCOMP
:
3039 plog(LLV_ERROR
, LOCATION
, NULL
,
3040 "invalid proto type: %u\n",
3041 xisr
->sadb_x_ipsecrequest_proto
);
3044 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
3046 switch (xisr
->sadb_x_ipsecrequest_mode
) {
3047 case IPSEC_MODE_TRANSPORT
:
3048 case IPSEC_MODE_TUNNEL
:
3050 case IPSEC_MODE_ANY
:
3052 plog(LLV_ERROR
, LOCATION
, NULL
,
3053 "invalid mode: %u\n",
3054 xisr
->sadb_x_ipsecrequest_mode
);
3057 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
3059 switch (xisr
->sadb_x_ipsecrequest_level
) {
3060 case IPSEC_LEVEL_DEFAULT
:
3061 case IPSEC_LEVEL_USE
:
3062 case IPSEC_LEVEL_REQUIRE
:
3064 case IPSEC_LEVEL_UNIQUE
:
3065 (*p_isr
)->saidx
.reqid
=
3066 xisr
->sadb_x_ipsecrequest_reqid
;
3070 plog(LLV_ERROR
, LOCATION
, NULL
,
3071 "invalid level: %u\n",
3072 xisr
->sadb_x_ipsecrequest_level
);
3075 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
3077 /* set IP addresses if there */
3078 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
3079 struct sockaddr
*paddr
;
3081 paddr
= (struct sockaddr
*)(xisr
+ 1);
3082 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
3083 sysdep_sa_len(paddr
));
3085 paddr
= (struct sockaddr
*)((caddr_t
)paddr
3086 + sysdep_sa_len(paddr
));
3087 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
3088 sysdep_sa_len(paddr
));
3093 /* initialization for the next. */
3094 p_isr
= &(*p_isr
)->next
;
3095 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
3097 /* validity check */
3099 plog(LLV_ERROR
, LOCATION
, NULL
,
3100 "becoming tlen < 0\n");
3103 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
3104 + xisr
->sadb_x_ipsecrequest_len
);
3109 plog(LLV_ERROR
, LOCATION
, NULL
,
3110 "invalid policy type.\n");
3114 #ifdef HAVE_PFKEY_POLICY_PRIORITY
3115 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3118 saddr
->sadb_address_prefixlen
,
3119 daddr
->sadb_address_prefixlen
,
3120 saddr
->sadb_address_proto
,
3121 xpl
->sadb_x_policy_priority
,
3124 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3127 saddr
->sadb_address_prefixlen
,
3128 daddr
->sadb_address_prefixlen
,
3129 saddr
->sadb_address_proto
,
3138 /* proto/mode/src->dst spi */
3140 sadbsecas2str(src
, dst
, proto
, spi
, mode
)
3141 struct sockaddr
*src
, *dst
;
3146 static char buf
[256];
3147 u_int doi_proto
, doi_mode
= 0;
3151 doi_proto
= pfkey2ipsecdoi_proto(proto
);
3152 if (doi_proto
== ~0)
3155 doi_mode
= pfkey2ipsecdoi_mode(mode
);
3160 blen
= sizeof(buf
) - 1;
3163 i
= snprintf(p
, blen
, "%s%s%s ",
3164 s_ipsecdoi_proto(doi_proto
),
3166 mode
? s_ipsecdoi_encmode(doi_mode
) : "");
3167 if (i
< 0 || i
>= blen
)
3172 i
= snprintf(p
, blen
, "%s->", saddr2str(src
));
3173 if (i
< 0 || i
>= blen
)
3178 i
= snprintf(p
, blen
, "%s ", saddr2str(dst
));
3179 if (i
< 0 || i
>= blen
)
3185 snprintf(p
, blen
, "spi=%lu(0x%lx)", (unsigned long)ntohl(spi
),
3186 (unsigned long)ntohl(spi
));