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
));
1416 iph2
->ph1
->ph2cnt
++;
1418 /* turn off schedule */
1420 SCHED_KILL(iph2
->scr
);
1423 * since we are going to reuse the phase2 handler, we need to
1424 * remain it and refresh all the references between ph1 and ph2 to use.
1428 iph2
->sce
= sched_new(iph2
->approval
->lifetime
,
1429 isakmp_ph2expire_stub
, iph2
);
1431 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1440 struct ph2handle
*iph2
;
1443 struct sockaddr
*src
= NULL
, *dst
= NULL
;
1444 u_int e_type
, e_keylen
, a_type
, a_keylen
, flags
;
1446 u_int64_t lifebyte
= 0;
1447 u_int wsize
= 4; /* XXX static size of window */
1449 struct ph2natt natt
;
1452 if (iph2
->approval
== NULL
) {
1453 plog(LLV_ERROR
, LOCATION
, NULL
,
1454 "no approvaled SAs found.\n");
1457 if (iph2
->side
== INITIATOR
)
1458 proxy
= iph2
->ph1
->rmconf
->support_proxy
;
1459 else if (iph2
->sainfo
&& iph2
->sainfo
->id_i
)
1462 /* for mobile IPv6 */
1463 if (proxy
&& iph2
->src_id
&& iph2
->dst_id
&&
1464 ipsecdoi_transportmode(iph2
->approval
)) {
1472 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1473 /* validity check */
1474 satype
= ipsecdoi2pfkey_proto(pr
->proto_id
);
1476 plog(LLV_ERROR
, LOCATION
, NULL
,
1477 "invalid proto_id %d\n", pr
->proto_id
);
1480 else if (satype
== SADB_X_SATYPE_IPCOMP
) {
1481 /* no replay window for IPCOMP */
1484 #ifdef ENABLE_SAMODE_UNSPECIFIED
1485 mode
= IPSEC_MODE_ANY
;
1487 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
1489 plog(LLV_ERROR
, LOCATION
, NULL
,
1490 "invalid encmode %d\n", pr
->encmode
);
1495 /* set algorithm type and key length */
1496 e_keylen
= pr
->head
->encklen
;
1497 if (pfkey_convertfromipsecdoi(
1502 &a_type
, &a_keylen
, &flags
) < 0)
1506 lifebyte
= iph2
->approval
->lifebyte
* 1024,
1513 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1515 if (pr
->udp_encap
) {
1516 memset (&natt
, 0, sizeof (natt
));
1517 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1518 flags
|= SADB_X_EXT_NATT
;
1519 if (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) {
1520 if (iph2
->ph1
->rmconf
->natt_keepalive
== TRUE
)
1521 flags
|= SADB_X_EXT_NATT_KEEPALIVE
;
1523 else if (iph2
->ph1
->rmconf
->natt_multiple_user
== TRUE
&&
1524 mode
== IPSEC_MODE_TRANSPORT
&&
1525 dst
->sa_family
== AF_INET
)
1526 flags
|= SADB_X_EXT_NATT_MULTIPLEUSERS
;
1528 memset (&natt
, 0, sizeof (natt
));
1530 /* Remove port information, that SA doesn't use it */
1545 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1546 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1547 iph2
->seq
,natt
.dport
) < 0) {
1548 plog(LLV_ERROR
, LOCATION
, NULL
,
1549 "libipsec failed send add (%s)\n",
1554 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1556 /* Remove port information, it is not used without NAT-T */
1570 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1571 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1572 iph2
->seq
, 0) < 0) {
1573 plog(LLV_ERROR
, LOCATION
, NULL
,
1574 "libipsec failed send add (%s)\n",
1578 #endif /* ENABLE_NATT */
1579 #else /* __APPLE__ */
1581 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add_nat\n");
1583 if (pr
->udp_encap
) {
1584 memset (&natt
, 0, sizeof (natt
));
1585 natt
.type
= UDP_ENCAP_ESPINUDP
;
1586 natt
.sport
= extract_port (iph2
->ph1
->local
);
1587 natt
.dport
= extract_port (iph2
->ph1
->remote
);
1588 natt
.oa
= NULL
; // FIXME: Here comes OA!!!
1589 natt
.frag
= iph2
->ph1
->rmconf
->esp_frag
;
1591 memset (&natt
, 0, sizeof (natt
));
1593 /* Remove port information, that SA doesn't use it */
1598 if (pfkey_send_add_nat(
1608 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1609 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1611 natt
.type
, natt
.sport
, natt
.dport
, natt
.oa
,
1613 plog(LLV_ERROR
, LOCATION
, NULL
,
1614 "libipsec failed send add_nat (%s)\n",
1619 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_add\n");
1621 /* Remove port information, it is not used without NAT-T */
1635 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1636 0, lifebyte
, iph2
->approval
->lifetime
, 0,
1638 plog(LLV_ERROR
, LOCATION
, NULL
,
1639 "libipsec failed send add (%s)\n",
1643 #endif /* ENABLE_NATT */
1644 #endif /* __APPLE__ */
1646 if (!lcconf
->pathinfo
[LC_PATHTYPE_BACKUPSA
])
1650 * It maybe good idea to call backupsa_to_file() after
1651 * racoon will receive the sadb_update messages.
1652 * But it is impossible because there is not key in the
1653 * information from the kernel.
1655 if (backupsa_to_file(satype
, mode
, src
, dst
,
1656 pr
->spi_p
, pr
->reqid_out
, 4,
1658 e_type
, e_keylen
, a_type
, a_keylen
, flags
,
1659 0, iph2
->approval
->lifebyte
* 1024,
1660 iph2
->approval
->lifetime
, 0,
1662 plog(LLV_ERROR
, LOCATION
, NULL
,
1663 "backuped SA failed: %s\n",
1664 sadbsecas2str(src
, dst
,
1665 satype
, pr
->spi_p
, mode
));
1667 plog(LLV_DEBUG
, LOCATION
, NULL
,
1668 "backuped SA: %s\n",
1669 sadbsecas2str(src
, dst
,
1670 satype
, pr
->spi_p
, mode
));
1680 struct sadb_msg
*msg
;
1682 struct sockaddr
*src
, *dst
;
1683 struct ph2handle
*iph2
;
1686 /* ignore this message because of local test mode. */
1692 || mhp
[SADB_EXT_SA
] == NULL
1693 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1694 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1695 plog(LLV_ERROR
, LOCATION
, NULL
,
1696 "inappropriate sadb add message passed.\n");
1699 msg
= (struct sadb_msg
*)mhp
[0];
1700 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1701 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1702 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1704 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1706 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1708 /* the message has to be processed or not ? */
1709 if (msg
->sadb_msg_pid
!= getpid()) {
1710 plog(LLV_DEBUG
, LOCATION
, NULL
,
1711 "%s message is not interesting "
1712 "because pid %d is not mine.\n",
1713 s_pfkey_type(msg
->sadb_msg_type
),
1718 iph2
= getph2byseq(msg
->sadb_msg_seq
);
1720 plog(LLV_DEBUG
, LOCATION
, NULL
,
1721 "seq %d of %s message not interesting.\n",
1723 s_pfkey_type(msg
->sadb_msg_type
));
1728 * NOTE don't update any status of phase2 handle
1729 * because they must be updated by SADB_UPDATE message
1732 plog(LLV_INFO
, LOCATION
, NULL
,
1733 "IPsec-SA established: %s\n",
1734 sadbsecas2str(iph2
->src
, iph2
->dst
,
1735 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1737 ike_session_cleanup_other_established_ph2s(iph2
->parent_session
, iph2
);
1739 #ifdef ENABLE_VPNCONTROL_PORT
1743 if (iph2
->dst
->sa_family
== AF_INET
)
1744 address
= ((struct sockaddr_in
*)iph2
->dst
)->sin_addr
.s_addr
;
1747 vpncontrol_notify_phase_change(0, FROM_LOCAL
, NULL
, iph2
);
1751 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1759 struct sadb_msg
*msg
;
1761 struct sockaddr
*src
, *dst
;
1762 struct ph2handle
*iph2
;
1763 u_int proto_id
, sa_mode
;
1767 || mhp
[SADB_EXT_SA
] == NULL
1768 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1769 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1770 || (mhp
[SADB_EXT_LIFETIME_HARD
] != NULL
1771 && mhp
[SADB_EXT_LIFETIME_SOFT
] != NULL
)) {
1772 plog(LLV_ERROR
, LOCATION
, NULL
,
1773 "inappropriate sadb expire message passed.\n");
1776 msg
= (struct sadb_msg
*)mhp
[0];
1777 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1778 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1779 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1781 sa_mode
= mhp
[SADB_X_EXT_SA2
] == NULL
1783 : ((struct sadb_x_sa2
*)mhp
[SADB_X_EXT_SA2
])->sadb_x_sa2_mode
;
1785 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1786 if (proto_id
== ~0) {
1787 plog(LLV_ERROR
, LOCATION
, NULL
,
1788 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
1792 plog(LLV_INFO
, LOCATION
, NULL
,
1793 "IPsec-SA expired: %s\n",
1794 sadbsecas2str(src
, dst
,
1795 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, sa_mode
));
1797 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1800 * Ignore it because two expire messages are come up.
1801 * phase2 handler has been deleted already when 2nd message
1804 plog(LLV_DEBUG
, LOCATION
, NULL
,
1805 "no such a SA found: %s\n",
1806 sadbsecas2str(src
, dst
,
1807 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
,
1811 if (iph2
->status
!= PHASE2ST_ESTABLISHED
) {
1813 * If the status is not equal to PHASE2ST_ESTABLISHED,
1814 * racoon ignores this expire message. There are two reason.
1815 * One is that the phase 2 probably starts because there is
1816 * a potential that racoon receives the acquire message
1817 * without receiving a expire message. Another is that racoon
1818 * may receive the multiple expire messages from the kernel.
1820 plog(LLV_WARNING
, LOCATION
, NULL
,
1821 "the expire message is received "
1822 "but the handler has not been established.\n");
1826 /* turn off the timer for calling isakmp_ph2expire() */
1827 SCHED_KILL(iph2
->sce
);
1829 iph2
->status
= PHASE2ST_EXPIRED
;
1831 /* INITIATOR, begin phase 2 exchange only if there's no other established ph2. */
1832 /* allocate buffer for status management of pfkey message */
1833 if (iph2
->side
== INITIATOR
&&
1834 !ike_session_has_other_established_ph2(iph2
->parent_session
, iph2
)) {
1838 /* update status for re-use */
1839 iph2
->status
= PHASE2ST_STATUS2
;
1841 /* start isakmp initiation by using ident exchange */
1842 if (isakmp_post_acquire(iph2
) < 0) {
1843 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
1844 "failed to begin ipsec sa "
1845 "re-negotiation.\n");
1856 /* If not received SADB_EXPIRE, INITIATOR delete ph2handle. */
1857 /* RESPONDER always delete ph2handle, keep silent. RESPONDER doesn't
1858 * manage IPsec SA, so delete the list */
1870 struct sadb_msg
*msg
;
1871 struct sadb_x_policy
*xpl
;
1872 struct secpolicy
*sp_out
= NULL
, *sp_in
= NULL
;
1873 #define MAXNESTEDSA 5 /* XXX */
1874 struct ph2handle
*iph2
[MAXNESTEDSA
];
1875 struct sockaddr
*src
, *dst
;
1876 int n
; /* # of phase 2 handler */
1878 /* ignore this message because of local test mode. */
1884 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1885 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
1886 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
1887 plog(LLV_ERROR
, LOCATION
, NULL
,
1888 "inappropriate sadb acquire message passed.\n");
1891 msg
= (struct sadb_msg
*)mhp
[0];
1892 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
1893 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1894 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1896 /* ignore if type is not IPSEC_POLICY_IPSEC */
1897 if (xpl
->sadb_x_policy_type
!= IPSEC_POLICY_IPSEC
) {
1898 plog(LLV_DEBUG
, LOCATION
, NULL
,
1899 "ignore ACQUIRE message. type is not IPsec.\n");
1903 /* ignore it if src is multicast address */
1905 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1907 if ((sa
->sa_family
== AF_INET
1908 && IN_MULTICAST(ntohl(((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
)))
1910 || (sa
->sa_family
== AF_INET6
1911 && IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)sa
)->sin6_addr
))
1914 plog(LLV_DEBUG
, LOCATION
, NULL
,
1915 "ignore due to multicast address: %s.\n",
1921 /* ignore, if we do not listen on source address */
1924 * - if we'll contact peer from address we do not listen -
1925 * we will be unable to complete negotiation;
1926 * - if we'll negotiate using address we're listening -
1927 * remote peer will send packets to address different
1928 * than one in the policy, so kernel will drop them;
1929 * => therefore this acquire is not for us! --Aidas
1931 struct sockaddr
*sa
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1934 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1935 if (!cmpsaddrwop(p
->addr
, sa
)) {
1942 plog(LLV_DEBUG
, LOCATION
, NULL
,
1943 "ignore because do not listen on source address : %s.\n",
1950 * If there is a phase 2 handler against the policy identifier in
1951 * the acquire message, and if
1952 * 1. its state is less than PHASE2ST_ESTABLISHED, then racoon
1953 * should ignore such a acquire message because the phase 2
1954 * is just negotiating.
1955 * 2. its state is equal to PHASE2ST_ESTABLISHED, then racoon
1956 * has to process such a acquire message because racoon may
1957 * have lost the expire message.
1959 iph2
[0] = getph2byid(src
, dst
, xpl
->sadb_x_policy_id
);
1960 if (iph2
[0] != NULL
) {
1961 if (iph2
[0]->status
< PHASE2ST_ESTABLISHED
) {
1962 plog(LLV_DEBUG
, LOCATION
, NULL
,
1963 "ignore the acquire because ph2 found\n");
1966 if (iph2
[0]->status
== PHASE2ST_EXPIRED
)
1971 /* search for proper policyindex */
1972 sp_out
= getspbyspid(xpl
->sadb_x_policy_id
);
1973 if (sp_out
== NULL
) {
1974 plog(LLV_ERROR
, LOCATION
, NULL
, "no policy found: id:%d.\n",
1975 xpl
->sadb_x_policy_id
);
1978 plog(LLV_DEBUG
, LOCATION
, NULL
,
1979 "suitable outbound SP found: %s.\n", spidx2str(&sp_out
->spidx
));
1981 /* get inbound policy */
1983 struct policyindex spidx
;
1985 spidx
.dir
= IPSEC_DIR_INBOUND
;
1986 memcpy(&spidx
.src
, &sp_out
->spidx
.dst
, sizeof(spidx
.src
));
1987 memcpy(&spidx
.dst
, &sp_out
->spidx
.src
, sizeof(spidx
.dst
));
1988 spidx
.prefs
= sp_out
->spidx
.prefd
;
1989 spidx
.prefd
= sp_out
->spidx
.prefs
;
1990 spidx
.ul_proto
= sp_out
->spidx
.ul_proto
;
1992 sp_in
= getsp(&spidx
);
1994 plog(LLV_DEBUG
, LOCATION
, NULL
,
1995 "suitable inbound SP found: %s.\n",
1996 spidx2str(&sp_in
->spidx
));
1998 plog(LLV_NOTIFY
, LOCATION
, NULL
,
1999 "no in-bound policy found: %s\n",
2004 memset(iph2
, 0, MAXNESTEDSA
);
2008 /* allocate a phase 2 */
2010 if (iph2
[n
] == NULL
) {
2011 plog(LLV_ERROR
, LOCATION
, NULL
,
2012 "failed to allocate phase2 entry.\n");
2015 iph2
[n
]->side
= INITIATOR
;
2016 iph2
[n
]->spid
= xpl
->sadb_x_policy_id
;
2017 iph2
[n
]->satype
= msg
->sadb_msg_satype
;
2018 iph2
[n
]->seq
= msg
->sadb_msg_seq
;
2019 iph2
[n
]->status
= PHASE2ST_STATUS2
;
2021 /* set end addresses of SA */
2022 iph2
[n
]->dst
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]));
2023 if (iph2
[n
]->dst
== NULL
) {
2027 iph2
[n
]->src
= dupsaddr(PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]));
2028 if (iph2
[n
]->src
== NULL
) {
2033 plog(LLV_DEBUG
, LOCATION
, NULL
,
2034 "new acquire %s\n", spidx2str(&sp_out
->spidx
));
2038 vchar_t
*idsrc
, *iddst
;
2040 idsrc
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.src
,
2041 sp_out
->spidx
.prefs
, sp_out
->spidx
.ul_proto
);
2042 if (idsrc
== NULL
) {
2043 plog(LLV_ERROR
, LOCATION
, NULL
,
2044 "failed to get ID for %s\n",
2045 spidx2str(&sp_out
->spidx
));
2049 iddst
= ipsecdoi_sockaddr2id((struct sockaddr
*)&sp_out
->spidx
.dst
,
2050 sp_out
->spidx
.prefd
, sp_out
->spidx
.ul_proto
);
2051 if (iddst
== NULL
) {
2052 plog(LLV_ERROR
, LOCATION
, NULL
,
2053 "failed to get ID for %s\n",
2054 spidx2str(&sp_out
->spidx
));
2059 iph2
[n
]->sainfo
= getsainfo(idsrc
, iddst
, NULL
, 0);
2062 if (iph2
[n
]->sainfo
== NULL
) {
2063 plog(LLV_ERROR
, LOCATION
, NULL
,
2064 "failed to get sainfo.\n");
2067 /* XXX should use the algorithm list from register message */
2070 if (link_sainfo_to_ph2(iph2
[n
]->sainfo
) != 0) {
2071 plog(LLV_ERROR
, LOCATION
, NULL
,
2072 "failed to link sainfo\n");
2073 iph2
[n
]->sainfo
= NULL
;
2080 if (set_proposal_from_policy(iph2
[n
], sp_out
, sp_in
) < 0) {
2081 plog(LLV_ERROR
, LOCATION
, NULL
,
2082 "failed to create saprop.\n");
2088 /* start isakmp initiation by using ident exchange */
2089 /* XXX should be looped if there are multiple phase 2 handler. */
2090 if (isakmp_post_acquire(iph2
[n
]) < 0) {
2091 plog(LLV_ERROR
, LOCATION
, NULL
,
2092 "failed to begin ipsec sa negotiation.\n");
2096 #if !TARGET_OS_EMBEDDED
2097 if ( lcconf
->vt
== NULL
){
2098 if (!(lcconf
->vt
= vproc_transaction_begin(NULL
)))
2099 plog(LLV_ERROR
, LOCATION
, NULL
,
2100 "vproc_transaction_begin returns NULL.\n");
2109 unbindph12(iph2
[n
]);
2122 struct sadb_msg
*msg
;
2124 struct sockaddr
*src
, *dst
;
2125 struct ph2handle
*iph2
= NULL
;
2128 /* ignore this message because of local test mode. */
2134 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2135 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
2136 plog(LLV_ERROR
, LOCATION
, NULL
,
2137 "inappropriate sadb delete message passed.\n");
2140 msg
= (struct sadb_msg
*)mhp
[0];
2141 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
2142 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
2143 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
2145 /* the message has to be processed or not ? */
2146 if (msg
->sadb_msg_pid
== getpid()) {
2147 plog(LLV_DEBUG
, LOCATION
, NULL
,
2148 "%s message is not interesting "
2149 "because the message was originated by me.\n",
2150 s_pfkey_type(msg
->sadb_msg_type
));
2154 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
2155 if (proto_id
== ~0) {
2156 plog(LLV_ERROR
, LOCATION
, NULL
,
2157 "invalid proto_id %d\n", msg
->sadb_msg_satype
);
2161 plog(LLV_DEBUG2
, LOCATION
, NULL
, "SADB delete message: proto-id %d\n", proto_id
);
2162 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str(src
));
2163 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str(dst
));
2166 deleteallph2(src
, dst
, proto_id
);
2167 deleteallph1(src
, dst
);
2171 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
2174 plog(LLV_ERROR
, LOCATION
, NULL
,
2175 "no iph2 found: %s\n",
2176 sadbsecas2str(src
, dst
, msg
->sadb_msg_satype
,
2177 sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2181 plog(LLV_ERROR
, LOCATION
, NULL
,
2182 "pfkey DELETE received: %s\n",
2183 sadbsecas2str(iph2
->src
, iph2
->dst
,
2184 msg
->sadb_msg_satype
, sa
->sadb_sa_spi
, IPSEC_MODE_ANY
));
2186 /* send delete information */
2187 if (iph2
->status
== PHASE2ST_ESTABLISHED
)
2188 isakmp_info_send_d2(iph2
);
2190 ike_session_cleanup_ph1s_by_ph2(iph2
);
2202 /* ignore this message because of local test mode. */
2207 if (mhp
[0] == NULL
) {
2208 plog(LLV_ERROR
, LOCATION
, NULL
,
2209 "inappropriate sadb flush message passed.\n");
2220 getsadbpolicy(policy0
, policylen0
, type
, iph2
)
2222 int *policylen0
, type
;
2223 struct ph2handle
*iph2
;
2225 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2226 struct sadb_x_policy
*xpl
;
2227 struct sadb_x_ipsecrequest
*xisr
;
2234 /* get policy buffer size */
2235 policylen
= sizeof(struct sadb_x_policy
);
2236 if (type
!= SADB_X_SPDDELETE
) {
2237 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2238 xisrlen
= sizeof(*xisr
);
2239 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2240 xisrlen
+= (sysdep_sa_len(iph2
->src
)
2241 + sysdep_sa_len(iph2
->dst
));
2244 policylen
+= PFKEY_ALIGN8(xisrlen
);
2248 /* make policy structure */
2249 policy
= racoon_malloc(policylen
);
2251 plog(LLV_ERROR
, LOCATION
, NULL
,
2252 "buffer allocation failed.\n");
2256 xpl
= (struct sadb_x_policy
*)policy
;
2257 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(policylen
);
2258 xpl
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2259 xpl
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2260 xpl
->sadb_x_policy_dir
= spidx
->dir
;
2261 xpl
->sadb_x_policy_id
= 0;
2262 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2263 xpl
->sadb_x_policy_priority
= PRIORITY_DEFAULT
;
2266 /* no need to append policy information any more if type is SPDDELETE */
2267 if (type
== SADB_X_SPDDELETE
)
2270 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
2272 for (pr
= iph2
->approval
->head
; pr
; pr
= pr
->next
) {
2274 satype
= doi2ipproto(pr
->proto_id
);
2276 plog(LLV_ERROR
, LOCATION
, NULL
,
2277 "invalid proto_id %d\n", pr
->proto_id
);
2280 mode
= ipsecdoi2pfkey_mode(pr
->encmode
);
2282 plog(LLV_ERROR
, LOCATION
, NULL
,
2283 "invalid encmode %d\n", pr
->encmode
);
2288 * the policy level cannot be unique because the policy
2289 * is defined later than SA, so req_id cannot be bound to SA.
2291 xisr
->sadb_x_ipsecrequest_proto
= satype
;
2292 xisr
->sadb_x_ipsecrequest_mode
= mode
;
2293 xisr
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2294 xisr
->sadb_x_ipsecrequest_reqid
= 0;
2295 p
= (caddr_t
)(xisr
+ 1);
2297 xisrlen
= sizeof(*xisr
);
2299 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TUNNEL
) {
2300 int src_len
, dst_len
;
2302 src_len
= sysdep_sa_len(iph2
->src
);
2303 dst_len
= sysdep_sa_len(iph2
->dst
);
2304 xisrlen
+= src_len
+ dst_len
;
2306 memcpy(p
, iph2
->src
, src_len
);
2309 memcpy(p
, iph2
->dst
, dst_len
);
2313 xisr
->sadb_x_ipsecrequest_len
= PFKEY_ALIGN8(xisrlen
);
2318 *policylen0
= policylen
;
2324 racoon_free(policy
);
2330 pk_sendspdupdate2(iph2
)
2331 struct ph2handle
*iph2
;
2333 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2334 caddr_t policy
= NULL
;
2336 u_int64_t ltime
, vtime
;
2338 ltime
= iph2
->approval
->lifetime
;
2341 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDUPDATE
, iph2
)) {
2342 plog(LLV_ERROR
, LOCATION
, NULL
,
2343 "getting sadb policy failed.\n");
2347 if (pfkey_send_spdupdate2(
2349 (struct sockaddr
*)&spidx
->src
,
2351 (struct sockaddr
*)&spidx
->dst
,
2355 policy
, policylen
, 0) < 0) {
2356 plog(LLV_ERROR
, LOCATION
, NULL
,
2357 "libipsec failed send spdupdate2 (%s)\n",
2361 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdupdate2\n");
2365 racoon_free(policy
);
2371 pk_recvspdupdate(mhp
)
2374 struct sadb_address
*saddr
, *daddr
;
2375 struct sadb_x_policy
*xpl
;
2376 struct policyindex spidx
;
2377 struct secpolicy
*sp
;
2381 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2382 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2383 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2384 plog(LLV_ERROR
, LOCATION
, NULL
,
2385 "inappropriate sadb spdupdate message passed.\n");
2388 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2389 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2390 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2392 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2393 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2396 saddr
->sadb_address_prefixlen
,
2397 daddr
->sadb_address_prefixlen
,
2398 saddr
->sadb_address_proto
,
2399 xpl
->sadb_x_policy_priority
,
2402 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2405 saddr
->sadb_address_prefixlen
,
2406 daddr
->sadb_address_prefixlen
,
2407 saddr
->sadb_address_proto
,
2413 plog(LLV_ERROR
, LOCATION
, NULL
,
2414 "such policy does not already exist: \"%s\"\n",
2421 if (addnewsp(mhp
) < 0)
2428 * this function has to be used by responder side.
2431 pk_sendspdadd2(iph2
)
2432 struct ph2handle
*iph2
;
2434 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2435 caddr_t policy
= NULL
;
2437 u_int64_t ltime
, vtime
;
2439 ltime
= iph2
->approval
->lifetime
;
2442 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDADD
, iph2
)) {
2443 plog(LLV_ERROR
, LOCATION
, NULL
,
2444 "getting sadb policy failed.\n");
2448 if (pfkey_send_spdadd2(
2450 (struct sockaddr
*)&spidx
->src
,
2452 (struct sockaddr
*)&spidx
->dst
,
2456 policy
, policylen
, 0) < 0) {
2457 plog(LLV_ERROR
, LOCATION
, NULL
,
2458 "libipsec failed send spdadd2 (%s)\n",
2462 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spdadd2\n");
2466 racoon_free(policy
);
2475 struct sadb_address
*saddr
, *daddr
;
2476 struct sadb_x_policy
*xpl
;
2477 struct policyindex spidx
;
2478 struct secpolicy
*sp
;
2482 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2483 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2484 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2485 plog(LLV_ERROR
, LOCATION
, NULL
,
2486 "inappropriate sadb spdadd message passed.\n");
2489 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2490 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2491 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2493 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2494 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2497 saddr
->sadb_address_prefixlen
,
2498 daddr
->sadb_address_prefixlen
,
2499 saddr
->sadb_address_proto
,
2500 xpl
->sadb_x_policy_priority
,
2503 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2506 saddr
->sadb_address_prefixlen
,
2507 daddr
->sadb_address_prefixlen
,
2508 saddr
->sadb_address_proto
,
2514 plog(LLV_ERROR
, LOCATION
, NULL
,
2515 "such policy already exists. "
2516 "anyway replace it: %s\n",
2522 if (addnewsp(mhp
) < 0)
2529 * this function has to be used by responder side.
2532 pk_sendspddelete(iph2
)
2533 struct ph2handle
*iph2
;
2535 struct policyindex
*spidx
= (struct policyindex
*)iph2
->spidx_gen
;
2536 caddr_t policy
= NULL
;
2539 if (getsadbpolicy(&policy
, &policylen
, SADB_X_SPDDELETE
, iph2
)) {
2540 plog(LLV_ERROR
, LOCATION
, NULL
,
2541 "getting sadb policy failed.\n");
2545 if (pfkey_send_spddelete(
2547 (struct sockaddr
*)&spidx
->src
,
2549 (struct sockaddr
*)&spidx
->dst
,
2552 policy
, policylen
, 0) < 0) {
2553 plog(LLV_ERROR
, LOCATION
, NULL
,
2554 "libipsec failed send spddelete (%s)\n",
2558 plog(LLV_DEBUG
, LOCATION
, NULL
, "call pfkey_send_spddelete\n");
2562 racoon_free(policy
);
2568 pk_recvspddelete(mhp
)
2571 struct sadb_address
*saddr
, *daddr
;
2572 struct sadb_x_policy
*xpl
;
2573 struct policyindex spidx
;
2574 struct secpolicy
*sp
;
2578 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2579 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2580 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2581 plog(LLV_ERROR
, LOCATION
, NULL
,
2582 "inappropriate sadb spddelete message passed.\n");
2585 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2586 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2587 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2589 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2590 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2593 saddr
->sadb_address_prefixlen
,
2594 daddr
->sadb_address_prefixlen
,
2595 saddr
->sadb_address_proto
,
2596 xpl
->sadb_x_policy_priority
,
2599 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2602 saddr
->sadb_address_prefixlen
,
2603 daddr
->sadb_address_prefixlen
,
2604 saddr
->sadb_address_proto
,
2610 plog(LLV_ERROR
, LOCATION
, NULL
,
2611 "no policy found: %s\n",
2616 purgephXbyspid(xpl
->sadb_x_policy_id
, true);
2625 pk_recvspdexpire(mhp
)
2628 struct sadb_address
*saddr
, *daddr
;
2629 struct sadb_x_policy
*xpl
;
2630 struct policyindex spidx
;
2631 struct secpolicy
*sp
;
2635 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
2636 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
2637 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
2638 plog(LLV_ERROR
, LOCATION
, NULL
,
2639 "inappropriate sadb spdexpire message passed.\n");
2642 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2643 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2644 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2646 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2647 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2650 saddr
->sadb_address_prefixlen
,
2651 daddr
->sadb_address_prefixlen
,
2652 saddr
->sadb_address_proto
,
2653 xpl
->sadb_x_policy_priority
,
2656 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2659 saddr
->sadb_address_prefixlen
,
2660 daddr
->sadb_address_prefixlen
,
2661 saddr
->sadb_address_proto
,
2667 plog(LLV_ERROR
, LOCATION
, NULL
,
2668 "no policy found: %s\n",
2673 purgephXbyspid(xpl
->sadb_x_policy_id
, false);
2686 if (mhp
[0] == NULL
) {
2687 plog(LLV_ERROR
, LOCATION
, NULL
,
2688 "inappropriate sadb spdget message passed.\n");
2699 struct sadb_msg
*msg
;
2700 struct sadb_address
*saddr
, *daddr
;
2701 struct sadb_x_policy
*xpl
;
2702 struct policyindex spidx
;
2703 struct secpolicy
*sp
;
2706 if (mhp
[0] == NULL
) {
2707 plog(LLV_ERROR
, LOCATION
, NULL
,
2708 "inappropriate sadb spddump message passed.\n");
2711 msg
= (struct sadb_msg
*)mhp
[0];
2713 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
2714 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
2715 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
2717 if (saddr
== NULL
|| daddr
== NULL
|| xpl
== NULL
) {
2718 plog(LLV_ERROR
, LOCATION
, NULL
,
2719 "inappropriate sadb spddump message passed.\n");
2723 #ifdef HAVE_PFKEY_POLICY_PRIORITY
2724 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2727 saddr
->sadb_address_prefixlen
,
2728 daddr
->sadb_address_prefixlen
,
2729 saddr
->sadb_address_proto
,
2730 xpl
->sadb_x_policy_priority
,
2733 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
2736 saddr
->sadb_address_prefixlen
,
2737 daddr
->sadb_address_prefixlen
,
2738 saddr
->sadb_address_proto
,
2744 plog(LLV_ERROR
, LOCATION
, NULL
,
2745 "such policy already exists. "
2746 "anyway replace it: %s\n",
2752 if (addnewsp(mhp
) < 0)
2759 pk_recvspdflush(mhp
)
2763 if (mhp
[0] == NULL
) {
2764 plog(LLV_ERROR
, LOCATION
, NULL
,
2765 "inappropriate sadb spdflush message passed.\n");
2777 * send error against acquire message to kenrel.
2780 pk_sendeacquire(iph2
)
2781 struct ph2handle
*iph2
;
2783 struct sadb_msg
*newmsg
;
2786 len
= sizeof(struct sadb_msg
);
2787 newmsg
= racoon_calloc(1, len
);
2788 if (newmsg
== NULL
) {
2789 plog(LLV_ERROR
, LOCATION
, NULL
,
2790 "failed to get buffer to send acquire.\n");
2794 memset(newmsg
, 0, len
);
2795 newmsg
->sadb_msg_version
= PF_KEY_V2
;
2796 newmsg
->sadb_msg_type
= SADB_ACQUIRE
;
2797 newmsg
->sadb_msg_errno
= ENOENT
; /* XXX */
2798 newmsg
->sadb_msg_satype
= iph2
->satype
;
2799 newmsg
->sadb_msg_len
= PFKEY_UNIT64(len
);
2800 newmsg
->sadb_msg_reserved
= 0;
2801 newmsg
->sadb_msg_seq
= iph2
->seq
;
2802 newmsg
->sadb_msg_pid
= (u_int32_t
)getpid();
2805 len
= pfkey_send(lcconf
->sock_pfkey
, newmsg
, len
);
2807 racoon_free(newmsg
);
2813 pk_sendget_inbound_sastats(ike_session_t
*session
)
2815 u_int32_t max_stats
;
2819 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2823 session
->traffic_monitor
.num_in_curr_req
= 0;
2824 bzero(session
->traffic_monitor
.in_curr_req
, sizeof(session
->traffic_monitor
.in_curr_req
));
2825 max_stats
= (sizeof(session
->traffic_monitor
.in_curr_req
) / sizeof(session
->traffic_monitor
.in_curr_req
[0]));
2828 if ((session
->traffic_monitor
.num_in_curr_req
= ike_session_get_sas_for_stats(session
,
2831 session
->traffic_monitor
.in_curr_req
,
2833 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2835 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2837 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2842 session
->traffic_monitor
.in_curr_req
,
2843 session
->traffic_monitor
.num_in_curr_req
) < 0) {
2846 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2848 return session
->traffic_monitor
.num_in_curr_req
;
2854 pk_sendget_outbound_sastats(ike_session_t
*session
)
2856 u_int32_t max_stats
;
2860 plog(LLV_DEBUG
, LOCATION
, NULL
, "invalid args in %s \n", __FUNCTION__
);
2864 session
->traffic_monitor
.num_out_curr_req
= 0;
2865 bzero(session
->traffic_monitor
.out_curr_req
, sizeof(session
->traffic_monitor
.out_curr_req
));
2866 max_stats
= (sizeof(session
->traffic_monitor
.out_curr_req
) / sizeof(session
->traffic_monitor
.out_curr_req
[0]));
2869 if ((session
->traffic_monitor
.num_out_curr_req
= ike_session_get_sas_for_stats(session
,
2872 session
->traffic_monitor
.out_curr_req
,
2874 u_int64_t session_ids
[] = {(u_int64_t
)session
, 0};
2876 plog(LLV_DEBUG
, LOCATION
, NULL
, "about to call %s\n", __FUNCTION__
);
2878 if (pfkey_send_getsastats(lcconf
->sock_pfkey
,
2883 session
->traffic_monitor
.out_curr_req
,
2884 session
->traffic_monitor
.num_out_curr_req
) < 0) {
2887 plog(LLV_DEBUG
, LOCATION
, NULL
, "%s successful\n", __FUNCTION__
);
2889 return session
->traffic_monitor
.num_out_curr_req
;
2895 * receive GETSPDSTAT from kernel.
2898 pk_recvgetsastat(mhp
)
2901 struct sadb_msg
*msg
;
2902 struct sadb_session_id
*session_id
;
2903 struct sadb_sastat
*stat_resp
;
2904 ike_session_t
*session
;
2906 /* validity check */
2907 if (mhp
[0] == NULL
||
2908 mhp
[SADB_EXT_SESSION_ID
] == NULL
||
2909 mhp
[SADB_EXT_SASTAT
] == NULL
) {
2910 plog(LLV_ERROR
, LOCATION
, NULL
,
2911 "inappropriate sadb getsastat response.\n");
2914 msg
= (struct sadb_msg
*)mhp
[0];
2915 session_id
= (ike_session_t
*)mhp
[SADB_EXT_SESSION_ID
];
2916 stat_resp
= (struct sadb_sastat
*)mhp
[SADB_EXT_SASTAT
];
2918 /* the message has to be processed or not ? */
2919 if (msg
->sadb_msg_pid
!= getpid()) {
2920 plog(LLV_DEBUG
, LOCATION
, NULL
,
2921 "%s message is not interesting "
2922 "because pid %d is not mine.\n",
2923 s_pfkey_type(msg
->sadb_msg_type
),
2927 if (!session_id
->sadb_session_id_v
[0]) {
2928 plog(LLV_DEBUG
, LOCATION
, NULL
,
2929 "%s message is bad "
2930 "because session-id[0] is invalid.\n",
2931 s_pfkey_type(msg
->sadb_msg_type
));
2934 session
= (__typeof__(session
))session_id
->sadb_session_id_v
[0];
2936 if (!stat_resp
->sadb_sastat_list_len
) {
2937 plog(LLV_DEBUG
, LOCATION
, NULL
,
2938 "%s message is bad "
2939 "because it has no sastats.\n",
2940 s_pfkey_type(msg
->sadb_msg_type
));
2944 ike_session_update_traffic_idle_status(session
,
2945 stat_resp
->sadb_sastat_dir
,
2946 (struct sastat
*)(stat_resp
+ 1),
2947 stat_resp
->sadb_sastat_list_len
);
2952 * check if the algorithm is supported or not.
2957 pk_checkalg(class, calg
, keylen
)
2958 int class, calg
, keylen
;
2962 struct sadb_alg alg0
;
2964 switch (algclass2doi(class)) {
2965 case IPSECDOI_PROTO_IPSEC_ESP
:
2966 sup
= SADB_EXT_SUPPORTED_ENCRYPT
;
2968 case IPSECDOI_ATTR_AUTH
:
2969 sup
= SADB_EXT_SUPPORTED_AUTH
;
2971 case IPSECDOI_PROTO_IPCOMP
:
2972 plog(LLV_DEBUG
, LOCATION
, NULL
,
2973 "compression algorithm can not be checked "
2974 "because sadb message doesn't support it.\n");
2977 plog(LLV_ERROR
, LOCATION
, NULL
,
2978 "invalid algorithm class.\n");
2981 alg
= ipsecdoi2pfkey_alg(algclass2doi(class), algtype2doi(class, calg
));
2986 if (ipsec_get_keylen(sup
, alg
, &alg0
)) {
2987 plog(LLV_ERROR
, LOCATION
, NULL
,
2988 "%s.\n", ipsec_strerror());
2991 keylen
= alg0
.sadb_alg_minbits
;
2994 error
= ipsec_check_keylen(sup
, alg
, keylen
);
2996 plog(LLV_ERROR
, LOCATION
, NULL
,
2997 "%s.\n", ipsec_strerror());
3003 * differences with pfkey_recv() in libipsec/pfkey.c:
3004 * - never performs busy wait loop.
3005 * - returns NULL and set *lenp to negative on fatal failures
3006 * - returns NULL and set *lenp to non-negative on non-fatal failures
3007 * - returns non-NULL on success
3009 static struct sadb_msg
*
3014 struct sadb_msg
*newmsg
;
3016 socklen_t optlen
= sizeof(reallen
);
3018 if (getsockopt(so
, SOL_SOCKET
, SO_NREAD
, &reallen
, &optlen
) < 0)
3019 return NULL
; /*fatal*/
3024 if ((newmsg
= racoon_calloc(1, reallen
)) == NULL
)
3027 *lenp
= recv(so
, (caddr_t
)newmsg
, reallen
, 0);
3029 racoon_free(newmsg
);
3030 return NULL
; /*fatal*/
3031 } else if (*lenp
!= reallen
|| *lenp
< sizeof(struct sadb_msg
)) {
3032 racoon_free(newmsg
);
3043 return eay_random();
3050 struct secpolicy
*new;
3051 struct sadb_address
*saddr
, *daddr
;
3052 struct sadb_x_policy
*xpl
;
3055 if (mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
3056 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
3057 || mhp
[SADB_X_EXT_POLICY
] == NULL
) {
3058 plog(LLV_ERROR
, LOCATION
, NULL
,
3059 "inappropriate sadb spd management message passed.\n");
3063 saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
3064 daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
3065 xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
3068 /* bsd skips over per-socket policies because there will be no
3069 * src and dst extensions in spddump messages. On Linux the only
3070 * way to achieve the same is check for policy id.
3072 if (xpl
->sadb_x_policy_id
% 8 >= 3) return 0;
3077 plog(LLV_ERROR
, LOCATION
, NULL
,
3078 "failed to allocate buffer\n");
3082 new->spidx
.dir
= xpl
->sadb_x_policy_dir
;
3083 new->id
= xpl
->sadb_x_policy_id
;
3084 new->policy
= xpl
->sadb_x_policy_type
;
3088 switch (xpl
->sadb_x_policy_type
) {
3089 case IPSEC_POLICY_DISCARD
:
3090 case IPSEC_POLICY_GENERATE
:
3091 case IPSEC_POLICY_NONE
:
3092 case IPSEC_POLICY_ENTRUST
:
3093 case IPSEC_POLICY_BYPASS
:
3096 case IPSEC_POLICY_IPSEC
:
3099 struct sadb_x_ipsecrequest
*xisr
;
3100 struct ipsecrequest
**p_isr
= &new->req
;
3102 /* validity check */
3103 if (PFKEY_EXTLEN(xpl
) < sizeof(*xpl
)) {
3104 plog(LLV_ERROR
, LOCATION
, NULL
,
3105 "invalid msg length.\n");
3109 tlen
= PFKEY_EXTLEN(xpl
) - sizeof(*xpl
);
3110 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
3115 if (xisr
->sadb_x_ipsecrequest_len
< sizeof(*xisr
)) {
3116 plog(LLV_ERROR
, LOCATION
, NULL
,
3117 "invalid msg length.\n");
3121 /* allocate request buffer */
3122 *p_isr
= newipsecreq();
3123 if (*p_isr
== NULL
) {
3124 plog(LLV_ERROR
, LOCATION
, NULL
,
3125 "failed to get new ipsecreq.\n");
3130 (*p_isr
)->next
= NULL
;
3132 switch (xisr
->sadb_x_ipsecrequest_proto
) {
3135 case IPPROTO_IPCOMP
:
3138 plog(LLV_ERROR
, LOCATION
, NULL
,
3139 "invalid proto type: %u\n",
3140 xisr
->sadb_x_ipsecrequest_proto
);
3143 (*p_isr
)->saidx
.proto
= xisr
->sadb_x_ipsecrequest_proto
;
3145 switch (xisr
->sadb_x_ipsecrequest_mode
) {
3146 case IPSEC_MODE_TRANSPORT
:
3147 case IPSEC_MODE_TUNNEL
:
3149 case IPSEC_MODE_ANY
:
3151 plog(LLV_ERROR
, LOCATION
, NULL
,
3152 "invalid mode: %u\n",
3153 xisr
->sadb_x_ipsecrequest_mode
);
3156 (*p_isr
)->saidx
.mode
= xisr
->sadb_x_ipsecrequest_mode
;
3158 switch (xisr
->sadb_x_ipsecrequest_level
) {
3159 case IPSEC_LEVEL_DEFAULT
:
3160 case IPSEC_LEVEL_USE
:
3161 case IPSEC_LEVEL_REQUIRE
:
3163 case IPSEC_LEVEL_UNIQUE
:
3164 (*p_isr
)->saidx
.reqid
=
3165 xisr
->sadb_x_ipsecrequest_reqid
;
3169 plog(LLV_ERROR
, LOCATION
, NULL
,
3170 "invalid level: %u\n",
3171 xisr
->sadb_x_ipsecrequest_level
);
3174 (*p_isr
)->level
= xisr
->sadb_x_ipsecrequest_level
;
3176 /* set IP addresses if there */
3177 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
3178 struct sockaddr
*paddr
;
3180 paddr
= (struct sockaddr
*)(xisr
+ 1);
3181 bcopy(paddr
, &(*p_isr
)->saidx
.src
,
3182 sysdep_sa_len(paddr
));
3184 paddr
= (struct sockaddr
*)((caddr_t
)paddr
3185 + sysdep_sa_len(paddr
));
3186 bcopy(paddr
, &(*p_isr
)->saidx
.dst
,
3187 sysdep_sa_len(paddr
));
3192 /* initialization for the next. */
3193 p_isr
= &(*p_isr
)->next
;
3194 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
3196 /* validity check */
3198 plog(LLV_ERROR
, LOCATION
, NULL
,
3199 "becoming tlen < 0\n");
3202 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
3203 + xisr
->sadb_x_ipsecrequest_len
);
3208 plog(LLV_ERROR
, LOCATION
, NULL
,
3209 "invalid policy type.\n");
3213 #ifdef HAVE_PFKEY_POLICY_PRIORITY
3214 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3217 saddr
->sadb_address_prefixlen
,
3218 daddr
->sadb_address_prefixlen
,
3219 saddr
->sadb_address_proto
,
3220 xpl
->sadb_x_policy_priority
,
3223 KEY_SETSECSPIDX(xpl
->sadb_x_policy_dir
,
3226 saddr
->sadb_address_prefixlen
,
3227 daddr
->sadb_address_prefixlen
,
3228 saddr
->sadb_address_proto
,
3237 /* proto/mode/src->dst spi */
3239 sadbsecas2str(src
, dst
, proto
, spi
, mode
)
3240 struct sockaddr
*src
, *dst
;
3245 static char buf
[256];
3246 u_int doi_proto
, doi_mode
= 0;
3250 doi_proto
= pfkey2ipsecdoi_proto(proto
);
3251 if (doi_proto
== ~0)
3254 doi_mode
= pfkey2ipsecdoi_mode(mode
);
3259 blen
= sizeof(buf
) - 1;
3262 i
= snprintf(p
, blen
, "%s%s%s ",
3263 s_ipsecdoi_proto(doi_proto
),
3265 mode
? s_ipsecdoi_encmode(doi_mode
) : "");
3266 if (i
< 0 || i
>= blen
)
3271 i
= snprintf(p
, blen
, "%s->", saddr2str(src
));
3272 if (i
< 0 || i
>= blen
)
3277 i
= snprintf(p
, blen
, "%s ", saddr2str(dst
));
3278 if (i
< 0 || i
>= blen
)
3284 snprintf(p
, blen
, "spi=%lu(0x%lx)", (unsigned long)ntohl(spi
),
3285 (unsigned long)ntohl(spi
));