1 /* $KAME: isakmp_inf.c,v 1.81 2002/04/15 01:58:37 itojun 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
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
36 #include <net/pfkeyv2.h>
37 #include <netkey/keydb.h>
38 #include <netkey/key_var.h>
39 #include <netinet/in.h>
40 #ifdef IPV6_INRIA_VERSION
41 #include <sys/queue.h>
42 #include <netinet/ipsec.h>
44 #include <netinet6/ipsec.h>
51 #if TIME_WITH_SYS_TIME
52 # include <sys/time.h>
56 # include <sys/time.h>
72 #include "localconf.h"
73 #include "remoteconf.h"
75 #include "isakmp_var.h"
77 #include "isakmp_inf.h"
80 #include "ipsec_doi.h"
81 #include "crypto_openssl.h"
84 #include "algorithm.h"
89 /* information exchange */
90 static int isakmp_info_recv_n
__P((struct ph1handle
*, vchar_t
*));
91 static int isakmp_info_recv_d
__P((struct ph1handle
*, vchar_t
*));
93 static void purge_isakmp_spi
__P((int, isakmp_index
*, size_t));
94 static void purge_ipsec_spi
__P((struct sockaddr
*, int, u_int32_t
*, size_t));
95 static void info_recv_initialcontact
__P((struct ph1handle
*));
98 * Information Exchange
101 * receive Information
104 isakmp_info_recv(iph1
, msg0
)
105 struct ph1handle
*iph1
;
110 struct isakmp
*isakmp
;
111 struct isakmp_gen
*gen
;
115 plog(LLV_DEBUG
, LOCATION
, NULL
, "receive Information.\n");
117 encrypted
= ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
);
119 /* Use new IV to decrypt Informational message. */
122 struct isakmp_ivm
*ivm
;
125 ivm
= oakley_newiv2(iph1
, ((struct isakmp
*)msg0
->v
)->msgid
);
129 msg
= oakley_do_decrypt(iph1
, msg0
, ivm
->iv
, ivm
->ive
);
137 isakmp
= (struct isakmp
*)msg
->v
;
138 gen
= (struct isakmp_gen
*)((caddr_t
)isakmp
+ sizeof(struct isakmp
));
139 if (isakmp
->np
== ISAKMP_NPTYPE_HASH
)
144 /* make sure the packet were encrypted. */
146 switch (iph1
->etype
) {
147 case ISAKMP_ETYPE_AGG
:
148 case ISAKMP_ETYPE_BASE
:
149 case ISAKMP_ETYPE_IDENT
:
150 if ((iph1
->side
== INITIATOR
&& iph1
->status
< PHASE1ST_MSG3SENT
)
151 || (iph1
->side
== RESPONDER
&& iph1
->status
< PHASE1ST_MSG2SENT
)) {
156 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
157 "%s message must be encrypted\n",
158 s_isakmp_nptype(np
));
164 case ISAKMP_NPTYPE_N
:
165 if (isakmp_info_recv_n(iph1
, msg
) < 0)
168 case ISAKMP_NPTYPE_D
:
169 if (isakmp_info_recv_d(iph1
, msg
) < 0)
172 case ISAKMP_NPTYPE_NONCE
:
173 /* XXX to be 6.4.2 ike-01.txt */
174 /* XXX IV is to be synchronized. */
175 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
176 "ignore Acknowledged Informational\n");
179 /* don't send information, see isakmp_ident_r1() */
181 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
182 "reject the packet, "
183 "received unexpecting payload type %d.\n",
196 * send Delete payload (for ISAKMP SA) in Informational exchange.
199 isakmp_info_send_d1(iph1
)
200 struct ph1handle
*iph1
;
202 struct isakmp_pl_d
*d
;
203 vchar_t
*payload
= NULL
;
207 if (iph1
->status
!= PHASE2ST_ESTABLISHED
)
210 /* create delete payload */
212 /* send SPIs of inbound SAs. */
213 /* XXX should send outbound SAs's ? */
214 tlen
= sizeof(*d
) + sizeof(isakmp_index
);
215 payload
= vmalloc(tlen
);
216 if (payload
== NULL
) {
217 plog(LLV_ERROR
, LOCATION
, NULL
,
218 "failed to get buffer for payload.\n");
222 d
= (struct isakmp_pl_d
*)payload
->v
;
223 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
224 d
->h
.len
= htons(tlen
);
225 d
->doi
= htonl(IPSEC_DOI
);
226 d
->proto_id
= IPSECDOI_PROTO_ISAKMP
;
227 d
->spi_size
= sizeof(isakmp_index
);
228 d
->num_spi
= htons(1);
229 memcpy(d
+ 1, &iph1
->index
, sizeof(isakmp_index
));
231 error
= isakmp_info_send_common(iph1
, payload
,
239 * send Delete payload (for IPsec SA) in Informational exchange, based on
240 * pfkey msg. It sends always single SPI.
243 isakmp_info_send_d2(iph2
)
244 struct ph2handle
*iph2
;
246 struct ph1handle
*iph1
;
248 struct isakmp_pl_d
*d
;
249 vchar_t
*payload
= NULL
;
254 if (iph2
->status
!= PHASE2ST_ESTABLISHED
)
258 * don't send delete information if there is no phase 1 handler.
259 * It's nonsensical to negotiate phase 1 to send the information.
261 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
265 /* create delete payload */
266 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
268 /* send SPIs of inbound SAs. */
270 * XXX should I send outbound SAs's ?
271 * I send inbound SAs's SPI only at the moment because I can't
272 * decode any more if peer send encoded packet without aware of
273 * deletion of SA. Outbound SAs don't come under the situation.
275 tlen
= sizeof(*d
) + pr
->spisize
;
276 payload
= vmalloc(tlen
);
277 if (payload
== NULL
) {
278 plog(LLV_ERROR
, LOCATION
, NULL
,
279 "failed to get buffer for payload.\n");
283 d
= (struct isakmp_pl_d
*)payload
->v
;
284 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
285 d
->h
.len
= htons(tlen
);
286 d
->doi
= htonl(IPSEC_DOI
);
287 d
->proto_id
= pr
->proto_id
;
288 d
->spi_size
= pr
->spisize
;
289 d
->num_spi
= htons(1);
291 * XXX SPI bits are left-filled, for use with IPComp.
292 * we should be switching to variable-length spi field...
294 spi
= (u_int8_t
*)&pr
->spi
;
295 spi
+= sizeof(pr
->spi
);
297 memcpy(d
+ 1, spi
, pr
->spisize
);
299 error
= isakmp_info_send_common(iph1
, payload
,
308 * send Notification payload (for without ISAKMP SA) in Informational exchange
311 isakmp_info_send_nx(isakmp
, remote
, local
, type
, data
)
312 struct isakmp
*isakmp
;
313 struct sockaddr
*remote
, *local
;
317 struct ph1handle
*iph1
= NULL
;
318 struct remoteconf
*rmconf
;
319 vchar_t
*payload
= NULL
;
322 struct isakmp_pl_n
*n
;
323 int spisiz
= 0; /* see below */
325 /* search appropreate configuration */
326 rmconf
= getrmconf(remote
);
327 if (rmconf
== NULL
) {
328 plog(LLV_ERROR
, LOCATION
, remote
,
329 "no configuration found for peer address.\n");
333 /* add new entry to isakmp status table. */
338 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(cookie_t
));
339 isakmp_newcookie((char *)&iph1
->index
.r_ck
, remote
, local
);
340 iph1
->status
= PHASE1ST_START
;
341 iph1
->rmconf
= rmconf
;
342 iph1
->side
= INITIATOR
;
343 iph1
->version
= isakmp
->v
;
345 iph1
->msgid
= 0; /* XXX */
347 /* copy remote address */
348 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0)
351 tlen
= sizeof(*n
) + spisiz
;
354 payload
= vmalloc(tlen
);
355 if (payload
== NULL
) {
356 plog(LLV_ERROR
, LOCATION
, NULL
,
357 "failed to get buffer to send.\n");
361 n
= (struct isakmp_pl_n
*)payload
->v
;
362 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
363 n
->h
.len
= htons(tlen
);
364 n
->doi
= htonl(IPSEC_DOI
);
365 n
->proto_id
= IPSECDOI_KEY_IKE
;
366 n
->spi_size
= spisiz
;
367 n
->type
= htons(type
);
369 memset(n
+ 1, 0, spisiz
); /*XXX*/
371 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
373 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
384 * send Notification payload (for ISAKMP SA) in Informational exchange
387 isakmp_info_send_n1(iph1
, type
, data
)
388 struct ph1handle
*iph1
;
392 vchar_t
*payload
= NULL
;
395 struct isakmp_pl_n
*n
;
399 * note on SPI size: which description is correct? I have chosen
402 * RFC2408 3.1, 2nd paragraph says: ISAKMP SA is identified by
403 * Initiator/Responder cookie and SPI has no meaning, SPI size = 0.
404 * RFC2408 3.1, first paragraph on page 40: ISAKMP SA is identified
405 * by cookie and SPI has no meaning, 0 <= SPI size <= 16.
406 * RFC2407 4.6.3.3, INITIAL-CONTACT is required to set to 16.
408 if (type
== ISAKMP_NTYPE_INITIAL_CONTACT
)
409 spisiz
= sizeof(isakmp_index
);
413 tlen
= sizeof(*n
) + spisiz
;
416 payload
= vmalloc(tlen
);
417 if (payload
== NULL
) {
418 plog(LLV_ERROR
, LOCATION
, NULL
,
419 "failed to get buffer to send.\n");
423 n
= (struct isakmp_pl_n
*)payload
->v
;
424 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
425 n
->h
.len
= htons(tlen
);
426 n
->doi
= htonl(iph1
->rmconf
->doitype
);
427 n
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX to be configurable ? */
428 n
->spi_size
= spisiz
;
429 n
->type
= htons(type
);
431 memcpy(n
+ 1, &iph1
->index
, sizeof(isakmp_index
));
433 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
435 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph1
->flags
);
442 * send Notification payload (for IPsec SA) in Informational exchange
445 isakmp_info_send_n2(iph2
, type
, data
)
446 struct ph2handle
*iph2
;
450 struct ph1handle
*iph1
= iph2
->ph1
;
451 vchar_t
*payload
= NULL
;
454 struct isakmp_pl_n
*n
;
460 pr
= iph2
->approval
->head
;
462 /* XXX must be get proper spi */
463 tlen
= sizeof(*n
) + pr
->spisize
;
466 payload
= vmalloc(tlen
);
467 if (payload
== NULL
) {
468 plog(LLV_ERROR
, LOCATION
, NULL
,
469 "failed to get buffer to send.\n");
473 n
= (struct isakmp_pl_n
*)payload
->v
;
474 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
475 n
->h
.len
= htons(tlen
);
476 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
477 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
478 n
->spi_size
= pr
->spisize
;
479 n
->type
= htons(type
);
480 *(u_int32_t
*)(n
+ 1) = pr
->spi
;
482 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
484 iph2
->flags
|= ISAKMP_FLAG_E
; /* XXX Should we do FLAG_A ? */
485 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph2
->flags
);
493 * When ph1->skeyid_a == NULL, send message without encoding.
496 isakmp_info_send_common(iph1
, payload
, np
, flags
)
497 struct ph1handle
*iph1
;
502 struct ph2handle
*iph2
= NULL
;
503 vchar_t
*hash
= NULL
;
504 struct isakmp
*isakmp
;
505 struct isakmp_gen
*gen
;
510 /* add new entry to isakmp status table */
515 iph2
->dst
= dupsaddr(iph1
->remote
);
516 iph2
->src
= dupsaddr(iph1
->local
);
517 switch (iph1
->remote
->sa_family
) {
519 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
520 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
524 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
525 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
529 plog(LLV_ERROR
, LOCATION
, NULL
,
530 "invalid family: %d\n", iph1
->remote
->sa_family
);
535 iph2
->side
= INITIATOR
;
536 iph2
->status
= PHASE2ST_START
;
537 iph2
->msgid
= isakmp_newmsgid2(iph1
);
539 /* get IV and HASH(1) if skeyid_a was generated. */
540 if (iph1
->skeyid_a
!= NULL
) {
541 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
542 if (iph2
->ivm
== NULL
) {
547 /* generate HASH(1) */
548 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, payload
);
554 /* initialized total buffer length */
556 tlen
+= sizeof(*gen
);
558 /* IKE-SA is not established */
561 /* initialized total buffer length */
564 if ((flags
& ISAKMP_FLAG_A
) == 0)
565 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_E
);
567 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_A
);
570 bindph12(iph1
, iph2
);
572 tlen
+= sizeof(*isakmp
) + payload
->l
;
574 /* create buffer for isakmp payload */
575 iph2
->sendbuf
= vmalloc(tlen
);
576 if (iph2
->sendbuf
== NULL
) {
577 plog(LLV_ERROR
, LOCATION
, NULL
,
578 "failed to get buffer to send.\n");
582 /* create isakmp header */
583 isakmp
= (struct isakmp
*)iph2
->sendbuf
->v
;
584 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
585 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
586 isakmp
->np
= hash
== NULL
? (np
& 0xff) : ISAKMP_NPTYPE_HASH
;
587 isakmp
->v
= iph1
->version
;
588 isakmp
->etype
= ISAKMP_ETYPE_INFO
;
589 isakmp
->flags
= iph2
->flags
;
590 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
591 isakmp
->len
= htonl(tlen
);
592 p
= (char *)(isakmp
+ 1);
594 /* create HASH payload */
596 gen
= (struct isakmp_gen
*)p
;
598 gen
->len
= htons(sizeof(*gen
) + hash
->l
);
600 memcpy(p
, hash
->v
, hash
->l
);
605 memcpy(p
, payload
->v
, payload
->l
);
608 #ifdef HAVE_PRINT_ISAKMP_C
609 isakmp_printpacket(iph2
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
613 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
)) {
616 tmp
= oakley_do_encrypt(iph2
->ph1
, iph2
->sendbuf
, iph2
->ivm
->ive
,
619 vfree(iph2
->sendbuf
);
620 iph2
->sendbuf
= NULL
;
623 vfree(iph2
->sendbuf
);
627 /* HDR*, HASH(1), N */
628 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
629 vfree(iph2
->sendbuf
);
630 iph2
->sendbuf
= NULL
;
634 plog(LLV_DEBUG
, LOCATION
, NULL
,
635 "sendto Information %s.\n", s_isakmp_nptype(np
));
638 * don't resend notify message because peer can use Acknowledged
639 * Informational if peer requires the reply of the notify message.
642 /* XXX If Acknowledged Informational required, don't delete ph2handle */
644 vfree(iph2
->sendbuf
);
645 iph2
->sendbuf
= NULL
;
661 * add a notify payload to buffer by reallocating buffer.
662 * If buf == NULL, the function only create a notify payload.
664 * XXX Which is SPI to be included, inbound or outbound ?
667 isakmp_add_pl_n(buf0
, np_p
, type
, pr
, data
)
675 struct isakmp_pl_n
*n
;
680 **np_p
= ISAKMP_NPTYPE_N
;
682 tlen
= sizeof(*n
) + pr
->spisize
;
688 buf
= vrealloc(buf0
, buf0
->l
+ tlen
);
692 plog(LLV_ERROR
, LOCATION
, NULL
,
693 "failed to get a payload buffer.\n");
697 n
= (struct isakmp_pl_n
*)(buf
->v
+ oldlen
);
698 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
699 n
->h
.len
= htons(tlen
);
700 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
701 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
702 n
->spi_size
= pr
->spisize
;
703 n
->type
= htons(type
);
704 *(u_int32_t
*)(n
+ 1) = pr
->spi
; /* XXX */
706 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
708 /* save the pointer of next payload type */
715 * handling to receive Notification payload
718 isakmp_info_recv_n(iph1
, msg
)
719 struct ph1handle
*iph1
;
722 struct isakmp_pl_n
*n
= NULL
;
725 struct isakmp_parse_t
*pa
, *pap
;
728 if (!(pbuf
= isakmp_parse(msg
)))
730 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
731 for (pap
= pa
; pap
->type
; pap
++) {
733 case ISAKMP_NPTYPE_HASH
:
734 /* do something here */
736 case ISAKMP_NPTYPE_NONCE
:
739 case ISAKMP_NPTYPE_N
:
740 n
= (struct isakmp_pl_n
*)pap
->ptr
;
751 type
= ntohs(n
->type
);
754 case ISAKMP_NTYPE_CONNECTED
:
755 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
756 case ISAKMP_NTYPE_REPLAY_STATUS
:
759 case ISAKMP_NTYPE_INITIAL_CONTACT
:
760 info_recv_initialcontact(iph1
);
764 u_int32_t msgid
= ((struct isakmp
*)msg
->v
)->msgid
;
765 struct ph2handle
*iph2
;
767 /* XXX there is a potential of dos attack. */
770 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
771 "delete phase1 handle.\n");
774 iph2
= getph2bymsgid(iph1
, msgid
);
776 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
777 "unknown notify message, "
778 "no phase2 handle found.\n");
790 /* get spi and allocate */
791 if (ntohs(n
->h
.len
) < sizeof(*n
) + n
->spi_size
) {
792 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
793 "invalid spi_size in notification payload.\n");
796 spi
= val2str((u_char
*)(n
+ 1), n
->spi_size
);
798 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
799 "notification message %d:%s, "
800 "doi=%d proto_id=%d spi=%s(size=%d).\n",
801 type
, s_isakmp_notify_msg(type
),
802 ntohl(n
->doi
), n
->proto_id
, spi
, n
->spi_size
);
810 purge_isakmp_spi(proto
, spi
, n
)
812 isakmp_index
*spi
; /*network byteorder*/
815 struct ph1handle
*iph1
;
818 for (i
= 0; i
< n
; i
++) {
819 iph1
= getph1byindex(&spi
[i
]);
823 plog(LLV_INFO
, LOCATION
, NULL
,
824 "purged ISAKMP-SA proto_id=%s spi=%s.\n",
825 s_ipsecdoi_proto(proto
),
826 isakmp_pindex(&spi
[i
], 0));
829 SCHED_KILL(iph1
->sce
);
830 iph1
->status
= PHASE1ST_EXPIRED
;
831 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
836 purge_ipsec_spi(dst0
, proto
, spi
, n
)
837 struct sockaddr
*dst0
;
839 u_int32_t
*spi
; /*network byteorder*/
843 struct sadb_msg
*msg
, *next
, *end
;
845 struct sockaddr
*src
, *dst
;
846 struct ph2handle
*iph2
;
848 caddr_t mhp
[SADB_EXT_MAX
+ 1];
850 buf
= pfkey_dump_sadb(ipsecdoi2pfkey_proto(proto
));
852 plog(LLV_DEBUG
, LOCATION
, NULL
,
853 "pfkey_dump_sadb returned nothing.\n");
857 msg
= (struct sadb_msg
*)buf
->v
;
858 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
861 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
863 next
= (struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
864 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
869 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
870 plog(LLV_ERROR
, LOCATION
, NULL
,
871 "pfkey_check (%s)\n", ipsec_strerror());
876 sa
= (struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
878 || !mhp
[SADB_EXT_ADDRESS_SRC
]
879 || !mhp
[SADB_EXT_ADDRESS_DST
]) {
883 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
884 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
886 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
887 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
892 /* XXX n^2 algorithm, inefficient */
894 /* don't delete inbound SAs at the moment */
895 /* XXX should we remove SAs with opposite direction as well? */
896 if (cmpsaddrwop(dst0
, dst
)) {
901 for (i
= 0; i
< n
; i
++) {
902 plog(LLV_DEBUG
, LOCATION
, NULL
,
903 "check spi(packet)=%u spi(db)=%u.\n",
904 ntohl(spi
[i
]), ntohl(sa
->sadb_sa_spi
));
905 if (spi
[i
] != sa
->sadb_sa_spi
)
908 pfkey_send_delete(lcconf
->sock_pfkey
,
909 msg
->sadb_msg_satype
,
911 src
, dst
, sa
->sadb_sa_spi
);
914 * delete a relative phase 2 handler.
915 * continue to process if no relative phase 2 handler
918 iph2
= getph2bysaidx(src
, dst
, proto
, spi
[i
]);
925 plog(LLV_INFO
, LOCATION
, NULL
,
926 "purged IPsec-SA proto_id=%s spi=%u.\n",
927 s_ipsecdoi_proto(proto
),
939 * delete all phase2 sa relatived to the destination address.
940 * Don't delete Phase 1 handlers on INITIAL-CONTACT, and don't ignore
941 * an INITIAL-CONTACT if we have contacted the peer. This matches the
942 * Sun IKE behavior, and makes rekeying work much better when the peer
946 info_recv_initialcontact(iph1
)
947 struct ph1handle
*iph1
;
950 struct sadb_msg
*msg
, *next
, *end
;
952 struct sockaddr
*src
, *dst
;
953 caddr_t mhp
[SADB_EXT_MAX
+ 1];
955 struct ph2handle
*iph2
;
964 loc
= strdup(saddrwop2str(iph1
->local
));
965 rem
= strdup(saddrwop2str(iph1
->remote
));
968 * Purge all IPSEC-SAs for the peer. We can do this
969 * the easy way (using a PF_KEY SADB_DELETE extension)
970 * or we can do it the hard way.
972 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
973 proto_id
= pfkey2ipsecdoi_proto(pfkey_satypes
[i
].ps_satype
);
975 plog(LLV_INFO
, LOCATION
, NULL
,
976 "purging %s SAs for %s -> %s\n",
977 pfkey_satypes
[i
].ps_name
, loc
, rem
);
978 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
979 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
980 iph1
->local
, iph1
->remote
) == -1) {
981 plog(LLV_ERROR
, LOCATION
, NULL
,
982 "delete_all %s -> %s failed for %s (%s)\n",
984 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
988 deleteallph2(iph1
->local
, iph1
->remote
, proto_id
);
990 plog(LLV_INFO
, LOCATION
, NULL
,
991 "purging %s SAs for %s -> %s\n",
992 pfkey_satypes
[i
].ps_name
, rem
, loc
);
993 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
994 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
995 iph1
->remote
, iph1
->local
) == -1) {
996 plog(LLV_ERROR
, LOCATION
, NULL
,
997 "delete_all %s -> %s failed for %s (%s)\n",
999 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1003 deleteallph2(iph1
->remote
, iph1
->local
, proto_id
);
1015 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
1017 plog(LLV_DEBUG
, LOCATION
, NULL
,
1018 "pfkey_dump_sadb returned nothing.\n");
1022 msg
= (struct sadb_msg
*)buf
->v
;
1023 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
1026 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1028 next
= (struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1029 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1034 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1035 plog(LLV_ERROR
, LOCATION
, NULL
,
1036 "pfkey_check (%s)\n", ipsec_strerror());
1041 if (mhp
[SADB_EXT_SA
] == NULL
1042 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1043 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1047 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1048 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1049 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1051 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1052 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1058 * RFC2407 4.6.3.3 INITIAL-CONTACT is the message that
1059 * announces the sender of the message was rebooted.
1060 * it is interpreted to delete all SAs which source address
1061 * is the sender of the message.
1062 * racoon only deletes SA which is matched both the
1063 * source address and the destination accress.
1065 if (cmpsaddrwop(iph1
->local
, src
) == 0 &&
1066 cmpsaddrwop(iph1
->remote
, dst
) == 0)
1068 else if (cmpsaddrwop(iph1
->remote
, src
) == 0 &&
1069 cmpsaddrwop(iph1
->local
, dst
) == 0)
1077 * Make sure this is an SATYPE that we manage.
1078 * This is gross; too bad we couldn't do it the
1081 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1082 if (pfkey_satypes
[i
].ps_satype
==
1083 msg
->sadb_msg_satype
)
1086 if (i
== pfkey_nsatypes
) {
1091 plog(LLV_INFO
, LOCATION
, NULL
,
1092 "purging spi=%u.\n", ntohl(sa
->sadb_sa_spi
));
1093 pfkey_send_delete(lcconf
->sock_pfkey
,
1094 msg
->sadb_msg_satype
,
1095 IPSEC_MODE_ANY
, src
, dst
, sa
->sadb_sa_spi
);
1098 * delete a relative phase 2 handler.
1099 * continue to process if no relative phase 2 handler
1102 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1103 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1117 * handling to receive Deletion payload
1120 isakmp_info_recv_d(iph1
, msg
)
1121 struct ph1handle
*iph1
;
1124 struct isakmp_pl_d
*d
;
1127 struct isakmp_parse_t
*pa
, *pap
;
1134 /* validate the type of next payload */
1135 if (!(pbuf
= isakmp_parse(msg
)))
1137 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
1138 for (pap
= pa
; pap
->type
; pap
++) {
1139 switch (pap
->type
) {
1140 case ISAKMP_NPTYPE_D
:
1142 case ISAKMP_NPTYPE_HASH
:
1147 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1148 "received next payload type %d "
1149 "in wrong place (must be the first payload).\n",
1154 /* don't send information, see isakmp_ident_r1() */
1155 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1156 "reject the packet, "
1157 "received unexpecting payload type %d.\n",
1165 plog(LLV_ERROR
, LOCATION
, NULL
,
1166 "delete payload is not proteted, "
1172 /* process a delete payload */
1173 for (pap
= pa
; pap
->type
; pap
++) {
1174 if (pap
->type
!= ISAKMP_NPTYPE_D
)
1177 d
= (struct isakmp_pl_d
*)pap
->ptr
;
1179 if (ntohl(d
->doi
) != IPSEC_DOI
) {
1180 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1181 "delete payload with invalid doi:%d.\n",
1186 num_spi
= ntohs(d
->num_spi
);
1187 tlen
= ntohs(d
->h
.len
) - sizeof(struct isakmp_pl_d
);
1189 if (tlen
!= num_spi
* d
->spi_size
) {
1190 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1191 "deletion payload with invalid length.\n");
1196 switch (d
->proto_id
) {
1197 case IPSECDOI_PROTO_ISAKMP
:
1198 if (d
->spi_size
!= sizeof(isakmp_index
)) {
1199 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1200 "delete payload with strange spi "
1201 "size %d(proto_id:%d)\n",
1202 d
->spi_size
, d
->proto_id
);
1205 purge_isakmp_spi(d
->proto_id
,
1206 (isakmp_index
*)(d
+ 1), num_spi
);
1209 case IPSECDOI_PROTO_IPSEC_AH
:
1210 case IPSECDOI_PROTO_IPSEC_ESP
:
1211 if (d
->spi_size
!= sizeof(u_int32_t
)) {
1212 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1213 "delete payload with strange spi "
1214 "size %d(proto_id:%d)\n",
1215 d
->spi_size
, d
->proto_id
);
1218 purge_ipsec_spi(iph1
->remote
, d
->proto_id
,
1219 (u_int32_t
*)(d
+ 1), num_spi
);
1222 case IPSECDOI_PROTO_IPCOMP
:
1223 /* need to handle both 16bit/32bit SPI */
1224 memset(&spi
, 0, sizeof(spi
));
1225 if (d
->spi_size
== sizeof(spi
.spi16
[1])) {
1226 memcpy(&spi
.spi16
[1], d
+ 1,
1227 sizeof(spi
.spi16
[1]));
1228 } else if (d
->spi_size
== sizeof(spi
.spi32
))
1229 memcpy(&spi
.spi32
, d
+ 1, sizeof(spi
.spi32
));
1231 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1232 "delete payload with strange spi "
1233 "size %d(proto_id:%d)\n",
1234 d
->spi_size
, d
->proto_id
);
1237 purge_ipsec_spi(iph1
->remote
, d
->proto_id
,
1238 &spi
.spi32
, num_spi
);
1242 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1243 "deletion message received, "
1244 "invalid proto_id: %d\n",
1249 plog(LLV_DEBUG
, LOCATION
, NULL
, "purged SAs.\n");
1258 isakmp_check_notify(gen
, iph1
)
1259 struct isakmp_gen
*gen
; /* points to Notify payload */
1260 struct ph1handle
*iph1
;
1262 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1264 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
1265 "Notify Message received\n");
1267 switch (ntohs(notify
->type
)) {
1268 case ISAKMP_NTYPE_CONNECTED
:
1269 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1270 "ignore CONNECTED notification.\n");
1272 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1273 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1274 "ignore RESPONDER-LIFETIME notification.\n");
1276 case ISAKMP_NTYPE_REPLAY_STATUS
:
1277 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1278 "ignore REPLAY-STATUS notification.\n");
1280 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1281 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1282 "ignore INITIAL-CONTACT notification, "
1283 "because it is only accepted after phase1.\n");
1286 isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1287 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1288 "received unknown notification type %u.\n",
1289 ntohs(notify
->type
));