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
));
141 if (isakmp
->np
!= ISAKMP_NPTYPE_HASH
) {
142 plog(LLV_ERROR
, LOCATION
, NULL
,
143 "ignore information because the message has no hash payload.\n");
147 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
148 plog(LLV_ERROR
, LOCATION
, NULL
,
149 "ignore information because ISAKMP-SA has not been established yet.\n");
157 vchar_t
*hash
, *payload
;
158 struct isakmp_gen
*nd
;
161 * XXX: gen->len includes isakmp header length
163 p
= (caddr_t
) gen
+ sizeof(struct isakmp_gen
);
164 nd
= (struct isakmp_gen
*) ((caddr_t
) gen
+ ntohs(gen
->len
));
166 /* nd length check */
167 if (ntohs(nd
->len
) > msg
->l
- (sizeof(struct isakmp
) + ntohs(gen
->len
))) {
168 plog(LLV_ERROR
, LOCATION
, NULL
,
169 "too long payload length (broken message?)\n");
173 payload
= vmalloc(ntohs(nd
->len
));
174 if (payload
== NULL
) {
175 plog(LLV_ERROR
, LOCATION
, NULL
,
176 "cannot allocate memory\n");
180 memcpy(payload
->v
, (caddr_t
) nd
, ntohs(nd
->len
));
183 hash
= oakley_compute_hash1(iph1
, isakmp
->msgid
, payload
);
185 plog(LLV_ERROR
, LOCATION
, NULL
,
186 "cannot compute hash\n");
192 if (ntohs(gen
->len
) - sizeof(struct isakmp_gen
) != hash
->l
) {
193 plog(LLV_ERROR
, LOCATION
, NULL
,
194 "ignore information due to hash length mismatch\n");
201 if (memcmp(p
, hash
->v
, hash
->l
) != 0) {
202 plog(LLV_ERROR
, LOCATION
, NULL
,
203 "ignore information due to hash mismatch\n");
210 plog(LLV_DEBUG
, LOCATION
, NULL
, "hash validated.\n");
216 /* make sure the packet were encrypted. */
218 switch (iph1
->etype
) {
219 case ISAKMP_ETYPE_AGG
:
220 case ISAKMP_ETYPE_BASE
:
221 case ISAKMP_ETYPE_IDENT
:
222 if ((iph1
->side
== INITIATOR
&& iph1
->status
< PHASE1ST_MSG3SENT
)
223 || (iph1
->side
== RESPONDER
&& iph1
->status
< PHASE1ST_MSG2SENT
)) {
228 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
229 "%s message must be encrypted\n",
230 s_isakmp_nptype(np
));
236 case ISAKMP_NPTYPE_N
:
237 if (isakmp_info_recv_n(iph1
, msg
) < 0)
240 case ISAKMP_NPTYPE_D
:
241 if (isakmp_info_recv_d(iph1
, msg
) < 0)
244 case ISAKMP_NPTYPE_NONCE
:
245 /* XXX to be 6.4.2 ike-01.txt */
246 /* XXX IV is to be synchronized. */
247 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
248 "ignore Acknowledged Informational\n");
251 /* don't send information, see isakmp_ident_r1() */
253 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
254 "reject the packet, "
255 "received unexpecting payload type %d.\n",
268 * send Delete payload (for ISAKMP SA) in Informational exchange.
271 isakmp_info_send_d1(iph1
)
272 struct ph1handle
*iph1
;
274 struct isakmp_pl_d
*d
;
275 vchar_t
*payload
= NULL
;
279 if (iph1
->status
!= PHASE2ST_ESTABLISHED
)
282 /* create delete payload */
284 /* send SPIs of inbound SAs. */
285 /* XXX should send outbound SAs's ? */
286 tlen
= sizeof(*d
) + sizeof(isakmp_index
);
287 payload
= vmalloc(tlen
);
288 if (payload
== NULL
) {
289 plog(LLV_ERROR
, LOCATION
, NULL
,
290 "failed to get buffer for payload.\n");
294 d
= (struct isakmp_pl_d
*)payload
->v
;
295 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
296 d
->h
.len
= htons(tlen
);
297 d
->doi
= htonl(IPSEC_DOI
);
298 d
->proto_id
= IPSECDOI_PROTO_ISAKMP
;
299 d
->spi_size
= sizeof(isakmp_index
);
300 d
->num_spi
= htons(1);
301 memcpy(d
+ 1, &iph1
->index
, sizeof(isakmp_index
));
303 error
= isakmp_info_send_common(iph1
, payload
,
311 * send Delete payload (for IPsec SA) in Informational exchange, based on
312 * pfkey msg. It sends always single SPI.
315 isakmp_info_send_d2(iph2
)
316 struct ph2handle
*iph2
;
318 struct ph1handle
*iph1
;
320 struct isakmp_pl_d
*d
;
321 vchar_t
*payload
= NULL
;
326 if (iph2
->status
!= PHASE2ST_ESTABLISHED
)
330 * don't send delete information if there is no phase 1 handler.
331 * It's nonsensical to negotiate phase 1 to send the information.
333 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
337 /* create delete payload */
338 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
340 /* send SPIs of inbound SAs. */
342 * XXX should I send outbound SAs's ?
343 * I send inbound SAs's SPI only at the moment because I can't
344 * decode any more if peer send encoded packet without aware of
345 * deletion of SA. Outbound SAs don't come under the situation.
347 tlen
= sizeof(*d
) + pr
->spisize
;
348 payload
= vmalloc(tlen
);
349 if (payload
== NULL
) {
350 plog(LLV_ERROR
, LOCATION
, NULL
,
351 "failed to get buffer for payload.\n");
355 d
= (struct isakmp_pl_d
*)payload
->v
;
356 d
->h
.np
= ISAKMP_NPTYPE_NONE
;
357 d
->h
.len
= htons(tlen
);
358 d
->doi
= htonl(IPSEC_DOI
);
359 d
->proto_id
= pr
->proto_id
;
360 d
->spi_size
= pr
->spisize
;
361 d
->num_spi
= htons(1);
363 * XXX SPI bits are left-filled, for use with IPComp.
364 * we should be switching to variable-length spi field...
366 spi
= (u_int8_t
*)&pr
->spi
;
367 spi
+= sizeof(pr
->spi
);
369 memcpy(d
+ 1, spi
, pr
->spisize
);
371 error
= isakmp_info_send_common(iph1
, payload
,
380 * send Notification payload (for without ISAKMP SA) in Informational exchange
383 isakmp_info_send_nx(isakmp
, remote
, local
, type
, data
)
384 struct isakmp
*isakmp
;
385 struct sockaddr
*remote
, *local
;
389 struct ph1handle
*iph1
= NULL
;
390 struct remoteconf
*rmconf
;
391 vchar_t
*payload
= NULL
;
394 struct isakmp_pl_n
*n
;
395 int spisiz
= 0; /* see below */
397 /* search appropreate configuration */
398 rmconf
= getrmconf(remote
);
399 if (rmconf
== NULL
) {
400 plog(LLV_ERROR
, LOCATION
, remote
,
401 "no configuration found for peer address.\n");
405 /* add new entry to isakmp status table. */
410 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(cookie_t
));
411 isakmp_newcookie((char *)&iph1
->index
.r_ck
, remote
, local
);
412 iph1
->status
= PHASE1ST_START
;
413 iph1
->rmconf
= rmconf
;
414 iph1
->side
= INITIATOR
;
415 iph1
->version
= isakmp
->v
;
417 iph1
->msgid
= 0; /* XXX */
419 /* copy remote address */
420 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0)
423 tlen
= sizeof(*n
) + spisiz
;
426 payload
= vmalloc(tlen
);
427 if (payload
== NULL
) {
428 plog(LLV_ERROR
, LOCATION
, NULL
,
429 "failed to get buffer to send.\n");
433 n
= (struct isakmp_pl_n
*)payload
->v
;
434 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
435 n
->h
.len
= htons(tlen
);
436 n
->doi
= htonl(IPSEC_DOI
);
437 n
->proto_id
= IPSECDOI_KEY_IKE
;
438 n
->spi_size
= spisiz
;
439 n
->type
= htons(type
);
441 memset(n
+ 1, 0, spisiz
); /*XXX*/
443 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
445 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, 0);
456 * send Notification payload (for ISAKMP SA) in Informational exchange
459 isakmp_info_send_n1(iph1
, type
, data
)
460 struct ph1handle
*iph1
;
464 vchar_t
*payload
= NULL
;
467 struct isakmp_pl_n
*n
;
471 * note on SPI size: which description is correct? I have chosen
474 * RFC2408 3.1, 2nd paragraph says: ISAKMP SA is identified by
475 * Initiator/Responder cookie and SPI has no meaning, SPI size = 0.
476 * RFC2408 3.1, first paragraph on page 40: ISAKMP SA is identified
477 * by cookie and SPI has no meaning, 0 <= SPI size <= 16.
478 * RFC2407 4.6.3.3, INITIAL-CONTACT is required to set to 16.
480 if (type
== ISAKMP_NTYPE_INITIAL_CONTACT
)
481 spisiz
= sizeof(isakmp_index
);
485 tlen
= sizeof(*n
) + spisiz
;
488 payload
= vmalloc(tlen
);
489 if (payload
== NULL
) {
490 plog(LLV_ERROR
, LOCATION
, NULL
,
491 "failed to get buffer to send.\n");
495 n
= (struct isakmp_pl_n
*)payload
->v
;
496 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
497 n
->h
.len
= htons(tlen
);
498 n
->doi
= htonl(iph1
->rmconf
->doitype
);
499 n
->proto_id
= IPSECDOI_PROTO_ISAKMP
; /* XXX to be configurable ? */
500 n
->spi_size
= spisiz
;
501 n
->type
= htons(type
);
503 memcpy(n
+ 1, &iph1
->index
, sizeof(isakmp_index
));
505 memcpy((caddr_t
)(n
+ 1) + spisiz
, data
->v
, data
->l
);
507 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph1
->flags
);
514 * send Notification payload (for IPsec SA) in Informational exchange
517 isakmp_info_send_n2(iph2
, type
, data
)
518 struct ph2handle
*iph2
;
522 struct ph1handle
*iph1
= iph2
->ph1
;
523 vchar_t
*payload
= NULL
;
526 struct isakmp_pl_n
*n
;
532 pr
= iph2
->approval
->head
;
534 /* XXX must be get proper spi */
535 tlen
= sizeof(*n
) + pr
->spisize
;
538 payload
= vmalloc(tlen
);
539 if (payload
== NULL
) {
540 plog(LLV_ERROR
, LOCATION
, NULL
,
541 "failed to get buffer to send.\n");
545 n
= (struct isakmp_pl_n
*)payload
->v
;
546 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
547 n
->h
.len
= htons(tlen
);
548 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
549 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
550 n
->spi_size
= pr
->spisize
;
551 n
->type
= htons(type
);
552 *(u_int32_t
*)(n
+ 1) = pr
->spi
;
554 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
556 iph2
->flags
|= ISAKMP_FLAG_E
; /* XXX Should we do FLAG_A ? */
557 error
= isakmp_info_send_common(iph1
, payload
, ISAKMP_NPTYPE_N
, iph2
->flags
);
565 * When ph1->skeyid_a == NULL, send message without encoding.
568 isakmp_info_send_common(iph1
, payload
, np
, flags
)
569 struct ph1handle
*iph1
;
574 struct ph2handle
*iph2
= NULL
;
575 vchar_t
*hash
= NULL
;
576 struct isakmp
*isakmp
;
577 struct isakmp_gen
*gen
;
582 /* add new entry to isakmp status table */
587 iph2
->dst
= dupsaddr(iph1
->remote
);
588 iph2
->src
= dupsaddr(iph1
->local
);
589 switch (iph1
->remote
->sa_family
) {
591 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
592 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
596 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
597 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
601 plog(LLV_ERROR
, LOCATION
, NULL
,
602 "invalid family: %d\n", iph1
->remote
->sa_family
);
607 iph2
->side
= INITIATOR
;
608 iph2
->status
= PHASE2ST_START
;
609 iph2
->msgid
= isakmp_newmsgid2(iph1
);
611 /* get IV and HASH(1) if skeyid_a was generated. */
612 if (iph1
->skeyid_a
!= NULL
) {
613 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
614 if (iph2
->ivm
== NULL
) {
619 /* generate HASH(1) */
620 hash
= oakley_compute_hash1(iph2
->ph1
, iph2
->msgid
, payload
);
626 /* initialized total buffer length */
628 tlen
+= sizeof(*gen
);
630 /* IKE-SA is not established */
633 /* initialized total buffer length */
636 if ((flags
& ISAKMP_FLAG_A
) == 0)
637 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_E
);
639 iph2
->flags
= (hash
== NULL
? 0 : ISAKMP_FLAG_A
);
642 bindph12(iph1
, iph2
);
644 tlen
+= sizeof(*isakmp
) + payload
->l
;
646 /* create buffer for isakmp payload */
647 iph2
->sendbuf
= vmalloc(tlen
);
648 if (iph2
->sendbuf
== NULL
) {
649 plog(LLV_ERROR
, LOCATION
, NULL
,
650 "failed to get buffer to send.\n");
654 /* create isakmp header */
655 isakmp
= (struct isakmp
*)iph2
->sendbuf
->v
;
656 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
657 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
658 isakmp
->np
= hash
== NULL
? (np
& 0xff) : ISAKMP_NPTYPE_HASH
;
659 isakmp
->v
= iph1
->version
;
660 isakmp
->etype
= ISAKMP_ETYPE_INFO
;
661 isakmp
->flags
= iph2
->flags
;
662 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
663 isakmp
->len
= htonl(tlen
);
664 p
= (char *)(isakmp
+ 1);
666 /* create HASH payload */
668 gen
= (struct isakmp_gen
*)p
;
670 gen
->len
= htons(sizeof(*gen
) + hash
->l
);
672 memcpy(p
, hash
->v
, hash
->l
);
677 memcpy(p
, payload
->v
, payload
->l
);
680 #ifdef HAVE_PRINT_ISAKMP_C
681 isakmp_printpacket(iph2
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
685 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
)) {
688 tmp
= oakley_do_encrypt(iph2
->ph1
, iph2
->sendbuf
, iph2
->ivm
->ive
,
691 vfree(iph2
->sendbuf
);
692 iph2
->sendbuf
= NULL
;
695 vfree(iph2
->sendbuf
);
699 /* HDR*, HASH(1), N */
700 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0) {
701 vfree(iph2
->sendbuf
);
702 iph2
->sendbuf
= NULL
;
706 plog(LLV_DEBUG
, LOCATION
, NULL
,
707 "sendto Information %s.\n", s_isakmp_nptype(np
));
710 * don't resend notify message because peer can use Acknowledged
711 * Informational if peer requires the reply of the notify message.
714 /* XXX If Acknowledged Informational required, don't delete ph2handle */
716 vfree(iph2
->sendbuf
);
717 iph2
->sendbuf
= NULL
;
733 * add a notify payload to buffer by reallocating buffer.
734 * If buf == NULL, the function only create a notify payload.
736 * XXX Which is SPI to be included, inbound or outbound ?
739 isakmp_add_pl_n(buf0
, np_p
, type
, pr
, data
)
747 struct isakmp_pl_n
*n
;
752 **np_p
= ISAKMP_NPTYPE_N
;
754 tlen
= sizeof(*n
) + pr
->spisize
;
760 buf
= vrealloc(buf0
, buf0
->l
+ tlen
);
764 plog(LLV_ERROR
, LOCATION
, NULL
,
765 "failed to get a payload buffer.\n");
769 n
= (struct isakmp_pl_n
*)(buf
->v
+ oldlen
);
770 n
->h
.np
= ISAKMP_NPTYPE_NONE
;
771 n
->h
.len
= htons(tlen
);
772 n
->doi
= htonl(IPSEC_DOI
); /* IPSEC DOI (1) */
773 n
->proto_id
= pr
->proto_id
; /* IPSEC AH/ESP/whatever*/
774 n
->spi_size
= pr
->spisize
;
775 n
->type
= htons(type
);
776 *(u_int32_t
*)(n
+ 1) = pr
->spi
; /* XXX */
778 memcpy((caddr_t
)(n
+ 1) + pr
->spisize
, data
->v
, data
->l
);
780 /* save the pointer of next payload type */
787 * handling to receive Notification payload
790 isakmp_info_recv_n(iph1
, msg
)
791 struct ph1handle
*iph1
;
794 struct isakmp_pl_n
*n
= NULL
;
797 struct isakmp_parse_t
*pa
, *pap
;
800 if (!(pbuf
= isakmp_parse(msg
)))
802 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
803 for (pap
= pa
; pap
->type
; pap
++) {
805 case ISAKMP_NPTYPE_HASH
:
806 /* do something here */
808 case ISAKMP_NPTYPE_NONCE
:
811 case ISAKMP_NPTYPE_N
:
812 n
= (struct isakmp_pl_n
*)pap
->ptr
;
823 type
= ntohs(n
->type
);
826 case ISAKMP_NTYPE_CONNECTED
:
827 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
828 case ISAKMP_NTYPE_REPLAY_STATUS
:
831 case ISAKMP_NTYPE_INITIAL_CONTACT
:
832 info_recv_initialcontact(iph1
);
836 u_int32_t msgid
= ((struct isakmp
*)msg
->v
)->msgid
;
837 struct ph2handle
*iph2
;
839 /* XXX there is a potential of dos attack. */
842 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
843 "delete phase1 handle.\n");
846 iph2
= getph2bymsgid(iph1
, msgid
);
848 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
849 "unknown notify message, "
850 "no phase2 handle found.\n");
862 /* get spi and allocate */
863 if (ntohs(n
->h
.len
) < sizeof(*n
) + n
->spi_size
) {
864 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
865 "invalid spi_size in notification payload.\n");
868 spi
= val2str((u_char
*)(n
+ 1), n
->spi_size
);
870 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
871 "notification message %d:%s, "
872 "doi=%d proto_id=%d spi=%s(size=%d).\n",
873 type
, s_isakmp_notify_msg(type
),
874 ntohl(n
->doi
), n
->proto_id
, spi
, n
->spi_size
);
882 purge_isakmp_spi(proto
, spi
, n
)
884 isakmp_index
*spi
; /*network byteorder*/
887 struct ph1handle
*iph1
;
890 for (i
= 0; i
< n
; i
++) {
891 iph1
= getph1byindex(&spi
[i
]);
895 plog(LLV_INFO
, LOCATION
, NULL
,
896 "purged ISAKMP-SA proto_id=%s spi=%s.\n",
897 s_ipsecdoi_proto(proto
),
898 isakmp_pindex(&spi
[i
], 0));
901 SCHED_KILL(iph1
->sce
);
902 iph1
->status
= PHASE1ST_EXPIRED
;
903 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
908 purge_ipsec_spi(dst0
, proto
, spi
, n
)
909 struct sockaddr
*dst0
;
911 u_int32_t
*spi
; /*network byteorder*/
915 struct sadb_msg
*msg
, *next
, *end
;
917 struct sockaddr
*src
, *dst
;
918 struct ph2handle
*iph2
;
920 caddr_t mhp
[SADB_EXT_MAX
+ 1];
922 buf
= pfkey_dump_sadb(ipsecdoi2pfkey_proto(proto
));
924 plog(LLV_DEBUG
, LOCATION
, NULL
,
925 "pfkey_dump_sadb returned nothing.\n");
929 msg
= (struct sadb_msg
*)buf
->v
;
930 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
933 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
935 next
= (struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
936 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
941 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
942 plog(LLV_ERROR
, LOCATION
, NULL
,
943 "pfkey_check (%s)\n", ipsec_strerror());
948 sa
= (struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
950 || !mhp
[SADB_EXT_ADDRESS_SRC
]
951 || !mhp
[SADB_EXT_ADDRESS_DST
]) {
955 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
956 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
958 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
959 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
964 /* XXX n^2 algorithm, inefficient */
966 /* don't delete inbound SAs at the moment */
967 /* XXX should we remove SAs with opposite direction as well? */
968 if (cmpsaddrwop(dst0
, dst
)) {
973 for (i
= 0; i
< n
; i
++) {
974 plog(LLV_DEBUG
, LOCATION
, NULL
,
975 "check spi(packet)=%u spi(db)=%u.\n",
976 ntohl(spi
[i
]), ntohl(sa
->sadb_sa_spi
));
977 if (spi
[i
] != sa
->sadb_sa_spi
)
980 pfkey_send_delete(lcconf
->sock_pfkey
,
981 msg
->sadb_msg_satype
,
983 src
, dst
, sa
->sadb_sa_spi
);
986 * delete a relative phase 2 handler.
987 * continue to process if no relative phase 2 handler
990 iph2
= getph2bysaidx(src
, dst
, proto
, spi
[i
]);
997 plog(LLV_INFO
, LOCATION
, NULL
,
998 "purged IPsec-SA proto_id=%s spi=%u.\n",
999 s_ipsecdoi_proto(proto
),
1011 * delete all phase2 sa relatived to the destination address.
1012 * Don't delete Phase 1 handlers on INITIAL-CONTACT, and don't ignore
1013 * an INITIAL-CONTACT if we have contacted the peer. This matches the
1014 * Sun IKE behavior, and makes rekeying work much better when the peer
1018 info_recv_initialcontact(iph1
)
1019 struct ph1handle
*iph1
;
1021 vchar_t
*buf
= NULL
;
1022 struct sadb_msg
*msg
, *next
, *end
;
1024 struct sockaddr
*src
, *dst
;
1025 caddr_t mhp
[SADB_EXT_MAX
+ 1];
1027 struct ph2handle
*iph2
;
1036 loc
= strdup(saddrwop2str(iph1
->local
));
1037 rem
= strdup(saddrwop2str(iph1
->remote
));
1040 * Purge all IPSEC-SAs for the peer. We can do this
1041 * the easy way (using a PF_KEY SADB_DELETE extension)
1042 * or we can do it the hard way.
1044 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1045 proto_id
= pfkey2ipsecdoi_proto(pfkey_satypes
[i
].ps_satype
);
1047 plog(LLV_INFO
, LOCATION
, NULL
,
1048 "purging %s SAs for %s -> %s\n",
1049 pfkey_satypes
[i
].ps_name
, loc
, rem
);
1050 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1051 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1052 iph1
->local
, iph1
->remote
) == -1) {
1053 plog(LLV_ERROR
, LOCATION
, NULL
,
1054 "delete_all %s -> %s failed for %s (%s)\n",
1056 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1060 deleteallph2(iph1
->local
, iph1
->remote
, proto_id
);
1062 plog(LLV_INFO
, LOCATION
, NULL
,
1063 "purging %s SAs for %s -> %s\n",
1064 pfkey_satypes
[i
].ps_name
, rem
, loc
);
1065 if (pfkey_send_delete_all(lcconf
->sock_pfkey
,
1066 pfkey_satypes
[i
].ps_satype
, IPSEC_MODE_ANY
,
1067 iph1
->remote
, iph1
->local
) == -1) {
1068 plog(LLV_ERROR
, LOCATION
, NULL
,
1069 "delete_all %s -> %s failed for %s (%s)\n",
1071 pfkey_satypes
[i
].ps_name
, ipsec_strerror());
1075 deleteallph2(iph1
->remote
, iph1
->local
, proto_id
);
1087 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
1089 plog(LLV_DEBUG
, LOCATION
, NULL
,
1090 "pfkey_dump_sadb returned nothing.\n");
1094 msg
= (struct sadb_msg
*)buf
->v
;
1095 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
1098 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
1100 next
= (struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
1101 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
1106 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
1107 plog(LLV_ERROR
, LOCATION
, NULL
,
1108 "pfkey_check (%s)\n", ipsec_strerror());
1113 if (mhp
[SADB_EXT_SA
] == NULL
1114 || mhp
[SADB_EXT_ADDRESS_SRC
] == NULL
1115 || mhp
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1119 sa
= (struct sadb_sa
*)mhp
[SADB_EXT_SA
];
1120 src
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
1121 dst
= PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
1123 if (sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
1124 && sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
1130 * RFC2407 4.6.3.3 INITIAL-CONTACT is the message that
1131 * announces the sender of the message was rebooted.
1132 * it is interpreted to delete all SAs which source address
1133 * is the sender of the message.
1134 * racoon only deletes SA which is matched both the
1135 * source address and the destination accress.
1137 if (cmpsaddrwop(iph1
->local
, src
) == 0 &&
1138 cmpsaddrwop(iph1
->remote
, dst
) == 0)
1140 else if (cmpsaddrwop(iph1
->remote
, src
) == 0 &&
1141 cmpsaddrwop(iph1
->local
, dst
) == 0)
1149 * Make sure this is an SATYPE that we manage.
1150 * This is gross; too bad we couldn't do it the
1153 for (i
= 0; i
< pfkey_nsatypes
; i
++) {
1154 if (pfkey_satypes
[i
].ps_satype
==
1155 msg
->sadb_msg_satype
)
1158 if (i
== pfkey_nsatypes
) {
1163 plog(LLV_INFO
, LOCATION
, NULL
,
1164 "purging spi=%u.\n", ntohl(sa
->sadb_sa_spi
));
1165 pfkey_send_delete(lcconf
->sock_pfkey
,
1166 msg
->sadb_msg_satype
,
1167 IPSEC_MODE_ANY
, src
, dst
, sa
->sadb_sa_spi
);
1170 * delete a relative phase 2 handler.
1171 * continue to process if no relative phase 2 handler
1174 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
1175 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
1189 * handling to receive Deletion payload
1192 isakmp_info_recv_d(iph1
, msg
)
1193 struct ph1handle
*iph1
;
1196 struct isakmp_pl_d
*d
;
1199 struct isakmp_parse_t
*pa
, *pap
;
1206 /* validate the type of next payload */
1207 if (!(pbuf
= isakmp_parse(msg
)))
1209 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
1210 for (pap
= pa
; pap
->type
; pap
++) {
1211 switch (pap
->type
) {
1212 case ISAKMP_NPTYPE_D
:
1214 case ISAKMP_NPTYPE_HASH
:
1219 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1220 "received next payload type %d "
1221 "in wrong place (must be the first payload).\n",
1226 /* don't send information, see isakmp_ident_r1() */
1227 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1228 "reject the packet, "
1229 "received unexpecting payload type %d.\n",
1237 plog(LLV_ERROR
, LOCATION
, NULL
,
1238 "delete payload is not proteted, "
1244 /* process a delete payload */
1245 for (pap
= pa
; pap
->type
; pap
++) {
1246 if (pap
->type
!= ISAKMP_NPTYPE_D
)
1249 d
= (struct isakmp_pl_d
*)pap
->ptr
;
1251 if (ntohl(d
->doi
) != IPSEC_DOI
) {
1252 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1253 "delete payload with invalid doi:%d.\n",
1258 num_spi
= ntohs(d
->num_spi
);
1259 tlen
= ntohs(d
->h
.len
) - sizeof(struct isakmp_pl_d
);
1261 if (tlen
!= num_spi
* d
->spi_size
) {
1262 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1263 "deletion payload with invalid length.\n");
1268 switch (d
->proto_id
) {
1269 case IPSECDOI_PROTO_ISAKMP
:
1270 if (d
->spi_size
!= sizeof(isakmp_index
)) {
1271 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1272 "delete payload with strange spi "
1273 "size %d(proto_id:%d)\n",
1274 d
->spi_size
, d
->proto_id
);
1277 purge_isakmp_spi(d
->proto_id
,
1278 (isakmp_index
*)(d
+ 1), num_spi
);
1281 case IPSECDOI_PROTO_IPSEC_AH
:
1282 case IPSECDOI_PROTO_IPSEC_ESP
:
1283 if (d
->spi_size
!= sizeof(u_int32_t
)) {
1284 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1285 "delete payload with strange spi "
1286 "size %d(proto_id:%d)\n",
1287 d
->spi_size
, d
->proto_id
);
1290 purge_ipsec_spi(iph1
->remote
, d
->proto_id
,
1291 (u_int32_t
*)(d
+ 1), num_spi
);
1294 case IPSECDOI_PROTO_IPCOMP
:
1295 /* need to handle both 16bit/32bit SPI */
1296 memset(&spi
, 0, sizeof(spi
));
1297 if (d
->spi_size
== sizeof(spi
.spi16
[1])) {
1298 memcpy(&spi
.spi16
[1], d
+ 1,
1299 sizeof(spi
.spi16
[1]));
1300 } else if (d
->spi_size
== sizeof(spi
.spi32
))
1301 memcpy(&spi
.spi32
, d
+ 1, sizeof(spi
.spi32
));
1303 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1304 "delete payload with strange spi "
1305 "size %d(proto_id:%d)\n",
1306 d
->spi_size
, d
->proto_id
);
1309 purge_ipsec_spi(iph1
->remote
, d
->proto_id
,
1310 &spi
.spi32
, num_spi
);
1314 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1315 "deletion message received, "
1316 "invalid proto_id: %d\n",
1321 plog(LLV_DEBUG
, LOCATION
, NULL
, "purged SAs.\n");
1330 isakmp_check_notify(gen
, iph1
)
1331 struct isakmp_gen
*gen
; /* points to Notify payload */
1332 struct ph1handle
*iph1
;
1334 struct isakmp_pl_n
*notify
= (struct isakmp_pl_n
*)gen
;
1336 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
1337 "Notify Message received\n");
1339 switch (ntohs(notify
->type
)) {
1340 case ISAKMP_NTYPE_CONNECTED
:
1341 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1342 "ignore CONNECTED notification.\n");
1344 case ISAKMP_NTYPE_RESPONDER_LIFETIME
:
1345 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1346 "ignore RESPONDER-LIFETIME notification.\n");
1348 case ISAKMP_NTYPE_REPLAY_STATUS
:
1349 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1350 "ignore REPLAY-STATUS notification.\n");
1352 case ISAKMP_NTYPE_INITIAL_CONTACT
:
1353 plog(LLV_WARNING
, LOCATION
, iph1
->remote
,
1354 "ignore INITIAL-CONTACT notification, "
1355 "because it is only accepted after phase1.\n");
1358 isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_INVALID_PAYLOAD_TYPE
, NULL
);
1359 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1360 "received unknown notification type %u.\n",
1361 ntohs(notify
->type
));