1 /* $KAME: isakmp.c,v 1.171 2001/12/12 22:35: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>
35 #include <sys/queue.h>
37 #include <netkey/key_var.h>
38 #include <netinet/in.h>
44 #if TIME_WITH_SYS_TIME
45 # include <sys/time.h>
49 # include <sys/time.h>
60 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
72 #include "remoteconf.h"
73 #include "localconf.h"
74 #include "grabmyaddr.h"
75 #include "isakmp_var.h"
79 #include "ipsec_doi.h"
81 #include "crypto_openssl.h"
83 #include "isakmp_ident.h"
84 #include "isakmp_agg.h"
85 #include "isakmp_base.h"
86 #include "isakmp_quick.h"
87 #include "isakmp_inf.h"
88 #include "isakmp_newg.h"
91 static int nostate1
__P((struct ph1handle
*, vchar_t
*));
92 static int nostate2
__P((struct ph2handle
*, vchar_t
*));
94 extern caddr_t
val2str(const char *, size_t);
96 static int (*ph1exchange
[][2][PHASE1ST_MAX
])
97 __P((struct ph1handle
*, vchar_t
*)) = {
100 /* Identity Protection exchange */
102 { nostate1
, ident_i1send
, nostate1
, ident_i2recv
, ident_i2send
,
103 ident_i3recv
, ident_i3send
, ident_i4recv
, ident_i4send
, nostate1
, },
104 { nostate1
, ident_r1recv
, ident_r1send
, ident_r2recv
, ident_r2send
,
105 ident_r3recv
, ident_r3send
, nostate1
, nostate1
, nostate1
, },
107 /* Aggressive exchange */
109 { nostate1
, agg_i1send
, nostate1
, agg_i2recv
, agg_i2send
,
110 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
111 { nostate1
, agg_r1recv
, agg_r1send
, agg_r2recv
, agg_r2send
,
112 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
116 { nostate1
, base_i1send
, nostate1
, base_i2recv
, base_i2send
,
117 base_i3recv
, base_i3send
, nostate1
, nostate1
, nostate1
, },
118 { nostate1
, base_r1recv
, base_r1send
, base_r2recv
, base_r2send
,
119 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
123 static int (*ph2exchange
[][2][PHASE2ST_MAX
])
124 __P((struct ph2handle
*, vchar_t
*)) = {
127 /* Quick mode for IKE*/
129 { nostate2
, nostate2
, quick_i1prep
, nostate2
, quick_i1send
,
130 quick_i2recv
, quick_i2send
, quick_i3recv
, nostate2
, nostate2
, },
131 { nostate2
, quick_r1recv
, quick_r1prep
, nostate2
, quick_r2send
,
132 quick_r3recv
, quick_r3prep
, quick_r3send
, nostate2
, nostate2
, }
136 static u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
138 static int isakmp_main
__P((vchar_t
*, struct sockaddr
*, struct sockaddr
*));
139 static int ph1_main
__P((struct ph1handle
*, vchar_t
*));
140 static int quick_main
__P((struct ph2handle
*, vchar_t
*));
141 static int isakmp_ph1begin_r
__P((vchar_t
*,
142 struct sockaddr
*, struct sockaddr
*, u_int8_t
));
143 static int isakmp_ph2begin_i
__P((struct ph1handle
*, struct ph2handle
*));
144 static int isakmp_ph2begin_r
__P((struct ph1handle
*, vchar_t
*));
145 static int etypesw1
__P((int));
146 static int etypesw2
__P((int));
149 * isakmp packet handler
152 isakmp_handler(so_isakmp
)
155 struct isakmp isakmp
;
156 struct sockaddr_storage remote
;
157 struct sockaddr_storage local
;
158 int remote_len
= sizeof(remote
);
159 int local_len
= sizeof(local
);
165 /* read message by MSG_PEEK */
166 while ((len
= recvfromto(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
167 MSG_PEEK
, (struct sockaddr
*)&remote
, &remote_len
,
168 (struct sockaddr
*)&local
, &local_len
)) < 0) {
171 plog(LLV_ERROR
, LOCATION
, NULL
,
172 "failed to receive isakmp packet\n");
176 /* check isakmp header length */
177 if (len
< sizeof(isakmp
)) {
178 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
179 "packet shorter than isakmp header size.\n");
181 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
182 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
183 plog(LLV_ERROR
, LOCATION
, NULL
,
184 "failed to receive isakmp packet\n");
189 /* read real message */
190 if ((buf
= vmalloc(ntohl(isakmp
.len
))) == NULL
) {
191 plog(LLV_ERROR
, LOCATION
, NULL
,
192 "failed to allocate reading buffer\n");
194 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
195 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
196 plog(LLV_ERROR
, LOCATION
, NULL
,
197 "failed to receive isakmp packet\n");
202 while ((len
= recvfromto(so_isakmp
, buf
->v
, buf
->l
,
203 0, (struct sockaddr
*)&remote
, &remote_len
,
204 (struct sockaddr
*)&local
, &local_len
)) < 0) {
207 plog(LLV_ERROR
, LOCATION
, NULL
,
208 "failed to receive isakmp packet\n");
213 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
214 "received invalid length, why ?\n");
218 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
219 plog(LLV_DEBUG
, LOCATION
, (struct sockaddr
*)&local
,
220 "%d bytes message received from %s\n",
221 len
, saddr2str((struct sockaddr
*)&remote
));
222 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
224 /* avoid packets with malicious port/address */
225 switch (remote
.ss_family
) {
227 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
231 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
235 plog(LLV_ERROR
, LOCATION
, NULL
,
236 "invalid family: %d\n", remote
.ss_family
);
240 plog(LLV_ERROR
, LOCATION
, (struct sockaddr
*)&remote
,
241 "src port == 0 (valid as UDP but not with IKE)\n");
245 /* XXX: check sender whether to be allowed or not to accept */
247 /* XXX: I don't know how to check isakmp half connection attack. */
249 /* simply reply if the packet was processed. */
250 if (check_recvdpkt((struct sockaddr
*)&remote
,
251 (struct sockaddr
*)&local
, buf
)) {
252 plog(LLV_NOTIFY
, LOCATION
, NULL
,
253 "the packet is retransmitted by %s.\n",
254 saddr2str((struct sockaddr
*)&remote
));
259 /* isakmp main routine */
260 if (isakmp_main(buf
, (struct sockaddr
*)&remote
,
261 (struct sockaddr
*)&local
) != 0) goto end
;
273 * main processing to handle isakmp payload
276 isakmp_main(msg
, remote
, local
)
278 struct sockaddr
*remote
, *local
;
280 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
281 isakmp_index
*index
= (isakmp_index
*)isakmp
;
282 u_int32_t msgid
= isakmp
->msgid
;
283 struct ph1handle
*iph1
;
285 #ifdef HAVE_PRINT_ISAKMP_C
286 isakmp_printpacket(msg
, remote
, local
, 0);
289 /* the initiator's cookie must not be zero */
290 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
291 plog(LLV_ERROR
, LOCATION
, remote
,
292 "malformed cookie received.\n");
296 /* Check the Major and Minor Version fields. */
298 * XXX Is is right to check version here ?
299 * I think it may no be here because the version depends
300 * on exchange status.
302 if (isakmp
->v
< ISAKMP_VERSION_NUMBER
) {
303 if (ISAKMP_GETMAJORV(isakmp
->v
) < ISAKMP_MAJOR_VERSION
) {
304 plog(LLV_ERROR
, LOCATION
, remote
,
305 "invalid major version %d.\n",
306 ISAKMP_GETMAJORV(isakmp
->v
));
309 #if ISAKMP_MINOR_VERSION > 0
310 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) {
311 plog(LLV_ERROR
, LOCATION
, remote
,
312 "invalid minor version %d.\n",
313 ISAKMP_GETMINORV(isakmp
->v
));
319 /* check the Flags field. */
320 /* XXX How is the exclusive check, E and A ? */
321 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
322 plog(LLV_ERROR
, LOCATION
, remote
,
323 "invalid flag 0x%02x.\n", isakmp
->flags
);
327 /* ignore commit bit. */
328 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
329 if (isakmp
->msgid
== 0) {
330 isakmp_info_send_nx(isakmp
, remote
, local
,
331 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
332 plog(LLV_ERROR
, LOCATION
, remote
,
333 "Commit bit on phase1 forbidden.\n");
338 iph1
= getph1byindex(index
);
341 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
342 iph1
->side
== INITIATOR
) {
343 plog(LLV_DEBUG
, LOCATION
, remote
,
344 "malformed cookie received or "
345 "the initiator's cookies collide.\n");
349 /* must be same addresses in one stream of a phase at least. */
350 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
351 char *saddr_db
, *saddr_act
;
353 saddr_db
= strdup(saddr2str(iph1
->remote
));
354 saddr_act
= strdup(saddr2str(remote
));
356 plog(LLV_WARNING
, LOCATION
, remote
,
357 "remote address mismatched. db=%s, act=%s\n",
358 saddr_db
, saddr_act
);
360 racoon_free(saddr_db
);
361 racoon_free(saddr_act
);
364 * don't check of exchange type here because other type will be
365 * with same index, for example, informational exchange.
368 /* XXX more acceptable check */
371 switch (isakmp
->etype
) {
372 case ISAKMP_ETYPE_IDENT
:
373 case ISAKMP_ETYPE_AGG
:
374 case ISAKMP_ETYPE_BASE
:
375 /* phase 1 validity check */
376 if (isakmp
->msgid
!= 0) {
377 plog(LLV_ERROR
, LOCATION
, remote
,
378 "message id should be zero in phase1.\n");
382 /* search for isakmp status record of phase 1 */
385 * the packet must be the 1st message from a initiator
386 * or the 2nd message from the responder.
389 /* search for phase1 handle by index without r_ck */
390 iph1
= getph1byindex0(index
);
392 /*it must be the 1st message from a initiator.*/
393 if (memcmp(&isakmp
->r_ck
, r_ck0
,
394 sizeof(cookie_t
)) != 0) {
396 plog(LLV_DEBUG
, LOCATION
, remote
,
397 "malformed cookie received "
398 "or the spi expired.\n");
402 /* it must be responder's 1st exchange. */
403 if (isakmp_ph1begin_r(msg
, remote
, local
,
411 /* it must be the 2nd message from the responder. */
412 if (iph1
->side
!= INITIATOR
) {
413 plog(LLV_DEBUG
, LOCATION
, remote
,
414 "malformed cookie received. "
415 "it has to be as the initiator. %s\n",
416 isakmp_pindex(&iph1
->index
, 0));
422 * Don't delete phase 1 handler when the exchange type
423 * in handler is not equal to packet's one because of no
424 * authencication completed.
426 if (iph1
->etype
!= isakmp
->etype
) {
427 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
428 "exchange type is mismatched: "
429 "db=%s packet=%s, ignore it.\n",
430 s_isakmp_etype(iph1
->etype
),
431 s_isakmp_etype(isakmp
->etype
));
435 /* call main process of phase 1 */
436 if (ph1_main(iph1
, msg
) < 0) {
437 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
438 "phase1 negotiation failed.\n");
445 case ISAKMP_ETYPE_AUTH
:
446 plog(LLV_INFO
, LOCATION
, remote
,
447 "unsupported exchange %d received.\n",
451 case ISAKMP_ETYPE_INFO
:
452 case ISAKMP_ETYPE_ACKINFO
:
454 * iph1 must be present for Information message.
455 * if iph1 is null then trying to get the phase1 status
456 * as the packet from responder againt initiator's 1st
457 * exchange in phase 1.
458 * NOTE: We think such informational exchange should be ignored.
461 iph1
= getph1byindex0(index
);
463 plog(LLV_ERROR
, LOCATION
, remote
,
464 "unknown Informational "
465 "exchange received.\n");
468 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
469 plog(LLV_WARNING
, LOCATION
, remote
,
470 "remote address mismatched. "
472 saddr2str(iph1
->remote
));
476 if (isakmp_info_recv(iph1
, msg
) < 0)
480 case ISAKMP_ETYPE_QUICK
:
482 struct ph2handle
*iph2
;
485 isakmp_info_send_nx(isakmp
, remote
, local
,
486 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
487 plog(LLV_ERROR
, LOCATION
, remote
,
488 "can't start the quick mode, "
489 "there is no ISAKMP-SA, %s\n",
490 isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
495 /* check status of phase 1 whether negotiated or not. */
496 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
497 plog(LLV_ERROR
, LOCATION
, remote
,
498 "can't start the quick mode, "
499 "there is no valid ISAKMP-SA, %s\n",
500 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
504 /* search isakmp phase 2 stauts record. */
505 iph2
= getph2bymsgid(iph1
, msgid
);
507 /* it must be new negotiation as responder */
508 if (isakmp_ph2begin_r(iph1
, msg
) < 0)
516 * we keep to set commit bit during negotiation.
517 * When SA is configured, bit will be reset.
519 * don't initiate commit bit. should be fixed in the future.
521 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
522 iph2
->flags
|= ISAKMP_FLAG_C
;
524 /* call main process of quick mode */
525 if (quick_main(iph2
, msg
) < 0) {
526 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
527 "phase2 negotiation failed.\n");
536 case ISAKMP_ETYPE_NEWGRP
:
538 plog(LLV_ERROR
, LOCATION
, remote
,
539 "Unknown new group mode exchange, "
540 "there is no ISAKMP-SA.\n");
543 isakmp_newgroup_r(iph1
, msg
);
546 case ISAKMP_ETYPE_NONE
:
548 plog(LLV_ERROR
, LOCATION
, NULL
,
549 "Invalid exchange type %d from %s.\n",
550 isakmp
->etype
, saddr2str(remote
));
558 * main function of phase 1.
562 struct ph1handle
*iph1
;
567 struct timeval start
, end
;
570 /* ignore a packet */
571 if (iph1
->status
== PHASE1ST_ESTABLISHED
)
575 gettimeofday(&start
, NULL
);
578 if (ph1exchange
[etypesw1(iph1
->etype
)]
580 [iph1
->status
] == NULL
) {
581 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
582 "why isn't the function defined.\n");
585 error
= (ph1exchange
[etypesw1(iph1
->etype
)]
587 [iph1
->status
])(iph1
, msg
);
591 * When an invalid packet is received on phase1, it should
592 * be selected to process this packet. That is to respond
593 * with a notify and delete phase 1 handler, OR not to respond
594 * and keep phase 1 handler.
596 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
597 "failed to pre-process packet.\n");
600 /* ignore the error and keep phase 1 handler */
605 /* free resend buffer */
606 if (iph1
->sendbuf
== NULL
) {
607 plog(LLV_ERROR
, LOCATION
, NULL
,
608 "no buffer found as sendbuf\n");
611 vfree(iph1
->sendbuf
);
612 iph1
->sendbuf
= NULL
;
614 /* turn off schedule */
616 SCHED_KILL(iph1
->scr
);
619 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
620 if ((ph1exchange
[etypesw1(iph1
->etype
)]
622 [iph1
->status
])(iph1
, msg
) != 0) {
623 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
624 "failed to process packet.\n");
629 gettimeofday(&end
, NULL
);
630 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
631 "phase1", s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
632 timedelta(&start
, &end
));
634 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
637 gettimeofday(&iph1
->end
, NULL
);
638 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
639 "phase1", s_isakmp_etype(iph1
->etype
),
640 timedelta(&iph1
->start
, &iph1
->end
));
643 /* save created date. */
644 (void)time(&iph1
->created
);
646 /* add to the schedule to expire, and seve back pointer. */
647 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
648 isakmp_ph1expire_stub
, iph1
);
650 /* INITIAL-CONTACT processing */
651 /* don't anything if local test mode. */
653 && iph1
->rmconf
->ini_contact
&& !getcontacted(iph1
->remote
)) {
654 /* send INITIAL-CONTACT */
655 isakmp_info_send_n1(iph1
,
656 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
657 /* insert a node into contacted list. */
658 if (inscontacted(iph1
->remote
) == -1) {
659 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
660 "failed to add contacted list.\n");
665 log_ph1established(iph1
);
666 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
673 * main function of quick mode.
676 quick_main(iph2
, msg
)
677 struct ph2handle
*iph2
;
680 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
683 struct timeval start
, end
;
686 /* ignore a packet */
687 if (iph2
->status
== PHASE2ST_ESTABLISHED
688 || iph2
->status
== PHASE2ST_GETSPISENT
)
692 gettimeofday(&start
, NULL
);
696 if (ph2exchange
[etypesw2(isakmp
->etype
)]
698 [iph2
->status
] == NULL
) {
699 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
700 "why isn't the function defined.\n");
703 error
= (ph2exchange
[etypesw2(isakmp
->etype
)]
705 [iph2
->status
])(iph2
, msg
);
707 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
708 "failed to pre-process packet.\n");
709 if (error
== ISAKMP_INTERNAL_ERROR
)
711 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
715 /* when using commit bit, status will be reached here. */
716 if (iph2
->status
== PHASE2ST_ADDSA
)
719 /* free resend buffer */
720 if (iph2
->sendbuf
== NULL
) {
721 plog(LLV_ERROR
, LOCATION
, NULL
,
722 "no buffer found as sendbuf\n");
725 vfree(iph2
->sendbuf
);
726 iph2
->sendbuf
= NULL
;
728 /* turn off schedule */
730 SCHED_KILL(iph2
->scr
);
733 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
734 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
736 [iph2
->status
])(iph2
, msg
) != 0) {
737 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
738 "failed to process packet.\n");
743 gettimeofday(&end
, NULL
);
744 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
746 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
747 timedelta(&start
, &end
));
753 /* new negotiation of phase 1 for initiator */
755 isakmp_ph1begin_i(rmconf
, remote
)
756 struct remoteconf
*rmconf
;
757 struct sockaddr
*remote
;
759 struct ph1handle
*iph1
;
761 struct timeval start
, end
;
764 /* get new entry to isakmp status table. */
769 iph1
->status
= PHASE1ST_START
;
770 iph1
->rmconf
= rmconf
;
771 iph1
->side
= INITIATOR
;
772 iph1
->version
= ISAKMP_VERSION_NUMBER
;
777 iph1
->gssapi_state
= NULL
;
779 iph1
->approval
= NULL
;
781 /* XXX copy remote address */
782 if (copy_ph1addresses(iph1
, rmconf
, remote
, NULL
) < 0)
787 /* start phase 1 exchange */
788 iph1
->etype
= rmconf
->etypes
->type
;
790 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
794 a
= strdup(saddr2str(iph1
->local
));
795 plog(LLV_INFO
, LOCATION
, NULL
,
796 "initiate new phase 1 negotiation: %s<=>%s\n",
797 a
, saddr2str(iph1
->remote
));
800 plog(LLV_INFO
, LOCATION
, NULL
,
802 s_isakmp_etype(iph1
->etype
));
805 gettimeofday(&iph1
->start
, NULL
);
806 gettimeofday(&start
, NULL
);
809 if ((ph1exchange
[etypesw1(iph1
->etype
)]
811 [iph1
->status
])(iph1
, NULL
) != 0) {
812 /* failed to start phase 1 negotiation */
820 gettimeofday(&end
, NULL
);
821 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
823 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
824 timedelta(&start
, &end
));
830 /* new negotiation of phase 1 for responder */
832 isakmp_ph1begin_r(msg
, remote
, local
, etype
)
834 struct sockaddr
*remote
, *local
;
837 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
838 struct remoteconf
*rmconf
;
839 struct ph1handle
*iph1
;
840 struct etypes
*etypeok
;
842 struct timeval start
, end
;
845 /* look for my configuration */
846 rmconf
= getrmconf(remote
);
847 if (rmconf
== NULL
) {
848 plog(LLV_ERROR
, LOCATION
, remote
,
854 /* check to be acceptable exchange type */
855 etypeok
= check_etypeok(rmconf
, etype
);
856 if (etypeok
== NULL
) {
857 plog(LLV_ERROR
, LOCATION
, remote
,
858 "not acceptable %s mode\n", s_isakmp_etype(etype
));
862 /* get new entry to isakmp status table. */
867 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
868 iph1
->status
= PHASE1ST_START
;
869 iph1
->rmconf
= rmconf
;
871 iph1
->side
= RESPONDER
;
872 iph1
->etype
= etypeok
->type
;
873 iph1
->version
= isakmp
->v
;
876 iph1
->gssapi_state
= NULL
;
878 iph1
->approval
= NULL
;
880 /* copy remote address */
881 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0)
886 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
890 a
= strdup(saddr2str(iph1
->local
));
891 plog(LLV_INFO
, LOCATION
, NULL
,
892 "respond new phase 1 negotiation: %s<=>%s\n",
893 a
, saddr2str(iph1
->remote
));
896 plog(LLV_INFO
, LOCATION
, NULL
,
897 "begin %s mode.\n", s_isakmp_etype(etype
));
900 gettimeofday(&iph1
->start
, NULL
);
901 gettimeofday(&start
, NULL
);
904 if ((ph1exchange
[etypesw1(iph1
->etype
)]
906 [iph1
->status
])(iph1
, msg
) < 0
907 || (ph1exchange
[etypesw1(iph1
->etype
)]
909 [iph1
->status
])(iph1
, msg
) < 0) {
910 plog(LLV_ERROR
, LOCATION
, remote
,
911 "failed to process packet.\n");
917 gettimeofday(&end
, NULL
);
918 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
920 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
921 timedelta(&start
, &end
));
927 /* new negotiation of phase 2 for initiator */
929 isakmp_ph2begin_i(iph1
, iph2
)
930 struct ph1handle
*iph1
;
931 struct ph2handle
*iph2
;
933 /* found ISAKMP-SA. */
934 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
935 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
938 a
= strdup(saddr2str(iph2
->src
));
939 plog(LLV_INFO
, LOCATION
, NULL
,
940 "initiate new phase 2 negotiation: %s<=>%s\n",
941 a
, saddr2str(iph2
->dst
));
946 gettimeofday(&iph2
->start
, NULL
);
948 /* found isakmp-sa */
949 bindph12(iph1
, iph2
);
950 iph2
->status
= PHASE2ST_STATUS2
;
952 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
954 [iph2
->status
])(iph2
, NULL
) < 0) {
956 /* release ipsecsa handler due to internal error. */
964 /* new negotiation of phase 2 for responder */
966 isakmp_ph2begin_r(iph1
, msg
)
967 struct ph1handle
*iph1
;
970 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
971 struct ph2handle
*iph2
= 0;
974 struct timeval start
, end
;
979 plog(LLV_ERROR
, LOCATION
, NULL
,
980 "failed to allocate phase2 entry.\n");
985 iph2
->side
= RESPONDER
;
986 iph2
->status
= PHASE2ST_START
;
987 iph2
->flags
= isakmp
->flags
;
988 iph2
->msgid
= isakmp
->msgid
;
989 iph2
->seq
= pk_getseq();
990 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
991 if (iph2
->ivm
== NULL
) {
995 iph2
->dst
= dupsaddr(iph1
->remote
); /* XXX should be considered */
996 if (iph2
->dst
== NULL
) {
1000 switch (iph2
->dst
->sa_family
) {
1002 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1006 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1010 plog(LLV_ERROR
, LOCATION
, NULL
,
1011 "invalid family: %d\n", iph2
->dst
->sa_family
);
1016 iph2
->src
= dupsaddr(iph1
->local
); /* XXX should be considered */
1017 if (iph2
->src
== NULL
) {
1021 switch (iph2
->src
->sa_family
) {
1023 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1027 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1031 plog(LLV_ERROR
, LOCATION
, NULL
,
1032 "invalid family: %d\n", iph2
->src
->sa_family
);
1037 /* add new entry to isakmp status table */
1039 bindph12(iph1
, iph2
);
1041 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1045 a
= strdup(saddr2str(iph2
->src
));
1046 plog(LLV_INFO
, LOCATION
, NULL
,
1047 "respond new phase 2 negotiation: %s<=>%s\n",
1048 a
, saddr2str(iph2
->dst
));
1053 gettimeofday(&start
, NULL
);
1056 error
= (ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1058 [iph2
->status
])(iph2
, msg
);
1060 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1061 "failed to pre-process packet.\n");
1062 if (error
!= ISAKMP_INTERNAL_ERROR
)
1063 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
1065 * release handler because it's wrong that ph2handle is kept
1066 * after failed to check message for responder's.
1075 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1076 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
1078 [iph2
->status
])(iph2
, msg
) < 0) {
1079 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1080 "failed to process packet.\n");
1081 /* don't release handler */
1085 gettimeofday(&end
, NULL
);
1086 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1088 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1089 timedelta(&start
, &end
));
1096 * parse ISAKMP payloads, without ISAKMP base header.
1099 isakmp_parsewoh(np0
, gen
, len
)
1101 struct isakmp_gen
*gen
;
1104 u_char np
= np0
& 0xff;
1107 struct isakmp_parse_t
*p
, *ep
;
1109 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
1112 * 5 is a magic number, but any value larger than 2 should be fine
1113 * as we do vrealloc() in the following loop.
1115 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1116 if (result
== NULL
) {
1117 plog(LLV_ERROR
, LOCATION
, NULL
,
1118 "failed to get buffer.\n");
1121 p
= (struct isakmp_parse_t
*)result
->v
;
1122 ep
= (struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1126 /* parse through general headers */
1127 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1128 if (tlen
<= sizeof(struct isakmp_gen
)) {
1129 /* don't send information, see isakmp_ident_r1() */
1130 plog(LLV_ERROR
, LOCATION
, NULL
,
1131 "invalid length of payload\n");
1136 plog(LLV_DEBUG
, LOCATION
, NULL
,
1137 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1140 p
->len
= ntohs(gen
->len
);
1141 if (p
->len
== 0 || p
->len
> tlen
) {
1142 plog(LLV_DEBUG
, LOCATION
, NULL
,
1143 "invalid length of payload\n");
1152 off
= p
- (struct isakmp_parse_t
*)result
->v
;
1153 result
= vrealloc(result
, result
->l
* 2);
1154 if (result
== NULL
) {
1155 plog(LLV_DEBUG
, LOCATION
, NULL
,
1156 "failed to realloc buffer.\n");
1160 ep
= (struct isakmp_parse_t
*)
1161 (result
->v
+ result
->l
- sizeof(*ep
));
1162 p
= (struct isakmp_parse_t
*)result
->v
;
1167 plen
= ntohs(gen
->len
);
1168 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1171 p
->type
= ISAKMP_NPTYPE_NONE
;
1175 plog(LLV_DEBUG
, LOCATION
, NULL
, "succeed.\n");
1181 * parse ISAKMP payloads, including ISAKMP base header.
1187 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1188 struct isakmp_gen
*gen
;
1194 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1195 tlen
= buf
->l
- sizeof(struct isakmp
);
1196 result
= isakmp_parsewoh(np
, gen
, tlen
);
1205 /* initialize a isakmp status table */
1213 if (isakmp_open() < 0)
1224 * make strings containing i_cookie + r_cookie + msgid
1227 isakmp_pindex(index
, msgid
)
1228 const isakmp_index
*index
;
1229 const u_int32_t msgid
;
1231 static char buf
[64];
1235 memset(buf
, 0, sizeof(buf
));
1238 p
= (const u_char
*)index
;
1239 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1240 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1252 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1257 /* open ISAKMP sockets. */
1269 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1273 /* warn if wildcard address - should we forbid this? */
1274 switch (p
->addr
->sa_family
) {
1276 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1277 plog(LLV_WARNING
, LOCATION
, NULL
,
1278 "listening to wildcard address,"
1279 "broadcast IKE packet may kill you\n");
1283 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1284 plog(LLV_WARNING
, LOCATION
, NULL
,
1285 "listening to wildcard address, "
1286 "broadcast IKE packet may kill you\n");
1290 plog(LLV_ERROR
, LOCATION
, NULL
,
1291 "unsupported address family %d\n",
1292 lcconf
->default_af
);
1296 if ((p
->sock
= socket(p
->addr
->sa_family
, SOCK_DGRAM
, 0)) < 0) {
1297 plog(LLV_ERROR
, LOCATION
, NULL
,
1298 "socket (%s)\n", strerror(errno
));
1302 /* receive my interface address on inbound packets. */
1303 switch (p
->addr
->sa_family
) {
1305 if (setsockopt(p
->sock
, IPPROTO_IP
, IP_RECVDSTADDR
,
1306 (const void *)&yes
, sizeof(yes
)) < 0) {
1307 plog(LLV_ERROR
, LOCATION
, NULL
,
1308 "setsockopt (%s)\n", strerror(errno
));
1315 #ifdef IPV6_RECVPKTINFO
1316 pktinfo
= IPV6_RECVPKTINFO
;
1317 #else /* old adv. API */
1318 pktinfo
= IPV6_PKTINFO
;
1319 #endif /* IPV6_RECVPKTINFO */
1321 pktinfo
= IPV6_RECVDSTADDR
;
1323 if (setsockopt(p
->sock
, IPPROTO_IPV6
, pktinfo
,
1324 (const void *)&yes
, sizeof(yes
)) < 0)
1326 plog(LLV_ERROR
, LOCATION
, NULL
,
1327 "setsockopt(%d): %s\n",
1328 pktinfo
, strerror(errno
));
1335 #ifdef IPV6_USE_MIN_MTU
1336 if (p
->addr
->sa_family
== AF_INET6
&&
1337 setsockopt(p
->sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1338 (void *)&yes
, sizeof(yes
)) < 0) {
1339 plog(LLV_ERROR
, LOCATION
, NULL
,
1340 "setsockopt (%s)\n", strerror(errno
));
1345 if (setsockopt_bypass(p
->sock
, p
->addr
->sa_family
) < 0)
1348 if (bind(p
->sock
, p
->addr
, p
->addr
->sa_len
) < 0) {
1349 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1350 "failed to bind (%s).\n", strerror(errno
));
1357 plog(LLV_INFO
, LOCATION
, NULL
,
1358 "%s used as isakmp port (fd=%d)\n",
1359 saddr2str(p
->addr
), p
->sock
);
1364 racoon_free(p
->addr
);
1366 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
)
1372 plog(LLV_ERROR
, LOCATION
, NULL
,
1373 "no address could be bound.\n");
1383 struct myaddrs
*p
, *next
;
1385 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
1391 racoon_free(p
->addr
);
1395 lcconf
->myaddrs
= NULL
;
1399 isakmp_send(iph1
, sbuf
)
1400 struct ph1handle
*iph1
;
1406 /* select the socket to be sent */
1407 s
= getsockmyaddr(iph1
->local
);
1411 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
1412 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
1414 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
1421 /* called from scheduler */
1423 isakmp_ph1resend_stub(p
)
1426 (void)isakmp_ph1resend((struct ph1handle
*)p
);
1430 isakmp_ph1resend(iph1
)
1431 struct ph1handle
*iph1
;
1433 if (iph1
->retry_counter
< 0) {
1434 plog(LLV_ERROR
, LOCATION
, NULL
,
1435 "phase1 negotiation failed due to time up. %s\n",
1436 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1443 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0)
1446 plog(LLV_DEBUG
, LOCATION
, NULL
,
1447 "resend phase1 packet %s\n",
1448 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
1450 iph1
->retry_counter
--;
1452 iph1
->scr
= sched_new(iph1
->rmconf
->retry_interval
,
1453 isakmp_ph1resend_stub
, iph1
);
1458 /* called from scheduler */
1460 isakmp_ph2resend_stub(p
)
1464 (void)isakmp_ph2resend((struct ph2handle
*)p
);
1468 isakmp_ph2resend(iph2
)
1469 struct ph2handle
*iph2
;
1471 if (iph2
->retry_counter
< 0) {
1472 plog(LLV_ERROR
, LOCATION
, NULL
,
1473 "phase2 negotiation failed due to time up. %s\n",
1474 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1481 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0)
1484 plog(LLV_DEBUG
, LOCATION
, NULL
,
1485 "resend phase2 packet %s\n",
1486 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
1488 iph2
->retry_counter
--;
1490 iph2
->scr
= sched_new(iph2
->ph1
->rmconf
->retry_interval
,
1491 isakmp_ph2resend_stub
, iph2
);
1496 /* called from scheduler */
1498 isakmp_ph1expire_stub(p
)
1502 isakmp_ph1expire((struct ph1handle
*)p
);
1506 isakmp_ph1expire(iph1
)
1507 struct ph1handle
*iph1
;
1511 src
= strdup(saddr2str(iph1
->local
));
1512 dst
= strdup(saddr2str(iph1
->remote
));
1513 plog(LLV_INFO
, LOCATION
, NULL
,
1514 "ISAKMP-SA expired %s-%s spi:%s\n",
1516 isakmp_pindex(&iph1
->index
, 0));
1520 SCHED_KILL(iph1
->sce
);
1522 iph1
->status
= PHASE1ST_EXPIRED
;
1525 * the phase1 deletion is postponed until there is no phase2.
1527 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
1528 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
1532 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
1535 /* called from scheduler */
1537 isakmp_ph1delete_stub(p
)
1541 isakmp_ph1delete((struct ph1handle
*)p
);
1545 isakmp_ph1delete(iph1
)
1546 struct ph1handle
*iph1
;
1550 SCHED_KILL(iph1
->sce
);
1552 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
1553 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
1557 /* don't re-negosiation when the phase 1 SA expires. */
1559 src
= strdup(saddr2str(iph1
->local
));
1560 dst
= strdup(saddr2str(iph1
->remote
));
1561 plog(LLV_INFO
, LOCATION
, NULL
,
1562 "ISAKMP-SA deleted %s-%s spi:%s\n",
1563 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
1573 /* called from scheduler.
1574 * this function will call only isakmp_ph2delete().
1575 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
1576 * by something cause. That's why this function is called after phase 2 SA
1577 * expires in the userland.
1580 isakmp_ph2expire_stub(p
)
1584 isakmp_ph2expire((struct ph2handle
*)p
);
1588 isakmp_ph2expire(iph2
)
1589 struct ph2handle
*iph2
;
1593 SCHED_KILL(iph2
->sce
);
1595 src
= strdup(saddrwop2str(iph2
->src
));
1596 dst
= strdup(saddrwop2str(iph2
->dst
));
1597 plog(LLV_INFO
, LOCATION
, NULL
,
1598 "phase2 sa expired %s-%s\n", src
, dst
);
1602 iph2
->status
= PHASE2ST_EXPIRED
;
1604 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
1609 /* called from scheduler */
1611 isakmp_ph2delete_stub(p
)
1615 isakmp_ph2delete((struct ph2handle
*)p
);
1619 isakmp_ph2delete(iph2
)
1620 struct ph2handle
*iph2
;
1624 SCHED_KILL(iph2
->sce
);
1626 src
= strdup(saddrwop2str(iph2
->src
));
1627 dst
= strdup(saddrwop2str(iph2
->dst
));
1628 plog(LLV_INFO
, LOCATION
, NULL
,
1629 "phase2 sa deleted %s-%s\n", src
, dst
);
1641 * Interface between PF_KEYv2 and ISAKMP
1644 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
1645 * if phase1 has been finished, begin phase2.
1648 isakmp_post_acquire(iph2
)
1649 struct ph2handle
*iph2
;
1651 struct remoteconf
*rmconf
;
1652 struct ph1handle
*iph1
= NULL
;
1654 /* search appropreate configuration with masking port. */
1655 rmconf
= getrmconf(iph2
->dst
);
1656 if (rmconf
== NULL
) {
1657 plog(LLV_ERROR
, LOCATION
, NULL
,
1658 "no configuration found for %s.\n",
1659 saddrwop2str(iph2
->dst
));
1663 /* if passive mode, ignore the acquire message */
1664 if (rmconf
->passive
) {
1665 plog(LLV_DEBUG
, LOCATION
, NULL
,
1666 "because of passive mode, "
1667 "ignore the acquire message for %s.\n",
1668 saddrwop2str(iph2
->dst
));
1672 /* search isakmp status table by address with masking port */
1673 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
1675 /* no ISAKMP-SA found. */
1679 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
1680 sc
= sched_new(1, isakmp_chkph1there_stub
, iph2
);
1681 plog(LLV_INFO
, LOCATION
, NULL
,
1682 "IPsec-SA request for %s queued "
1683 "due to no phase1 found.\n",
1684 saddrwop2str(iph2
->dst
));
1686 /* start phase 1 negotiation as a initiator. */
1687 if (isakmp_ph1begin_i(rmconf
, iph2
->dst
) < 0) {
1696 /* found ISAKMP-SA, but on negotiation. */
1697 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
1698 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
1699 sched_new(1, isakmp_chkph1there_stub
, iph2
);
1700 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
1701 "request for establishing IPsec-SA was queued "
1702 "due to no phase1 found.\n");
1707 /* found established ISAKMP-SA */
1708 /* i.e. iph1->status == PHASE1ST_ESTABLISHED */
1710 /* found ISAKMP-SA. */
1711 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
1713 /* begin quick mode */
1714 if (isakmp_ph2begin_i(iph1
, iph2
))
1721 * receive GETSPI from kernel.
1724 isakmp_post_getspi(iph2
)
1725 struct ph2handle
*iph2
;
1728 struct timeval start
, end
;
1731 /* don't process it because there is no suitable phase1-sa. */
1732 if (iph2
->ph1
->status
== PHASE2ST_EXPIRED
) {
1733 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1734 "the negotiation is stopped, "
1735 "because there is no suitable ISAKMP-SA.\n");
1740 gettimeofday(&start
, NULL
);
1742 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1744 [iph2
->status
])(iph2
, NULL
) != 0)
1747 gettimeofday(&end
, NULL
);
1748 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1750 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1751 timedelta(&start
, &end
));
1757 /* called by scheduler */
1759 isakmp_chkph1there_stub(p
)
1762 isakmp_chkph1there((struct ph2handle
*)p
);
1766 isakmp_chkph1there(iph2
)
1767 struct ph2handle
*iph2
;
1769 struct ph1handle
*iph1
;
1771 iph2
->retry_checkph1
--;
1772 if (iph2
->retry_checkph1
< 0) {
1773 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
1774 "phase2 negotiation failed "
1775 "due to time up waiting for phase1. %s\n",
1776 sadbsecas2str(iph2
->dst
, iph2
->src
,
1777 iph2
->satype
, 0, 0));
1778 plog(LLV_INFO
, LOCATION
, NULL
,
1779 "delete phase 2 handler.\n");
1781 /* send acquire to kernel as error */
1782 pk_sendeacquire(iph2
);
1791 iph1
= getph1byaddr(iph2
->src
, iph2
->dst
);
1793 /* XXX Even if ph1 as responder is there, should we not start
1794 * phase 2 negotiation ? */
1796 && iph1
->status
== PHASE1ST_ESTABLISHED
) {
1797 /* found isakmp-sa */
1798 /* begin quick mode */
1799 (void)isakmp_ph2begin_i(iph1
, iph2
);
1803 /* no isakmp-sa found */
1804 sched_new(1, isakmp_chkph1there_stub
, iph2
);
1809 /* copy variable data into ALLOCATED buffer. */
1811 isakmp_set_attr_v(buf
, type
, val
, len
)
1817 struct isakmp_data
*data
;
1819 data
= (struct isakmp_data
*)buf
;
1820 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
1821 data
->lorv
= htons((u_int16_t
)len
);
1822 memcpy(data
+ 1, val
, len
);
1824 return buf
+ sizeof(*data
) + len
;
1827 /* copy fixed length data into ALLOCATED buffer. */
1829 isakmp_set_attr_l(buf
, type
, val
)
1834 struct isakmp_data
*data
;
1836 data
= (struct isakmp_data
*)buf
;
1837 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
1838 data
->lorv
= htons((u_int16_t
)val
);
1840 return buf
+ sizeof(*data
);
1843 /* add a variable data attribute to the buffer by reallocating it. */
1845 isakmp_add_attr_v(buf0
, type
, val
, len
)
1851 vchar_t
*buf
= NULL
;
1852 struct isakmp_data
*data
;
1856 tlen
= sizeof(*data
) + len
;
1860 buf
= vrealloc(buf0
, oldlen
+ tlen
);
1862 buf
= vmalloc(tlen
);
1864 plog(LLV_ERROR
, LOCATION
, NULL
,
1865 "failed to get a attribute buffer.\n");
1869 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
1870 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
1871 data
->lorv
= htons((u_int16_t
)len
);
1872 memcpy(data
+ 1, val
, len
);
1877 /* add a fixed data attribute to the buffer by reallocating it. */
1879 isakmp_add_attr_l(buf0
, type
, val
)
1884 vchar_t
*buf
= NULL
;
1885 struct isakmp_data
*data
;
1889 tlen
= sizeof(*data
);
1893 buf
= vrealloc(buf0
, oldlen
+ tlen
);
1895 buf
= vmalloc(tlen
);
1897 plog(LLV_ERROR
, LOCATION
, NULL
,
1898 "failed to get a attribute buffer.\n");
1902 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
1903 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
1904 data
->lorv
= htons((u_int16_t
)val
);
1910 * calculate cookie and set.
1913 isakmp_newcookie(place
, remote
, local
)
1915 struct sockaddr
*remote
;
1916 struct sockaddr
*local
;
1918 vchar_t
*buf
= NULL
, *buf2
= NULL
;
1928 if (remote
->sa_family
!= local
->sa_family
) {
1929 plog(LLV_ERROR
, LOCATION
, NULL
,
1930 "address family mismatch, remote:%d local:%d\n",
1931 remote
->sa_family
, local
->sa_family
);
1934 switch (remote
->sa_family
) {
1936 alen
= sizeof(struct in_addr
);
1937 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
1938 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
1942 alen
= sizeof(struct in_addr
);
1943 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
1944 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
1948 plog(LLV_ERROR
, LOCATION
, NULL
,
1949 "invalid family: %d\n", remote
->sa_family
);
1952 blen
= (alen
+ sizeof(u_short
)) * 2
1953 + sizeof(time_t) + lcconf
->secret_size
;
1954 buf
= vmalloc(blen
);
1956 plog(LLV_ERROR
, LOCATION
, NULL
,
1957 "failed to get a cookie.\n");
1962 /* copy my address */
1963 memcpy(p
, sa1
, alen
);
1965 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
1966 memcpy(p
, &port
, sizeof(u_short
));
1967 p
+= sizeof(u_short
);
1969 /* copy target address */
1970 memcpy(p
, sa2
, alen
);
1972 port
= ((struct sockaddr_in
*)local
)->sin_port
;
1973 memcpy(p
, &port
, sizeof(u_short
));
1974 p
+= sizeof(u_short
);
1978 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
1981 /* copy random value */
1982 buf2
= eay_set_random(lcconf
->secret_size
);
1985 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
1986 p
+= lcconf
->secret_size
;
1989 buf2
= eay_sha1_one(buf
);
1990 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
1992 sa1
= val2str(place
, sizeof (cookie_t
));
1993 plog(LLV_DEBUG
, LOCATION
, NULL
, "new cookie:\n%s\n", sa1
);
2006 * save partner's(payload) data into phhandle.
2009 isakmp_p2ph(buf
, gen
)
2011 struct isakmp_gen
*gen
;
2013 /* XXX to be checked in each functions for logging. */
2015 plog(LLV_WARNING
, LOCATION
, NULL
,
2016 "ignore this payload, same payload type exist.\n");
2020 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
2022 plog(LLV_ERROR
, LOCATION
, NULL
,
2023 "failed to get buffer.\n");
2026 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
2032 isakmp_newmsgid2(iph1
)
2033 struct ph1handle
*iph1
;
2039 } while (getph2bymsgid(iph1
, msgid2
));
2045 * set values into allocated buffer of isakmp header for phase 1
2048 set_isakmp_header(vbuf
, iph1
, nptype
)
2050 struct ph1handle
*iph1
;
2053 struct isakmp
*isakmp
;
2055 if (vbuf
->l
< sizeof(*isakmp
))
2058 isakmp
= (struct isakmp
*)vbuf
->v
;
2059 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
2060 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
2061 isakmp
->np
= nptype
;
2062 isakmp
->v
= iph1
->version
;
2063 isakmp
->etype
= iph1
->etype
;
2064 isakmp
->flags
= iph1
->flags
;
2065 isakmp
->msgid
= iph1
->msgid
;
2066 isakmp
->len
= htonl(vbuf
->l
);
2068 return vbuf
->v
+ sizeof(*isakmp
);
2072 * set values into allocated buffer of isakmp header for phase 2
2075 set_isakmp_header2(vbuf
, iph2
, nptype
)
2077 struct ph2handle
*iph2
;
2080 struct isakmp
*isakmp
;
2082 if (vbuf
->l
< sizeof(*isakmp
))
2085 isakmp
= (struct isakmp
*)vbuf
->v
;
2086 memcpy(&isakmp
->i_ck
, &iph2
->ph1
->index
.i_ck
, sizeof(cookie_t
));
2087 memcpy(&isakmp
->r_ck
, &iph2
->ph1
->index
.r_ck
, sizeof(cookie_t
));
2088 isakmp
->np
= nptype
;
2089 isakmp
->v
= iph2
->ph1
->version
;
2090 isakmp
->etype
= ISAKMP_ETYPE_QUICK
;
2091 isakmp
->flags
= iph2
->flags
;
2092 memcpy(&isakmp
->msgid
, &iph2
->msgid
, sizeof(isakmp
->msgid
));
2093 isakmp
->len
= htonl(vbuf
->l
);
2095 return vbuf
->v
+ sizeof(*isakmp
);
2099 * set values into allocated buffer of isakmp payload.
2102 set_isakmp_payload(buf
, src
, nptype
)
2107 struct isakmp_gen
*gen
;
2110 plog(LLV_DEBUG
, LOCATION
, NULL
, "add payload of len %d, next type %d\n",
2113 gen
= (struct isakmp_gen
*)p
;
2115 gen
->len
= htons(sizeof(*gen
) + src
->l
);
2117 memcpy(p
, src
->v
, src
->l
);
2128 case ISAKMP_ETYPE_IDENT
:
2130 case ISAKMP_ETYPE_AGG
:
2132 case ISAKMP_ETYPE_BASE
:
2145 case ISAKMP_ETYPE_QUICK
:
2153 #ifdef HAVE_PRINT_ISAKMP_C
2154 /* for print-isakmp.c */
2156 extern void isakmp_print
__P((const u_char
*, u_int
, const u_char
*));
2158 char *getname
__P((const u_char
*));
2160 char *getname6
__P((const u_char
*));
2162 int safeputchar
__P((int));
2165 * Return a name for the IP address pointed to by ap. This address
2166 * is assumed to be in network byte order.
2172 struct sockaddr_in addr
;
2173 static char ntop_buf
[NI_MAXHOST
];
2175 memset(&addr
, 0, sizeof(addr
));
2176 addr
.sin_len
= sizeof(struct sockaddr_in
);
2177 addr
.sin_family
= AF_INET
;
2178 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
2179 if (getnameinfo((struct sockaddr
*)&addr
, addr
.sin_len
,
2180 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2181 NI_NUMERICHOST
| niflags
))
2182 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2189 * Return a name for the IP6 address pointed to by ap. This address
2190 * is assumed to be in network byte order.
2196 struct sockaddr_in6 addr
;
2197 static char ntop_buf
[NI_MAXHOST
];
2199 memset(&addr
, 0, sizeof(addr
));
2200 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
2201 addr
.sin6_family
= AF_INET6
;
2202 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
2203 if (getnameinfo((struct sockaddr
*)&addr
, addr
.sin6_len
,
2204 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
2205 NI_NUMERICHOST
| niflags
))
2206 strncpy(ntop_buf
, "?", sizeof(ntop_buf
));
2218 ch
= (unsigned char)(c
& 0xff);
2219 if (c
< 0x80 && isprint(c
))
2220 return printf("%c", c
& 0xff);
2222 return printf("\\%03o", c
& 0xff);
2226 isakmp_printpacket(msg
, from
, my
, decoded
)
2228 struct sockaddr
*from
;
2229 struct sockaddr
*my
;
2235 char hostbuf
[NI_MAXHOST
];
2236 char portbuf
[NI_MAXSERV
];
2237 struct isakmp
*isakmp
;
2241 if (loglevel
< LLV_DEBUG
)
2245 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
2247 gettimeofday(&tv
, NULL
);
2248 s
= tv
.tv_sec
% 3600;
2249 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
2252 if (getnameinfo(from
, from
->sa_len
, hostbuf
, sizeof(hostbuf
),
2253 portbuf
, sizeof(portbuf
),
2254 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2255 strncpy(hostbuf
, "?", sizeof(hostbuf
));
2256 strncpy(portbuf
, "?", sizeof(portbuf
));
2258 printf("%s:%s", hostbuf
, portbuf
);
2263 if (getnameinfo(my
, my
->sa_len
, hostbuf
, sizeof(hostbuf
),
2264 portbuf
, sizeof(portbuf
),
2265 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
2266 strncpy(hostbuf
, "?", sizeof(hostbuf
));
2267 strncpy(portbuf
, "?", sizeof(portbuf
));
2269 printf("%s:%s", hostbuf
, portbuf
);
2276 printf("(malloc fail)\n");
2280 isakmp
= (struct isakmp
*)buf
->v
;
2281 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
2284 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
2285 if (buf
->l
< pad
&& 2 < vflag
)
2286 printf("(wrong padding)");
2288 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
2292 snapend
= buf
->v
+ buf
->l
;
2293 isakmp_print(buf
->v
, buf
->l
, NULL
);
2301 #endif /*HAVE_PRINT_ISAKMP_C*/
2304 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
2305 struct ph1handle
*iph1
;
2306 struct remoteconf
*rmconf
;
2307 struct sockaddr
*remote
, *local
;
2309 u_short
*port
= NULL
;
2311 /* address portion must be grabbed from real remote address "remote" */
2312 iph1
->remote
= dupsaddr(remote
);
2313 if (iph1
->remote
== NULL
) {
2319 * if remote has no port # (in case of initiator - from ACQUIRE msg)
2320 * - if remote.conf specifies port #, use that
2321 * - if remote.conf does not, use 500
2322 * if remote has port # (in case of responder - from recvfrom(2))
2323 * respect content of "remote".
2325 switch (iph1
->remote
->sa_family
) {
2327 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
2330 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
2333 *port
= htons(PORT_ISAKMP
);
2337 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
2340 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
2343 *port
= htons(PORT_ISAKMP
);
2347 plog(LLV_ERROR
, LOCATION
, NULL
,
2348 "invalid family: %d\n", iph1
->remote
->sa_family
);
2353 iph1
->local
= getlocaladdr(iph1
->remote
);
2355 iph1
->local
= dupsaddr(local
);
2356 if (iph1
->local
== NULL
) {
2360 switch (iph1
->local
->sa_family
) {
2362 ((struct sockaddr_in
*)iph1
->local
)->sin_port
2363 = getmyaddrsport(iph1
->local
);
2367 ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
2368 = getmyaddrsport(iph1
->local
);
2372 plog(LLV_ERROR
, LOCATION
, NULL
,
2373 "invalid family: %d\n", iph1
->remote
->sa_family
);
2383 struct ph1handle
*iph1
;
2386 plog(LLV_ERROR
, LOCATION
, iph1
->remote
, "wrong state %u.\n",
2393 struct ph2handle
*iph2
;
2396 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
, "wrong state %u.\n",
2402 log_ph1established(iph1
)
2403 const struct ph1handle
*iph1
;
2407 src
= strdup(saddr2str(iph1
->local
));
2408 dst
= strdup(saddr2str(iph1
->remote
));
2409 plog(LLV_INFO
, LOCATION
, NULL
,
2410 "ISAKMP-SA established %s-%s spi:%s\n",
2412 isakmp_pindex(&iph1
->index
, 0));