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>
48 # include <linux/udp.h>
50 # if defined(__NetBSD__) || defined(__FreeBSD__)
51 # include <netinet/udp.h>
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <sys/queue.h>
59 #include <sys/sysctl.h>
61 #include <net/route.h>
63 #include <System/net/pfkeyv2.h>
65 #include <net/pfkeyv2.h>
68 #include <netinet/in.h>
69 #ifndef HAVE_NETINET6_IPSEC
70 #include <netinet/ipsec.h>
72 #include <netinet6/ipsec.h>
85 #include "localconf.h"
86 #include "remoteconf.h"
87 #include "isakmp_var.h"
89 #include "isakmp_inf.h"
90 #include "ipsec_doi.h"
95 #include "algorithm.h"
100 #include "strnames.h"
101 #include "backupsa.h"
102 #include "gcmalloc.h"
103 #include "nattraversal.h"
104 #include "crypto_openssl.h"
105 #include "grabmyaddr.h"
106 #include "vpn_control.h"
107 #include "vpn_control_var.h"
108 #include "ike_session.h"
109 #include "ipsecSessionTracer.h"
110 #include "ipsecMessageTracer.h"
112 #if defined(SADB_X_EALG_RIJNDAELCBC) && !defined(SADB_X_EALG_AESCBC)
113 #define SADB_X_EALG_AESCBC SADB_X_EALG_RIJNDAELCBC
117 static u_int ipsecdoi2pfkey_aalg
__P((u_int
));
118 static u_int ipsecdoi2pfkey_ealg
__P((u_int
));
119 static u_int ipsecdoi2pfkey_calg
__P((u_int
));
120 static u_int ipsecdoi2pfkey_alg
__P((u_int
, u_int
));
121 static u_int keylen_aalg
__P((u_int
));
122 static u_int keylen_ealg
__P((u_int
, int));
124 static int pk_recvgetspi
__P((caddr_t
*));
125 static int pk_recvupdate
__P((caddr_t
*));
126 static int pk_recvadd
__P((caddr_t
*));
127 static int pk_recvdelete
__P((caddr_t
*));
128 static int pk_recvacquire
__P((caddr_t
*));
129 static int pk_recvexpire
__P((caddr_t
*));
130 static int pk_recvflush
__P((caddr_t
*));
131 static int getsadbpolicy
__P((caddr_t
*, int *, int, struct ph2handle
*));
132 static int pk_recvspdupdate
__P((caddr_t
*));
133 static int pk_recvspdadd
__P((caddr_t
*));
134 static int pk_recvspddelete
__P((caddr_t
*));
135 static int pk_recvspdexpire
__P((caddr_t
*));
136 static int pk_recvspdget
__P((caddr_t
*));
137 static int pk_recvspddump
__P((caddr_t
*));
138 static int pk_recvspdflush
__P((caddr_t
*));
139 static int pk_recvgetsastat
__P((caddr_t
*));
140 static struct sadb_msg
*pk_recv
__P((int, int *));
142 static int (*pkrecvf
[]) __P((caddr_t
*)) = {
150 NULL
, /* SABD_REGISTER */
153 NULL
, /* SADB_DUMP */
154 NULL
, /* SADB_X_PROMISC */
155 NULL
, /* SADB_X_PCHANGE */
160 NULL
, /* SADB_X_SPDACQUIRE */
163 NULL
, /* SADB_X_SPDSETIDX */
165 NULL
, /* SADB_X_SPDDELETE2 */
166 pk_recvgetsastat
, /* SADB_GETSASTAT */
167 NULL
, /* SADB_X_NAT_T_NEW_MAPPING */
168 NULL
, /* SADB_X_MIGRATE */
170 #error "SADB extra message?"
174 static int addnewsp
__P((caddr_t
*));
176 /* cope with old kame headers - ugly */
177 #ifndef SADB_X_AALG_MD5
178 #define SADB_X_AALG_MD5 SADB_AALG_MD5
180 #ifndef SADB_X_AALG_SHA
181 #define SADB_X_AALG_SHA SADB_AALG_SHA
183 #ifndef SADB_X_AALG_NULL
184 #define SADB_X_AALG_NULL SADB_AALG_NULL
187 #ifndef SADB_X_EALG_BLOWFISHCBC
188 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
190 #ifndef SADB_X_EALG_CAST128CBC
191 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC
193 #ifndef SADB_X_EALG_RC5CBC
194 #ifdef SADB_EALG_RC5CBC
195 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
201 struct sadb_msg
*msg
;
203 caddr_t mhp
[SADB_EXT_MAX
+ 1];
206 plog(LLV_DEBUG
, LOCATION
, NULL
, "get pfkey %s message\n",
207 s_pfkey_type(msg
->sadb_msg_type
));
208 plogdump(LLV_DEBUG2
, msg
, msg
->sadb_msg_len
<< 3);
211 if (msg
->sadb_msg_errno
) {
214 /* when SPD is empty, treat the state as no error. */
215 if (msg
->sadb_msg_type
== SADB_X_SPDDUMP
&&
216 msg
->sadb_msg_errno
== ENOENT
)
221 plog(pri
, LOCATION
, NULL
,
222 "pfkey %s failed: %s\n",
223 s_pfkey_type(msg
->sadb_msg_type
),
224 strerror(msg
->sadb_msg_errno
));
229 /* check pfkey message. */
230 if (pfkey_align(msg
, mhp
)) {
231 plog(LLV_ERROR
, LOCATION
, NULL
,
232 "libipsec failed pfkey align (%s)\n",
236 if (pfkey_check(mhp
)) {
237 plog(LLV_ERROR
, LOCATION
, NULL
,
238 "libipsec failed pfkey check (%s)\n",
242 msg
= (struct sadb_msg
*)mhp
[0];
245 if (msg
->sadb_msg_type
>= ARRAYLEN(pkrecvf
)) {
246 plog(LLV_ERROR
, LOCATION
, NULL
,
247 "unknown PF_KEY message type=%u\n",
252 if (pkrecvf
[msg
->sadb_msg_type
] == NULL
) {
253 plog(LLV_INFO
, LOCATION
, NULL
,
254 "unsupported PF_KEY message %s\n",
255 s_pfkey_type(msg
->sadb_msg_type
));
259 if ((pkrecvf
[msg
->sadb_msg_type
])(mhp
) < 0)
270 * PF_KEY packet handler
277 struct sadb_msg
*msg
;
280 /* receive pfkey message. */
282 msg
= (struct sadb_msg
*)pk_recv(lcconf
->sock_pfkey
, &len
);
285 plog(LLV_ERROR
, LOCATION
, NULL
,
286 "failed to recv from pfkey (%s)\n",
290 /* short message - msg not ready */
294 return pfkey_process(msg
);
300 struct saved_msg_elem
*elem
;
301 struct saved_msg_elem
*elem_tmp
= NULL
;
303 TAILQ_FOREACH_SAFE(elem
, &lcconf
->saved_msg_queue
, chain
, elem_tmp
) {
304 pfkey_process((struct sadb_msg
*)elem
->msg
);
305 TAILQ_REMOVE(&lcconf
->saved_msg_queue
, elem
, chain
);
313 struct sadb_msg
*msg
;
315 struct saved_msg_elem
*elem
;
317 elem
= (struct saved_msg_elem
*)racoon_calloc(sizeof(struct saved_msg_elem
), 1);
321 TAILQ_INSERT_TAIL(&lcconf
->saved_msg_queue
, elem
, chain
);
329 pfkey_dump_sadb(satype
)
334 pid_t pid
= getpid();
335 struct sadb_msg
*msg
= NULL
;
339 if ((s
= privsep_pfkey_open()) < 0) {
340 plog(LLV_ERROR
, LOCATION
, NULL
,
341 "libipsec failed pfkey open: %s\n",
346 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_dump\n");
347 if (pfkey_send_dump(s
, satype
) < 0) {
348 plog(LLV_ERROR
, LOCATION
, NULL
,
349 "libipsec failed dump: %s\n", ipsec_strerror());
356 msg
= pk_recv(s
, &len
);
364 if (msg
->sadb_msg_pid
!= pid
)
368 * for multi-processor system this had to be added because the messages can
369 * be interleaved - they won't all be dump messages
371 if (msg
->sadb_msg_type
!= SADB_DUMP
) { /* save for later processing */
377 ml
= msg
->sadb_msg_len
<< 3;
378 bl
= buf
? buf
->l
: 0;
379 buf
= vrealloc(buf
, bl
+ ml
);
381 plog(LLV_ERROR
, LOCATION
, NULL
,
382 "failed to reallocate buffer to dump.\n");
385 memcpy(buf
->v
+ bl
, msg
, ml
);
387 if (msg
->sadb_msg_seq
== 0)
400 privsep_pfkey_close(s
);
404 #ifdef ENABLE_ADMINPORT
409 pfkey_flush_sadb(proto
)
414 /* convert to SADB_SATYPE */
415 if ((satype
= admin2pfkey_proto(proto
)) < 0)
418 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_flush\n");
419 if (pfkey_send_flush(lcconf
->sock_pfkey
, satype
) < 0) {
420 plog(LLV_ERROR
, LOCATION
, NULL
,
421 "libipsec failed send flush (%s)\n", ipsec_strerror());
430 * These are the SATYPEs that we manage. We register to get
431 * PF_KEY messages related to these SATYPEs, and we also use
432 * this list to determine which SATYPEs to delete SAs for when
433 * we receive an INITIAL-CONTACT.
435 const struct pfkey_satype pfkey_satypes
[] = {
436 { SADB_SATYPE_AH
, "AH" },
437 { SADB_SATYPE_ESP
, "ESP" },
438 { SADB_X_SATYPE_IPCOMP
, "IPCOMP" },
440 const int pfkey_nsatypes
=
441 sizeof(pfkey_satypes
) / sizeof(pfkey_satypes
[0]);
444 * PF_KEY initialization
451 if ((lcconf
->sock_pfkey
= privsep_pfkey_open()) < 0) {
452 plog(LLV_ERROR
, LOCATION
, NULL
,
453 "libipsec failed pfkey open (%s)\n", ipsec_strerror());
457 for (i
= 0, reg_fail
= 0; i
< pfkey_nsatypes
; i
++) {
458 plog(LLV_DEBUG
, LOCATION
, NULL
,
459 "call pfkey_send_register for %s\n",
460 pfkey_satypes
[i
].ps_name
);
461 if (pfkey_send_register(lcconf
->sock_pfkey
,
462 pfkey_satypes
[i
].ps_satype
) < 0 ||
463 pfkey_recv_register(lcconf
->sock_pfkey
) < 0) {
464 plog(LLV_WARNING
, LOCATION
, NULL
,
465 "failed to register %s (%s)\n",
466 pfkey_satypes
[i
].ps_name
,
472 if (reg_fail
== pfkey_nsatypes
) {
473 plog(LLV_ERROR
, LOCATION
, NULL
,
474 "failed to regist any protocol.\n");
475 pfkey_close(lcconf
->sock_pfkey
);
481 if (pfkey_send_spddump(lcconf
->sock_pfkey
) < 0) {
482 plog(LLV_ERROR
, LOCATION
, NULL
,
483 "libipsec sending spddump failed: %s\n",
485 pfkey_close(lcconf
->sock_pfkey
);
489 if (pfkey_promisc_toggle(1) < 0) {
490 pfkey_close(lcconf
->sock_pfkey
);
497 /* %%% for conversion */
498 /* IPSECDOI_ATTR_AUTH -> SADB_AALG */
500 ipsecdoi2pfkey_aalg(hashtype
)
504 case IPSECDOI_ATTR_AUTH_HMAC_MD5
:
505 return SADB_AALG_MD5HMAC
;
506 case IPSECDOI_ATTR_AUTH_HMAC_SHA1
:
507 return SADB_AALG_SHA1HMAC
;
508 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_256
:
509 #if (defined SADB_X_AALG_SHA2_256) && !defined(SADB_X_AALG_SHA2_256HMAC)
510 return SADB_X_AALG_SHA2_256
;
512 return SADB_X_AALG_SHA2_256HMAC
;
514 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_384
:
515 #if (defined SADB_X_AALG_SHA2_384) && !defined(SADB_X_AALG_SHA2_384HMAC)
516 return SADB_X_AALG_SHA2_384
;
518 return SADB_X_AALG_SHA2_384HMAC
;
520 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_512
:
521 #if (defined SADB_X_AALG_SHA2_512) && !defined(SADB_X_AALG_SHA2_512HMAC)
522 return SADB_X_AALG_SHA2_512
;
524 return SADB_X_AALG_SHA2_512HMAC
;
526 case IPSECDOI_ATTR_AUTH_KPDK
: /* need special care */
527 return SADB_AALG_NONE
;
530 case IPSECDOI_ATTR_AUTH_DES_MAC
:
531 plog(LLV_ERROR
, LOCATION
, NULL
,
532 "Not supported hash type: %u\n", hashtype
);
535 case 0: /* reserved */
537 return SADB_AALG_NONE
;
539 plog(LLV_ERROR
, LOCATION
, NULL
,
540 "Invalid hash type: %u\n", hashtype
);
546 /* IPSECDOI_ESP -> SADB_EALG */
548 ipsecdoi2pfkey_ealg(t_id
)
552 case IPSECDOI_ESP_DES_IV64
: /* sa_flags |= SADB_X_EXT_OLD */
553 return SADB_EALG_DESCBC
;
554 case IPSECDOI_ESP_DES
:
555 return SADB_EALG_DESCBC
;
556 case IPSECDOI_ESP_3DES
:
557 return SADB_EALG_3DESCBC
;
558 #ifdef SADB_X_EALG_RC5CBC
559 case IPSECDOI_ESP_RC5
:
560 return SADB_X_EALG_RC5CBC
;
562 case IPSECDOI_ESP_CAST
:
563 return SADB_X_EALG_CAST128CBC
;
564 case IPSECDOI_ESP_BLOWFISH
:
565 return SADB_X_EALG_BLOWFISHCBC
;
566 case IPSECDOI_ESP_DES_IV32
: /* flags |= (SADB_X_EXT_OLD|
568 return SADB_EALG_DESCBC
;
569 case IPSECDOI_ESP_NULL
:
570 return SADB_EALG_NULL
;
571 #ifdef SADB_X_EALG_AESCBC
572 case IPSECDOI_ESP_AES
:
573 return SADB_X_EALG_AESCBC
;
575 #ifdef SADB_X_EALG_TWOFISHCBC
576 case IPSECDOI_ESP_TWOFISH
:
577 return SADB_X_EALG_TWOFISHCBC
;
581 case IPSECDOI_ESP_3IDEA
:
582 case IPSECDOI_ESP_IDEA
:
583 case IPSECDOI_ESP_RC4
:
584 plog(LLV_ERROR
, LOCATION
, NULL
,
585 "Not supported transform: %u\n", t_id
);
588 case 0: /* reserved */
590 plog(LLV_ERROR
, LOCATION
, NULL
,
591 "Invalid transform id: %u\n", t_id
);
597 /* IPCOMP -> SADB_CALG */
599 ipsecdoi2pfkey_calg(t_id
)
603 case IPSECDOI_IPCOMP_OUI
:
604 return SADB_X_CALG_OUI
;
605 case IPSECDOI_IPCOMP_DEFLATE
:
606 return SADB_X_CALG_DEFLATE
;
607 case IPSECDOI_IPCOMP_LZS
:
608 return SADB_X_CALG_LZS
;
610 case 0: /* reserved */
612 plog(LLV_ERROR
, LOCATION
, NULL
,
613 "Invalid transform id: %u\n", t_id
);
619 /* IPSECDOI_PROTO -> SADB_SATYPE */
621 ipsecdoi2pfkey_proto(proto
)
625 case IPSECDOI_PROTO_IPSEC_AH
:
626 return SADB_SATYPE_AH
;
627 case IPSECDOI_PROTO_IPSEC_ESP
:
628 return SADB_SATYPE_ESP
;
629 case IPSECDOI_PROTO_IPCOMP
:
630 return SADB_X_SATYPE_IPCOMP
;
633 plog(LLV_ERROR
, LOCATION
, NULL
,
634 "Invalid ipsec_doi proto: %u\n", proto
);
641 ipsecdoi2pfkey_alg(algclass
, type
)
642 u_int algclass
, type
;
645 case IPSECDOI_ATTR_AUTH
:
646 return ipsecdoi2pfkey_aalg(type
);
647 case IPSECDOI_PROTO_IPSEC_ESP
:
648 return ipsecdoi2pfkey_ealg(type
);
649 case IPSECDOI_PROTO_IPCOMP
:
650 return ipsecdoi2pfkey_calg(type
);
652 plog(LLV_ERROR
, LOCATION
, NULL
,
653 "Invalid ipsec_doi algclass: %u\n", algclass
);
659 /* SADB_SATYPE -> IPSECDOI_PROTO */
661 pfkey2ipsecdoi_proto(satype
)
666 return IPSECDOI_PROTO_IPSEC_AH
;
667 case SADB_SATYPE_ESP
:
668 return IPSECDOI_PROTO_IPSEC_ESP
;
669 case SADB_X_SATYPE_IPCOMP
:
670 return IPSECDOI_PROTO_IPCOMP
;
673 plog(LLV_ERROR
, LOCATION
, NULL
,
674 "Invalid pfkey proto: %u\n", satype
);
680 /* IPSECDOI_ATTR_ENC_MODE -> IPSEC_MODE */
682 ipsecdoi2pfkey_mode(mode
)
686 case IPSECDOI_ATTR_ENC_MODE_TUNNEL
:
688 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
:
689 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
:
691 return IPSEC_MODE_TUNNEL
;
692 case IPSECDOI_ATTR_ENC_MODE_TRNS
:
694 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
:
695 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
:
697 return IPSEC_MODE_TRANSPORT
;
699 plog(LLV_ERROR
, LOCATION
, NULL
, "Invalid mode type: %u\n", mode
);
705 /* IPSECDOI_ATTR_ENC_MODE -> IPSEC_MODE */
707 pfkey2ipsecdoi_mode(mode
)
711 case IPSEC_MODE_TUNNEL
:
712 return IPSECDOI_ATTR_ENC_MODE_TUNNEL
;
713 case IPSEC_MODE_TRANSPORT
:
714 return IPSECDOI_ATTR_ENC_MODE_TRNS
;
716 return IPSECDOI_ATTR_ENC_MODE_ANY
;
718 plog(LLV_ERROR
, LOCATION
, NULL
, "Invalid mode type: %u\n", mode
);
724 /* default key length for encryption algorithm */
726 keylen_aalg(hashtype
)
732 return SADB_AALG_NONE
;
734 res
= alg_ipsec_hmacdef_hashlen(hashtype
);
736 plog(LLV_ERROR
, LOCATION
, NULL
,
737 "invalid hmac algorithm %u.\n", hashtype
);
743 /* default key length for encryption algorithm */
745 keylen_ealg(enctype
, encklen
)
751 res
= alg_ipsec_encdef_keylen(enctype
, encklen
);
753 plog(LLV_ERROR
, LOCATION
, NULL
,
754 "invalid encryption algorithm %u.\n", enctype
);
761 pfkey_convertfromipsecdoi(proto_id
, t_id
, hashtype
,
762 e_type
, e_keylen
, a_type
, a_keylen
, flags
)
774 case IPSECDOI_PROTO_IPSEC_ESP
:
775 if ((*e_type
= ipsecdoi2pfkey_ealg(t_id
)) == ~0)
777 if ((*e_keylen
= keylen_ealg(t_id
, *e_keylen
)) == ~0)
781 if ((*a_type
= ipsecdoi2pfkey_aalg(hashtype
)) == ~0)
783 if ((*a_keylen
= keylen_aalg(hashtype
)) == ~0)
787 if (*e_type
== SADB_EALG_NONE
) {
788 plog(LLV_ERROR
, LOCATION
, NULL
, "no ESP algorithm.\n");
793 case IPSECDOI_PROTO_IPSEC_AH
:
794 if ((*a_type
= ipsecdoi2pfkey_aalg(hashtype
)) == ~0)
796 if ((*a_keylen
= keylen_aalg(hashtype
)) == ~0)
800 if (t_id
== IPSECDOI_ATTR_AUTH_HMAC_MD5
801 && hashtype
== IPSECDOI_ATTR_AUTH_KPDK
) {
802 /* AH_MD5 + Auth(KPDK) = RFC1826 keyed-MD5 */
803 *a_type
= SADB_X_AALG_MD5
;
804 *flags
|= SADB_X_EXT_OLD
;
806 *e_type
= SADB_EALG_NONE
;
808 if (*a_type
== SADB_AALG_NONE
) {
809 plog(LLV_ERROR
, LOCATION
, NULL
, "no AH algorithm.\n");
814 case IPSECDOI_PROTO_IPCOMP
:
815 if ((*e_type
= ipsecdoi2pfkey_calg(t_id
)) == ~0)
819 *flags
= SADB_X_EXT_RAWCPI
;
821 *a_type
= SADB_AALG_NONE
;
823 if (*e_type
== SADB_X_CALG_NONE
) {
824 plog(LLV_ERROR
, LOCATION
, NULL
, "no IPCOMP algorithm.\n");
830 plog(LLV_ERROR
, LOCATION
, NULL
, "unknown IPsec protocol.\n");
841 /* called from scheduler */
843 pfkey_timeover_stub(p
)
847 pfkey_timeover((struct ph2handle
*)p
);
852 struct ph2handle
*iph2
;
854 plog(LLV_ERROR
, LOCATION
, NULL
,
855 "%s give up to get IPsec-SA due to time up to wait.\n",
856 saddrwop2str(iph2
->dst
));
857 SCHED_KILL(iph2
->sce
);
859 /* If initiator side, send error to kernel by SADB_ACQUIRE. */
860 if (iph2
->side
== INITIATOR
)
861 pk_sendeacquire(iph2
);
871 /* send getspi message per ipsec protocol per remote address */
873 * the local address and remote address in ph1handle are dealed
874 * with destination address and source address respectively.
875 * Because SPI is decided by responder.
879 struct ph2handle
*iph2
;
881 struct sockaddr
*src
= NULL
, *dst
= NULL
;
885 u_int32_t minspi
, maxspi
;
888 if (iph2
->side
== INITIATOR
) {
890 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
893 if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
897 /* for mobile IPv6 */
898 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
899 ipsecdoi_transportmode(pp
)) {
907 for (pr
= pp
->head
; pr
!= NULL
; pr
= pr
->next
) {
910 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
912 plog(LLV_ERROR
, LOCATION
, NULL
,
913 "invalid proto_id %d\n", pr
->proto_id
);
916 /* this works around a bug in Linux kernel where it allocates 4 byte
918 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
926 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
928 plog(LLV_ERROR
, LOCATION
, NULL
,
929 "invalid encmode %d\n", pr
->encmode
);
933 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_getspi\n");
934 if (pfkey_send_getspi(
941 pr
->reqid_in
, iph2
->seq
) < 0) {
942 plog(LLV_ERROR
, LOCATION
, NULL
,
943 "ipseclib failed send getspi (%s)\n",
947 plog(LLV_DEBUG
, LOCATION
, NULL
,
948 "pfkey GETSPI sent: %s\n",
949 sadbsecas2str(dst
, src
, satype
, 0, mode
));
956 * receive GETSPI from kernel.
962 struct sadb_msg
*msg
;
964 struct ph2handle
*iph2
;
965 struct sockaddr
*dst
;
967 int allspiok
, notfound
;
972 if (mhp
[SADB_EXT_SA
] == NULL
973 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
974 plog(LLV_ERROR
, LOCATION
, NULL
,
975 "inappropriate sadb getspi message passed.\n");
978 msg
= (struct sadb_msg
*)mhp
[0];
979 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
980 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]); /* note SA dir */
982 /* the message has to be processed or not ? */
983 if (msg
->sadb_msg_pid
!= getpid()) {
984 plog(LLV_DEBUG
, LOCATION
, NULL
,
985 "%s message is not interesting "
986 "because pid %d is not mine.\n",
987 s_pfkey_type(msg
->sadb_msg_type
),
992 iph2
= getph2byseq(msg
->sadb_msg_seq
);
994 plog(LLV_DEBUG
, LOCATION
, NULL
,
995 "seq %d of %s message not interesting.\n",
997 s_pfkey_type(msg
->sadb_msg_type
));
1001 if (iph2
->is_dying
) {
1002 plog(LLV_ERROR
, LOCATION
, NULL
,
1003 "status mismatch phase2 dying (db:%d msg:%d)\n",
1004 iph2
->status
, PHASE2ST_GETSPISENT
);
1008 if (iph2
->status
!= PHASE2ST_GETSPISENT
) {
1009 plog(LLV_ERROR
, LOCATION
, NULL
,
1010 "status mismatch (db:%d msg:%d)\n",
1011 iph2
->status
, PHASE2ST_GETSPISENT
);
1015 // check the underlying iph2->ph1
1017 if (!ike_session_update_ph2_ph1bind(iph2
)) {
1018 plog(LLV_ERROR
, LOCATION
, NULL
,
1019 "can't proceed with getspi for %s. no suitable ISAKMP-SA found \n",
1020 saddrwop2str(iph2
->dst
));
1028 /* set SPI, and check to get all spi whether or not */
1031 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1032 pp
= iph2
->side
== INITIATOR
? iph2
->proposal
: iph2
->approval
;
1034 for (pr
= pp
->head
; pr
!= NULL
; pr
= pr
->next
) {
1035 if (pr
->proto_id
== proto_id
&& pr
->spi
== 0) {
1036 pr
->spi
= sa
->sadb_sa_spi
;
1038 plog(LLV_DEBUG
, LOCATION
, NULL
,
1039 "pfkey GETSPI succeeded: %s\n",
1040 sadbsecas2str(iph2
->dst
, iph2
->src
,
1041 msg
->sadb_msg_satype
,
1043 ipsecdoi2pfkey_mode(pr
->encmode
)));
1046 allspiok
= 0; /* not get all spi */
1050 plog(LLV_ERROR
, LOCATION
, NULL
,
1051 "get spi for unknown address %s\n",
1052 saddrwop2str(iph2
->dst
));
1061 iph2
->status
= PHASE2ST_GETSPIDONE
;
1062 if (isakmp_post_getspi(iph2
) < 0) {
1063 plog(LLV_ERROR
, LOCATION
, NULL
,
1064 "failed to start post getspi.\n");
1081 struct ph2handle
*iph2
;
1084 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1085 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1087 u_int64_t lifebyte
= 0;
1088 u_int wsize
= 4; /* XXX static size of window */
1090 struct ph2natt natt
;
1093 if (iph2
->approval
== NULL
) {
1094 plog(LLV_ERROR
, LOCATION
, NULL
,
1095 "no approvaled SAs found.\n");
1098 if (iph2
->side
== INITIATOR
)
1099 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1100 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1103 /* for mobile IPv6 */
1104 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1105 ipsecdoi_transportmode(iph2
->approval
)) {
1113 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1114 /* validity check */
1115 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1117 plog(LLV_ERROR
, LOCATION
, NULL
,
1118 "invalid proto_id %d\n", pr
->proto_id
);
1121 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1122 /* IPCOMP has no replay window */
1125 #ifdef ENABLE_SAMODE_UNSPECIFIED
1126 mode
= IPSEC_MODE_ANY
;
1128 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1130 plog(LLV_ERROR
, LOCATION
, NULL
,
1131 "invalid encmode %d\n", pr
->encmode
);
1136 /* set algorithm type and key length */
1137 e_keylen
= pr
->head
->encklen
;
1138 if (pfkey_convertfromipsecdoi(
1143 &a_type
, &a_keylen
, &flags
) < 0)
1147 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1154 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1155 if (pr
->udp_encap
) {
1156 memset (&natt
, 0, sizeof (natt
));
1157 natt
.sport
= extract_port (iph2
->ph1
->remote
);
1158 flags
|= SADB_X_EXT_NATT
;
1159 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1160 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1161 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1163 else if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1164 mode
== IPSEC_MODE_TRANSPORT
&&
1165 src
->sa_family
== AF_INET
)
1166 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1168 memset (&natt
, 0, sizeof (natt
));
1171 if (pfkey_send_update(
1181 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1182 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1183 iph2
->seq
, natt
.sport
) < 0) {
1184 plog(LLV_ERROR
, LOCATION
, NULL
,
1185 "libipsec failed send update (%s)\n",
1190 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1191 if (pfkey_send_update(
1201 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1202 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1203 iph2
->seq
, 0) < 0) {
1204 plog(LLV_ERROR
, LOCATION
, NULL
,
1205 "libipsec failed send update (%s)\n",
1209 #endif /* ENABLE_NATT */
1212 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update_nat\n");
1213 if (pr
->udp_encap
) {
1214 memset (&natt
, 0, sizeof (natt
));
1215 natt
.type
= iph2
->ph1
->natt_options
->encaps_type
;
1216 natt
.sport
= extract_port (iph2
->ph1
->remote
);
1217 natt
.dport
= extract_port (iph2
->ph1
->local
);
1218 natt
.oa
= NULL
; // FIXME: Here comes OA!!!
1219 natt
.frag
= iph2
->ph1
->rmconf
->esp_frag
;
1221 memset (&natt
, 0, sizeof (natt
));
1224 if (pfkey_send_update_nat(
1234 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1235 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1237 natt
.type
, natt
.sport
, natt
.dport
, natt
.oa
,
1239 plog(LLV_ERROR
, LOCATION
, NULL
,
1240 "libipsec failed send update_nat (%s)\n",
1245 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1246 if (pfkey_send_update(
1256 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1257 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1259 plog(LLV_ERROR
, LOCATION
, NULL
,
1260 "libipsec failed send update (%s)\n",
1264 #endif /* ENABLE_NATT */
1265 #endif /* __APPLE__ */
1267 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
1271 * It maybe good idea to call backupsa_to_file() after
1272 * racoon will receive the sadb_update messages.
1273 * But it is impossible because there is not key in the
1274 * information from the kernel.
1276 if (backupsa_to_file(satype
, mode
, dst
, src
,
1277 pr
->spi
, pr
->reqid_in
, 4,
1279 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1280 0, iph2
->approval
->lifebyte
* 1024,
1281 iph2
->approval
->lifetime
, 0,
1283 plog(LLV_ERROR
, LOCATION
, NULL
,
1284 "backuped SA failed: %s\n",
1285 sadbsecas2str(dst
, src
,
1286 satype
, pr
->spi
, mode
));
1288 plog(LLV_DEBUG
, LOCATION
, NULL
,
1289 "backuped SA: %s\n",
1290 sadbsecas2str(dst
, src
,
1291 satype
, pr
->spi
, mode
));
1301 struct sadb_msg
*msg
;
1303 struct sockaddr
*src
, *dst
;
1304 struct ph2handle
*iph2
;
1305 u_int proto_id
, encmode
, sa_mode
;
1309 /* ignore this message because of local test mode. */
1315 || mhp
[SADB_EXT_SA
] == NULL
1316 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1317 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1318 plog(LLV_ERROR
, LOCATION
, NULL
,
1319 "inappropriate sadb update message passed.\n");
1322 msg
= (struct sadb_msg
*)mhp
[0];
1323 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1324 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1325 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1327 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1329 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1331 /* the message has to be processed or not ? */
1332 if (msg
->sadb_msg_pid
!= getpid()) {
1333 plog(LLV_DEBUG
, LOCATION
, NULL
,
1334 "%s message is not interesting "
1335 "because pid %d is not mine.\n",
1336 s_pfkey_type(msg
->sadb_msg_type
),
1341 iph2
= getph2byseq(msg
->sadb_msg_seq
);
1343 plog(LLV_DEBUG
, LOCATION
, NULL
,
1344 "seq %d of %s message not interesting.\n",
1346 s_pfkey_type(msg
->sadb_msg_type
));
1350 if (iph2
->is_dying
) {
1351 plog(LLV_ERROR
, LOCATION
, NULL
,
1352 "status mismatch phase2 dying (db:%d msg:%d)\n",
1353 iph2
->status
, PHASE2ST_ADDSA
);
1357 if (iph2
->status
!= PHASE2ST_ADDSA
) {
1358 plog(LLV_ERROR
, LOCATION
, NULL
,
1359 "status mismatch (db:%d msg:%d)\n",
1360 iph2
->status
, PHASE2ST_ADDSA
);
1364 /* check to complete all keys ? */
1365 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1366 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1367 if (proto_id
== ~0) {
1368 plog(LLV_ERROR
, LOCATION
, NULL
,
1369 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1372 encmode
= pfkey2ipsecdoi_mode(sa_mode
);
1373 if (encmode
== ~0) {
1374 plog(LLV_ERROR
, LOCATION
, NULL
,
1375 "invalid encmode %d\n", sa_mode
);
1379 if (pr
->proto_id
== proto_id
1380 && pr
->spi
== sa
->sadb_sa_spi
) {
1382 plog(LLV_DEBUG
, LOCATION
, NULL
,
1383 "pfkey UPDATE succeeded: %s\n",
1384 sadbsecas2str(iph2
->dst
, iph2
->src
,
1385 msg
->sadb_msg_satype
,
1389 plog(LLV_INFO
, LOCATION
, NULL
,
1390 "IPsec-SA established: %s\n",
1391 sadbsecas2str(iph2
->dst
, iph2
->src
,
1392 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1403 /* turn off the timer for calling pfkey_timeover() */
1404 SCHED_KILL(iph2
->sce
);
1407 iph2
->status
= PHASE2ST_ESTABLISHED
;
1409 if (iph2
->side
== INITIATOR
) {
1410 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1411 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_SUCC
,
1412 CONSTSTR("Initiator, Quick-Mode"),
1415 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1416 IPSECSESSIONEVENTCODE_IKEV1_PH2_RESP_SUCC
,
1417 CONSTSTR("Responder, Quick-Mode"),
1421 ike_session_ph2_established(iph2
);
1424 gettimeofday(&iph2
->end
, NULL
);
1425 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1426 "phase2", "quick", timedelta(&iph2
->start
, &iph2
->end
));
1431 iph2
->ph1
->ph2cnt
++;
1433 /* turn off schedule */
1435 SCHED_KILL(iph2
->scr
);
1438 * since we are going to reuse the phase2 handler, we need to
1439 * remain it and refresh all the references between ph1 and ph2 to use.
1443 iph2
->sce
= sched_new(iph2
->approval
->lifetime
,
1444 isakmp_ph2expire_stub
, iph2
);
1446 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1455 struct ph2handle
*iph2
;
1458 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1459 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1461 u_int64_t lifebyte
= 0;
1462 u_int wsize
= 4; /* XXX static size of window */
1464 struct ph2natt natt
;
1467 if (iph2
->approval
== NULL
) {
1468 plog(LLV_ERROR
, LOCATION
, NULL
,
1469 "no approvaled SAs found.\n");
1472 if (iph2
->side
== INITIATOR
)
1473 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1474 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1477 /* for mobile IPv6 */
1478 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1479 ipsecdoi_transportmode(iph2
->approval
)) {
1487 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1488 /* validity check */
1489 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1491 plog(LLV_ERROR
, LOCATION
, NULL
,
1492 "invalid proto_id %d\n", pr
->proto_id
);
1495 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1496 /* no replay window for IPCOMP */
1499 #ifdef ENABLE_SAMODE_UNSPECIFIED
1500 mode
= IPSEC_MODE_ANY
;
1502 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1504 plog(LLV_ERROR
, LOCATION
, NULL
,
1505 "invalid encmode %d\n", pr
->encmode
);
1510 /* set algorithm type and key length */
1511 e_keylen
= pr
->head
->encklen
;
1512 if (pfkey_convertfromipsecdoi(
1517 &a_type
, &a_keylen
, &flags
) < 0)
1521 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1528 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1530 if (pr
->udp_encap
) {
1531 memset (&natt
, 0, sizeof (natt
));
1532 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1533 flags
|= SADB_X_EXT_NATT
;
1534 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1535 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1536 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1538 else if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1539 mode
== IPSEC_MODE_TRANSPORT
&&
1540 dst
->sa_family
== AF_INET
)
1541 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1543 memset (&natt
, 0, sizeof (natt
));
1545 /* Remove port information, that SA doesn't use it */
1560 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1561 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1562 iph2
->seq
,natt
.dport
) < 0) {
1563 plog(LLV_ERROR
, LOCATION
, NULL
,
1564 "libipsec failed send add (%s)\n",
1569 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1571 /* Remove port information, it is not used without NAT-T */
1585 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1586 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1587 iph2
->seq
, 0) < 0) {
1588 plog(LLV_ERROR
, LOCATION
, NULL
,
1589 "libipsec failed send add (%s)\n",
1593 #endif /* ENABLE_NATT */
1594 #else /* __APPLE__ */
1596 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add_nat\n");
1598 if (pr
->udp_encap
) {
1599 memset (&natt
, 0, sizeof (natt
));
1600 natt
.type
= UDP_ENCAP_ESPINUDP
;
1601 natt
.sport
= extract_port (iph2
->ph1
->local
);
1602 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1603 natt
.oa
= NULL
; // FIXME: Here comes OA!!!
1604 natt
.frag
= iph2
->ph1
->rmconf
->esp_frag
;
1606 memset (&natt
, 0, sizeof (natt
));
1608 /* Remove port information, that SA doesn't use it */
1613 if (pfkey_send_add_nat(
1623 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1624 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1626 natt
.type
, natt
.sport
, natt
.dport
, natt
.oa
,
1628 plog(LLV_ERROR
, LOCATION
, NULL
,
1629 "libipsec failed send add_nat (%s)\n",
1634 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1636 /* Remove port information, it is not used without NAT-T */
1650 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1651 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1653 plog(LLV_ERROR
, LOCATION
, NULL
,
1654 "libipsec failed send add (%s)\n",
1658 #endif /* ENABLE_NATT */
1659 #endif /* __APPLE__ */
1661 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
1665 * It maybe good idea to call backupsa_to_file() after
1666 * racoon will receive the sadb_update messages.
1667 * But it is impossible because there is not key in the
1668 * information from the kernel.
1670 if (backupsa_to_file(satype
, mode
, src
, dst
,
1671 pr
->spi_p
, pr
->reqid_out
, 4,
1673 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1674 0, iph2
->approval
->lifebyte
* 1024,
1675 iph2
->approval
->lifetime
, 0,
1677 plog(LLV_ERROR
, LOCATION
, NULL
,
1678 "backuped SA failed: %s\n",
1679 sadbsecas2str(src
, dst
,
1680 satype
, pr
->spi_p
, mode
));
1682 plog(LLV_DEBUG
, LOCATION
, NULL
,
1683 "backuped SA: %s\n",
1684 sadbsecas2str(src
, dst
,
1685 satype
, pr
->spi_p
, mode
));
1695 struct sadb_msg
*msg
;
1697 struct sockaddr
*src
, *dst
;
1698 struct ph2handle
*iph2
;
1701 /* ignore this message because of local test mode. */
1707 || mhp
[SADB_EXT_SA
] == NULL
1708 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1709 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1710 plog(LLV_ERROR
, LOCATION
, NULL
,
1711 "inappropriate sadb add message passed.\n");
1714 msg
= (struct sadb_msg
*)mhp
[0];
1715 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1716 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1717 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1719 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1721 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1723 /* the message has to be processed or not ? */
1724 if (msg
->sadb_msg_pid
!= getpid()) {
1725 plog(LLV_DEBUG
, LOCATION
, NULL
,
1726 "%s message is not interesting "
1727 "because pid %d is not mine.\n",
1728 s_pfkey_type(msg
->sadb_msg_type
),
1733 iph2
= getph2byseq(msg
->sadb_msg_seq
);
1735 plog(LLV_DEBUG
, LOCATION
, NULL
,
1736 "seq %d of %s message not interesting.\n",
1738 s_pfkey_type(msg
->sadb_msg_type
));
1743 * NOTE don't update any status of phase2 handle
1744 * because they must be updated by SADB_UPDATE message
1747 plog(LLV_INFO
, LOCATION
, NULL
,
1748 "IPsec-SA established: %s\n",
1749 sadbsecas2str(iph2
->src
, iph2
->dst
,
1750 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1752 ike_session_cleanup_other_established_ph2s(iph2
->parent_session
, iph2
);
1754 #ifdef ENABLE_VPNCONTROL_PORT
1758 if (iph2
->dst
->sa_family
== AF_INET
)
1759 address
= ((struct sockaddr_in
*)iph2
->dst
)->sin_addr
.s_addr
;
1762 vpncontrol_notify_phase_change(0, FROM_LOCAL
, NULL
, iph2
);
1766 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1774 struct sadb_msg
*msg
;
1776 struct sockaddr
*src
, *dst
;
1777 struct ph2handle
*iph2
;
1778 u_int proto_id
, sa_mode
;
1782 || mhp
[SADB_EXT_SA
] == NULL
1783 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1784 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1785 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
1786 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
1787 plog(LLV_ERROR
, LOCATION
, NULL
,
1788 "inappropriate sadb expire message passed.\n");
1791 msg
= (struct sadb_msg
*)mhp
[0];
1792 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1793 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1794 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1796 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1798 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1800 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1801 if (proto_id
== ~0) {
1802 plog(LLV_ERROR
, LOCATION
, NULL
,
1803 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1807 plog(LLV_INFO
, LOCATION
, NULL
,
1808 "IPsec-SA expired: %s\n",
1809 sadbsecas2str(src
, dst
,
1810 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1812 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1815 * Ignore it because two expire messages are come up.
1816 * phase2 handler has been deleted already when 2nd message
1819 plog(LLV_DEBUG
, LOCATION
, NULL
,
1820 "no such a SA found: %s\n",
1821 sadbsecas2str(src
, dst
,
1822 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1826 if (iph2
->is_dying
|| iph2
->status
!= PHASE2ST_ESTABLISHED
) {
1828 * If the status is not equal to PHASE2ST_ESTABLISHED,
1829 * racoon ignores this expire message. There are two reason.
1830 * One is that the phase 2 probably starts because there is
1831 * a potential that racoon receives the acquire message
1832 * without receiving a expire message. Another is that racoon
1833 * may receive the multiple expire messages from the kernel.
1835 plog(LLV_WARNING
, LOCATION
, NULL
,
1836 "the expire message is received "
1837 "but the handler is dying or has not been established.\n");
1841 /* turn off the timer for calling isakmp_ph2expire() */
1842 SCHED_KILL(iph2
->sce
);
1844 iph2
->status
= PHASE2ST_EXPIRED
;
1846 /* INITIATOR, begin phase 2 exchange only if there's no other established ph2. */
1847 /* allocate buffer for status management of pfkey message */
1848 if (iph2
->side
== INITIATOR
&&
1849 !ike_session_has_other_established_ph2(iph2
->parent_session
, iph2
) &&
1850 !ike_session_drop_rekey(iph2
->parent_session
)) {
1854 /* update status for re-use */
1855 iph2
->status
= PHASE2ST_STATUS2
;
1857 /* start isakmp initiation by using ident exchange */
1858 if (isakmp_post_acquire(iph2
) < 0) {
1859 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
1860 "failed to begin ipsec sa "
1861 "re-negotiation.\n");
1872 /* If not received SADB_EXPIRE, INITIATOR delete ph2handle. */
1873 /* RESPONDER always delete ph2handle, keep silent. RESPONDER doesn't
1874 * manage IPsec SA, so delete the list */
1886 struct sadb_msg
*msg
;
1887 struct sadb_x_policy
*xpl
;
1888 struct secpolicy
*sp_out
= NULL
, *sp_in
= NULL
;
1889 #define MAXNESTEDSA 5 /* XXX */
1890 struct ph2handle
*iph2
[MAXNESTEDSA
];
1891 struct sockaddr
*src
, *dst
;
1892 int n
; /* # of phase 2 handler */
1894 /* ignore this message because of local test mode. */
1900 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1901 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1902 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1903 plog(LLV_ERROR
, LOCATION
, NULL
,
1904 "inappropriate sadb acquire message passed.\n");
1907 msg
= (struct sadb_msg
*)mhp
[0];
1908 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
1909 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1910 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1912 /* ignore if type is not IPSEC_POLICY_IPSEC */
1913 if (xpl
->sadb_x_policy_type
!= IPSEC_POLICY_IPSEC
) {
1914 plog(LLV_DEBUG
, LOCATION
, NULL
,
1915 "ignore ACQUIRE message. type is not IPsec.\n");
1919 /* ignore it if src is multicast address */
1921 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1923 if ((sa
->sa_family
== AF_INET
1924 && IN_MULTICAST(ntohl(((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
)))
1926 || (sa
->sa_family
== AF_INET6
1927 && IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)sa
)->sin6_addr
))
1930 plog(LLV_DEBUG
, LOCATION
, NULL
,
1931 "ignore due to multicast address: %s.\n",
1937 /* ignore, if we do not listen on source address */
1940 * - if we'll contact peer from address we do not listen -
1941 * we will be unable to complete negotiation;
1942 * - if we'll negotiate using address we're listening -
1943 * remote peer will send packets to address different
1944 * than one in the policy, so kernel will drop them;
1945 * => therefore this acquire is not for us! --Aidas
1947 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1950 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1951 if (!cmpsaddrwop(p
->addr
, sa
)) {
1958 plog(LLV_DEBUG
, LOCATION
, NULL
,
1959 "ignore because do not listen on source address : %s.\n",
1966 * If there is a phase 2 handler against the policy identifier in
1967 * the acquire message, and if
1968 * 1. its state is less than PHASE2ST_ESTABLISHED, then racoon
1969 * should ignore such a acquire message because the phase 2
1970 * is just negotiating.
1971 * 2. its state is equal to PHASE2ST_ESTABLISHED, then racoon
1972 * has to process such a acquire message because racoon may
1973 * have lost the expire message.
1975 iph2
[0] = getph2byid(src
, dst
, xpl
->sadb_x_policy_id
);
1976 if (iph2
[0] != NULL
) {
1977 if (iph2
[0]->status
< PHASE2ST_ESTABLISHED
) {
1978 plog(LLV_DEBUG
, LOCATION
, NULL
,
1979 "ignore the acquire because ph2 found\n");
1982 if (iph2
[0]->status
== PHASE2ST_EXPIRED
)
1987 /* search for proper policyindex */
1988 sp_out
= getspbyspid(xpl
->sadb_x_policy_id
);
1989 if (sp_out
== NULL
) {
1990 plog(LLV_ERROR
, LOCATION
, NULL
, "no policy found: id:%d.\n",
1991 xpl
->sadb_x_policy_id
);
1994 plog(LLV_DEBUG
, LOCATION
, NULL
,
1995 "suitable outbound SP found: %s.\n", spidx2str(&sp_out
->spidx
));
1997 /* get inbound policy */
1999 struct policyindex spidx
;
2001 spidx
.dir
= IPSEC_DIR_INBOUND
;
2002 memcpy(&spidx
.src
, &sp_out
->spidx
.dst
, sizeof(spidx
.src
));
2003 memcpy(&spidx
.dst
, &sp_out
->spidx
.src
, sizeof(spidx
.dst
));
2004 spidx
.prefs
= sp_out
->spidx
.prefd
;
2005 spidx
.prefd
= sp_out
->spidx
.prefs
;
2006 spidx
.ul_proto
= sp_out
->spidx
.ul_proto
;
2008 sp_in
= getsp(&spidx
);
2010 plog(LLV_DEBUG
, LOCATION
, NULL
,
2011 "suitable inbound SP found: %s.\n",
2012 spidx2str(&sp_in
->spidx
));
2014 plog(LLV_NOTIFY
, LOCATION
, NULL
,
2015 "no in-bound policy found: %s\n",
2020 memset(iph2
, 0, MAXNESTEDSA
);
2024 /* allocate a phase 2 */
2026 if (iph2
[n
] == NULL
) {
2027 plog(LLV_ERROR
, LOCATION
, NULL
,
2028 "failed to allocate phase2 entry.\n");
2031 iph2
[n
]->side
= INITIATOR
;
2032 iph2
[n
]->spid
= xpl
->sadb_x_policy_id
;
2033 iph2
[n
]->satype
= msg
->sadb_msg_satype
;
2034 iph2
[n
]->seq
= msg
->sadb_msg_seq
;
2035 iph2
[n
]->status
= PHASE2ST_STATUS2
;
2037 /* set end addresses of SA */
2038 iph2
[n
]->dst
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]));
2039 if (iph2
[n
]->dst
== NULL
) {
2043 iph2
[n
]->src
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]));
2044 if (iph2
[n
]->src
== NULL
) {
2049 plog(LLV_DEBUG
, LOCATION
, NULL
,
2050 "new acquire %s\n", spidx2str(&sp_out
->spidx
));
2054 vchar_t
*idsrc
, *iddst
;
2056 idsrc
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.src
,
2057 sp_out
->spidx
.prefs
, sp_out
->spidx
.ul_proto
);
2058 if (idsrc
== NULL
) {
2059 plog(LLV_ERROR
, LOCATION
, NULL
,
2060 "failed to get ID for %s\n",
2061 spidx2str(&sp_out
->spidx
));
2065 iddst
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.dst
,
2066 sp_out
->spidx
.prefd
, sp_out
->spidx
.ul_proto
);
2067 if (iddst
== NULL
) {
2068 plog(LLV_ERROR
, LOCATION
, NULL
,
2069 "failed to get ID for %s\n",
2070 spidx2str(&sp_out
->spidx
));
2075 iph2
[n
]->sainfo
= getsainfo(idsrc
, iddst
, NULL
, 0);
2078 if (iph2
[n
]->sainfo
== NULL
) {
2079 plog(LLV_ERROR
, LOCATION
, NULL
,
2080 "failed to get sainfo.\n");
2083 /* XXX should use the algorithm list from register message */
2086 if (link_sainfo_to_ph2(iph2
[n
]->sainfo
) != 0) {
2087 plog(LLV_ERROR
, LOCATION
, NULL
,
2088 "failed to link sainfo\n");
2089 iph2
[n
]->sainfo
= NULL
;
2096 if (set_proposal_from_policy(iph2
[n
], sp_out
, sp_in
) < 0) {
2097 plog(LLV_ERROR
, LOCATION
, NULL
,
2098 "failed to create saprop.\n");
2104 /* start isakmp initiation by using ident exchange */
2105 /* XXX should be looped if there are multiple phase 2 handler. */
2106 if (isakmp_post_acquire(iph2
[n
]) < 0) {
2107 plog(LLV_ERROR
, LOCATION
, NULL
,
2108 "failed to begin ipsec sa negotiation.\n");
2112 #if !TARGET_OS_EMBEDDED
2113 if ( lcconf
->vt
== NULL
){
2114 if (!(lcconf
->vt
= vproc_transaction_begin(NULL
)))
2115 plog(LLV_ERROR
, LOCATION
, NULL
,
2116 "vproc_transaction_begin returns NULL.\n");
2125 unbindph12(iph2
[n
]);
2138 struct sadb_msg
*msg
;
2140 struct sockaddr
*src
, *dst
;
2141 struct ph2handle
*iph2
= NULL
;
2144 /* ignore this message because of local test mode. */
2150 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2151 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
2152 plog(LLV_ERROR
, LOCATION
, NULL
,
2153 "inappropriate sadb delete message passed.\n");
2156 msg
= (struct sadb_msg
*)mhp
[0];
2157 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
2158 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
2159 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
2161 /* the message has to be processed or not ? */
2162 if (msg
->sadb_msg_pid
== getpid()) {
2163 plog(LLV_DEBUG
, LOCATION
, NULL
,
2164 "%s message is not interesting "
2165 "because the message was originated by me.\n",
2166 s_pfkey_type(msg
->sadb_msg_type
));
2170 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
2171 if (proto_id
== ~0) {
2172 plog(LLV_ERROR
, LOCATION
, NULL
,
2173 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
2177 plog(LLV_DEBUG2
, LOCATION
, NULL
, "SADB delete message: proto-id %d\n", proto_id
);
2178 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str(src
));
2179 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str(dst
));
2182 deleteallph2(src
, dst
, proto_id
);
2183 deleteallph1(src
, dst
);
2187 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
2190 plog(LLV_ERROR
, LOCATION
, NULL
,
2191 "no iph2 found: %s\n",
2192 sadbsecas2str(src
, dst
, msg
->sadb_msg_satype
,
2193 sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2197 plog(LLV_ERROR
, LOCATION
, NULL
,
2198 "pfkey DELETE received: %s\n",
2199 sadbsecas2str(iph2
->src
, iph2
->dst
,
2200 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2202 /* send delete information */
2203 if (iph2
->status
== PHASE2ST_ESTABLISHED
)
2204 isakmp_info_send_d2(iph2
);
2206 ike_session_cleanup_ph1s_by_ph2(iph2
);
2218 /* ignore this message because of local test mode. */
2223 if (mhp
[0] == NULL
) {
2224 plog(LLV_ERROR
, LOCATION
, NULL
,
2225 "inappropriate sadb flush message passed.\n");
2236 getsadbpolicy(policy0
, policylen0
, type
, iph2
)
2238 int *policylen0
, type
;
2239 struct ph2handle
*iph2
;
2241 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2242 struct sadb_x_policy
*xpl
;
2243 struct sadb_x_ipsecrequest
*xisr
;
2250 /* get policy buffer size */
2251 policylen
= sizeof(struct sadb_x_policy
);
2252 if (type
!= SADB_X_SPDDELETE
) {
2253 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2254 xisrlen
= sizeof(*xisr
);
2255 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2256 xisrlen
+= (sysdep_sa_len(iph2
->src
)
2257 + sysdep_sa_len(iph2
->dst
));
2260 policylen
+= PFKEY_ALIGN8(xisrlen
);
2264 /* make policy structure */
2265 policy
= racoon_malloc(policylen
);
2267 plog(LLV_ERROR
, LOCATION
, NULL
,
2268 "buffer allocation failed.\n");
2272 xpl
= (struct sadb_x_policy
*)policy
;
2273 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(policylen
);
2274 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2275 xpl
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2276 xpl
->sadb_x_policy_dir
= spidx
->dir
;
2277 xpl
->sadb_x_policy_id
= 0;
2278 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2279 xpl
->sadb_x_policy_priority
= PRIORITY_DEFAULT
;
2282 /* no need to append policy information any more if type is SPDDELETE */
2283 if (type
== SADB_X_SPDDELETE
)
2286 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
2288 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2290 satype
= doi2ipproto(pr
->proto_id
);
2292 plog(LLV_ERROR
, LOCATION
, NULL
,
2293 "invalid proto_id %d\n", pr
->proto_id
);
2296 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
2298 plog(LLV_ERROR
, LOCATION
, NULL
,
2299 "invalid encmode %d\n", pr
->encmode
);
2304 * the policy level cannot be unique because the policy
2305 * is defined later than SA, so req_id cannot be bound to SA.
2307 xisr
->sadb_x_ipsecrequest_proto
= satype
;
2308 xisr
->sadb_x_ipsecrequest_mode
= mode
;
2309 xisr
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2310 xisr
->sadb_x_ipsecrequest_reqid
= 0;
2311 p
= (caddr_t
)(xisr
+ 1);
2313 xisrlen
= sizeof(*xisr
);
2315 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2316 int src_len
, dst_len
;
2318 src_len
= sysdep_sa_len(iph2
->src
);
2319 dst_len
= sysdep_sa_len(iph2
->dst
);
2320 xisrlen
+= src_len
+ dst_len
;
2322 memcpy(p
, iph2
->src
, src_len
);
2325 memcpy(p
, iph2
->dst
, dst_len
);
2329 xisr
->sadb_x_ipsecrequest_len
= PFKEY_ALIGN8(xisrlen
);
2334 *policylen0
= policylen
;
2340 racoon_free(policy
);
2346 pk_sendspdupdate2(iph2
)
2347 struct ph2handle
*iph2
;
2349 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2350 caddr_t policy
= NULL
;
2352 u_int64_t ltime
, vtime
;
2354 ltime
= iph2
->approval
->lifetime
;
2357 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDUPDATE
, iph2
)) {
2358 plog(LLV_ERROR
, LOCATION
, NULL
,
2359 "getting sadb policy failed.\n");
2363 if (pfkey_send_spdupdate2(
2365 (struct sockaddr
*)&spidx
->src
,
2367 (struct sockaddr
*)&spidx
->dst
,
2371 policy
, policylen
, 0) < 0) {
2372 plog(LLV_ERROR
, LOCATION
, NULL
,
2373 "libipsec failed send spdupdate2 (%s)\n",
2377 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdupdate2\n");
2381 racoon_free(policy
);
2387 pk_recvspdupdate(mhp
)
2390 struct sadb_address
*saddr
, *daddr
;
2391 struct sadb_x_policy
*xpl
;
2392 struct policyindex spidx
;
2393 struct secpolicy
*sp
;
2397 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2398 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2399 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2400 plog(LLV_ERROR
, LOCATION
, NULL
,
2401 "inappropriate sadb spdupdate message passed.\n");
2404 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2405 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2406 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2408 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2409 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2412 saddr
->sadb_address_prefixlen
,
2413 daddr
->sadb_address_prefixlen
,
2414 saddr
->sadb_address_proto
,
2415 xpl
->sadb_x_policy_priority
,
2418 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2421 saddr
->sadb_address_prefixlen
,
2422 daddr
->sadb_address_prefixlen
,
2423 saddr
->sadb_address_proto
,
2429 plog(LLV_ERROR
, LOCATION
, NULL
,
2430 "such policy does not already exist: \"%s\"\n",
2437 if (addnewsp(mhp
) < 0)
2444 * this function has to be used by responder side.
2447 pk_sendspdadd2(iph2
)
2448 struct ph2handle
*iph2
;
2450 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2451 caddr_t policy
= NULL
;
2453 u_int64_t ltime
, vtime
;
2455 ltime
= iph2
->approval
->lifetime
;
2458 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDADD
, iph2
)) {
2459 plog(LLV_ERROR
, LOCATION
, NULL
,
2460 "getting sadb policy failed.\n");
2464 if (pfkey_send_spdadd2(
2466 (struct sockaddr
*)&spidx
->src
,
2468 (struct sockaddr
*)&spidx
->dst
,
2472 policy
, policylen
, 0) < 0) {
2473 plog(LLV_ERROR
, LOCATION
, NULL
,
2474 "libipsec failed send spdadd2 (%s)\n",
2478 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdadd2\n");
2482 racoon_free(policy
);
2491 struct sadb_address
*saddr
, *daddr
;
2492 struct sadb_x_policy
*xpl
;
2493 struct policyindex spidx
;
2494 struct secpolicy
*sp
;
2498 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2499 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2500 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2501 plog(LLV_ERROR
, LOCATION
, NULL
,
2502 "inappropriate sadb spdadd message passed.\n");
2505 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2506 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2507 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2509 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2510 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2513 saddr
->sadb_address_prefixlen
,
2514 daddr
->sadb_address_prefixlen
,
2515 saddr
->sadb_address_proto
,
2516 xpl
->sadb_x_policy_priority
,
2519 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2522 saddr
->sadb_address_prefixlen
,
2523 daddr
->sadb_address_prefixlen
,
2524 saddr
->sadb_address_proto
,
2530 plog(LLV_ERROR
, LOCATION
, NULL
,
2531 "such policy already exists. "
2532 "anyway replace it: %s\n",
2538 if (addnewsp(mhp
) < 0)
2545 * this function has to be used by responder side.
2548 pk_sendspddelete(iph2
)
2549 struct ph2handle
*iph2
;
2551 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2552 caddr_t policy
= NULL
;
2555 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDDELETE
, iph2
)) {
2556 plog(LLV_ERROR
, LOCATION
, NULL
,
2557 "getting sadb policy failed.\n");
2561 if (pfkey_send_spddelete(
2563 (struct sockaddr
*)&spidx
->src
,
2565 (struct sockaddr
*)&spidx
->dst
,
2568 policy
, policylen
, 0) < 0) {
2569 plog(LLV_ERROR
, LOCATION
, NULL
,
2570 "libipsec failed send spddelete (%s)\n",
2574 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spddelete\n");
2578 racoon_free(policy
);
2584 pk_recvspddelete(mhp
)
2587 struct sadb_address
*saddr
, *daddr
;
2588 struct sadb_x_policy
*xpl
;
2589 struct policyindex spidx
;
2590 struct secpolicy
*sp
;
2594 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2595 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2596 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2597 plog(LLV_ERROR
, LOCATION
, NULL
,
2598 "inappropriate sadb spddelete message passed.\n");
2601 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2602 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2603 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2605 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2606 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2609 saddr
->sadb_address_prefixlen
,
2610 daddr
->sadb_address_prefixlen
,
2611 saddr
->sadb_address_proto
,
2612 xpl
->sadb_x_policy_priority
,
2615 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2618 saddr
->sadb_address_prefixlen
,
2619 daddr
->sadb_address_prefixlen
,
2620 saddr
->sadb_address_proto
,
2626 plog(LLV_ERROR
, LOCATION
, NULL
,
2627 "no policy found: %s\n",
2632 purgephXbyspid(xpl
->sadb_x_policy_id
, true);
2641 pk_recvspdexpire(mhp
)
2644 struct sadb_address
*saddr
, *daddr
;
2645 struct sadb_x_policy
*xpl
;
2646 struct policyindex spidx
;
2647 struct secpolicy
*sp
;
2651 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2652 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2653 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2654 plog(LLV_ERROR
, LOCATION
, NULL
,
2655 "inappropriate sadb spdexpire message passed.\n");
2658 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2659 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2660 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2662 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2663 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2666 saddr
->sadb_address_prefixlen
,
2667 daddr
->sadb_address_prefixlen
,
2668 saddr
->sadb_address_proto
,
2669 xpl
->sadb_x_policy_priority
,
2672 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2675 saddr
->sadb_address_prefixlen
,
2676 daddr
->sadb_address_prefixlen
,
2677 saddr
->sadb_address_proto
,
2683 plog(LLV_ERROR
, LOCATION
, NULL
,
2684 "no policy found: %s\n",
2689 purgephXbyspid(xpl
->sadb_x_policy_id
, false);
2702 if (mhp
[0] == NULL
) {
2703 plog(LLV_ERROR
, LOCATION
, NULL
,
2704 "inappropriate sadb spdget message passed.\n");
2715 struct sadb_msg
*msg
;
2716 struct sadb_address
*saddr
, *daddr
;
2717 struct sadb_x_policy
*xpl
;
2718 struct policyindex spidx
;
2719 struct secpolicy
*sp
;
2722 if (mhp
[0] == NULL
) {
2723 plog(LLV_ERROR
, LOCATION
, NULL
,
2724 "inappropriate sadb spddump message passed.\n");
2727 msg
= (struct sadb_msg
*)mhp
[0];
2729 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2730 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2731 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2733 if (saddr
== NULL
|| daddr
== NULL
|| xpl
== NULL
) {
2734 plog(LLV_ERROR
, LOCATION
, NULL
,
2735 "inappropriate sadb spddump message passed.\n");
2739 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2740 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2743 saddr
->sadb_address_prefixlen
,
2744 daddr
->sadb_address_prefixlen
,
2745 saddr
->sadb_address_proto
,
2746 xpl
->sadb_x_policy_priority
,
2749 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2752 saddr
->sadb_address_prefixlen
,
2753 daddr
->sadb_address_prefixlen
,
2754 saddr
->sadb_address_proto
,
2760 plog(LLV_ERROR
, LOCATION
, NULL
,
2761 "such policy already exists. "
2762 "anyway replace it: %s\n",
2768 if (addnewsp(mhp
) < 0)
2775 pk_recvspdflush(mhp
)
2779 if (mhp
[0] == NULL
) {
2780 plog(LLV_ERROR
, LOCATION
, NULL
,
2781 "inappropriate sadb spdflush message passed.\n");
2793 * send error against acquire message to kenrel.
2796 pk_sendeacquire(iph2
)
2797 struct ph2handle
*iph2
;
2799 struct sadb_msg
*newmsg
;
2802 len
= sizeof(struct sadb_msg
);
2803 newmsg
= racoon_calloc(1, len
);
2804 if (newmsg
== NULL
) {
2805 plog(LLV_ERROR
, LOCATION
, NULL
,
2806 "failed to get buffer to send acquire.\n");
2810 memset(newmsg
, 0, len
);
2811 newmsg
->sadb_msg_version
= PF_KEY_V2
;
2812 newmsg
->sadb_msg_type
= SADB_ACQUIRE
;
2813 newmsg
->sadb_msg_errno
= ENOENT
; /* XXX */
2814 newmsg
->sadb_msg_satype
= iph2
->satype
;
2815 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
2816 newmsg
->sadb_msg_reserved
= 0;
2817 newmsg
->sadb_msg_seq
= iph2
->seq
;
2818 newmsg
->sadb_msg_pid
= (u_int32_t
)getpid();
2821 len
= pfkey_send(lcconf
->sock_pfkey
, newmsg
, len
);
2823 racoon_free(newmsg
);
2829 pk_sendget_inbound_sastats(ike_session_t
*session
)
2831 u_int32_t max_stats
;
2835 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2839 session
->traffic_monitor
.num_in_curr_req
= 0;
2840 bzero(session
->traffic_monitor
.in_curr_req
, sizeof(session
->traffic_monitor
.in_curr_req
));
2841 max_stats
= (sizeof(session
->traffic_monitor
.in_curr_req
) / sizeof(session
->traffic_monitor
.in_curr_req
[0]));
2844 if ((session
->traffic_monitor
.num_in_curr_req
= ike_session_get_sas_for_stats(session
,
2847 session
->traffic_monitor
.in_curr_req
,
2849 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2851 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2853 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2858 session
->traffic_monitor
.in_curr_req
,
2859 session
->traffic_monitor
.num_in_curr_req
) < 0) {
2862 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2864 return session
->traffic_monitor
.num_in_curr_req
;
2870 pk_sendget_outbound_sastats(ike_session_t
*session
)
2872 u_int32_t max_stats
;
2876 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2880 session
->traffic_monitor
.num_out_curr_req
= 0;
2881 bzero(session
->traffic_monitor
.out_curr_req
, sizeof(session
->traffic_monitor
.out_curr_req
));
2882 max_stats
= (sizeof(session
->traffic_monitor
.out_curr_req
) / sizeof(session
->traffic_monitor
.out_curr_req
[0]));
2885 if ((session
->traffic_monitor
.num_out_curr_req
= ike_session_get_sas_for_stats(session
,
2888 session
->traffic_monitor
.out_curr_req
,
2890 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2892 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2894 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2899 session
->traffic_monitor
.out_curr_req
,
2900 session
->traffic_monitor
.num_out_curr_req
) < 0) {
2903 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2905 return session
->traffic_monitor
.num_out_curr_req
;
2911 * receive GETSPDSTAT from kernel.
2914 pk_recvgetsastat(mhp
)
2917 struct sadb_msg
*msg
;
2918 struct sadb_session_id
*session_id
;
2919 struct sadb_sastat
*stat_resp
;
2920 ike_session_t
*session
;
2922 /* validity check */
2923 if (mhp
[0] == NULL
||
2924 mhp
[SADB_EXT_SESSION_ID
] == NULL
||
2925 mhp
[SADB_EXT_SASTAT
] == NULL
) {
2926 plog(LLV_ERROR
, LOCATION
, NULL
,
2927 "inappropriate sadb getsastat response.\n");
2930 msg
= (struct sadb_msg
*)mhp
[0];
2931 session_id
= (ike_session_t
*)mhp
[SADB_EXT_SESSION_ID
];
2932 stat_resp
= (struct sadb_sastat
*)mhp
[SADB_EXT_SASTAT
];
2934 /* the message has to be processed or not ? */
2935 if (msg
->sadb_msg_pid
!= getpid()) {
2936 plog(LLV_DEBUG
, LOCATION
, NULL
,
2937 "%s message is not interesting "
2938 "because pid %d is not mine.\n",
2939 s_pfkey_type(msg
->sadb_msg_type
),
2943 if (!session_id
->sadb_session_id_v
[0]) {
2944 plog(LLV_DEBUG
, LOCATION
, NULL
,
2945 "%s message is bad "
2946 "because session-id[0] is invalid.\n",
2947 s_pfkey_type(msg
->sadb_msg_type
));
2950 session
= (__typeof__(session
))session_id
->sadb_session_id_v
[0];
2952 if (!stat_resp
->sadb_sastat_list_len
) {
2953 plog(LLV_DEBUG
, LOCATION
, NULL
,
2954 "%s message is bad "
2955 "because it has no sastats.\n",
2956 s_pfkey_type(msg
->sadb_msg_type
));
2960 ike_session_update_traffic_idle_status(session
,
2961 stat_resp
->sadb_sastat_dir
,
2962 (struct sastat
*)(stat_resp
+ 1),
2963 stat_resp
->sadb_sastat_list_len
);
2968 * check if the algorithm is supported or not.
2973 pk_checkalg(class, calg
, keylen
)
2974 int class, calg
, keylen
;
2978 struct sadb_alg alg0
;
2980 switch (algclass2doi(class)) {
2981 case IPSECDOI_PROTO_IPSEC_ESP
:
2982 sup
= SADB_EXT_SUPPORTED_ENCRYPT
;
2984 case IPSECDOI_ATTR_AUTH
:
2985 sup
= SADB_EXT_SUPPORTED_AUTH
;
2987 case IPSECDOI_PROTO_IPCOMP
:
2988 plog(LLV_DEBUG
, LOCATION
, NULL
,
2989 "compression algorithm can not be checked "
2990 "because sadb message doesn't support it.\n");
2993 plog(LLV_ERROR
, LOCATION
, NULL
,
2994 "invalid algorithm class.\n");
2997 alg
= ipsecdoi2pfkey_alg(algclass2doi(class), algtype2doi(class, calg
));
3002 if (ipsec_get_keylen(sup
, alg
, &alg0
)) {
3003 plog(LLV_ERROR
, LOCATION
, NULL
,
3004 "%s.\n", ipsec_strerror());
3007 keylen
= alg0
.sadb_alg_minbits
;
3010 error
= ipsec_check_keylen(sup
, alg
, keylen
);
3012 plog(LLV_ERROR
, LOCATION
, NULL
,
3013 "%s.\n", ipsec_strerror());
3019 * differences with pfkey_recv() in libipsec/pfkey.c:
3020 * - never performs busy wait loop.
3021 * - returns NULL and set *lenp to negative on fatal failures
3022 * - returns NULL and set *lenp to non-negative on non-fatal failures
3023 * - returns non-NULL on success
3025 static struct sadb_msg
*
3030 struct sadb_msg
*newmsg
;
3032 socklen_t optlen
= sizeof(reallen
);
3034 if (getsockopt(so
, SOL_SOCKET
, SO_NREAD
, &reallen
, &optlen
) < 0)
3035 return NULL
; /*fatal*/
3040 if ((newmsg
= racoon_calloc(1, reallen
)) == NULL
)
3043 *lenp
= recv(so
, (caddr_t
)newmsg
, reallen
, 0);
3045 racoon_free(newmsg
);
3046 return NULL
; /*fatal*/
3047 } else if (*lenp
!= reallen
|| *lenp
< sizeof(struct sadb_msg
)) {
3048 racoon_free(newmsg
);
3059 return eay_random();
3066 struct secpolicy
*new;
3067 struct sadb_address
*saddr
, *daddr
;
3068 struct sadb_x_policy
*xpl
;
3071 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
3072 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
3073 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
3074 plog(LLV_ERROR
, LOCATION
, NULL
,
3075 "inappropriate sadb spd management message passed.\n");
3079 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
3080 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
3081 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
3084 /* bsd skips over per-socket policies because there will be no
3085 * src and dst extensions in spddump messages. On Linux the only
3086 * way to achieve the same is check for policy id.
3088 if (xpl
->sadb_x_policy_id
% 8 >= 3) return 0;
3093 plog(LLV_ERROR
, LOCATION
, NULL
,
3094 "failed to allocate buffer\n");
3098 new->spidx
.dir
= xpl
->sadb_x_policy_dir
;
3099 new->id
= xpl
->sadb_x_policy_id
;
3100 new->policy
= xpl
->sadb_x_policy_type
;
3104 switch (xpl
->sadb_x_policy_type
) {
3105 case IPSEC_POLICY_DISCARD
:
3106 case IPSEC_POLICY_GENERATE
:
3107 case IPSEC_POLICY_NONE
:
3108 case IPSEC_POLICY_ENTRUST
:
3109 case IPSEC_POLICY_BYPASS
:
3112 case IPSEC_POLICY_IPSEC
:
3115 struct sadb_x_ipsecrequest
*xisr
;
3116 struct ipsecrequest
**p_isr
= &new->req
;
3118 /* validity check */
3119 if (PFKEY_EXTLEN(xpl
) < sizeof(*xpl
)) {
3120 plog(LLV_ERROR
, LOCATION
, NULL
,
3121 "invalid msg length.\n");
3125 tlen
= PFKEY_EXTLEN(xpl
) - sizeof(*xpl
);
3126 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
3131 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
3132 plog(LLV_ERROR
, LOCATION
, NULL
,
3133 "invalid msg length.\n");
3137 /* allocate request buffer */
3138 *p_isr
= newipsecreq();
3139 if (*p_isr
== NULL
) {
3140 plog(LLV_ERROR
, LOCATION
, NULL
,
3141 "failed to get new ipsecreq.\n");
3146 (*p_isr
)->next
= NULL
;
3148 switch (xisr
->sadb_x_ipsecrequest_proto
) {
3151 case IPPROTO_IPCOMP
:
3154 plog(LLV_ERROR
, LOCATION
, NULL
,
3155 "invalid proto type: %u\n",
3156 xisr
->sadb_x_ipsecrequest_proto
);
3159 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
3161 switch (xisr
->sadb_x_ipsecrequest_mode
) {
3162 case IPSEC_MODE_TRANSPORT
:
3163 case IPSEC_MODE_TUNNEL
:
3165 case IPSEC_MODE_ANY
:
3167 plog(LLV_ERROR
, LOCATION
, NULL
,
3168 "invalid mode: %u\n",
3169 xisr
->sadb_x_ipsecrequest_mode
);
3172 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
3174 switch (xisr
->sadb_x_ipsecrequest_level
) {
3175 case IPSEC_LEVEL_DEFAULT
:
3176 case IPSEC_LEVEL_USE
:
3177 case IPSEC_LEVEL_REQUIRE
:
3179 case IPSEC_LEVEL_UNIQUE
:
3180 (*p_isr
)->saidx
.reqid
=
3181 xisr
->sadb_x_ipsecrequest_reqid
;
3185 plog(LLV_ERROR
, LOCATION
, NULL
,
3186 "invalid level: %u\n",
3187 xisr
->sadb_x_ipsecrequest_level
);
3190 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
3192 /* set IP addresses if there */
3193 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
3194 struct sockaddr
*paddr
;
3196 paddr
= (struct sockaddr
*)(xisr
+ 1);
3197 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
3198 sysdep_sa_len(paddr
));
3200 paddr
= (struct sockaddr
*)((caddr_t
)paddr
3201 + sysdep_sa_len(paddr
));
3202 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
3203 sysdep_sa_len(paddr
));
3208 /* initialization for the next. */
3209 p_isr
= &(*p_isr
)->next
;
3210 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
3212 /* validity check */
3214 plog(LLV_ERROR
, LOCATION
, NULL
,
3215 "becoming tlen < 0\n");
3218 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
3219 + xisr
->sadb_x_ipsecrequest_len
);
3224 plog(LLV_ERROR
, LOCATION
, NULL
,
3225 "invalid policy type.\n");
3229 #ifdef HAVE_PFKEY_POLICY_PRIORITY
3230 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3233 saddr
->sadb_address_prefixlen
,
3234 daddr
->sadb_address_prefixlen
,
3235 saddr
->sadb_address_proto
,
3236 xpl
->sadb_x_policy_priority
,
3239 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3242 saddr
->sadb_address_prefixlen
,
3243 daddr
->sadb_address_prefixlen
,
3244 saddr
->sadb_address_proto
,
3253 /* proto/mode/src->dst spi */
3255 sadbsecas2str(src
, dst
, proto
, spi
, mode
)
3256 struct sockaddr
*src
, *dst
;
3261 static char buf
[256];
3262 u_int doi_proto
, doi_mode
= 0;
3266 doi_proto
= pfkey2ipsecdoi_proto(proto
);
3267 if (doi_proto
== ~0)
3270 doi_mode
= pfkey2ipsecdoi_mode(mode
);
3275 blen
= sizeof(buf
) - 1;
3278 i
= snprintf(p
, blen
, "%s%s%s ",
3279 s_ipsecdoi_proto(doi_proto
),
3281 mode
? s_ipsecdoi_encmode(doi_mode
) : "");
3282 if (i
< 0 || i
>= blen
)
3287 i
= snprintf(p
, blen
, "%s->", saddr2str(src
));
3288 if (i
< 0 || i
>= blen
)
3293 i
= snprintf(p
, blen
, "%s ", saddr2str(dst
));
3294 if (i
< 0 || i
>= blen
)
3300 snprintf(p
, blen
, "spi=%lu(0x%lx)", (unsigned long)ntohl(spi
),
3301 (unsigned long)ntohl(spi
));