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
->status
!= PHASE2ST_GETSPISENT
) {
1002 plog(LLV_ERROR
, LOCATION
, NULL
,
1003 "status mismatch (db:%d msg:%d)\n",
1004 iph2
->status
, PHASE2ST_GETSPISENT
);
1008 // check the underlying iph2->ph1
1010 if (!ike_session_update_ph2_ph1bind(iph2
)) {
1011 plog(LLV_ERROR
, LOCATION
, NULL
,
1012 "can't proceed with getspi for %s. no suitable ISAKMP-SA found \n",
1013 saddrwop2str(iph2
->dst
));
1021 /* set SPI, and check to get all spi whether or not */
1024 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1025 pp
= iph2
->side
== INITIATOR
? iph2
->proposal
: iph2
->approval
;
1027 for (pr
= pp
->head
; pr
!= NULL
; pr
= pr
->next
) {
1028 if (pr
->proto_id
== proto_id
&& pr
->spi
== 0) {
1029 pr
->spi
= sa
->sadb_sa_spi
;
1031 plog(LLV_DEBUG
, LOCATION
, NULL
,
1032 "pfkey GETSPI succeeded: %s\n",
1033 sadbsecas2str(iph2
->dst
, iph2
->src
,
1034 msg
->sadb_msg_satype
,
1036 ipsecdoi2pfkey_mode(pr
->encmode
)));
1039 allspiok
= 0; /* not get all spi */
1043 plog(LLV_ERROR
, LOCATION
, NULL
,
1044 "get spi for unknown address %s\n",
1045 saddrwop2str(iph2
->dst
));
1054 iph2
->status
= PHASE2ST_GETSPIDONE
;
1055 if (isakmp_post_getspi(iph2
) < 0) {
1056 plog(LLV_ERROR
, LOCATION
, NULL
,
1057 "failed to start post getspi.\n");
1074 struct ph2handle
*iph2
;
1077 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1078 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1080 u_int64_t lifebyte
= 0;
1081 u_int wsize
= 4; /* XXX static size of window */
1083 struct ph2natt natt
;
1086 if (iph2
->approval
== NULL
) {
1087 plog(LLV_ERROR
, LOCATION
, NULL
,
1088 "no approvaled SAs found.\n");
1091 if (iph2
->side
== INITIATOR
)
1092 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1093 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1096 /* for mobile IPv6 */
1097 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1098 ipsecdoi_transportmode(iph2
->approval
)) {
1106 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1107 /* validity check */
1108 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1110 plog(LLV_ERROR
, LOCATION
, NULL
,
1111 "invalid proto_id %d\n", pr
->proto_id
);
1114 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1115 /* IPCOMP has no replay window */
1118 #ifdef ENABLE_SAMODE_UNSPECIFIED
1119 mode
= IPSEC_MODE_ANY
;
1121 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1123 plog(LLV_ERROR
, LOCATION
, NULL
,
1124 "invalid encmode %d\n", pr
->encmode
);
1129 /* set algorithm type and key length */
1130 e_keylen
= pr
->head
->encklen
;
1131 if (pfkey_convertfromipsecdoi(
1136 &a_type
, &a_keylen
, &flags
) < 0)
1140 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1147 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1148 if (pr
->udp_encap
) {
1149 memset (&natt
, 0, sizeof (natt
));
1150 natt
.sport
= extract_port (iph2
->ph1
->remote
);
1151 flags
|= SADB_X_EXT_NATT
;
1152 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1153 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1154 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1156 else if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1157 mode
== IPSEC_MODE_TRANSPORT
&&
1158 src
->sa_family
== AF_INET
)
1159 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1161 memset (&natt
, 0, sizeof (natt
));
1164 if (pfkey_send_update(
1174 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1175 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1176 iph2
->seq
, natt
.sport
) < 0) {
1177 plog(LLV_ERROR
, LOCATION
, NULL
,
1178 "libipsec failed send update (%s)\n",
1183 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1184 if (pfkey_send_update(
1194 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1195 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1196 iph2
->seq
, 0) < 0) {
1197 plog(LLV_ERROR
, LOCATION
, NULL
,
1198 "libipsec failed send update (%s)\n",
1202 #endif /* ENABLE_NATT */
1205 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update_nat\n");
1206 if (pr
->udp_encap
) {
1207 memset (&natt
, 0, sizeof (natt
));
1208 natt
.type
= iph2
->ph1
->natt_options
->encaps_type
;
1209 natt
.sport
= extract_port (iph2
->ph1
->remote
);
1210 natt
.dport
= extract_port (iph2
->ph1
->local
);
1211 natt
.oa
= NULL
; // FIXME: Here comes OA!!!
1212 natt
.frag
= iph2
->ph1
->rmconf
->esp_frag
;
1214 memset (&natt
, 0, sizeof (natt
));
1217 if (pfkey_send_update_nat(
1227 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1228 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1230 natt
.type
, natt
.sport
, natt
.dport
, natt
.oa
,
1232 plog(LLV_ERROR
, LOCATION
, NULL
,
1233 "libipsec failed send update_nat (%s)\n",
1238 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_update\n");
1239 if (pfkey_send_update(
1249 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1250 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1252 plog(LLV_ERROR
, LOCATION
, NULL
,
1253 "libipsec failed send update (%s)\n",
1257 #endif /* ENABLE_NATT */
1258 #endif /* __APPLE__ */
1260 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
1264 * It maybe good idea to call backupsa_to_file() after
1265 * racoon will receive the sadb_update messages.
1266 * But it is impossible because there is not key in the
1267 * information from the kernel.
1269 if (backupsa_to_file(satype
, mode
, dst
, src
,
1270 pr
->spi
, pr
->reqid_in
, 4,
1272 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1273 0, iph2
->approval
->lifebyte
* 1024,
1274 iph2
->approval
->lifetime
, 0,
1276 plog(LLV_ERROR
, LOCATION
, NULL
,
1277 "backuped SA failed: %s\n",
1278 sadbsecas2str(dst
, src
,
1279 satype
, pr
->spi
, mode
));
1281 plog(LLV_DEBUG
, LOCATION
, NULL
,
1282 "backuped SA: %s\n",
1283 sadbsecas2str(dst
, src
,
1284 satype
, pr
->spi
, mode
));
1294 struct sadb_msg
*msg
;
1296 struct sockaddr
*src
, *dst
;
1297 struct ph2handle
*iph2
;
1298 u_int proto_id
, encmode
, sa_mode
;
1302 /* ignore this message because of local test mode. */
1308 || mhp
[SADB_EXT_SA
] == NULL
1309 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1310 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1311 plog(LLV_ERROR
, LOCATION
, NULL
,
1312 "inappropriate sadb update message passed.\n");
1315 msg
= (struct sadb_msg
*)mhp
[0];
1316 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1317 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1318 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1320 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1322 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1324 /* the message has to be processed or not ? */
1325 if (msg
->sadb_msg_pid
!= getpid()) {
1326 plog(LLV_DEBUG
, LOCATION
, NULL
,
1327 "%s message is not interesting "
1328 "because pid %d is not mine.\n",
1329 s_pfkey_type(msg
->sadb_msg_type
),
1334 iph2
= getph2byseq(msg
->sadb_msg_seq
);
1336 plog(LLV_DEBUG
, LOCATION
, NULL
,
1337 "seq %d of %s message not interesting.\n",
1339 s_pfkey_type(msg
->sadb_msg_type
));
1343 if (iph2
->status
!= PHASE2ST_ADDSA
) {
1344 plog(LLV_ERROR
, LOCATION
, NULL
,
1345 "status mismatch (db:%d msg:%d)\n",
1346 iph2
->status
, PHASE2ST_ADDSA
);
1350 /* check to complete all keys ? */
1351 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1352 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1353 if (proto_id
== ~0) {
1354 plog(LLV_ERROR
, LOCATION
, NULL
,
1355 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1358 encmode
= pfkey2ipsecdoi_mode(sa_mode
);
1359 if (encmode
== ~0) {
1360 plog(LLV_ERROR
, LOCATION
, NULL
,
1361 "invalid encmode %d\n", sa_mode
);
1365 if (pr
->proto_id
== proto_id
1366 && pr
->spi
== sa
->sadb_sa_spi
) {
1368 plog(LLV_DEBUG
, LOCATION
, NULL
,
1369 "pfkey UPDATE succeeded: %s\n",
1370 sadbsecas2str(iph2
->dst
, iph2
->src
,
1371 msg
->sadb_msg_satype
,
1375 plog(LLV_INFO
, LOCATION
, NULL
,
1376 "IPsec-SA established: %s\n",
1377 sadbsecas2str(iph2
->dst
, iph2
->src
,
1378 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1389 /* turn off the timer for calling pfkey_timeover() */
1390 SCHED_KILL(iph2
->sce
);
1393 iph2
->status
= PHASE2ST_ESTABLISHED
;
1395 if (iph2
->side
== INITIATOR
) {
1396 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1397 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_SUCC
,
1398 CONSTSTR("Initiator, Quick-Mode"),
1401 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
1402 IPSECSESSIONEVENTCODE_IKEV1_PH2_RESP_SUCC
,
1403 CONSTSTR("Responder, Quick-Mode"),
1407 ike_session_ph2_established(iph2
);
1410 gettimeofday(&iph2
->end
, NULL
);
1411 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1412 "phase2", "quick", timedelta(&iph2
->start
, &iph2
->end
));
1417 iph2
->ph1
->ph2cnt
++;
1419 /* turn off schedule */
1421 SCHED_KILL(iph2
->scr
);
1424 * since we are going to reuse the phase2 handler, we need to
1425 * remain it and refresh all the references between ph1 and ph2 to use.
1429 iph2
->sce
= sched_new(iph2
->approval
->lifetime
,
1430 isakmp_ph2expire_stub
, iph2
);
1432 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1441 struct ph2handle
*iph2
;
1444 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1445 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1447 u_int64_t lifebyte
= 0;
1448 u_int wsize
= 4; /* XXX static size of window */
1450 struct ph2natt natt
;
1453 if (iph2
->approval
== NULL
) {
1454 plog(LLV_ERROR
, LOCATION
, NULL
,
1455 "no approvaled SAs found.\n");
1458 if (iph2
->side
== INITIATOR
)
1459 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1460 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1463 /* for mobile IPv6 */
1464 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1465 ipsecdoi_transportmode(iph2
->approval
)) {
1473 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1474 /* validity check */
1475 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1477 plog(LLV_ERROR
, LOCATION
, NULL
,
1478 "invalid proto_id %d\n", pr
->proto_id
);
1481 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1482 /* no replay window for IPCOMP */
1485 #ifdef ENABLE_SAMODE_UNSPECIFIED
1486 mode
= IPSEC_MODE_ANY
;
1488 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1490 plog(LLV_ERROR
, LOCATION
, NULL
,
1491 "invalid encmode %d\n", pr
->encmode
);
1496 /* set algorithm type and key length */
1497 e_keylen
= pr
->head
->encklen
;
1498 if (pfkey_convertfromipsecdoi(
1503 &a_type
, &a_keylen
, &flags
) < 0)
1507 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1514 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1516 if (pr
->udp_encap
) {
1517 memset (&natt
, 0, sizeof (natt
));
1518 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1519 flags
|= SADB_X_EXT_NATT
;
1520 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1521 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1522 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1524 else if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1525 mode
== IPSEC_MODE_TRANSPORT
&&
1526 dst
->sa_family
== AF_INET
)
1527 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1529 memset (&natt
, 0, sizeof (natt
));
1531 /* Remove port information, that SA doesn't use it */
1546 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1547 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1548 iph2
->seq
,natt
.dport
) < 0) {
1549 plog(LLV_ERROR
, LOCATION
, NULL
,
1550 "libipsec failed send add (%s)\n",
1555 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1557 /* Remove port information, it is not used without NAT-T */
1571 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1572 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1573 iph2
->seq
, 0) < 0) {
1574 plog(LLV_ERROR
, LOCATION
, NULL
,
1575 "libipsec failed send add (%s)\n",
1579 #endif /* ENABLE_NATT */
1580 #else /* __APPLE__ */
1582 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add_nat\n");
1584 if (pr
->udp_encap
) {
1585 memset (&natt
, 0, sizeof (natt
));
1586 natt
.type
= UDP_ENCAP_ESPINUDP
;
1587 natt
.sport
= extract_port (iph2
->ph1
->local
);
1588 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1589 natt
.oa
= NULL
; // FIXME: Here comes OA!!!
1590 natt
.frag
= iph2
->ph1
->rmconf
->esp_frag
;
1592 memset (&natt
, 0, sizeof (natt
));
1594 /* Remove port information, that SA doesn't use it */
1599 if (pfkey_send_add_nat(
1609 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1610 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1612 natt
.type
, natt
.sport
, natt
.dport
, natt
.oa
,
1614 plog(LLV_ERROR
, LOCATION
, NULL
,
1615 "libipsec failed send add_nat (%s)\n",
1620 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1622 /* Remove port information, it is not used without NAT-T */
1636 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1637 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1639 plog(LLV_ERROR
, LOCATION
, NULL
,
1640 "libipsec failed send add (%s)\n",
1644 #endif /* ENABLE_NATT */
1645 #endif /* __APPLE__ */
1647 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
1651 * It maybe good idea to call backupsa_to_file() after
1652 * racoon will receive the sadb_update messages.
1653 * But it is impossible because there is not key in the
1654 * information from the kernel.
1656 if (backupsa_to_file(satype
, mode
, src
, dst
,
1657 pr
->spi_p
, pr
->reqid_out
, 4,
1659 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1660 0, iph2
->approval
->lifebyte
* 1024,
1661 iph2
->approval
->lifetime
, 0,
1663 plog(LLV_ERROR
, LOCATION
, NULL
,
1664 "backuped SA failed: %s\n",
1665 sadbsecas2str(src
, dst
,
1666 satype
, pr
->spi_p
, mode
));
1668 plog(LLV_DEBUG
, LOCATION
, NULL
,
1669 "backuped SA: %s\n",
1670 sadbsecas2str(src
, dst
,
1671 satype
, pr
->spi_p
, mode
));
1681 struct sadb_msg
*msg
;
1683 struct sockaddr
*src
, *dst
;
1684 struct ph2handle
*iph2
;
1687 /* ignore this message because of local test mode. */
1693 || mhp
[SADB_EXT_SA
] == NULL
1694 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1695 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1696 plog(LLV_ERROR
, LOCATION
, NULL
,
1697 "inappropriate sadb add message passed.\n");
1700 msg
= (struct sadb_msg
*)mhp
[0];
1701 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1702 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1703 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1705 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1707 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1709 /* the message has to be processed or not ? */
1710 if (msg
->sadb_msg_pid
!= getpid()) {
1711 plog(LLV_DEBUG
, LOCATION
, NULL
,
1712 "%s message is not interesting "
1713 "because pid %d is not mine.\n",
1714 s_pfkey_type(msg
->sadb_msg_type
),
1719 iph2
= getph2byseq(msg
->sadb_msg_seq
);
1721 plog(LLV_DEBUG
, LOCATION
, NULL
,
1722 "seq %d of %s message not interesting.\n",
1724 s_pfkey_type(msg
->sadb_msg_type
));
1729 * NOTE don't update any status of phase2 handle
1730 * because they must be updated by SADB_UPDATE message
1733 plog(LLV_INFO
, LOCATION
, NULL
,
1734 "IPsec-SA established: %s\n",
1735 sadbsecas2str(iph2
->src
, iph2
->dst
,
1736 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1738 ike_session_cleanup_other_established_ph2s(iph2
->parent_session
, iph2
);
1740 #ifdef ENABLE_VPNCONTROL_PORT
1744 if (iph2
->dst
->sa_family
== AF_INET
)
1745 address
= ((struct sockaddr_in
*)iph2
->dst
)->sin_addr
.s_addr
;
1748 vpncontrol_notify_phase_change(0, FROM_LOCAL
, NULL
, iph2
);
1752 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1760 struct sadb_msg
*msg
;
1762 struct sockaddr
*src
, *dst
;
1763 struct ph2handle
*iph2
;
1764 u_int proto_id
, sa_mode
;
1768 || mhp
[SADB_EXT_SA
] == NULL
1769 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1770 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1771 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
1772 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
1773 plog(LLV_ERROR
, LOCATION
, NULL
,
1774 "inappropriate sadb expire message passed.\n");
1777 msg
= (struct sadb_msg
*)mhp
[0];
1778 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1779 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1780 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1782 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1784 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1786 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1787 if (proto_id
== ~0) {
1788 plog(LLV_ERROR
, LOCATION
, NULL
,
1789 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1793 plog(LLV_INFO
, LOCATION
, NULL
,
1794 "IPsec-SA expired: %s\n",
1795 sadbsecas2str(src
, dst
,
1796 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1798 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1801 * Ignore it because two expire messages are come up.
1802 * phase2 handler has been deleted already when 2nd message
1805 plog(LLV_DEBUG
, LOCATION
, NULL
,
1806 "no such a SA found: %s\n",
1807 sadbsecas2str(src
, dst
,
1808 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1812 if (iph2
->status
!= PHASE2ST_ESTABLISHED
) {
1814 * If the status is not equal to PHASE2ST_ESTABLISHED,
1815 * racoon ignores this expire message. There are two reason.
1816 * One is that the phase 2 probably starts because there is
1817 * a potential that racoon receives the acquire message
1818 * without receiving a expire message. Another is that racoon
1819 * may receive the multiple expire messages from the kernel.
1821 plog(LLV_WARNING
, LOCATION
, NULL
,
1822 "the expire message is received "
1823 "but the handler has not been established.\n");
1827 /* turn off the timer for calling isakmp_ph2expire() */
1828 SCHED_KILL(iph2
->sce
);
1830 iph2
->status
= PHASE2ST_EXPIRED
;
1832 /* INITIATOR, begin phase 2 exchange only if there's no other established ph2. */
1833 /* allocate buffer for status management of pfkey message */
1834 if (iph2
->side
== INITIATOR
&&
1835 !ike_session_has_other_established_ph2(iph2
->parent_session
, iph2
) &&
1836 !ike_session_drop_rekey(iph2
->parent_session
)) {
1840 /* update status for re-use */
1841 iph2
->status
= PHASE2ST_STATUS2
;
1843 /* start isakmp initiation by using ident exchange */
1844 if (isakmp_post_acquire(iph2
) < 0) {
1845 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
1846 "failed to begin ipsec sa "
1847 "re-negotiation.\n");
1858 /* If not received SADB_EXPIRE, INITIATOR delete ph2handle. */
1859 /* RESPONDER always delete ph2handle, keep silent. RESPONDER doesn't
1860 * manage IPsec SA, so delete the list */
1872 struct sadb_msg
*msg
;
1873 struct sadb_x_policy
*xpl
;
1874 struct secpolicy
*sp_out
= NULL
, *sp_in
= NULL
;
1875 #define MAXNESTEDSA 5 /* XXX */
1876 struct ph2handle
*iph2
[MAXNESTEDSA
];
1877 struct sockaddr
*src
, *dst
;
1878 int n
; /* # of phase 2 handler */
1880 /* ignore this message because of local test mode. */
1886 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1887 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1888 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1889 plog(LLV_ERROR
, LOCATION
, NULL
,
1890 "inappropriate sadb acquire message passed.\n");
1893 msg
= (struct sadb_msg
*)mhp
[0];
1894 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
1895 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1896 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1898 /* ignore if type is not IPSEC_POLICY_IPSEC */
1899 if (xpl
->sadb_x_policy_type
!= IPSEC_POLICY_IPSEC
) {
1900 plog(LLV_DEBUG
, LOCATION
, NULL
,
1901 "ignore ACQUIRE message. type is not IPsec.\n");
1905 /* ignore it if src is multicast address */
1907 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1909 if ((sa
->sa_family
== AF_INET
1910 && IN_MULTICAST(ntohl(((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
)))
1912 || (sa
->sa_family
== AF_INET6
1913 && IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)sa
)->sin6_addr
))
1916 plog(LLV_DEBUG
, LOCATION
, NULL
,
1917 "ignore due to multicast address: %s.\n",
1923 /* ignore, if we do not listen on source address */
1926 * - if we'll contact peer from address we do not listen -
1927 * we will be unable to complete negotiation;
1928 * - if we'll negotiate using address we're listening -
1929 * remote peer will send packets to address different
1930 * than one in the policy, so kernel will drop them;
1931 * => therefore this acquire is not for us! --Aidas
1933 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1936 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1937 if (!cmpsaddrwop(p
->addr
, sa
)) {
1944 plog(LLV_DEBUG
, LOCATION
, NULL
,
1945 "ignore because do not listen on source address : %s.\n",
1952 * If there is a phase 2 handler against the policy identifier in
1953 * the acquire message, and if
1954 * 1. its state is less than PHASE2ST_ESTABLISHED, then racoon
1955 * should ignore such a acquire message because the phase 2
1956 * is just negotiating.
1957 * 2. its state is equal to PHASE2ST_ESTABLISHED, then racoon
1958 * has to process such a acquire message because racoon may
1959 * have lost the expire message.
1961 iph2
[0] = getph2byid(src
, dst
, xpl
->sadb_x_policy_id
);
1962 if (iph2
[0] != NULL
) {
1963 if (iph2
[0]->status
< PHASE2ST_ESTABLISHED
) {
1964 plog(LLV_DEBUG
, LOCATION
, NULL
,
1965 "ignore the acquire because ph2 found\n");
1968 if (iph2
[0]->status
== PHASE2ST_EXPIRED
)
1973 /* search for proper policyindex */
1974 sp_out
= getspbyspid(xpl
->sadb_x_policy_id
);
1975 if (sp_out
== NULL
) {
1976 plog(LLV_ERROR
, LOCATION
, NULL
, "no policy found: id:%d.\n",
1977 xpl
->sadb_x_policy_id
);
1980 plog(LLV_DEBUG
, LOCATION
, NULL
,
1981 "suitable outbound SP found: %s.\n", spidx2str(&sp_out
->spidx
));
1983 /* get inbound policy */
1985 struct policyindex spidx
;
1987 spidx
.dir
= IPSEC_DIR_INBOUND
;
1988 memcpy(&spidx
.src
, &sp_out
->spidx
.dst
, sizeof(spidx
.src
));
1989 memcpy(&spidx
.dst
, &sp_out
->spidx
.src
, sizeof(spidx
.dst
));
1990 spidx
.prefs
= sp_out
->spidx
.prefd
;
1991 spidx
.prefd
= sp_out
->spidx
.prefs
;
1992 spidx
.ul_proto
= sp_out
->spidx
.ul_proto
;
1994 sp_in
= getsp(&spidx
);
1996 plog(LLV_DEBUG
, LOCATION
, NULL
,
1997 "suitable inbound SP found: %s.\n",
1998 spidx2str(&sp_in
->spidx
));
2000 plog(LLV_NOTIFY
, LOCATION
, NULL
,
2001 "no in-bound policy found: %s\n",
2006 memset(iph2
, 0, MAXNESTEDSA
);
2010 /* allocate a phase 2 */
2012 if (iph2
[n
] == NULL
) {
2013 plog(LLV_ERROR
, LOCATION
, NULL
,
2014 "failed to allocate phase2 entry.\n");
2017 iph2
[n
]->side
= INITIATOR
;
2018 iph2
[n
]->spid
= xpl
->sadb_x_policy_id
;
2019 iph2
[n
]->satype
= msg
->sadb_msg_satype
;
2020 iph2
[n
]->seq
= msg
->sadb_msg_seq
;
2021 iph2
[n
]->status
= PHASE2ST_STATUS2
;
2023 /* set end addresses of SA */
2024 iph2
[n
]->dst
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]));
2025 if (iph2
[n
]->dst
== NULL
) {
2029 iph2
[n
]->src
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]));
2030 if (iph2
[n
]->src
== NULL
) {
2035 plog(LLV_DEBUG
, LOCATION
, NULL
,
2036 "new acquire %s\n", spidx2str(&sp_out
->spidx
));
2040 vchar_t
*idsrc
, *iddst
;
2042 idsrc
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.src
,
2043 sp_out
->spidx
.prefs
, sp_out
->spidx
.ul_proto
);
2044 if (idsrc
== NULL
) {
2045 plog(LLV_ERROR
, LOCATION
, NULL
,
2046 "failed to get ID for %s\n",
2047 spidx2str(&sp_out
->spidx
));
2051 iddst
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.dst
,
2052 sp_out
->spidx
.prefd
, sp_out
->spidx
.ul_proto
);
2053 if (iddst
== NULL
) {
2054 plog(LLV_ERROR
, LOCATION
, NULL
,
2055 "failed to get ID for %s\n",
2056 spidx2str(&sp_out
->spidx
));
2061 iph2
[n
]->sainfo
= getsainfo(idsrc
, iddst
, NULL
, 0);
2064 if (iph2
[n
]->sainfo
== NULL
) {
2065 plog(LLV_ERROR
, LOCATION
, NULL
,
2066 "failed to get sainfo.\n");
2069 /* XXX should use the algorithm list from register message */
2072 if (link_sainfo_to_ph2(iph2
[n
]->sainfo
) != 0) {
2073 plog(LLV_ERROR
, LOCATION
, NULL
,
2074 "failed to link sainfo\n");
2075 iph2
[n
]->sainfo
= NULL
;
2082 if (set_proposal_from_policy(iph2
[n
], sp_out
, sp_in
) < 0) {
2083 plog(LLV_ERROR
, LOCATION
, NULL
,
2084 "failed to create saprop.\n");
2090 /* start isakmp initiation by using ident exchange */
2091 /* XXX should be looped if there are multiple phase 2 handler. */
2092 if (isakmp_post_acquire(iph2
[n
]) < 0) {
2093 plog(LLV_ERROR
, LOCATION
, NULL
,
2094 "failed to begin ipsec sa negotiation.\n");
2098 #if !TARGET_OS_EMBEDDED
2099 if ( lcconf
->vt
== NULL
){
2100 if (!(lcconf
->vt
= vproc_transaction_begin(NULL
)))
2101 plog(LLV_ERROR
, LOCATION
, NULL
,
2102 "vproc_transaction_begin returns NULL.\n");
2111 unbindph12(iph2
[n
]);
2124 struct sadb_msg
*msg
;
2126 struct sockaddr
*src
, *dst
;
2127 struct ph2handle
*iph2
= NULL
;
2130 /* ignore this message because of local test mode. */
2136 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2137 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
2138 plog(LLV_ERROR
, LOCATION
, NULL
,
2139 "inappropriate sadb delete message passed.\n");
2142 msg
= (struct sadb_msg
*)mhp
[0];
2143 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
2144 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
2145 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
2147 /* the message has to be processed or not ? */
2148 if (msg
->sadb_msg_pid
== getpid()) {
2149 plog(LLV_DEBUG
, LOCATION
, NULL
,
2150 "%s message is not interesting "
2151 "because the message was originated by me.\n",
2152 s_pfkey_type(msg
->sadb_msg_type
));
2156 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
2157 if (proto_id
== ~0) {
2158 plog(LLV_ERROR
, LOCATION
, NULL
,
2159 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
2163 plog(LLV_DEBUG2
, LOCATION
, NULL
, "SADB delete message: proto-id %d\n", proto_id
);
2164 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str(src
));
2165 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str(dst
));
2168 deleteallph2(src
, dst
, proto_id
);
2169 deleteallph1(src
, dst
);
2173 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
2176 plog(LLV_ERROR
, LOCATION
, NULL
,
2177 "no iph2 found: %s\n",
2178 sadbsecas2str(src
, dst
, msg
->sadb_msg_satype
,
2179 sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2183 plog(LLV_ERROR
, LOCATION
, NULL
,
2184 "pfkey DELETE received: %s\n",
2185 sadbsecas2str(iph2
->src
, iph2
->dst
,
2186 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2188 /* send delete information */
2189 if (iph2
->status
== PHASE2ST_ESTABLISHED
)
2190 isakmp_info_send_d2(iph2
);
2192 ike_session_cleanup_ph1s_by_ph2(iph2
);
2204 /* ignore this message because of local test mode. */
2209 if (mhp
[0] == NULL
) {
2210 plog(LLV_ERROR
, LOCATION
, NULL
,
2211 "inappropriate sadb flush message passed.\n");
2222 getsadbpolicy(policy0
, policylen0
, type
, iph2
)
2224 int *policylen0
, type
;
2225 struct ph2handle
*iph2
;
2227 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2228 struct sadb_x_policy
*xpl
;
2229 struct sadb_x_ipsecrequest
*xisr
;
2236 /* get policy buffer size */
2237 policylen
= sizeof(struct sadb_x_policy
);
2238 if (type
!= SADB_X_SPDDELETE
) {
2239 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2240 xisrlen
= sizeof(*xisr
);
2241 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2242 xisrlen
+= (sysdep_sa_len(iph2
->src
)
2243 + sysdep_sa_len(iph2
->dst
));
2246 policylen
+= PFKEY_ALIGN8(xisrlen
);
2250 /* make policy structure */
2251 policy
= racoon_malloc(policylen
);
2253 plog(LLV_ERROR
, LOCATION
, NULL
,
2254 "buffer allocation failed.\n");
2258 xpl
= (struct sadb_x_policy
*)policy
;
2259 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(policylen
);
2260 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2261 xpl
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2262 xpl
->sadb_x_policy_dir
= spidx
->dir
;
2263 xpl
->sadb_x_policy_id
= 0;
2264 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2265 xpl
->sadb_x_policy_priority
= PRIORITY_DEFAULT
;
2268 /* no need to append policy information any more if type is SPDDELETE */
2269 if (type
== SADB_X_SPDDELETE
)
2272 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
2274 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2276 satype
= doi2ipproto(pr
->proto_id
);
2278 plog(LLV_ERROR
, LOCATION
, NULL
,
2279 "invalid proto_id %d\n", pr
->proto_id
);
2282 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
2284 plog(LLV_ERROR
, LOCATION
, NULL
,
2285 "invalid encmode %d\n", pr
->encmode
);
2290 * the policy level cannot be unique because the policy
2291 * is defined later than SA, so req_id cannot be bound to SA.
2293 xisr
->sadb_x_ipsecrequest_proto
= satype
;
2294 xisr
->sadb_x_ipsecrequest_mode
= mode
;
2295 xisr
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2296 xisr
->sadb_x_ipsecrequest_reqid
= 0;
2297 p
= (caddr_t
)(xisr
+ 1);
2299 xisrlen
= sizeof(*xisr
);
2301 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2302 int src_len
, dst_len
;
2304 src_len
= sysdep_sa_len(iph2
->src
);
2305 dst_len
= sysdep_sa_len(iph2
->dst
);
2306 xisrlen
+= src_len
+ dst_len
;
2308 memcpy(p
, iph2
->src
, src_len
);
2311 memcpy(p
, iph2
->dst
, dst_len
);
2315 xisr
->sadb_x_ipsecrequest_len
= PFKEY_ALIGN8(xisrlen
);
2320 *policylen0
= policylen
;
2326 racoon_free(policy
);
2332 pk_sendspdupdate2(iph2
)
2333 struct ph2handle
*iph2
;
2335 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2336 caddr_t policy
= NULL
;
2338 u_int64_t ltime
, vtime
;
2340 ltime
= iph2
->approval
->lifetime
;
2343 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDUPDATE
, iph2
)) {
2344 plog(LLV_ERROR
, LOCATION
, NULL
,
2345 "getting sadb policy failed.\n");
2349 if (pfkey_send_spdupdate2(
2351 (struct sockaddr
*)&spidx
->src
,
2353 (struct sockaddr
*)&spidx
->dst
,
2357 policy
, policylen
, 0) < 0) {
2358 plog(LLV_ERROR
, LOCATION
, NULL
,
2359 "libipsec failed send spdupdate2 (%s)\n",
2363 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdupdate2\n");
2367 racoon_free(policy
);
2373 pk_recvspdupdate(mhp
)
2376 struct sadb_address
*saddr
, *daddr
;
2377 struct sadb_x_policy
*xpl
;
2378 struct policyindex spidx
;
2379 struct secpolicy
*sp
;
2383 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2384 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2385 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2386 plog(LLV_ERROR
, LOCATION
, NULL
,
2387 "inappropriate sadb spdupdate message passed.\n");
2390 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2391 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2392 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2394 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2395 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2398 saddr
->sadb_address_prefixlen
,
2399 daddr
->sadb_address_prefixlen
,
2400 saddr
->sadb_address_proto
,
2401 xpl
->sadb_x_policy_priority
,
2404 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2407 saddr
->sadb_address_prefixlen
,
2408 daddr
->sadb_address_prefixlen
,
2409 saddr
->sadb_address_proto
,
2415 plog(LLV_ERROR
, LOCATION
, NULL
,
2416 "such policy does not already exist: \"%s\"\n",
2423 if (addnewsp(mhp
) < 0)
2430 * this function has to be used by responder side.
2433 pk_sendspdadd2(iph2
)
2434 struct ph2handle
*iph2
;
2436 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2437 caddr_t policy
= NULL
;
2439 u_int64_t ltime
, vtime
;
2441 ltime
= iph2
->approval
->lifetime
;
2444 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDADD
, iph2
)) {
2445 plog(LLV_ERROR
, LOCATION
, NULL
,
2446 "getting sadb policy failed.\n");
2450 if (pfkey_send_spdadd2(
2452 (struct sockaddr
*)&spidx
->src
,
2454 (struct sockaddr
*)&spidx
->dst
,
2458 policy
, policylen
, 0) < 0) {
2459 plog(LLV_ERROR
, LOCATION
, NULL
,
2460 "libipsec failed send spdadd2 (%s)\n",
2464 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdadd2\n");
2468 racoon_free(policy
);
2477 struct sadb_address
*saddr
, *daddr
;
2478 struct sadb_x_policy
*xpl
;
2479 struct policyindex spidx
;
2480 struct secpolicy
*sp
;
2484 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2485 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2486 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2487 plog(LLV_ERROR
, LOCATION
, NULL
,
2488 "inappropriate sadb spdadd message passed.\n");
2491 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2492 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2493 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2495 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2496 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2499 saddr
->sadb_address_prefixlen
,
2500 daddr
->sadb_address_prefixlen
,
2501 saddr
->sadb_address_proto
,
2502 xpl
->sadb_x_policy_priority
,
2505 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2508 saddr
->sadb_address_prefixlen
,
2509 daddr
->sadb_address_prefixlen
,
2510 saddr
->sadb_address_proto
,
2516 plog(LLV_ERROR
, LOCATION
, NULL
,
2517 "such policy already exists. "
2518 "anyway replace it: %s\n",
2524 if (addnewsp(mhp
) < 0)
2531 * this function has to be used by responder side.
2534 pk_sendspddelete(iph2
)
2535 struct ph2handle
*iph2
;
2537 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2538 caddr_t policy
= NULL
;
2541 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDDELETE
, iph2
)) {
2542 plog(LLV_ERROR
, LOCATION
, NULL
,
2543 "getting sadb policy failed.\n");
2547 if (pfkey_send_spddelete(
2549 (struct sockaddr
*)&spidx
->src
,
2551 (struct sockaddr
*)&spidx
->dst
,
2554 policy
, policylen
, 0) < 0) {
2555 plog(LLV_ERROR
, LOCATION
, NULL
,
2556 "libipsec failed send spddelete (%s)\n",
2560 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spddelete\n");
2564 racoon_free(policy
);
2570 pk_recvspddelete(mhp
)
2573 struct sadb_address
*saddr
, *daddr
;
2574 struct sadb_x_policy
*xpl
;
2575 struct policyindex spidx
;
2576 struct secpolicy
*sp
;
2580 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2581 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2582 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2583 plog(LLV_ERROR
, LOCATION
, NULL
,
2584 "inappropriate sadb spddelete message passed.\n");
2587 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2588 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2589 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2591 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2592 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2595 saddr
->sadb_address_prefixlen
,
2596 daddr
->sadb_address_prefixlen
,
2597 saddr
->sadb_address_proto
,
2598 xpl
->sadb_x_policy_priority
,
2601 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2604 saddr
->sadb_address_prefixlen
,
2605 daddr
->sadb_address_prefixlen
,
2606 saddr
->sadb_address_proto
,
2612 plog(LLV_ERROR
, LOCATION
, NULL
,
2613 "no policy found: %s\n",
2618 purgephXbyspid(xpl
->sadb_x_policy_id
, true);
2627 pk_recvspdexpire(mhp
)
2630 struct sadb_address
*saddr
, *daddr
;
2631 struct sadb_x_policy
*xpl
;
2632 struct policyindex spidx
;
2633 struct secpolicy
*sp
;
2637 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2638 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2639 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2640 plog(LLV_ERROR
, LOCATION
, NULL
,
2641 "inappropriate sadb spdexpire message passed.\n");
2644 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2645 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2646 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2648 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2649 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2652 saddr
->sadb_address_prefixlen
,
2653 daddr
->sadb_address_prefixlen
,
2654 saddr
->sadb_address_proto
,
2655 xpl
->sadb_x_policy_priority
,
2658 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2661 saddr
->sadb_address_prefixlen
,
2662 daddr
->sadb_address_prefixlen
,
2663 saddr
->sadb_address_proto
,
2669 plog(LLV_ERROR
, LOCATION
, NULL
,
2670 "no policy found: %s\n",
2675 purgephXbyspid(xpl
->sadb_x_policy_id
, false);
2688 if (mhp
[0] == NULL
) {
2689 plog(LLV_ERROR
, LOCATION
, NULL
,
2690 "inappropriate sadb spdget message passed.\n");
2701 struct sadb_msg
*msg
;
2702 struct sadb_address
*saddr
, *daddr
;
2703 struct sadb_x_policy
*xpl
;
2704 struct policyindex spidx
;
2705 struct secpolicy
*sp
;
2708 if (mhp
[0] == NULL
) {
2709 plog(LLV_ERROR
, LOCATION
, NULL
,
2710 "inappropriate sadb spddump message passed.\n");
2713 msg
= (struct sadb_msg
*)mhp
[0];
2715 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2716 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2717 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2719 if (saddr
== NULL
|| daddr
== NULL
|| xpl
== NULL
) {
2720 plog(LLV_ERROR
, LOCATION
, NULL
,
2721 "inappropriate sadb spddump message passed.\n");
2725 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2726 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2729 saddr
->sadb_address_prefixlen
,
2730 daddr
->sadb_address_prefixlen
,
2731 saddr
->sadb_address_proto
,
2732 xpl
->sadb_x_policy_priority
,
2735 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2738 saddr
->sadb_address_prefixlen
,
2739 daddr
->sadb_address_prefixlen
,
2740 saddr
->sadb_address_proto
,
2746 plog(LLV_ERROR
, LOCATION
, NULL
,
2747 "such policy already exists. "
2748 "anyway replace it: %s\n",
2754 if (addnewsp(mhp
) < 0)
2761 pk_recvspdflush(mhp
)
2765 if (mhp
[0] == NULL
) {
2766 plog(LLV_ERROR
, LOCATION
, NULL
,
2767 "inappropriate sadb spdflush message passed.\n");
2779 * send error against acquire message to kenrel.
2782 pk_sendeacquire(iph2
)
2783 struct ph2handle
*iph2
;
2785 struct sadb_msg
*newmsg
;
2788 len
= sizeof(struct sadb_msg
);
2789 newmsg
= racoon_calloc(1, len
);
2790 if (newmsg
== NULL
) {
2791 plog(LLV_ERROR
, LOCATION
, NULL
,
2792 "failed to get buffer to send acquire.\n");
2796 memset(newmsg
, 0, len
);
2797 newmsg
->sadb_msg_version
= PF_KEY_V2
;
2798 newmsg
->sadb_msg_type
= SADB_ACQUIRE
;
2799 newmsg
->sadb_msg_errno
= ENOENT
; /* XXX */
2800 newmsg
->sadb_msg_satype
= iph2
->satype
;
2801 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
2802 newmsg
->sadb_msg_reserved
= 0;
2803 newmsg
->sadb_msg_seq
= iph2
->seq
;
2804 newmsg
->sadb_msg_pid
= (u_int32_t
)getpid();
2807 len
= pfkey_send(lcconf
->sock_pfkey
, newmsg
, len
);
2809 racoon_free(newmsg
);
2815 pk_sendget_inbound_sastats(ike_session_t
*session
)
2817 u_int32_t max_stats
;
2821 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2825 session
->traffic_monitor
.num_in_curr_req
= 0;
2826 bzero(session
->traffic_monitor
.in_curr_req
, sizeof(session
->traffic_monitor
.in_curr_req
));
2827 max_stats
= (sizeof(session
->traffic_monitor
.in_curr_req
) / sizeof(session
->traffic_monitor
.in_curr_req
[0]));
2830 if ((session
->traffic_monitor
.num_in_curr_req
= ike_session_get_sas_for_stats(session
,
2833 session
->traffic_monitor
.in_curr_req
,
2835 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2837 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2839 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2844 session
->traffic_monitor
.in_curr_req
,
2845 session
->traffic_monitor
.num_in_curr_req
) < 0) {
2848 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2850 return session
->traffic_monitor
.num_in_curr_req
;
2856 pk_sendget_outbound_sastats(ike_session_t
*session
)
2858 u_int32_t max_stats
;
2862 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2866 session
->traffic_monitor
.num_out_curr_req
= 0;
2867 bzero(session
->traffic_monitor
.out_curr_req
, sizeof(session
->traffic_monitor
.out_curr_req
));
2868 max_stats
= (sizeof(session
->traffic_monitor
.out_curr_req
) / sizeof(session
->traffic_monitor
.out_curr_req
[0]));
2871 if ((session
->traffic_monitor
.num_out_curr_req
= ike_session_get_sas_for_stats(session
,
2874 session
->traffic_monitor
.out_curr_req
,
2876 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2878 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2880 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2885 session
->traffic_monitor
.out_curr_req
,
2886 session
->traffic_monitor
.num_out_curr_req
) < 0) {
2889 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2891 return session
->traffic_monitor
.num_out_curr_req
;
2897 * receive GETSPDSTAT from kernel.
2900 pk_recvgetsastat(mhp
)
2903 struct sadb_msg
*msg
;
2904 struct sadb_session_id
*session_id
;
2905 struct sadb_sastat
*stat_resp
;
2906 ike_session_t
*session
;
2908 /* validity check */
2909 if (mhp
[0] == NULL
||
2910 mhp
[SADB_EXT_SESSION_ID
] == NULL
||
2911 mhp
[SADB_EXT_SASTAT
] == NULL
) {
2912 plog(LLV_ERROR
, LOCATION
, NULL
,
2913 "inappropriate sadb getsastat response.\n");
2916 msg
= (struct sadb_msg
*)mhp
[0];
2917 session_id
= (ike_session_t
*)mhp
[SADB_EXT_SESSION_ID
];
2918 stat_resp
= (struct sadb_sastat
*)mhp
[SADB_EXT_SASTAT
];
2920 /* the message has to be processed or not ? */
2921 if (msg
->sadb_msg_pid
!= getpid()) {
2922 plog(LLV_DEBUG
, LOCATION
, NULL
,
2923 "%s message is not interesting "
2924 "because pid %d is not mine.\n",
2925 s_pfkey_type(msg
->sadb_msg_type
),
2929 if (!session_id
->sadb_session_id_v
[0]) {
2930 plog(LLV_DEBUG
, LOCATION
, NULL
,
2931 "%s message is bad "
2932 "because session-id[0] is invalid.\n",
2933 s_pfkey_type(msg
->sadb_msg_type
));
2936 session
= (__typeof__(session
))session_id
->sadb_session_id_v
[0];
2938 if (!stat_resp
->sadb_sastat_list_len
) {
2939 plog(LLV_DEBUG
, LOCATION
, NULL
,
2940 "%s message is bad "
2941 "because it has no sastats.\n",
2942 s_pfkey_type(msg
->sadb_msg_type
));
2946 ike_session_update_traffic_idle_status(session
,
2947 stat_resp
->sadb_sastat_dir
,
2948 (struct sastat
*)(stat_resp
+ 1),
2949 stat_resp
->sadb_sastat_list_len
);
2954 * check if the algorithm is supported or not.
2959 pk_checkalg(class, calg
, keylen
)
2960 int class, calg
, keylen
;
2964 struct sadb_alg alg0
;
2966 switch (algclass2doi(class)) {
2967 case IPSECDOI_PROTO_IPSEC_ESP
:
2968 sup
= SADB_EXT_SUPPORTED_ENCRYPT
;
2970 case IPSECDOI_ATTR_AUTH
:
2971 sup
= SADB_EXT_SUPPORTED_AUTH
;
2973 case IPSECDOI_PROTO_IPCOMP
:
2974 plog(LLV_DEBUG
, LOCATION
, NULL
,
2975 "compression algorithm can not be checked "
2976 "because sadb message doesn't support it.\n");
2979 plog(LLV_ERROR
, LOCATION
, NULL
,
2980 "invalid algorithm class.\n");
2983 alg
= ipsecdoi2pfkey_alg(algclass2doi(class), algtype2doi(class, calg
));
2988 if (ipsec_get_keylen(sup
, alg
, &alg0
)) {
2989 plog(LLV_ERROR
, LOCATION
, NULL
,
2990 "%s.\n", ipsec_strerror());
2993 keylen
= alg0
.sadb_alg_minbits
;
2996 error
= ipsec_check_keylen(sup
, alg
, keylen
);
2998 plog(LLV_ERROR
, LOCATION
, NULL
,
2999 "%s.\n", ipsec_strerror());
3005 * differences with pfkey_recv() in libipsec/pfkey.c:
3006 * - never performs busy wait loop.
3007 * - returns NULL and set *lenp to negative on fatal failures
3008 * - returns NULL and set *lenp to non-negative on non-fatal failures
3009 * - returns non-NULL on success
3011 static struct sadb_msg
*
3016 struct sadb_msg
*newmsg
;
3018 socklen_t optlen
= sizeof(reallen
);
3020 if (getsockopt(so
, SOL_SOCKET
, SO_NREAD
, &reallen
, &optlen
) < 0)
3021 return NULL
; /*fatal*/
3026 if ((newmsg
= racoon_calloc(1, reallen
)) == NULL
)
3029 *lenp
= recv(so
, (caddr_t
)newmsg
, reallen
, 0);
3031 racoon_free(newmsg
);
3032 return NULL
; /*fatal*/
3033 } else if (*lenp
!= reallen
|| *lenp
< sizeof(struct sadb_msg
)) {
3034 racoon_free(newmsg
);
3045 return eay_random();
3052 struct secpolicy
*new;
3053 struct sadb_address
*saddr
, *daddr
;
3054 struct sadb_x_policy
*xpl
;
3057 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
3058 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
3059 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
3060 plog(LLV_ERROR
, LOCATION
, NULL
,
3061 "inappropriate sadb spd management message passed.\n");
3065 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
3066 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
3067 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
3070 /* bsd skips over per-socket policies because there will be no
3071 * src and dst extensions in spddump messages. On Linux the only
3072 * way to achieve the same is check for policy id.
3074 if (xpl
->sadb_x_policy_id
% 8 >= 3) return 0;
3079 plog(LLV_ERROR
, LOCATION
, NULL
,
3080 "failed to allocate buffer\n");
3084 new->spidx
.dir
= xpl
->sadb_x_policy_dir
;
3085 new->id
= xpl
->sadb_x_policy_id
;
3086 new->policy
= xpl
->sadb_x_policy_type
;
3090 switch (xpl
->sadb_x_policy_type
) {
3091 case IPSEC_POLICY_DISCARD
:
3092 case IPSEC_POLICY_GENERATE
:
3093 case IPSEC_POLICY_NONE
:
3094 case IPSEC_POLICY_ENTRUST
:
3095 case IPSEC_POLICY_BYPASS
:
3098 case IPSEC_POLICY_IPSEC
:
3101 struct sadb_x_ipsecrequest
*xisr
;
3102 struct ipsecrequest
**p_isr
= &new->req
;
3104 /* validity check */
3105 if (PFKEY_EXTLEN(xpl
) < sizeof(*xpl
)) {
3106 plog(LLV_ERROR
, LOCATION
, NULL
,
3107 "invalid msg length.\n");
3111 tlen
= PFKEY_EXTLEN(xpl
) - sizeof(*xpl
);
3112 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
3117 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
3118 plog(LLV_ERROR
, LOCATION
, NULL
,
3119 "invalid msg length.\n");
3123 /* allocate request buffer */
3124 *p_isr
= newipsecreq();
3125 if (*p_isr
== NULL
) {
3126 plog(LLV_ERROR
, LOCATION
, NULL
,
3127 "failed to get new ipsecreq.\n");
3132 (*p_isr
)->next
= NULL
;
3134 switch (xisr
->sadb_x_ipsecrequest_proto
) {
3137 case IPPROTO_IPCOMP
:
3140 plog(LLV_ERROR
, LOCATION
, NULL
,
3141 "invalid proto type: %u\n",
3142 xisr
->sadb_x_ipsecrequest_proto
);
3145 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
3147 switch (xisr
->sadb_x_ipsecrequest_mode
) {
3148 case IPSEC_MODE_TRANSPORT
:
3149 case IPSEC_MODE_TUNNEL
:
3151 case IPSEC_MODE_ANY
:
3153 plog(LLV_ERROR
, LOCATION
, NULL
,
3154 "invalid mode: %u\n",
3155 xisr
->sadb_x_ipsecrequest_mode
);
3158 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
3160 switch (xisr
->sadb_x_ipsecrequest_level
) {
3161 case IPSEC_LEVEL_DEFAULT
:
3162 case IPSEC_LEVEL_USE
:
3163 case IPSEC_LEVEL_REQUIRE
:
3165 case IPSEC_LEVEL_UNIQUE
:
3166 (*p_isr
)->saidx
.reqid
=
3167 xisr
->sadb_x_ipsecrequest_reqid
;
3171 plog(LLV_ERROR
, LOCATION
, NULL
,
3172 "invalid level: %u\n",
3173 xisr
->sadb_x_ipsecrequest_level
);
3176 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
3178 /* set IP addresses if there */
3179 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
3180 struct sockaddr
*paddr
;
3182 paddr
= (struct sockaddr
*)(xisr
+ 1);
3183 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
3184 sysdep_sa_len(paddr
));
3186 paddr
= (struct sockaddr
*)((caddr_t
)paddr
3187 + sysdep_sa_len(paddr
));
3188 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
3189 sysdep_sa_len(paddr
));
3194 /* initialization for the next. */
3195 p_isr
= &(*p_isr
)->next
;
3196 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
3198 /* validity check */
3200 plog(LLV_ERROR
, LOCATION
, NULL
,
3201 "becoming tlen < 0\n");
3204 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
3205 + xisr
->sadb_x_ipsecrequest_len
);
3210 plog(LLV_ERROR
, LOCATION
, NULL
,
3211 "invalid policy type.\n");
3215 #ifdef HAVE_PFKEY_POLICY_PRIORITY
3216 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3219 saddr
->sadb_address_prefixlen
,
3220 daddr
->sadb_address_prefixlen
,
3221 saddr
->sadb_address_proto
,
3222 xpl
->sadb_x_policy_priority
,
3225 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3228 saddr
->sadb_address_prefixlen
,
3229 daddr
->sadb_address_prefixlen
,
3230 saddr
->sadb_address_proto
,
3239 /* proto/mode/src->dst spi */
3241 sadbsecas2str(src
, dst
, proto
, spi
, mode
)
3242 struct sockaddr
*src
, *dst
;
3247 static char buf
[256];
3248 u_int doi_proto
, doi_mode
= 0;
3252 doi_proto
= pfkey2ipsecdoi_proto(proto
);
3253 if (doi_proto
== ~0)
3256 doi_mode
= pfkey2ipsecdoi_mode(mode
);
3261 blen
= sizeof(buf
) - 1;
3264 i
= snprintf(p
, blen
, "%s%s%s ",
3265 s_ipsecdoi_proto(doi_proto
),
3267 mode
? s_ipsecdoi_encmode(doi_mode
) : "");
3268 if (i
< 0 || i
>= blen
)
3273 i
= snprintf(p
, blen
, "%s->", saddr2str(src
));
3274 if (i
< 0 || i
>= blen
)
3279 i
= snprintf(p
, blen
, "%s ", saddr2str(dst
));
3280 if (i
< 0 || i
>= blen
)
3286 snprintf(p
, blen
, "spi=%lu(0x%lx)", (unsigned long)ntohl(spi
),
3287 (unsigned long)ntohl(spi
));